… | |
… | |
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 | |
|
|
102 | // MUST copy forwards |
103 | template<class I> |
103 | template<class I> |
104 | 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)) |
105 | { |
105 | { |
106 | while (n--) |
106 | while (n--) |
107 | op (dst++, src++); |
107 | op (dst++, src++); |
108 | } |
108 | } |
109 | |
109 | |
110 | static void copy_lower (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 | copy_lower<iterator> (dst, src, n, cop_new); |
|
|
116 | } |
|
|
117 | |
|
|
118 | static void copy_higher (iterator dst, iterator src, size_type n, void (*op)(iterator, iterator) = cop_new) |
|
|
119 | { |
|
|
120 | if (is_simple_enough ()) |
|
|
121 | memmove (dst, src, sizeof (T) * n); |
|
|
122 | else |
|
|
123 | while (n--) |
|
|
124 | op (dst + n, src + n); |
|
|
125 | } |
|
|
126 | |
|
|
127 | template<class I> |
|
|
128 | static void copy (iterator dst, I src, size_type n, void (*op)(iterator, I) = cop_new) |
|
|
129 | { |
|
|
130 | copy_lower<I> (dst, src, n, op); |
|
|
131 | } |
|
|
132 | |
|
|
133 | 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)) |
134 | { |
111 | { |
135 | if (is_simple_enough ()) |
112 | if (is_simple_enough ()) |
136 | memcpy (dst, src, sizeof (T) * n); |
113 | memcpy (dst, src, sizeof (T) * n); |
137 | else |
114 | else |
138 | copy<iterator> (dst, src, n, op); |
115 | copy<iterator> (dst, src, n, op); |
… | |
… | |
167 | dealloc (); |
144 | dealloc (); |
168 | buf = nbuf; |
145 | buf = nbuf; |
169 | } |
146 | } |
170 | |
147 | |
171 | construct (buf + sze, n); |
148 | construct (buf + sze, n); |
172 | copy_higher (buf + pos + n, buf + pos, sze - pos, cop_set); |
149 | |
173 | 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); |
174 | } |
158 | } |
175 | |
159 | |
176 | public: |
160 | public: |
177 | size_type capacity () const { return res; } |
161 | size_type capacity () const { return res; } |
178 | size_type size () const { return sze; } |
162 | size_type size () const { return sze; } |
… | |
… | |
198 | return; |
182 | return; |
199 | |
183 | |
200 | sz = good_size (sz); |
184 | sz = good_size (sz); |
201 | T *nbuf = alloc (sz); |
185 | T *nbuf = alloc (sz); |
202 | |
186 | |
203 | copy (nbuf, begin (), sze); |
187 | copy (nbuf, begin (), sze, cop_new); |
204 | dealloc (); |
188 | dealloc (); |
205 | |
189 | |
206 | buf = nbuf; |
190 | buf = nbuf; |
207 | res = sz; |
191 | res = sz; |
208 | } |
192 | } |
… | |
… | |
237 | template<class I> |
221 | template<class I> |
238 | simplevec (I first, I last) |
222 | simplevec (I first, I last) |
239 | { |
223 | { |
240 | sze = res = last - first; |
224 | sze = res = last - first; |
241 | buf = alloc (sze); |
225 | buf = alloc (sze); |
242 | copy (buf, first, sze); |
226 | copy (buf, first, sze, cop_new); |
243 | } |
227 | } |
244 | |
228 | |
245 | simplevec (const simplevec<T> &v) |
229 | simplevec (const simplevec<T> &v) |
246 | : sze(0), res(0), buf(0) |
230 | : sze(0), res(0), buf(0) |
247 | { |
231 | { |
248 | sze = res = v.size (); |
232 | sze = res = v.size (); |
249 | buf = alloc (sze); |
233 | buf = alloc (sze); |
250 | copy (buf, v.begin (), sze); |
234 | copy (buf, v.begin (), sze, cop_new); |
251 | } |
235 | } |
252 | |
236 | |
253 | ~simplevec () |
237 | ~simplevec () |
254 | { |
238 | { |
255 | dealloc (); |
239 | dealloc (); |
… | |
… | |
334 | *i++ = t; |
318 | *i++ = t; |
335 | |
319 | |
336 | return buf + at; |
320 | return buf + at; |
337 | } |
321 | } |
338 | |
322 | |
339 | void erase (iterator first, iterator last) |
323 | iterator erase (iterator first, iterator last) |
340 | { |
324 | { |
341 | size_t n = last - first; |
325 | size_type n = last - first; |
|
|
326 | size_type c = end () - last; |
342 | |
327 | |
343 | copy_lower (last, first, end () - last, cop_set); |
328 | if (is_simple_enough ()) |
|
|
329 | memmove (first, last, sizeof (T) * c); |
|
|
330 | else |
|
|
331 | copy<iterator> (first, last, c, cop_set); |
|
|
332 | |
344 | sze -= n; |
333 | sze -= n; |
345 | destruct (buf + sze, n); |
334 | destruct (buf + sze, n); |
346 | } |
|
|
347 | |
335 | |
|
|
336 | return first; |
|
|
337 | } |
|
|
338 | |
348 | void erase (iterator pos) |
339 | iterator erase (iterator pos) |
349 | { |
340 | { |
350 | if (pos != end ()) |
341 | if (pos != end ()) |
351 | erase (pos, pos + 1); |
342 | erase (pos, pos + 1); |
|
|
343 | |
|
|
344 | return pos; |
352 | } |
345 | } |
353 | }; |
346 | }; |
354 | |
347 | |
355 | template<class T> |
348 | template<class T> |
356 | bool operator ==(const simplevec<T> &v1, const simplevec<T> &v2) |
349 | bool operator ==(const simplevec<T> &v1, const simplevec<T> &v2) |