File indexing completed on 2026-05-03 08:14:07
0001
0002
0003
0004
0005
0006
0007
0008
0009 #ifndef _LIBCPP___VECTOR_VECTOR_H
0010 #define _LIBCPP___VECTOR_VECTOR_H
0011
0012 #include <__algorithm/copy.h>
0013 #include <__algorithm/copy_n.h>
0014 #include <__algorithm/fill_n.h>
0015 #include <__algorithm/max.h>
0016 #include <__algorithm/min.h>
0017 #include <__algorithm/move.h>
0018 #include <__algorithm/move_backward.h>
0019 #include <__algorithm/ranges_copy_n.h>
0020 #include <__algorithm/rotate.h>
0021 #include <__assert>
0022 #include <__config>
0023 #include <__debug_utils/sanitizers.h>
0024 #include <__format/enable_insertable.h>
0025 #include <__fwd/vector.h>
0026 #include <__iterator/advance.h>
0027 #include <__iterator/bounded_iter.h>
0028 #include <__iterator/concepts.h>
0029 #include <__iterator/distance.h>
0030 #include <__iterator/iterator_traits.h>
0031 #include <__iterator/move_iterator.h>
0032 #include <__iterator/next.h>
0033 #include <__iterator/reverse_iterator.h>
0034 #include <__iterator/wrap_iter.h>
0035 #include <__memory/addressof.h>
0036 #include <__memory/allocate_at_least.h>
0037 #include <__memory/allocator.h>
0038 #include <__memory/allocator_traits.h>
0039 #include <__memory/compressed_pair.h>
0040 #include <__memory/noexcept_move_assign_container.h>
0041 #include <__memory/pointer_traits.h>
0042 #include <__memory/swap_allocator.h>
0043 #include <__memory/temp_value.h>
0044 #include <__memory/uninitialized_algorithms.h>
0045 #include <__ranges/access.h>
0046 #include <__ranges/concepts.h>
0047 #include <__ranges/container_compatible_range.h>
0048 #include <__ranges/from_range.h>
0049 #include <__split_buffer>
0050 #include <__type_traits/conditional.h>
0051 #include <__type_traits/enable_if.h>
0052 #include <__type_traits/is_allocator.h>
0053 #include <__type_traits/is_constant_evaluated.h>
0054 #include <__type_traits/is_constructible.h>
0055 #include <__type_traits/is_nothrow_assignable.h>
0056 #include <__type_traits/is_nothrow_constructible.h>
0057 #include <__type_traits/is_pointer.h>
0058 #include <__type_traits/is_same.h>
0059 #include <__type_traits/is_trivially_relocatable.h>
0060 #include <__type_traits/type_identity.h>
0061 #include <__utility/exception_guard.h>
0062 #include <__utility/forward.h>
0063 #include <__utility/is_pointer_in_range.h>
0064 #include <__utility/move.h>
0065 #include <__utility/pair.h>
0066 #include <__utility/swap.h>
0067 #include <initializer_list>
0068 #include <limits>
0069 #include <stdexcept>
0070
0071
0072 #include <__vector/comparison.h>
0073 #include <__vector/container_traits.h>
0074 #include <__vector/swap.h>
0075
0076 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
0077 # pragma GCC system_header
0078 #endif
0079
0080 _LIBCPP_PUSH_MACROS
0081 #include <__undef_macros>
0082
0083 _LIBCPP_BEGIN_NAMESPACE_STD
0084
0085 template <class _Tp, class _Allocator >
0086 class _LIBCPP_TEMPLATE_VIS vector {
0087 private:
0088 typedef allocator<_Tp> __default_allocator_type;
0089
0090 public:
0091
0092
0093
0094 typedef vector __self;
0095 typedef _Tp value_type;
0096 typedef _Allocator allocator_type;
0097 typedef allocator_traits<allocator_type> __alloc_traits;
0098 typedef value_type& reference;
0099 typedef const value_type& const_reference;
0100 typedef typename __alloc_traits::size_type size_type;
0101 typedef typename __alloc_traits::difference_type difference_type;
0102 typedef typename __alloc_traits::pointer pointer;
0103 typedef typename __alloc_traits::const_pointer const_pointer;
0104 #ifdef _LIBCPP_ABI_BOUNDED_ITERATORS_IN_VECTOR
0105
0106
0107
0108 typedef __bounded_iter<__wrap_iter<pointer> > iterator;
0109 typedef __bounded_iter<__wrap_iter<const_pointer> > const_iterator;
0110 #else
0111 typedef __wrap_iter<pointer> iterator;
0112 typedef __wrap_iter<const_pointer> const_iterator;
0113 #endif
0114 typedef std::reverse_iterator<iterator> reverse_iterator;
0115 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
0116
0117
0118
0119
0120
0121 using __trivially_relocatable _LIBCPP_NODEBUG = __conditional_t<
0122 __libcpp_is_trivially_relocatable<pointer>::value && __libcpp_is_trivially_relocatable<allocator_type>::value,
0123 vector,
0124 void>;
0125
0126 static_assert(__check_valid_allocator<allocator_type>::value, "");
0127 static_assert(is_same<typename allocator_type::value_type, value_type>::value,
0128 "Allocator::value_type must be same type as value_type");
0129
0130
0131
0132
0133 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI vector()
0134 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value) {}
0135 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI explicit vector(const allocator_type& __a)
0136 #if _LIBCPP_STD_VER <= 14
0137 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value)
0138 #else
0139 noexcept
0140 #endif
0141 : __alloc_(__a) {
0142 }
0143
0144 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI explicit vector(size_type __n) {
0145 auto __guard = std::__make_exception_guard(__destroy_vector(*this));
0146 if (__n > 0) {
0147 __vallocate(__n);
0148 __construct_at_end(__n);
0149 }
0150 __guard.__complete();
0151 }
0152
0153 #if _LIBCPP_STD_VER >= 14
0154 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI explicit vector(size_type __n, const allocator_type& __a)
0155 : __alloc_(__a) {
0156 auto __guard = std::__make_exception_guard(__destroy_vector(*this));
0157 if (__n > 0) {
0158 __vallocate(__n);
0159 __construct_at_end(__n);
0160 }
0161 __guard.__complete();
0162 }
0163 #endif
0164
0165 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI vector(size_type __n, const value_type& __x) {
0166 auto __guard = std::__make_exception_guard(__destroy_vector(*this));
0167 if (__n > 0) {
0168 __vallocate(__n);
0169 __construct_at_end(__n, __x);
0170 }
0171 __guard.__complete();
0172 }
0173
0174 template <__enable_if_t<__is_allocator<_Allocator>::value, int> = 0>
0175 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI
0176 vector(size_type __n, const value_type& __x, const allocator_type& __a)
0177 : __alloc_(__a) {
0178 auto __guard = std::__make_exception_guard(__destroy_vector(*this));
0179 if (__n > 0) {
0180 __vallocate(__n);
0181 __construct_at_end(__n, __x);
0182 }
0183 __guard.__complete();
0184 }
0185
0186 template <class _InputIterator,
0187 __enable_if_t<__has_exactly_input_iterator_category<_InputIterator>::value &&
0188 is_constructible<value_type, typename iterator_traits<_InputIterator>::reference>::value,
0189 int> = 0>
0190 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI vector(_InputIterator __first, _InputIterator __last) {
0191 __init_with_sentinel(__first, __last);
0192 }
0193
0194 template <class _InputIterator,
0195 __enable_if_t<__has_exactly_input_iterator_category<_InputIterator>::value &&
0196 is_constructible<value_type, typename iterator_traits<_InputIterator>::reference>::value,
0197 int> = 0>
0198 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI
0199 vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a)
0200 : __alloc_(__a) {
0201 __init_with_sentinel(__first, __last);
0202 }
0203
0204 template <
0205 class _ForwardIterator,
0206 __enable_if_t<__has_forward_iterator_category<_ForwardIterator>::value &&
0207 is_constructible<value_type, typename iterator_traits<_ForwardIterator>::reference>::value,
0208 int> = 0>
0209 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI vector(_ForwardIterator __first, _ForwardIterator __last) {
0210 size_type __n = static_cast<size_type>(std::distance(__first, __last));
0211 __init_with_size(__first, __last, __n);
0212 }
0213
0214 template <
0215 class _ForwardIterator,
0216 __enable_if_t<__has_forward_iterator_category<_ForwardIterator>::value &&
0217 is_constructible<value_type, typename iterator_traits<_ForwardIterator>::reference>::value,
0218 int> = 0>
0219 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI
0220 vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a)
0221 : __alloc_(__a) {
0222 size_type __n = static_cast<size_type>(std::distance(__first, __last));
0223 __init_with_size(__first, __last, __n);
0224 }
0225
0226 #if _LIBCPP_STD_VER >= 23
0227 template <_ContainerCompatibleRange<_Tp> _Range>
0228 _LIBCPP_HIDE_FROM_ABI constexpr vector(
0229 from_range_t, _Range&& __range, const allocator_type& __alloc = allocator_type())
0230 : __alloc_(__alloc) {
0231 if constexpr (ranges::forward_range<_Range> || ranges::sized_range<_Range>) {
0232 auto __n = static_cast<size_type>(ranges::distance(__range));
0233 __init_with_size(ranges::begin(__range), ranges::end(__range), __n);
0234
0235 } else {
0236 __init_with_sentinel(ranges::begin(__range), ranges::end(__range));
0237 }
0238 }
0239 #endif
0240
0241 private:
0242 class __destroy_vector {
0243 public:
0244 _LIBCPP_CONSTEXPR _LIBCPP_HIDE_FROM_ABI __destroy_vector(vector& __vec) : __vec_(__vec) {}
0245
0246 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void operator()() {
0247 if (__vec_.__begin_ != nullptr) {
0248 __vec_.clear();
0249 __vec_.__annotate_delete();
0250 __alloc_traits::deallocate(__vec_.__alloc_, __vec_.__begin_, __vec_.capacity());
0251 }
0252 }
0253
0254 private:
0255 vector& __vec_;
0256 };
0257
0258 public:
0259 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI ~vector() { __destroy_vector (*this)(); }
0260
0261 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI vector(const vector& __x)
0262 : __alloc_(__alloc_traits::select_on_container_copy_construction(__x.__alloc_)) {
0263 __init_with_size(__x.__begin_, __x.__end_, __x.size());
0264 }
0265 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI
0266 vector(const vector& __x, const __type_identity_t<allocator_type>& __a)
0267 : __alloc_(__a) {
0268 __init_with_size(__x.__begin_, __x.__end_, __x.size());
0269 }
0270 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI vector& operator=(const vector& __x);
0271
0272 #ifndef _LIBCPP_CXX03_LANG
0273 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI vector(initializer_list<value_type> __il) {
0274 __init_with_size(__il.begin(), __il.end(), __il.size());
0275 }
0276
0277 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI
0278 vector(initializer_list<value_type> __il, const allocator_type& __a)
0279 : __alloc_(__a) {
0280 __init_with_size(__il.begin(), __il.end(), __il.size());
0281 }
0282
0283 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI vector& operator=(initializer_list<value_type> __il) {
0284 assign(__il.begin(), __il.end());
0285 return *this;
0286 }
0287 #endif
0288
0289 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI vector(vector&& __x)
0290 #if _LIBCPP_STD_VER >= 17
0291 noexcept;
0292 #else
0293 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value);
0294 #endif
0295
0296 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI
0297 vector(vector&& __x, const __type_identity_t<allocator_type>& __a);
0298 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI vector& operator=(vector&& __x)
0299 _NOEXCEPT_(__noexcept_move_assign_container<_Allocator, __alloc_traits>::value) {
0300 __move_assign(__x, integral_constant<bool, __alloc_traits::propagate_on_container_move_assignment::value>());
0301 return *this;
0302 }
0303
0304 template <class _InputIterator,
0305 __enable_if_t<__has_exactly_input_iterator_category<_InputIterator>::value &&
0306 is_constructible<value_type, typename iterator_traits<_InputIterator>::reference>::value,
0307 int> = 0>
0308 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void assign(_InputIterator __first, _InputIterator __last) {
0309 __assign_with_sentinel(__first, __last);
0310 }
0311 template <
0312 class _ForwardIterator,
0313 __enable_if_t<__has_forward_iterator_category<_ForwardIterator>::value &&
0314 is_constructible<value_type, typename iterator_traits<_ForwardIterator>::reference>::value,
0315 int> = 0>
0316 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void assign(_ForwardIterator __first, _ForwardIterator __last) {
0317 __assign_with_size(__first, __last, std::distance(__first, __last));
0318 }
0319
0320 #if _LIBCPP_STD_VER >= 23
0321 template <_ContainerCompatibleRange<_Tp> _Range>
0322 _LIBCPP_HIDE_FROM_ABI constexpr void assign_range(_Range&& __range) {
0323 if constexpr (ranges::forward_range<_Range> || ranges::sized_range<_Range>) {
0324 auto __n = static_cast<size_type>(ranges::distance(__range));
0325 __assign_with_size(ranges::begin(__range), ranges::end(__range), __n);
0326
0327 } else {
0328 __assign_with_sentinel(ranges::begin(__range), ranges::end(__range));
0329 }
0330 }
0331 #endif
0332
0333 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void assign(size_type __n, const_reference __u);
0334
0335 #ifndef _LIBCPP_CXX03_LANG
0336 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void assign(initializer_list<value_type> __il) {
0337 assign(__il.begin(), __il.end());
0338 }
0339 #endif
0340
0341 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI allocator_type get_allocator() const _NOEXCEPT {
0342 return this->__alloc_;
0343 }
0344
0345
0346
0347
0348 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI iterator begin() _NOEXCEPT {
0349 return __make_iter(__add_alignment_assumption(this->__begin_));
0350 }
0351 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI const_iterator begin() const _NOEXCEPT {
0352 return __make_iter(__add_alignment_assumption(this->__begin_));
0353 }
0354 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI iterator end() _NOEXCEPT {
0355 return __make_iter(__add_alignment_assumption(this->__end_));
0356 }
0357 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI const_iterator end() const _NOEXCEPT {
0358 return __make_iter(__add_alignment_assumption(this->__end_));
0359 }
0360
0361 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI reverse_iterator rbegin() _NOEXCEPT {
0362 return reverse_iterator(end());
0363 }
0364 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI const_reverse_iterator rbegin() const _NOEXCEPT {
0365 return const_reverse_iterator(end());
0366 }
0367 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI reverse_iterator rend() _NOEXCEPT {
0368 return reverse_iterator(begin());
0369 }
0370 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI const_reverse_iterator rend() const _NOEXCEPT {
0371 return const_reverse_iterator(begin());
0372 }
0373
0374 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI const_iterator cbegin() const _NOEXCEPT { return begin(); }
0375 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI const_iterator cend() const _NOEXCEPT { return end(); }
0376 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI const_reverse_iterator crbegin() const _NOEXCEPT {
0377 return rbegin();
0378 }
0379 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI const_reverse_iterator crend() const _NOEXCEPT { return rend(); }
0380
0381
0382
0383
0384 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI size_type size() const _NOEXCEPT {
0385 return static_cast<size_type>(this->__end_ - this->__begin_);
0386 }
0387 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI size_type capacity() const _NOEXCEPT {
0388 return static_cast<size_type>(this->__cap_ - this->__begin_);
0389 }
0390 [[__nodiscard__]] _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI bool empty() const _NOEXCEPT {
0391 return this->__begin_ == this->__end_;
0392 }
0393 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI size_type max_size() const _NOEXCEPT {
0394 return std::min<size_type>(__alloc_traits::max_size(this->__alloc_), numeric_limits<difference_type>::max());
0395 }
0396 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void reserve(size_type __n);
0397 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void shrink_to_fit() _NOEXCEPT;
0398
0399
0400
0401
0402 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI reference operator[](size_type __n) _NOEXCEPT {
0403 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(__n < size(), "vector[] index out of bounds");
0404 return this->__begin_[__n];
0405 }
0406 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI const_reference operator[](size_type __n) const _NOEXCEPT {
0407 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(__n < size(), "vector[] index out of bounds");
0408 return this->__begin_[__n];
0409 }
0410 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI reference at(size_type __n) {
0411 if (__n >= size())
0412 this->__throw_out_of_range();
0413 return this->__begin_[__n];
0414 }
0415 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI const_reference at(size_type __n) const {
0416 if (__n >= size())
0417 this->__throw_out_of_range();
0418 return this->__begin_[__n];
0419 }
0420
0421 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI reference front() _NOEXCEPT {
0422 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(!empty(), "front() called on an empty vector");
0423 return *this->__begin_;
0424 }
0425 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI const_reference front() const _NOEXCEPT {
0426 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(!empty(), "front() called on an empty vector");
0427 return *this->__begin_;
0428 }
0429 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI reference back() _NOEXCEPT {
0430 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(!empty(), "back() called on an empty vector");
0431 return *(this->__end_ - 1);
0432 }
0433 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI const_reference back() const _NOEXCEPT {
0434 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(!empty(), "back() called on an empty vector");
0435 return *(this->__end_ - 1);
0436 }
0437
0438
0439
0440
0441 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI value_type* data() _NOEXCEPT {
0442 return std::__to_address(this->__begin_);
0443 }
0444
0445 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI const value_type* data() const _NOEXCEPT {
0446 return std::__to_address(this->__begin_);
0447 }
0448
0449
0450
0451
0452 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void push_back(const_reference __x) { emplace_back(__x); }
0453
0454 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void push_back(value_type&& __x) { emplace_back(std::move(__x)); }
0455
0456 template <class... _Args>
0457 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI
0458 #if _LIBCPP_STD_VER >= 17
0459 reference
0460 emplace_back(_Args&&... __args);
0461 #else
0462 void
0463 emplace_back(_Args&&... __args);
0464 #endif
0465
0466 #if _LIBCPP_STD_VER >= 23
0467 template <_ContainerCompatibleRange<_Tp> _Range>
0468 _LIBCPP_HIDE_FROM_ABI constexpr void append_range(_Range&& __range) {
0469 insert_range(end(), std::forward<_Range>(__range));
0470 }
0471 #endif
0472
0473 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void pop_back() {
0474 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(!empty(), "vector::pop_back called on an empty vector");
0475 this->__destruct_at_end(this->__end_ - 1);
0476 }
0477
0478 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI iterator insert(const_iterator __position, const_reference __x);
0479
0480 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI iterator insert(const_iterator __position, value_type&& __x);
0481 template <class... _Args>
0482 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI iterator emplace(const_iterator __position, _Args&&... __args);
0483
0484 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI iterator
0485 insert(const_iterator __position, size_type __n, const_reference __x);
0486
0487 template <class _InputIterator,
0488 __enable_if_t<__has_exactly_input_iterator_category<_InputIterator>::value &&
0489 is_constructible< value_type, typename iterator_traits<_InputIterator>::reference>::value,
0490 int> = 0>
0491 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI iterator
0492 insert(const_iterator __position, _InputIterator __first, _InputIterator __last) {
0493 return __insert_with_sentinel(__position, __first, __last);
0494 }
0495
0496 template <
0497 class _ForwardIterator,
0498 __enable_if_t<__has_forward_iterator_category<_ForwardIterator>::value &&
0499 is_constructible< value_type, typename iterator_traits<_ForwardIterator>::reference>::value,
0500 int> = 0>
0501 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI iterator
0502 insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last) {
0503 return __insert_with_size(__position, __first, __last, std::distance(__first, __last));
0504 }
0505
0506 #if _LIBCPP_STD_VER >= 23
0507 template <_ContainerCompatibleRange<_Tp> _Range>
0508 _LIBCPP_HIDE_FROM_ABI constexpr iterator insert_range(const_iterator __position, _Range&& __range) {
0509 if constexpr (ranges::forward_range<_Range> || ranges::sized_range<_Range>) {
0510 auto __n = static_cast<size_type>(ranges::distance(__range));
0511 return __insert_with_size(__position, ranges::begin(__range), ranges::end(__range), __n);
0512
0513 } else {
0514 return __insert_with_sentinel(__position, ranges::begin(__range), ranges::end(__range));
0515 }
0516 }
0517 #endif
0518
0519 #ifndef _LIBCPP_CXX03_LANG
0520 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI iterator
0521 insert(const_iterator __position, initializer_list<value_type> __il) {
0522 return insert(__position, __il.begin(), __il.end());
0523 }
0524 #endif
0525
0526 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI iterator erase(const_iterator __position);
0527 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI iterator erase(const_iterator __first, const_iterator __last);
0528
0529 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void clear() _NOEXCEPT {
0530 size_type __old_size = size();
0531 __base_destruct_at_end(this->__begin_);
0532 __annotate_shrink(__old_size);
0533 }
0534
0535 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void resize(size_type __sz);
0536 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void resize(size_type __sz, const_reference __x);
0537
0538 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void swap(vector&)
0539 #if _LIBCPP_STD_VER >= 14
0540 _NOEXCEPT;
0541 #else
0542 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value || __is_nothrow_swappable_v<allocator_type>);
0543 #endif
0544
0545 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI bool __invariants() const;
0546
0547 private:
0548 pointer __begin_ = nullptr;
0549 pointer __end_ = nullptr;
0550 _LIBCPP_COMPRESSED_PAIR(pointer, __cap_ = nullptr, allocator_type, __alloc_);
0551
0552
0553
0554
0555
0556
0557
0558
0559 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __vallocate(size_type __n) {
0560 if (__n > max_size())
0561 __throw_length_error();
0562 auto __allocation = std::__allocate_at_least(this->__alloc_, __n);
0563 __begin_ = __allocation.ptr;
0564 __end_ = __allocation.ptr;
0565 __cap_ = __begin_ + __allocation.count;
0566 __annotate_new(0);
0567 }
0568
0569 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __vdeallocate() _NOEXCEPT;
0570 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI size_type __recommend(size_type __new_size) const;
0571 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __construct_at_end(size_type __n);
0572 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __construct_at_end(size_type __n, const_reference __x);
0573
0574 template <class _InputIterator, class _Sentinel>
0575 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void
0576 __init_with_size(_InputIterator __first, _Sentinel __last, size_type __n) {
0577 auto __guard = std::__make_exception_guard(__destroy_vector(*this));
0578
0579 if (__n > 0) {
0580 __vallocate(__n);
0581 __construct_at_end(std::move(__first), std::move(__last), __n);
0582 }
0583
0584 __guard.__complete();
0585 }
0586
0587 template <class _InputIterator, class _Sentinel>
0588 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void
0589 __init_with_sentinel(_InputIterator __first, _Sentinel __last) {
0590 auto __guard = std::__make_exception_guard(__destroy_vector(*this));
0591
0592 for (; __first != __last; ++__first)
0593 emplace_back(*__first);
0594
0595 __guard.__complete();
0596 }
0597
0598 template <class _Iterator, class _Sentinel>
0599 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __assign_with_sentinel(_Iterator __first, _Sentinel __last);
0600
0601
0602
0603
0604 template <class _Iterator, class _Sentinel>
0605 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void
0606 __assign_with_size(_Iterator __first, _Sentinel __last, difference_type __n);
0607
0608 template <class _InputIterator, class _Sentinel>
0609 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI iterator
0610 __insert_with_sentinel(const_iterator __position, _InputIterator __first, _Sentinel __last);
0611
0612 template <class _Iterator, class _Sentinel>
0613 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI iterator
0614 __insert_with_size(const_iterator __position, _Iterator __first, _Sentinel __last, difference_type __n);
0615
0616 template <class _InputIterator, class _Sentinel>
0617 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void
0618 __construct_at_end(_InputIterator __first, _Sentinel __last, size_type __n);
0619
0620 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __append(size_type __n);
0621 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __append(size_type __n, const_reference __x);
0622
0623 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI iterator __make_iter(pointer __p) _NOEXCEPT {
0624 #ifdef _LIBCPP_ABI_BOUNDED_ITERATORS_IN_VECTOR
0625
0626
0627
0628
0629
0630
0631
0632
0633
0634 return std::__make_bounded_iter(
0635 std::__wrap_iter<pointer>(__p),
0636 std::__wrap_iter<pointer>(this->__begin_),
0637 std::__wrap_iter<pointer>(this->__cap_));
0638 #else
0639 return iterator(__p);
0640 #endif
0641 }
0642
0643 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI const_iterator __make_iter(const_pointer __p) const _NOEXCEPT {
0644 #ifdef _LIBCPP_ABI_BOUNDED_ITERATORS_IN_VECTOR
0645
0646 return std::__make_bounded_iter(
0647 std::__wrap_iter<const_pointer>(__p),
0648 std::__wrap_iter<const_pointer>(this->__begin_),
0649 std::__wrap_iter<const_pointer>(this->__cap_));
0650 #else
0651 return const_iterator(__p);
0652 #endif
0653 }
0654
0655 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void
0656 __swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v);
0657 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI pointer
0658 __swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v, pointer __p);
0659 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void
0660 __move_range(pointer __from_s, pointer __from_e, pointer __to);
0661 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __move_assign(vector& __c, true_type)
0662 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value);
0663 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __move_assign(vector& __c, false_type)
0664 _NOEXCEPT_(__alloc_traits::is_always_equal::value);
0665 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __destruct_at_end(pointer __new_last) _NOEXCEPT {
0666 size_type __old_size = size();
0667 __base_destruct_at_end(__new_last);
0668 __annotate_shrink(__old_size);
0669 }
0670
0671 template <class... _Args>
0672 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI inline pointer __emplace_back_slow_path(_Args&&... __args);
0673
0674
0675
0676
0677
0678
0679
0680
0681
0682 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void
0683 __annotate_contiguous_container(const void* __old_mid, const void* __new_mid) const {
0684 std::__annotate_contiguous_container<_Allocator>(data(), data() + capacity(), __old_mid, __new_mid);
0685 }
0686
0687 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __annotate_new(size_type __current_size) const _NOEXCEPT {
0688 (void)__current_size;
0689 #if _LIBCPP_HAS_ASAN
0690 __annotate_contiguous_container(data() + capacity(), data() + __current_size);
0691 #endif
0692 }
0693
0694 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __annotate_delete() const _NOEXCEPT {
0695 #if _LIBCPP_HAS_ASAN
0696 __annotate_contiguous_container(data() + size(), data() + capacity());
0697 #endif
0698 }
0699
0700 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __annotate_increase(size_type __n) const _NOEXCEPT {
0701 (void)__n;
0702 #if _LIBCPP_HAS_ASAN
0703 __annotate_contiguous_container(data() + size(), data() + size() + __n);
0704 #endif
0705 }
0706
0707 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __annotate_shrink(size_type __old_size) const _NOEXCEPT {
0708 (void)__old_size;
0709 #if _LIBCPP_HAS_ASAN
0710 __annotate_contiguous_container(data() + __old_size, data() + size());
0711 #endif
0712 }
0713
0714 struct _ConstructTransaction {
0715 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI explicit _ConstructTransaction(vector& __v, size_type __n)
0716 : __v_(__v), __pos_(__v.__end_), __new_end_(__v.__end_ + __n) {
0717 #if _LIBCPP_HAS_ASAN
0718 __v_.__annotate_increase(__n);
0719 #endif
0720 }
0721
0722 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI ~_ConstructTransaction() {
0723 __v_.__end_ = __pos_;
0724 #if _LIBCPP_HAS_ASAN
0725 if (__pos_ != __new_end_) {
0726 __v_.__annotate_shrink(__new_end_ - __v_.__begin_);
0727 }
0728 #endif
0729 }
0730
0731 vector& __v_;
0732 pointer __pos_;
0733 const_pointer const __new_end_;
0734
0735 _ConstructTransaction(_ConstructTransaction const&) = delete;
0736 _ConstructTransaction& operator=(_ConstructTransaction const&) = delete;
0737 };
0738
0739 template <class... _Args>
0740 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __construct_one_at_end(_Args&&... __args) {
0741 _ConstructTransaction __tx(*this, 1);
0742 __alloc_traits::construct(this->__alloc_, std::__to_address(__tx.__pos_), std::forward<_Args>(__args)...);
0743 ++__tx.__pos_;
0744 }
0745
0746 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __base_destruct_at_end(pointer __new_last) _NOEXCEPT {
0747 pointer __soon_to_be_end = this->__end_;
0748 while (__new_last != __soon_to_be_end)
0749 __alloc_traits::destroy(this->__alloc_, std::__to_address(--__soon_to_be_end));
0750 this->__end_ = __new_last;
0751 }
0752
0753 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __copy_assign_alloc(const vector& __c) {
0754 __copy_assign_alloc(__c, integral_constant<bool, __alloc_traits::propagate_on_container_copy_assignment::value>());
0755 }
0756
0757 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __move_assign_alloc(vector& __c)
0758 _NOEXCEPT_(!__alloc_traits::propagate_on_container_move_assignment::value ||
0759 is_nothrow_move_assignable<allocator_type>::value) {
0760 __move_assign_alloc(__c, integral_constant<bool, __alloc_traits::propagate_on_container_move_assignment::value>());
0761 }
0762
0763 [[__noreturn__]] _LIBCPP_HIDE_FROM_ABI static void __throw_length_error() { std::__throw_length_error("vector"); }
0764
0765 [[__noreturn__]] _LIBCPP_HIDE_FROM_ABI static void __throw_out_of_range() { std::__throw_out_of_range("vector"); }
0766
0767 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __copy_assign_alloc(const vector& __c, true_type) {
0768 if (this->__alloc_ != __c.__alloc_) {
0769 clear();
0770 __annotate_delete();
0771 __alloc_traits::deallocate(this->__alloc_, this->__begin_, capacity());
0772 this->__begin_ = this->__end_ = this->__cap_ = nullptr;
0773 }
0774 this->__alloc_ = __c.__alloc_;
0775 }
0776
0777 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __copy_assign_alloc(const vector&, false_type) {}
0778
0779 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __move_assign_alloc(vector& __c, true_type)
0780 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value) {
0781 this->__alloc_ = std::move(__c.__alloc_);
0782 }
0783
0784 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __move_assign_alloc(vector&, false_type) _NOEXCEPT {}
0785
0786 template <class _Ptr = pointer, __enable_if_t<is_pointer<_Ptr>::value, int> = 0>
0787 static _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI _LIBCPP_NO_CFI pointer
0788 __add_alignment_assumption(_Ptr __p) _NOEXCEPT {
0789 if (!__libcpp_is_constant_evaluated()) {
0790 return static_cast<pointer>(__builtin_assume_aligned(__p, _LIBCPP_ALIGNOF(decltype(*__p))));
0791 }
0792 return __p;
0793 }
0794
0795 template <class _Ptr = pointer, __enable_if_t<!is_pointer<_Ptr>::value, int> = 0>
0796 static _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI _LIBCPP_NO_CFI pointer
0797 __add_alignment_assumption(_Ptr __p) _NOEXCEPT {
0798 return __p;
0799 }
0800 };
0801
0802 #if _LIBCPP_STD_VER >= 17
0803 template <class _InputIterator,
0804 class _Alloc = allocator<__iter_value_type<_InputIterator>>,
0805 class = enable_if_t<__has_input_iterator_category<_InputIterator>::value>,
0806 class = enable_if_t<__is_allocator<_Alloc>::value> >
0807 vector(_InputIterator, _InputIterator) -> vector<__iter_value_type<_InputIterator>, _Alloc>;
0808
0809 template <class _InputIterator,
0810 class _Alloc,
0811 class = enable_if_t<__has_input_iterator_category<_InputIterator>::value>,
0812 class = enable_if_t<__is_allocator<_Alloc>::value> >
0813 vector(_InputIterator, _InputIterator, _Alloc) -> vector<__iter_value_type<_InputIterator>, _Alloc>;
0814 #endif
0815
0816 #if _LIBCPP_STD_VER >= 23
0817 template <ranges::input_range _Range,
0818 class _Alloc = allocator<ranges::range_value_t<_Range>>,
0819 class = enable_if_t<__is_allocator<_Alloc>::value> >
0820 vector(from_range_t, _Range&&, _Alloc = _Alloc()) -> vector<ranges::range_value_t<_Range>, _Alloc>;
0821 #endif
0822
0823
0824
0825
0826 template <class _Tp, class _Allocator>
0827 _LIBCPP_CONSTEXPR_SINCE_CXX20 void
0828 vector<_Tp, _Allocator>::__swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v) {
0829 __annotate_delete();
0830 auto __new_begin = __v.__begin_ - (__end_ - __begin_);
0831 std::__uninitialized_allocator_relocate(
0832 this->__alloc_, std::__to_address(__begin_), std::__to_address(__end_), std::__to_address(__new_begin));
0833 __v.__begin_ = __new_begin;
0834 __end_ = __begin_;
0835 std::swap(this->__begin_, __v.__begin_);
0836 std::swap(this->__end_, __v.__end_);
0837 std::swap(this->__cap_, __v.__cap_);
0838 __v.__first_ = __v.__begin_;
0839 __annotate_new(size());
0840 }
0841
0842
0843
0844
0845
0846 template <class _Tp, class _Allocator>
0847 _LIBCPP_CONSTEXPR_SINCE_CXX20 typename vector<_Tp, _Allocator>::pointer
0848 vector<_Tp, _Allocator>::__swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v, pointer __p) {
0849 __annotate_delete();
0850 pointer __ret = __v.__begin_;
0851
0852
0853
0854 std::__uninitialized_allocator_relocate(
0855 this->__alloc_, std::__to_address(__p), std::__to_address(__end_), std::__to_address(__v.__end_));
0856 __v.__end_ += (__end_ - __p);
0857 __end_ = __p;
0858 auto __new_begin = __v.__begin_ - (__p - __begin_);
0859
0860 std::__uninitialized_allocator_relocate(
0861 this->__alloc_, std::__to_address(__begin_), std::__to_address(__p), std::__to_address(__new_begin));
0862 __v.__begin_ = __new_begin;
0863 __end_ = __begin_;
0864
0865 std::swap(this->__begin_, __v.__begin_);
0866 std::swap(this->__end_, __v.__end_);
0867 std::swap(this->__cap_, __v.__cap_);
0868 __v.__first_ = __v.__begin_;
0869 __annotate_new(size());
0870 return __ret;
0871 }
0872
0873 template <class _Tp, class _Allocator>
0874 _LIBCPP_CONSTEXPR_SINCE_CXX20 void vector<_Tp, _Allocator>::__vdeallocate() _NOEXCEPT {
0875 if (this->__begin_ != nullptr) {
0876 clear();
0877 __annotate_delete();
0878 __alloc_traits::deallocate(this->__alloc_, this->__begin_, capacity());
0879 this->__begin_ = this->__end_ = this->__cap_ = nullptr;
0880 }
0881 }
0882
0883
0884 template <class _Tp, class _Allocator>
0885 _LIBCPP_CONSTEXPR_SINCE_CXX20 inline _LIBCPP_HIDE_FROM_ABI typename vector<_Tp, _Allocator>::size_type
0886 vector<_Tp, _Allocator>::__recommend(size_type __new_size) const {
0887 const size_type __ms = max_size();
0888 if (__new_size > __ms)
0889 this->__throw_length_error();
0890 const size_type __cap = capacity();
0891 if (__cap >= __ms / 2)
0892 return __ms;
0893 return std::max<size_type>(2 * __cap, __new_size);
0894 }
0895
0896
0897
0898
0899
0900
0901 template <class _Tp, class _Allocator>
0902 _LIBCPP_CONSTEXPR_SINCE_CXX20 void vector<_Tp, _Allocator>::__construct_at_end(size_type __n) {
0903 _ConstructTransaction __tx(*this, __n);
0904 const_pointer __new_end = __tx.__new_end_;
0905 for (pointer __pos = __tx.__pos_; __pos != __new_end; __tx.__pos_ = ++__pos) {
0906 __alloc_traits::construct(this->__alloc_, std::__to_address(__pos));
0907 }
0908 }
0909
0910
0911
0912
0913
0914
0915
0916 template <class _Tp, class _Allocator>
0917 _LIBCPP_CONSTEXPR_SINCE_CXX20 inline void
0918 vector<_Tp, _Allocator>::__construct_at_end(size_type __n, const_reference __x) {
0919 _ConstructTransaction __tx(*this, __n);
0920 const_pointer __new_end = __tx.__new_end_;
0921 for (pointer __pos = __tx.__pos_; __pos != __new_end; __tx.__pos_ = ++__pos) {
0922 __alloc_traits::construct(this->__alloc_, std::__to_address(__pos), __x);
0923 }
0924 }
0925
0926 template <class _Tp, class _Allocator>
0927 template <class _InputIterator, class _Sentinel>
0928 _LIBCPP_CONSTEXPR_SINCE_CXX20 void
0929 vector<_Tp, _Allocator>::__construct_at_end(_InputIterator __first, _Sentinel __last, size_type __n) {
0930 _ConstructTransaction __tx(*this, __n);
0931 __tx.__pos_ = std::__uninitialized_allocator_copy(this->__alloc_, std::move(__first), std::move(__last), __tx.__pos_);
0932 }
0933
0934
0935
0936
0937
0938 template <class _Tp, class _Allocator>
0939 _LIBCPP_CONSTEXPR_SINCE_CXX20 void vector<_Tp, _Allocator>::__append(size_type __n) {
0940 if (static_cast<size_type>(this->__cap_ - this->__end_) >= __n)
0941 this->__construct_at_end(__n);
0942 else {
0943 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), size(), this->__alloc_);
0944 __v.__construct_at_end(__n);
0945 __swap_out_circular_buffer(__v);
0946 }
0947 }
0948
0949
0950
0951
0952
0953 template <class _Tp, class _Allocator>
0954 _LIBCPP_CONSTEXPR_SINCE_CXX20 void vector<_Tp, _Allocator>::__append(size_type __n, const_reference __x) {
0955 if (static_cast<size_type>(this->__cap_ - this->__end_) >= __n)
0956 this->__construct_at_end(__n, __x);
0957 else {
0958 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), size(), this->__alloc_);
0959 __v.__construct_at_end(__n, __x);
0960 __swap_out_circular_buffer(__v);
0961 }
0962 }
0963
0964 template <class _Tp, class _Allocator>
0965 _LIBCPP_CONSTEXPR_SINCE_CXX20 inline _LIBCPP_HIDE_FROM_ABI vector<_Tp, _Allocator>::vector(vector&& __x)
0966 #if _LIBCPP_STD_VER >= 17
0967 noexcept
0968 #else
0969 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
0970 #endif
0971 : __alloc_(std::move(__x.__alloc_)) {
0972 this->__begin_ = __x.__begin_;
0973 this->__end_ = __x.__end_;
0974 this->__cap_ = __x.__cap_;
0975 __x.__begin_ = __x.__end_ = __x.__cap_ = nullptr;
0976 }
0977
0978 template <class _Tp, class _Allocator>
0979 _LIBCPP_CONSTEXPR_SINCE_CXX20 inline _LIBCPP_HIDE_FROM_ABI
0980 vector<_Tp, _Allocator>::vector(vector&& __x, const __type_identity_t<allocator_type>& __a)
0981 : __alloc_(__a) {
0982 if (__a == __x.__alloc_) {
0983 this->__begin_ = __x.__begin_;
0984 this->__end_ = __x.__end_;
0985 this->__cap_ = __x.__cap_;
0986 __x.__begin_ = __x.__end_ = __x.__cap_ = nullptr;
0987 } else {
0988 typedef move_iterator<iterator> _Ip;
0989 __init_with_size(_Ip(__x.begin()), _Ip(__x.end()), __x.size());
0990 }
0991 }
0992
0993 template <class _Tp, class _Allocator>
0994 _LIBCPP_CONSTEXPR_SINCE_CXX20 void vector<_Tp, _Allocator>::__move_assign(vector& __c, false_type)
0995 _NOEXCEPT_(__alloc_traits::is_always_equal::value) {
0996 if (this->__alloc_ != __c.__alloc_) {
0997 typedef move_iterator<iterator> _Ip;
0998 assign(_Ip(__c.begin()), _Ip(__c.end()));
0999 } else
1000 __move_assign(__c, true_type());
1001 }
1002
1003 template <class _Tp, class _Allocator>
1004 _LIBCPP_CONSTEXPR_SINCE_CXX20 void vector<_Tp, _Allocator>::__move_assign(vector& __c, true_type)
1005 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value) {
1006 __vdeallocate();
1007 __move_assign_alloc(__c);
1008 this->__begin_ = __c.__begin_;
1009 this->__end_ = __c.__end_;
1010 this->__cap_ = __c.__cap_;
1011 __c.__begin_ = __c.__end_ = __c.__cap_ = nullptr;
1012 }
1013
1014 template <class _Tp, class _Allocator>
1015 _LIBCPP_CONSTEXPR_SINCE_CXX20 inline _LIBCPP_HIDE_FROM_ABI vector<_Tp, _Allocator>&
1016 vector<_Tp, _Allocator>::operator=(const vector& __x) {
1017 if (this != std::addressof(__x)) {
1018 __copy_assign_alloc(__x);
1019 assign(__x.__begin_, __x.__end_);
1020 }
1021 return *this;
1022 }
1023
1024 template <class _Tp, class _Allocator>
1025 template <class _Iterator, class _Sentinel>
1026 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void
1027 vector<_Tp, _Allocator>::__assign_with_sentinel(_Iterator __first, _Sentinel __last) {
1028 pointer __cur = __begin_;
1029 for (; __first != __last && __cur != __end_; ++__first, (void)++__cur)
1030 *__cur = *__first;
1031 if (__cur != __end_) {
1032 __destruct_at_end(__cur);
1033 } else {
1034 for (; __first != __last; ++__first)
1035 emplace_back(*__first);
1036 }
1037 }
1038
1039 template <class _Tp, class _Allocator>
1040 template <class _Iterator, class _Sentinel>
1041 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void
1042 vector<_Tp, _Allocator>::__assign_with_size(_Iterator __first, _Sentinel __last, difference_type __n) {
1043 size_type __new_size = static_cast<size_type>(__n);
1044 if (__new_size <= capacity()) {
1045 if (__new_size > size()) {
1046 #if _LIBCPP_STD_VER >= 23
1047 auto __mid = ranges::copy_n(std::move(__first), size(), this->__begin_).in;
1048 __construct_at_end(std::move(__mid), std::move(__last), __new_size - size());
1049 #else
1050 _Iterator __mid = std::next(__first, size());
1051 std::copy(__first, __mid, this->__begin_);
1052 __construct_at_end(__mid, __last, __new_size - size());
1053 #endif
1054 } else {
1055 pointer __m = std::__copy(std::move(__first), __last, this->__begin_).second;
1056 this->__destruct_at_end(__m);
1057 }
1058 } else {
1059 __vdeallocate();
1060 __vallocate(__recommend(__new_size));
1061 __construct_at_end(std::move(__first), std::move(__last), __new_size);
1062 }
1063 }
1064
1065 template <class _Tp, class _Allocator>
1066 _LIBCPP_CONSTEXPR_SINCE_CXX20 void vector<_Tp, _Allocator>::assign(size_type __n, const_reference __u) {
1067 if (__n <= capacity()) {
1068 size_type __s = size();
1069 std::fill_n(this->__begin_, std::min(__n, __s), __u);
1070 if (__n > __s)
1071 __construct_at_end(__n - __s, __u);
1072 else
1073 this->__destruct_at_end(this->__begin_ + __n);
1074 } else {
1075 __vdeallocate();
1076 __vallocate(__recommend(static_cast<size_type>(__n)));
1077 __construct_at_end(__n, __u);
1078 }
1079 }
1080
1081 template <class _Tp, class _Allocator>
1082 _LIBCPP_CONSTEXPR_SINCE_CXX20 void vector<_Tp, _Allocator>::reserve(size_type __n) {
1083 if (__n > capacity()) {
1084 if (__n > max_size())
1085 this->__throw_length_error();
1086 __split_buffer<value_type, allocator_type&> __v(__n, size(), this->__alloc_);
1087 __swap_out_circular_buffer(__v);
1088 }
1089 }
1090
1091 template <class _Tp, class _Allocator>
1092 _LIBCPP_CONSTEXPR_SINCE_CXX20 void vector<_Tp, _Allocator>::shrink_to_fit() _NOEXCEPT {
1093 if (capacity() > size()) {
1094 #if _LIBCPP_HAS_EXCEPTIONS
1095 try {
1096 #endif
1097 __split_buffer<value_type, allocator_type&> __v(size(), size(), this->__alloc_);
1098
1099
1100
1101 if (__v.capacity() < capacity())
1102 __swap_out_circular_buffer(__v);
1103 #if _LIBCPP_HAS_EXCEPTIONS
1104 } catch (...) {
1105 }
1106 #endif
1107 }
1108 }
1109
1110 template <class _Tp, class _Allocator>
1111 template <class... _Args>
1112 _LIBCPP_CONSTEXPR_SINCE_CXX20 typename vector<_Tp, _Allocator>::pointer
1113 vector<_Tp, _Allocator>::__emplace_back_slow_path(_Args&&... __args) {
1114 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), size(), this->__alloc_);
1115
1116 __alloc_traits::construct(this->__alloc_, std::__to_address(__v.__end_), std::forward<_Args>(__args)...);
1117 __v.__end_++;
1118 __swap_out_circular_buffer(__v);
1119 return this->__end_;
1120 }
1121
1122 template <class _Tp, class _Allocator>
1123 template <class... _Args>
1124 _LIBCPP_CONSTEXPR_SINCE_CXX20 inline
1125 #if _LIBCPP_STD_VER >= 17
1126 typename vector<_Tp, _Allocator>::reference
1127 #else
1128 void
1129 #endif
1130 vector<_Tp, _Allocator>::emplace_back(_Args&&... __args) {
1131 pointer __end = this->__end_;
1132 if (__end < this->__cap_) {
1133 __construct_one_at_end(std::forward<_Args>(__args)...);
1134 ++__end;
1135 } else {
1136 __end = __emplace_back_slow_path(std::forward<_Args>(__args)...);
1137 }
1138 this->__end_ = __end;
1139 #if _LIBCPP_STD_VER >= 17
1140 return *(__end - 1);
1141 #endif
1142 }
1143
1144 template <class _Tp, class _Allocator>
1145 _LIBCPP_CONSTEXPR_SINCE_CXX20 inline _LIBCPP_HIDE_FROM_ABI typename vector<_Tp, _Allocator>::iterator
1146 vector<_Tp, _Allocator>::erase(const_iterator __position) {
1147 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
1148 __position != end(), "vector::erase(iterator) called with a non-dereferenceable iterator");
1149 difference_type __ps = __position - cbegin();
1150 pointer __p = this->__begin_ + __ps;
1151 this->__destruct_at_end(std::move(__p + 1, this->__end_, __p));
1152 return __make_iter(__p);
1153 }
1154
1155 template <class _Tp, class _Allocator>
1156 _LIBCPP_CONSTEXPR_SINCE_CXX20 typename vector<_Tp, _Allocator>::iterator
1157 vector<_Tp, _Allocator>::erase(const_iterator __first, const_iterator __last) {
1158 _LIBCPP_ASSERT_VALID_INPUT_RANGE(__first <= __last, "vector::erase(first, last) called with invalid range");
1159 pointer __p = this->__begin_ + (__first - begin());
1160 if (__first != __last) {
1161 this->__destruct_at_end(std::move(__p + (__last - __first), this->__end_, __p));
1162 }
1163 return __make_iter(__p);
1164 }
1165
1166 template <class _Tp, class _Allocator>
1167 _LIBCPP_CONSTEXPR_SINCE_CXX20 void
1168 vector<_Tp, _Allocator>::__move_range(pointer __from_s, pointer __from_e, pointer __to) {
1169 pointer __old_last = this->__end_;
1170 difference_type __n = __old_last - __to;
1171 {
1172 pointer __i = __from_s + __n;
1173 _ConstructTransaction __tx(*this, __from_e - __i);
1174 for (pointer __pos = __tx.__pos_; __i < __from_e; ++__i, (void)++__pos, __tx.__pos_ = __pos) {
1175 __alloc_traits::construct(this->__alloc_, std::__to_address(__pos), std::move(*__i));
1176 }
1177 }
1178 std::move_backward(__from_s, __from_s + __n, __old_last);
1179 }
1180
1181 template <class _Tp, class _Allocator>
1182 _LIBCPP_CONSTEXPR_SINCE_CXX20 typename vector<_Tp, _Allocator>::iterator
1183 vector<_Tp, _Allocator>::insert(const_iterator __position, const_reference __x) {
1184 pointer __p = this->__begin_ + (__position - begin());
1185 if (this->__end_ < this->__cap_) {
1186 if (__p == this->__end_) {
1187 __construct_one_at_end(__x);
1188 } else {
1189 __move_range(__p, this->__end_, __p + 1);
1190 const_pointer __xr = pointer_traits<const_pointer>::pointer_to(__x);
1191 if (std::__is_pointer_in_range(std::__to_address(__p), std::__to_address(__end_), std::addressof(__x)))
1192 ++__xr;
1193 *__p = *__xr;
1194 }
1195 } else {
1196 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), __p - this->__begin_, this->__alloc_);
1197 __v.emplace_back(__x);
1198 __p = __swap_out_circular_buffer(__v, __p);
1199 }
1200 return __make_iter(__p);
1201 }
1202
1203 template <class _Tp, class _Allocator>
1204 _LIBCPP_CONSTEXPR_SINCE_CXX20 typename vector<_Tp, _Allocator>::iterator
1205 vector<_Tp, _Allocator>::insert(const_iterator __position, value_type&& __x) {
1206 pointer __p = this->__begin_ + (__position - begin());
1207 if (this->__end_ < this->__cap_) {
1208 if (__p == this->__end_) {
1209 __construct_one_at_end(std::move(__x));
1210 } else {
1211 __move_range(__p, this->__end_, __p + 1);
1212 *__p = std::move(__x);
1213 }
1214 } else {
1215 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), __p - this->__begin_, this->__alloc_);
1216 __v.emplace_back(std::move(__x));
1217 __p = __swap_out_circular_buffer(__v, __p);
1218 }
1219 return __make_iter(__p);
1220 }
1221
1222 template <class _Tp, class _Allocator>
1223 template <class... _Args>
1224 _LIBCPP_CONSTEXPR_SINCE_CXX20 typename vector<_Tp, _Allocator>::iterator
1225 vector<_Tp, _Allocator>::emplace(const_iterator __position, _Args&&... __args) {
1226 pointer __p = this->__begin_ + (__position - begin());
1227 if (this->__end_ < this->__cap_) {
1228 if (__p == this->__end_) {
1229 __construct_one_at_end(std::forward<_Args>(__args)...);
1230 } else {
1231 __temp_value<value_type, _Allocator> __tmp(this->__alloc_, std::forward<_Args>(__args)...);
1232 __move_range(__p, this->__end_, __p + 1);
1233 *__p = std::move(__tmp.get());
1234 }
1235 } else {
1236 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), __p - this->__begin_, this->__alloc_);
1237 __v.emplace_back(std::forward<_Args>(__args)...);
1238 __p = __swap_out_circular_buffer(__v, __p);
1239 }
1240 return __make_iter(__p);
1241 }
1242
1243 template <class _Tp, class _Allocator>
1244 _LIBCPP_CONSTEXPR_SINCE_CXX20 typename vector<_Tp, _Allocator>::iterator
1245 vector<_Tp, _Allocator>::insert(const_iterator __position, size_type __n, const_reference __x) {
1246 pointer __p = this->__begin_ + (__position - begin());
1247 if (__n > 0) {
1248
1249 if (!__libcpp_is_constant_evaluated() && __n <= static_cast<size_type>(this->__cap_ - this->__end_)) {
1250 size_type __old_n = __n;
1251 pointer __old_last = this->__end_;
1252 if (__n > static_cast<size_type>(this->__end_ - __p)) {
1253 size_type __cx = __n - (this->__end_ - __p);
1254 __construct_at_end(__cx, __x);
1255 __n -= __cx;
1256 }
1257 if (__n > 0) {
1258 __move_range(__p, __old_last, __p + __old_n);
1259 const_pointer __xr = pointer_traits<const_pointer>::pointer_to(__x);
1260 if (__p <= __xr && __xr < this->__end_)
1261 __xr += __old_n;
1262 std::fill_n(__p, __n, *__xr);
1263 }
1264 } else {
1265 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), __p - this->__begin_, this->__alloc_);
1266 __v.__construct_at_end(__n, __x);
1267 __p = __swap_out_circular_buffer(__v, __p);
1268 }
1269 }
1270 return __make_iter(__p);
1271 }
1272
1273 template <class _Tp, class _Allocator>
1274 template <class _InputIterator, class _Sentinel>
1275 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI typename vector<_Tp, _Allocator>::iterator
1276 vector<_Tp, _Allocator>::__insert_with_sentinel(const_iterator __position, _InputIterator __first, _Sentinel __last) {
1277 difference_type __off = __position - begin();
1278 pointer __p = this->__begin_ + __off;
1279 pointer __old_last = this->__end_;
1280 for (; this->__end_ != this->__cap_ && __first != __last; ++__first)
1281 __construct_one_at_end(*__first);
1282
1283 if (__first == __last)
1284 (void)std::rotate(__p, __old_last, this->__end_);
1285 else {
1286 __split_buffer<value_type, allocator_type&> __v(__alloc_);
1287 auto __guard = std::__make_exception_guard(
1288 _AllocatorDestroyRangeReverse<allocator_type, pointer>(__alloc_, __old_last, this->__end_));
1289 __v.__construct_at_end_with_sentinel(std::move(__first), std::move(__last));
1290 __split_buffer<value_type, allocator_type&> __merged(
1291 __recommend(size() + __v.size()), __off, __alloc_);
1292 std::__uninitialized_allocator_relocate(
1293 __alloc_, std::__to_address(__old_last), std::__to_address(this->__end_), std::__to_address(__merged.__end_));
1294 __guard.__complete();
1295 __merged.__end_ += this->__end_ - __old_last;
1296 this->__end_ = __old_last;
1297 std::__uninitialized_allocator_relocate(
1298 __alloc_, std::__to_address(__v.__begin_), std::__to_address(__v.__end_), std::__to_address(__merged.__end_));
1299 __merged.__end_ += __v.size();
1300 __v.__end_ = __v.__begin_;
1301 __p = __swap_out_circular_buffer(__merged, __p);
1302 }
1303 return __make_iter(__p);
1304 }
1305
1306 template <class _Tp, class _Allocator>
1307 template <class _Iterator, class _Sentinel>
1308 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI typename vector<_Tp, _Allocator>::iterator
1309 vector<_Tp, _Allocator>::__insert_with_size(
1310 const_iterator __position, _Iterator __first, _Sentinel __last, difference_type __n) {
1311 pointer __p = this->__begin_ + (__position - begin());
1312 if (__n > 0) {
1313 if (__n <= this->__cap_ - this->__end_) {
1314 pointer __old_last = this->__end_;
1315 difference_type __dx = this->__end_ - __p;
1316 if (__n > __dx) {
1317 #if _LIBCPP_STD_VER >= 23
1318 if constexpr (!forward_iterator<_Iterator>) {
1319 __construct_at_end(std::move(__first), std::move(__last), __n);
1320 std::rotate(__p, __old_last, this->__end_);
1321 } else
1322 #endif
1323 {
1324 _Iterator __m = std::next(__first, __dx);
1325 __construct_at_end(__m, __last, __n - __dx);
1326 if (__dx > 0) {
1327 __move_range(__p, __old_last, __p + __n);
1328 std::copy(__first, __m, __p);
1329 }
1330 }
1331 } else {
1332 __move_range(__p, __old_last, __p + __n);
1333 #if _LIBCPP_STD_VER >= 23
1334 if constexpr (!forward_iterator<_Iterator>) {
1335 ranges::copy_n(std::move(__first), __n, __p);
1336 } else
1337 #endif
1338 {
1339 std::copy_n(__first, __n, __p);
1340 }
1341 }
1342 } else {
1343 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), __p - this->__begin_, this->__alloc_);
1344 __v.__construct_at_end_with_size(std::move(__first), __n);
1345 __p = __swap_out_circular_buffer(__v, __p);
1346 }
1347 }
1348 return __make_iter(__p);
1349 }
1350
1351 template <class _Tp, class _Allocator>
1352 _LIBCPP_CONSTEXPR_SINCE_CXX20 void vector<_Tp, _Allocator>::resize(size_type __sz) {
1353 size_type __cs = size();
1354 if (__cs < __sz)
1355 this->__append(__sz - __cs);
1356 else if (__cs > __sz)
1357 this->__destruct_at_end(this->__begin_ + __sz);
1358 }
1359
1360 template <class _Tp, class _Allocator>
1361 _LIBCPP_CONSTEXPR_SINCE_CXX20 void vector<_Tp, _Allocator>::resize(size_type __sz, const_reference __x) {
1362 size_type __cs = size();
1363 if (__cs < __sz)
1364 this->__append(__sz - __cs, __x);
1365 else if (__cs > __sz)
1366 this->__destruct_at_end(this->__begin_ + __sz);
1367 }
1368
1369 template <class _Tp, class _Allocator>
1370 _LIBCPP_CONSTEXPR_SINCE_CXX20 void vector<_Tp, _Allocator>::swap(vector& __x)
1371 #if _LIBCPP_STD_VER >= 14
1372 _NOEXCEPT
1373 #else
1374 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value || __is_nothrow_swappable_v<allocator_type>)
1375 #endif
1376 {
1377 _LIBCPP_ASSERT_COMPATIBLE_ALLOCATOR(
1378 __alloc_traits::propagate_on_container_swap::value || this->__alloc_ == __x.__alloc_,
1379 "vector::swap: Either propagate_on_container_swap must be true"
1380 " or the allocators must compare equal");
1381 std::swap(this->__begin_, __x.__begin_);
1382 std::swap(this->__end_, __x.__end_);
1383 std::swap(this->__cap_, __x.__cap_);
1384 std::__swap_allocator(this->__alloc_, __x.__alloc_);
1385 }
1386
1387 template <class _Tp, class _Allocator>
1388 _LIBCPP_CONSTEXPR_SINCE_CXX20 bool vector<_Tp, _Allocator>::__invariants() const {
1389 if (this->__begin_ == nullptr) {
1390 if (this->__end_ != nullptr || this->__cap_ != nullptr)
1391 return false;
1392 } else {
1393 if (this->__begin_ > this->__end_)
1394 return false;
1395 if (this->__begin_ == this->__cap_)
1396 return false;
1397 if (this->__end_ > this->__cap_)
1398 return false;
1399 }
1400 return true;
1401 }
1402
1403 #if _LIBCPP_STD_VER >= 20
1404 template <>
1405 inline constexpr bool __format::__enable_insertable<vector<char>> = true;
1406 # if _LIBCPP_HAS_WIDE_CHARACTERS
1407 template <>
1408 inline constexpr bool __format::__enable_insertable<vector<wchar_t>> = true;
1409 # endif
1410 #endif
1411
1412 _LIBCPP_END_NAMESPACE_STD
1413
1414 _LIBCPP_POP_MACROS
1415
1416 #endif