… | |
… | |
63 | |
63 | |
64 | // we shamelessly optimise for "simple" types. everything |
64 | // we shamelessly optimise for "simple" types. everything |
65 | // "not simple enough" will use the slow path. |
65 | // "not simple enough" will use the slow path. |
66 | static bool is_simple_enough () |
66 | static bool is_simple_enough () |
67 | { |
67 | { |
68 | return 1; // we are not there yet |
|
|
69 | #if __cplusplus >= 201103L |
68 | #if __cplusplus >= 201103L |
70 | return std::is_trivially_assignable<T, T>::value |
69 | return std::is_trivially_assignable<T, T>::value |
71 | && std::is_trivially_constructable<T>::value |
70 | && std::is_trivially_constructable<T>::value |
72 | && std::is_trivially_copyable<T>::value |
71 | && std::is_trivially_copyable<T>::value |
73 | && std::is_trivially_destructible<T>::value; |
72 | && std::is_trivially_destructible<T>::value; |
… | |
… | |
83 | |
82 | |
84 | static void construct (iterator a, size_type n = 1) |
83 | static void construct (iterator a, size_type n = 1) |
85 | { |
84 | { |
86 | if (!is_simple_enough ()) |
85 | if (!is_simple_enough ()) |
87 | while (n--) |
86 | while (n--) |
88 | new (*a++) T (); |
87 | new (a++) T (); |
89 | } |
88 | } |
90 | |
89 | |
91 | static void destruct (iterator a, size_type n = 1) |
90 | static void destruct (iterator a, size_type n = 1) |
92 | { |
91 | { |
93 | if (!is_simple_enough ()) |
92 | if (!is_simple_enough ()) |
… | |
… | |
98 | template<class I> |
97 | template<class I> |
99 | 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); } |
100 | template<class I> |
99 | template<class I> |
101 | static void cop_set (iterator a, I b) { *a = *b ; } |
100 | static void cop_set (iterator a, I b) { *a = *b ; } |
102 | |
101 | |
103 | static void copy_higher (iterator dst, iterator src, size_type n, void (*op)(iterator, iterator) = cop_new) |
102 | // 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> |
103 | template<class I> |
113 | static void copy (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)) |
114 | { |
105 | { |
115 | while (n--) |
106 | while (n--) |
116 | op (dst++, src++); |
107 | op (dst++, src++); |
117 | } |
108 | } |
118 | |
109 | |
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<iterator> (dst, src, n, op); |
115 | copy<iterator> (dst, src, n, op); |
… | |
… | |
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 | |
159 | sze += n; |
150 | sze += n; |
|
|
151 | |
|
|
152 | iterator src = buf + pos; |
|
|
153 | if (is_simple_enough ()) |
|
|
154 | memmove (src + n, src, sizeof (T) * n); |
|
|
155 | else |
|
|
156 | for (size_type i = n; i--; ) |
|
|
157 | cop_set (src + n + i, src + i); |
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; } |
… | |
… | |
184 | return; |
182 | return; |
185 | |
183 | |
186 | sz = good_size (sz); |
184 | sz = good_size (sz); |
187 | T *nbuf = alloc (sz); |
185 | T *nbuf = alloc (sz); |
188 | |
186 | |
189 | copy (nbuf, begin (), sze); |
187 | copy (nbuf, begin (), sze, cop_new); |
190 | dealloc (); |
188 | dealloc (); |
191 | |
189 | |
192 | buf = nbuf; |
190 | buf = nbuf; |
193 | res = sz; |
191 | res = sz; |
194 | } |
192 | } |
… | |
… | |
223 | template<class I> |
221 | template<class I> |
224 | simplevec (I first, I last) |
222 | simplevec (I first, I last) |
225 | { |
223 | { |
226 | sze = res = last - first; |
224 | sze = res = last - first; |
227 | buf = alloc (sze); |
225 | buf = alloc (sze); |
228 | copy (buf, first, sze); |
226 | copy (buf, first, sze, cop_new); |
229 | } |
227 | } |
230 | |
228 | |
231 | simplevec (const simplevec<T> &v) |
229 | simplevec (const simplevec<T> &v) |
232 | : sze(0), res(0), buf(0) |
230 | : sze(0), res(0), buf(0) |
233 | { |
231 | { |
234 | sze = res = v.size (); |
232 | sze = res = v.size (); |
235 | buf = alloc (sze); |
233 | buf = alloc (sze); |
236 | copy (buf, v.begin (), sze); |
234 | copy (buf, v.begin (), sze, cop_new); |
237 | } |
235 | } |
238 | |
236 | |
239 | ~simplevec () |
237 | ~simplevec () |
240 | { |
238 | { |
241 | dealloc (); |
239 | dealloc (); |
… | |
… | |
320 | *i++ = t; |
318 | *i++ = t; |
321 | |
319 | |
322 | return buf + at; |
320 | return buf + at; |
323 | } |
321 | } |
324 | |
322 | |
325 | void erase (iterator first, iterator last) |
323 | iterator erase (iterator first, iterator last) |
326 | { |
324 | { |
327 | size_t n = last - first; |
325 | size_t n = last - first; |
328 | |
326 | |
329 | if (is_simple_enough ()) |
327 | if (is_simple_enough ()) |
330 | memmove (first, last, sizeof (T) * n); |
328 | memmove (first, last, sizeof (T) * n); |
331 | else |
329 | else |
332 | copy<iterator> (first, last, n, cop_new); |
330 | copy<iterator> (first, last, n, cop_set); |
333 | |
331 | |
334 | sze -= n; |
332 | sze -= n; |
335 | destruct (buf + sze, n); |
333 | destruct (buf + sze, n); |
336 | } |
|
|
337 | |
334 | |
|
|
335 | return first; |
|
|
336 | } |
|
|
337 | |
338 | void erase (iterator pos) |
338 | iterator erase (iterator pos) |
339 | { |
339 | { |
340 | if (pos != end ()) |
340 | if (pos != end ()) |
341 | erase (pos, pos + 1); |
341 | erase (pos, pos + 1); |
|
|
342 | |
|
|
343 | return pos; |
342 | } |
344 | } |
343 | }; |
345 | }; |
344 | |
346 | |
345 | template<class T> |
347 | template<class T> |
346 | bool operator ==(const simplevec<T> &v1, const simplevec<T> &v2) |
348 | bool operator ==(const simplevec<T> &v1, const simplevec<T> &v2) |