-
Notifications
You must be signed in to change notification settings - Fork 1
/
SmartVectorUnion.cpp
284 lines (238 loc) · 6.1 KB
/
SmartVectorUnion.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
#include<vector>
#include<variant>
#include<array>
#include<cstdint>
#include<iostream>
#include<cassert>
// a mimimal smart vector that can be either an array or a vector
template<typename T>
class SmartVector {
public :
using Vector = std::vector<T>;
static constexpr uint32_t maxSize = sizeof(Vector)/sizeof(T)-1;
using Array = std::array<T,sizeof(Vector)/sizeof(T)>;
union Variant {
// all nop
Variant(){}
~Variant(){}
Variant(const Variant &){}
Variant & operator=(const Variant &){ return *this;}
Variant(Variant&&){}
Variant & operator=(Variant &&){ return *this;}
Array a;
Vector v;
};
SmartVector(){
m_container.a.back()=0;
}
~SmartVector(){
if(!m_isArray)
m_container.v.~Vector();
}
SmartVector(const SmartVector& sm) : m_isArray(sm.m_isArray) {
if(m_isArray) {
m_container.a=sm.m_container.a;
} else {
std::fill(m_container.a.begin(), m_container.a.end(),0);
m_container.v=sm.m_container.v;
}
}
SmartVector& operator=(const SmartVector& sm) {
if(!m_isArray) m_container.v.~Vector();
if(sm.m_isArray) {
m_container.a=sm.m_container.a;
} else {
std::fill(m_container.a.begin(), m_container.a.end(),0);
m_container.v=sm.m_container.v;
}
m_isArray = sm.m_isArray;
return *this;
}
SmartVector(SmartVector&& sm) : m_isArray(sm.m_isArray) {
if(m_isArray) {
m_container.a=std::move(sm.m_container.a);
} else {
std::fill(m_container.a.begin(), m_container.a.end(),0);
m_container.v=std::move(sm.m_container.v);
}
}
SmartVector& operator=(SmartVector&& sm) {
if(!m_isArray) m_container.v.~Vector();
if(sm.m_isArray) {
m_container.a=std::move(sm.m_container.a);
} else {
if(m_isArray) std::fill(m_container.a.begin(), m_container.a.end(),0);
m_container.v=std::move(sm.m_container.v);
}
m_isArray = sm.m_isArray;
return *this;
}
template<typename Iter>
SmartVector(Iter b, Iter e) {
initialize(b,e);
}
void initialize(uint32_t size) {
if(!m_isArray) m_container.v.~Vector();
if (size<=maxSize) {
m_isArray=true;
m_container.a.back()=size;
} else {
m_isArray=false;
std::fill(m_container.a.begin(), m_container.a.end(),0);
m_container.v.resize(size);
}
}
template<typename Iter>
void initialize(Iter b, Iter e) {
if(!m_isArray) m_container.v.~Vector();
if (e-b<=maxSize) {
m_isArray=true;
auto & a = m_container.a;
std::copy(b,e,a.begin());
a.back()=e-b;
} else {
m_isArray=false;
std::fill(m_container.a.begin(), m_container.a.end(),0);
m_container.v.insert(m_container.v.end(),b,e);
}
}
template<typename Iter>
void extend(Iter b, Iter e) {
if(m_isArray) {
auto & a = m_container.a;
auto cs = a.back();
uint32_t ns = (e-b)+cs;
if (ns<=maxSize) {
std::copy(b,e,&a[cs]);
a.back()=ns;
} else {
Vector v; v.reserve(ns);
v.insert(v.end(),m_container.a.begin(),m_container.a.begin()+cs);
v.insert(v.end(),b,e);
std::fill(m_container.a.begin(), m_container.a.end(),0);
m_container.v = std::move(v);
m_isArray=false;
}
}else {
m_container.v.insert(m_container.v.end(),b,e);
}
}
T const * data() const {
if(m_isArray)
return m_container.a.data();
else
return m_container.v.data();
}
T * data() {
if(m_isArray)
return m_container.a.data();
else
return m_container.v.data();
}
T const * begin() const {
return data();
}
T const * end() const {
if(m_isArray)
return m_container.a.data() + m_container.a.back();
else
return m_container.v.data() + m_container.v.size();
}
T const & operator[](uint32_t i) const {
return *(begin()+i);
}
uint32_t size() const {
if(m_isArray)
return m_container.a.back();
else
return m_container.v.size();
}
bool isArray() const { return m_isArray;}
private:
Variant m_container;
bool m_isArray = true;
};
template<typename T>
int go() {
using Vector = std::vector<T>;
using Array = std::array<T,sizeof(Vector)/sizeof(T)>;
using Variant = SmartVector<T>;
std::cout << sizeof(Vector) <<' '<< sizeof(Array) <<' '<< sizeof(Variant) << std::endl;
{
Variant v;
assert(v.isArray());
assert(0==v.size());
}
T data[128];
for (int i=0; i<128; ++i) data[i]=i;
T i=0;
Variant va(data,data+5);
assert(5==va.size());
assert(5==va.end()-va.begin());
assert(va.isArray());
i=0; for (auto c : va) assert(c==i++);
auto vc = va;
assert(5==vc.size());
assert(5==vc.end()-vc.begin());
assert(vc.isArray());
i=0; for (auto c : vc) assert(c==i++);
{
Variant vd;
vd.initialize(5);
for (int i=0; i<5; ++i)
vd.data()[i] = i;
assert(5==vd.size());
assert(5==vd.end()-vd.begin());
assert(vd.isArray());
i=0; for (auto c : vd) assert(c==i++);
vd.initialize(24);
for (int i=0; i<24; ++i)
vd.data()[i] = i;
assert(24==vd.size());
assert(24==vd.end()-vd.begin());
assert(!vd.isArray());
i=0; for (auto c : vd) assert(c==i++);
}
{
Variant vb; vb.initialize(data,data+24);
assert(24==vb.size());
assert(24==vb.end()-vb.begin());
assert(!vb.isArray());
i=0; for (auto c : vb) assert(c==i++);
}
Variant vv; vv.extend(data,data+64);
assert(64==vv.size());
assert(64==vv.end()-vv.begin());
assert(!vv.isArray());
i=0; for (auto c : vv) assert(c==i++);
va.extend(data+5,data+10);
assert(10==va.size());
assert(10==va.end()-va.begin());
if constexpr (sizeof(T)<4)
assert(va.isArray());
else
assert(!va.isArray());
i=0; for (auto c : va) assert(c==i++);
va.extend(data+10,data+64);
assert(64==va.size());
assert(64==va.end()-va.begin());
assert(!va.isArray());
i=0; for (auto c : va) assert(c==i++);
vv.extend(data+64,data+72);
assert(72==vv.size());
assert(72==vv.end()-vv.begin());
assert(!vv.isArray());
i=0; for (auto c : vv) assert(c==i++);
vc = vv;
assert(72==vc.size());
assert(72==vc.end()-vc.begin());
assert(!vc.isArray());
i=0; for (auto c : vc) assert(c==i++);
return 0;
}
int main() {
go<uint8_t>();
go<uint16_t>();
go<int32_t>();
return 0;
}