1 // <bitset> -*- C++ -*-
3 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
4 // Free Software Foundation, Inc.
6 // This file is part of the GNU ISO C++ Library. This library is free
7 // software; you can redistribute it and/or modify it under the
8 // terms of the GNU General Public License as published by the
9 // Free Software Foundation; either version 3, or (at your option)
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
17 // Under Section 7 of GPL version 3, you are granted additional
18 // permissions described in the GCC Runtime Library Exception, version
19 // 3.1, as published by the Free Software Foundation.
21 // You should have received a copy of the GNU General Public License and
22 // a copy of the GCC Runtime Library Exception along with this program;
23 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24 // <http://www.gnu.org/licenses/>.
28 * Silicon Graphics Computer Systems, Inc.
30 * Permission to use, copy, modify, distribute and sell this software
31 * and its documentation for any purpose is hereby granted without fee,
32 * provided that the above copyright notice appear in all copies and
33 * that both that copyright notice and this permission notice appear
34 * in supporting documentation. Silicon Graphics makes no
35 * representations about the suitability of this software for any
36 * purpose. It is provided "as is" without express or implied warranty.
39 /** @file include/bitset
40 * This is a Standard C++ Library header.
43 #ifndef _GLIBCXX_BITSET
44 #define _GLIBCXX_BITSET 1
46 #pragma GCC system_header
49 #include <bits/functexcept.h> // For invalid_argument, out_of_range,
52 #include <cxxabi-forced.h>
54 #define _GLIBCXX_BITSET_BITS_PER_WORD (__CHAR_BIT__ * sizeof(unsigned long))
55 #define _GLIBCXX_BITSET_WORDS(__n) \
56 ((__n) / _GLIBCXX_BITSET_BITS_PER_WORD + \
57 ((__n) % _GLIBCXX_BITSET_BITS_PER_WORD == 0 ? 0 : 1))
59 _GLIBCXX_BEGIN_NESTED_NAMESPACE(std, _GLIBCXX_STD_D)
62 * Base class, general case. It is a class invariant that _Nw will be
65 * See documentation for bitset.
70 typedef unsigned long _WordT;
72 /// 0 is the least significant word.
78 #ifdef __GXX_EXPERIMENTAL_CXX0X__
79 _Base_bitset(unsigned long long __val)
81 _Base_bitset(unsigned long __val)
86 #ifdef __GXX_EXPERIMENTAL_CXX0X__
87 if (sizeof(unsigned long long) > sizeof(unsigned long))
88 _M_w[1] = __val >> _GLIBCXX_BITSET_BITS_PER_WORD;
93 _S_whichword(size_t __pos )
94 { return __pos / _GLIBCXX_BITSET_BITS_PER_WORD; }
97 _S_whichbyte(size_t __pos )
98 { return (__pos % _GLIBCXX_BITSET_BITS_PER_WORD) / __CHAR_BIT__; }
101 _S_whichbit(size_t __pos )
102 { return __pos % _GLIBCXX_BITSET_BITS_PER_WORD; }
105 _S_maskbit(size_t __pos )
106 { return (static_cast<_WordT>(1)) << _S_whichbit(__pos); }
109 _M_getword(size_t __pos)
110 { return _M_w[_S_whichword(__pos)]; }
113 _M_getword(size_t __pos) const
114 { return _M_w[_S_whichword(__pos)]; }
116 #ifdef __GXX_EXPERIMENTAL_CXX0X__
124 { return _M_w[_Nw - 1]; }
128 { return _M_w[_Nw - 1]; }
131 _M_do_and(const _Base_bitset<_Nw>& __x)
133 for (size_t __i = 0; __i < _Nw; __i++)
134 _M_w[__i] &= __x._M_w[__i];
138 _M_do_or(const _Base_bitset<_Nw>& __x)
140 for (size_t __i = 0; __i < _Nw; __i++)
141 _M_w[__i] |= __x._M_w[__i];
145 _M_do_xor(const _Base_bitset<_Nw>& __x)
147 for (size_t __i = 0; __i < _Nw; __i++)
148 _M_w[__i] ^= __x._M_w[__i];
152 _M_do_left_shift(size_t __shift);
155 _M_do_right_shift(size_t __shift);
160 for (size_t __i = 0; __i < _Nw; __i++)
161 _M_w[__i] = ~_M_w[__i];
167 for (size_t __i = 0; __i < _Nw; __i++)
168 _M_w[__i] = ~static_cast<_WordT>(0);
173 { __builtin_memset(_M_w, 0, _Nw * sizeof(_WordT)); }
176 _M_is_equal(const _Base_bitset<_Nw>& __x) const
178 for (size_t __i = 0; __i < _Nw; ++__i)
179 if (_M_w[__i] != __x._M_w[__i])
185 _M_are_all_aux() const
187 for (size_t __i = 0; __i < _Nw - 1; __i++)
188 if (_M_w[__i] != ~static_cast<_WordT>(0))
190 return ((_Nw - 1) * _GLIBCXX_BITSET_BITS_PER_WORD
191 + __builtin_popcountl(_M_hiword()));
197 for (size_t __i = 0; __i < _Nw; __i++)
198 if (_M_w[__i] != static_cast<_WordT>(0))
207 for (size_t __i = 0; __i < _Nw; __i++)
208 __result += __builtin_popcountl(_M_w[__i]);
213 _M_do_to_ulong() const;
215 #ifdef __GXX_EXPERIMENTAL_CXX0X__
217 _M_do_to_ullong() const;
220 // find first "on" bit
222 _M_do_find_first(size_t __not_found) const;
224 // find the next "on" bit that follows "prev"
226 _M_do_find_next(size_t __prev, size_t __not_found) const;
229 // Definitions of non-inline functions from _Base_bitset.
232 _Base_bitset<_Nw>::_M_do_left_shift(size_t __shift)
234 if (__builtin_expect(__shift != 0, 1))
236 const size_t __wshift = __shift / _GLIBCXX_BITSET_BITS_PER_WORD;
237 const size_t __offset = __shift % _GLIBCXX_BITSET_BITS_PER_WORD;
240 for (size_t __n = _Nw - 1; __n >= __wshift; --__n)
241 _M_w[__n] = _M_w[__n - __wshift];
244 const size_t __sub_offset = (_GLIBCXX_BITSET_BITS_PER_WORD
246 for (size_t __n = _Nw - 1; __n > __wshift; --__n)
247 _M_w[__n] = ((_M_w[__n - __wshift] << __offset)
248 | (_M_w[__n - __wshift - 1] >> __sub_offset));
249 _M_w[__wshift] = _M_w[0] << __offset;
252 std::fill(_M_w + 0, _M_w + __wshift, static_cast<_WordT>(0));
258 _Base_bitset<_Nw>::_M_do_right_shift(size_t __shift)
260 if (__builtin_expect(__shift != 0, 1))
262 const size_t __wshift = __shift / _GLIBCXX_BITSET_BITS_PER_WORD;
263 const size_t __offset = __shift % _GLIBCXX_BITSET_BITS_PER_WORD;
264 const size_t __limit = _Nw - __wshift - 1;
267 for (size_t __n = 0; __n <= __limit; ++__n)
268 _M_w[__n] = _M_w[__n + __wshift];
271 const size_t __sub_offset = (_GLIBCXX_BITSET_BITS_PER_WORD
273 for (size_t __n = 0; __n < __limit; ++__n)
274 _M_w[__n] = ((_M_w[__n + __wshift] >> __offset)
275 | (_M_w[__n + __wshift + 1] << __sub_offset));
276 _M_w[__limit] = _M_w[_Nw-1] >> __offset;
279 std::fill(_M_w + __limit + 1, _M_w + _Nw, static_cast<_WordT>(0));
285 _Base_bitset<_Nw>::_M_do_to_ulong() const
287 for (size_t __i = 1; __i < _Nw; ++__i)
289 __throw_overflow_error(__N("_Base_bitset::_M_do_to_ulong"));
293 #ifdef __GXX_EXPERIMENTAL_CXX0X__
296 _Base_bitset<_Nw>::_M_do_to_ullong() const
298 const bool __dw = sizeof(unsigned long long) > sizeof(unsigned long);
299 for (size_t __i = 1 + __dw; __i < _Nw; ++__i)
301 __throw_overflow_error(__N("_Base_bitset::_M_do_to_ullong"));
304 return _M_w[0] + (static_cast<unsigned long long>(_M_w[1])
305 << _GLIBCXX_BITSET_BITS_PER_WORD);
312 _Base_bitset<_Nw>::_M_do_find_first(size_t __not_found) const
314 for (size_t __i = 0; __i < _Nw; __i++)
316 _WordT __thisword = _M_w[__i];
317 if (__thisword != static_cast<_WordT>(0))
318 return (__i * _GLIBCXX_BITSET_BITS_PER_WORD
319 + __builtin_ctzl(__thisword));
321 // not found, so return an indication of failure.
327 _Base_bitset<_Nw>::_M_do_find_next(size_t __prev, size_t __not_found) const
329 // make bound inclusive
332 // check out of bounds
333 if (__prev >= _Nw * _GLIBCXX_BITSET_BITS_PER_WORD)
337 size_t __i = _S_whichword(__prev);
338 _WordT __thisword = _M_w[__i];
340 // mask off bits below bound
341 __thisword &= (~static_cast<_WordT>(0)) << _S_whichbit(__prev);
343 if (__thisword != static_cast<_WordT>(0))
344 return (__i * _GLIBCXX_BITSET_BITS_PER_WORD
345 + __builtin_ctzl(__thisword));
347 // check subsequent words
349 for (; __i < _Nw; __i++)
351 __thisword = _M_w[__i];
352 if (__thisword != static_cast<_WordT>(0))
353 return (__i * _GLIBCXX_BITSET_BITS_PER_WORD
354 + __builtin_ctzl(__thisword));
356 // not found, so return an indication of failure.
358 } // end _M_do_find_next
361 * Base class, specialization for a single word.
363 * See documentation for bitset.
366 struct _Base_bitset<1>
368 typedef unsigned long _WordT;
375 #ifdef __GXX_EXPERIMENTAL_CXX0X__
376 _Base_bitset(unsigned long long __val)
378 _Base_bitset(unsigned long __val)
384 _S_whichword(size_t __pos )
385 { return __pos / _GLIBCXX_BITSET_BITS_PER_WORD; }
388 _S_whichbyte(size_t __pos )
389 { return (__pos % _GLIBCXX_BITSET_BITS_PER_WORD) / __CHAR_BIT__; }
392 _S_whichbit(size_t __pos )
393 { return __pos % _GLIBCXX_BITSET_BITS_PER_WORD; }
396 _S_maskbit(size_t __pos )
397 { return (static_cast<_WordT>(1)) << _S_whichbit(__pos); }
404 _M_getword(size_t) const
407 #ifdef __GXX_EXPERIMENTAL_CXX0X__
422 _M_do_and(const _Base_bitset<1>& __x)
423 { _M_w &= __x._M_w; }
426 _M_do_or(const _Base_bitset<1>& __x)
427 { _M_w |= __x._M_w; }
430 _M_do_xor(const _Base_bitset<1>& __x)
431 { _M_w ^= __x._M_w; }
434 _M_do_left_shift(size_t __shift)
435 { _M_w <<= __shift; }
438 _M_do_right_shift(size_t __shift)
439 { _M_w >>= __shift; }
447 { _M_w = ~static_cast<_WordT>(0); }
454 _M_is_equal(const _Base_bitset<1>& __x) const
455 { return _M_w == __x._M_w; }
458 _M_are_all_aux() const
459 { return __builtin_popcountl(_M_w); }
463 { return _M_w != 0; }
467 { return __builtin_popcountl(_M_w); }
470 _M_do_to_ulong() const
473 #ifdef __GXX_EXPERIMENTAL_CXX0X__
475 _M_do_to_ullong() const
480 _M_do_find_first(size_t __not_found) const
483 return __builtin_ctzl(_M_w);
488 // find the next "on" bit that follows "prev"
490 _M_do_find_next(size_t __prev, size_t __not_found) const
493 if (__prev >= ((size_t) _GLIBCXX_BITSET_BITS_PER_WORD))
496 _WordT __x = _M_w >> __prev;
498 return __builtin_ctzl(__x) + __prev;
505 * Base class, specialization for no storage (zero-length %bitset).
507 * See documentation for bitset.
510 struct _Base_bitset<0>
512 typedef unsigned long _WordT;
517 #ifdef __GXX_EXPERIMENTAL_CXX0X__
518 _Base_bitset(unsigned long long)
520 _Base_bitset(unsigned long)
525 _S_whichword(size_t __pos )
526 { return __pos / _GLIBCXX_BITSET_BITS_PER_WORD; }
529 _S_whichbyte(size_t __pos )
530 { return (__pos % _GLIBCXX_BITSET_BITS_PER_WORD) / __CHAR_BIT__; }
533 _S_whichbit(size_t __pos )
534 { return __pos % _GLIBCXX_BITSET_BITS_PER_WORD; }
537 _S_maskbit(size_t __pos )
538 { return (static_cast<_WordT>(1)) << _S_whichbit(__pos); }
540 // This would normally give access to the data. The bounds-checking
541 // in the bitset class will prevent the user from getting this far,
542 // but (1) it must still return an lvalue to compile, and (2) the
543 // user might call _Unchecked_set directly, in which case this /needs/
544 // to fail. Let's not penalize zero-length users unless they actually
545 // make an unchecked call; all the memory ugliness is therefore
546 // localized to this single should-never-get-this-far function.
548 _M_getword(size_t) const
550 __throw_out_of_range(__N("_Base_bitset::_M_getword"));
559 _M_do_and(const _Base_bitset<0>&)
563 _M_do_or(const _Base_bitset<0>&)
567 _M_do_xor(const _Base_bitset<0>&)
571 _M_do_left_shift(size_t)
575 _M_do_right_shift(size_t)
590 // Are all empty bitsets equal to each other? Are they equal to
591 // themselves? How to compare a thing which has no state? What is
592 // the sound of one zero-length bitset clapping?
594 _M_is_equal(const _Base_bitset<0>&) const
598 _M_are_all_aux() const
610 _M_do_to_ulong() const
613 #ifdef __GXX_EXPERIMENTAL_CXX0X__
615 _M_do_to_ullong() const
619 // Normally "not found" is the size, but that could also be
620 // misinterpreted as an index in this corner case. Oh well.
622 _M_do_find_first(size_t) const
626 _M_do_find_next(size_t, size_t) const
631 // Helper class to zero out the unused high-order bits in the highest word.
632 template<size_t _Extrabits>
635 static void _S_do_sanitize(unsigned long& __val)
636 { __val &= ~((~static_cast<unsigned long>(0)) << _Extrabits); }
641 { static void _S_do_sanitize(unsigned long) {} };
644 * @brief The %bitset class represents a @e fixed-size sequence of bits.
646 * @ingroup containers
648 * (Note that %bitset does @e not meet the formal requirements of a
649 * <a href="tables.html#65">container</a>. Mainly, it lacks iterators.)
651 * The template argument, @a Nb, may be any non-negative number,
652 * specifying the number of bits (e.g., "0", "12", "1024*1024").
654 * In the general unoptimized case, storage is allocated in word-sized
655 * blocks. Let B be the number of bits in a word, then (Nb+(B-1))/B
656 * words will be used for storage. B - Nb%B bits are unused. (They are
657 * the high-order bits in the highest word.) It is a class invariant
658 * that those unused bits are always zero.
660 * If you think of %bitset as <em>a simple array of bits</em>, be
661 * aware that your mental picture is reversed: a %bitset behaves
662 * the same way as bits in integers do, with the bit at index 0 in
663 * the <em>least significant / right-hand</em> position, and the bit at
664 * index Nb-1 in the <em>most significant / left-hand</em> position.
665 * Thus, unlike other containers, a %bitset's index <em>counts from
666 * right to left</em>, to put it very loosely.
668 * This behavior is preserved when translating to and from strings. For
669 * example, the first line of the following program probably prints
670 * <em>b('a') is 0001100001</em> on a modern ASCII system.
674 * #include <iostream>
677 * using namespace std;
684 * cout << "b('a') is " << b << endl;
688 * string str = s.str();
689 * cout << "index 3 in the string is " << str[3] << " but\n"
690 * << "index 3 in the bitset is " << b[3] << endl;
695 * http://gcc.gnu.org/onlinedocs/libstdc++/manual/bk01pt12ch33s02.html
696 * for a description of extensions.
698 * Most of the actual code isn't contained in %bitset<> itself, but in the
699 * base class _Base_bitset. The base class works with whole words, not with
700 * individual bits. This allows us to specialize _Base_bitset for the
701 * important special case where the %bitset is only a single word.
703 * Extra confusion can result due to the fact that the storage for
704 * _Base_bitset @e is a regular array, and is indexed as such. This is
705 * carefully encapsulated.
709 : private _Base_bitset<_GLIBCXX_BITSET_WORDS(_Nb)>
712 typedef _Base_bitset<_GLIBCXX_BITSET_WORDS(_Nb)> _Base;
713 typedef unsigned long _WordT;
718 _Sanitize<_Nb % _GLIBCXX_BITSET_BITS_PER_WORD>::
719 _S_do_sanitize(this->_M_hiword());
722 #ifdef __GXX_EXPERIMENTAL_CXX0X__
723 template<typename> friend class hash;
728 * This encapsulates the concept of a single bit. An instance of this
729 * class is a proxy for an actual bit; this way the individual bit
730 * operations are done as faster word-size bitwise instructions.
732 * Most users will never need to use this class directly; conversions
733 * to and from bool are automatic and should be transparent. Overloaded
734 * operators help to preserve the illusion.
736 * (On a typical system, this <em>bit %reference</em> is 64
737 * times the size of an actual bit. Ha.)
750 reference(bitset& __b, size_t __pos)
752 _M_wp = &__b._M_getword(__pos);
753 _M_bpos = _Base::_S_whichbit(__pos);
764 *_M_wp |= _Base::_S_maskbit(_M_bpos);
766 *_M_wp &= ~_Base::_S_maskbit(_M_bpos);
770 // For b[i] = b[__j];
772 operator=(const reference& __j)
774 if ((*(__j._M_wp) & _Base::_S_maskbit(__j._M_bpos)))
775 *_M_wp |= _Base::_S_maskbit(_M_bpos);
777 *_M_wp &= ~_Base::_S_maskbit(_M_bpos);
784 { return (*(_M_wp) & _Base::_S_maskbit(_M_bpos)) == 0; }
787 operator bool() const
788 { return (*(_M_wp) & _Base::_S_maskbit(_M_bpos)) != 0; }
794 *_M_wp ^= _Base::_S_maskbit(_M_bpos);
798 friend class reference;
800 // 23.3.5.1 constructors:
801 /// All bits set to zero.
805 /// Initial bits bitwise-copied from a single word (others set to zero).
806 #ifdef __GXX_EXPERIMENTAL_CXX0X__
807 bitset(unsigned long long __val)
809 bitset(unsigned long __val)
812 { _M_do_sanitize(); }
815 * @brief Use a subset of a string.
816 * @param s A string of @a 0 and @a 1 characters.
817 * @param position Index of the first character in @a s to use;
819 * @throw std::out_of_range If @a pos is bigger the size of @a s.
820 * @throw std::invalid_argument If a character appears in the string
821 * which is neither @a 0 nor @a 1.
823 template<class _CharT, class _Traits, class _Alloc>
825 bitset(const std::basic_string<_CharT, _Traits, _Alloc>& __s,
826 size_t __position = 0)
829 if (__position > __s.size())
830 __throw_out_of_range(__N("bitset::bitset initial position "
832 _M_copy_from_string(__s, __position,
833 std::basic_string<_CharT, _Traits, _Alloc>::npos,
834 _CharT('0'), _CharT('1'));
838 * @brief Use a subset of a string.
839 * @param s A string of @a 0 and @a 1 characters.
840 * @param position Index of the first character in @a s to use.
841 * @param n The number of characters to copy.
842 * @throw std::out_of_range If @a pos is bigger the size of @a s.
843 * @throw std::invalid_argument If a character appears in the string
844 * which is neither @a 0 nor @a 1.
846 template<class _CharT, class _Traits, class _Alloc>
847 bitset(const std::basic_string<_CharT, _Traits, _Alloc>& __s,
848 size_t __position, size_t __n)
851 if (__position > __s.size())
852 __throw_out_of_range(__N("bitset::bitset initial position "
854 _M_copy_from_string(__s, __position, __n, _CharT('0'), _CharT('1'));
857 // _GLIBCXX_RESOLVE_LIB_DEFECTS
858 // 396. what are characters zero and one.
859 template<class _CharT, class _Traits, class _Alloc>
860 bitset(const std::basic_string<_CharT, _Traits, _Alloc>& __s,
861 size_t __position, size_t __n,
862 _CharT __zero, _CharT __one = _CharT('1'))
865 if (__position > __s.size())
866 __throw_out_of_range(__N("bitset::bitset initial position "
868 _M_copy_from_string(__s, __position, __n, __zero, __one);
871 #ifdef __GXX_EXPERIMENTAL_CXX0X__
873 * @brief Construct from a character %array.
874 * @param str An %array of characters @a zero and @a one.
875 * @param n The number of characters to use.
876 * @param zero The character corresponding to the value 0.
877 * @param one The character corresponding to the value 1.
878 * @throw std::invalid_argument If a character appears in the string
879 * which is neither @a zero nor @a one.
881 template<typename _CharT>
883 bitset(const _CharT* __str,
884 typename std::basic_string<_CharT>::size_type __n
885 = std::basic_string<_CharT>::npos,
886 _CharT __zero = _CharT('0'), _CharT __one = _CharT('1'))
890 __throw_logic_error(__N("bitset::bitset(const _CharT*, ...)"));
892 if (__n == std::basic_string<_CharT>::npos)
893 __n = std::char_traits<_CharT>::length(__str);
894 _M_copy_from_ptr<_CharT, std::char_traits<_CharT>>(__str, __n, 0,
900 // 23.3.5.2 bitset operations:
903 * @brief Operations on bitsets.
904 * @param rhs A same-sized bitset.
906 * These should be self-explanatory.
909 operator&=(const bitset<_Nb>& __rhs)
911 this->_M_do_and(__rhs);
916 operator|=(const bitset<_Nb>& __rhs)
918 this->_M_do_or(__rhs);
923 operator^=(const bitset<_Nb>& __rhs)
925 this->_M_do_xor(__rhs);
932 * @brief Operations on bitsets.
933 * @param position The number of places to shift.
935 * These should be self-explanatory.
938 operator<<=(size_t __position)
940 if (__builtin_expect(__position < _Nb, 1))
942 this->_M_do_left_shift(__position);
943 this->_M_do_sanitize();
951 operator>>=(size_t __position)
953 if (__builtin_expect(__position < _Nb, 1))
955 this->_M_do_right_shift(__position);
956 this->_M_do_sanitize();
966 * These versions of single-bit set, reset, flip, and test are
967 * extensions from the SGI version. They do no range checking.
968 * @ingroup SGIextensions
971 _Unchecked_set(size_t __pos)
973 this->_M_getword(__pos) |= _Base::_S_maskbit(__pos);
978 _Unchecked_set(size_t __pos, int __val)
981 this->_M_getword(__pos) |= _Base::_S_maskbit(__pos);
983 this->_M_getword(__pos) &= ~_Base::_S_maskbit(__pos);
988 _Unchecked_reset(size_t __pos)
990 this->_M_getword(__pos) &= ~_Base::_S_maskbit(__pos);
995 _Unchecked_flip(size_t __pos)
997 this->_M_getword(__pos) ^= _Base::_S_maskbit(__pos);
1002 _Unchecked_test(size_t __pos) const
1003 { return ((this->_M_getword(__pos) & _Base::_S_maskbit(__pos))
1004 != static_cast<_WordT>(0)); }
1007 // Set, reset, and flip.
1009 * @brief Sets every bit to true.
1015 this->_M_do_sanitize();
1020 * @brief Sets a given bit to a particular value.
1021 * @param position The index of the bit.
1022 * @param val Either true or false, defaults to true.
1023 * @throw std::out_of_range If @a pos is bigger the size of the %set.
1026 set(size_t __position, bool __val = true)
1028 if (__position >= _Nb)
1029 __throw_out_of_range(__N("bitset::set"));
1030 return _Unchecked_set(__position, __val);
1034 * @brief Sets every bit to false.
1039 this->_M_do_reset();
1044 * @brief Sets a given bit to false.
1045 * @param position The index of the bit.
1046 * @throw std::out_of_range If @a pos is bigger the size of the %set.
1048 * Same as writing @c set(pos,false).
1051 reset(size_t __position)
1053 if (__position >= _Nb)
1054 __throw_out_of_range(__N("bitset::reset"));
1055 return _Unchecked_reset(__position);
1059 * @brief Toggles every bit to its opposite value.
1065 this->_M_do_sanitize();
1070 * @brief Toggles a given bit to its opposite value.
1071 * @param position The index of the bit.
1072 * @throw std::out_of_range If @a pos is bigger the size of the %set.
1075 flip(size_t __position)
1077 if (__position >= _Nb)
1078 __throw_out_of_range(__N("bitset::flip"));
1079 return _Unchecked_flip(__position);
1082 /// See the no-argument flip().
1085 { return bitset<_Nb>(*this).flip(); }
1089 * @brief Array-indexing support.
1090 * @param position Index into the %bitset.
1091 * @return A bool for a <em>const %bitset</em>. For non-const bitsets, an
1092 * instance of the reference proxy class.
1093 * @note These operators do no range checking and throw no exceptions,
1094 * as required by DR 11 to the standard.
1096 * _GLIBCXX_RESOLVE_LIB_DEFECTS Note that this implementation already
1097 * resolves DR 11 (items 1 and 2), but does not do the range-checking
1098 * required by that DR's resolution. -pme
1099 * The DR has since been changed: range-checking is a precondition
1100 * (users' responsibility), and these functions must not throw. -pme
1103 operator[](size_t __position)
1104 { return reference(*this,__position); }
1107 operator[](size_t __position) const
1108 { return _Unchecked_test(__position); }
1112 * @brief Returns a numerical interpretation of the %bitset.
1113 * @return The integral equivalent of the bits.
1114 * @throw std::overflow_error If there are too many bits to be
1115 * represented in an @c unsigned @c long.
1119 { return this->_M_do_to_ulong(); }
1121 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1124 { return this->_M_do_to_ullong(); }
1128 * @brief Returns a character interpretation of the %bitset.
1129 * @return The string equivalent of the bits.
1131 * Note the ordering of the bits: decreasing character positions
1132 * correspond to increasing bit positions (see the main class notes for
1135 template<class _CharT, class _Traits, class _Alloc>
1136 std::basic_string<_CharT, _Traits, _Alloc>
1139 std::basic_string<_CharT, _Traits, _Alloc> __result;
1140 _M_copy_to_string(__result, _CharT('0'), _CharT('1'));
1144 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1145 // 396. what are characters zero and one.
1146 template<class _CharT, class _Traits, class _Alloc>
1147 std::basic_string<_CharT, _Traits, _Alloc>
1148 to_string(_CharT __zero, _CharT __one = _CharT('1')) const
1150 std::basic_string<_CharT, _Traits, _Alloc> __result;
1151 _M_copy_to_string(__result, __zero, __one);
1155 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1156 // 434. bitset::to_string() hard to use.
1157 template<class _CharT, class _Traits>
1158 std::basic_string<_CharT, _Traits, std::allocator<_CharT> >
1160 { return to_string<_CharT, _Traits, std::allocator<_CharT> >(); }
1162 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1163 // 853. to_string needs updating with zero and one.
1164 template<class _CharT, class _Traits>
1165 std::basic_string<_CharT, _Traits, std::allocator<_CharT> >
1166 to_string(_CharT __zero, _CharT __one = _CharT('1')) const
1167 { return to_string<_CharT, _Traits,
1168 std::allocator<_CharT> >(__zero, __one); }
1170 template<class _CharT>
1171 std::basic_string<_CharT, std::char_traits<_CharT>,
1172 std::allocator<_CharT> >
1175 return to_string<_CharT, std::char_traits<_CharT>,
1176 std::allocator<_CharT> >();
1179 template<class _CharT>
1180 std::basic_string<_CharT, std::char_traits<_CharT>,
1181 std::allocator<_CharT> >
1182 to_string(_CharT __zero, _CharT __one = _CharT('1')) const
1184 return to_string<_CharT, std::char_traits<_CharT>,
1185 std::allocator<_CharT> >(__zero, __one);
1188 std::basic_string<char, std::char_traits<char>, std::allocator<char> >
1191 return to_string<char, std::char_traits<char>,
1192 std::allocator<char> >();
1195 std::basic_string<char, std::char_traits<char>, std::allocator<char> >
1196 to_string(char __zero, char __one = '1') const
1198 return to_string<char, std::char_traits<char>,
1199 std::allocator<char> >(__zero, __one);
1202 // Helper functions for string operations.
1203 template<class _CharT, class _Traits>
1205 _M_copy_from_ptr(const _CharT*, size_t, size_t, size_t,
1208 template<class _CharT, class _Traits, class _Alloc>
1210 _M_copy_from_string(const std::basic_string<_CharT,
1211 _Traits, _Alloc>& __s, size_t __pos, size_t __n,
1212 _CharT __zero, _CharT __one)
1213 { _M_copy_from_ptr<_CharT, _Traits>(__s.data(), __s.size(), __pos, __n,
1216 template<class _CharT, class _Traits, class _Alloc>
1218 _M_copy_to_string(std::basic_string<_CharT, _Traits, _Alloc>&,
1219 _CharT, _CharT) const;
1221 // NB: Backward compat.
1222 template<class _CharT, class _Traits, class _Alloc>
1224 _M_copy_from_string(const std::basic_string<_CharT,
1225 _Traits, _Alloc>& __s, size_t __pos, size_t __n)
1226 { _M_copy_from_string(__s, __pos, __n, _CharT('0'), _CharT('1')); }
1228 template<class _CharT, class _Traits, class _Alloc>
1230 _M_copy_to_string(std::basic_string<_CharT, _Traits,_Alloc>& __s) const
1231 { _M_copy_to_string(__s, _CharT('0'), _CharT('1')); }
1233 /// Returns the number of bits which are set.
1236 { return this->_M_do_count(); }
1238 /// Returns the total number of bits.
1244 /// These comparisons for equality/inequality are, well, @e bitwise.
1246 operator==(const bitset<_Nb>& __rhs) const
1247 { return this->_M_is_equal(__rhs); }
1250 operator!=(const bitset<_Nb>& __rhs) const
1251 { return !this->_M_is_equal(__rhs); }
1255 * @brief Tests the value of a bit.
1256 * @param position The index of a bit.
1257 * @return The value at @a pos.
1258 * @throw std::out_of_range If @a pos is bigger the size of the %set.
1261 test(size_t __position) const
1263 if (__position >= _Nb)
1264 __throw_out_of_range(__N("bitset::test"));
1265 return _Unchecked_test(__position);
1268 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1269 // DR 693. std::bitset::all() missing.
1271 * @brief Tests whether all the bits are on.
1272 * @return True if all the bits are set.
1276 { return this->_M_are_all_aux() == _Nb; }
1279 * @brief Tests whether any of the bits are on.
1280 * @return True if at least one bit is set.
1284 { return this->_M_is_any(); }
1287 * @brief Tests whether any of the bits are on.
1288 * @return True if none of the bits are set.
1292 { return !this->_M_is_any(); }
1295 /// Self-explanatory.
1297 operator<<(size_t __position) const
1298 { return bitset<_Nb>(*this) <<= __position; }
1301 operator>>(size_t __position) const
1302 { return bitset<_Nb>(*this) >>= __position; }
1306 * @brief Finds the index of the first "on" bit.
1307 * @return The index of the first bit set, or size() if not found.
1308 * @ingroup SGIextensions
1313 { return this->_M_do_find_first(_Nb); }
1316 * @brief Finds the index of the next "on" bit after prev.
1317 * @return The index of the next bit set, or size() if not found.
1318 * @param prev Where to start searching.
1319 * @ingroup SGIextensions
1323 _Find_next(size_t __prev ) const
1324 { return this->_M_do_find_next(__prev, _Nb); }
1327 // Definitions of non-inline member functions.
1328 template<size_t _Nb>
1329 template<class _CharT, class _Traits>
1332 _M_copy_from_ptr(const _CharT* __s, size_t __len,
1333 size_t __pos, size_t __n, _CharT __zero, _CharT __one)
1336 const size_t __nbits = std::min(_Nb, std::min(__n, __len - __pos));
1337 for (size_t __i = __nbits; __i > 0; --__i)
1339 const _CharT __c = __s[__pos + __nbits - __i];
1340 if (_Traits::eq(__c, __zero))
1342 else if (_Traits::eq(__c, __one))
1343 _Unchecked_set(__i - 1);
1345 __throw_invalid_argument(__N("bitset::_M_copy_from_ptr"));
1349 template<size_t _Nb>
1350 template<class _CharT, class _Traits, class _Alloc>
1353 _M_copy_to_string(std::basic_string<_CharT, _Traits, _Alloc>& __s,
1354 _CharT __zero, _CharT __one) const
1356 __s.assign(_Nb, __zero);
1357 for (size_t __i = _Nb; __i > 0; --__i)
1358 if (_Unchecked_test(__i - 1))
1359 _Traits::assign(__s[_Nb - __i], __one);
1362 // 23.3.5.3 bitset operations:
1365 * @brief Global bitwise operations on bitsets.
1366 * @param x A bitset.
1367 * @param y A bitset of the same size as @a x.
1368 * @return A new bitset.
1370 * These should be self-explanatory.
1372 template<size_t _Nb>
1374 operator&(const bitset<_Nb>& __x, const bitset<_Nb>& __y)
1376 bitset<_Nb> __result(__x);
1381 template<size_t _Nb>
1383 operator|(const bitset<_Nb>& __x, const bitset<_Nb>& __y)
1385 bitset<_Nb> __result(__x);
1390 template <size_t _Nb>
1392 operator^(const bitset<_Nb>& __x, const bitset<_Nb>& __y)
1394 bitset<_Nb> __result(__x);
1402 * @brief Global I/O operators for bitsets.
1404 * Direct I/O between streams and bitsets is supported. Output is
1405 * straightforward. Input will skip whitespace, only accept @a 0 and @a 1
1406 * characters, and will only extract as many digits as the %bitset will
1409 template<class _CharT, class _Traits, size_t _Nb>
1410 std::basic_istream<_CharT, _Traits>&
1411 operator>>(std::basic_istream<_CharT, _Traits>& __is, bitset<_Nb>& __x)
1413 typedef typename _Traits::char_type char_type;
1414 typedef std::basic_istream<_CharT, _Traits> __istream_type;
1415 typedef typename __istream_type::ios_base __ios_base;
1417 std::basic_string<_CharT, _Traits> __tmp;
1420 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1421 // 303. Bitset input operator underspecified
1422 const char_type __zero = __is.widen('0');
1423 const char_type __one = __is.widen('1');
1425 typename __ios_base::iostate __state = __ios_base::goodbit;
1426 typename __istream_type::sentry __sentry(__is);
1431 for (size_t __i = _Nb; __i > 0; --__i)
1433 static typename _Traits::int_type __eof = _Traits::eof();
1435 typename _Traits::int_type __c1 = __is.rdbuf()->sbumpc();
1436 if (_Traits::eq_int_type(__c1, __eof))
1438 __state |= __ios_base::eofbit;
1443 const char_type __c2 = _Traits::to_char_type(__c1);
1444 if (_Traits::eq(__c2, __zero))
1445 __tmp.push_back(__zero);
1446 else if (_Traits::eq(__c2, __one))
1447 __tmp.push_back(__one);
1449 eq_int_type(__is.rdbuf()->sputbackc(__c2),
1452 __state |= __ios_base::failbit;
1458 __catch(__cxxabiv1::__forced_unwind&)
1460 __is._M_setstate(__ios_base::badbit);
1461 __throw_exception_again;
1464 { __is._M_setstate(__ios_base::badbit); }
1467 if (__tmp.empty() && _Nb)
1468 __state |= __ios_base::failbit;
1470 __x._M_copy_from_string(__tmp, static_cast<size_t>(0), _Nb,
1473 __is.setstate(__state);
1477 template <class _CharT, class _Traits, size_t _Nb>
1478 std::basic_ostream<_CharT, _Traits>&
1479 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1480 const bitset<_Nb>& __x)
1482 std::basic_string<_CharT, _Traits> __tmp;
1484 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1485 // 396. what are characters zero and one.
1486 const ctype<_CharT>& __ct = use_facet<ctype<_CharT> >(__os.getloc());
1487 __x._M_copy_to_string(__tmp, __ct.widen('0'), __ct.widen('1'));
1488 return __os << __tmp;
1492 _GLIBCXX_END_NESTED_NAMESPACE
1494 #undef _GLIBCXX_BITSET_WORDS
1495 #undef _GLIBCXX_BITSET_BITS_PER_WORD
1497 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1499 #include <bits/functional_hash.h>
1501 _GLIBCXX_BEGIN_NAMESPACE(std)
1504 /// std::hash specialization for bitset.
1505 template<size_t _Nb>
1506 struct hash<_GLIBCXX_STD_D::bitset<_Nb>>
1507 : public __hash_base<size_t, _GLIBCXX_STD_D::bitset<_Nb>>
1510 operator()(const _GLIBCXX_STD_D::bitset<_Nb>& __b) const
1512 const size_t __clength = (_Nb + __CHAR_BIT__ - 1) / __CHAR_BIT__;
1513 return std::_Hash_impl::hash(__b._M_getdata(), __clength);
1518 struct hash<_GLIBCXX_STD_D::bitset<0>>
1519 : public __hash_base<size_t, _GLIBCXX_STD_D::bitset<0>>
1522 operator()(const _GLIBCXX_STD_D::bitset<0>&) const
1526 _GLIBCXX_END_NAMESPACE
1528 #endif // __GXX_EXPERIMENTAL_CXX0X__
1530 #ifdef _GLIBCXX_DEBUG
1531 # include <debug/bitset>
1534 #ifdef _GLIBCXX_PROFILE
1535 # include <profile/bitset>
1538 #endif /* _GLIBCXX_BITSET */