29 #ifndef _GLIBCXX_TR2_DYNAMIC_BITSET
30 #define _GLIBCXX_TR2_DYNAMIC_BITSET 1
32 #ifdef _GLIBCXX_SYSHDR
33 #pragma GCC system_header
44 namespace std _GLIBCXX_VISIBILITY(default)
46 _GLIBCXX_BEGIN_NAMESPACE_VERSION
62 template<
typename _WordT =
unsigned long long,
67 "_WordT not an unsigned integral type");
69 typedef _WordT block_type;
70 typedef _Alloc allocator_type;
71 typedef size_t size_type;
73 static const size_type _S_bits_per_block = __CHAR_BIT__ *
sizeof(block_type);
74 static const size_type npos =
static_cast<size_type
>(-1);
93 const allocator_type& __alloc = allocator_type())
94 :
_M_w(__nbits / _S_bits_per_block + (__nbits % _S_bits_per_block > 0),
95 block_type(0), __alloc)
98 __val &= ~(-1ULL << __nbits);
102 #pragma GCC diagnostic push
103 #pragma GCC diagnostic ignored "-Wc++17-extensions" // if constexpr
104 if constexpr (
sizeof(__val) ==
sizeof(block_type))
110 for (
size_t __i = 0; __val && __i < __n; ++__i)
112 _M_w[__i] =
static_cast<block_type
>(__val);
113 __val >>= _S_bits_per_block;
116 #pragma GCC diagnostic pop
120 _M_swap(__dynamic_bitset_base& __b) noexcept
121 { this->_M_w.
swap(__b._M_w); }
125 { this->_M_w.
clear(); }
128 _M_resize(
size_t __nbits,
bool __value)
130 size_t __sz = __nbits / _S_bits_per_block;
131 if (__nbits % _S_bits_per_block > 0)
133 if (__sz != this->_M_w.
size())
135 block_type __val = 0;
138 this->_M_w.
resize(__sz, __val);
143 _M_get_allocator() const noexcept
147 _S_whichword(size_type __pos) noexcept
148 {
return __pos / _S_bits_per_block; }
151 _S_whichbyte(size_type __pos) noexcept
152 {
return (__pos % _S_bits_per_block) / __CHAR_BIT__; }
155 _S_whichbit(size_type __pos) noexcept
156 {
return __pos % _S_bits_per_block; }
159 _S_maskbit(size_type __pos) noexcept
160 {
return (
static_cast<block_type
>(1)) << _S_whichbit(__pos); }
163 _M_getword(size_type __pos) noexcept
164 {
return this->_M_w[_S_whichword(__pos)]; }
167 _M_getword(size_type __pos)
const noexcept
168 {
return this->_M_w[_S_whichword(__pos)]; }
172 {
return this->_M_w[
_M_w.
size() - 1]; }
175 _M_hiword() const noexcept
176 {
return this->_M_w[
_M_w.
size() - 1]; }
179 _M_do_and(
const __dynamic_bitset_base& __x) noexcept
181 if (__x._M_w.size() == this->_M_w.size())
182 for (
size_t __i = 0; __i < this->_M_w.
size(); ++__i)
183 this->_M_w[__i] &= __x._M_w[__i];
189 _M_do_or(
const __dynamic_bitset_base& __x) noexcept
191 if (__x._M_w.size() == this->_M_w.size())
192 for (
size_t __i = 0; __i < this->_M_w.
size(); ++__i)
193 this->_M_w[__i] |= __x._M_w[__i];
199 _M_do_xor(
const __dynamic_bitset_base& __x) noexcept
201 if (__x._M_w.size() == this->_M_w.size())
202 for (
size_t __i = 0; __i < this->_M_w.
size(); ++__i)
203 this->_M_w[__i] ^= __x._M_w[__i];
209 _M_do_dif(
const __dynamic_bitset_base& __x) noexcept
211 if (__x._M_w.size() == this->_M_w.size())
212 for (
size_t __i = 0; __i < this->_M_w.
size(); ++__i)
213 this->_M_w[__i] &= ~__x._M_w[__i];
219 _M_do_left_shift(
size_t __shift);
222 _M_do_right_shift(
size_t __shift);
225 _M_do_flip() noexcept
227 for (
size_t __i = 0; __i < this->_M_w.
size(); ++__i)
228 this->_M_w[__i] = ~this->_M_w[__i];
234 for (
size_t __i = 0; __i < this->_M_w.
size(); ++__i)
235 this->_M_w[__i] =
static_cast<block_type
>(-1);
239 _M_do_reset() noexcept
245 _M_is_equal(
const __dynamic_bitset_base& __x)
const noexcept
247 if (__x._M_w.size() == this->_M_w.size())
249 for (
size_t __i = 0; __i < this->_M_w.
size(); ++__i)
250 if (this->_M_w[__i] != __x._M_w[__i])
259 _M_is_less(
const __dynamic_bitset_base& __x)
const noexcept
261 if (__x._M_w.size() == this->_M_w.size())
263 for (
size_t __i = this->_M_w.
size(); __i > 0; --__i)
265 if (this->_M_w[__i-1] < __x._M_w[__i-1])
267 else if (this->_M_w[__i-1] > __x._M_w[__i-1])
277 _M_are_all_aux() const noexcept
279 for (
size_t __i = 0; __i < this->_M_w.
size() - 1; ++__i)
280 if (
_M_w[__i] !=
static_cast<block_type
>(-1))
282 return ((this->_M_w.
size() - 1) * _S_bits_per_block
283 + __builtin_popcountll(this->_M_hiword()));
287 _M_is_any() const noexcept
289 for (
size_t __i = 0; __i < this->_M_w.
size(); ++__i)
290 if (this->_M_w[__i] !=
static_cast<block_type
>(0))
296 _M_is_subset_of(
const __dynamic_bitset_base& __b) noexcept
298 if (__b._M_w.size() == this->_M_w.size())
300 for (
size_t __i = 0; __i < this->_M_w.
size(); ++__i)
301 if (this->_M_w[__i] != (this->_M_w[__i] | __b._M_w[__i]))
310 _M_is_proper_subset_of(
const __dynamic_bitset_base& __b)
const noexcept
312 if (this->_M_is_subset_of(__b))
324 _M_do_count() const noexcept
327 for (
size_t __i = 0; __i < this->_M_w.
size(); ++__i)
328 __result += __builtin_popcountll(this->_M_w[__i]);
333 _M_size() const noexcept
334 {
return this->_M_w.
size(); }
337 _M_do_to_ulong()
const;
340 _M_do_to_ullong()
const;
344 _M_do_find_first(
size_t __not_found)
const;
348 _M_do_find_next(
size_t __prev,
size_t __not_found)
const;
352 _M_do_append_block(block_type __block, size_type __pos)
354 size_t __offset = __pos % _S_bits_per_block;
359 this->_M_hiword() |= (__block << __offset);
360 this->_M_w.
push_back(__block >> (_S_bits_per_block - __offset));
422 template<
typename _WordT =
unsigned long long,
428 "_WordT not an unsigned integral type");
433 typedef _WordT block_type;
434 typedef _Alloc allocator_type;
435 typedef size_t size_type;
437 static const size_type bits_per_block = __CHAR_BIT__ *
sizeof(block_type);
439 static const size_type npos =
static_cast<size_type
>(-1);
447 size_type __shift = this->_M_Nb % bits_per_block;
449 this->_M_hiword() &= block_type(~(block_type(-1) << __shift));
456 size_type __shift = this->_M_Nb % bits_per_block;
458 this->_M_hiword() |= block_type(block_type(-1) << __shift);
466 _M_unchecked_set(size_type __pos) noexcept
468 this->_M_getword(__pos) |= _Base::_S_maskbit(__pos);
473 _M_unchecked_set(size_type __pos,
int __val) noexcept
476 this->_M_getword(__pos) |= _Base::_S_maskbit(__pos);
478 this->_M_getword(__pos) &= ~
_Base::_S_maskbit(__pos);
483 _M_unchecked_reset(size_type __pos) noexcept
485 this->_M_getword(__pos) &= ~
_Base::_S_maskbit(__pos);
490 _M_unchecked_flip(size_type __pos) noexcept
492 this->_M_getword(__pos) ^= _Base::_S_maskbit(__pos);
497 _M_unchecked_test(size_type __pos)
const noexcept
498 {
return ((this->_M_getword(__pos) & _Base::_S_maskbit(__pos))
499 !=
static_cast<_WordT
>(0)); }
528 this->_M_wp = &__b._M_getword(__pos);
529 this->_M_bpos = _Base::_S_whichbit(__pos);
534 operator=(
bool __x) noexcept
537 *this->_M_wp |= _Base::_S_maskbit(this->_M_bpos);
539 *this->_M_wp &= ~
_Base::_S_maskbit(this->_M_bpos);
547 if ((*(__j._M_wp) & _Base::_S_maskbit(__j._M_bpos)))
548 *this->_M_wp |= _Base::_S_maskbit(this->_M_bpos);
550 *this->_M_wp &= ~
_Base::_S_maskbit(this->_M_bpos);
556 operator~()
const noexcept
557 {
return (*(_M_wp) & _Base::_S_maskbit(this->_M_bpos)) == 0; }
560 operator bool()
const noexcept
561 {
return (*(this->_M_wp) & _Base::_S_maskbit(this->_M_bpos)) != 0; }
567 *this->_M_wp ^= _Base::_S_maskbit(this->_M_bpos);
574 typedef bool const_reference;
590 const allocator_type& __alloc = allocator_type())
591 :
_Base(__nbits, __val, __alloc),
596 const allocator_type& __alloc = allocator_type())
612 template<
typename _CharT,
typename _Traits,
typename _Alloc1>
615 typename basic_string<_CharT,_Traits,_Alloc1>::size_type
617 typename basic_string<_CharT,_Traits,_Alloc1>::size_type
619 _CharT __zero = _CharT(
'0'), _CharT __one = _CharT(
'1'),
620 const allocator_type& __alloc = allocator_type())
623 if (__pos > __str.
size())
624 __throw_out_of_range(__N(
"dynamic_bitset::bitset initial position "
628 this->_M_Nb = (__n > __str.
size() ? __str.
size() - __pos : __n);
629 this->
resize(this->_M_Nb);
630 this->_M_copy_from_string(__str, __pos, __n, __zero, __one);
642 const allocator_type& __alloc = allocator_type())
643 :
_Base(__builtin_strlen(__str), 0ULL, __alloc),
644 _M_Nb(__builtin_strlen(__str))
646 this->_M_copy_from_ptr(__str, _M_Nb, 0, _M_Nb);
662 std::swap(this->_M_Nb, __b._M_Nb);
673 #pragma GCC diagnostic push
674 #pragma GCC diagnostic ignored "-Wc++17-extensions" // if constexpr
675 static_cast<_Base&
>(*this) =
static_cast<_Base&&
>(__b);
682 #pragma GCC diagnostic pop
690 {
return this->_M_get_allocator(); }
696 resize(size_type __nbits,
bool __value =
false)
700 this->_M_resize(__nbits, __value);
701 this->_M_Nb = __nbits;
702 this->_M_do_sanitize();
721 if (this->
size() % bits_per_block == 0)
722 this->_M_do_append_block(block_type(__bit), this->_M_Nb);
724 this->_M_unchecked_set(this->_M_Nb, __bit);
736 this->_M_do_append_block(__block, this->_M_Nb);
737 this->_M_Nb += bits_per_block;
745 { this->
append(__il.begin(), __il.end()); }
750 template <
typename _BlockInputIterator>
752 append(_BlockInputIterator __first, _BlockInputIterator __last)
754 for (; __first != __last; ++__first)
769 this->_M_do_and(__rhs);
783 this->_M_do_or(__rhs);
790 this->_M_do_xor(__rhs);
797 this->_M_do_dif(__rhs);
812 if (__builtin_expect(__pos < this->_M_Nb, 1))
814 this->_M_do_left_shift(__pos);
815 this->_M_do_sanitize();
825 if (__builtin_expect(__pos < this->_M_Nb, 1))
826 this->_M_do_right_shift(__pos);
841 this->_M_do_sanitize();
852 set(size_type __pos,
bool __val =
true)
855 __throw_out_of_range(__N(
"dynamic_bitset::set"));
856 return this->_M_unchecked_set(__pos, __val);
880 __throw_out_of_range(__N(
"dynamic_bitset::reset"));
881 return this->_M_unchecked_reset(__pos);
891 this->_M_do_sanitize();
904 __throw_out_of_range(__N(
"dynamic_bitset::flip"));
905 return this->_M_unchecked_flip(__pos);
928 {
return _M_unchecked_test(__pos); }
939 {
return this->_M_do_to_ulong(); }
949 {
return this->_M_do_to_ullong(); }
959 template<
typename _CharT = char,
963 to_string(_CharT __zero = _CharT(
'0'), _CharT __one = _CharT(
'1'))
const
966 _M_copy_to_string(__result, __zero, __one);
971 template<
typename _Traits = std::
char_traits<
char>,
972 typename _CharT =
typename _Traits::
char_type>
974 _M_copy_from_ptr(
const _CharT*,
size_t,
size_t,
size_t,
975 _CharT __zero = _CharT(
'0'),
976 _CharT __one = _CharT(
'1'));
978 template<
typename _CharT,
typename _Traits,
typename _Alloc1>
981 size_t __pos,
size_t __n,
982 _CharT __zero = _CharT(
'0'),
983 _CharT __one = _CharT(
'1'))
985 _M_copy_from_ptr<_Traits>(__str.
data(), __str.
size(), __pos, __n,
989 template<
typename _CharT,
typename _Traits,
typename _Alloc1>
992 _CharT __zero = _CharT(
'0'),
993 _CharT __one = _CharT(
'1'))
const;
998 {
return this->_M_do_count(); }
1003 {
return this->_M_Nb; }
1008 {
return this->_M_size(); }
1011 _GLIBCXX_NODISCARD
bool
1013 {
return (this->_M_Nb == 0); }
1032 __throw_out_of_range(__N(
"dynamic_bitset::test"));
1033 return _M_unchecked_test(__pos);
1042 {
return this->_M_are_all_aux() == _M_Nb; }
1050 {
return this->_M_is_any(); }
1058 {
return !this->_M_is_any(); }
1078 {
return this->_M_do_find_first(this->_M_Nb); }
1088 {
return this->_M_do_find_next(__prev, this->_M_Nb); }
1092 {
return this->_M_is_subset_of(__b); }
1096 {
return this->_M_is_proper_subset_of(__b); }
1101 {
return __lhs._M_Nb == __rhs._M_Nb && __lhs._M_is_equal(__rhs); }
1106 {
return __lhs._M_is_less(__rhs) || __lhs._M_Nb < __rhs._M_Nb; }
1109 template<
typename _WordT,
typename _Alloc>
1110 template<
typename _CharT,
typename _Traits,
typename _Alloc1>
1112 dynamic_bitset<_WordT, _Alloc>::
1114 _CharT __zero, _CharT __one)
const
1116 __str.
assign(_M_Nb, __zero);
1117 for (
size_t __i = _M_Nb; __i > 0; --__i)
1118 if (_M_unchecked_test(__i - 1))
1119 _Traits::assign(__str[_M_Nb - __i], __one);
1126 template<
typename _WordT,
typename _Alloc>
1130 {
return !(__lhs == __rhs); }
1132 template<
typename _WordT,
typename _Alloc>
1136 {
return !(__lhs > __rhs); }
1138 template<
typename _WordT,
typename _Alloc>
1142 {
return __rhs < __lhs; }
1144 template<
typename _WordT,
typename _Alloc>
1148 {
return !(__lhs < __rhs); }
1161 template<
typename _WordT,
typename _Alloc>
1162 inline dynamic_bitset<_WordT, _Alloc>
1171 template<
typename _WordT,
typename _Alloc>
1172 inline dynamic_bitset<_WordT, _Alloc>
1181 template <
typename _WordT,
typename _Alloc>
1182 inline dynamic_bitset<_WordT, _Alloc>
1191 template <
typename _WordT,
typename _Alloc>
1192 inline dynamic_bitset<_WordT, _Alloc>
1203 template <
typename _CharT,
typename _Traits,
1204 typename _WordT,
typename _Alloc>
1211 const ctype<_CharT>& __ct = use_facet<ctype<_CharT>>(__os.getloc());
1212 __x._M_copy_to_string(__tmp, __ct.
widen(
'0'), __ct.
widen(
'1'));
1213 return __os << __tmp;
1220 _GLIBCXX_END_NAMESPACE_VERSION