… | |
… | |
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 | private: |
|
|
68 | scoped_ptr (const scoped_ptr &); |
|
|
69 | scoped_ptr &operator =(const scoped_ptr &); |
|
|
70 | }; |
|
|
71 | |
|
|
72 | template<typename T> |
|
|
73 | struct scoped_array |
|
|
74 | { |
|
|
75 | T *p; |
|
|
76 | |
|
|
77 | scoped_array () : p (0) { } |
|
|
78 | |
|
|
79 | explicit |
|
|
80 | scoped_array (T *a) : p (a) { } |
|
|
81 | |
|
|
82 | ~scoped_array () |
|
|
83 | { |
|
|
84 | delete [] p; |
|
|
85 | } |
|
|
86 | |
|
|
87 | void reset (T *a) |
|
|
88 | { |
|
|
89 | delete [] p; |
|
|
90 | p = a; |
|
|
91 | } |
|
|
92 | |
|
|
93 | operator T *() { return p; } |
|
|
94 | T *get () const { return p; } |
|
|
95 | |
|
|
96 | private: |
|
|
97 | scoped_array (const scoped_array &); |
|
|
98 | scoped_array &operator =(const scoped_array &); |
|
|
99 | }; |
|
|
100 | } |
|
|
101 | |
|
|
102 | // original version taken from MICO, but this has been completely rewritten |
|
|
103 | // known limitations w.r.t. std::vector |
|
|
104 | // - many methods missing |
|
|
105 | // - no error checking, no exceptions thrown (e.g. at()) |
|
|
106 | // - size_type is 32bit even on 64 bit hosts, so limited to 2**31 elements |
|
|
107 | // - no allocator support |
|
|
108 | // - we don't really care about const correctness, but we try |
|
|
109 | // - we don't care about namespaces and stupid macros the user might define |
|
|
110 | // - no bool specialisation |
|
|
111 | template<class T> |
|
|
112 | struct simplevec |
|
|
113 | { |
|
|
114 | typedef estl::size_type size_type; |
48 | |
115 | |
49 | typedef T value_type; |
116 | typedef T value_type; |
50 | typedef T *iterator; |
117 | typedef T *iterator; |
51 | typedef const T *const_iterator; |
118 | typedef const T *const_iterator; |
52 | typedef T *pointer; |
119 | typedef T *pointer; |
… | |
… | |
62 | |
129 | |
63 | // we shamelessly optimise for "simple" types. everything |
130 | // we shamelessly optimise for "simple" types. everything |
64 | // "not simple enough" will use the slow path. |
131 | // "not simple enough" will use the slow path. |
65 | static bool is_simple_enough () |
132 | static bool is_simple_enough () |
66 | { |
133 | { |
67 | return 1; // we are not there yet |
134 | #if ECB_CPP11 |
68 | #if __cplusplus >= 201103L |
|
|
69 | return std::is_trivially_assignable<T, T>::value |
135 | return std::is_trivially_assignable<T, T>::value |
70 | && std::is_trivially_constructable<T>::value |
136 | && std::is_trivially_constructible<T>::value |
71 | && std::is_trivially_copyable<T>::value |
137 | && std::is_trivially_copyable<T>::value |
72 | && std::is_trivially_destructible<T>::value; |
138 | && std::is_trivially_destructible<T>::value; |
73 | #elif ECB_GCC_VERSION(4,4) |
139 | #elif ECB_GCC_VERSION(4,4) || ECB_CLANG_VERSION(2,8) |
74 | return __has_trivial_assign (T) |
140 | return __has_trivial_assign (T) |
75 | && __has_trivial_constructor (T) |
141 | && __has_trivial_constructor (T) |
76 | && __has_trivial_copy (T) |
142 | && __has_trivial_copy (T) |
77 | && __has_trivial_destructor (T); |
143 | && __has_trivial_destructor (T); |
78 | #else |
144 | #else |
… | |
… | |
82 | |
148 | |
83 | static void construct (iterator a, size_type n = 1) |
149 | static void construct (iterator a, size_type n = 1) |
84 | { |
150 | { |
85 | if (!is_simple_enough ()) |
151 | if (!is_simple_enough ()) |
86 | while (n--) |
152 | while (n--) |
87 | new (*a++) T (); |
153 | new (a++) T (); |
88 | } |
154 | } |
89 | |
155 | |
90 | static void destruct (iterator a, size_type n = 1) |
156 | static void destruct (iterator a, size_type n = 1) |
91 | { |
157 | { |
92 | if (!is_simple_enough ()) |
158 | if (!is_simple_enough ()) |
93 | while (n--) |
159 | while (n--) |
94 | (*a++).~T (); |
160 | (*a++).~T (); |
95 | } |
161 | } |
96 | |
162 | |
|
|
163 | template<class I> |
97 | static void cop_new (iterator a, iterator b) { new (a) T (*b); } |
164 | static void cop_new (iterator a, I b) { new (a) T (*b); } |
|
|
165 | template<class I> |
98 | static void cop_set (iterator a, iterator b) { *a = *b ; } |
166 | static void cop_set (iterator a, I b) { *a = *b ; } |
99 | |
167 | |
100 | // these copy helpers actually use the copy constructor, not assignment |
168 | // MUST copy forwards |
|
|
169 | template<class I> |
101 | static void copy_lower (iterator dst, iterator src, size_type n, void (*op)(iterator, iterator) = cop_new) |
170 | static void copy (iterator dst, I src, size_type n, void (*op)(iterator, I)) |
102 | { |
171 | { |
103 | if (is_simple_enough ()) |
|
|
104 | memmove (dst, src, sizeof (T) * n); |
|
|
105 | else |
|
|
106 | while (n--) |
172 | while (n--) |
107 | op (dst++, src++); |
173 | op (dst++, src++); |
108 | } |
174 | } |
109 | |
175 | |
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) |
176 | static void copy (iterator dst, iterator src, size_type n, void (*op)(iterator, iterator)) |
120 | { |
177 | { |
121 | if (is_simple_enough ()) |
178 | if (is_simple_enough ()) |
122 | memcpy (dst, src, sizeof (T) * n); |
179 | memcpy (dst, src, sizeof (T) * n); |
123 | else |
180 | else |
124 | copy_lower (dst, src, n, op); |
181 | copy<iterator> (dst, src, n, op); |
125 | } |
182 | } |
126 | |
183 | |
127 | static T *alloc (size_type n) ecb_cold |
184 | static T *alloc (size_type n) ecb_cold |
128 | { |
185 | { |
129 | return (T *)::operator new ((size_t) (sizeof (T) * n)); |
186 | return (T *)::operator new ((size_t) (sizeof (T) * n)); |
… | |
… | |
153 | dealloc (); |
210 | dealloc (); |
154 | buf = nbuf; |
211 | buf = nbuf; |
155 | } |
212 | } |
156 | |
213 | |
157 | construct (buf + sze, n); |
214 | construct (buf + sze, n); |
158 | copy_higher (buf + pos + n, buf + pos, sze - pos, cop_set); |
215 | |
|
|
216 | iterator src = buf + pos; |
|
|
217 | if (is_simple_enough ()) |
|
|
218 | memmove (src + n, src, sizeof (T) * (sze - pos)); |
|
|
219 | else |
|
|
220 | for (size_type i = sze - pos; i--; ) |
|
|
221 | cop_set (src + n + i, src + i); |
|
|
222 | |
159 | sze += n; |
223 | sze += n; |
160 | } |
224 | } |
161 | |
225 | |
162 | public: |
226 | public: |
163 | size_type capacity () const { return res; } |
227 | size_type capacity () const { return res; } |
164 | size_type size () const { return sze; } |
228 | size_type size () const { return sze; } |
165 | bool empty () const { return size () == 0; } |
229 | bool empty () const { return size () == 0; } |
|
|
230 | |
|
|
231 | size_t max_size () const |
|
|
232 | { |
|
|
233 | return (~(size_type)0) >> 1; |
|
|
234 | } |
166 | |
235 | |
167 | const_iterator begin () const { return &buf [ 0]; } |
236 | const_iterator begin () const { return &buf [ 0]; } |
168 | iterator begin () { return &buf [ 0]; } |
237 | iterator begin () { return &buf [ 0]; } |
169 | const_iterator end () const { return &buf [sze ]; } |
238 | const_iterator end () const { return &buf [sze ]; } |
170 | iterator end () { return &buf [sze ]; } |
239 | iterator end () { return &buf [sze ]; } |
… | |
… | |
179 | return; |
248 | return; |
180 | |
249 | |
181 | sz = good_size (sz); |
250 | sz = good_size (sz); |
182 | T *nbuf = alloc (sz); |
251 | T *nbuf = alloc (sz); |
183 | |
252 | |
184 | copy (nbuf, begin (), sze); |
253 | copy (nbuf, begin (), sze, cop_new); |
185 | dealloc (); |
254 | dealloc (); |
186 | |
255 | |
187 | buf = nbuf; |
256 | buf = nbuf; |
188 | res = sz; |
257 | res = sz; |
189 | } |
258 | } |
… | |
… | |
205 | : sze(0), res(0), buf(0) |
274 | : sze(0), res(0), buf(0) |
206 | { |
275 | { |
207 | } |
276 | } |
208 | |
277 | |
209 | simplevec (size_type n, const T &t = T ()) |
278 | simplevec (size_type n, const T &t = T ()) |
210 | : sze(0), res(0), buf(0) |
|
|
211 | { |
279 | { |
212 | insert (begin (), n, t); |
280 | sze = res = n; |
|
|
281 | buf = alloc (sze); |
|
|
282 | |
|
|
283 | while (n--) |
|
|
284 | new (buf + n) T (t); |
213 | } |
285 | } |
214 | |
286 | |
215 | simplevec (const_iterator first, const_iterator last) |
287 | simplevec (const_iterator first, const_iterator last) |
216 | : sze(0), res(0), buf(0) |
|
|
217 | { |
288 | { |
218 | insert (begin (), first, last); |
289 | sze = res = last - first; |
|
|
290 | buf = alloc (sze); |
|
|
291 | copy (buf, first, sze, cop_new); |
219 | } |
292 | } |
220 | |
293 | |
221 | simplevec (const simplevec<T> &v) |
294 | simplevec (const simplevec<T> &v) |
222 | : sze(0), res(0), buf(0) |
295 | : sze(0), res(0), buf(0) |
223 | { |
296 | { |
224 | insert (begin (), v.begin (), v.end ()); |
297 | sze = res = v.size (); |
225 | } |
298 | buf = alloc (sze); |
226 | |
299 | 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 | } |
300 | } |
232 | |
301 | |
233 | ~simplevec () |
302 | ~simplevec () |
234 | { |
303 | { |
235 | dealloc (); |
304 | dealloc (); |
… | |
… | |
257 | void pop_back () |
326 | void pop_back () |
258 | { |
327 | { |
259 | destruct (buf + --sze); |
328 | destruct (buf + --sze); |
260 | } |
329 | } |
261 | |
330 | |
262 | const T &operator [](size_type idx) const { return buf[idx]; } |
331 | const_reference operator [](size_type idx) const { return buf[idx]; } |
263 | T &operator [](size_type idx) { return buf[idx]; } |
332 | reference operator [](size_type idx) { return buf[idx]; } |
|
|
333 | |
|
|
334 | const_reference at (size_type idx) const { return buf [idx]; } |
|
|
335 | reference at (size_type idx) { return buf [idx]; } |
|
|
336 | |
|
|
337 | void assign (const_iterator first, const_iterator last) |
|
|
338 | { |
|
|
339 | simplevec<T> v (first, last); |
|
|
340 | swap (v); |
|
|
341 | } |
|
|
342 | |
|
|
343 | void assign (size_type n, const T &t) |
|
|
344 | { |
|
|
345 | simplevec<T> v (n, t); |
|
|
346 | swap (v); |
|
|
347 | } |
|
|
348 | |
|
|
349 | simplevec<T> &operator= (const simplevec<T> &v) |
|
|
350 | { |
|
|
351 | assign (v.begin (), v.end ()); |
|
|
352 | return *this; |
|
|
353 | } |
264 | |
354 | |
265 | iterator insert (iterator pos, const T &t) |
355 | iterator insert (iterator pos, const T &t) |
266 | { |
356 | { |
267 | size_type at = pos - begin (); |
357 | size_type at = pos - begin (); |
268 | |
358 | |
… | |
… | |
293 | *i++ = t; |
383 | *i++ = t; |
294 | |
384 | |
295 | return buf + at; |
385 | return buf + at; |
296 | } |
386 | } |
297 | |
387 | |
298 | void erase (iterator first, iterator last) |
388 | iterator erase (iterator first, iterator last) |
299 | { |
389 | { |
300 | size_t n = last - first; |
390 | size_type n = last - first; |
|
|
391 | size_type c = end () - last; |
301 | |
392 | |
302 | copy_lower (last, first, end () - last, cop_set); |
393 | if (is_simple_enough ()) |
|
|
394 | memmove (first, last, sizeof (T) * c); |
|
|
395 | else |
|
|
396 | copy (first, last, c, cop_set); |
|
|
397 | |
303 | sze -= n; |
398 | sze -= n; |
304 | destruct (buf + sze, n); |
399 | destruct (buf + sze, n); |
305 | } |
|
|
306 | |
400 | |
|
|
401 | return first; |
|
|
402 | } |
|
|
403 | |
307 | void erase (iterator pos) |
404 | iterator erase (iterator pos) |
308 | { |
405 | { |
309 | if (pos != end ()) |
406 | if (pos != end ()) |
310 | erase (pos, pos + 1); |
407 | erase (pos, pos + 1); |
|
|
408 | |
|
|
409 | return pos; |
311 | } |
410 | } |
312 | }; |
411 | }; |
313 | |
412 | |
314 | template<class T> |
413 | template<class T> |
315 | bool operator ==(const simplevec<T> &v1, const simplevec<T> &v2) |
414 | bool operator ==(const simplevec<T> &v1, const simplevec<T> &v2) |