]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/include/bits/vector.tcc
Update copyright years in libstdc++-v3/
[thirdparty/gcc.git] / libstdc++-v3 / include / bits / vector.tcc
CommitLineData
83144cfc
PE
1// Vector implementation (out of line) -*- C++ -*-
2
aa118a03 3// Copyright (C) 2001-2014 Free Software Foundation, Inc.
83144cfc
PE
4//
5// This file is part of the GNU ISO C++ Library. This library is free
6// software; you can redistribute it and/or modify it under the
7// terms of the GNU General Public License as published by the
748086b7 8// Free Software Foundation; either version 3, or (at your option)
83144cfc
PE
9// any later version.
10
11// This library is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
15
748086b7
JJ
16// Under Section 7 of GPL version 3, you are granted additional
17// permissions described in the GCC Runtime Library Exception, version
18// 3.1, as published by the Free Software Foundation.
83144cfc 19
748086b7
JJ
20// You should have received a copy of the GNU General Public License and
21// a copy of the GCC Runtime Library Exception along with this program;
22// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23// <http://www.gnu.org/licenses/>.
83144cfc
PE
24
25/*
26 *
27 * Copyright (c) 1994
28 * Hewlett-Packard Company
29 *
30 * Permission to use, copy, modify, distribute and sell this software
31 * and its documentation for any purpose is hereby granted without fee,
32 * provided that the above copyright notice appear in all copies and
33 * that both that copyright notice and this permission notice appear
34 * in supporting documentation. Hewlett-Packard Company makes no
35 * representations about the suitability of this software for any
36 * purpose. It is provided "as is" without express or implied warranty.
37 *
38 *
39 * Copyright (c) 1996
40 * Silicon Graphics Computer Systems, Inc.
41 *
42 * Permission to use, copy, modify, distribute and sell this software
43 * and its documentation for any purpose is hereby granted without fee,
44 * provided that the above copyright notice appear in all copies and
45 * that both that copyright notice and this permission notice appear
46 * in supporting documentation. Silicon Graphics makes no
47 * representations about the suitability of this software for any
48 * purpose. It is provided "as is" without express or implied warranty.
49 */
50
f910786b 51/** @file bits/vector.tcc
83144cfc 52 * This is an internal header file, included by other library headers.
f910786b 53 * Do not attempt to use it directly. @headername{vector}
83144cfc
PE
54 */
55
3d7c150e
BK
56#ifndef _VECTOR_TCC
57#define _VECTOR_TCC 1
83144cfc 58
12ffa228
BK
59namespace std _GLIBCXX_VISIBILITY(default)
60{
61_GLIBCXX_BEGIN_NAMESPACE_CONTAINER
3cbc7af0 62
af5fb6ab 63 template<typename _Tp, typename _Alloc>
3971a4d2 64 void
874e7baa 65 vector<_Tp, _Alloc>::
3971a4d2 66 reserve(size_type __n)
83144cfc 67 {
48d1c3c5 68 if (__n > this->max_size())
ba9119ec 69 __throw_length_error(__N("vector::reserve"));
48d1c3c5
BK
70 if (this->capacity() < __n)
71 {
72 const size_type __old_size = size();
245a5fe5 73 pointer __tmp = _M_allocate_and_copy(__n,
74a2a1b4
PC
74 _GLIBCXX_MAKE_MOVE_IF_NOEXCEPT_ITERATOR(this->_M_impl._M_start),
75 _GLIBCXX_MAKE_MOVE_IF_NOEXCEPT_ITERATOR(this->_M_impl._M_finish));
1985f1cd 76 std::_Destroy(this->_M_impl._M_start, this->_M_impl._M_finish,
4fd20a8f 77 _M_get_Tp_allocator());
03f9ea44 78 _M_deallocate(this->_M_impl._M_start,
874e7baa
PC
79 this->_M_impl._M_end_of_storage
80 - this->_M_impl._M_start);
03f9ea44
DM
81 this->_M_impl._M_start = __tmp;
82 this->_M_impl._M_finish = __tmp + __old_size;
83 this->_M_impl._M_end_of_storage = this->_M_impl._M_start + __n;
48d1c3c5 84 }
3971a4d2 85 }
ed6814f7 86
734f5023 87#if __cplusplus >= 201103L
4dc3e453
PC
88 template<typename _Tp, typename _Alloc>
89 template<typename... _Args>
90 void
91 vector<_Tp, _Alloc>::
92 emplace_back(_Args&&... __args)
93 {
94 if (this->_M_impl._M_finish != this->_M_impl._M_end_of_storage)
95 {
bd8485dc
JW
96 _Alloc_traits::construct(this->_M_impl, this->_M_impl._M_finish,
97 std::forward<_Args>(__args)...);
4dc3e453
PC
98 ++this->_M_impl._M_finish;
99 }
100 else
cc2ba8e3 101 _M_emplace_back_aux(std::forward<_Args>(__args)...);
4dc3e453
PC
102 }
103#endif
104
af5fb6ab 105 template<typename _Tp, typename _Alloc>
874e7baa
PC
106 typename vector<_Tp, _Alloc>::iterator
107 vector<_Tp, _Alloc>::
7b61c5a9
PC
108#if __cplusplus >= 201103L
109 insert(const_iterator __position, const value_type& __x)
110#else
3971a4d2 111 insert(iterator __position, const value_type& __x)
7b61c5a9 112#endif
3971a4d2 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 {
bd8485dc 118 _Alloc_traits::construct(this->_M_impl, this->_M_impl._M_finish, __x);
874e7baa
PC
119 ++this->_M_impl._M_finish;
120 }
83144cfc 121 else
812e8c79 122 {
734f5023 123#if __cplusplus >= 201103L
812e8c79
PC
124 if (this->_M_impl._M_finish != this->_M_impl._M_end_of_storage)
125 {
126 _Tp __x_copy = __x;
7b61c5a9 127 _M_insert_aux(__position._M_const_cast(), std::move(__x_copy));
812e8c79
PC
128 }
129 else
130#endif
7b61c5a9 131 _M_insert_aux(__position._M_const_cast(), __x);
812e8c79 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>::
94938aec 139 _M_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;
bd8485dc 144 _Alloc_traits::destroy(this->_M_impl, 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>::
94938aec 151 _M_erase(iterator __first, iterator __last)
83144cfc 152 {
a7cee01d
PC
153 if (__first != __last)
154 {
155 if (__last != end())
156 _GLIBCXX_MOVE3(__last, end(), __first);
157 _M_erase_at_end(__first.base() + (end() - __last));
158 }
3971a4d2 159 return __first;
83144cfc 160 }
ed6814f7 161
af5fb6ab 162 template<typename _Tp, typename _Alloc>
874e7baa
PC
163 vector<_Tp, _Alloc>&
164 vector<_Tp, _Alloc>::
165 operator=(const vector<_Tp, _Alloc>& __x)
83144cfc 166 {
3971a4d2 167 if (&__x != this)
874e7baa 168 {
734f5023 169#if __cplusplus >= 201103L
bd8485dc
JW
170 if (_Alloc_traits::_S_propagate_on_copy_assign())
171 {
172 if (!_Alloc_traits::_S_always_equal()
173 && _M_get_Tp_allocator() != __x._M_get_Tp_allocator())
174 {
175 // replacement allocator cannot free existing storage
176 this->clear();
177 _M_deallocate(this->_M_impl._M_start,
178 this->_M_impl._M_end_of_storage
179 - this->_M_impl._M_start);
68d047cb
FD
180 this->_M_impl._M_start = nullptr;
181 this->_M_impl._M_finish = nullptr;
182 this->_M_impl._M_end_of_storage = nullptr;
bd8485dc
JW
183 }
184 std::__alloc_on_copy(_M_get_Tp_allocator(),
185 __x._M_get_Tp_allocator());
186 }
187#endif
874e7baa
PC
188 const size_type __xlen = __x.size();
189 if (__xlen > capacity())
190 {
191 pointer __tmp = _M_allocate_and_copy(__xlen, __x.begin(),
192 __x.end());
1985f1cd 193 std::_Destroy(this->_M_impl._M_start, this->_M_impl._M_finish,
4fd20a8f 194 _M_get_Tp_allocator());
874e7baa
PC
195 _M_deallocate(this->_M_impl._M_start,
196 this->_M_impl._M_end_of_storage
197 - this->_M_impl._M_start);
198 this->_M_impl._M_start = __tmp;
199 this->_M_impl._M_end_of_storage = this->_M_impl._M_start + __xlen;
200 }
201 else if (size() >= __xlen)
202 {
bc9053ab
PC
203 std::_Destroy(std::copy(__x.begin(), __x.end(), begin()),
204 end(), _M_get_Tp_allocator());
874e7baa
PC
205 }
206 else
207 {
bc9053ab 208 std::copy(__x._M_impl._M_start, __x._M_impl._M_start + size(),
874e7baa 209 this->_M_impl._M_start);
bc9053ab
PC
210 std::__uninitialized_copy_a(__x._M_impl._M_start + size(),
211 __x._M_impl._M_finish,
212 this->_M_impl._M_finish,
4fd20a8f 213 _M_get_Tp_allocator());
874e7baa
PC
214 }
215 this->_M_impl._M_finish = this->_M_impl._M_start + __xlen;
216 }
3971a4d2 217 return *this;
83144cfc 218 }
ed6814f7 219
af5fb6ab 220 template<typename _Tp, typename _Alloc>
3971a4d2 221 void
874e7baa 222 vector<_Tp, _Alloc>::
3971a4d2 223 _M_fill_assign(size_t __n, const value_type& __val)
83144cfc 224 {
3971a4d2 225 if (__n > capacity())
874e7baa 226 {
4fd20a8f 227 vector __tmp(__n, __val, _M_get_Tp_allocator());
874e7baa
PC
228 __tmp.swap(*this);
229 }
3971a4d2 230 else if (__n > size())
874e7baa
PC
231 {
232 std::fill(begin(), end(), __val);
1985f1cd
MA
233 std::__uninitialized_fill_n_a(this->_M_impl._M_finish,
234 __n - size(), __val,
4fd20a8f 235 _M_get_Tp_allocator());
368b7a30 236 this->_M_impl._M_finish += __n - size();
874e7baa 237 }
3971a4d2 238 else
bc9053ab 239 _M_erase_at_end(std::fill_n(this->_M_impl._M_start, __n, __val));
83144cfc 240 }
ed6814f7 241
874e7baa
PC
242 template<typename _Tp, typename _Alloc>
243 template<typename _InputIterator>
244 void
245 vector<_Tp, _Alloc>::
246 _M_assign_aux(_InputIterator __first, _InputIterator __last,
6323b34e 247 std::input_iterator_tag)
3971a4d2 248 {
bc9053ab
PC
249 pointer __cur(this->_M_impl._M_start);
250 for (; __first != __last && __cur != this->_M_impl._M_finish;
251 ++__cur, ++__first)
874e7baa
PC
252 *__cur = *__first;
253 if (__first == __last)
bc9053ab 254 _M_erase_at_end(__cur);
874e7baa
PC
255 else
256 insert(end(), __first, __last);
3971a4d2 257 }
874e7baa
PC
258
259 template<typename _Tp, typename _Alloc>
260 template<typename _ForwardIterator>
261 void
43da93a7 262 vector<_Tp, _Alloc>::
874e7baa 263 _M_assign_aux(_ForwardIterator __first, _ForwardIterator __last,
6323b34e 264 std::forward_iterator_tag)
3971a4d2 265 {
43da93a7 266 const size_type __len = std::distance(__first, __last);
874e7baa
PC
267
268 if (__len > capacity())
269 {
270 pointer __tmp(_M_allocate_and_copy(__len, __first, __last));
1985f1cd 271 std::_Destroy(this->_M_impl._M_start, this->_M_impl._M_finish,
4fd20a8f 272 _M_get_Tp_allocator());
874e7baa
PC
273 _M_deallocate(this->_M_impl._M_start,
274 this->_M_impl._M_end_of_storage
275 - this->_M_impl._M_start);
276 this->_M_impl._M_start = __tmp;
277 this->_M_impl._M_finish = this->_M_impl._M_start + __len;
278 this->_M_impl._M_end_of_storage = this->_M_impl._M_finish;
279 }
280 else if (size() >= __len)
bc9053ab 281 _M_erase_at_end(std::copy(__first, __last, this->_M_impl._M_start));
874e7baa
PC
282 else
283 {
284 _ForwardIterator __mid = __first;
285 std::advance(__mid, size());
286 std::copy(__first, __mid, this->_M_impl._M_start);
1985f1cd
MA
287 this->_M_impl._M_finish =
288 std::__uninitialized_copy_a(__mid, __last,
289 this->_M_impl._M_finish,
4fd20a8f 290 _M_get_Tp_allocator());
874e7baa 291 }
3971a4d2 292 }
ed6814f7 293
734f5023 294#if __cplusplus >= 201103L
6eef7402
CJ
295 template<typename _Tp, typename _Alloc>
296 template<typename... _Args>
297 typename vector<_Tp, _Alloc>::iterator
298 vector<_Tp, _Alloc>::
7b61c5a9 299 emplace(const_iterator __position, _Args&&... __args)
6eef7402
CJ
300 {
301 const size_type __n = __position - begin();
302 if (this->_M_impl._M_finish != this->_M_impl._M_end_of_storage
303 && __position == end())
304 {
bd8485dc
JW
305 _Alloc_traits::construct(this->_M_impl, this->_M_impl._M_finish,
306 std::forward<_Args>(__args)...);
6eef7402
CJ
307 ++this->_M_impl._M_finish;
308 }
309 else
7b61c5a9
PC
310 _M_insert_aux(__position._M_const_cast(),
311 std::forward<_Args>(__args)...);
6eef7402
CJ
312 return iterator(this->_M_impl._M_start + __n);
313 }
314
315 template<typename _Tp, typename _Alloc>
316 template<typename... _Args>
317 void
318 vector<_Tp, _Alloc>::
319 _M_insert_aux(iterator __position, _Args&&... __args)
6eef7402 320#else
af5fb6ab 321 template<typename _Tp, typename _Alloc>
3971a4d2 322 void
43da93a7 323 vector<_Tp, _Alloc>::
3971a4d2 324 _M_insert_aux(iterator __position, const _Tp& __x)
6eef7402 325#endif
812e8c79 326 {
03f9ea44 327 if (this->_M_impl._M_finish != this->_M_impl._M_end_of_storage)
874e7baa 328 {
bd8485dc
JW
329 _Alloc_traits::construct(this->_M_impl, this->_M_impl._M_finish,
330 _GLIBCXX_MOVE(*(this->_M_impl._M_finish
331 - 1)));
874e7baa 332 ++this->_M_impl._M_finish;
734f5023 333#if __cplusplus < 201103L
874e7baa 334 _Tp __x_copy = __x;
6eef7402
CJ
335#endif
336 _GLIBCXX_MOVE_BACKWARD3(__position.base(),
337 this->_M_impl._M_finish - 2,
338 this->_M_impl._M_finish - 1);
734f5023 339#if __cplusplus < 201103L
812e8c79
PC
340 *__position = __x_copy;
341#else
342 *__position = _Tp(std::forward<_Args>(__args)...);
343#endif
874e7baa 344 }
83144cfc 345 else
874e7baa 346 {
be1088fa
ML
347 const size_type __len =
348 _M_check_len(size_type(1), "vector::_M_insert_aux");
d2219f89 349 const size_type __elems_before = __position - begin();
bc9053ab
PC
350 pointer __new_start(this->_M_allocate(__len));
351 pointer __new_finish(__new_start);
bc2631e0 352 __try
874e7baa 353 {
d2219f89
PC
354 // The order of the three operations is dictated by the C++0x
355 // case, where the moves could alter a new element belonging
356 // to the existing vector. This is an issue only for callers
357 // taking the element by const lvalue ref (see 23.1/13).
bd8485dc
JW
358 _Alloc_traits::construct(this->_M_impl,
359 __new_start + __elems_before,
734f5023 360#if __cplusplus >= 201103L
bd8485dc 361 std::forward<_Args>(__args)...);
d2219f89 362#else
bd8485dc 363 __x);
812e8c79 364#endif
d2219f89
PC
365 __new_finish = 0;
366
74a2a1b4
PC
367 __new_finish
368 = std::__uninitialized_move_if_noexcept_a
369 (this->_M_impl._M_start, __position.base(),
370 __new_start, _M_get_Tp_allocator());
371
874e7baa 372 ++__new_finish;
d2219f89 373
74a2a1b4
PC
374 __new_finish
375 = std::__uninitialized_move_if_noexcept_a
376 (__position.base(), this->_M_impl._M_finish,
377 __new_finish, _M_get_Tp_allocator());
996e5395 378 }
bc2631e0 379 __catch(...)
874e7baa 380 {
d2219f89 381 if (!__new_finish)
bd8485dc
JW
382 _Alloc_traits::destroy(this->_M_impl,
383 __new_start + __elems_before);
d2219f89
PC
384 else
385 std::_Destroy(__new_start, __new_finish, _M_get_Tp_allocator());
bc9053ab 386 _M_deallocate(__new_start, __len);
874e7baa
PC
387 __throw_exception_again;
388 }
bc9053ab
PC
389 std::_Destroy(this->_M_impl._M_start, this->_M_impl._M_finish,
390 _M_get_Tp_allocator());
874e7baa
PC
391 _M_deallocate(this->_M_impl._M_start,
392 this->_M_impl._M_end_of_storage
393 - this->_M_impl._M_start);
bc9053ab
PC
394 this->_M_impl._M_start = __new_start;
395 this->_M_impl._M_finish = __new_finish;
396 this->_M_impl._M_end_of_storage = __new_start + __len;
874e7baa 397 }
83144cfc 398 }
64ebadac 399
734f5023 400#if __cplusplus >= 201103L
cc2ba8e3
PC
401 template<typename _Tp, typename _Alloc>
402 template<typename... _Args>
403 void
404 vector<_Tp, _Alloc>::
405 _M_emplace_back_aux(_Args&&... __args)
406 {
407 const size_type __len =
408 _M_check_len(size_type(1), "vector::_M_emplace_back_aux");
409 pointer __new_start(this->_M_allocate(__len));
410 pointer __new_finish(__new_start);
411 __try
412 {
413 _Alloc_traits::construct(this->_M_impl, __new_start + size(),
414 std::forward<_Args>(__args)...);
415 __new_finish = 0;
416
417 __new_finish
418 = std::__uninitialized_move_if_noexcept_a
419 (this->_M_impl._M_start, this->_M_impl._M_finish,
420 __new_start, _M_get_Tp_allocator());
421
422 ++__new_finish;
423 }
424 __catch(...)
425 {
426 if (!__new_finish)
427 _Alloc_traits::destroy(this->_M_impl, __new_start + size());
428 else
429 std::_Destroy(__new_start, __new_finish, _M_get_Tp_allocator());
430 _M_deallocate(__new_start, __len);
431 __throw_exception_again;
432 }
433 std::_Destroy(this->_M_impl._M_start, this->_M_impl._M_finish,
434 _M_get_Tp_allocator());
435 _M_deallocate(this->_M_impl._M_start,
436 this->_M_impl._M_end_of_storage
437 - this->_M_impl._M_start);
438 this->_M_impl._M_start = __new_start;
439 this->_M_impl._M_finish = __new_finish;
440 this->_M_impl._M_end_of_storage = __new_start + __len;
441 }
442#endif
443
af5fb6ab 444 template<typename _Tp, typename _Alloc>
3971a4d2 445 void
43da93a7 446 vector<_Tp, _Alloc>::
3971a4d2
PE
447 _M_fill_insert(iterator __position, size_type __n, const value_type& __x)
448 {
449 if (__n != 0)
874e7baa
PC
450 {
451 if (size_type(this->_M_impl._M_end_of_storage
452 - this->_M_impl._M_finish) >= __n)
453 {
454 value_type __x_copy = __x;
455 const size_type __elems_after = end() - __position;
bc9053ab 456 pointer __old_finish(this->_M_impl._M_finish);
874e7baa
PC
457 if (__elems_after > __n)
458 {
6eef7402 459 std::__uninitialized_move_a(this->_M_impl._M_finish - __n,
1985f1cd
MA
460 this->_M_impl._M_finish,
461 this->_M_impl._M_finish,
4fd20a8f 462 _M_get_Tp_allocator());
874e7baa 463 this->_M_impl._M_finish += __n;
6eef7402
CJ
464 _GLIBCXX_MOVE_BACKWARD3(__position.base(),
465 __old_finish - __n, __old_finish);
bc9053ab
PC
466 std::fill(__position.base(), __position.base() + __n,
467 __x_copy);
874e7baa
PC
468 }
469 else
470 {
1985f1cd
MA
471 std::__uninitialized_fill_n_a(this->_M_impl._M_finish,
472 __n - __elems_after,
473 __x_copy,
4fd20a8f 474 _M_get_Tp_allocator());
874e7baa 475 this->_M_impl._M_finish += __n - __elems_after;
6eef7402 476 std::__uninitialized_move_a(__position.base(), __old_finish,
1985f1cd 477 this->_M_impl._M_finish,
4fd20a8f 478 _M_get_Tp_allocator());
874e7baa 479 this->_M_impl._M_finish += __elems_after;
bc9053ab 480 std::fill(__position.base(), __old_finish, __x_copy);
874e7baa
PC
481 }
482 }
483 else
484 {
be1088fa
ML
485 const size_type __len =
486 _M_check_len(__n, "vector::_M_fill_insert");
d2219f89 487 const size_type __elems_before = __position - begin();
bc9053ab
PC
488 pointer __new_start(this->_M_allocate(__len));
489 pointer __new_finish(__new_start);
bc2631e0 490 __try
874e7baa 491 {
d2219f89
PC
492 // See _M_insert_aux above.
493 std::__uninitialized_fill_n_a(__new_start + __elems_before,
494 __n, __x,
495 _M_get_Tp_allocator());
496 __new_finish = 0;
497
74a2a1b4
PC
498 __new_finish
499 = std::__uninitialized_move_if_noexcept_a
500 (this->_M_impl._M_start, __position.base(),
501 __new_start, _M_get_Tp_allocator());
502
368b7a30 503 __new_finish += __n;
d2219f89 504
74a2a1b4
PC
505 __new_finish
506 = std::__uninitialized_move_if_noexcept_a
507 (__position.base(), this->_M_impl._M_finish,
508 __new_finish, _M_get_Tp_allocator());
874e7baa 509 }
bc2631e0 510 __catch(...)
874e7baa 511 {
d2219f89
PC
512 if (!__new_finish)
513 std::_Destroy(__new_start + __elems_before,
514 __new_start + __elems_before + __n,
515 _M_get_Tp_allocator());
516 else
517 std::_Destroy(__new_start, __new_finish,
518 _M_get_Tp_allocator());
bc9053ab 519 _M_deallocate(__new_start, __len);
874e7baa
PC
520 __throw_exception_again;
521 }
1985f1cd 522 std::_Destroy(this->_M_impl._M_start, this->_M_impl._M_finish,
4fd20a8f 523 _M_get_Tp_allocator());
874e7baa
PC
524 _M_deallocate(this->_M_impl._M_start,
525 this->_M_impl._M_end_of_storage
526 - this->_M_impl._M_start);
bc9053ab
PC
527 this->_M_impl._M_start = __new_start;
528 this->_M_impl._M_finish = __new_finish;
529 this->_M_impl._M_end_of_storage = __new_start + __len;
874e7baa
PC
530 }
531 }
3971a4d2 532 }
ed6814f7 533
734f5023 534#if __cplusplus >= 201103L
dc2cf706
PC
535 template<typename _Tp, typename _Alloc>
536 void
537 vector<_Tp, _Alloc>::
538 _M_default_append(size_type __n)
539 {
540 if (__n != 0)
541 {
542 if (size_type(this->_M_impl._M_end_of_storage
543 - this->_M_impl._M_finish) >= __n)
544 {
545 std::__uninitialized_default_n_a(this->_M_impl._M_finish,
546 __n, _M_get_Tp_allocator());
547 this->_M_impl._M_finish += __n;
548 }
549 else
550 {
551 const size_type __len =
552 _M_check_len(__n, "vector::_M_default_append");
553 const size_type __old_size = this->size();
554 pointer __new_start(this->_M_allocate(__len));
555 pointer __new_finish(__new_start);
556 __try
557 {
74a2a1b4
PC
558 __new_finish
559 = std::__uninitialized_move_if_noexcept_a
560 (this->_M_impl._M_start, this->_M_impl._M_finish,
561 __new_start, _M_get_Tp_allocator());
dc2cf706
PC
562 std::__uninitialized_default_n_a(__new_finish, __n,
563 _M_get_Tp_allocator());
564 __new_finish += __n;
565 }
566 __catch(...)
567 {
568 std::_Destroy(__new_start, __new_finish,
569 _M_get_Tp_allocator());
570 _M_deallocate(__new_start, __len);
571 __throw_exception_again;
572 }
573 std::_Destroy(this->_M_impl._M_start, this->_M_impl._M_finish,
574 _M_get_Tp_allocator());
575 _M_deallocate(this->_M_impl._M_start,
576 this->_M_impl._M_end_of_storage
577 - this->_M_impl._M_start);
578 this->_M_impl._M_start = __new_start;
579 this->_M_impl._M_finish = __new_finish;
580 this->_M_impl._M_end_of_storage = __new_start + __len;
581 }
582 }
583 }
8a752dfe
FD
584
585 template<typename _Tp, typename _Alloc>
586 bool
587 vector<_Tp, _Alloc>::
588 _M_shrink_to_fit()
589 {
590 if (capacity() == size())
591 return false;
592 return std::__shrink_to_fit_aux<vector>::_S_do_it(*this);
593 }
dc2cf706
PC
594#endif
595
232c4925
PC
596 template<typename _Tp, typename _Alloc>
597 template<typename _InputIterator>
598 void
599 vector<_Tp, _Alloc>::
600 _M_range_insert(iterator __pos, _InputIterator __first,
601 _InputIterator __last, std::input_iterator_tag)
602 {
603 for (; __first != __last; ++__first)
604 {
605 __pos = insert(__pos, *__first);
606 ++__pos;
607 }
608 }
ed6814f7 609
874e7baa
PC
610 template<typename _Tp, typename _Alloc>
611 template<typename _ForwardIterator>
612 void
43da93a7 613 vector<_Tp, _Alloc>::
996e5395 614 _M_range_insert(iterator __position, _ForwardIterator __first,
6323b34e 615 _ForwardIterator __last, std::forward_iterator_tag)
3971a4d2 616 {
874e7baa
PC
617 if (__first != __last)
618 {
43da93a7 619 const size_type __n = std::distance(__first, __last);
874e7baa
PC
620 if (size_type(this->_M_impl._M_end_of_storage
621 - this->_M_impl._M_finish) >= __n)
622 {
623 const size_type __elems_after = end() - __position;
bc9053ab 624 pointer __old_finish(this->_M_impl._M_finish);
874e7baa
PC
625 if (__elems_after > __n)
626 {
6eef7402 627 std::__uninitialized_move_a(this->_M_impl._M_finish - __n,
1985f1cd
MA
628 this->_M_impl._M_finish,
629 this->_M_impl._M_finish,
4fd20a8f 630 _M_get_Tp_allocator());
874e7baa 631 this->_M_impl._M_finish += __n;
6eef7402
CJ
632 _GLIBCXX_MOVE_BACKWARD3(__position.base(),
633 __old_finish - __n, __old_finish);
874e7baa
PC
634 std::copy(__first, __last, __position);
635 }
636 else
637 {
638 _ForwardIterator __mid = __first;
639 std::advance(__mid, __elems_after);
1985f1cd
MA
640 std::__uninitialized_copy_a(__mid, __last,
641 this->_M_impl._M_finish,
4fd20a8f 642 _M_get_Tp_allocator());
874e7baa 643 this->_M_impl._M_finish += __n - __elems_after;
6eef7402 644 std::__uninitialized_move_a(__position.base(),
bc9053ab 645 __old_finish,
1985f1cd 646 this->_M_impl._M_finish,
4fd20a8f 647 _M_get_Tp_allocator());
874e7baa
PC
648 this->_M_impl._M_finish += __elems_after;
649 std::copy(__first, __mid, __position);
650 }
651 }
652 else
653 {
be1088fa
ML
654 const size_type __len =
655 _M_check_len(__n, "vector::_M_range_insert");
bc9053ab
PC
656 pointer __new_start(this->_M_allocate(__len));
657 pointer __new_finish(__new_start);
bc2631e0 658 __try
874e7baa 659 {
74a2a1b4
PC
660 __new_finish
661 = std::__uninitialized_move_if_noexcept_a
662 (this->_M_impl._M_start, __position.base(),
663 __new_start, _M_get_Tp_allocator());
664 __new_finish
665 = std::__uninitialized_copy_a(__first, __last,
666 __new_finish,
667 _M_get_Tp_allocator());
668 __new_finish
669 = std::__uninitialized_move_if_noexcept_a
670 (__position.base(), this->_M_impl._M_finish,
671 __new_finish, _M_get_Tp_allocator());
874e7baa 672 }
bc2631e0 673 __catch(...)
874e7baa 674 {
bc9053ab 675 std::_Destroy(__new_start, __new_finish,
4fd20a8f 676 _M_get_Tp_allocator());
bc9053ab 677 _M_deallocate(__new_start, __len);
874e7baa
PC
678 __throw_exception_again;
679 }
1985f1cd 680 std::_Destroy(this->_M_impl._M_start, this->_M_impl._M_finish,
4fd20a8f 681 _M_get_Tp_allocator());
874e7baa
PC
682 _M_deallocate(this->_M_impl._M_start,
683 this->_M_impl._M_end_of_storage
684 - this->_M_impl._M_start);
bc9053ab
PC
685 this->_M_impl._M_start = __new_start;
686 this->_M_impl._M_finish = __new_finish;
687 this->_M_impl._M_end_of_storage = __new_start + __len;
874e7baa
PC
688 }
689 }
83144cfc 690 }
3cbc7af0 691
232c4925
PC
692
693 // vector<bool>
c62df8fd
PC
694 template<typename _Alloc>
695 void
696 vector<bool, _Alloc>::
8a752dfe 697 _M_reallocate(size_type __n)
c62df8fd 698 {
8a752dfe
FD
699 _Bit_type* __q = this->_M_allocate(__n);
700 this->_M_impl._M_finish = _M_copy_aligned(begin(), end(),
701 iterator(__q, 0));
702 this->_M_deallocate();
703 this->_M_impl._M_start = iterator(__q, 0);
704 this->_M_impl._M_end_of_storage = __q + _S_nword(__n);
c62df8fd
PC
705 }
706
232c4925
PC
707 template<typename _Alloc>
708 void
709 vector<bool, _Alloc>::
710 _M_fill_insert(iterator __position, size_type __n, bool __x)
711 {
712 if (__n == 0)
713 return;
714 if (capacity() - size() >= __n)
715 {
716 std::copy_backward(__position, end(),
717 this->_M_impl._M_finish + difference_type(__n));
718 std::fill(__position, __position + difference_type(__n), __x);
719 this->_M_impl._M_finish += difference_type(__n);
720 }
721 else
722 {
be1088fa
ML
723 const size_type __len =
724 _M_check_len(__n, "vector<bool>::_M_fill_insert");
232c4925
PC
725 _Bit_type * __q = this->_M_allocate(__len);
726 iterator __i = _M_copy_aligned(begin(), __position,
727 iterator(__q, 0));
728 std::fill(__i, __i + difference_type(__n), __x);
729 this->_M_impl._M_finish = std::copy(__position, end(),
730 __i + difference_type(__n));
731 this->_M_deallocate();
8a752dfe 732 this->_M_impl._M_end_of_storage = __q + _S_nword(__len);
232c4925
PC
733 this->_M_impl._M_start = iterator(__q, 0);
734 }
735 }
736
737 template<typename _Alloc>
738 template<typename _ForwardIterator>
739 void
740 vector<bool, _Alloc>::
741 _M_insert_range(iterator __position, _ForwardIterator __first,
742 _ForwardIterator __last, std::forward_iterator_tag)
743 {
744 if (__first != __last)
745 {
746 size_type __n = std::distance(__first, __last);
747 if (capacity() - size() >= __n)
748 {
749 std::copy_backward(__position, end(),
750 this->_M_impl._M_finish
751 + difference_type(__n));
752 std::copy(__first, __last, __position);
753 this->_M_impl._M_finish += difference_type(__n);
754 }
755 else
756 {
be1088fa
ML
757 const size_type __len =
758 _M_check_len(__n, "vector<bool>::_M_insert_range");
232c4925
PC
759 _Bit_type * __q = this->_M_allocate(__len);
760 iterator __i = _M_copy_aligned(begin(), __position,
761 iterator(__q, 0));
762 __i = std::copy(__first, __last, __i);
763 this->_M_impl._M_finish = std::copy(__position, end(), __i);
764 this->_M_deallocate();
8a752dfe 765 this->_M_impl._M_end_of_storage = __q + _S_nword(__len);
232c4925
PC
766 this->_M_impl._M_start = iterator(__q, 0);
767 }
768 }
769 }
770
771 template<typename _Alloc>
772 void
773 vector<bool, _Alloc>::
774 _M_insert_aux(iterator __position, bool __x)
775 {
776 if (this->_M_impl._M_finish._M_p != this->_M_impl._M_end_of_storage)
777 {
778 std::copy_backward(__position, this->_M_impl._M_finish,
779 this->_M_impl._M_finish + 1);
780 *__position = __x;
781 ++this->_M_impl._M_finish;
782 }
783 else
784 {
be1088fa
ML
785 const size_type __len =
786 _M_check_len(size_type(1), "vector<bool>::_M_insert_aux");
232c4925
PC
787 _Bit_type * __q = this->_M_allocate(__len);
788 iterator __i = _M_copy_aligned(begin(), __position,
789 iterator(__q, 0));
790 *__i++ = __x;
791 this->_M_impl._M_finish = std::copy(__position, end(), __i);
792 this->_M_deallocate();
8a752dfe 793 this->_M_impl._M_end_of_storage = __q + _S_nword(__len);
232c4925
PC
794 this->_M_impl._M_start = iterator(__q, 0);
795 }
796 }
797
94938aec
PC
798 template<typename _Alloc>
799 typename vector<bool, _Alloc>::iterator
800 vector<bool, _Alloc>::
801 _M_erase(iterator __position)
802 {
803 if (__position + 1 != end())
804 std::copy(__position + 1, end(), __position);
805 --this->_M_impl._M_finish;
806 return __position;
807 }
808
809 template<typename _Alloc>
810 typename vector<bool, _Alloc>::iterator
811 vector<bool, _Alloc>::
812 _M_erase(iterator __first, iterator __last)
813 {
814 if (__first != __last)
815 _M_erase_at_end(std::copy(__last, end(), __first));
816 return __first;
817 }
818
734f5023 819#if __cplusplus >= 201103L
8a752dfe
FD
820 template<typename _Alloc>
821 bool
822 vector<bool, _Alloc>::
823 _M_shrink_to_fit()
824 {
825 if (capacity() - size() < int(_S_word_bit))
826 return false;
827 __try
828 {
829 _M_reallocate(size());
830 return true;
831 }
832 __catch(...)
833 { return false; }
834 }
835#endif
836
12ffa228
BK
837_GLIBCXX_END_NAMESPACE_CONTAINER
838} // namespace std
83144cfc 839
734f5023 840#if __cplusplus >= 201103L
f54e96d9 841
12ffa228
BK
842namespace std _GLIBCXX_VISIBILITY(default)
843{
844_GLIBCXX_BEGIN_NAMESPACE_VERSION
f54e96d9
PC
845
846 template<typename _Alloc>
847 size_t
12ffa228 848 hash<_GLIBCXX_STD_C::vector<bool, _Alloc>>::
72f1c34b 849 operator()(const _GLIBCXX_STD_C::vector<bool, _Alloc>& __b) const noexcept
f54e96d9
PC
850 {
851 size_t __hash = 0;
12ffa228
BK
852 using _GLIBCXX_STD_C::_S_word_bit;
853 using _GLIBCXX_STD_C::_Bit_type;
f54e96d9
PC
854
855 const size_t __words = __b.size() / _S_word_bit;
856 if (__words)
857 {
055f6a47 858 const size_t __clength = __words * sizeof(_Bit_type);
e7f72940 859 __hash = std::_Hash_impl::hash(__b._M_impl._M_start._M_p, __clength);
f54e96d9
PC
860 }
861
862 const size_t __extrabits = __b.size() % _S_word_bit;
863 if (__extrabits)
864 {
865 _Bit_type __hiword = *__b._M_impl._M_finish._M_p;
866 __hiword &= ~((~static_cast<_Bit_type>(0)) << __extrabits);
867
055f6a47 868 const size_t __clength
f54e96d9
PC
869 = (__extrabits + __CHAR_BIT__ - 1) / __CHAR_BIT__;
870 if (__words)
e7f72940 871 __hash = std::_Hash_impl::hash(&__hiword, __clength, __hash);
f54e96d9 872 else
e7f72940 873 __hash = std::_Hash_impl::hash(&__hiword, __clength);
f54e96d9
PC
874 }
875
876 return __hash;
877 }
878
12ffa228
BK
879_GLIBCXX_END_NAMESPACE_VERSION
880} // namespace std
f54e96d9 881
734f5023 882#endif // C++11
f54e96d9 883
3d7c150e 884#endif /* _VECTOR_TCC */