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(unsigned 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 namespace std _GLIBCXX_VISIBILITY(default)
62 _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
65 * Base class, general case. It is a class invariant that _Nw will be
68 * See documentation for bitset.
73 typedef unsigned long _WordT;
75 /// 0 is the least significant word.
78 _GLIBCXX_CONSTEXPR _Base_bitset()
81 #ifdef __GXX_EXPERIMENTAL_CXX0X__
82 constexpr _Base_bitset(unsigned long long __val)
83 : _M_w({ _WordT(__val)
84 #if __SIZEOF_LONG_LONG__ > __SIZEOF_LONG__
85 , _WordT(__val >> _GLIBCXX_BITSET_BITS_PER_WORD)
89 _Base_bitset(unsigned long __val)
94 static _GLIBCXX_CONSTEXPR size_t
95 _S_whichword(size_t __pos )
96 { return __pos / _GLIBCXX_BITSET_BITS_PER_WORD; }
98 static _GLIBCXX_CONSTEXPR size_t
99 _S_whichbyte(size_t __pos )
100 { return (__pos % _GLIBCXX_BITSET_BITS_PER_WORD) / __CHAR_BIT__; }
102 static _GLIBCXX_CONSTEXPR size_t
103 _S_whichbit(size_t __pos )
104 { return __pos % _GLIBCXX_BITSET_BITS_PER_WORD; }
106 static _GLIBCXX_CONSTEXPR _WordT
107 _S_maskbit(size_t __pos )
108 { return (static_cast<_WordT>(1)) << _S_whichbit(__pos); }
111 _M_getword(size_t __pos)
112 { return _M_w[_S_whichword(__pos)]; }
115 _M_getword(size_t __pos) const
116 { return _M_w[_S_whichword(__pos)]; }
118 #ifdef __GXX_EXPERIMENTAL_CXX0X__
126 { return _M_w[_Nw - 1]; }
128 _GLIBCXX_CONSTEXPR _WordT
130 { return _M_w[_Nw - 1]; }
133 _M_do_and(const _Base_bitset<_Nw>& __x)
135 for (size_t __i = 0; __i < _Nw; __i++)
136 _M_w[__i] &= __x._M_w[__i];
140 _M_do_or(const _Base_bitset<_Nw>& __x)
142 for (size_t __i = 0; __i < _Nw; __i++)
143 _M_w[__i] |= __x._M_w[__i];
147 _M_do_xor(const _Base_bitset<_Nw>& __x)
149 for (size_t __i = 0; __i < _Nw; __i++)
150 _M_w[__i] ^= __x._M_w[__i];
154 _M_do_left_shift(size_t __shift);
157 _M_do_right_shift(size_t __shift);
162 for (size_t __i = 0; __i < _Nw; __i++)
163 _M_w[__i] = ~_M_w[__i];
169 for (size_t __i = 0; __i < _Nw; __i++)
170 _M_w[__i] = ~static_cast<_WordT>(0);
175 { __builtin_memset(_M_w, 0, _Nw * sizeof(_WordT)); }
178 _M_is_equal(const _Base_bitset<_Nw>& __x) const
180 for (size_t __i = 0; __i < _Nw; ++__i)
181 if (_M_w[__i] != __x._M_w[__i])
187 _M_are_all_aux() const
189 for (size_t __i = 0; __i < _Nw - 1; __i++)
190 if (_M_w[__i] != ~static_cast<_WordT>(0))
192 return ((_Nw - 1) * _GLIBCXX_BITSET_BITS_PER_WORD
193 + __builtin_popcountl(_M_hiword()));
199 for (size_t __i = 0; __i < _Nw; __i++)
200 if (_M_w[__i] != static_cast<_WordT>(0))
209 for (size_t __i = 0; __i < _Nw; __i++)
210 __result += __builtin_popcountl(_M_w[__i]);
215 _M_do_to_ulong() const;
217 #ifdef __GXX_EXPERIMENTAL_CXX0X__
219 _M_do_to_ullong() const;
222 // find first "on" bit
224 _M_do_find_first(size_t __not_found) const;
226 // find the next "on" bit that follows "prev"
228 _M_do_find_next(size_t __prev, size_t __not_found) const;
231 // Definitions of non-inline functions from _Base_bitset.
234 _Base_bitset<_Nw>::_M_do_left_shift(size_t __shift)
236 if (__builtin_expect(__shift != 0, 1))
238 const size_t __wshift = __shift / _GLIBCXX_BITSET_BITS_PER_WORD;
239 const size_t __offset = __shift % _GLIBCXX_BITSET_BITS_PER_WORD;
242 for (size_t __n = _Nw - 1; __n >= __wshift; --__n)
243 _M_w[__n] = _M_w[__n - __wshift];
246 const size_t __sub_offset = (_GLIBCXX_BITSET_BITS_PER_WORD
248 for (size_t __n = _Nw - 1; __n > __wshift; --__n)
249 _M_w[__n] = ((_M_w[__n - __wshift] << __offset)
250 | (_M_w[__n - __wshift - 1] >> __sub_offset));
251 _M_w[__wshift] = _M_w[0] << __offset;
254 std::fill(_M_w + 0, _M_w + __wshift, static_cast<_WordT>(0));
260 _Base_bitset<_Nw>::_M_do_right_shift(size_t __shift)
262 if (__builtin_expect(__shift != 0, 1))
264 const size_t __wshift = __shift / _GLIBCXX_BITSET_BITS_PER_WORD;
265 const size_t __offset = __shift % _GLIBCXX_BITSET_BITS_PER_WORD;
266 const size_t __limit = _Nw - __wshift - 1;
269 for (size_t __n = 0; __n <= __limit; ++__n)
270 _M_w[__n] = _M_w[__n + __wshift];
273 const size_t __sub_offset = (_GLIBCXX_BITSET_BITS_PER_WORD
275 for (size_t __n = 0; __n < __limit; ++__n)
276 _M_w[__n] = ((_M_w[__n + __wshift] >> __offset)
277 | (_M_w[__n + __wshift + 1] << __sub_offset));
278 _M_w[__limit] = _M_w[_Nw-1] >> __offset;
281 std::fill(_M_w + __limit + 1, _M_w + _Nw, static_cast<_WordT>(0));
287 _Base_bitset<_Nw>::_M_do_to_ulong() const
289 for (size_t __i = 1; __i < _Nw; ++__i)
291 __throw_overflow_error(__N("_Base_bitset::_M_do_to_ulong"));
295 #ifdef __GXX_EXPERIMENTAL_CXX0X__
298 _Base_bitset<_Nw>::_M_do_to_ullong() const
300 const bool __dw = sizeof(unsigned long long) > sizeof(unsigned long);
301 for (size_t __i = 1 + __dw; __i < _Nw; ++__i)
303 __throw_overflow_error(__N("_Base_bitset::_M_do_to_ullong"));
306 return _M_w[0] + (static_cast<unsigned long long>(_M_w[1])
307 << _GLIBCXX_BITSET_BITS_PER_WORD);
314 _Base_bitset<_Nw>::_M_do_find_first(size_t __not_found) const
316 for (size_t __i = 0; __i < _Nw; __i++)
318 _WordT __thisword = _M_w[__i];
319 if (__thisword != static_cast<_WordT>(0))
320 return (__i * _GLIBCXX_BITSET_BITS_PER_WORD
321 + __builtin_ctzl(__thisword));
323 // not found, so return an indication of failure.
329 _Base_bitset<_Nw>::_M_do_find_next(size_t __prev, size_t __not_found) const
331 // make bound inclusive
334 // check out of bounds
335 if (__prev >= _Nw * _GLIBCXX_BITSET_BITS_PER_WORD)
339 size_t __i = _S_whichword(__prev);
340 _WordT __thisword = _M_w[__i];
342 // mask off bits below bound
343 __thisword &= (~static_cast<_WordT>(0)) << _S_whichbit(__prev);
345 if (__thisword != static_cast<_WordT>(0))
346 return (__i * _GLIBCXX_BITSET_BITS_PER_WORD
347 + __builtin_ctzl(__thisword));
349 // check subsequent words
351 for (; __i < _Nw; __i++)
353 __thisword = _M_w[__i];
354 if (__thisword != static_cast<_WordT>(0))
355 return (__i * _GLIBCXX_BITSET_BITS_PER_WORD
356 + __builtin_ctzl(__thisword));
358 // not found, so return an indication of failure.
360 } // end _M_do_find_next
363 * Base class, specialization for a single word.
365 * See documentation for bitset.
368 struct _Base_bitset<1>
370 typedef unsigned long _WordT;
373 _GLIBCXX_CONSTEXPR _Base_bitset()
377 #ifdef __GXX_EXPERIMENTAL_CXX0X__
378 constexpr _Base_bitset(unsigned long long __val)
380 _Base_bitset(unsigned long __val)
385 static _GLIBCXX_CONSTEXPR size_t
386 _S_whichword(size_t __pos )
387 { return __pos / _GLIBCXX_BITSET_BITS_PER_WORD; }
389 static _GLIBCXX_CONSTEXPR size_t
390 _S_whichbyte(size_t __pos )
391 { return (__pos % _GLIBCXX_BITSET_BITS_PER_WORD) / __CHAR_BIT__; }
393 static _GLIBCXX_CONSTEXPR size_t
394 _S_whichbit(size_t __pos )
395 { return __pos % _GLIBCXX_BITSET_BITS_PER_WORD; }
397 static _GLIBCXX_CONSTEXPR _WordT
398 _S_maskbit(size_t __pos )
399 { return (static_cast<_WordT>(1)) << _S_whichbit(__pos); }
406 _M_getword(size_t) const
409 #ifdef __GXX_EXPERIMENTAL_CXX0X__
419 _GLIBCXX_CONSTEXPR _WordT
424 _M_do_and(const _Base_bitset<1>& __x)
425 { _M_w &= __x._M_w; }
428 _M_do_or(const _Base_bitset<1>& __x)
429 { _M_w |= __x._M_w; }
432 _M_do_xor(const _Base_bitset<1>& __x)
433 { _M_w ^= __x._M_w; }
436 _M_do_left_shift(size_t __shift)
437 { _M_w <<= __shift; }
440 _M_do_right_shift(size_t __shift)
441 { _M_w >>= __shift; }
449 { _M_w = ~static_cast<_WordT>(0); }
456 _M_is_equal(const _Base_bitset<1>& __x) const
457 { return _M_w == __x._M_w; }
460 _M_are_all_aux() const
461 { return __builtin_popcountl(_M_w); }
465 { return _M_w != 0; }
469 { return __builtin_popcountl(_M_w); }
472 _M_do_to_ulong() const
475 #ifdef __GXX_EXPERIMENTAL_CXX0X__
477 _M_do_to_ullong() const
482 _M_do_find_first(size_t __not_found) const
485 return __builtin_ctzl(_M_w);
490 // find the next "on" bit that follows "prev"
492 _M_do_find_next(size_t __prev, size_t __not_found) const
495 if (__prev >= ((size_t) _GLIBCXX_BITSET_BITS_PER_WORD))
498 _WordT __x = _M_w >> __prev;
500 return __builtin_ctzl(__x) + __prev;
507 * Base class, specialization for no storage (zero-length %bitset).
509 * See documentation for bitset.
512 struct _Base_bitset<0>
514 typedef unsigned long _WordT;
516 _GLIBCXX_CONSTEXPR _Base_bitset()
519 #ifdef __GXX_EXPERIMENTAL_CXX0X__
520 constexpr _Base_bitset(unsigned long long)
522 _Base_bitset(unsigned long)
526 static _GLIBCXX_CONSTEXPR size_t
527 _S_whichword(size_t __pos )
528 { return __pos / _GLIBCXX_BITSET_BITS_PER_WORD; }
530 static _GLIBCXX_CONSTEXPR size_t
531 _S_whichbyte(size_t __pos )
532 { return (__pos % _GLIBCXX_BITSET_BITS_PER_WORD) / __CHAR_BIT__; }
534 static _GLIBCXX_CONSTEXPR size_t
535 _S_whichbit(size_t __pos )
536 { return __pos % _GLIBCXX_BITSET_BITS_PER_WORD; }
538 static _GLIBCXX_CONSTEXPR _WordT
539 _S_maskbit(size_t __pos )
540 { return (static_cast<_WordT>(1)) << _S_whichbit(__pos); }
542 // This would normally give access to the data. The bounds-checking
543 // in the bitset class will prevent the user from getting this far,
544 // but (1) it must still return an lvalue to compile, and (2) the
545 // user might call _Unchecked_set directly, in which case this /needs/
546 // to fail. Let's not penalize zero-length users unless they actually
547 // make an unchecked call; all the memory ugliness is therefore
548 // localized to this single should-never-get-this-far function.
552 __throw_out_of_range(__N("_Base_bitset::_M_getword"));
557 _M_getword(size_t __pos) const
560 _GLIBCXX_CONSTEXPR _WordT
565 _M_do_and(const _Base_bitset<0>&)
569 _M_do_or(const _Base_bitset<0>&)
573 _M_do_xor(const _Base_bitset<0>&)
577 _M_do_left_shift(size_t)
581 _M_do_right_shift(size_t)
596 // Are all empty bitsets equal to each other? Are they equal to
597 // themselves? How to compare a thing which has no state? What is
598 // the sound of one zero-length bitset clapping?
600 _M_is_equal(const _Base_bitset<0>&) const
604 _M_are_all_aux() const
616 _M_do_to_ulong() const
619 #ifdef __GXX_EXPERIMENTAL_CXX0X__
621 _M_do_to_ullong() const
625 // Normally "not found" is the size, but that could also be
626 // misinterpreted as an index in this corner case. Oh well.
628 _M_do_find_first(size_t) const
632 _M_do_find_next(size_t, size_t) const
637 // Helper class to zero out the unused high-order bits in the highest word.
638 template<size_t _Extrabits>
641 typedef unsigned long _WordT;
644 _S_do_sanitize(_WordT& __val)
645 { __val &= ~((~static_cast<_WordT>(0)) << _Extrabits); }
651 typedef unsigned long _WordT;
654 _S_do_sanitize(_WordT) { }
658 * @brief The %bitset class represents a @e fixed-size sequence of bits.
660 * @ingroup containers
662 * (Note that %bitset does @e not meet the formal requirements of a
663 * <a href="tables.html#65">container</a>. Mainly, it lacks iterators.)
665 * The template argument, @a Nb, may be any non-negative number,
666 * specifying the number of bits (e.g., "0", "12", "1024*1024").
668 * In the general unoptimized case, storage is allocated in word-sized
669 * blocks. Let B be the number of bits in a word, then (Nb+(B-1))/B
670 * words will be used for storage. B - Nb%B bits are unused. (They are
671 * the high-order bits in the highest word.) It is a class invariant
672 * that those unused bits are always zero.
674 * If you think of %bitset as <em>a simple array of bits</em>, be
675 * aware that your mental picture is reversed: a %bitset behaves
676 * the same way as bits in integers do, with the bit at index 0 in
677 * the <em>least significant / right-hand</em> position, and the bit at
678 * index Nb-1 in the <em>most significant / left-hand</em> position.
679 * Thus, unlike other containers, a %bitset's index <em>counts from
680 * right to left</em>, to put it very loosely.
682 * This behavior is preserved when translating to and from strings. For
683 * example, the first line of the following program probably prints
684 * <em>b('a') is 0001100001</em> on a modern ASCII system.
688 * #include <iostream>
691 * using namespace std;
698 * cout << "b('a') is " << b << endl;
702 * string str = s.str();
703 * cout << "index 3 in the string is " << str[3] << " but\n"
704 * << "index 3 in the bitset is " << b[3] << endl;
709 * http://gcc.gnu.org/onlinedocs/libstdc++/manual/bk01pt12ch33s02.html
710 * for a description of extensions.
712 * Most of the actual code isn't contained in %bitset<> itself, but in the
713 * base class _Base_bitset. The base class works with whole words, not with
714 * individual bits. This allows us to specialize _Base_bitset for the
715 * important special case where the %bitset is only a single word.
717 * Extra confusion can result due to the fact that the storage for
718 * _Base_bitset @e is a regular array, and is indexed as such. This is
719 * carefully encapsulated.
723 : private _Base_bitset<_GLIBCXX_BITSET_WORDS(_Nb)>
726 typedef _Base_bitset<_GLIBCXX_BITSET_WORDS(_Nb)> _Base;
727 typedef unsigned long _WordT;
732 typedef _Sanitize<_Nb % _GLIBCXX_BITSET_BITS_PER_WORD> __sanitize_type;
733 __sanitize_type::_S_do_sanitize(this->_M_hiword());
736 #ifdef __GXX_EXPERIMENTAL_CXX0X__
737 template<typename> friend class hash;
742 * This encapsulates the concept of a single bit. An instance of this
743 * class is a proxy for an actual bit; this way the individual bit
744 * operations are done as faster word-size bitwise instructions.
746 * Most users will never need to use this class directly; conversions
747 * to and from bool are automatic and should be transparent. Overloaded
748 * operators help to preserve the illusion.
750 * (On a typical system, this <em>bit %reference</em> is 64
751 * times the size of an actual bit. Ha.)
764 reference(bitset& __b, size_t __pos)
766 _M_wp = &__b._M_getword(__pos);
767 _M_bpos = _Base::_S_whichbit(__pos);
778 *_M_wp |= _Base::_S_maskbit(_M_bpos);
780 *_M_wp &= ~_Base::_S_maskbit(_M_bpos);
784 // For b[i] = b[__j];
786 operator=(const reference& __j)
788 if ((*(__j._M_wp) & _Base::_S_maskbit(__j._M_bpos)))
789 *_M_wp |= _Base::_S_maskbit(_M_bpos);
791 *_M_wp &= ~_Base::_S_maskbit(_M_bpos);
798 { return (*(_M_wp) & _Base::_S_maskbit(_M_bpos)) == 0; }
801 operator bool() const
802 { return (*(_M_wp) & _Base::_S_maskbit(_M_bpos)) != 0; }
808 *_M_wp ^= _Base::_S_maskbit(_M_bpos);
812 friend class reference;
814 // 23.3.5.1 constructors:
815 /// All bits set to zero.
816 _GLIBCXX_CONSTEXPR bitset()
819 /// Initial bits bitwise-copied from a single word (others set to zero).
820 #ifdef __GXX_EXPERIMENTAL_CXX0X__
821 constexpr bitset(unsigned long long __val)
824 bitset(unsigned long __val)
826 { _M_do_sanitize(); }
830 * @brief Use a subset of a string.
831 * @param s A string of @a 0 and @a 1 characters.
832 * @param position Index of the first character in @a s to use;
834 * @throw std::out_of_range If @a pos is bigger the size of @a s.
835 * @throw std::invalid_argument If a character appears in the string
836 * which is neither @a 0 nor @a 1.
838 template<class _CharT, class _Traits, class _Alloc>
840 bitset(const std::basic_string<_CharT, _Traits, _Alloc>& __s,
841 size_t __position = 0)
844 if (__position > __s.size())
845 __throw_out_of_range(__N("bitset::bitset initial position "
847 _M_copy_from_string(__s, __position,
848 std::basic_string<_CharT, _Traits, _Alloc>::npos,
849 _CharT('0'), _CharT('1'));
853 * @brief Use a subset of a string.
854 * @param s A string of @a 0 and @a 1 characters.
855 * @param position Index of the first character in @a s to use.
856 * @param n The number of characters to copy.
857 * @throw std::out_of_range If @a pos is bigger the size of @a s.
858 * @throw std::invalid_argument If a character appears in the string
859 * which is neither @a 0 nor @a 1.
861 template<class _CharT, class _Traits, class _Alloc>
862 bitset(const std::basic_string<_CharT, _Traits, _Alloc>& __s,
863 size_t __position, size_t __n)
866 if (__position > __s.size())
867 __throw_out_of_range(__N("bitset::bitset initial position "
869 _M_copy_from_string(__s, __position, __n, _CharT('0'), _CharT('1'));
872 // _GLIBCXX_RESOLVE_LIB_DEFECTS
873 // 396. what are characters zero and one.
874 template<class _CharT, class _Traits, class _Alloc>
875 bitset(const std::basic_string<_CharT, _Traits, _Alloc>& __s,
876 size_t __position, size_t __n,
877 _CharT __zero, _CharT __one = _CharT('1'))
880 if (__position > __s.size())
881 __throw_out_of_range(__N("bitset::bitset initial position "
883 _M_copy_from_string(__s, __position, __n, __zero, __one);
886 #ifdef __GXX_EXPERIMENTAL_CXX0X__
888 * @brief Construct from a character %array.
889 * @param str An %array of characters @a zero and @a one.
890 * @param n The number of characters to use.
891 * @param zero The character corresponding to the value 0.
892 * @param one The character corresponding to the value 1.
893 * @throw std::invalid_argument If a character appears in the string
894 * which is neither @a zero nor @a one.
896 template<typename _CharT>
898 bitset(const _CharT* __str,
899 typename std::basic_string<_CharT>::size_type __n
900 = std::basic_string<_CharT>::npos,
901 _CharT __zero = _CharT('0'), _CharT __one = _CharT('1'))
905 __throw_logic_error(__N("bitset::bitset(const _CharT*, ...)"));
907 if (__n == std::basic_string<_CharT>::npos)
908 __n = std::char_traits<_CharT>::length(__str);
909 _M_copy_from_ptr<_CharT, std::char_traits<_CharT>>(__str, __n, 0,
915 // 23.3.5.2 bitset operations:
918 * @brief Operations on bitsets.
919 * @param rhs A same-sized bitset.
921 * These should be self-explanatory.
924 operator&=(const bitset<_Nb>& __rhs)
926 this->_M_do_and(__rhs);
931 operator|=(const bitset<_Nb>& __rhs)
933 this->_M_do_or(__rhs);
938 operator^=(const bitset<_Nb>& __rhs)
940 this->_M_do_xor(__rhs);
947 * @brief Operations on bitsets.
948 * @param position The number of places to shift.
950 * These should be self-explanatory.
953 operator<<=(size_t __position)
955 if (__builtin_expect(__position < _Nb, 1))
957 this->_M_do_left_shift(__position);
958 this->_M_do_sanitize();
966 operator>>=(size_t __position)
968 if (__builtin_expect(__position < _Nb, 1))
970 this->_M_do_right_shift(__position);
971 this->_M_do_sanitize();
981 * These versions of single-bit set, reset, flip, and test are
982 * extensions from the SGI version. They do no range checking.
983 * @ingroup SGIextensions
986 _Unchecked_set(size_t __pos)
988 this->_M_getword(__pos) |= _Base::_S_maskbit(__pos);
993 _Unchecked_set(size_t __pos, int __val)
996 this->_M_getword(__pos) |= _Base::_S_maskbit(__pos);
998 this->_M_getword(__pos) &= ~_Base::_S_maskbit(__pos);
1003 _Unchecked_reset(size_t __pos)
1005 this->_M_getword(__pos) &= ~_Base::_S_maskbit(__pos);
1010 _Unchecked_flip(size_t __pos)
1012 this->_M_getword(__pos) ^= _Base::_S_maskbit(__pos);
1017 _Unchecked_test(size_t __pos) const
1018 { return ((this->_M_getword(__pos) & _Base::_S_maskbit(__pos))
1019 != static_cast<_WordT>(0)); }
1022 // Set, reset, and flip.
1024 * @brief Sets every bit to true.
1030 this->_M_do_sanitize();
1035 * @brief Sets a given bit to a particular value.
1036 * @param position The index of the bit.
1037 * @param val Either true or false, defaults to true.
1038 * @throw std::out_of_range If @a pos is bigger the size of the %set.
1041 set(size_t __position, bool __val = true)
1043 if (__position >= _Nb)
1044 __throw_out_of_range(__N("bitset::set"));
1045 return _Unchecked_set(__position, __val);
1049 * @brief Sets every bit to false.
1054 this->_M_do_reset();
1059 * @brief Sets a given bit to false.
1060 * @param position The index of the bit.
1061 * @throw std::out_of_range If @a pos is bigger the size of the %set.
1063 * Same as writing @c set(pos,false).
1066 reset(size_t __position)
1068 if (__position >= _Nb)
1069 __throw_out_of_range(__N("bitset::reset"));
1070 return _Unchecked_reset(__position);
1074 * @brief Toggles every bit to its opposite value.
1080 this->_M_do_sanitize();
1085 * @brief Toggles a given bit to its opposite value.
1086 * @param position The index of the bit.
1087 * @throw std::out_of_range If @a pos is bigger the size of the %set.
1090 flip(size_t __position)
1092 if (__position >= _Nb)
1093 __throw_out_of_range(__N("bitset::flip"));
1094 return _Unchecked_flip(__position);
1097 /// See the no-argument flip().
1100 { return bitset<_Nb>(*this).flip(); }
1104 * @brief Array-indexing support.
1105 * @param position Index into the %bitset.
1106 * @return A bool for a <em>const %bitset</em>. For non-const
1107 * bitsets, an instance of the reference proxy class.
1108 * @note These operators do no range checking and throw no exceptions,
1109 * as required by DR 11 to the standard.
1111 * _GLIBCXX_RESOLVE_LIB_DEFECTS Note that this implementation already
1112 * resolves DR 11 (items 1 and 2), but does not do the range-checking
1113 * required by that DR's resolution. -pme
1114 * The DR has since been changed: range-checking is a precondition
1115 * (users' responsibility), and these functions must not throw. -pme
1118 operator[](size_t __position)
1119 { return reference(*this, __position); }
1122 operator[](size_t __position) const
1123 { return _Unchecked_test(__position); }
1127 * @brief Returns a numerical interpretation of the %bitset.
1128 * @return The integral equivalent of the bits.
1129 * @throw std::overflow_error If there are too many bits to be
1130 * represented in an @c unsigned @c long.
1134 { return this->_M_do_to_ulong(); }
1136 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1139 { return this->_M_do_to_ullong(); }
1143 * @brief Returns a character interpretation of the %bitset.
1144 * @return The string equivalent of the bits.
1146 * Note the ordering of the bits: decreasing character positions
1147 * correspond to increasing bit positions (see the main class notes for
1150 template<class _CharT, class _Traits, class _Alloc>
1151 std::basic_string<_CharT, _Traits, _Alloc>
1154 std::basic_string<_CharT, _Traits, _Alloc> __result;
1155 _M_copy_to_string(__result, _CharT('0'), _CharT('1'));
1159 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1160 // 396. what are characters zero and one.
1161 template<class _CharT, class _Traits, class _Alloc>
1162 std::basic_string<_CharT, _Traits, _Alloc>
1163 to_string(_CharT __zero, _CharT __one = _CharT('1')) const
1165 std::basic_string<_CharT, _Traits, _Alloc> __result;
1166 _M_copy_to_string(__result, __zero, __one);
1170 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1171 // 434. bitset::to_string() hard to use.
1172 template<class _CharT, class _Traits>
1173 std::basic_string<_CharT, _Traits, std::allocator<_CharT> >
1175 { return to_string<_CharT, _Traits, std::allocator<_CharT> >(); }
1177 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1178 // 853. to_string needs updating with zero and one.
1179 template<class _CharT, class _Traits>
1180 std::basic_string<_CharT, _Traits, std::allocator<_CharT> >
1181 to_string(_CharT __zero, _CharT __one = _CharT('1')) const
1182 { return to_string<_CharT, _Traits,
1183 std::allocator<_CharT> >(__zero, __one); }
1185 template<class _CharT>
1186 std::basic_string<_CharT, std::char_traits<_CharT>,
1187 std::allocator<_CharT> >
1190 return to_string<_CharT, std::char_traits<_CharT>,
1191 std::allocator<_CharT> >();
1194 template<class _CharT>
1195 std::basic_string<_CharT, std::char_traits<_CharT>,
1196 std::allocator<_CharT> >
1197 to_string(_CharT __zero, _CharT __one = _CharT('1')) const
1199 return to_string<_CharT, std::char_traits<_CharT>,
1200 std::allocator<_CharT> >(__zero, __one);
1203 std::basic_string<char, std::char_traits<char>, std::allocator<char> >
1206 return to_string<char, std::char_traits<char>,
1207 std::allocator<char> >();
1210 std::basic_string<char, std::char_traits<char>, std::allocator<char> >
1211 to_string(char __zero, char __one = '1') const
1213 return to_string<char, std::char_traits<char>,
1214 std::allocator<char> >(__zero, __one);
1217 // Helper functions for string operations.
1218 template<class _CharT, class _Traits>
1220 _M_copy_from_ptr(const _CharT*, size_t, size_t, size_t,
1223 template<class _CharT, class _Traits, class _Alloc>
1225 _M_copy_from_string(const std::basic_string<_CharT,
1226 _Traits, _Alloc>& __s, size_t __pos, size_t __n,
1227 _CharT __zero, _CharT __one)
1228 { _M_copy_from_ptr<_CharT, _Traits>(__s.data(), __s.size(), __pos, __n,
1231 template<class _CharT, class _Traits, class _Alloc>
1233 _M_copy_to_string(std::basic_string<_CharT, _Traits, _Alloc>&,
1234 _CharT, _CharT) const;
1236 // NB: Backward compat.
1237 template<class _CharT, class _Traits, class _Alloc>
1239 _M_copy_from_string(const std::basic_string<_CharT,
1240 _Traits, _Alloc>& __s, size_t __pos, size_t __n)
1241 { _M_copy_from_string(__s, __pos, __n, _CharT('0'), _CharT('1')); }
1243 template<class _CharT, class _Traits, class _Alloc>
1245 _M_copy_to_string(std::basic_string<_CharT, _Traits,_Alloc>& __s) const
1246 { _M_copy_to_string(__s, _CharT('0'), _CharT('1')); }
1248 /// Returns the number of bits which are set.
1251 { return this->_M_do_count(); }
1253 /// Returns the total number of bits.
1254 _GLIBCXX_CONSTEXPR size_t
1259 /// These comparisons for equality/inequality are, well, @e bitwise.
1261 operator==(const bitset<_Nb>& __rhs) const
1262 { return this->_M_is_equal(__rhs); }
1265 operator!=(const bitset<_Nb>& __rhs) const
1266 { return !this->_M_is_equal(__rhs); }
1270 * @brief Tests the value of a bit.
1271 * @param position The index of a bit.
1272 * @return The value at @a pos.
1273 * @throw std::out_of_range If @a pos is bigger the size of the %set.
1276 test(size_t __position) const
1278 if (__position >= _Nb)
1279 __throw_out_of_range(__N("bitset::test"));
1280 return _Unchecked_test(__position);
1283 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1284 // DR 693. std::bitset::all() missing.
1286 * @brief Tests whether all the bits are on.
1287 * @return True if all the bits are set.
1291 { return this->_M_are_all_aux() == _Nb; }
1294 * @brief Tests whether any of the bits are on.
1295 * @return True if at least one bit is set.
1299 { return this->_M_is_any(); }
1302 * @brief Tests whether any of the bits are on.
1303 * @return True if none of the bits are set.
1307 { return !this->_M_is_any(); }
1310 /// Self-explanatory.
1312 operator<<(size_t __position) const
1313 { return bitset<_Nb>(*this) <<= __position; }
1316 operator>>(size_t __position) const
1317 { return bitset<_Nb>(*this) >>= __position; }
1321 * @brief Finds the index of the first "on" bit.
1322 * @return The index of the first bit set, or size() if not found.
1323 * @ingroup SGIextensions
1328 { return this->_M_do_find_first(_Nb); }
1331 * @brief Finds the index of the next "on" bit after prev.
1332 * @return The index of the next bit set, or size() if not found.
1333 * @param prev Where to start searching.
1334 * @ingroup SGIextensions
1338 _Find_next(size_t __prev ) const
1339 { return this->_M_do_find_next(__prev, _Nb); }
1342 // Definitions of non-inline member functions.
1343 template<size_t _Nb>
1344 template<class _CharT, class _Traits>
1347 _M_copy_from_ptr(const _CharT* __s, size_t __len,
1348 size_t __pos, size_t __n, _CharT __zero, _CharT __one)
1351 const size_t __nbits = std::min(_Nb, std::min(__n, __len - __pos));
1352 for (size_t __i = __nbits; __i > 0; --__i)
1354 const _CharT __c = __s[__pos + __nbits - __i];
1355 if (_Traits::eq(__c, __zero))
1357 else if (_Traits::eq(__c, __one))
1358 _Unchecked_set(__i - 1);
1360 __throw_invalid_argument(__N("bitset::_M_copy_from_ptr"));
1364 template<size_t _Nb>
1365 template<class _CharT, class _Traits, class _Alloc>
1368 _M_copy_to_string(std::basic_string<_CharT, _Traits, _Alloc>& __s,
1369 _CharT __zero, _CharT __one) const
1371 __s.assign(_Nb, __zero);
1372 for (size_t __i = _Nb; __i > 0; --__i)
1373 if (_Unchecked_test(__i - 1))
1374 _Traits::assign(__s[_Nb - __i], __one);
1377 // 23.3.5.3 bitset operations:
1380 * @brief Global bitwise operations on bitsets.
1381 * @param x A bitset.
1382 * @param y A bitset of the same size as @a x.
1383 * @return A new bitset.
1385 * These should be self-explanatory.
1387 template<size_t _Nb>
1389 operator&(const bitset<_Nb>& __x, const bitset<_Nb>& __y)
1391 bitset<_Nb> __result(__x);
1396 template<size_t _Nb>
1398 operator|(const bitset<_Nb>& __x, const bitset<_Nb>& __y)
1400 bitset<_Nb> __result(__x);
1405 template <size_t _Nb>
1407 operator^(const bitset<_Nb>& __x, const bitset<_Nb>& __y)
1409 bitset<_Nb> __result(__x);
1417 * @brief Global I/O operators for bitsets.
1419 * Direct I/O between streams and bitsets is supported. Output is
1420 * straightforward. Input will skip whitespace, only accept @a 0 and @a 1
1421 * characters, and will only extract as many digits as the %bitset will
1424 template<class _CharT, class _Traits, size_t _Nb>
1425 std::basic_istream<_CharT, _Traits>&
1426 operator>>(std::basic_istream<_CharT, _Traits>& __is, bitset<_Nb>& __x)
1428 typedef typename _Traits::char_type char_type;
1429 typedef std::basic_istream<_CharT, _Traits> __istream_type;
1430 typedef typename __istream_type::ios_base __ios_base;
1432 std::basic_string<_CharT, _Traits> __tmp;
1435 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1436 // 303. Bitset input operator underspecified
1437 const char_type __zero = __is.widen('0');
1438 const char_type __one = __is.widen('1');
1440 typename __ios_base::iostate __state = __ios_base::goodbit;
1441 typename __istream_type::sentry __sentry(__is);
1446 for (size_t __i = _Nb; __i > 0; --__i)
1448 static typename _Traits::int_type __eof = _Traits::eof();
1450 typename _Traits::int_type __c1 = __is.rdbuf()->sbumpc();
1451 if (_Traits::eq_int_type(__c1, __eof))
1453 __state |= __ios_base::eofbit;
1458 const char_type __c2 = _Traits::to_char_type(__c1);
1459 if (_Traits::eq(__c2, __zero))
1460 __tmp.push_back(__zero);
1461 else if (_Traits::eq(__c2, __one))
1462 __tmp.push_back(__one);
1464 eq_int_type(__is.rdbuf()->sputbackc(__c2),
1467 __state |= __ios_base::failbit;
1473 __catch(__cxxabiv1::__forced_unwind&)
1475 __is._M_setstate(__ios_base::badbit);
1476 __throw_exception_again;
1479 { __is._M_setstate(__ios_base::badbit); }
1482 if (__tmp.empty() && _Nb)
1483 __state |= __ios_base::failbit;
1485 __x._M_copy_from_string(__tmp, static_cast<size_t>(0), _Nb,
1488 __is.setstate(__state);
1492 template <class _CharT, class _Traits, size_t _Nb>
1493 std::basic_ostream<_CharT, _Traits>&
1494 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1495 const bitset<_Nb>& __x)
1497 std::basic_string<_CharT, _Traits> __tmp;
1499 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1500 // 396. what are characters zero and one.
1501 const ctype<_CharT>& __ct = use_facet<ctype<_CharT> >(__os.getloc());
1502 __x._M_copy_to_string(__tmp, __ct.widen('0'), __ct.widen('1'));
1503 return __os << __tmp;
1507 _GLIBCXX_END_NAMESPACE_CONTAINER
1510 #undef _GLIBCXX_BITSET_WORDS
1511 #undef _GLIBCXX_BITSET_BITS_PER_WORD
1513 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1515 #include <bits/functional_hash.h>
1517 namespace std _GLIBCXX_VISIBILITY(default)
1519 _GLIBCXX_BEGIN_NAMESPACE_VERSION
1522 /// std::hash specialization for bitset.
1523 template<size_t _Nb>
1524 struct hash<_GLIBCXX_STD_C::bitset<_Nb>>
1525 : public __hash_base<size_t, _GLIBCXX_STD_C::bitset<_Nb>>
1528 operator()(const _GLIBCXX_STD_C::bitset<_Nb>& __b) const
1530 const size_t __clength = (_Nb + __CHAR_BIT__ - 1) / __CHAR_BIT__;
1531 return std::_Hash_impl::hash(__b._M_getdata(), __clength);
1536 struct hash<_GLIBCXX_STD_C::bitset<0>>
1537 : public __hash_base<size_t, _GLIBCXX_STD_C::bitset<0>>
1540 operator()(const _GLIBCXX_STD_C::bitset<0>&) const
1544 _GLIBCXX_END_NAMESPACE_VERSION
1547 #endif // __GXX_EXPERIMENTAL_CXX0X__
1549 #ifdef _GLIBCXX_DEBUG
1550 # include <debug/bitset>
1553 #ifdef _GLIBCXX_PROFILE
1554 # include <profile/bitset>
1557 #endif /* _GLIBCXX_BITSET */