]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/include/bits/vector.tcc
re PR libstdc++/25191 (exception_defines.h #defines try/catch)
[thirdparty/gcc.git] / libstdc++-v3 / include / bits / vector.tcc
CommitLineData
83144cfc
PE
1// Vector implementation (out of line) -*- C++ -*-
2
bc2631e0 3// Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
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
4dc3e453
PC
91#ifdef __GXX_EXPERIMENTAL_CXX0X__
92 template<typename _Tp, typename _Alloc>
93 template<typename... _Args>
94 void
95 vector<_Tp, _Alloc>::
96 emplace_back(_Args&&... __args)
97 {
98 if (this->_M_impl._M_finish != this->_M_impl._M_end_of_storage)
99 {
100 this->_M_impl.construct(this->_M_impl._M_finish,
101 std::forward<_Args>(__args)...);
102 ++this->_M_impl._M_finish;
103 }
104 else
105 _M_insert_aux(end(), std::forward<_Args>(__args)...);
106 }
107#endif
108
af5fb6ab 109 template<typename _Tp, typename _Alloc>
874e7baa
PC
110 typename vector<_Tp, _Alloc>::iterator
111 vector<_Tp, _Alloc>::
3971a4d2
PE
112 insert(iterator __position, const value_type& __x)
113 {
43da93a7 114 const size_type __n = __position - begin();
874e7baa
PC
115 if (this->_M_impl._M_finish != this->_M_impl._M_end_of_storage
116 && __position == end())
117 {
1985f1cd 118 this->_M_impl.construct(this->_M_impl._M_finish, __x);
874e7baa
PC
119 ++this->_M_impl._M_finish;
120 }
83144cfc 121 else
812e8c79
PC
122 {
123#ifdef __GXX_EXPERIMENTAL_CXX0X__
124 if (this->_M_impl._M_finish != this->_M_impl._M_end_of_storage)
125 {
126 _Tp __x_copy = __x;
127 _M_insert_aux(__position, std::move(__x_copy));
128 }
129 else
130#endif
131 _M_insert_aux(__position, __x);
132 }
bc9053ab 133 return iterator(this->_M_impl._M_start + __n);
83144cfc 134 }
ed6814f7 135
af5fb6ab 136 template<typename _Tp, typename _Alloc>
874e7baa
PC
137 typename vector<_Tp, _Alloc>::iterator
138 vector<_Tp, _Alloc>::
3971a4d2 139 erase(iterator __position)
83144cfc 140 {
3971a4d2 141 if (__position + 1 != end())
245a5fe5 142 _GLIBCXX_MOVE3(__position + 1, end(), __position);
03f9ea44 143 --this->_M_impl._M_finish;
1985f1cd 144 this->_M_impl.destroy(this->_M_impl._M_finish);
3971a4d2 145 return __position;
83144cfc 146 }
ed6814f7 147
af5fb6ab 148 template<typename _Tp, typename _Alloc>
874e7baa
PC
149 typename vector<_Tp, _Alloc>::iterator
150 vector<_Tp, _Alloc>::
3971a4d2 151 erase(iterator __first, iterator __last)
83144cfc 152 {
bc9053ab 153 if (__last != end())
245a5fe5 154 _GLIBCXX_MOVE3(__last, end(), __first);
bc9053ab 155 _M_erase_at_end(__first.base() + (end() - __last));
3971a4d2 156 return __first;
83144cfc 157 }
ed6814f7 158
af5fb6ab 159 template<typename _Tp, typename _Alloc>
874e7baa
PC
160 vector<_Tp, _Alloc>&
161 vector<_Tp, _Alloc>::
162 operator=(const vector<_Tp, _Alloc>& __x)
83144cfc 163 {
3971a4d2 164 if (&__x != this)
874e7baa
PC
165 {
166 const size_type __xlen = __x.size();
167 if (__xlen > capacity())
168 {
169 pointer __tmp = _M_allocate_and_copy(__xlen, __x.begin(),
170 __x.end());
1985f1cd 171 std::_Destroy(this->_M_impl._M_start, this->_M_impl._M_finish,
4fd20a8f 172 _M_get_Tp_allocator());
874e7baa
PC
173 _M_deallocate(this->_M_impl._M_start,
174 this->_M_impl._M_end_of_storage
175 - this->_M_impl._M_start);
176 this->_M_impl._M_start = __tmp;
177 this->_M_impl._M_end_of_storage = this->_M_impl._M_start + __xlen;
178 }
179 else if (size() >= __xlen)
180 {
bc9053ab
PC
181 std::_Destroy(std::copy(__x.begin(), __x.end(), begin()),
182 end(), _M_get_Tp_allocator());
874e7baa
PC
183 }
184 else
185 {
bc9053ab 186 std::copy(__x._M_impl._M_start, __x._M_impl._M_start + size(),
874e7baa 187 this->_M_impl._M_start);
bc9053ab
PC
188 std::__uninitialized_copy_a(__x._M_impl._M_start + size(),
189 __x._M_impl._M_finish,
190 this->_M_impl._M_finish,
4fd20a8f 191 _M_get_Tp_allocator());
874e7baa
PC
192 }
193 this->_M_impl._M_finish = this->_M_impl._M_start + __xlen;
194 }
3971a4d2 195 return *this;
83144cfc 196 }
ed6814f7 197
af5fb6ab 198 template<typename _Tp, typename _Alloc>
3971a4d2 199 void
874e7baa 200 vector<_Tp, _Alloc>::
3971a4d2 201 _M_fill_assign(size_t __n, const value_type& __val)
83144cfc 202 {
3971a4d2 203 if (__n > capacity())
874e7baa 204 {
4fd20a8f 205 vector __tmp(__n, __val, _M_get_Tp_allocator());
874e7baa
PC
206 __tmp.swap(*this);
207 }
3971a4d2 208 else if (__n > size())
874e7baa
PC
209 {
210 std::fill(begin(), end(), __val);
1985f1cd
MA
211 std::__uninitialized_fill_n_a(this->_M_impl._M_finish,
212 __n - size(), __val,
4fd20a8f 213 _M_get_Tp_allocator());
368b7a30 214 this->_M_impl._M_finish += __n - size();
874e7baa 215 }
3971a4d2 216 else
bc9053ab 217 _M_erase_at_end(std::fill_n(this->_M_impl._M_start, __n, __val));
83144cfc 218 }
ed6814f7 219
874e7baa
PC
220 template<typename _Tp, typename _Alloc>
221 template<typename _InputIterator>
222 void
223 vector<_Tp, _Alloc>::
224 _M_assign_aux(_InputIterator __first, _InputIterator __last,
6323b34e 225 std::input_iterator_tag)
3971a4d2 226 {
bc9053ab
PC
227 pointer __cur(this->_M_impl._M_start);
228 for (; __first != __last && __cur != this->_M_impl._M_finish;
229 ++__cur, ++__first)
874e7baa
PC
230 *__cur = *__first;
231 if (__first == __last)
bc9053ab 232 _M_erase_at_end(__cur);
874e7baa
PC
233 else
234 insert(end(), __first, __last);
3971a4d2 235 }
874e7baa
PC
236
237 template<typename _Tp, typename _Alloc>
238 template<typename _ForwardIterator>
239 void
43da93a7 240 vector<_Tp, _Alloc>::
874e7baa 241 _M_assign_aux(_ForwardIterator __first, _ForwardIterator __last,
6323b34e 242 std::forward_iterator_tag)
3971a4d2 243 {
43da93a7 244 const size_type __len = std::distance(__first, __last);
874e7baa
PC
245
246 if (__len > capacity())
247 {
248 pointer __tmp(_M_allocate_and_copy(__len, __first, __last));
1985f1cd 249 std::_Destroy(this->_M_impl._M_start, this->_M_impl._M_finish,
4fd20a8f 250 _M_get_Tp_allocator());
874e7baa
PC
251 _M_deallocate(this->_M_impl._M_start,
252 this->_M_impl._M_end_of_storage
253 - this->_M_impl._M_start);
254 this->_M_impl._M_start = __tmp;
255 this->_M_impl._M_finish = this->_M_impl._M_start + __len;
256 this->_M_impl._M_end_of_storage = this->_M_impl._M_finish;
257 }
258 else if (size() >= __len)
bc9053ab 259 _M_erase_at_end(std::copy(__first, __last, this->_M_impl._M_start));
874e7baa
PC
260 else
261 {
262 _ForwardIterator __mid = __first;
263 std::advance(__mid, size());
264 std::copy(__first, __mid, this->_M_impl._M_start);
1985f1cd
MA
265 this->_M_impl._M_finish =
266 std::__uninitialized_copy_a(__mid, __last,
267 this->_M_impl._M_finish,
4fd20a8f 268 _M_get_Tp_allocator());
874e7baa 269 }
3971a4d2 270 }
ed6814f7 271
6eef7402
CJ
272#ifdef __GXX_EXPERIMENTAL_CXX0X__
273 template<typename _Tp, typename _Alloc>
274 template<typename... _Args>
275 typename vector<_Tp, _Alloc>::iterator
276 vector<_Tp, _Alloc>::
277 emplace(iterator __position, _Args&&... __args)
278 {
279 const size_type __n = __position - begin();
280 if (this->_M_impl._M_finish != this->_M_impl._M_end_of_storage
281 && __position == end())
282 {
283 this->_M_impl.construct(this->_M_impl._M_finish,
284 std::forward<_Args>(__args)...);
285 ++this->_M_impl._M_finish;
286 }
287 else
288 _M_insert_aux(__position, std::forward<_Args>(__args)...);
289 return iterator(this->_M_impl._M_start + __n);
290 }
291
292 template<typename _Tp, typename _Alloc>
293 template<typename... _Args>
294 void
295 vector<_Tp, _Alloc>::
296 _M_insert_aux(iterator __position, _Args&&... __args)
6eef7402 297#else
af5fb6ab 298 template<typename _Tp, typename _Alloc>
3971a4d2 299 void
43da93a7 300 vector<_Tp, _Alloc>::
3971a4d2 301 _M_insert_aux(iterator __position, const _Tp& __x)
6eef7402 302#endif
812e8c79 303 {
03f9ea44 304 if (this->_M_impl._M_finish != this->_M_impl._M_end_of_storage)
874e7baa 305 {
1985f1cd 306 this->_M_impl.construct(this->_M_impl._M_finish,
6eef7402
CJ
307 _GLIBCXX_MOVE(*(this->_M_impl._M_finish
308 - 1)));
874e7baa 309 ++this->_M_impl._M_finish;
6eef7402 310#ifndef __GXX_EXPERIMENTAL_CXX0X__
874e7baa 311 _Tp __x_copy = __x;
6eef7402
CJ
312#endif
313 _GLIBCXX_MOVE_BACKWARD3(__position.base(),
314 this->_M_impl._M_finish - 2,
315 this->_M_impl._M_finish - 1);
812e8c79
PC
316#ifndef __GXX_EXPERIMENTAL_CXX0X__
317 *__position = __x_copy;
318#else
319 *__position = _Tp(std::forward<_Args>(__args)...);
320#endif
874e7baa 321 }
83144cfc 322 else
874e7baa 323 {
be1088fa
ML
324 const size_type __len =
325 _M_check_len(size_type(1), "vector::_M_insert_aux");
d2219f89 326 const size_type __elems_before = __position - begin();
bc9053ab
PC
327 pointer __new_start(this->_M_allocate(__len));
328 pointer __new_finish(__new_start);
bc2631e0 329 __try
874e7baa 330 {
d2219f89
PC
331 // The order of the three operations is dictated by the C++0x
332 // case, where the moves could alter a new element belonging
333 // to the existing vector. This is an issue only for callers
334 // taking the element by const lvalue ref (see 23.1/13).
335 this->_M_impl.construct(__new_start + __elems_before,
812e8c79 336#ifdef __GXX_EXPERIMENTAL_CXX0X__
812e8c79 337 std::forward<_Args>(__args)...);
d2219f89
PC
338#else
339 __x);
812e8c79 340#endif
d2219f89
PC
341 __new_finish = 0;
342
1985f1cd 343 __new_finish =
6eef7402 344 std::__uninitialized_move_a(this->_M_impl._M_start,
bc9053ab 345 __position.base(), __new_start,
4fd20a8f 346 _M_get_Tp_allocator());
874e7baa 347 ++__new_finish;
d2219f89 348
1985f1cd 349 __new_finish =
6eef7402 350 std::__uninitialized_move_a(__position.base(),
bc9053ab 351 this->_M_impl._M_finish,
1985f1cd 352 __new_finish,
4fd20a8f 353 _M_get_Tp_allocator());
996e5395 354 }
bc2631e0 355 __catch(...)
874e7baa 356 {
d2219f89
PC
357 if (!__new_finish)
358 this->_M_impl.destroy(__new_start + __elems_before);
359 else
360 std::_Destroy(__new_start, __new_finish, _M_get_Tp_allocator());
bc9053ab 361 _M_deallocate(__new_start, __len);
874e7baa
PC
362 __throw_exception_again;
363 }
bc9053ab
PC
364 std::_Destroy(this->_M_impl._M_start, this->_M_impl._M_finish,
365 _M_get_Tp_allocator());
874e7baa
PC
366 _M_deallocate(this->_M_impl._M_start,
367 this->_M_impl._M_end_of_storage
368 - this->_M_impl._M_start);
bc9053ab
PC
369 this->_M_impl._M_start = __new_start;
370 this->_M_impl._M_finish = __new_finish;
371 this->_M_impl._M_end_of_storage = __new_start + __len;
874e7baa 372 }
83144cfc 373 }
64ebadac 374
af5fb6ab 375 template<typename _Tp, typename _Alloc>
3971a4d2 376 void
43da93a7 377 vector<_Tp, _Alloc>::
3971a4d2
PE
378 _M_fill_insert(iterator __position, size_type __n, const value_type& __x)
379 {
380 if (__n != 0)
874e7baa
PC
381 {
382 if (size_type(this->_M_impl._M_end_of_storage
383 - this->_M_impl._M_finish) >= __n)
384 {
385 value_type __x_copy = __x;
386 const size_type __elems_after = end() - __position;
bc9053ab 387 pointer __old_finish(this->_M_impl._M_finish);
874e7baa
PC
388 if (__elems_after > __n)
389 {
6eef7402 390 std::__uninitialized_move_a(this->_M_impl._M_finish - __n,
1985f1cd
MA
391 this->_M_impl._M_finish,
392 this->_M_impl._M_finish,
4fd20a8f 393 _M_get_Tp_allocator());
874e7baa 394 this->_M_impl._M_finish += __n;
6eef7402
CJ
395 _GLIBCXX_MOVE_BACKWARD3(__position.base(),
396 __old_finish - __n, __old_finish);
bc9053ab
PC
397 std::fill(__position.base(), __position.base() + __n,
398 __x_copy);
874e7baa
PC
399 }
400 else
401 {
1985f1cd
MA
402 std::__uninitialized_fill_n_a(this->_M_impl._M_finish,
403 __n - __elems_after,
404 __x_copy,
4fd20a8f 405 _M_get_Tp_allocator());
874e7baa 406 this->_M_impl._M_finish += __n - __elems_after;
6eef7402 407 std::__uninitialized_move_a(__position.base(), __old_finish,
1985f1cd 408 this->_M_impl._M_finish,
4fd20a8f 409 _M_get_Tp_allocator());
874e7baa 410 this->_M_impl._M_finish += __elems_after;
bc9053ab 411 std::fill(__position.base(), __old_finish, __x_copy);
874e7baa
PC
412 }
413 }
414 else
415 {
be1088fa
ML
416 const size_type __len =
417 _M_check_len(__n, "vector::_M_fill_insert");
d2219f89 418 const size_type __elems_before = __position - begin();
bc9053ab
PC
419 pointer __new_start(this->_M_allocate(__len));
420 pointer __new_finish(__new_start);
bc2631e0 421 __try
874e7baa 422 {
d2219f89
PC
423 // See _M_insert_aux above.
424 std::__uninitialized_fill_n_a(__new_start + __elems_before,
425 __n, __x,
426 _M_get_Tp_allocator());
427 __new_finish = 0;
428
1985f1cd 429 __new_finish =
6eef7402 430 std::__uninitialized_move_a(this->_M_impl._M_start,
bc9053ab 431 __position.base(),
1985f1cd 432 __new_start,
4fd20a8f 433 _M_get_Tp_allocator());
368b7a30 434 __new_finish += __n;
d2219f89 435
1985f1cd 436 __new_finish =
6eef7402 437 std::__uninitialized_move_a(__position.base(),
bc9053ab
PC
438 this->_M_impl._M_finish,
439 __new_finish,
4fd20a8f 440 _M_get_Tp_allocator());
874e7baa 441 }
bc2631e0 442 __catch(...)
874e7baa 443 {
d2219f89
PC
444 if (!__new_finish)
445 std::_Destroy(__new_start + __elems_before,
446 __new_start + __elems_before + __n,
447 _M_get_Tp_allocator());
448 else
449 std::_Destroy(__new_start, __new_finish,
450 _M_get_Tp_allocator());
bc9053ab 451 _M_deallocate(__new_start, __len);
874e7baa
PC
452 __throw_exception_again;
453 }
1985f1cd 454 std::_Destroy(this->_M_impl._M_start, this->_M_impl._M_finish,
4fd20a8f 455 _M_get_Tp_allocator());
874e7baa
PC
456 _M_deallocate(this->_M_impl._M_start,
457 this->_M_impl._M_end_of_storage
458 - this->_M_impl._M_start);
bc9053ab
PC
459 this->_M_impl._M_start = __new_start;
460 this->_M_impl._M_finish = __new_finish;
461 this->_M_impl._M_end_of_storage = __new_start + __len;
874e7baa
PC
462 }
463 }
3971a4d2 464 }
ed6814f7 465
232c4925
PC
466 template<typename _Tp, typename _Alloc>
467 template<typename _InputIterator>
468 void
469 vector<_Tp, _Alloc>::
470 _M_range_insert(iterator __pos, _InputIterator __first,
471 _InputIterator __last, std::input_iterator_tag)
472 {
473 for (; __first != __last; ++__first)
474 {
475 __pos = insert(__pos, *__first);
476 ++__pos;
477 }
478 }
ed6814f7 479
874e7baa
PC
480 template<typename _Tp, typename _Alloc>
481 template<typename _ForwardIterator>
482 void
43da93a7 483 vector<_Tp, _Alloc>::
996e5395 484 _M_range_insert(iterator __position, _ForwardIterator __first,
6323b34e 485 _ForwardIterator __last, std::forward_iterator_tag)
3971a4d2 486 {
874e7baa
PC
487 if (__first != __last)
488 {
43da93a7 489 const size_type __n = std::distance(__first, __last);
874e7baa
PC
490 if (size_type(this->_M_impl._M_end_of_storage
491 - this->_M_impl._M_finish) >= __n)
492 {
493 const size_type __elems_after = end() - __position;
bc9053ab 494 pointer __old_finish(this->_M_impl._M_finish);
874e7baa
PC
495 if (__elems_after > __n)
496 {
6eef7402 497 std::__uninitialized_move_a(this->_M_impl._M_finish - __n,
1985f1cd
MA
498 this->_M_impl._M_finish,
499 this->_M_impl._M_finish,
4fd20a8f 500 _M_get_Tp_allocator());
874e7baa 501 this->_M_impl._M_finish += __n;
6eef7402
CJ
502 _GLIBCXX_MOVE_BACKWARD3(__position.base(),
503 __old_finish - __n, __old_finish);
874e7baa
PC
504 std::copy(__first, __last, __position);
505 }
506 else
507 {
508 _ForwardIterator __mid = __first;
509 std::advance(__mid, __elems_after);
1985f1cd
MA
510 std::__uninitialized_copy_a(__mid, __last,
511 this->_M_impl._M_finish,
4fd20a8f 512 _M_get_Tp_allocator());
874e7baa 513 this->_M_impl._M_finish += __n - __elems_after;
6eef7402 514 std::__uninitialized_move_a(__position.base(),
bc9053ab 515 __old_finish,
1985f1cd 516 this->_M_impl._M_finish,
4fd20a8f 517 _M_get_Tp_allocator());
874e7baa
PC
518 this->_M_impl._M_finish += __elems_after;
519 std::copy(__first, __mid, __position);
520 }
521 }
522 else
523 {
be1088fa
ML
524 const size_type __len =
525 _M_check_len(__n, "vector::_M_range_insert");
bc9053ab
PC
526 pointer __new_start(this->_M_allocate(__len));
527 pointer __new_finish(__new_start);
bc2631e0 528 __try
874e7baa 529 {
1985f1cd 530 __new_finish =
6eef7402 531 std::__uninitialized_move_a(this->_M_impl._M_start,
bc9053ab 532 __position.base(),
1985f1cd 533 __new_start,
4fd20a8f 534 _M_get_Tp_allocator());
1985f1cd 535 __new_finish =
6eef7402
CJ
536 std::__uninitialized_copy_a(__first, __last,
537 __new_finish,
4fd20a8f 538 _M_get_Tp_allocator());
1985f1cd 539 __new_finish =
6eef7402 540 std::__uninitialized_move_a(__position.base(),
bc9053ab 541 this->_M_impl._M_finish,
1985f1cd 542 __new_finish,
4fd20a8f 543 _M_get_Tp_allocator());
874e7baa 544 }
bc2631e0 545 __catch(...)
874e7baa 546 {
bc9053ab 547 std::_Destroy(__new_start, __new_finish,
4fd20a8f 548 _M_get_Tp_allocator());
bc9053ab 549 _M_deallocate(__new_start, __len);
874e7baa
PC
550 __throw_exception_again;
551 }
1985f1cd 552 std::_Destroy(this->_M_impl._M_start, this->_M_impl._M_finish,
4fd20a8f 553 _M_get_Tp_allocator());
874e7baa
PC
554 _M_deallocate(this->_M_impl._M_start,
555 this->_M_impl._M_end_of_storage
556 - this->_M_impl._M_start);
bc9053ab
PC
557 this->_M_impl._M_start = __new_start;
558 this->_M_impl._M_finish = __new_finish;
559 this->_M_impl._M_end_of_storage = __new_start + __len;
874e7baa
PC
560 }
561 }
83144cfc 562 }
3cbc7af0 563
232c4925
PC
564
565 // vector<bool>
566
c62df8fd
PC
567 template<typename _Alloc>
568 void
569 vector<bool, _Alloc>::
570 reserve(size_type __n)
571 {
572 if (__n > this->max_size())
573 __throw_length_error(__N("vector::reserve"));
574 if (this->capacity() < __n)
575 {
576 _Bit_type* __q = this->_M_allocate(__n);
577 this->_M_impl._M_finish = _M_copy_aligned(begin(), end(),
578 iterator(__q, 0));
579 this->_M_deallocate();
580 this->_M_impl._M_start = iterator(__q, 0);
581 this->_M_impl._M_end_of_storage = (__q + (__n + int(_S_word_bit) - 1)
582 / int(_S_word_bit));
583 }
584 }
585
232c4925
PC
586 template<typename _Alloc>
587 void
588 vector<bool, _Alloc>::
589 _M_fill_insert(iterator __position, size_type __n, bool __x)
590 {
591 if (__n == 0)
592 return;
593 if (capacity() - size() >= __n)
594 {
595 std::copy_backward(__position, end(),
596 this->_M_impl._M_finish + difference_type(__n));
597 std::fill(__position, __position + difference_type(__n), __x);
598 this->_M_impl._M_finish += difference_type(__n);
599 }
600 else
601 {
be1088fa
ML
602 const size_type __len =
603 _M_check_len(__n, "vector<bool>::_M_fill_insert");
232c4925
PC
604 _Bit_type * __q = this->_M_allocate(__len);
605 iterator __i = _M_copy_aligned(begin(), __position,
606 iterator(__q, 0));
607 std::fill(__i, __i + difference_type(__n), __x);
608 this->_M_impl._M_finish = std::copy(__position, end(),
609 __i + difference_type(__n));
610 this->_M_deallocate();
611 this->_M_impl._M_end_of_storage = (__q + ((__len
612 + int(_S_word_bit) - 1)
613 / int(_S_word_bit)));
614 this->_M_impl._M_start = iterator(__q, 0);
615 }
616 }
617
618 template<typename _Alloc>
619 template<typename _ForwardIterator>
620 void
621 vector<bool, _Alloc>::
622 _M_insert_range(iterator __position, _ForwardIterator __first,
623 _ForwardIterator __last, std::forward_iterator_tag)
624 {
625 if (__first != __last)
626 {
627 size_type __n = std::distance(__first, __last);
628 if (capacity() - size() >= __n)
629 {
630 std::copy_backward(__position, end(),
631 this->_M_impl._M_finish
632 + difference_type(__n));
633 std::copy(__first, __last, __position);
634 this->_M_impl._M_finish += difference_type(__n);
635 }
636 else
637 {
be1088fa
ML
638 const size_type __len =
639 _M_check_len(__n, "vector<bool>::_M_insert_range");
232c4925
PC
640 _Bit_type * __q = this->_M_allocate(__len);
641 iterator __i = _M_copy_aligned(begin(), __position,
642 iterator(__q, 0));
643 __i = std::copy(__first, __last, __i);
644 this->_M_impl._M_finish = std::copy(__position, end(), __i);
645 this->_M_deallocate();
646 this->_M_impl._M_end_of_storage = (__q
647 + ((__len
648 + int(_S_word_bit) - 1)
649 / int(_S_word_bit)));
650 this->_M_impl._M_start = iterator(__q, 0);
651 }
652 }
653 }
654
655 template<typename _Alloc>
656 void
657 vector<bool, _Alloc>::
658 _M_insert_aux(iterator __position, bool __x)
659 {
660 if (this->_M_impl._M_finish._M_p != this->_M_impl._M_end_of_storage)
661 {
662 std::copy_backward(__position, this->_M_impl._M_finish,
663 this->_M_impl._M_finish + 1);
664 *__position = __x;
665 ++this->_M_impl._M_finish;
666 }
667 else
668 {
be1088fa
ML
669 const size_type __len =
670 _M_check_len(size_type(1), "vector<bool>::_M_insert_aux");
232c4925
PC
671 _Bit_type * __q = this->_M_allocate(__len);
672 iterator __i = _M_copy_aligned(begin(), __position,
673 iterator(__q, 0));
674 *__i++ = __x;
675 this->_M_impl._M_finish = std::copy(__position, end(), __i);
676 this->_M_deallocate();
677 this->_M_impl._M_end_of_storage = (__q + ((__len
678 + int(_S_word_bit) - 1)
679 / int(_S_word_bit)));
680 this->_M_impl._M_start = iterator(__q, 0);
681 }
682 }
683
3cbc7af0 684_GLIBCXX_END_NESTED_NAMESPACE
83144cfc 685
3d7c150e 686#endif /* _VECTOR_TCC */