1// TR2 <dynamic_bitset> -*- C++ -*-
3// Copyright (C) 2009-2025 Free Software Foundation, Inc.
5// This file is part of the GNU ISO C++ Library. This library is free
6// software; you can redistribute it and/or modify it under the
7// terms of the GNU General Public License as published by the
8// Free Software Foundation; either version 3, or (at your option)
11// This library is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
16// Under Section 7 of GPL version 3, you are granted additional
17// permissions described in the GCC Runtime Library Exception, version
18// 3.1, as published by the Free Software Foundation.
20// You should have received a copy of the GNU General Public License and
21// a copy of the GCC Runtime Library Exception along with this program;
22// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23// <http://www.gnu.org/licenses/>.
25/** @file tr2/dynamic_bitset
26 * This is a TR2 C++ Library header.
29#ifndef _GLIBCXX_TR2_DYNAMIC_BITSET
30#define _GLIBCXX_TR2_DYNAMIC_BITSET 1
33#pragma GCC system_header
40#include <bits/functexcept.h>
41#include <bits/stl_algo.h> // For fill
42#include <bits/cxxabi_forced.h>
44namespace std _GLIBCXX_VISIBILITY(default)
46_GLIBCXX_BEGIN_NAMESPACE_VERSION
51 * @defgroup dynamic_bitset Dynamic Bitset.
58 * Base class, general case.
60 * See documentation for dynamic_bitset.
62 template<typename _WordT = unsigned long long,
63 typename _Alloc = std::allocator<_WordT>>
64 struct __dynamic_bitset_base
66 static_assert(std::is_unsigned<_WordT>::value, "template argument "
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);
76 /// 0 is the least significant word.
77 std::vector<block_type, allocator_type> _M_w;
80 __dynamic_bitset_base(const allocator_type& __alloc)
84 __dynamic_bitset_base() = default;
85 __dynamic_bitset_base(const __dynamic_bitset_base&) = default;
86 __dynamic_bitset_base(__dynamic_bitset_base&& __b) = default;
87 __dynamic_bitset_base& operator=(const __dynamic_bitset_base&) = default;
88 __dynamic_bitset_base& operator=(__dynamic_bitset_base&&) = default;
89 ~__dynamic_bitset_base() = default;
92 __dynamic_bitset_base(size_type __nbits, unsigned long long __val = 0ULL,
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)
97 if (__nbits < std::numeric_limits<decltype(__val)>::digits)
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))
109 = std::min(_M_w.size(), 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;
137 __val = std::numeric_limits<block_type>::max();
138 this->_M_w.resize(__sz, __val);
143 _M_get_allocator() const noexcept
144 { return this->_M_w.get_allocator(); }
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
241 std::fill(_M_w.begin(), _M_w.end(), static_cast<block_type>(0));
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;
342 // find first "on" bit
344 _M_do_find_first(size_t __not_found) const;
346 // find the next "on" bit that follows "prev"
348 _M_do_find_next(size_t __prev, size_t __not_found) const;
350 // do append of block
352 _M_do_append_block(block_type __block, size_type __pos)
354 size_t __offset = __pos % _S_bits_per_block;
356 this->_M_w.push_back(__block);
359 this->_M_hiword() |= (__block << __offset);
360 this->_M_w.push_back(__block >> (_S_bits_per_block - __offset));
366 * @brief The %dynamic_bitset class represents a sequence of bits.
369 * Proposal to Add a Dynamically Sizeable Bitset to the Standard Library.
370 * http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2006/n2050.pdf
372 * In the general unoptimized case, storage is allocated in
373 * word-sized blocks. Let B be the number of bits in a word, then
374 * (Nb+(B-1))/B words will be used for storage. B - Nb%B bits are
375 * unused. (They are the high-order bits in the highest word.) It
376 * is a class invariant that those unused bits are always zero.
378 * If you think of %dynamic_bitset as "a simple array of bits," be
379 * aware that your mental picture is reversed: a %dynamic_bitset
380 * behaves the same way as bits in integers do, with the bit at
381 * index 0 in the "least significant / right-hand" position, and
382 * the bit at index Nb-1 in the "most significant / left-hand"
383 * position. Thus, unlike other containers, a %dynamic_bitset's
384 * index "counts from right to left," to put it very loosely.
386 * This behavior is preserved when translating to and from strings.
387 * For example, the first line of the following program probably
388 * prints "b('a') is 0001100001" on a modern ASCII system.
391 * #include <dynamic_bitset>
392 * #include <iostream>
395 * using namespace std;
400 * dynamic_bitset<> b(a);
402 * cout << "b('a') is " << b << endl;
406 * string str = s.str();
407 * cout << "index 3 in the string is " << str[3] << " but\n"
408 * << "index 3 in the bitset is " << b[3] << endl;
412 * Most of the actual code isn't contained in %dynamic_bitset<>
413 * itself, but in the base class __dynamic_bitset_base. The base
414 * class works with whole words, not with individual bits. This
415 * allows us to specialize __dynamic_bitset_base for the important
416 * special case where the %dynamic_bitset is only a single word.
418 * Extra confusion can result due to the fact that the storage for
419 * __dynamic_bitset_base @e is a vector, and is indexed as such. This is
420 * carefully encapsulated.
422 template<typename _WordT = unsigned long long,
423 typename _Alloc = std::allocator<_WordT>>
425 : private __dynamic_bitset_base<_WordT, _Alloc>
427 static_assert(std::is_unsigned<_WordT>::value, "template argument "
428 "_WordT not an unsigned integral type");
432 typedef __dynamic_bitset_base<_WordT, _Alloc> _Base;
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);
438 // Use this: constexpr size_type std::numeric_limits<size_type>::max().
439 static const size_type npos = static_cast<size_type>(-1);
443 // Clear the unused bits in the uppermost word.
447 size_type __shift = this->_M_Nb % bits_per_block;
449 this->_M_hiword() &= block_type(~(block_type(-1) << __shift));
452 // Set the unused bits in the uppermost word.
456 size_type __shift = this->_M_Nb % bits_per_block;
458 this->_M_hiword() |= block_type(block_type(-1) << __shift);
462 * These versions of single-bit set, reset, flip, and test
463 * do no range checking.
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)); }
505 * This encapsulates the concept of a single bit. An instance
506 * of this class is a proxy for an actual bit; this way the
507 * individual bit operations are done as faster word-size
508 * bitwise instructions.
510 * Most users will never need to use this class directly;
511 * conversions to and from bool are automatic and should be
512 * transparent. Overloaded operators help to preserve the
515 * (On a typical system, this "bit %reference" is 64 times the
516 * size of an actual bit. Ha.)
520 friend class dynamic_bitset;
526 reference(dynamic_bitset& __b, size_type __pos) noexcept
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);
543 // For b[i] = b[__j];
545 operator=(const reference& __j) noexcept
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);
572 friend class reference;
574 typedef bool const_reference;
576 // 23.3.5.1 constructors:
578 /// All bits set to zero.
579 dynamic_bitset() = default;
581 /// All bits set to zero.
583 dynamic_bitset(const allocator_type& __alloc)
587 /// Initial bits bitwise-copied from a single word (others set to zero).
589 dynamic_bitset(size_type __nbits, unsigned long long __val = 0ULL,
590 const allocator_type& __alloc = allocator_type())
591 : _Base(__nbits, __val, __alloc),
595 dynamic_bitset(initializer_list<block_type> __il,
596 const allocator_type& __alloc = allocator_type())
598 { this->append(__il); }
601 * @brief Use a subset of a string.
602 * @param __str A string of '0' and '1' characters.
603 * @param __pos Index of the first character in @p __str to use.
604 * @param __n The number of characters to copy.
605 * @param __zero The character to use for unset bits.
606 * @param __one The character to use for set bits.
607 * @param __alloc An allocator.
608 * @throw std::out_of_range If @p __pos is bigger the size of @p __str.
609 * @throw std::invalid_argument If a character appears in the string
610 * which is neither '0' nor '1'.
612 template<typename _CharT, typename _Traits, typename _Alloc1>
614 dynamic_bitset(const std::basic_string<_CharT, _Traits, _Alloc1>& __str,
615 typename basic_string<_CharT,_Traits,_Alloc1>::size_type
617 typename basic_string<_CharT,_Traits,_Alloc1>::size_type
618 __n = std::basic_string<_CharT, _Traits, _Alloc1>::npos,
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);
634 * @brief Construct from a string.
635 * @param __str A string of '0' and '1' characters.
636 * @param __alloc An allocator.
637 * @throw std::invalid_argument If a character appears in the string
638 * which is neither '0' nor '1'.
641 dynamic_bitset(const char* __str,
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);
649 /// Copy constructor.
650 dynamic_bitset(const dynamic_bitset&) = default;
652 /// Move constructor.
653 dynamic_bitset(dynamic_bitset&& __b) noexcept
654 : _Base(std::move(__b)), _M_Nb(__b._M_Nb)
657 /// Swap with another bitset.
659 swap(dynamic_bitset& __b) noexcept
662 std::swap(this->_M_Nb, __b._M_Nb);
665 /// Copy assignment operator.
666 dynamic_bitset& operator=(const dynamic_bitset&) = default;
668 /// Move assignment operator.
670 operator=(dynamic_bitset&& __b)
671 noexcept(std::is_nothrow_move_assignable<_Base>::value)
673#pragma GCC diagnostic push
674#pragma GCC diagnostic ignored "-Wc++17-extensions" // if constexpr
675 static_cast<_Base&>(*this) = static_cast<_Base&&>(__b);
677 if constexpr (std::is_nothrow_move_assignable<_Base>::value)
679 else if (get_allocator() == __b.get_allocator())
682#pragma GCC diagnostic pop
686 * @brief Return the allocator for the bitset.
689 get_allocator() const noexcept
690 { return this->_M_get_allocator(); }
693 * @brief Resize the bitset.
696 resize(size_type __nbits, bool __value = false)
700 this->_M_resize(__nbits, __value);
701 this->_M_Nb = __nbits;
702 this->_M_do_sanitize();
706 * @brief Clear the bitset.
716 * @brief Push a bit onto the high end of the bitset.
719 push_back(bool __bit)
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);
728 // XXX why is there no pop_back() member in the proposal?
731 * @brief Append a block.
734 append(block_type __block)
736 this->_M_do_append_block(__block, this->_M_Nb);
737 this->_M_Nb += bits_per_block;
744 append(initializer_list<block_type> __il)
745 { this->append(__il.begin(), __il.end()); }
748 * @brief Append an iterator range of blocks.
750 template <typename _BlockInputIterator>
752 append(_BlockInputIterator __first, _BlockInputIterator __last)
754 for (; __first != __last; ++__first)
755 this->append(*__first);
758 // 23.3.5.2 dynamic_bitset operations:
761 * @brief Operations on dynamic_bitsets.
762 * @param __rhs A same-sized dynamic_bitset.
764 * These should be self-explanatory.
767 operator&=(const dynamic_bitset& __rhs)
769 this->_M_do_and(__rhs);
774 operator&=(dynamic_bitset&& __rhs)
776 this->_M_do_and(std::move(__rhs));
781 operator|=(const dynamic_bitset& __rhs)
783 this->_M_do_or(__rhs);
788 operator^=(const dynamic_bitset& __rhs)
790 this->_M_do_xor(__rhs);
795 operator-=(const dynamic_bitset& __rhs)
797 this->_M_do_dif(__rhs);
804 * @brief Operations on dynamic_bitsets.
805 * @param __pos The number of places to shift.
807 * These should be self-explanatory.
810 operator<<=(size_type __pos)
812 if (__builtin_expect(__pos < this->_M_Nb, 1))
814 this->_M_do_left_shift(__pos);
815 this->_M_do_sanitize();
823 operator>>=(size_type __pos)
825 if (__builtin_expect(__pos < this->_M_Nb, 1))
826 this->_M_do_right_shift(__pos);
833 // Set, reset, and flip.
835 * @brief Sets every bit to true.
841 this->_M_do_sanitize();
846 * @brief Sets a given bit to a particular value.
847 * @param __pos The index of the bit.
848 * @param __val Either true or false, defaults to true.
849 * @throw std::out_of_range If @a __pos is bigger the size of the %set.
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);
860 * @brief Sets every bit to false.
870 * @brief Sets a given bit to false.
871 * @param __pos The index of the bit.
872 * @throw std::out_of_range If @a __pos is bigger the size of the %set.
874 * Same as writing @c set(__pos, false).
877 reset(size_type __pos)
880 __throw_out_of_range(__N("dynamic_bitset::reset"));
881 return this->_M_unchecked_reset(__pos);
885 * @brief Toggles every bit to its opposite value.
891 this->_M_do_sanitize();
896 * @brief Toggles a given bit to its opposite value.
897 * @param __pos The index of the bit.
898 * @throw std::out_of_range If @a __pos is bigger the size of the %set.
901 flip(size_type __pos)
904 __throw_out_of_range(__N("dynamic_bitset::flip"));
905 return this->_M_unchecked_flip(__pos);
908 /// See the no-argument flip().
911 { return dynamic_bitset<_WordT, _Alloc>(*this).flip(); }
915 * @brief Array-indexing support.
916 * @param __pos Index into the %dynamic_bitset.
917 * @return A bool for a 'const %dynamic_bitset'. For non-const
918 * bitsets, an instance of the reference proxy class.
919 * @note These operators do no range checking and throw no
920 * exceptions, as required by DR 11 to the standard.
923 operator[](size_type __pos)
924 { return reference(*this,__pos); }
927 operator[](size_type __pos) const
928 { return _M_unchecked_test(__pos); }
932 * @brief Returns a numerical interpretation of the %dynamic_bitset.
933 * @return The integral equivalent of the bits.
934 * @throw std::overflow_error If there are too many bits to be
935 * represented in an @c unsigned @c long.
939 { return this->_M_do_to_ulong(); }
942 * @brief Returns a numerical interpretation of the %dynamic_bitset.
943 * @return The integral equivalent of the bits.
944 * @throw std::overflow_error If there are too many bits to be
945 * represented in an @c unsigned @c long.
949 { return this->_M_do_to_ullong(); }
952 * @brief Returns a character interpretation of the %dynamic_bitset.
953 * @return The string equivalent of the bits.
955 * Note the ordering of the bits: decreasing character positions
956 * correspond to increasing bit positions (see the main class notes for
959 template<typename _CharT = char,
960 typename _Traits = std::char_traits<_CharT>,
961 typename _Alloc1 = std::allocator<_CharT>>
962 std::basic_string<_CharT, _Traits, _Alloc1>
963 to_string(_CharT __zero = _CharT('0'), _CharT __one = _CharT('1')) const
965 std::basic_string<_CharT, _Traits, _Alloc1> __result;
966 _M_copy_to_string(__result, __zero, __one);
970 // Helper functions for string operations.
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>
980 _M_copy_from_string(const basic_string<_CharT, _Traits, _Alloc1>& __str,
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>
991 _M_copy_to_string(std::basic_string<_CharT, _Traits, _Alloc1>& __str,
992 _CharT __zero = _CharT('0'),
993 _CharT __one = _CharT('1')) const;
995 /// Returns the number of bits which are set.
997 count() const noexcept
998 { return this->_M_do_count(); }
1000 /// Returns the total number of bits.
1002 size() const noexcept
1003 { return this->_M_Nb; }
1005 /// Returns the total number of blocks.
1007 num_blocks() const noexcept
1008 { return this->_M_size(); }
1010 /// Returns true if the dynamic_bitset is empty.
1011 _GLIBCXX_NODISCARD bool
1012 empty() const noexcept
1013 { return (this->_M_Nb == 0); }
1015 /// Returns the maximum size of a dynamic_bitset object having the same
1017 /// The real answer is max() * bits_per_block but is likely to overflow.
1020 { return std::numeric_limits<block_type>::max(); }
1023 * @brief Tests the value of a bit.
1024 * @param __pos The index of a bit.
1025 * @return The value at @a __pos.
1026 * @throw std::out_of_range If @a __pos is bigger the size of the %set.
1029 test(size_type __pos) const
1032 __throw_out_of_range(__N("dynamic_bitset::test"));
1033 return _M_unchecked_test(__pos);
1037 * @brief Tests whether all the bits are on.
1038 * @return True if all the bits are set.
1042 { return this->_M_are_all_aux() == _M_Nb; }
1045 * @brief Tests whether any of the bits are on.
1046 * @return True if at least one bit is set.
1050 { return this->_M_is_any(); }
1053 * @brief Tests whether any of the bits are on.
1054 * @return True if none of the bits are set.
1058 { return !this->_M_is_any(); }
1061 /// Self-explanatory.
1063 operator<<(size_type __pos) const
1064 { return dynamic_bitset(*this) <<= __pos; }
1067 operator>>(size_type __pos) const
1068 { return dynamic_bitset(*this) >>= __pos; }
1072 * @brief Finds the index of the first "on" bit.
1073 * @return The index of the first bit set, or size() if not found.
1078 { return this->_M_do_find_first(this->_M_Nb); }
1081 * @brief Finds the index of the next "on" bit after prev.
1082 * @return The index of the next bit set, or size() if not found.
1083 * @param __prev Where to start searching.
1087 find_next(size_t __prev) const
1088 { return this->_M_do_find_next(__prev, this->_M_Nb); }
1091 is_subset_of(const dynamic_bitset& __b) const
1092 { return this->_M_is_subset_of(__b); }
1095 is_proper_subset_of(const dynamic_bitset& __b) const
1096 { return this->_M_is_proper_subset_of(__b); }
1099 operator==(const dynamic_bitset& __lhs,
1100 const dynamic_bitset& __rhs) noexcept
1101 { return __lhs._M_Nb == __rhs._M_Nb && __lhs._M_is_equal(__rhs); }
1104 operator<(const dynamic_bitset& __lhs,
1105 const dynamic_bitset& __rhs) noexcept
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>::
1113 _M_copy_to_string(std::basic_string<_CharT, _Traits, _Alloc1>& __str,
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);
1124 /// These comparisons for equality/inequality are, well, @e bitwise.
1126 template<typename _WordT, typename _Alloc>
1128 operator!=(const dynamic_bitset<_WordT, _Alloc>& __lhs,
1129 const dynamic_bitset<_WordT, _Alloc>& __rhs)
1130 { return !(__lhs == __rhs); }
1132 template<typename _WordT, typename _Alloc>
1134 operator<=(const dynamic_bitset<_WordT, _Alloc>& __lhs,
1135 const dynamic_bitset<_WordT, _Alloc>& __rhs)
1136 { return !(__lhs > __rhs); }
1138 template<typename _WordT, typename _Alloc>
1140 operator>(const dynamic_bitset<_WordT, _Alloc>& __lhs,
1141 const dynamic_bitset<_WordT, _Alloc>& __rhs)
1142 { return __rhs < __lhs; }
1144 template<typename _WordT, typename _Alloc>
1146 operator>=(const dynamic_bitset<_WordT, _Alloc>& __lhs,
1147 const dynamic_bitset<_WordT, _Alloc>& __rhs)
1148 { return !(__lhs < __rhs); }
1151 // 23.3.5.3 bitset operations:
1154 * @brief Global bitwise operations on bitsets.
1155 * @param __x A bitset.
1156 * @param __y A bitset of the same size as @a __x.
1157 * @return A new bitset.
1159 * These should be self-explanatory.
1161 template<typename _WordT, typename _Alloc>
1162 inline dynamic_bitset<_WordT, _Alloc>
1163 operator&(const dynamic_bitset<_WordT, _Alloc>& __x,
1164 const dynamic_bitset<_WordT, _Alloc>& __y)
1166 dynamic_bitset<_WordT, _Alloc> __result(__x);
1171 template<typename _WordT, typename _Alloc>
1172 inline dynamic_bitset<_WordT, _Alloc>
1173 operator|(const dynamic_bitset<_WordT, _Alloc>& __x,
1174 const dynamic_bitset<_WordT, _Alloc>& __y)
1176 dynamic_bitset<_WordT, _Alloc> __result(__x);
1181 template <typename _WordT, typename _Alloc>
1182 inline dynamic_bitset<_WordT, _Alloc>
1183 operator^(const dynamic_bitset<_WordT, _Alloc>& __x,
1184 const dynamic_bitset<_WordT, _Alloc>& __y)
1186 dynamic_bitset<_WordT, _Alloc> __result(__x);
1191 template <typename _WordT, typename _Alloc>
1192 inline dynamic_bitset<_WordT, _Alloc>
1193 operator-(const dynamic_bitset<_WordT, _Alloc>& __x,
1194 const dynamic_bitset<_WordT, _Alloc>& __y)
1196 dynamic_bitset<_WordT, _Alloc> __result(__x);
1202 /// Stream output operator for dynamic_bitset.
1203 template <typename _CharT, typename _Traits,
1204 typename _WordT, typename _Alloc>
1205 inline std::basic_ostream<_CharT, _Traits>&
1206 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1207 const dynamic_bitset<_WordT, _Alloc>& __x)
1209 std::basic_string<_CharT, _Traits> __tmp;
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
1223#include <tr2/dynamic_bitset.tcc>
1225#endif /* _GLIBCXX_TR2_DYNAMIC_BITSET */