… | |
… | |
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 | |
103 | static void copy_higher (iterator dst, iterator src, size_type n, void (*op)(iterator, iterator) = cop_new) |
162 | // MUST copy forwards |
104 | { |
|
|
105 | if (is_simple_enough ()) |
|
|
106 | memmove (dst, src, sizeof (T) * n); |
|
|
107 | else |
|
|
108 | while (n--) |
|
|
109 | op (dst + n, src + n); |
|
|
110 | } |
|
|
111 | |
|
|
112 | template<class I> |
163 | template<class I> |
113 | static void copy (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)) |
114 | { |
165 | { |
115 | while (n--) |
166 | while (n--) |
116 | op (dst++, src++); |
167 | op (dst++, src++); |
117 | } |
168 | } |
118 | |
169 | |
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<iterator> (dst, src, n, op); |
175 | copy<iterator> (dst, src, n, op); |
… | |
… | |
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; } |
… | |
… | |
184 | return; |
242 | return; |
185 | |
243 | |
186 | sz = good_size (sz); |
244 | sz = good_size (sz); |
187 | T *nbuf = alloc (sz); |
245 | T *nbuf = alloc (sz); |
188 | |
246 | |
189 | copy (nbuf, begin (), sze); |
247 | copy (nbuf, begin (), sze, cop_new); |
190 | dealloc (); |
248 | dealloc (); |
191 | |
249 | |
192 | buf = nbuf; |
250 | buf = nbuf; |
193 | res = sz; |
251 | res = sz; |
194 | } |
252 | } |
… | |
… | |
218 | |
276 | |
219 | while (n--) |
277 | while (n--) |
220 | new (buf + n) T (t); |
278 | new (buf + n) T (t); |
221 | } |
279 | } |
222 | |
280 | |
223 | template<class I> |
281 | simplevec (const_iterator first, const_iterator last) |
224 | simplevec (I first, I last) |
|
|
225 | { |
282 | { |
226 | sze = res = last - first; |
283 | sze = res = last - first; |
227 | buf = alloc (sze); |
284 | buf = alloc (sze); |
228 | copy (buf, first, sze); |
285 | copy (buf, first, sze, cop_new); |
229 | } |
286 | } |
230 | |
287 | |
231 | simplevec (const simplevec<T> &v) |
288 | simplevec (const simplevec<T> &v) |
232 | : sze(0), res(0), buf(0) |
289 | : sze(0), res(0), buf(0) |
233 | { |
290 | { |
234 | sze = res = v.size (); |
291 | sze = res = v.size (); |
235 | buf = alloc (sze); |
292 | buf = alloc (sze); |
236 | copy (buf, v.begin (), sze); |
293 | copy (buf, v.begin (), sze, cop_new); |
237 | } |
294 | } |
238 | |
295 | |
239 | ~simplevec () |
296 | ~simplevec () |
240 | { |
297 | { |
241 | dealloc (); |
298 | dealloc (); |
… | |
… | |
269 | reference operator [](size_type idx) { return buf[idx]; } |
326 | reference operator [](size_type idx) { return buf[idx]; } |
270 | |
327 | |
271 | const_reference at (size_type idx) const { return buf [idx]; } |
328 | const_reference at (size_type idx) const { return buf [idx]; } |
272 | reference at (size_type idx) { return buf [idx]; } |
329 | reference at (size_type idx) { return buf [idx]; } |
273 | |
330 | |
274 | template<class I> |
331 | void assign (const_iterator first, const_iterator last) |
275 | void assign (I first, I last) |
|
|
276 | { |
332 | { |
277 | swap (simplevec<T> (first, last)); |
333 | simplevec<T> v (first, last); |
|
|
334 | swap (v); |
278 | } |
335 | } |
279 | |
336 | |
280 | void assign (size_type n, const T &t) |
337 | void assign (size_type n, const T &t) |
281 | { |
338 | { |
282 | swap (simplevec<T> (n, t)); |
339 | simplevec<T> v (n, t); |
|
|
340 | swap (v); |
283 | } |
341 | } |
284 | |
342 | |
285 | simplevec<T> &operator= (const simplevec<T> &v) |
343 | simplevec<T> &operator= (const simplevec<T> &v) |
286 | { |
344 | { |
287 | assign (v.begin (), v.end ()); |
345 | assign (v.begin (), v.end ()); |
… | |
… | |
296 | buf [at] = t; |
354 | buf [at] = t; |
297 | |
355 | |
298 | return buf + at; |
356 | return buf + at; |
299 | } |
357 | } |
300 | |
358 | |
301 | template<class I> |
|
|
302 | iterator insert (iterator pos, I first, I last) |
359 | iterator insert (iterator pos, const_iterator first, const_iterator last) |
303 | { |
360 | { |
304 | size_type n = last - first; |
361 | size_type n = last - first; |
305 | size_type at = pos - begin (); |
362 | size_type at = pos - begin (); |
306 | |
363 | |
307 | ins (pos, n); |
364 | ins (pos, n); |
… | |
… | |
320 | *i++ = t; |
377 | *i++ = t; |
321 | |
378 | |
322 | return buf + at; |
379 | return buf + at; |
323 | } |
380 | } |
324 | |
381 | |
325 | void erase (iterator first, iterator last) |
382 | iterator erase (iterator first, iterator last) |
326 | { |
383 | { |
327 | size_t n = last - first; |
384 | size_type n = last - first; |
|
|
385 | size_type c = end () - last; |
328 | |
386 | |
329 | if (is_simple_enough ()) |
387 | if (is_simple_enough ()) |
330 | memmove (first, last, sizeof (T) * n); |
388 | memmove (first, last, sizeof (T) * c); |
331 | else |
389 | else |
332 | copy<iterator> (first, last, n, cop_new); |
390 | copy (first, last, c, cop_set); |
333 | |
391 | |
334 | sze -= n; |
392 | sze -= n; |
335 | destruct (buf + sze, n); |
393 | destruct (buf + sze, n); |
336 | } |
|
|
337 | |
394 | |
|
|
395 | return first; |
|
|
396 | } |
|
|
397 | |
338 | void erase (iterator pos) |
398 | iterator erase (iterator pos) |
339 | { |
399 | { |
340 | if (pos != end ()) |
400 | if (pos != end ()) |
341 | erase (pos, pos + 1); |
401 | erase (pos, pos + 1); |
|
|
402 | |
|
|
403 | return pos; |
342 | } |
404 | } |
343 | }; |
405 | }; |
344 | |
406 | |
345 | template<class T> |
407 | template<class T> |
346 | bool operator ==(const simplevec<T> &v1, const simplevec<T> &v2) |
408 | bool operator ==(const simplevec<T> &v1, const simplevec<T> &v2) |