… | |
… | |
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 | |
|
|
29 | namespace estl |
|
|
30 | { |
|
|
31 | #if ESTL_LARGE_MEMORY_MODEL |
|
|
32 | // should use size_t/ssize_t, but that's not portable enough for us |
|
|
33 | typedef unsigned long size_type; |
|
|
34 | typedef long difference_type; |
|
|
35 | #else |
|
|
36 | typedef uint32_t size_type; |
|
|
37 | typedef int32_t difference_type; |
|
|
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 | } |
28 | |
95 | |
29 | // original version taken from MICO, but this has been completely rewritten |
96 | // original version taken from MICO, but this has been completely rewritten |
30 | // known limitations w.r.t. std::vector |
97 | // known limitations w.r.t. std::vector |
31 | // - many methods missing |
98 | // - many methods missing |
32 | // - no error checking, no exceptions thrown (e.g. at()) |
99 | // - no error checking, no exceptions thrown (e.g. at()) |
… | |
… | |
36 | // - we don't care about namespaces and stupid macros the user might define |
103 | // - we don't care about namespaces and stupid macros the user might define |
37 | // - no bool specialisation |
104 | // - no bool specialisation |
38 | template<class T> |
105 | template<class T> |
39 | struct simplevec |
106 | struct simplevec |
40 | { |
107 | { |
41 | #if ESTL_BIG_VECTOR |
108 | typedef estl::size_type size_type; |
42 | // shoudl use size_t/ssize_t, but that's not portable enough for us |
|
|
43 | typedef unsigned long size_type; |
|
|
44 | typedef long difference_type; |
|
|
45 | #else |
|
|
46 | typedef uint32_t size_type; |
|
|
47 | typedef int32_t difference_type; |
|
|
48 | #endif |
|
|
49 | |
109 | |
50 | typedef T value_type; |
110 | typedef T value_type; |
51 | typedef T *iterator; |
111 | typedef T *iterator; |
52 | typedef const T *const_iterator; |
112 | typedef const T *const_iterator; |
53 | typedef T *pointer; |
113 | typedef T *pointer; |
… | |
… | |
63 | |
123 | |
64 | // we shamelessly optimise for "simple" types. everything |
124 | // we shamelessly optimise for "simple" types. everything |
65 | // "not simple enough" will use the slow path. |
125 | // "not simple enough" will use the slow path. |
66 | static bool is_simple_enough () |
126 | static bool is_simple_enough () |
67 | { |
127 | { |
68 | return 1; // we are not there yet |
128 | #if ECB_CPP11 |
69 | #if __cplusplus >= 201103L |
|
|
70 | return std::is_trivially_assignable<T, T>::value |
129 | return std::is_trivially_assignable<T, T>::value |
71 | && std::is_trivially_constructable<T>::value |
130 | && std::is_trivially_constructible<T>::value |
72 | && std::is_trivially_copyable<T>::value |
131 | && std::is_trivially_copyable<T>::value |
73 | && std::is_trivially_destructible<T>::value; |
132 | && std::is_trivially_destructible<T>::value; |
74 | #elif ECB_GCC_VERSION(4,4) |
133 | #elif ECB_GCC_VERSION(4,4) || ECB_CLANG_VERSION(2,8) |
75 | return __has_trivial_assign (T) |
134 | return __has_trivial_assign (T) |
76 | && __has_trivial_constructor (T) |
135 | && __has_trivial_constructor (T) |
77 | && __has_trivial_copy (T) |
136 | && __has_trivial_copy (T) |
78 | && __has_trivial_destructor (T); |
137 | && __has_trivial_destructor (T); |
79 | #else |
138 | #else |
… | |
… | |
83 | |
142 | |
84 | static void construct (iterator a, size_type n = 1) |
143 | static void construct (iterator a, size_type n = 1) |
85 | { |
144 | { |
86 | if (!is_simple_enough ()) |
145 | if (!is_simple_enough ()) |
87 | while (n--) |
146 | while (n--) |
88 | new (*a++) T (); |
147 | new (a++) T (); |
89 | } |
148 | } |
90 | |
149 | |
91 | static void destruct (iterator a, size_type n = 1) |
150 | static void destruct (iterator a, size_type n = 1) |
92 | { |
151 | { |
93 | if (!is_simple_enough ()) |
152 | if (!is_simple_enough ()) |
… | |
… | |
98 | template<class I> |
157 | template<class I> |
99 | static void cop_new (iterator a, I b) { new (a) T (*b); } |
158 | static void cop_new (iterator a, I b) { new (a) T (*b); } |
100 | template<class I> |
159 | template<class I> |
101 | static void cop_set (iterator a, I b) { *a = *b ; } |
160 | static void cop_set (iterator a, I b) { *a = *b ; } |
102 | |
161 | |
|
|
162 | // MUST copy forwards |
103 | template<class I> |
163 | template<class I> |
104 | static void copy_lower (iterator dst, I src, size_type n, void (*op)(iterator, I ) = cop_new) |
164 | static void copy (iterator dst, I src, size_type n, void (*op)(iterator, I)) |
105 | { |
165 | { |
106 | while (n--) |
166 | while (n--) |
107 | op (dst++, src++); |
167 | op (dst++, src++); |
108 | } |
168 | } |
109 | |
169 | |
110 | static void copy_lower (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 | copy_lower<iterator> (dst, src, n, cop_new); |
|
|
116 | } |
|
|
117 | |
|
|
118 | static void copy_higher (iterator dst, iterator src, size_type n, void (*op)(iterator, iterator) = cop_new) |
|
|
119 | { |
|
|
120 | if (is_simple_enough ()) |
|
|
121 | memmove (dst, src, sizeof (T) * n); |
|
|
122 | else |
|
|
123 | while (n--) |
|
|
124 | op (dst + n, src + n); |
|
|
125 | } |
|
|
126 | |
|
|
127 | template<class I> |
|
|
128 | static void copy (iterator dst, I src, size_type n, void (*op)(iterator, I) = cop_new) |
|
|
129 | { |
|
|
130 | copy_lower<I> (dst, src, n, op); |
|
|
131 | } |
|
|
132 | |
|
|
133 | 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)) |
134 | { |
171 | { |
135 | if (is_simple_enough ()) |
172 | if (is_simple_enough ()) |
136 | memcpy (dst, src, sizeof (T) * n); |
173 | memcpy (dst, src, sizeof (T) * n); |
137 | else |
174 | else |
138 | copy<iterator> (dst, src, n, op); |
175 | copy<iterator> (dst, src, n, op); |
… | |
… | |
167 | dealloc (); |
204 | dealloc (); |
168 | buf = nbuf; |
205 | buf = nbuf; |
169 | } |
206 | } |
170 | |
207 | |
171 | construct (buf + sze, n); |
208 | construct (buf + sze, n); |
172 | 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 | |
173 | sze += n; |
217 | sze += n; |
174 | } |
218 | } |
175 | |
219 | |
176 | public: |
220 | public: |
177 | size_type capacity () const { return res; } |
221 | size_type capacity () const { return res; } |
… | |
… | |
198 | return; |
242 | return; |
199 | |
243 | |
200 | sz = good_size (sz); |
244 | sz = good_size (sz); |
201 | T *nbuf = alloc (sz); |
245 | T *nbuf = alloc (sz); |
202 | |
246 | |
203 | copy (nbuf, begin (), sze); |
247 | copy (nbuf, begin (), sze, cop_new); |
204 | dealloc (); |
248 | dealloc (); |
205 | |
249 | |
206 | buf = nbuf; |
250 | buf = nbuf; |
207 | res = sz; |
251 | res = sz; |
208 | } |
252 | } |
… | |
… | |
232 | |
276 | |
233 | while (n--) |
277 | while (n--) |
234 | new (buf + n) T (t); |
278 | new (buf + n) T (t); |
235 | } |
279 | } |
236 | |
280 | |
237 | template<class I> |
281 | simplevec (const_iterator first, const_iterator last) |
238 | simplevec (I first, I last) |
|
|
239 | { |
282 | { |
240 | sze = res = last - first; |
283 | sze = res = last - first; |
241 | buf = alloc (sze); |
284 | buf = alloc (sze); |
242 | copy (buf, first, sze); |
285 | copy (buf, first, sze, cop_new); |
243 | } |
286 | } |
244 | |
287 | |
245 | simplevec (const simplevec<T> &v) |
288 | simplevec (const simplevec<T> &v) |
246 | : sze(0), res(0), buf(0) |
289 | : sze(0), res(0), buf(0) |
247 | { |
290 | { |
248 | sze = res = v.size (); |
291 | sze = res = v.size (); |
249 | buf = alloc (sze); |
292 | buf = alloc (sze); |
250 | copy (buf, v.begin (), sze); |
293 | copy (buf, v.begin (), sze, cop_new); |
251 | } |
294 | } |
252 | |
295 | |
253 | ~simplevec () |
296 | ~simplevec () |
254 | { |
297 | { |
255 | dealloc (); |
298 | dealloc (); |
… | |
… | |
283 | reference operator [](size_type idx) { return buf[idx]; } |
326 | reference operator [](size_type idx) { return buf[idx]; } |
284 | |
327 | |
285 | const_reference at (size_type idx) const { return buf [idx]; } |
328 | const_reference at (size_type idx) const { return buf [idx]; } |
286 | reference at (size_type idx) { return buf [idx]; } |
329 | reference at (size_type idx) { return buf [idx]; } |
287 | |
330 | |
288 | template<class I> |
331 | void assign (const_iterator first, const_iterator last) |
289 | void assign (I first, I last) |
|
|
290 | { |
332 | { |
291 | swap (simplevec<T> (first, last)); |
333 | simplevec<T> v (first, last); |
|
|
334 | swap (v); |
292 | } |
335 | } |
293 | |
336 | |
294 | void assign (size_type n, const T &t) |
337 | void assign (size_type n, const T &t) |
295 | { |
338 | { |
296 | swap (simplevec<T> (n, t)); |
339 | simplevec<T> v (n, t); |
|
|
340 | swap (v); |
297 | } |
341 | } |
298 | |
342 | |
299 | simplevec<T> &operator= (const simplevec<T> &v) |
343 | simplevec<T> &operator= (const simplevec<T> &v) |
300 | { |
344 | { |
301 | assign (v.begin (), v.end ()); |
345 | assign (v.begin (), v.end ()); |
… | |
… | |
310 | buf [at] = t; |
354 | buf [at] = t; |
311 | |
355 | |
312 | return buf + at; |
356 | return buf + at; |
313 | } |
357 | } |
314 | |
358 | |
315 | template<class I> |
|
|
316 | iterator insert (iterator pos, I first, I last) |
359 | iterator insert (iterator pos, const_iterator first, const_iterator last) |
317 | { |
360 | { |
318 | size_type n = last - first; |
361 | size_type n = last - first; |
319 | size_type at = pos - begin (); |
362 | size_type at = pos - begin (); |
320 | |
363 | |
321 | ins (pos, n); |
364 | ins (pos, n); |
… | |
… | |
334 | *i++ = t; |
377 | *i++ = t; |
335 | |
378 | |
336 | return buf + at; |
379 | return buf + at; |
337 | } |
380 | } |
338 | |
381 | |
339 | void erase (iterator first, iterator last) |
382 | iterator erase (iterator first, iterator last) |
340 | { |
383 | { |
341 | size_t n = last - first; |
384 | size_type n = last - first; |
|
|
385 | size_type c = end () - last; |
342 | |
386 | |
343 | 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 | |
344 | sze -= n; |
392 | sze -= n; |
345 | destruct (buf + sze, n); |
393 | destruct (buf + sze, n); |
346 | } |
|
|
347 | |
394 | |
|
|
395 | return first; |
|
|
396 | } |
|
|
397 | |
348 | void erase (iterator pos) |
398 | iterator erase (iterator pos) |
349 | { |
399 | { |
350 | if (pos != end ()) |
400 | if (pos != end ()) |
351 | erase (pos, pos + 1); |
401 | erase (pos, pos + 1); |
|
|
402 | |
|
|
403 | return pos; |
352 | } |
404 | } |
353 | }; |
405 | }; |
354 | |
406 | |
355 | template<class T> |
407 | template<class T> |
356 | bool operator ==(const simplevec<T> &v1, const simplevec<T> &v2) |
408 | bool operator ==(const simplevec<T> &v1, const simplevec<T> &v2) |