57 #ifndef _STL_BVECTOR_H
58 #define _STL_BVECTOR_H 1
60 #include <initializer_list>
62 _GLIBCXX_BEGIN_NESTED_NAMESPACE(
std, _GLIBCXX_STD_D)
64 typedef
unsigned long _Bit_type;
65 enum { _S_word_bit = int(__CHAR_BIT__ *
sizeof(_Bit_type)) };
72 _Bit_reference(_Bit_type * __x, _Bit_type __y)
73 : _M_p(__x), _M_mask(__y) { }
75 _Bit_reference() : _M_p(0), _M_mask(0) { }
78 {
return !!(*_M_p & _M_mask); }
91 operator=(
const _Bit_reference& __x)
92 {
return *
this = bool(__x); }
95 operator==(
const _Bit_reference& __x)
const
96 {
return bool(*
this) == bool(__x); }
99 operator<(
const _Bit_reference& __x)
const
100 {
return !bool(*
this) && bool(__x); }
104 { *_M_p ^= _M_mask; }
107 struct _Bit_iterator_base
108 :
public std::iterator<std::random_access_iterator_tag, bool>
111 unsigned int _M_offset;
113 _Bit_iterator_base(_Bit_type * __x,
unsigned int __y)
114 : _M_p(__x), _M_offset(__y) { }
119 if (_M_offset++ ==
int(_S_word_bit) - 1)
129 if (_M_offset-- == 0)
131 _M_offset = int(_S_word_bit) - 1;
137 _M_incr(ptrdiff_t __i)
139 difference_type __n = __i + _M_offset;
140 _M_p += __n / int(_S_word_bit);
141 __n = __n % int(_S_word_bit);
144 __n += int(_S_word_bit);
147 _M_offset =
static_cast<unsigned int>(__n);
151 operator==(
const _Bit_iterator_base& __i)
const
152 {
return _M_p == __i._M_p && _M_offset == __i._M_offset; }
155 operator<(
const _Bit_iterator_base& __i)
const
157 return _M_p < __i._M_p
158 || (_M_p == __i._M_p && _M_offset < __i._M_offset);
162 operator!=(
const _Bit_iterator_base& __i)
const
163 {
return !(*
this == __i); }
166 operator>(
const _Bit_iterator_base& __i)
const
167 {
return __i < *
this; }
170 operator<=(
const _Bit_iterator_base& __i)
const
171 {
return !(__i < *
this); }
174 operator>=(
const _Bit_iterator_base& __i)
const
175 {
return !(*
this < __i); }
179 operator-(
const _Bit_iterator_base& __x,
const _Bit_iterator_base& __y)
181 return (
int(_S_word_bit) * (__x._M_p - __y._M_p)
182 + __x._M_offset - __y._M_offset);
185 struct _Bit_iterator :
public _Bit_iterator_base
187 typedef _Bit_reference reference;
188 typedef _Bit_reference* pointer;
189 typedef _Bit_iterator iterator;
191 _Bit_iterator() : _Bit_iterator_base(0, 0) { }
193 _Bit_iterator(_Bit_type * __x,
unsigned int __y)
194 : _Bit_iterator_base(__x, __y) { }
198 {
return reference(_M_p, 1UL << _M_offset); }
210 iterator __tmp = *
this;
225 iterator __tmp = *
this;
231 operator+=(difference_type __i)
238 operator-=(difference_type __i)
247 iterator __tmp = *
this;
252 operator-(difference_type __i)
const
254 iterator __tmp = *
this;
259 operator[](difference_type __i)
const
260 {
return *(*
this + __i); }
264 operator+(ptrdiff_t __n,
const _Bit_iterator& __x)
265 {
return __x + __n; }
267 struct _Bit_const_iterator :
public _Bit_iterator_base
269 typedef bool reference;
270 typedef bool const_reference;
271 typedef const bool* pointer;
272 typedef _Bit_const_iterator const_iterator;
274 _Bit_const_iterator() : _Bit_iterator_base(0, 0) { }
276 _Bit_const_iterator(_Bit_type * __x,
unsigned int __y)
277 : _Bit_iterator_base(__x, __y) { }
279 _Bit_const_iterator(
const _Bit_iterator& __x)
280 : _Bit_iterator_base(__x._M_p, __x._M_offset) { }
284 {
return _Bit_reference(_M_p, 1UL << _M_offset); }
296 const_iterator __tmp = *
this;
311 const_iterator __tmp = *
this;
317 operator+=(difference_type __i)
324 operator-=(difference_type __i)
333 const_iterator __tmp = *
this;
338 operator-(difference_type __i)
const
340 const_iterator __tmp = *
this;
345 operator[](difference_type __i)
const
346 {
return *(*
this + __i); }
349 inline _Bit_const_iterator
350 operator+(ptrdiff_t __n,
const _Bit_const_iterator& __x)
351 {
return __x + __n; }
354 __fill_bvector(_Bit_iterator __first, _Bit_iterator __last,
bool __x)
356 for (; __first != __last; ++__first)
361 fill(_Bit_iterator __first, _Bit_iterator __last,
const bool& __x)
363 if (__first._M_p != __last._M_p)
365 std::fill(__first._M_p + 1, __last._M_p, __x ? ~0 : 0);
366 __fill_bvector(__first, _Bit_iterator(__first._M_p + 1, 0), __x);
367 __fill_bvector(_Bit_iterator(__last._M_p, 0), __last, __x);
370 __fill_bvector(__first, __last, __x);
373 template<
typename _Alloc>
376 typedef typename _Alloc::template rebind<_Bit_type>::other
380 :
public _Bit_alloc_type
382 _Bit_iterator _M_start;
383 _Bit_iterator _M_finish;
384 _Bit_type* _M_end_of_storage;
387 : _Bit_alloc_type(), _M_start(), _M_finish(), _M_end_of_storage(0)
390 _Bvector_impl(
const _Bit_alloc_type& __a)
391 : _Bit_alloc_type(__a), _M_start(), _M_finish(), _M_end_of_storage(0)
396 typedef _Alloc allocator_type;
399 _M_get_Bit_allocator()
400 {
return *
static_cast<_Bit_alloc_type*
>(&this->_M_impl); }
402 const _Bit_alloc_type&
403 _M_get_Bit_allocator()
const
404 {
return *
static_cast<const _Bit_alloc_type*
>(&this->_M_impl); }
407 get_allocator()
const
408 {
return allocator_type(_M_get_Bit_allocator()); }
413 _Bvector_base(
const allocator_type& __a)
416 #ifdef __GXX_EXPERIMENTAL_CXX0X__
417 _Bvector_base(_Bvector_base&& __x)
418 : _M_impl(__x._M_get_Bit_allocator())
420 this->_M_impl._M_start = __x._M_impl._M_start;
421 this->_M_impl._M_finish = __x._M_impl._M_finish;
422 this->_M_impl._M_end_of_storage = __x._M_impl._M_end_of_storage;
423 __x._M_impl._M_start = _Bit_iterator();
424 __x._M_impl._M_finish = _Bit_iterator();
425 __x._M_impl._M_end_of_storage = 0;
430 { this->_M_deallocate(); }
433 _Bvector_impl _M_impl;
436 _M_allocate(
size_t __n)
437 {
return _M_impl.allocate((__n +
int(_S_word_bit) - 1)
438 /
int(_S_word_bit)); }
443 if (_M_impl._M_start._M_p)
444 _M_impl.deallocate(_M_impl._M_start._M_p,
445 _M_impl._M_end_of_storage - _M_impl._M_start._M_p);
449 _GLIBCXX_END_NESTED_NAMESPACE
454 _GLIBCXX_BEGIN_NESTED_NAMESPACE(
std, _GLIBCXX_STD_D)
473 template<typename _Alloc>
474 class
vector<
bool, _Alloc> : protected _Bvector_base<_Alloc>
476 typedef _Bvector_base<_Alloc> _Base;
479 typedef bool value_type;
480 typedef size_t size_type;
481 typedef ptrdiff_t difference_type;
482 typedef _Bit_reference reference;
483 typedef bool const_reference;
484 typedef _Bit_reference* pointer;
485 typedef const bool* const_pointer;
486 typedef _Bit_iterator iterator;
487 typedef _Bit_const_iterator const_iterator;
490 typedef _Alloc allocator_type;
492 allocator_type get_allocator()
const
493 {
return _Base::get_allocator(); }
496 using _Base::_M_allocate;
497 using _Base::_M_deallocate;
498 using _Base::_M_get_Bit_allocator;
505 vector(
const allocator_type& __a)
509 vector(size_type __n,
const bool& __value =
bool(),
510 const allocator_type& __a = allocator_type())
514 std::fill(this->_M_impl._M_start._M_p, this->_M_impl._M_end_of_storage,
519 : _Base(__x._M_get_Bit_allocator())
521 _M_initialize(__x.
size());
522 _M_copy_aligned(__x.
begin(), __x.
end(), this->_M_impl._M_start);
525 #ifdef __GXX_EXPERIMENTAL_CXX0X__
527 : _Base(std::forward<_Base>(__x)) { }
529 vector(initializer_list<bool> __l,
530 const allocator_type& __a = allocator_type())
533 _M_initialize_range(__l.begin(), __l.end(),
538 template<
typename _InputIterator>
539 vector(_InputIterator __first, _InputIterator __last,
540 const allocator_type& __a = allocator_type())
543 typedef typename std::__is_integer<_InputIterator>::__type _Integral;
544 _M_initialize_dispatch(__first, __last, _Integral());
550 operator=(
const vector& __x)
554 if (__x.
size() > capacity())
556 this->_M_deallocate();
557 _M_initialize(__x.
size());
559 this->_M_impl._M_finish = _M_copy_aligned(__x.
begin(), __x.
end(),
564 #ifdef __GXX_EXPERIMENTAL_CXX0X__
575 operator=(initializer_list<bool> __l)
577 this->assign (__l.begin(), __l.end());
587 assign(size_type __n,
const bool& __x)
588 { _M_fill_assign(__n, __x); }
590 template<
typename _InputIterator>
592 assign(_InputIterator __first, _InputIterator __last)
594 typedef typename std::__is_integer<_InputIterator>::__type _Integral;
595 _M_assign_dispatch(__first, __last, _Integral());
598 #ifdef __GXX_EXPERIMENTAL_CXX0X__
600 assign(initializer_list<bool> __l)
601 { this->assign(__l.begin(), __l.end()); }
606 {
return this->_M_impl._M_start; }
610 {
return this->_M_impl._M_start; }
614 {
return this->_M_impl._M_finish; }
618 {
return this->_M_impl._M_finish; }
622 {
return reverse_iterator(end()); }
624 const_reverse_iterator
626 {
return const_reverse_iterator(end()); }
630 {
return reverse_iterator(begin()); }
632 const_reverse_iterator
634 {
return const_reverse_iterator(begin()); }
636 #ifdef __GXX_EXPERIMENTAL_CXX0X__
639 {
return this->_M_impl._M_start; }
643 {
return this->_M_impl._M_finish; }
645 const_reverse_iterator
647 {
return const_reverse_iterator(end()); }
649 const_reverse_iterator
651 {
return const_reverse_iterator(begin()); }
656 {
return size_type(end() - begin()); }
661 const size_type __isize =
662 __gnu_cxx::__numeric_traits<difference_type>::__max
663 - int(_S_word_bit) + 1;
664 const size_type __asize = _M_get_Bit_allocator().max_size();
665 return (__asize <= __isize /
int(_S_word_bit)
666 ? __asize *
int(_S_word_bit) : __isize);
671 {
return size_type(const_iterator(this->_M_impl._M_end_of_storage, 0)
676 {
return begin() == end(); }
679 operator[](size_type __n)
681 return *iterator(this->_M_impl._M_start._M_p
682 + __n /
int(_S_word_bit), __n %
int(_S_word_bit));
686 operator[](size_type __n)
const
688 return *const_iterator(this->_M_impl._M_start._M_p
689 + __n /
int(_S_word_bit), __n %
int(_S_word_bit));
694 _M_range_check(size_type __n)
const
696 if (__n >= this->size())
697 __throw_out_of_range(__N(
"vector<bool>::_M_range_check"));
703 { _M_range_check(__n);
return (*
this)[__n]; }
706 at(size_type __n)
const
707 { _M_range_check(__n);
return (*
this)[__n]; }
710 reserve(size_type __n);
722 {
return *(end() - 1); }
726 {
return *(end() - 1); }
739 if (this->_M_impl._M_finish._M_p != this->_M_impl._M_end_of_storage)
740 *this->_M_impl._M_finish++ = __x;
742 _M_insert_aux(end(), __x);
746 #ifdef __GXX_EXPERIMENTAL_CXX0X__
752 std::swap(this->_M_impl._M_start, __x._M_impl._M_start);
753 std::swap(this->_M_impl._M_finish, __x._M_impl._M_finish);
754 std::swap(this->_M_impl._M_end_of_storage,
755 __x._M_impl._M_end_of_storage);
759 std::__alloc_swap<typename _Base::_Bit_alloc_type>::
760 _S_do_it(_M_get_Bit_allocator(), __x._M_get_Bit_allocator());
765 swap(reference __x, reference __y)
773 insert(iterator __position,
const bool& __x =
bool())
775 const difference_type __n = __position - begin();
776 if (this->_M_impl._M_finish._M_p != this->_M_impl._M_end_of_storage
777 && __position == end())
778 *this->_M_impl._M_finish++ = __x;
780 _M_insert_aux(__position, __x);
781 return begin() + __n;
784 template<
typename _InputIterator>
786 insert(iterator __position,
787 _InputIterator __first, _InputIterator __last)
789 typedef typename std::__is_integer<_InputIterator>::__type _Integral;
790 _M_insert_dispatch(__position, __first, __last, _Integral());
794 insert(iterator __position, size_type __n,
const bool& __x)
795 { _M_fill_insert(__position, __n, __x); }
797 #ifdef __GXX_EXPERIMENTAL_CXX0X__
798 void insert(iterator __p, initializer_list<bool> __l)
799 { this->insert(__p, __l.begin(), __l.end()); }
804 { --this->_M_impl._M_finish; }
807 erase(iterator __position)
809 if (__position + 1 != end())
810 std::copy(__position + 1, end(), __position);
811 --this->_M_impl._M_finish;
816 erase(iterator __first, iterator __last)
818 _M_erase_at_end(std::copy(__last, end(), __first));
823 resize(size_type __new_size,
bool __x =
bool())
825 if (__new_size < size())
826 _M_erase_at_end(begin() + difference_type(__new_size));
828 insert(end(), __new_size - size(), __x);
834 for (_Bit_type * __p = this->_M_impl._M_start._M_p;
835 __p != this->_M_impl._M_end_of_storage; ++__p)
841 { _M_erase_at_end(begin()); }
847 _M_copy_aligned(const_iterator __first, const_iterator __last,
850 _Bit_type* __q = std::copy(__first._M_p, __last._M_p, __result._M_p);
851 return std::copy(const_iterator(__last._M_p, 0), __last,
856 _M_initialize(size_type __n)
858 _Bit_type* __q = this->_M_allocate(__n);
859 this->_M_impl._M_end_of_storage = (__q
860 + ((__n + int(_S_word_bit) - 1)
861 /
int(_S_word_bit)));
862 this->_M_impl._M_start = iterator(__q, 0);
863 this->_M_impl._M_finish = this->_M_impl._M_start + difference_type(__n);
870 template<
typename _Integer>
872 _M_initialize_dispatch(_Integer __n, _Integer __x, __true_type)
874 _M_initialize(static_cast<size_type>(__n));
875 std::fill(this->_M_impl._M_start._M_p,
876 this->_M_impl._M_end_of_storage, __x ? ~0 : 0);
879 template<
typename _InputIterator>
881 _M_initialize_dispatch(_InputIterator __first, _InputIterator __last,
883 { _M_initialize_range(__first, __last,
886 template<
typename _InputIterator>
888 _M_initialize_range(_InputIterator __first, _InputIterator __last,
891 for (; __first != __last; ++__first)
895 template<
typename _ForwardIterator>
897 _M_initialize_range(_ForwardIterator __first, _ForwardIterator __last,
902 std::copy(__first, __last, this->_M_impl._M_start);
907 template<
typename _Integer>
909 _M_assign_dispatch(_Integer __n, _Integer __val, __true_type)
910 { _M_fill_assign(__n, __val); }
912 template<
class _InputIterator>
914 _M_assign_dispatch(_InputIterator __first, _InputIterator __last,
919 _M_fill_assign(
size_t __n,
bool __x)
923 std::fill(this->_M_impl._M_start._M_p,
924 this->_M_impl._M_end_of_storage, __x ? ~0 : 0);
925 insert(end(), __n - size(), __x);
929 _M_erase_at_end(begin() + __n);
930 std::fill(this->_M_impl._M_start._M_p,
931 this->_M_impl._M_end_of_storage, __x ? ~0 : 0);
935 template<
typename _InputIterator>
937 _M_assign_aux(_InputIterator __first, _InputIterator __last,
940 iterator __cur = begin();
941 for (; __first != __last && __cur != end(); ++__cur, ++__first)
943 if (__first == __last)
944 _M_erase_at_end(__cur);
946 insert(end(), __first, __last);
949 template<
typename _ForwardIterator>
951 _M_assign_aux(_ForwardIterator __first, _ForwardIterator __last,
956 _M_erase_at_end(std::copy(__first, __last, begin()));
959 _ForwardIterator __mid = __first;
961 std::copy(__first, __mid, begin());
962 insert(end(), __mid, __last);
970 template<
typename _Integer>
972 _M_insert_dispatch(iterator __pos, _Integer __n, _Integer __x,
974 { _M_fill_insert(__pos, __n, __x); }
976 template<
typename _InputIterator>
978 _M_insert_dispatch(iterator __pos,
979 _InputIterator __first, _InputIterator __last,
981 { _M_insert_range(__pos, __first, __last,
985 _M_fill_insert(iterator __position, size_type __n,
bool __x);
987 template<
typename _InputIterator>
989 _M_insert_range(iterator __pos, _InputIterator __first,
992 for (; __first != __last; ++__first)
994 __pos = insert(__pos, *__first);
999 template<
typename _ForwardIterator>
1001 _M_insert_range(iterator __position, _ForwardIterator __first,
1005 _M_insert_aux(iterator __position,
bool __x);
1008 _M_check_len(size_type __n,
const char* __s)
const
1010 if (max_size() - size() < __n)
1011 __throw_length_error(__N(__s));
1013 const size_type __len = size() +
std::max(size(), __n);
1014 return (__len < size() || __len > max_size()) ? max_size() : __len;
1018 _M_erase_at_end(iterator __pos)
1019 { this->_M_impl._M_finish = __pos; }
1022 _GLIBCXX_END_NESTED_NAMESPACE
ISO C++ entities toplevel namespace is std.
basic_string< _CharT, _Traits, _Alloc > operator+(const basic_string< _CharT, _Traits, _Alloc > &__lhs, const basic_string< _CharT, _Traits, _Alloc > &__rhs)
Concatenate two strings.
bool operator<(const basic_string< _CharT, _Traits, _Alloc > &__lhs, const basic_string< _CharT, _Traits, _Alloc > &__rhs)
Test if string precedes string.
Forward iterators support a superset of input iterator operations.
bool operator<=(const basic_string< _CharT, _Traits, _Alloc > &__lhs, const basic_string< _CharT, _Traits, _Alloc > &__rhs)
Test if string doesn't follow string.
iterator_traits< _Iter >::iterator_category __iterator_category(const _Iter &)
A standard container which offers fixed time access to individual elements in any order...
const _Tp & max(const _Tp &, const _Tp &)
This does what you think it does.
Random-access iterators support a superset of bidirectional iterator operations.
void advance(_InputIterator &__i, _Distance __n)
A generalization of pointer arithmetic.
iterator_traits< _InputIterator >::difference_type distance(_InputIterator __first, _InputIterator __last)
A generalization of pointer arithmetic.
void swap(_Tp &, _Tp &)
Swaps two values.
bool operator>(const basic_string< _CharT, _Traits, _Alloc > &__lhs, const basic_string< _CharT, _Traits, _Alloc > &__rhs)
Test if string follows string.
bool operator>=(const basic_string< _CharT, _Traits, _Alloc > &__lhs, const basic_string< _CharT, _Traits, _Alloc > &__rhs)
Test if string doesn't precede string.