61 _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
63 template<
typename _Tp,
typename _Alloc>
66 reserve(size_type __n)
68 if (__n > this->max_size())
69 __throw_length_error(__N(
"vector::reserve"));
70 if (this->capacity() < __n)
72 const size_type __old_size = size();
73 pointer __tmp = _M_allocate_and_copy(__n,
76 std::_Destroy(this->_M_impl._M_start, this->_M_impl._M_finish,
77 _M_get_Tp_allocator());
78 _M_deallocate(this->_M_impl._M_start,
79 this->_M_impl._M_end_of_storage
80 - this->_M_impl._M_start);
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;
87 #if __cplusplus >= 201103L
88 template<
typename _Tp,
typename _Alloc>
89 template<
typename... _Args>
92 emplace_back(_Args&&... __args)
94 if (this->_M_impl._M_finish != this->_M_impl._M_end_of_storage)
96 _Alloc_traits::construct(this->_M_impl, this->_M_impl._M_finish,
97 std::forward<_Args>(__args)...);
98 ++this->_M_impl._M_finish;
101 _M_emplace_back_aux(std::forward<_Args>(__args)...);
105 template<
typename _Tp,
typename _Alloc>
106 typename vector<_Tp, _Alloc>::iterator
107 vector<_Tp, _Alloc>::
108 insert(iterator __position,
const value_type& __x)
110 const size_type __n = __position - begin();
111 if (this->_M_impl._M_finish != this->_M_impl._M_end_of_storage
112 && __position == end())
114 _Alloc_traits::construct(this->_M_impl, this->_M_impl._M_finish, __x);
115 ++this->_M_impl._M_finish;
119 #if __cplusplus >= 201103L
120 if (this->_M_impl._M_finish != this->_M_impl._M_end_of_storage)
123 _M_insert_aux(__position, std::move(__x_copy));
127 _M_insert_aux(__position, __x);
129 return iterator(this->_M_impl._M_start + __n);
132 template<
typename _Tp,
typename _Alloc>
133 typename vector<_Tp, _Alloc>::iterator
134 vector<_Tp, _Alloc>::
135 erase(iterator __position)
137 if (__position + 1 != end())
139 --this->_M_impl._M_finish;
140 _Alloc_traits::destroy(this->_M_impl, this->_M_impl._M_finish);
144 template<
typename _Tp,
typename _Alloc>
145 typename vector<_Tp, _Alloc>::iterator
146 vector<_Tp, _Alloc>::
147 erase(iterator __first, iterator __last)
149 if (__first != __last)
153 _M_erase_at_end(__first.base() + (end() - __last));
158 template<
typename _Tp,
typename _Alloc>
160 vector<_Tp, _Alloc>::
161 operator=(
const vector<_Tp, _Alloc>& __x)
165 #if __cplusplus >= 201103L
166 if (_Alloc_traits::_S_propagate_on_copy_assign())
168 if (!_Alloc_traits::_S_always_equal()
169 && _M_get_Tp_allocator() != __x._M_get_Tp_allocator())
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 =
nullptr;
177 this->_M_impl._M_finish =
nullptr;
178 this->_M_impl._M_end_of_storage =
nullptr;
180 std::__alloc_on_copy(_M_get_Tp_allocator(),
181 __x._M_get_Tp_allocator());
184 const size_type __xlen = __x.size();
185 if (__xlen > capacity())
187 pointer __tmp = _M_allocate_and_copy(__xlen, __x.begin(),
189 std::_Destroy(this->_M_impl._M_start, this->_M_impl._M_finish,
190 _M_get_Tp_allocator());
191 _M_deallocate(this->_M_impl._M_start,
192 this->_M_impl._M_end_of_storage
193 - this->_M_impl._M_start);
194 this->_M_impl._M_start = __tmp;
195 this->_M_impl._M_end_of_storage = this->_M_impl._M_start + __xlen;
197 else if (size() >= __xlen)
199 std::_Destroy(std::copy(__x.begin(), __x.end(), begin()),
200 end(), _M_get_Tp_allocator());
204 std::copy(__x._M_impl._M_start, __x._M_impl._M_start + size(),
205 this->_M_impl._M_start);
206 std::__uninitialized_copy_a(__x._M_impl._M_start + size(),
207 __x._M_impl._M_finish,
208 this->_M_impl._M_finish,
209 _M_get_Tp_allocator());
211 this->_M_impl._M_finish = this->_M_impl._M_start + __xlen;
216 template<
typename _Tp,
typename _Alloc>
218 vector<_Tp, _Alloc>::
219 _M_fill_assign(
size_t __n,
const value_type& __val)
221 if (__n > capacity())
223 vector __tmp(__n, __val, _M_get_Tp_allocator());
226 else if (__n > size())
228 std::fill(begin(), end(), __val);
229 std::__uninitialized_fill_n_a(this->_M_impl._M_finish,
231 _M_get_Tp_allocator());
232 this->_M_impl._M_finish += __n - size();
235 _M_erase_at_end(std::fill_n(this->_M_impl._M_start, __n, __val));
238 template<
typename _Tp,
typename _Alloc>
239 template<
typename _InputIterator>
241 vector<_Tp, _Alloc>::
242 _M_assign_aux(_InputIterator __first, _InputIterator __last,
243 std::input_iterator_tag)
245 pointer __cur(this->_M_impl._M_start);
246 for (; __first != __last && __cur != this->_M_impl._M_finish;
249 if (__first == __last)
250 _M_erase_at_end(__cur);
252 insert(end(), __first, __last);
255 template<
typename _Tp,
typename _Alloc>
256 template<
typename _ForwardIterator>
258 vector<_Tp, _Alloc>::
259 _M_assign_aux(_ForwardIterator __first, _ForwardIterator __last,
260 std::forward_iterator_tag)
262 const size_type __len = std::distance(__first, __last);
264 if (__len > capacity())
266 pointer __tmp(_M_allocate_and_copy(__len, __first, __last));
267 std::_Destroy(this->_M_impl._M_start, this->_M_impl._M_finish,
268 _M_get_Tp_allocator());
269 _M_deallocate(this->_M_impl._M_start,
270 this->_M_impl._M_end_of_storage
271 - this->_M_impl._M_start);
272 this->_M_impl._M_start = __tmp;
273 this->_M_impl._M_finish = this->_M_impl._M_start + __len;
274 this->_M_impl._M_end_of_storage = this->_M_impl._M_finish;
276 else if (size() >= __len)
277 _M_erase_at_end(std::copy(__first, __last, this->_M_impl._M_start));
280 _ForwardIterator __mid = __first;
281 std::advance(__mid, size());
282 std::copy(__first, __mid, this->_M_impl._M_start);
283 this->_M_impl._M_finish =
284 std::__uninitialized_copy_a(__mid, __last,
285 this->_M_impl._M_finish,
286 _M_get_Tp_allocator());
290 #if __cplusplus >= 201103L
291 template<
typename _Tp,
typename _Alloc>
292 template<
typename... _Args>
293 typename vector<_Tp, _Alloc>::iterator
294 vector<_Tp, _Alloc>::
295 emplace(iterator __position, _Args&&... __args)
297 const size_type __n = __position - begin();
298 if (this->_M_impl._M_finish != this->_M_impl._M_end_of_storage
299 && __position == end())
301 _Alloc_traits::construct(this->_M_impl, this->_M_impl._M_finish,
302 std::forward<_Args>(__args)...);
303 ++this->_M_impl._M_finish;
306 _M_insert_aux(__position, std::forward<_Args>(__args)...);
307 return iterator(this->_M_impl._M_start + __n);
310 template<
typename _Tp,
typename _Alloc>
311 template<
typename... _Args>
313 vector<_Tp, _Alloc>::
314 _M_insert_aux(iterator __position, _Args&&... __args)
316 template<
typename _Tp,
typename _Alloc>
318 vector<_Tp, _Alloc>::
319 _M_insert_aux(iterator __position,
const _Tp& __x)
322 if (this->_M_impl._M_finish != this->_M_impl._M_end_of_storage)
324 _Alloc_traits::construct(this->_M_impl, this->_M_impl._M_finish,
327 ++this->_M_impl._M_finish;
328 #if __cplusplus < 201103L
332 this->_M_impl._M_finish - 2,
333 this->_M_impl._M_finish - 1);
334 #if __cplusplus < 201103L
335 *__position = __x_copy;
337 *__position = _Tp(std::forward<_Args>(__args)...);
342 const size_type __len =
343 _M_check_len(size_type(1),
"vector::_M_insert_aux");
344 const size_type __elems_before = __position - begin();
345 pointer __new_start(this->_M_allocate(__len));
346 pointer __new_finish(__new_start);
353 _Alloc_traits::construct(this->_M_impl,
354 __new_start + __elems_before,
355 #
if __cplusplus >= 201103L
356 std::forward<_Args>(__args)...);
363 = std::__uninitialized_move_if_noexcept_a
364 (this->_M_impl._M_start, __position.base(),
365 __new_start, _M_get_Tp_allocator());
370 = std::__uninitialized_move_if_noexcept_a
371 (__position.base(), this->_M_impl._M_finish,
372 __new_finish, _M_get_Tp_allocator());
377 _Alloc_traits::destroy(this->_M_impl,
378 __new_start + __elems_before);
380 std::_Destroy(__new_start, __new_finish, _M_get_Tp_allocator());
381 _M_deallocate(__new_start, __len);
384 std::_Destroy(this->_M_impl._M_start, this->_M_impl._M_finish,
385 _M_get_Tp_allocator());
386 _M_deallocate(this->_M_impl._M_start,
387 this->_M_impl._M_end_of_storage
388 - this->_M_impl._M_start);
389 this->_M_impl._M_start = __new_start;
390 this->_M_impl._M_finish = __new_finish;
391 this->_M_impl._M_end_of_storage = __new_start + __len;
395 #if __cplusplus >= 201103L
396 template<
typename _Tp,
typename _Alloc>
397 template<
typename... _Args>
399 vector<_Tp, _Alloc>::
400 _M_emplace_back_aux(_Args&&... __args)
402 const size_type __len =
403 _M_check_len(size_type(1),
"vector::_M_emplace_back_aux");
404 pointer __new_start(this->_M_allocate(__len));
405 pointer __new_finish(__new_start);
408 _Alloc_traits::construct(this->_M_impl, __new_start + size(),
409 std::forward<_Args>(__args)...);
413 = std::__uninitialized_move_if_noexcept_a
414 (this->_M_impl._M_start, this->_M_impl._M_finish,
415 __new_start, _M_get_Tp_allocator());
422 _Alloc_traits::destroy(this->_M_impl, __new_start + size());
424 std::_Destroy(__new_start, __new_finish, _M_get_Tp_allocator());
425 _M_deallocate(__new_start, __len);
428 std::_Destroy(this->_M_impl._M_start, this->_M_impl._M_finish,
429 _M_get_Tp_allocator());
430 _M_deallocate(this->_M_impl._M_start,
431 this->_M_impl._M_end_of_storage
432 - this->_M_impl._M_start);
433 this->_M_impl._M_start = __new_start;
434 this->_M_impl._M_finish = __new_finish;
435 this->_M_impl._M_end_of_storage = __new_start + __len;
439 template<
typename _Tp,
typename _Alloc>
441 vector<_Tp, _Alloc>::
442 _M_fill_insert(iterator __position, size_type __n,
const value_type& __x)
446 if (size_type(this->_M_impl._M_end_of_storage
447 - this->_M_impl._M_finish) >= __n)
449 value_type __x_copy = __x;
450 const size_type __elems_after = end() - __position;
451 pointer __old_finish(this->_M_impl._M_finish);
452 if (__elems_after > __n)
454 std::__uninitialized_move_a(this->_M_impl._M_finish - __n,
455 this->_M_impl._M_finish,
456 this->_M_impl._M_finish,
457 _M_get_Tp_allocator());
458 this->_M_impl._M_finish += __n;
460 __old_finish - __n, __old_finish);
461 std::fill(__position.base(), __position.base() + __n,
466 std::__uninitialized_fill_n_a(this->_M_impl._M_finish,
469 _M_get_Tp_allocator());
470 this->_M_impl._M_finish += __n - __elems_after;
471 std::__uninitialized_move_a(__position.base(), __old_finish,
472 this->_M_impl._M_finish,
473 _M_get_Tp_allocator());
474 this->_M_impl._M_finish += __elems_after;
475 std::fill(__position.base(), __old_finish, __x_copy);
480 const size_type __len =
481 _M_check_len(__n,
"vector::_M_fill_insert");
482 const size_type __elems_before = __position - begin();
483 pointer __new_start(this->_M_allocate(__len));
484 pointer __new_finish(__new_start);
488 std::__uninitialized_fill_n_a(__new_start + __elems_before,
490 _M_get_Tp_allocator());
494 = std::__uninitialized_move_if_noexcept_a
495 (this->_M_impl._M_start, __position.base(),
496 __new_start, _M_get_Tp_allocator());
501 = std::__uninitialized_move_if_noexcept_a
502 (__position.base(), this->_M_impl._M_finish,
503 __new_finish, _M_get_Tp_allocator());
508 std::_Destroy(__new_start + __elems_before,
509 __new_start + __elems_before + __n,
510 _M_get_Tp_allocator());
512 std::_Destroy(__new_start, __new_finish,
513 _M_get_Tp_allocator());
514 _M_deallocate(__new_start, __len);
517 std::_Destroy(this->_M_impl._M_start, this->_M_impl._M_finish,
518 _M_get_Tp_allocator());
519 _M_deallocate(this->_M_impl._M_start,
520 this->_M_impl._M_end_of_storage
521 - this->_M_impl._M_start);
522 this->_M_impl._M_start = __new_start;
523 this->_M_impl._M_finish = __new_finish;
524 this->_M_impl._M_end_of_storage = __new_start + __len;
529 #if __cplusplus >= 201103L
530 template<
typename _Tp,
typename _Alloc>
532 vector<_Tp, _Alloc>::
533 _M_default_append(size_type __n)
537 if (size_type(this->_M_impl._M_end_of_storage
538 - this->_M_impl._M_finish) >= __n)
540 std::__uninitialized_default_n_a(this->_M_impl._M_finish,
541 __n, _M_get_Tp_allocator());
542 this->_M_impl._M_finish += __n;
546 const size_type __len =
547 _M_check_len(__n,
"vector::_M_default_append");
548 const size_type __old_size = this->size();
549 pointer __new_start(this->_M_allocate(__len));
550 pointer __new_finish(__new_start);
554 = std::__uninitialized_move_if_noexcept_a
555 (this->_M_impl._M_start, this->_M_impl._M_finish,
556 __new_start, _M_get_Tp_allocator());
557 std::__uninitialized_default_n_a(__new_finish, __n,
558 _M_get_Tp_allocator());
563 std::_Destroy(__new_start, __new_finish,
564 _M_get_Tp_allocator());
565 _M_deallocate(__new_start, __len);
568 std::_Destroy(this->_M_impl._M_start, this->_M_impl._M_finish,
569 _M_get_Tp_allocator());
570 _M_deallocate(this->_M_impl._M_start,
571 this->_M_impl._M_end_of_storage
572 - this->_M_impl._M_start);
573 this->_M_impl._M_start = __new_start;
574 this->_M_impl._M_finish = __new_finish;
575 this->_M_impl._M_end_of_storage = __new_start + __len;
580 template<
typename _Tp,
typename _Alloc>
582 vector<_Tp, _Alloc>::
585 if (capacity() == size())
587 return std::__shrink_to_fit_aux<vector>::_S_do_it(*
this);
591 template<
typename _Tp,
typename _Alloc>
592 template<
typename _InputIterator>
594 vector<_Tp, _Alloc>::
595 _M_range_insert(iterator __pos, _InputIterator __first,
596 _InputIterator __last, std::input_iterator_tag)
598 for (; __first != __last; ++__first)
600 __pos = insert(__pos, *__first);
605 template<
typename _Tp,
typename _Alloc>
606 template<
typename _ForwardIterator>
608 vector<_Tp, _Alloc>::
609 _M_range_insert(iterator __position, _ForwardIterator __first,
610 _ForwardIterator __last, std::forward_iterator_tag)
612 if (__first != __last)
614 const size_type __n = std::distance(__first, __last);
615 if (size_type(this->_M_impl._M_end_of_storage
616 - this->_M_impl._M_finish) >= __n)
618 const size_type __elems_after = end() - __position;
619 pointer __old_finish(this->_M_impl._M_finish);
620 if (__elems_after > __n)
622 std::__uninitialized_move_a(this->_M_impl._M_finish - __n,
623 this->_M_impl._M_finish,
624 this->_M_impl._M_finish,
625 _M_get_Tp_allocator());
626 this->_M_impl._M_finish += __n;
628 __old_finish - __n, __old_finish);
629 std::copy(__first, __last, __position);
633 _ForwardIterator __mid = __first;
634 std::advance(__mid, __elems_after);
635 std::__uninitialized_copy_a(__mid, __last,
636 this->_M_impl._M_finish,
637 _M_get_Tp_allocator());
638 this->_M_impl._M_finish += __n - __elems_after;
639 std::__uninitialized_move_a(__position.base(),
641 this->_M_impl._M_finish,
642 _M_get_Tp_allocator());
643 this->_M_impl._M_finish += __elems_after;
644 std::copy(__first, __mid, __position);
649 const size_type __len =
650 _M_check_len(__n,
"vector::_M_range_insert");
651 pointer __new_start(this->_M_allocate(__len));
652 pointer __new_finish(__new_start);
656 = std::__uninitialized_move_if_noexcept_a
657 (this->_M_impl._M_start, __position.base(),
658 __new_start, _M_get_Tp_allocator());
660 = std::__uninitialized_copy_a(__first, __last,
662 _M_get_Tp_allocator());
664 = std::__uninitialized_move_if_noexcept_a
665 (__position.base(), this->_M_impl._M_finish,
666 __new_finish, _M_get_Tp_allocator());
670 std::_Destroy(__new_start, __new_finish,
671 _M_get_Tp_allocator());
672 _M_deallocate(__new_start, __len);
675 std::_Destroy(this->_M_impl._M_start, this->_M_impl._M_finish,
676 _M_get_Tp_allocator());
677 _M_deallocate(this->_M_impl._M_start,
678 this->_M_impl._M_end_of_storage
679 - this->_M_impl._M_start);
680 this->_M_impl._M_start = __new_start;
681 this->_M_impl._M_finish = __new_finish;
682 this->_M_impl._M_end_of_storage = __new_start + __len;
689 template<
typename _Alloc>
691 vector<bool, _Alloc>::
692 _M_reallocate(size_type __n)
694 _Bit_type* __q = this->_M_allocate(__n);
695 this->_M_impl._M_finish = _M_copy_aligned(begin(), end(),
697 this->_M_deallocate();
698 this->_M_impl._M_start = iterator(__q, 0);
699 this->_M_impl._M_end_of_storage = __q + _S_nword(__n);
702 template<
typename _Alloc>
704 vector<bool, _Alloc>::
705 _M_fill_insert(iterator __position, size_type __n,
bool __x)
709 if (capacity() - size() >= __n)
711 std::copy_backward(__position, end(),
712 this->_M_impl._M_finish + difference_type(__n));
713 std::fill(__position, __position + difference_type(__n), __x);
714 this->_M_impl._M_finish += difference_type(__n);
718 const size_type __len =
719 _M_check_len(__n,
"vector<bool>::_M_fill_insert");
720 _Bit_type * __q = this->_M_allocate(__len);
721 iterator __i = _M_copy_aligned(begin(), __position,
723 std::fill(__i, __i + difference_type(__n), __x);
724 this->_M_impl._M_finish = std::copy(__position, end(),
725 __i + difference_type(__n));
726 this->_M_deallocate();
727 this->_M_impl._M_end_of_storage = __q + _S_nword(__len);
728 this->_M_impl._M_start = iterator(__q, 0);
732 template<
typename _Alloc>
733 template<
typename _ForwardIterator>
735 vector<bool, _Alloc>::
736 _M_insert_range(iterator __position, _ForwardIterator __first,
737 _ForwardIterator __last, std::forward_iterator_tag)
739 if (__first != __last)
741 size_type __n = std::distance(__first, __last);
742 if (capacity() - size() >= __n)
744 std::copy_backward(__position, end(),
745 this->_M_impl._M_finish
746 + difference_type(__n));
747 std::copy(__first, __last, __position);
748 this->_M_impl._M_finish += difference_type(__n);
752 const size_type __len =
753 _M_check_len(__n,
"vector<bool>::_M_insert_range");
754 _Bit_type * __q = this->_M_allocate(__len);
755 iterator __i = _M_copy_aligned(begin(), __position,
757 __i = std::copy(__first, __last, __i);
758 this->_M_impl._M_finish = std::copy(__position, end(), __i);
759 this->_M_deallocate();
760 this->_M_impl._M_end_of_storage = __q + _S_nword(__len);
761 this->_M_impl._M_start = iterator(__q, 0);
766 template<
typename _Alloc>
768 vector<bool, _Alloc>::
769 _M_insert_aux(iterator __position,
bool __x)
771 if (this->_M_impl._M_finish._M_p != this->_M_impl._M_end_of_storage)
773 std::copy_backward(__position, this->_M_impl._M_finish,
774 this->_M_impl._M_finish + 1);
776 ++this->_M_impl._M_finish;
780 const size_type __len =
781 _M_check_len(size_type(1),
"vector<bool>::_M_insert_aux");
782 _Bit_type * __q = this->_M_allocate(__len);
783 iterator __i = _M_copy_aligned(begin(), __position,
786 this->_M_impl._M_finish = std::copy(__position, end(), __i);
787 this->_M_deallocate();
788 this->_M_impl._M_end_of_storage = __q + _S_nword(__len);
789 this->_M_impl._M_start = iterator(__q, 0);
793 #if __cplusplus >= 201103L
794 template<
typename _Alloc>
796 vector<bool, _Alloc>::
799 if (capacity() - size() <
int(_S_word_bit))
803 _M_reallocate(size());
811 _GLIBCXX_END_NAMESPACE_CONTAINER
#define _GLIBCXX_MOVE(__val)
Definition: move.h:145
#define __try
Definition: exception_defines.h:35
#define __throw_exception_again
Definition: exception_defines.h:37
#define _GLIBCXX_MOVE_BACKWARD3(_Tp, _Up, _Vp)
#define _GLIBCXX_MOVE3(_Tp, _Up, _Vp)
#define __catch(X)
Definition: exception_defines.h:36
#define _GLIBCXX_MAKE_MOVE_IF_NOEXCEPT_ITERATOR(_Iter)
Definition: stl_iterator.h:1145