1//===----------------------------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#ifndef _LIBCPP___VECTOR_VECTOR_H
10#define _LIBCPP___VECTOR_VECTOR_H
11
12#include <__algorithm/copy.h>
13#include <__algorithm/copy_n.h>
14#include <__algorithm/fill_n.h>
15#include <__algorithm/iterator_operations.h>
16#include <__algorithm/max.h>
17#include <__algorithm/min.h>
18#include <__algorithm/move.h>
19#include <__algorithm/move_backward.h>
20#include <__algorithm/rotate.h>
21#include <__assert>
22#include <__config>
23#include <__debug_utils/sanitizers.h>
24#include <__format/enable_insertable.h>
25#include <__fwd/vector.h>
26#include <__iterator/bounded_iter.h>
27#include <__iterator/concepts.h>
28#include <__iterator/distance.h>
29#include <__iterator/iterator_traits.h>
30#include <__iterator/move_iterator.h>
31#include <__iterator/next.h>
32#include <__iterator/reverse_iterator.h>
33#include <__iterator/wrap_iter.h>
34#include <__memory/addressof.h>
35#include <__memory/allocate_at_least.h>
36#include <__memory/allocator.h>
37#include <__memory/allocator_traits.h>
38#include <__memory/compressed_pair.h>
39#include <__memory/noexcept_move_assign_container.h>
40#include <__memory/pointer_traits.h>
41#include <__memory/swap_allocator.h>
42#include <__memory/temp_value.h>
43#include <__memory/uninitialized_algorithms.h>
44#include <__ranges/access.h>
45#include <__ranges/as_rvalue_view.h>
46#include <__ranges/concepts.h>
47#include <__ranges/container_compatible_range.h>
48#include <__ranges/from_range.h>
49#include <__split_buffer>
50#include <__type_traits/conditional.h>
51#include <__type_traits/enable_if.h>
52#include <__type_traits/is_allocator.h>
53#include <__type_traits/is_constant_evaluated.h>
54#include <__type_traits/is_constructible.h>
55#include <__type_traits/is_nothrow_assignable.h>
56#include <__type_traits/is_nothrow_constructible.h>
57#include <__type_traits/is_pointer.h>
58#include <__type_traits/is_relocatable.h>
59#include <__type_traits/is_same.h>
60#include <__type_traits/is_swappable.h>
61#include <__type_traits/remove_const_ref.h>
62#include <__type_traits/type_identity.h>
63#include <__utility/declval.h>
64#include <__utility/exception_guard.h>
65#include <__utility/forward.h>
66#include <__utility/is_pointer_in_range.h>
67#include <__utility/move.h>
68#include <__utility/pair.h>
69#include <__utility/swap.h>
70#include <initializer_list>
71#include <limits>
72#include <stdexcept>
73
74// These headers define parts of vectors definition, since they define ADL functions or class specializations.
75#include <__vector/comparison.h>
76#include <__vector/container_traits.h>
77#include <__vector/layout.h>
78#include <__vector/swap.h>
79
80#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
81# pragma GCC system_header
82#endif
83
84_LIBCPP_PUSH_MACROS
85#include <__undef_macros>
86
87_LIBCPP_BEGIN_NAMESPACE_STD
88
89template <class _Tp, class _Allocator /* = allocator<_Tp> */>
90class _LIBCPP_WARN_UNUSED vector {
91 using __base_type _LIBCPP_NODEBUG = __vector_layout<_Tp, _Allocator>;
92 using __bound_type _LIBCPP_NODEBUG = typename __base_type::__bound_type;
93 using _SplitBuffer _LIBCPP_NODEBUG = typename __base_type::_SplitBuffer;
94
95public:
96 //
97 // Types
98 //
99 using value_type = _Tp;
100 using allocator_type = _Allocator;
101 using __alloc_traits _LIBCPP_NODEBUG = allocator_traits<_Allocator>;
102 using reference = _Tp&;
103 using const_reference = const _Tp&;
104 using size_type = typename __alloc_traits::size_type;
105 using difference_type = typename __alloc_traits::difference_type;
106 using pointer = typename __alloc_traits::pointer;
107 using const_pointer = typename __alloc_traits::const_pointer;
108#ifdef _LIBCPP_ABI_BOUNDED_ITERATORS_IN_VECTOR
109 // Users might provide custom allocators, and prior to C++20 we have no existing way to detect whether the allocator's
110 // pointer type is contiguous (though it has to be by the Standard). Using the wrapper type ensures the iterator is
111 // considered contiguous.
112 using iterator = __bounded_iter<pointer>;
113 using const_iterator = __bounded_iter<const_pointer>;
114#else
115 using iterator = __wrap_iter<pointer>;
116 using const_iterator = __wrap_iter<const_pointer>;
117#endif
118 using reverse_iterator = std::reverse_iterator<iterator>;
119 using const_reverse_iterator = std::reverse_iterator<const_iterator>;
120
121 // A vector contains the following members which may be trivially relocatable:
122 // - pointer: may be trivially relocatable, so it's checked
123 // - allocator_type: may be trivially relocatable, so it's checked
124 // vector doesn't contain any self-references, so it's trivially relocatable if its members are.
125 using __trivially_relocatable _LIBCPP_NODEBUG =
126 __conditional_t<__is_trivially_relocatable_v<pointer> && __is_trivially_relocatable_v<allocator_type>,
127 vector,
128 void>;
129
130 static_assert(__check_valid_allocator<allocator_type>::value, "");
131 static_assert(is_same<typename allocator_type::value_type, value_type>::value,
132 "Allocator::value_type must be same type as value_type");
133
134 //
135 // [vector.cons], construct/copy/destroy
136 //
137 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI vector()
138 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value) {}
139 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI explicit vector(const allocator_type& __a)
140#if _LIBCPP_STD_VER <= 14
141 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value)
142#else
143 noexcept
144#endif
145 : __layout_(__a) {
146 }
147
148 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI explicit vector(size_type __n) {
149 auto __guard = std::__make_exception_guard(__destroy_vector(*this));
150 if (__n > 0) {
151 __vallocate(__n);
152 __construct_at_end(__n);
153 }
154 __guard.__complete();
155 }
156
157 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI explicit vector(size_type __n, const allocator_type& __a)
158 : __layout_(__a) {
159 auto __guard = std::__make_exception_guard(__destroy_vector(*this));
160 if (__n > 0) {
161 __vallocate(__n);
162 __construct_at_end(__n);
163 }
164 __guard.__complete();
165 }
166
167 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI vector(size_type __n, const value_type& __x) {
168 auto __guard = std::__make_exception_guard(__destroy_vector(*this));
169 if (__n > 0) {
170 __vallocate(__n);
171 __construct_at_end(__n, __x);
172 }
173 __guard.__complete();
174 }
175
176 template <__enable_if_t<__is_allocator_v<_Allocator>, int> = 0>
177 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI
178 vector(size_type __n, const value_type& __x, const allocator_type& __a)
179 : __layout_(__a) {
180 auto __guard = std::__make_exception_guard(__destroy_vector(*this));
181 if (__n > 0) {
182 __vallocate(__n);
183 __construct_at_end(__n, __x);
184 }
185 __guard.__complete();
186 }
187
188 template <class _InputIterator,
189 __enable_if_t<__has_exactly_input_iterator_category<_InputIterator>::value &&
190 is_constructible<value_type, typename iterator_traits<_InputIterator>::reference>::value,
191 int> = 0>
192 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI vector(_InputIterator __first, _InputIterator __last) {
193 __init_with_sentinel(__first, __last);
194 }
195
196 template <class _InputIterator,
197 __enable_if_t<__has_exactly_input_iterator_category<_InputIterator>::value &&
198 is_constructible<value_type, typename iterator_traits<_InputIterator>::reference>::value,
199 int> = 0>
200 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI
201 vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a)
202 : __layout_(__a) {
203 __init_with_sentinel(__first, __last);
204 }
205
206 template <
207 class _ForwardIterator,
208 __enable_if_t<__has_forward_iterator_category<_ForwardIterator>::value &&
209 is_constructible<value_type, typename iterator_traits<_ForwardIterator>::reference>::value,
210 int> = 0>
211 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI vector(_ForwardIterator __first, _ForwardIterator __last) {
212 size_type __n = static_cast<size_type>(std::distance(__first, __last));
213 __init_with_size(__first, __last, __n);
214 }
215
216 template <
217 class _ForwardIterator,
218 __enable_if_t<__has_forward_iterator_category<_ForwardIterator>::value &&
219 is_constructible<value_type, typename iterator_traits<_ForwardIterator>::reference>::value,
220 int> = 0>
221 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI
222 vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a)
223 : __layout_(__a) {
224 size_type __n = static_cast<size_type>(std::distance(__first, __last));
225 __init_with_size(__first, __last, __n);
226 }
227
228#if _LIBCPP_STD_VER >= 23
229 template <_ContainerCompatibleRange<_Tp> _Range>
230 _LIBCPP_HIDE_FROM_ABI constexpr vector(from_range_t, _Range&& __range, const allocator_type& __a = allocator_type())
231 : __layout_(__a) {
232 if constexpr (ranges::forward_range<_Range> || ranges::sized_range<_Range>) {
233 auto __n = static_cast<size_type>(ranges::distance(__range));
234 __init_with_size(ranges::begin(__range), ranges::end(__range), __n);
235
236 } else {
237 __init_with_sentinel(ranges::begin(__range), ranges::end(__range));
238 }
239 }
240#endif
241
242private:
243 class __destroy_vector {
244 public:
245 _LIBCPP_CONSTEXPR _LIBCPP_HIDE_FROM_ABI __destroy_vector(vector& __vec) : __vec_(__vec) {}
246
247 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void operator()() {
248 if (__vec_.__layout_.__begin_ptr() != nullptr) {
249 __vec_.clear();
250 __vec_.__annotate_delete();
251 __alloc_traits::deallocate(__vec_.__layout_.__alloc(), __vec_.__layout_.__begin_ptr(), __vec_.capacity());
252 }
253 }
254
255 private:
256 vector& __vec_;
257 };
258
259 using __emplace_back_result_t _LIBCPP_NODEBUG = _If<(_LIBCPP_STD_VER < 17), void, reference>;
260
261public:
262 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI ~vector() { __destroy_vector (*this)(); }
263
264 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI vector(const vector& __x)
265 : __layout_(__alloc_traits::select_on_container_copy_construction(__x.__layout_.__alloc())) {
266 __init_with_size(__x.__layout_.__begin_ptr(), __x.__layout_.__end_ptr(), __x.size());
267 }
268 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI
269 vector(const vector& __x, const __type_identity_t<allocator_type>& __a)
270 : __layout_(__a) {
271 __init_with_size(__x.__layout_.__begin_ptr(), __x.__layout_.__end_ptr(), __x.size());
272 }
273
274 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI vector& operator=(const vector& __other) {
275 if (this == std::addressof(__other))
276 return *this;
277
278 if _LIBCPP_CONSTEXPR (__alloc_traits::propagate_on_container_copy_assignment::value) {
279 if (this->__layout_.__alloc() != __other.__layout_.__alloc()) {
280 clear();
281 __annotate_delete();
282 __alloc_traits::deallocate(this->__layout_.__alloc(), this->__layout_.__begin_ptr(), capacity());
283 __layout_.__reset_without_allocator();
284 }
285 this->__layout_.__alloc() = __other.__layout_.__alloc();
286 }
287
288 assign(__other.__layout_.__begin_ptr(), __other.__layout_.__end_ptr());
289 return *this;
290 }
291
292#ifndef _LIBCPP_CXX03_LANG
293 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI vector(initializer_list<value_type> __il) {
294 __init_with_size(__il.begin(), __il.end(), __il.size());
295 }
296
297 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI
298 vector(initializer_list<value_type> __il, const allocator_type& __a)
299 : __layout_(__a) {
300 __init_with_size(__il.begin(), __il.end(), __il.size());
301 }
302
303 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI vector& operator=(initializer_list<value_type> __il) {
304 assign(__il.begin(), __il.end());
305 return *this;
306 }
307#endif // !_LIBCPP_CXX03_LANG
308
309 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI vector(vector&& __x)
310#if _LIBCPP_STD_VER >= 17
311 noexcept;
312#else
313 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value);
314#endif
315
316 _LIBCPP_CONSTEXPR_SINCE_CXX20
317 _LIBCPP_HIDE_FROM_ABI vector(vector&& __x, const __type_identity_t<allocator_type>& __a);
318
319 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI vector& operator=(vector&& __other)
320 _NOEXCEPT_(__noexcept_move_assign_container<_Allocator, __alloc_traits>::value) {
321 if _LIBCPP_CONSTEXPR (__alloc_traits::propagate_on_container_move_assignment::value) {
322 __vdeallocate();
323 __layout_.__alloc() = std::move(__other.__layout_.__alloc());
324 } else {
325 if (__layout_.__alloc() != __other.__layout_.__alloc()) {
326 typedef move_iterator<iterator> _Ip;
327 assign(_Ip(__other.begin()), _Ip(__other.end()));
328 return *this;
329 }
330 __vdeallocate();
331 }
332
333 __layout_.__move_assign_without_allocator(__other.__layout_);
334 return *this;
335 }
336
337 template <class _InputIterator,
338 __enable_if_t<__has_exactly_input_iterator_category<_InputIterator>::value &&
339 is_constructible<value_type, typename iterator_traits<_InputIterator>::reference>::value,
340 int> = 0>
341 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void assign(_InputIterator __first, _InputIterator __last) {
342 __assign_with_sentinel(__first, __last);
343 }
344 template <
345 class _ForwardIterator,
346 __enable_if_t<__has_forward_iterator_category<_ForwardIterator>::value &&
347 is_constructible<value_type, typename iterator_traits<_ForwardIterator>::reference>::value,
348 int> = 0>
349 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void assign(_ForwardIterator __first, _ForwardIterator __last) {
350 __assign_with_size<_ClassicAlgPolicy>(__first, __last, std::distance(__first, __last));
351 }
352
353#if _LIBCPP_STD_VER >= 23
354 template <_ContainerCompatibleRange<_Tp> _Range>
355 _LIBCPP_HIDE_FROM_ABI constexpr void assign_range(_Range&& __range) {
356 if constexpr (ranges::forward_range<_Range> || ranges::sized_range<_Range>) {
357 auto __n = static_cast<size_type>(ranges::distance(__range));
358 __assign_with_size<_RangeAlgPolicy>(ranges::begin(__range), ranges::end(__range), __n);
359
360 } else {
361 __assign_with_sentinel(ranges::begin(__range), ranges::end(__range));
362 }
363 }
364#endif
365
366 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void assign(size_type __n, const_reference __u);
367
368#ifndef _LIBCPP_CXX03_LANG
369 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void assign(initializer_list<value_type> __il) {
370 assign(__il.begin(), __il.end());
371 }
372#endif
373
374 [[__nodiscard__]] _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI allocator_type get_allocator() const _NOEXCEPT {
375 return this->__layout_.__alloc();
376 }
377
378 //
379 // Iterators
380 //
381 [[__nodiscard__]] _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI iterator begin() _NOEXCEPT {
382 return __make_iter(__add_alignment_assumption(this->__layout_.__begin_ptr()));
383 }
384 [[__nodiscard__]] _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI const_iterator begin() const _NOEXCEPT {
385 return __make_iter(__add_alignment_assumption(this->__layout_.__begin_ptr()));
386 }
387 [[__nodiscard__]] _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI iterator end() _NOEXCEPT {
388 return __make_iter(__add_alignment_assumption(__layout_.__end_ptr()));
389 }
390 [[__nodiscard__]] _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI const_iterator end() const _NOEXCEPT {
391 return __make_iter(__add_alignment_assumption(__layout_.__end_ptr()));
392 }
393
394 [[__nodiscard__]] _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI reverse_iterator rbegin() _NOEXCEPT {
395 return reverse_iterator(end());
396 }
397 [[__nodiscard__]] _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI const_reverse_iterator
398 rbegin() const _NOEXCEPT {
399 return const_reverse_iterator(end());
400 }
401 [[__nodiscard__]] _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI reverse_iterator rend() _NOEXCEPT {
402 return reverse_iterator(begin());
403 }
404 [[__nodiscard__]] _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI const_reverse_iterator rend() const _NOEXCEPT {
405 return const_reverse_iterator(begin());
406 }
407
408 [[__nodiscard__]] _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI const_iterator cbegin() const _NOEXCEPT {
409 return begin();
410 }
411 [[__nodiscard__]] _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI const_iterator cend() const _NOEXCEPT {
412 return end();
413 }
414 [[__nodiscard__]] _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI const_reverse_iterator
415 crbegin() const _NOEXCEPT {
416 return rbegin();
417 }
418 [[__nodiscard__]] _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI const_reverse_iterator crend() const _NOEXCEPT {
419 return rend();
420 }
421
422 //
423 // [vector.capacity], capacity
424 //
425 [[__nodiscard__]] _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI size_type size() const _NOEXCEPT {
426 return __layout_.__size();
427 }
428 [[__nodiscard__]] _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI size_type capacity() const _NOEXCEPT {
429 return __layout_.__capacity();
430 }
431 [[__nodiscard__]] _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI bool empty() const _NOEXCEPT {
432 return __layout_.__empty();
433 }
434
435 [[__nodiscard__]] _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI size_type max_size() const _NOEXCEPT {
436 return std::min<size_type>(__alloc_traits::max_size(__layout_.__alloc()), numeric_limits<difference_type>::max());
437 }
438 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void reserve(size_type __n);
439 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void shrink_to_fit() _NOEXCEPT;
440
441 //
442 // element access
443 //
444 [[__nodiscard__]] _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI reference operator[](size_type __n) _NOEXCEPT {
445 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(__n < size(), "vector[] index out of bounds");
446 return this->__layout_.__begin_ptr()[__n];
447 }
448 [[__nodiscard__]] _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI const_reference
449 operator[](size_type __n) const _NOEXCEPT {
450 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(__n < size(), "vector[] index out of bounds");
451 return this->__layout_.__begin_ptr()[__n];
452 }
453 [[__nodiscard__]] _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI reference at(size_type __n) {
454 if (__n >= size())
455 this->__throw_out_of_range();
456 return this->__layout_.__begin_ptr()[__n];
457 }
458 [[__nodiscard__]] _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI const_reference at(size_type __n) const {
459 if (__n >= size())
460 this->__throw_out_of_range();
461 return this->__layout_.__begin_ptr()[__n];
462 }
463
464 [[__nodiscard__]] _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI reference front() _NOEXCEPT {
465 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(!empty(), "front() called on an empty vector");
466 return *this->__layout_.__begin_ptr();
467 }
468 [[__nodiscard__]] _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI const_reference front() const _NOEXCEPT {
469 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(!empty(), "front() called on an empty vector");
470 return *this->__layout_.__begin_ptr();
471 }
472 [[__nodiscard__]] _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI reference back() _NOEXCEPT {
473 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(!empty(), "back() called on an empty vector");
474 return end()[-1];
475 }
476 [[__nodiscard__]] _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI const_reference back() const _NOEXCEPT {
477 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(!empty(), "back() called on an empty vector");
478 return end()[-1];
479 }
480
481 //
482 // [vector.data], data access
483 //
484 [[__nodiscard__]] _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI value_type* data() _NOEXCEPT {
485 return __layout_.__data();
486 }
487
488 [[__nodiscard__]] _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI const value_type* data() const _NOEXCEPT {
489 return __layout_.__data();
490 }
491
492 //
493 // [vector.modifiers], modifiers
494 //
495 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void push_back(const_reference __x) { emplace_back(__x); }
496
497 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void push_back(value_type&& __x) { emplace_back(std::move(__x)); }
498
499 template <class... _Args>
500 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI __emplace_back_result_t emplace_back(_Args&&... __args);
501
502 template <class... _Args>
503 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __emplace_back_assume_capacity(_Args&&... __args) {
504 _LIBCPP_ASSERT_INTERNAL(
505 size() < capacity(), "We assume that we have enough space to insert an element at the end of the vector");
506 _ConstructTransaction __tx(*this, 1);
507 __alloc_traits::construct(
508 this->__layout_.__alloc(), std::__to_address(__tx.__pos_), std::forward<_Args>(__args)...);
509 ++__tx.__pos_;
510 }
511
512#if _LIBCPP_STD_VER >= 23
513 template <_ContainerCompatibleRange<_Tp> _Range>
514 _LIBCPP_HIDE_FROM_ABI constexpr void append_range(_Range&& __range) {
515 if constexpr (ranges::forward_range<_Range> || ranges::sized_range<_Range>) {
516 auto __len = ranges::distance(__range);
517 if (__len <= static_cast<difference_type>(__layout_.__remaining_capacity())) {
518 __construct_at_end(ranges::begin(__range), ranges::end(__range), __len);
519 } else {
520 _SplitBuffer __buffer(__recommend(new_size: size() + __len), size(), __layout_.__alloc());
521 __buffer.__construct_at_end_with_size(ranges::begin(__range), __len);
522 __layout_.__relocate(__buffer);
523 }
524 } else {
525 vector __buffer(__layout_.__alloc());
526 for (auto&& __val : __range)
527 __buffer.emplace_back(std::forward<decltype(__val)>(__val));
528 append_range(ranges::as_rvalue_view(__buffer));
529 }
530 }
531#endif
532
533 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void pop_back() {
534 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(!empty(), "vector::pop_back called on an empty vector");
535 this->__destruct_at_end(new_last: __layout_.__end_ptr() - 1);
536 }
537
538 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI iterator insert(const_iterator __position, const_reference __x) {
539 return emplace(__position, __x);
540 }
541
542 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI iterator insert(const_iterator __position, value_type&& __x) {
543 return emplace(__position, std::move(__x));
544 }
545
546 template <class... _Args>
547 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI iterator emplace(const_iterator __position, _Args&&... __args);
548
549 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI iterator
550 insert(const_iterator __position, size_type __n, const_reference __x);
551
552 template <class _InputIterator,
553 __enable_if_t<__has_exactly_input_iterator_category<_InputIterator>::value &&
554 is_constructible< value_type, typename iterator_traits<_InputIterator>::reference>::value,
555 int> = 0>
556 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI iterator
557 insert(const_iterator __position, _InputIterator __first, _InputIterator __last) {
558 return __insert_with_sentinel(__position, __first, __last);
559 }
560
561 template <
562 class _ForwardIterator,
563 __enable_if_t<__has_forward_iterator_category<_ForwardIterator>::value &&
564 is_constructible< value_type, typename iterator_traits<_ForwardIterator>::reference>::value,
565 int> = 0>
566 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI iterator
567 insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last) {
568 return __insert_with_size<_ClassicAlgPolicy>(__position, __first, __last, std::distance(__first, __last));
569 }
570
571#if _LIBCPP_STD_VER >= 23
572 template <_ContainerCompatibleRange<_Tp> _Range>
573 _LIBCPP_HIDE_FROM_ABI constexpr iterator insert_range(const_iterator __position, _Range&& __range) {
574 if constexpr (ranges::forward_range<_Range> || ranges::sized_range<_Range>) {
575 auto __n = static_cast<size_type>(ranges::distance(__range));
576 return __insert_with_size<_RangeAlgPolicy>(__position, ranges::begin(__range), ranges::end(__range), __n);
577
578 } else {
579 return __insert_with_sentinel(__position, ranges::begin(__range), ranges::end(__range));
580 }
581 }
582#endif
583
584#ifndef _LIBCPP_CXX03_LANG
585 _LIBCPP_CONSTEXPR_SINCE_CXX20
586 _LIBCPP_HIDE_FROM_ABI iterator insert(const_iterator __position, initializer_list<value_type> __il) {
587 return insert(__position, __il.begin(), __il.end());
588 }
589#endif
590
591 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI iterator erase(const_iterator __position) {
592 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
593 __position != end(), "vector::erase(iterator) called with a non-dereferenceable iterator");
594 return erase(__position, __position + 1);
595 }
596
597 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI iterator erase(const_iterator __first, const_iterator __last) {
598 _LIBCPP_ASSERT_VALID_INPUT_RANGE(__first <= __last, "vector::erase(first, last) called with invalid range");
599 pointer __pos = this->__layout_.__begin_ptr() + (__first - begin());
600 auto __hole_size = __last - __first;
601 if (__first != __last) {
602#ifndef _LIBCPP_CXX03_LANG
603 // If the value_type is trivially relocatable, we destroy the range being erased and we relocate the tail
604 // of the vector into the created gap. Otherwise, we use the standard technique with move-assignments.
605 //
606 // We really only need is_nothrow_relocatable, but vector::erase is currently overspecified to use assignment.
607 // We use is_trivially_relocatable unil that is relaxed.
608 if constexpr (__is_trivially_relocatable_v<value_type> &&
609 __allocator_has_trivial_move_construct_v<allocator_type, value_type> &&
610 __allocator_has_trivial_destroy_v<allocator_type, value_type>) {
611 auto __raw_pos = std::__to_address(__pos);
612 auto __old_size = size();
613 std::__destroy(__raw_pos, __raw_pos + __hole_size);
614 std::__uninitialized_relocate(__raw_pos + __hole_size, std::__to_address(__layout_.__end_ptr()), __raw_pos);
615 __layout_.__set_bound_using_pointer(__layout_.__end_ptr() - __hole_size);
616 __annotate_shrink(__old_size);
617 return __make_iter(__pos);
618 }
619#endif
620 this->__destruct_at_end(new_last: std::move(__pos + __hole_size, __layout_.__end_ptr(), __pos));
621 }
622 return __make_iter(__pos);
623 }
624
625 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void clear() _NOEXCEPT {
626 __destruct_at_end(new_last: __layout_.__begin_ptr());
627 }
628
629 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void resize(size_type __sz);
630 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void resize(size_type __sz, const_reference __x);
631
632 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void swap(vector&)
633#if _LIBCPP_STD_VER >= 14
634 _NOEXCEPT;
635#else
636 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value || __is_nothrow_swappable_v<allocator_type>);
637#endif
638
639 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI bool __invariants() const _NOEXCEPT {
640 return __layout_.__invariants();
641 }
642
643private:
644 __base_type __layout_;
645
646 // Allocate space for __n objects
647 // throws length_error if __n > max_size()
648 // throws (probably bad_alloc) if memory run out
649 // Precondition: begin() == nullptr
650 // Precondition: size() == 0
651 // Precondition: capacity() == 0
652 // Precondition: __n > 0
653 // Postcondition: capacity() >= __n
654 // Postcondition: size() == 0
655 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __vallocate(size_type __n) {
656 _LIBCPP_ASSERT_INTERNAL(
657 __layout_.__begin_ptr() == nullptr,
658 "vector::__vallocate can only be called on a vector that hasn't allocated memory. This vector either already "
659 "owns a buffer, or a deallocation function didn't reset the layout's begin pointer.");
660 _LIBCPP_ASSERT_INTERNAL(
661 size() == 0,
662 "vector::__vallocate can only be called on a vector that hasn't allocated memory. This vector either already "
663 "owns a buffer, or a deallocation function didn't reset the layout's size.");
664 _LIBCPP_ASSERT_INTERNAL(
665 __layout_.__capacity() == 0,
666 "vector::__vallocate can only be called on a vector that hasn't allocated memory. This vector either already "
667 "owns a buffer, or a deallocation function didn't reset the layout's capacity.");
668 _LIBCPP_ASSERT_INTERNAL(__n > 0, "vector::__vallocate cannot allocate 0 bytes");
669
670 if (__n > max_size())
671 this->__throw_length_error();
672 auto __allocation = std::__allocate_at_least(this->__layout_.__alloc(), __n);
673 __layout_.__set_layout(__allocation.ptr, 0, __allocation.count);
674 __annotate_new(current_size: 0);
675 }
676
677 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __vdeallocate() _NOEXCEPT;
678 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI size_type __recommend(size_type __new_size) const;
679 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __construct_at_end(size_type __n);
680 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __construct_at_end(size_type __n, const_reference __x);
681
682 template <class _InputIterator, class _Sentinel>
683 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void
684 __init_with_size(_InputIterator __first, _Sentinel __last, size_type __n) {
685 auto __guard = std::__make_exception_guard(__destroy_vector(*this));
686
687 if (__n > 0) {
688 __vallocate(__n);
689 __construct_at_end(std::move(__first), std::move(__last), __n);
690 }
691
692 __guard.__complete();
693 }
694
695 template <class _InputIterator, class _Sentinel>
696 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void
697 __init_with_sentinel(_InputIterator __first, _Sentinel __last) {
698 auto __guard = std::__make_exception_guard(__destroy_vector(*this));
699
700 for (; __first != __last; ++__first)
701 emplace_back(*__first);
702
703 __guard.__complete();
704 }
705
706 template <class _Iterator, class _Sentinel>
707 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __assign_with_sentinel(_Iterator __first, _Sentinel __last);
708
709 // The `_Iterator` in `*_with_size` functions can be input-only only if called from `*_range` (since C++23).
710 // Otherwise, `_Iterator` is a forward iterator.
711
712 template <class _AlgPolicy, class _Iterator, class _Sentinel>
713 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void
714 __assign_with_size(_Iterator __first, _Sentinel __last, difference_type __n);
715
716 template <class _AlgPolicy,
717 class _Iterator,
718 __enable_if_t<!is_same<__policy_value_type<_AlgPolicy, _Iterator>, value_type>::value, int> = 0>
719 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void
720 __insert_assign_n_unchecked(_Iterator __first, difference_type __n, pointer __position) {
721 for (pointer __end_position = __position + __n; __position != __end_position; ++__position, (void)++__first) {
722 __temp_value<value_type, _Allocator> __tmp(this->__layout_.__alloc(), *__first);
723 *__position = std::move(__tmp.get());
724 }
725 }
726
727 template <class _AlgPolicy,
728 class _Iterator,
729 __enable_if_t<is_same<__policy_value_type<_AlgPolicy, _Iterator>, value_type>::value, int> = 0>
730 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void
731 __insert_assign_n_unchecked(_Iterator __first, difference_type __n, pointer __position) {
732 std::__copy_n<_AlgPolicy>(std::move(__first), __n, __position);
733 }
734
735 template <class _InputIterator, class _Sentinel>
736 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI iterator
737 __insert_with_sentinel(const_iterator __position, _InputIterator __first, _Sentinel __last);
738
739 template <class _AlgPolicy, class _Iterator, class _Sentinel>
740 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI iterator
741 __insert_with_size(const_iterator __position, _Iterator __first, _Sentinel __last, difference_type __n);
742
743 template <class _InputIterator, class _Sentinel>
744 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void
745 __construct_at_end(_InputIterator __first, _Sentinel __last, size_type __n);
746
747 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI iterator __make_iter(pointer __p) _NOEXCEPT {
748#ifdef _LIBCPP_ABI_BOUNDED_ITERATORS_IN_VECTOR
749 // Bound the iterator according to the capacity, rather than the size.
750 //
751 // Vector guarantees that iterators stay valid as long as no reallocation occurs even if new elements are inserted
752 // into the container; for these cases, we need to make sure that the newly-inserted elements can be accessed
753 // through the bounded iterator without failing checks. The downside is that the bounded iterator won't catch
754 // access that is logically out-of-bounds, i.e., goes beyond the size, but is still within the capacity. With the
755 // current implementation, there is no connection between a bounded iterator and its associated container, so we
756 // don't have a way to update existing valid iterators when the container is resized and thus have to go with
757 // a laxer approach.
758 return std::__make_bounded_iter(__p, __layout_.__begin_ptr(), __layout_.__capacity_ptr());
759#else
760 return iterator(__p);
761#endif // _LIBCPP_ABI_BOUNDED_ITERATORS_IN_VECTOR
762 }
763
764 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI const_iterator __make_iter(const_pointer __p) const _NOEXCEPT {
765#ifdef _LIBCPP_ABI_BOUNDED_ITERATORS_IN_VECTOR
766 // Bound the iterator according to the capacity, rather than the size.
767 return std::__make_bounded_iter(__p, __layout_.__begin_ptr(), __layout_.__capacity_ptr());
768#else
769 return const_iterator(__p);
770#endif // _LIBCPP_ABI_BOUNDED_ITERATORS_IN_VECTOR
771 }
772
773 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void
774 __move_range(pointer __from_s, pointer __from_e, pointer __to);
775
776 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __destruct_at_end(pointer __new_last) _NOEXCEPT {
777 size_type __old_size = size();
778 std::__allocator_destroy(__layout_.__alloc(), __new_last, __layout_.__end_ptr());
779 __layout_.__set_bound_using_pointer(__new_last);
780 __annotate_shrink(__old_size);
781 }
782
783 // The following functions are no-ops outside of AddressSanitizer mode.
784 // We call annotations for every allocator, unless explicitly disabled.
785 //
786 // To disable annotations for a particular allocator, change value of
787 // __asan_annotate_container_with_allocator to false.
788 // For more details, see the "Using libc++" documentation page or
789 // the documentation for __sanitizer_annotate_contiguous_container.
790
791 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void
792 __annotate_contiguous_container(const void* __old_mid, const void* __new_mid) const {
793 std::__annotate_contiguous_container<_Allocator>(data(), data() + capacity(), __old_mid, __new_mid);
794 }
795
796 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __annotate_new(size_type __current_size) const _NOEXCEPT {
797 __annotate_contiguous_container(old_mid: data() + capacity(), new_mid: data() + __current_size);
798 }
799
800 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __annotate_delete() const _NOEXCEPT {
801 __annotate_contiguous_container(old_mid: data() + size(), new_mid: data() + capacity());
802 }
803
804 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __annotate_increase(size_type __n) const _NOEXCEPT {
805 __annotate_contiguous_container(old_mid: data() + size(), new_mid: data() + size() + __n);
806 }
807
808 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __annotate_shrink(size_type __old_size) const _NOEXCEPT {
809 __annotate_contiguous_container(old_mid: data() + __old_size, new_mid: data() + size());
810 }
811
812 struct _ConstructTransaction {
813 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI explicit _ConstructTransaction(vector& __v, size_type __n)
814 : __v_(__v), __pos_(__v.__layout_.__end_ptr()), __new_end_(__pos_ + __n) {
815 __v_.__annotate_increase(__n);
816 }
817
818 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI ~_ConstructTransaction() {
819 __v_.__layout_.__set_bound_using_pointer(__pos_);
820 if (__pos_ != __new_end_) {
821 __v_.__annotate_shrink(old_size: __new_end_ - __v_.__layout_.__begin_ptr());
822 }
823 }
824
825 vector& __v_;
826 pointer __pos_;
827 const_pointer const __new_end_;
828
829 _ConstructTransaction(_ConstructTransaction const&) = delete;
830 _ConstructTransaction& operator=(_ConstructTransaction const&) = delete;
831 };
832
833 [[__noreturn__]] _LIBCPP_HIDE_FROM_ABI static void __throw_length_error() { std::__throw_length_error(msg: "vector"); }
834
835 [[__noreturn__]] _LIBCPP_HIDE_FROM_ABI static void __throw_out_of_range() { std::__throw_out_of_range(msg: "vector"); }
836
837 template <class _Ptr = pointer, __enable_if_t<is_pointer<_Ptr>::value, int> = 0>
838 static _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI _LIBCPP_NO_CFI _Ptr
839 __add_alignment_assumption(_Ptr __p) _NOEXCEPT {
840 if (!__libcpp_is_constant_evaluated()) {
841 return static_cast<_Ptr>(__builtin_assume_aligned(__p, _LIBCPP_ALIGNOF(decltype(*__p))));
842 }
843 return __p;
844 }
845
846 template <class _Ptr = pointer, __enable_if_t<!is_pointer<_Ptr>::value, int> = 0>
847 static _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI _LIBCPP_NO_CFI _Ptr
848 __add_alignment_assumption(_Ptr __p) _NOEXCEPT {
849 return __p;
850 }
851};
852
853#if _LIBCPP_STD_VER >= 17
854template <class _InputIterator,
855 class _Alloc = allocator<__iterator_value_type<_InputIterator>>,
856 class = enable_if_t<__has_input_iterator_category<_InputIterator>::value>,
857 class = enable_if_t<__is_allocator_v<_Alloc>>>
858vector(_InputIterator, _InputIterator) -> vector<__iterator_value_type<_InputIterator>, _Alloc>;
859
860template <class _InputIterator,
861 class _Alloc,
862 class = enable_if_t<__has_input_iterator_category<_InputIterator>::value>,
863 class = enable_if_t<__is_allocator_v<_Alloc>>>
864vector(_InputIterator, _InputIterator, _Alloc) -> vector<__iterator_value_type<_InputIterator>, _Alloc>;
865#endif
866
867#if _LIBCPP_STD_VER >= 23
868template <ranges::input_range _Range,
869 class _Alloc = allocator<ranges::range_value_t<_Range>>,
870 class = enable_if_t<__is_allocator_v<_Alloc>>>
871vector(from_range_t, _Range&&, _Alloc = _Alloc()) -> vector<ranges::range_value_t<_Range>, _Alloc>;
872#endif
873
874template <class _Tp, class _Allocator>
875_LIBCPP_CONSTEXPR_SINCE_CXX20 void vector<_Tp, _Allocator>::__vdeallocate() _NOEXCEPT {
876 if (this->__layout_.__begin_ptr() != nullptr) {
877 clear();
878 __annotate_delete();
879 __alloc_traits::deallocate(this->__layout_.__alloc(), this->__layout_.__begin_ptr(), capacity());
880 __layout_.__reset_without_allocator();
881 }
882}
883
884// Precondition: __new_size > capacity()
885template <class _Tp, class _Allocator>
886_LIBCPP_CONSTEXPR_SINCE_CXX20 inline _LIBCPP_HIDE_FROM_ABI typename vector<_Tp, _Allocator>::size_type
887vector<_Tp, _Allocator>::__recommend(size_type __new_size) const {
888 const size_type __ms = max_size();
889 if (__new_size > __ms)
890 this->__throw_length_error();
891 const size_type __cap = capacity();
892 if (__cap >= __ms / 2)
893 return __ms;
894 return std::max<size_type>(2 * __cap, __new_size);
895}
896
897// Default constructs __n objects starting at __layout_.__end_ptr()
898// throws if construction throws
899// Precondition: __n > 0
900// Precondition: size() + __n <= capacity()
901// Postcondition: size() == size() + __n
902template <class _Tp, class _Allocator>
903_LIBCPP_CONSTEXPR_SINCE_CXX20 void vector<_Tp, _Allocator>::__construct_at_end(size_type __n) {
904 _ConstructTransaction __tx(*this, __n);
905 const_pointer __new_end = __tx.__new_end_;
906 for (pointer __pos = __tx.__pos_; __pos != __new_end; __tx.__pos_ = ++__pos) {
907 __alloc_traits::construct(this->__layout_.__alloc(), std::__to_address(__pos));
908 }
909}
910
911// Copy constructs __n objects starting at __layout_.__end_ptr() from __x
912// throws if construction throws
913// Precondition: __n > 0
914// Precondition: size() + __n <= capacity()
915// Postcondition: size() == old size() + __n
916// Postcondition: [i] == __x for all i in [size() - __n, __n)
917template <class _Tp, class _Allocator>
918_LIBCPP_CONSTEXPR_SINCE_CXX20 inline void
919vector<_Tp, _Allocator>::__construct_at_end(size_type __n, const_reference __x) {
920 _ConstructTransaction __tx(*this, __n);
921 const_pointer __new_end = __tx.__new_end_;
922 for (pointer __pos = __tx.__pos_; __pos != __new_end; __tx.__pos_ = ++__pos) {
923 __alloc_traits::construct(this->__layout_.__alloc(), std::__to_address(__pos), __x);
924 }
925}
926
927template <class _Tp, class _Allocator>
928template <class _InputIterator, class _Sentinel>
929_LIBCPP_CONSTEXPR_SINCE_CXX20 void
930vector<_Tp, _Allocator>::__construct_at_end(_InputIterator __first, _Sentinel, size_type __n) {
931 _ConstructTransaction __tx(*this, __n);
932 __tx.__pos_ = std::__uninitialized_allocator_copy_n(this->__layout_.__alloc(), std::move(__first), __n, __tx.__pos_);
933}
934
935template <class _Tp, class _Allocator>
936_LIBCPP_CONSTEXPR_SINCE_CXX20 inline _LIBCPP_HIDE_FROM_ABI vector<_Tp, _Allocator>::vector(vector&& __x)
937#if _LIBCPP_STD_VER >= 17
938 noexcept
939#else
940 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
941#endif
942 : __layout_(std::move(__x.__layout_)) {
943}
944
945template <class _Tp, class _Allocator>
946_LIBCPP_CONSTEXPR_SINCE_CXX20 inline _LIBCPP_HIDE_FROM_ABI
947vector<_Tp, _Allocator>::vector(vector&& __x, const __type_identity_t<allocator_type>& __a)
948 : __layout_(__a) {
949 if (__a == __x.__layout_.__alloc()) {
950 __layout_.__move_assign_without_allocator(__x.__layout_);
951 } else {
952 typedef move_iterator<iterator> _Ip;
953 __init_with_size(_Ip(__x.begin()), _Ip(__x.end()), __x.size());
954 }
955}
956
957template <class _Tp, class _Allocator>
958template <class _Iterator, class _Sentinel>
959_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void
960vector<_Tp, _Allocator>::__assign_with_sentinel(_Iterator __first, _Sentinel __last) {
961 pointer __cur = __layout_.__begin_ptr();
962 pointer __end = __layout_.__end_ptr();
963 for (; __first != __last && __cur != __end; ++__first, (void)++__cur)
964 *__cur = *__first;
965 if (__cur != __end) {
966 __destruct_at_end(new_last: __cur);
967 } else {
968 for (; __first != __last; ++__first)
969 emplace_back(*__first);
970 }
971}
972
973template <class _Tp, class _Allocator>
974template <class _AlgPolicy, class _Iterator, class _Sentinel>
975_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void
976vector<_Tp, _Allocator>::__assign_with_size(_Iterator __first, _Sentinel __last, difference_type __n) {
977 size_type __new_size = static_cast<size_type>(__n);
978 if (__new_size <= capacity()) {
979 auto const __size = size();
980 if (__new_size > __size) {
981 auto __mid = std::__copy_n<_AlgPolicy>(std::move(__first), __size, this->__layout_.__begin_ptr()).__in_;
982 __construct_at_end(std::move(__mid), std::move(__last), __new_size - __size);
983 } else {
984 pointer __m = std::__copy_n<_AlgPolicy>(std::move(__first), __n, this->__layout_.__begin_ptr()).__out_;
985 this->__destruct_at_end(new_last: __m);
986 }
987 } else {
988 __vdeallocate();
989 __vallocate(n: __recommend(__new_size));
990 __construct_at_end(std::move(__first), std::move(__last), __new_size);
991 }
992}
993
994template <class _Tp, class _Allocator>
995_LIBCPP_CONSTEXPR_SINCE_CXX20 void vector<_Tp, _Allocator>::assign(size_type __n, const_reference __u) {
996 if (__n <= capacity()) {
997 size_type __s = size();
998 std::fill_n(this->__layout_.__begin_ptr(), std::min(__n, __s), __u);
999 if (__n > __s)
1000 __construct_at_end(__n - __s, __u);
1001 else
1002 this->__destruct_at_end(new_last: this->__layout_.__begin_ptr() + __n);
1003 } else {
1004 __vdeallocate();
1005 __vallocate(n: __recommend(new_size: static_cast<size_type>(__n)));
1006 __construct_at_end(__n, __u);
1007 }
1008}
1009
1010template <class _Tp, class _Allocator>
1011_LIBCPP_CONSTEXPR_SINCE_CXX20 void vector<_Tp, _Allocator>::reserve(size_type __n) {
1012 if (__n > capacity()) {
1013 if (__n > max_size())
1014 this->__throw_length_error();
1015 _SplitBuffer __v(__n, size(), this->__layout_.__alloc());
1016 __layout_.__relocate(__v);
1017 }
1018}
1019
1020template <class _Tp, class _Allocator>
1021_LIBCPP_CONSTEXPR_SINCE_CXX20 void vector<_Tp, _Allocator>::shrink_to_fit() _NOEXCEPT {
1022 if (capacity() > size()) {
1023#if _LIBCPP_HAS_EXCEPTIONS
1024 try {
1025#endif // _LIBCPP_HAS_EXCEPTIONS
1026 _SplitBuffer __v(size(), size(), this->__layout_.__alloc());
1027 // The Standard mandates shrink_to_fit() does not increase the capacity.
1028 // With equal capacity keep the existing buffer. This avoids extra work
1029 // due to swapping the elements.
1030 if (__v.capacity() < capacity())
1031 __layout_.__relocate(__v);
1032#if _LIBCPP_HAS_EXCEPTIONS
1033 } catch (...) {
1034 }
1035#endif // _LIBCPP_HAS_EXCEPTIONS
1036 }
1037}
1038
1039// This makes the compiler inline `__else()` if `__cond` is known to be false. Currently LLVM doesn't do that without
1040// the `__builtin_constant_p`, since it considers `__else` unlikely even through it's known to be run.
1041// See https://llvm.org/PR154292
1042template <class _If, class _Else, class... _Args>
1043_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 decltype(std::declval<_If>()(std::declval<_Args&&>()...))
1044__if_likely_else(bool __cond, _If __if, _Else __else, _Args&&... __args) {
1045 static_assert(
1046 is_same<decltype(__if(std::forward<_Args>(__args)...)), decltype(__else(std::forward<_Args>(__args)...))>::value,
1047 "Return type of if and else branch have to be the same type");
1048 if (__builtin_constant_p(__cond)) {
1049 if (__cond)
1050 return __if(std::forward<_Args>(__args)...);
1051 else
1052 return __else(std::forward<_Args>(__args)...);
1053 } else {
1054 if (__cond) [[__likely__]]
1055 return __if(std::forward<_Args>(__args)...);
1056 else
1057 return __else(std::forward<_Args>(__args)...);
1058 }
1059}
1060
1061template <class _Tp, class _Alloc>
1062template <class... _Args>
1063_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI typename vector<_Tp, _Alloc>::__emplace_back_result_t
1064vector<_Tp, _Alloc>::emplace_back(_Args&&... __args) {
1065 std::__if_likely_else(
1066 size() != capacity(),
1067 [](vector& __self, _Args&&... __largs) static {
1068 __self.__emplace_back_assume_capacity(std::forward<_Args>(__largs)...);
1069 },
1070 [](vector& __self, _Args&&... __largs) static {
1071 _SplitBuffer __v(__self.__recommend(new_size: __self.size() + 1), __self.size(), __self.__layout_.__alloc());
1072 // __v.emplace_back(std::forward<_Args>(__args)...);
1073 pointer __end = __v.end();
1074 __alloc_traits::construct(
1075 __self.__layout_.__alloc(), std::__to_address(__end), std::forward<_Args>(__largs)...);
1076 __v.__set_sentinel(++__end);
1077 __self.__layout_.__relocate(__v);
1078 },
1079 *this,
1080 std::forward<_Args>(__args)...);
1081
1082#if _LIBCPP_STD_VER >= 17
1083 return back();
1084#endif
1085}
1086
1087template <class _Tp, class _Allocator>
1088_LIBCPP_CONSTEXPR_SINCE_CXX20 void
1089vector<_Tp, _Allocator>::__move_range(pointer __from_s, pointer __from_e, pointer __to) {
1090 pointer __old_last = __layout_.__end_ptr();
1091 difference_type __n = __old_last - __to;
1092 {
1093 pointer __i = __from_s + __n;
1094 _ConstructTransaction __tx(*this, __from_e - __i);
1095 for (pointer __pos = __tx.__pos_; __i < __from_e; ++__i, (void)++__pos, __tx.__pos_ = __pos) {
1096 __alloc_traits::construct(this->__layout_.__alloc(), std::__to_address(__pos), std::move(*__i));
1097 }
1098 }
1099 std::move_backward(__from_s, __from_s + __n, __old_last);
1100}
1101
1102template <class _Tp, class _Allocator>
1103template <class... _Args>
1104_LIBCPP_CONSTEXPR_SINCE_CXX20 typename vector<_Tp, _Allocator>::iterator
1105vector<_Tp, _Allocator>::emplace(const_iterator __position, _Args&&... __args) {
1106 pointer __p = this->__layout_.__begin_ptr() + (__position - begin());
1107 if (size() != capacity()) {
1108 pointer __end = __layout_.__end_ptr();
1109 if (__p == __end) {
1110 __emplace_back_assume_capacity(std::forward<_Args>(__args)...);
1111 } else {
1112 __temp_value<value_type, _Allocator> __tmp(this->__layout_.__alloc(), std::forward<_Args>(__args)...);
1113 __move_range(from_s: __p, from_e: __end, to: __p + 1);
1114 *__p = std::move(__tmp.get());
1115 }
1116 } else {
1117 _SplitBuffer __v(__recommend(new_size: size() + 1), __p - this->__layout_.__begin_ptr(), this->__layout_.__alloc());
1118 __v.emplace_back(std::forward<_Args>(__args)...);
1119 __p = __layout_.__relocate_with_pivot(__v, __p);
1120 }
1121 return __make_iter(__p);
1122}
1123
1124template <class _Tp, class _Allocator>
1125_LIBCPP_CONSTEXPR_SINCE_CXX20 typename vector<_Tp, _Allocator>::iterator
1126vector<_Tp, _Allocator>::insert(const_iterator __position, size_type __n, const_reference __x) {
1127 pointer __p = this->__layout_.__begin_ptr() + (__position - begin());
1128 if (__n > 0) {
1129 if (__n <= __layout_.__remaining_capacity()) {
1130 size_type __old_n = __n;
1131 pointer __end = __layout_.__end_ptr();
1132 pointer __old_last = __end;
1133 if (__n > static_cast<size_type>(__end - __p)) {
1134 size_type __cx = __n - (__end - __p);
1135 __construct_at_end(__cx, __x);
1136 __n -= __cx;
1137 }
1138 if (__n > 0) {
1139 __move_range(from_s: __p, from_e: __old_last, to: __p + __old_n);
1140 const_pointer __xr = pointer_traits<const_pointer>::pointer_to(__x);
1141 if (std::__is_pointer_in_range(std::__to_address(__p), std::__to_address(__end), std::addressof(__x)))
1142 __xr += __old_n;
1143 std::fill_n(__p, __n, *__xr);
1144 }
1145 } else {
1146 _SplitBuffer __v(__recommend(new_size: size() + __n), __p - this->__layout_.__begin_ptr(), this->__layout_.__alloc());
1147 __v.__construct_at_end(__n, __x);
1148 __p = __layout_.__relocate_with_pivot(__v, __p);
1149 }
1150 }
1151 return __make_iter(__p);
1152}
1153
1154template <class _Tp, class _Allocator>
1155template <class _InputIterator, class _Sentinel>
1156_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI typename vector<_Tp, _Allocator>::iterator
1157vector<_Tp, _Allocator>::__insert_with_sentinel(const_iterator __position, _InputIterator __first, _Sentinel __last) {
1158 difference_type __off = __position - begin();
1159 pointer __p = this->__layout_.__begin_ptr() + __off;
1160 pointer __old_last = __layout_.__end_ptr();
1161 for (; size() != capacity() && __first != __last; ++__first)
1162 __emplace_back_assume_capacity(*__first);
1163
1164 if (__first == __last)
1165 (void)std::rotate(__p, __old_last, __layout_.__end_ptr());
1166 else {
1167 _SplitBuffer __v(__layout_.__alloc());
1168 pointer __end = __layout_.__end_ptr();
1169 auto __guard = std::__make_exception_guard(
1170 _AllocatorDestroyRangeReverse<allocator_type, pointer>(__layout_.__alloc(), __old_last, __end));
1171 __v.__construct_at_end_with_sentinel(std::move(__first), std::move(__last));
1172 _SplitBuffer __merged(
1173 __recommend(new_size: size() + __v.size()), __off, __layout_.__alloc()); // has `__off` positions available at the front
1174 std::__uninitialized_allocator_relocate(
1175 __layout_.__alloc(),
1176 std::__to_address(__old_last),
1177 std::__to_address(__layout_.__end_ptr()),
1178 std::__to_address(__merged.end()));
1179 __guard.__complete(); // Release the guard once objects in [__old_last_, __layout_.__end_ptr()) have been
1180 // successfully relocated.
1181 __merged.__set_sentinel(__merged.end() + (__layout_.__end_ptr() - __old_last));
1182 __layout_.__set_bound_using_pointer(__old_last);
1183 std::__uninitialized_allocator_relocate(
1184 __layout_.__alloc(),
1185 std::__to_address(__v.begin()),
1186 std::__to_address(__v.end()),
1187 std::__to_address(__merged.end()));
1188 __merged.__set_sentinel(__merged.size() + __v.size());
1189 __v.__set_sentinel(__v.begin());
1190 __p = __layout_.__relocate_with_pivot(__merged, __p);
1191 }
1192 return __make_iter(__p);
1193}
1194
1195template <class _Tp, class _Allocator>
1196template <class _AlgPolicy, class _Iterator, class _Sentinel>
1197_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI typename vector<_Tp, _Allocator>::iterator
1198vector<_Tp, _Allocator>::__insert_with_size(
1199 const_iterator __position, _Iterator __first, _Sentinel __last, difference_type __n) {
1200 pointer __p = this->__layout_.__begin_ptr() + (__position - begin());
1201 if (__n > 0) {
1202 if (__n <= static_cast<difference_type>(__layout_.__remaining_capacity())) {
1203 pointer __end = __layout_.__end_ptr();
1204 pointer __old_last = __end;
1205 difference_type __dx = __end - __p;
1206 if (__n > __dx) {
1207#if _LIBCPP_STD_VER >= 23
1208 if constexpr (!forward_iterator<_Iterator>) {
1209 __construct_at_end(std::move(__first), std::move(__last), __n);
1210 std::rotate(__p, __old_last, __end);
1211 } else
1212#endif
1213 {
1214 _Iterator __m = std::next(__first, __dx);
1215 __construct_at_end(__m, __last, __n - __dx);
1216 if (__dx > 0) {
1217 __move_range(from_s: __p, from_e: __old_last, to: __p + __n);
1218 __insert_assign_n_unchecked<_AlgPolicy>(__first, __dx, __p);
1219 }
1220 }
1221 } else {
1222 __move_range(from_s: __p, from_e: __old_last, to: __p + __n);
1223 __insert_assign_n_unchecked<_AlgPolicy>(std::move(__first), __n, __p);
1224 }
1225 } else {
1226 _SplitBuffer __v(__recommend(new_size: size() + __n), __p - this->__layout_.__begin_ptr(), this->__layout_.__alloc());
1227 __v.__construct_at_end_with_size(std::move(__first), __n);
1228 __p = __layout_.__relocate_with_pivot(__v, __p);
1229 }
1230 }
1231 return __make_iter(__p);
1232}
1233
1234template <class _Tp, class _Allocator>
1235_LIBCPP_CONSTEXPR_SINCE_CXX20 void vector<_Tp, _Allocator>::resize(size_type __new_size) {
1236 size_type __current_size = size();
1237 if (__current_size < __new_size) {
1238 if (__new_size <= capacity()) {
1239 __construct_at_end(__new_size - __current_size);
1240 } else {
1241 _SplitBuffer __v(__recommend(__new_size), __current_size, __layout_.__alloc());
1242 __v.__construct_at_end(__new_size - __current_size);
1243 __layout_.__relocate(__v);
1244 }
1245 } else if (__current_size > __new_size) {
1246 this->__destruct_at_end(new_last: this->__layout_.__begin_ptr() + __new_size);
1247 }
1248}
1249
1250template <class _Tp, class _Allocator>
1251_LIBCPP_CONSTEXPR_SINCE_CXX20 void vector<_Tp, _Allocator>::resize(size_type __new_size, const_reference __x) {
1252 size_type __current_size = size();
1253 if (__current_size < __new_size) {
1254 if (__new_size <= capacity())
1255 __construct_at_end(__new_size - __current_size, __x);
1256 else {
1257 _SplitBuffer __v(__recommend(__new_size), __current_size, __layout_.__alloc());
1258 __v.__construct_at_end(__new_size - __current_size, __x);
1259 __layout_.__relocate(__v);
1260 }
1261 } else if (__current_size > __new_size) {
1262 this->__destruct_at_end(new_last: this->__layout_.__begin_ptr() + __new_size);
1263 }
1264}
1265
1266template <class _Tp, class _Allocator>
1267_LIBCPP_CONSTEXPR_SINCE_CXX20 void vector<_Tp, _Allocator>::swap(vector& __x)
1268#if _LIBCPP_STD_VER >= 14
1269 _NOEXCEPT
1270#else
1271 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value || __is_nothrow_swappable_v<allocator_type>)
1272#endif
1273{
1274 _LIBCPP_ASSERT_COMPATIBLE_ALLOCATOR(
1275 __alloc_traits::propagate_on_container_swap::value || __layout_.__alloc() == __x.__layout_.__alloc(),
1276 "vector::swap: Either propagate_on_container_swap must be true"
1277 " or the allocators must compare equal");
1278 __layout_.__swap(__x.__layout_);
1279}
1280
1281#if _LIBCPP_STD_VER >= 20
1282template <>
1283inline constexpr bool __format::__enable_insertable<vector<char>> = true;
1284# if _LIBCPP_HAS_WIDE_CHARACTERS
1285template <>
1286inline constexpr bool __format::__enable_insertable<vector<wchar_t>> = true;
1287# endif
1288#endif // _LIBCPP_STD_VER >= 20
1289
1290_LIBCPP_END_NAMESPACE_STD
1291
1292_LIBCPP_POP_MACROS
1293
1294#endif // _LIBCPP___VECTOR_VECTOR_H
1295