]>
Commit | Line | Data |
---|---|---|
83144cfc PE |
1 | // Vector implementation (out of line) -*- C++ -*- |
2 | ||
d2219f89 | 3 | // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008 |
232c4925 | 4 | // Free Software Foundation, Inc. |
83144cfc PE |
5 | // |
6 | // This file is part of the GNU ISO C++ Library. This library is free | |
7 | // software; you can redistribute it and/or modify it under the | |
8 | // terms of the GNU General Public License as published by the | |
9 | // Free Software Foundation; either version 2, or (at your option) | |
10 | // any later version. | |
11 | ||
12 | // This library is distributed in the hope that it will be useful, | |
13 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | // GNU General Public License for more details. | |
16 | ||
17 | // You should have received a copy of the GNU General Public License along | |
18 | // with this library; see the file COPYING. If not, write to the Free | |
83f51799 | 19 | // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, |
83144cfc PE |
20 | // USA. |
21 | ||
22 | // As a special exception, you may use this file as part of a free software | |
23 | // library without restriction. Specifically, if other files instantiate | |
24 | // templates or use macros or inline functions from this file, or you compile | |
25 | // this file and link it with other files to produce an executable, this | |
26 | // file does not by itself cause the resulting executable to be covered by | |
27 | // the GNU General Public License. This exception does not however | |
28 | // invalidate any other reasons why the executable file might be covered by | |
29 | // the GNU General Public License. | |
30 | ||
31 | /* | |
32 | * | |
33 | * Copyright (c) 1994 | |
34 | * Hewlett-Packard Company | |
35 | * | |
36 | * Permission to use, copy, modify, distribute and sell this software | |
37 | * and its documentation for any purpose is hereby granted without fee, | |
38 | * provided that the above copyright notice appear in all copies and | |
39 | * that both that copyright notice and this permission notice appear | |
40 | * in supporting documentation. Hewlett-Packard Company makes no | |
41 | * representations about the suitability of this software for any | |
42 | * purpose. It is provided "as is" without express or implied warranty. | |
43 | * | |
44 | * | |
45 | * Copyright (c) 1996 | |
46 | * Silicon Graphics Computer Systems, Inc. | |
47 | * | |
48 | * Permission to use, copy, modify, distribute and sell this software | |
49 | * and its documentation for any purpose is hereby granted without fee, | |
50 | * provided that the above copyright notice appear in all copies and | |
51 | * that both that copyright notice and this permission notice appear | |
52 | * in supporting documentation. Silicon Graphics makes no | |
53 | * representations about the suitability of this software for any | |
54 | * purpose. It is provided "as is" without express or implied warranty. | |
55 | */ | |
56 | ||
57 | /** @file vector.tcc | |
58 | * This is an internal header file, included by other library headers. | |
59 | * You should not attempt to use it directly. | |
60 | */ | |
61 | ||
3d7c150e BK |
62 | #ifndef _VECTOR_TCC |
63 | #define _VECTOR_TCC 1 | |
83144cfc | 64 | |
c2ba9709 | 65 | _GLIBCXX_BEGIN_NESTED_NAMESPACE(std, _GLIBCXX_STD_D) |
3cbc7af0 | 66 | |
af5fb6ab | 67 | template<typename _Tp, typename _Alloc> |
3971a4d2 | 68 | void |
874e7baa | 69 | vector<_Tp, _Alloc>:: |
3971a4d2 | 70 | reserve(size_type __n) |
83144cfc | 71 | { |
48d1c3c5 | 72 | if (__n > this->max_size()) |
ba9119ec | 73 | __throw_length_error(__N("vector::reserve")); |
48d1c3c5 BK |
74 | if (this->capacity() < __n) |
75 | { | |
76 | const size_type __old_size = size(); | |
245a5fe5 PC |
77 | pointer __tmp = _M_allocate_and_copy(__n, |
78 | _GLIBCXX_MAKE_MOVE_ITERATOR(this->_M_impl._M_start), | |
79 | _GLIBCXX_MAKE_MOVE_ITERATOR(this->_M_impl._M_finish)); | |
1985f1cd | 80 | std::_Destroy(this->_M_impl._M_start, this->_M_impl._M_finish, |
4fd20a8f | 81 | _M_get_Tp_allocator()); |
03f9ea44 | 82 | _M_deallocate(this->_M_impl._M_start, |
874e7baa PC |
83 | this->_M_impl._M_end_of_storage |
84 | - this->_M_impl._M_start); | |
03f9ea44 DM |
85 | this->_M_impl._M_start = __tmp; |
86 | this->_M_impl._M_finish = __tmp + __old_size; | |
87 | this->_M_impl._M_end_of_storage = this->_M_impl._M_start + __n; | |
48d1c3c5 | 88 | } |
3971a4d2 | 89 | } |
ed6814f7 | 90 | |
af5fb6ab | 91 | template<typename _Tp, typename _Alloc> |
874e7baa PC |
92 | typename vector<_Tp, _Alloc>::iterator |
93 | vector<_Tp, _Alloc>:: | |
3971a4d2 PE |
94 | insert(iterator __position, const value_type& __x) |
95 | { | |
43da93a7 | 96 | const size_type __n = __position - begin(); |
874e7baa PC |
97 | if (this->_M_impl._M_finish != this->_M_impl._M_end_of_storage |
98 | && __position == end()) | |
99 | { | |
1985f1cd | 100 | this->_M_impl.construct(this->_M_impl._M_finish, __x); |
874e7baa PC |
101 | ++this->_M_impl._M_finish; |
102 | } | |
83144cfc | 103 | else |
812e8c79 PC |
104 | { |
105 | #ifdef __GXX_EXPERIMENTAL_CXX0X__ | |
106 | if (this->_M_impl._M_finish != this->_M_impl._M_end_of_storage) | |
107 | { | |
108 | _Tp __x_copy = __x; | |
109 | _M_insert_aux(__position, std::move(__x_copy)); | |
110 | } | |
111 | else | |
112 | #endif | |
113 | _M_insert_aux(__position, __x); | |
114 | } | |
bc9053ab | 115 | return iterator(this->_M_impl._M_start + __n); |
83144cfc | 116 | } |
ed6814f7 | 117 | |
af5fb6ab | 118 | template<typename _Tp, typename _Alloc> |
874e7baa PC |
119 | typename vector<_Tp, _Alloc>::iterator |
120 | vector<_Tp, _Alloc>:: | |
3971a4d2 | 121 | erase(iterator __position) |
83144cfc | 122 | { |
3971a4d2 | 123 | if (__position + 1 != end()) |
245a5fe5 | 124 | _GLIBCXX_MOVE3(__position + 1, end(), __position); |
03f9ea44 | 125 | --this->_M_impl._M_finish; |
1985f1cd | 126 | this->_M_impl.destroy(this->_M_impl._M_finish); |
3971a4d2 | 127 | return __position; |
83144cfc | 128 | } |
ed6814f7 | 129 | |
af5fb6ab | 130 | template<typename _Tp, typename _Alloc> |
874e7baa PC |
131 | typename vector<_Tp, _Alloc>::iterator |
132 | vector<_Tp, _Alloc>:: | |
3971a4d2 | 133 | erase(iterator __first, iterator __last) |
83144cfc | 134 | { |
bc9053ab | 135 | if (__last != end()) |
245a5fe5 | 136 | _GLIBCXX_MOVE3(__last, end(), __first); |
bc9053ab | 137 | _M_erase_at_end(__first.base() + (end() - __last)); |
3971a4d2 | 138 | return __first; |
83144cfc | 139 | } |
ed6814f7 | 140 | |
af5fb6ab | 141 | template<typename _Tp, typename _Alloc> |
874e7baa PC |
142 | vector<_Tp, _Alloc>& |
143 | vector<_Tp, _Alloc>:: | |
144 | operator=(const vector<_Tp, _Alloc>& __x) | |
83144cfc | 145 | { |
3971a4d2 | 146 | if (&__x != this) |
874e7baa PC |
147 | { |
148 | const size_type __xlen = __x.size(); | |
149 | if (__xlen > capacity()) | |
150 | { | |
151 | pointer __tmp = _M_allocate_and_copy(__xlen, __x.begin(), | |
152 | __x.end()); | |
1985f1cd | 153 | std::_Destroy(this->_M_impl._M_start, this->_M_impl._M_finish, |
4fd20a8f | 154 | _M_get_Tp_allocator()); |
874e7baa PC |
155 | _M_deallocate(this->_M_impl._M_start, |
156 | this->_M_impl._M_end_of_storage | |
157 | - this->_M_impl._M_start); | |
158 | this->_M_impl._M_start = __tmp; | |
159 | this->_M_impl._M_end_of_storage = this->_M_impl._M_start + __xlen; | |
160 | } | |
161 | else if (size() >= __xlen) | |
162 | { | |
bc9053ab PC |
163 | std::_Destroy(std::copy(__x.begin(), __x.end(), begin()), |
164 | end(), _M_get_Tp_allocator()); | |
874e7baa PC |
165 | } |
166 | else | |
167 | { | |
bc9053ab | 168 | std::copy(__x._M_impl._M_start, __x._M_impl._M_start + size(), |
874e7baa | 169 | this->_M_impl._M_start); |
bc9053ab PC |
170 | std::__uninitialized_copy_a(__x._M_impl._M_start + size(), |
171 | __x._M_impl._M_finish, | |
172 | this->_M_impl._M_finish, | |
4fd20a8f | 173 | _M_get_Tp_allocator()); |
874e7baa PC |
174 | } |
175 | this->_M_impl._M_finish = this->_M_impl._M_start + __xlen; | |
176 | } | |
3971a4d2 | 177 | return *this; |
83144cfc | 178 | } |
ed6814f7 | 179 | |
af5fb6ab | 180 | template<typename _Tp, typename _Alloc> |
3971a4d2 | 181 | void |
874e7baa | 182 | vector<_Tp, _Alloc>:: |
3971a4d2 | 183 | _M_fill_assign(size_t __n, const value_type& __val) |
83144cfc | 184 | { |
3971a4d2 | 185 | if (__n > capacity()) |
874e7baa | 186 | { |
4fd20a8f | 187 | vector __tmp(__n, __val, _M_get_Tp_allocator()); |
874e7baa PC |
188 | __tmp.swap(*this); |
189 | } | |
3971a4d2 | 190 | else if (__n > size()) |
874e7baa PC |
191 | { |
192 | std::fill(begin(), end(), __val); | |
1985f1cd MA |
193 | std::__uninitialized_fill_n_a(this->_M_impl._M_finish, |
194 | __n - size(), __val, | |
4fd20a8f | 195 | _M_get_Tp_allocator()); |
368b7a30 | 196 | this->_M_impl._M_finish += __n - size(); |
874e7baa | 197 | } |
3971a4d2 | 198 | else |
bc9053ab | 199 | _M_erase_at_end(std::fill_n(this->_M_impl._M_start, __n, __val)); |
83144cfc | 200 | } |
ed6814f7 | 201 | |
874e7baa PC |
202 | template<typename _Tp, typename _Alloc> |
203 | template<typename _InputIterator> | |
204 | void | |
205 | vector<_Tp, _Alloc>:: | |
206 | _M_assign_aux(_InputIterator __first, _InputIterator __last, | |
6323b34e | 207 | std::input_iterator_tag) |
3971a4d2 | 208 | { |
bc9053ab PC |
209 | pointer __cur(this->_M_impl._M_start); |
210 | for (; __first != __last && __cur != this->_M_impl._M_finish; | |
211 | ++__cur, ++__first) | |
874e7baa PC |
212 | *__cur = *__first; |
213 | if (__first == __last) | |
bc9053ab | 214 | _M_erase_at_end(__cur); |
874e7baa PC |
215 | else |
216 | insert(end(), __first, __last); | |
3971a4d2 | 217 | } |
874e7baa PC |
218 | |
219 | template<typename _Tp, typename _Alloc> | |
220 | template<typename _ForwardIterator> | |
221 | void | |
43da93a7 | 222 | vector<_Tp, _Alloc>:: |
874e7baa | 223 | _M_assign_aux(_ForwardIterator __first, _ForwardIterator __last, |
6323b34e | 224 | std::forward_iterator_tag) |
3971a4d2 | 225 | { |
43da93a7 | 226 | const size_type __len = std::distance(__first, __last); |
874e7baa PC |
227 | |
228 | if (__len > capacity()) | |
229 | { | |
230 | pointer __tmp(_M_allocate_and_copy(__len, __first, __last)); | |
1985f1cd | 231 | std::_Destroy(this->_M_impl._M_start, this->_M_impl._M_finish, |
4fd20a8f | 232 | _M_get_Tp_allocator()); |
874e7baa PC |
233 | _M_deallocate(this->_M_impl._M_start, |
234 | this->_M_impl._M_end_of_storage | |
235 | - this->_M_impl._M_start); | |
236 | this->_M_impl._M_start = __tmp; | |
237 | this->_M_impl._M_finish = this->_M_impl._M_start + __len; | |
238 | this->_M_impl._M_end_of_storage = this->_M_impl._M_finish; | |
239 | } | |
240 | else if (size() >= __len) | |
bc9053ab | 241 | _M_erase_at_end(std::copy(__first, __last, this->_M_impl._M_start)); |
874e7baa PC |
242 | else |
243 | { | |
244 | _ForwardIterator __mid = __first; | |
245 | std::advance(__mid, size()); | |
246 | std::copy(__first, __mid, this->_M_impl._M_start); | |
1985f1cd MA |
247 | this->_M_impl._M_finish = |
248 | std::__uninitialized_copy_a(__mid, __last, | |
249 | this->_M_impl._M_finish, | |
4fd20a8f | 250 | _M_get_Tp_allocator()); |
874e7baa | 251 | } |
3971a4d2 | 252 | } |
ed6814f7 | 253 | |
6eef7402 CJ |
254 | #ifdef __GXX_EXPERIMENTAL_CXX0X__ |
255 | template<typename _Tp, typename _Alloc> | |
256 | template<typename... _Args> | |
257 | typename vector<_Tp, _Alloc>::iterator | |
258 | vector<_Tp, _Alloc>:: | |
259 | emplace(iterator __position, _Args&&... __args) | |
260 | { | |
261 | const size_type __n = __position - begin(); | |
262 | if (this->_M_impl._M_finish != this->_M_impl._M_end_of_storage | |
263 | && __position == end()) | |
264 | { | |
265 | this->_M_impl.construct(this->_M_impl._M_finish, | |
266 | std::forward<_Args>(__args)...); | |
267 | ++this->_M_impl._M_finish; | |
268 | } | |
269 | else | |
270 | _M_insert_aux(__position, std::forward<_Args>(__args)...); | |
271 | return iterator(this->_M_impl._M_start + __n); | |
272 | } | |
273 | ||
274 | template<typename _Tp, typename _Alloc> | |
275 | template<typename... _Args> | |
276 | void | |
277 | vector<_Tp, _Alloc>:: | |
278 | _M_insert_aux(iterator __position, _Args&&... __args) | |
6eef7402 | 279 | #else |
af5fb6ab | 280 | template<typename _Tp, typename _Alloc> |
3971a4d2 | 281 | void |
43da93a7 | 282 | vector<_Tp, _Alloc>:: |
3971a4d2 | 283 | _M_insert_aux(iterator __position, const _Tp& __x) |
6eef7402 | 284 | #endif |
812e8c79 | 285 | { |
03f9ea44 | 286 | if (this->_M_impl._M_finish != this->_M_impl._M_end_of_storage) |
874e7baa | 287 | { |
1985f1cd | 288 | this->_M_impl.construct(this->_M_impl._M_finish, |
6eef7402 CJ |
289 | _GLIBCXX_MOVE(*(this->_M_impl._M_finish |
290 | - 1))); | |
874e7baa | 291 | ++this->_M_impl._M_finish; |
6eef7402 | 292 | #ifndef __GXX_EXPERIMENTAL_CXX0X__ |
874e7baa | 293 | _Tp __x_copy = __x; |
6eef7402 CJ |
294 | #endif |
295 | _GLIBCXX_MOVE_BACKWARD3(__position.base(), | |
296 | this->_M_impl._M_finish - 2, | |
297 | this->_M_impl._M_finish - 1); | |
812e8c79 PC |
298 | #ifndef __GXX_EXPERIMENTAL_CXX0X__ |
299 | *__position = __x_copy; | |
300 | #else | |
301 | *__position = _Tp(std::forward<_Args>(__args)...); | |
302 | #endif | |
874e7baa | 303 | } |
83144cfc | 304 | else |
874e7baa | 305 | { |
be1088fa ML |
306 | const size_type __len = |
307 | _M_check_len(size_type(1), "vector::_M_insert_aux"); | |
d2219f89 | 308 | const size_type __elems_before = __position - begin(); |
bc9053ab PC |
309 | pointer __new_start(this->_M_allocate(__len)); |
310 | pointer __new_finish(__new_start); | |
874e7baa PC |
311 | try |
312 | { | |
d2219f89 PC |
313 | // The order of the three operations is dictated by the C++0x |
314 | // case, where the moves could alter a new element belonging | |
315 | // to the existing vector. This is an issue only for callers | |
316 | // taking the element by const lvalue ref (see 23.1/13). | |
317 | this->_M_impl.construct(__new_start + __elems_before, | |
812e8c79 | 318 | #ifdef __GXX_EXPERIMENTAL_CXX0X__ |
812e8c79 | 319 | std::forward<_Args>(__args)...); |
d2219f89 PC |
320 | #else |
321 | __x); | |
812e8c79 | 322 | #endif |
d2219f89 PC |
323 | __new_finish = 0; |
324 | ||
1985f1cd | 325 | __new_finish = |
6eef7402 | 326 | std::__uninitialized_move_a(this->_M_impl._M_start, |
bc9053ab | 327 | __position.base(), __new_start, |
4fd20a8f | 328 | _M_get_Tp_allocator()); |
874e7baa | 329 | ++__new_finish; |
d2219f89 | 330 | |
1985f1cd | 331 | __new_finish = |
6eef7402 | 332 | std::__uninitialized_move_a(__position.base(), |
bc9053ab | 333 | this->_M_impl._M_finish, |
1985f1cd | 334 | __new_finish, |
4fd20a8f | 335 | _M_get_Tp_allocator()); |
996e5395 | 336 | } |
874e7baa PC |
337 | catch(...) |
338 | { | |
d2219f89 PC |
339 | if (!__new_finish) |
340 | this->_M_impl.destroy(__new_start + __elems_before); | |
341 | else | |
342 | std::_Destroy(__new_start, __new_finish, _M_get_Tp_allocator()); | |
bc9053ab | 343 | _M_deallocate(__new_start, __len); |
874e7baa PC |
344 | __throw_exception_again; |
345 | } | |
bc9053ab PC |
346 | std::_Destroy(this->_M_impl._M_start, this->_M_impl._M_finish, |
347 | _M_get_Tp_allocator()); | |
874e7baa PC |
348 | _M_deallocate(this->_M_impl._M_start, |
349 | this->_M_impl._M_end_of_storage | |
350 | - this->_M_impl._M_start); | |
bc9053ab PC |
351 | this->_M_impl._M_start = __new_start; |
352 | this->_M_impl._M_finish = __new_finish; | |
353 | this->_M_impl._M_end_of_storage = __new_start + __len; | |
874e7baa | 354 | } |
83144cfc | 355 | } |
64ebadac | 356 | |
af5fb6ab | 357 | template<typename _Tp, typename _Alloc> |
3971a4d2 | 358 | void |
43da93a7 | 359 | vector<_Tp, _Alloc>:: |
3971a4d2 PE |
360 | _M_fill_insert(iterator __position, size_type __n, const value_type& __x) |
361 | { | |
362 | if (__n != 0) | |
874e7baa PC |
363 | { |
364 | if (size_type(this->_M_impl._M_end_of_storage | |
365 | - this->_M_impl._M_finish) >= __n) | |
366 | { | |
367 | value_type __x_copy = __x; | |
368 | const size_type __elems_after = end() - __position; | |
bc9053ab | 369 | pointer __old_finish(this->_M_impl._M_finish); |
874e7baa PC |
370 | if (__elems_after > __n) |
371 | { | |
6eef7402 | 372 | std::__uninitialized_move_a(this->_M_impl._M_finish - __n, |
1985f1cd MA |
373 | this->_M_impl._M_finish, |
374 | this->_M_impl._M_finish, | |
4fd20a8f | 375 | _M_get_Tp_allocator()); |
874e7baa | 376 | this->_M_impl._M_finish += __n; |
6eef7402 CJ |
377 | _GLIBCXX_MOVE_BACKWARD3(__position.base(), |
378 | __old_finish - __n, __old_finish); | |
bc9053ab PC |
379 | std::fill(__position.base(), __position.base() + __n, |
380 | __x_copy); | |
874e7baa PC |
381 | } |
382 | else | |
383 | { | |
1985f1cd MA |
384 | std::__uninitialized_fill_n_a(this->_M_impl._M_finish, |
385 | __n - __elems_after, | |
386 | __x_copy, | |
4fd20a8f | 387 | _M_get_Tp_allocator()); |
874e7baa | 388 | this->_M_impl._M_finish += __n - __elems_after; |
6eef7402 | 389 | std::__uninitialized_move_a(__position.base(), __old_finish, |
1985f1cd | 390 | this->_M_impl._M_finish, |
4fd20a8f | 391 | _M_get_Tp_allocator()); |
874e7baa | 392 | this->_M_impl._M_finish += __elems_after; |
bc9053ab | 393 | std::fill(__position.base(), __old_finish, __x_copy); |
874e7baa PC |
394 | } |
395 | } | |
396 | else | |
397 | { | |
be1088fa ML |
398 | const size_type __len = |
399 | _M_check_len(__n, "vector::_M_fill_insert"); | |
d2219f89 | 400 | const size_type __elems_before = __position - begin(); |
bc9053ab PC |
401 | pointer __new_start(this->_M_allocate(__len)); |
402 | pointer __new_finish(__new_start); | |
874e7baa PC |
403 | try |
404 | { | |
d2219f89 PC |
405 | // See _M_insert_aux above. |
406 | std::__uninitialized_fill_n_a(__new_start + __elems_before, | |
407 | __n, __x, | |
408 | _M_get_Tp_allocator()); | |
409 | __new_finish = 0; | |
410 | ||
1985f1cd | 411 | __new_finish = |
6eef7402 | 412 | std::__uninitialized_move_a(this->_M_impl._M_start, |
bc9053ab | 413 | __position.base(), |
1985f1cd | 414 | __new_start, |
4fd20a8f | 415 | _M_get_Tp_allocator()); |
368b7a30 | 416 | __new_finish += __n; |
d2219f89 | 417 | |
1985f1cd | 418 | __new_finish = |
6eef7402 | 419 | std::__uninitialized_move_a(__position.base(), |
bc9053ab PC |
420 | this->_M_impl._M_finish, |
421 | __new_finish, | |
4fd20a8f | 422 | _M_get_Tp_allocator()); |
874e7baa PC |
423 | } |
424 | catch(...) | |
425 | { | |
d2219f89 PC |
426 | if (!__new_finish) |
427 | std::_Destroy(__new_start + __elems_before, | |
428 | __new_start + __elems_before + __n, | |
429 | _M_get_Tp_allocator()); | |
430 | else | |
431 | std::_Destroy(__new_start, __new_finish, | |
432 | _M_get_Tp_allocator()); | |
bc9053ab | 433 | _M_deallocate(__new_start, __len); |
874e7baa PC |
434 | __throw_exception_again; |
435 | } | |
1985f1cd | 436 | std::_Destroy(this->_M_impl._M_start, this->_M_impl._M_finish, |
4fd20a8f | 437 | _M_get_Tp_allocator()); |
874e7baa PC |
438 | _M_deallocate(this->_M_impl._M_start, |
439 | this->_M_impl._M_end_of_storage | |
440 | - this->_M_impl._M_start); | |
bc9053ab PC |
441 | this->_M_impl._M_start = __new_start; |
442 | this->_M_impl._M_finish = __new_finish; | |
443 | this->_M_impl._M_end_of_storage = __new_start + __len; | |
874e7baa PC |
444 | } |
445 | } | |
3971a4d2 | 446 | } |
ed6814f7 | 447 | |
232c4925 PC |
448 | template<typename _Tp, typename _Alloc> |
449 | template<typename _InputIterator> | |
450 | void | |
451 | vector<_Tp, _Alloc>:: | |
452 | _M_range_insert(iterator __pos, _InputIterator __first, | |
453 | _InputIterator __last, std::input_iterator_tag) | |
454 | { | |
455 | for (; __first != __last; ++__first) | |
456 | { | |
457 | __pos = insert(__pos, *__first); | |
458 | ++__pos; | |
459 | } | |
460 | } | |
ed6814f7 | 461 | |
874e7baa PC |
462 | template<typename _Tp, typename _Alloc> |
463 | template<typename _ForwardIterator> | |
464 | void | |
43da93a7 | 465 | vector<_Tp, _Alloc>:: |
996e5395 | 466 | _M_range_insert(iterator __position, _ForwardIterator __first, |
6323b34e | 467 | _ForwardIterator __last, std::forward_iterator_tag) |
3971a4d2 | 468 | { |
874e7baa PC |
469 | if (__first != __last) |
470 | { | |
43da93a7 | 471 | const size_type __n = std::distance(__first, __last); |
874e7baa PC |
472 | if (size_type(this->_M_impl._M_end_of_storage |
473 | - this->_M_impl._M_finish) >= __n) | |
474 | { | |
475 | const size_type __elems_after = end() - __position; | |
bc9053ab | 476 | pointer __old_finish(this->_M_impl._M_finish); |
874e7baa PC |
477 | if (__elems_after > __n) |
478 | { | |
6eef7402 | 479 | std::__uninitialized_move_a(this->_M_impl._M_finish - __n, |
1985f1cd MA |
480 | this->_M_impl._M_finish, |
481 | this->_M_impl._M_finish, | |
4fd20a8f | 482 | _M_get_Tp_allocator()); |
874e7baa | 483 | this->_M_impl._M_finish += __n; |
6eef7402 CJ |
484 | _GLIBCXX_MOVE_BACKWARD3(__position.base(), |
485 | __old_finish - __n, __old_finish); | |
874e7baa PC |
486 | std::copy(__first, __last, __position); |
487 | } | |
488 | else | |
489 | { | |
490 | _ForwardIterator __mid = __first; | |
491 | std::advance(__mid, __elems_after); | |
1985f1cd MA |
492 | std::__uninitialized_copy_a(__mid, __last, |
493 | this->_M_impl._M_finish, | |
4fd20a8f | 494 | _M_get_Tp_allocator()); |
874e7baa | 495 | this->_M_impl._M_finish += __n - __elems_after; |
6eef7402 | 496 | std::__uninitialized_move_a(__position.base(), |
bc9053ab | 497 | __old_finish, |
1985f1cd | 498 | this->_M_impl._M_finish, |
4fd20a8f | 499 | _M_get_Tp_allocator()); |
874e7baa PC |
500 | this->_M_impl._M_finish += __elems_after; |
501 | std::copy(__first, __mid, __position); | |
502 | } | |
503 | } | |
504 | else | |
505 | { | |
be1088fa ML |
506 | const size_type __len = |
507 | _M_check_len(__n, "vector::_M_range_insert"); | |
bc9053ab PC |
508 | pointer __new_start(this->_M_allocate(__len)); |
509 | pointer __new_finish(__new_start); | |
874e7baa PC |
510 | try |
511 | { | |
1985f1cd | 512 | __new_finish = |
6eef7402 | 513 | std::__uninitialized_move_a(this->_M_impl._M_start, |
bc9053ab | 514 | __position.base(), |
1985f1cd | 515 | __new_start, |
4fd20a8f | 516 | _M_get_Tp_allocator()); |
1985f1cd | 517 | __new_finish = |
6eef7402 CJ |
518 | std::__uninitialized_copy_a(__first, __last, |
519 | __new_finish, | |
4fd20a8f | 520 | _M_get_Tp_allocator()); |
1985f1cd | 521 | __new_finish = |
6eef7402 | 522 | std::__uninitialized_move_a(__position.base(), |
bc9053ab | 523 | this->_M_impl._M_finish, |
1985f1cd | 524 | __new_finish, |
4fd20a8f | 525 | _M_get_Tp_allocator()); |
874e7baa PC |
526 | } |
527 | catch(...) | |
528 | { | |
bc9053ab | 529 | std::_Destroy(__new_start, __new_finish, |
4fd20a8f | 530 | _M_get_Tp_allocator()); |
bc9053ab | 531 | _M_deallocate(__new_start, __len); |
874e7baa PC |
532 | __throw_exception_again; |
533 | } | |
1985f1cd | 534 | std::_Destroy(this->_M_impl._M_start, this->_M_impl._M_finish, |
4fd20a8f | 535 | _M_get_Tp_allocator()); |
874e7baa PC |
536 | _M_deallocate(this->_M_impl._M_start, |
537 | this->_M_impl._M_end_of_storage | |
538 | - this->_M_impl._M_start); | |
bc9053ab PC |
539 | this->_M_impl._M_start = __new_start; |
540 | this->_M_impl._M_finish = __new_finish; | |
541 | this->_M_impl._M_end_of_storage = __new_start + __len; | |
874e7baa PC |
542 | } |
543 | } | |
83144cfc | 544 | } |
3cbc7af0 | 545 | |
232c4925 PC |
546 | |
547 | // vector<bool> | |
548 | ||
c62df8fd PC |
549 | template<typename _Alloc> |
550 | void | |
551 | vector<bool, _Alloc>:: | |
552 | reserve(size_type __n) | |
553 | { | |
554 | if (__n > this->max_size()) | |
555 | __throw_length_error(__N("vector::reserve")); | |
556 | if (this->capacity() < __n) | |
557 | { | |
558 | _Bit_type* __q = this->_M_allocate(__n); | |
559 | this->_M_impl._M_finish = _M_copy_aligned(begin(), end(), | |
560 | iterator(__q, 0)); | |
561 | this->_M_deallocate(); | |
562 | this->_M_impl._M_start = iterator(__q, 0); | |
563 | this->_M_impl._M_end_of_storage = (__q + (__n + int(_S_word_bit) - 1) | |
564 | / int(_S_word_bit)); | |
565 | } | |
566 | } | |
567 | ||
232c4925 PC |
568 | template<typename _Alloc> |
569 | void | |
570 | vector<bool, _Alloc>:: | |
571 | _M_fill_insert(iterator __position, size_type __n, bool __x) | |
572 | { | |
573 | if (__n == 0) | |
574 | return; | |
575 | if (capacity() - size() >= __n) | |
576 | { | |
577 | std::copy_backward(__position, end(), | |
578 | this->_M_impl._M_finish + difference_type(__n)); | |
579 | std::fill(__position, __position + difference_type(__n), __x); | |
580 | this->_M_impl._M_finish += difference_type(__n); | |
581 | } | |
582 | else | |
583 | { | |
be1088fa ML |
584 | const size_type __len = |
585 | _M_check_len(__n, "vector<bool>::_M_fill_insert"); | |
232c4925 PC |
586 | _Bit_type * __q = this->_M_allocate(__len); |
587 | iterator __i = _M_copy_aligned(begin(), __position, | |
588 | iterator(__q, 0)); | |
589 | std::fill(__i, __i + difference_type(__n), __x); | |
590 | this->_M_impl._M_finish = std::copy(__position, end(), | |
591 | __i + difference_type(__n)); | |
592 | this->_M_deallocate(); | |
593 | this->_M_impl._M_end_of_storage = (__q + ((__len | |
594 | + int(_S_word_bit) - 1) | |
595 | / int(_S_word_bit))); | |
596 | this->_M_impl._M_start = iterator(__q, 0); | |
597 | } | |
598 | } | |
599 | ||
600 | template<typename _Alloc> | |
601 | template<typename _ForwardIterator> | |
602 | void | |
603 | vector<bool, _Alloc>:: | |
604 | _M_insert_range(iterator __position, _ForwardIterator __first, | |
605 | _ForwardIterator __last, std::forward_iterator_tag) | |
606 | { | |
607 | if (__first != __last) | |
608 | { | |
609 | size_type __n = std::distance(__first, __last); | |
610 | if (capacity() - size() >= __n) | |
611 | { | |
612 | std::copy_backward(__position, end(), | |
613 | this->_M_impl._M_finish | |
614 | + difference_type(__n)); | |
615 | std::copy(__first, __last, __position); | |
616 | this->_M_impl._M_finish += difference_type(__n); | |
617 | } | |
618 | else | |
619 | { | |
be1088fa ML |
620 | const size_type __len = |
621 | _M_check_len(__n, "vector<bool>::_M_insert_range"); | |
232c4925 PC |
622 | _Bit_type * __q = this->_M_allocate(__len); |
623 | iterator __i = _M_copy_aligned(begin(), __position, | |
624 | iterator(__q, 0)); | |
625 | __i = std::copy(__first, __last, __i); | |
626 | this->_M_impl._M_finish = std::copy(__position, end(), __i); | |
627 | this->_M_deallocate(); | |
628 | this->_M_impl._M_end_of_storage = (__q | |
629 | + ((__len | |
630 | + int(_S_word_bit) - 1) | |
631 | / int(_S_word_bit))); | |
632 | this->_M_impl._M_start = iterator(__q, 0); | |
633 | } | |
634 | } | |
635 | } | |
636 | ||
637 | template<typename _Alloc> | |
638 | void | |
639 | vector<bool, _Alloc>:: | |
640 | _M_insert_aux(iterator __position, bool __x) | |
641 | { | |
642 | if (this->_M_impl._M_finish._M_p != this->_M_impl._M_end_of_storage) | |
643 | { | |
644 | std::copy_backward(__position, this->_M_impl._M_finish, | |
645 | this->_M_impl._M_finish + 1); | |
646 | *__position = __x; | |
647 | ++this->_M_impl._M_finish; | |
648 | } | |
649 | else | |
650 | { | |
be1088fa ML |
651 | const size_type __len = |
652 | _M_check_len(size_type(1), "vector<bool>::_M_insert_aux"); | |
232c4925 PC |
653 | _Bit_type * __q = this->_M_allocate(__len); |
654 | iterator __i = _M_copy_aligned(begin(), __position, | |
655 | iterator(__q, 0)); | |
656 | *__i++ = __x; | |
657 | this->_M_impl._M_finish = std::copy(__position, end(), __i); | |
658 | this->_M_deallocate(); | |
659 | this->_M_impl._M_end_of_storage = (__q + ((__len | |
660 | + int(_S_word_bit) - 1) | |
661 | / int(_S_word_bit))); | |
662 | this->_M_impl._M_start = iterator(__q, 0); | |
663 | } | |
664 | } | |
665 | ||
3cbc7af0 | 666 | _GLIBCXX_END_NESTED_NAMESPACE |
83144cfc | 667 | |
3d7c150e | 668 | #endif /* _VECTOR_TCC */ |