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_BOOL_H
10#define _LIBCPP___VECTOR_VECTOR_BOOL_H
11
12#include <__algorithm/copy.h>
13#include <__algorithm/copy_backward.h>
14#include <__algorithm/copy_n.h>
15#include <__algorithm/fill_n.h>
16#include <__algorithm/iterator_operations.h>
17#include <__algorithm/max.h>
18#include <__algorithm/rotate.h>
19#include <__assert>
20#include <__bit_reference>
21#include <__config>
22#include <__functional/unary_function.h>
23#include <__fwd/bit_reference.h> // TODO: This is a workaround for https://llvm.org/PR131814
24#include <__fwd/functional.h>
25#include <__fwd/vector.h>
26#include <__iterator/distance.h>
27#include <__iterator/iterator_traits.h>
28#include <__iterator/reverse_iterator.h>
29#include <__memory/addressof.h>
30#include <__memory/allocate_at_least.h>
31#include <__memory/allocator.h>
32#include <__memory/allocator_traits.h>
33#include <__memory/compressed_pair.h>
34#include <__memory/construct_at.h>
35#include <__memory/noexcept_move_assign_container.h>
36#include <__memory/pointer_traits.h>
37#include <__memory/swap_allocator.h>
38#include <__ranges/access.h>
39#include <__ranges/concepts.h>
40#include <__ranges/container_compatible_range.h>
41#include <__ranges/from_range.h>
42#include <__type_traits/enable_if.h>
43#include <__type_traits/is_constant_evaluated.h>
44#include <__type_traits/is_nothrow_assignable.h>
45#include <__type_traits/is_nothrow_constructible.h>
46#include <__type_traits/type_identity.h>
47#include <__utility/exception_guard.h>
48#include <__utility/exchange.h>
49#include <__utility/forward.h>
50#include <__utility/move.h>
51#include <__utility/swap.h>
52#include <climits>
53#include <initializer_list>
54#include <limits>
55#include <stdexcept>
56
57// These headers define parts of vectors definition, since they define ADL functions or class specializations.
58#include <__vector/comparison.h>
59#include <__vector/container_traits.h>
60#include <__vector/swap.h>
61
62#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
63# pragma GCC system_header
64#endif
65
66_LIBCPP_PUSH_MACROS
67#include <__undef_macros>
68
69_LIBCPP_BEGIN_NAMESPACE_STD
70
71template <class _Allocator>
72struct hash<vector<bool, _Allocator> >;
73
74template <class _Allocator>
75struct __has_storage_type<vector<bool, _Allocator> > {
76 static const bool value = true;
77};
78
79template <class _Allocator>
80class vector<bool, _Allocator> {
81public:
82 using __self _LIBCPP_NODEBUG = vector;
83 using value_type = bool;
84 using allocator_type = _Allocator;
85 using __alloc_traits _LIBCPP_NODEBUG = allocator_traits<allocator_type>;
86 using size_type = typename __alloc_traits::size_type;
87 using difference_type = typename __alloc_traits::difference_type;
88 using __storage_type _LIBCPP_NODEBUG = size_type;
89 using pointer = __bit_iterator<vector, false>;
90 using const_pointer = __bit_iterator<vector, true>;
91 using iterator = pointer;
92 using const_iterator = const_pointer;
93 using reverse_iterator = std::reverse_iterator<iterator>;
94 using const_reverse_iterator = std::reverse_iterator<const_iterator>;
95
96private:
97 using __storage_allocator _LIBCPP_NODEBUG = __rebind_alloc<__alloc_traits, __storage_type>;
98 using __storage_traits _LIBCPP_NODEBUG = allocator_traits<__storage_allocator>;
99 using __storage_pointer _LIBCPP_NODEBUG = typename __storage_traits::pointer;
100 using __const_storage_pointer _LIBCPP_NODEBUG = typename __storage_traits::const_pointer;
101
102 __storage_pointer __begin_;
103 size_type __size_;
104 _LIBCPP_COMPRESSED_PAIR(size_type, __cap_, __storage_allocator, __alloc_);
105
106public:
107 using reference = __bit_reference<vector>;
108#ifdef _LIBCPP_ABI_BITSET_VECTOR_BOOL_CONST_SUBSCRIPT_RETURN_BOOL
109 using const_reference = bool;
110#else
111 using const_reference = __bit_const_reference<vector>;
112#endif
113
114private:
115 static const unsigned __bits_per_word = static_cast<unsigned>(sizeof(__storage_type) * CHAR_BIT);
116
117 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 static size_type
118 __internal_cap_to_external(size_type __n) _NOEXCEPT {
119 return __n * __bits_per_word;
120 }
121 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 static size_type
122 __external_cap_to_internal(size_type __n) _NOEXCEPT {
123 return __n > 0 ? (__n - 1) / __bits_per_word + 1 : size_type(0);
124 }
125
126public:
127 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 vector()
128 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value);
129
130 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 explicit vector(const allocator_type& __a)
131#if _LIBCPP_STD_VER <= 14
132 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value);
133#else
134 _NOEXCEPT;
135#endif
136
137private:
138 class __destroy_vector {
139 public:
140 _LIBCPP_CONSTEXPR _LIBCPP_HIDE_FROM_ABI __destroy_vector(vector& __vec) : __vec_(__vec) {}
141
142 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void operator()() {
143 if (__vec_.__begin_ != nullptr)
144 __storage_traits::deallocate(__vec_.__alloc_, __vec_.__begin_, __vec_.__cap_);
145 }
146
147 private:
148 vector& __vec_;
149 };
150
151public:
152 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 ~vector() { __destroy_vector (*this)(); }
153
154 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 explicit vector(size_type __n);
155 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 explicit vector(size_type __n, const allocator_type& __a);
156 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 vector(size_type __n, const value_type& __v);
157 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
158 vector(size_type __n, const value_type& __v, const allocator_type& __a);
159 template <class _InputIterator, __enable_if_t<__has_exactly_input_iterator_category<_InputIterator>::value, int> = 0>
160 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 vector(_InputIterator __first, _InputIterator __last);
161 template <class _InputIterator, __enable_if_t<__has_exactly_input_iterator_category<_InputIterator>::value, int> = 0>
162 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
163 vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a);
164 template <class _ForwardIterator, __enable_if_t<__has_forward_iterator_category<_ForwardIterator>::value, int> = 0>
165 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 vector(_ForwardIterator __first, _ForwardIterator __last);
166 template <class _ForwardIterator, __enable_if_t<__has_forward_iterator_category<_ForwardIterator>::value, int> = 0>
167 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
168 vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a);
169
170#if _LIBCPP_STD_VER >= 23
171 template <_ContainerCompatibleRange<bool> _Range>
172 _LIBCPP_HIDE_FROM_ABI constexpr vector(from_range_t, _Range&& __range, const allocator_type& __a = allocator_type())
173 : __begin_(nullptr), __size_(0), __cap_(0), __alloc_(static_cast<__storage_allocator>(__a)) {
174 if constexpr (ranges::forward_range<_Range> || ranges::sized_range<_Range>) {
175 auto __n = static_cast<size_type>(ranges::distance(__range));
176 __init_with_size(ranges::begin(__range), ranges::end(__range), __n);
177
178 } else {
179 __init_with_sentinel(ranges::begin(__range), ranges::end(__range));
180 }
181 }
182#endif
183
184 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 vector(const vector& __v);
185 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 vector(const vector& __v, const allocator_type& __a);
186 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 vector& operator=(const vector& __v);
187
188#ifndef _LIBCPP_CXX03_LANG
189 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 vector(initializer_list<value_type> __il);
190 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
191 vector(initializer_list<value_type> __il, const allocator_type& __a);
192
193 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 vector& operator=(initializer_list<value_type> __il) {
194 assign(__il.begin(), __il.end());
195 return *this;
196 }
197
198#endif // !_LIBCPP_CXX03_LANG
199
200 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 vector(vector&& __v)
201#if _LIBCPP_STD_VER >= 17
202 noexcept;
203#else
204 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value);
205#endif
206 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
207 vector(vector&& __v, const __type_identity_t<allocator_type>& __a);
208 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 vector& operator=(vector&& __v)
209 _NOEXCEPT_(__noexcept_move_assign_container<_Allocator, __alloc_traits>::value);
210
211 template <class _InputIterator, __enable_if_t<__has_exactly_input_iterator_category<_InputIterator>::value, int> = 0>
212 void _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 assign(_InputIterator __first, _InputIterator __last);
213 template <class _ForwardIterator, __enable_if_t<__has_forward_iterator_category<_ForwardIterator>::value, int> = 0>
214 void _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 assign(_ForwardIterator __first, _ForwardIterator __last);
215
216#if _LIBCPP_STD_VER >= 23
217 template <_ContainerCompatibleRange<bool> _Range>
218 _LIBCPP_HIDE_FROM_ABI constexpr void assign_range(_Range&& __range) {
219 if constexpr (ranges::forward_range<_Range> || ranges::sized_range<_Range>) {
220 auto __n = static_cast<size_type>(ranges::distance(__range));
221 __assign_with_size(ranges::begin(__range), ranges::end(__range), __n);
222
223 } else {
224 __assign_with_sentinel(ranges::begin(__range), ranges::end(__range));
225 }
226 }
227#endif
228
229 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void assign(size_type __n, const value_type& __x);
230
231#ifndef _LIBCPP_CXX03_LANG
232 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void assign(initializer_list<value_type> __il) {
233 assign(__il.begin(), __il.end());
234 }
235#endif
236
237 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 allocator_type get_allocator() const _NOEXCEPT {
238 return allocator_type(this->__alloc_);
239 }
240
241 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type max_size() const _NOEXCEPT;
242 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type capacity() const _NOEXCEPT {
243 return __internal_cap_to_external(n: __cap_);
244 }
245 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type size() const _NOEXCEPT {
246 return __size_;
247 }
248 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 bool empty() const _NOEXCEPT {
249 return __size_ == 0;
250 }
251 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void reserve(size_type __n);
252 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void shrink_to_fit() _NOEXCEPT;
253
254 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 iterator begin() _NOEXCEPT {
255 return __make_iter(0);
256 }
257 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 const_iterator begin() const _NOEXCEPT {
258 return __make_iter(0);
259 }
260 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 iterator end() _NOEXCEPT {
261 return __make_iter(__size_);
262 }
263 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 const_iterator end() const _NOEXCEPT {
264 return __make_iter(__size_);
265 }
266
267 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 reverse_iterator rbegin() _NOEXCEPT {
268 return reverse_iterator(end());
269 }
270 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 const_reverse_iterator
271 rbegin() const _NOEXCEPT {
272 return const_reverse_iterator(end());
273 }
274 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 reverse_iterator rend() _NOEXCEPT {
275 return reverse_iterator(begin());
276 }
277 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 const_reverse_iterator rend() const _NOEXCEPT {
278 return const_reverse_iterator(begin());
279 }
280
281 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 const_iterator cbegin() const _NOEXCEPT {
282 return __make_iter(0);
283 }
284 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 const_iterator cend() const _NOEXCEPT {
285 return __make_iter(__size_);
286 }
287 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 const_reverse_iterator
288 crbegin() const _NOEXCEPT {
289 return rbegin();
290 }
291 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 const_reverse_iterator crend() const _NOEXCEPT {
292 return rend();
293 }
294
295 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 reference operator[](size_type __n) {
296 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(__n < size(), "vector<bool>::operator[] index out of bounds");
297 return __make_ref(__n);
298 }
299 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 const_reference
300 operator[](size_type __n) const {
301 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(__n < size(), "vector<bool>::operator[] index out of bounds");
302 return __make_ref(__n);
303 }
304 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 reference at(size_type __n);
305 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 const_reference at(size_type __n) const;
306
307 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 reference front() {
308 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(!empty(), "vector<bool>::front() called on an empty vector");
309 return __make_ref(0);
310 }
311 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 const_reference front() const {
312 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(!empty(), "vector<bool>::front() called on an empty vector");
313 return __make_ref(0);
314 }
315 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 reference back() {
316 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(!empty(), "vector<bool>::back() called on an empty vector");
317 return __make_ref(__size_ - 1);
318 }
319 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 const_reference back() const {
320 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(!empty(), "vector<bool>::back() called on an empty vector");
321 return __make_ref(__size_ - 1);
322 }
323
324 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void push_back(const value_type& __x);
325#if _LIBCPP_STD_VER >= 14
326 template <class... _Args>
327# if _LIBCPP_STD_VER >= 17
328 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 reference emplace_back(_Args&&... __args)
329# else
330 _LIBCPP_HIDE_FROM_ABI void emplace_back(_Args&&... __args)
331# endif
332 {
333 push_back(x: value_type(std::forward<_Args>(__args)...));
334# if _LIBCPP_STD_VER >= 17
335 return this->back();
336# endif
337 }
338#endif
339
340#if _LIBCPP_STD_VER >= 23
341 template <_ContainerCompatibleRange<bool> _Range>
342 _LIBCPP_HIDE_FROM_ABI constexpr void append_range(_Range&& __range) {
343 insert_range(end(), std::forward<_Range>(__range));
344 }
345#endif
346
347 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void pop_back() {
348 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(!empty(), "vector<bool>::pop_back called on an empty vector");
349 --__size_;
350 }
351
352#if _LIBCPP_STD_VER >= 14
353 template <class... _Args>
354 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 iterator emplace(const_iterator __position, _Args&&... __args) {
355 return insert(__position, value_type(std::forward<_Args>(__args)...));
356 }
357#endif
358
359 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 iterator insert(const_iterator __position, const value_type& __x);
360 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 iterator
361 insert(const_iterator __position, size_type __n, const value_type& __x);
362 template <class _InputIterator, __enable_if_t<__has_exactly_input_iterator_category<_InputIterator>::value, int> = 0>
363 iterator _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
364 insert(const_iterator __position, _InputIterator __first, _InputIterator __last);
365 template <class _ForwardIterator, __enable_if_t<__has_forward_iterator_category<_ForwardIterator>::value, int> = 0>
366 iterator _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
367 insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last);
368
369#if _LIBCPP_STD_VER >= 23
370 template <_ContainerCompatibleRange<bool> _Range>
371 _LIBCPP_HIDE_FROM_ABI constexpr iterator insert_range(const_iterator __position, _Range&& __range) {
372 if constexpr (ranges::forward_range<_Range> || ranges::sized_range<_Range>) {
373 auto __n = static_cast<size_type>(ranges::distance(__range));
374 return __insert_with_size(__position, ranges::begin(__range), ranges::end(__range), __n);
375
376 } else {
377 return __insert_with_sentinel(__position, ranges::begin(__range), ranges::end(__range));
378 }
379 }
380#endif
381
382#ifndef _LIBCPP_CXX03_LANG
383 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 iterator
384 insert(const_iterator __position, initializer_list<value_type> __il) {
385 return insert(__position, __il.begin(), __il.end());
386 }
387#endif
388
389 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 iterator erase(const_iterator __position);
390 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 iterator erase(const_iterator __first, const_iterator __last);
391
392 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void clear() _NOEXCEPT { __size_ = 0; }
393
394 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void swap(vector&)
395#if _LIBCPP_STD_VER >= 14
396 _NOEXCEPT;
397#else
398 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value || __is_nothrow_swappable_v<allocator_type>);
399#endif
400 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 static void swap(reference __x, reference __y) _NOEXCEPT {
401 std::swap(__x, __y);
402 }
403
404 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void resize(size_type __sz, value_type __x = false);
405 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void flip() _NOEXCEPT;
406
407 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 bool __invariants() const;
408
409private:
410 [[__noreturn__]] _LIBCPP_HIDE_FROM_ABI static void __throw_length_error() { std::__throw_length_error(msg: "vector"); }
411
412 [[__noreturn__]] _LIBCPP_HIDE_FROM_ABI static void __throw_out_of_range() { std::__throw_out_of_range(msg: "vector"); }
413
414 template <class _InputIterator, class _Sentinel>
415 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void
416 __init_with_size(_InputIterator __first, _Sentinel __last, size_type __n) {
417 auto __guard = std::__make_exception_guard(__destroy_vector(*this));
418
419 if (__n > 0) {
420 __vallocate(__n);
421 __construct_at_end(std::move(__first), std::move(__last), __n);
422 }
423
424 __guard.__complete();
425 }
426
427 template <class _InputIterator, class _Sentinel>
428 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void
429 __init_with_sentinel(_InputIterator __first, _Sentinel __last) {
430 auto __guard = std::__make_exception_guard(__destroy_vector(*this));
431
432 for (; __first != __last; ++__first)
433 push_back(x: *__first);
434
435 __guard.__complete();
436 }
437
438 template <class _Iterator, class _Sentinel>
439 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __assign_with_sentinel(_Iterator __first, _Sentinel __last);
440
441 // The `_Iterator` in `*_with_size` functions can be input-only only if called from `*_range` (since C++23).
442 // Otherwise, `_Iterator` is a forward iterator.
443
444 template <class _Iterator, class _Sentinel>
445 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void
446 __assign_with_size(_Iterator __first, _Sentinel __last, difference_type __ns);
447
448 template <class _InputIterator, class _Sentinel>
449 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI iterator
450 __insert_with_sentinel(const_iterator __position, _InputIterator __first, _Sentinel __last);
451
452 template <class _Iterator, class _Sentinel>
453 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI iterator
454 __insert_with_size(const_iterator __position, _Iterator __first, _Sentinel __last, difference_type __n);
455
456 // Allocate space for __n objects
457 // throws length_error if __n > max_size()
458 // throws (probably bad_alloc) if memory run out
459 // Precondition: __begin_ == __end_ == __cap_ == nullptr
460 // Precondition: __n > 0
461 // Postcondition: capacity() >= __n
462 // Postcondition: size() == 0
463 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __vallocate(size_type __n) {
464 if (__n > max_size())
465 this->__throw_length_error();
466 auto __allocation = std::__allocate_at_least(__alloc_, __external_cap_to_internal(__n));
467 __begin_ = __allocation.ptr;
468 __size_ = 0;
469 __cap_ = __allocation.count;
470 if (__libcpp_is_constant_evaluated()) {
471 for (size_type __i = 0; __i != __cap_; ++__i)
472 std::__construct_at(std::__to_address(__begin_) + __i);
473 }
474 }
475
476 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __vdeallocate() _NOEXCEPT;
477 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 static size_type __align_it(size_type __new_size) _NOEXCEPT {
478 return (__new_size + (__bits_per_word - 1)) & ~((size_type)__bits_per_word - 1);
479 }
480 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type __recommend(size_type __new_size) const;
481 template <class _InputIterator, class _Sentinel>
482 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void
483 __construct_at_end(_InputIterator __first, _Sentinel __last, size_type __n);
484 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 reference __make_ref(size_type __pos) _NOEXCEPT {
485 return reference(__begin_ + __pos / __bits_per_word, __storage_type(1) << __pos % __bits_per_word);
486 }
487 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 const_reference __make_ref(size_type __pos) const _NOEXCEPT {
488 return __bit_const_reference<vector>(
489 __begin_ + __pos / __bits_per_word, __storage_type(1) << __pos % __bits_per_word);
490 }
491 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 iterator __make_iter(size_type __pos) _NOEXCEPT {
492 return iterator(__begin_ + __pos / __bits_per_word, static_cast<unsigned>(__pos % __bits_per_word));
493 }
494 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 const_iterator __make_iter(size_type __pos) const _NOEXCEPT {
495 return const_iterator(__begin_ + __pos / __bits_per_word, static_cast<unsigned>(__pos % __bits_per_word));
496 }
497 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 iterator __const_iterator_cast(const_iterator __p) _NOEXCEPT {
498 return begin() + (__p - cbegin());
499 }
500
501 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __copy_assign_alloc(const vector& __v) {
502 __copy_assign_alloc(
503 __v, integral_constant<bool, __storage_traits::propagate_on_container_copy_assignment::value>());
504 }
505 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __copy_assign_alloc(const vector& __c, true_type) {
506 if (__alloc_ != __c.__alloc_)
507 __vdeallocate();
508 __alloc_ = __c.__alloc_;
509 }
510
511 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __copy_assign_alloc(const vector&, false_type) {}
512
513 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __move_assign(vector& __c, false_type);
514 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __move_assign(vector& __c, true_type)
515 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value);
516 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __move_assign_alloc(vector& __c)
517 _NOEXCEPT_(!__storage_traits::propagate_on_container_move_assignment::value ||
518 is_nothrow_move_assignable<allocator_type>::value) {
519 __move_assign_alloc(
520 __c, integral_constant<bool, __storage_traits::propagate_on_container_move_assignment::value>());
521 }
522 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __move_assign_alloc(vector& __c, true_type)
523 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value) {
524 __alloc_ = std::move(__c.__alloc_);
525 }
526
527 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __move_assign_alloc(vector&, false_type) _NOEXCEPT {}
528
529 _LIBCPP_HIDE_FROM_ABI size_t __hash_code() const _NOEXCEPT;
530
531 friend class __bit_reference<vector>;
532 friend class __bit_const_reference<vector>;
533 friend class __bit_iterator<vector, false>;
534 friend class __bit_iterator<vector, true>;
535 friend struct __bit_array<vector>;
536 friend struct hash<vector>;
537};
538
539template <class _Allocator>
540_LIBCPP_CONSTEXPR_SINCE_CXX20 void vector<bool, _Allocator>::__vdeallocate() _NOEXCEPT {
541 if (this->__begin_ != nullptr) {
542 __storage_traits::deallocate(this->__alloc_, this->__begin_, __cap_);
543 this->__begin_ = nullptr;
544 this->__size_ = this->__cap_ = 0;
545 }
546}
547
548template <class _Allocator>
549_LIBCPP_CONSTEXPR_SINCE_CXX20 typename vector<bool, _Allocator>::size_type
550vector<bool, _Allocator>::max_size() const _NOEXCEPT {
551 size_type __amax = __storage_traits::max_size(__alloc_);
552 size_type __nmax = numeric_limits<difference_type>::max();
553 return __nmax / __bits_per_word <= __amax ? __nmax : __internal_cap_to_external(n: __amax);
554}
555
556// Precondition: __new_size > capacity()
557template <class _Allocator>
558inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 typename vector<bool, _Allocator>::size_type
559vector<bool, _Allocator>::__recommend(size_type __new_size) const {
560 const size_type __ms = max_size();
561 if (__new_size > __ms)
562 this->__throw_length_error();
563 const size_type __cap = capacity();
564 if (__cap >= __ms / 2)
565 return __ms;
566 return std::max<size_type>(2 * __cap, __align_it(__new_size));
567}
568
569template <class _Allocator>
570template <class _InputIterator, class _Sentinel>
571_LIBCPP_CONSTEXPR_SINCE_CXX20 void
572vector<bool, _Allocator>::__construct_at_end(_InputIterator __first, _Sentinel __last, size_type __n) {
573 _LIBCPP_ASSERT_INTERNAL(
574 capacity() >= size() + __n, "vector<bool>::__construct_at_end called with insufficient capacity");
575 std::__copy(std::move(__first), std::move(__last), end());
576 this->__size_ += __n;
577 if (end().__ctz_ != 0) // Ensure uninitialized leading bits in the last word are set to zero
578 std::fill_n(end(), __bits_per_word - end().__ctz_, 0);
579}
580
581template <class _Allocator>
582inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 vector<bool, _Allocator>::vector()
583 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
584 : __begin_(nullptr), __size_(0), __cap_(0) {}
585
586template <class _Allocator>
587inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 vector<bool, _Allocator>::vector(const allocator_type& __a)
588#if _LIBCPP_STD_VER <= 14
589 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value)
590#else
591 _NOEXCEPT
592#endif
593 : __begin_(nullptr), __size_(0), __cap_(0), __alloc_(static_cast<__storage_allocator>(__a)) {
594}
595
596template <class _Allocator>
597_LIBCPP_CONSTEXPR_SINCE_CXX20 vector<bool, _Allocator>::vector(size_type __n)
598 : __begin_(nullptr), __size_(0), __cap_(0) {
599 if (__n > 0) {
600 __vallocate(__n);
601 std::fill_n(__begin_, __external_cap_to_internal(__n), __storage_type(0));
602 __size_ = __n;
603 }
604}
605
606template <class _Allocator>
607_LIBCPP_CONSTEXPR_SINCE_CXX20 vector<bool, _Allocator>::vector(size_type __n, const allocator_type& __a)
608 : __begin_(nullptr), __size_(0), __cap_(0), __alloc_(static_cast<__storage_allocator>(__a)) {
609 if (__n > 0) {
610 __vallocate(__n);
611 std::fill_n(__begin_, __external_cap_to_internal(__n), __storage_type(0));
612 __size_ = __n;
613 }
614}
615
616template <class _Allocator>
617_LIBCPP_CONSTEXPR_SINCE_CXX20 vector<bool, _Allocator>::vector(size_type __n, const value_type& __x)
618 : __begin_(nullptr), __size_(0), __cap_(0) {
619 if (__n > 0) {
620 __vallocate(__n);
621 std::fill_n(__begin_, __external_cap_to_internal(__n), __storage_type(0) - __x);
622 __size_ = __n;
623 }
624}
625
626template <class _Allocator>
627_LIBCPP_CONSTEXPR_SINCE_CXX20
628vector<bool, _Allocator>::vector(size_type __n, const value_type& __x, const allocator_type& __a)
629 : __begin_(nullptr), __size_(0), __cap_(0), __alloc_(static_cast<__storage_allocator>(__a)) {
630 if (__n > 0) {
631 __vallocate(__n);
632 std::fill_n(__begin_, __external_cap_to_internal(__n), __storage_type(0) - __x);
633 __size_ = __n;
634 }
635}
636
637template <class _Allocator>
638template <class _InputIterator, __enable_if_t<__has_exactly_input_iterator_category<_InputIterator>::value, int> >
639_LIBCPP_CONSTEXPR_SINCE_CXX20 vector<bool, _Allocator>::vector(_InputIterator __first, _InputIterator __last)
640 : __begin_(nullptr), __size_(0), __cap_(0) {
641 __init_with_sentinel(__first, __last);
642}
643
644template <class _Allocator>
645template <class _InputIterator, __enable_if_t<__has_exactly_input_iterator_category<_InputIterator>::value, int> >
646_LIBCPP_CONSTEXPR_SINCE_CXX20
647vector<bool, _Allocator>::vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a)
648 : __begin_(nullptr), __size_(0), __cap_(0), __alloc_(static_cast<__storage_allocator>(__a)) {
649 __init_with_sentinel(__first, __last);
650}
651
652template <class _Allocator>
653template <class _ForwardIterator, __enable_if_t<__has_forward_iterator_category<_ForwardIterator>::value, int> >
654_LIBCPP_CONSTEXPR_SINCE_CXX20 vector<bool, _Allocator>::vector(_ForwardIterator __first, _ForwardIterator __last)
655 : __begin_(nullptr), __size_(0), __cap_(0) {
656 auto __n = static_cast<size_type>(std::distance(__first, __last));
657 __init_with_size(__first, __last, __n);
658}
659
660template <class _Allocator>
661template <class _ForwardIterator, __enable_if_t<__has_forward_iterator_category<_ForwardIterator>::value, int> >
662_LIBCPP_CONSTEXPR_SINCE_CXX20
663vector<bool, _Allocator>::vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a)
664 : __begin_(nullptr), __size_(0), __cap_(0), __alloc_(static_cast<__storage_allocator>(__a)) {
665 auto __n = static_cast<size_type>(std::distance(__first, __last));
666 __init_with_size(__first, __last, __n);
667}
668
669#ifndef _LIBCPP_CXX03_LANG
670
671template <class _Allocator>
672_LIBCPP_CONSTEXPR_SINCE_CXX20 vector<bool, _Allocator>::vector(initializer_list<value_type> __il)
673 : __begin_(nullptr), __size_(0), __cap_(0) {
674 size_type __n = static_cast<size_type>(__il.size());
675 if (__n > 0) {
676 __vallocate(__n);
677 __construct_at_end(__il.begin(), __il.end(), __n);
678 }
679}
680
681template <class _Allocator>
682_LIBCPP_CONSTEXPR_SINCE_CXX20
683vector<bool, _Allocator>::vector(initializer_list<value_type> __il, const allocator_type& __a)
684 : __begin_(nullptr), __size_(0), __cap_(0), __alloc_(static_cast<__storage_allocator>(__a)) {
685 size_type __n = static_cast<size_type>(__il.size());
686 if (__n > 0) {
687 __vallocate(__n);
688 __construct_at_end(__il.begin(), __il.end(), __n);
689 }
690}
691
692#endif // _LIBCPP_CXX03_LANG
693
694template <class _Allocator>
695_LIBCPP_CONSTEXPR_SINCE_CXX20 vector<bool, _Allocator>::vector(const vector& __v)
696 : __begin_(nullptr),
697 __size_(0),
698 __cap_(0),
699 __alloc_(__storage_traits::select_on_container_copy_construction(__v.__alloc_)) {
700 if (__v.size() > 0) {
701 __vallocate(n: __v.size());
702 std::copy_n(__v.__begin_, __external_cap_to_internal(n: __v.size()), __begin_);
703 __size_ = __v.size();
704 }
705}
706
707template <class _Allocator>
708_LIBCPP_CONSTEXPR_SINCE_CXX20 vector<bool, _Allocator>::vector(const vector& __v, const allocator_type& __a)
709 : __begin_(nullptr), __size_(0), __cap_(0), __alloc_(__a) {
710 if (__v.size() > 0) {
711 __vallocate(n: __v.size());
712 std::copy_n(__v.__begin_, __external_cap_to_internal(n: __v.size()), __begin_);
713 __size_ = __v.size();
714 }
715}
716
717template <class _Allocator>
718_LIBCPP_CONSTEXPR_SINCE_CXX20 vector<bool, _Allocator>& vector<bool, _Allocator>::operator=(const vector& __v) {
719 if (this != std::addressof(__v)) {
720 __copy_assign_alloc(__v);
721 if (__v.__size_) {
722 if (__v.__size_ > capacity()) {
723 __vdeallocate();
724 __vallocate(n: __v.__size_);
725 }
726 std::copy_n(__v.__begin_, __external_cap_to_internal(n: __v.size()), __begin_);
727 }
728 __size_ = __v.__size_;
729 }
730 return *this;
731}
732
733template <class _Allocator>
734inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 vector<bool, _Allocator>::vector(vector&& __v)
735#if _LIBCPP_STD_VER >= 17
736 _NOEXCEPT
737#else
738 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
739#endif
740 : __begin_(std::__exchange(__v.__begin_, nullptr)),
741 __size_(std::__exchange(__v.__size_, 0)),
742 __cap_(std::__exchange(__v.__cap_, 0)),
743 __alloc_(std::move(__v.__alloc_)) {
744}
745
746template <class _Allocator>
747_LIBCPP_CONSTEXPR_SINCE_CXX20
748vector<bool, _Allocator>::vector(vector&& __v, const __type_identity_t<allocator_type>& __a)
749 : __begin_(nullptr), __size_(0), __cap_(0), __alloc_(__a) {
750 if (__a == allocator_type(__v.__alloc_)) {
751 __begin_ = std::__exchange(__v.__begin_, nullptr);
752 __size_ = std::__exchange(__v.__size_, 0);
753 __cap_ = std::__exchange(__v.__cap_, 0);
754 } else if (__v.size() > 0) {
755 __vallocate(n: __v.size());
756 __size_ = __v.__size_;
757 std::copy_n(__v.__begin_, __external_cap_to_internal(n: __v.size()), __begin_);
758 }
759}
760
761template <class _Allocator>
762inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 vector<bool, _Allocator>&
763vector<bool, _Allocator>::operator=(vector&& __v)
764 _NOEXCEPT_(__noexcept_move_assign_container<_Allocator, __alloc_traits>::value) {
765 __move_assign(__v, integral_constant<bool, __storage_traits::propagate_on_container_move_assignment::value>());
766 return *this;
767}
768
769template <class _Allocator>
770_LIBCPP_CONSTEXPR_SINCE_CXX20 void vector<bool, _Allocator>::__move_assign(vector& __c, false_type) {
771 if (__alloc_ != __c.__alloc_)
772 assign(__c.begin(), __c.end());
773 else
774 __move_assign(__c, true_type());
775}
776
777template <class _Allocator>
778_LIBCPP_CONSTEXPR_SINCE_CXX20 void vector<bool, _Allocator>::__move_assign(vector& __c, true_type)
779 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value) {
780 __vdeallocate();
781 __move_assign_alloc(__c);
782 __begin_ = std::__exchange(__c.__begin_, nullptr);
783 __size_ = std::__exchange(__c.__size_, 0);
784 __cap_ = std::__exchange(__c.__cap_, 0);
785}
786
787template <class _Allocator>
788_LIBCPP_CONSTEXPR_SINCE_CXX20 void vector<bool, _Allocator>::assign(size_type __n, const value_type& __x) {
789 __size_ = 0;
790 if (__n > 0) {
791 size_type __c = capacity();
792 if (__n <= __c)
793 __size_ = __n;
794 else {
795 vector __v(get_allocator());
796 __v.reserve(n: __recommend(new_size: __n));
797 __v.__size_ = __n;
798 swap(__v);
799 }
800 std::fill_n(begin(), __n, __x);
801 }
802}
803
804template <class _Allocator>
805template <class _InputIterator, __enable_if_t<__has_exactly_input_iterator_category<_InputIterator>::value, int> >
806_LIBCPP_CONSTEXPR_SINCE_CXX20 void vector<bool, _Allocator>::assign(_InputIterator __first, _InputIterator __last) {
807 __assign_with_sentinel(__first, __last);
808}
809
810template <class _Allocator>
811template <class _Iterator, class _Sentinel>
812_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void
813vector<bool, _Allocator>::__assign_with_sentinel(_Iterator __first, _Sentinel __last) {
814 clear();
815 for (; __first != __last; ++__first)
816 push_back(x: *__first);
817}
818
819template <class _Allocator>
820template <class _ForwardIterator, __enable_if_t<__has_forward_iterator_category<_ForwardIterator>::value, int> >
821_LIBCPP_CONSTEXPR_SINCE_CXX20 void vector<bool, _Allocator>::assign(_ForwardIterator __first, _ForwardIterator __last) {
822 __assign_with_size(__first, __last, std::distance(__first, __last));
823}
824
825template <class _Allocator>
826template <class _Iterator, class _Sentinel>
827_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void
828vector<bool, _Allocator>::__assign_with_size(_Iterator __first, _Sentinel __last, difference_type __ns) {
829 _LIBCPP_ASSERT_VALID_INPUT_RANGE(__ns >= 0, "invalid range specified");
830
831 clear();
832
833 const size_t __n = static_cast<size_type>(__ns);
834 if (__n) {
835 if (__n > capacity()) {
836 __vdeallocate();
837 __vallocate(__n);
838 }
839 __construct_at_end(std::move(__first), std::move(__last), __n);
840 }
841}
842
843template <class _Allocator>
844_LIBCPP_CONSTEXPR_SINCE_CXX20 void vector<bool, _Allocator>::reserve(size_type __n) {
845 if (__n > capacity()) {
846 if (__n > max_size())
847 this->__throw_length_error();
848 vector __v(this->get_allocator());
849 __v.__vallocate(__n);
850 __v.__size_ = __size_;
851 std::copy_n(__begin_, __external_cap_to_internal(n: __size_), __v.__begin_);
852 swap(__v);
853 }
854}
855
856template <class _Allocator>
857_LIBCPP_CONSTEXPR_SINCE_CXX20 void vector<bool, _Allocator>::shrink_to_fit() _NOEXCEPT {
858 if (__external_cap_to_internal(n: size()) < __cap_) {
859#if _LIBCPP_HAS_EXCEPTIONS
860 try {
861#endif // _LIBCPP_HAS_EXCEPTIONS
862 vector __v(*this, allocator_type(__alloc_));
863 if (__v.__cap_ < __cap_)
864 __v.swap(*this);
865#if _LIBCPP_HAS_EXCEPTIONS
866 } catch (...) {
867 }
868#endif // _LIBCPP_HAS_EXCEPTIONS
869 }
870}
871
872template <class _Allocator>
873_LIBCPP_CONSTEXPR_SINCE_CXX20 typename vector<bool, _Allocator>::reference vector<bool, _Allocator>::at(size_type __n) {
874 if (__n >= size())
875 this->__throw_out_of_range();
876 return (*this)[__n];
877}
878
879template <class _Allocator>
880_LIBCPP_CONSTEXPR_SINCE_CXX20 typename vector<bool, _Allocator>::const_reference
881vector<bool, _Allocator>::at(size_type __n) const {
882 if (__n >= size())
883 this->__throw_out_of_range();
884 return (*this)[__n];
885}
886
887template <class _Allocator>
888_LIBCPP_CONSTEXPR_SINCE_CXX20 void vector<bool, _Allocator>::push_back(const value_type& __x) {
889 if (this->__size_ == this->capacity())
890 reserve(n: __recommend(new_size: this->__size_ + 1));
891 ++this->__size_;
892 back() = __x;
893}
894
895template <class _Allocator>
896_LIBCPP_CONSTEXPR_SINCE_CXX20 typename vector<bool, _Allocator>::iterator
897vector<bool, _Allocator>::insert(const_iterator __position, const value_type& __x) {
898 iterator __r;
899 if (size() < capacity()) {
900 const_iterator __old_end = end();
901 ++__size_;
902 std::copy_backward(__position, __old_end, end());
903 __r = __const_iterator_cast(p: __position);
904 } else {
905 vector __v(get_allocator());
906 __v.reserve(n: __recommend(new_size: __size_ + 1));
907 __v.__size_ = __size_ + 1;
908 __r = std::copy(cbegin(), __position, __v.begin());
909 std::copy_backward(__position, cend(), __v.end());
910 swap(__v);
911 }
912 *__r = __x;
913 return __r;
914}
915
916template <class _Allocator>
917_LIBCPP_CONSTEXPR_SINCE_CXX20 typename vector<bool, _Allocator>::iterator
918vector<bool, _Allocator>::insert(const_iterator __position, size_type __n, const value_type& __x) {
919 iterator __r;
920 size_type __c = capacity();
921 if (__n <= __c && size() <= __c - __n) {
922 const_iterator __old_end = end();
923 __size_ += __n;
924 std::copy_backward(__position, __old_end, end());
925 __r = __const_iterator_cast(p: __position);
926 } else {
927 vector __v(get_allocator());
928 __v.reserve(n: __recommend(new_size: __size_ + __n));
929 __v.__size_ = __size_ + __n;
930 __r = std::copy(cbegin(), __position, __v.begin());
931 std::copy_backward(__position, cend(), __v.end());
932 swap(__v);
933 }
934 std::fill_n(__r, __n, __x);
935 return __r;
936}
937
938template <class _Allocator>
939template <class _InputIterator, __enable_if_t<__has_exactly_input_iterator_category<_InputIterator>::value, int> >
940_LIBCPP_CONSTEXPR_SINCE_CXX20 typename vector<bool, _Allocator>::iterator
941vector<bool, _Allocator>::insert(const_iterator __position, _InputIterator __first, _InputIterator __last) {
942 return __insert_with_sentinel(__position, __first, __last);
943}
944
945template <class _Allocator>
946template <class _InputIterator, class _Sentinel>
947_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI typename vector<bool, _Allocator>::iterator
948vector<bool, _Allocator>::__insert_with_sentinel(const_iterator __position, _InputIterator __first, _Sentinel __last) {
949 difference_type __off = __position - begin();
950 iterator __p = __const_iterator_cast(p: __position);
951 iterator __old_end = end();
952 for (; size() != capacity() && __first != __last; ++__first) {
953 ++this->__size_;
954 back() = *__first;
955 }
956 vector __v(get_allocator());
957 if (__first != __last) {
958 auto __guard = std::__make_exception_guard([&] { erase(__old_end, end()); });
959 __v.__assign_with_sentinel(std::move(__first), std::move(__last));
960 difference_type __old_size = static_cast<difference_type>(__old_end - begin());
961 difference_type __old_p = __p - begin();
962 reserve(n: __recommend(new_size: size() + __v.size()));
963 __p = begin() + __old_p;
964 __old_end = begin() + __old_size;
965 __guard.__complete();
966 }
967 __p = std::rotate(__p, __old_end, end());
968 insert(__p, __v.begin(), __v.end());
969 return begin() + __off;
970}
971
972template <class _Allocator>
973template <class _ForwardIterator, __enable_if_t<__has_forward_iterator_category<_ForwardIterator>::value, int> >
974_LIBCPP_CONSTEXPR_SINCE_CXX20 typename vector<bool, _Allocator>::iterator
975vector<bool, _Allocator>::insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last) {
976 return __insert_with_size(__position, __first, __last, std::distance(__first, __last));
977}
978
979template <class _Allocator>
980template <class _Iterator, class _Sentinel>
981_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI typename vector<bool, _Allocator>::iterator
982vector<bool, _Allocator>::__insert_with_size(
983 const_iterator __position, _Iterator __first, _Sentinel __last, difference_type __n_signed) {
984 _LIBCPP_ASSERT_VALID_INPUT_RANGE(__n_signed >= 0, "invalid range specified");
985 const size_type __n = static_cast<size_type>(__n_signed);
986 iterator __r;
987 size_type __c = capacity();
988 if (__n <= __c && size() <= __c - __n) {
989 const_iterator __old_end = end();
990 __size_ += __n;
991 std::copy_backward(__position, __old_end, end());
992 __r = __const_iterator_cast(p: __position);
993 } else {
994 vector __v(get_allocator());
995 __v.reserve(n: __recommend(new_size: __size_ + __n));
996 __v.__size_ = __size_ + __n;
997 __r = std::copy(cbegin(), __position, __v.begin());
998 std::copy_backward(__position, cend(), __v.end());
999 swap(__v);
1000 }
1001 std::__copy(std::move(__first), std::move(__last), __r);
1002 return __r;
1003}
1004
1005template <class _Allocator>
1006inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 typename vector<bool, _Allocator>::iterator
1007vector<bool, _Allocator>::erase(const_iterator __position) {
1008 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
1009 __position != end(), "vector<bool>::erase(iterator) called with a non-dereferenceable iterator");
1010 iterator __r = __const_iterator_cast(p: __position);
1011 std::copy(__position + 1, this->cend(), __r);
1012 --__size_;
1013 return __r;
1014}
1015
1016template <class _Allocator>
1017_LIBCPP_CONSTEXPR_SINCE_CXX20 typename vector<bool, _Allocator>::iterator
1018vector<bool, _Allocator>::erase(const_iterator __first, const_iterator __last) {
1019 _LIBCPP_ASSERT_VALID_INPUT_RANGE(
1020 __first <= __last, "vector<bool>::erase(iterator, iterator) called with an invalid range");
1021 iterator __r = __const_iterator_cast(p: __first);
1022 difference_type __d = __last - __first;
1023 std::copy(__last, this->cend(), __r);
1024 __size_ -= __d;
1025 return __r;
1026}
1027
1028template <class _Allocator>
1029_LIBCPP_CONSTEXPR_SINCE_CXX20 void vector<bool, _Allocator>::swap(vector& __x)
1030#if _LIBCPP_STD_VER >= 14
1031 _NOEXCEPT
1032#else
1033 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value || __is_nothrow_swappable_v<allocator_type>)
1034#endif
1035{
1036 std::swap(this->__begin_, __x.__begin_);
1037 std::swap(this->__size_, __x.__size_);
1038 std::swap(this->__cap_, __x.__cap_);
1039 std::__swap_allocator(this->__alloc_, __x.__alloc_);
1040}
1041
1042template <class _Allocator>
1043_LIBCPP_CONSTEXPR_SINCE_CXX20 void vector<bool, _Allocator>::resize(size_type __new_size, value_type __x) {
1044 size_type __current_size = size();
1045 if (__new_size < __current_size) {
1046 __size_ = __new_size;
1047 return;
1048 }
1049
1050 reserve(n: __new_size);
1051 std::fill_n(end(), __new_size - __current_size, __x);
1052 __size_ = __new_size;
1053}
1054
1055template <class _Allocator>
1056_LIBCPP_CONSTEXPR_SINCE_CXX20 void vector<bool, _Allocator>::flip() _NOEXCEPT {
1057 // Flip each storage word entirely, including the last potentially partial word.
1058 // The unused bits in the last word are safe to flip as they won't be accessed.
1059 __storage_pointer __p = __begin_;
1060 for (size_type __n = __external_cap_to_internal(n: size()); __n != 0; ++__p, --__n)
1061 *__p = ~*__p;
1062}
1063
1064template <class _Allocator>
1065_LIBCPP_CONSTEXPR_SINCE_CXX20 bool vector<bool, _Allocator>::__invariants() const {
1066 if (this->__begin_ == nullptr) {
1067 if (this->__size_ != 0 || this->__cap_ != 0)
1068 return false;
1069 } else {
1070 if (this->__cap_ == 0)
1071 return false;
1072 if (this->__size_ > this->capacity())
1073 return false;
1074 }
1075 return true;
1076}
1077
1078template <class _Allocator>
1079size_t vector<bool, _Allocator>::__hash_code() const _NOEXCEPT {
1080 size_t __h = 0;
1081 // do middle whole words
1082 size_type __n = __size_;
1083 __storage_pointer __p = __begin_;
1084 for (; __n >= __bits_per_word; ++__p, __n -= __bits_per_word)
1085 __h ^= *__p;
1086 // do last partial word
1087 if (__n > 0) {
1088 const __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
1089 __h ^= *__p & __m;
1090 }
1091 return __h;
1092}
1093
1094template <class _Allocator>
1095struct hash<vector<bool, _Allocator> > : public __unary_function<vector<bool, _Allocator>, size_t> {
1096 _LIBCPP_HIDE_FROM_ABI size_t operator()(const vector<bool, _Allocator>& __vec) const _NOEXCEPT {
1097 return __vec.__hash_code();
1098 }
1099};
1100
1101_LIBCPP_END_NAMESPACE_STD
1102
1103_LIBCPP_POP_MACROS
1104
1105#endif // _LIBCPP___VECTOR_VECTOR_BOOL_H
1106