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
48 #include <cstddef> // For size_t
50 #include <bits/functexcept.h> // For invalid_argument, out_of_range,
53 #include <cxxabi-forced.h>
55 #define _GLIBCXX_BITSET_BITS_PER_WORD (__CHAR_BIT__ * sizeof(unsigned long))
56 #define _GLIBCXX_BITSET_WORDS(__n) \
57 ((__n) < 1 ? 0 : ((__n) + _GLIBCXX_BITSET_BITS_PER_WORD - 1) \
58 / _GLIBCXX_BITSET_BITS_PER_WORD)
60 _GLIBCXX_BEGIN_NESTED_NAMESPACE(std, _GLIBCXX_STD_D)
63 * Base class, general case. It is a class invariant that _Nw will be
66 * See documentation for bitset.
71 typedef unsigned long _WordT;
73 /// 0 is the least significant word.
79 #ifdef __GXX_EXPERIMENTAL_CXX0X__
80 _Base_bitset(unsigned long long __val)
82 _Base_bitset(unsigned long __val)
87 #ifdef __GXX_EXPERIMENTAL_CXX0X__
88 if (sizeof(unsigned long long) > sizeof(unsigned long))
89 _M_w[1] = __val >> _GLIBCXX_BITSET_BITS_PER_WORD;
94 _S_whichword(size_t __pos )
95 { return __pos / _GLIBCXX_BITSET_BITS_PER_WORD; }
98 _S_whichbyte(size_t __pos )
99 { return (__pos % _GLIBCXX_BITSET_BITS_PER_WORD) / __CHAR_BIT__; }
102 _S_whichbit(size_t __pos )
103 { return __pos % _GLIBCXX_BITSET_BITS_PER_WORD; }
106 _S_maskbit(size_t __pos )
107 { return (static_cast<_WordT>(1)) << _S_whichbit(__pos); }
110 _M_getword(size_t __pos)
111 { return _M_w[_S_whichword(__pos)]; }
114 _M_getword(size_t __pos) const
115 { return _M_w[_S_whichword(__pos)]; }
117 #ifdef __GXX_EXPERIMENTAL_CXX0X__
120 { return reinterpret_cast<const char*>(_M_w); }
125 { return _M_w[_Nw - 1]; }
129 { return _M_w[_Nw - 1]; }
132 _M_do_and(const _Base_bitset<_Nw>& __x)
134 for (size_t __i = 0; __i < _Nw; __i++)
135 _M_w[__i] &= __x._M_w[__i];
139 _M_do_or(const _Base_bitset<_Nw>& __x)
141 for (size_t __i = 0; __i < _Nw; __i++)
142 _M_w[__i] |= __x._M_w[__i];
146 _M_do_xor(const _Base_bitset<_Nw>& __x)
148 for (size_t __i = 0; __i < _Nw; __i++)
149 _M_w[__i] ^= __x._M_w[__i];
153 _M_do_left_shift(size_t __shift);
156 _M_do_right_shift(size_t __shift);
161 for (size_t __i = 0; __i < _Nw; __i++)
162 _M_w[__i] = ~_M_w[__i];
168 for (size_t __i = 0; __i < _Nw; __i++)
169 _M_w[__i] = ~static_cast<_WordT>(0);
174 { __builtin_memset(_M_w, 0, _Nw * sizeof(_WordT)); }
177 _M_is_equal(const _Base_bitset<_Nw>& __x) const
179 for (size_t __i = 0; __i < _Nw; ++__i)
180 if (_M_w[__i] != __x._M_w[__i])
186 _M_are_all_aux() const
188 for (size_t __i = 0; __i < _Nw - 1; __i++)
189 if (_M_w[__i] != ~static_cast<_WordT>(0))
191 return ((_Nw - 1) * _GLIBCXX_BITSET_BITS_PER_WORD
192 + __builtin_popcountl(_M_hiword()));
198 for (size_t __i = 0; __i < _Nw; __i++)
199 if (_M_w[__i] != static_cast<_WordT>(0))
208 for (size_t __i = 0; __i < _Nw; __i++)
209 __result += __builtin_popcountl(_M_w[__i]);
214 _M_do_to_ulong() const;
216 #ifdef __GXX_EXPERIMENTAL_CXX0X__
218 _M_do_to_ullong() const;
221 // find first "on" bit
223 _M_do_find_first(size_t __not_found) const;
225 // find the next "on" bit that follows "prev"
227 _M_do_find_next(size_t __prev, size_t __not_found) const;
230 // Definitions of non-inline functions from _Base_bitset.
233 _Base_bitset<_Nw>::_M_do_left_shift(size_t __shift)
235 if (__builtin_expect(__shift != 0, 1))
237 const size_t __wshift = __shift / _GLIBCXX_BITSET_BITS_PER_WORD;
238 const size_t __offset = __shift % _GLIBCXX_BITSET_BITS_PER_WORD;
241 for (size_t __n = _Nw - 1; __n >= __wshift; --__n)
242 _M_w[__n] = _M_w[__n - __wshift];
245 const size_t __sub_offset = (_GLIBCXX_BITSET_BITS_PER_WORD
247 for (size_t __n = _Nw - 1; __n > __wshift; --__n)
248 _M_w[__n] = ((_M_w[__n - __wshift] << __offset)
249 | (_M_w[__n - __wshift - 1] >> __sub_offset));
250 _M_w[__wshift] = _M_w[0] << __offset;
253 std::fill(_M_w + 0, _M_w + __wshift, static_cast<_WordT>(0));
259 _Base_bitset<_Nw>::_M_do_right_shift(size_t __shift)
261 if (__builtin_expect(__shift != 0, 1))
263 const size_t __wshift = __shift / _GLIBCXX_BITSET_BITS_PER_WORD;
264 const size_t __offset = __shift % _GLIBCXX_BITSET_BITS_PER_WORD;
265 const size_t __limit = _Nw - __wshift - 1;
268 for (size_t __n = 0; __n <= __limit; ++__n)
269 _M_w[__n] = _M_w[__n + __wshift];
272 const size_t __sub_offset = (_GLIBCXX_BITSET_BITS_PER_WORD
274 for (size_t __n = 0; __n < __limit; ++__n)
275 _M_w[__n] = ((_M_w[__n + __wshift] >> __offset)
276 | (_M_w[__n + __wshift + 1] << __sub_offset));
277 _M_w[__limit] = _M_w[_Nw-1] >> __offset;
280 std::fill(_M_w + __limit + 1, _M_w + _Nw, static_cast<_WordT>(0));
286 _Base_bitset<_Nw>::_M_do_to_ulong() const
288 for (size_t __i = 1; __i < _Nw; ++__i)
290 __throw_overflow_error(__N("_Base_bitset::_M_do_to_ulong"));
294 #ifdef __GXX_EXPERIMENTAL_CXX0X__
297 _Base_bitset<_Nw>::_M_do_to_ullong() const
299 const bool __dw = sizeof(unsigned long long) > sizeof(unsigned long);
300 for (size_t __i = 1 + __dw; __i < _Nw; ++__i)
302 __throw_overflow_error(__N("_Base_bitset::_M_do_to_ullong"));
305 return _M_w[0] + (static_cast<unsigned long long>(_M_w[1])
306 << _GLIBCXX_BITSET_BITS_PER_WORD);
313 _Base_bitset<_Nw>::_M_do_find_first(size_t __not_found) const
315 for (size_t __i = 0; __i < _Nw; __i++)
317 _WordT __thisword = _M_w[__i];
318 if (__thisword != static_cast<_WordT>(0))
319 return (__i * _GLIBCXX_BITSET_BITS_PER_WORD
320 + __builtin_ctzl(__thisword));
322 // not found, so return an indication of failure.
328 _Base_bitset<_Nw>::_M_do_find_next(size_t __prev, size_t __not_found) const
330 // make bound inclusive
333 // check out of bounds
334 if (__prev >= _Nw * _GLIBCXX_BITSET_BITS_PER_WORD)
338 size_t __i = _S_whichword(__prev);
339 _WordT __thisword = _M_w[__i];
341 // mask off bits below bound
342 __thisword &= (~static_cast<_WordT>(0)) << _S_whichbit(__prev);
344 if (__thisword != static_cast<_WordT>(0))
345 return (__i * _GLIBCXX_BITSET_BITS_PER_WORD
346 + __builtin_ctzl(__thisword));
348 // check subsequent words
350 for (; __i < _Nw; __i++)
352 __thisword = _M_w[__i];
353 if (__thisword != static_cast<_WordT>(0))
354 return (__i * _GLIBCXX_BITSET_BITS_PER_WORD
355 + __builtin_ctzl(__thisword));
357 // not found, so return an indication of failure.
359 } // end _M_do_find_next
362 * Base class, specialization for a single word.
364 * See documentation for bitset.
367 struct _Base_bitset<1>
369 typedef unsigned long _WordT;
376 #ifdef __GXX_EXPERIMENTAL_CXX0X__
377 _Base_bitset(unsigned long long __val)
379 _Base_bitset(unsigned long __val)
385 _S_whichword(size_t __pos )
386 { return __pos / _GLIBCXX_BITSET_BITS_PER_WORD; }
389 _S_whichbyte(size_t __pos )
390 { return (__pos % _GLIBCXX_BITSET_BITS_PER_WORD) / __CHAR_BIT__; }
393 _S_whichbit(size_t __pos )
394 { return __pos % _GLIBCXX_BITSET_BITS_PER_WORD; }
397 _S_maskbit(size_t __pos )
398 { return (static_cast<_WordT>(1)) << _S_whichbit(__pos); }
405 _M_getword(size_t) const
408 #ifdef __GXX_EXPERIMENTAL_CXX0X__
411 { return reinterpret_cast<const char*>(&_M_w); }
423 _M_do_and(const _Base_bitset<1>& __x)
424 { _M_w &= __x._M_w; }
427 _M_do_or(const _Base_bitset<1>& __x)
428 { _M_w |= __x._M_w; }
431 _M_do_xor(const _Base_bitset<1>& __x)
432 { _M_w ^= __x._M_w; }
435 _M_do_left_shift(size_t __shift)
436 { _M_w <<= __shift; }
439 _M_do_right_shift(size_t __shift)
440 { _M_w >>= __shift; }
448 { _M_w = ~static_cast<_WordT>(0); }
455 _M_is_equal(const _Base_bitset<1>& __x) const
456 { return _M_w == __x._M_w; }
459 _M_are_all_aux() const
460 { return __builtin_popcountl(_M_w); }
464 { return _M_w != 0; }
468 { return __builtin_popcountl(_M_w); }
471 _M_do_to_ulong() const
474 #ifdef __GXX_EXPERIMENTAL_CXX0X__
476 _M_do_to_ullong() const
481 _M_do_find_first(size_t __not_found) const
484 return __builtin_ctzl(_M_w);
489 // find the next "on" bit that follows "prev"
491 _M_do_find_next(size_t __prev, size_t __not_found) const
494 if (__prev >= ((size_t) _GLIBCXX_BITSET_BITS_PER_WORD))
497 _WordT __x = _M_w >> __prev;
499 return __builtin_ctzl(__x) + __prev;
506 * Base class, specialization for no storage (zero-length %bitset).
508 * See documentation for bitset.
511 struct _Base_bitset<0>
513 typedef unsigned long _WordT;
518 #ifdef __GXX_EXPERIMENTAL_CXX0X__
519 _Base_bitset(unsigned long long)
521 _Base_bitset(unsigned long)
526 _S_whichword(size_t __pos )
527 { return __pos / _GLIBCXX_BITSET_BITS_PER_WORD; }
530 _S_whichbyte(size_t __pos )
531 { return (__pos % _GLIBCXX_BITSET_BITS_PER_WORD) / __CHAR_BIT__; }
534 _S_whichbit(size_t __pos )
535 { return __pos % _GLIBCXX_BITSET_BITS_PER_WORD; }
538 _S_maskbit(size_t __pos )
539 { return (static_cast<_WordT>(1)) << _S_whichbit(__pos); }
541 // This would normally give access to the data. The bounds-checking
542 // in the bitset class will prevent the user from getting this far,
543 // but (1) it must still return an lvalue to compile, and (2) the
544 // user might call _Unchecked_set directly, in which case this /needs/
545 // to fail. Let's not penalize zero-length users unless they actually
546 // make an unchecked call; all the memory ugliness is therefore
547 // localized to this single should-never-get-this-far function.
549 _M_getword(size_t) const
551 __throw_out_of_range(__N("_Base_bitset::_M_getword"));
555 #ifdef __GXX_EXPERIMENTAL_CXX0X__
558 { return reinterpret_cast<const char*>(&_M_getword(0)); }
566 _M_do_and(const _Base_bitset<0>&)
570 _M_do_or(const _Base_bitset<0>&)
574 _M_do_xor(const _Base_bitset<0>&)
578 _M_do_left_shift(size_t)
582 _M_do_right_shift(size_t)
597 // Are all empty bitsets equal to each other? Are they equal to
598 // themselves? How to compare a thing which has no state? What is
599 // the sound of one zero-length bitset clapping?
601 _M_is_equal(const _Base_bitset<0>&) const
605 _M_are_all_aux() const
617 _M_do_to_ulong() const
620 #ifdef __GXX_EXPERIMENTAL_CXX0X__
622 _M_do_to_ullong() const
626 // Normally "not found" is the size, but that could also be
627 // misinterpreted as an index in this corner case. Oh well.
629 _M_do_find_first(size_t) const
633 _M_do_find_next(size_t, size_t) const
638 // Helper class to zero out the unused high-order bits in the highest word.
639 template<size_t _Extrabits>
642 static void _S_do_sanitize(unsigned long& __val)
643 { __val &= ~((~static_cast<unsigned long>(0)) << _Extrabits); }
648 { static void _S_do_sanitize(unsigned long) {} };
651 * @brief The %bitset class represents a @e fixed-size sequence of bits.
653 * @ingroup containers
655 * (Note that %bitset does @e not meet the formal requirements of a
656 * <a href="tables.html#65">container</a>. Mainly, it lacks iterators.)
658 * The template argument, @a Nb, may be any non-negative number,
659 * specifying the number of bits (e.g., "0", "12", "1024*1024").
661 * In the general unoptimized case, storage is allocated in word-sized
662 * blocks. Let B be the number of bits in a word, then (Nb+(B-1))/B
663 * words will be used for storage. B - Nb%B bits are unused. (They are
664 * the high-order bits in the highest word.) It is a class invariant
665 * that those unused bits are always zero.
667 * If you think of %bitset as <em>a simple array of bits</em>, be
668 * aware that your mental picture is reversed: a %bitset behaves
669 * the same way as bits in integers do, with the bit at index 0 in
670 * the <em>least significant / right-hand</em> position, and the bit at
671 * index Nb-1 in the <em>most significant / left-hand</em> position.
672 * Thus, unlike other containers, a %bitset's index <em>counts from
673 * right to left</em>, to put it very loosely.
675 * This behavior is preserved when translating to and from strings. For
676 * example, the first line of the following program probably prints
677 * <em>b('a') is 0001100001</em> on a modern ASCII system.
681 * #include <iostream>
684 * using namespace std;
691 * cout << "b('a') is " << b << endl;
695 * string str = s.str();
696 * cout << "index 3 in the string is " << str[3] << " but\n"
697 * << "index 3 in the bitset is " << b[3] << endl;
702 * http://gcc.gnu.org/onlinedocs/libstdc++/manual/bk01pt12ch33s02.html
703 * for a description of extensions.
705 * Most of the actual code isn't contained in %bitset<> itself, but in the
706 * base class _Base_bitset. The base class works with whole words, not with
707 * individual bits. This allows us to specialize _Base_bitset for the
708 * important special case where the %bitset is only a single word.
710 * Extra confusion can result due to the fact that the storage for
711 * _Base_bitset @e is a regular array, and is indexed as such. This is
712 * carefully encapsulated.
716 : private _Base_bitset<_GLIBCXX_BITSET_WORDS(_Nb)>
719 typedef _Base_bitset<_GLIBCXX_BITSET_WORDS(_Nb)> _Base;
720 typedef unsigned long _WordT;
725 _Sanitize<_Nb % _GLIBCXX_BITSET_BITS_PER_WORD>::
726 _S_do_sanitize(this->_M_hiword());
729 #ifdef __GXX_EXPERIMENTAL_CXX0X__
730 template<typename> friend class hash;
735 * This encapsulates the concept of a single bit. An instance of this
736 * class is a proxy for an actual bit; this way the individual bit
737 * operations are done as faster word-size bitwise instructions.
739 * Most users will never need to use this class directly; conversions
740 * to and from bool are automatic and should be transparent. Overloaded
741 * operators help to preserve the illusion.
743 * (On a typical system, this <em>bit %reference</em> is 64
744 * times the size of an actual bit. Ha.)
757 reference(bitset& __b, size_t __pos)
759 _M_wp = &__b._M_getword(__pos);
760 _M_bpos = _Base::_S_whichbit(__pos);
771 *_M_wp |= _Base::_S_maskbit(_M_bpos);
773 *_M_wp &= ~_Base::_S_maskbit(_M_bpos);
777 // For b[i] = b[__j];
779 operator=(const reference& __j)
781 if ((*(__j._M_wp) & _Base::_S_maskbit(__j._M_bpos)))
782 *_M_wp |= _Base::_S_maskbit(_M_bpos);
784 *_M_wp &= ~_Base::_S_maskbit(_M_bpos);
791 { return (*(_M_wp) & _Base::_S_maskbit(_M_bpos)) == 0; }
794 operator bool() const
795 { return (*(_M_wp) & _Base::_S_maskbit(_M_bpos)) != 0; }
801 *_M_wp ^= _Base::_S_maskbit(_M_bpos);
805 friend class reference;
807 // 23.3.5.1 constructors:
808 /// All bits set to zero.
812 /// Initial bits bitwise-copied from a single word (others set to zero).
813 #ifdef __GXX_EXPERIMENTAL_CXX0X__
814 bitset(unsigned long long __val)
816 bitset(unsigned long __val)
819 { _M_do_sanitize(); }
822 * @brief Use a subset of a string.
823 * @param s A string of @a 0 and @a 1 characters.
824 * @param position Index of the first character in @a s to use;
826 * @throw std::out_of_range If @a pos is bigger the size of @a s.
827 * @throw std::invalid_argument If a character appears in the string
828 * which is neither @a 0 nor @a 1.
830 template<class _CharT, class _Traits, class _Alloc>
832 bitset(const std::basic_string<_CharT, _Traits, _Alloc>& __s,
833 size_t __position = 0)
836 if (__position > __s.size())
837 __throw_out_of_range(__N("bitset::bitset initial position "
839 _M_copy_from_string(__s, __position,
840 std::basic_string<_CharT, _Traits, _Alloc>::npos,
841 _CharT('0'), _CharT('1'));
845 * @brief Use a subset of a string.
846 * @param s A string of @a 0 and @a 1 characters.
847 * @param position Index of the first character in @a s to use.
848 * @param n The number of characters to copy.
849 * @throw std::out_of_range If @a pos is bigger the size of @a s.
850 * @throw std::invalid_argument If a character appears in the string
851 * which is neither @a 0 nor @a 1.
853 template<class _CharT, class _Traits, class _Alloc>
854 bitset(const std::basic_string<_CharT, _Traits, _Alloc>& __s,
855 size_t __position, size_t __n)
858 if (__position > __s.size())
859 __throw_out_of_range(__N("bitset::bitset initial position "
861 _M_copy_from_string(__s, __position, __n, _CharT('0'), _CharT('1'));
864 // _GLIBCXX_RESOLVE_LIB_DEFECTS
865 // 396. what are characters zero and one.
866 template<class _CharT, class _Traits, class _Alloc>
867 bitset(const std::basic_string<_CharT, _Traits, _Alloc>& __s,
868 size_t __position, size_t __n,
869 _CharT __zero, _CharT __one = _CharT('1'))
872 if (__position > __s.size())
873 __throw_out_of_range(__N("bitset::bitset initial position "
875 _M_copy_from_string(__s, __position, __n, __zero, __one);
878 #ifdef __GXX_EXPERIMENTAL_CXX0X__
880 * @brief Construct from a string.
881 * @param str A string of @a 0 and @a 1 characters.
882 * @throw std::invalid_argument If a character appears in the string
883 * which is neither @a 0 nor @a 1.
886 bitset(const char* __str)
890 __throw_logic_error(__N("bitset::bitset(const char*)"));
892 const size_t __len = __builtin_strlen(__str);
893 _M_copy_from_ptr<char, std::char_traits<char>>(__str, __len, 0,
898 // 23.3.5.2 bitset operations:
901 * @brief Operations on bitsets.
902 * @param rhs A same-sized bitset.
904 * These should be self-explanatory.
907 operator&=(const bitset<_Nb>& __rhs)
909 this->_M_do_and(__rhs);
914 operator|=(const bitset<_Nb>& __rhs)
916 this->_M_do_or(__rhs);
921 operator^=(const bitset<_Nb>& __rhs)
923 this->_M_do_xor(__rhs);
930 * @brief Operations on bitsets.
931 * @param position The number of places to shift.
933 * These should be self-explanatory.
936 operator<<=(size_t __position)
938 if (__builtin_expect(__position < _Nb, 1))
940 this->_M_do_left_shift(__position);
941 this->_M_do_sanitize();
949 operator>>=(size_t __position)
951 if (__builtin_expect(__position < _Nb, 1))
953 this->_M_do_right_shift(__position);
954 this->_M_do_sanitize();
964 * These versions of single-bit set, reset, flip, and test are
965 * extensions from the SGI version. They do no range checking.
966 * @ingroup SGIextensions
969 _Unchecked_set(size_t __pos)
971 this->_M_getword(__pos) |= _Base::_S_maskbit(__pos);
976 _Unchecked_set(size_t __pos, int __val)
979 this->_M_getword(__pos) |= _Base::_S_maskbit(__pos);
981 this->_M_getword(__pos) &= ~_Base::_S_maskbit(__pos);
986 _Unchecked_reset(size_t __pos)
988 this->_M_getword(__pos) &= ~_Base::_S_maskbit(__pos);
993 _Unchecked_flip(size_t __pos)
995 this->_M_getword(__pos) ^= _Base::_S_maskbit(__pos);
1000 _Unchecked_test(size_t __pos) const
1001 { return ((this->_M_getword(__pos) & _Base::_S_maskbit(__pos))
1002 != static_cast<_WordT>(0)); }
1005 // Set, reset, and flip.
1007 * @brief Sets every bit to true.
1013 this->_M_do_sanitize();
1018 * @brief Sets a given bit to a particular value.
1019 * @param position The index of the bit.
1020 * @param val Either true or false, defaults to true.
1021 * @throw std::out_of_range If @a pos is bigger the size of the %set.
1024 set(size_t __position, bool __val = true)
1026 if (__position >= _Nb)
1027 __throw_out_of_range(__N("bitset::set"));
1028 return _Unchecked_set(__position, __val);
1032 * @brief Sets every bit to false.
1037 this->_M_do_reset();
1042 * @brief Sets a given bit to false.
1043 * @param position The index of the bit.
1044 * @throw std::out_of_range If @a pos is bigger the size of the %set.
1046 * Same as writing @c set(pos,false).
1049 reset(size_t __position)
1051 if (__position >= _Nb)
1052 __throw_out_of_range(__N("bitset::reset"));
1053 return _Unchecked_reset(__position);
1057 * @brief Toggles every bit to its opposite value.
1063 this->_M_do_sanitize();
1068 * @brief Toggles a given bit to its opposite value.
1069 * @param position The index of the bit.
1070 * @throw std::out_of_range If @a pos is bigger the size of the %set.
1073 flip(size_t __position)
1075 if (__position >= _Nb)
1076 __throw_out_of_range(__N("bitset::flip"));
1077 return _Unchecked_flip(__position);
1080 /// See the no-argument flip().
1083 { return bitset<_Nb>(*this).flip(); }
1087 * @brief Array-indexing support.
1088 * @param position Index into the %bitset.
1089 * @return A bool for a <em>const %bitset</em>. For non-const bitsets, an
1090 * instance of the reference proxy class.
1091 * @note These operators do no range checking and throw no exceptions,
1092 * as required by DR 11 to the standard.
1094 * _GLIBCXX_RESOLVE_LIB_DEFECTS Note that this implementation already
1095 * resolves DR 11 (items 1 and 2), but does not do the range-checking
1096 * required by that DR's resolution. -pme
1097 * The DR has since been changed: range-checking is a precondition
1098 * (users' responsibility), and these functions must not throw. -pme
1101 operator[](size_t __position)
1102 { return reference(*this,__position); }
1105 operator[](size_t __position) const
1106 { return _Unchecked_test(__position); }
1110 * @brief Returns a numerical interpretation of the %bitset.
1111 * @return The integral equivalent of the bits.
1112 * @throw std::overflow_error If there are too many bits to be
1113 * represented in an @c unsigned @c long.
1117 { return this->_M_do_to_ulong(); }
1119 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1122 { return this->_M_do_to_ullong(); }
1126 * @brief Returns a character interpretation of the %bitset.
1127 * @return The string equivalent of the bits.
1129 * Note the ordering of the bits: decreasing character positions
1130 * correspond to increasing bit positions (see the main class notes for
1133 template<class _CharT, class _Traits, class _Alloc>
1134 std::basic_string<_CharT, _Traits, _Alloc>
1137 std::basic_string<_CharT, _Traits, _Alloc> __result;
1138 _M_copy_to_string(__result, _CharT('0'), _CharT('1'));
1142 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1143 // 396. what are characters zero and one.
1144 template<class _CharT, class _Traits, class _Alloc>
1145 std::basic_string<_CharT, _Traits, _Alloc>
1146 to_string(_CharT __zero, _CharT __one = _CharT('1')) const
1148 std::basic_string<_CharT, _Traits, _Alloc> __result;
1149 _M_copy_to_string(__result, __zero, __one);
1153 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1154 // 434. bitset::to_string() hard to use.
1155 template<class _CharT, class _Traits>
1156 std::basic_string<_CharT, _Traits, std::allocator<_CharT> >
1158 { return to_string<_CharT, _Traits, std::allocator<_CharT> >(); }
1160 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1161 // 853. to_string needs updating with zero and one.
1162 template<class _CharT, class _Traits>
1163 std::basic_string<_CharT, _Traits, std::allocator<_CharT> >
1164 to_string(_CharT __zero, _CharT __one = _CharT('1')) const
1165 { return to_string<_CharT, _Traits,
1166 std::allocator<_CharT> >(__zero, __one); }
1168 template<class _CharT>
1169 std::basic_string<_CharT, std::char_traits<_CharT>,
1170 std::allocator<_CharT> >
1173 return to_string<_CharT, std::char_traits<_CharT>,
1174 std::allocator<_CharT> >();
1177 template<class _CharT>
1178 std::basic_string<_CharT, std::char_traits<_CharT>,
1179 std::allocator<_CharT> >
1180 to_string(_CharT __zero, _CharT __one = _CharT('1')) const
1182 return to_string<_CharT, std::char_traits<_CharT>,
1183 std::allocator<_CharT> >(__zero, __one);
1186 std::basic_string<char, std::char_traits<char>, std::allocator<char> >
1189 return to_string<char, std::char_traits<char>,
1190 std::allocator<char> >();
1193 std::basic_string<char, std::char_traits<char>, std::allocator<char> >
1194 to_string(char __zero, char __one = '1') const
1196 return to_string<char, std::char_traits<char>,
1197 std::allocator<char> >(__zero, __one);
1200 // Helper functions for string operations.
1201 template<class _CharT, class _Traits>
1203 _M_copy_from_ptr(const _CharT*, size_t, size_t, size_t,
1206 template<class _CharT, class _Traits, class _Alloc>
1208 _M_copy_from_string(const std::basic_string<_CharT,
1209 _Traits, _Alloc>& __s, size_t __pos, size_t __n,
1210 _CharT __zero, _CharT __one)
1211 { _M_copy_from_ptr<_CharT, _Traits>(__s.data(), __s.size(), __pos, __n,
1214 template<class _CharT, class _Traits, class _Alloc>
1216 _M_copy_to_string(std::basic_string<_CharT, _Traits, _Alloc>&,
1217 _CharT, _CharT) const;
1219 // NB: Backward compat.
1220 template<class _CharT, class _Traits, class _Alloc>
1222 _M_copy_from_string(const std::basic_string<_CharT,
1223 _Traits, _Alloc>& __s, size_t __pos, size_t __n)
1224 { _M_copy_from_string(__s, __pos, __n, _CharT('0'), _CharT('1')); }
1226 template<class _CharT, class _Traits, class _Alloc>
1228 _M_copy_to_string(std::basic_string<_CharT, _Traits,_Alloc>& __s) const
1229 { _M_copy_to_string(__s, _CharT('0'), _CharT('1')); }
1231 /// Returns the number of bits which are set.
1234 { return this->_M_do_count(); }
1236 /// Returns the total number of bits.
1242 /// These comparisons for equality/inequality are, well, @e bitwise.
1244 operator==(const bitset<_Nb>& __rhs) const
1245 { return this->_M_is_equal(__rhs); }
1248 operator!=(const bitset<_Nb>& __rhs) const
1249 { return !this->_M_is_equal(__rhs); }
1253 * @brief Tests the value of a bit.
1254 * @param position The index of a bit.
1255 * @return The value at @a pos.
1256 * @throw std::out_of_range If @a pos is bigger the size of the %set.
1259 test(size_t __position) const
1261 if (__position >= _Nb)
1262 __throw_out_of_range(__N("bitset::test"));
1263 return _Unchecked_test(__position);
1266 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1267 // DR 693. std::bitset::all() missing.
1269 * @brief Tests whether all the bits are on.
1270 * @return True if all the bits are set.
1274 { return this->_M_are_all_aux() == _Nb; }
1277 * @brief Tests whether any of the bits are on.
1278 * @return True if at least one bit is set.
1282 { return this->_M_is_any(); }
1285 * @brief Tests whether any of the bits are on.
1286 * @return True if none of the bits are set.
1290 { return !this->_M_is_any(); }
1293 /// Self-explanatory.
1295 operator<<(size_t __position) const
1296 { return bitset<_Nb>(*this) <<= __position; }
1299 operator>>(size_t __position) const
1300 { return bitset<_Nb>(*this) >>= __position; }
1304 * @brief Finds the index of the first "on" bit.
1305 * @return The index of the first bit set, or size() if not found.
1306 * @ingroup SGIextensions
1311 { return this->_M_do_find_first(_Nb); }
1314 * @brief Finds the index of the next "on" bit after prev.
1315 * @return The index of the next bit set, or size() if not found.
1316 * @param prev Where to start searching.
1317 * @ingroup SGIextensions
1321 _Find_next(size_t __prev ) const
1322 { return this->_M_do_find_next(__prev, _Nb); }
1325 // Definitions of non-inline member functions.
1326 template<size_t _Nb>
1327 template<class _CharT, class _Traits>
1330 _M_copy_from_ptr(const _CharT* __s, size_t __len,
1331 size_t __pos, size_t __n, _CharT __zero, _CharT __one)
1334 const size_t __nbits = std::min(_Nb, std::min(__n, __len - __pos));
1335 for (size_t __i = __nbits; __i > 0; --__i)
1337 const _CharT __c = __s[__pos + __nbits - __i];
1338 if (_Traits::eq(__c, __zero))
1340 else if (_Traits::eq(__c, __one))
1341 _Unchecked_set(__i - 1);
1343 __throw_invalid_argument(__N("bitset::_M_copy_from_ptr"));
1347 template<size_t _Nb>
1348 template<class _CharT, class _Traits, class _Alloc>
1351 _M_copy_to_string(std::basic_string<_CharT, _Traits, _Alloc>& __s,
1352 _CharT __zero, _CharT __one) const
1354 __s.assign(_Nb, __zero);
1355 for (size_t __i = _Nb; __i > 0; --__i)
1356 if (_Unchecked_test(__i - 1))
1357 _Traits::assign(__s[_Nb - __i], __one);
1360 // 23.3.5.3 bitset operations:
1363 * @brief Global bitwise operations on bitsets.
1364 * @param x A bitset.
1365 * @param y A bitset of the same size as @a x.
1366 * @return A new bitset.
1368 * These should be self-explanatory.
1370 template<size_t _Nb>
1372 operator&(const bitset<_Nb>& __x, const bitset<_Nb>& __y)
1374 bitset<_Nb> __result(__x);
1379 template<size_t _Nb>
1381 operator|(const bitset<_Nb>& __x, const bitset<_Nb>& __y)
1383 bitset<_Nb> __result(__x);
1388 template <size_t _Nb>
1390 operator^(const bitset<_Nb>& __x, const bitset<_Nb>& __y)
1392 bitset<_Nb> __result(__x);
1400 * @brief Global I/O operators for bitsets.
1402 * Direct I/O between streams and bitsets is supported. Output is
1403 * straightforward. Input will skip whitespace, only accept @a 0 and @a 1
1404 * characters, and will only extract as many digits as the %bitset will
1407 template<class _CharT, class _Traits, size_t _Nb>
1408 std::basic_istream<_CharT, _Traits>&
1409 operator>>(std::basic_istream<_CharT, _Traits>& __is, bitset<_Nb>& __x)
1411 typedef typename _Traits::char_type char_type;
1412 typedef std::basic_istream<_CharT, _Traits> __istream_type;
1413 typedef typename __istream_type::ios_base __ios_base;
1415 std::basic_string<_CharT, _Traits> __tmp;
1418 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1419 // 303. Bitset input operator underspecified
1420 const char_type __zero = __is.widen('0');
1421 const char_type __one = __is.widen('1');
1423 typename __ios_base::iostate __state = __ios_base::goodbit;
1424 typename __istream_type::sentry __sentry(__is);
1429 for (size_t __i = _Nb; __i > 0; --__i)
1431 static typename _Traits::int_type __eof = _Traits::eof();
1433 typename _Traits::int_type __c1 = __is.rdbuf()->sbumpc();
1434 if (_Traits::eq_int_type(__c1, __eof))
1436 __state |= __ios_base::eofbit;
1441 const char_type __c2 = _Traits::to_char_type(__c1);
1442 if (_Traits::eq(__c2, __zero))
1443 __tmp.push_back(__zero);
1444 else if (_Traits::eq(__c2, __one))
1445 __tmp.push_back(__one);
1447 eq_int_type(__is.rdbuf()->sputbackc(__c2),
1450 __state |= __ios_base::failbit;
1456 __catch(__cxxabiv1::__forced_unwind&)
1458 __is._M_setstate(__ios_base::badbit);
1459 __throw_exception_again;
1462 { __is._M_setstate(__ios_base::badbit); }
1465 if (__tmp.empty() && _Nb)
1466 __state |= __ios_base::failbit;
1468 __x._M_copy_from_string(__tmp, static_cast<size_t>(0), _Nb,
1471 __is.setstate(__state);
1475 template <class _CharT, class _Traits, size_t _Nb>
1476 std::basic_ostream<_CharT, _Traits>&
1477 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1478 const bitset<_Nb>& __x)
1480 std::basic_string<_CharT, _Traits> __tmp;
1482 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1483 // 396. what are characters zero and one.
1484 const ctype<_CharT>& __ct = use_facet<ctype<_CharT> >(__os.getloc());
1485 __x._M_copy_to_string(__tmp, __ct.widen('0'), __ct.widen('1'));
1486 return __os << __tmp;
1490 _GLIBCXX_END_NESTED_NAMESPACE
1492 #undef _GLIBCXX_BITSET_WORDS
1493 #undef _GLIBCXX_BITSET_BITS_PER_WORD
1495 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1499 /// std::hash specialization for bitset.
1500 template<size_t _Nb>
1501 struct hash<_GLIBCXX_STD_D::bitset<_Nb>>
1502 : public std::unary_function<_GLIBCXX_STD_D::bitset<_Nb>, size_t>
1505 operator()(const _GLIBCXX_STD_D::bitset<_Nb>& __b) const
1507 const size_t __size = (_Nb + __CHAR_BIT__ - 1) / __CHAR_BIT__;
1508 return std::_Fnv_hash::hash(__b._M_getdata(), __size);
1512 #endif // __GXX_EXPERIMENTAL_CXX0X__
1514 #ifdef _GLIBCXX_DEBUG
1515 # include <debug/bitset>
1518 #ifdef _GLIBCXX_PROFILE
1519 # include <profile/bitset>
1522 #endif /* _GLIBCXX_BITSET */