1 // TR2 <dynamic_bitset> -*- C++ -*-
3 // Copyright (C) 2009-2019 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
32 #pragma GCC system_header
38 #include <bits/functexcept.h>
39 #include <bits/stl_algo.h> // For fill
40 #include <bits/cxxabi_forced.h>
42 namespace std _GLIBCXX_VISIBILITY(default)
44 _GLIBCXX_BEGIN_NAMESPACE_VERSION
49 * @defgroup dynamic_bitset Dynamic Bitset.
56 * Base class, general case.
58 * See documentation for dynamic_bitset.
60 template<typename _WordT = unsigned long long,
61 typename _Alloc = std::allocator<_WordT>>
62 struct __dynamic_bitset_base
64 static_assert(std::is_unsigned<_WordT>::value, "template argument "
65 "_WordT not an unsigned integral type");
67 typedef _WordT block_type;
68 typedef _Alloc allocator_type;
69 typedef size_t size_type;
71 static const size_type _S_bits_per_block = __CHAR_BIT__ * sizeof(block_type);
72 static const size_type npos = static_cast<size_type>(-1);
74 /// 0 is the least significant word.
75 std::vector<block_type, allocator_type> _M_w;
78 __dynamic_bitset_base(const allocator_type& __alloc)
82 __dynamic_bitset_base() = default;
83 __dynamic_bitset_base(const __dynamic_bitset_base&) = default;
84 __dynamic_bitset_base(__dynamic_bitset_base&& __b) = default;
85 __dynamic_bitset_base& operator=(const __dynamic_bitset_base&) = default;
86 __dynamic_bitset_base& operator=(__dynamic_bitset_base&&) = default;
87 ~__dynamic_bitset_base() = default;
90 __dynamic_bitset_base(size_type __nbits, unsigned long long __val = 0ULL,
91 const allocator_type& __alloc = allocator_type())
92 : _M_w(__nbits / _S_bits_per_block + (__nbits % _S_bits_per_block > 0),
93 block_type(0), __alloc)
95 if (__nbits < std::numeric_limits<decltype(__val)>::digits)
96 __val &= ~(-1ULL << __nbits);
100 if _GLIBCXX17_CONSTEXPR (sizeof(__val) == sizeof(block_type))
105 = std::min(_M_w.size(), sizeof(__val) / sizeof(block_type));
106 for (size_t __i = 0; __val && __i < __n; ++__i)
108 _M_w[__i] = static_cast<block_type>(__val);
109 __val >>= _S_bits_per_block;
115 _M_swap(__dynamic_bitset_base& __b) noexcept
116 { this->_M_w.swap(__b._M_w); }
120 { this->_M_w.clear(); }
123 _M_resize(size_t __nbits, bool __value)
125 size_t __sz = __nbits / _S_bits_per_block;
126 if (__nbits % _S_bits_per_block > 0)
128 if (__sz != this->_M_w.size())
130 block_type __val = 0;
132 __val = std::numeric_limits<block_type>::max();
133 this->_M_w.resize(__sz, __val);
138 _M_get_allocator() const noexcept
139 { return this->_M_w.get_allocator(); }
142 _S_whichword(size_type __pos) noexcept
143 { return __pos / _S_bits_per_block; }
146 _S_whichbyte(size_type __pos) noexcept
147 { return (__pos % _S_bits_per_block) / __CHAR_BIT__; }
150 _S_whichbit(size_type __pos) noexcept
151 { return __pos % _S_bits_per_block; }
154 _S_maskbit(size_type __pos) noexcept
155 { return (static_cast<block_type>(1)) << _S_whichbit(__pos); }
158 _M_getword(size_type __pos) noexcept
159 { return this->_M_w[_S_whichword(__pos)]; }
162 _M_getword(size_type __pos) const noexcept
163 { return this->_M_w[_S_whichword(__pos)]; }
167 { return this->_M_w[_M_w.size() - 1]; }
170 _M_hiword() const noexcept
171 { return this->_M_w[_M_w.size() - 1]; }
174 _M_do_and(const __dynamic_bitset_base& __x) noexcept
176 if (__x._M_w.size() == this->_M_w.size())
177 for (size_t __i = 0; __i < this->_M_w.size(); ++__i)
178 this->_M_w[__i] &= __x._M_w[__i];
184 _M_do_or(const __dynamic_bitset_base& __x) noexcept
186 if (__x._M_w.size() == this->_M_w.size())
187 for (size_t __i = 0; __i < this->_M_w.size(); ++__i)
188 this->_M_w[__i] |= __x._M_w[__i];
194 _M_do_xor(const __dynamic_bitset_base& __x) noexcept
196 if (__x._M_w.size() == this->_M_w.size())
197 for (size_t __i = 0; __i < this->_M_w.size(); ++__i)
198 this->_M_w[__i] ^= __x._M_w[__i];
204 _M_do_dif(const __dynamic_bitset_base& __x) noexcept
206 if (__x._M_w.size() == this->_M_w.size())
207 for (size_t __i = 0; __i < this->_M_w.size(); ++__i)
208 this->_M_w[__i] &= ~__x._M_w[__i];
214 _M_do_left_shift(size_t __shift);
217 _M_do_right_shift(size_t __shift);
220 _M_do_flip() noexcept
222 for (size_t __i = 0; __i < this->_M_w.size(); ++__i)
223 this->_M_w[__i] = ~this->_M_w[__i];
229 for (size_t __i = 0; __i < this->_M_w.size(); ++__i)
230 this->_M_w[__i] = static_cast<block_type>(-1);
234 _M_do_reset() noexcept
236 std::fill(_M_w.begin(), _M_w.end(), static_cast<block_type>(0));
240 _M_is_equal(const __dynamic_bitset_base& __x) const noexcept
242 if (__x._M_w.size() == this->_M_w.size())
244 for (size_t __i = 0; __i < this->_M_w.size(); ++__i)
245 if (this->_M_w[__i] != __x._M_w[__i])
254 _M_is_less(const __dynamic_bitset_base& __x) const noexcept
256 if (__x._M_w.size() == this->_M_w.size())
258 for (size_t __i = this->_M_w.size(); __i > 0; --__i)
260 if (this->_M_w[__i-1] < __x._M_w[__i-1])
262 else if (this->_M_w[__i-1] > __x._M_w[__i-1])
272 _M_are_all_aux() const noexcept
274 for (size_t __i = 0; __i < this->_M_w.size() - 1; ++__i)
275 if (_M_w[__i] != static_cast<block_type>(-1))
277 return ((this->_M_w.size() - 1) * _S_bits_per_block
278 + __builtin_popcountll(this->_M_hiword()));
282 _M_is_any() const noexcept
284 for (size_t __i = 0; __i < this->_M_w.size(); ++__i)
285 if (this->_M_w[__i] != static_cast<block_type>(0))
291 _M_is_subset_of(const __dynamic_bitset_base& __b) noexcept
293 if (__b._M_w.size() == this->_M_w.size())
295 for (size_t __i = 0; __i < this->_M_w.size(); ++__i)
296 if (this->_M_w[__i] != (this->_M_w[__i] | __b._M_w[__i]))
305 _M_is_proper_subset_of(const __dynamic_bitset_base& __b) const noexcept
307 if (this->is_subset_of(__b))
319 _M_do_count() const noexcept
322 for (size_t __i = 0; __i < this->_M_w.size(); ++__i)
323 __result += __builtin_popcountll(this->_M_w[__i]);
328 _M_size() const noexcept
329 { return this->_M_w.size(); }
332 _M_do_to_ulong() const;
335 _M_do_to_ullong() const;
337 // find first "on" bit
339 _M_do_find_first(size_t __not_found) const;
341 // find the next "on" bit that follows "prev"
343 _M_do_find_next(size_t __prev, size_t __not_found) const;
345 // do append of block
347 _M_do_append_block(block_type __block, size_type __pos)
349 size_t __offset = __pos % _S_bits_per_block;
351 this->_M_w.push_back(__block);
354 this->_M_hiword() |= (__block << __offset);
355 this->_M_w.push_back(__block >> (_S_bits_per_block - __offset));
361 * @brief The %dynamic_bitset class represents a sequence of bits.
364 * Proposal to Add a Dynamically Sizeable Bitset to the Standard Library.
365 * http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2006/n2050.pdf
367 * In the general unoptimized case, storage is allocated in
368 * word-sized blocks. Let B be the number of bits in a word, then
369 * (Nb+(B-1))/B words will be used for storage. B - Nb%B bits are
370 * unused. (They are the high-order bits in the highest word.) It
371 * is a class invariant that those unused bits are always zero.
373 * If you think of %dynamic_bitset as "a simple array of bits," be
374 * aware that your mental picture is reversed: a %dynamic_bitset
375 * behaves the same way as bits in integers do, with the bit at
376 * index 0 in the "least significant / right-hand" position, and
377 * the bit at index Nb-1 in the "most significant / left-hand"
378 * position. Thus, unlike other containers, a %dynamic_bitset's
379 * index "counts from right to left," to put it very loosely.
381 * This behavior is preserved when translating to and from strings.
382 * For example, the first line of the following program probably
383 * prints "b('a') is 0001100001" on a modern ASCII system.
386 * #include <dynamic_bitset>
387 * #include <iostream>
390 * using namespace std;
395 * dynamic_bitset<> b(a);
397 * cout << "b('a') is " << b << endl;
401 * string str = s.str();
402 * cout << "index 3 in the string is " << str[3] << " but\n"
403 * << "index 3 in the bitset is " << b[3] << endl;
407 * Most of the actual code isn't contained in %dynamic_bitset<>
408 * itself, but in the base class __dynamic_bitset_base. The base
409 * class works with whole words, not with individual bits. This
410 * allows us to specialize __dynamic_bitset_base for the important
411 * special case where the %dynamic_bitset is only a single word.
413 * Extra confusion can result due to the fact that the storage for
414 * __dynamic_bitset_base @e is a vector, and is indexed as such. This is
415 * carefully encapsulated.
417 template<typename _WordT = unsigned long long,
418 typename _Alloc = std::allocator<_WordT>>
420 : private __dynamic_bitset_base<_WordT, _Alloc>
422 static_assert(std::is_unsigned<_WordT>::value, "template argument "
423 "_WordT not an unsigned integral type");
427 typedef __dynamic_bitset_base<_WordT, _Alloc> _Base;
428 typedef _WordT block_type;
429 typedef _Alloc allocator_type;
430 typedef size_t size_type;
432 static const size_type bits_per_block = __CHAR_BIT__ * sizeof(block_type);
433 // Use this: constexpr size_type std::numeric_limits<size_type>::max().
434 static const size_type npos = static_cast<size_type>(-1);
438 // Clear the unused bits in the uppermost word.
442 size_type __shift = this->_M_Nb % bits_per_block;
444 this->_M_hiword() &= block_type(~(block_type(-1) << __shift));
447 // Set the unused bits in the uppermost word.
451 size_type __shift = this->_M_Nb % bits_per_block;
453 this->_M_hiword() |= block_type(block_type(-1) << __shift);
457 * These versions of single-bit set, reset, flip, and test
458 * do no range checking.
461 _M_unchecked_set(size_type __pos) noexcept
463 this->_M_getword(__pos) |= _Base::_S_maskbit(__pos);
468 _M_unchecked_set(size_type __pos, int __val) noexcept
471 this->_M_getword(__pos) |= _Base::_S_maskbit(__pos);
473 this->_M_getword(__pos) &= ~_Base::_S_maskbit(__pos);
478 _M_unchecked_reset(size_type __pos) noexcept
480 this->_M_getword(__pos) &= ~_Base::_S_maskbit(__pos);
485 _M_unchecked_flip(size_type __pos) noexcept
487 this->_M_getword(__pos) ^= _Base::_S_maskbit(__pos);
492 _M_unchecked_test(size_type __pos) const noexcept
493 { return ((this->_M_getword(__pos) & _Base::_S_maskbit(__pos))
494 != static_cast<_WordT>(0)); }
500 * This encapsulates the concept of a single bit. An instance
501 * of this class is a proxy for an actual bit; this way the
502 * individual bit operations are done as faster word-size
503 * bitwise instructions.
505 * Most users will never need to use this class directly;
506 * conversions to and from bool are automatic and should be
507 * transparent. Overloaded operators help to preserve the
510 * (On a typical system, this "bit %reference" is 64 times the
511 * size of an actual bit. Ha.)
515 friend class dynamic_bitset;
521 reference(dynamic_bitset& __b, size_type __pos) noexcept
523 this->_M_wp = &__b._M_getword(__pos);
524 this->_M_bpos = _Base::_S_whichbit(__pos);
529 operator=(bool __x) noexcept
532 *this->_M_wp |= _Base::_S_maskbit(this->_M_bpos);
534 *this->_M_wp &= ~_Base::_S_maskbit(this->_M_bpos);
538 // For b[i] = b[__j];
540 operator=(const reference& __j) noexcept
542 if ((*(__j._M_wp) & _Base::_S_maskbit(__j._M_bpos)))
543 *this->_M_wp |= _Base::_S_maskbit(this->_M_bpos);
545 *this->_M_wp &= ~_Base::_S_maskbit(this->_M_bpos);
551 operator~() const noexcept
552 { return (*(_M_wp) & _Base::_S_maskbit(this->_M_bpos)) == 0; }
555 operator bool() const noexcept
556 { return (*(this->_M_wp) & _Base::_S_maskbit(this->_M_bpos)) != 0; }
562 *this->_M_wp ^= _Base::_S_maskbit(this->_M_bpos);
567 friend class reference;
569 typedef bool const_reference;
571 // 23.3.5.1 constructors:
573 /// All bits set to zero.
574 dynamic_bitset() = default;
576 /// All bits set to zero.
578 dynamic_bitset(const allocator_type& __alloc)
582 /// Initial bits bitwise-copied from a single word (others set to zero).
584 dynamic_bitset(size_type __nbits, unsigned long long __val = 0ULL,
585 const allocator_type& __alloc = allocator_type())
586 : _Base(__nbits, __val, __alloc),
590 dynamic_bitset(initializer_list<block_type> __il,
591 const allocator_type& __alloc = allocator_type())
593 { this->append(__il); }
596 * @brief Use a subset of a string.
597 * @param __str A string of '0' and '1' characters.
598 * @param __pos Index of the first character in @p __str to use.
599 * @param __n The number of characters to copy.
600 * @param __zero The character to use for unset bits.
601 * @param __one The character to use for set bits.
602 * @param __alloc An allocator.
603 * @throw std::out_of_range If @p __pos is bigger the size of @p __str.
604 * @throw std::invalid_argument If a character appears in the string
605 * which is neither '0' nor '1'.
607 template<typename _CharT, typename _Traits, typename _Alloc1>
609 dynamic_bitset(const std::basic_string<_CharT, _Traits, _Alloc1>& __str,
610 typename basic_string<_CharT,_Traits,_Alloc1>::size_type
612 typename basic_string<_CharT,_Traits,_Alloc1>::size_type
613 __n = std::basic_string<_CharT, _Traits, _Alloc1>::npos,
614 _CharT __zero = _CharT('0'), _CharT __one = _CharT('1'),
615 const allocator_type& __alloc = allocator_type())
618 if (__pos > __str.size())
619 __throw_out_of_range(__N("dynamic_bitset::bitset initial position "
623 this->_M_Nb = (__n > __str.size() ? __str.size() - __pos : __n);
624 this->resize(this->_M_Nb);
625 this->_M_copy_from_string(__str, __pos, __n);
629 * @brief Construct from a string.
630 * @param __str A string of '0' and '1' characters.
631 * @param __alloc An allocator.
632 * @throw std::invalid_argument If a character appears in the string
633 * which is neither '0' nor '1'.
636 dynamic_bitset(const char* __str,
637 const allocator_type& __alloc = allocator_type())
638 : _Base(__builtin_strlen(__str), 0ULL, __alloc),
639 _M_Nb(__builtin_strlen(__str))
641 this->_M_copy_from_ptr(__str, _M_Nb, 0, _M_Nb);
644 /// Copy constructor.
645 dynamic_bitset(const dynamic_bitset&) = default;
647 /// Move constructor.
648 dynamic_bitset(dynamic_bitset&& __b) noexcept
649 : _Base(std::move(__b)), _M_Nb(__b._M_Nb)
652 /// Swap with another bitset.
654 swap(dynamic_bitset& __b) noexcept
657 std::swap(this->_M_Nb, __b._M_Nb);
660 /// Copy assignment operator.
661 dynamic_bitset& operator=(const dynamic_bitset&) = default;
663 /// Move assignment operator.
665 operator=(dynamic_bitset&& __b)
666 noexcept(std::is_nothrow_move_assignable<_Base>::value)
668 static_cast<_Base&>(*this) = static_cast<_Base&&>(__b);
670 if _GLIBCXX17_CONSTEXPR (std::is_nothrow_move_assignable<_Base>::value)
672 else if (get_allocator() == __b.get_allocator())
678 * @brief Return the allocator for the bitset.
681 get_allocator() const noexcept
682 { return this->_M_get_allocator(); }
685 * @brief Resize the bitset.
688 resize(size_type __nbits, bool __value = false)
692 this->_M_resize(__nbits, __value);
693 this->_M_Nb = __nbits;
694 this->_M_do_sanitize();
698 * @brief Clear the bitset.
708 * @brief Push a bit onto the high end of the bitset.
711 push_back(bool __bit)
713 if (this->size() % bits_per_block == 0)
714 this->_M_do_append_block(block_type(__bit), this->_M_Nb);
716 this->_M_unchecked_set(this->_M_Nb, __bit);
720 // XXX why is there no pop_back() member in the proposal?
723 * @brief Append a block.
726 append(block_type __block)
728 this->_M_do_append_block(__block, this->_M_Nb);
729 this->_M_Nb += bits_per_block;
736 append(initializer_list<block_type> __il)
737 { this->append(__il.begin(), __il.end()); }
740 * @brief Append an iterator range of blocks.
742 template <typename _BlockInputIterator>
744 append(_BlockInputIterator __first, _BlockInputIterator __last)
746 for (; __first != __last; ++__first)
747 this->append(*__first);
750 // 23.3.5.2 dynamic_bitset operations:
753 * @brief Operations on dynamic_bitsets.
754 * @param __rhs A same-sized dynamic_bitset.
756 * These should be self-explanatory.
759 operator&=(const dynamic_bitset& __rhs)
761 this->_M_do_and(__rhs);
766 operator&=(dynamic_bitset&& __rhs)
768 this->_M_do_and(std::move(__rhs));
773 operator|=(const dynamic_bitset& __rhs)
775 this->_M_do_or(__rhs);
780 operator^=(const dynamic_bitset& __rhs)
782 this->_M_do_xor(__rhs);
787 operator-=(const dynamic_bitset& __rhs)
789 this->_M_do_dif(__rhs);
796 * @brief Operations on dynamic_bitsets.
797 * @param __pos The number of places to shift.
799 * These should be self-explanatory.
802 operator<<=(size_type __pos)
804 if (__builtin_expect(__pos < this->_M_Nb, 1))
806 this->_M_do_left_shift(__pos);
807 this->_M_do_sanitize();
815 operator>>=(size_type __pos)
817 if (__builtin_expect(__pos < this->_M_Nb, 1))
819 this->_M_do_right_shift(__pos);
820 this->_M_do_sanitize();
828 // Set, reset, and flip.
830 * @brief Sets every bit to true.
836 this->_M_do_sanitize();
841 * @brief Sets a given bit to a particular value.
842 * @param __pos The index of the bit.
843 * @param __val Either true or false, defaults to true.
844 * @throw std::out_of_range If @a __pos is bigger the size of the %set.
847 set(size_type __pos, bool __val = true)
850 __throw_out_of_range(__N("dynamic_bitset::set"));
851 return this->_M_unchecked_set(__pos, __val);
855 * @brief Sets every bit to false.
865 * @brief Sets a given bit to false.
866 * @param __pos The index of the bit.
867 * @throw std::out_of_range If @a __pos is bigger the size of the %set.
869 * Same as writing @c set(__pos, false).
872 reset(size_type __pos)
875 __throw_out_of_range(__N("dynamic_bitset::reset"));
876 return this->_M_unchecked_reset(__pos);
880 * @brief Toggles every bit to its opposite value.
886 this->_M_do_sanitize();
891 * @brief Toggles a given bit to its opposite value.
892 * @param __pos The index of the bit.
893 * @throw std::out_of_range If @a __pos is bigger the size of the %set.
896 flip(size_type __pos)
899 __throw_out_of_range(__N("dynamic_bitset::flip"));
900 return this->_M_unchecked_flip(__pos);
903 /// See the no-argument flip().
906 { return dynamic_bitset<_WordT, _Alloc>(*this).flip(); }
910 * @brief Array-indexing support.
911 * @param __pos Index into the %dynamic_bitset.
912 * @return A bool for a 'const %dynamic_bitset'. For non-const
913 * bitsets, an instance of the reference proxy class.
914 * @note These operators do no range checking and throw no
915 * exceptions, as required by DR 11 to the standard.
918 operator[](size_type __pos)
919 { return reference(*this,__pos); }
922 operator[](size_type __pos) const
923 { return _M_unchecked_test(__pos); }
927 * @brief Returns a numerical interpretation of the %dynamic_bitset.
928 * @return The integral equivalent of the bits.
929 * @throw std::overflow_error If there are too many bits to be
930 * represented in an @c unsigned @c long.
934 { return this->_M_do_to_ulong(); }
937 * @brief Returns a numerical interpretation of the %dynamic_bitset.
938 * @return The integral equivalent of the bits.
939 * @throw std::overflow_error If there are too many bits to be
940 * represented in an @c unsigned @c long.
944 { return this->_M_do_to_ullong(); }
947 * @brief Returns a character interpretation of the %dynamic_bitset.
948 * @return The string equivalent of the bits.
950 * Note the ordering of the bits: decreasing character positions
951 * correspond to increasing bit positions (see the main class notes for
954 template<typename _CharT = char,
955 typename _Traits = std::char_traits<_CharT>,
956 typename _Alloc1 = std::allocator<_CharT>>
957 std::basic_string<_CharT, _Traits, _Alloc1>
958 to_string(_CharT __zero = _CharT('0'), _CharT __one = _CharT('1')) const
960 std::basic_string<_CharT, _Traits, _Alloc1> __result;
961 _M_copy_to_string(__result, __zero, __one);
965 // Helper functions for string operations.
966 template<typename _Traits = std::char_traits<char>,
967 typename _CharT = typename _Traits::char_type>
969 _M_copy_from_ptr(const _CharT*, size_t, size_t, size_t,
970 _CharT __zero = _CharT('0'),
971 _CharT __one = _CharT('1'));
973 template<typename _CharT, typename _Traits, typename _Alloc1>
975 _M_copy_from_string(const basic_string<_CharT, _Traits, _Alloc1>& __str,
976 size_t __pos, size_t __n,
977 _CharT __zero = _CharT('0'),
978 _CharT __one = _CharT('1'))
980 _M_copy_from_ptr<_Traits>(__str.data(), __str.size(), __pos, __n,
984 template<typename _CharT, typename _Traits, typename _Alloc1>
986 _M_copy_to_string(std::basic_string<_CharT, _Traits, _Alloc1>& __str,
987 _CharT __zero = _CharT('0'),
988 _CharT __one = _CharT('1')) const;
990 /// Returns the number of bits which are set.
992 count() const noexcept
993 { return this->_M_do_count(); }
995 /// Returns the total number of bits.
997 size() const noexcept
998 { return this->_M_Nb; }
1000 /// Returns the total number of blocks.
1002 num_blocks() const noexcept
1003 { return this->_M_size(); }
1005 /// Returns true if the dynamic_bitset is empty.
1006 _GLIBCXX_NODISCARD bool
1007 empty() const noexcept
1008 { return (this->_M_Nb == 0); }
1010 /// Returns the maximum size of a dynamic_bitset object having the same
1012 /// The real answer is max() * bits_per_block but is likely to overflow.
1015 { return std::numeric_limits<block_type>::max(); }
1018 * @brief Tests the value of a bit.
1019 * @param __pos The index of a bit.
1020 * @return The value at @a __pos.
1021 * @throw std::out_of_range If @a __pos is bigger the size of the %set.
1024 test(size_type __pos) const
1027 __throw_out_of_range(__N("dynamic_bitset::test"));
1028 return _M_unchecked_test(__pos);
1032 * @brief Tests whether all the bits are on.
1033 * @return True if all the bits are set.
1037 { return this->_M_are_all_aux() == _M_Nb; }
1040 * @brief Tests whether any of the bits are on.
1041 * @return True if at least one bit is set.
1045 { return this->_M_is_any(); }
1048 * @brief Tests whether any of the bits are on.
1049 * @return True if none of the bits are set.
1053 { return !this->_M_is_any(); }
1056 /// Self-explanatory.
1058 operator<<(size_type __pos) const
1059 { return dynamic_bitset(*this) <<= __pos; }
1062 operator>>(size_type __pos) const
1063 { return dynamic_bitset(*this) >>= __pos; }
1067 * @brief Finds the index of the first "on" bit.
1068 * @return The index of the first bit set, or size() if not found.
1073 { return this->_M_do_find_first(this->_M_Nb); }
1076 * @brief Finds the index of the next "on" bit after prev.
1077 * @return The index of the next bit set, or size() if not found.
1078 * @param __prev Where to start searching.
1082 find_next(size_t __prev) const
1083 { return this->_M_do_find_next(__prev, this->_M_Nb); }
1086 is_subset_of(const dynamic_bitset& __b) const
1087 { return this->_M_is_subset_of(__b); }
1090 is_proper_subset_of(const dynamic_bitset& __b) const
1091 { return this->_M_is_proper_subset_of(__b); }
1094 operator==(const dynamic_bitset& __lhs,
1095 const dynamic_bitset& __rhs) noexcept
1096 { return __lhs._M_Nb == __rhs._M_Nb && __lhs._M_is_equal(__rhs); }
1099 operator<(const dynamic_bitset& __lhs,
1100 const dynamic_bitset& __rhs) noexcept
1101 { return __lhs._M_is_less(__rhs) || __lhs._M_Nb < __rhs._M_Nb; }
1104 template<typename _WordT, typename _Alloc>
1105 template<typename _CharT, typename _Traits, typename _Alloc1>
1107 dynamic_bitset<_WordT, _Alloc>::
1108 _M_copy_to_string(std::basic_string<_CharT, _Traits, _Alloc1>& __str,
1109 _CharT __zero, _CharT __one) const
1111 __str.assign(_M_Nb, __zero);
1112 for (size_t __i = _M_Nb; __i > 0; --__i)
1113 if (_M_unchecked_test(__i - 1))
1114 _Traits::assign(__str[_M_Nb - __i], __one);
1119 /// These comparisons for equality/inequality are, well, @e bitwise.
1121 template<typename _WordT, typename _Alloc>
1123 operator!=(const dynamic_bitset<_WordT, _Alloc>& __lhs,
1124 const dynamic_bitset<_WordT, _Alloc>& __rhs)
1125 { return !(__lhs == __rhs); }
1127 template<typename _WordT, typename _Alloc>
1129 operator<=(const dynamic_bitset<_WordT, _Alloc>& __lhs,
1130 const dynamic_bitset<_WordT, _Alloc>& __rhs)
1131 { return !(__lhs > __rhs); }
1133 template<typename _WordT, typename _Alloc>
1135 operator>(const dynamic_bitset<_WordT, _Alloc>& __lhs,
1136 const dynamic_bitset<_WordT, _Alloc>& __rhs)
1137 { return __rhs < __lhs; }
1139 template<typename _WordT, typename _Alloc>
1141 operator>=(const dynamic_bitset<_WordT, _Alloc>& __lhs,
1142 const dynamic_bitset<_WordT, _Alloc>& __rhs)
1143 { return !(__lhs < __rhs); }
1146 // 23.3.5.3 bitset operations:
1149 * @brief Global bitwise operations on bitsets.
1150 * @param __x A bitset.
1151 * @param __y A bitset of the same size as @a __x.
1152 * @return A new bitset.
1154 * These should be self-explanatory.
1156 template<typename _WordT, typename _Alloc>
1157 inline dynamic_bitset<_WordT, _Alloc>
1158 operator&(const dynamic_bitset<_WordT, _Alloc>& __x,
1159 const dynamic_bitset<_WordT, _Alloc>& __y)
1161 dynamic_bitset<_WordT, _Alloc> __result(__x);
1166 template<typename _WordT, typename _Alloc>
1167 inline dynamic_bitset<_WordT, _Alloc>
1168 operator|(const dynamic_bitset<_WordT, _Alloc>& __x,
1169 const dynamic_bitset<_WordT, _Alloc>& __y)
1171 dynamic_bitset<_WordT, _Alloc> __result(__x);
1176 template <typename _WordT, typename _Alloc>
1177 inline dynamic_bitset<_WordT, _Alloc>
1178 operator^(const dynamic_bitset<_WordT, _Alloc>& __x,
1179 const dynamic_bitset<_WordT, _Alloc>& __y)
1181 dynamic_bitset<_WordT, _Alloc> __result(__x);
1186 template <typename _WordT, typename _Alloc>
1187 inline dynamic_bitset<_WordT, _Alloc>
1188 operator-(const dynamic_bitset<_WordT, _Alloc>& __x,
1189 const dynamic_bitset<_WordT, _Alloc>& __y)
1191 dynamic_bitset<_WordT, _Alloc> __result(__x);
1197 /// Stream output operator for dynamic_bitset.
1198 template <typename _CharT, typename _Traits,
1199 typename _WordT, typename _Alloc>
1200 inline std::basic_ostream<_CharT, _Traits>&
1201 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1202 const dynamic_bitset<_WordT, _Alloc>& __x)
1204 std::basic_string<_CharT, _Traits> __tmp;
1206 const ctype<_CharT>& __ct = use_facet<ctype<_CharT>>(__os.getloc());
1207 __x._M_copy_to_string(__tmp, __ct.widen('0'), __ct.widen('1'));
1208 return __os << __tmp;
1215 _GLIBCXX_END_NAMESPACE_VERSION
1218 #include <tr2/dynamic_bitset.tcc>
1220 #endif /* _GLIBCXX_TR2_DYNAMIC_BITSET */