1 // <bitset> -*- C++ -*-
3 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
4 // Free Software Foundation, Inc.
6 // This file is part of the GNU ISO C++ Library. This library is free
7 // software; you can redistribute it and/or modify it under the
8 // terms of the GNU General Public License as published by the
9 // Free Software Foundation; either version 3, or (at your option)
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
17 // Under Section 7 of GPL version 3, you are granted additional
18 // permissions described in the GCC Runtime Library Exception, version
19 // 3.1, as published by the Free Software Foundation.
21 // You should have received a copy of the GNU General Public License and
22 // a copy of the GCC Runtime Library Exception along with this program;
23 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24 // <http://www.gnu.org/licenses/>.
28 * Silicon Graphics Computer Systems, Inc.
30 * Permission to use, copy, modify, distribute and sell this software
31 * and its documentation for any purpose is hereby granted without fee,
32 * provided that the above copyright notice appear in all copies and
33 * that both that copyright notice and this permission notice appear
34 * in supporting documentation. Silicon Graphics makes no
35 * representations about the suitability of this software for any
36 * purpose. It is provided "as is" without express or implied warranty.
39 /** @file include/bitset
40 * This is a Standard C++ Library header.
43 #ifndef _GLIBCXX_BITSET
44 #define _GLIBCXX_BITSET 1
46 #pragma GCC system_header
49 #include <bits/functexcept.h> // For invalid_argument, out_of_range,
52 #include <cxxabi-forced.h>
54 #define _GLIBCXX_BITSET_BITS_PER_WORD (__CHAR_BIT__ * sizeof(unsigned long))
55 #define _GLIBCXX_BITSET_WORDS(__n) \
56 ((__n) / _GLIBCXX_BITSET_BITS_PER_WORD + \
57 ((__n) % _GLIBCXX_BITSET_BITS_PER_WORD == 0 ? 0 : 1))
59 _GLIBCXX_BEGIN_NESTED_NAMESPACE(std, _GLIBCXX_STD_D)
62 * Base class, general case. It is a class invariant that _Nw will be
65 * See documentation for bitset.
70 typedef unsigned long _WordT;
72 /// 0 is the least significant word.
75 _GLIBCXX_CONSTEXPR _Base_bitset()
78 #ifdef __GXX_EXPERIMENTAL_CXX0X__
79 constexpr _Base_bitset(unsigned long long __val)
80 : _M_w({ _WordT(__val)
81 #if __SIZEOF_LONG_LONG__ > __SIZEOF_LONG__
82 , _WordT(__val >> _GLIBCXX_BITSET_BITS_PER_WORD)
86 _Base_bitset(unsigned long __val)
91 static _GLIBCXX_CONSTEXPR size_t
92 _S_whichword(size_t __pos )
93 { return __pos / _GLIBCXX_BITSET_BITS_PER_WORD; }
95 static _GLIBCXX_CONSTEXPR size_t
96 _S_whichbyte(size_t __pos )
97 { return (__pos % _GLIBCXX_BITSET_BITS_PER_WORD) / __CHAR_BIT__; }
99 static _GLIBCXX_CONSTEXPR size_t
100 _S_whichbit(size_t __pos )
101 { return __pos % _GLIBCXX_BITSET_BITS_PER_WORD; }
103 static _GLIBCXX_CONSTEXPR _WordT
104 _S_maskbit(size_t __pos )
105 { return (static_cast<_WordT>(1)) << _S_whichbit(__pos); }
108 _M_getword(size_t __pos)
109 { return _M_w[_S_whichword(__pos)]; }
112 _M_getword(size_t __pos) const
113 { return _M_w[_S_whichword(__pos)]; }
115 #ifdef __GXX_EXPERIMENTAL_CXX0X__
123 { return _M_w[_Nw - 1]; }
125 _GLIBCXX_CONSTEXPR _WordT
127 { return _M_w[_Nw - 1]; }
130 _M_do_and(const _Base_bitset<_Nw>& __x)
132 for (size_t __i = 0; __i < _Nw; __i++)
133 _M_w[__i] &= __x._M_w[__i];
137 _M_do_or(const _Base_bitset<_Nw>& __x)
139 for (size_t __i = 0; __i < _Nw; __i++)
140 _M_w[__i] |= __x._M_w[__i];
144 _M_do_xor(const _Base_bitset<_Nw>& __x)
146 for (size_t __i = 0; __i < _Nw; __i++)
147 _M_w[__i] ^= __x._M_w[__i];
151 _M_do_left_shift(size_t __shift);
154 _M_do_right_shift(size_t __shift);
159 for (size_t __i = 0; __i < _Nw; __i++)
160 _M_w[__i] = ~_M_w[__i];
166 for (size_t __i = 0; __i < _Nw; __i++)
167 _M_w[__i] = ~static_cast<_WordT>(0);
172 { __builtin_memset(_M_w, 0, _Nw * sizeof(_WordT)); }
175 _M_is_equal(const _Base_bitset<_Nw>& __x) const
177 for (size_t __i = 0; __i < _Nw; ++__i)
178 if (_M_w[__i] != __x._M_w[__i])
184 _M_are_all_aux() const
186 for (size_t __i = 0; __i < _Nw - 1; __i++)
187 if (_M_w[__i] != ~static_cast<_WordT>(0))
189 return ((_Nw - 1) * _GLIBCXX_BITSET_BITS_PER_WORD
190 + __builtin_popcountl(_M_hiword()));
196 for (size_t __i = 0; __i < _Nw; __i++)
197 if (_M_w[__i] != static_cast<_WordT>(0))
206 for (size_t __i = 0; __i < _Nw; __i++)
207 __result += __builtin_popcountl(_M_w[__i]);
212 _M_do_to_ulong() const;
214 #ifdef __GXX_EXPERIMENTAL_CXX0X__
216 _M_do_to_ullong() const;
219 // find first "on" bit
221 _M_do_find_first(size_t __not_found) const;
223 // find the next "on" bit that follows "prev"
225 _M_do_find_next(size_t __prev, size_t __not_found) const;
228 // Definitions of non-inline functions from _Base_bitset.
231 _Base_bitset<_Nw>::_M_do_left_shift(size_t __shift)
233 if (__builtin_expect(__shift != 0, 1))
235 const size_t __wshift = __shift / _GLIBCXX_BITSET_BITS_PER_WORD;
236 const size_t __offset = __shift % _GLIBCXX_BITSET_BITS_PER_WORD;
239 for (size_t __n = _Nw - 1; __n >= __wshift; --__n)
240 _M_w[__n] = _M_w[__n - __wshift];
243 const size_t __sub_offset = (_GLIBCXX_BITSET_BITS_PER_WORD
245 for (size_t __n = _Nw - 1; __n > __wshift; --__n)
246 _M_w[__n] = ((_M_w[__n - __wshift] << __offset)
247 | (_M_w[__n - __wshift - 1] >> __sub_offset));
248 _M_w[__wshift] = _M_w[0] << __offset;
251 std::fill(_M_w + 0, _M_w + __wshift, static_cast<_WordT>(0));
257 _Base_bitset<_Nw>::_M_do_right_shift(size_t __shift)
259 if (__builtin_expect(__shift != 0, 1))
261 const size_t __wshift = __shift / _GLIBCXX_BITSET_BITS_PER_WORD;
262 const size_t __offset = __shift % _GLIBCXX_BITSET_BITS_PER_WORD;
263 const size_t __limit = _Nw - __wshift - 1;
266 for (size_t __n = 0; __n <= __limit; ++__n)
267 _M_w[__n] = _M_w[__n + __wshift];
270 const size_t __sub_offset = (_GLIBCXX_BITSET_BITS_PER_WORD
272 for (size_t __n = 0; __n < __limit; ++__n)
273 _M_w[__n] = ((_M_w[__n + __wshift] >> __offset)
274 | (_M_w[__n + __wshift + 1] << __sub_offset));
275 _M_w[__limit] = _M_w[_Nw-1] >> __offset;
278 std::fill(_M_w + __limit + 1, _M_w + _Nw, static_cast<_WordT>(0));
284 _Base_bitset<_Nw>::_M_do_to_ulong() const
286 for (size_t __i = 1; __i < _Nw; ++__i)
288 __throw_overflow_error(__N("_Base_bitset::_M_do_to_ulong"));
292 #ifdef __GXX_EXPERIMENTAL_CXX0X__
295 _Base_bitset<_Nw>::_M_do_to_ullong() const
297 const bool __dw = sizeof(unsigned long long) > sizeof(unsigned long);
298 for (size_t __i = 1 + __dw; __i < _Nw; ++__i)
300 __throw_overflow_error(__N("_Base_bitset::_M_do_to_ullong"));
303 return _M_w[0] + (static_cast<unsigned long long>(_M_w[1])
304 << _GLIBCXX_BITSET_BITS_PER_WORD);
311 _Base_bitset<_Nw>::_M_do_find_first(size_t __not_found) const
313 for (size_t __i = 0; __i < _Nw; __i++)
315 _WordT __thisword = _M_w[__i];
316 if (__thisword != static_cast<_WordT>(0))
317 return (__i * _GLIBCXX_BITSET_BITS_PER_WORD
318 + __builtin_ctzl(__thisword));
320 // not found, so return an indication of failure.
326 _Base_bitset<_Nw>::_M_do_find_next(size_t __prev, size_t __not_found) const
328 // make bound inclusive
331 // check out of bounds
332 if (__prev >= _Nw * _GLIBCXX_BITSET_BITS_PER_WORD)
336 size_t __i = _S_whichword(__prev);
337 _WordT __thisword = _M_w[__i];
339 // mask off bits below bound
340 __thisword &= (~static_cast<_WordT>(0)) << _S_whichbit(__prev);
342 if (__thisword != static_cast<_WordT>(0))
343 return (__i * _GLIBCXX_BITSET_BITS_PER_WORD
344 + __builtin_ctzl(__thisword));
346 // check subsequent words
348 for (; __i < _Nw; __i++)
350 __thisword = _M_w[__i];
351 if (__thisword != static_cast<_WordT>(0))
352 return (__i * _GLIBCXX_BITSET_BITS_PER_WORD
353 + __builtin_ctzl(__thisword));
355 // not found, so return an indication of failure.
357 } // end _M_do_find_next
360 * Base class, specialization for a single word.
362 * See documentation for bitset.
365 struct _Base_bitset<1>
367 typedef unsigned long _WordT;
370 _GLIBCXX_CONSTEXPR _Base_bitset()
374 #ifdef __GXX_EXPERIMENTAL_CXX0X__
375 constexpr _Base_bitset(unsigned long long __val)
377 _Base_bitset(unsigned long __val)
382 static _GLIBCXX_CONSTEXPR size_t
383 _S_whichword(size_t __pos )
384 { return __pos / _GLIBCXX_BITSET_BITS_PER_WORD; }
386 static _GLIBCXX_CONSTEXPR size_t
387 _S_whichbyte(size_t __pos )
388 { return (__pos % _GLIBCXX_BITSET_BITS_PER_WORD) / __CHAR_BIT__; }
390 static _GLIBCXX_CONSTEXPR size_t
391 _S_whichbit(size_t __pos )
392 { return __pos % _GLIBCXX_BITSET_BITS_PER_WORD; }
394 static _GLIBCXX_CONSTEXPR _WordT
395 _S_maskbit(size_t __pos )
396 { return (static_cast<_WordT>(1)) << _S_whichbit(__pos); }
403 _M_getword(size_t) const
406 #ifdef __GXX_EXPERIMENTAL_CXX0X__
416 _GLIBCXX_CONSTEXPR _WordT
421 _M_do_and(const _Base_bitset<1>& __x)
422 { _M_w &= __x._M_w; }
425 _M_do_or(const _Base_bitset<1>& __x)
426 { _M_w |= __x._M_w; }
429 _M_do_xor(const _Base_bitset<1>& __x)
430 { _M_w ^= __x._M_w; }
433 _M_do_left_shift(size_t __shift)
434 { _M_w <<= __shift; }
437 _M_do_right_shift(size_t __shift)
438 { _M_w >>= __shift; }
446 { _M_w = ~static_cast<_WordT>(0); }
453 _M_is_equal(const _Base_bitset<1>& __x) const
454 { return _M_w == __x._M_w; }
457 _M_are_all_aux() const
458 { return __builtin_popcountl(_M_w); }
462 { return _M_w != 0; }
466 { return __builtin_popcountl(_M_w); }
469 _M_do_to_ulong() const
472 #ifdef __GXX_EXPERIMENTAL_CXX0X__
474 _M_do_to_ullong() const
479 _M_do_find_first(size_t __not_found) const
482 return __builtin_ctzl(_M_w);
487 // find the next "on" bit that follows "prev"
489 _M_do_find_next(size_t __prev, size_t __not_found) const
492 if (__prev >= ((size_t) _GLIBCXX_BITSET_BITS_PER_WORD))
495 _WordT __x = _M_w >> __prev;
497 return __builtin_ctzl(__x) + __prev;
504 * Base class, specialization for no storage (zero-length %bitset).
506 * See documentation for bitset.
509 struct _Base_bitset<0>
511 typedef unsigned long _WordT;
513 _GLIBCXX_CONSTEXPR _Base_bitset()
516 #ifdef __GXX_EXPERIMENTAL_CXX0X__
517 constexpr _Base_bitset(unsigned long long)
519 _Base_bitset(unsigned long)
523 static _GLIBCXX_CONSTEXPR size_t
524 _S_whichword(size_t __pos )
525 { return __pos / _GLIBCXX_BITSET_BITS_PER_WORD; }
527 static _GLIBCXX_CONSTEXPR size_t
528 _S_whichbyte(size_t __pos )
529 { return (__pos % _GLIBCXX_BITSET_BITS_PER_WORD) / __CHAR_BIT__; }
531 static _GLIBCXX_CONSTEXPR size_t
532 _S_whichbit(size_t __pos )
533 { return __pos % _GLIBCXX_BITSET_BITS_PER_WORD; }
535 static _GLIBCXX_CONSTEXPR _WordT
536 _S_maskbit(size_t __pos )
537 { return (static_cast<_WordT>(1)) << _S_whichbit(__pos); }
539 // This would normally give access to the data. The bounds-checking
540 // in the bitset class will prevent the user from getting this far,
541 // but (1) it must still return an lvalue to compile, and (2) the
542 // user might call _Unchecked_set directly, in which case this /needs/
543 // to fail. Let's not penalize zero-length users unless they actually
544 // make an unchecked call; all the memory ugliness is therefore
545 // localized to this single should-never-get-this-far function.
549 __throw_out_of_range(__N("_Base_bitset::_M_getword"));
554 _M_getword(size_t __pos) const
557 _GLIBCXX_CONSTEXPR _WordT
562 _M_do_and(const _Base_bitset<0>&)
566 _M_do_or(const _Base_bitset<0>&)
570 _M_do_xor(const _Base_bitset<0>&)
574 _M_do_left_shift(size_t)
578 _M_do_right_shift(size_t)
593 // Are all empty bitsets equal to each other? Are they equal to
594 // themselves? How to compare a thing which has no state? What is
595 // the sound of one zero-length bitset clapping?
597 _M_is_equal(const _Base_bitset<0>&) const
601 _M_are_all_aux() const
613 _M_do_to_ulong() const
616 #ifdef __GXX_EXPERIMENTAL_CXX0X__
618 _M_do_to_ullong() const
622 // Normally "not found" is the size, but that could also be
623 // misinterpreted as an index in this corner case. Oh well.
625 _M_do_find_first(size_t) const
629 _M_do_find_next(size_t, size_t) const
634 // Helper class to zero out the unused high-order bits in the highest word.
635 template<size_t _Extrabits>
638 typedef unsigned long _WordT;
641 _S_do_sanitize(_WordT& __val)
642 { __val &= ~((~static_cast<_WordT>(0)) << _Extrabits); }
648 typedef unsigned long _WordT;
651 _S_do_sanitize(_WordT) { }
655 * @brief The %bitset class represents a @e fixed-size sequence of bits.
657 * @ingroup containers
659 * (Note that %bitset does @e not meet the formal requirements of a
660 * <a href="tables.html#65">container</a>. Mainly, it lacks iterators.)
662 * The template argument, @a Nb, may be any non-negative number,
663 * specifying the number of bits (e.g., "0", "12", "1024*1024").
665 * In the general unoptimized case, storage is allocated in word-sized
666 * blocks. Let B be the number of bits in a word, then (Nb+(B-1))/B
667 * words will be used for storage. B - Nb%B bits are unused. (They are
668 * the high-order bits in the highest word.) It is a class invariant
669 * that those unused bits are always zero.
671 * If you think of %bitset as <em>a simple array of bits</em>, be
672 * aware that your mental picture is reversed: a %bitset behaves
673 * the same way as bits in integers do, with the bit at index 0 in
674 * the <em>least significant / right-hand</em> position, and the bit at
675 * index Nb-1 in the <em>most significant / left-hand</em> position.
676 * Thus, unlike other containers, a %bitset's index <em>counts from
677 * right to left</em>, to put it very loosely.
679 * This behavior is preserved when translating to and from strings. For
680 * example, the first line of the following program probably prints
681 * <em>b('a') is 0001100001</em> on a modern ASCII system.
685 * #include <iostream>
688 * using namespace std;
695 * cout << "b('a') is " << b << endl;
699 * string str = s.str();
700 * cout << "index 3 in the string is " << str[3] << " but\n"
701 * << "index 3 in the bitset is " << b[3] << endl;
706 * http://gcc.gnu.org/onlinedocs/libstdc++/manual/bk01pt12ch33s02.html
707 * for a description of extensions.
709 * Most of the actual code isn't contained in %bitset<> itself, but in the
710 * base class _Base_bitset. The base class works with whole words, not with
711 * individual bits. This allows us to specialize _Base_bitset for the
712 * important special case where the %bitset is only a single word.
714 * Extra confusion can result due to the fact that the storage for
715 * _Base_bitset @e is a regular array, and is indexed as such. This is
716 * carefully encapsulated.
720 : private _Base_bitset<_GLIBCXX_BITSET_WORDS(_Nb)>
723 typedef _Base_bitset<_GLIBCXX_BITSET_WORDS(_Nb)> _Base;
724 typedef unsigned long _WordT;
729 typedef _Sanitize<_Nb % _GLIBCXX_BITSET_BITS_PER_WORD> __sanitize_type;
730 __sanitize_type::_S_do_sanitize(this->_M_hiword());
733 #ifdef __GXX_EXPERIMENTAL_CXX0X__
734 template<typename> friend class hash;
739 * This encapsulates the concept of a single bit. An instance of this
740 * class is a proxy for an actual bit; this way the individual bit
741 * operations are done as faster word-size bitwise instructions.
743 * Most users will never need to use this class directly; conversions
744 * to and from bool are automatic and should be transparent. Overloaded
745 * operators help to preserve the illusion.
747 * (On a typical system, this <em>bit %reference</em> is 64
748 * times the size of an actual bit. Ha.)
761 reference(bitset& __b, size_t __pos)
763 _M_wp = &__b._M_getword(__pos);
764 _M_bpos = _Base::_S_whichbit(__pos);
775 *_M_wp |= _Base::_S_maskbit(_M_bpos);
777 *_M_wp &= ~_Base::_S_maskbit(_M_bpos);
781 // For b[i] = b[__j];
783 operator=(const reference& __j)
785 if ((*(__j._M_wp) & _Base::_S_maskbit(__j._M_bpos)))
786 *_M_wp |= _Base::_S_maskbit(_M_bpos);
788 *_M_wp &= ~_Base::_S_maskbit(_M_bpos);
795 { return (*(_M_wp) & _Base::_S_maskbit(_M_bpos)) == 0; }
798 operator bool() const
799 { return (*(_M_wp) & _Base::_S_maskbit(_M_bpos)) != 0; }
805 *_M_wp ^= _Base::_S_maskbit(_M_bpos);
809 friend class reference;
811 // 23.3.5.1 constructors:
812 /// All bits set to zero.
813 _GLIBCXX_CONSTEXPR bitset()
816 /// Initial bits bitwise-copied from a single word (others set to zero).
817 #ifdef __GXX_EXPERIMENTAL_CXX0X__
818 constexpr bitset(unsigned long long __val)
821 bitset(unsigned long __val)
823 { _M_do_sanitize(); }
827 * @brief Use a subset of a string.
828 * @param s A string of @a 0 and @a 1 characters.
829 * @param position Index of the first character in @a s to use;
831 * @throw std::out_of_range If @a pos is bigger the size of @a s.
832 * @throw std::invalid_argument If a character appears in the string
833 * which is neither @a 0 nor @a 1.
835 template<class _CharT, class _Traits, class _Alloc>
837 bitset(const std::basic_string<_CharT, _Traits, _Alloc>& __s,
838 size_t __position = 0)
841 if (__position > __s.size())
842 __throw_out_of_range(__N("bitset::bitset initial position "
844 _M_copy_from_string(__s, __position,
845 std::basic_string<_CharT, _Traits, _Alloc>::npos,
846 _CharT('0'), _CharT('1'));
850 * @brief Use a subset of a string.
851 * @param s A string of @a 0 and @a 1 characters.
852 * @param position Index of the first character in @a s to use.
853 * @param n The number of characters to copy.
854 * @throw std::out_of_range If @a pos is bigger the size of @a s.
855 * @throw std::invalid_argument If a character appears in the string
856 * which is neither @a 0 nor @a 1.
858 template<class _CharT, class _Traits, class _Alloc>
859 bitset(const std::basic_string<_CharT, _Traits, _Alloc>& __s,
860 size_t __position, size_t __n)
863 if (__position > __s.size())
864 __throw_out_of_range(__N("bitset::bitset initial position "
866 _M_copy_from_string(__s, __position, __n, _CharT('0'), _CharT('1'));
869 // _GLIBCXX_RESOLVE_LIB_DEFECTS
870 // 396. what are characters zero and one.
871 template<class _CharT, class _Traits, class _Alloc>
872 bitset(const std::basic_string<_CharT, _Traits, _Alloc>& __s,
873 size_t __position, size_t __n,
874 _CharT __zero, _CharT __one = _CharT('1'))
877 if (__position > __s.size())
878 __throw_out_of_range(__N("bitset::bitset initial position "
880 _M_copy_from_string(__s, __position, __n, __zero, __one);
883 #ifdef __GXX_EXPERIMENTAL_CXX0X__
885 * @brief Construct from a character %array.
886 * @param str An %array of characters @a zero and @a one.
887 * @param n The number of characters to use.
888 * @param zero The character corresponding to the value 0.
889 * @param one The character corresponding to the value 1.
890 * @throw std::invalid_argument If a character appears in the string
891 * which is neither @a zero nor @a one.
893 template<typename _CharT>
895 bitset(const _CharT* __str,
896 typename std::basic_string<_CharT>::size_type __n
897 = std::basic_string<_CharT>::npos,
898 _CharT __zero = _CharT('0'), _CharT __one = _CharT('1'))
902 __throw_logic_error(__N("bitset::bitset(const _CharT*, ...)"));
904 if (__n == std::basic_string<_CharT>::npos)
905 __n = std::char_traits<_CharT>::length(__str);
906 _M_copy_from_ptr<_CharT, std::char_traits<_CharT>>(__str, __n, 0,
912 // 23.3.5.2 bitset operations:
915 * @brief Operations on bitsets.
916 * @param rhs A same-sized bitset.
918 * These should be self-explanatory.
921 operator&=(const bitset<_Nb>& __rhs)
923 this->_M_do_and(__rhs);
928 operator|=(const bitset<_Nb>& __rhs)
930 this->_M_do_or(__rhs);
935 operator^=(const bitset<_Nb>& __rhs)
937 this->_M_do_xor(__rhs);
944 * @brief Operations on bitsets.
945 * @param position The number of places to shift.
947 * These should be self-explanatory.
950 operator<<=(size_t __position)
952 if (__builtin_expect(__position < _Nb, 1))
954 this->_M_do_left_shift(__position);
955 this->_M_do_sanitize();
963 operator>>=(size_t __position)
965 if (__builtin_expect(__position < _Nb, 1))
967 this->_M_do_right_shift(__position);
968 this->_M_do_sanitize();
978 * These versions of single-bit set, reset, flip, and test are
979 * extensions from the SGI version. They do no range checking.
980 * @ingroup SGIextensions
983 _Unchecked_set(size_t __pos)
985 this->_M_getword(__pos) |= _Base::_S_maskbit(__pos);
990 _Unchecked_set(size_t __pos, int __val)
993 this->_M_getword(__pos) |= _Base::_S_maskbit(__pos);
995 this->_M_getword(__pos) &= ~_Base::_S_maskbit(__pos);
1000 _Unchecked_reset(size_t __pos)
1002 this->_M_getword(__pos) &= ~_Base::_S_maskbit(__pos);
1007 _Unchecked_flip(size_t __pos)
1009 this->_M_getword(__pos) ^= _Base::_S_maskbit(__pos);
1014 _Unchecked_test(size_t __pos) const
1015 { return ((this->_M_getword(__pos) & _Base::_S_maskbit(__pos))
1016 != static_cast<_WordT>(0)); }
1019 // Set, reset, and flip.
1021 * @brief Sets every bit to true.
1027 this->_M_do_sanitize();
1032 * @brief Sets a given bit to a particular value.
1033 * @param position The index of the bit.
1034 * @param val Either true or false, defaults to true.
1035 * @throw std::out_of_range If @a pos is bigger the size of the %set.
1038 set(size_t __position, bool __val = true)
1040 if (__position >= _Nb)
1041 __throw_out_of_range(__N("bitset::set"));
1042 return _Unchecked_set(__position, __val);
1046 * @brief Sets every bit to false.
1051 this->_M_do_reset();
1056 * @brief Sets a given bit to false.
1057 * @param position The index of the bit.
1058 * @throw std::out_of_range If @a pos is bigger the size of the %set.
1060 * Same as writing @c set(pos,false).
1063 reset(size_t __position)
1065 if (__position >= _Nb)
1066 __throw_out_of_range(__N("bitset::reset"));
1067 return _Unchecked_reset(__position);
1071 * @brief Toggles every bit to its opposite value.
1077 this->_M_do_sanitize();
1082 * @brief Toggles a given bit to its opposite value.
1083 * @param position The index of the bit.
1084 * @throw std::out_of_range If @a pos is bigger the size of the %set.
1087 flip(size_t __position)
1089 if (__position >= _Nb)
1090 __throw_out_of_range(__N("bitset::flip"));
1091 return _Unchecked_flip(__position);
1094 /// See the no-argument flip().
1097 { return bitset<_Nb>(*this).flip(); }
1101 * @brief Array-indexing support.
1102 * @param position Index into the %bitset.
1103 * @return A bool for a <em>const %bitset</em>. For non-const
1104 * bitsets, an instance of the reference proxy class.
1105 * @note These operators do no range checking and throw no exceptions,
1106 * as required by DR 11 to the standard.
1108 * _GLIBCXX_RESOLVE_LIB_DEFECTS Note that this implementation already
1109 * resolves DR 11 (items 1 and 2), but does not do the range-checking
1110 * required by that DR's resolution. -pme
1111 * The DR has since been changed: range-checking is a precondition
1112 * (users' responsibility), and these functions must not throw. -pme
1115 operator[](size_t __position)
1116 { return reference(*this, __position); }
1119 operator[](size_t __position) const
1120 { return _Unchecked_test(__position); }
1124 * @brief Returns a numerical interpretation of the %bitset.
1125 * @return The integral equivalent of the bits.
1126 * @throw std::overflow_error If there are too many bits to be
1127 * represented in an @c unsigned @c long.
1131 { return this->_M_do_to_ulong(); }
1133 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1136 { return this->_M_do_to_ullong(); }
1140 * @brief Returns a character interpretation of the %bitset.
1141 * @return The string equivalent of the bits.
1143 * Note the ordering of the bits: decreasing character positions
1144 * correspond to increasing bit positions (see the main class notes for
1147 template<class _CharT, class _Traits, class _Alloc>
1148 std::basic_string<_CharT, _Traits, _Alloc>
1151 std::basic_string<_CharT, _Traits, _Alloc> __result;
1152 _M_copy_to_string(__result, _CharT('0'), _CharT('1'));
1156 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1157 // 396. what are characters zero and one.
1158 template<class _CharT, class _Traits, class _Alloc>
1159 std::basic_string<_CharT, _Traits, _Alloc>
1160 to_string(_CharT __zero, _CharT __one = _CharT('1')) const
1162 std::basic_string<_CharT, _Traits, _Alloc> __result;
1163 _M_copy_to_string(__result, __zero, __one);
1167 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1168 // 434. bitset::to_string() hard to use.
1169 template<class _CharT, class _Traits>
1170 std::basic_string<_CharT, _Traits, std::allocator<_CharT> >
1172 { return to_string<_CharT, _Traits, std::allocator<_CharT> >(); }
1174 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1175 // 853. to_string needs updating with zero and one.
1176 template<class _CharT, class _Traits>
1177 std::basic_string<_CharT, _Traits, std::allocator<_CharT> >
1178 to_string(_CharT __zero, _CharT __one = _CharT('1')) const
1179 { return to_string<_CharT, _Traits,
1180 std::allocator<_CharT> >(__zero, __one); }
1182 template<class _CharT>
1183 std::basic_string<_CharT, std::char_traits<_CharT>,
1184 std::allocator<_CharT> >
1187 return to_string<_CharT, std::char_traits<_CharT>,
1188 std::allocator<_CharT> >();
1191 template<class _CharT>
1192 std::basic_string<_CharT, std::char_traits<_CharT>,
1193 std::allocator<_CharT> >
1194 to_string(_CharT __zero, _CharT __one = _CharT('1')) const
1196 return to_string<_CharT, std::char_traits<_CharT>,
1197 std::allocator<_CharT> >(__zero, __one);
1200 std::basic_string<char, std::char_traits<char>, std::allocator<char> >
1203 return to_string<char, std::char_traits<char>,
1204 std::allocator<char> >();
1207 std::basic_string<char, std::char_traits<char>, std::allocator<char> >
1208 to_string(char __zero, char __one = '1') const
1210 return to_string<char, std::char_traits<char>,
1211 std::allocator<char> >(__zero, __one);
1214 // Helper functions for string operations.
1215 template<class _CharT, class _Traits>
1217 _M_copy_from_ptr(const _CharT*, size_t, size_t, size_t,
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 _CharT __zero, _CharT __one)
1225 { _M_copy_from_ptr<_CharT, _Traits>(__s.data(), __s.size(), __pos, __n,
1228 template<class _CharT, class _Traits, class _Alloc>
1230 _M_copy_to_string(std::basic_string<_CharT, _Traits, _Alloc>&,
1231 _CharT, _CharT) const;
1233 // NB: Backward compat.
1234 template<class _CharT, class _Traits, class _Alloc>
1236 _M_copy_from_string(const std::basic_string<_CharT,
1237 _Traits, _Alloc>& __s, size_t __pos, size_t __n)
1238 { _M_copy_from_string(__s, __pos, __n, _CharT('0'), _CharT('1')); }
1240 template<class _CharT, class _Traits, class _Alloc>
1242 _M_copy_to_string(std::basic_string<_CharT, _Traits,_Alloc>& __s) const
1243 { _M_copy_to_string(__s, _CharT('0'), _CharT('1')); }
1245 /// Returns the number of bits which are set.
1248 { return this->_M_do_count(); }
1250 /// Returns the total number of bits.
1251 _GLIBCXX_CONSTEXPR size_t
1256 /// These comparisons for equality/inequality are, well, @e bitwise.
1258 operator==(const bitset<_Nb>& __rhs) const
1259 { return this->_M_is_equal(__rhs); }
1262 operator!=(const bitset<_Nb>& __rhs) const
1263 { return !this->_M_is_equal(__rhs); }
1267 * @brief Tests the value of a bit.
1268 * @param position The index of a bit.
1269 * @return The value at @a pos.
1270 * @throw std::out_of_range If @a pos is bigger the size of the %set.
1273 test(size_t __position) const
1275 if (__position >= _Nb)
1276 __throw_out_of_range(__N("bitset::test"));
1277 return _Unchecked_test(__position);
1280 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1281 // DR 693. std::bitset::all() missing.
1283 * @brief Tests whether all the bits are on.
1284 * @return True if all the bits are set.
1288 { return this->_M_are_all_aux() == _Nb; }
1291 * @brief Tests whether any of the bits are on.
1292 * @return True if at least one bit is set.
1296 { return this->_M_is_any(); }
1299 * @brief Tests whether any of the bits are on.
1300 * @return True if none of the bits are set.
1304 { return !this->_M_is_any(); }
1307 /// Self-explanatory.
1309 operator<<(size_t __position) const
1310 { return bitset<_Nb>(*this) <<= __position; }
1313 operator>>(size_t __position) const
1314 { return bitset<_Nb>(*this) >>= __position; }
1318 * @brief Finds the index of the first "on" bit.
1319 * @return The index of the first bit set, or size() if not found.
1320 * @ingroup SGIextensions
1325 { return this->_M_do_find_first(_Nb); }
1328 * @brief Finds the index of the next "on" bit after prev.
1329 * @return The index of the next bit set, or size() if not found.
1330 * @param prev Where to start searching.
1331 * @ingroup SGIextensions
1335 _Find_next(size_t __prev ) const
1336 { return this->_M_do_find_next(__prev, _Nb); }
1339 // Definitions of non-inline member functions.
1340 template<size_t _Nb>
1341 template<class _CharT, class _Traits>
1344 _M_copy_from_ptr(const _CharT* __s, size_t __len,
1345 size_t __pos, size_t __n, _CharT __zero, _CharT __one)
1348 const size_t __nbits = std::min(_Nb, std::min(__n, __len - __pos));
1349 for (size_t __i = __nbits; __i > 0; --__i)
1351 const _CharT __c = __s[__pos + __nbits - __i];
1352 if (_Traits::eq(__c, __zero))
1354 else if (_Traits::eq(__c, __one))
1355 _Unchecked_set(__i - 1);
1357 __throw_invalid_argument(__N("bitset::_M_copy_from_ptr"));
1361 template<size_t _Nb>
1362 template<class _CharT, class _Traits, class _Alloc>
1365 _M_copy_to_string(std::basic_string<_CharT, _Traits, _Alloc>& __s,
1366 _CharT __zero, _CharT __one) const
1368 __s.assign(_Nb, __zero);
1369 for (size_t __i = _Nb; __i > 0; --__i)
1370 if (_Unchecked_test(__i - 1))
1371 _Traits::assign(__s[_Nb - __i], __one);
1374 // 23.3.5.3 bitset operations:
1377 * @brief Global bitwise operations on bitsets.
1378 * @param x A bitset.
1379 * @param y A bitset of the same size as @a x.
1380 * @return A new bitset.
1382 * These should be self-explanatory.
1384 template<size_t _Nb>
1386 operator&(const bitset<_Nb>& __x, const bitset<_Nb>& __y)
1388 bitset<_Nb> __result(__x);
1393 template<size_t _Nb>
1395 operator|(const bitset<_Nb>& __x, const bitset<_Nb>& __y)
1397 bitset<_Nb> __result(__x);
1402 template <size_t _Nb>
1404 operator^(const bitset<_Nb>& __x, const bitset<_Nb>& __y)
1406 bitset<_Nb> __result(__x);
1414 * @brief Global I/O operators for bitsets.
1416 * Direct I/O between streams and bitsets is supported. Output is
1417 * straightforward. Input will skip whitespace, only accept @a 0 and @a 1
1418 * characters, and will only extract as many digits as the %bitset will
1421 template<class _CharT, class _Traits, size_t _Nb>
1422 std::basic_istream<_CharT, _Traits>&
1423 operator>>(std::basic_istream<_CharT, _Traits>& __is, bitset<_Nb>& __x)
1425 typedef typename _Traits::char_type char_type;
1426 typedef std::basic_istream<_CharT, _Traits> __istream_type;
1427 typedef typename __istream_type::ios_base __ios_base;
1429 std::basic_string<_CharT, _Traits> __tmp;
1432 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1433 // 303. Bitset input operator underspecified
1434 const char_type __zero = __is.widen('0');
1435 const char_type __one = __is.widen('1');
1437 typename __ios_base::iostate __state = __ios_base::goodbit;
1438 typename __istream_type::sentry __sentry(__is);
1443 for (size_t __i = _Nb; __i > 0; --__i)
1445 static typename _Traits::int_type __eof = _Traits::eof();
1447 typename _Traits::int_type __c1 = __is.rdbuf()->sbumpc();
1448 if (_Traits::eq_int_type(__c1, __eof))
1450 __state |= __ios_base::eofbit;
1455 const char_type __c2 = _Traits::to_char_type(__c1);
1456 if (_Traits::eq(__c2, __zero))
1457 __tmp.push_back(__zero);
1458 else if (_Traits::eq(__c2, __one))
1459 __tmp.push_back(__one);
1461 eq_int_type(__is.rdbuf()->sputbackc(__c2),
1464 __state |= __ios_base::failbit;
1470 __catch(__cxxabiv1::__forced_unwind&)
1472 __is._M_setstate(__ios_base::badbit);
1473 __throw_exception_again;
1476 { __is._M_setstate(__ios_base::badbit); }
1479 if (__tmp.empty() && _Nb)
1480 __state |= __ios_base::failbit;
1482 __x._M_copy_from_string(__tmp, static_cast<size_t>(0), _Nb,
1485 __is.setstate(__state);
1489 template <class _CharT, class _Traits, size_t _Nb>
1490 std::basic_ostream<_CharT, _Traits>&
1491 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1492 const bitset<_Nb>& __x)
1494 std::basic_string<_CharT, _Traits> __tmp;
1496 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1497 // 396. what are characters zero and one.
1498 const ctype<_CharT>& __ct = use_facet<ctype<_CharT> >(__os.getloc());
1499 __x._M_copy_to_string(__tmp, __ct.widen('0'), __ct.widen('1'));
1500 return __os << __tmp;
1504 _GLIBCXX_END_NESTED_NAMESPACE
1506 #undef _GLIBCXX_BITSET_WORDS
1507 #undef _GLIBCXX_BITSET_BITS_PER_WORD
1509 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1511 #include <bits/functional_hash.h>
1513 _GLIBCXX_BEGIN_NAMESPACE(std)
1516 /// std::hash specialization for bitset.
1517 template<size_t _Nb>
1518 struct hash<_GLIBCXX_STD_D::bitset<_Nb>>
1519 : public __hash_base<size_t, _GLIBCXX_STD_D::bitset<_Nb>>
1522 operator()(const _GLIBCXX_STD_D::bitset<_Nb>& __b) const
1524 const size_t __clength = (_Nb + __CHAR_BIT__ - 1) / __CHAR_BIT__;
1525 return std::_Hash_impl::hash(__b._M_getdata(), __clength);
1530 struct hash<_GLIBCXX_STD_D::bitset<0>>
1531 : public __hash_base<size_t, _GLIBCXX_STD_D::bitset<0>>
1534 operator()(const _GLIBCXX_STD_D::bitset<0>&) const
1538 _GLIBCXX_END_NAMESPACE
1540 #endif // __GXX_EXPERIMENTAL_CXX0X__
1542 #ifdef _GLIBCXX_DEBUG
1543 # include <debug/bitset>
1546 #ifdef _GLIBCXX_PROFILE
1547 # include <profile/bitset>
1550 #endif /* _GLIBCXX_BITSET */