… | |
… | |
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 | // original version taken from MICO, but this has been completely rewritten |
30 | // known limitations w.r.t. std::vector |
30 | // known limitations w.r.t. std::vector |
31 | // - many methods missing |
31 | // - many methods missing |
32 | // - no error checking, no exceptions thrown |
32 | // - no error checking, no exceptions thrown (e.g. at()) |
33 | // - size_type is 32bit even on 64 bit hosts, so limited to 2**31 elements |
33 | // - size_type is 32bit even on 64 bit hosts, so limited to 2**31 elements |
34 | // - no allocator support |
34 | // - no allocator support |
35 | // - we don't really care about const correctness, but we try |
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 |
36 | // - we don't care about namespaces and stupid macros the user might define |
|
|
37 | // - no bool specialisation |
37 | template<class T> |
38 | template<class T> |
38 | struct simplevec |
39 | struct simplevec |
39 | { |
40 | { |
40 | #if ESTL_BIG_VECTOR |
41 | #if ESTL_BIG_VECTOR |
41 | // shoudl use size_t/ssize_t, but that's not portable enough for us |
42 | // should use size_t/ssize_t, but that's not portable enough for us |
42 | typedef unsigned long size_type; |
43 | typedef unsigned long size_type; |
43 | typedef long difference_type; |
44 | typedef long difference_type; |
44 | #else |
45 | #else |
45 | typedef uint32_t size_type; |
46 | typedef uint32_t size_type; |
46 | typedef int32_t difference_type; |
47 | typedef int32_t difference_type; |
… | |
… | |
62 | |
63 | |
63 | // we shamelessly optimise for "simple" types. everything |
64 | // we shamelessly optimise for "simple" types. everything |
64 | // "not simple enough" will use the slow path. |
65 | // "not simple enough" will use the slow path. |
65 | static bool is_simple_enough () |
66 | static bool is_simple_enough () |
66 | { |
67 | { |
67 | return 1; // we are not there yet |
68 | #if ECB_CPP11 |
68 | #if __cplusplus >= 201103L |
|
|
69 | return std::is_trivially_assignable<T, T>::value |
69 | return std::is_trivially_assignable<T, T>::value |
70 | && std::is_trivially_constructable<T>::value |
70 | && std::is_trivially_constructable<T>::value |
71 | && std::is_trivially_copyable<T>::value |
71 | && std::is_trivially_copyable<T>::value |
72 | && std::is_trivially_destructible<T>::value; |
72 | && std::is_trivially_destructible<T>::value; |
73 | #elif ECB_GCC_VERSION(4,4) |
73 | #elif ECB_GCC_VERSION(4,4) |
… | |
… | |
82 | |
82 | |
83 | static void construct (iterator a, size_type n = 1) |
83 | static void construct (iterator a, size_type n = 1) |
84 | { |
84 | { |
85 | if (!is_simple_enough ()) |
85 | if (!is_simple_enough ()) |
86 | while (n--) |
86 | while (n--) |
87 | new (*a++) T (); |
87 | new (a++) T (); |
88 | } |
88 | } |
89 | |
89 | |
90 | static void destruct (iterator a, size_type n = 1) |
90 | static void destruct (iterator a, size_type n = 1) |
91 | { |
91 | { |
92 | if (!is_simple_enough ()) |
92 | if (!is_simple_enough ()) |
… | |
… | |
97 | template<class I> |
97 | template<class I> |
98 | static void cop_new (iterator a, I b) { new (a) T (*b); } |
98 | static void cop_new (iterator a, I b) { new (a) T (*b); } |
99 | template<class I> |
99 | template<class I> |
100 | static void cop_set (iterator a, I b) { *a = *b ; } |
100 | static void cop_set (iterator a, I b) { *a = *b ; } |
101 | |
101 | |
|
|
102 | // MUST copy forwards |
102 | template<class I> |
103 | template<class I> |
103 | static void copy_lower (iterator dst, I src, size_type n, void (*op)(iterator, I ) = cop_new) |
104 | static void copy (iterator dst, I src, size_type n, void (*op)(iterator, I)) |
104 | { |
105 | { |
105 | while (n--) |
106 | while (n--) |
106 | op (dst++, src++); |
107 | op (dst++, src++); |
107 | } |
108 | } |
108 | |
109 | |
109 | static void copy_lower (iterator dst, iterator src, size_type n, void (*op)(iterator, iterator) = cop_new) |
|
|
110 | { |
|
|
111 | if (is_simple_enough ()) |
|
|
112 | memmove (dst, src, sizeof (T) * n); |
|
|
113 | else |
|
|
114 | copy_lower<iterator> (dst, src, n, cop_new); |
|
|
115 | } |
|
|
116 | |
|
|
117 | static void copy_higher (iterator dst, iterator src, size_type n, void (*op)(iterator, iterator) = cop_new) |
|
|
118 | { |
|
|
119 | if (is_simple_enough ()) |
|
|
120 | memmove (dst, src, sizeof (T) * n); |
|
|
121 | else |
|
|
122 | while (n--) |
|
|
123 | op (dst + n, src + n); |
|
|
124 | } |
|
|
125 | |
|
|
126 | template<class I> |
|
|
127 | static void copy (iterator dst, I src, size_type n, void (*op)(iterator, I) = cop_new) |
|
|
128 | { |
|
|
129 | copy_lower<I> (dst, src, n, op); |
|
|
130 | } |
|
|
131 | |
|
|
132 | static void copy (iterator dst, iterator src, size_type n, void (*op)(iterator, iterator) = cop_new) |
110 | static void copy (iterator dst, iterator src, size_type n, void (*op)(iterator, iterator)) |
133 | { |
111 | { |
134 | if (is_simple_enough ()) |
112 | if (is_simple_enough ()) |
135 | memcpy (dst, src, sizeof (T) * n); |
113 | memcpy (dst, src, sizeof (T) * n); |
136 | else |
114 | else |
137 | copy<iterator> (dst, src, n, op); |
115 | copy<iterator> (dst, src, n, op); |
… | |
… | |
166 | dealloc (); |
144 | dealloc (); |
167 | buf = nbuf; |
145 | buf = nbuf; |
168 | } |
146 | } |
169 | |
147 | |
170 | construct (buf + sze, n); |
148 | construct (buf + sze, n); |
171 | copy_higher (buf + pos + n, buf + pos, sze - pos, cop_set); |
149 | |
|
|
150 | iterator src = buf + pos; |
|
|
151 | if (is_simple_enough ()) |
|
|
152 | memmove (src + n, src, sizeof (T) * (sze - pos)); |
|
|
153 | else |
|
|
154 | for (size_type i = sze - pos; i--; ) |
|
|
155 | cop_set (src + n + i, src + i); |
|
|
156 | |
172 | sze += n; |
157 | sze += n; |
173 | } |
158 | } |
174 | |
159 | |
175 | public: |
160 | public: |
176 | size_type capacity () const { return res; } |
161 | size_type capacity () const { return res; } |
… | |
… | |
197 | return; |
182 | return; |
198 | |
183 | |
199 | sz = good_size (sz); |
184 | sz = good_size (sz); |
200 | T *nbuf = alloc (sz); |
185 | T *nbuf = alloc (sz); |
201 | |
186 | |
202 | copy (nbuf, begin (), sze); |
187 | copy (nbuf, begin (), sze, cop_new); |
203 | dealloc (); |
188 | dealloc (); |
204 | |
189 | |
205 | buf = nbuf; |
190 | buf = nbuf; |
206 | res = sz; |
191 | res = sz; |
207 | } |
192 | } |
… | |
… | |
231 | |
216 | |
232 | while (n--) |
217 | while (n--) |
233 | new (buf + n) T (t); |
218 | new (buf + n) T (t); |
234 | } |
219 | } |
235 | |
220 | |
236 | template<class I> |
221 | simplevec (const_iterator first, const_iterator last) |
237 | simplevec (I first, I last) |
|
|
238 | { |
222 | { |
239 | sze = res = last - first; |
223 | sze = res = last - first; |
240 | buf = alloc (sze); |
224 | buf = alloc (sze); |
241 | copy (buf, first, sze); |
225 | copy (buf, first, sze, cop_new); |
242 | } |
226 | } |
243 | |
227 | |
244 | simplevec (const simplevec<T> &v) |
228 | simplevec (const simplevec<T> &v) |
245 | : sze(0), res(0), buf(0) |
229 | : sze(0), res(0), buf(0) |
246 | { |
230 | { |
247 | sze = res = v.size (); |
231 | sze = res = v.size (); |
248 | buf = alloc (sze); |
232 | buf = alloc (sze); |
249 | copy (buf, v.begin (), sze); |
233 | copy (buf, v.begin (), sze, cop_new); |
250 | } |
234 | } |
251 | |
235 | |
252 | ~simplevec () |
236 | ~simplevec () |
253 | { |
237 | { |
254 | dealloc (); |
238 | dealloc (); |
… | |
… | |
282 | reference operator [](size_type idx) { return buf[idx]; } |
266 | reference operator [](size_type idx) { return buf[idx]; } |
283 | |
267 | |
284 | const_reference at (size_type idx) const { return buf [idx]; } |
268 | const_reference at (size_type idx) const { return buf [idx]; } |
285 | reference at (size_type idx) { return buf [idx]; } |
269 | reference at (size_type idx) { return buf [idx]; } |
286 | |
270 | |
287 | template<class I> |
271 | void assign (const_iterator first, const_iterator last) |
288 | void assign (I first, I last) |
|
|
289 | { |
272 | { |
290 | swap (simplevec<T> (first, last)); |
273 | simplevec<T> v (first, last); |
|
|
274 | swap (v); |
291 | } |
275 | } |
292 | |
276 | |
293 | void assign (size_type n, const T &t) |
277 | void assign (size_type n, const T &t) |
294 | { |
278 | { |
295 | swap (simplevec<T> (n, t)); |
279 | simplevec<T> v (n, t); |
|
|
280 | swap (v); |
296 | } |
281 | } |
297 | |
282 | |
298 | simplevec<T> &operator= (const simplevec<T> &v) |
283 | simplevec<T> &operator= (const simplevec<T> &v) |
299 | { |
284 | { |
300 | assign (v.begin (), v.end ()); |
285 | assign (v.begin (), v.end ()); |
… | |
… | |
309 | buf [at] = t; |
294 | buf [at] = t; |
310 | |
295 | |
311 | return buf + at; |
296 | return buf + at; |
312 | } |
297 | } |
313 | |
298 | |
314 | template<class I> |
|
|
315 | iterator insert (iterator pos, I first, I last) |
299 | iterator insert (iterator pos, const_iterator first, const_iterator last) |
316 | { |
300 | { |
317 | size_type n = last - first; |
301 | size_type n = last - first; |
318 | size_type at = pos - begin (); |
302 | size_type at = pos - begin (); |
319 | |
303 | |
320 | ins (pos, n); |
304 | ins (pos, n); |
… | |
… | |
333 | *i++ = t; |
317 | *i++ = t; |
334 | |
318 | |
335 | return buf + at; |
319 | return buf + at; |
336 | } |
320 | } |
337 | |
321 | |
338 | void erase (iterator first, iterator last) |
322 | iterator erase (iterator first, iterator last) |
339 | { |
323 | { |
340 | size_t n = last - first; |
324 | size_type n = last - first; |
|
|
325 | size_type c = end () - last; |
341 | |
326 | |
342 | copy_lower (last, first, end () - last, cop_set); |
327 | if (is_simple_enough ()) |
|
|
328 | memmove (first, last, sizeof (T) * c); |
|
|
329 | else |
|
|
330 | copy (first, last, c, cop_set); |
|
|
331 | |
343 | sze -= n; |
332 | sze -= n; |
344 | destruct (buf + sze, n); |
333 | destruct (buf + sze, n); |
345 | } |
|
|
346 | |
334 | |
|
|
335 | return first; |
|
|
336 | } |
|
|
337 | |
347 | void erase (iterator pos) |
338 | iterator erase (iterator pos) |
348 | { |
339 | { |
349 | if (pos != end ()) |
340 | if (pos != end ()) |
350 | erase (pos, pos + 1); |
341 | erase (pos, pos + 1); |
|
|
342 | |
|
|
343 | return pos; |
351 | } |
344 | } |
352 | }; |
345 | }; |
353 | |
346 | |
354 | template<class T> |
347 | template<class T> |
355 | bool operator ==(const simplevec<T> &v1, const simplevec<T> &v2) |
348 | bool operator ==(const simplevec<T> &v1, const simplevec<T> &v2) |