1 // vector<bool> specialization -*- C++ -*-
3 // Copyright (C) 2001-2023 Free Software Foundation, Inc.
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)
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.
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.
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/>.
28 * Hewlett-Packard Company
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.
39 * Copyright (c) 1996-1999
40 * Silicon Graphics Computer Systems, Inc.
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.
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}
56 #ifndef _STL_BVECTOR_H
57 #define _STL_BVECTOR_H 1
59 #ifndef _GLIBCXX_ALWAYS_INLINE
60 #define _GLIBCXX_ALWAYS_INLINE inline __attribute__((__always_inline__))
63 #if __cplusplus >= 201103L
64 #include <initializer_list>
65 #include <bits/functional_hash.h>
68 namespace std
_GLIBCXX_VISIBILITY(default)
70 _GLIBCXX_BEGIN_NAMESPACE_VERSION
72 typedef unsigned long _Bit_type
;
73 enum { _S_word_bit
= int(__CHAR_BIT__
* sizeof(_Bit_type
)) };
75 __attribute__((__nonnull__
))
78 __fill_bvector_n(_Bit_type
*, size_t, bool) _GLIBCXX_NOEXCEPT
;
80 _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
88 _Bit_reference(_Bit_type
* __x
, _Bit_type __y
)
89 : _M_p(__x
), _M_mask(__y
) { }
92 _Bit_reference() _GLIBCXX_NOEXCEPT
: _M_p(0), _M_mask(0) { }
94 #if __cplusplus >= 201103L
95 _Bit_reference(const _Bit_reference
&) = default;
98 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
99 operator bool() const _GLIBCXX_NOEXCEPT
100 { return !!(*_M_p
& _M_mask
); }
104 operator=(bool __x
) _GLIBCXX_NOEXCEPT
113 #if __cplusplus > 202002L
114 constexpr const _Bit_reference
&
115 operator=(bool __x
) const noexcept
127 operator=(const _Bit_reference
& __x
) _GLIBCXX_NOEXCEPT
128 { return *this = bool(__x
); }
130 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
132 operator==(const _Bit_reference
& __x
) const
133 { return bool(*this) == bool(__x
); }
135 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
137 operator<(const _Bit_reference
& __x
) const
138 { return !bool(*this) && bool(__x
); }
142 flip() _GLIBCXX_NOEXCEPT
143 { *_M_p
^= _M_mask
; }
145 #if __cplusplus >= 201103L
148 swap(_Bit_reference __x
, _Bit_reference __y
) noexcept
157 swap(_Bit_reference __x
, bool& __y
) noexcept
166 swap(bool& __x
, _Bit_reference __y
) noexcept
175 // Ignore warnings about std::iterator.
176 #pragma GCC diagnostic push
177 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
178 struct _Bit_iterator_base
179 : public std::iterator
<std::random_access_iterator_tag
, bool>
182 unsigned int _M_offset
;
184 _GLIBCXX20_CONSTEXPR _GLIBCXX_ALWAYS_INLINE
186 _M_assume_normalized() const
188 #if __has_attribute(__assume__) && !defined(__clang__)
189 unsigned int __ofst
= _M_offset
;
190 __attribute__ ((__assume__ (__ofst
< unsigned(_S_word_bit
))));
195 _Bit_iterator_base(_Bit_type
* __x
, unsigned int __y
)
196 : _M_p(__x
), _M_offset(__y
) { }
202 _M_assume_normalized();
203 if (_M_offset
++ == int(_S_word_bit
) - 1)
214 _M_assume_normalized();
215 if (_M_offset
-- == 0)
217 _M_offset
= int(_S_word_bit
) - 1;
224 _M_incr(ptrdiff_t __i
)
226 _M_assume_normalized();
227 difference_type __n
= __i
+ _M_offset
;
228 _M_p
+= __n
/ int(_S_word_bit
);
229 __n
= __n
% int(_S_word_bit
);
232 __n
+= int(_S_word_bit
);
235 _M_offset
= static_cast<unsigned int>(__n
);
239 friend _GLIBCXX20_CONSTEXPR
bool
240 operator==(const _Bit_iterator_base
& __x
, const _Bit_iterator_base
& __y
)
242 __x
._M_assume_normalized();
243 __y
._M_assume_normalized();
244 return __x
._M_p
== __y
._M_p
&& __x
._M_offset
== __y
._M_offset
;
247 #if __cpp_lib_three_way_comparison
249 friend constexpr strong_ordering
250 operator<=>(const _Bit_iterator_base
& __x
, const _Bit_iterator_base
& __y
)
253 __x
._M_assume_normalized();
254 __y
._M_assume_normalized();
255 if (const auto __cmp
= __x
._M_p
<=> __y
._M_p
; __cmp
!= 0)
257 return __x
._M_offset
<=> __y
._M_offset
;
262 operator<(const _Bit_iterator_base
& __x
, const _Bit_iterator_base
& __y
)
264 __x
._M_assume_normalized();
265 __y
._M_assume_normalized();
266 return __x
._M_p
< __y
._M_p
267 || (__x
._M_p
== __y
._M_p
&& __x
._M_offset
< __y
._M_offset
);
272 operator!=(const _Bit_iterator_base
& __x
, const _Bit_iterator_base
& __y
)
273 { return !(__x
== __y
); }
277 operator>(const _Bit_iterator_base
& __x
, const _Bit_iterator_base
& __y
)
278 { return __y
< __x
; }
282 operator<=(const _Bit_iterator_base
& __x
, const _Bit_iterator_base
& __y
)
283 { return !(__y
< __x
); }
287 operator>=(const _Bit_iterator_base
& __x
, const _Bit_iterator_base
& __y
)
288 { return !(__x
< __y
); }
289 #endif // three-way comparison
291 friend _GLIBCXX20_CONSTEXPR
ptrdiff_t
292 operator-(const _Bit_iterator_base
& __x
, const _Bit_iterator_base
& __y
)
294 __x
._M_assume_normalized();
295 __y
._M_assume_normalized();
296 return (int(_S_word_bit
) * (__x
._M_p
- __y
._M_p
)
297 + __x
._M_offset
- __y
._M_offset
);
300 #pragma GCC diagnostic pop
302 struct _Bit_iterator
: public _Bit_iterator_base
304 typedef _Bit_reference reference
;
305 #if __cplusplus > 201703L
306 typedef void pointer
;
308 typedef _Bit_reference
* pointer
;
310 typedef _Bit_iterator iterator
;
313 _Bit_iterator() : _Bit_iterator_base(0, 0) { }
316 _Bit_iterator(_Bit_type
* __x
, unsigned int __y
)
317 : _Bit_iterator_base(__x
, __y
) { }
321 _M_const_cast() const
324 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
328 _M_assume_normalized();
329 return reference(_M_p
, 1UL << _M_offset
);
344 iterator __tmp
= *this;
361 iterator __tmp
= *this;
368 operator+=(difference_type __i
)
376 operator-=(difference_type __i
)
382 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
384 operator[](difference_type __i
) const
385 { return *(*this + __i
); }
388 friend _GLIBCXX20_CONSTEXPR iterator
389 operator+(const iterator
& __x
, difference_type __n
)
391 iterator __tmp
= __x
;
397 friend _GLIBCXX20_CONSTEXPR iterator
398 operator+(difference_type __n
, const iterator
& __x
)
399 { return __x
+ __n
; }
402 friend _GLIBCXX20_CONSTEXPR iterator
403 operator-(const iterator
& __x
, difference_type __n
)
405 iterator __tmp
= __x
;
411 struct _Bit_const_iterator
: public _Bit_iterator_base
413 typedef bool reference
;
414 typedef bool const_reference
;
415 #if __cplusplus > 201703L
416 typedef void pointer
;
418 typedef const bool* pointer
;
420 typedef _Bit_const_iterator const_iterator
;
423 _Bit_const_iterator() : _Bit_iterator_base(0, 0) { }
426 _Bit_const_iterator(_Bit_type
* __x
, unsigned int __y
)
427 : _Bit_iterator_base(__x
, __y
) { }
430 _Bit_const_iterator(const _Bit_iterator
& __x
)
431 : _Bit_iterator_base(__x
._M_p
, __x
._M_offset
) { }
435 _M_const_cast() const
436 { return _Bit_iterator(_M_p
, _M_offset
); }
438 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
442 _M_assume_normalized();
443 return _Bit_reference(_M_p
, 1UL << _M_offset
);
458 const_iterator __tmp
= *this;
475 const_iterator __tmp
= *this;
482 operator+=(difference_type __i
)
490 operator-=(difference_type __i
)
496 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
498 operator[](difference_type __i
) const
499 { return *(*this + __i
); }
502 friend _GLIBCXX20_CONSTEXPR const_iterator
503 operator+(const const_iterator
& __x
, difference_type __n
)
505 const_iterator __tmp
= __x
;
511 friend _GLIBCXX20_CONSTEXPR const_iterator
512 operator-(const const_iterator
& __x
, difference_type __n
)
514 const_iterator __tmp
= __x
;
520 friend _GLIBCXX20_CONSTEXPR const_iterator
521 operator+(difference_type __n
, const const_iterator
& __x
)
522 { return __x
+ __n
; }
525 template<typename _Alloc
>
528 typedef typename
__gnu_cxx::__alloc_traits
<_Alloc
>::template
529 rebind
<_Bit_type
>::other _Bit_alloc_type
;
530 typedef typename
__gnu_cxx::__alloc_traits
<_Bit_alloc_type
>
532 typedef typename
_Bit_alloc_traits::pointer _Bit_pointer
;
534 struct _Bvector_impl_data
536 #if !_GLIBCXX_INLINE_VERSION
537 _Bit_iterator _M_start
;
539 // We don't need the offset field for the start, it's always zero.
542 // Allow assignment from iterators (assume offset is zero):
544 void operator=(_Bit_iterator __it
) { _M_p
= __it
._M_p
; }
547 _Bit_iterator _M_finish
;
548 _Bit_pointer _M_end_of_storage
;
551 _Bvector_impl_data() _GLIBCXX_NOEXCEPT
552 : _M_start(), _M_finish(), _M_end_of_storage()
555 #if __cplusplus >= 201103L
556 _Bvector_impl_data(const _Bvector_impl_data
&) = default;
559 operator=(const _Bvector_impl_data
&) = default;
562 _Bvector_impl_data(_Bvector_impl_data
&& __x
) noexcept
563 : _Bvector_impl_data(__x
)
568 _M_move_data(_Bvector_impl_data
&& __x
) noexcept
577 _M_reset() _GLIBCXX_NOEXCEPT
578 { *this = _Bvector_impl_data(); }
582 _M_swap_data(_Bvector_impl_data
& __x
) _GLIBCXX_NOEXCEPT
584 // Do not use std::swap(_M_start, __x._M_start), etc as it loses
585 // information used by TBAA.
586 std::swap(*this, __x
);
591 : public _Bit_alloc_type
, public _Bvector_impl_data
594 _Bvector_impl() _GLIBCXX_NOEXCEPT_IF(
595 is_nothrow_default_constructible
<_Bit_alloc_type
>::value
)
600 _Bvector_impl(const _Bit_alloc_type
& __a
) _GLIBCXX_NOEXCEPT
601 : _Bit_alloc_type(__a
)
604 #if __cplusplus >= 201103L
605 // Not defaulted, to enforce noexcept(true) even when
606 // !is_nothrow_move_constructible<_Bit_alloc_type>.
608 _Bvector_impl(_Bvector_impl
&& __x
) noexcept
609 : _Bit_alloc_type(std::move(__x
)), _Bvector_impl_data(std::move(__x
))
613 _Bvector_impl(_Bit_alloc_type
&& __a
, _Bvector_impl
&& __x
) noexcept
614 : _Bit_alloc_type(std::move(__a
)), _Bvector_impl_data(std::move(__x
))
620 _M_end_addr() const _GLIBCXX_NOEXCEPT
622 if (this->_M_end_of_storage
)
623 return std::__addressof(this->_M_end_of_storage
[-1]) + 1;
629 typedef _Alloc allocator_type
;
633 _M_get_Bit_allocator() _GLIBCXX_NOEXCEPT
634 { return this->_M_impl
; }
637 const _Bit_alloc_type
&
638 _M_get_Bit_allocator() const _GLIBCXX_NOEXCEPT
639 { return this->_M_impl
; }
643 get_allocator() const _GLIBCXX_NOEXCEPT
644 { return allocator_type(_M_get_Bit_allocator()); }
646 #if __cplusplus >= 201103L
647 _Bvector_base() = default;
653 _Bvector_base(const allocator_type
& __a
)
656 #if __cplusplus >= 201103L
657 _Bvector_base(_Bvector_base
&&) = default;
660 _Bvector_base(_Bvector_base
&& __x
, const allocator_type
& __a
) noexcept
661 : _M_impl(_Bit_alloc_type(__a
), std::move(__x
._M_impl
))
667 { this->_M_deallocate(); }
670 _Bvector_impl _M_impl
;
674 _M_allocate(size_t __n
)
676 _Bit_pointer __p
= _Bit_alloc_traits::allocate(_M_impl
, _S_nword(__n
));
677 #if __cpp_lib_is_constant_evaluated
678 if (std::is_constant_evaluated())
681 for (size_t __i
= 0; __i
< __n
; ++__i
)
692 if (_M_impl
._M_start
._M_p
)
694 const size_t __n
= _M_impl
._M_end_addr() - _M_impl
._M_start
._M_p
;
695 _Bit_alloc_traits::deallocate(_M_impl
,
696 _M_impl
._M_end_of_storage
- __n
,
702 #if __cplusplus >= 201103L
705 _M_move_data(_Bvector_base
&& __x
) noexcept
706 { _M_impl
._M_move_data(std::move(__x
._M_impl
)); }
712 { return (__n
+ int(_S_word_bit
) - 1) / int(_S_word_bit
); }
716 * @brief A specialization of vector for booleans which offers fixed time
717 * access to individual elements in any order.
723 * @tparam _Alloc Allocator type.
725 * Note that vector<bool> does not actually meet the requirements for being
726 * a container. This is because the reference and pointer types are not
727 * really references and pointers to bool. See DR96 for details. @see
728 * vector for function documentation.
730 * In some terminology a %vector can be described as a dynamic
731 * C-style array, it offers fast and efficient access to individual
732 * elements in any order and saves the user from worrying about
733 * memory and size allocation. Subscripting ( @c [] ) access is
734 * also provided as with C-style arrays.
736 template<typename _Alloc
>
737 class vector
<bool, _Alloc
> : protected _Bvector_base
<_Alloc
>
739 typedef _Bvector_base
<_Alloc
> _Base
;
740 typedef typename
_Base::_Bit_pointer _Bit_pointer
;
741 typedef typename
_Base::_Bit_alloc_traits _Bit_alloc_traits
;
743 #if __cplusplus >= 201103L
744 friend struct std::hash
<vector
>;
748 typedef bool value_type
;
749 typedef size_t size_type
;
750 typedef ptrdiff_t difference_type
;
751 typedef _Bit_reference reference
;
752 typedef bool const_reference
;
753 typedef _Bit_reference
* pointer
;
754 typedef const bool* const_pointer
;
755 typedef _Bit_iterator iterator
;
756 typedef _Bit_const_iterator const_iterator
;
757 typedef std::reverse_iterator
<const_iterator
> const_reverse_iterator
;
758 typedef std::reverse_iterator
<iterator
> reverse_iterator
;
759 typedef _Alloc allocator_type
;
763 get_allocator() const
764 { return _Base::get_allocator(); }
767 using _Base::_M_allocate
;
768 using _Base::_M_deallocate
;
769 using _Base::_S_nword
;
770 using _Base::_M_get_Bit_allocator
;
773 #if __cplusplus >= 201103L
781 vector(const allocator_type
& __a
)
784 #if __cplusplus >= 201103L
787 vector(size_type __n
, const allocator_type
& __a
= allocator_type())
788 : vector(__n
, false, __a
)
792 vector(size_type __n
, const bool& __value
,
793 const allocator_type
& __a
= allocator_type())
796 vector(size_type __n
, const bool& __value
= bool(),
797 const allocator_type
& __a
= allocator_type())
802 _M_initialize_value(__value
);
806 vector(const vector
& __x
)
807 : _Base(_Bit_alloc_traits::_S_select_on_copy(__x
._M_get_Bit_allocator()))
809 const_iterator __xbegin
= __x
.begin(), __xend
= __x
.end();
810 _M_initialize(__x
.size());
811 _M_copy_aligned(__xbegin
, __xend
, begin());
814 #if __cplusplus >= 201103L
815 vector(vector
&&) = default;
819 vector(vector
&& __x
, const allocator_type
& __a
, true_type
) noexcept
820 : _Base(std::move(__x
), __a
)
824 vector(vector
&& __x
, const allocator_type
& __a
, false_type
)
827 if (__x
.get_allocator() == __a
)
828 this->_M_move_data(std::move(__x
));
831 _M_initialize(__x
.size());
832 _M_copy_aligned(__x
.begin(), __x
.end(), begin());
839 vector(vector
&& __x
, const __type_identity_t
<allocator_type
>& __a
)
840 noexcept(_Bit_alloc_traits::_S_always_equal())
841 : vector(std::move(__x
), __a
,
842 typename
_Bit_alloc_traits::is_always_equal
{})
846 vector(const vector
& __x
, const __type_identity_t
<allocator_type
>& __a
)
849 _M_initialize(__x
.size());
850 _M_copy_aligned(__x
.begin(), __x
.end(), begin());
854 vector(initializer_list
<bool> __l
,
855 const allocator_type
& __a
= allocator_type())
858 _M_initialize_range(__l
.begin(), __l
.end(),
859 random_access_iterator_tag());
863 #if __cplusplus >= 201103L
864 template<typename _InputIterator
,
865 typename
= std::_RequireInputIter
<_InputIterator
>>
867 vector(_InputIterator __first
, _InputIterator __last
,
868 const allocator_type
& __a
= allocator_type())
871 _M_initialize_range(__first
, __last
,
872 std::__iterator_category(__first
));
875 template<typename _InputIterator
>
876 vector(_InputIterator __first
, _InputIterator __last
,
877 const allocator_type
& __a
= allocator_type())
880 // Check whether it's an integral type. If so, it's not an iterator.
881 typedef typename
std::__is_integer
<_InputIterator
>::__type _Integral
;
882 _M_initialize_dispatch(__first
, __last
, _Integral());
887 ~vector() _GLIBCXX_NOEXCEPT
{ }
891 operator=(const vector
& __x
)
895 #if __cplusplus >= 201103L
896 if (_Bit_alloc_traits::_S_propagate_on_copy_assign())
898 if (this->_M_get_Bit_allocator() != __x
._M_get_Bit_allocator())
900 this->_M_deallocate();
901 std::__alloc_on_copy(_M_get_Bit_allocator(),
902 __x
._M_get_Bit_allocator());
903 _M_initialize(__x
.size());
906 std::__alloc_on_copy(_M_get_Bit_allocator(),
907 __x
._M_get_Bit_allocator());
910 if (__x
.size() > capacity())
912 this->_M_deallocate();
913 _M_initialize(__x
.size());
915 this->_M_impl
._M_finish
= _M_copy_aligned(__x
.begin(), __x
.end(),
920 #if __cplusplus >= 201103L
923 operator=(vector
&& __x
) noexcept(_Bit_alloc_traits::_S_nothrow_move())
925 if (_Bit_alloc_traits::_S_propagate_on_move_assign()
926 || this->_M_get_Bit_allocator() == __x
._M_get_Bit_allocator())
928 this->_M_deallocate();
929 this->_M_move_data(std::move(__x
));
930 std::__alloc_on_move(_M_get_Bit_allocator(),
931 __x
._M_get_Bit_allocator());
935 if (__x
.size() > capacity())
937 this->_M_deallocate();
938 _M_initialize(__x
.size());
940 this->_M_impl
._M_finish
= _M_copy_aligned(__x
.begin(), __x
.end(),
949 operator=(initializer_list
<bool> __l
)
951 this->assign(__l
.begin(), __l
.end());
956 // assign(), a generalized assignment member function. Two
957 // versions: one that takes a count, and one that takes a range.
958 // The range version is a member template, so we dispatch on whether
959 // or not the type is an integer.
962 assign(size_type __n
, const bool& __x
)
963 { _M_fill_assign(__n
, __x
); }
965 #if __cplusplus >= 201103L
966 template<typename _InputIterator
,
967 typename
= std::_RequireInputIter
<_InputIterator
>>
970 assign(_InputIterator __first
, _InputIterator __last
)
971 { _M_assign_aux(__first
, __last
, std::__iterator_category(__first
)); }
973 template<typename _InputIterator
>
975 assign(_InputIterator __first
, _InputIterator __last
)
977 // Check whether it's an integral type. If so, it's not an iterator.
978 typedef typename
std::__is_integer
<_InputIterator
>::__type _Integral
;
979 _M_assign_dispatch(__first
, __last
, _Integral());
983 #if __cplusplus >= 201103L
986 assign(initializer_list
<bool> __l
)
987 { _M_assign_aux(__l
.begin(), __l
.end(), random_access_iterator_tag()); }
990 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
992 begin() _GLIBCXX_NOEXCEPT
993 { return iterator(this->_M_impl
._M_start
._M_p
, 0); }
995 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
997 begin() const _GLIBCXX_NOEXCEPT
998 { return const_iterator(this->_M_impl
._M_start
._M_p
, 0); }
1000 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1002 end() _GLIBCXX_NOEXCEPT
1003 { return this->_M_impl
._M_finish
; }
1005 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1007 end() const _GLIBCXX_NOEXCEPT
1008 { return this->_M_impl
._M_finish
; }
1010 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1012 rbegin() _GLIBCXX_NOEXCEPT
1013 { return reverse_iterator(end()); }
1015 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1016 const_reverse_iterator
1017 rbegin() const _GLIBCXX_NOEXCEPT
1018 { return const_reverse_iterator(end()); }
1020 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1022 rend() _GLIBCXX_NOEXCEPT
1023 { return reverse_iterator(begin()); }
1025 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1026 const_reverse_iterator
1027 rend() const _GLIBCXX_NOEXCEPT
1028 { return const_reverse_iterator(begin()); }
1030 #if __cplusplus >= 201103L
1031 [[__nodiscard__
]] _GLIBCXX20_CONSTEXPR
1033 cbegin() const noexcept
1034 { return const_iterator(this->_M_impl
._M_start
._M_p
, 0); }
1036 [[__nodiscard__
]] _GLIBCXX20_CONSTEXPR
1038 cend() const noexcept
1039 { return this->_M_impl
._M_finish
; }
1041 [[__nodiscard__
]] _GLIBCXX20_CONSTEXPR
1042 const_reverse_iterator
1043 crbegin() const noexcept
1044 { return const_reverse_iterator(end()); }
1046 [[__nodiscard__
]] _GLIBCXX20_CONSTEXPR
1047 const_reverse_iterator
1048 crend() const noexcept
1049 { return const_reverse_iterator(begin()); }
1052 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1054 size() const _GLIBCXX_NOEXCEPT
1055 { return size_type(end() - begin()); }
1057 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1059 max_size() const _GLIBCXX_NOEXCEPT
1061 const size_type __isize
=
1062 __gnu_cxx::__numeric_traits
<difference_type
>::__max
1063 - int(_S_word_bit
) + 1;
1064 const size_type __asize
1065 = _Bit_alloc_traits::max_size(_M_get_Bit_allocator());
1066 return (__asize
<= __isize
/ int(_S_word_bit
)
1067 ? __asize
* int(_S_word_bit
) : __isize
);
1070 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1072 capacity() const _GLIBCXX_NOEXCEPT
1073 { return size_type(const_iterator(this->_M_impl
._M_end_addr(), 0)
1076 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1078 empty() const _GLIBCXX_NOEXCEPT
1079 { return begin() == end(); }
1081 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1083 operator[](size_type __n
)
1084 { return begin()[__n
]; }
1086 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1088 operator[](size_type __n
) const
1089 { return begin()[__n
]; }
1092 _GLIBCXX20_CONSTEXPR
1094 _M_range_check(size_type __n
) const
1096 if (__n
>= this->size())
1097 __throw_out_of_range_fmt(__N("vector<bool>::_M_range_check: __n "
1098 "(which is %zu) >= this->size() "
1104 _GLIBCXX20_CONSTEXPR
1108 _M_range_check(__n
);
1109 return (*this)[__n
];
1112 _GLIBCXX20_CONSTEXPR
1114 at(size_type __n
) const
1116 _M_range_check(__n
);
1117 return (*this)[__n
];
1120 _GLIBCXX20_CONSTEXPR
1122 reserve(size_type __n
)
1124 if (__n
> max_size())
1125 __throw_length_error(__N("vector::reserve"));
1126 if (capacity() < __n
)
1130 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1133 { return *begin(); }
1135 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1138 { return *begin(); }
1140 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1143 { return *(end() - 1); }
1145 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1148 { return *(end() - 1); }
1150 _GLIBCXX20_CONSTEXPR
1154 if (this->_M_impl
._M_finish
._M_p
!= this->_M_impl
._M_end_addr())
1155 *this->_M_impl
._M_finish
++ = __x
;
1157 _M_insert_aux(end(), __x
);
1160 _GLIBCXX20_CONSTEXPR
1162 swap(vector
& __x
) _GLIBCXX_NOEXCEPT
1164 #if __cplusplus >= 201103L
1165 __glibcxx_assert(_Bit_alloc_traits::propagate_on_container_swap::value
1166 || _M_get_Bit_allocator() == __x
._M_get_Bit_allocator());
1168 this->_M_impl
._M_swap_data(__x
._M_impl
);
1169 _Bit_alloc_traits::_S_on_swap(_M_get_Bit_allocator(),
1170 __x
._M_get_Bit_allocator());
1173 // [23.2.5]/1, third-to-last entry in synopsis listing
1174 _GLIBCXX20_CONSTEXPR
1176 swap(reference __x
, reference __y
) _GLIBCXX_NOEXCEPT
1183 _GLIBCXX20_CONSTEXPR
1185 #if __cplusplus >= 201103L
1186 insert(const_iterator __position
, const bool& __x
)
1188 insert(iterator __position
, const bool& __x
)
1191 const difference_type __n
= __position
- begin();
1192 if (this->_M_impl
._M_finish
._M_p
!= this->_M_impl
._M_end_addr()
1193 && __position
== end())
1194 *this->_M_impl
._M_finish
++ = __x
;
1196 _M_insert_aux(__position
._M_const_cast(), __x
);
1197 return begin() + __n
;
1200 #if _GLIBCXX_USE_DEPRECATED
1201 _GLIBCXX_DEPRECATED_SUGGEST("insert(position, false)")
1203 insert(const_iterator __position
)
1204 { return this->insert(__position
._M_const_cast(), false); }
1207 #if __cplusplus >= 201103L
1208 template<typename _InputIterator
,
1209 typename
= std::_RequireInputIter
<_InputIterator
>>
1210 _GLIBCXX20_CONSTEXPR
1212 insert(const_iterator __position
,
1213 _InputIterator __first
, _InputIterator __last
)
1215 difference_type __offset
= __position
- cbegin();
1216 _M_insert_range(__position
._M_const_cast(),
1218 std::__iterator_category(__first
));
1219 return begin() + __offset
;
1222 template<typename _InputIterator
>
1224 insert(iterator __position
,
1225 _InputIterator __first
, _InputIterator __last
)
1227 // Check whether it's an integral type. If so, it's not an iterator.
1228 typedef typename
std::__is_integer
<_InputIterator
>::__type _Integral
;
1229 _M_insert_dispatch(__position
, __first
, __last
, _Integral());
1233 #if __cplusplus >= 201103L
1234 _GLIBCXX20_CONSTEXPR
1236 insert(const_iterator __position
, size_type __n
, const bool& __x
)
1238 difference_type __offset
= __position
- cbegin();
1239 _M_fill_insert(__position
._M_const_cast(), __n
, __x
);
1240 return begin() + __offset
;
1244 insert(iterator __position
, size_type __n
, const bool& __x
)
1245 { _M_fill_insert(__position
, __n
, __x
); }
1248 #if __cplusplus >= 201103L
1249 _GLIBCXX20_CONSTEXPR
1251 insert(const_iterator __p
, initializer_list
<bool> __l
)
1252 { return this->insert(__p
, __l
.begin(), __l
.end()); }
1255 _GLIBCXX20_CONSTEXPR
1258 { --this->_M_impl
._M_finish
; }
1260 _GLIBCXX20_CONSTEXPR
1262 #if __cplusplus >= 201103L
1263 erase(const_iterator __position
)
1265 erase(iterator __position
)
1267 { return _M_erase(__position
._M_const_cast()); }
1269 _GLIBCXX20_CONSTEXPR
1271 #if __cplusplus >= 201103L
1272 erase(const_iterator __first
, const_iterator __last
)
1274 erase(iterator __first
, iterator __last
)
1276 { return _M_erase(__first
._M_const_cast(), __last
._M_const_cast()); }
1278 _GLIBCXX20_CONSTEXPR
1280 resize(size_type __new_size
, bool __x
= bool())
1282 if (__new_size
< size())
1283 _M_erase_at_end(begin() + difference_type(__new_size
));
1285 insert(end(), __new_size
- size(), __x
);
1288 #if __cplusplus >= 201103L
1289 _GLIBCXX20_CONSTEXPR
1292 { _M_shrink_to_fit(); }
1295 _GLIBCXX20_CONSTEXPR
1297 flip() _GLIBCXX_NOEXCEPT
1299 _Bit_type
* const __end
= this->_M_impl
._M_end_addr();
1300 for (_Bit_type
* __p
= this->_M_impl
._M_start
._M_p
; __p
!= __end
; ++__p
)
1304 _GLIBCXX20_CONSTEXPR
1306 clear() _GLIBCXX_NOEXCEPT
1307 { _M_erase_at_end(begin()); }
1309 #if __cplusplus >= 201103L
1310 template<typename
... _Args
>
1311 #if __cplusplus > 201402L
1312 _GLIBCXX20_CONSTEXPR
1317 emplace_back(_Args
&&... __args
)
1319 push_back(bool(__args
...));
1320 #if __cplusplus > 201402L
1325 template<typename
... _Args
>
1326 _GLIBCXX20_CONSTEXPR
1328 emplace(const_iterator __pos
, _Args
&&... __args
)
1329 { return insert(__pos
, bool(__args
...)); }
1333 // Precondition: __first._M_offset == 0 && __result._M_offset == 0.
1334 _GLIBCXX20_CONSTEXPR
1336 _M_copy_aligned(const_iterator __first
, const_iterator __last
,
1339 _Bit_type
* __q
= std::copy(__first
._M_p
, __last
._M_p
, __result
._M_p
);
1340 return std::copy(const_iterator(__last
._M_p
, 0), __last
,
1344 _GLIBCXX20_CONSTEXPR
1346 _M_initialize(size_type __n
)
1350 _Bit_pointer __q
= this->_M_allocate(__n
);
1351 this->_M_impl
._M_end_of_storage
= __q
+ _S_nword(__n
);
1352 iterator __start
= iterator(std::__addressof(*__q
), 0);
1353 this->_M_impl
._M_start
= __start
;
1354 this->_M_impl
._M_finish
= __start
+ difference_type(__n
);
1358 _GLIBCXX20_CONSTEXPR
1360 _M_initialize_value(bool __x
) _GLIBCXX_NOEXCEPT
1362 if (_Bit_type
* __p
= this->_M_impl
._M_start
._M_p
)
1363 __fill_bvector_n(__p
, this->_M_impl
._M_end_addr() - __p
, __x
);
1366 _GLIBCXX20_CONSTEXPR
1368 _M_reallocate(size_type __n
);
1370 #if __cplusplus >= 201103L
1371 _GLIBCXX20_CONSTEXPR
1376 #if __cplusplus < 201103L
1377 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1378 // 438. Ambiguity in the "do the right thing" clause
1379 template<typename _Integer
>
1381 _M_initialize_dispatch(_Integer __n
, _Integer __x
, __true_type
)
1383 _M_initialize(static_cast<size_type
>(__n
));
1384 _M_initialize_value(__x
);
1387 template<typename _InputIterator
>
1389 _M_initialize_dispatch(_InputIterator __first
, _InputIterator __last
,
1391 { _M_initialize_range(__first
, __last
,
1392 std::__iterator_category(__first
)); }
1395 template<typename _InputIterator
>
1396 _GLIBCXX20_CONSTEXPR
1398 _M_initialize_range(_InputIterator __first
, _InputIterator __last
,
1399 std::input_iterator_tag
)
1401 for (; __first
!= __last
; ++__first
)
1402 push_back(*__first
);
1405 template<typename _ForwardIterator
>
1406 _GLIBCXX20_CONSTEXPR
1408 _M_initialize_range(_ForwardIterator __first
, _ForwardIterator __last
,
1409 std::forward_iterator_tag
)
1411 const size_type __n
= std::distance(__first
, __last
);
1413 std::copy(__first
, __last
, begin());
1416 #if __cplusplus < 201103L
1417 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1418 // 438. Ambiguity in the "do the right thing" clause
1419 template<typename _Integer
>
1421 _M_assign_dispatch(_Integer __n
, _Integer __val
, __true_type
)
1422 { _M_fill_assign(__n
, __val
); }
1424 template<class _InputIterator
>
1426 _M_assign_dispatch(_InputIterator __first
, _InputIterator __last
,
1428 { _M_assign_aux(__first
, __last
, std::__iterator_category(__first
)); }
1431 _GLIBCXX20_CONSTEXPR
1433 _M_fill_assign(size_t __n
, bool __x
)
1437 _M_initialize_value(__x
);
1438 insert(end(), __n
- size(), __x
);
1442 _M_erase_at_end(begin() + __n
);
1443 _M_initialize_value(__x
);
1447 template<typename _InputIterator
>
1448 _GLIBCXX20_CONSTEXPR
1450 _M_assign_aux(_InputIterator __first
, _InputIterator __last
,
1451 std::input_iterator_tag
)
1453 iterator __cur
= begin();
1454 for (; __first
!= __last
&& __cur
!= end(); ++__cur
, (void)++__first
)
1456 if (__first
== __last
)
1457 _M_erase_at_end(__cur
);
1459 insert(end(), __first
, __last
);
1462 template<typename _ForwardIterator
>
1463 _GLIBCXX20_CONSTEXPR
1465 _M_assign_aux(_ForwardIterator __first
, _ForwardIterator __last
,
1466 std::forward_iterator_tag
)
1468 const size_type __len
= std::distance(__first
, __last
);
1470 _M_erase_at_end(std::copy(__first
, __last
, begin()));
1473 _ForwardIterator __mid
= __first
;
1474 std::advance(__mid
, size());
1475 std::copy(__first
, __mid
, begin());
1476 insert(end(), __mid
, __last
);
1480 #if __cplusplus < 201103L
1481 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1482 // 438. Ambiguity in the "do the right thing" clause
1483 template<typename _Integer
>
1485 _M_insert_dispatch(iterator __pos
, _Integer __n
, _Integer __x
,
1487 { _M_fill_insert(__pos
, __n
, __x
); }
1489 template<typename _InputIterator
>
1491 _M_insert_dispatch(iterator __pos
,
1492 _InputIterator __first
, _InputIterator __last
,
1494 { _M_insert_range(__pos
, __first
, __last
,
1495 std::__iterator_category(__first
)); }
1498 _GLIBCXX20_CONSTEXPR
1500 _M_fill_insert(iterator __position
, size_type __n
, bool __x
);
1502 template<typename _InputIterator
>
1503 _GLIBCXX20_CONSTEXPR
1505 _M_insert_range(iterator __pos
, _InputIterator __first
,
1506 _InputIterator __last
, std::input_iterator_tag
)
1508 for (; __first
!= __last
; ++__first
)
1510 __pos
= insert(__pos
, *__first
);
1515 template<typename _ForwardIterator
>
1516 _GLIBCXX20_CONSTEXPR
1518 _M_insert_range(iterator __position
, _ForwardIterator __first
,
1519 _ForwardIterator __last
, std::forward_iterator_tag
);
1521 _GLIBCXX20_CONSTEXPR
1523 _M_insert_aux(iterator __position
, bool __x
);
1525 _GLIBCXX20_CONSTEXPR
1527 _M_check_len(size_type __n
, const char* __s
) const
1529 if (max_size() - size() < __n
)
1530 __throw_length_error(__N(__s
));
1532 const size_type __len
= size() + std::max(size(), __n
);
1533 return (__len
< size() || __len
> max_size()) ? max_size() : __len
;
1536 _GLIBCXX20_CONSTEXPR
1538 _M_erase_at_end(iterator __pos
)
1539 { this->_M_impl
._M_finish
= __pos
; }
1541 _GLIBCXX20_CONSTEXPR
1543 _M_erase(iterator __pos
);
1545 _GLIBCXX20_CONSTEXPR
1547 _M_erase(iterator __first
, iterator __last
);
1550 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1551 // DR 464. Suggestion for new member functions in standard containers.
1552 // N.B. DR 464 says nothing about vector<bool> but we need something
1553 // here due to the using-declaration in __gnu_debug::vector.
1555 #if __cplusplus >= 201103L
1556 void data() = delete;
1562 _GLIBCXX_END_NAMESPACE_CONTAINER
1564 // Fill a partial word.
1565 _GLIBCXX20_CONSTEXPR
1567 __fill_bvector(_Bit_type
* __v
, unsigned int __first
, unsigned int __last
,
1568 bool __x
) _GLIBCXX_NOEXCEPT
1570 const _Bit_type __fmask
= ~0ul << __first
;
1571 const _Bit_type __lmask
= ~0ul >> (_S_word_bit
- __last
);
1572 const _Bit_type __mask
= __fmask
& __lmask
;
1580 // Fill N full words, as if using memset, but usable in constant expressions.
1581 __attribute__((__nonnull__
))
1582 _GLIBCXX20_CONSTEXPR
1584 __fill_bvector_n(_Bit_type
* __p
, size_t __n
, bool __x
) _GLIBCXX_NOEXCEPT
1586 #if __cpp_lib_is_constant_evaluated
1587 if (std::is_constant_evaluated())
1589 for (size_t __i
= 0; __i
< __n
; ++__i
)
1590 __p
[__i
] = __x
? ~0ul : 0ul;
1594 __builtin_memset(__p
, __x
? ~0 : 0, __n
* sizeof(_Bit_type
));
1598 _GLIBCXX20_CONSTEXPR
1600 __fill_a1(_GLIBCXX_STD_C::_Bit_iterator __first
,
1601 _GLIBCXX_STD_C::_Bit_iterator __last
, const bool& __x
)
1603 if (__first
._M_p
!= __last
._M_p
)
1605 _Bit_type
* __first_p
= __first
._M_p
;
1606 if (__first
._M_offset
!= 0)
1607 __fill_bvector(__first_p
++, __first
._M_offset
, _S_word_bit
, __x
);
1609 __fill_bvector_n(__first_p
, __last
._M_p
- __first_p
, __x
);
1611 if (__last
._M_offset
!= 0)
1612 __fill_bvector(__last
._M_p
, 0, __last
._M_offset
, __x
);
1614 else if (__first
._M_offset
!= __last
._M_offset
)
1615 __fill_bvector(__first
._M_p
, __first
._M_offset
, __last
._M_offset
, __x
);
1618 #if __cplusplus >= 201103L
1620 /// std::hash specialization for vector<bool>.
1621 template<typename _Alloc
>
1622 struct hash
<_GLIBCXX_STD_C::vector
<bool, _Alloc
>>
1623 : public __hash_base
<size_t, _GLIBCXX_STD_C::vector
<bool, _Alloc
>>
1626 operator()(const _GLIBCXX_STD_C::vector
<bool, _Alloc
>&) const noexcept
;
1630 _GLIBCXX_END_NAMESPACE_VERSION