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() _GLIBCXX_NOEXCEPT
81 #ifdef __GXX_EXPERIMENTAL_CXX0X__
82 constexpr _Base_bitset(unsigned long long __val) noexcept
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 ) _GLIBCXX_NOEXCEPT
96 { return __pos / _GLIBCXX_BITSET_BITS_PER_WORD; }
98 static _GLIBCXX_CONSTEXPR size_t
99 _S_whichbyte(size_t __pos ) _GLIBCXX_NOEXCEPT
100 { return (__pos % _GLIBCXX_BITSET_BITS_PER_WORD) / __CHAR_BIT__; }
102 static _GLIBCXX_CONSTEXPR size_t
103 _S_whichbit(size_t __pos ) _GLIBCXX_NOEXCEPT
104 { return __pos % _GLIBCXX_BITSET_BITS_PER_WORD; }
106 static _GLIBCXX_CONSTEXPR _WordT
107 _S_maskbit(size_t __pos ) _GLIBCXX_NOEXCEPT
108 { return (static_cast<_WordT>(1)) << _S_whichbit(__pos); }
111 _M_getword(size_t __pos) _GLIBCXX_NOEXCEPT
112 { return _M_w[_S_whichword(__pos)]; }
114 _GLIBCXX_CONSTEXPR _WordT
115 _M_getword(size_t __pos) const _GLIBCXX_NOEXCEPT
116 { return _M_w[_S_whichword(__pos)]; }
118 #ifdef __GXX_EXPERIMENTAL_CXX0X__
120 _M_getdata() const noexcept
125 _M_hiword() _GLIBCXX_NOEXCEPT
126 { return _M_w[_Nw - 1]; }
128 _GLIBCXX_CONSTEXPR _WordT
129 _M_hiword() const _GLIBCXX_NOEXCEPT
130 { return _M_w[_Nw - 1]; }
133 _M_do_and(const _Base_bitset<_Nw>& __x) _GLIBCXX_NOEXCEPT
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) _GLIBCXX_NOEXCEPT
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) _GLIBCXX_NOEXCEPT
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) _GLIBCXX_NOEXCEPT;
157 _M_do_right_shift(size_t __shift) _GLIBCXX_NOEXCEPT;
160 _M_do_flip() _GLIBCXX_NOEXCEPT
162 for (size_t __i = 0; __i < _Nw; __i++)
163 _M_w[__i] = ~_M_w[__i];
167 _M_do_set() _GLIBCXX_NOEXCEPT
169 for (size_t __i = 0; __i < _Nw; __i++)
170 _M_w[__i] = ~static_cast<_WordT>(0);
174 _M_do_reset() _GLIBCXX_NOEXCEPT
175 { __builtin_memset(_M_w, 0, _Nw * sizeof(_WordT)); }
178 _M_is_equal(const _Base_bitset<_Nw>& __x) const _GLIBCXX_NOEXCEPT
180 for (size_t __i = 0; __i < _Nw; ++__i)
181 if (_M_w[__i] != __x._M_w[__i])
187 _M_are_all_aux() const _GLIBCXX_NOEXCEPT
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()));
197 _M_is_any() const _GLIBCXX_NOEXCEPT
199 for (size_t __i = 0; __i < _Nw; __i++)
200 if (_M_w[__i] != static_cast<_WordT>(0))
206 _M_do_count() const _GLIBCXX_NOEXCEPT
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) const _GLIBCXX_NOEXCEPT;
226 // find the next "on" bit that follows "prev"
228 _M_do_find_next(size_t, size_t) const _GLIBCXX_NOEXCEPT;
231 // Definitions of non-inline functions from _Base_bitset.
234 _Base_bitset<_Nw>::_M_do_left_shift(size_t __shift) _GLIBCXX_NOEXCEPT
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) _GLIBCXX_NOEXCEPT
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);
315 _M_do_find_first(size_t __not_found) const _GLIBCXX_NOEXCEPT
317 for (size_t __i = 0; __i < _Nw; __i++)
319 _WordT __thisword = _M_w[__i];
320 if (__thisword != static_cast<_WordT>(0))
321 return (__i * _GLIBCXX_BITSET_BITS_PER_WORD
322 + __builtin_ctzl(__thisword));
324 // not found, so return an indication of failure.
331 _M_do_find_next(size_t __prev, size_t __not_found) const _GLIBCXX_NOEXCEPT
333 // make bound inclusive
336 // check out of bounds
337 if (__prev >= _Nw * _GLIBCXX_BITSET_BITS_PER_WORD)
341 size_t __i = _S_whichword(__prev);
342 _WordT __thisword = _M_w[__i];
344 // mask off bits below bound
345 __thisword &= (~static_cast<_WordT>(0)) << _S_whichbit(__prev);
347 if (__thisword != static_cast<_WordT>(0))
348 return (__i * _GLIBCXX_BITSET_BITS_PER_WORD
349 + __builtin_ctzl(__thisword));
351 // check subsequent words
353 for (; __i < _Nw; __i++)
355 __thisword = _M_w[__i];
356 if (__thisword != static_cast<_WordT>(0))
357 return (__i * _GLIBCXX_BITSET_BITS_PER_WORD
358 + __builtin_ctzl(__thisword));
360 // not found, so return an indication of failure.
362 } // end _M_do_find_next
365 * Base class, specialization for a single word.
367 * See documentation for bitset.
370 struct _Base_bitset<1>
372 typedef unsigned long _WordT;
375 _GLIBCXX_CONSTEXPR _Base_bitset() _GLIBCXX_NOEXCEPT
379 #ifdef __GXX_EXPERIMENTAL_CXX0X__
380 constexpr _Base_bitset(unsigned long long __val) noexcept
382 _Base_bitset(unsigned long __val)
387 static _GLIBCXX_CONSTEXPR size_t
388 _S_whichword(size_t __pos ) _GLIBCXX_NOEXCEPT
389 { return __pos / _GLIBCXX_BITSET_BITS_PER_WORD; }
391 static _GLIBCXX_CONSTEXPR size_t
392 _S_whichbyte(size_t __pos ) _GLIBCXX_NOEXCEPT
393 { return (__pos % _GLIBCXX_BITSET_BITS_PER_WORD) / __CHAR_BIT__; }
395 static _GLIBCXX_CONSTEXPR size_t
396 _S_whichbit(size_t __pos ) _GLIBCXX_NOEXCEPT
397 { return __pos % _GLIBCXX_BITSET_BITS_PER_WORD; }
399 static _GLIBCXX_CONSTEXPR _WordT
400 _S_maskbit(size_t __pos ) _GLIBCXX_NOEXCEPT
401 { return (static_cast<_WordT>(1)) << _S_whichbit(__pos); }
404 _M_getword(size_t) _GLIBCXX_NOEXCEPT
407 _GLIBCXX_CONSTEXPR _WordT
408 _M_getword(size_t) const _GLIBCXX_NOEXCEPT
411 #ifdef __GXX_EXPERIMENTAL_CXX0X__
413 _M_getdata() const noexcept
418 _M_hiword() _GLIBCXX_NOEXCEPT
421 _GLIBCXX_CONSTEXPR _WordT
422 _M_hiword() const _GLIBCXX_NOEXCEPT
426 _M_do_and(const _Base_bitset<1>& __x) _GLIBCXX_NOEXCEPT
427 { _M_w &= __x._M_w; }
430 _M_do_or(const _Base_bitset<1>& __x) _GLIBCXX_NOEXCEPT
431 { _M_w |= __x._M_w; }
434 _M_do_xor(const _Base_bitset<1>& __x) _GLIBCXX_NOEXCEPT
435 { _M_w ^= __x._M_w; }
438 _M_do_left_shift(size_t __shift) _GLIBCXX_NOEXCEPT
439 { _M_w <<= __shift; }
442 _M_do_right_shift(size_t __shift) _GLIBCXX_NOEXCEPT
443 { _M_w >>= __shift; }
446 _M_do_flip() _GLIBCXX_NOEXCEPT
450 _M_do_set() _GLIBCXX_NOEXCEPT
451 { _M_w = ~static_cast<_WordT>(0); }
454 _M_do_reset() _GLIBCXX_NOEXCEPT
458 _M_is_equal(const _Base_bitset<1>& __x) const _GLIBCXX_NOEXCEPT
459 { return _M_w == __x._M_w; }
462 _M_are_all_aux() const _GLIBCXX_NOEXCEPT
463 { return __builtin_popcountl(_M_w); }
466 _M_is_any() const _GLIBCXX_NOEXCEPT
467 { return _M_w != 0; }
470 _M_do_count() const _GLIBCXX_NOEXCEPT
471 { return __builtin_popcountl(_M_w); }
474 _M_do_to_ulong() const _GLIBCXX_NOEXCEPT
477 #ifdef __GXX_EXPERIMENTAL_CXX0X__
479 _M_do_to_ullong() const noexcept
484 _M_do_find_first(size_t __not_found) const _GLIBCXX_NOEXCEPT
487 return __builtin_ctzl(_M_w);
492 // find the next "on" bit that follows "prev"
494 _M_do_find_next(size_t __prev, size_t __not_found) const
498 if (__prev >= ((size_t) _GLIBCXX_BITSET_BITS_PER_WORD))
501 _WordT __x = _M_w >> __prev;
503 return __builtin_ctzl(__x) + __prev;
510 * Base class, specialization for no storage (zero-length %bitset).
512 * See documentation for bitset.
515 struct _Base_bitset<0>
517 typedef unsigned long _WordT;
519 _GLIBCXX_CONSTEXPR _Base_bitset() _GLIBCXX_NOEXCEPT
522 #ifdef __GXX_EXPERIMENTAL_CXX0X__
523 constexpr _Base_bitset(unsigned long long) noexcept
525 _Base_bitset(unsigned long)
529 static _GLIBCXX_CONSTEXPR size_t
530 _S_whichword(size_t __pos ) _GLIBCXX_NOEXCEPT
531 { return __pos / _GLIBCXX_BITSET_BITS_PER_WORD; }
533 static _GLIBCXX_CONSTEXPR size_t
534 _S_whichbyte(size_t __pos ) _GLIBCXX_NOEXCEPT
535 { return (__pos % _GLIBCXX_BITSET_BITS_PER_WORD) / __CHAR_BIT__; }
537 static _GLIBCXX_CONSTEXPR size_t
538 _S_whichbit(size_t __pos ) _GLIBCXX_NOEXCEPT
539 { return __pos % _GLIBCXX_BITSET_BITS_PER_WORD; }
541 static _GLIBCXX_CONSTEXPR _WordT
542 _S_maskbit(size_t __pos ) _GLIBCXX_NOEXCEPT
543 { return (static_cast<_WordT>(1)) << _S_whichbit(__pos); }
545 // This would normally give access to the data. The bounds-checking
546 // in the bitset class will prevent the user from getting this far,
547 // but (1) it must still return an lvalue to compile, and (2) the
548 // user might call _Unchecked_set directly, in which case this /needs/
549 // to fail. Let's not penalize zero-length users unless they actually
550 // make an unchecked call; all the memory ugliness is therefore
551 // localized to this single should-never-get-this-far function.
553 _M_getword(size_t) _GLIBCXX_NOEXCEPT
555 __throw_out_of_range(__N("_Base_bitset::_M_getword"));
559 _GLIBCXX_CONSTEXPR _WordT
560 _M_getword(size_t __pos) const _GLIBCXX_NOEXCEPT
563 _GLIBCXX_CONSTEXPR _WordT
564 _M_hiword() const _GLIBCXX_NOEXCEPT
568 _M_do_and(const _Base_bitset<0>&) _GLIBCXX_NOEXCEPT
572 _M_do_or(const _Base_bitset<0>&) _GLIBCXX_NOEXCEPT
576 _M_do_xor(const _Base_bitset<0>&) _GLIBCXX_NOEXCEPT
580 _M_do_left_shift(size_t) _GLIBCXX_NOEXCEPT
584 _M_do_right_shift(size_t) _GLIBCXX_NOEXCEPT
588 _M_do_flip() _GLIBCXX_NOEXCEPT
592 _M_do_set() _GLIBCXX_NOEXCEPT
596 _M_do_reset() _GLIBCXX_NOEXCEPT
599 // Are all empty bitsets equal to each other? Are they equal to
600 // themselves? How to compare a thing which has no state? What is
601 // the sound of one zero-length bitset clapping?
603 _M_is_equal(const _Base_bitset<0>&) const _GLIBCXX_NOEXCEPT
607 _M_are_all_aux() const _GLIBCXX_NOEXCEPT
611 _M_is_any() const _GLIBCXX_NOEXCEPT
615 _M_do_count() const _GLIBCXX_NOEXCEPT
619 _M_do_to_ulong() const _GLIBCXX_NOEXCEPT
622 #ifdef __GXX_EXPERIMENTAL_CXX0X__
624 _M_do_to_ullong() const noexcept
628 // Normally "not found" is the size, but that could also be
629 // misinterpreted as an index in this corner case. Oh well.
631 _M_do_find_first(size_t) const _GLIBCXX_NOEXCEPT
635 _M_do_find_next(size_t, size_t) const _GLIBCXX_NOEXCEPT
640 // Helper class to zero out the unused high-order bits in the highest word.
641 template<size_t _Extrabits>
644 typedef unsigned long _WordT;
647 _S_do_sanitize(_WordT& __val) _GLIBCXX_NOEXCEPT
648 { __val &= ~((~static_cast<_WordT>(0)) << _Extrabits); }
654 typedef unsigned long _WordT;
657 _S_do_sanitize(_WordT) _GLIBCXX_NOEXCEPT { }
661 * @brief The %bitset class represents a @e fixed-size sequence of bits.
663 * @ingroup containers
665 * (Note that %bitset does @e not meet the formal requirements of a
666 * <a href="tables.html#65">container</a>. Mainly, it lacks iterators.)
668 * The template argument, @a Nb, may be any non-negative number,
669 * specifying the number of bits (e.g., "0", "12", "1024*1024").
671 * In the general unoptimized case, storage is allocated in word-sized
672 * blocks. Let B be the number of bits in a word, then (Nb+(B-1))/B
673 * words will be used for storage. B - Nb%B bits are unused. (They are
674 * the high-order bits in the highest word.) It is a class invariant
675 * that those unused bits are always zero.
677 * If you think of %bitset as <em>a simple array of bits</em>, be
678 * aware that your mental picture is reversed: a %bitset behaves
679 * the same way as bits in integers do, with the bit at index 0 in
680 * the <em>least significant / right-hand</em> position, and the bit at
681 * index Nb-1 in the <em>most significant / left-hand</em> position.
682 * Thus, unlike other containers, a %bitset's index <em>counts from
683 * right to left</em>, to put it very loosely.
685 * This behavior is preserved when translating to and from strings. For
686 * example, the first line of the following program probably prints
687 * <em>b('a') is 0001100001</em> on a modern ASCII system.
691 * #include <iostream>
694 * using namespace std;
701 * cout << "b('a') is " << b << endl;
705 * string str = s.str();
706 * cout << "index 3 in the string is " << str[3] << " but\n"
707 * << "index 3 in the bitset is " << b[3] << endl;
712 * http://gcc.gnu.org/onlinedocs/libstdc++/manual/bk01pt12ch33s02.html
713 * for a description of extensions.
715 * Most of the actual code isn't contained in %bitset<> itself, but in the
716 * base class _Base_bitset. The base class works with whole words, not with
717 * individual bits. This allows us to specialize _Base_bitset for the
718 * important special case where the %bitset is only a single word.
720 * Extra confusion can result due to the fact that the storage for
721 * _Base_bitset @e is a regular array, and is indexed as such. This is
722 * carefully encapsulated.
726 : private _Base_bitset<_GLIBCXX_BITSET_WORDS(_Nb)>
729 typedef _Base_bitset<_GLIBCXX_BITSET_WORDS(_Nb)> _Base;
730 typedef unsigned long _WordT;
733 _M_do_sanitize() _GLIBCXX_NOEXCEPT
735 typedef _Sanitize<_Nb % _GLIBCXX_BITSET_BITS_PER_WORD> __sanitize_type;
736 __sanitize_type::_S_do_sanitize(this->_M_hiword());
739 #ifdef __GXX_EXPERIMENTAL_CXX0X__
740 template<typename> friend class hash;
745 * This encapsulates the concept of a single bit. An instance of this
746 * class is a proxy for an actual bit; this way the individual bit
747 * operations are done as faster word-size bitwise instructions.
749 * Most users will never need to use this class directly; conversions
750 * to and from bool are automatic and should be transparent. Overloaded
751 * operators help to preserve the illusion.
753 * (On a typical system, this <em>bit %reference</em> is 64
754 * times the size of an actual bit. Ha.)
767 reference(bitset& __b, size_t __pos) _GLIBCXX_NOEXCEPT
769 _M_wp = &__b._M_getword(__pos);
770 _M_bpos = _Base::_S_whichbit(__pos);
773 ~reference() _GLIBCXX_NOEXCEPT
778 operator=(bool __x) _GLIBCXX_NOEXCEPT
781 *_M_wp |= _Base::_S_maskbit(_M_bpos);
783 *_M_wp &= ~_Base::_S_maskbit(_M_bpos);
787 // For b[i] = b[__j];
789 operator=(const reference& __j) _GLIBCXX_NOEXCEPT
791 if ((*(__j._M_wp) & _Base::_S_maskbit(__j._M_bpos)))
792 *_M_wp |= _Base::_S_maskbit(_M_bpos);
794 *_M_wp &= ~_Base::_S_maskbit(_M_bpos);
800 operator~() const _GLIBCXX_NOEXCEPT
801 { return (*(_M_wp) & _Base::_S_maskbit(_M_bpos)) == 0; }
804 operator bool() const _GLIBCXX_NOEXCEPT
805 { return (*(_M_wp) & _Base::_S_maskbit(_M_bpos)) != 0; }
809 flip() _GLIBCXX_NOEXCEPT
811 *_M_wp ^= _Base::_S_maskbit(_M_bpos);
815 friend class reference;
817 // 23.3.5.1 constructors:
818 /// All bits set to zero.
819 _GLIBCXX_CONSTEXPR bitset() _GLIBCXX_NOEXCEPT
822 /// Initial bits bitwise-copied from a single word (others set to zero).
823 #ifdef __GXX_EXPERIMENTAL_CXX0X__
824 constexpr bitset(unsigned long long __val) noexcept
827 bitset(unsigned long __val)
829 { _M_do_sanitize(); }
833 * @brief Use a subset of a string.
834 * @param s A string of @a 0 and @a 1 characters.
835 * @param position Index of the first character in @a s to use;
837 * @throw std::out_of_range If @a pos is bigger the size of @a s.
838 * @throw std::invalid_argument If a character appears in the string
839 * which is neither @a 0 nor @a 1.
841 template<class _CharT, class _Traits, class _Alloc>
843 bitset(const std::basic_string<_CharT, _Traits, _Alloc>& __s,
844 size_t __position = 0)
847 if (__position > __s.size())
848 __throw_out_of_range(__N("bitset::bitset initial position "
850 _M_copy_from_string(__s, __position,
851 std::basic_string<_CharT, _Traits, _Alloc>::npos,
852 _CharT('0'), _CharT('1'));
856 * @brief Use a subset of a string.
857 * @param s A string of @a 0 and @a 1 characters.
858 * @param position Index of the first character in @a s to use.
859 * @param n The number of characters to copy.
860 * @throw std::out_of_range If @a pos is bigger the size of @a s.
861 * @throw std::invalid_argument If a character appears in the string
862 * which is neither @a 0 nor @a 1.
864 template<class _CharT, class _Traits, class _Alloc>
865 bitset(const std::basic_string<_CharT, _Traits, _Alloc>& __s,
866 size_t __position, size_t __n)
869 if (__position > __s.size())
870 __throw_out_of_range(__N("bitset::bitset initial position "
872 _M_copy_from_string(__s, __position, __n, _CharT('0'), _CharT('1'));
875 // _GLIBCXX_RESOLVE_LIB_DEFECTS
876 // 396. what are characters zero and one.
877 template<class _CharT, class _Traits, class _Alloc>
878 bitset(const std::basic_string<_CharT, _Traits, _Alloc>& __s,
879 size_t __position, size_t __n,
880 _CharT __zero, _CharT __one = _CharT('1'))
883 if (__position > __s.size())
884 __throw_out_of_range(__N("bitset::bitset initial position "
886 _M_copy_from_string(__s, __position, __n, __zero, __one);
889 #ifdef __GXX_EXPERIMENTAL_CXX0X__
891 * @brief Construct from a character %array.
892 * @param str An %array of characters @a zero and @a one.
893 * @param n The number of characters to use.
894 * @param zero The character corresponding to the value 0.
895 * @param one The character corresponding to the value 1.
896 * @throw std::invalid_argument If a character appears in the string
897 * which is neither @a zero nor @a one.
899 template<typename _CharT>
901 bitset(const _CharT* __str,
902 typename std::basic_string<_CharT>::size_type __n
903 = std::basic_string<_CharT>::npos,
904 _CharT __zero = _CharT('0'), _CharT __one = _CharT('1'))
908 __throw_logic_error(__N("bitset::bitset(const _CharT*, ...)"));
910 if (__n == std::basic_string<_CharT>::npos)
911 __n = std::char_traits<_CharT>::length(__str);
912 _M_copy_from_ptr<_CharT, std::char_traits<_CharT>>(__str, __n, 0,
918 // 23.3.5.2 bitset operations:
921 * @brief Operations on bitsets.
922 * @param rhs A same-sized bitset.
924 * These should be self-explanatory.
927 operator&=(const bitset<_Nb>& __rhs) _GLIBCXX_NOEXCEPT
929 this->_M_do_and(__rhs);
934 operator|=(const bitset<_Nb>& __rhs) _GLIBCXX_NOEXCEPT
936 this->_M_do_or(__rhs);
941 operator^=(const bitset<_Nb>& __rhs) _GLIBCXX_NOEXCEPT
943 this->_M_do_xor(__rhs);
950 * @brief Operations on bitsets.
951 * @param position The number of places to shift.
953 * These should be self-explanatory.
956 operator<<=(size_t __position) _GLIBCXX_NOEXCEPT
958 if (__builtin_expect(__position < _Nb, 1))
960 this->_M_do_left_shift(__position);
961 this->_M_do_sanitize();
969 operator>>=(size_t __position) _GLIBCXX_NOEXCEPT
971 if (__builtin_expect(__position < _Nb, 1))
973 this->_M_do_right_shift(__position);
974 this->_M_do_sanitize();
984 * These versions of single-bit set, reset, flip, and test are
985 * extensions from the SGI version. They do no range checking.
986 * @ingroup SGIextensions
989 _Unchecked_set(size_t __pos) _GLIBCXX_NOEXCEPT
991 this->_M_getword(__pos) |= _Base::_S_maskbit(__pos);
996 _Unchecked_set(size_t __pos, int __val) _GLIBCXX_NOEXCEPT
999 this->_M_getword(__pos) |= _Base::_S_maskbit(__pos);
1001 this->_M_getword(__pos) &= ~_Base::_S_maskbit(__pos);
1006 _Unchecked_reset(size_t __pos) _GLIBCXX_NOEXCEPT
1008 this->_M_getword(__pos) &= ~_Base::_S_maskbit(__pos);
1013 _Unchecked_flip(size_t __pos) _GLIBCXX_NOEXCEPT
1015 this->_M_getword(__pos) ^= _Base::_S_maskbit(__pos);
1019 _GLIBCXX_CONSTEXPR bool
1020 _Unchecked_test(size_t __pos) const _GLIBCXX_NOEXCEPT
1021 { return ((this->_M_getword(__pos) & _Base::_S_maskbit(__pos))
1022 != static_cast<_WordT>(0)); }
1025 // Set, reset, and flip.
1027 * @brief Sets every bit to true.
1030 set() _GLIBCXX_NOEXCEPT
1033 this->_M_do_sanitize();
1038 * @brief Sets a given bit to a particular value.
1039 * @param position The index of the bit.
1040 * @param val Either true or false, defaults to true.
1041 * @throw std::out_of_range If @a pos is bigger the size of the %set.
1044 set(size_t __position, bool __val = true)
1046 if (__position >= _Nb)
1047 __throw_out_of_range(__N("bitset::set"));
1048 return _Unchecked_set(__position, __val);
1052 * @brief Sets every bit to false.
1055 reset() _GLIBCXX_NOEXCEPT
1057 this->_M_do_reset();
1062 * @brief Sets a given bit to false.
1063 * @param position The index of the bit.
1064 * @throw std::out_of_range If @a pos is bigger the size of the %set.
1066 * Same as writing @c set(pos,false).
1069 reset(size_t __position)
1071 if (__position >= _Nb)
1072 __throw_out_of_range(__N("bitset::reset"));
1073 return _Unchecked_reset(__position);
1077 * @brief Toggles every bit to its opposite value.
1080 flip() _GLIBCXX_NOEXCEPT
1083 this->_M_do_sanitize();
1088 * @brief Toggles a given bit to its opposite value.
1089 * @param position The index of the bit.
1090 * @throw std::out_of_range If @a pos is bigger the size of the %set.
1093 flip(size_t __position)
1095 if (__position >= _Nb)
1096 __throw_out_of_range(__N("bitset::flip"));
1097 return _Unchecked_flip(__position);
1100 /// See the no-argument flip().
1102 operator~() const _GLIBCXX_NOEXCEPT
1103 { return bitset<_Nb>(*this).flip(); }
1107 * @brief Array-indexing support.
1108 * @param position Index into the %bitset.
1109 * @return A bool for a <em>const %bitset</em>. For non-const
1110 * bitsets, an instance of the reference proxy class.
1111 * @note These operators do no range checking and throw no exceptions,
1112 * as required by DR 11 to the standard.
1114 * _GLIBCXX_RESOLVE_LIB_DEFECTS Note that this implementation already
1115 * resolves DR 11 (items 1 and 2), but does not do the range-checking
1116 * required by that DR's resolution. -pme
1117 * The DR has since been changed: range-checking is a precondition
1118 * (users' responsibility), and these functions must not throw. -pme
1121 operator[](size_t __position)
1122 { return reference(*this, __position); }
1124 _GLIBCXX_CONSTEXPR bool
1125 operator[](size_t __position) const
1126 { return _Unchecked_test(__position); }
1130 * @brief Returns a numerical interpretation of the %bitset.
1131 * @return The integral equivalent of the bits.
1132 * @throw std::overflow_error If there are too many bits to be
1133 * represented in an @c unsigned @c long.
1137 { return this->_M_do_to_ulong(); }
1139 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1142 { return this->_M_do_to_ullong(); }
1146 * @brief Returns a character interpretation of the %bitset.
1147 * @return The string equivalent of the bits.
1149 * Note the ordering of the bits: decreasing character positions
1150 * correspond to increasing bit positions (see the main class notes for
1153 template<class _CharT, class _Traits, class _Alloc>
1154 std::basic_string<_CharT, _Traits, _Alloc>
1157 std::basic_string<_CharT, _Traits, _Alloc> __result;
1158 _M_copy_to_string(__result, _CharT('0'), _CharT('1'));
1162 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1163 // 396. what are characters zero and one.
1164 template<class _CharT, class _Traits, class _Alloc>
1165 std::basic_string<_CharT, _Traits, _Alloc>
1166 to_string(_CharT __zero, _CharT __one = _CharT('1')) const
1168 std::basic_string<_CharT, _Traits, _Alloc> __result;
1169 _M_copy_to_string(__result, __zero, __one);
1173 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1174 // 434. bitset::to_string() hard to use.
1175 template<class _CharT, class _Traits>
1176 std::basic_string<_CharT, _Traits, std::allocator<_CharT> >
1178 { return to_string<_CharT, _Traits, std::allocator<_CharT> >(); }
1180 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1181 // 853. to_string needs updating with zero and one.
1182 template<class _CharT, class _Traits>
1183 std::basic_string<_CharT, _Traits, std::allocator<_CharT> >
1184 to_string(_CharT __zero, _CharT __one = _CharT('1')) const
1185 { return to_string<_CharT, _Traits,
1186 std::allocator<_CharT> >(__zero, __one); }
1188 template<class _CharT>
1189 std::basic_string<_CharT, std::char_traits<_CharT>,
1190 std::allocator<_CharT> >
1193 return to_string<_CharT, std::char_traits<_CharT>,
1194 std::allocator<_CharT> >();
1197 template<class _CharT>
1198 std::basic_string<_CharT, std::char_traits<_CharT>,
1199 std::allocator<_CharT> >
1200 to_string(_CharT __zero, _CharT __one = _CharT('1')) const
1202 return to_string<_CharT, std::char_traits<_CharT>,
1203 std::allocator<_CharT> >(__zero, __one);
1206 std::basic_string<char, std::char_traits<char>, std::allocator<char> >
1209 return to_string<char, std::char_traits<char>,
1210 std::allocator<char> >();
1213 std::basic_string<char, std::char_traits<char>, std::allocator<char> >
1214 to_string(char __zero, char __one = '1') const
1216 return to_string<char, std::char_traits<char>,
1217 std::allocator<char> >(__zero, __one);
1220 // Helper functions for string operations.
1221 template<class _CharT, class _Traits>
1223 _M_copy_from_ptr(const _CharT*, size_t, size_t, size_t,
1226 template<class _CharT, class _Traits, class _Alloc>
1228 _M_copy_from_string(const std::basic_string<_CharT,
1229 _Traits, _Alloc>& __s, size_t __pos, size_t __n,
1230 _CharT __zero, _CharT __one)
1231 { _M_copy_from_ptr<_CharT, _Traits>(__s.data(), __s.size(), __pos, __n,
1234 template<class _CharT, class _Traits, class _Alloc>
1236 _M_copy_to_string(std::basic_string<_CharT, _Traits, _Alloc>&,
1237 _CharT, _CharT) const;
1239 // NB: Backward compat.
1240 template<class _CharT, class _Traits, class _Alloc>
1242 _M_copy_from_string(const std::basic_string<_CharT,
1243 _Traits, _Alloc>& __s, size_t __pos, size_t __n)
1244 { _M_copy_from_string(__s, __pos, __n, _CharT('0'), _CharT('1')); }
1246 template<class _CharT, class _Traits, class _Alloc>
1248 _M_copy_to_string(std::basic_string<_CharT, _Traits,_Alloc>& __s) const
1249 { _M_copy_to_string(__s, _CharT('0'), _CharT('1')); }
1251 /// Returns the number of bits which are set.
1253 count() const _GLIBCXX_NOEXCEPT
1254 { return this->_M_do_count(); }
1256 /// Returns the total number of bits.
1257 _GLIBCXX_CONSTEXPR size_t
1258 size() const _GLIBCXX_NOEXCEPT
1262 /// These comparisons for equality/inequality are, well, @e bitwise.
1264 operator==(const bitset<_Nb>& __rhs) const _GLIBCXX_NOEXCEPT
1265 { return this->_M_is_equal(__rhs); }
1268 operator!=(const bitset<_Nb>& __rhs) const _GLIBCXX_NOEXCEPT
1269 { return !this->_M_is_equal(__rhs); }
1273 * @brief Tests the value of a bit.
1274 * @param position The index of a bit.
1275 * @return The value at @a pos.
1276 * @throw std::out_of_range If @a pos is bigger the size of the %set.
1279 test(size_t __position) const
1281 if (__position >= _Nb)
1282 __throw_out_of_range(__N("bitset::test"));
1283 return _Unchecked_test(__position);
1286 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1287 // DR 693. std::bitset::all() missing.
1289 * @brief Tests whether all the bits are on.
1290 * @return True if all the bits are set.
1293 all() const _GLIBCXX_NOEXCEPT
1294 { return this->_M_are_all_aux() == _Nb; }
1297 * @brief Tests whether any of the bits are on.
1298 * @return True if at least one bit is set.
1301 any() const _GLIBCXX_NOEXCEPT
1302 { return this->_M_is_any(); }
1305 * @brief Tests whether any of the bits are on.
1306 * @return True if none of the bits are set.
1309 none() const _GLIBCXX_NOEXCEPT
1310 { return !this->_M_is_any(); }
1313 /// Self-explanatory.
1315 operator<<(size_t __position) const _GLIBCXX_NOEXCEPT
1316 { return bitset<_Nb>(*this) <<= __position; }
1319 operator>>(size_t __position) const _GLIBCXX_NOEXCEPT
1320 { return bitset<_Nb>(*this) >>= __position; }
1324 * @brief Finds the index of the first "on" bit.
1325 * @return The index of the first bit set, or size() if not found.
1326 * @ingroup SGIextensions
1330 _Find_first() const _GLIBCXX_NOEXCEPT
1331 { return this->_M_do_find_first(_Nb); }
1334 * @brief Finds the index of the next "on" bit after prev.
1335 * @return The index of the next bit set, or size() if not found.
1336 * @param prev Where to start searching.
1337 * @ingroup SGIextensions
1341 _Find_next(size_t __prev ) const _GLIBCXX_NOEXCEPT
1342 { return this->_M_do_find_next(__prev, _Nb); }
1345 // Definitions of non-inline member functions.
1346 template<size_t _Nb>
1347 template<class _CharT, class _Traits>
1350 _M_copy_from_ptr(const _CharT* __s, size_t __len,
1351 size_t __pos, size_t __n, _CharT __zero, _CharT __one)
1354 const size_t __nbits = std::min(_Nb, std::min(__n, __len - __pos));
1355 for (size_t __i = __nbits; __i > 0; --__i)
1357 const _CharT __c = __s[__pos + __nbits - __i];
1358 if (_Traits::eq(__c, __zero))
1360 else if (_Traits::eq(__c, __one))
1361 _Unchecked_set(__i - 1);
1363 __throw_invalid_argument(__N("bitset::_M_copy_from_ptr"));
1367 template<size_t _Nb>
1368 template<class _CharT, class _Traits, class _Alloc>
1371 _M_copy_to_string(std::basic_string<_CharT, _Traits, _Alloc>& __s,
1372 _CharT __zero, _CharT __one) const
1374 __s.assign(_Nb, __zero);
1375 for (size_t __i = _Nb; __i > 0; --__i)
1376 if (_Unchecked_test(__i - 1))
1377 _Traits::assign(__s[_Nb - __i], __one);
1380 // 23.3.5.3 bitset operations:
1383 * @brief Global bitwise operations on bitsets.
1384 * @param x A bitset.
1385 * @param y A bitset of the same size as @a x.
1386 * @return A new bitset.
1388 * These should be self-explanatory.
1390 template<size_t _Nb>
1392 operator&(const bitset<_Nb>& __x, const bitset<_Nb>& __y) _GLIBCXX_NOEXCEPT
1394 bitset<_Nb> __result(__x);
1399 template<size_t _Nb>
1401 operator|(const bitset<_Nb>& __x, const bitset<_Nb>& __y) _GLIBCXX_NOEXCEPT
1403 bitset<_Nb> __result(__x);
1408 template <size_t _Nb>
1410 operator^(const bitset<_Nb>& __x, const bitset<_Nb>& __y) _GLIBCXX_NOEXCEPT
1412 bitset<_Nb> __result(__x);
1420 * @brief Global I/O operators for bitsets.
1422 * Direct I/O between streams and bitsets is supported. Output is
1423 * straightforward. Input will skip whitespace, only accept @a 0 and @a 1
1424 * characters, and will only extract as many digits as the %bitset will
1427 template<class _CharT, class _Traits, size_t _Nb>
1428 std::basic_istream<_CharT, _Traits>&
1429 operator>>(std::basic_istream<_CharT, _Traits>& __is, bitset<_Nb>& __x)
1431 typedef typename _Traits::char_type char_type;
1432 typedef std::basic_istream<_CharT, _Traits> __istream_type;
1433 typedef typename __istream_type::ios_base __ios_base;
1435 std::basic_string<_CharT, _Traits> __tmp;
1438 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1439 // 303. Bitset input operator underspecified
1440 const char_type __zero = __is.widen('0');
1441 const char_type __one = __is.widen('1');
1443 typename __ios_base::iostate __state = __ios_base::goodbit;
1444 typename __istream_type::sentry __sentry(__is);
1449 for (size_t __i = _Nb; __i > 0; --__i)
1451 static typename _Traits::int_type __eof = _Traits::eof();
1453 typename _Traits::int_type __c1 = __is.rdbuf()->sbumpc();
1454 if (_Traits::eq_int_type(__c1, __eof))
1456 __state |= __ios_base::eofbit;
1461 const char_type __c2 = _Traits::to_char_type(__c1);
1462 if (_Traits::eq(__c2, __zero))
1463 __tmp.push_back(__zero);
1464 else if (_Traits::eq(__c2, __one))
1465 __tmp.push_back(__one);
1467 eq_int_type(__is.rdbuf()->sputbackc(__c2),
1470 __state |= __ios_base::failbit;
1476 __catch(__cxxabiv1::__forced_unwind&)
1478 __is._M_setstate(__ios_base::badbit);
1479 __throw_exception_again;
1482 { __is._M_setstate(__ios_base::badbit); }
1485 if (__tmp.empty() && _Nb)
1486 __state |= __ios_base::failbit;
1488 __x._M_copy_from_string(__tmp, static_cast<size_t>(0), _Nb,
1491 __is.setstate(__state);
1495 template <class _CharT, class _Traits, size_t _Nb>
1496 std::basic_ostream<_CharT, _Traits>&
1497 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1498 const bitset<_Nb>& __x)
1500 std::basic_string<_CharT, _Traits> __tmp;
1502 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1503 // 396. what are characters zero and one.
1504 const ctype<_CharT>& __ct = use_facet<ctype<_CharT> >(__os.getloc());
1505 __x._M_copy_to_string(__tmp, __ct.widen('0'), __ct.widen('1'));
1506 return __os << __tmp;
1510 _GLIBCXX_END_NAMESPACE_CONTAINER
1513 #undef _GLIBCXX_BITSET_WORDS
1514 #undef _GLIBCXX_BITSET_BITS_PER_WORD
1516 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1518 #include <bits/functional_hash.h>
1520 namespace std _GLIBCXX_VISIBILITY(default)
1522 _GLIBCXX_BEGIN_NAMESPACE_VERSION
1525 /// std::hash specialization for bitset.
1526 template<size_t _Nb>
1527 struct hash<_GLIBCXX_STD_C::bitset<_Nb>>
1528 : public __hash_base<size_t, _GLIBCXX_STD_C::bitset<_Nb>>
1531 operator()(const _GLIBCXX_STD_C::bitset<_Nb>& __b) const
1533 const size_t __clength = (_Nb + __CHAR_BIT__ - 1) / __CHAR_BIT__;
1534 return std::_Hash_impl::hash(__b._M_getdata(), __clength);
1539 struct hash<_GLIBCXX_STD_C::bitset<0>>
1540 : public __hash_base<size_t, _GLIBCXX_STD_C::bitset<0>>
1543 operator()(const _GLIBCXX_STD_C::bitset<0>&) const
1547 _GLIBCXX_END_NAMESPACE_VERSION
1550 #endif // __GXX_EXPERIMENTAL_CXX0X__
1552 #ifdef _GLIBCXX_DEBUG
1553 # include <debug/bitset>
1556 #ifdef _GLIBCXX_PROFILE
1557 # include <profile/bitset>
1560 #endif /* _GLIBCXX_BITSET */