libstdc++: stl_bvector.h Source File

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>
65#include <bits/functional_hash.h>
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;
707 _Bit_alloc_traits::deallocate(_M_impl,
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 _GLIBCXX_CONSTEXPR
722 static size_t
723 _S_nword(size_t __n)
724 { return (__n + int(_S_word_bit) - 1) / int(_S_word_bit); }
725 };
726
727 /**
728 * @brief A specialization of vector for booleans which offers fixed time
729 * access to individual elements in any order.
730 *
731 * @ingroup sequences
732 * @headerfile vector
733 * @since C++98
734 *
735 * @tparam _Alloc Allocator type.
736 *
737 * Note that vector<bool> does not actually meet the requirements for being
738 * a container. This is because the reference and pointer types are not
739 * really references and pointers to bool. See DR96 for details. @see
740 * vector for function documentation.
741 *
742 * In some terminology a %vector can be described as a dynamic
743 * C-style array, it offers fast and efficient access to individual
744 * elements in any order and saves the user from worrying about
745 * memory and size allocation. Subscripting ( @c [] ) access is
746 * also provided as with C-style arrays.
747 */
748 template<typename _Alloc>
749 class vector<bool, _Alloc> : protected _Bvector_base<_Alloc>
750 {
751 typedef _Bvector_base<_Alloc> _Base;
752 typedef typename _Base::_Bit_pointer _Bit_pointer;
753 typedef typename _Base::_Bit_alloc_traits _Bit_alloc_traits;
754
755#if __cplusplus >= 201103L
756 friend struct std::hash<vector>;
757# if __cplusplus > 201703L // || defined __STRICT_ANSI__
758 static_assert(is_same<typename _Alloc::value_type, bool>::value,
759 "std::vector must have the same value_type as its allocator");
760# endif
761#endif
762
763 public:
764 typedef bool value_type;
765 typedef size_t size_type;
766 typedef ptrdiff_t difference_type;
767 typedef _Bit_reference reference;
768 typedef bool const_reference;
769 typedef _Bit_reference* pointer;
770 typedef const bool* const_pointer;
771 typedef _Bit_iterator iterator;
772 typedef _Bit_const_iterator const_iterator;
773 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
774 typedef std::reverse_iterator<iterator> reverse_iterator;
775 typedef _Alloc allocator_type;
776
777 _GLIBCXX20_CONSTEXPR
778 allocator_type
779 get_allocator() const
780 { return _Base::get_allocator(); }
781
782 protected:
783 using _Base::_M_allocate;
784 using _Base::_M_deallocate;
785 using _Base::_S_nword;
786 using _Base::_M_get_Bit_allocator;
787
788 public:
789#if __cplusplus >= 201103L
790 vector() = default;
791#else
792 vector() { }
793#endif
794
795 _GLIBCXX20_CONSTEXPR
796 explicit
797 vector(const allocator_type& __a) _GLIBCXX_NOEXCEPT
798 : _Base(__a) { }
799
800#if __cplusplus >= 201103L
801 _GLIBCXX20_CONSTEXPR
802 explicit
803 vector(size_type __n, const allocator_type& __a = allocator_type())
804 : vector(__n, false, __a)
805 { }
806
807 _GLIBCXX20_CONSTEXPR
808 vector(size_type __n, const bool& __value,
809 const allocator_type& __a = allocator_type())
810#else
811 explicit
812 vector(size_type __n, const bool& __value = bool(),
813 const allocator_type& __a = allocator_type())
814#endif
815 : _Base(__a)
816 {
817 _M_initialize(__n);
818 _M_initialize_value(__value);
819 }
820
821 _GLIBCXX20_CONSTEXPR
822 vector(const vector& __x)
823 : _Base(_Bit_alloc_traits::_S_select_on_copy(__x._M_get_Bit_allocator()))
824 {
825 const_iterator __xbegin = __x.begin(), __xend = __x.end();
826 _M_initialize(__x.size());
827 _M_copy_aligned(__xbegin, __xend, begin());
828 }
829
830#if __cplusplus >= 201103L
831 vector(vector&&) = default;
832
833 private:
834 _GLIBCXX20_CONSTEXPR
835 vector(vector&& __x, const allocator_type& __a, true_type) noexcept
836 : _Base(std::move(__x), __a)
837 { }
838
839 _GLIBCXX20_CONSTEXPR
840 vector(vector&& __x, const allocator_type& __a, false_type)
841 : _Base(__a)
842 {
843 if (__x.get_allocator() == __a)
844 this->_M_move_data(std::move(__x));
845 else
846 {
847 _M_initialize(__x.size());
848 _M_copy_aligned(__x.begin(), __x.end(), begin());
849 __x.clear();
850 }
851 }
852
853 public:
854 _GLIBCXX20_CONSTEXPR
855 vector(vector&& __x, const __type_identity_t<allocator_type>& __a)
856 noexcept(_Bit_alloc_traits::_S_always_equal())
857 : vector(std::move(__x), __a,
858 typename _Bit_alloc_traits::is_always_equal{})
859 { }
860
861 _GLIBCXX20_CONSTEXPR
862 vector(const vector& __x, const __type_identity_t<allocator_type>& __a)
863 : _Base(__a)
864 {
865 _M_initialize(__x.size());
866 _M_copy_aligned(__x.begin(), __x.end(), begin());
867 }
868
869 _GLIBCXX20_CONSTEXPR
870 vector(initializer_list<bool> __l,
871 const allocator_type& __a = allocator_type())
872 : _Base(__a)
873 {
874 _M_initialize_range(__l.begin(), __l.end(),
875 random_access_iterator_tag());
876 }
877#endif
878
879#if __cplusplus >= 201103L
880 template<typename _InputIterator,
881 typename = std::_RequireInputIter<_InputIterator>>
882 _GLIBCXX20_CONSTEXPR
883 vector(_InputIterator __first, _InputIterator __last,
884 const allocator_type& __a = allocator_type())
885 : _Base(__a)
886 {
887 _M_initialize_range(__first, __last,
888 std::__iterator_category(__first));
889 }
890#else
891 template<typename _InputIterator>
892 vector(_InputIterator __first, _InputIterator __last,
893 const allocator_type& __a = allocator_type())
894 : _Base(__a)
895 {
896 // Check whether it's an integral type. If so, it's not an iterator.
897 typedef typename std::__is_integer<_InputIterator>::__type _Integral;
898 _M_initialize_dispatch(__first, __last, _Integral());
899 }
900#endif
901
902#if __glibcxx_containers_ranges // C++ >= 23
903 /**
904 * @brief Construct a vector from a range.
905 * @param __rg A range of values that are convertible to `value_type`.
906 * @since C++23
907 */
908 template<__detail::__container_compatible_range<bool> _Rg>
909 constexpr
910 vector(from_range_t, _Rg&& __rg, const _Alloc& __a = _Alloc())
911 : _Base(__a)
912 {
913 if constexpr (ranges::forward_range<_Rg> || ranges::sized_range<_Rg>)
914 {
915 _M_initialize(size_type(ranges::distance(__rg)));
916 ranges::copy(__rg, begin());
917 }
918 else
919 {
920 auto __first = ranges::begin(__rg);
921 const auto __last = ranges::end(__rg);
922 for (; __first != __last; ++__first)
923 emplace_back(*__first);
924 }
925 }
926#endif
927
928 _GLIBCXX20_CONSTEXPR
929 ~vector() _GLIBCXX_NOEXCEPT { }
930
931 _GLIBCXX20_CONSTEXPR
932 vector&
933 operator=(const vector& __x)
934 {
935 if (&__x == this)
936 return *this;
937#if __cplusplus >= 201103L
938 if (_Bit_alloc_traits::_S_propagate_on_copy_assign())
939 {
940 if (this->_M_get_Bit_allocator() != __x._M_get_Bit_allocator())
941 {
942 this->_M_deallocate();
943 std::__alloc_on_copy(_M_get_Bit_allocator(),
944 __x._M_get_Bit_allocator());
945 _M_initialize(__x.size());
946 }
947 else
948 std::__alloc_on_copy(_M_get_Bit_allocator(),
949 __x._M_get_Bit_allocator());
950 }
951#endif
952 if (__x.size() > capacity())
953 {
954 this->_M_deallocate();
955 _M_initialize(__x.size());
956 }
957 this->_M_impl._M_finish = _M_copy_aligned(__x.begin(), __x.end(),
958 begin());
959 return *this;
960 }
961
962#if __cplusplus >= 201103L
963 _GLIBCXX20_CONSTEXPR
964 vector&
965 operator=(vector&& __x) noexcept(_Bit_alloc_traits::_S_nothrow_move())
966 {
967 if (_Bit_alloc_traits::_S_propagate_on_move_assign()
968 || this->_M_get_Bit_allocator() == __x._M_get_Bit_allocator())
969 {
970 this->_M_deallocate();
971 this->_M_move_data(std::move(__x));
972 std::__alloc_on_move(_M_get_Bit_allocator(),
973 __x._M_get_Bit_allocator());
974 }
975 else
976 {
977 if (__x.size() > capacity())
978 {
979 this->_M_deallocate();
980 _M_initialize(__x.size());
981 }
982 this->_M_impl._M_finish = _M_copy_aligned(__x.begin(), __x.end(),
983 begin());
984 __x.clear();
985 }
986 return *this;
987 }
988
989 _GLIBCXX20_CONSTEXPR
990 vector&
991 operator=(initializer_list<bool> __l)
992 {
993 this->assign(__l.begin(), __l.end());
994 return *this;
995 }
996#endif
997
998 // assign(), a generalized assignment member function. Two
999 // versions: one that takes a count, and one that takes a range.
1000 // The range version is a member template, so we dispatch on whether
1001 // or not the type is an integer.
1002 _GLIBCXX20_CONSTEXPR
1003 void
1004 assign(size_type __n, const bool& __x)
1005 { _M_fill_assign(__n, __x); }
1006
1007#if __cplusplus >= 201103L
1008 template<typename _InputIterator,
1009 typename = std::_RequireInputIter<_InputIterator>>
1010 _GLIBCXX20_CONSTEXPR
1011 void
1012 assign(_InputIterator __first, _InputIterator __last)
1013 { _M_assign_aux(__first, __last, std::__iterator_category(__first)); }
1014#else
1015 template<typename _InputIterator>
1016 void
1017 assign(_InputIterator __first, _InputIterator __last)
1018 {
1019 // Check whether it's an integral type. If so, it's not an iterator.
1020 typedef typename std::__is_integer<_InputIterator>::__type _Integral;
1021 _M_assign_dispatch(__first, __last, _Integral());
1022 }
1023#endif
1024
1025#if __cplusplus >= 201103L
1026 _GLIBCXX20_CONSTEXPR
1027 void
1028 assign(initializer_list<bool> __l)
1029 { _M_assign_aux(__l.begin(), __l.end(), random_access_iterator_tag()); }
1030#endif
1031
1032#if __glibcxx_containers_ranges // C++ >= 23
1033 /**
1034 * @brief Assign a range to the vector.
1035 * @param __rg A range of values that are convertible to `value_type`.
1036 * @pre `__rg` and `*this` do not overlap.
1037 * @since C++23
1038 */
1039 template<__detail::__container_compatible_range<bool> _Rg>
1040 constexpr void
1041 assign_range(_Rg&& __rg)
1042 {
1043 static_assert(assignable_from<bool&, ranges::range_reference_t<_Rg>>);
1044 if constexpr (ranges::forward_range<_Rg> || ranges::sized_range<_Rg>)
1045 {
1046 if (auto __n = size_type(ranges::distance(__rg)))
1047 {
1048 reserve(__n);
1049 this->_M_impl._M_finish
1050 = ranges::copy(std::forward<_Rg>(__rg), begin()).out;
1051 }
1052 else
1053 clear();
1054 }
1055 else
1056 {
1057 clear();
1058 auto __first = ranges::begin(__rg);
1059 const auto __last = ranges::end(__rg);
1060 for (; __first != __last; ++__first)
1061 emplace_back(*__first);
1062 }
1063 }
1064#endif
1065
1066 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1067 iterator
1068 begin() _GLIBCXX_NOEXCEPT
1069 { return iterator(this->_M_impl._M_start._M_p, 0); }
1070
1071 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1072 const_iterator
1073 begin() const _GLIBCXX_NOEXCEPT
1074 { return const_iterator(this->_M_impl._M_start._M_p, 0); }
1075
1076 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1077 iterator
1078 end() _GLIBCXX_NOEXCEPT
1079 { return this->_M_impl._M_finish; }
1080
1081 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1082 const_iterator
1083 end() const _GLIBCXX_NOEXCEPT
1084 { return this->_M_impl._M_finish; }
1085
1086 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1087 reverse_iterator
1088 rbegin() _GLIBCXX_NOEXCEPT
1089 { return reverse_iterator(end()); }
1090
1091 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1092 const_reverse_iterator
1093 rbegin() const _GLIBCXX_NOEXCEPT
1094 { return const_reverse_iterator(end()); }
1095
1096 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1097 reverse_iterator
1098 rend() _GLIBCXX_NOEXCEPT
1099 { return reverse_iterator(begin()); }
1100
1101 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1102 const_reverse_iterator
1103 rend() const _GLIBCXX_NOEXCEPT
1104 { return const_reverse_iterator(begin()); }
1105
1106#if __cplusplus >= 201103L
1107 [[__nodiscard__]] _GLIBCXX20_CONSTEXPR
1108 const_iterator
1109 cbegin() const noexcept
1110 { return const_iterator(this->_M_impl._M_start._M_p, 0); }
1111
1112 [[__nodiscard__]] _GLIBCXX20_CONSTEXPR
1113 const_iterator
1114 cend() const noexcept
1115 { return this->_M_impl._M_finish; }
1116
1117 [[__nodiscard__]] _GLIBCXX20_CONSTEXPR
1118 const_reverse_iterator
1119 crbegin() const noexcept
1120 { return const_reverse_iterator(end()); }
1121
1122 [[__nodiscard__]] _GLIBCXX20_CONSTEXPR
1123 const_reverse_iterator
1124 crend() const noexcept
1125 { return const_reverse_iterator(begin()); }
1126#endif
1127
1128 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1129 size_type
1130 size() const _GLIBCXX_NOEXCEPT
1131 { return size_type(end() - begin()); }
1132
1133 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1134 size_type
1135 max_size() const _GLIBCXX_NOEXCEPT
1136 {
1137 const size_type __isize =
1138 __gnu_cxx::__numeric_traits<difference_type>::__max
1139 - int(_S_word_bit) + 1;
1140 const size_type __asize
1141 = _Bit_alloc_traits::max_size(_M_get_Bit_allocator());
1142 return (__asize <= __isize / int(_S_word_bit)
1143 ? __asize * int(_S_word_bit) : __isize);
1144 }
1145
1146 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1147 size_type
1148 capacity() const _GLIBCXX_NOEXCEPT
1149 { return size_type(const_iterator(this->_M_impl._M_end_addr(), 0)
1150 - begin()); }
1151
1152 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1153 bool
1154 empty() const _GLIBCXX_NOEXCEPT
1155 { return begin() == end(); }
1156
1157 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1158 reference
1159 operator[](size_type __n)
1160 {
1161 __glibcxx_requires_subscript(__n);
1162 return _Bit_reference (this->_M_impl._M_start._M_p
1163 + __n / int(_S_word_bit),
1164 1UL << __n % int(_S_word_bit));
1165 }
1166
1167 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1168 const_reference
1169 operator[](size_type __n) const
1170 {
1171 __glibcxx_requires_subscript(__n);
1172 return _Bit_reference (this->_M_impl._M_start._M_p
1173 + __n / int(_S_word_bit),
1174 1UL << __n % int(_S_word_bit));
1175 }
1176
1177 protected:
1178 _GLIBCXX20_CONSTEXPR
1179 void
1180 _M_range_check(size_type __n) const
1181 {
1182 if (__n >= this->size())
1183 __throw_out_of_range_fmt(__N("vector<bool>::_M_range_check: __n "
1184 "(which is %zu) >= this->size() "
1185 "(which is %zu)"),
1186 __n, this->size());
1187 }
1188
1189 public:
1190 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1191 reference
1192 at(size_type __n)
1193 {
1194 _M_range_check(__n);
1195 return (*this)[__n];
1196 }
1197
1198 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1199 const_reference
1200 at(size_type __n) const
1201 {
1202 _M_range_check(__n);
1203 return (*this)[__n];
1204 }
1205
1206 _GLIBCXX20_CONSTEXPR
1207 void
1208 reserve(size_type __n)
1209 {
1210 if (__n > max_size())
1211 __throw_length_error(__N("vector::reserve"));
1212 if (capacity() < __n)
1213 _M_reallocate(__n);
1214 }
1215
1216 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1217 reference
1218 front()
1219 {
1220 __glibcxx_requires_nonempty();
1221 return *begin();
1222 }
1223
1224 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1225 const_reference
1226 front() const
1227 {
1228 __glibcxx_requires_nonempty();
1229 return *begin();
1230 }
1231
1232 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1233 reference
1234 back()
1235 {
1236 __glibcxx_requires_nonempty();
1237 return *(end() - 1);
1238 }
1239
1240 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1241 const_reference
1242 back() const
1243 {
1244 __glibcxx_requires_nonempty();
1245 return *(end() - 1);
1246 }
1247
1248 _GLIBCXX20_CONSTEXPR
1249 void
1250 push_back(bool __x)
1251 {
1252 if (this->_M_impl._M_finish._M_p != this->_M_impl._M_end_addr())
1253 *this->_M_impl._M_finish++ = __x;
1254 else
1255 _M_insert_aux(end(), __x);
1256 }
1257
1258 _GLIBCXX20_CONSTEXPR
1259 void
1260 swap(vector& __x) _GLIBCXX_NOEXCEPT
1261 {
1262#if __cplusplus >= 201103L
1263 __glibcxx_assert(_Bit_alloc_traits::propagate_on_container_swap::value
1264 || _M_get_Bit_allocator() == __x._M_get_Bit_allocator());
1265#endif
1266 this->_M_impl._M_swap_data(__x._M_impl);
1267 _Bit_alloc_traits::_S_on_swap(_M_get_Bit_allocator(),
1268 __x._M_get_Bit_allocator());
1269 }
1270
1271 // [23.2.5]/1, third-to-last entry in synopsis listing
1272 _GLIBCXX26_DEPRECATED_SUGGEST("swap' invoked via ADL or 'std::ranges::swap")
1273 _GLIBCXX20_CONSTEXPR
1274 static void
1275 swap(reference __x, reference __y) _GLIBCXX_NOEXCEPT
1276 {
1277 bool __tmp = __x;
1278 __x = __y;
1279 __y = __tmp;
1280 }
1281
1282 _GLIBCXX20_CONSTEXPR
1283 iterator
1284#if __cplusplus >= 201103L
1285 insert(const_iterator __position, const bool& __x)
1286#else
1287 insert(iterator __position, const bool& __x)
1288#endif
1289 {
1290 const difference_type __n = __position - begin();
1291 if (this->_M_impl._M_finish._M_p != this->_M_impl._M_end_addr()
1292 && __position == end())
1293 *this->_M_impl._M_finish++ = __x;
1294 else
1295 _M_insert_aux(__position._M_const_cast(), __x);
1296 return begin() + __n;
1297 }
1298
1299#if _GLIBCXX_USE_DEPRECATED
1300 _GLIBCXX_DEPRECATED_SUGGEST("insert(position, false)")
1301 iterator
1302 insert(const_iterator __position)
1303 { return this->insert(__position._M_const_cast(), false); }
1304#endif
1305
1306#if __cplusplus >= 201103L
1307 template<typename _InputIterator,
1308 typename = std::_RequireInputIter<_InputIterator>>
1309 _GLIBCXX20_CONSTEXPR
1310 iterator
1311 insert(const_iterator __position,
1312 _InputIterator __first, _InputIterator __last)
1313 {
1314 difference_type __offset = __position - cbegin();
1315 _M_insert_range(__position._M_const_cast(),
1316 __first, __last,
1317 std::__iterator_category(__first));
1318 return begin() + __offset;
1319 }
1320#else
1321 template<typename _InputIterator>
1322 void
1323 insert(iterator __position,
1324 _InputIterator __first, _InputIterator __last)
1325 {
1326 // Check whether it's an integral type. If so, it's not an iterator.
1327 typedef typename std::__is_integer<_InputIterator>::__type _Integral;
1328 _M_insert_dispatch(__position, __first, __last, _Integral());
1329 }
1330#endif
1331
1332#if __cplusplus >= 201103L
1333 _GLIBCXX20_CONSTEXPR
1334 iterator
1335 insert(const_iterator __position, size_type __n, const bool& __x)
1336 {
1337 difference_type __offset = __position - cbegin();
1338 _M_fill_insert(__position._M_const_cast(), __n, __x);
1339 return begin() + __offset;
1340 }
1341#else
1342 void
1343 insert(iterator __position, size_type __n, const bool& __x)
1344 { _M_fill_insert(__position, __n, __x); }
1345#endif
1346
1347#if __cplusplus >= 201103L
1348 _GLIBCXX20_CONSTEXPR
1349 iterator
1350 insert(const_iterator __p, initializer_list<bool> __l)
1351 { return this->insert(__p, __l.begin(), __l.end()); }
1352#endif
1353
1354#if __glibcxx_containers_ranges // C++ >= 23
1355 /**
1356 * @brief Insert a range into the vector.
1357 * @param __rg A range of values that are convertible to `bool`.
1358 * @return An iterator that points to the first new element inserted,
1359 * or to `__pos` if `__rg` is an empty range.
1360 * @pre `__rg` and `*this` do not overlap.
1361 * @since C++23
1362 */
1363 template<__detail::__container_compatible_range<bool> _Rg>
1364 constexpr iterator
1365 insert_range(const_iterator __pos, _Rg&& __rg)
1366 {
1367 if constexpr (ranges::forward_range<_Rg> || ranges::sized_range<_Rg>)
1368 {
1369 if (auto __n = size_type(ranges::distance(__rg)))
1370 {
1371 if (capacity() - size() >= __n)
1372 {
1373 std::copy_backward(__pos._M_const_cast(), end(),
1374 this->_M_impl._M_finish
1375 + difference_type(__n));
1376 ranges::copy(__rg, __pos._M_const_cast());
1377 this->_M_impl._M_finish += difference_type(__n);
1378 return __pos._M_const_cast();
1379 }
1380 else
1381 {
1382 const size_type __len =
1383 _M_check_len(__n, "vector<bool>::insert_range");
1384 const iterator __begin = begin(), __end = end();
1385 _Bit_pointer __q = this->_M_allocate(__len);
1386 iterator __start(std::__addressof(*__q), 0);
1387 iterator __i = _M_copy_aligned(__begin,
1388 __pos._M_const_cast(),
1389 __start);
1390 iterator __j = ranges::copy(__rg, __i).out;
1391 iterator __finish = std::copy(__pos._M_const_cast(),
1392 __end, __j);
1393 this->_M_deallocate();
1394 this->_M_impl._M_end_of_storage = __q + _S_nword(__len);
1395 this->_M_impl._M_start = __start;
1396 this->_M_impl._M_finish = __finish;
1397 return __i;
1398 }
1399 }
1400 else
1401 return __pos._M_const_cast();
1402 }
1403 else
1404 return insert_range(__pos,
1405 vector(from_range, __rg, get_allocator()));
1406 }
1407
1408 /**
1409 * @brief Append a range at the end of the vector.
1410 * @since C++23
1411 */
1412 template<__detail::__container_compatible_range<bool> _Rg>
1413 constexpr void
1414 append_range(_Rg&& __rg)
1415 {
1416 // N.B. __rg may overlap with *this, so we must copy from __rg before
1417 // existing elements or iterators referring to *this are invalidated.
1418 // e.g. in v.append_range(views::concat(v, foo)) rg overlaps v.
1419 if constexpr (ranges::forward_range<_Rg> || ranges::sized_range<_Rg>)
1420 {
1421 const auto __n = size_type(ranges::distance(__rg));
1422
1423 // If there is no existing storage, there are no iterators that
1424 // can be referring to our storage, so it's safe to allocate now.
1425 if (capacity() == 0)
1426 reserve(__n);
1427
1428 const auto __sz = size();
1429 const auto __capacity = capacity();
1430 if ((__capacity - __sz) >= __n)
1431 {
1432 this->_M_impl._M_finish
1433 = ranges::copy(std::forward<_Rg>(__rg), end()).out;
1434 return;
1435 }
1436
1437 vector __tmp(get_allocator());
1438 __tmp.reserve(_M_check_len(__n, "vector::append_range"));
1439 __tmp._M_impl._M_finish
1440 = _M_copy_aligned(cbegin(), cend(), __tmp.begin());
1441 __tmp._M_impl._M_finish
1442 = ranges::copy(std::forward<_Rg>(__rg), __tmp.end()).out;
1443 swap(__tmp);
1444 }
1445 else
1446 {
1447 auto __first = ranges::begin(__rg);
1448 const auto __last = ranges::end(__rg);
1449
1450 // Fill up to the end of current capacity.
1451 for (auto __free = capacity() - size();
1452 __first != __last && __free > 0;
1453 ++__first, (void) --__free)
1454 emplace_back(*__first);
1455
1456 if (__first == __last)
1457 return;
1458
1459 // Copy the rest of the range to a new vector.
1460 ranges::subrange __rest(std::move(__first), __last);
1461 vector __tmp(from_range, __rest, get_allocator());
1462 insert(end(), __tmp.begin(), __tmp.end());
1463 }
1464 }
1465#endif // containers_ranges
1466
1467 _GLIBCXX20_CONSTEXPR
1468 void
1469 pop_back()
1470 { --this->_M_impl._M_finish; }
1471
1472 _GLIBCXX20_CONSTEXPR
1473 iterator
1474#if __cplusplus >= 201103L
1475 erase(const_iterator __position)
1476#else
1477 erase(iterator __position)
1478#endif
1479 { return _M_erase(__position._M_const_cast()); }
1480
1481 _GLIBCXX20_CONSTEXPR
1482 iterator
1483#if __cplusplus >= 201103L
1484 erase(const_iterator __first, const_iterator __last)
1485#else
1486 erase(iterator __first, iterator __last)
1487#endif
1488 { return _M_erase(__first._M_const_cast(), __last._M_const_cast()); }
1489
1490 _GLIBCXX20_CONSTEXPR
1491 void
1492 resize(size_type __new_size, bool __x = bool())
1493 {
1494 if (__new_size < size())
1495 _M_erase_at_end(begin() + difference_type(__new_size));
1496 else
1497 insert(end(), __new_size - size(), __x);
1498 }
1499
1500#if __cplusplus >= 201103L
1501 _GLIBCXX20_CONSTEXPR
1502 void
1503 shrink_to_fit()
1504 { _M_shrink_to_fit(); }
1505#endif
1506
1507 _GLIBCXX20_CONSTEXPR
1508 void
1509 flip() _GLIBCXX_NOEXCEPT
1510 {
1511 _Bit_type * const __end = this->_M_impl._M_end_addr();
1512 for (_Bit_type * __p = this->_M_impl._M_start._M_p; __p != __end; ++__p)
1513 *__p = ~*__p;
1514 }
1515
1516 _GLIBCXX20_CONSTEXPR
1517 void
1518 clear() _GLIBCXX_NOEXCEPT
1519 { _M_erase_at_end(begin()); }
1520
1521#if __cplusplus >= 201103L
1522 template<typename... _Args>
1523#if __cplusplus > 201402L
1524 _GLIBCXX20_CONSTEXPR
1525 reference
1526#else
1527 void
1528#endif
1529 emplace_back(_Args&&... __args)
1530 {
1531 push_back(bool(std::forward<_Args>(__args)...));
1532#if __cplusplus > 201402L
1533 return back();
1534#endif
1535 }
1536
1537 template<typename... _Args>
1538 _GLIBCXX20_CONSTEXPR
1539 iterator
1540 emplace(const_iterator __pos, _Args&&... __args)
1541 { return insert(__pos, bool(std::forward<_Args>(__args)...)); }
1542#endif
1543
1544 protected:
1545 // Precondition: __first._M_offset == 0 && __result._M_offset == 0.
1546 _GLIBCXX20_CONSTEXPR
1547 iterator
1548 _M_copy_aligned(const_iterator __first, const_iterator __last,
1549 iterator __result)
1550 {
1551 _Bit_type* __q = std::copy(__first._M_p, __last._M_p, __result._M_p);
1552 return std::copy(const_iterator(__last._M_p, 0), __last,
1553 iterator(__q, 0));
1554 }
1555
1556 _GLIBCXX20_CONSTEXPR
1557 void
1558 _M_initialize(size_type __n)
1559 {
1560 if (__n)
1561 {
1562 _Bit_pointer __q = this->_M_allocate(__n);
1563 this->_M_impl._M_end_of_storage = __q + _S_nword(__n);
1564 iterator __start = iterator(std::__addressof(*__q), 0);
1565 this->_M_impl._M_start = __start;
1566 this->_M_impl._M_finish = __start + difference_type(__n);
1567 }
1568 }
1569
1570 _GLIBCXX20_CONSTEXPR
1571 void
1572 _M_initialize_value(bool __x) _GLIBCXX_NOEXCEPT
1573 {
1574 if (_Bit_type* __p = this->_M_impl._M_start._M_p)
1575 __fill_bvector_n(__p, this->_M_impl._M_end_addr() - __p, __x);
1576 }
1577
1578 _GLIBCXX20_CONSTEXPR
1579 void
1580 _M_reallocate(size_type __n);
1581
1582#if __cplusplus >= 201103L
1583 _GLIBCXX20_CONSTEXPR
1584 bool
1585 _M_shrink_to_fit();
1586#endif
1587
1588#if __cplusplus < 201103L
1589 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1590 // 438. Ambiguity in the "do the right thing" clause
1591 template<typename _Integer>
1592 void
1593 _M_initialize_dispatch(_Integer __n, _Integer __x, __true_type)
1594 {
1595 _M_initialize(static_cast<size_type>(__n));
1596 _M_initialize_value(__x);
1597 }
1598
1599 template<typename _InputIterator>
1600 void
1601 _M_initialize_dispatch(_InputIterator __first, _InputIterator __last,
1602 __false_type)
1603 { _M_initialize_range(__first, __last,
1604 std::__iterator_category(__first)); }
1605#endif
1606
1607 template<typename _InputIterator>
1608 _GLIBCXX20_CONSTEXPR
1609 void
1610 _M_initialize_range(_InputIterator __first, _InputIterator __last,
1611 std::input_iterator_tag)
1612 {
1613 for (; __first != __last; ++__first)
1614 push_back(*__first);
1615 }
1616
1617 template<typename _ForwardIterator>
1618 _GLIBCXX20_CONSTEXPR
1619 void
1620 _M_initialize_range(_ForwardIterator __first, _ForwardIterator __last,
1621 std::forward_iterator_tag)
1622 {
1623 const size_type __n = std::distance(__first, __last);
1624 _M_initialize(__n);
1625 std::copy(__first, __last, begin());
1626 }
1627
1628#if __cplusplus < 201103L
1629 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1630 // 438. Ambiguity in the "do the right thing" clause
1631 template<typename _Integer>
1632 void
1633 _M_assign_dispatch(_Integer __n, _Integer __val, __true_type)
1634 { _M_fill_assign(__n, __val); }
1635
1636 template<class _InputIterator>
1637 void
1638 _M_assign_dispatch(_InputIterator __first, _InputIterator __last,
1639 __false_type)
1640 { _M_assign_aux(__first, __last, std::__iterator_category(__first)); }
1641#endif
1642
1643 _GLIBCXX20_CONSTEXPR
1644 void
1645 _M_fill_assign(size_t __n, bool __x)
1646 {
1647 if (__n > size())
1648 {
1649 _M_initialize_value(__x);
1650 insert(end(), __n - size(), __x);
1651 }
1652 else
1653 {
1654 _M_erase_at_end(begin() + __n);
1655 _M_initialize_value(__x);
1656 }
1657 }
1658
1659 template<typename _InputIterator>
1660 _GLIBCXX20_CONSTEXPR
1661 void
1662 _M_assign_aux(_InputIterator __first, _InputIterator __last,
1663 std::input_iterator_tag)
1664 {
1665 iterator __cur = begin();
1666 for (; __first != __last && __cur != end(); ++__cur, (void)++__first)
1667 *__cur = *__first;
1668 if (__first == __last)
1669 _M_erase_at_end(__cur);
1670 else
1671 insert(end(), __first, __last);
1672 }
1673
1674 template<typename _ForwardIterator>
1675 _GLIBCXX20_CONSTEXPR
1676 void
1677 _M_assign_aux(_ForwardIterator __first, _ForwardIterator __last,
1678 std::forward_iterator_tag)
1679 {
1680 const size_type __len = std::distance(__first, __last);
1681 if (__len < size())
1682 _M_erase_at_end(std::copy(__first, __last, begin()));
1683 else
1684 {
1685 _ForwardIterator __mid = __first;
1686 std::advance(__mid, size());
1687 std::copy(__first, __mid, begin());
1688 insert(end(), __mid, __last);
1689 }
1690 }
1691
1692#if __cplusplus < 201103L
1693 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1694 // 438. Ambiguity in the "do the right thing" clause
1695 template<typename _Integer>
1696 void
1697 _M_insert_dispatch(iterator __pos, _Integer __n, _Integer __x,
1698 __true_type)
1699 { _M_fill_insert(__pos, __n, __x); }
1700
1701 template<typename _InputIterator>
1702 void
1703 _M_insert_dispatch(iterator __pos,
1704 _InputIterator __first, _InputIterator __last,
1705 __false_type)
1706 { _M_insert_range(__pos, __first, __last,
1707 std::__iterator_category(__first)); }
1708#endif
1709
1710 _GLIBCXX20_CONSTEXPR
1711 void
1712 _M_fill_insert(iterator __position, size_type __n, bool __x);
1713
1714 template<typename _InputIterator>
1715 _GLIBCXX20_CONSTEXPR
1716 void
1717 _M_insert_range(iterator __pos, _InputIterator __first,
1718 _InputIterator __last, std::input_iterator_tag)
1719 {
1720 for (; __first != __last; ++__first)
1721 {
1722 __pos = insert(__pos, *__first);
1723 ++__pos;
1724 }
1725 }
1726
1727 template<typename _ForwardIterator>
1728 _GLIBCXX20_CONSTEXPR
1729 void
1730 _M_insert_range(iterator __position, _ForwardIterator __first,
1731 _ForwardIterator __last, std::forward_iterator_tag);
1732
1733 _GLIBCXX20_CONSTEXPR
1734 void
1735 _M_insert_aux(iterator __position, bool __x);
1736
1737 _GLIBCXX20_CONSTEXPR
1738 size_type
1739 _M_check_len(size_type __n, const char* __s) const
1740 {
1741 if (max_size() - size() < __n)
1742 __throw_length_error(__N(__s));
1743
1744 const size_type __len = size() + std::max(size(), __n);
1745 return (__len < size() || __len > max_size()) ? max_size() : __len;
1746 }
1747
1748 _GLIBCXX20_CONSTEXPR
1749 void
1750 _M_erase_at_end(iterator __pos)
1751 { this->_M_impl._M_finish = __pos; }
1752
1753 _GLIBCXX20_CONSTEXPR
1754 iterator
1755 _M_erase(iterator __pos);
1756
1757 _GLIBCXX20_CONSTEXPR
1758 iterator
1759 _M_erase(iterator __first, iterator __last);
1760
1761 protected:
1762 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1763 // DR 464. Suggestion for new member functions in standard containers.
1764 // N.B. DR 464 says nothing about vector<bool> but we need something
1765 // here due to the using-declaration in __gnu_debug::vector.
1766 // vector class.
1767#if __cplusplus >= 201103L
1768 void data() = delete;
1769#else
1770 void data() { }
1771#endif
1772 };
1773
1774_GLIBCXX_END_NAMESPACE_CONTAINER
1775
1776 // Fill a partial word.
1777 _GLIBCXX20_CONSTEXPR
1778 inline void
1779 __fill_bvector(_Bit_type* __v, unsigned int __first, unsigned int __last,
1780 bool __x) _GLIBCXX_NOEXCEPT
1781 {
1782 const _Bit_type __fmask = ~0ul << __first;
1783 const _Bit_type __lmask = ~0ul >> (_S_word_bit - __last);
1784 const _Bit_type __mask = __fmask & __lmask;
1785
1786 if (__x)
1787 *__v |= __mask;
1788 else
1789 *__v &= ~__mask;
1790 }
1791
1792 // Fill N full words, as if using memset, but usable in constant expressions.
1793 __attribute__((__nonnull__))
1794 _GLIBCXX20_CONSTEXPR
1795 inline void
1796 __fill_bvector_n(_Bit_type* __p, size_t __n, bool __x) _GLIBCXX_NOEXCEPT
1797 {
1798#if __cpp_lib_is_constant_evaluated
1799 if (std::is_constant_evaluated())
1800 {
1801 for (size_t __i = 0; __i < __n; ++__i)
1802 __p[__i] = __x ? ~0ul : 0ul;
1803 return;
1804 }
1805#endif
1806 __builtin_memset(__p, __x ? ~0 : 0, __n * sizeof(_Bit_type));
1807 }
1808
1809
1810 _GLIBCXX20_CONSTEXPR
1811 inline void
1812 __fill_a1(_GLIBCXX_STD_C::_Bit_iterator __first,
1813 _GLIBCXX_STD_C::_Bit_iterator __last, const bool& __x)
1814 {
1815 if (__first._M_p != __last._M_p)
1816 {
1817 _Bit_type* __first_p = __first._M_p;
1818 if (__first._M_offset != 0)
1819 __fill_bvector(__first_p++, __first._M_offset, _S_word_bit, __x);
1820
1821 __fill_bvector_n(__first_p, __last._M_p - __first_p, __x);
1822
1823 if (__last._M_offset != 0)
1824 __fill_bvector(__last._M_p, 0, __last._M_offset, __x);
1825 }
1826 else if (__first._M_offset != __last._M_offset)
1827 __fill_bvector(__first._M_p, __first._M_offset, __last._M_offset, __x);
1828 }
1829
1830#if __cplusplus >= 201103L
1831 // DR 1182.
1832 /// std::hash specialization for vector<bool>.
1833 template<typename _Alloc>
1834 struct hash<_GLIBCXX_STD_C::vector<bool, _Alloc>>
1835 : public __hash_base<size_t, _GLIBCXX_STD_C::vector<bool, _Alloc>>
1836 {
1837 size_t
1838 operator()(const _GLIBCXX_STD_C::vector<bool, _Alloc>&) const noexcept;
1839 };
1840#endif // C++11
1841
1842_GLIBCXX_END_NAMESPACE_VERSION
1843} // namespace std
1844
1845#endif
initializer_list
functional_hash.h
std::chrono::operator<=
constexpr bool operator<=(const duration< _Rep1, _Period1 > &__lhs, const duration< _Rep2, _Period2 > &__rhs)
Definition chrono.h:863
std::chrono::operator>=
constexpr bool operator>=(const duration< _Rep1, _Period1 > &__lhs, const duration< _Rep2, _Period2 > &__rhs)
Definition chrono.h:877
std::chrono::operator<
constexpr bool operator<(const duration< _Rep1, _Period1 > &__lhs, const duration< _Rep2, _Period2 > &__rhs)
Definition chrono.h:830
std::chrono::operator>
constexpr bool operator>(const duration< _Rep1, _Period1 > &__lhs, const duration< _Rep2, _Period2 > &__rhs)
Definition chrono.h:870
std::to_address
constexpr _Tp * to_address(_Tp *__ptr) noexcept
Obtain address referenced by a pointer to an object.
Definition ptr_traits.h:234
std::true_type
__bool_constant< true > true_type
The type used as a compile-time boolean with true value.
Definition type_traits:119
std::false_type
__bool_constant< false > false_type
The type used as a compile-time boolean with false value.
Definition type_traits:122
std::move
constexpr std::remove_reference< _Tp >::type && move(_Tp &&__t) noexcept
Convert a value to an rvalue.
Definition move.h:138
std::__addressof
constexpr _Tp * __addressof(_Tp &__r) noexcept
Same as C++11 std::addressof.
Definition move.h:52
std::forward
constexpr _Tp && forward(typename std::remove_reference< _Tp >::type &__t) noexcept
Forward an lvalue.
Definition move.h:72
std::max
constexpr const _Tp & max(const _Tp &, const _Tp &)
This does what you think it does.
Definition stl_algobase.h:256
std
ISO C++ entities toplevel namespace is std.
std::distance
constexpr iterator_traits< _InputIterator >::difference_type distance(_InputIterator __first, _InputIterator __last)
A generalization of pointer arithmetic.
Definition stl_iterator_base_funcs.h:178
std::advance
constexpr void advance(_InputIterator &__i, _Distance __n)
A generalization of pointer arithmetic.
Definition stl_iterator_base_funcs.h:271
std::initializer_list
initializer_list
Definition initializer_list:53
std::hash
Primary class template hash.
Definition functional_hash.h:106
std::is_same
is_same
Definition type_traits:1649
__gnu_cxx::__alloc_traits< _Bit_alloc_type >::is_always_equal
typename __detected_or_t< is_empty< _Bit_alloc_type >, __equal, _Bit_alloc_type >::type is_always_equal
Definition bits/alloc_traits.h:372
std::ranges::subrange
The ranges::subrange class template.
Definition ranges_util.h:270
std::reverse_iterator
Definition bits/stl_iterator.h:137
std::input_iterator_tag
Marking input iterators.
Definition stl_iterator_base_types.h:97
std::forward_iterator_tag
Forward iterators support a superset of input iterator operations.
Definition stl_iterator_base_types.h:103
std::random_access_iterator_tag
Random-access iterators support a superset of bidirectional iterator operations.
Definition stl_iterator_base_types.h:111
std::iterator< std::random_access_iterator_tag, bool >::difference_type
ptrdiff_t difference_type
Definition stl_iterator_base_types.h:141
std::iterator< std::random_access_iterator_tag, bool >::reference
bool & reference
Definition stl_iterator_base_types.h:145
std::vector
A standard container which offers fixed time access to individual elements in any order.
Definition stl_vector.h:511
std::vector::insert
constexpr iterator insert(const_iterator __position, const value_type &__x)
Inserts given value into vector before specified iterator.
Definition vector.tcc:134
std::vector::push_back
constexpr void push_back(const value_type &__x)
Add data to the end of the vector.
Definition stl_vector.h:1458
std::vector::rbegin
constexpr reverse_iterator rbegin() noexcept
Definition stl_vector.h:1080
std::vector::end
constexpr iterator end() noexcept
Definition stl_vector.h:1060
std::vector::vector
vector()=default
Creates a vector with no elements.
std::vector::emplace
constexpr iterator emplace(const_iterator __position, _Args &&... __args)
Inserts an object in vector before specified iterator.
Definition stl_vector.h:1523
std::vector::begin
constexpr iterator begin() noexcept
Definition stl_vector.h:1040
std::vector::capacity
constexpr size_type capacity() const noexcept
Definition stl_vector.h:1250
std::vector::~vector
constexpr ~vector() noexcept
Definition stl_vector.h:842
std::vector::assign
constexpr void assign(size_type __n, const value_type &__val)
Assigns a given value to a vector.
Definition stl_vector.h:917
std::vector::swap
constexpr void swap(vector &__x) noexcept
Swaps data with another vector.
Definition stl_vector.h:1887
std::vector::data
constexpr _Tp * data() noexcept
Definition stl_vector.h:1437
std::vector::pop_back
constexpr void pop_back() noexcept
Removes last element.
Definition stl_vector.h:1499
std::vector::reserve
constexpr void reserve(size_type __n)
Attempt to preallocate enough memory for specified number of elements.
Definition vector.tcc:71
std::vector::at
constexpr reference at(size_type __n)
Provides access to the data contained in the vector.
Definition stl_vector.h:1355
std::vector::resize
constexpr void resize(size_type __new_size)
Resizes the vector to the specified number of elements.
Definition stl_vector.h:1185
std::vector::_M_range_check
constexpr void _M_range_check(size_type __n) const
Safety check used only from at().
Definition stl_vector.h:1332
std::vector::front
constexpr reference front() noexcept
Definition stl_vector.h:1386
std::vector::erase
constexpr iterator erase(const_iterator __position)
Remove element at given position.
Definition stl_vector.h:1835
std::vector::empty
constexpr bool empty() const noexcept
Definition stl_vector.h:1265
std::vector::rend
constexpr reverse_iterator rend() noexcept
Definition stl_vector.h:1100
std::vector::crbegin
constexpr const_reverse_iterator crbegin() const noexcept
Definition stl_vector.h:1141
std::vector::cbegin
constexpr const_iterator cbegin() const noexcept
Definition stl_vector.h:1121
std::vector::clear
constexpr void clear() noexcept
Definition stl_vector.h:1906
std::vector::get_allocator
constexpr allocator_type get_allocator() const noexcept
Get a copy of the memory allocation object.
Definition stl_vector.h:317
std::vector::size
constexpr size_type size() const noexcept
Definition stl_vector.h:1159
std::vector::operator=
constexpr vector & operator=(const vector &__x)
Vector assignment operator.
Definition vector.tcc:210
std::vector::back
constexpr reference back() noexcept
Definition stl_vector.h:1410
std::vector::crend
constexpr const_reverse_iterator crend() const noexcept
Definition stl_vector.h:1151
std::vector::cend
constexpr const_iterator cend() const noexcept
Definition stl_vector.h:1131
std::vector::operator[]
constexpr reference operator[](size_type __n) noexcept
Subscript access to the data contained in the vector.
Definition stl_vector.h:1303
std::vector::shrink_to_fit
constexpr void shrink_to_fit()
Definition stl_vector.h:1240
std::vector::max_size
constexpr size_type max_size() const noexcept
Definition stl_vector.h:1170
__gnu_cxx::__alloc_traits< _Bit_alloc_type >::allocate
static constexpr pointer allocate(_Bit_alloc_type &__a, size_type __n)
__gnu_cxx::__alloc_traits< _Bit_alloc_type >::deallocate
static constexpr void deallocate(_Bit_alloc_type &__a, pointer __p, size_type __n)
__gnu_cxx::__alloc_traits< _Bit_alloc_type >::max_size
static constexpr size_type max_size(const _Bit_alloc_type &__a) noexcept
std::assignable_from
[concept.assignable], concept assignable_from
Definition concepts:149
std::ranges::sized_range
[range.sized] The sized_range concept.
Definition ranges_base.h:553
std::ranges::forward_range
A range for which ranges::begin returns a forward iterator.
Definition ranges_base.h:624