1 // <bitset> -*- C++ -*-
3 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010,
5 // Free Software Foundation, Inc.
7 // This file is part of the GNU ISO C++ Library. This library is free
8 // software; you can redistribute it and/or modify it under the
9 // terms of the GNU General Public License as published by the
10 // Free Software Foundation; either version 3, or (at your option)
13 // This library is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
18 // Under Section 7 of GPL version 3, you are granted additional
19 // permissions described in the GCC Runtime Library Exception, version
20 // 3.1, as published by the Free Software Foundation.
22 // You should have received a copy of the GNU General Public License and
23 // a copy of the GCC Runtime Library Exception along with this program;
24 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
25 // <http://www.gnu.org/licenses/>.
29 * Silicon Graphics Computer Systems, Inc.
31 * Permission to use, copy, modify, distribute and sell this software
32 * and its documentation for any purpose is hereby granted without fee,
33 * provided that the above copyright notice appear in all copies and
34 * that both that copyright notice and this permission notice appear
35 * in supporting documentation. Silicon Graphics makes no
36 * representations about the suitability of this software for any
37 * purpose. It is provided "as is" without express or implied warranty.
40 /** @file include/bitset
41 * This is a Standard C++ Library header.
44 #ifndef _GLIBCXX_BITSET
45 #define _GLIBCXX_BITSET 1
47 #pragma GCC system_header
50 #include <bits/functexcept.h> // For invalid_argument, out_of_range,
53 #include <bits/cxxabi_forced.h>
55 #define _GLIBCXX_BITSET_BITS_PER_WORD (__CHAR_BIT__ * __SIZEOF_LONG__)
56 #define _GLIBCXX_BITSET_WORDS(__n) \
57 ((__n) / _GLIBCXX_BITSET_BITS_PER_WORD + \
58 ((__n) % _GLIBCXX_BITSET_BITS_PER_WORD == 0 ? 0 : 1))
60 #define _GLIBCXX_BITSET_BITS_PER_ULL (__CHAR_BIT__ * __SIZEOF_LONG_LONG__)
62 namespace std _GLIBCXX_VISIBILITY(default)
64 _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
67 * Base class, general case. It is a class invariant that _Nw will be
70 * See documentation for bitset.
75 typedef unsigned long _WordT;
77 /// 0 is the least significant word.
80 _GLIBCXX_CONSTEXPR _Base_bitset()
83 #ifdef __GXX_EXPERIMENTAL_CXX0X__
84 constexpr _Base_bitset(unsigned long long __val)
85 : _M_w({ _WordT(__val)
86 #if __SIZEOF_LONG_LONG__ > __SIZEOF_LONG__
87 , _WordT(__val >> _GLIBCXX_BITSET_BITS_PER_WORD)
91 _Base_bitset(unsigned long __val)
96 static _GLIBCXX_CONSTEXPR size_t
97 _S_whichword(size_t __pos )
98 { return __pos / _GLIBCXX_BITSET_BITS_PER_WORD; }
100 static _GLIBCXX_CONSTEXPR size_t
101 _S_whichbyte(size_t __pos )
102 { return (__pos % _GLIBCXX_BITSET_BITS_PER_WORD) / __CHAR_BIT__; }
104 static _GLIBCXX_CONSTEXPR size_t
105 _S_whichbit(size_t __pos )
106 { return __pos % _GLIBCXX_BITSET_BITS_PER_WORD; }
108 static _GLIBCXX_CONSTEXPR _WordT
109 _S_maskbit(size_t __pos )
110 { return (static_cast<_WordT>(1)) << _S_whichbit(__pos); }
113 _M_getword(size_t __pos)
114 { return _M_w[_S_whichword(__pos)]; }
117 _M_getword(size_t __pos) const
118 { return _M_w[_S_whichword(__pos)]; }
120 #ifdef __GXX_EXPERIMENTAL_CXX0X__
128 { return _M_w[_Nw - 1]; }
130 _GLIBCXX_CONSTEXPR _WordT
132 { return _M_w[_Nw - 1]; }
135 _M_do_and(const _Base_bitset<_Nw>& __x)
137 for (size_t __i = 0; __i < _Nw; __i++)
138 _M_w[__i] &= __x._M_w[__i];
142 _M_do_or(const _Base_bitset<_Nw>& __x)
144 for (size_t __i = 0; __i < _Nw; __i++)
145 _M_w[__i] |= __x._M_w[__i];
149 _M_do_xor(const _Base_bitset<_Nw>& __x)
151 for (size_t __i = 0; __i < _Nw; __i++)
152 _M_w[__i] ^= __x._M_w[__i];
156 _M_do_left_shift(size_t __shift);
159 _M_do_right_shift(size_t __shift);
164 for (size_t __i = 0; __i < _Nw; __i++)
165 _M_w[__i] = ~_M_w[__i];
171 for (size_t __i = 0; __i < _Nw; __i++)
172 _M_w[__i] = ~static_cast<_WordT>(0);
177 { __builtin_memset(_M_w, 0, _Nw * sizeof(_WordT)); }
180 _M_is_equal(const _Base_bitset<_Nw>& __x) const
182 for (size_t __i = 0; __i < _Nw; ++__i)
183 if (_M_w[__i] != __x._M_w[__i])
189 _M_are_all_aux() const
191 for (size_t __i = 0; __i < _Nw - 1; __i++)
192 if (_M_w[__i] != ~static_cast<_WordT>(0))
194 return ((_Nw - 1) * _GLIBCXX_BITSET_BITS_PER_WORD
195 + __builtin_popcountl(_M_hiword()));
201 for (size_t __i = 0; __i < _Nw; __i++)
202 if (_M_w[__i] != static_cast<_WordT>(0))
211 for (size_t __i = 0; __i < _Nw; __i++)
212 __result += __builtin_popcountl(_M_w[__i]);
217 _M_do_to_ulong() const;
219 #ifdef __GXX_EXPERIMENTAL_CXX0X__
221 _M_do_to_ullong() const;
224 // find first "on" bit
226 _M_do_find_first(size_t __not_found) const;
228 // find the next "on" bit that follows "prev"
230 _M_do_find_next(size_t __prev, size_t __not_found) const;
233 // Definitions of non-inline functions from _Base_bitset.
236 _Base_bitset<_Nw>::_M_do_left_shift(size_t __shift)
238 if (__builtin_expect(__shift != 0, 1))
240 const size_t __wshift = __shift / _GLIBCXX_BITSET_BITS_PER_WORD;
241 const size_t __offset = __shift % _GLIBCXX_BITSET_BITS_PER_WORD;
244 for (size_t __n = _Nw - 1; __n >= __wshift; --__n)
245 _M_w[__n] = _M_w[__n - __wshift];
248 const size_t __sub_offset = (_GLIBCXX_BITSET_BITS_PER_WORD
250 for (size_t __n = _Nw - 1; __n > __wshift; --__n)
251 _M_w[__n] = ((_M_w[__n - __wshift] << __offset)
252 | (_M_w[__n - __wshift - 1] >> __sub_offset));
253 _M_w[__wshift] = _M_w[0] << __offset;
256 std::fill(_M_w + 0, _M_w + __wshift, static_cast<_WordT>(0));
262 _Base_bitset<_Nw>::_M_do_right_shift(size_t __shift)
264 if (__builtin_expect(__shift != 0, 1))
266 const size_t __wshift = __shift / _GLIBCXX_BITSET_BITS_PER_WORD;
267 const size_t __offset = __shift % _GLIBCXX_BITSET_BITS_PER_WORD;
268 const size_t __limit = _Nw - __wshift - 1;
271 for (size_t __n = 0; __n <= __limit; ++__n)
272 _M_w[__n] = _M_w[__n + __wshift];
275 const size_t __sub_offset = (_GLIBCXX_BITSET_BITS_PER_WORD
277 for (size_t __n = 0; __n < __limit; ++__n)
278 _M_w[__n] = ((_M_w[__n + __wshift] >> __offset)
279 | (_M_w[__n + __wshift + 1] << __sub_offset));
280 _M_w[__limit] = _M_w[_Nw-1] >> __offset;
283 std::fill(_M_w + __limit + 1, _M_w + _Nw, static_cast<_WordT>(0));
289 _Base_bitset<_Nw>::_M_do_to_ulong() const
291 for (size_t __i = 1; __i < _Nw; ++__i)
293 __throw_overflow_error(__N("_Base_bitset::_M_do_to_ulong"));
297 #ifdef __GXX_EXPERIMENTAL_CXX0X__
300 _Base_bitset<_Nw>::_M_do_to_ullong() const
302 const bool __dw = sizeof(unsigned long long) > sizeof(unsigned long);
303 for (size_t __i = 1 + __dw; __i < _Nw; ++__i)
305 __throw_overflow_error(__N("_Base_bitset::_M_do_to_ullong"));
308 return _M_w[0] + (static_cast<unsigned long long>(_M_w[1])
309 << _GLIBCXX_BITSET_BITS_PER_WORD);
316 _Base_bitset<_Nw>::_M_do_find_first(size_t __not_found) const
318 for (size_t __i = 0; __i < _Nw; __i++)
320 _WordT __thisword = _M_w[__i];
321 if (__thisword != static_cast<_WordT>(0))
322 return (__i * _GLIBCXX_BITSET_BITS_PER_WORD
323 + __builtin_ctzl(__thisword));
325 // not found, so return an indication of failure.
331 _Base_bitset<_Nw>::_M_do_find_next(size_t __prev, size_t __not_found) const
333 // make bound inclusive
336 // check out of bounds
337 if (__prev >= _Nw * _GLIBCXX_BITSET_BITS_PER_WORD)
341 size_t __i = _S_whichword(__prev);
342 _WordT __thisword = _M_w[__i];
344 // mask off bits below bound
345 __thisword &= (~static_cast<_WordT>(0)) << _S_whichbit(__prev);
347 if (__thisword != static_cast<_WordT>(0))
348 return (__i * _GLIBCXX_BITSET_BITS_PER_WORD
349 + __builtin_ctzl(__thisword));
351 // check subsequent words
353 for (; __i < _Nw; __i++)
355 __thisword = _M_w[__i];
356 if (__thisword != static_cast<_WordT>(0))
357 return (__i * _GLIBCXX_BITSET_BITS_PER_WORD
358 + __builtin_ctzl(__thisword));
360 // not found, so return an indication of failure.
362 } // end _M_do_find_next
365 * Base class, specialization for a single word.
367 * See documentation for bitset.
370 struct _Base_bitset<1>
372 typedef unsigned long _WordT;
375 _GLIBCXX_CONSTEXPR _Base_bitset()
379 #ifdef __GXX_EXPERIMENTAL_CXX0X__
380 constexpr _Base_bitset(unsigned long long __val)
382 _Base_bitset(unsigned long __val)
387 static _GLIBCXX_CONSTEXPR size_t
388 _S_whichword(size_t __pos )
389 { return __pos / _GLIBCXX_BITSET_BITS_PER_WORD; }
391 static _GLIBCXX_CONSTEXPR size_t
392 _S_whichbyte(size_t __pos )
393 { return (__pos % _GLIBCXX_BITSET_BITS_PER_WORD) / __CHAR_BIT__; }
395 static _GLIBCXX_CONSTEXPR size_t
396 _S_whichbit(size_t __pos )
397 { return __pos % _GLIBCXX_BITSET_BITS_PER_WORD; }
399 static _GLIBCXX_CONSTEXPR _WordT
400 _S_maskbit(size_t __pos )
401 { return (static_cast<_WordT>(1)) << _S_whichbit(__pos); }
408 _M_getword(size_t) const
411 #ifdef __GXX_EXPERIMENTAL_CXX0X__
421 _GLIBCXX_CONSTEXPR _WordT
426 _M_do_and(const _Base_bitset<1>& __x)
427 { _M_w &= __x._M_w; }
430 _M_do_or(const _Base_bitset<1>& __x)
431 { _M_w |= __x._M_w; }
434 _M_do_xor(const _Base_bitset<1>& __x)
435 { _M_w ^= __x._M_w; }
438 _M_do_left_shift(size_t __shift)
439 { _M_w <<= __shift; }
442 _M_do_right_shift(size_t __shift)
443 { _M_w >>= __shift; }
451 { _M_w = ~static_cast<_WordT>(0); }
458 _M_is_equal(const _Base_bitset<1>& __x) const
459 { return _M_w == __x._M_w; }
462 _M_are_all_aux() const
463 { return __builtin_popcountl(_M_w); }
467 { return _M_w != 0; }
471 { return __builtin_popcountl(_M_w); }
474 _M_do_to_ulong() const
477 #ifdef __GXX_EXPERIMENTAL_CXX0X__
479 _M_do_to_ullong() const
484 _M_do_find_first(size_t __not_found) const
487 return __builtin_ctzl(_M_w);
492 // find the next "on" bit that follows "prev"
494 _M_do_find_next(size_t __prev, size_t __not_found) const
497 if (__prev >= ((size_t) _GLIBCXX_BITSET_BITS_PER_WORD))
500 _WordT __x = _M_w >> __prev;
502 return __builtin_ctzl(__x) + __prev;
509 * Base class, specialization for no storage (zero-length %bitset).
511 * See documentation for bitset.
514 struct _Base_bitset<0>
516 typedef unsigned long _WordT;
518 _GLIBCXX_CONSTEXPR _Base_bitset()
521 #ifdef __GXX_EXPERIMENTAL_CXX0X__
522 constexpr _Base_bitset(unsigned long long)
524 _Base_bitset(unsigned long)
528 static _GLIBCXX_CONSTEXPR size_t
529 _S_whichword(size_t __pos )
530 { return __pos / _GLIBCXX_BITSET_BITS_PER_WORD; }
532 static _GLIBCXX_CONSTEXPR size_t
533 _S_whichbyte(size_t __pos )
534 { return (__pos % _GLIBCXX_BITSET_BITS_PER_WORD) / __CHAR_BIT__; }
536 static _GLIBCXX_CONSTEXPR size_t
537 _S_whichbit(size_t __pos )
538 { return __pos % _GLIBCXX_BITSET_BITS_PER_WORD; }
540 static _GLIBCXX_CONSTEXPR _WordT
541 _S_maskbit(size_t __pos )
542 { return (static_cast<_WordT>(1)) << _S_whichbit(__pos); }
544 // This would normally give access to the data. The bounds-checking
545 // in the bitset class will prevent the user from getting this far,
546 // but (1) it must still return an lvalue to compile, and (2) the
547 // user might call _Unchecked_set directly, in which case this /needs/
548 // to fail. Let's not penalize zero-length users unless they actually
549 // make an unchecked call; all the memory ugliness is therefore
550 // localized to this single should-never-get-this-far function.
554 __throw_out_of_range(__N("_Base_bitset::_M_getword"));
559 _M_getword(size_t __pos) const
562 _GLIBCXX_CONSTEXPR _WordT
567 _M_do_and(const _Base_bitset<0>&)
571 _M_do_or(const _Base_bitset<0>&)
575 _M_do_xor(const _Base_bitset<0>&)
579 _M_do_left_shift(size_t)
583 _M_do_right_shift(size_t)
598 // Are all empty bitsets equal to each other? Are they equal to
599 // themselves? How to compare a thing which has no state? What is
600 // the sound of one zero-length bitset clapping?
602 _M_is_equal(const _Base_bitset<0>&) const
606 _M_are_all_aux() const
618 _M_do_to_ulong() const
621 #ifdef __GXX_EXPERIMENTAL_CXX0X__
623 _M_do_to_ullong() const
627 // Normally "not found" is the size, but that could also be
628 // misinterpreted as an index in this corner case. Oh well.
630 _M_do_find_first(size_t) const
634 _M_do_find_next(size_t, size_t) const
639 // Helper class to zero out the unused high-order bits in the highest word.
640 template<size_t _Extrabits>
643 typedef unsigned long _WordT;
646 _S_do_sanitize(_WordT& __val)
647 { __val &= ~((~static_cast<_WordT>(0)) << _Extrabits); }
653 typedef unsigned long _WordT;
656 _S_do_sanitize(_WordT) { }
659 #ifdef __GXX_EXPERIMENTAL_CXX0X__
660 template<size_t _Nb, bool = _Nb < _GLIBCXX_BITSET_BITS_PER_ULL>
663 static constexpr unsigned long long
664 _S_do_sanitize_val(unsigned long long __val)
669 struct _Sanitize_val<_Nb, true>
671 static constexpr unsigned long long
672 _S_do_sanitize_val(unsigned long long __val)
673 { return __val & ~((~static_cast<unsigned long long>(0)) << _Nb); }
678 * @brief The %bitset class represents a @e fixed-size sequence of bits.
680 * @ingroup containers
682 * (Note that %bitset does @e not meet the formal requirements of a
683 * <a href="tables.html#65">container</a>. Mainly, it lacks iterators.)
685 * The template argument, @a Nb, may be any non-negative number,
686 * specifying the number of bits (e.g., "0", "12", "1024*1024").
688 * In the general unoptimized case, storage is allocated in word-sized
689 * blocks. Let B be the number of bits in a word, then (Nb+(B-1))/B
690 * words will be used for storage. B - Nb%B bits are unused. (They are
691 * the high-order bits in the highest word.) It is a class invariant
692 * that those unused bits are always zero.
694 * If you think of %bitset as <em>a simple array of bits</em>, be
695 * aware that your mental picture is reversed: a %bitset behaves
696 * the same way as bits in integers do, with the bit at index 0 in
697 * the <em>least significant / right-hand</em> position, and the bit at
698 * index Nb-1 in the <em>most significant / left-hand</em> position.
699 * Thus, unlike other containers, a %bitset's index <em>counts from
700 * right to left</em>, to put it very loosely.
702 * This behavior is preserved when translating to and from strings. For
703 * example, the first line of the following program probably prints
704 * <em>b('a') is 0001100001</em> on a modern ASCII system.
708 * #include <iostream>
711 * using namespace std;
718 * cout << "b('a') is " << b << endl;
722 * string str = s.str();
723 * cout << "index 3 in the string is " << str[3] << " but\n"
724 * << "index 3 in the bitset is " << b[3] << endl;
729 * http://gcc.gnu.org/onlinedocs/libstdc++/manual/bk01pt12ch33s02.html
730 * for a description of extensions.
732 * Most of the actual code isn't contained in %bitset<> itself, but in the
733 * base class _Base_bitset. The base class works with whole words, not with
734 * individual bits. This allows us to specialize _Base_bitset for the
735 * important special case where the %bitset is only a single word.
737 * Extra confusion can result due to the fact that the storage for
738 * _Base_bitset @e is a regular array, and is indexed as such. This is
739 * carefully encapsulated.
743 : private _Base_bitset<_GLIBCXX_BITSET_WORDS(_Nb)>
746 typedef _Base_bitset<_GLIBCXX_BITSET_WORDS(_Nb)> _Base;
747 typedef unsigned long _WordT;
752 typedef _Sanitize<_Nb % _GLIBCXX_BITSET_BITS_PER_WORD> __sanitize_type;
753 __sanitize_type::_S_do_sanitize(this->_M_hiword());
756 #ifdef __GXX_EXPERIMENTAL_CXX0X__
757 template<typename> friend class hash;
762 * This encapsulates the concept of a single bit. An instance of this
763 * class is a proxy for an actual bit; this way the individual bit
764 * operations are done as faster word-size bitwise instructions.
766 * Most users will never need to use this class directly; conversions
767 * to and from bool are automatic and should be transparent. Overloaded
768 * operators help to preserve the illusion.
770 * (On a typical system, this <em>bit %reference</em> is 64
771 * times the size of an actual bit. Ha.)
784 reference(bitset& __b, size_t __pos)
786 _M_wp = &__b._M_getword(__pos);
787 _M_bpos = _Base::_S_whichbit(__pos);
798 *_M_wp |= _Base::_S_maskbit(_M_bpos);
800 *_M_wp &= ~_Base::_S_maskbit(_M_bpos);
804 // For b[i] = b[__j];
806 operator=(const reference& __j)
808 if ((*(__j._M_wp) & _Base::_S_maskbit(__j._M_bpos)))
809 *_M_wp |= _Base::_S_maskbit(_M_bpos);
811 *_M_wp &= ~_Base::_S_maskbit(_M_bpos);
818 { return (*(_M_wp) & _Base::_S_maskbit(_M_bpos)) == 0; }
821 operator bool() const
822 { return (*(_M_wp) & _Base::_S_maskbit(_M_bpos)) != 0; }
828 *_M_wp ^= _Base::_S_maskbit(_M_bpos);
832 friend class reference;
834 // 23.3.5.1 constructors:
835 /// All bits set to zero.
836 _GLIBCXX_CONSTEXPR bitset()
839 /// Initial bits bitwise-copied from a single word (others set to zero).
840 #ifdef __GXX_EXPERIMENTAL_CXX0X__
841 constexpr bitset(unsigned long long __val)
842 : _Base(_Sanitize_val<_Nb>::_S_do_sanitize_val(__val)) { }
844 bitset(unsigned long __val)
846 { _M_do_sanitize(); }
850 * @brief Use a subset of a string.
851 * @param s A string of @a 0 and @a 1 characters.
852 * @param position Index of the first character in @a s to use;
854 * @throw std::out_of_range If @a pos is bigger the size of @a s.
855 * @throw std::invalid_argument If a character appears in the string
856 * which is neither @a 0 nor @a 1.
858 template<class _CharT, class _Traits, class _Alloc>
860 bitset(const std::basic_string<_CharT, _Traits, _Alloc>& __s,
861 size_t __position = 0)
864 if (__position > __s.size())
865 __throw_out_of_range(__N("bitset::bitset initial position "
867 _M_copy_from_string(__s, __position,
868 std::basic_string<_CharT, _Traits, _Alloc>::npos,
869 _CharT('0'), _CharT('1'));
873 * @brief Use a subset of a string.
874 * @param s A string of @a 0 and @a 1 characters.
875 * @param position Index of the first character in @a s to use.
876 * @param n The number of characters to copy.
877 * @throw std::out_of_range If @a pos is bigger the size of @a s.
878 * @throw std::invalid_argument If a character appears in the string
879 * which is neither @a 0 nor @a 1.
881 template<class _CharT, class _Traits, class _Alloc>
882 bitset(const std::basic_string<_CharT, _Traits, _Alloc>& __s,
883 size_t __position, size_t __n)
886 if (__position > __s.size())
887 __throw_out_of_range(__N("bitset::bitset initial position "
889 _M_copy_from_string(__s, __position, __n, _CharT('0'), _CharT('1'));
892 // _GLIBCXX_RESOLVE_LIB_DEFECTS
893 // 396. what are characters zero and one.
894 template<class _CharT, class _Traits, class _Alloc>
895 bitset(const std::basic_string<_CharT, _Traits, _Alloc>& __s,
896 size_t __position, size_t __n,
897 _CharT __zero, _CharT __one = _CharT('1'))
900 if (__position > __s.size())
901 __throw_out_of_range(__N("bitset::bitset initial position "
903 _M_copy_from_string(__s, __position, __n, __zero, __one);
906 #ifdef __GXX_EXPERIMENTAL_CXX0X__
908 * @brief Construct from a character %array.
909 * @param str An %array of characters @a zero and @a one.
910 * @param n The number of characters to use.
911 * @param zero The character corresponding to the value 0.
912 * @param one The character corresponding to the value 1.
913 * @throw std::invalid_argument If a character appears in the string
914 * which is neither @a zero nor @a one.
916 template<typename _CharT>
918 bitset(const _CharT* __str,
919 typename std::basic_string<_CharT>::size_type __n
920 = std::basic_string<_CharT>::npos,
921 _CharT __zero = _CharT('0'), _CharT __one = _CharT('1'))
925 __throw_logic_error(__N("bitset::bitset(const _CharT*, ...)"));
927 if (__n == std::basic_string<_CharT>::npos)
928 __n = std::char_traits<_CharT>::length(__str);
929 _M_copy_from_ptr<_CharT, std::char_traits<_CharT>>(__str, __n, 0,
935 // 23.3.5.2 bitset operations:
938 * @brief Operations on bitsets.
939 * @param rhs A same-sized bitset.
941 * These should be self-explanatory.
944 operator&=(const bitset<_Nb>& __rhs)
946 this->_M_do_and(__rhs);
951 operator|=(const bitset<_Nb>& __rhs)
953 this->_M_do_or(__rhs);
958 operator^=(const bitset<_Nb>& __rhs)
960 this->_M_do_xor(__rhs);
967 * @brief Operations on bitsets.
968 * @param position The number of places to shift.
970 * These should be self-explanatory.
973 operator<<=(size_t __position)
975 if (__builtin_expect(__position < _Nb, 1))
977 this->_M_do_left_shift(__position);
978 this->_M_do_sanitize();
986 operator>>=(size_t __position)
988 if (__builtin_expect(__position < _Nb, 1))
990 this->_M_do_right_shift(__position);
991 this->_M_do_sanitize();
1001 * These versions of single-bit set, reset, flip, and test are
1002 * extensions from the SGI version. They do no range checking.
1003 * @ingroup SGIextensions
1006 _Unchecked_set(size_t __pos)
1008 this->_M_getword(__pos) |= _Base::_S_maskbit(__pos);
1013 _Unchecked_set(size_t __pos, int __val)
1016 this->_M_getword(__pos) |= _Base::_S_maskbit(__pos);
1018 this->_M_getword(__pos) &= ~_Base::_S_maskbit(__pos);
1023 _Unchecked_reset(size_t __pos)
1025 this->_M_getword(__pos) &= ~_Base::_S_maskbit(__pos);
1030 _Unchecked_flip(size_t __pos)
1032 this->_M_getword(__pos) ^= _Base::_S_maskbit(__pos);
1037 _Unchecked_test(size_t __pos) const
1038 { return ((this->_M_getword(__pos) & _Base::_S_maskbit(__pos))
1039 != static_cast<_WordT>(0)); }
1042 // Set, reset, and flip.
1044 * @brief Sets every bit to true.
1050 this->_M_do_sanitize();
1055 * @brief Sets a given bit to a particular value.
1056 * @param position The index of the bit.
1057 * @param val Either true or false, defaults to true.
1058 * @throw std::out_of_range If @a pos is bigger the size of the %set.
1061 set(size_t __position, bool __val = true)
1063 if (__position >= _Nb)
1064 __throw_out_of_range(__N("bitset::set"));
1065 return _Unchecked_set(__position, __val);
1069 * @brief Sets every bit to false.
1074 this->_M_do_reset();
1079 * @brief Sets a given bit to false.
1080 * @param position The index of the bit.
1081 * @throw std::out_of_range If @a pos is bigger the size of the %set.
1083 * Same as writing @c set(pos,false).
1086 reset(size_t __position)
1088 if (__position >= _Nb)
1089 __throw_out_of_range(__N("bitset::reset"));
1090 return _Unchecked_reset(__position);
1094 * @brief Toggles every bit to its opposite value.
1100 this->_M_do_sanitize();
1105 * @brief Toggles a given bit to its opposite value.
1106 * @param position The index of the bit.
1107 * @throw std::out_of_range If @a pos is bigger the size of the %set.
1110 flip(size_t __position)
1112 if (__position >= _Nb)
1113 __throw_out_of_range(__N("bitset::flip"));
1114 return _Unchecked_flip(__position);
1117 /// See the no-argument flip().
1120 { return bitset<_Nb>(*this).flip(); }
1124 * @brief Array-indexing support.
1125 * @param position Index into the %bitset.
1126 * @return A bool for a <em>const %bitset</em>. For non-const
1127 * bitsets, an instance of the reference proxy class.
1128 * @note These operators do no range checking and throw no exceptions,
1129 * as required by DR 11 to the standard.
1131 * _GLIBCXX_RESOLVE_LIB_DEFECTS Note that this implementation already
1132 * resolves DR 11 (items 1 and 2), but does not do the range-checking
1133 * required by that DR's resolution. -pme
1134 * The DR has since been changed: range-checking is a precondition
1135 * (users' responsibility), and these functions must not throw. -pme
1138 operator[](size_t __position)
1139 { return reference(*this, __position); }
1142 operator[](size_t __position) const
1143 { return _Unchecked_test(__position); }
1147 * @brief Returns a numerical interpretation of the %bitset.
1148 * @return The integral equivalent of the bits.
1149 * @throw std::overflow_error If there are too many bits to be
1150 * represented in an @c unsigned @c long.
1154 { return this->_M_do_to_ulong(); }
1156 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1159 { return this->_M_do_to_ullong(); }
1163 * @brief Returns a character interpretation of the %bitset.
1164 * @return The string equivalent of the bits.
1166 * Note the ordering of the bits: decreasing character positions
1167 * correspond to increasing bit positions (see the main class notes for
1170 template<class _CharT, class _Traits, class _Alloc>
1171 std::basic_string<_CharT, _Traits, _Alloc>
1174 std::basic_string<_CharT, _Traits, _Alloc> __result;
1175 _M_copy_to_string(__result, _CharT('0'), _CharT('1'));
1179 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1180 // 396. what are characters zero and one.
1181 template<class _CharT, class _Traits, class _Alloc>
1182 std::basic_string<_CharT, _Traits, _Alloc>
1183 to_string(_CharT __zero, _CharT __one = _CharT('1')) const
1185 std::basic_string<_CharT, _Traits, _Alloc> __result;
1186 _M_copy_to_string(__result, __zero, __one);
1190 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1191 // 434. bitset::to_string() hard to use.
1192 template<class _CharT, class _Traits>
1193 std::basic_string<_CharT, _Traits, std::allocator<_CharT> >
1195 { return to_string<_CharT, _Traits, std::allocator<_CharT> >(); }
1197 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1198 // 853. to_string needs updating with zero and one.
1199 template<class _CharT, class _Traits>
1200 std::basic_string<_CharT, _Traits, std::allocator<_CharT> >
1201 to_string(_CharT __zero, _CharT __one = _CharT('1')) const
1202 { return to_string<_CharT, _Traits,
1203 std::allocator<_CharT> >(__zero, __one); }
1205 template<class _CharT>
1206 std::basic_string<_CharT, std::char_traits<_CharT>,
1207 std::allocator<_CharT> >
1210 return to_string<_CharT, std::char_traits<_CharT>,
1211 std::allocator<_CharT> >();
1214 template<class _CharT>
1215 std::basic_string<_CharT, std::char_traits<_CharT>,
1216 std::allocator<_CharT> >
1217 to_string(_CharT __zero, _CharT __one = _CharT('1')) const
1219 return to_string<_CharT, std::char_traits<_CharT>,
1220 std::allocator<_CharT> >(__zero, __one);
1223 std::basic_string<char, std::char_traits<char>, std::allocator<char> >
1226 return to_string<char, std::char_traits<char>,
1227 std::allocator<char> >();
1230 std::basic_string<char, std::char_traits<char>, std::allocator<char> >
1231 to_string(char __zero, char __one = '1') const
1233 return to_string<char, std::char_traits<char>,
1234 std::allocator<char> >(__zero, __one);
1237 // Helper functions for string operations.
1238 template<class _CharT, class _Traits>
1240 _M_copy_from_ptr(const _CharT*, size_t, size_t, size_t,
1243 template<class _CharT, class _Traits, class _Alloc>
1245 _M_copy_from_string(const std::basic_string<_CharT,
1246 _Traits, _Alloc>& __s, size_t __pos, size_t __n,
1247 _CharT __zero, _CharT __one)
1248 { _M_copy_from_ptr<_CharT, _Traits>(__s.data(), __s.size(), __pos, __n,
1251 template<class _CharT, class _Traits, class _Alloc>
1253 _M_copy_to_string(std::basic_string<_CharT, _Traits, _Alloc>&,
1254 _CharT, _CharT) const;
1256 // NB: Backward compat.
1257 template<class _CharT, class _Traits, class _Alloc>
1259 _M_copy_from_string(const std::basic_string<_CharT,
1260 _Traits, _Alloc>& __s, size_t __pos, size_t __n)
1261 { _M_copy_from_string(__s, __pos, __n, _CharT('0'), _CharT('1')); }
1263 template<class _CharT, class _Traits, class _Alloc>
1265 _M_copy_to_string(std::basic_string<_CharT, _Traits,_Alloc>& __s) const
1266 { _M_copy_to_string(__s, _CharT('0'), _CharT('1')); }
1268 /// Returns the number of bits which are set.
1271 { return this->_M_do_count(); }
1273 /// Returns the total number of bits.
1274 _GLIBCXX_CONSTEXPR size_t
1279 /// These comparisons for equality/inequality are, well, @e bitwise.
1281 operator==(const bitset<_Nb>& __rhs) const
1282 { return this->_M_is_equal(__rhs); }
1285 operator!=(const bitset<_Nb>& __rhs) const
1286 { return !this->_M_is_equal(__rhs); }
1290 * @brief Tests the value of a bit.
1291 * @param position The index of a bit.
1292 * @return The value at @a pos.
1293 * @throw std::out_of_range If @a pos is bigger the size of the %set.
1296 test(size_t __position) const
1298 if (__position >= _Nb)
1299 __throw_out_of_range(__N("bitset::test"));
1300 return _Unchecked_test(__position);
1303 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1304 // DR 693. std::bitset::all() missing.
1306 * @brief Tests whether all the bits are on.
1307 * @return True if all the bits are set.
1311 { return this->_M_are_all_aux() == _Nb; }
1314 * @brief Tests whether any of the bits are on.
1315 * @return True if at least one bit is set.
1319 { return this->_M_is_any(); }
1322 * @brief Tests whether any of the bits are on.
1323 * @return True if none of the bits are set.
1327 { return !this->_M_is_any(); }
1330 /// Self-explanatory.
1332 operator<<(size_t __position) const
1333 { return bitset<_Nb>(*this) <<= __position; }
1336 operator>>(size_t __position) const
1337 { return bitset<_Nb>(*this) >>= __position; }
1341 * @brief Finds the index of the first "on" bit.
1342 * @return The index of the first bit set, or size() if not found.
1343 * @ingroup SGIextensions
1348 { return this->_M_do_find_first(_Nb); }
1351 * @brief Finds the index of the next "on" bit after prev.
1352 * @return The index of the next bit set, or size() if not found.
1353 * @param prev Where to start searching.
1354 * @ingroup SGIextensions
1358 _Find_next(size_t __prev ) const
1359 { return this->_M_do_find_next(__prev, _Nb); }
1362 // Definitions of non-inline member functions.
1363 template<size_t _Nb>
1364 template<class _CharT, class _Traits>
1367 _M_copy_from_ptr(const _CharT* __s, size_t __len,
1368 size_t __pos, size_t __n, _CharT __zero, _CharT __one)
1371 const size_t __nbits = std::min(_Nb, std::min(__n, __len - __pos));
1372 for (size_t __i = __nbits; __i > 0; --__i)
1374 const _CharT __c = __s[__pos + __nbits - __i];
1375 if (_Traits::eq(__c, __zero))
1377 else if (_Traits::eq(__c, __one))
1378 _Unchecked_set(__i - 1);
1380 __throw_invalid_argument(__N("bitset::_M_copy_from_ptr"));
1384 template<size_t _Nb>
1385 template<class _CharT, class _Traits, class _Alloc>
1388 _M_copy_to_string(std::basic_string<_CharT, _Traits, _Alloc>& __s,
1389 _CharT __zero, _CharT __one) const
1391 __s.assign(_Nb, __zero);
1392 for (size_t __i = _Nb; __i > 0; --__i)
1393 if (_Unchecked_test(__i - 1))
1394 _Traits::assign(__s[_Nb - __i], __one);
1397 // 23.3.5.3 bitset operations:
1400 * @brief Global bitwise operations on bitsets.
1401 * @param x A bitset.
1402 * @param y A bitset of the same size as @a x.
1403 * @return A new bitset.
1405 * These should be self-explanatory.
1407 template<size_t _Nb>
1409 operator&(const bitset<_Nb>& __x, const bitset<_Nb>& __y)
1411 bitset<_Nb> __result(__x);
1416 template<size_t _Nb>
1418 operator|(const bitset<_Nb>& __x, const bitset<_Nb>& __y)
1420 bitset<_Nb> __result(__x);
1425 template <size_t _Nb>
1427 operator^(const bitset<_Nb>& __x, const bitset<_Nb>& __y)
1429 bitset<_Nb> __result(__x);
1437 * @brief Global I/O operators for bitsets.
1439 * Direct I/O between streams and bitsets is supported. Output is
1440 * straightforward. Input will skip whitespace, only accept @a 0 and @a 1
1441 * characters, and will only extract as many digits as the %bitset will
1444 template<class _CharT, class _Traits, size_t _Nb>
1445 std::basic_istream<_CharT, _Traits>&
1446 operator>>(std::basic_istream<_CharT, _Traits>& __is, bitset<_Nb>& __x)
1448 typedef typename _Traits::char_type char_type;
1449 typedef std::basic_istream<_CharT, _Traits> __istream_type;
1450 typedef typename __istream_type::ios_base __ios_base;
1452 std::basic_string<_CharT, _Traits> __tmp;
1455 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1456 // 303. Bitset input operator underspecified
1457 const char_type __zero = __is.widen('0');
1458 const char_type __one = __is.widen('1');
1460 typename __ios_base::iostate __state = __ios_base::goodbit;
1461 typename __istream_type::sentry __sentry(__is);
1466 for (size_t __i = _Nb; __i > 0; --__i)
1468 static typename _Traits::int_type __eof = _Traits::eof();
1470 typename _Traits::int_type __c1 = __is.rdbuf()->sbumpc();
1471 if (_Traits::eq_int_type(__c1, __eof))
1473 __state |= __ios_base::eofbit;
1478 const char_type __c2 = _Traits::to_char_type(__c1);
1479 if (_Traits::eq(__c2, __zero))
1480 __tmp.push_back(__zero);
1481 else if (_Traits::eq(__c2, __one))
1482 __tmp.push_back(__one);
1484 eq_int_type(__is.rdbuf()->sputbackc(__c2),
1487 __state |= __ios_base::failbit;
1493 __catch(__cxxabiv1::__forced_unwind&)
1495 __is._M_setstate(__ios_base::badbit);
1496 __throw_exception_again;
1499 { __is._M_setstate(__ios_base::badbit); }
1502 if (__tmp.empty() && _Nb)
1503 __state |= __ios_base::failbit;
1505 __x._M_copy_from_string(__tmp, static_cast<size_t>(0), _Nb,
1508 __is.setstate(__state);
1512 template <class _CharT, class _Traits, size_t _Nb>
1513 std::basic_ostream<_CharT, _Traits>&
1514 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1515 const bitset<_Nb>& __x)
1517 std::basic_string<_CharT, _Traits> __tmp;
1519 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1520 // 396. what are characters zero and one.
1521 const ctype<_CharT>& __ct = use_facet<ctype<_CharT> >(__os.getloc());
1522 __x._M_copy_to_string(__tmp, __ct.widen('0'), __ct.widen('1'));
1523 return __os << __tmp;
1527 _GLIBCXX_END_NAMESPACE_CONTAINER
1530 #undef _GLIBCXX_BITSET_WORDS
1531 #undef _GLIBCXX_BITSET_BITS_PER_WORD
1532 #undef _GLIBCXX_BITSET_BITS_PER_ULL
1534 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1536 #include <bits/functional_hash.h>
1538 namespace std _GLIBCXX_VISIBILITY(default)
1540 _GLIBCXX_BEGIN_NAMESPACE_VERSION
1543 /// std::hash specialization for bitset.
1544 template<size_t _Nb>
1545 struct hash<_GLIBCXX_STD_C::bitset<_Nb>>
1546 : public __hash_base<size_t, _GLIBCXX_STD_C::bitset<_Nb>>
1549 operator()(const _GLIBCXX_STD_C::bitset<_Nb>& __b) const
1551 const size_t __clength = (_Nb + __CHAR_BIT__ - 1) / __CHAR_BIT__;
1552 return std::_Hash_impl::hash(__b._M_getdata(), __clength);
1557 struct hash<_GLIBCXX_STD_C::bitset<0>>
1558 : public __hash_base<size_t, _GLIBCXX_STD_C::bitset<0>>
1561 operator()(const _GLIBCXX_STD_C::bitset<0>&) const
1565 _GLIBCXX_END_NAMESPACE_VERSION
1568 #endif // __GXX_EXPERIMENTAL_CXX0X__
1570 #ifdef _GLIBCXX_DEBUG
1571 # include <debug/bitset>
1574 #ifdef _GLIBCXX_PROFILE
1575 # include <profile/bitset>
1578 #endif /* _GLIBCXX_BITSET */