57 #ifndef _STL_BVECTOR_H
58 #define _STL_BVECTOR_H 1
60 #ifdef __GXX_EXPERIMENTAL_CXX0X__
64 namespace std _GLIBCXX_VISIBILITY(default)
66 _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
68 typedef unsigned long _Bit_type;
69 enum { _S_word_bit = int(__CHAR_BIT__ *
sizeof(_Bit_type)) };
76 _Bit_reference(_Bit_type * __x, _Bit_type __y)
77 : _M_p(__x), _M_mask(__y) { }
79 _Bit_reference() _GLIBCXX_NOEXCEPT : _M_p(0), _M_mask(0) { }
81 operator bool() const _GLIBCXX_NOEXCEPT
82 {
return !!(*_M_p & _M_mask); }
85 operator=(
bool __x) _GLIBCXX_NOEXCEPT
95 operator=(
const _Bit_reference& __x) _GLIBCXX_NOEXCEPT
96 {
return *
this = bool(__x); }
99 operator==(
const _Bit_reference& __x)
const
100 {
return bool(*
this) == bool(__x); }
103 operator<(
const _Bit_reference& __x)
const
104 {
return !bool(*
this) && bool(__x); }
107 flip() _GLIBCXX_NOEXCEPT
108 { *_M_p ^= _M_mask; }
111 struct _Bit_iterator_base
112 :
public std::iterator<std::random_access_iterator_tag, bool>
115 unsigned int _M_offset;
117 _Bit_iterator_base(_Bit_type * __x,
unsigned int __y)
118 : _M_p(__x), _M_offset(__y) { }
123 if (_M_offset++ ==
int(_S_word_bit) - 1)
133 if (_M_offset-- == 0)
135 _M_offset = int(_S_word_bit) - 1;
141 _M_incr(ptrdiff_t __i)
144 _M_p += __n / int(_S_word_bit);
145 __n = __n % int(_S_word_bit);
148 __n += int(_S_word_bit);
151 _M_offset =
static_cast<unsigned int>(__n);
155 operator==(
const _Bit_iterator_base& __i)
const
156 {
return _M_p == __i._M_p && _M_offset == __i._M_offset; }
159 operator<(
const _Bit_iterator_base& __i)
const
161 return _M_p < __i._M_p
162 || (_M_p == __i._M_p && _M_offset < __i._M_offset);
166 operator!=(
const _Bit_iterator_base& __i)
const
167 {
return !(*
this == __i); }
170 operator>(
const _Bit_iterator_base& __i)
const
171 {
return __i < *
this; }
174 operator<=(
const _Bit_iterator_base& __i)
const
175 {
return !(__i < *
this); }
178 operator>=(
const _Bit_iterator_base& __i)
const
179 {
return !(*
this < __i); }
183 operator-(
const _Bit_iterator_base& __x,
const _Bit_iterator_base& __y)
185 return (
int(_S_word_bit) * (__x._M_p - __y._M_p)
186 + __x._M_offset - __y._M_offset);
189 struct _Bit_iterator :
public _Bit_iterator_base
191 typedef _Bit_reference reference;
192 typedef _Bit_reference* pointer;
193 typedef _Bit_iterator iterator;
195 _Bit_iterator() : _Bit_iterator_base(0, 0) { }
197 _Bit_iterator(_Bit_type * __x,
unsigned int __y)
198 : _Bit_iterator_base(__x, __y) { }
202 {
return reference(_M_p, 1UL << _M_offset); }
214 iterator __tmp = *
this;
229 iterator __tmp = *
this;
235 operator+=(difference_type __i)
242 operator-=(difference_type __i)
251 iterator __tmp = *
this;
258 iterator __tmp = *
this;
264 {
return *(*
this + __i); }
268 operator+(ptrdiff_t __n,
const _Bit_iterator& __x)
269 {
return __x + __n; }
271 struct _Bit_const_iterator :
public _Bit_iterator_base
273 typedef bool reference;
274 typedef bool const_reference;
275 typedef const bool* pointer;
276 typedef _Bit_const_iterator const_iterator;
278 _Bit_const_iterator() : _Bit_iterator_base(0, 0) { }
280 _Bit_const_iterator(_Bit_type * __x,
unsigned int __y)
281 : _Bit_iterator_base(__x, __y) { }
283 _Bit_const_iterator(
const _Bit_iterator& __x)
284 : _Bit_iterator_base(__x._M_p, __x._M_offset) { }
288 {
return _Bit_reference(_M_p, 1UL << _M_offset); }
300 const_iterator __tmp = *
this;
315 const_iterator __tmp = *
this;
321 operator+=(difference_type __i)
328 operator-=(difference_type __i)
337 const_iterator __tmp = *
this;
344 const_iterator __tmp = *
this;
350 {
return *(*
this + __i); }
353 inline _Bit_const_iterator
354 operator+(ptrdiff_t __n,
const _Bit_const_iterator& __x)
355 {
return __x + __n; }
358 __fill_bvector(_Bit_iterator __first, _Bit_iterator __last,
bool __x)
360 for (; __first != __last; ++__first)
365 fill(_Bit_iterator __first, _Bit_iterator __last,
const bool& __x)
367 if (__first._M_p != __last._M_p)
369 std::fill(__first._M_p + 1, __last._M_p, __x ? ~0 : 0);
370 __fill_bvector(__first, _Bit_iterator(__first._M_p + 1, 0), __x);
371 __fill_bvector(_Bit_iterator(__last._M_p, 0), __last, __x);
374 __fill_bvector(__first, __last, __x);
377 template<
typename _Alloc>
380 typedef typename _Alloc::template rebind<_Bit_type>::other
384 :
public _Bit_alloc_type
386 _Bit_iterator _M_start;
387 _Bit_iterator _M_finish;
388 _Bit_type* _M_end_of_storage;
391 : _Bit_alloc_type(), _M_start(), _M_finish(), _M_end_of_storage(0)
394 _Bvector_impl(
const _Bit_alloc_type& __a)
395 : _Bit_alloc_type(__a), _M_start(), _M_finish(), _M_end_of_storage(0)
398 #ifdef __GXX_EXPERIMENTAL_CXX0X__
399 _Bvector_impl(_Bit_alloc_type&& __a)
400 : _Bit_alloc_type(std::move(__a)), _M_start(), _M_finish(),
407 typedef _Alloc allocator_type;
410 _M_get_Bit_allocator() _GLIBCXX_NOEXCEPT
411 {
return *
static_cast<_Bit_alloc_type*
>(&this->_M_impl); }
413 const _Bit_alloc_type&
414 _M_get_Bit_allocator() const _GLIBCXX_NOEXCEPT
415 {
return *
static_cast<const _Bit_alloc_type*
>(&this->_M_impl); }
418 get_allocator() const _GLIBCXX_NOEXCEPT
419 {
return allocator_type(_M_get_Bit_allocator()); }
424 _Bvector_base(
const allocator_type& __a)
427 #ifdef __GXX_EXPERIMENTAL_CXX0X__
428 _Bvector_base(_Bvector_base&& __x) noexcept
429 : _M_impl(std::move(__x._M_get_Bit_allocator()))
431 this->_M_impl._M_start = __x._M_impl._M_start;
432 this->_M_impl._M_finish = __x._M_impl._M_finish;
433 this->_M_impl._M_end_of_storage = __x._M_impl._M_end_of_storage;
434 __x._M_impl._M_start = _Bit_iterator();
435 __x._M_impl._M_finish = _Bit_iterator();
436 __x._M_impl._M_end_of_storage = 0;
441 { this->_M_deallocate(); }
444 _Bvector_impl _M_impl;
447 _M_allocate(
size_t __n)
448 {
return _M_impl.allocate(_S_nword(__n)); }
453 if (_M_impl._M_start._M_p)
454 _M_impl.deallocate(_M_impl._M_start._M_p,
455 _M_impl._M_end_of_storage - _M_impl._M_start._M_p);
460 {
return (__n +
int(_S_word_bit) - 1) / int(_S_word_bit); }
463 _GLIBCXX_END_NAMESPACE_CONTAINER
469 namespace std _GLIBCXX_VISIBILITY(default)
471 _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
490 template<
typename _Alloc>
491 class vector<bool, _Alloc> :
protected _Bvector_base<_Alloc>
493 typedef _Bvector_base<_Alloc> _Base;
495 #ifdef __GXX_EXPERIMENTAL_CXX0X__
496 template<
typename>
friend class hash;
500 typedef bool value_type;
501 typedef size_t size_type;
502 typedef ptrdiff_t difference_type;
503 typedef _Bit_reference reference;
504 typedef bool const_reference;
505 typedef _Bit_reference* pointer;
506 typedef const bool* const_pointer;
507 typedef _Bit_iterator iterator;
508 typedef _Bit_const_iterator const_iterator;
511 typedef _Alloc allocator_type;
513 allocator_type get_allocator()
const
514 {
return _Base::get_allocator(); }
517 using _Base::_M_allocate;
518 using _Base::_M_deallocate;
519 using _Base::_S_nword;
520 using _Base::_M_get_Bit_allocator;
527 vector(
const allocator_type& __a)
531 vector(size_type __n,
const bool& __value =
bool(),
532 const allocator_type& __a = allocator_type())
536 std::fill(this->_M_impl._M_start._M_p, this->_M_impl._M_end_of_storage,
541 : _Base(__x._M_get_Bit_allocator())
543 _M_initialize(__x.
size());
544 _M_copy_aligned(__x.
begin(), __x.
end(), this->_M_impl._M_start);
547 #ifdef __GXX_EXPERIMENTAL_CXX0X__
549 : _Base(std::move(__x)) { }
552 const allocator_type& __a = allocator_type())
555 _M_initialize_range(__l.begin(), __l.end(),
560 template<
typename _InputIterator>
561 vector(_InputIterator __first, _InputIterator __last,
562 const allocator_type& __a = allocator_type())
565 typedef typename std::__is_integer<_InputIterator>::__type _Integral;
566 _M_initialize_dispatch(__first, __last, _Integral());
569 ~vector() _GLIBCXX_NOEXCEPT { }
578 this->_M_deallocate();
579 _M_initialize(__x.
size());
581 this->_M_impl._M_finish = _M_copy_aligned(__x.
begin(), __x.
end(),
586 #ifdef __GXX_EXPERIMENTAL_CXX0X__
600 this->
assign (__l.begin(), __l.end());
610 assign(size_type __n,
const bool& __x)
611 { _M_fill_assign(__n, __x); }
613 template<
typename _InputIterator>
615 assign(_InputIterator __first, _InputIterator __last)
617 typedef typename std::__is_integer<_InputIterator>::__type _Integral;
618 _M_assign_dispatch(__first, __last, _Integral());
621 #ifdef __GXX_EXPERIMENTAL_CXX0X__
624 { this->
assign(__l.begin(), __l.end()); }
628 begin() _GLIBCXX_NOEXCEPT
629 {
return this->_M_impl._M_start; }
632 begin()
const _GLIBCXX_NOEXCEPT
633 {
return this->_M_impl._M_start; }
636 end() _GLIBCXX_NOEXCEPT
637 {
return this->_M_impl._M_finish; }
640 end()
const _GLIBCXX_NOEXCEPT
641 {
return this->_M_impl._M_finish; }
644 rbegin() _GLIBCXX_NOEXCEPT
648 rbegin()
const _GLIBCXX_NOEXCEPT
652 rend() _GLIBCXX_NOEXCEPT
656 rend()
const _GLIBCXX_NOEXCEPT
659 #ifdef __GXX_EXPERIMENTAL_CXX0X__
662 {
return this->_M_impl._M_start; }
665 cend()
const noexcept
666 {
return this->_M_impl._M_finish; }
673 crend()
const noexcept
678 size()
const _GLIBCXX_NOEXCEPT
679 {
return size_type(
end() -
begin()); }
684 const size_type __isize =
685 __gnu_cxx::__numeric_traits<difference_type>::__max
686 - int(_S_word_bit) + 1;
687 const size_type __asize = _M_get_Bit_allocator().max_size();
688 return (__asize <= __isize /
int(_S_word_bit)
689 ? __asize *
int(_S_word_bit) : __isize);
694 {
return size_type(const_iterator(this->_M_impl._M_end_of_storage, 0)
698 empty()
const _GLIBCXX_NOEXCEPT
704 return *iterator(this->_M_impl._M_start._M_p
705 + __n /
int(_S_word_bit), __n %
int(_S_word_bit));
711 return *const_iterator(this->_M_impl._M_start._M_p
712 + __n /
int(_S_word_bit), __n %
int(_S_word_bit));
719 if (__n >= this->
size())
720 __throw_out_of_range(__N(
"vector<bool>::_M_range_check"));
729 at(size_type __n)
const
736 __throw_length_error(__N(
"vector::reserve"));
751 {
return *(
end() - 1); }
755 {
return *(
end() - 1); }
763 data() _GLIBCXX_NOEXCEPT { }
768 if (this->_M_impl._M_finish._M_p != this->_M_impl._M_end_of_storage)
769 *this->_M_impl._M_finish++ = __x;
771 _M_insert_aux(
end(), __x);
777 std::swap(this->_M_impl._M_start, __x._M_impl._M_start);
778 std::swap(this->_M_impl._M_finish, __x._M_impl._M_finish);
779 std::swap(this->_M_impl._M_end_of_storage,
780 __x._M_impl._M_end_of_storage);
784 std::__alloc_swap<typename _Base::_Bit_alloc_type>::
785 _S_do_it(_M_get_Bit_allocator(), __x._M_get_Bit_allocator());
790 swap(reference __x, reference __y) _GLIBCXX_NOEXCEPT
798 insert(iterator __position,
const bool& __x =
bool())
800 const difference_type __n = __position -
begin();
801 if (this->_M_impl._M_finish._M_p != this->_M_impl._M_end_of_storage
802 && __position ==
end())
803 *this->_M_impl._M_finish++ = __x;
805 _M_insert_aux(__position, __x);
806 return begin() + __n;
809 template<
typename _InputIterator>
811 insert(iterator __position,
812 _InputIterator __first, _InputIterator __last)
814 typedef typename std::__is_integer<_InputIterator>::__type _Integral;
815 _M_insert_dispatch(__position, __first, __last, _Integral());
819 insert(iterator __position, size_type __n,
const bool& __x)
820 { _M_fill_insert(__position, __n, __x); }
822 #ifdef __GXX_EXPERIMENTAL_CXX0X__
824 { this->
insert(__p, __l.begin(), __l.end()); }
829 { --this->_M_impl._M_finish; }
832 erase(iterator __position)
834 if (__position + 1 !=
end())
835 std::copy(__position + 1,
end(), __position);
836 --this->_M_impl._M_finish;
841 erase(iterator __first, iterator __last)
843 if (__first != __last)
844 _M_erase_at_end(std::copy(__last,
end(), __first));
849 resize(size_type __new_size,
bool __x =
bool())
851 if (__new_size <
size())
852 _M_erase_at_end(
begin() + difference_type(__new_size));
857 #ifdef __GXX_EXPERIMENTAL_CXX0X__
860 { _M_shrink_to_fit(); }
864 flip() _GLIBCXX_NOEXCEPT
866 for (_Bit_type * __p = this->_M_impl._M_start._M_p;
867 __p != this->_M_impl._M_end_of_storage; ++__p)
872 clear() _GLIBCXX_NOEXCEPT
873 { _M_erase_at_end(
begin()); }
879 _M_copy_aligned(const_iterator __first, const_iterator __last,
882 _Bit_type* __q = std::copy(__first._M_p, __last._M_p, __result._M_p);
883 return std::copy(const_iterator(__last._M_p, 0), __last,
888 _M_initialize(size_type __n)
890 _Bit_type* __q = this->_M_allocate(__n);
891 this->_M_impl._M_end_of_storage = __q + _S_nword(__n);
892 this->_M_impl._M_start = iterator(__q, 0);
893 this->_M_impl._M_finish = this->_M_impl._M_start + difference_type(__n);
897 _M_reallocate(size_type __n);
899 #ifdef __GXX_EXPERIMENTAL_CXX0X__
908 template<
typename _Integer>
910 _M_initialize_dispatch(_Integer __n, _Integer __x, __true_type)
912 _M_initialize(static_cast<size_type>(__n));
913 std::fill(this->_M_impl._M_start._M_p,
914 this->_M_impl._M_end_of_storage, __x ? ~0 : 0);
917 template<
typename _InputIterator>
919 _M_initialize_dispatch(_InputIterator __first, _InputIterator __last,
921 { _M_initialize_range(__first, __last,
924 template<
typename _InputIterator>
926 _M_initialize_range(_InputIterator __first, _InputIterator __last,
929 for (; __first != __last; ++__first)
933 template<
typename _ForwardIterator>
935 _M_initialize_range(_ForwardIterator __first, _ForwardIterator __last,
940 std::copy(__first, __last, this->_M_impl._M_start);
945 template<
typename _Integer>
947 _M_assign_dispatch(_Integer __n, _Integer __val, __true_type)
948 { _M_fill_assign(__n, __val); }
950 template<
class _InputIterator>
952 _M_assign_dispatch(_InputIterator __first, _InputIterator __last,
957 _M_fill_assign(
size_t __n,
bool __x)
961 std::fill(this->_M_impl._M_start._M_p,
962 this->_M_impl._M_end_of_storage, __x ? ~0 : 0);
967 _M_erase_at_end(
begin() + __n);
968 std::fill(this->_M_impl._M_start._M_p,
969 this->_M_impl._M_end_of_storage, __x ? ~0 : 0);
973 template<
typename _InputIterator>
975 _M_assign_aux(_InputIterator __first, _InputIterator __last,
978 iterator __cur =
begin();
979 for (; __first != __last && __cur !=
end(); ++__cur, ++__first)
981 if (__first == __last)
982 _M_erase_at_end(__cur);
987 template<
typename _ForwardIterator>
989 _M_assign_aux(_ForwardIterator __first, _ForwardIterator __last,
994 _M_erase_at_end(std::copy(__first, __last,
begin()));
997 _ForwardIterator __mid = __first;
999 std::copy(__first, __mid,
begin());
1008 template<
typename _Integer>
1010 _M_insert_dispatch(iterator __pos, _Integer __n, _Integer __x,
1012 { _M_fill_insert(__pos, __n, __x); }
1014 template<
typename _InputIterator>
1016 _M_insert_dispatch(iterator __pos,
1017 _InputIterator __first, _InputIterator __last,
1019 { _M_insert_range(__pos, __first, __last,
1023 _M_fill_insert(iterator __position, size_type __n,
bool __x);
1025 template<
typename _InputIterator>
1027 _M_insert_range(iterator __pos, _InputIterator __first,
1030 for (; __first != __last; ++__first)
1032 __pos =
insert(__pos, *__first);
1037 template<
typename _ForwardIterator>
1039 _M_insert_range(iterator __position, _ForwardIterator __first,
1043 _M_insert_aux(iterator __position,
bool __x);
1046 _M_check_len(size_type __n,
const char* __s)
const
1049 __throw_length_error(__N(__s));
1056 _M_erase_at_end(iterator __pos)
1057 { this->_M_impl._M_finish = __pos; }
1060 _GLIBCXX_END_NAMESPACE_CONTAINER
1063 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1067 namespace std _GLIBCXX_VISIBILITY(default)
1069 _GLIBCXX_BEGIN_NAMESPACE_VERSION
1073 template<
typename _Alloc>
1075 :
public __hash_base<size_t, _GLIBCXX_STD_C::vector<bool, _Alloc>>
1078 operator()(
const _GLIBCXX_STD_C::vector<bool, _Alloc>&)
const noexcept;
1081 _GLIBCXX_END_NAMESPACE_VERSION
1084 #endif // __GXX_EXPERIMENTAL_CXX0X__
size_type max_size() const _GLIBCXX_NOEXCEPT
size_type capacity() const _GLIBCXX_NOEXCEPT
const_reverse_iterator crend() const noexcept
const_iterator cbegin() const noexcept
bitset< _Nb > & flip() _GLIBCXX_NOEXCEPT
Toggles every bit to its opposite value.
const_iterator cend() const noexcept
vector & operator=(const vector &__x)
Vector assignment operator.
iterator end() _GLIBCXX_NOEXCEPT
void swap(vector &__x) noexcept(_Alloc_traits::_S_nothrow_swap())
Swaps data with another vector.
void reserve(size_type __n)
Attempt to preallocate enough memory for specified number of elements.
reference at(size_type __n)
Provides access to the data contained in the vector.
iterator erase(iterator __position)
Remove element at given position.
void clear() _GLIBCXX_NOEXCEPT
bool empty() const _GLIBCXX_NOEXCEPT
void _M_range_check(size_type __n) const
Safety check used only from at().
_Tp * data() _GLIBCXX_NOEXCEPT
void pop_back()
Removes last element.
size_type size() const _GLIBCXX_NOEXCEPT
complex< _Tp > operator-(const complex< _Tp > &__x, const complex< _Tp > &__y)
Return new complex value x minus y.
reference operator[](size_type __n)
Subscript access to the data contained in the vector.
iterator_traits< _InputIterator >::difference_type distance(_InputIterator __first, _InputIterator __last)
A generalization of pointer arithmetic.
complex< _Tp > operator+(const complex< _Tp > &__x, const complex< _Tp > &__y)
Return new complex value x plus y.
reference operator[](size_t __position)
Array-indexing support.
~vector() _GLIBCXX_NOEXCEPT
reverse_iterator rbegin() _GLIBCXX_NOEXCEPT
iterator begin() _GLIBCXX_NOEXCEPT
vector()
Default constructor creates no elements.
Forward iterators support a superset of input iterator operations.
const _Tp & max(const _Tp &, const _Tp &)
This does what you think it does.
void advance(_InputIterator &__i, _Distance __n)
A generalization of pointer arithmetic.
iterator insert(iterator __position, const value_type &__x)
Inserts given value into vector before specified iterator.
const_reverse_iterator crbegin() const noexcept
iterator_traits< _Iter >::iterator_category __iterator_category(const _Iter &)
Random-access iterators support a superset of bidirectional iterator operations.
void push_back(const value_type &__x)
Add data to the end of the vector.
Primary class template hash.
void assign(size_type __n, const value_type &__val)
Assigns a given value to a vector.
complex< _Tp > operator*(const complex< _Tp > &__x, const complex< _Tp > &__y)
Return new complex value x times y.
reverse_iterator rend() _GLIBCXX_NOEXCEPT
void resize(size_type __new_size)
Resizes the vector to the specified number of elements.
ptrdiff_t difference_type
Distance between iterators is represented as this type.
A standard container which offers fixed time access to individual elements in any order...