29 #ifndef _GLIBCXX_TR2_DYNAMIC_BITSET
30 #define _GLIBCXX_TR2_DYNAMIC_BITSET 1
32 #pragma GCC system_header
43 namespace std _GLIBCXX_VISIBILITY(default)
47 _GLIBCXX_BEGIN_NAMESPACE_VERSION
65 class _Bool2UChar<bool>
68 typedef unsigned char type;
78 template<
typename _WordT =
unsigned long long,
82 static_assert(std::is_unsigned<_WordT>::value,
"template argument "
83 "_WordT not an unsigned integral type");
85 typedef _WordT block_type;
86 typedef _Alloc allocator_type;
87 typedef size_t size_type;
89 static const size_type _S_bits_per_block = __CHAR_BIT__ *
sizeof(block_type);
90 static const size_type npos =
static_cast<size_type
>(-1);
102 { this->
_M_w.swap(__b._M_w); }
105 __dynamic_bitset_base(size_type __nbits,
unsigned long long __val = 0ULL,
106 const allocator_type& __alloc = allocator_type())
107 :
_M_w(__nbits / _S_bits_per_block
108 + (__nbits % _S_bits_per_block > 0),
111 unsigned long long __mask = ~static_cast<block_type>(0);
113 sizeof(
unsigned long long) /
sizeof(block_type));
114 for (
size_t __i = 0; __i < __n; ++__i)
116 this->
_M_w[__i] = (__val & __mask) >> (__i * _S_bits_per_block);
117 __mask <<= _S_bits_per_block;
122 _M_assign(
const __dynamic_bitset_base& __b)
123 { this->
_M_w = __b._M_w; }
126 _M_swap(__dynamic_bitset_base& __b)
127 { this->
_M_w.swap(__b._M_w); }
134 _M_resize(
size_t __nbits,
bool __value)
136 size_t __sz = __nbits / _S_bits_per_block;
137 if (__nbits % _S_bits_per_block > 0)
141 block_type __val = 0;
149 _M_get_allocator()
const
150 {
return this->
_M_w.get_allocator(); }
153 _S_whichword(size_type __pos) noexcept
154 {
return __pos / _S_bits_per_block; }
157 _S_whichbyte(size_type __pos) noexcept
158 {
return (__pos % _S_bits_per_block) / __CHAR_BIT__; }
161 _S_whichbit(size_type __pos) noexcept
162 {
return __pos % _S_bits_per_block; }
165 _S_maskbit(size_type __pos) noexcept
166 {
return (static_cast<block_type>(1)) << _S_whichbit(__pos); }
169 _M_getword(size_type __pos)
170 {
return this->
_M_w[_S_whichword(__pos)]; }
173 _M_getword(size_type __pos)
const
174 {
return this->
_M_w[_S_whichword(__pos)]; }
185 _M_do_and(
const __dynamic_bitset_base& __x)
187 if (__x._M_w.size() == this->
_M_w.
size())
188 for (
size_t __i = 0; __i < this->
_M_w.
size(); ++__i)
189 this->
_M_w[__i] &= __x._M_w[__i];
195 _M_do_or(
const __dynamic_bitset_base& __x)
197 if (__x._M_w.size() == this->
_M_w.
size())
198 for (
size_t __i = 0; __i < this->
_M_w.
size(); ++__i)
199 this->
_M_w[__i] |= __x._M_w[__i];
205 _M_do_xor(
const __dynamic_bitset_base& __x)
207 if (__x._M_w.size() == this->
_M_w.
size())
208 for (
size_t __i = 0; __i < this->
_M_w.
size(); ++__i)
209 this->
_M_w[__i] ^= __x._M_w[__i];
215 _M_do_dif(
const __dynamic_bitset_base& __x)
217 if (__x._M_w.size() == this->
_M_w.
size())
218 for (
size_t __i = 0; __i < this->
_M_w.
size(); ++__i)
219 this->
_M_w[__i] &= ~__x._M_w[__i];
225 _M_do_left_shift(
size_t __shift);
228 _M_do_right_shift(
size_t __shift);
233 for (
size_t __i = 0; __i < this->
_M_w.
size(); ++__i)
240 for (
size_t __i = 0; __i < this->
_M_w.
size(); ++__i)
241 this->
_M_w[__i] = ~static_cast<block_type>(0);
247 for (
size_t __i = 0; __i < this->
_M_w.
size(); ++__i)
248 this->
_M_w[__i] = static_cast<block_type>(0);
252 _M_is_equal(
const __dynamic_bitset_base& __x)
const
254 if (__x._M_w.size() == this->
_M_w.
size())
256 for (
size_t __i = 0; __i < this->
_M_w.
size(); ++__i)
257 if (this->
_M_w[__i] != __x._M_w[__i])
266 _M_is_less(
const __dynamic_bitset_base& __x)
const
268 if (__x._M_w.size() == this->
_M_w.
size())
270 for (
size_t __i = this->
_M_w.
size(); __i > 0; --__i)
272 if (this->
_M_w[__i-1] < __x._M_w[__i-1])
274 else if (this->
_M_w[__i-1] > __x._M_w[__i-1])
284 _M_are_all_aux()
const
286 for (
size_t __i = 0; __i < this->
_M_w.
size() - 1; ++__i)
287 if (
_M_w[__i] != ~static_cast<block_type>(0))
289 return ((this->
_M_w.
size() - 1) * _S_bits_per_block
290 + __builtin_popcountll(this->_M_hiword()));
296 for (
size_t __i = 0; __i < this->
_M_w.
size(); ++__i)
297 if (this->
_M_w[__i] != static_cast<block_type>(0))
303 _M_is_subset_of(
const __dynamic_bitset_base& __b)
305 if (__b._M_w.size() == this->
_M_w.
size())
307 for (
size_t __i = 0; __i < this->
_M_w.
size(); ++__i)
308 if (this->
_M_w[__i] != (this->
_M_w[__i] | __b._M_w[__i]))
317 _M_is_proper_subset_of(
const __dynamic_bitset_base& __b)
const
319 if (this->is_subset_of(__b))
334 for (
size_t __i = 0; __i < this->
_M_w.
size(); ++__i)
335 __result += __builtin_popcountll(this->
_M_w[__i]);
340 _M_size() const noexcept
344 _M_do_to_ulong()
const;
347 _M_do_to_ullong()
const;
351 _M_do_find_first(
size_t __not_found)
const;
355 _M_do_find_next(
size_t __prev,
size_t __not_found)
const;
359 _M_do_append_block(block_type __block, size_type __pos)
361 size_t __offset = __pos % _S_bits_per_block;
366 this->_M_hiword() |= (__block << __offset);
367 this->
_M_w.
push_back(__block >> (_S_bits_per_block - __offset));
438 template<
typename _WordT =
unsigned long long,
443 static_assert(std::is_unsigned<_WordT>::value,
"template argument "
444 "_WordT not an unsigned integral type");
449 typedef _WordT block_type;
450 typedef _Alloc allocator_type;
451 typedef size_t size_type;
453 static const size_type bits_per_block = __CHAR_BIT__ *
sizeof(block_type);
455 static const size_type npos =
static_cast<size_type
>(-1);
463 size_type __shift = this->_M_Nb % bits_per_block;
465 this->_M_hiword() &= ~((~static_cast<block_type>(0)) << __shift);
472 size_type __shift = this->_M_Nb % bits_per_block;
474 this->_M_hiword() |= ((~static_cast<block_type>(0)) << __shift);
482 _M_unchecked_set(size_type __pos)
484 this->_M_getword(__pos) |= _Base::_S_maskbit(__pos);
489 _M_unchecked_set(size_type __pos,
int __val)
492 this->_M_getword(__pos) |= _Base::_S_maskbit(__pos);
494 this->_M_getword(__pos) &= ~
_Base::_S_maskbit(__pos);
499 _M_unchecked_reset(size_type __pos)
501 this->_M_getword(__pos) &= ~
_Base::_S_maskbit(__pos);
506 _M_unchecked_flip(size_type __pos)
508 this->_M_getword(__pos) ^= _Base::_S_maskbit(__pos);
513 _M_unchecked_test(size_type __pos)
const
514 {
return ((this->_M_getword(__pos) & _Base::_S_maskbit(__pos))
515 != static_cast<_WordT>(0)); }
547 this->_M_wp = &__b._M_getword(__pos);
548 this->_M_bpos = _Base::_S_whichbit(__pos);
559 *this->_M_wp |= _Base::_S_maskbit(this->_M_bpos);
561 *this->_M_wp &= ~
_Base::_S_maskbit(this->_M_bpos);
569 if ((*(__j._M_wp) & _Base::_S_maskbit(__j._M_bpos)))
570 *this->_M_wp |= _Base::_S_maskbit(this->_M_bpos);
572 *this->_M_wp &= ~
_Base::_S_maskbit(this->_M_bpos);
579 {
return (*(_M_wp) & _Base::_S_maskbit(this->_M_bpos)) == 0; }
582 operator bool()
const
583 {
return (*(this->_M_wp) & _Base::_S_maskbit(this->_M_bpos)) != 0; }
589 *this->_M_wp ^= _Base::_S_maskbit(this->_M_bpos);
596 typedef bool const_reference;
608 const allocator_type& __alloc = allocator_type())
609 :
_Base(__nbits, __val, __alloc),
614 const allocator_type& __alloc = allocator_type())
615 : _Base(__alloc), _M_Nb(0)
627 template<
typename _CharT,
typename _Traits,
typename _Alloc1>
630 typename basic_string<_CharT,_Traits,_Alloc1>::size_type
632 typename basic_string<_CharT,_Traits,_Alloc1>::size_type
634 _CharT __zero = _CharT(
'0'), _CharT __one = _CharT(
'1'),
635 const allocator_type& __alloc = allocator_type())
639 if (__pos > __str.
size())
640 __throw_out_of_range(__N(
"dynamic_bitset::bitset initial position "
644 this->_M_Nb = (__n > __str.
size() ? __str.
size() - __pos : __n);
645 this->
resize(this->_M_Nb);
646 this->_M_copy_from_string(__str, __pos, __n,
647 _CharT(
'0'), _CharT(
'1'));
658 const allocator_type& __alloc = allocator_type())
663 while (__str[__len] !=
'\0')
666 this->_M_copy_from_ptr<char,std::char_traits<char>>
667 (__str, __len, 0, __len,
'0',
'1');
702 this->_M_assign(__b);
703 this->_M_Nb = __b._M_Nb;
722 {
return this->_M_get_allocator(); }
728 resize(size_type __nbits,
bool __value =
false)
732 this->_M_resize(__nbits, __value);
733 this->_M_Nb = __nbits;
734 this->_M_do_sanitize();
753 if (
size_t __offset = this->
size() % bits_per_block == 0)
754 this->_M_do_append_block(block_type(0), this->_M_Nb);
756 this->_M_unchecked_set(this->_M_Nb, __bit);
765 this->_M_do_append_block(__block, this->_M_Nb);
766 this->_M_Nb += bits_per_block;
774 { this->
append(__il.begin(), __il.end()); }
779 template <
typename _BlockInputIterator>
781 append(_BlockInputIterator __first, _BlockInputIterator __last)
783 for (; __first != __last; ++__first)
798 this->_M_do_and(__rhs);
805 this->_M_do_and(std::move(__rhs));
812 this->_M_do_or(__rhs);
819 this->_M_do_xor(__rhs);
826 this->_M_do_dif(__rhs);
841 if (__builtin_expect(__pos < this->_M_Nb, 1))
843 this->_M_do_left_shift(__pos);
844 this->_M_do_sanitize();
854 if (__builtin_expect(__pos < this->_M_Nb, 1))
856 this->_M_do_right_shift(__pos);
857 this->_M_do_sanitize();
873 this->_M_do_sanitize();
884 set(size_type __pos,
bool __val =
true)
887 __throw_out_of_range(__N(
"dynamic_bitset::set"));
888 return this->_M_unchecked_set(__pos, __val);
912 __throw_out_of_range(__N(
"dynamic_bitset::reset"));
913 return this->_M_unchecked_reset(__pos);
923 this->_M_do_sanitize();
936 __throw_out_of_range(__N(
"dynamic_bitset::flip"));
937 return this->_M_unchecked_flip(__pos);
960 {
return _M_unchecked_test(__pos); }
971 {
return this->_M_do_to_ulong(); }
981 {
return this->_M_do_to_ullong(); }
991 template<
typename _CharT = char,
995 to_string(_CharT __zero = _CharT(
'0'), _CharT __one = _CharT(
'1'))
const
998 _M_copy_to_string(__result, __zero, __one);
1003 template<
typename _CharT,
typename _Traits>
1005 _M_copy_from_ptr(
const _CharT*,
size_t,
size_t,
size_t,
1008 template<
typename _CharT,
typename _Traits,
typename _Alloc1>
1011 _Traits, _Alloc1>& __str,
size_t __pos,
size_t __n,
1012 _CharT __zero = _CharT(
'0'),
1013 _CharT __one = _CharT(
'1'))
1014 { _M_copy_from_ptr<_CharT, _Traits>(__str.data(), __str.size(),
1015 __pos, __n, __zero, __one); }
1017 template<
typename _CharT,
typename _Traits,
typename _Alloc1>
1020 _CharT __zero = _CharT(
'0'),
1021 _CharT __one = _CharT(
'1'))
const;
1026 {
return this->_M_do_count(); }
1031 {
return this->_M_Nb; }
1036 {
return this->_M_size(); }
1041 {
return (this->_M_Nb == 0); }
1060 __throw_out_of_range(__N(
"dynamic_bitset::test"));
1061 return _M_unchecked_test(__pos);
1070 {
return this->_M_are_all_aux() == _M_Nb; }
1078 {
return this->_M_is_any(); }
1086 {
return !this->_M_is_any(); }
1106 {
return this->_M_do_find_first(this->_M_Nb); }
1116 {
return this->_M_do_find_next(__prev, this->_M_Nb); }
1120 {
return this->_M_is_subset_of(__b); }
1124 {
return this->_M_is_proper_subset_of(__b); }
1127 operator==(
const dynamic_bitset<_WordT, _Alloc>& __lhs,
1128 const dynamic_bitset<_WordT, _Alloc>& __rhs)
1129 {
return __lhs._M_is_equal(__rhs); }
1132 operator<(const dynamic_bitset<_WordT, _Alloc>& __lhs,
1133 const dynamic_bitset<_WordT, _Alloc>& __rhs)
1134 {
return __lhs._M_is_less(__rhs); }
1137 template<
typename _WordT,
typename _Alloc>
1138 template<
typename _CharT,
typename _Traits,
typename _Alloc1>
1140 dynamic_bitset<_WordT, _Alloc>::
1142 _CharT __zero, _CharT __one)
const
1144 __str.
assign(_M_Nb, __zero);
1145 for (
size_t __i = _M_Nb; __i > 0; --__i)
1146 if (_M_unchecked_test(__i - 1))
1147 _Traits::assign(__str[_M_Nb - __i], __one);
1154 template<
typename _WordT,
typename _Alloc>
1158 {
return !(__lhs == __rhs); }
1160 template<
typename _WordT,
typename _Alloc>
1162 operator<=(const dynamic_bitset<_WordT, _Alloc>& __lhs,
1164 {
return !(__lhs > __rhs); }
1166 template<
typename _WordT,
typename _Alloc>
1170 {
return __rhs < __lhs; }
1172 template<
typename _WordT,
typename _Alloc>
1176 {
return !(__lhs < __rhs); }
1189 template<
typename _WordT,
typename _Alloc>
1190 inline dynamic_bitset<_WordT, _Alloc>
1199 template<
typename _WordT,
typename _Alloc>
1200 inline dynamic_bitset<_WordT, _Alloc>
1209 template <
typename _WordT,
typename _Alloc>
1210 inline dynamic_bitset<_WordT, _Alloc>
1219 template <
typename _WordT,
typename _Alloc>
1220 inline dynamic_bitset<_WordT, _Alloc>
1240 template <
typename _CharT,
typename _Traits,
1241 typename _WordT,
typename _Alloc>
1243 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1244 const dynamic_bitset<_WordT, _Alloc>& __x)
1248 const ctype<_CharT>& __ct = use_facet<ctype<_CharT>>(__os.getloc());
1249 __x._M_copy_to_string(__tmp, __ct.
widen(
'0'), __ct.
widen(
'1'));
1250 return __os << __tmp;
1256 _GLIBCXX_END_NAMESPACE_VERSION
The dynamic_bitset class represents a sequence of bits.
size_type count() const noexcept
Returns the number of bits which are set.
unsigned long long to_ullong() const
Returns a numerical interpretation of the dynamic_bitset.
const_reference operator[](size_type __pos) const
Array-indexing support.
dynamic_bitset< _WordT, _Alloc > operator-(const dynamic_bitset< _WordT, _Alloc > &__x, const dynamic_bitset< _WordT, _Alloc > &__y)
Global bitwise operations on bitsets.
Primary class template ctype facet.This template class defines classification and conversion function...
bitset< _Nb > operator~() const noexcept
See the no-argument flip().
bitset< _Nb > operator|(const bitset< _Nb > &__x, const bitset< _Nb > &__y) noexcept
Global bitwise operations on bitsets.
allocator_type get_allocator() const
Return the allocator for the bitset.
bool any() const
Tests whether any of the bits are on.
size_type size() const noexcept
Returns the total number of bits.
dynamic_bitset< _WordT, _Alloc > & operator&=(dynamic_bitset< _WordT, _Alloc > &&__rhs)
Operations on dynamic_bitsets.
std::vector< block_type, allocator_type > _M_w
0 is the least significant word.
void swap(dynamic_bitset &__b)
Swap with another bitset.
bitset< _Nb > & flip() noexcept
Toggles every bit to its opposite value.
unsigned long to_ulong() const
Returns a numerical interpretation of the dynamic_bitset.
void append(block_type __block)
Append a block.
dynamic_bitset(size_type __nbits, unsigned long long __val=0ULL, const allocator_type &__alloc=allocator_type())
Initial bits bitwise-copied from a single word (others set to zero).
size_type size() const noexcept
Returns the number of characters in the string, not including any null-termination.
dynamic_bitset(const dynamic_bitset &__b)
Copy constructor.
size_type find_first() const
Finds the index of the first "on" bit.
bool operator>=(const dynamic_bitset< _WordT, _Alloc > &__lhs, const dynamic_bitset< _WordT, _Alloc > &__rhs)
These comparisons for equality/inequality are, well, bitwise.
char_type widen(char __c) const
Widen char to char_type.
size_type size() const noexcept
dynamic_bitset(const char *__str, const allocator_type &__alloc=allocator_type())
Construct from a string.
static constexpr _Tp max() noexcept
dynamic_bitset< _WordT, _Alloc > & operator<<=(size_type __pos)
Operations on dynamic_bitsets.
dynamic_bitset & operator=(const dynamic_bitset &__b)
Assignment.
dynamic_bitset< _WordT, _Alloc > & operator-=(const dynamic_bitset< _WordT, _Alloc > &__rhs)
Operations on dynamic_bitsets.
size_type num_blocks() const noexcept
Returns the total number of blocks.
dynamic_bitset(const std::basic_string< _CharT, _Traits, _Alloc1 > &__str, typename basic_string< _CharT, _Traits, _Alloc1 >::size_type __pos=0, typename basic_string< _CharT, _Traits, _Alloc1 >::size_type __n=std::basic_string< _CharT, _Traits, _Alloc1 >::npos, _CharT __zero=_CharT('0'), _CharT __one=_CharT('1'), const allocator_type &__alloc=allocator_type())
Use a subset of a string.
void push_back(bool __bit)
Push a bit onto the high end of the bitset.
dynamic_bitset< _WordT, _Alloc > & reset(size_type __pos)
Sets a given bit to false.
const _Tp & min(const _Tp &, const _Tp &)
This does what you think it does.
reference operator[](size_type __pos)
Array-indexing support.
void clear()
Clear the bitset.
size_type find_next(size_t __prev) const
Finds the index of the next "on" bit after prev.
dynamic_bitset< _WordT, _Alloc > & set(size_type __pos, bool __val=true)
Sets a given bit to a particular value.
constexpr _Tp && forward(typename std::remove_reference< _Tp >::type &__t) noexcept
Forward an lvalue.
dynamic_bitset< _WordT, _Alloc > & operator^=(const dynamic_bitset< _WordT, _Alloc > &__rhs)
Operations on dynamic_bitsets.
dynamic_bitset< _WordT, _Alloc > & operator>>=(size_type __pos)
Operations on dynamic_bitsets.
void resize(size_type __nbits, bool __value=false)
Resize the bitset.
void push_back(const value_type &__x)
Add data to the end of the vector.
bool test(size_type __pos) const
Tests the value of a bit.
dynamic_bitset< _WordT, _Alloc > & operator|=(const dynamic_bitset< _WordT, _Alloc > &__rhs)
Operations on dynamic_bitsets.
dynamic_bitset< _WordT, _Alloc > & reset()
Sets every bit to false.
dynamic_bitset< _WordT, _Alloc > operator~() const
See the no-argument flip().
constexpr size_type max_size() noexcept
Returns the maximum size of a dynamic_bitset object having the same type as *this. The real answer is max() * bits_per_block but is likely to overflow.
void resize(size_type __new_size)
Resizes the vector to the specified number of elements.
bool operator>(const dynamic_bitset< _WordT, _Alloc > &__lhs, const dynamic_bitset< _WordT, _Alloc > &__rhs)
These comparisons for equality/inequality are, well, bitwise.
bool all() const
Tests whether all the bits are on.
The standard allocator, as per [20.4].
Basis for explicit traits specializations.
void swap(function< _Res(_Args...)> &__x, function< _Res(_Args...)> &__y)
Swap the targets of two polymorphic function object wrappers.
dynamic_bitset< _WordT, _Alloc > operator<<(size_type __pos) const
Self-explanatory.
dynamic_bitset(dynamic_bitset &&__b)
Move constructor.
bitset< _Nb > operator&(const bitset< _Nb > &__x, const bitset< _Nb > &__y) noexcept
Global bitwise operations on bitsets.
dynamic_bitset< _WordT, _Alloc > & flip()
Toggles every bit to its opposite value.
Managing sequences of characters and character-like objects.
Template class basic_ostream.
dynamic_bitset< _WordT, _Alloc > & operator&=(const dynamic_bitset< _WordT, _Alloc > &__rhs)
Operations on dynamic_bitsets.
dynamic_bitset & operator=(dynamic_bitset &&__b)
Move assignment.
bitset< _Nb > operator^(const bitset< _Nb > &__x, const bitset< _Nb > &__y) noexcept
Global bitwise operations on bitsets.
basic_string & assign(const basic_string &__str)
Set value to contents of another string.
void append(_BlockInputIterator __first, _BlockInputIterator __last)
Append an iterator range of blocks.
dynamic_bitset< _WordT, _Alloc > & set()
Sets every bit to true.
dynamic_bitset< _WordT, _Alloc > operator>>(size_type __pos) const
Self-explanatory.
bool none() const
Tests whether any of the bits are on.
dynamic_bitset< _WordT, _Alloc > & flip(size_type __pos)
Toggles a given bit to its opposite value.
std::basic_string< _CharT, _Traits, _Alloc1 > to_string(_CharT __zero=_CharT('0'), _CharT __one=_CharT('1')) const
Returns a character interpretation of the dynamic_bitset.
bool empty() const noexcept
Returns true if the dynamic_bitset is empty.