57 #ifndef _STL_BVECTOR_H
58 #define _STL_BVECTOR_H 1
62 namespace std _GLIBCXX_VISIBILITY(default)
64 _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
66 typedef unsigned long _Bit_type;
67 enum { _S_word_bit = int(__CHAR_BIT__ *
sizeof(_Bit_type)) };
74 _Bit_reference(_Bit_type * __x, _Bit_type __y)
75 : _M_p(__x), _M_mask(__y) { }
77 _Bit_reference() : _M_p(0), _M_mask(0) { }
80 {
return !!(*_M_p & _M_mask); }
93 operator=(
const _Bit_reference& __x)
94 {
return *
this = bool(__x); }
97 operator==(
const _Bit_reference& __x)
const
98 {
return bool(*
this) == bool(__x); }
101 operator<(
const _Bit_reference& __x)
const
102 {
return !bool(*
this) && bool(__x); }
106 { *_M_p ^= _M_mask; }
109 struct _Bit_iterator_base
110 :
public std::iterator<std::random_access_iterator_tag, bool>
113 unsigned int _M_offset;
115 _Bit_iterator_base(_Bit_type * __x,
unsigned int __y)
116 : _M_p(__x), _M_offset(__y) { }
121 if (_M_offset++ ==
int(_S_word_bit) - 1)
131 if (_M_offset-- == 0)
133 _M_offset = int(_S_word_bit) - 1;
139 _M_incr(ptrdiff_t __i)
142 _M_p += __n / int(_S_word_bit);
143 __n = __n % int(_S_word_bit);
146 __n += int(_S_word_bit);
149 _M_offset =
static_cast<unsigned int>(__n);
153 operator==(
const _Bit_iterator_base& __i)
const
154 {
return _M_p == __i._M_p && _M_offset == __i._M_offset; }
157 operator<(
const _Bit_iterator_base& __i)
const
159 return _M_p < __i._M_p
160 || (_M_p == __i._M_p && _M_offset < __i._M_offset);
164 operator!=(
const _Bit_iterator_base& __i)
const
165 {
return !(*
this == __i); }
168 operator>(
const _Bit_iterator_base& __i)
const
169 {
return __i < *
this; }
172 operator<=(
const _Bit_iterator_base& __i)
const
173 {
return !(__i < *
this); }
176 operator>=(
const _Bit_iterator_base& __i)
const
177 {
return !(*
this < __i); }
181 operator-(
const _Bit_iterator_base& __x,
const _Bit_iterator_base& __y)
183 return (
int(_S_word_bit) * (__x._M_p - __y._M_p)
184 + __x._M_offset - __y._M_offset);
187 struct _Bit_iterator :
public _Bit_iterator_base
189 typedef _Bit_reference reference;
190 typedef _Bit_reference* pointer;
191 typedef _Bit_iterator iterator;
193 _Bit_iterator() : _Bit_iterator_base(0, 0) { }
195 _Bit_iterator(_Bit_type * __x,
unsigned int __y)
196 : _Bit_iterator_base(__x, __y) { }
200 {
return reference(_M_p, 1UL << _M_offset); }
212 iterator __tmp = *
this;
227 iterator __tmp = *
this;
233 operator+=(difference_type __i)
240 operator-=(difference_type __i)
249 iterator __tmp = *
this;
256 iterator __tmp = *
this;
262 {
return *(*
this + __i); }
266 operator+(ptrdiff_t __n,
const _Bit_iterator& __x)
267 {
return __x + __n; }
269 struct _Bit_const_iterator :
public _Bit_iterator_base
271 typedef bool reference;
272 typedef bool const_reference;
273 typedef const bool* pointer;
274 typedef _Bit_const_iterator const_iterator;
276 _Bit_const_iterator() : _Bit_iterator_base(0, 0) { }
278 _Bit_const_iterator(_Bit_type * __x,
unsigned int __y)
279 : _Bit_iterator_base(__x, __y) { }
281 _Bit_const_iterator(
const _Bit_iterator& __x)
282 : _Bit_iterator_base(__x._M_p, __x._M_offset) { }
286 {
return _Bit_reference(_M_p, 1UL << _M_offset); }
298 const_iterator __tmp = *
this;
313 const_iterator __tmp = *
this;
319 operator+=(difference_type __i)
326 operator-=(difference_type __i)
335 const_iterator __tmp = *
this;
342 const_iterator __tmp = *
this;
348 {
return *(*
this + __i); }
351 inline _Bit_const_iterator
352 operator+(ptrdiff_t __n,
const _Bit_const_iterator& __x)
353 {
return __x + __n; }
356 __fill_bvector(_Bit_iterator __first, _Bit_iterator __last,
bool __x)
358 for (; __first != __last; ++__first)
363 fill(_Bit_iterator __first, _Bit_iterator __last,
const bool& __x)
365 if (__first._M_p != __last._M_p)
367 std::fill(__first._M_p + 1, __last._M_p, __x ? ~0 : 0);
368 __fill_bvector(__first, _Bit_iterator(__first._M_p + 1, 0), __x);
369 __fill_bvector(_Bit_iterator(__last._M_p, 0), __last, __x);
372 __fill_bvector(__first, __last, __x);
375 template<
typename _Alloc>
378 typedef typename _Alloc::template rebind<_Bit_type>::other
382 :
public _Bit_alloc_type
384 _Bit_iterator _M_start;
385 _Bit_iterator _M_finish;
386 _Bit_type* _M_end_of_storage;
389 : _Bit_alloc_type(), _M_start(), _M_finish(), _M_end_of_storage(0)
392 _Bvector_impl(
const _Bit_alloc_type& __a)
393 : _Bit_alloc_type(__a), _M_start(), _M_finish(), _M_end_of_storage(0)
398 typedef _Alloc allocator_type;
401 _M_get_Bit_allocator()
402 {
return *
static_cast<_Bit_alloc_type*
>(&this->_M_impl); }
404 const _Bit_alloc_type&
405 _M_get_Bit_allocator()
const
406 {
return *
static_cast<const _Bit_alloc_type*
>(&this->_M_impl); }
409 get_allocator()
const
410 {
return allocator_type(_M_get_Bit_allocator()); }
415 _Bvector_base(
const allocator_type& __a)
418 #ifdef __GXX_EXPERIMENTAL_CXX0X__
419 _Bvector_base(_Bvector_base&& __x)
420 : _M_impl(__x._M_get_Bit_allocator())
422 this->_M_impl._M_start = __x._M_impl._M_start;
423 this->_M_impl._M_finish = __x._M_impl._M_finish;
424 this->_M_impl._M_end_of_storage = __x._M_impl._M_end_of_storage;
425 __x._M_impl._M_start = _Bit_iterator();
426 __x._M_impl._M_finish = _Bit_iterator();
427 __x._M_impl._M_end_of_storage = 0;
432 { this->_M_deallocate(); }
435 _Bvector_impl _M_impl;
438 _M_allocate(
size_t __n)
439 {
return _M_impl.allocate((__n +
int(_S_word_bit) - 1)
440 /
int(_S_word_bit)); }
445 if (_M_impl._M_start._M_p)
446 _M_impl.deallocate(_M_impl._M_start._M_p,
447 _M_impl._M_end_of_storage - _M_impl._M_start._M_p);
451 _GLIBCXX_END_NAMESPACE_CONTAINER
457 namespace std _GLIBCXX_VISIBILITY(default)
459 _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
478 template<
typename _Alloc>
479 class vector<bool, _Alloc> :
protected _Bvector_base<_Alloc>
481 typedef _Bvector_base<_Alloc> _Base;
483 #ifdef __GXX_EXPERIMENTAL_CXX0X__
484 template<
typename>
friend class hash;
488 typedef bool value_type;
489 typedef size_t size_type;
490 typedef ptrdiff_t difference_type;
491 typedef _Bit_reference reference;
492 typedef bool const_reference;
493 typedef _Bit_reference* pointer;
494 typedef const bool* const_pointer;
495 typedef _Bit_iterator iterator;
496 typedef _Bit_const_iterator const_iterator;
499 typedef _Alloc allocator_type;
501 allocator_type get_allocator()
const
502 {
return _Base::get_allocator(); }
505 using _Base::_M_allocate;
506 using _Base::_M_deallocate;
507 using _Base::_M_get_Bit_allocator;
514 vector(
const allocator_type& __a)
518 vector(size_type __n,
const bool& __value =
bool(),
519 const allocator_type& __a = allocator_type())
523 std::fill(this->_M_impl._M_start._M_p, this->_M_impl._M_end_of_storage,
528 : _Base(__x._M_get_Bit_allocator())
530 _M_initialize(__x.
size());
531 _M_copy_aligned(__x.
begin(), __x.
end(), this->_M_impl._M_start);
534 #ifdef __GXX_EXPERIMENTAL_CXX0X__
536 : _Base(std::move(__x)) { }
539 const allocator_type& __a = allocator_type())
542 _M_initialize_range(__l.begin(), __l.end(),
547 template<
typename _InputIterator>
548 vector(_InputIterator __first, _InputIterator __last,
549 const allocator_type& __a = allocator_type())
552 typedef typename std::__is_integer<_InputIterator>::__type _Integral;
553 _M_initialize_dispatch(__first, __last, _Integral());
565 this->_M_deallocate();
566 _M_initialize(__x.
size());
568 this->_M_impl._M_finish = _M_copy_aligned(__x.
begin(), __x.
end(),
573 #ifdef __GXX_EXPERIMENTAL_CXX0X__
587 this->
assign (__l.begin(), __l.end());
597 assign(size_type __n,
const bool& __x)
598 { _M_fill_assign(__n, __x); }
600 template<
typename _InputIterator>
602 assign(_InputIterator __first, _InputIterator __last)
604 typedef typename std::__is_integer<_InputIterator>::__type _Integral;
605 _M_assign_dispatch(__first, __last, _Integral());
608 #ifdef __GXX_EXPERIMENTAL_CXX0X__
611 { this->
assign(__l.begin(), __l.end()); }
616 {
return this->_M_impl._M_start; }
620 {
return this->_M_impl._M_start; }
624 {
return this->_M_impl._M_finish; }
628 {
return this->_M_impl._M_finish; }
646 #ifdef __GXX_EXPERIMENTAL_CXX0X__
649 {
return this->_M_impl._M_start; }
653 {
return this->_M_impl._M_finish; }
666 {
return size_type(
end() -
begin()); }
671 const size_type __isize =
672 __gnu_cxx::__numeric_traits<difference_type>::__max
673 - int(_S_word_bit) + 1;
674 const size_type __asize = _M_get_Bit_allocator().max_size();
675 return (__asize <= __isize /
int(_S_word_bit)
676 ? __asize *
int(_S_word_bit) : __isize);
681 {
return size_type(const_iterator(this->_M_impl._M_end_of_storage, 0)
691 return *iterator(this->_M_impl._M_start._M_p
692 + __n /
int(_S_word_bit), __n %
int(_S_word_bit));
698 return *const_iterator(this->_M_impl._M_start._M_p
699 + __n /
int(_S_word_bit), __n %
int(_S_word_bit));
706 if (__n >= this->
size())
707 __throw_out_of_range(__N(
"vector<bool>::_M_range_check"));
716 at(size_type __n)
const
732 {
return *(
end() - 1); }
736 {
return *(
end() - 1); }
749 if (this->_M_impl._M_finish._M_p != this->_M_impl._M_end_of_storage)
750 *this->_M_impl._M_finish++ = __x;
752 _M_insert_aux(
end(), __x);
758 std::swap(this->_M_impl._M_start, __x._M_impl._M_start);
759 std::swap(this->_M_impl._M_finish, __x._M_impl._M_finish);
760 std::swap(this->_M_impl._M_end_of_storage,
761 __x._M_impl._M_end_of_storage);
765 std::__alloc_swap<typename _Base::_Bit_alloc_type>::
766 _S_do_it(_M_get_Bit_allocator(), __x._M_get_Bit_allocator());
771 swap(reference __x, reference __y)
779 insert(iterator __position,
const bool& __x =
bool())
781 const difference_type __n = __position -
begin();
782 if (this->_M_impl._M_finish._M_p != this->_M_impl._M_end_of_storage
783 && __position ==
end())
784 *this->_M_impl._M_finish++ = __x;
786 _M_insert_aux(__position, __x);
787 return begin() + __n;
790 template<
typename _InputIterator>
792 insert(iterator __position,
793 _InputIterator __first, _InputIterator __last)
795 typedef typename std::__is_integer<_InputIterator>::__type _Integral;
796 _M_insert_dispatch(__position, __first, __last, _Integral());
800 insert(iterator __position, size_type __n,
const bool& __x)
801 { _M_fill_insert(__position, __n, __x); }
803 #ifdef __GXX_EXPERIMENTAL_CXX0X__
805 { this->
insert(__p, __l.begin(), __l.end()); }
810 { --this->_M_impl._M_finish; }
813 erase(iterator __position)
815 if (__position + 1 !=
end())
816 std::copy(__position + 1,
end(), __position);
817 --this->_M_impl._M_finish;
822 erase(iterator __first, iterator __last)
824 if (__first != __last)
825 _M_erase_at_end(std::copy(__last,
end(), __first));
830 resize(size_type __new_size,
bool __x =
bool())
832 if (__new_size <
size())
833 _M_erase_at_end(
begin() + difference_type(__new_size));
838 #ifdef __GXX_EXPERIMENTAL_CXX0X__
841 { std::__shrink_to_fit<vector>::_S_do_it(*
this); }
847 for (_Bit_type * __p = this->_M_impl._M_start._M_p;
848 __p != this->_M_impl._M_end_of_storage; ++__p)
854 { _M_erase_at_end(
begin()); }
860 _M_copy_aligned(const_iterator __first, const_iterator __last,
863 _Bit_type* __q = std::copy(__first._M_p, __last._M_p, __result._M_p);
864 return std::copy(const_iterator(__last._M_p, 0), __last,
869 _M_initialize(size_type __n)
871 _Bit_type* __q = this->_M_allocate(__n);
872 this->_M_impl._M_end_of_storage = (__q
873 + ((__n + int(_S_word_bit) - 1)
874 /
int(_S_word_bit)));
875 this->_M_impl._M_start = iterator(__q, 0);
876 this->_M_impl._M_finish = this->_M_impl._M_start + difference_type(__n);
883 template<
typename _Integer>
885 _M_initialize_dispatch(_Integer __n, _Integer __x, __true_type)
887 _M_initialize(static_cast<size_type>(__n));
888 std::fill(this->_M_impl._M_start._M_p,
889 this->_M_impl._M_end_of_storage, __x ? ~0 : 0);
892 template<
typename _InputIterator>
894 _M_initialize_dispatch(_InputIterator __first, _InputIterator __last,
896 { _M_initialize_range(__first, __last,
899 template<
typename _InputIterator>
901 _M_initialize_range(_InputIterator __first, _InputIterator __last,
904 for (; __first != __last; ++__first)
908 template<
typename _ForwardIterator>
910 _M_initialize_range(_ForwardIterator __first, _ForwardIterator __last,
915 std::copy(__first, __last, this->_M_impl._M_start);
920 template<
typename _Integer>
922 _M_assign_dispatch(_Integer __n, _Integer __val, __true_type)
923 { _M_fill_assign(__n, __val); }
925 template<
class _InputIterator>
927 _M_assign_dispatch(_InputIterator __first, _InputIterator __last,
932 _M_fill_assign(
size_t __n,
bool __x)
936 std::fill(this->_M_impl._M_start._M_p,
937 this->_M_impl._M_end_of_storage, __x ? ~0 : 0);
942 _M_erase_at_end(
begin() + __n);
943 std::fill(this->_M_impl._M_start._M_p,
944 this->_M_impl._M_end_of_storage, __x ? ~0 : 0);
948 template<
typename _InputIterator>
950 _M_assign_aux(_InputIterator __first, _InputIterator __last,
953 iterator __cur =
begin();
954 for (; __first != __last && __cur !=
end(); ++__cur, ++__first)
956 if (__first == __last)
957 _M_erase_at_end(__cur);
962 template<
typename _ForwardIterator>
964 _M_assign_aux(_ForwardIterator __first, _ForwardIterator __last,
969 _M_erase_at_end(std::copy(__first, __last,
begin()));
972 _ForwardIterator __mid = __first;
974 std::copy(__first, __mid,
begin());
983 template<
typename _Integer>
985 _M_insert_dispatch(iterator __pos, _Integer __n, _Integer __x,
987 { _M_fill_insert(__pos, __n, __x); }
989 template<
typename _InputIterator>
991 _M_insert_dispatch(iterator __pos,
992 _InputIterator __first, _InputIterator __last,
994 { _M_insert_range(__pos, __first, __last,
998 _M_fill_insert(iterator __position, size_type __n,
bool __x);
1000 template<
typename _InputIterator>
1002 _M_insert_range(iterator __pos, _InputIterator __first,
1005 for (; __first != __last; ++__first)
1007 __pos =
insert(__pos, *__first);
1012 template<
typename _ForwardIterator>
1014 _M_insert_range(iterator __position, _ForwardIterator __first,
1018 _M_insert_aux(iterator __position,
bool __x);
1021 _M_check_len(size_type __n,
const char* __s)
const
1024 __throw_length_error(__N(__s));
1031 _M_erase_at_end(iterator __pos)
1032 { this->_M_impl._M_finish = __pos; }
1035 _GLIBCXX_END_NAMESPACE_CONTAINER
1038 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1042 namespace std _GLIBCXX_VISIBILITY(default)
1044 _GLIBCXX_BEGIN_NAMESPACE_VERSION
1048 template<
typename _Alloc>
1050 :
public __hash_base<size_t, _GLIBCXX_STD_C::vector<bool, _Alloc>>
1053 operator()(
const _GLIBCXX_STD_C::vector<bool, _Alloc>& __b)
const;
1056 _GLIBCXX_END_NAMESPACE_VERSION
1059 #endif // __GXX_EXPERIMENTAL_CXX0X__