56 #ifndef _STL_BVECTOR_H
57 #define _STL_BVECTOR_H 1
59 #if __cplusplus >= 201103L
60 #include <initializer_list>
63 namespace std _GLIBCXX_VISIBILITY(default)
65 _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
67 typedef unsigned long _Bit_type;
68 enum { _S_word_bit = int(__CHAR_BIT__ *
sizeof(_Bit_type)) };
75 _Bit_reference(_Bit_type * __x, _Bit_type __y)
76 : _M_p(__x), _M_mask(__y) { }
78 _Bit_reference() _GLIBCXX_NOEXCEPT : _M_p(0), _M_mask(0) { }
80 operator bool() const _GLIBCXX_NOEXCEPT
81 {
return !!(*_M_p & _M_mask); }
84 operator=(
bool __x) _GLIBCXX_NOEXCEPT
94 operator=(
const _Bit_reference& __x) _GLIBCXX_NOEXCEPT
95 {
return *
this = bool(__x); }
98 operator==(
const _Bit_reference& __x)
const
99 {
return bool(*
this) == bool(__x); }
102 operator<(
const _Bit_reference& __x)
const
103 {
return !bool(*
this) && bool(__x); }
106 flip() _GLIBCXX_NOEXCEPT
107 { *_M_p ^= _M_mask; }
110 #if __cplusplus >= 201103L
112 swap(_Bit_reference __x, _Bit_reference __y) noexcept
120 swap(_Bit_reference __x,
bool& __y) noexcept
128 swap(
bool& __x, _Bit_reference __y) noexcept
136 struct _Bit_iterator_base
137 :
public std::iterator<std::random_access_iterator_tag, bool>
140 unsigned int _M_offset;
142 _Bit_iterator_base(_Bit_type * __x,
unsigned int __y)
143 : _M_p(__x), _M_offset(__y) { }
148 if (_M_offset++ ==
int(_S_word_bit) - 1)
158 if (_M_offset-- == 0)
160 _M_offset = int(_S_word_bit) - 1;
166 _M_incr(ptrdiff_t __i)
169 _M_p += __n / int(_S_word_bit);
170 __n = __n % int(_S_word_bit);
173 __n += int(_S_word_bit);
176 _M_offset =
static_cast<unsigned int>(__n);
180 operator==(
const _Bit_iterator_base& __i)
const
181 {
return _M_p == __i._M_p && _M_offset == __i._M_offset; }
184 operator<(
const _Bit_iterator_base& __i)
const
186 return _M_p < __i._M_p
187 || (_M_p == __i._M_p && _M_offset < __i._M_offset);
191 operator!=(
const _Bit_iterator_base& __i)
const
192 {
return !(*
this == __i); }
195 operator>(
const _Bit_iterator_base& __i)
const
196 {
return __i < *
this; }
199 operator<=(
const _Bit_iterator_base& __i)
const
200 {
return !(__i < *
this); }
203 operator>=(
const _Bit_iterator_base& __i)
const
204 {
return !(*
this < __i); }
208 operator-(
const _Bit_iterator_base& __x,
const _Bit_iterator_base& __y)
210 return (
int(_S_word_bit) * (__x._M_p - __y._M_p)
211 + __x._M_offset - __y._M_offset);
214 struct _Bit_iterator :
public _Bit_iterator_base
216 typedef _Bit_reference reference;
217 typedef _Bit_reference* pointer;
218 typedef _Bit_iterator iterator;
220 _Bit_iterator() : _Bit_iterator_base(0, 0) { }
222 _Bit_iterator(_Bit_type * __x,
unsigned int __y)
223 : _Bit_iterator_base(__x, __y) { }
227 {
return reference(_M_p, 1UL << _M_offset); }
239 iterator __tmp = *
this;
254 iterator __tmp = *
this;
260 operator+=(difference_type __i)
267 operator-=(difference_type __i)
276 iterator __tmp = *
this;
281 operator-(difference_type __i)
const
283 iterator __tmp = *
this;
288 operator[](difference_type __i)
const
289 {
return *(*
this + __i); }
293 operator+(ptrdiff_t __n,
const _Bit_iterator& __x)
294 {
return __x + __n; }
296 struct _Bit_const_iterator :
public _Bit_iterator_base
298 typedef bool reference;
299 typedef bool const_reference;
300 typedef const bool* pointer;
301 typedef _Bit_const_iterator const_iterator;
303 _Bit_const_iterator() : _Bit_iterator_base(0, 0) { }
305 _Bit_const_iterator(_Bit_type * __x,
unsigned int __y)
306 : _Bit_iterator_base(__x, __y) { }
308 _Bit_const_iterator(
const _Bit_iterator& __x)
309 : _Bit_iterator_base(__x._M_p, __x._M_offset) { }
313 {
return _Bit_reference(_M_p, 1UL << _M_offset); }
325 const_iterator __tmp = *
this;
340 const_iterator __tmp = *
this;
346 operator+=(difference_type __i)
353 operator-=(difference_type __i)
362 const_iterator __tmp = *
this;
367 operator-(difference_type __i)
const
369 const_iterator __tmp = *
this;
374 operator[](difference_type __i)
const
375 {
return *(*
this + __i); }
378 inline _Bit_const_iterator
379 operator+(ptrdiff_t __n,
const _Bit_const_iterator& __x)
380 {
return __x + __n; }
383 __fill_bvector(_Bit_iterator __first, _Bit_iterator __last,
bool __x)
385 for (; __first != __last; ++__first)
390 fill(_Bit_iterator __first, _Bit_iterator __last,
const bool& __x)
392 if (__first._M_p != __last._M_p)
394 std::fill(__first._M_p + 1, __last._M_p, __x ? ~0 : 0);
395 __fill_bvector(__first, _Bit_iterator(__first._M_p + 1, 0), __x);
396 __fill_bvector(_Bit_iterator(__last._M_p, 0), __last, __x);
399 __fill_bvector(__first, __last, __x);
402 template<
typename _Alloc>
405 typedef typename _Alloc::template rebind<_Bit_type>::other
409 :
public _Bit_alloc_type
411 _Bit_iterator _M_start;
412 _Bit_iterator _M_finish;
413 _Bit_type* _M_end_of_storage;
416 : _Bit_alloc_type(), _M_start(), _M_finish(), _M_end_of_storage(0)
419 _Bvector_impl(
const _Bit_alloc_type& __a)
420 : _Bit_alloc_type(__a), _M_start(), _M_finish(), _M_end_of_storage(0)
423 #if __cplusplus >= 201103L
424 _Bvector_impl(_Bit_alloc_type&& __a)
425 : _Bit_alloc_type(
std::
move(__a)), _M_start(), _M_finish(),
432 typedef _Alloc allocator_type;
435 _M_get_Bit_allocator() _GLIBCXX_NOEXCEPT
436 {
return *
static_cast<_Bit_alloc_type*
>(&this->_M_impl); }
438 const _Bit_alloc_type&
439 _M_get_Bit_allocator() const _GLIBCXX_NOEXCEPT
440 {
return *
static_cast<const _Bit_alloc_type*
>(&this->_M_impl); }
443 get_allocator() const _GLIBCXX_NOEXCEPT
444 {
return allocator_type(_M_get_Bit_allocator()); }
449 _Bvector_base(
const allocator_type& __a)
452 #if __cplusplus >= 201103L
453 _Bvector_base(_Bvector_base&& __x) noexcept
454 : _M_impl(
std::
move(__x._M_get_Bit_allocator()))
456 this->_M_impl._M_start = __x._M_impl._M_start;
457 this->_M_impl._M_finish = __x._M_impl._M_finish;
458 this->_M_impl._M_end_of_storage = __x._M_impl._M_end_of_storage;
459 __x._M_impl._M_start = _Bit_iterator();
460 __x._M_impl._M_finish = _Bit_iterator();
461 __x._M_impl._M_end_of_storage = 0;
466 { this->_M_deallocate(); }
469 _Bvector_impl _M_impl;
472 _M_allocate(
size_t __n)
473 {
return _M_impl.allocate(_S_nword(__n)); }
478 if (_M_impl._M_start._M_p)
479 _M_impl.deallocate(_M_impl._M_start._M_p,
480 _M_impl._M_end_of_storage - _M_impl._M_start._M_p);
485 {
return (__n +
int(_S_word_bit) - 1) / int(_S_word_bit); }
488 _GLIBCXX_END_NAMESPACE_CONTAINER
494 namespace std _GLIBCXX_VISIBILITY(default)
496 _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
517 template<
typename _Alloc>
518 class vector<bool, _Alloc> :
protected _Bvector_base<_Alloc>
520 typedef _Bvector_base<_Alloc> _Base;
522 #if __cplusplus >= 201103L
523 template<
typename>
friend struct hash;
527 typedef bool value_type;
528 typedef size_t size_type;
529 typedef ptrdiff_t difference_type;
530 typedef _Bit_reference reference;
531 typedef bool const_reference;
532 typedef _Bit_reference* pointer;
533 typedef const bool* const_pointer;
534 typedef _Bit_iterator iterator;
535 typedef _Bit_const_iterator const_iterator;
538 typedef _Alloc allocator_type;
540 allocator_type get_allocator()
const
541 {
return _Base::get_allocator(); }
544 using _Base::_M_allocate;
545 using _Base::_M_deallocate;
546 using _Base::_S_nword;
547 using _Base::_M_get_Bit_allocator;
554 vector(
const allocator_type& __a)
557 #if __cplusplus >= 201103L
559 vector(size_type __n,
const allocator_type& __a = allocator_type())
563 vector(size_type __n,
const bool& __value,
564 const allocator_type& __a = allocator_type())
568 std::fill(this->_M_impl._M_start._M_p, this->_M_impl._M_end_of_storage,
573 vector(size_type __n,
const bool& __value =
bool(),
574 const allocator_type& __a = allocator_type())
578 std::fill(this->_M_impl._M_start._M_p, this->_M_impl._M_end_of_storage,
584 : _Base(__x._M_get_Bit_allocator())
586 _M_initialize(__x.
size());
587 _M_copy_aligned(__x.
begin(), __x.
end(), this->_M_impl._M_start);
590 #if __cplusplus >= 201103L
594 vector(initializer_list<bool> __l,
595 const allocator_type& __a = allocator_type())
598 _M_initialize_range(__l.begin(), __l.end(),
603 #if __cplusplus >= 201103L
604 template<
typename _InputIterator,
605 typename = std::_RequireInputIter<_InputIterator>>
606 vector(_InputIterator __first, _InputIterator __last,
607 const allocator_type& __a = allocator_type())
609 { _M_initialize_dispatch(__first, __last, __false_type()); }
611 template<
typename _InputIterator>
612 vector(_InputIterator __first, _InputIterator __last,
613 const allocator_type& __a = allocator_type())
616 typedef typename std::__is_integer<_InputIterator>::__type _Integral;
617 _M_initialize_dispatch(__first, __last, _Integral());
621 ~vector() _GLIBCXX_NOEXCEPT { }
630 this->_M_deallocate();
631 _M_initialize(__x.
size());
633 this->_M_impl._M_finish = _M_copy_aligned(__x.
begin(), __x.
end(),
638 #if __cplusplus >= 201103L
652 this->
assign (__l.begin(), __l.end());
662 assign(size_type __n,
const bool& __x)
663 { _M_fill_assign(__n, __x); }
665 #if __cplusplus >= 201103L
666 template<
typename _InputIterator,
667 typename = std::_RequireInputIter<_InputIterator>>
669 assign(_InputIterator __first, _InputIterator __last)
670 { _M_assign_dispatch(__first, __last, __false_type()); }
672 template<
typename _InputIterator>
674 assign(_InputIterator __first, _InputIterator __last)
676 typedef typename std::__is_integer<_InputIterator>::__type _Integral;
677 _M_assign_dispatch(__first, __last, _Integral());
681 #if __cplusplus >= 201103L
683 assign(initializer_list<bool> __l)
684 { this->
assign(__l.begin(), __l.end()); }
688 begin() _GLIBCXX_NOEXCEPT
689 {
return this->_M_impl._M_start; }
692 begin()
const _GLIBCXX_NOEXCEPT
693 {
return this->_M_impl._M_start; }
696 end() _GLIBCXX_NOEXCEPT
697 {
return this->_M_impl._M_finish; }
700 end()
const _GLIBCXX_NOEXCEPT
701 {
return this->_M_impl._M_finish; }
704 rbegin() _GLIBCXX_NOEXCEPT
705 {
return reverse_iterator(
end()); }
707 const_reverse_iterator
708 rbegin()
const _GLIBCXX_NOEXCEPT
709 {
return const_reverse_iterator(
end()); }
712 rend() _GLIBCXX_NOEXCEPT
713 {
return reverse_iterator(
begin()); }
715 const_reverse_iterator
716 rend()
const _GLIBCXX_NOEXCEPT
717 {
return const_reverse_iterator(
begin()); }
719 #if __cplusplus >= 201103L
722 {
return this->_M_impl._M_start; }
725 cend()
const noexcept
726 {
return this->_M_impl._M_finish; }
728 const_reverse_iterator
730 {
return const_reverse_iterator(
end()); }
732 const_reverse_iterator
733 crend()
const noexcept
734 {
return const_reverse_iterator(
begin()); }
738 size()
const _GLIBCXX_NOEXCEPT
739 {
return size_type(
end() -
begin()); }
744 const size_type __isize =
745 __gnu_cxx::__numeric_traits<difference_type>::__max
746 - int(_S_word_bit) + 1;
747 const size_type __asize = _M_get_Bit_allocator().max_size();
748 return (__asize <= __isize /
int(_S_word_bit)
749 ? __asize *
int(_S_word_bit) : __isize);
754 {
return size_type(const_iterator(this->_M_impl._M_end_of_storage, 0)
758 empty()
const _GLIBCXX_NOEXCEPT
764 return *iterator(this->_M_impl._M_start._M_p
765 + __n /
int(_S_word_bit), __n %
int(_S_word_bit));
771 return *const_iterator(this->_M_impl._M_start._M_p
772 + __n /
int(_S_word_bit), __n %
int(_S_word_bit));
779 if (__n >= this->
size())
780 __throw_out_of_range(__N(
"vector<bool>::_M_range_check"));
789 at(size_type __n)
const
796 __throw_length_error(__N(
"vector::reserve"));
811 {
return *(
end() - 1); }
815 {
return *(
end() - 1); }
823 data() _GLIBCXX_NOEXCEPT { }
828 if (this->_M_impl._M_finish._M_p != this->_M_impl._M_end_of_storage)
829 *this->_M_impl._M_finish++ = __x;
831 _M_insert_aux(
end(), __x);
837 std::swap(this->_M_impl._M_start, __x._M_impl._M_start);
838 std::swap(this->_M_impl._M_finish, __x._M_impl._M_finish);
839 std::swap(this->_M_impl._M_end_of_storage,
840 __x._M_impl._M_end_of_storage);
844 std::__alloc_swap<typename _Base::_Bit_alloc_type>::
845 _S_do_it(_M_get_Bit_allocator(), __x._M_get_Bit_allocator());
850 swap(reference __x, reference __y) _GLIBCXX_NOEXCEPT
858 insert(iterator __position,
const bool& __x =
bool())
860 const difference_type __n = __position -
begin();
861 if (this->_M_impl._M_finish._M_p != this->_M_impl._M_end_of_storage
862 && __position ==
end())
863 *this->_M_impl._M_finish++ = __x;
865 _M_insert_aux(__position, __x);
866 return begin() + __n;
869 #if __cplusplus >= 201103L
870 template<
typename _InputIterator,
871 typename = std::_RequireInputIter<_InputIterator>>
873 insert(iterator __position,
874 _InputIterator __first, _InputIterator __last)
875 { _M_insert_dispatch(__position, __first, __last, __false_type()); }
877 template<
typename _InputIterator>
879 insert(iterator __position,
880 _InputIterator __first, _InputIterator __last)
882 typedef typename std::__is_integer<_InputIterator>::__type _Integral;
883 _M_insert_dispatch(__position, __first, __last, _Integral());
888 insert(iterator __position, size_type __n,
const bool& __x)
889 { _M_fill_insert(__position, __n, __x); }
891 #if __cplusplus >= 201103L
892 void insert(iterator __p, initializer_list<bool> __l)
893 { this->
insert(__p, __l.begin(), __l.end()); }
898 { --this->_M_impl._M_finish; }
901 erase(iterator __position)
903 if (__position + 1 !=
end())
904 std::copy(__position + 1,
end(), __position);
905 --this->_M_impl._M_finish;
910 erase(iterator __first, iterator __last)
912 if (__first != __last)
913 _M_erase_at_end(std::copy(__last,
end(), __first));
918 resize(size_type __new_size,
bool __x =
bool())
920 if (__new_size <
size())
921 _M_erase_at_end(
begin() + difference_type(__new_size));
926 #if __cplusplus >= 201103L
929 { _M_shrink_to_fit(); }
933 flip() _GLIBCXX_NOEXCEPT
935 for (_Bit_type * __p = this->_M_impl._M_start._M_p;
936 __p != this->_M_impl._M_end_of_storage; ++__p)
941 clear() _GLIBCXX_NOEXCEPT
942 { _M_erase_at_end(
begin()); }
948 _M_copy_aligned(const_iterator __first, const_iterator __last,
951 _Bit_type* __q = std::copy(__first._M_p, __last._M_p, __result._M_p);
952 return std::copy(const_iterator(__last._M_p, 0), __last,
957 _M_initialize(size_type __n)
959 _Bit_type* __q = this->_M_allocate(__n);
960 this->_M_impl._M_end_of_storage = __q + _S_nword(__n);
961 this->_M_impl._M_start = iterator(__q, 0);
962 this->_M_impl._M_finish = this->_M_impl._M_start + difference_type(__n);
966 _M_reallocate(size_type __n);
968 #if __cplusplus >= 201103L
977 template<
typename _Integer>
979 _M_initialize_dispatch(_Integer __n, _Integer __x, __true_type)
981 _M_initialize(static_cast<size_type>(__n));
982 std::fill(this->_M_impl._M_start._M_p,
983 this->_M_impl._M_end_of_storage, __x ? ~0 : 0);
986 template<
typename _InputIterator>
988 _M_initialize_dispatch(_InputIterator __first, _InputIterator __last,
990 { _M_initialize_range(__first, __last,
993 template<
typename _InputIterator>
995 _M_initialize_range(_InputIterator __first, _InputIterator __last,
998 for (; __first != __last; ++__first)
1002 template<
typename _ForwardIterator>
1004 _M_initialize_range(_ForwardIterator __first, _ForwardIterator __last,
1009 std::copy(__first, __last, this->_M_impl._M_start);
1014 template<
typename _Integer>
1016 _M_assign_dispatch(_Integer __n, _Integer __val, __true_type)
1017 { _M_fill_assign(__n, __val); }
1019 template<
class _InputIterator>
1021 _M_assign_dispatch(_InputIterator __first, _InputIterator __last,
1026 _M_fill_assign(
size_t __n,
bool __x)
1030 std::fill(this->_M_impl._M_start._M_p,
1031 this->_M_impl._M_end_of_storage, __x ? ~0 : 0);
1036 _M_erase_at_end(
begin() + __n);
1037 std::fill(this->_M_impl._M_start._M_p,
1038 this->_M_impl._M_end_of_storage, __x ? ~0 : 0);
1042 template<
typename _InputIterator>
1044 _M_assign_aux(_InputIterator __first, _InputIterator __last,
1047 iterator __cur =
begin();
1048 for (; __first != __last && __cur !=
end(); ++__cur, ++__first)
1050 if (__first == __last)
1051 _M_erase_at_end(__cur);
1056 template<
typename _ForwardIterator>
1058 _M_assign_aux(_ForwardIterator __first, _ForwardIterator __last,
1063 _M_erase_at_end(std::copy(__first, __last,
begin()));
1066 _ForwardIterator __mid = __first;
1068 std::copy(__first, __mid,
begin());
1077 template<
typename _Integer>
1079 _M_insert_dispatch(iterator __pos, _Integer __n, _Integer __x,
1081 { _M_fill_insert(__pos, __n, __x); }
1083 template<
typename _InputIterator>
1085 _M_insert_dispatch(iterator __pos,
1086 _InputIterator __first, _InputIterator __last,
1088 { _M_insert_range(__pos, __first, __last,
1092 _M_fill_insert(iterator __position, size_type __n,
bool __x);
1094 template<
typename _InputIterator>
1096 _M_insert_range(iterator __pos, _InputIterator __first,
1099 for (; __first != __last; ++__first)
1101 __pos =
insert(__pos, *__first);
1106 template<
typename _ForwardIterator>
1108 _M_insert_range(iterator __position, _ForwardIterator __first,
1112 _M_insert_aux(iterator __position,
bool __x);
1115 _M_check_len(size_type __n,
const char* __s)
const
1118 __throw_length_error(__N(__s));
1125 _M_erase_at_end(iterator __pos)
1126 { this->_M_impl._M_finish = __pos; }
1129 _GLIBCXX_END_NAMESPACE_CONTAINER
1132 #if __cplusplus >= 201103L
1136 namespace std _GLIBCXX_VISIBILITY(default)
1138 _GLIBCXX_BEGIN_NAMESPACE_VERSION
1142 template<
typename _Alloc>
1144 :
public __hash_base<size_t, _GLIBCXX_STD_C::vector<bool, _Alloc>>
1147 operator()(
const _GLIBCXX_STD_C::vector<bool, _Alloc>&)
const noexcept;
1150 _GLIBCXX_END_NAMESPACE_VERSION
void resize(size_type __new_size)
Resizes the vector to the specified number of elements.
void push_back(const value_type &__x)
Add data to the end of the vector.
iterator begin() noexcept
A standard container which offers fixed time access to individual elements in any order...
const_reverse_iterator crend() const noexcept
const _Tp & max(const _Tp &, const _Tp &)
This does what you think it does.
iterator_traits< _InputIterator >::difference_type distance(_InputIterator __first, _InputIterator __last)
A generalization of pointer arithmetic.
void _M_range_check(size_type __n) const
Safety check used only from at().
void swap(vector &__x) noexcept(_Alloc_traits::_S_nothrow_swap())
Swaps data with another vector.
size_type capacity() const noexcept
vector()
Default constructor creates no elements.
constexpr std::remove_reference< _Tp >::type && move(_Tp &&__t) noexcept
Convert a value to an rvalue.
Random-access iterators support a superset of bidirectional iterator operations.
reference operator[](size_type __n)
Subscript access to the data contained in the vector.
bool operator>=(const basic_string< _CharT, _Traits, _Alloc > &__lhs, const basic_string< _CharT, _Traits, _Alloc > &__rhs)
Test if string doesn't precede string.
iterator insert(iterator __position, const value_type &__x)
Inserts given value into vector before specified iterator.
void advance(_InputIterator &__i, _Distance __n)
A generalization of pointer arithmetic.
ISO C++ entities toplevel namespace is std.
iterator_traits< _Iter >::iterator_category __iterator_category(const _Iter &)
const_reverse_iterator crbegin() const noexcept
reference at(size_type __n)
Provides access to the data contained in the vector.
const_iterator cbegin() const noexcept
iterator erase(iterator __position)
Remove element at given position.
bool operator<=(const basic_string< _CharT, _Traits, _Alloc > &__lhs, const basic_string< _CharT, _Traits, _Alloc > &__rhs)
Test if string doesn't follow string.
void reserve(size_type __n)
Attempt to preallocate enough memory for specified number of elements.
bool operator<(const basic_string< _CharT, _Traits, _Alloc > &__lhs, const basic_string< _CharT, _Traits, _Alloc > &__rhs)
Test if string precedes string.
vector & operator=(const vector &__x)
Vector assignment operator.
Forward iterators support a superset of input iterator operations.
void assign(size_type __n, const value_type &__val)
Assigns a given value to a vector.
basic_string< _CharT, _Traits, _Alloc > operator+(const basic_string< _CharT, _Traits, _Alloc > &__lhs, const basic_string< _CharT, _Traits, _Alloc > &__rhs)
Concatenate two strings.
reverse_iterator rbegin() noexcept
void swap(_Tp &, _Tp &) noexcept(__and_< is_nothrow_move_constructible< _Tp >, is_nothrow_move_assignable< _Tp >>::value)
Swaps two values.
const_iterator cend() const noexcept
void pop_back()
Removes last element.
ptrdiff_t difference_type
Distance between iterators is represented as this type.
Primary class template hash.
size_type size() const noexcept
reverse_iterator rend() noexcept
size_type max_size() const noexcept
bool empty() const noexcept
bool operator>(const basic_string< _CharT, _Traits, _Alloc > &__lhs, const basic_string< _CharT, _Traits, _Alloc > &__rhs)
Test if string follows string.