… | |
… | |
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 ()) |
93 | while (n--) |
93 | while (n--) |
94 | (*a++).~T (); |
94 | (*a++).~T (); |
95 | } |
95 | } |
96 | |
96 | |
|
|
97 | template<class I> |
97 | static void cop_new (iterator a, iterator b) { new (a) T (*b); } |
98 | static void cop_new (iterator a, I b) { new (a) T (*b); } |
|
|
99 | template<class I> |
98 | static void cop_set (iterator a, iterator b) { *a = *b ; } |
100 | static void cop_set (iterator a, I b) { *a = *b ; } |
99 | |
101 | |
100 | // these copy helpers actually use the copy constructor, not assignment |
102 | // MUST copy forwards |
|
|
103 | template<class I> |
101 | static void copy_lower (iterator dst, iterator src, size_type n, void (*op)(iterator, iterator) = cop_new) |
104 | static void copy (iterator dst, I src, size_type n, void (*op)(iterator, I)) |
102 | { |
105 | { |
103 | if (is_simple_enough ()) |
|
|
104 | memmove (dst, src, sizeof (T) * n); |
|
|
105 | else |
|
|
106 | while (n--) |
106 | while (n--) |
107 | op (dst++, src++); |
107 | op (dst++, src++); |
108 | } |
108 | } |
109 | |
109 | |
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) |
110 | static void copy (iterator dst, iterator src, size_type n, void (*op)(iterator, iterator)) |
120 | { |
111 | { |
121 | if (is_simple_enough ()) |
112 | if (is_simple_enough ()) |
122 | memcpy (dst, src, sizeof (T) * n); |
113 | memcpy (dst, src, sizeof (T) * n); |
123 | else |
114 | else |
124 | copy_lower (dst, src, n, op); |
115 | copy<iterator> (dst, src, n, op); |
125 | } |
116 | } |
126 | |
117 | |
127 | static T *alloc (size_type n) ecb_cold |
118 | static T *alloc (size_type n) ecb_cold |
128 | { |
119 | { |
129 | return (T *)::operator new ((size_t) (sizeof (T) * n)); |
120 | return (T *)::operator new ((size_t) (sizeof (T) * n)); |
… | |
… | |
153 | dealloc (); |
144 | dealloc (); |
154 | buf = nbuf; |
145 | buf = nbuf; |
155 | } |
146 | } |
156 | |
147 | |
157 | construct (buf + sze, n); |
148 | construct (buf + sze, n); |
158 | 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 | |
159 | sze += n; |
157 | sze += n; |
160 | } |
158 | } |
161 | |
159 | |
162 | public: |
160 | public: |
163 | size_type capacity () const { return res; } |
161 | size_type capacity () const { return res; } |
164 | size_type size () const { return sze; } |
162 | size_type size () const { return sze; } |
165 | bool empty () const { return size () == 0; } |
163 | bool empty () const { return size () == 0; } |
|
|
164 | |
|
|
165 | size_t max_size () const |
|
|
166 | { |
|
|
167 | return (~(size_type)0) >> 1; |
|
|
168 | } |
166 | |
169 | |
167 | const_iterator begin () const { return &buf [ 0]; } |
170 | const_iterator begin () const { return &buf [ 0]; } |
168 | iterator begin () { return &buf [ 0]; } |
171 | iterator begin () { return &buf [ 0]; } |
169 | const_iterator end () const { return &buf [sze ]; } |
172 | const_iterator end () const { return &buf [sze ]; } |
170 | iterator end () { return &buf [sze ]; } |
173 | iterator end () { return &buf [sze ]; } |
… | |
… | |
179 | return; |
182 | return; |
180 | |
183 | |
181 | sz = good_size (sz); |
184 | sz = good_size (sz); |
182 | T *nbuf = alloc (sz); |
185 | T *nbuf = alloc (sz); |
183 | |
186 | |
184 | copy (nbuf, begin (), sze); |
187 | copy (nbuf, begin (), sze, cop_new); |
185 | dealloc (); |
188 | dealloc (); |
186 | |
189 | |
187 | buf = nbuf; |
190 | buf = nbuf; |
188 | res = sz; |
191 | res = sz; |
189 | } |
192 | } |
… | |
… | |
205 | : sze(0), res(0), buf(0) |
208 | : sze(0), res(0), buf(0) |
206 | { |
209 | { |
207 | } |
210 | } |
208 | |
211 | |
209 | simplevec (size_type n, const T &t = T ()) |
212 | simplevec (size_type n, const T &t = T ()) |
210 | : sze(0), res(0), buf(0) |
|
|
211 | { |
213 | { |
212 | insert (begin (), n, t); |
214 | sze = res = n; |
|
|
215 | buf = alloc (sze); |
|
|
216 | |
|
|
217 | while (n--) |
|
|
218 | new (buf + n) T (t); |
213 | } |
219 | } |
214 | |
220 | |
215 | simplevec (const_iterator first, const_iterator last) |
221 | simplevec (const_iterator first, const_iterator last) |
216 | : sze(0), res(0), buf(0) |
|
|
217 | { |
222 | { |
218 | insert (begin (), first, last); |
223 | sze = res = last - first; |
|
|
224 | buf = alloc (sze); |
|
|
225 | copy (buf, first, sze, cop_new); |
219 | } |
226 | } |
220 | |
227 | |
221 | simplevec (const simplevec<T> &v) |
228 | simplevec (const simplevec<T> &v) |
222 | : sze(0), res(0), buf(0) |
229 | : sze(0), res(0), buf(0) |
223 | { |
230 | { |
224 | insert (begin (), v.begin (), v.end ()); |
231 | sze = res = v.size (); |
225 | } |
232 | buf = alloc (sze); |
226 | |
233 | 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 | } |
234 | } |
232 | |
235 | |
233 | ~simplevec () |
236 | ~simplevec () |
234 | { |
237 | { |
235 | dealloc (); |
238 | dealloc (); |
… | |
… | |
257 | void pop_back () |
260 | void pop_back () |
258 | { |
261 | { |
259 | destruct (buf + --sze); |
262 | destruct (buf + --sze); |
260 | } |
263 | } |
261 | |
264 | |
262 | const T &operator [](size_type idx) const { return buf[idx]; } |
265 | const_reference operator [](size_type idx) const { return buf[idx]; } |
263 | T &operator [](size_type idx) { return buf[idx]; } |
266 | reference operator [](size_type idx) { return buf[idx]; } |
|
|
267 | |
|
|
268 | const_reference at (size_type idx) const { return buf [idx]; } |
|
|
269 | reference at (size_type idx) { return buf [idx]; } |
|
|
270 | |
|
|
271 | void assign (const_iterator first, const_iterator last) |
|
|
272 | { |
|
|
273 | simplevec<T> v (first, last); |
|
|
274 | swap (v); |
|
|
275 | } |
|
|
276 | |
|
|
277 | void assign (size_type n, const T &t) |
|
|
278 | { |
|
|
279 | simplevec<T> v (n, t); |
|
|
280 | swap (v); |
|
|
281 | } |
|
|
282 | |
|
|
283 | simplevec<T> &operator= (const simplevec<T> &v) |
|
|
284 | { |
|
|
285 | assign (v.begin (), v.end ()); |
|
|
286 | return *this; |
|
|
287 | } |
264 | |
288 | |
265 | iterator insert (iterator pos, const T &t) |
289 | iterator insert (iterator pos, const T &t) |
266 | { |
290 | { |
267 | size_type at = pos - begin (); |
291 | size_type at = pos - begin (); |
268 | |
292 | |
… | |
… | |
293 | *i++ = t; |
317 | *i++ = t; |
294 | |
318 | |
295 | return buf + at; |
319 | return buf + at; |
296 | } |
320 | } |
297 | |
321 | |
298 | void erase (iterator first, iterator last) |
322 | iterator erase (iterator first, iterator last) |
299 | { |
323 | { |
300 | size_t n = last - first; |
324 | size_type n = last - first; |
|
|
325 | size_type c = end () - last; |
301 | |
326 | |
302 | 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 | |
303 | sze -= n; |
332 | sze -= n; |
304 | destruct (buf + sze, n); |
333 | destruct (buf + sze, n); |
305 | } |
|
|
306 | |
334 | |
|
|
335 | return first; |
|
|
336 | } |
|
|
337 | |
307 | void erase (iterator pos) |
338 | iterator erase (iterator pos) |
308 | { |
339 | { |
309 | if (pos != end ()) |
340 | if (pos != end ()) |
310 | erase (pos, pos + 1); |
341 | erase (pos, pos + 1); |
|
|
342 | |
|
|
343 | return pos; |
311 | } |
344 | } |
312 | }; |
345 | }; |
313 | |
346 | |
314 | template<class T> |
347 | template<class T> |
315 | bool operator ==(const simplevec<T> &v1, const simplevec<T> &v2) |
348 | bool operator ==(const simplevec<T> &v1, const simplevec<T> &v2) |