… | |
… | |
20 | return first; |
20 | return first; |
21 | } |
21 | } |
22 | |
22 | |
23 | #include <new> |
23 | #include <new> |
24 | |
24 | |
25 | #if __cplusplus >= 201103L |
25 | #if ECB_CPP11 |
26 | #include <type_traits> |
26 | #include <type_traits> |
27 | #endif |
27 | #endif |
28 | |
28 | |
29 | // original version taken from MICO, but this has been completely rewritten |
29 | namespace estl |
30 | // known limitations w.r.t. std::vector |
|
|
31 | // - many methods missing |
|
|
32 | // - no error checking, no exceptions thrown |
|
|
33 | // - size_type is 32bit even on 64 bit hosts, so limited to 2**31 elements |
|
|
34 | // - no allocator support |
|
|
35 | // - we don't really care about const correctness, but we try |
|
|
36 | // - we don't care about namespaces and stupid macros the user might define |
|
|
37 | template<class T> |
|
|
38 | struct simplevec |
|
|
39 | { |
30 | { |
40 | #if ESTL_BIG_VECTOR |
31 | #if ESTL_LARGE_MEMORY_MODEL |
41 | // shoudl use size_t/ssize_t, but that's not portable enough for us |
32 | // should use size_t/ssize_t, but that's not portable enough for us |
42 | typedef unsigned long size_type; |
33 | typedef unsigned long size_type; |
43 | typedef long difference_type; |
34 | typedef long difference_type; |
44 | #else |
35 | #else |
45 | typedef uint32_t size_type; |
36 | typedef uint32_t size_type; |
46 | typedef int32_t difference_type; |
37 | typedef int32_t difference_type; |
47 | #endif |
38 | #endif |
|
|
39 | |
|
|
40 | template<typename T> |
|
|
41 | struct scoped_ptr |
|
|
42 | { |
|
|
43 | T *p; |
|
|
44 | |
|
|
45 | scoped_ptr () : p (0) { } |
|
|
46 | |
|
|
47 | explicit |
|
|
48 | scoped_ptr (T *a) : p (a) { } |
|
|
49 | |
|
|
50 | ~scoped_ptr () |
|
|
51 | { |
|
|
52 | delete p; |
|
|
53 | } |
|
|
54 | |
|
|
55 | void reset (T *a) |
|
|
56 | { |
|
|
57 | delete p; |
|
|
58 | p = a; |
|
|
59 | } |
|
|
60 | |
|
|
61 | T *operator ->() const { return p; } |
|
|
62 | T &operator *() const { return *p; } |
|
|
63 | |
|
|
64 | operator T *() { return p; } |
|
|
65 | T *get () const { return p; } |
|
|
66 | }; |
|
|
67 | |
|
|
68 | template<typename T> |
|
|
69 | struct scoped_array |
|
|
70 | { |
|
|
71 | T *p; |
|
|
72 | |
|
|
73 | scoped_array () : p (0) { } |
|
|
74 | |
|
|
75 | explicit |
|
|
76 | scoped_array (T *a) : p (a) { } |
|
|
77 | |
|
|
78 | ~scoped_array () |
|
|
79 | { |
|
|
80 | delete [] p; |
|
|
81 | } |
|
|
82 | |
|
|
83 | void reset (T *a) |
|
|
84 | { |
|
|
85 | delete [] p; |
|
|
86 | p = a; |
|
|
87 | } |
|
|
88 | |
|
|
89 | T & operator [](size_type idx) const { return p[idx]; } |
|
|
90 | |
|
|
91 | operator T *() { return p; } |
|
|
92 | T *get () const { return p; } |
|
|
93 | }; |
|
|
94 | } |
|
|
95 | |
|
|
96 | // original version taken from MICO, but this has been completely rewritten |
|
|
97 | // known limitations w.r.t. std::vector |
|
|
98 | // - many methods missing |
|
|
99 | // - no error checking, no exceptions thrown (e.g. at()) |
|
|
100 | // - size_type is 32bit even on 64 bit hosts, so limited to 2**31 elements |
|
|
101 | // - no allocator support |
|
|
102 | // - we don't really care about const correctness, but we try |
|
|
103 | // - we don't care about namespaces and stupid macros the user might define |
|
|
104 | // - no bool specialisation |
|
|
105 | template<class T> |
|
|
106 | struct simplevec |
|
|
107 | { |
|
|
108 | typedef estl::size_type size_type; |
48 | |
109 | |
49 | typedef T value_type; |
110 | typedef T value_type; |
50 | typedef T *iterator; |
111 | typedef T *iterator; |
51 | typedef const T *const_iterator; |
112 | typedef const T *const_iterator; |
52 | typedef T *pointer; |
113 | typedef T *pointer; |
… | |
… | |
62 | |
123 | |
63 | // we shamelessly optimise for "simple" types. everything |
124 | // we shamelessly optimise for "simple" types. everything |
64 | // "not simple enough" will use the slow path. |
125 | // "not simple enough" will use the slow path. |
65 | static bool is_simple_enough () |
126 | static bool is_simple_enough () |
66 | { |
127 | { |
67 | return 1; // we are not there yet |
128 | #if ECB_CPP11 |
68 | #if __cplusplus >= 201103L |
|
|
69 | return std::is_trivially_assignable<T, T>::value |
129 | return std::is_trivially_assignable<T, T>::value |
70 | && std::is_trivially_constructable<T>::value |
130 | && std::is_trivially_constructible<T>::value |
71 | && std::is_trivially_copyable<T>::value |
131 | && std::is_trivially_copyable<T>::value |
72 | && std::is_trivially_destructible<T>::value; |
132 | && std::is_trivially_destructible<T>::value; |
73 | #elif ECB_GCC_VERSION(4,4) |
133 | #elif ECB_GCC_VERSION(4,4) || ECB_CLANG_VERSION(2,8) |
74 | return __has_trivial_assign (T) |
134 | return __has_trivial_assign (T) |
75 | && __has_trivial_constructor (T) |
135 | && __has_trivial_constructor (T) |
76 | && __has_trivial_copy (T) |
136 | && __has_trivial_copy (T) |
77 | && __has_trivial_destructor (T); |
137 | && __has_trivial_destructor (T); |
78 | #else |
138 | #else |
… | |
… | |
82 | |
142 | |
83 | static void construct (iterator a, size_type n = 1) |
143 | static void construct (iterator a, size_type n = 1) |
84 | { |
144 | { |
85 | if (!is_simple_enough ()) |
145 | if (!is_simple_enough ()) |
86 | while (n--) |
146 | while (n--) |
87 | new (*a++) T (); |
147 | new (a++) T (); |
88 | } |
148 | } |
89 | |
149 | |
90 | static void destruct (iterator a, size_type n = 1) |
150 | static void destruct (iterator a, size_type n = 1) |
91 | { |
151 | { |
92 | if (!is_simple_enough ()) |
152 | if (!is_simple_enough ()) |
93 | while (n--) |
153 | while (n--) |
94 | (*a++).~T (); |
154 | (*a++).~T (); |
95 | } |
155 | } |
96 | |
156 | |
|
|
157 | template<class I> |
97 | static void cop_new (iterator a, iterator b) { new (a) T (*b); } |
158 | static void cop_new (iterator a, I b) { new (a) T (*b); } |
|
|
159 | template<class I> |
98 | static void cop_set (iterator a, iterator b) { *a = *b ; } |
160 | static void cop_set (iterator a, I b) { *a = *b ; } |
99 | |
161 | |
100 | // these copy helpers actually use the copy constructor, not assignment |
162 | // MUST copy forwards |
|
|
163 | template<class I> |
101 | static void copy_lower (iterator dst, iterator src, size_type n, void (*op)(iterator, iterator) = cop_new) |
164 | static void copy (iterator dst, I src, size_type n, void (*op)(iterator, I)) |
102 | { |
165 | { |
103 | if (is_simple_enough ()) |
|
|
104 | memmove (dst, src, sizeof (T) * n); |
|
|
105 | else |
|
|
106 | while (n--) |
166 | while (n--) |
107 | op (dst++, src++); |
167 | op (dst++, src++); |
108 | } |
168 | } |
109 | |
169 | |
110 | static void copy_higher (iterator dst, iterator src, size_type n, void (*op)(iterator, iterator) = cop_new) |
|
|
111 | { |
|
|
112 | if (is_simple_enough ()) |
|
|
113 | memmove (dst, src, sizeof (T) * n); |
|
|
114 | else |
|
|
115 | while (n--) |
|
|
116 | op (dst + n, src + n); |
|
|
117 | } |
|
|
118 | |
|
|
119 | static void copy (iterator dst, iterator src, size_type n, void (*op)(iterator, iterator) = cop_new) |
170 | static void copy (iterator dst, iterator src, size_type n, void (*op)(iterator, iterator)) |
120 | { |
171 | { |
121 | if (is_simple_enough ()) |
172 | if (is_simple_enough ()) |
122 | memcpy (dst, src, sizeof (T) * n); |
173 | memcpy (dst, src, sizeof (T) * n); |
123 | else |
174 | else |
124 | copy_lower (dst, src, n, op); |
175 | copy<iterator> (dst, src, n, op); |
125 | } |
176 | } |
126 | |
177 | |
127 | static T *alloc (size_type n) ecb_cold |
178 | static T *alloc (size_type n) ecb_cold |
128 | { |
179 | { |
129 | return (T *)::operator new ((size_t) (sizeof (T) * n)); |
180 | return (T *)::operator new ((size_t) (sizeof (T) * n)); |
… | |
… | |
153 | dealloc (); |
204 | dealloc (); |
154 | buf = nbuf; |
205 | buf = nbuf; |
155 | } |
206 | } |
156 | |
207 | |
157 | construct (buf + sze, n); |
208 | construct (buf + sze, n); |
158 | copy_higher (buf + pos + n, buf + pos, sze - pos, cop_set); |
209 | |
|
|
210 | iterator src = buf + pos; |
|
|
211 | if (is_simple_enough ()) |
|
|
212 | memmove (src + n, src, sizeof (T) * (sze - pos)); |
|
|
213 | else |
|
|
214 | for (size_type i = sze - pos; i--; ) |
|
|
215 | cop_set (src + n + i, src + i); |
|
|
216 | |
159 | sze += n; |
217 | sze += n; |
160 | } |
218 | } |
161 | |
219 | |
162 | public: |
220 | public: |
163 | size_type capacity () const { return res; } |
221 | size_type capacity () const { return res; } |
164 | size_type size () const { return sze; } |
222 | size_type size () const { return sze; } |
165 | bool empty () const { return size () == 0; } |
223 | bool empty () const { return size () == 0; } |
|
|
224 | |
|
|
225 | size_t max_size () const |
|
|
226 | { |
|
|
227 | return (~(size_type)0) >> 1; |
|
|
228 | } |
166 | |
229 | |
167 | const_iterator begin () const { return &buf [ 0]; } |
230 | const_iterator begin () const { return &buf [ 0]; } |
168 | iterator begin () { return &buf [ 0]; } |
231 | iterator begin () { return &buf [ 0]; } |
169 | const_iterator end () const { return &buf [sze ]; } |
232 | const_iterator end () const { return &buf [sze ]; } |
170 | iterator end () { return &buf [sze ]; } |
233 | iterator end () { return &buf [sze ]; } |
… | |
… | |
179 | return; |
242 | return; |
180 | |
243 | |
181 | sz = good_size (sz); |
244 | sz = good_size (sz); |
182 | T *nbuf = alloc (sz); |
245 | T *nbuf = alloc (sz); |
183 | |
246 | |
184 | copy (nbuf, begin (), sze); |
247 | copy (nbuf, begin (), sze, cop_new); |
185 | dealloc (); |
248 | dealloc (); |
186 | |
249 | |
187 | buf = nbuf; |
250 | buf = nbuf; |
188 | res = sz; |
251 | res = sz; |
189 | } |
252 | } |
… | |
… | |
205 | : sze(0), res(0), buf(0) |
268 | : sze(0), res(0), buf(0) |
206 | { |
269 | { |
207 | } |
270 | } |
208 | |
271 | |
209 | simplevec (size_type n, const T &t = T ()) |
272 | simplevec (size_type n, const T &t = T ()) |
210 | : sze(0), res(0), buf(0) |
|
|
211 | { |
273 | { |
212 | insert (begin (), n, t); |
274 | sze = res = n; |
|
|
275 | buf = alloc (sze); |
|
|
276 | |
|
|
277 | while (n--) |
|
|
278 | new (buf + n) T (t); |
213 | } |
279 | } |
214 | |
280 | |
215 | simplevec (const_iterator first, const_iterator last) |
281 | simplevec (const_iterator first, const_iterator last) |
216 | : sze(0), res(0), buf(0) |
|
|
217 | { |
282 | { |
218 | insert (begin (), first, last); |
283 | sze = res = last - first; |
|
|
284 | buf = alloc (sze); |
|
|
285 | copy (buf, first, sze, cop_new); |
219 | } |
286 | } |
220 | |
287 | |
221 | simplevec (const simplevec<T> &v) |
288 | simplevec (const simplevec<T> &v) |
222 | : sze(0), res(0), buf(0) |
289 | : sze(0), res(0), buf(0) |
223 | { |
290 | { |
224 | insert (begin (), v.begin (), v.end ()); |
291 | sze = res = v.size (); |
225 | } |
292 | buf = alloc (sze); |
226 | |
293 | copy (buf, v.begin (), sze, cop_new); |
227 | simplevec<T> &operator= (const simplevec<T> &v) |
|
|
228 | { |
|
|
229 | swap (simplevec<T> (v)); |
|
|
230 | return *this; |
|
|
231 | } |
294 | } |
232 | |
295 | |
233 | ~simplevec () |
296 | ~simplevec () |
234 | { |
297 | { |
235 | dealloc (); |
298 | dealloc (); |
… | |
… | |
257 | void pop_back () |
320 | void pop_back () |
258 | { |
321 | { |
259 | destruct (buf + --sze); |
322 | destruct (buf + --sze); |
260 | } |
323 | } |
261 | |
324 | |
262 | const T &operator [](size_type idx) const { return buf[idx]; } |
325 | const_reference operator [](size_type idx) const { return buf[idx]; } |
263 | T &operator [](size_type idx) { return buf[idx]; } |
326 | reference operator [](size_type idx) { return buf[idx]; } |
|
|
327 | |
|
|
328 | const_reference at (size_type idx) const { return buf [idx]; } |
|
|
329 | reference at (size_type idx) { return buf [idx]; } |
|
|
330 | |
|
|
331 | void assign (const_iterator first, const_iterator last) |
|
|
332 | { |
|
|
333 | simplevec<T> v (first, last); |
|
|
334 | swap (v); |
|
|
335 | } |
|
|
336 | |
|
|
337 | void assign (size_type n, const T &t) |
|
|
338 | { |
|
|
339 | simplevec<T> v (n, t); |
|
|
340 | swap (v); |
|
|
341 | } |
|
|
342 | |
|
|
343 | simplevec<T> &operator= (const simplevec<T> &v) |
|
|
344 | { |
|
|
345 | assign (v.begin (), v.end ()); |
|
|
346 | return *this; |
|
|
347 | } |
264 | |
348 | |
265 | iterator insert (iterator pos, const T &t) |
349 | iterator insert (iterator pos, const T &t) |
266 | { |
350 | { |
267 | size_type at = pos - begin (); |
351 | size_type at = pos - begin (); |
|
|
352 | |
268 | ins (pos, 1); |
353 | ins (pos, 1); |
269 | buf [pos] = t; |
354 | buf [at] = t; |
270 | return pos; |
355 | |
|
|
356 | return buf + at; |
271 | } |
357 | } |
272 | |
358 | |
273 | iterator insert (iterator pos, const_iterator first, const_iterator last) |
359 | iterator insert (iterator pos, const_iterator first, const_iterator last) |
274 | { |
360 | { |
275 | size_type n = last - first; |
361 | size_type n = last - first; |
276 | size_type at = pos - begin (); |
362 | size_type at = pos - begin (); |
277 | |
363 | |
278 | ins (pos, n); |
364 | ins (pos, n); |
279 | copy (pos, first, n, cop_set); |
365 | copy (buf + at, first, n, cop_set); |
280 | |
366 | |
281 | return pos; |
367 | return buf + at; |
282 | } |
368 | } |
283 | |
369 | |
284 | iterator insert (iterator pos, size_type n, const T &t) |
370 | iterator insert (iterator pos, size_type n, const T &t) |
285 | { |
371 | { |
286 | size_type at = pos - begin (); |
372 | size_type at = pos - begin (); |
287 | |
373 | |
288 | ins (pos, n); |
374 | ins (pos, n); |
289 | |
375 | |
290 | for (size_type i = 0; i < n; ++i) |
376 | for (iterator i = buf + at; n--; ) |
291 | buf [at + i] = t; |
377 | *i++ = t; |
292 | |
378 | |
293 | return pos; |
379 | return buf + at; |
294 | } |
380 | } |
295 | |
381 | |
296 | void erase (iterator first, iterator last) |
382 | iterator erase (iterator first, iterator last) |
297 | { |
383 | { |
298 | size_t n = last - first; |
384 | size_type n = last - first; |
|
|
385 | size_type c = end () - last; |
299 | |
386 | |
300 | copy_lower (last, first, end () - last, cop_set); |
387 | if (is_simple_enough ()) |
|
|
388 | memmove (first, last, sizeof (T) * c); |
|
|
389 | else |
|
|
390 | copy (first, last, c, cop_set); |
|
|
391 | |
301 | sze -= n; |
392 | sze -= n; |
302 | destruct (buf + sze, n); |
393 | destruct (buf + sze, n); |
303 | } |
|
|
304 | |
394 | |
|
|
395 | return first; |
|
|
396 | } |
|
|
397 | |
305 | void erase (iterator pos) |
398 | iterator erase (iterator pos) |
306 | { |
399 | { |
307 | if (pos != end ()) |
400 | if (pos != end ()) |
308 | erase (pos, pos + 1); |
401 | erase (pos, pos + 1); |
|
|
402 | |
|
|
403 | return pos; |
309 | } |
404 | } |
310 | }; |
405 | }; |
311 | |
406 | |
312 | template<class T> |
407 | template<class T> |
313 | bool operator ==(const simplevec<T> &v1, const simplevec<T> &v2) |
408 | bool operator ==(const simplevec<T> &v1, const simplevec<T> &v2) |