… | |
… | |
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 | 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 | } |
28 | |
101 | |
29 | // original version taken from MICO, but this has been completely rewritten |
102 | // original version taken from MICO, but this has been completely rewritten |
30 | // known limitations w.r.t. std::vector |
103 | // known limitations w.r.t. std::vector |
31 | // - many methods missing |
104 | // - many methods missing |
32 | // - no error checking, no exceptions thrown (e.g. at()) |
105 | // - no error checking, no exceptions thrown (e.g. at()) |
… | |
… | |
36 | // - we don't care about namespaces and stupid macros the user might define |
109 | // - we don't care about namespaces and stupid macros the user might define |
37 | // - no bool specialisation |
110 | // - no bool specialisation |
38 | template<class T> |
111 | template<class T> |
39 | struct simplevec |
112 | struct simplevec |
40 | { |
113 | { |
41 | #if ESTL_BIG_VECTOR |
114 | 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 | |
115 | |
50 | typedef T value_type; |
116 | typedef T value_type; |
51 | typedef T *iterator; |
117 | typedef T *iterator; |
52 | typedef const T *const_iterator; |
118 | typedef const T *const_iterator; |
53 | typedef T *pointer; |
119 | typedef T *pointer; |
… | |
… | |
63 | |
129 | |
64 | // we shamelessly optimise for "simple" types. everything |
130 | // we shamelessly optimise for "simple" types. everything |
65 | // "not simple enough" will use the slow path. |
131 | // "not simple enough" will use the slow path. |
66 | static bool is_simple_enough () |
132 | static bool is_simple_enough () |
67 | { |
133 | { |
68 | return 1; // we are not there yet |
134 | #if ECB_CPP11 |
69 | #if __cplusplus >= 201103L |
|
|
70 | return std::is_trivially_assignable<T, T>::value |
135 | return std::is_trivially_assignable<T, T>::value |
71 | && std::is_trivially_constructable<T>::value |
136 | && std::is_trivially_constructible<T>::value |
72 | && std::is_trivially_copyable<T>::value |
137 | && std::is_trivially_copyable<T>::value |
73 | && std::is_trivially_destructible<T>::value; |
138 | && std::is_trivially_destructible<T>::value; |
74 | #elif ECB_GCC_VERSION(4,4) |
139 | #elif ECB_GCC_VERSION(4,4) || ECB_CLANG_VERSION(2,8) |
75 | return __has_trivial_assign (T) |
140 | return __has_trivial_assign (T) |
76 | && __has_trivial_constructor (T) |
141 | && __has_trivial_constructor (T) |
77 | && __has_trivial_copy (T) |
142 | && __has_trivial_copy (T) |
78 | && __has_trivial_destructor (T); |
143 | && __has_trivial_destructor (T); |
79 | #else |
144 | #else |
… | |
… | |
83 | |
148 | |
84 | static void construct (iterator a, size_type n = 1) |
149 | static void construct (iterator a, size_type n = 1) |
85 | { |
150 | { |
86 | if (!is_simple_enough ()) |
151 | if (!is_simple_enough ()) |
87 | while (n--) |
152 | while (n--) |
88 | new (*a++) T (); |
153 | new (a++) T (); |
89 | } |
154 | } |
90 | |
155 | |
91 | static void destruct (iterator a, size_type n = 1) |
156 | static void destruct (iterator a, size_type n = 1) |
92 | { |
157 | { |
93 | if (!is_simple_enough ()) |
158 | if (!is_simple_enough ()) |
… | |
… | |
100 | template<class I> |
165 | template<class I> |
101 | static void cop_set (iterator a, I b) { *a = *b ; } |
166 | static void cop_set (iterator a, I b) { *a = *b ; } |
102 | |
167 | |
103 | // MUST copy forwards |
168 | // MUST copy forwards |
104 | template<class I> |
169 | template<class I> |
105 | static void copy (iterator dst, I src, size_type n, void (*op)(iterator, I) = cop_new) |
170 | static void copy (iterator dst, I src, size_type n, void (*op)(iterator, I)) |
106 | { |
171 | { |
107 | while (n--) |
172 | while (n--) |
108 | op (dst++, src++); |
173 | op (dst++, src++); |
109 | } |
174 | } |
110 | |
175 | |
111 | 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)) |
112 | { |
177 | { |
113 | if (is_simple_enough ()) |
178 | if (is_simple_enough ()) |
114 | memcpy (dst, src, sizeof (T) * n); |
179 | memcpy (dst, src, sizeof (T) * n); |
115 | else |
180 | else |
116 | copy<iterator> (dst, src, n, op); |
181 | copy<iterator> (dst, src, n, op); |
… | |
… | |
146 | buf = nbuf; |
211 | buf = nbuf; |
147 | } |
212 | } |
148 | |
213 | |
149 | construct (buf + sze, n); |
214 | construct (buf + sze, n); |
150 | |
215 | |
151 | sze += n; |
|
|
152 | |
|
|
153 | iterator src = buf + pos; |
216 | iterator src = buf + pos; |
154 | if (is_simple_enough ()) |
217 | if (is_simple_enough ()) |
155 | memmove (src + n, src, sizeof (T) * n); |
218 | memmove (src + n, src, sizeof (T) * (sze - pos)); |
156 | else |
219 | else |
157 | for (size_type i = n; i--; ) |
220 | for (size_type i = sze - pos; i--; ) |
158 | op (src + n + i, src + i); |
221 | cop_set (src + n + i, src + i); |
|
|
222 | |
|
|
223 | sze += n; |
159 | } |
224 | } |
160 | |
225 | |
161 | public: |
226 | public: |
162 | size_type capacity () const { return res; } |
227 | size_type capacity () const { return res; } |
163 | size_type size () const { return sze; } |
228 | size_type size () const { return sze; } |
… | |
… | |
183 | return; |
248 | return; |
184 | |
249 | |
185 | sz = good_size (sz); |
250 | sz = good_size (sz); |
186 | T *nbuf = alloc (sz); |
251 | T *nbuf = alloc (sz); |
187 | |
252 | |
188 | copy (nbuf, begin (), sze); |
253 | copy (nbuf, begin (), sze, cop_new); |
189 | dealloc (); |
254 | dealloc (); |
190 | |
255 | |
191 | buf = nbuf; |
256 | buf = nbuf; |
192 | res = sz; |
257 | res = sz; |
193 | } |
258 | } |
… | |
… | |
217 | |
282 | |
218 | while (n--) |
283 | while (n--) |
219 | new (buf + n) T (t); |
284 | new (buf + n) T (t); |
220 | } |
285 | } |
221 | |
286 | |
222 | template<class I> |
287 | simplevec (const_iterator first, const_iterator last) |
223 | simplevec (I first, I last) |
|
|
224 | { |
288 | { |
225 | sze = res = last - first; |
289 | sze = res = last - first; |
226 | buf = alloc (sze); |
290 | buf = alloc (sze); |
227 | copy (buf, first, sze); |
291 | copy (buf, first, sze, cop_new); |
228 | } |
292 | } |
229 | |
293 | |
230 | simplevec (const simplevec<T> &v) |
294 | simplevec (const simplevec<T> &v) |
231 | : sze(0), res(0), buf(0) |
295 | : sze(0), res(0), buf(0) |
232 | { |
296 | { |
233 | sze = res = v.size (); |
297 | sze = res = v.size (); |
234 | buf = alloc (sze); |
298 | buf = alloc (sze); |
235 | copy (buf, v.begin (), sze); |
299 | copy (buf, v.begin (), sze, cop_new); |
236 | } |
300 | } |
237 | |
301 | |
238 | ~simplevec () |
302 | ~simplevec () |
239 | { |
303 | { |
240 | dealloc (); |
304 | dealloc (); |
… | |
… | |
268 | reference operator [](size_type idx) { return buf[idx]; } |
332 | reference operator [](size_type idx) { return buf[idx]; } |
269 | |
333 | |
270 | const_reference at (size_type idx) const { return buf [idx]; } |
334 | const_reference at (size_type idx) const { return buf [idx]; } |
271 | reference at (size_type idx) { return buf [idx]; } |
335 | reference at (size_type idx) { return buf [idx]; } |
272 | |
336 | |
273 | template<class I> |
337 | void assign (const_iterator first, const_iterator last) |
274 | void assign (I first, I last) |
|
|
275 | { |
338 | { |
276 | swap (simplevec<T> (first, last)); |
339 | simplevec<T> v (first, last); |
|
|
340 | swap (v); |
277 | } |
341 | } |
278 | |
342 | |
279 | void assign (size_type n, const T &t) |
343 | void assign (size_type n, const T &t) |
280 | { |
344 | { |
281 | swap (simplevec<T> (n, t)); |
345 | simplevec<T> v (n, t); |
|
|
346 | swap (v); |
282 | } |
347 | } |
283 | |
348 | |
284 | simplevec<T> &operator= (const simplevec<T> &v) |
349 | simplevec<T> &operator= (const simplevec<T> &v) |
285 | { |
350 | { |
286 | assign (v.begin (), v.end ()); |
351 | assign (v.begin (), v.end ()); |
… | |
… | |
295 | buf [at] = t; |
360 | buf [at] = t; |
296 | |
361 | |
297 | return buf + at; |
362 | return buf + at; |
298 | } |
363 | } |
299 | |
364 | |
300 | template<class I> |
|
|
301 | iterator insert (iterator pos, I first, I last) |
365 | iterator insert (iterator pos, const_iterator first, const_iterator last) |
302 | { |
366 | { |
303 | size_type n = last - first; |
367 | size_type n = last - first; |
304 | size_type at = pos - begin (); |
368 | size_type at = pos - begin (); |
305 | |
369 | |
306 | ins (pos, n); |
370 | ins (pos, n); |
… | |
… | |
319 | *i++ = t; |
383 | *i++ = t; |
320 | |
384 | |
321 | return buf + at; |
385 | return buf + at; |
322 | } |
386 | } |
323 | |
387 | |
324 | void erase (iterator first, iterator last) |
388 | iterator erase (iterator first, iterator last) |
325 | { |
389 | { |
326 | size_t n = last - first; |
390 | size_type n = last - first; |
|
|
391 | size_type c = end () - last; |
327 | |
392 | |
328 | if (is_simple_enough ()) |
393 | if (is_simple_enough ()) |
329 | memmove (first, last, sizeof (T) * n); |
394 | memmove (first, last, sizeof (T) * c); |
330 | else |
395 | else |
331 | copy<iterator> (first, last, n, cop_new); |
396 | copy (first, last, c, cop_set); |
332 | |
397 | |
333 | sze -= n; |
398 | sze -= n; |
334 | destruct (buf + sze, n); |
399 | destruct (buf + sze, n); |
335 | } |
|
|
336 | |
400 | |
|
|
401 | return first; |
|
|
402 | } |
|
|
403 | |
337 | void erase (iterator pos) |
404 | iterator erase (iterator pos) |
338 | { |
405 | { |
339 | if (pos != end ()) |
406 | if (pos != end ()) |
340 | erase (pos, pos + 1); |
407 | erase (pos, pos + 1); |
|
|
408 | |
|
|
409 | return pos; |
341 | } |
410 | } |
342 | }; |
411 | }; |
343 | |
412 | |
344 | template<class T> |
413 | template<class T> |
345 | bool operator ==(const simplevec<T> &v1, const simplevec<T> &v2) |
414 | bool operator ==(const simplevec<T> &v1, const simplevec<T> &v2) |