1 // vector<bool> specialization -*- C++ -*-
3 // Copyright (C) 2001-2019 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
);
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
; }
190 operator<(const _Bit_iterator_base
& __x
, const _Bit_iterator_base
& __y
)
192 return __x
._M_p
< __y
._M_p
193 || (__x
._M_p
== __y
._M_p
&& __x
._M_offset
< __y
._M_offset
);
197 operator!=(const _Bit_iterator_base
& __x
, const _Bit_iterator_base
& __y
)
198 { return !(__x
== __y
); }
201 operator>(const _Bit_iterator_base
& __x
, const _Bit_iterator_base
& __y
)
202 { return __y
< __x
; }
205 operator<=(const _Bit_iterator_base
& __x
, const _Bit_iterator_base
& __y
)
206 { return !(__y
< __x
); }
209 operator>=(const _Bit_iterator_base
& __x
, const _Bit_iterator_base
& __y
)
210 { return !(__x
< __y
); }
213 operator-(const _Bit_iterator_base
& __x
, const _Bit_iterator_base
& __y
)
215 return (int(_S_word_bit
) * (__x
._M_p
- __y
._M_p
)
216 + __x
._M_offset
- __y
._M_offset
);
220 struct _Bit_iterator
: public _Bit_iterator_base
222 typedef _Bit_reference reference
;
223 typedef _Bit_reference
* pointer
;
224 typedef _Bit_iterator iterator
;
226 _Bit_iterator() : _Bit_iterator_base(0, 0) { }
228 _Bit_iterator(_Bit_type
* __x
, unsigned int __y
)
229 : _Bit_iterator_base(__x
, __y
) { }
232 _M_const_cast() const
237 { return reference(_M_p
, 1UL << _M_offset
); }
249 iterator __tmp
= *this;
264 iterator __tmp
= *this;
270 operator+=(difference_type __i
)
277 operator-=(difference_type __i
)
284 operator[](difference_type __i
) const
285 { return *(*this + __i
); }
288 operator+(const iterator
& __x
, difference_type __n
)
290 iterator __tmp
= __x
;
296 operator+(difference_type __n
, const iterator
& __x
)
297 { return __x
+ __n
; }
300 operator-(const iterator
& __x
, difference_type __n
)
302 iterator __tmp
= __x
;
308 struct _Bit_const_iterator
: public _Bit_iterator_base
310 typedef bool reference
;
311 typedef bool const_reference
;
312 typedef const bool* pointer
;
313 typedef _Bit_const_iterator const_iterator
;
315 _Bit_const_iterator() : _Bit_iterator_base(0, 0) { }
317 _Bit_const_iterator(_Bit_type
* __x
, unsigned int __y
)
318 : _Bit_iterator_base(__x
, __y
) { }
320 _Bit_const_iterator(const _Bit_iterator
& __x
)
321 : _Bit_iterator_base(__x
._M_p
, __x
._M_offset
) { }
324 _M_const_cast() const
325 { return _Bit_iterator(_M_p
, _M_offset
); }
329 { return _Bit_reference(_M_p
, 1UL << _M_offset
); }
341 const_iterator __tmp
= *this;
356 const_iterator __tmp
= *this;
362 operator+=(difference_type __i
)
369 operator-=(difference_type __i
)
376 operator[](difference_type __i
) const
377 { return *(*this + __i
); }
379 friend const_iterator
380 operator+(const const_iterator
& __x
, difference_type __n
)
382 const_iterator __tmp
= __x
;
387 friend const_iterator
388 operator-(const const_iterator
& __x
, difference_type __n
)
390 const_iterator __tmp
= __x
;
395 friend const_iterator
396 operator+(difference_type __n
, const const_iterator
& __x
)
397 { return __x
+ __n
; }
401 __fill_bvector(_Bit_type
* __v
,
402 unsigned int __first
, unsigned int __last
, bool __x
)
404 const _Bit_type __fmask
= ~0ul << __first
;
405 const _Bit_type __lmask
= ~0ul >> (_S_word_bit
- __last
);
406 const _Bit_type __mask
= __fmask
& __lmask
;
415 fill(_Bit_iterator __first
, _Bit_iterator __last
, const bool& __x
)
417 if (__first
._M_p
!= __last
._M_p
)
419 _Bit_type
* __first_p
= __first
._M_p
;
420 if (__first
._M_offset
!= 0)
421 __fill_bvector(__first_p
++, __first
._M_offset
, _S_word_bit
, __x
);
423 __builtin_memset(__first_p
, __x
? ~0 : 0,
424 (__last
._M_p
- __first_p
) * sizeof(_Bit_type
));
426 if (__last
._M_offset
!= 0)
427 __fill_bvector(__last
._M_p
, 0, __last
._M_offset
, __x
);
429 else if (__first
._M_offset
!= __last
._M_offset
)
430 __fill_bvector(__first
._M_p
, __first
._M_offset
, __last
._M_offset
, __x
);
433 template<typename _Alloc
>
436 typedef typename
__gnu_cxx::__alloc_traits
<_Alloc
>::template
437 rebind
<_Bit_type
>::other _Bit_alloc_type
;
438 typedef typename
__gnu_cxx::__alloc_traits
<_Bit_alloc_type
>
440 typedef typename
_Bit_alloc_traits::pointer _Bit_pointer
;
442 struct _Bvector_impl_data
444 _Bit_iterator _M_start
;
445 _Bit_iterator _M_finish
;
446 _Bit_pointer _M_end_of_storage
;
448 _Bvector_impl_data() _GLIBCXX_NOEXCEPT
449 : _M_start(), _M_finish(), _M_end_of_storage()
452 #if __cplusplus >= 201103L
453 _Bvector_impl_data(_Bvector_impl_data
&& __x
) noexcept
454 : _M_start(__x
._M_start
), _M_finish(__x
._M_finish
)
455 , _M_end_of_storage(__x
._M_end_of_storage
)
459 _M_move_data(_Bvector_impl_data
&& __x
) noexcept
461 this->_M_start
= __x
._M_start
;
462 this->_M_finish
= __x
._M_finish
;
463 this->_M_end_of_storage
= __x
._M_end_of_storage
;
469 _M_reset() _GLIBCXX_NOEXCEPT
471 _M_start
= _M_finish
= _Bit_iterator();
472 _M_end_of_storage
= _Bit_pointer();
477 : 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 _Bvector_impl(_Bvector_impl
&&) = default;
494 _M_end_addr() const _GLIBCXX_NOEXCEPT
496 if (this->_M_end_of_storage
)
497 return std::__addressof(this->_M_end_of_storage
[-1]) + 1;
503 typedef _Alloc allocator_type
;
506 _M_get_Bit_allocator() _GLIBCXX_NOEXCEPT
507 { return this->_M_impl
; }
509 const _Bit_alloc_type
&
510 _M_get_Bit_allocator() const _GLIBCXX_NOEXCEPT
511 { return this->_M_impl
; }
514 get_allocator() const _GLIBCXX_NOEXCEPT
515 { return allocator_type(_M_get_Bit_allocator()); }
517 #if __cplusplus >= 201103L
518 _Bvector_base() = default;
523 _Bvector_base(const allocator_type
& __a
)
526 #if __cplusplus >= 201103L
527 _Bvector_base(_Bvector_base
&&) = default;
531 { this->_M_deallocate(); }
534 _Bvector_impl _M_impl
;
537 _M_allocate(size_t __n
)
538 { return _Bit_alloc_traits::allocate(_M_impl
, _S_nword(__n
)); }
543 if (_M_impl
._M_start
._M_p
)
545 const size_t __n
= _M_impl
._M_end_addr() - _M_impl
._M_start
._M_p
;
546 _Bit_alloc_traits::deallocate(_M_impl
,
547 _M_impl
._M_end_of_storage
- __n
,
553 #if __cplusplus >= 201103L
555 _M_move_data(_Bvector_base
&& __x
) noexcept
556 { _M_impl
._M_move_data(std::move(__x
._M_impl
)); }
561 { return (__n
+ int(_S_word_bit
) - 1) / int(_S_word_bit
); }
564 _GLIBCXX_END_NAMESPACE_CONTAINER
565 _GLIBCXX_END_NAMESPACE_VERSION
568 // Declare a partial specialization of vector<T, Alloc>.
569 #include <bits/stl_vector.h>
571 namespace std
_GLIBCXX_VISIBILITY(default)
573 _GLIBCXX_BEGIN_NAMESPACE_VERSION
574 _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
577 * @brief A specialization of vector for booleans which offers fixed time
578 * access to individual elements in any order.
582 * @tparam _Alloc Allocator type.
584 * Note that vector<bool> does not actually meet the requirements for being
585 * a container. This is because the reference and pointer types are not
586 * really references and pointers to bool. See DR96 for details. @see
587 * vector for function documentation.
589 * In some terminology a %vector can be described as a dynamic
590 * C-style array, it offers fast and efficient access to individual
591 * elements in any order and saves the user from worrying about
592 * memory and size allocation. Subscripting ( @c [] ) access is
593 * also provided as with C-style arrays.
595 template<typename _Alloc
>
596 class vector
<bool, _Alloc
> : protected _Bvector_base
<_Alloc
>
598 typedef _Bvector_base
<_Alloc
> _Base
;
599 typedef typename
_Base::_Bit_pointer _Bit_pointer
;
600 typedef typename
_Base::_Bit_alloc_traits _Bit_alloc_traits
;
602 #if __cplusplus >= 201103L
603 friend struct std::hash
<vector
>;
607 typedef bool value_type
;
608 typedef size_t size_type
;
609 typedef ptrdiff_t difference_type
;
610 typedef _Bit_reference reference
;
611 typedef bool const_reference
;
612 typedef _Bit_reference
* pointer
;
613 typedef const bool* const_pointer
;
614 typedef _Bit_iterator iterator
;
615 typedef _Bit_const_iterator const_iterator
;
616 typedef std::reverse_iterator
<const_iterator
> const_reverse_iterator
;
617 typedef std::reverse_iterator
<iterator
> reverse_iterator
;
618 typedef _Alloc allocator_type
;
621 get_allocator() const
622 { return _Base::get_allocator(); }
625 using _Base::_M_allocate
;
626 using _Base::_M_deallocate
;
627 using _Base::_S_nword
;
628 using _Base::_M_get_Bit_allocator
;
631 #if __cplusplus >= 201103L
638 vector(const allocator_type
& __a
)
641 #if __cplusplus >= 201103L
643 vector(size_type __n
, const allocator_type
& __a
= allocator_type())
644 : vector(__n
, false, __a
)
647 vector(size_type __n
, const bool& __value
,
648 const allocator_type
& __a
= allocator_type())
651 vector(size_type __n
, const bool& __value
= bool(),
652 const allocator_type
& __a
= allocator_type())
657 _M_initialize_value(__value
);
660 vector(const vector
& __x
)
661 : _Base(_Bit_alloc_traits::_S_select_on_copy(__x
._M_get_Bit_allocator()))
663 _M_initialize(__x
.size());
664 _M_copy_aligned(__x
.begin(), __x
.end(), this->_M_impl
._M_start
);
667 #if __cplusplus >= 201103L
668 vector(vector
&&) = default;
670 vector(vector
&& __x
, const allocator_type
& __a
)
671 noexcept(_Bit_alloc_traits::_S_always_equal())
674 if (__x
.get_allocator() == __a
)
675 this->_M_move_data(std::move(__x
));
678 _M_initialize(__x
.size());
679 _M_copy_aligned(__x
.begin(), __x
.end(), begin());
684 vector(const vector
& __x
, const allocator_type
& __a
)
687 _M_initialize(__x
.size());
688 _M_copy_aligned(__x
.begin(), __x
.end(), this->_M_impl
._M_start
);
691 vector(initializer_list
<bool> __l
,
692 const allocator_type
& __a
= allocator_type())
695 _M_initialize_range(__l
.begin(), __l
.end(),
696 random_access_iterator_tag());
700 #if __cplusplus >= 201103L
701 template<typename _InputIterator
,
702 typename
= std::_RequireInputIter
<_InputIterator
>>
703 vector(_InputIterator __first
, _InputIterator __last
,
704 const allocator_type
& __a
= allocator_type())
706 { _M_initialize_dispatch(__first
, __last
, __false_type()); }
708 template<typename _InputIterator
>
709 vector(_InputIterator __first
, _InputIterator __last
,
710 const allocator_type
& __a
= allocator_type())
713 typedef typename
std::__is_integer
<_InputIterator
>::__type _Integral
;
714 _M_initialize_dispatch(__first
, __last
, _Integral());
718 ~vector() _GLIBCXX_NOEXCEPT
{ }
721 operator=(const vector
& __x
)
725 #if __cplusplus >= 201103L
726 if (_Bit_alloc_traits::_S_propagate_on_copy_assign())
728 if (this->_M_get_Bit_allocator() != __x
._M_get_Bit_allocator())
730 this->_M_deallocate();
731 std::__alloc_on_copy(_M_get_Bit_allocator(),
732 __x
._M_get_Bit_allocator());
733 _M_initialize(__x
.size());
736 std::__alloc_on_copy(_M_get_Bit_allocator(),
737 __x
._M_get_Bit_allocator());
740 if (__x
.size() > capacity())
742 this->_M_deallocate();
743 _M_initialize(__x
.size());
745 this->_M_impl
._M_finish
= _M_copy_aligned(__x
.begin(), __x
.end(),
750 #if __cplusplus >= 201103L
752 operator=(vector
&& __x
) noexcept(_Bit_alloc_traits::_S_nothrow_move())
754 if (_Bit_alloc_traits::_S_propagate_on_move_assign()
755 || this->_M_get_Bit_allocator() == __x
._M_get_Bit_allocator())
757 this->_M_deallocate();
758 this->_M_move_data(std::move(__x
));
759 std::__alloc_on_move(_M_get_Bit_allocator(),
760 __x
._M_get_Bit_allocator());
764 if (__x
.size() > capacity())
766 this->_M_deallocate();
767 _M_initialize(__x
.size());
769 this->_M_impl
._M_finish
= _M_copy_aligned(__x
.begin(), __x
.end(),
777 operator=(initializer_list
<bool> __l
)
779 this->assign (__l
.begin(), __l
.end());
784 // assign(), a generalized assignment member function. Two
785 // versions: one that takes a count, and one that takes a range.
786 // The range version is a member template, so we dispatch on whether
787 // or not the type is an integer.
789 assign(size_type __n
, const bool& __x
)
790 { _M_fill_assign(__n
, __x
); }
792 #if __cplusplus >= 201103L
793 template<typename _InputIterator
,
794 typename
= std::_RequireInputIter
<_InputIterator
>>
796 assign(_InputIterator __first
, _InputIterator __last
)
797 { _M_assign_aux(__first
, __last
, std::__iterator_category(__first
)); }
799 template<typename _InputIterator
>
801 assign(_InputIterator __first
, _InputIterator __last
)
803 typedef typename
std::__is_integer
<_InputIterator
>::__type _Integral
;
804 _M_assign_dispatch(__first
, __last
, _Integral());
808 #if __cplusplus >= 201103L
810 assign(initializer_list
<bool> __l
)
811 { _M_assign_aux(__l
.begin(), __l
.end(), random_access_iterator_tag()); }
815 begin() _GLIBCXX_NOEXCEPT
816 { return iterator(this->_M_impl
._M_start
._M_p
, 0); }
819 begin() const _GLIBCXX_NOEXCEPT
820 { return const_iterator(this->_M_impl
._M_start
._M_p
, 0); }
823 end() _GLIBCXX_NOEXCEPT
824 { return this->_M_impl
._M_finish
; }
827 end() const _GLIBCXX_NOEXCEPT
828 { return this->_M_impl
._M_finish
; }
831 rbegin() _GLIBCXX_NOEXCEPT
832 { return reverse_iterator(end()); }
834 const_reverse_iterator
835 rbegin() const _GLIBCXX_NOEXCEPT
836 { return const_reverse_iterator(end()); }
839 rend() _GLIBCXX_NOEXCEPT
840 { return reverse_iterator(begin()); }
842 const_reverse_iterator
843 rend() const _GLIBCXX_NOEXCEPT
844 { return const_reverse_iterator(begin()); }
846 #if __cplusplus >= 201103L
848 cbegin() const noexcept
849 { return const_iterator(this->_M_impl
._M_start
._M_p
, 0); }
852 cend() const noexcept
853 { return this->_M_impl
._M_finish
; }
855 const_reverse_iterator
856 crbegin() const noexcept
857 { return const_reverse_iterator(end()); }
859 const_reverse_iterator
860 crend() const noexcept
861 { return const_reverse_iterator(begin()); }
865 size() const _GLIBCXX_NOEXCEPT
866 { return size_type(end() - begin()); }
869 max_size() const _GLIBCXX_NOEXCEPT
871 const size_type __isize
=
872 __gnu_cxx::__numeric_traits
<difference_type
>::__max
873 - int(_S_word_bit
) + 1;
874 const size_type __asize
875 = _Bit_alloc_traits::max_size(_M_get_Bit_allocator());
876 return (__asize
<= __isize
/ int(_S_word_bit
)
877 ? __asize
* int(_S_word_bit
) : __isize
);
881 capacity() const _GLIBCXX_NOEXCEPT
882 { return size_type(const_iterator(this->_M_impl
._M_end_addr(), 0)
885 _GLIBCXX_NODISCARD
bool
886 empty() const _GLIBCXX_NOEXCEPT
887 { return begin() == end(); }
890 operator[](size_type __n
)
892 return *iterator(this->_M_impl
._M_start
._M_p
893 + __n
/ int(_S_word_bit
), __n
% int(_S_word_bit
));
897 operator[](size_type __n
) const
899 return *const_iterator(this->_M_impl
._M_start
._M_p
900 + __n
/ int(_S_word_bit
), __n
% int(_S_word_bit
));
905 _M_range_check(size_type __n
) const
907 if (__n
>= this->size())
908 __throw_out_of_range_fmt(__N("vector<bool>::_M_range_check: __n "
909 "(which is %zu) >= this->size() "
917 { _M_range_check(__n
); return (*this)[__n
]; }
920 at(size_type __n
) const
921 { _M_range_check(__n
); return (*this)[__n
]; }
924 reserve(size_type __n
)
926 if (__n
> max_size())
927 __throw_length_error(__N("vector::reserve"));
928 if (capacity() < __n
)
942 { return *(end() - 1); }
946 { return *(end() - 1); }
948 // _GLIBCXX_RESOLVE_LIB_DEFECTS
949 // DR 464. Suggestion for new member functions in standard containers.
950 // N.B. DR 464 says nothing about vector<bool> but we need something
951 // here due to the way we are implementing DR 464 in the debug-mode
954 data() _GLIBCXX_NOEXCEPT
{ }
959 if (this->_M_impl
._M_finish
._M_p
!= this->_M_impl
._M_end_addr())
960 *this->_M_impl
._M_finish
++ = __x
;
962 _M_insert_aux(end(), __x
);
966 swap(vector
& __x
) _GLIBCXX_NOEXCEPT
968 std::swap(this->_M_impl
._M_start
, __x
._M_impl
._M_start
);
969 std::swap(this->_M_impl
._M_finish
, __x
._M_impl
._M_finish
);
970 std::swap(this->_M_impl
._M_end_of_storage
,
971 __x
._M_impl
._M_end_of_storage
);
972 _Bit_alloc_traits::_S_on_swap(_M_get_Bit_allocator(),
973 __x
._M_get_Bit_allocator());
976 // [23.2.5]/1, third-to-last entry in synopsis listing
978 swap(reference __x
, reference __y
) _GLIBCXX_NOEXCEPT
986 #if __cplusplus >= 201103L
987 insert(const_iterator __position
, const bool& __x
= bool())
989 insert(iterator __position
, const bool& __x
= bool())
992 const difference_type __n
= __position
- begin();
993 if (this->_M_impl
._M_finish
._M_p
!= this->_M_impl
._M_end_addr()
994 && __position
== end())
995 *this->_M_impl
._M_finish
++ = __x
;
997 _M_insert_aux(__position
._M_const_cast(), __x
);
998 return begin() + __n
;
1001 #if __cplusplus >= 201103L
1002 template<typename _InputIterator
,
1003 typename
= std::_RequireInputIter
<_InputIterator
>>
1005 insert(const_iterator __position
,
1006 _InputIterator __first
, _InputIterator __last
)
1008 difference_type __offset
= __position
- cbegin();
1009 _M_insert_dispatch(__position
._M_const_cast(),
1010 __first
, __last
, __false_type());
1011 return begin() + __offset
;
1014 template<typename _InputIterator
>
1016 insert(iterator __position
,
1017 _InputIterator __first
, _InputIterator __last
)
1019 typedef typename
std::__is_integer
<_InputIterator
>::__type _Integral
;
1020 _M_insert_dispatch(__position
, __first
, __last
, _Integral());
1024 #if __cplusplus >= 201103L
1026 insert(const_iterator __position
, size_type __n
, const bool& __x
)
1028 difference_type __offset
= __position
- cbegin();
1029 _M_fill_insert(__position
._M_const_cast(), __n
, __x
);
1030 return begin() + __offset
;
1034 insert(iterator __position
, size_type __n
, const bool& __x
)
1035 { _M_fill_insert(__position
, __n
, __x
); }
1038 #if __cplusplus >= 201103L
1040 insert(const_iterator __p
, initializer_list
<bool> __l
)
1041 { return this->insert(__p
, __l
.begin(), __l
.end()); }
1046 { --this->_M_impl
._M_finish
; }
1049 #if __cplusplus >= 201103L
1050 erase(const_iterator __position
)
1052 erase(iterator __position
)
1054 { return _M_erase(__position
._M_const_cast()); }
1057 #if __cplusplus >= 201103L
1058 erase(const_iterator __first
, const_iterator __last
)
1060 erase(iterator __first
, iterator __last
)
1062 { return _M_erase(__first
._M_const_cast(), __last
._M_const_cast()); }
1065 resize(size_type __new_size
, bool __x
= bool())
1067 if (__new_size
< size())
1068 _M_erase_at_end(begin() + difference_type(__new_size
));
1070 insert(end(), __new_size
- size(), __x
);
1073 #if __cplusplus >= 201103L
1076 { _M_shrink_to_fit(); }
1080 flip() _GLIBCXX_NOEXCEPT
1082 _Bit_type
* const __end
= this->_M_impl
._M_end_addr();
1083 for (_Bit_type
* __p
= this->_M_impl
._M_start
._M_p
; __p
!= __end
; ++__p
)
1088 clear() _GLIBCXX_NOEXCEPT
1089 { _M_erase_at_end(begin()); }
1091 #if __cplusplus >= 201103L
1092 template<typename
... _Args
>
1093 #if __cplusplus > 201402L
1098 emplace_back(_Args
&&... __args
)
1100 push_back(bool(__args
...));
1101 #if __cplusplus > 201402L
1106 template<typename
... _Args
>
1108 emplace(const_iterator __pos
, _Args
&&... __args
)
1109 { return insert(__pos
, bool(__args
...)); }
1113 // Precondition: __first._M_offset == 0 && __result._M_offset == 0.
1115 _M_copy_aligned(const_iterator __first
, const_iterator __last
,
1118 _Bit_type
* __q
= std::copy(__first
._M_p
, __last
._M_p
, __result
._M_p
);
1119 return std::copy(const_iterator(__last
._M_p
, 0), __last
,
1124 _M_initialize(size_type __n
)
1128 _Bit_pointer __q
= this->_M_allocate(__n
);
1129 this->_M_impl
._M_end_of_storage
= __q
+ _S_nword(__n
);
1130 this->_M_impl
._M_start
= iterator(std::__addressof(*__q
), 0);
1134 this->_M_impl
._M_end_of_storage
= _Bit_pointer();
1135 this->_M_impl
._M_start
= iterator(0, 0);
1137 this->_M_impl
._M_finish
= this->_M_impl
._M_start
+ difference_type(__n
);
1142 _M_initialize_value(bool __x
)
1144 if (_Bit_type
* __p
= this->_M_impl
._M_start
._M_p
)
1145 __builtin_memset(__p
, __x
? ~0 : 0,
1146 (this->_M_impl
._M_end_addr() - __p
)
1147 * sizeof(_Bit_type
));
1151 _M_reallocate(size_type __n
);
1153 #if __cplusplus >= 201103L
1158 // Check whether it's an integral type. If so, it's not an iterator.
1160 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1161 // 438. Ambiguity in the "do the right thing" clause
1162 template<typename _Integer
>
1164 _M_initialize_dispatch(_Integer __n
, _Integer __x
, __true_type
)
1166 _M_initialize(static_cast<size_type
>(__n
));
1167 _M_initialize_value(__x
);
1170 template<typename _InputIterator
>
1172 _M_initialize_dispatch(_InputIterator __first
, _InputIterator __last
,
1174 { _M_initialize_range(__first
, __last
,
1175 std::__iterator_category(__first
)); }
1177 template<typename _InputIterator
>
1179 _M_initialize_range(_InputIterator __first
, _InputIterator __last
,
1180 std::input_iterator_tag
)
1182 for (; __first
!= __last
; ++__first
)
1183 push_back(*__first
);
1186 template<typename _ForwardIterator
>
1188 _M_initialize_range(_ForwardIterator __first
, _ForwardIterator __last
,
1189 std::forward_iterator_tag
)
1191 const size_type __n
= std::distance(__first
, __last
);
1193 std::copy(__first
, __last
, this->_M_impl
._M_start
);
1196 #if __cplusplus < 201103L
1197 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1198 // 438. Ambiguity in the "do the right thing" clause
1199 template<typename _Integer
>
1201 _M_assign_dispatch(_Integer __n
, _Integer __val
, __true_type
)
1202 { _M_fill_assign(__n
, __val
); }
1204 template<class _InputIterator
>
1206 _M_assign_dispatch(_InputIterator __first
, _InputIterator __last
,
1208 { _M_assign_aux(__first
, __last
, std::__iterator_category(__first
)); }
1212 _M_fill_assign(size_t __n
, bool __x
)
1216 _M_initialize_value(__x
);
1217 insert(end(), __n
- size(), __x
);
1221 _M_erase_at_end(begin() + __n
);
1222 _M_initialize_value(__x
);
1226 template<typename _InputIterator
>
1228 _M_assign_aux(_InputIterator __first
, _InputIterator __last
,
1229 std::input_iterator_tag
)
1231 iterator __cur
= begin();
1232 for (; __first
!= __last
&& __cur
!= end(); ++__cur
, (void)++__first
)
1234 if (__first
== __last
)
1235 _M_erase_at_end(__cur
);
1237 insert(end(), __first
, __last
);
1240 template<typename _ForwardIterator
>
1242 _M_assign_aux(_ForwardIterator __first
, _ForwardIterator __last
,
1243 std::forward_iterator_tag
)
1245 const size_type __len
= std::distance(__first
, __last
);
1247 _M_erase_at_end(std::copy(__first
, __last
, begin()));
1250 _ForwardIterator __mid
= __first
;
1251 std::advance(__mid
, size());
1252 std::copy(__first
, __mid
, begin());
1253 insert(end(), __mid
, __last
);
1257 // Check whether it's an integral type. If so, it's not an iterator.
1259 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1260 // 438. Ambiguity in the "do the right thing" clause
1261 template<typename _Integer
>
1263 _M_insert_dispatch(iterator __pos
, _Integer __n
, _Integer __x
,
1265 { _M_fill_insert(__pos
, __n
, __x
); }
1267 template<typename _InputIterator
>
1269 _M_insert_dispatch(iterator __pos
,
1270 _InputIterator __first
, _InputIterator __last
,
1272 { _M_insert_range(__pos
, __first
, __last
,
1273 std::__iterator_category(__first
)); }
1276 _M_fill_insert(iterator __position
, size_type __n
, bool __x
);
1278 template<typename _InputIterator
>
1280 _M_insert_range(iterator __pos
, _InputIterator __first
,
1281 _InputIterator __last
, std::input_iterator_tag
)
1283 for (; __first
!= __last
; ++__first
)
1285 __pos
= insert(__pos
, *__first
);
1290 template<typename _ForwardIterator
>
1292 _M_insert_range(iterator __position
, _ForwardIterator __first
,
1293 _ForwardIterator __last
, std::forward_iterator_tag
);
1296 _M_insert_aux(iterator __position
, bool __x
);
1299 _M_check_len(size_type __n
, const char* __s
) const
1301 if (max_size() - size() < __n
)
1302 __throw_length_error(__N(__s
));
1304 const size_type __len
= size() + std::max(size(), __n
);
1305 return (__len
< size() || __len
> max_size()) ? max_size() : __len
;
1309 _M_erase_at_end(iterator __pos
)
1310 { this->_M_impl
._M_finish
= __pos
; }
1313 _M_erase(iterator __pos
);
1316 _M_erase(iterator __first
, iterator __last
);
1319 _GLIBCXX_END_NAMESPACE_CONTAINER
1320 _GLIBCXX_END_NAMESPACE_VERSION
1323 #if __cplusplus >= 201103L
1325 namespace std
_GLIBCXX_VISIBILITY(default)
1327 _GLIBCXX_BEGIN_NAMESPACE_VERSION
1330 /// std::hash specialization for vector<bool>.
1331 template<typename _Alloc
>
1332 struct hash
<_GLIBCXX_STD_C::vector
<bool, _Alloc
>>
1333 : public __hash_base
<size_t, _GLIBCXX_STD_C::vector
<bool, _Alloc
>>
1336 operator()(const _GLIBCXX_STD_C::vector
<bool, _Alloc
>&) const noexcept
;
1339 _GLIBCXX_END_NAMESPACE_VERSION