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 _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.
76 _GLIBCXX_CONSTEXPR _Base_bitset()
79 #ifdef __GXX_EXPERIMENTAL_CXX0X__
80 constexpr _Base_bitset(unsigned long long __val)
81 : _M_w({ _WordT(__val)
82 #if __SIZEOF_LONG_LONG__ > __SIZEOF_LONG__
83 , _WordT(__val >> _GLIBCXX_BITSET_BITS_PER_WORD)
87 _Base_bitset(unsigned long __val)
92 static _GLIBCXX_CONSTEXPR size_t
93 _S_whichword(size_t __pos )
94 { return __pos / _GLIBCXX_BITSET_BITS_PER_WORD; }
96 static _GLIBCXX_CONSTEXPR size_t
97 _S_whichbyte(size_t __pos )
98 { return (__pos % _GLIBCXX_BITSET_BITS_PER_WORD) / __CHAR_BIT__; }
100 static _GLIBCXX_CONSTEXPR size_t
101 _S_whichbit(size_t __pos )
102 { return __pos % _GLIBCXX_BITSET_BITS_PER_WORD; }
104 static _GLIBCXX_CONSTEXPR _WordT
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]; }
126 _GLIBCXX_CONSTEXPR _WordT
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;
371 _GLIBCXX_CONSTEXPR _Base_bitset()
375 #ifdef __GXX_EXPERIMENTAL_CXX0X__
376 constexpr _Base_bitset(unsigned long long __val)
378 _Base_bitset(unsigned long __val)
383 static _GLIBCXX_CONSTEXPR size_t
384 _S_whichword(size_t __pos )
385 { return __pos / _GLIBCXX_BITSET_BITS_PER_WORD; }
387 static _GLIBCXX_CONSTEXPR size_t
388 _S_whichbyte(size_t __pos )
389 { return (__pos % _GLIBCXX_BITSET_BITS_PER_WORD) / __CHAR_BIT__; }
391 static _GLIBCXX_CONSTEXPR size_t
392 _S_whichbit(size_t __pos )
393 { return __pos % _GLIBCXX_BITSET_BITS_PER_WORD; }
395 static _GLIBCXX_CONSTEXPR _WordT
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__
417 _GLIBCXX_CONSTEXPR _WordT
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;
514 _GLIBCXX_CONSTEXPR _Base_bitset()
517 #ifdef __GXX_EXPERIMENTAL_CXX0X__
518 constexpr _Base_bitset(unsigned long long)
520 _Base_bitset(unsigned long)
524 static _GLIBCXX_CONSTEXPR size_t
525 _S_whichword(size_t __pos )
526 { return __pos / _GLIBCXX_BITSET_BITS_PER_WORD; }
528 static _GLIBCXX_CONSTEXPR size_t
529 _S_whichbyte(size_t __pos )
530 { return (__pos % _GLIBCXX_BITSET_BITS_PER_WORD) / __CHAR_BIT__; }
532 static _GLIBCXX_CONSTEXPR size_t
533 _S_whichbit(size_t __pos )
534 { return __pos % _GLIBCXX_BITSET_BITS_PER_WORD; }
536 static _GLIBCXX_CONSTEXPR _WordT
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.
550 __throw_out_of_range(__N("_Base_bitset::_M_getword"));
555 _M_getword(size_t __pos) const
558 _GLIBCXX_CONSTEXPR _WordT
563 _M_do_and(const _Base_bitset<0>&)
567 _M_do_or(const _Base_bitset<0>&)
571 _M_do_xor(const _Base_bitset<0>&)
575 _M_do_left_shift(size_t)
579 _M_do_right_shift(size_t)
594 // Are all empty bitsets equal to each other? Are they equal to
595 // themselves? How to compare a thing which has no state? What is
596 // the sound of one zero-length bitset clapping?
598 _M_is_equal(const _Base_bitset<0>&) const
602 _M_are_all_aux() const
614 _M_do_to_ulong() const
617 #ifdef __GXX_EXPERIMENTAL_CXX0X__
619 _M_do_to_ullong() const
623 // Normally "not found" is the size, but that could also be
624 // misinterpreted as an index in this corner case. Oh well.
626 _M_do_find_first(size_t) const
630 _M_do_find_next(size_t, size_t) const
635 // Helper class to zero out the unused high-order bits in the highest word.
636 template<size_t _Extrabits>
639 typedef unsigned long _WordT;
642 _S_do_sanitize(_WordT& __val)
643 { __val &= ~((~static_cast<_WordT>(0)) << _Extrabits); }
649 typedef unsigned long _WordT;
652 _S_do_sanitize(_WordT) { }
656 * @brief The %bitset class represents a @e fixed-size sequence of bits.
658 * @ingroup containers
660 * (Note that %bitset does @e not meet the formal requirements of a
661 * <a href="tables.html#65">container</a>. Mainly, it lacks iterators.)
663 * The template argument, @a Nb, may be any non-negative number,
664 * specifying the number of bits (e.g., "0", "12", "1024*1024").
666 * In the general unoptimized case, storage is allocated in word-sized
667 * blocks. Let B be the number of bits in a word, then (Nb+(B-1))/B
668 * words will be used for storage. B - Nb%B bits are unused. (They are
669 * the high-order bits in the highest word.) It is a class invariant
670 * that those unused bits are always zero.
672 * If you think of %bitset as <em>a simple array of bits</em>, be
673 * aware that your mental picture is reversed: a %bitset behaves
674 * the same way as bits in integers do, with the bit at index 0 in
675 * the <em>least significant / right-hand</em> position, and the bit at
676 * index Nb-1 in the <em>most significant / left-hand</em> position.
677 * Thus, unlike other containers, a %bitset's index <em>counts from
678 * right to left</em>, to put it very loosely.
680 * This behavior is preserved when translating to and from strings. For
681 * example, the first line of the following program probably prints
682 * <em>b('a') is 0001100001</em> on a modern ASCII system.
686 * #include <iostream>
689 * using namespace std;
696 * cout << "b('a') is " << b << endl;
700 * string str = s.str();
701 * cout << "index 3 in the string is " << str[3] << " but\n"
702 * << "index 3 in the bitset is " << b[3] << endl;
707 * http://gcc.gnu.org/onlinedocs/libstdc++/manual/bk01pt12ch33s02.html
708 * for a description of extensions.
710 * Most of the actual code isn't contained in %bitset<> itself, but in the
711 * base class _Base_bitset. The base class works with whole words, not with
712 * individual bits. This allows us to specialize _Base_bitset for the
713 * important special case where the %bitset is only a single word.
715 * Extra confusion can result due to the fact that the storage for
716 * _Base_bitset @e is a regular array, and is indexed as such. This is
717 * carefully encapsulated.
721 : private _Base_bitset<_GLIBCXX_BITSET_WORDS(_Nb)>
724 typedef _Base_bitset<_GLIBCXX_BITSET_WORDS(_Nb)> _Base;
725 typedef unsigned long _WordT;
730 typedef _Sanitize<_Nb % _GLIBCXX_BITSET_BITS_PER_WORD> __sanitize_type;
731 __sanitize_type::_S_do_sanitize(this->_M_hiword());
734 #ifdef __GXX_EXPERIMENTAL_CXX0X__
735 template<typename> friend class hash;
740 * This encapsulates the concept of a single bit. An instance of this
741 * class is a proxy for an actual bit; this way the individual bit
742 * operations are done as faster word-size bitwise instructions.
744 * Most users will never need to use this class directly; conversions
745 * to and from bool are automatic and should be transparent. Overloaded
746 * operators help to preserve the illusion.
748 * (On a typical system, this <em>bit %reference</em> is 64
749 * times the size of an actual bit. Ha.)
762 reference(bitset& __b, size_t __pos)
764 _M_wp = &__b._M_getword(__pos);
765 _M_bpos = _Base::_S_whichbit(__pos);
776 *_M_wp |= _Base::_S_maskbit(_M_bpos);
778 *_M_wp &= ~_Base::_S_maskbit(_M_bpos);
782 // For b[i] = b[__j];
784 operator=(const reference& __j)
786 if ((*(__j._M_wp) & _Base::_S_maskbit(__j._M_bpos)))
787 *_M_wp |= _Base::_S_maskbit(_M_bpos);
789 *_M_wp &= ~_Base::_S_maskbit(_M_bpos);
796 { return (*(_M_wp) & _Base::_S_maskbit(_M_bpos)) == 0; }
799 operator bool() const
800 { return (*(_M_wp) & _Base::_S_maskbit(_M_bpos)) != 0; }
806 *_M_wp ^= _Base::_S_maskbit(_M_bpos);
810 friend class reference;
812 // 23.3.5.1 constructors:
813 /// All bits set to zero.
814 _GLIBCXX_CONSTEXPR bitset()
817 /// Initial bits bitwise-copied from a single word (others set to zero).
818 #ifdef __GXX_EXPERIMENTAL_CXX0X__
819 constexpr bitset(unsigned long long __val)
822 bitset(unsigned long __val)
824 { _M_do_sanitize(); }
828 * @brief Use a subset of a string.
829 * @param s A string of @a 0 and @a 1 characters.
830 * @param position Index of the first character in @a s to use;
832 * @throw std::out_of_range If @a pos is bigger the size of @a s.
833 * @throw std::invalid_argument If a character appears in the string
834 * which is neither @a 0 nor @a 1.
836 template<class _CharT, class _Traits, class _Alloc>
838 bitset(const std::basic_string<_CharT, _Traits, _Alloc>& __s,
839 size_t __position = 0)
842 if (__position > __s.size())
843 __throw_out_of_range(__N("bitset::bitset initial position "
845 _M_copy_from_string(__s, __position,
846 std::basic_string<_CharT, _Traits, _Alloc>::npos,
847 _CharT('0'), _CharT('1'));
851 * @brief Use a subset of a string.
852 * @param s A string of @a 0 and @a 1 characters.
853 * @param position Index of the first character in @a s to use.
854 * @param n The number of characters to copy.
855 * @throw std::out_of_range If @a pos is bigger the size of @a s.
856 * @throw std::invalid_argument If a character appears in the string
857 * which is neither @a 0 nor @a 1.
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)
864 if (__position > __s.size())
865 __throw_out_of_range(__N("bitset::bitset initial position "
867 _M_copy_from_string(__s, __position, __n, _CharT('0'), _CharT('1'));
870 // _GLIBCXX_RESOLVE_LIB_DEFECTS
871 // 396. what are characters zero and one.
872 template<class _CharT, class _Traits, class _Alloc>
873 bitset(const std::basic_string<_CharT, _Traits, _Alloc>& __s,
874 size_t __position, size_t __n,
875 _CharT __zero, _CharT __one = _CharT('1'))
878 if (__position > __s.size())
879 __throw_out_of_range(__N("bitset::bitset initial position "
881 _M_copy_from_string(__s, __position, __n, __zero, __one);
884 #ifdef __GXX_EXPERIMENTAL_CXX0X__
886 * @brief Construct from a character %array.
887 * @param str An %array of characters @a zero and @a one.
888 * @param n The number of characters to use.
889 * @param zero The character corresponding to the value 0.
890 * @param one The character corresponding to the value 1.
891 * @throw std::invalid_argument If a character appears in the string
892 * which is neither @a zero nor @a one.
894 template<typename _CharT>
896 bitset(const _CharT* __str,
897 typename std::basic_string<_CharT>::size_type __n
898 = std::basic_string<_CharT>::npos,
899 _CharT __zero = _CharT('0'), _CharT __one = _CharT('1'))
903 __throw_logic_error(__N("bitset::bitset(const _CharT*, ...)"));
905 if (__n == std::basic_string<_CharT>::npos)
906 __n = std::char_traits<_CharT>::length(__str);
907 _M_copy_from_ptr<_CharT, std::char_traits<_CharT>>(__str, __n, 0,
913 // 23.3.5.2 bitset operations:
916 * @brief Operations on bitsets.
917 * @param rhs A same-sized bitset.
919 * These should be self-explanatory.
922 operator&=(const bitset<_Nb>& __rhs)
924 this->_M_do_and(__rhs);
929 operator|=(const bitset<_Nb>& __rhs)
931 this->_M_do_or(__rhs);
936 operator^=(const bitset<_Nb>& __rhs)
938 this->_M_do_xor(__rhs);
945 * @brief Operations on bitsets.
946 * @param position The number of places to shift.
948 * These should be self-explanatory.
951 operator<<=(size_t __position)
953 if (__builtin_expect(__position < _Nb, 1))
955 this->_M_do_left_shift(__position);
956 this->_M_do_sanitize();
964 operator>>=(size_t __position)
966 if (__builtin_expect(__position < _Nb, 1))
968 this->_M_do_right_shift(__position);
969 this->_M_do_sanitize();
979 * These versions of single-bit set, reset, flip, and test are
980 * extensions from the SGI version. They do no range checking.
981 * @ingroup SGIextensions
984 _Unchecked_set(size_t __pos)
986 this->_M_getword(__pos) |= _Base::_S_maskbit(__pos);
991 _Unchecked_set(size_t __pos, int __val)
994 this->_M_getword(__pos) |= _Base::_S_maskbit(__pos);
996 this->_M_getword(__pos) &= ~_Base::_S_maskbit(__pos);
1001 _Unchecked_reset(size_t __pos)
1003 this->_M_getword(__pos) &= ~_Base::_S_maskbit(__pos);
1008 _Unchecked_flip(size_t __pos)
1010 this->_M_getword(__pos) ^= _Base::_S_maskbit(__pos);
1015 _Unchecked_test(size_t __pos) const
1016 { return ((this->_M_getword(__pos) & _Base::_S_maskbit(__pos))
1017 != static_cast<_WordT>(0)); }
1020 // Set, reset, and flip.
1022 * @brief Sets every bit to true.
1028 this->_M_do_sanitize();
1033 * @brief Sets a given bit to a particular value.
1034 * @param position The index of the bit.
1035 * @param val Either true or false, defaults to true.
1036 * @throw std::out_of_range If @a pos is bigger the size of the %set.
1039 set(size_t __position, bool __val = true)
1041 if (__position >= _Nb)
1042 __throw_out_of_range(__N("bitset::set"));
1043 return _Unchecked_set(__position, __val);
1047 * @brief Sets every bit to false.
1052 this->_M_do_reset();
1057 * @brief Sets a given bit to false.
1058 * @param position The index of the bit.
1059 * @throw std::out_of_range If @a pos is bigger the size of the %set.
1061 * Same as writing @c set(pos,false).
1064 reset(size_t __position)
1066 if (__position >= _Nb)
1067 __throw_out_of_range(__N("bitset::reset"));
1068 return _Unchecked_reset(__position);
1072 * @brief Toggles every bit to its opposite value.
1078 this->_M_do_sanitize();
1083 * @brief Toggles a given bit to its opposite value.
1084 * @param position The index of the bit.
1085 * @throw std::out_of_range If @a pos is bigger the size of the %set.
1088 flip(size_t __position)
1090 if (__position >= _Nb)
1091 __throw_out_of_range(__N("bitset::flip"));
1092 return _Unchecked_flip(__position);
1095 /// See the no-argument flip().
1098 { return bitset<_Nb>(*this).flip(); }
1102 * @brief Array-indexing support.
1103 * @param position Index into the %bitset.
1104 * @return A bool for a <em>const %bitset</em>. For non-const
1105 * bitsets, an instance of the reference proxy class.
1106 * @note These operators do no range checking and throw no exceptions,
1107 * as required by DR 11 to the standard.
1109 * _GLIBCXX_RESOLVE_LIB_DEFECTS Note that this implementation already
1110 * resolves DR 11 (items 1 and 2), but does not do the range-checking
1111 * required by that DR's resolution. -pme
1112 * The DR has since been changed: range-checking is a precondition
1113 * (users' responsibility), and these functions must not throw. -pme
1116 operator[](size_t __position)
1117 { return reference(*this, __position); }
1120 operator[](size_t __position) const
1121 { return _Unchecked_test(__position); }
1125 * @brief Returns a numerical interpretation of the %bitset.
1126 * @return The integral equivalent of the bits.
1127 * @throw std::overflow_error If there are too many bits to be
1128 * represented in an @c unsigned @c long.
1132 { return this->_M_do_to_ulong(); }
1134 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1137 { return this->_M_do_to_ullong(); }
1141 * @brief Returns a character interpretation of the %bitset.
1142 * @return The string equivalent of the bits.
1144 * Note the ordering of the bits: decreasing character positions
1145 * correspond to increasing bit positions (see the main class notes for
1148 template<class _CharT, class _Traits, class _Alloc>
1149 std::basic_string<_CharT, _Traits, _Alloc>
1152 std::basic_string<_CharT, _Traits, _Alloc> __result;
1153 _M_copy_to_string(__result, _CharT('0'), _CharT('1'));
1157 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1158 // 396. what are characters zero and one.
1159 template<class _CharT, class _Traits, class _Alloc>
1160 std::basic_string<_CharT, _Traits, _Alloc>
1161 to_string(_CharT __zero, _CharT __one = _CharT('1')) const
1163 std::basic_string<_CharT, _Traits, _Alloc> __result;
1164 _M_copy_to_string(__result, __zero, __one);
1168 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1169 // 434. bitset::to_string() hard to use.
1170 template<class _CharT, class _Traits>
1171 std::basic_string<_CharT, _Traits, std::allocator<_CharT> >
1173 { return to_string<_CharT, _Traits, std::allocator<_CharT> >(); }
1175 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1176 // 853. to_string needs updating with zero and one.
1177 template<class _CharT, class _Traits>
1178 std::basic_string<_CharT, _Traits, std::allocator<_CharT> >
1179 to_string(_CharT __zero, _CharT __one = _CharT('1')) const
1180 { return to_string<_CharT, _Traits,
1181 std::allocator<_CharT> >(__zero, __one); }
1183 template<class _CharT>
1184 std::basic_string<_CharT, std::char_traits<_CharT>,
1185 std::allocator<_CharT> >
1188 return to_string<_CharT, std::char_traits<_CharT>,
1189 std::allocator<_CharT> >();
1192 template<class _CharT>
1193 std::basic_string<_CharT, std::char_traits<_CharT>,
1194 std::allocator<_CharT> >
1195 to_string(_CharT __zero, _CharT __one = _CharT('1')) const
1197 return to_string<_CharT, std::char_traits<_CharT>,
1198 std::allocator<_CharT> >(__zero, __one);
1201 std::basic_string<char, std::char_traits<char>, std::allocator<char> >
1204 return to_string<char, std::char_traits<char>,
1205 std::allocator<char> >();
1208 std::basic_string<char, std::char_traits<char>, std::allocator<char> >
1209 to_string(char __zero, char __one = '1') const
1211 return to_string<char, std::char_traits<char>,
1212 std::allocator<char> >(__zero, __one);
1215 // Helper functions for string operations.
1216 template<class _CharT, class _Traits>
1218 _M_copy_from_ptr(const _CharT*, size_t, size_t, size_t,
1221 template<class _CharT, class _Traits, class _Alloc>
1223 _M_copy_from_string(const std::basic_string<_CharT,
1224 _Traits, _Alloc>& __s, size_t __pos, size_t __n,
1225 _CharT __zero, _CharT __one)
1226 { _M_copy_from_ptr<_CharT, _Traits>(__s.data(), __s.size(), __pos, __n,
1229 template<class _CharT, class _Traits, class _Alloc>
1231 _M_copy_to_string(std::basic_string<_CharT, _Traits, _Alloc>&,
1232 _CharT, _CharT) const;
1234 // NB: Backward compat.
1235 template<class _CharT, class _Traits, class _Alloc>
1237 _M_copy_from_string(const std::basic_string<_CharT,
1238 _Traits, _Alloc>& __s, size_t __pos, size_t __n)
1239 { _M_copy_from_string(__s, __pos, __n, _CharT('0'), _CharT('1')); }
1241 template<class _CharT, class _Traits, class _Alloc>
1243 _M_copy_to_string(std::basic_string<_CharT, _Traits,_Alloc>& __s) const
1244 { _M_copy_to_string(__s, _CharT('0'), _CharT('1')); }
1246 /// Returns the number of bits which are set.
1249 { return this->_M_do_count(); }
1251 /// Returns the total number of bits.
1252 _GLIBCXX_CONSTEXPR size_t
1257 /// These comparisons for equality/inequality are, well, @e bitwise.
1259 operator==(const bitset<_Nb>& __rhs) const
1260 { return this->_M_is_equal(__rhs); }
1263 operator!=(const bitset<_Nb>& __rhs) const
1264 { return !this->_M_is_equal(__rhs); }
1268 * @brief Tests the value of a bit.
1269 * @param position The index of a bit.
1270 * @return The value at @a pos.
1271 * @throw std::out_of_range If @a pos is bigger the size of the %set.
1274 test(size_t __position) const
1276 if (__position >= _Nb)
1277 __throw_out_of_range(__N("bitset::test"));
1278 return _Unchecked_test(__position);
1281 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1282 // DR 693. std::bitset::all() missing.
1284 * @brief Tests whether all the bits are on.
1285 * @return True if all the bits are set.
1289 { return this->_M_are_all_aux() == _Nb; }
1292 * @brief Tests whether any of the bits are on.
1293 * @return True if at least one bit is set.
1297 { return this->_M_is_any(); }
1300 * @brief Tests whether any of the bits are on.
1301 * @return True if none of the bits are set.
1305 { return !this->_M_is_any(); }
1308 /// Self-explanatory.
1310 operator<<(size_t __position) const
1311 { return bitset<_Nb>(*this) <<= __position; }
1314 operator>>(size_t __position) const
1315 { return bitset<_Nb>(*this) >>= __position; }
1319 * @brief Finds the index of the first "on" bit.
1320 * @return The index of the first bit set, or size() if not found.
1321 * @ingroup SGIextensions
1326 { return this->_M_do_find_first(_Nb); }
1329 * @brief Finds the index of the next "on" bit after prev.
1330 * @return The index of the next bit set, or size() if not found.
1331 * @param prev Where to start searching.
1332 * @ingroup SGIextensions
1336 _Find_next(size_t __prev ) const
1337 { return this->_M_do_find_next(__prev, _Nb); }
1340 // Definitions of non-inline member functions.
1341 template<size_t _Nb>
1342 template<class _CharT, class _Traits>
1345 _M_copy_from_ptr(const _CharT* __s, size_t __len,
1346 size_t __pos, size_t __n, _CharT __zero, _CharT __one)
1349 const size_t __nbits = std::min(_Nb, std::min(__n, __len - __pos));
1350 for (size_t __i = __nbits; __i > 0; --__i)
1352 const _CharT __c = __s[__pos + __nbits - __i];
1353 if (_Traits::eq(__c, __zero))
1355 else if (_Traits::eq(__c, __one))
1356 _Unchecked_set(__i - 1);
1358 __throw_invalid_argument(__N("bitset::_M_copy_from_ptr"));
1362 template<size_t _Nb>
1363 template<class _CharT, class _Traits, class _Alloc>
1366 _M_copy_to_string(std::basic_string<_CharT, _Traits, _Alloc>& __s,
1367 _CharT __zero, _CharT __one) const
1369 __s.assign(_Nb, __zero);
1370 for (size_t __i = _Nb; __i > 0; --__i)
1371 if (_Unchecked_test(__i - 1))
1372 _Traits::assign(__s[_Nb - __i], __one);
1375 // 23.3.5.3 bitset operations:
1378 * @brief Global bitwise operations on bitsets.
1379 * @param x A bitset.
1380 * @param y A bitset of the same size as @a x.
1381 * @return A new bitset.
1383 * These should be self-explanatory.
1385 template<size_t _Nb>
1387 operator&(const bitset<_Nb>& __x, const bitset<_Nb>& __y)
1389 bitset<_Nb> __result(__x);
1394 template<size_t _Nb>
1396 operator|(const bitset<_Nb>& __x, const bitset<_Nb>& __y)
1398 bitset<_Nb> __result(__x);
1403 template <size_t _Nb>
1405 operator^(const bitset<_Nb>& __x, const bitset<_Nb>& __y)
1407 bitset<_Nb> __result(__x);
1415 * @brief Global I/O operators for bitsets.
1417 * Direct I/O between streams and bitsets is supported. Output is
1418 * straightforward. Input will skip whitespace, only accept @a 0 and @a 1
1419 * characters, and will only extract as many digits as the %bitset will
1422 template<class _CharT, class _Traits, size_t _Nb>
1423 std::basic_istream<_CharT, _Traits>&
1424 operator>>(std::basic_istream<_CharT, _Traits>& __is, bitset<_Nb>& __x)
1426 typedef typename _Traits::char_type char_type;
1427 typedef std::basic_istream<_CharT, _Traits> __istream_type;
1428 typedef typename __istream_type::ios_base __ios_base;
1430 std::basic_string<_CharT, _Traits> __tmp;
1433 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1434 // 303. Bitset input operator underspecified
1435 const char_type __zero = __is.widen('0');
1436 const char_type __one = __is.widen('1');
1438 typename __ios_base::iostate __state = __ios_base::goodbit;
1439 typename __istream_type::sentry __sentry(__is);
1444 for (size_t __i = _Nb; __i > 0; --__i)
1446 static typename _Traits::int_type __eof = _Traits::eof();
1448 typename _Traits::int_type __c1 = __is.rdbuf()->sbumpc();
1449 if (_Traits::eq_int_type(__c1, __eof))
1451 __state |= __ios_base::eofbit;
1456 const char_type __c2 = _Traits::to_char_type(__c1);
1457 if (_Traits::eq(__c2, __zero))
1458 __tmp.push_back(__zero);
1459 else if (_Traits::eq(__c2, __one))
1460 __tmp.push_back(__one);
1462 eq_int_type(__is.rdbuf()->sputbackc(__c2),
1465 __state |= __ios_base::failbit;
1471 __catch(__cxxabiv1::__forced_unwind&)
1473 __is._M_setstate(__ios_base::badbit);
1474 __throw_exception_again;
1477 { __is._M_setstate(__ios_base::badbit); }
1480 if (__tmp.empty() && _Nb)
1481 __state |= __ios_base::failbit;
1483 __x._M_copy_from_string(__tmp, static_cast<size_t>(0), _Nb,
1486 __is.setstate(__state);
1490 template <class _CharT, class _Traits, size_t _Nb>
1491 std::basic_ostream<_CharT, _Traits>&
1492 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1493 const bitset<_Nb>& __x)
1495 std::basic_string<_CharT, _Traits> __tmp;
1497 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1498 // 396. what are characters zero and one.
1499 const ctype<_CharT>& __ct = use_facet<ctype<_CharT> >(__os.getloc());
1500 __x._M_copy_to_string(__tmp, __ct.widen('0'), __ct.widen('1'));
1501 return __os << __tmp;
1505 _GLIBCXX_END_NESTED_NAMESPACE
1507 #undef _GLIBCXX_BITSET_WORDS
1508 #undef _GLIBCXX_BITSET_BITS_PER_WORD
1510 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1512 #include <bits/functional_hash.h>
1514 _GLIBCXX_BEGIN_NAMESPACE(std)
1517 /// std::hash specialization for bitset.
1518 template<size_t _Nb>
1519 struct hash<_GLIBCXX_STD_D::bitset<_Nb>>
1520 : public __hash_base<size_t, _GLIBCXX_STD_D::bitset<_Nb>>
1523 operator()(const _GLIBCXX_STD_D::bitset<_Nb>& __b) const
1525 const size_t __clength = (_Nb + __CHAR_BIT__ - 1) / __CHAR_BIT__;
1526 return std::_Hash_impl::hash(__b._M_getdata(), __clength);
1531 struct hash<_GLIBCXX_STD_D::bitset<0>>
1532 : public __hash_base<size_t, _GLIBCXX_STD_D::bitset<0>>
1535 operator()(const _GLIBCXX_STD_D::bitset<0>&) const
1539 _GLIBCXX_END_NAMESPACE
1541 #endif // __GXX_EXPERIMENTAL_CXX0X__
1543 #ifdef _GLIBCXX_DEBUG
1544 # include <debug/bitset>
1547 #ifdef _GLIBCXX_PROFILE
1548 # include <profile/bitset>
1551 #endif /* _GLIBCXX_BITSET */