A specialization of vector for booleans which offers fixed time access to individual elements in any order.
Note that vector<bool> does not actually meet the requirements for being a container. This is because the reference and pointer types are not really references and pointers to bool. See DR96 for details.
In some terminology a vector can be described as a dynamic C-style array, it offers fast and efficient access to individual elements in any order and saves the user from worrying about memory and size allocation. Subscripting (
[] ) access is also provided as with C-style arrays.
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); }
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;
168 difference_type __n = __i + _M_offset;
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;
283 iterator __tmp = *
this;
288 operator[](difference_type __i)
const
289 {
return *(*
this + __i); }
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;
369 const_iterator __tmp = *
this;
374 operator[](difference_type __i)
const
375 {
return *(*
this + __i); }
378 inline _Bit_const_iterator
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
bool operator>=(const _Safe_iterator< _IteratorL, _Sequence > &__lhs, const _Safe_iterator< _IteratorR, _Sequence > &__rhs)
Definition: safe_iterator.h:644
bool operator==(const exception_ptr &, const exception_ptr &) _GLIBCXX_USE_NOEXCEPT __attribute__((__pure__))
bool operator<=(const _Safe_iterator< _IteratorL, _Sequence > &__lhs, const _Safe_iterator< _IteratorR, _Sequence > &__rhs)
Definition: safe_iterator.h:580
bool operator<(const _Safe_iterator< _IteratorL, _Sequence > &__lhs, const _Safe_iterator< _IteratorR, _Sequence > &__rhs)
Definition: safe_iterator.h:548
#define bool
Definition: stdbool.h:33
_Safe_iterator< _Iterator, _Sequence > operator+(typename _Safe_iterator< _Iterator, _Sequence >::difference_type __n, const _Safe_iterator< _Iterator, _Sequence > &__i)
Definition: safe_iterator.h:712
bool operator>(const _Safe_iterator< _IteratorL, _Sequence > &__lhs, const _Safe_iterator< _IteratorR, _Sequence > &__rhs)
Definition: safe_iterator.h:612
_Safe_iterator< _IteratorL, _Sequence >::difference_type operator-(const _Safe_iterator< _IteratorL, _Sequence > &__lhs, const _Safe_iterator< _IteratorR, _Sequence > &__rhs)
Definition: safe_iterator.h:680
bool operator!=(const exception_ptr &, const exception_ptr &) _GLIBCXX_USE_NOEXCEPT __attribute__((__pure__))
__PTRDIFF_TYPE__ ptrdiff_t
Definition: stddef.h:147
void swap(exception_ptr &__lhs, exception_ptr &__rhs)
Definition: exception_ptr.h:160