1 // vector<bool> specialization -*- C++ -*-
3 // Copyright (C) 2001-2021 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 #if __cplusplus >= 201103L
60 #include <initializer_list>
61 #include <bits/functional_hash.h>
64 namespace std
_GLIBCXX_VISIBILITY(default)
66 _GLIBCXX_BEGIN_NAMESPACE_VERSION
67 _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
69 typedef unsigned long _Bit_type
;
70 enum { _S_word_bit
= int(__CHAR_BIT__
* sizeof(_Bit_type
)) };
77 _Bit_reference(_Bit_type
* __x
, _Bit_type __y
)
78 : _M_p(__x
), _M_mask(__y
) { }
80 _Bit_reference() _GLIBCXX_NOEXCEPT
: _M_p(0), _M_mask(0) { }
82 #if __cplusplus >= 201103L
83 _Bit_reference(const _Bit_reference
&) = default;
86 operator bool() const _GLIBCXX_NOEXCEPT
87 { return !!(*_M_p
& _M_mask
); }
90 operator=(bool __x
) _GLIBCXX_NOEXCEPT
100 operator=(const _Bit_reference
& __x
) _GLIBCXX_NOEXCEPT
101 { return *this = bool(__x
); }
104 operator==(const _Bit_reference
& __x
) const
105 { return bool(*this) == bool(__x
); }
108 operator<(const _Bit_reference
& __x
) const
109 { return !bool(*this) && bool(__x
); }
112 flip() _GLIBCXX_NOEXCEPT
113 { *_M_p
^= _M_mask
; }
116 #if __cplusplus >= 201103L
118 swap(_Bit_reference __x
, _Bit_reference __y
) noexcept
126 swap(_Bit_reference __x
, bool& __y
) noexcept
134 swap(bool& __x
, _Bit_reference __y
) noexcept
142 struct _Bit_iterator_base
143 : public std::iterator
<std::random_access_iterator_tag
, bool>
146 unsigned int _M_offset
;
148 _Bit_iterator_base(_Bit_type
* __x
, unsigned int __y
)
149 : _M_p(__x
), _M_offset(__y
) { }
154 if (_M_offset
++ == int(_S_word_bit
) - 1)
164 if (_M_offset
-- == 0)
166 _M_offset
= int(_S_word_bit
) - 1;
172 _M_incr(ptrdiff_t __i
)
174 difference_type __n
= __i
+ _M_offset
;
175 _M_p
+= __n
/ int(_S_word_bit
);
176 __n
= __n
% int(_S_word_bit
);
179 __n
+= int(_S_word_bit
);
182 _M_offset
= static_cast<unsigned int>(__n
);
185 friend _GLIBCXX20_CONSTEXPR
bool
186 operator==(const _Bit_iterator_base
& __x
, const _Bit_iterator_base
& __y
)
187 { return __x
._M_p
== __y
._M_p
&& __x
._M_offset
== __y
._M_offset
; }
189 #if __cpp_lib_three_way_comparison
190 friend constexpr strong_ordering
191 operator<=>(const _Bit_iterator_base
& __x
, const _Bit_iterator_base
& __y
)
194 if (const auto __cmp
= __x
._M_p
<=> __y
._M_p
; __cmp
!= 0)
196 return __x
._M_offset
<=> __y
._M_offset
;
200 operator<(const _Bit_iterator_base
& __x
, const _Bit_iterator_base
& __y
)
202 return __x
._M_p
< __y
._M_p
203 || (__x
._M_p
== __y
._M_p
&& __x
._M_offset
< __y
._M_offset
);
207 operator!=(const _Bit_iterator_base
& __x
, const _Bit_iterator_base
& __y
)
208 { return !(__x
== __y
); }
211 operator>(const _Bit_iterator_base
& __x
, const _Bit_iterator_base
& __y
)
212 { return __y
< __x
; }
215 operator<=(const _Bit_iterator_base
& __x
, const _Bit_iterator_base
& __y
)
216 { return !(__y
< __x
); }
219 operator>=(const _Bit_iterator_base
& __x
, const _Bit_iterator_base
& __y
)
220 { return !(__x
< __y
); }
221 #endif // three-way comparison
224 operator-(const _Bit_iterator_base
& __x
, const _Bit_iterator_base
& __y
)
226 return (int(_S_word_bit
) * (__x
._M_p
- __y
._M_p
)
227 + __x
._M_offset
- __y
._M_offset
);
231 struct _Bit_iterator
: public _Bit_iterator_base
233 typedef _Bit_reference reference
;
234 #if __cplusplus > 201703L
235 typedef void pointer
;
237 typedef _Bit_reference
* pointer
;
239 typedef _Bit_iterator iterator
;
241 _Bit_iterator() : _Bit_iterator_base(0, 0) { }
243 _Bit_iterator(_Bit_type
* __x
, unsigned int __y
)
244 : _Bit_iterator_base(__x
, __y
) { }
247 _M_const_cast() const
252 { return reference(_M_p
, 1UL << _M_offset
); }
264 iterator __tmp
= *this;
279 iterator __tmp
= *this;
285 operator+=(difference_type __i
)
292 operator-=(difference_type __i
)
299 operator[](difference_type __i
) const
300 { return *(*this + __i
); }
303 operator+(const iterator
& __x
, difference_type __n
)
305 iterator __tmp
= __x
;
311 operator+(difference_type __n
, const iterator
& __x
)
312 { return __x
+ __n
; }
315 operator-(const iterator
& __x
, difference_type __n
)
317 iterator __tmp
= __x
;
323 struct _Bit_const_iterator
: public _Bit_iterator_base
325 typedef bool reference
;
326 typedef bool const_reference
;
327 #if __cplusplus > 201703L
328 typedef void pointer
;
330 typedef const bool* pointer
;
332 typedef _Bit_const_iterator const_iterator
;
334 _Bit_const_iterator() : _Bit_iterator_base(0, 0) { }
336 _Bit_const_iterator(_Bit_type
* __x
, unsigned int __y
)
337 : _Bit_iterator_base(__x
, __y
) { }
339 _Bit_const_iterator(const _Bit_iterator
& __x
)
340 : _Bit_iterator_base(__x
._M_p
, __x
._M_offset
) { }
343 _M_const_cast() const
344 { return _Bit_iterator(_M_p
, _M_offset
); }
348 { return _Bit_reference(_M_p
, 1UL << _M_offset
); }
360 const_iterator __tmp
= *this;
375 const_iterator __tmp
= *this;
381 operator+=(difference_type __i
)
388 operator-=(difference_type __i
)
395 operator[](difference_type __i
) const
396 { return *(*this + __i
); }
398 friend const_iterator
399 operator+(const const_iterator
& __x
, difference_type __n
)
401 const_iterator __tmp
= __x
;
406 friend const_iterator
407 operator-(const const_iterator
& __x
, difference_type __n
)
409 const_iterator __tmp
= __x
;
414 friend const_iterator
415 operator+(difference_type __n
, const const_iterator
& __x
)
416 { return __x
+ __n
; }
419 template<typename _Alloc
>
422 typedef typename
__gnu_cxx::__alloc_traits
<_Alloc
>::template
423 rebind
<_Bit_type
>::other _Bit_alloc_type
;
424 typedef typename
__gnu_cxx::__alloc_traits
<_Bit_alloc_type
>
426 typedef typename
_Bit_alloc_traits::pointer _Bit_pointer
;
428 struct _Bvector_impl_data
430 #if !_GLIBCXX_INLINE_VERSION
431 _Bit_iterator _M_start
;
433 // We don't need the offset field for the start, it's always zero.
436 // Allow assignment from iterators (assume offset is zero):
437 void operator=(_Bit_iterator __it
) { _M_p
= __it
._M_p
; }
440 _Bit_iterator _M_finish
;
441 _Bit_pointer _M_end_of_storage
;
443 _Bvector_impl_data() _GLIBCXX_NOEXCEPT
444 : _M_start(), _M_finish(), _M_end_of_storage()
447 #if __cplusplus >= 201103L
448 _Bvector_impl_data(const _Bvector_impl_data
&) = default;
450 operator=(const _Bvector_impl_data
&) = default;
452 _Bvector_impl_data(_Bvector_impl_data
&& __x
) noexcept
453 : _Bvector_impl_data(__x
)
457 _M_move_data(_Bvector_impl_data
&& __x
) noexcept
465 _M_reset() _GLIBCXX_NOEXCEPT
466 { *this = _Bvector_impl_data(); }
469 _M_swap_data(_Bvector_impl_data
& __x
) _GLIBCXX_NOEXCEPT
471 // Do not use std::swap(_M_start, __x._M_start), etc as it loses
472 // information used by TBAA.
473 std::swap(*this, __x
);
478 : public _Bit_alloc_type
, public _Bvector_impl_data
480 _Bvector_impl() _GLIBCXX_NOEXCEPT_IF(
481 is_nothrow_default_constructible
<_Bit_alloc_type
>::value
)
485 _Bvector_impl(const _Bit_alloc_type
& __a
) _GLIBCXX_NOEXCEPT
486 : _Bit_alloc_type(__a
)
489 #if __cplusplus >= 201103L
490 // Not defaulted, to enforce noexcept(true) even when
491 // !is_nothrow_move_constructible<_Bit_alloc_type>.
492 _Bvector_impl(_Bvector_impl
&& __x
) noexcept
493 : _Bit_alloc_type(std::move(__x
)), _Bvector_impl_data(std::move(__x
))
496 _Bvector_impl(_Bit_alloc_type
&& __a
, _Bvector_impl
&& __x
) noexcept
497 : _Bit_alloc_type(std::move(__a
)), _Bvector_impl_data(std::move(__x
))
502 _M_end_addr() const _GLIBCXX_NOEXCEPT
504 if (this->_M_end_of_storage
)
505 return std::__addressof(this->_M_end_of_storage
[-1]) + 1;
511 typedef _Alloc allocator_type
;
514 _M_get_Bit_allocator() _GLIBCXX_NOEXCEPT
515 { return this->_M_impl
; }
517 const _Bit_alloc_type
&
518 _M_get_Bit_allocator() const _GLIBCXX_NOEXCEPT
519 { return this->_M_impl
; }
522 get_allocator() const _GLIBCXX_NOEXCEPT
523 { return allocator_type(_M_get_Bit_allocator()); }
525 #if __cplusplus >= 201103L
526 _Bvector_base() = default;
531 _Bvector_base(const allocator_type
& __a
)
534 #if __cplusplus >= 201103L
535 _Bvector_base(_Bvector_base
&&) = default;
537 _Bvector_base(_Bvector_base
&& __x
, const allocator_type
& __a
) noexcept
538 : _M_impl(_Bit_alloc_type(__a
), std::move(__x
._M_impl
))
543 { this->_M_deallocate(); }
546 _Bvector_impl _M_impl
;
549 _M_allocate(size_t __n
)
550 { return _Bit_alloc_traits::allocate(_M_impl
, _S_nword(__n
)); }
555 if (_M_impl
._M_start
._M_p
)
557 const size_t __n
= _M_impl
._M_end_addr() - _M_impl
._M_start
._M_p
;
558 _Bit_alloc_traits::deallocate(_M_impl
,
559 _M_impl
._M_end_of_storage
- __n
,
565 #if __cplusplus >= 201103L
567 _M_move_data(_Bvector_base
&& __x
) noexcept
568 { _M_impl
._M_move_data(std::move(__x
._M_impl
)); }
573 { return (__n
+ int(_S_word_bit
) - 1) / int(_S_word_bit
); }
576 _GLIBCXX_END_NAMESPACE_CONTAINER
577 _GLIBCXX_END_NAMESPACE_VERSION
580 // Declare a partial specialization of vector<T, Alloc>.
581 #include <bits/stl_vector.h>
583 namespace std
_GLIBCXX_VISIBILITY(default)
585 _GLIBCXX_BEGIN_NAMESPACE_VERSION
586 _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
589 * @brief A specialization of vector for booleans which offers fixed time
590 * access to individual elements in any order.
594 * @tparam _Alloc Allocator type.
596 * Note that vector<bool> does not actually meet the requirements for being
597 * a container. This is because the reference and pointer types are not
598 * really references and pointers to bool. See DR96 for details. @see
599 * vector for function documentation.
601 * In some terminology a %vector can be described as a dynamic
602 * C-style array, it offers fast and efficient access to individual
603 * elements in any order and saves the user from worrying about
604 * memory and size allocation. Subscripting ( @c [] ) access is
605 * also provided as with C-style arrays.
607 template<typename _Alloc
>
608 class vector
<bool, _Alloc
> : protected _Bvector_base
<_Alloc
>
610 typedef _Bvector_base
<_Alloc
> _Base
;
611 typedef typename
_Base::_Bit_pointer _Bit_pointer
;
612 typedef typename
_Base::_Bit_alloc_traits _Bit_alloc_traits
;
614 #if __cplusplus >= 201103L
615 friend struct std::hash
<vector
>;
619 typedef bool value_type
;
620 typedef size_t size_type
;
621 typedef ptrdiff_t difference_type
;
622 typedef _Bit_reference reference
;
623 typedef bool const_reference
;
624 typedef _Bit_reference
* pointer
;
625 typedef const bool* const_pointer
;
626 typedef _Bit_iterator iterator
;
627 typedef _Bit_const_iterator const_iterator
;
628 typedef std::reverse_iterator
<const_iterator
> const_reverse_iterator
;
629 typedef std::reverse_iterator
<iterator
> reverse_iterator
;
630 typedef _Alloc allocator_type
;
633 get_allocator() const
634 { return _Base::get_allocator(); }
637 using _Base::_M_allocate
;
638 using _Base::_M_deallocate
;
639 using _Base::_S_nword
;
640 using _Base::_M_get_Bit_allocator
;
643 #if __cplusplus >= 201103L
650 vector(const allocator_type
& __a
)
653 #if __cplusplus >= 201103L
655 vector(size_type __n
, const allocator_type
& __a
= allocator_type())
656 : vector(__n
, false, __a
)
659 vector(size_type __n
, const bool& __value
,
660 const allocator_type
& __a
= allocator_type())
663 vector(size_type __n
, const bool& __value
= bool(),
664 const allocator_type
& __a
= allocator_type())
669 _M_initialize_value(__value
);
672 vector(const vector
& __x
)
673 : _Base(_Bit_alloc_traits::_S_select_on_copy(__x
._M_get_Bit_allocator()))
675 _M_initialize(__x
.size());
676 _M_copy_aligned(__x
.begin(), __x
.end(), begin());
679 #if __cplusplus >= 201103L
680 vector(vector
&&) = default;
683 vector(vector
&& __x
, const allocator_type
& __a
, true_type
) noexcept
684 : _Base(std::move(__x
), __a
)
687 vector(vector
&& __x
, const allocator_type
& __a
, false_type
)
690 if (__x
.get_allocator() == __a
)
691 this->_M_move_data(std::move(__x
));
694 _M_initialize(__x
.size());
695 _M_copy_aligned(__x
.begin(), __x
.end(), begin());
701 vector(vector
&& __x
, const allocator_type
& __a
)
702 noexcept(_Bit_alloc_traits::_S_always_equal())
703 : vector(std::move(__x
), __a
,
704 typename
_Bit_alloc_traits::is_always_equal
{})
707 vector(const vector
& __x
, const allocator_type
& __a
)
710 _M_initialize(__x
.size());
711 _M_copy_aligned(__x
.begin(), __x
.end(), begin());
714 vector(initializer_list
<bool> __l
,
715 const allocator_type
& __a
= allocator_type())
718 _M_initialize_range(__l
.begin(), __l
.end(),
719 random_access_iterator_tag());
723 #if __cplusplus >= 201103L
724 template<typename _InputIterator
,
725 typename
= std::_RequireInputIter
<_InputIterator
>>
726 vector(_InputIterator __first
, _InputIterator __last
,
727 const allocator_type
& __a
= allocator_type())
730 _M_initialize_range(__first
, __last
,
731 std::__iterator_category(__first
));
734 template<typename _InputIterator
>
735 vector(_InputIterator __first
, _InputIterator __last
,
736 const allocator_type
& __a
= allocator_type())
739 // Check whether it's an integral type. If so, it's not an iterator.
740 typedef typename
std::__is_integer
<_InputIterator
>::__type _Integral
;
741 _M_initialize_dispatch(__first
, __last
, _Integral());
745 ~vector() _GLIBCXX_NOEXCEPT
{ }
748 operator=(const vector
& __x
)
752 #if __cplusplus >= 201103L
753 if (_Bit_alloc_traits::_S_propagate_on_copy_assign())
755 if (this->_M_get_Bit_allocator() != __x
._M_get_Bit_allocator())
757 this->_M_deallocate();
758 std::__alloc_on_copy(_M_get_Bit_allocator(),
759 __x
._M_get_Bit_allocator());
760 _M_initialize(__x
.size());
763 std::__alloc_on_copy(_M_get_Bit_allocator(),
764 __x
._M_get_Bit_allocator());
767 if (__x
.size() > capacity())
769 this->_M_deallocate();
770 _M_initialize(__x
.size());
772 this->_M_impl
._M_finish
= _M_copy_aligned(__x
.begin(), __x
.end(),
777 #if __cplusplus >= 201103L
779 operator=(vector
&& __x
) noexcept(_Bit_alloc_traits::_S_nothrow_move())
781 if (_Bit_alloc_traits::_S_propagate_on_move_assign()
782 || this->_M_get_Bit_allocator() == __x
._M_get_Bit_allocator())
784 this->_M_deallocate();
785 this->_M_move_data(std::move(__x
));
786 std::__alloc_on_move(_M_get_Bit_allocator(),
787 __x
._M_get_Bit_allocator());
791 if (__x
.size() > capacity())
793 this->_M_deallocate();
794 _M_initialize(__x
.size());
796 this->_M_impl
._M_finish
= _M_copy_aligned(__x
.begin(), __x
.end(),
804 operator=(initializer_list
<bool> __l
)
806 this->assign(__l
.begin(), __l
.end());
811 // assign(), a generalized assignment member function. Two
812 // versions: one that takes a count, and one that takes a range.
813 // The range version is a member template, so we dispatch on whether
814 // or not the type is an integer.
816 assign(size_type __n
, const bool& __x
)
817 { _M_fill_assign(__n
, __x
); }
819 #if __cplusplus >= 201103L
820 template<typename _InputIterator
,
821 typename
= std::_RequireInputIter
<_InputIterator
>>
823 assign(_InputIterator __first
, _InputIterator __last
)
824 { _M_assign_aux(__first
, __last
, std::__iterator_category(__first
)); }
826 template<typename _InputIterator
>
828 assign(_InputIterator __first
, _InputIterator __last
)
830 // Check whether it's an integral type. If so, it's not an iterator.
831 typedef typename
std::__is_integer
<_InputIterator
>::__type _Integral
;
832 _M_assign_dispatch(__first
, __last
, _Integral());
836 #if __cplusplus >= 201103L
838 assign(initializer_list
<bool> __l
)
839 { _M_assign_aux(__l
.begin(), __l
.end(), random_access_iterator_tag()); }
843 begin() _GLIBCXX_NOEXCEPT
844 { return iterator(this->_M_impl
._M_start
._M_p
, 0); }
847 begin() const _GLIBCXX_NOEXCEPT
848 { return const_iterator(this->_M_impl
._M_start
._M_p
, 0); }
851 end() _GLIBCXX_NOEXCEPT
852 { return this->_M_impl
._M_finish
; }
855 end() const _GLIBCXX_NOEXCEPT
856 { return this->_M_impl
._M_finish
; }
859 rbegin() _GLIBCXX_NOEXCEPT
860 { return reverse_iterator(end()); }
862 const_reverse_iterator
863 rbegin() const _GLIBCXX_NOEXCEPT
864 { return const_reverse_iterator(end()); }
867 rend() _GLIBCXX_NOEXCEPT
868 { return reverse_iterator(begin()); }
870 const_reverse_iterator
871 rend() const _GLIBCXX_NOEXCEPT
872 { return const_reverse_iterator(begin()); }
874 #if __cplusplus >= 201103L
876 cbegin() const noexcept
877 { return const_iterator(this->_M_impl
._M_start
._M_p
, 0); }
880 cend() const noexcept
881 { return this->_M_impl
._M_finish
; }
883 const_reverse_iterator
884 crbegin() const noexcept
885 { return const_reverse_iterator(end()); }
887 const_reverse_iterator
888 crend() const noexcept
889 { return const_reverse_iterator(begin()); }
893 size() const _GLIBCXX_NOEXCEPT
894 { return size_type(end() - begin()); }
897 max_size() const _GLIBCXX_NOEXCEPT
899 const size_type __isize
=
900 __gnu_cxx::__numeric_traits
<difference_type
>::__max
901 - int(_S_word_bit
) + 1;
902 const size_type __asize
903 = _Bit_alloc_traits::max_size(_M_get_Bit_allocator());
904 return (__asize
<= __isize
/ int(_S_word_bit
)
905 ? __asize
* int(_S_word_bit
) : __isize
);
909 capacity() const _GLIBCXX_NOEXCEPT
910 { return size_type(const_iterator(this->_M_impl
._M_end_addr(), 0)
913 _GLIBCXX_NODISCARD
bool
914 empty() const _GLIBCXX_NOEXCEPT
915 { return begin() == end(); }
918 operator[](size_type __n
)
919 { return begin()[__n
]; }
922 operator[](size_type __n
) const
923 { return begin()[__n
]; }
927 _M_range_check(size_type __n
) const
929 if (__n
>= this->size())
930 __throw_out_of_range_fmt(__N("vector<bool>::_M_range_check: __n "
931 "(which is %zu) >= this->size() "
939 { _M_range_check(__n
); return (*this)[__n
]; }
942 at(size_type __n
) const
943 { _M_range_check(__n
); return (*this)[__n
]; }
946 reserve(size_type __n
)
948 if (__n
> max_size())
949 __throw_length_error(__N("vector::reserve"));
950 if (capacity() < __n
)
964 { return *(end() - 1); }
968 { return *(end() - 1); }
970 // _GLIBCXX_RESOLVE_LIB_DEFECTS
971 // DR 464. Suggestion for new member functions in standard containers.
972 // N.B. DR 464 says nothing about vector<bool> but we need something
973 // here due to the way we are implementing DR 464 in the debug-mode
976 data() _GLIBCXX_NOEXCEPT
{ }
981 if (this->_M_impl
._M_finish
._M_p
!= this->_M_impl
._M_end_addr())
982 *this->_M_impl
._M_finish
++ = __x
;
984 _M_insert_aux(end(), __x
);
988 swap(vector
& __x
) _GLIBCXX_NOEXCEPT
990 #if __cplusplus >= 201103L
991 __glibcxx_assert(_Bit_alloc_traits::propagate_on_container_swap::value
992 || _M_get_Bit_allocator() == __x
._M_get_Bit_allocator());
994 this->_M_impl
._M_swap_data(__x
._M_impl
);
995 _Bit_alloc_traits::_S_on_swap(_M_get_Bit_allocator(),
996 __x
._M_get_Bit_allocator());
999 // [23.2.5]/1, third-to-last entry in synopsis listing
1001 swap(reference __x
, reference __y
) _GLIBCXX_NOEXCEPT
1009 #if __cplusplus >= 201103L
1010 insert(const_iterator __position
, const bool& __x
= bool())
1012 insert(iterator __position
, const bool& __x
= bool())
1015 const difference_type __n
= __position
- begin();
1016 if (this->_M_impl
._M_finish
._M_p
!= this->_M_impl
._M_end_addr()
1017 && __position
== end())
1018 *this->_M_impl
._M_finish
++ = __x
;
1020 _M_insert_aux(__position
._M_const_cast(), __x
);
1021 return begin() + __n
;
1024 #if __cplusplus >= 201103L
1025 template<typename _InputIterator
,
1026 typename
= std::_RequireInputIter
<_InputIterator
>>
1028 insert(const_iterator __position
,
1029 _InputIterator __first
, _InputIterator __last
)
1031 difference_type __offset
= __position
- cbegin();
1032 _M_insert_range(__position
._M_const_cast(),
1034 std::__iterator_category(__first
));
1035 return begin() + __offset
;
1038 template<typename _InputIterator
>
1040 insert(iterator __position
,
1041 _InputIterator __first
, _InputIterator __last
)
1043 // Check whether it's an integral type. If so, it's not an iterator.
1044 typedef typename
std::__is_integer
<_InputIterator
>::__type _Integral
;
1045 _M_insert_dispatch(__position
, __first
, __last
, _Integral());
1049 #if __cplusplus >= 201103L
1051 insert(const_iterator __position
, size_type __n
, const bool& __x
)
1053 difference_type __offset
= __position
- cbegin();
1054 _M_fill_insert(__position
._M_const_cast(), __n
, __x
);
1055 return begin() + __offset
;
1059 insert(iterator __position
, size_type __n
, const bool& __x
)
1060 { _M_fill_insert(__position
, __n
, __x
); }
1063 #if __cplusplus >= 201103L
1065 insert(const_iterator __p
, initializer_list
<bool> __l
)
1066 { return this->insert(__p
, __l
.begin(), __l
.end()); }
1071 { --this->_M_impl
._M_finish
; }
1074 #if __cplusplus >= 201103L
1075 erase(const_iterator __position
)
1077 erase(iterator __position
)
1079 { return _M_erase(__position
._M_const_cast()); }
1082 #if __cplusplus >= 201103L
1083 erase(const_iterator __first
, const_iterator __last
)
1085 erase(iterator __first
, iterator __last
)
1087 { return _M_erase(__first
._M_const_cast(), __last
._M_const_cast()); }
1090 resize(size_type __new_size
, bool __x
= bool())
1092 if (__new_size
< size())
1093 _M_erase_at_end(begin() + difference_type(__new_size
));
1095 insert(end(), __new_size
- size(), __x
);
1098 #if __cplusplus >= 201103L
1101 { _M_shrink_to_fit(); }
1105 flip() _GLIBCXX_NOEXCEPT
1107 _Bit_type
* const __end
= this->_M_impl
._M_end_addr();
1108 for (_Bit_type
* __p
= this->_M_impl
._M_start
._M_p
; __p
!= __end
; ++__p
)
1113 clear() _GLIBCXX_NOEXCEPT
1114 { _M_erase_at_end(begin()); }
1116 #if __cplusplus >= 201103L
1117 template<typename
... _Args
>
1118 #if __cplusplus > 201402L
1123 emplace_back(_Args
&&... __args
)
1125 push_back(bool(__args
...));
1126 #if __cplusplus > 201402L
1131 template<typename
... _Args
>
1133 emplace(const_iterator __pos
, _Args
&&... __args
)
1134 { return insert(__pos
, bool(__args
...)); }
1138 // Precondition: __first._M_offset == 0 && __result._M_offset == 0.
1140 _M_copy_aligned(const_iterator __first
, const_iterator __last
,
1143 _Bit_type
* __q
= std::copy(__first
._M_p
, __last
._M_p
, __result
._M_p
);
1144 return std::copy(const_iterator(__last
._M_p
, 0), __last
,
1149 _M_initialize(size_type __n
)
1153 _Bit_pointer __q
= this->_M_allocate(__n
);
1154 this->_M_impl
._M_end_of_storage
= __q
+ _S_nword(__n
);
1155 iterator __start
= iterator(std::__addressof(*__q
), 0);
1156 this->_M_impl
._M_start
= __start
;
1157 this->_M_impl
._M_finish
= __start
+ difference_type(__n
);
1162 _M_initialize_value(bool __x
)
1164 if (_Bit_type
* __p
= this->_M_impl
._M_start
._M_p
)
1165 __builtin_memset(__p
, __x
? ~0 : 0,
1166 (this->_M_impl
._M_end_addr() - __p
)
1167 * sizeof(_Bit_type
));
1171 _M_reallocate(size_type __n
);
1173 #if __cplusplus >= 201103L
1178 #if __cplusplus < 201103L
1179 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1180 // 438. Ambiguity in the "do the right thing" clause
1181 template<typename _Integer
>
1183 _M_initialize_dispatch(_Integer __n
, _Integer __x
, __true_type
)
1185 _M_initialize(static_cast<size_type
>(__n
));
1186 _M_initialize_value(__x
);
1189 template<typename _InputIterator
>
1191 _M_initialize_dispatch(_InputIterator __first
, _InputIterator __last
,
1193 { _M_initialize_range(__first
, __last
,
1194 std::__iterator_category(__first
)); }
1197 template<typename _InputIterator
>
1199 _M_initialize_range(_InputIterator __first
, _InputIterator __last
,
1200 std::input_iterator_tag
)
1202 for (; __first
!= __last
; ++__first
)
1203 push_back(*__first
);
1206 template<typename _ForwardIterator
>
1208 _M_initialize_range(_ForwardIterator __first
, _ForwardIterator __last
,
1209 std::forward_iterator_tag
)
1211 const size_type __n
= std::distance(__first
, __last
);
1213 std::copy(__first
, __last
, begin());
1216 #if __cplusplus < 201103L
1217 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1218 // 438. Ambiguity in the "do the right thing" clause
1219 template<typename _Integer
>
1221 _M_assign_dispatch(_Integer __n
, _Integer __val
, __true_type
)
1222 { _M_fill_assign(__n
, __val
); }
1224 template<class _InputIterator
>
1226 _M_assign_dispatch(_InputIterator __first
, _InputIterator __last
,
1228 { _M_assign_aux(__first
, __last
, std::__iterator_category(__first
)); }
1232 _M_fill_assign(size_t __n
, bool __x
)
1236 _M_initialize_value(__x
);
1237 insert(end(), __n
- size(), __x
);
1241 _M_erase_at_end(begin() + __n
);
1242 _M_initialize_value(__x
);
1246 template<typename _InputIterator
>
1248 _M_assign_aux(_InputIterator __first
, _InputIterator __last
,
1249 std::input_iterator_tag
)
1251 iterator __cur
= begin();
1252 for (; __first
!= __last
&& __cur
!= end(); ++__cur
, (void)++__first
)
1254 if (__first
== __last
)
1255 _M_erase_at_end(__cur
);
1257 insert(end(), __first
, __last
);
1260 template<typename _ForwardIterator
>
1262 _M_assign_aux(_ForwardIterator __first
, _ForwardIterator __last
,
1263 std::forward_iterator_tag
)
1265 const size_type __len
= std::distance(__first
, __last
);
1267 _M_erase_at_end(std::copy(__first
, __last
, begin()));
1270 _ForwardIterator __mid
= __first
;
1271 std::advance(__mid
, size());
1272 std::copy(__first
, __mid
, begin());
1273 insert(end(), __mid
, __last
);
1277 #if __cplusplus < 201103L
1278 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1279 // 438. Ambiguity in the "do the right thing" clause
1280 template<typename _Integer
>
1282 _M_insert_dispatch(iterator __pos
, _Integer __n
, _Integer __x
,
1284 { _M_fill_insert(__pos
, __n
, __x
); }
1286 template<typename _InputIterator
>
1288 _M_insert_dispatch(iterator __pos
,
1289 _InputIterator __first
, _InputIterator __last
,
1291 { _M_insert_range(__pos
, __first
, __last
,
1292 std::__iterator_category(__first
)); }
1296 _M_fill_insert(iterator __position
, size_type __n
, bool __x
);
1298 template<typename _InputIterator
>
1300 _M_insert_range(iterator __pos
, _InputIterator __first
,
1301 _InputIterator __last
, std::input_iterator_tag
)
1303 for (; __first
!= __last
; ++__first
)
1305 __pos
= insert(__pos
, *__first
);
1310 template<typename _ForwardIterator
>
1312 _M_insert_range(iterator __position
, _ForwardIterator __first
,
1313 _ForwardIterator __last
, std::forward_iterator_tag
);
1316 _M_insert_aux(iterator __position
, bool __x
);
1319 _M_check_len(size_type __n
, const char* __s
) const
1321 if (max_size() - size() < __n
)
1322 __throw_length_error(__N(__s
));
1324 const size_type __len
= size() + std::max(size(), __n
);
1325 return (__len
< size() || __len
> max_size()) ? max_size() : __len
;
1329 _M_erase_at_end(iterator __pos
)
1330 { this->_M_impl
._M_finish
= __pos
; }
1333 _M_erase(iterator __pos
);
1336 _M_erase(iterator __first
, iterator __last
);
1339 _GLIBCXX_END_NAMESPACE_CONTAINER
1342 __fill_bvector(_GLIBCXX_STD_C::_Bit_type
* __v
,
1343 unsigned int __first
, unsigned int __last
, bool __x
)
1345 using _GLIBCXX_STD_C::_Bit_type
;
1346 using _GLIBCXX_STD_C::_S_word_bit
;
1347 const _Bit_type __fmask
= ~0ul << __first
;
1348 const _Bit_type __lmask
= ~0ul >> (_S_word_bit
- __last
);
1349 const _Bit_type __mask
= __fmask
& __lmask
;
1358 __fill_a1(_GLIBCXX_STD_C::_Bit_iterator __first
,
1359 _GLIBCXX_STD_C::_Bit_iterator __last
, const bool& __x
)
1361 using _GLIBCXX_STD_C::_Bit_type
;
1362 using _GLIBCXX_STD_C::_S_word_bit
;
1363 if (__first
._M_p
!= __last
._M_p
)
1365 _Bit_type
* __first_p
= __first
._M_p
;
1366 if (__first
._M_offset
!= 0)
1367 __fill_bvector(__first_p
++, __first
._M_offset
, _S_word_bit
, __x
);
1369 __builtin_memset(__first_p
, __x
? ~0 : 0,
1370 (__last
._M_p
- __first_p
) * sizeof(_Bit_type
));
1372 if (__last
._M_offset
!= 0)
1373 __fill_bvector(__last
._M_p
, 0, __last
._M_offset
, __x
);
1375 else if (__first
._M_offset
!= __last
._M_offset
)
1376 __fill_bvector(__first
._M_p
, __first
._M_offset
, __last
._M_offset
, __x
);
1379 #if __cplusplus >= 201103L
1381 /// std::hash specialization for vector<bool>.
1382 template<typename _Alloc
>
1383 struct hash
<_GLIBCXX_STD_C::vector
<bool, _Alloc
>>
1384 : public __hash_base
<size_t, _GLIBCXX_STD_C::vector
<bool, _Alloc
>>
1387 operator()(const _GLIBCXX_STD_C::vector
<bool, _Alloc
>&) const noexcept
;
1391 _GLIBCXX_END_NAMESPACE_VERSION