libstdc++
stl_bvector.h
Go to the documentation of this file.
1// vector<bool> specialization -*- C++ -*-
2
3// Copyright (C) 2001-2026 Free Software Foundation, Inc.
4//
5// This file is part of the GNU ISO C++ Library. This library is free
6// software; you can redistribute it and/or modify it under the
7// terms of the GNU General Public License as published by the
8// Free Software Foundation; either version 3, or (at your option)
9// any later version.
10
11// This library is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
15
16// Under Section 7 of GPL version 3, you are granted additional
17// permissions described in the GCC Runtime Library Exception, version
18// 3.1, as published by the Free Software Foundation.
19
20// You should have received a copy of the GNU General Public License and
21// a copy of the GCC Runtime Library Exception along with this program;
22// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23// <http://www.gnu.org/licenses/>.
24
25/*
26 *
27 * Copyright (c) 1994
28 * Hewlett-Packard Company
29 *
30 * Permission to use, copy, modify, distribute and sell this software
31 * and its documentation for any purpose is hereby granted without fee,
32 * provided that the above copyright notice appear in all copies and
33 * that both that copyright notice and this permission notice appear
34 * in supporting documentation. Hewlett-Packard Company makes no
35 * representations about the suitability of this software for any
36 * purpose. It is provided "as is" without express or implied warranty.
37 *
38 *
39 * Copyright (c) 1996-1999
40 * Silicon Graphics Computer Systems, Inc.
41 *
42 * Permission to use, copy, modify, distribute and sell this software
43 * and its documentation for any purpose is hereby granted without fee,
44 * provided that the above copyright notice appear in all copies and
45 * that both that copyright notice and this permission notice appear
46 * in supporting documentation. Silicon Graphics makes no
47 * representations about the suitability of this software for any
48 * purpose. It is provided "as is" without express or implied warranty.
49 */
50
51/** @file bits/stl_bvector.h
52 * This is an internal header file, included by other library headers.
53 * Do not attempt to use it directly. @headername{vector}
54 */
55
56#ifndef _STL_BVECTOR_H
57#define _STL_BVECTOR_H 1
58
59#ifndef _GLIBCXX_ALWAYS_INLINE
60#define _GLIBCXX_ALWAYS_INLINE inline __attribute__((__always_inline__))
61#endif
62
63#if __cplusplus >= 201103L
64#include <initializer_list>
66#endif
67
68namespace std _GLIBCXX_VISIBILITY(default)
69{
70_GLIBCXX_BEGIN_NAMESPACE_VERSION
71
72 typedef unsigned long _Bit_type;
73 enum { _S_word_bit = int(__CHAR_BIT__ * sizeof(_Bit_type)) };
74
75 __attribute__((__nonnull__))
76 _GLIBCXX20_CONSTEXPR
77 void
78 __fill_bvector_n(_Bit_type*, size_t, bool) _GLIBCXX_NOEXCEPT;
79
80_GLIBCXX_BEGIN_NAMESPACE_CONTAINER
81
82 struct _Bit_reference
83 {
84 private:
85 template<typename, typename> friend class vector;
86 friend struct _Bit_iterator;
87 friend struct _Bit_const_iterator;
88
89 _GLIBCXX20_CONSTEXPR
90 _Bit_reference() _GLIBCXX_NOEXCEPT : _M_p(0), _M_mask(0) { }
91
92 _Bit_type * _M_p;
93 _Bit_type _M_mask;
94
95 _GLIBCXX20_CONSTEXPR
96 _Bit_reference(_Bit_type * __x, _Bit_type __y)
97 : _M_p(__x), _M_mask(__y) { }
98
99 public:
100#if __cplusplus >= 201103L
101 _Bit_reference(const _Bit_reference&) = default;
102#endif
103
104 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
105 operator bool() const _GLIBCXX_NOEXCEPT
106 { return !!(*_M_p & _M_mask); }
107
108 _GLIBCXX20_CONSTEXPR
109 _Bit_reference&
110 operator=(bool __x) _GLIBCXX_NOEXCEPT
111 {
112 if (__x)
113 *_M_p |= _M_mask;
114 else
115 *_M_p &= ~_M_mask;
116 return *this;
117 }
118
119#if __glibcxx_ranges_zip // >= C++23
120 constexpr const _Bit_reference&
121 operator=(bool __x) const noexcept
122 {
123 if (__x)
124 *_M_p |= _M_mask;
125 else
126 *_M_p &= ~_M_mask;
127 return *this;
128 }
129#endif // C++23
130
131 _GLIBCXX20_CONSTEXPR
132 _Bit_reference&
133 operator=(const _Bit_reference& __x) _GLIBCXX_NOEXCEPT
134 { return *this = bool(__x); }
135
136 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
137 bool
138 operator==(const _Bit_reference& __x) const
139 { return bool(*this) == bool(__x); }
140
141 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
142 bool
143 operator<(const _Bit_reference& __x) const
144 { return !bool(*this) && bool(__x); }
145
146 _GLIBCXX20_CONSTEXPR
147 void
148 flip() _GLIBCXX_NOEXCEPT
149 { *_M_p ^= _M_mask; }
150
151#if __cplusplus >= 201103L
152 // _GLIBCXX_RESOLVE_LIB_DEFECTS
153 // 3638. vector<bool>::swap(reference, reference) is useless
154
155 _GLIBCXX20_CONSTEXPR
156 friend void
157 swap(_Bit_reference __x, _Bit_reference __y) noexcept
158 {
159 bool __tmp = __x;
160 __x = __y;
161 __y = __tmp;
162 }
163
164 _GLIBCXX20_CONSTEXPR
165 friend void
166 swap(_Bit_reference __x, bool& __y) noexcept
167 {
168 bool __tmp = __x;
169 __x = __y;
170 __y = __tmp;
171 }
172
173 _GLIBCXX20_CONSTEXPR
174 friend void
175 swap(bool& __x, _Bit_reference __y) noexcept
176 {
177 bool __tmp = __x;
178 __x = __y;
179 __y = __tmp;
180 }
181#endif
182 };
183
184// Ignore warnings about std::iterator.
185#pragma GCC diagnostic push
186#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
187 struct _Bit_iterator_base
188 : public std::iterator<std::random_access_iterator_tag, bool>
189 {
190 _Bit_type * _M_p;
191 unsigned int _M_offset;
192
193 _GLIBCXX20_CONSTEXPR _GLIBCXX_ALWAYS_INLINE
194 void
195 _M_assume_normalized() const
196 {
197#if __has_attribute(__assume__) && !defined(_GLIBCXX_CLANG)
198 unsigned int __ofst = _M_offset;
199 __attribute__ ((__assume__ (__ofst < unsigned(_S_word_bit))));
200#endif
201 }
202
203 _GLIBCXX20_CONSTEXPR
204 _Bit_iterator_base(_Bit_type * __x, unsigned int __y)
205 : _M_p(__x), _M_offset(__y) { }
206
207 _GLIBCXX20_CONSTEXPR
208 void
209 _M_bump_up()
210 {
211 _M_assume_normalized();
212 if (_M_offset++ == int(_S_word_bit) - 1)
213 {
214 _M_offset = 0;
215 ++_M_p;
216 }
217 }
218
219 _GLIBCXX20_CONSTEXPR
220 void
221 _M_bump_down()
222 {
223 _M_assume_normalized();
224 if (_M_offset-- == 0)
225 {
226 _M_offset = int(_S_word_bit) - 1;
227 --_M_p;
228 }
229 }
230
231 _GLIBCXX20_CONSTEXPR
232 void
233 _M_incr(ptrdiff_t __i)
234 {
235 _M_assume_normalized();
236 difference_type __n = __i + _M_offset;
237 _M_p += __n / int(_S_word_bit);
238 __n = __n % int(_S_word_bit);
239 if (__n < 0)
240 {
241 __n += int(_S_word_bit);
242 --_M_p;
243 }
244 _M_offset = static_cast<unsigned int>(__n);
245 }
246
247 _GLIBCXX_NODISCARD
248 friend _GLIBCXX20_CONSTEXPR bool
249 operator==(const _Bit_iterator_base& __x, const _Bit_iterator_base& __y)
250 {
251 __x._M_assume_normalized();
252 __y._M_assume_normalized();
253 return __x._M_p == __y._M_p && __x._M_offset == __y._M_offset;
254 }
255
256#if __cpp_lib_three_way_comparison
257 [[nodiscard]]
258 friend constexpr strong_ordering
259 operator<=>(const _Bit_iterator_base& __x, const _Bit_iterator_base& __y)
260 noexcept
261 {
262 __x._M_assume_normalized();
263 __y._M_assume_normalized();
264 if (const auto __cmp = __x._M_p <=> __y._M_p; __cmp != 0)
265 return __cmp;
266 return __x._M_offset <=> __y._M_offset;
267 }
268#else
269 _GLIBCXX_NODISCARD
270 friend bool
271 operator<(const _Bit_iterator_base& __x, const _Bit_iterator_base& __y)
272 {
273 __x._M_assume_normalized();
274 __y._M_assume_normalized();
275 return __x._M_p < __y._M_p
276 || (__x._M_p == __y._M_p && __x._M_offset < __y._M_offset);
277 }
278
279 _GLIBCXX_NODISCARD
280 friend bool
281 operator!=(const _Bit_iterator_base& __x, const _Bit_iterator_base& __y)
282 { return !(__x == __y); }
283
284 _GLIBCXX_NODISCARD
285 friend bool
286 operator>(const _Bit_iterator_base& __x, const _Bit_iterator_base& __y)
287 { return __y < __x; }
288
289 _GLIBCXX_NODISCARD
290 friend bool
291 operator<=(const _Bit_iterator_base& __x, const _Bit_iterator_base& __y)
292 { return !(__y < __x); }
293
294 _GLIBCXX_NODISCARD
295 friend bool
296 operator>=(const _Bit_iterator_base& __x, const _Bit_iterator_base& __y)
297 { return !(__x < __y); }
298#endif // three-way comparison
299
300 friend _GLIBCXX20_CONSTEXPR ptrdiff_t
301 operator-(const _Bit_iterator_base& __x, const _Bit_iterator_base& __y)
302 {
303 __x._M_assume_normalized();
304 __y._M_assume_normalized();
305 return (int(_S_word_bit) * (__x._M_p - __y._M_p)
306 + __x._M_offset - __y._M_offset);
307 }
308 };
309#pragma GCC diagnostic pop
310
311 struct _Bit_iterator : public _Bit_iterator_base
312 {
313 typedef _Bit_reference reference;
314#if __cplusplus > 201703L
315 typedef void pointer;
316#else
317 typedef _Bit_reference* pointer;
318#endif
319 typedef _Bit_iterator iterator;
320
321 _GLIBCXX20_CONSTEXPR
322 _Bit_iterator() : _Bit_iterator_base(0, 0) { }
323
324 _GLIBCXX20_CONSTEXPR
325 _Bit_iterator(_Bit_type * __x, unsigned int __y)
326 : _Bit_iterator_base(__x, __y) { }
327
328 _GLIBCXX20_CONSTEXPR
329 iterator
330 _M_const_cast() const
331 { return *this; }
332
333 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
334 reference
335 operator*() const
336 {
337 _M_assume_normalized();
338 return reference(_M_p, 1UL << _M_offset);
339 }
340
341 _GLIBCXX20_CONSTEXPR
342 iterator&
343 operator++()
344 {
345 _M_bump_up();
346 return *this;
347 }
348
349 _GLIBCXX20_CONSTEXPR
350 iterator
351 operator++(int)
352 {
353 iterator __tmp = *this;
354 _M_bump_up();
355 return __tmp;
356 }
357
358 _GLIBCXX20_CONSTEXPR
359 iterator&
360 operator--()
361 {
362 _M_bump_down();
363 return *this;
364 }
365
366 _GLIBCXX20_CONSTEXPR
367 iterator
368 operator--(int)
369 {
370 iterator __tmp = *this;
371 _M_bump_down();
372 return __tmp;
373 }
374
375 _GLIBCXX20_CONSTEXPR
376 iterator&
377 operator+=(difference_type __i)
378 {
379 _M_incr(__i);
380 return *this;
381 }
382
383 _GLIBCXX20_CONSTEXPR
384 iterator&
385 operator-=(difference_type __i)
386 {
387 *this += -__i;
388 return *this;
389 }
390
391 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
392 reference
393 operator[](difference_type __i) const
394 { return *(*this + __i); }
395
396 _GLIBCXX_NODISCARD
397 friend _GLIBCXX20_CONSTEXPR iterator
398 operator+(const iterator& __x, difference_type __n)
399 {
400 iterator __tmp = __x;
401 __tmp += __n;
402 return __tmp;
403 }
404
405 _GLIBCXX_NODISCARD
406 friend _GLIBCXX20_CONSTEXPR iterator
407 operator+(difference_type __n, const iterator& __x)
408 { return __x + __n; }
409
410 _GLIBCXX_NODISCARD
411 friend _GLIBCXX20_CONSTEXPR iterator
412 operator-(const iterator& __x, difference_type __n)
413 {
414 iterator __tmp = __x;
415 __tmp -= __n;
416 return __tmp;
417 }
418 };
419
420 struct _Bit_const_iterator : public _Bit_iterator_base
421 {
422 typedef bool reference;
423 typedef bool const_reference;
424#if __cplusplus > 201703L
425 typedef void pointer;
426#else
427 typedef const bool* pointer;
428#endif
429 typedef _Bit_const_iterator const_iterator;
430
431 _GLIBCXX20_CONSTEXPR
432 _Bit_const_iterator() : _Bit_iterator_base(0, 0) { }
433
434 _GLIBCXX20_CONSTEXPR
435 _Bit_const_iterator(_Bit_type * __x, unsigned int __y)
436 : _Bit_iterator_base(__x, __y) { }
437
438 _GLIBCXX20_CONSTEXPR
439 _Bit_const_iterator(const _Bit_iterator& __x)
440 : _Bit_iterator_base(__x._M_p, __x._M_offset) { }
441
442 _GLIBCXX20_CONSTEXPR
443 _Bit_iterator
444 _M_const_cast() const
445 { return _Bit_iterator(_M_p, _M_offset); }
446
447 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
448 const_reference
449 operator*() const
450 {
451 _M_assume_normalized();
452 return _Bit_reference(_M_p, 1UL << _M_offset);
453 }
454
455 _GLIBCXX20_CONSTEXPR
456 const_iterator&
457 operator++()
458 {
459 _M_bump_up();
460 return *this;
461 }
462
463 _GLIBCXX20_CONSTEXPR
464 const_iterator
465 operator++(int)
466 {
467 const_iterator __tmp = *this;
468 _M_bump_up();
469 return __tmp;
470 }
471
472 _GLIBCXX20_CONSTEXPR
473 const_iterator&
474 operator--()
475 {
476 _M_bump_down();
477 return *this;
478 }
479
480 _GLIBCXX20_CONSTEXPR
481 const_iterator
482 operator--(int)
483 {
484 const_iterator __tmp = *this;
485 _M_bump_down();
486 return __tmp;
487 }
488
489 _GLIBCXX20_CONSTEXPR
490 const_iterator&
491 operator+=(difference_type __i)
492 {
493 _M_incr(__i);
494 return *this;
495 }
496
497 _GLIBCXX20_CONSTEXPR
498 const_iterator&
499 operator-=(difference_type __i)
500 {
501 *this += -__i;
502 return *this;
503 }
504
505 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
506 const_reference
507 operator[](difference_type __i) const
508 { return *(*this + __i); }
509
510 _GLIBCXX_NODISCARD
511 friend _GLIBCXX20_CONSTEXPR const_iterator
512 operator+(const const_iterator& __x, difference_type __n)
513 {
514 const_iterator __tmp = __x;
515 __tmp += __n;
516 return __tmp;
517 }
518
519 _GLIBCXX_NODISCARD
520 friend _GLIBCXX20_CONSTEXPR const_iterator
521 operator-(const const_iterator& __x, difference_type __n)
522 {
523 const_iterator __tmp = __x;
524 __tmp -= __n;
525 return __tmp;
526 }
527
528 _GLIBCXX_NODISCARD
529 friend _GLIBCXX20_CONSTEXPR const_iterator
530 operator+(difference_type __n, const const_iterator& __x)
531 { return __x + __n; }
532 };
533
534 template<typename _Alloc>
535 struct _Bvector_base
536 {
537 typedef typename __gnu_cxx::__alloc_traits<_Alloc>::template
538 rebind<_Bit_type>::other _Bit_alloc_type;
539 typedef typename __gnu_cxx::__alloc_traits<_Bit_alloc_type>
540 _Bit_alloc_traits;
541 typedef typename _Bit_alloc_traits::pointer _Bit_pointer;
542
543 struct _Bvector_impl_data
544 {
545#if !_GLIBCXX_INLINE_VERSION
546 _Bit_iterator _M_start;
547#else
548 // We don't need the offset field for the start, it's always zero.
549 struct {
550 _Bit_type* _M_p;
551 // Allow assignment from iterators (assume offset is zero):
552 _GLIBCXX20_CONSTEXPR
553 void operator=(_Bit_iterator __it) { _M_p = __it._M_p; }
554 } _M_start;
555#endif
556 _Bit_iterator _M_finish;
557 _Bit_pointer _M_end_of_storage;
558
559 _GLIBCXX20_CONSTEXPR
560 _Bvector_impl_data() _GLIBCXX_NOEXCEPT
561 : _M_start(), _M_finish(), _M_end_of_storage()
562 { }
563
564#if __cplusplus >= 201103L
565 _Bvector_impl_data(const _Bvector_impl_data&) = default;
566
567 _Bvector_impl_data&
568 operator=(const _Bvector_impl_data&) = default;
569
570 _GLIBCXX20_CONSTEXPR
571 _Bvector_impl_data(_Bvector_impl_data&& __x) noexcept
572 : _Bvector_impl_data(__x)
573 { __x._M_reset(); }
574
575 _GLIBCXX20_CONSTEXPR
576 void
577 _M_move_data(_Bvector_impl_data&& __x) noexcept
578 {
579 *this = __x;
580 __x._M_reset();
581 }
582#endif
583
584 _GLIBCXX20_CONSTEXPR
585 void
586 _M_reset() _GLIBCXX_NOEXCEPT
587 { *this = _Bvector_impl_data(); }
588
589 _GLIBCXX20_CONSTEXPR
590 void
591 _M_swap_data(_Bvector_impl_data& __x) _GLIBCXX_NOEXCEPT
592 {
593 // Do not use std::swap(_M_start, __x._M_start), etc as it loses
594 // information used by TBAA.
595 std::swap(*this, __x);
596 }
597 };
598
599 struct _Bvector_impl
600 : public _Bit_alloc_type, public _Bvector_impl_data
601 {
602 _GLIBCXX20_CONSTEXPR
603 _Bvector_impl() _GLIBCXX_NOEXCEPT_IF(
604 is_nothrow_default_constructible<_Bit_alloc_type>::value)
605#if __cpp_concepts && __glibcxx_type_trait_variable_templates
606 requires is_default_constructible_v<_Bit_alloc_type>
607#endif
608 : _Bit_alloc_type()
609 { }
610
611 _GLIBCXX20_CONSTEXPR
612 _Bvector_impl(const _Bit_alloc_type& __a) _GLIBCXX_NOEXCEPT
613 : _Bit_alloc_type(__a)
614 { }
615
616#if __cplusplus >= 201103L
617 // Not defaulted, to enforce noexcept(true) even when
618 // !is_nothrow_move_constructible<_Bit_alloc_type>.
619 _GLIBCXX20_CONSTEXPR
620 _Bvector_impl(_Bvector_impl&& __x) noexcept
621 : _Bit_alloc_type(std::move(__x)), _Bvector_impl_data(std::move(__x))
622 { }
623
624 _GLIBCXX20_CONSTEXPR
625 _Bvector_impl(_Bit_alloc_type&& __a, _Bvector_impl&& __x) noexcept
626 : _Bit_alloc_type(std::move(__a)), _Bvector_impl_data(std::move(__x))
627 { }
628#endif
629
630 _GLIBCXX20_CONSTEXPR
631 _Bit_type*
632 _M_end_addr() const _GLIBCXX_NOEXCEPT
633 {
634 if (this->_M_end_of_storage)
635 return std::__addressof(this->_M_end_of_storage[-1]) + 1;
636 return 0;
637 }
638 };
639
640 public:
641 typedef _Alloc allocator_type;
642
643 _GLIBCXX20_CONSTEXPR
644 _Bit_alloc_type&
645 _M_get_Bit_allocator() _GLIBCXX_NOEXCEPT
646 { return this->_M_impl; }
647
648 _GLIBCXX20_CONSTEXPR
649 const _Bit_alloc_type&
650 _M_get_Bit_allocator() const _GLIBCXX_NOEXCEPT
651 { return this->_M_impl; }
652
653 _GLIBCXX20_CONSTEXPR
654 allocator_type
655 get_allocator() const _GLIBCXX_NOEXCEPT
656 { return allocator_type(_M_get_Bit_allocator()); }
657
658#if __cplusplus >= 201103L
659 _Bvector_base() = default;
660#else
661 _Bvector_base() { }
662#endif
663
664 _GLIBCXX20_CONSTEXPR
665 _Bvector_base(const allocator_type& __a)
666 : _M_impl(_Bit_alloc_type(__a)) { }
667
668#if __cplusplus >= 201103L
669 _Bvector_base(_Bvector_base&&) = default;
670
671 _GLIBCXX20_CONSTEXPR
672 _Bvector_base(_Bvector_base&& __x, const allocator_type& __a) noexcept
673 : _M_impl(_Bit_alloc_type(__a), std::move(__x._M_impl))
674 { }
675#endif
676
677 _GLIBCXX20_CONSTEXPR
678 ~_Bvector_base()
679 { this->_M_deallocate(); }
680
681 protected:
682 _Bvector_impl _M_impl;
683
684 _GLIBCXX20_CONSTEXPR
685 _Bit_pointer
686 _M_allocate(size_t __n)
687 {
688 _Bit_pointer __p = _Bit_alloc_traits::allocate(_M_impl, _S_nword(__n));
689#if __cpp_lib_is_constant_evaluated && __cpp_constexpr_dynamic_alloc
690 if (std::is_constant_evaluated())
691 {
692 __n = _S_nword(__n);
693 for (size_t __i = 0; __i < __n; ++__i)
694 std::construct_at(std::to_address(__p) + __i);
695 }
696#endif
697 return __p;
698 }
699
700 _GLIBCXX20_CONSTEXPR
701 void
702 _M_deallocate()
703 {
704 if (_M_impl._M_start._M_p)
705 {
706 const size_t __n = _M_impl._M_end_addr() - _M_impl._M_start._M_p;
708 _M_impl._M_end_of_storage - __n,
709 __n);
710 _M_impl._M_reset();
711 }
712 }
713
714#if __cplusplus >= 201103L
715 _GLIBCXX20_CONSTEXPR
716 void
717 _M_move_data(_Bvector_base&& __x) noexcept
718 { _M_impl._M_move_data(std::move(__x._M_impl)); }
719#endif
720
721 // Caller must ensure the addition won't overflow.
722 _GLIBCXX_CONSTEXPR
723 static size_t
724 _S_nword(size_t __n)
725 { return (__n + int(_S_word_bit) - 1) / int(_S_word_bit); }
726 };
727
728 /**
729 * @brief A specialization of vector for booleans which offers fixed time
730 * access to individual elements in any order.
731 *
732 * @ingroup sequences
733 * @headerfile vector
734 * @since C++98
735 *
736 * @tparam _Alloc Allocator type.
737 *
738 * Note that vector<bool> does not actually meet the requirements for being
739 * a container. This is because the reference and pointer types are not
740 * really references and pointers to bool. See DR96 for details. @see
741 * vector for function documentation.
742 *
743 * In some terminology a %vector can be described as a dynamic
744 * C-style array, it offers fast and efficient access to individual
745 * elements in any order and saves the user from worrying about
746 * memory and size allocation. Subscripting ( @c [] ) access is
747 * also provided as with C-style arrays.
748 */
749 template<typename _Alloc>
750 class vector<bool, _Alloc> : protected _Bvector_base<_Alloc>
751 {
752 typedef _Bvector_base<_Alloc> _Base;
753 typedef typename _Base::_Bit_pointer _Bit_pointer;
754 typedef typename _Base::_Bit_alloc_traits _Bit_alloc_traits;
755
756#if __cplusplus >= 201103L
757 friend struct std::hash<vector>;
758# if __cplusplus > 201703L // || defined __STRICT_ANSI__
760 "std::vector must have the same value_type as its allocator");
761# endif
762#endif
763
764 public:
765 typedef bool value_type;
766 typedef size_t size_type;
767 typedef ptrdiff_t difference_type;
768 typedef _Bit_reference reference;
769 typedef bool const_reference;
770 typedef _Bit_reference* pointer;
771 typedef const bool* const_pointer;
772 typedef _Bit_iterator iterator;
773 typedef _Bit_const_iterator const_iterator;
774 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
775 typedef std::reverse_iterator<iterator> reverse_iterator;
776 typedef _Alloc allocator_type;
777
778 _GLIBCXX20_CONSTEXPR
779 allocator_type
780 get_allocator() const
781 { return _Base::get_allocator(); }
782
783 protected:
784 using _Base::_M_allocate;
785 using _Base::_M_deallocate;
786 using _Base::_S_nword;
787 using _Base::_M_get_Bit_allocator;
788
789 public:
790#if __cplusplus >= 201103L
791 vector() = default;
792#else
793 vector() { }
794#endif
795
796 _GLIBCXX20_CONSTEXPR
797 explicit
798 vector(const allocator_type& __a) _GLIBCXX_NOEXCEPT
799 : _Base(__a) { }
800
801#if __cplusplus >= 201103L
802 _GLIBCXX20_CONSTEXPR
803 explicit
804 vector(size_type __n, const allocator_type& __a = allocator_type())
805 : vector(__n, false, __a)
806 { }
807
808 _GLIBCXX20_CONSTEXPR
809 vector(size_type __n, const bool& __value,
810 const allocator_type& __a = allocator_type())
811#else
812 explicit
813 vector(size_type __n, const bool& __value = bool(),
814 const allocator_type& __a = allocator_type())
815#endif
816 : _Base(__a)
817 {
818 _M_check_and_initialize(__n);
819 _M_initialize_value(__value);
820 }
821
822 _GLIBCXX20_CONSTEXPR
823 vector(const vector& __x)
824 : _Base(_Bit_alloc_traits::_S_select_on_copy(__x._M_get_Bit_allocator()))
825 {
826 const_iterator __xbegin = __x.begin(), __xend = __x.end();
827 _M_initialize(__x.size());
828 _M_copy_aligned(__xbegin, __xend, begin());
829 }
830
831#if __cplusplus >= 201103L
832 vector(vector&&) = default;
833
834 private:
835 _GLIBCXX20_CONSTEXPR
836 vector(vector&& __x, const allocator_type& __a, true_type) noexcept
837 : _Base(std::move(__x), __a)
838 { }
839
840 _GLIBCXX20_CONSTEXPR
841 vector(vector&& __x, const allocator_type& __a, false_type)
842 : _Base(__a)
843 {
844 if (__x.get_allocator() == __a)
845 this->_M_move_data(std::move(__x));
846 else
847 {
848 _M_initialize(__x.size());
849 _M_copy_aligned(__x.begin(), __x.end(), begin());
850 __x.clear();
851 }
852 }
853
854 public:
855 _GLIBCXX20_CONSTEXPR
856 vector(vector&& __x, const __type_identity_t<allocator_type>& __a)
857 noexcept(_Bit_alloc_traits::_S_always_equal())
858 : vector(std::move(__x), __a,
860 { }
861
862 _GLIBCXX20_CONSTEXPR
863 vector(const vector& __x, const __type_identity_t<allocator_type>& __a)
864 : _Base(__a)
865 {
866 _M_initialize(__x.size());
867 _M_copy_aligned(__x.begin(), __x.end(), begin());
868 }
869
870 _GLIBCXX20_CONSTEXPR
872 const allocator_type& __a = allocator_type())
873 : _Base(__a)
874 {
875 _M_initialize_range(__l.begin(), __l.end(),
877 }
878#endif
879
880#if __cplusplus >= 201103L
881 template<typename _InputIterator,
882 typename = std::_RequireInputIter<_InputIterator>>
883 _GLIBCXX20_CONSTEXPR
884 vector(_InputIterator __first, _InputIterator __last,
885 const allocator_type& __a = allocator_type())
886 : _Base(__a)
887 {
888 _M_initialize_range(__first, __last,
889 std::__iterator_category(__first));
890 }
891#else
892 template<typename _InputIterator>
893 vector(_InputIterator __first, _InputIterator __last,
894 const allocator_type& __a = allocator_type())
895 : _Base(__a)
896 {
897 // Check whether it's an integral type. If so, it's not an iterator.
898 typedef typename std::__is_integer<_InputIterator>::__type _Integral;
899 _M_initialize_dispatch(__first, __last, _Integral());
900 }
901#endif
902
903#if __glibcxx_containers_ranges // C++ >= 23
904 /**
905 * @brief Construct a vector from a range.
906 * @param __rg A range of values that are convertible to `value_type`.
907 * @since C++23
908 */
909 template<__detail::__container_compatible_range<bool> _Rg>
910 constexpr
911 vector(from_range_t, _Rg&& __rg, const _Alloc& __a = _Alloc())
912 : _Base(__a)
913 {
915 {
916 _M_check_and_initialize(ranges::distance(__rg));
917 ranges::copy(__rg, begin());
918 }
919 else
920 {
921 auto __first = ranges::begin(__rg);
922 const auto __last = ranges::end(__rg);
923 for (; __first != __last; ++__first)
924 emplace_back(*__first);
925 }
926 }
927#endif
928
929 _GLIBCXX20_CONSTEXPR
930 ~vector() _GLIBCXX_NOEXCEPT { }
931
932 _GLIBCXX20_CONSTEXPR
933 vector&
934 operator=(const vector& __x)
935 {
936 if (&__x == this)
937 return *this;
938#if __cplusplus >= 201103L
939 if (_Bit_alloc_traits::_S_propagate_on_copy_assign())
940 {
941 if (this->_M_get_Bit_allocator() != __x._M_get_Bit_allocator())
942 {
943 this->_M_deallocate();
944 std::__alloc_on_copy(_M_get_Bit_allocator(),
945 __x._M_get_Bit_allocator());
946 _M_initialize(__x.size());
947 }
948 else
949 std::__alloc_on_copy(_M_get_Bit_allocator(),
950 __x._M_get_Bit_allocator());
951 }
952#endif
953 if (__x.size() > capacity())
954 {
955 this->_M_deallocate();
956 _M_initialize(__x.size());
957 }
958 this->_M_impl._M_finish = _M_copy_aligned(__x.begin(), __x.end(),
959 begin());
960 return *this;
961 }
962
963#if __cplusplus >= 201103L
964 _GLIBCXX20_CONSTEXPR
965 vector&
966 operator=(vector&& __x) noexcept(_Bit_alloc_traits::_S_nothrow_move())
967 {
968 if (_Bit_alloc_traits::_S_propagate_on_move_assign()
969 || this->_M_get_Bit_allocator() == __x._M_get_Bit_allocator())
970 {
971 this->_M_deallocate();
972 this->_M_move_data(std::move(__x));
973 std::__alloc_on_move(_M_get_Bit_allocator(),
974 __x._M_get_Bit_allocator());
975 }
976 else
977 {
978 if (__x.size() > capacity())
979 {
980 this->_M_deallocate();
981 _M_initialize(__x.size());
982 }
983 this->_M_impl._M_finish = _M_copy_aligned(__x.begin(), __x.end(),
984 begin());
985 __x.clear();
986 }
987 return *this;
988 }
989
990 _GLIBCXX20_CONSTEXPR
991 vector&
993 {
994 this->assign(__l.begin(), __l.end());
995 return *this;
996 }
997#endif
998
999 // assign(), a generalized assignment member function. Two
1000 // versions: one that takes a count, and one that takes a range.
1001 // The range version is a member template, so we dispatch on whether
1002 // or not the type is an integer.
1003 _GLIBCXX20_CONSTEXPR
1004 void
1005 assign(size_type __n, const bool& __x)
1006 { _M_fill_assign(__n, __x); }
1007
1008#if __cplusplus >= 201103L
1009 template<typename _InputIterator,
1010 typename = std::_RequireInputIter<_InputIterator>>
1011 _GLIBCXX20_CONSTEXPR
1012 void
1013 assign(_InputIterator __first, _InputIterator __last)
1014 { _M_assign_aux(__first, __last, std::__iterator_category(__first)); }
1015#else
1016 template<typename _InputIterator>
1017 void
1018 assign(_InputIterator __first, _InputIterator __last)
1019 {
1020 // Check whether it's an integral type. If so, it's not an iterator.
1021 typedef typename std::__is_integer<_InputIterator>::__type _Integral;
1022 _M_assign_dispatch(__first, __last, _Integral());
1023 }
1024#endif
1025
1026#if __cplusplus >= 201103L
1027 _GLIBCXX20_CONSTEXPR
1028 void
1030 { _M_assign_aux(__l.begin(), __l.end(), random_access_iterator_tag()); }
1031#endif
1032
1033#if __glibcxx_containers_ranges // C++ >= 23
1034 /**
1035 * @brief Assign a range to the vector.
1036 * @param __rg A range of values that are convertible to `value_type`.
1037 * @pre `__rg` and `*this` do not overlap.
1038 * @since C++23
1039 */
1040 template<__detail::__container_compatible_range<bool> _Rg>
1041 constexpr void
1042 assign_range(_Rg&& __rg)
1043 {
1046 {
1047 if (auto __n = size_type(ranges::distance(__rg)))
1048 {
1049 reserve(__n);
1050 this->_M_impl._M_finish
1051 = ranges::copy(std::forward<_Rg>(__rg), begin()).out;
1052 }
1053 else
1054 clear();
1055 }
1056 else
1057 {
1058 clear();
1059 auto __first = ranges::begin(__rg);
1060 const auto __last = ranges::end(__rg);
1061 for (; __first != __last; ++__first)
1062 emplace_back(*__first);
1063 }
1064 }
1065#endif
1066
1067 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1068 iterator
1069 begin() _GLIBCXX_NOEXCEPT
1070 { return iterator(this->_M_impl._M_start._M_p, 0); }
1071
1072 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1073 const_iterator
1074 begin() const _GLIBCXX_NOEXCEPT
1075 { return const_iterator(this->_M_impl._M_start._M_p, 0); }
1076
1077 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1078 iterator
1079 end() _GLIBCXX_NOEXCEPT
1080 { return this->_M_impl._M_finish; }
1081
1082 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1083 const_iterator
1084 end() const _GLIBCXX_NOEXCEPT
1085 { return this->_M_impl._M_finish; }
1086
1087 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1088 reverse_iterator
1089 rbegin() _GLIBCXX_NOEXCEPT
1090 { return reverse_iterator(end()); }
1091
1092 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1093 const_reverse_iterator
1094 rbegin() const _GLIBCXX_NOEXCEPT
1095 { return const_reverse_iterator(end()); }
1096
1097 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1098 reverse_iterator
1099 rend() _GLIBCXX_NOEXCEPT
1100 { return reverse_iterator(begin()); }
1101
1102 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1103 const_reverse_iterator
1104 rend() const _GLIBCXX_NOEXCEPT
1105 { return const_reverse_iterator(begin()); }
1106
1107#if __cplusplus >= 201103L
1108 [[__nodiscard__]] _GLIBCXX20_CONSTEXPR
1109 const_iterator
1110 cbegin() const noexcept
1111 { return const_iterator(this->_M_impl._M_start._M_p, 0); }
1112
1113 [[__nodiscard__]] _GLIBCXX20_CONSTEXPR
1114 const_iterator
1115 cend() const noexcept
1116 { return this->_M_impl._M_finish; }
1117
1118 [[__nodiscard__]] _GLIBCXX20_CONSTEXPR
1119 const_reverse_iterator
1120 crbegin() const noexcept
1121 { return const_reverse_iterator(end()); }
1122
1123 [[__nodiscard__]] _GLIBCXX20_CONSTEXPR
1124 const_reverse_iterator
1125 crend() const noexcept
1126 { return const_reverse_iterator(begin()); }
1127#endif
1128
1129 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1130 size_type
1131 size() const _GLIBCXX_NOEXCEPT
1132 { return size_type(end() - begin()); }
1133
1134 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1135 size_type
1136 max_size() const _GLIBCXX_NOEXCEPT
1137 {
1138 const size_type __isize =
1139 __gnu_cxx::__numeric_traits<difference_type>::__max
1140 - int(_S_word_bit) + 1;
1141 const size_type __asize
1142 = _Bit_alloc_traits::max_size(_M_get_Bit_allocator());
1143 return (__asize <= __isize / int(_S_word_bit)
1144 ? __asize * int(_S_word_bit) : __isize);
1145 }
1146
1147 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1148 size_type
1149 capacity() const _GLIBCXX_NOEXCEPT
1150 { return size_type(const_iterator(this->_M_impl._M_end_addr(), 0)
1151 - begin()); }
1152
1153 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1154 bool
1155 empty() const _GLIBCXX_NOEXCEPT
1156 { return begin() == end(); }
1157
1158 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1159 reference
1160 operator[](size_type __n)
1161 {
1162 __glibcxx_requires_subscript(__n);
1163 return _Bit_reference (this->_M_impl._M_start._M_p
1164 + __n / int(_S_word_bit),
1165 1UL << __n % int(_S_word_bit));
1166 }
1167
1168 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1169 const_reference
1170 operator[](size_type __n) const
1171 {
1172 __glibcxx_requires_subscript(__n);
1173 return _Bit_reference (this->_M_impl._M_start._M_p
1174 + __n / int(_S_word_bit),
1175 1UL << __n % int(_S_word_bit));
1176 }
1177
1178 protected:
1179 _GLIBCXX20_CONSTEXPR
1180 void
1181 _M_range_check(size_type __n) const
1182 {
1183 if (__n >= this->size())
1184 __throw_out_of_range_fmt(__N("vector<bool>::_M_range_check: __n "
1185 "(which is %zu) >= this->size() "
1186 "(which is %zu)"),
1187 __n, this->size());
1188 }
1189
1190 public:
1191 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1192 reference
1193 at(size_type __n)
1194 {
1195 _M_range_check(__n);
1196 return (*this)[__n];
1197 }
1198
1199 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1200 const_reference
1201 at(size_type __n) const
1202 {
1203 _M_range_check(__n);
1204 return (*this)[__n];
1205 }
1206
1207 _GLIBCXX20_CONSTEXPR
1208 void
1209 reserve(size_type __n)
1210 {
1211 if (__n > max_size())
1212 __throw_length_error(__N("vector::reserve"));
1213 if (capacity() < __n)
1214 _M_reallocate(__n);
1215 }
1216
1217 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1218 reference
1219 front()
1220 {
1221 __glibcxx_requires_nonempty();
1222 return *begin();
1223 }
1224
1225 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1226 const_reference
1227 front() const
1228 {
1229 __glibcxx_requires_nonempty();
1230 return *begin();
1231 }
1232
1233 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1234 reference
1235 back()
1236 {
1237 __glibcxx_requires_nonempty();
1238 return *(end() - 1);
1239 }
1240
1241 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1242 const_reference
1243 back() const
1244 {
1245 __glibcxx_requires_nonempty();
1246 return *(end() - 1);
1247 }
1248
1249 _GLIBCXX20_CONSTEXPR
1250 void
1251 push_back(bool __x)
1252 {
1253 if (this->_M_impl._M_finish._M_p != this->_M_impl._M_end_addr())
1254 *this->_M_impl._M_finish++ = __x;
1255 else
1256 _M_insert_aux(end(), __x);
1257 }
1258
1259 _GLIBCXX20_CONSTEXPR
1260 void
1261 swap(vector& __x) _GLIBCXX_NOEXCEPT
1262 {
1263#if __cplusplus >= 201103L
1264 __glibcxx_assert(_Bit_alloc_traits::propagate_on_container_swap::value
1265 || _M_get_Bit_allocator() == __x._M_get_Bit_allocator());
1266#endif
1267 this->_M_impl._M_swap_data(__x._M_impl);
1268 _Bit_alloc_traits::_S_on_swap(_M_get_Bit_allocator(),
1269 __x._M_get_Bit_allocator());
1270 }
1271
1272 // [23.2.5]/1, third-to-last entry in synopsis listing
1273 _GLIBCXX26_DEPRECATED_SUGGEST("swap' invoked via ADL or 'std::ranges::swap")
1274 _GLIBCXX20_CONSTEXPR
1275 static void
1276 swap(reference __x, reference __y) _GLIBCXX_NOEXCEPT
1277 {
1278 bool __tmp = __x;
1279 __x = __y;
1280 __y = __tmp;
1281 }
1282
1283 _GLIBCXX20_CONSTEXPR
1284 iterator
1285#if __cplusplus >= 201103L
1286 insert(const_iterator __position, const bool& __x)
1287#else
1288 insert(iterator __position, const bool& __x)
1289#endif
1290 {
1291 const difference_type __n = __position - begin();
1292 if (this->_M_impl._M_finish._M_p != this->_M_impl._M_end_addr()
1293 && __position == end())
1294 *this->_M_impl._M_finish++ = __x;
1295 else
1296 _M_insert_aux(__position._M_const_cast(), __x);
1297 return begin() + __n;
1298 }
1299
1300#if _GLIBCXX_USE_DEPRECATED
1301 _GLIBCXX_DEPRECATED_SUGGEST("insert(position, false)")
1302 iterator
1303 insert(const_iterator __position)
1304 { return this->insert(__position._M_const_cast(), false); }
1305#endif
1306
1307#if __cplusplus >= 201103L
1308 template<typename _InputIterator,
1309 typename = std::_RequireInputIter<_InputIterator>>
1310 _GLIBCXX20_CONSTEXPR
1311 iterator
1312 insert(const_iterator __position,
1313 _InputIterator __first, _InputIterator __last)
1314 {
1315 difference_type __offset = __position - cbegin();
1316 _M_insert_range(__position._M_const_cast(),
1317 __first, __last,
1318 std::__iterator_category(__first));
1319 return begin() + __offset;
1320 }
1321#else
1322 template<typename _InputIterator>
1323 void
1324 insert(iterator __position,
1325 _InputIterator __first, _InputIterator __last)
1326 {
1327 // Check whether it's an integral type. If so, it's not an iterator.
1328 typedef typename std::__is_integer<_InputIterator>::__type _Integral;
1329 _M_insert_dispatch(__position, __first, __last, _Integral());
1330 }
1331#endif
1332
1333#if __cplusplus >= 201103L
1334 _GLIBCXX20_CONSTEXPR
1335 iterator
1336 insert(const_iterator __position, size_type __n, const bool& __x)
1337 {
1338 difference_type __offset = __position - cbegin();
1339 _M_fill_insert(__position._M_const_cast(), __n, __x);
1340 return begin() + __offset;
1341 }
1342#else
1343 void
1344 insert(iterator __position, size_type __n, const bool& __x)
1345 { _M_fill_insert(__position, __n, __x); }
1346#endif
1347
1348#if __cplusplus >= 201103L
1349 _GLIBCXX20_CONSTEXPR
1350 iterator
1351 insert(const_iterator __p, initializer_list<bool> __l)
1352 { return this->insert(__p, __l.begin(), __l.end()); }
1353#endif
1354
1355#if __glibcxx_containers_ranges // C++ >= 23
1356 /**
1357 * @brief Insert a range into the vector.
1358 * @param __rg A range of values that are convertible to `bool`.
1359 * @return An iterator that points to the first new element inserted,
1360 * or to `__pos` if `__rg` is an empty range.
1361 * @pre `__rg` and `*this` do not overlap.
1362 * @since C++23
1363 */
1364 template<__detail::__container_compatible_range<bool> _Rg>
1365 constexpr iterator
1366 insert_range(const_iterator __pos, _Rg&& __rg)
1367 {
1369 {
1370 if (auto __n = size_type(ranges::distance(__rg)))
1371 {
1372 if (capacity() - size() >= __n)
1373 {
1374 std::copy_backward(__pos._M_const_cast(), end(),
1375 this->_M_impl._M_finish
1376 + difference_type(__n));
1377 ranges::copy(__rg, __pos._M_const_cast());
1378 this->_M_impl._M_finish += difference_type(__n);
1379 return __pos._M_const_cast();
1380 }
1381 else
1382 {
1383 const size_type __len =
1384 _M_check_len(__n, "vector<bool>::insert_range");
1385 const iterator __begin = begin(), __end = end();
1386 _Bit_pointer __q = this->_M_allocate(__len);
1387 iterator __start(std::__addressof(*__q), 0);
1388 iterator __i = _M_copy_aligned(__begin,
1389 __pos._M_const_cast(),
1390 __start);
1391 iterator __j = ranges::copy(__rg, __i).out;
1392 iterator __finish = std::copy(__pos._M_const_cast(),
1393 __end, __j);
1394 this->_M_deallocate();
1395 this->_M_impl._M_end_of_storage = __q + _S_nword(__len);
1396 this->_M_impl._M_start = __start;
1397 this->_M_impl._M_finish = __finish;
1398 return __i;
1399 }
1400 }
1401 else
1402 return __pos._M_const_cast();
1403 }
1404 else
1405 return insert_range(__pos,
1406 vector(from_range, __rg, get_allocator()));
1407 }
1408
1409 /**
1410 * @brief Append a range at the end of the vector.
1411 * @since C++23
1412 */
1413 template<__detail::__container_compatible_range<bool> _Rg>
1414 constexpr void
1415 append_range(_Rg&& __rg)
1416 {
1417 // N.B. __rg may overlap with *this, so we must copy from __rg before
1418 // existing elements or iterators referring to *this are invalidated.
1419 // e.g. in v.append_range(views::concat(v, foo)) rg overlaps v.
1421 {
1422 const auto __n = size_type(ranges::distance(__rg));
1423
1424 // If there is no existing storage, there are no iterators that
1425 // can be referring to our storage, so it's safe to allocate now.
1426 if (capacity() == 0)
1427 reserve(__n);
1428
1429 const auto __sz = size();
1430 const auto __capacity = capacity();
1431 if ((__capacity - __sz) >= __n)
1432 {
1433 this->_M_impl._M_finish
1434 = ranges::copy(std::forward<_Rg>(__rg), end()).out;
1435 return;
1436 }
1437
1438 vector __tmp(get_allocator());
1439 __tmp.reserve(_M_check_len(__n, "vector::append_range"));
1440 __tmp._M_impl._M_finish
1441 = _M_copy_aligned(cbegin(), cend(), __tmp.begin());
1442 __tmp._M_impl._M_finish
1443 = ranges::copy(std::forward<_Rg>(__rg), __tmp.end()).out;
1444 swap(__tmp);
1445 }
1446 else
1447 {
1448 auto __first = ranges::begin(__rg);
1449 const auto __last = ranges::end(__rg);
1450
1451 // Fill up to the end of current capacity.
1452 for (auto __free = capacity() - size();
1453 __first != __last && __free > 0;
1454 ++__first, (void) --__free)
1455 emplace_back(*__first);
1456
1457 if (__first == __last)
1458 return;
1459
1460 // Copy the rest of the range to a new vector.
1461 ranges::subrange __rest(std::move(__first), __last);
1462 vector __tmp(from_range, __rest, get_allocator());
1463 insert(end(), __tmp.begin(), __tmp.end());
1464 }
1465 }
1466#endif // containers_ranges
1467
1468 _GLIBCXX20_CONSTEXPR
1469 void
1470 pop_back()
1471 { --this->_M_impl._M_finish; }
1472
1473 _GLIBCXX20_CONSTEXPR
1474 iterator
1475#if __cplusplus >= 201103L
1476 erase(const_iterator __position)
1477#else
1478 erase(iterator __position)
1479#endif
1480 { return _M_erase(__position._M_const_cast()); }
1481
1482 _GLIBCXX20_CONSTEXPR
1483 iterator
1484#if __cplusplus >= 201103L
1485 erase(const_iterator __first, const_iterator __last)
1486#else
1487 erase(iterator __first, iterator __last)
1488#endif
1489 { return _M_erase(__first._M_const_cast(), __last._M_const_cast()); }
1490
1491 _GLIBCXX20_CONSTEXPR
1492 void
1493 resize(size_type __new_size, bool __x = bool())
1494 {
1495 if (__new_size < size())
1496 _M_erase_at_end(begin() + difference_type(__new_size));
1497 else
1498 insert(end(), __new_size - size(), __x);
1499 }
1500
1501#if __cplusplus >= 201103L
1502 _GLIBCXX20_CONSTEXPR
1503 void
1505 { _M_shrink_to_fit(); }
1506#endif
1507
1508 _GLIBCXX20_CONSTEXPR
1509 void
1510 flip() _GLIBCXX_NOEXCEPT
1511 {
1512 _Bit_type * const __end = this->_M_impl._M_end_addr();
1513 for (_Bit_type * __p = this->_M_impl._M_start._M_p; __p != __end; ++__p)
1514 *__p = ~*__p;
1515 }
1516
1517 _GLIBCXX20_CONSTEXPR
1518 void
1519 clear() _GLIBCXX_NOEXCEPT
1520 { _M_erase_at_end(begin()); }
1521
1522#if __cplusplus >= 201103L
1523 template<typename... _Args>
1524#if __cplusplus > 201402L
1525 _GLIBCXX20_CONSTEXPR
1526 reference
1527#else
1528 void
1529#endif
1530 emplace_back(_Args&&... __args)
1531 {
1532 push_back(bool(std::forward<_Args>(__args)...));
1533#if __cplusplus > 201402L
1534 return back();
1535#endif
1536 }
1537
1538 template<typename... _Args>
1539 _GLIBCXX20_CONSTEXPR
1540 iterator
1541 emplace(const_iterator __pos, _Args&&... __args)
1542 { return insert(__pos, bool(std::forward<_Args>(__args)...)); }
1543#endif
1544
1545 protected:
1546 // Precondition: __first._M_offset == 0 && __result._M_offset == 0.
1547 _GLIBCXX20_CONSTEXPR
1548 iterator
1549 _M_copy_aligned(const_iterator __first, const_iterator __last,
1550 iterator __result)
1551 {
1552 _Bit_type* __q = std::copy(__first._M_p, __last._M_p, __result._M_p);
1553 return std::copy(const_iterator(__last._M_p, 0), __last,
1554 iterator(__q, 0));
1555 }
1556
1557 _GLIBCXX20_CONSTEXPR
1558 void
1559 _M_initialize(size_type __n)
1560 {
1561 // Caller must ensure this, to avoid overflow in _S_nword.
1562 _GLIBCXX_DEBUG_ASSERT(__n <= (size_t)-_S_word_bit);
1563
1564 if (__n)
1565 {
1566 _Bit_pointer __q = this->_M_allocate(__n);
1567 this->_M_impl._M_end_of_storage = __q + _S_nword(__n);
1568 iterator __start = iterator(std::__addressof(*__q), 0);
1569 this->_M_impl._M_start = __start;
1570 this->_M_impl._M_finish = __start + difference_type(__n);
1571 }
1572 }
1573
1574 template<typename _SizeT>
1575 _GLIBCXX20_CONSTEXPR
1576 void
1577 _M_check_and_initialize(_SizeT __n)
1578 {
1579 if (__n > max_size() || __n < 0)
1580 __throw_length_error(
1581 __N("Cannot create std::vector<bool> larger than max_size()"));
1582 _M_initialize(static_cast<size_type>(__n));
1583 }
1584
1585 _GLIBCXX20_CONSTEXPR
1586 void
1587 _M_initialize_value(bool __x) _GLIBCXX_NOEXCEPT
1588 {
1589 if (_Bit_type* __p = this->_M_impl._M_start._M_p)
1590 __fill_bvector_n(__p, this->_M_impl._M_end_addr() - __p, __x);
1591 }
1592
1593 _GLIBCXX20_CONSTEXPR
1594 void
1595 _M_reallocate(size_type __n);
1596
1597#if __cplusplus >= 201103L
1598 _GLIBCXX20_CONSTEXPR
1599 bool
1600 _M_shrink_to_fit();
1601#endif
1602
1603#if __cplusplus < 201103L
1604 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1605 // 438. Ambiguity in the "do the right thing" clause
1606 template<typename _Integer>
1607 void
1608 _M_initialize_dispatch(_Integer __n, _Integer __x, __true_type)
1609 {
1610 _M_check_and_initialize(static_cast<size_type>(__n));
1611 _M_initialize_value(__x);
1612 }
1613
1614 template<typename _InputIterator>
1615 void
1616 _M_initialize_dispatch(_InputIterator __first, _InputIterator __last,
1617 __false_type)
1618 { _M_initialize_range(__first, __last,
1619 std::__iterator_category(__first)); }
1620#endif
1621
1622 template<typename _InputIterator>
1623 _GLIBCXX20_CONSTEXPR
1624 void
1625 _M_initialize_range(_InputIterator __first, _InputIterator __last,
1627 {
1628 for (; __first != __last; ++__first)
1629 push_back(*__first);
1630 }
1631
1632 template<typename _ForwardIterator>
1633 _GLIBCXX20_CONSTEXPR
1634 void
1635 _M_initialize_range(_ForwardIterator __first, _ForwardIterator __last,
1637 {
1638 _M_check_and_initialize(std::distance(__first, __last));
1639 std::copy(__first, __last, begin());
1640 }
1641
1642#if __cplusplus < 201103L
1643 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1644 // 438. Ambiguity in the "do the right thing" clause
1645 template<typename _Integer>
1646 void
1647 _M_assign_dispatch(_Integer __n, _Integer __val, __true_type)
1648 { _M_fill_assign(__n, __val); }
1649
1650 template<class _InputIterator>
1651 void
1652 _M_assign_dispatch(_InputIterator __first, _InputIterator __last,
1653 __false_type)
1654 { _M_assign_aux(__first, __last, std::__iterator_category(__first)); }
1655#endif
1656
1657 _GLIBCXX20_CONSTEXPR
1658 void
1659 _M_fill_assign(size_t __n, bool __x)
1660 {
1661 if (__n > size())
1662 {
1663 _M_initialize_value(__x);
1664 insert(end(), __n - size(), __x);
1665 }
1666 else
1667 {
1668 _M_erase_at_end(begin() + __n);
1669 _M_initialize_value(__x);
1670 }
1671 }
1672
1673 template<typename _InputIterator>
1674 _GLIBCXX20_CONSTEXPR
1675 void
1676 _M_assign_aux(_InputIterator __first, _InputIterator __last,
1678 {
1679 iterator __cur = begin();
1680 for (; __first != __last && __cur != end(); ++__cur, (void)++__first)
1681 *__cur = *__first;
1682 if (__first == __last)
1683 _M_erase_at_end(__cur);
1684 else
1685 insert(end(), __first, __last);
1686 }
1687
1688 template<typename _ForwardIterator>
1689 _GLIBCXX20_CONSTEXPR
1690 void
1691 _M_assign_aux(_ForwardIterator __first, _ForwardIterator __last,
1693 {
1694 const size_type __len = std::distance(__first, __last);
1695 if (__len < size())
1696 _M_erase_at_end(std::copy(__first, __last, begin()));
1697 else
1698 {
1699 _ForwardIterator __mid = __first;
1700 std::advance(__mid, size());
1701 std::copy(__first, __mid, begin());
1702 insert(end(), __mid, __last);
1703 }
1704 }
1705
1706#if __cplusplus < 201103L
1707 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1708 // 438. Ambiguity in the "do the right thing" clause
1709 template<typename _Integer>
1710 void
1711 _M_insert_dispatch(iterator __pos, _Integer __n, _Integer __x,
1712 __true_type)
1713 { _M_fill_insert(__pos, __n, __x); }
1714
1715 template<typename _InputIterator>
1716 void
1717 _M_insert_dispatch(iterator __pos,
1718 _InputIterator __first, _InputIterator __last,
1719 __false_type)
1720 { _M_insert_range(__pos, __first, __last,
1721 std::__iterator_category(__first)); }
1722#endif
1723
1724 _GLIBCXX20_CONSTEXPR
1725 void
1726 _M_fill_insert(iterator __position, size_type __n, bool __x);
1727
1728 template<typename _InputIterator>
1729 _GLIBCXX20_CONSTEXPR
1730 void
1731 _M_insert_range(iterator __pos, _InputIterator __first,
1732 _InputIterator __last, std::input_iterator_tag)
1733 {
1734 for (; __first != __last; ++__first)
1735 {
1736 __pos = insert(__pos, *__first);
1737 ++__pos;
1738 }
1739 }
1740
1741 template<typename _ForwardIterator>
1742 _GLIBCXX20_CONSTEXPR
1743 void
1744 _M_insert_range(iterator __position, _ForwardIterator __first,
1745 _ForwardIterator __last, std::forward_iterator_tag);
1746
1747 _GLIBCXX20_CONSTEXPR
1748 void
1749 _M_insert_aux(iterator __position, bool __x);
1750
1751 _GLIBCXX20_CONSTEXPR
1752 size_type
1753 _M_check_len(size_type __n, const char* __s) const
1754 {
1755 if (max_size() - size() < __n)
1756 __throw_length_error(__N(__s));
1757
1758 const size_type __len = size() + std::max(size(), __n);
1759 return (__len < size() || __len > max_size()) ? max_size() : __len;
1760 }
1761
1762 _GLIBCXX20_CONSTEXPR
1763 void
1764 _M_erase_at_end(iterator __pos)
1765 { this->_M_impl._M_finish = __pos; }
1766
1767 _GLIBCXX20_CONSTEXPR
1768 iterator
1769 _M_erase(iterator __pos);
1770
1771 _GLIBCXX20_CONSTEXPR
1772 iterator
1773 _M_erase(iterator __first, iterator __last);
1774
1775 protected:
1776 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1777 // DR 464. Suggestion for new member functions in standard containers.
1778 // N.B. DR 464 says nothing about vector<bool> but we need something
1779 // here due to the using-declaration in __gnu_debug::vector.
1780#pragma GCC diagnostic push
1781#pragma GCC diagnostic ignored "-Wc++11-extensions"
1782 void data() = delete;
1783#pragma GCC diagnostic pop
1784 };
1785
1786_GLIBCXX_END_NAMESPACE_CONTAINER
1787
1788 // Fill a partial word.
1789 _GLIBCXX20_CONSTEXPR
1790 inline void
1791 __fill_bvector(_Bit_type* __v, unsigned int __first, unsigned int __last,
1792 bool __x) _GLIBCXX_NOEXCEPT
1793 {
1794 const _Bit_type __fmask = ~0ul << __first;
1795 const _Bit_type __lmask = ~0ul >> (_S_word_bit - __last);
1796 const _Bit_type __mask = __fmask & __lmask;
1797
1798 if (__x)
1799 *__v |= __mask;
1800 else
1801 *__v &= ~__mask;
1802 }
1803
1804 // Fill N full words, as if using memset, but usable in constant expressions.
1805 __attribute__((__nonnull__))
1806 _GLIBCXX20_CONSTEXPR
1807 inline void
1808 __fill_bvector_n(_Bit_type* __p, size_t __n, bool __x) _GLIBCXX_NOEXCEPT
1809 {
1810#if __cpp_lib_is_constant_evaluated
1811 if (std::is_constant_evaluated())
1812 {
1813 for (size_t __i = 0; __i < __n; ++__i)
1814 __p[__i] = __x ? ~0ul : 0ul;
1815 return;
1816 }
1817#endif
1818 __builtin_memset(__p, __x ? ~0 : 0, __n * sizeof(_Bit_type));
1819 }
1820
1821
1822 _GLIBCXX20_CONSTEXPR
1823 inline void
1824 __fill_a1(_GLIBCXX_STD_C::_Bit_iterator __first,
1825 _GLIBCXX_STD_C::_Bit_iterator __last, const bool& __x)
1826 {
1827 if (__first._M_p != __last._M_p)
1828 {
1829 _Bit_type* __first_p = __first._M_p;
1830 if (__first._M_offset != 0)
1831 __fill_bvector(__first_p++, __first._M_offset, _S_word_bit, __x);
1832
1833 __fill_bvector_n(__first_p, __last._M_p - __first_p, __x);
1834
1835 if (__last._M_offset != 0)
1836 __fill_bvector(__last._M_p, 0, __last._M_offset, __x);
1837 }
1838 else if (__first._M_offset != __last._M_offset)
1839 __fill_bvector(__first._M_p, __first._M_offset, __last._M_offset, __x);
1840 }
1841
1842#if __cplusplus >= 201103L
1843 // DR 1182.
1844 /// std::hash specialization for vector<bool>.
1845 template<typename _Alloc>
1846 struct hash<_GLIBCXX_STD_C::vector<bool, _Alloc>>
1847 : public __hash_base<size_t, _GLIBCXX_STD_C::vector<bool, _Alloc>>
1848 {
1849 size_t
1850 operator()(const _GLIBCXX_STD_C::vector<bool, _Alloc>&) const noexcept;
1851 };
1852#endif // C++11
1853
1854_GLIBCXX_END_NAMESPACE_VERSION
1855} // namespace std
1856
1857#endif
constexpr bool operator<=(const duration< _Rep1, _Period1 > &__lhs, const duration< _Rep2, _Period2 > &__rhs)
Definition chrono.h:863
constexpr bool operator>=(const duration< _Rep1, _Period1 > &__lhs, const duration< _Rep2, _Period2 > &__rhs)
Definition chrono.h:877
constexpr bool operator<(const duration< _Rep1, _Period1 > &__lhs, const duration< _Rep2, _Period2 > &__rhs)
Definition chrono.h:830
constexpr bool operator>(const duration< _Rep1, _Period1 > &__lhs, const duration< _Rep2, _Period2 > &__rhs)
Definition chrono.h:870
constexpr _Tp * to_address(_Tp *__ptr) noexcept
Obtain address referenced by a pointer to an object.
Definition ptr_traits.h:234
__bool_constant< true > true_type
The type used as a compile-time boolean with true value.
Definition type_traits:121
__bool_constant< false > false_type
The type used as a compile-time boolean with false value.
Definition type_traits:124
constexpr std::remove_reference< _Tp >::type && move(_Tp &&__t) noexcept
Convert a value to an rvalue.
Definition move.h:138
constexpr _Tp * __addressof(_Tp &__r) noexcept
Same as C++11 std::addressof.
Definition move.h:52
constexpr _Tp && forward(typename std::remove_reference< _Tp >::type &__t) noexcept
Forward an lvalue.
Definition move.h:72
constexpr const _Tp & max(const _Tp &, const _Tp &)
This does what you think it does.
ISO C++ entities toplevel namespace is std.
constexpr iterator_traits< _InputIterator >::difference_type distance(_InputIterator __first, _InputIterator __last)
A generalization of pointer arithmetic.
constexpr void advance(_InputIterator &__i, _Distance __n)
A generalization of pointer arithmetic.
initializer_list
Primary class template hash.
typename __detected_or_t< is_empty< _Bit_alloc_type >, __equal, _Bit_alloc_type >::type is_always_equal
The ranges::subrange class template.
Marking input iterators.
Forward iterators support a superset of input iterator operations.
Random-access iterators support a superset of bidirectional iterator operations.
A standard container which offers fixed time access to individual elements in any order.
Definition stl_vector.h:511
constexpr iterator insert(const_iterator __position, const value_type &__x)
Inserts given value into vector before specified iterator.
Definition vector.tcc:134
constexpr void push_back(const value_type &__x)
Add data to the end of the vector.
constexpr reverse_iterator rbegin() noexcept
constexpr iterator end() noexcept
vector()=default
Creates a vector with no elements.
constexpr iterator emplace(const_iterator __position, _Args &&... __args)
Inserts an object in vector before specified iterator.
constexpr iterator begin() noexcept
constexpr size_type capacity() const noexcept
constexpr ~vector() noexcept
Definition stl_vector.h:842
constexpr void assign(size_type __n, const value_type &__val)
Assigns a given value to a vector.
Definition stl_vector.h:917
constexpr void swap(vector &__x) noexcept
Swaps data with another vector.
constexpr _Tp * data() noexcept
constexpr void pop_back() noexcept
Removes last element.
constexpr void reserve(size_type __n)
Attempt to preallocate enough memory for specified number of elements.
Definition vector.tcc:71
constexpr reference at(size_type __n)
Provides access to the data contained in the vector.
constexpr void resize(size_type __new_size)
Resizes the vector to the specified number of elements.
constexpr void _M_range_check(size_type __n) const
Safety check used only from at().
constexpr reference front() noexcept
constexpr iterator erase(const_iterator __position)
Remove element at given position.
constexpr bool empty() const noexcept
constexpr reverse_iterator rend() noexcept
constexpr const_reverse_iterator crbegin() const noexcept
constexpr const_iterator cbegin() const noexcept
constexpr void clear() noexcept
constexpr allocator_type get_allocator() const noexcept
Get a copy of the memory allocation object.
Definition stl_vector.h:317
constexpr size_type size() const noexcept
constexpr vector & operator=(const vector &__x)
Vector assignment operator.
Definition vector.tcc:210
constexpr reference back() noexcept
constexpr const_reverse_iterator crend() const noexcept
constexpr const_iterator cend() const noexcept
constexpr reference operator[](size_type __n) noexcept
Subscript access to the data contained in the vector.
constexpr void shrink_to_fit()
constexpr size_type max_size() const noexcept
static constexpr pointer allocate(_Bit_alloc_type &__a, size_type __n)
static constexpr void deallocate(_Bit_alloc_type &__a, pointer __p, size_type __n)
static constexpr size_type max_size(const _Bit_alloc_type &__a) noexcept
[concept.assignable], concept assignable_from
Definition concepts:149
[range.sized] The sized_range concept.
A range for which ranges::begin returns a forward iterator.