… | |
… | |
98 | template<class I> |
98 | template<class I> |
99 | static void cop_new (iterator a, I b) { new (a) T (*b); } |
99 | static void cop_new (iterator a, I b) { new (a) T (*b); } |
100 | template<class I> |
100 | template<class I> |
101 | static void cop_set (iterator a, I b) { *a = *b ; } |
101 | static void cop_set (iterator a, I b) { *a = *b ; } |
102 | |
102 | |
|
|
103 | // MUST copy forwards |
103 | template<class I> |
104 | template<class I> |
104 | static void copy_lower (iterator dst, I src, size_type n, void (*op)(iterator, I ) = cop_new) |
105 | static void copy (iterator dst, I src, size_type n, void (*op)(iterator, I)) |
105 | { |
106 | { |
106 | while (n--) |
107 | while (n--) |
107 | op (dst++, src++); |
108 | op (dst++, src++); |
108 | } |
109 | } |
109 | |
110 | |
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) |
111 | static void copy (iterator dst, iterator src, size_type n, void (*op)(iterator, iterator)) |
134 | { |
112 | { |
135 | if (is_simple_enough ()) |
113 | if (is_simple_enough ()) |
136 | memcpy (dst, src, sizeof (T) * n); |
114 | memcpy (dst, src, sizeof (T) * n); |
137 | else |
115 | else |
138 | copy<iterator> (dst, src, n, op); |
116 | copy<iterator> (dst, src, n, op); |
… | |
… | |
167 | dealloc (); |
145 | dealloc (); |
168 | buf = nbuf; |
146 | buf = nbuf; |
169 | } |
147 | } |
170 | |
148 | |
171 | construct (buf + sze, n); |
149 | construct (buf + sze, n); |
172 | copy_higher (buf + pos + n, buf + pos, sze - pos, cop_set); |
150 | |
173 | sze += n; |
151 | sze += n; |
|
|
152 | |
|
|
153 | iterator src = buf + pos; |
|
|
154 | if (is_simple_enough ()) |
|
|
155 | memmove (src + n, src, sizeof (T) * n); |
|
|
156 | else |
|
|
157 | for (size_type i = n; i--; ) |
|
|
158 | op (src + n + i, src + i); |
174 | } |
159 | } |
175 | |
160 | |
176 | public: |
161 | public: |
177 | size_type capacity () const { return res; } |
162 | size_type capacity () const { return res; } |
178 | size_type size () const { return sze; } |
163 | size_type size () const { return sze; } |
… | |
… | |
198 | return; |
183 | return; |
199 | |
184 | |
200 | sz = good_size (sz); |
185 | sz = good_size (sz); |
201 | T *nbuf = alloc (sz); |
186 | T *nbuf = alloc (sz); |
202 | |
187 | |
203 | copy (nbuf, begin (), sze); |
188 | copy (nbuf, begin (), sze, cop_new); |
204 | dealloc (); |
189 | dealloc (); |
205 | |
190 | |
206 | buf = nbuf; |
191 | buf = nbuf; |
207 | res = sz; |
192 | res = sz; |
208 | } |
193 | } |
… | |
… | |
237 | template<class I> |
222 | template<class I> |
238 | simplevec (I first, I last) |
223 | simplevec (I first, I last) |
239 | { |
224 | { |
240 | sze = res = last - first; |
225 | sze = res = last - first; |
241 | buf = alloc (sze); |
226 | buf = alloc (sze); |
242 | copy (buf, first, sze); |
227 | copy (buf, first, sze, cop_new); |
243 | } |
228 | } |
244 | |
229 | |
245 | simplevec (const simplevec<T> &v) |
230 | simplevec (const simplevec<T> &v) |
246 | : sze(0), res(0), buf(0) |
231 | : sze(0), res(0), buf(0) |
247 | { |
232 | { |
248 | sze = res = v.size (); |
233 | sze = res = v.size (); |
249 | buf = alloc (sze); |
234 | buf = alloc (sze); |
250 | copy (buf, v.begin (), sze); |
235 | copy (buf, v.begin (), sze, cop_new); |
251 | } |
236 | } |
252 | |
237 | |
253 | ~simplevec () |
238 | ~simplevec () |
254 | { |
239 | { |
255 | dealloc (); |
240 | dealloc (); |
… | |
… | |
338 | |
323 | |
339 | void erase (iterator first, iterator last) |
324 | void erase (iterator first, iterator last) |
340 | { |
325 | { |
341 | size_t n = last - first; |
326 | size_t n = last - first; |
342 | |
327 | |
343 | copy_lower (last, first, end () - last, cop_set); |
328 | if (is_simple_enough ()) |
|
|
329 | memmove (first, last, sizeof (T) * n); |
|
|
330 | else |
|
|
331 | copy<iterator> (first, last, n, cop_new); |
|
|
332 | |
344 | sze -= n; |
333 | sze -= n; |
345 | destruct (buf + sze, n); |
334 | destruct (buf + sze, n); |
346 | } |
335 | } |
347 | |
336 | |
348 | void erase (iterator pos) |
337 | void erase (iterator pos) |