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)]; }
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 __not_found) const _GLIBCXX_NOEXCEPT;
226 // find the next "on" bit that follows "prev"
228 _M_do_find_next(size_t __prev, size_t __not_found) const
232 // Definitions of non-inline functions from _Base_bitset.
235 _Base_bitset<_Nw>::_M_do_left_shift(size_t __shift) _GLIBCXX_NOEXCEPT
237 if (__builtin_expect(__shift != 0, 1))
239 const size_t __wshift = __shift / _GLIBCXX_BITSET_BITS_PER_WORD;
240 const size_t __offset = __shift % _GLIBCXX_BITSET_BITS_PER_WORD;
243 for (size_t __n = _Nw - 1; __n >= __wshift; --__n)
244 _M_w[__n] = _M_w[__n - __wshift];
247 const size_t __sub_offset = (_GLIBCXX_BITSET_BITS_PER_WORD
249 for (size_t __n = _Nw - 1; __n > __wshift; --__n)
250 _M_w[__n] = ((_M_w[__n - __wshift] << __offset)
251 | (_M_w[__n - __wshift - 1] >> __sub_offset));
252 _M_w[__wshift] = _M_w[0] << __offset;
255 std::fill(_M_w + 0, _M_w + __wshift, static_cast<_WordT>(0));
261 _Base_bitset<_Nw>::_M_do_right_shift(size_t __shift) _GLIBCXX_NOEXCEPT
263 if (__builtin_expect(__shift != 0, 1))
265 const size_t __wshift = __shift / _GLIBCXX_BITSET_BITS_PER_WORD;
266 const size_t __offset = __shift % _GLIBCXX_BITSET_BITS_PER_WORD;
267 const size_t __limit = _Nw - __wshift - 1;
270 for (size_t __n = 0; __n <= __limit; ++__n)
271 _M_w[__n] = _M_w[__n + __wshift];
274 const size_t __sub_offset = (_GLIBCXX_BITSET_BITS_PER_WORD
276 for (size_t __n = 0; __n < __limit; ++__n)
277 _M_w[__n] = ((_M_w[__n + __wshift] >> __offset)
278 | (_M_w[__n + __wshift + 1] << __sub_offset));
279 _M_w[__limit] = _M_w[_Nw-1] >> __offset;
282 std::fill(_M_w + __limit + 1, _M_w + _Nw, static_cast<_WordT>(0));
288 _Base_bitset<_Nw>::_M_do_to_ulong() const
290 for (size_t __i = 1; __i < _Nw; ++__i)
292 __throw_overflow_error(__N("_Base_bitset::_M_do_to_ulong"));
296 #ifdef __GXX_EXPERIMENTAL_CXX0X__
299 _Base_bitset<_Nw>::_M_do_to_ullong() const
301 const bool __dw = sizeof(unsigned long long) > sizeof(unsigned long);
302 for (size_t __i = 1 + __dw; __i < _Nw; ++__i)
304 __throw_overflow_error(__N("_Base_bitset::_M_do_to_ullong"));
307 return _M_w[0] + (static_cast<unsigned long long>(_M_w[1])
308 << _GLIBCXX_BITSET_BITS_PER_WORD);
316 _M_do_find_first(size_t __not_found) const _GLIBCXX_NOEXCEPT
318 for (size_t __i = 0; __i < _Nw; __i++)
320 _WordT __thisword = _M_w[__i];
321 if (__thisword != static_cast<_WordT>(0))
322 return (__i * _GLIBCXX_BITSET_BITS_PER_WORD
323 + __builtin_ctzl(__thisword));
325 // not found, so return an indication of failure.
332 _M_do_find_next(size_t __prev, size_t __not_found) const _GLIBCXX_NOEXCEPT
334 // make bound inclusive
337 // check out of bounds
338 if (__prev >= _Nw * _GLIBCXX_BITSET_BITS_PER_WORD)
342 size_t __i = _S_whichword(__prev);
343 _WordT __thisword = _M_w[__i];
345 // mask off bits below bound
346 __thisword &= (~static_cast<_WordT>(0)) << _S_whichbit(__prev);
348 if (__thisword != static_cast<_WordT>(0))
349 return (__i * _GLIBCXX_BITSET_BITS_PER_WORD
350 + __builtin_ctzl(__thisword));
352 // check subsequent words
354 for (; __i < _Nw; __i++)
356 __thisword = _M_w[__i];
357 if (__thisword != static_cast<_WordT>(0))
358 return (__i * _GLIBCXX_BITSET_BITS_PER_WORD
359 + __builtin_ctzl(__thisword));
361 // not found, so return an indication of failure.
363 } // end _M_do_find_next
366 * Base class, specialization for a single word.
368 * See documentation for bitset.
371 struct _Base_bitset<1>
373 typedef unsigned long _WordT;
376 _GLIBCXX_CONSTEXPR _Base_bitset() _GLIBCXX_NOEXCEPT
380 #ifdef __GXX_EXPERIMENTAL_CXX0X__
381 constexpr _Base_bitset(unsigned long long __val) noexcept
383 _Base_bitset(unsigned long __val)
388 static _GLIBCXX_CONSTEXPR size_t
389 _S_whichword(size_t __pos ) _GLIBCXX_NOEXCEPT
390 { return __pos / _GLIBCXX_BITSET_BITS_PER_WORD; }
392 static _GLIBCXX_CONSTEXPR size_t
393 _S_whichbyte(size_t __pos ) _GLIBCXX_NOEXCEPT
394 { return (__pos % _GLIBCXX_BITSET_BITS_PER_WORD) / __CHAR_BIT__; }
396 static _GLIBCXX_CONSTEXPR size_t
397 _S_whichbit(size_t __pos ) _GLIBCXX_NOEXCEPT
398 { return __pos % _GLIBCXX_BITSET_BITS_PER_WORD; }
400 static _GLIBCXX_CONSTEXPR _WordT
401 _S_maskbit(size_t __pos ) _GLIBCXX_NOEXCEPT
402 { return (static_cast<_WordT>(1)) << _S_whichbit(__pos); }
405 _M_getword(size_t) _GLIBCXX_NOEXCEPT
409 _M_getword(size_t) const _GLIBCXX_NOEXCEPT
412 #ifdef __GXX_EXPERIMENTAL_CXX0X__
414 _M_getdata() const noexcept
419 _M_hiword() _GLIBCXX_NOEXCEPT
422 _GLIBCXX_CONSTEXPR _WordT
423 _M_hiword() const _GLIBCXX_NOEXCEPT
427 _M_do_and(const _Base_bitset<1>& __x) _GLIBCXX_NOEXCEPT
428 { _M_w &= __x._M_w; }
431 _M_do_or(const _Base_bitset<1>& __x) _GLIBCXX_NOEXCEPT
432 { _M_w |= __x._M_w; }
435 _M_do_xor(const _Base_bitset<1>& __x) _GLIBCXX_NOEXCEPT
436 { _M_w ^= __x._M_w; }
439 _M_do_left_shift(size_t __shift) _GLIBCXX_NOEXCEPT
440 { _M_w <<= __shift; }
443 _M_do_right_shift(size_t __shift) _GLIBCXX_NOEXCEPT
444 { _M_w >>= __shift; }
447 _M_do_flip() _GLIBCXX_NOEXCEPT
451 _M_do_set() _GLIBCXX_NOEXCEPT
452 { _M_w = ~static_cast<_WordT>(0); }
455 _M_do_reset() _GLIBCXX_NOEXCEPT
459 _M_is_equal(const _Base_bitset<1>& __x) const _GLIBCXX_NOEXCEPT
460 { return _M_w == __x._M_w; }
463 _M_are_all_aux() const _GLIBCXX_NOEXCEPT
464 { return __builtin_popcountl(_M_w); }
467 _M_is_any() const _GLIBCXX_NOEXCEPT
468 { return _M_w != 0; }
471 _M_do_count() const _GLIBCXX_NOEXCEPT
472 { return __builtin_popcountl(_M_w); }
475 _M_do_to_ulong() const _GLIBCXX_NOEXCEPT
478 #ifdef __GXX_EXPERIMENTAL_CXX0X__
480 _M_do_to_ullong() const noexcept
485 _M_do_find_first(size_t __not_found) const _GLIBCXX_NOEXCEPT
488 return __builtin_ctzl(_M_w);
493 // find the next "on" bit that follows "prev"
495 _M_do_find_next(size_t __prev, size_t __not_found) const
499 if (__prev >= ((size_t) _GLIBCXX_BITSET_BITS_PER_WORD))
502 _WordT __x = _M_w >> __prev;
504 return __builtin_ctzl(__x) + __prev;
511 * Base class, specialization for no storage (zero-length %bitset).
513 * See documentation for bitset.
516 struct _Base_bitset<0>
518 typedef unsigned long _WordT;
520 _GLIBCXX_CONSTEXPR _Base_bitset() _GLIBCXX_NOEXCEPT
523 #ifdef __GXX_EXPERIMENTAL_CXX0X__
524 constexpr _Base_bitset(unsigned long long) noexcept
526 _Base_bitset(unsigned long)
530 static _GLIBCXX_CONSTEXPR size_t
531 _S_whichword(size_t __pos ) _GLIBCXX_NOEXCEPT
532 { return __pos / _GLIBCXX_BITSET_BITS_PER_WORD; }
534 static _GLIBCXX_CONSTEXPR size_t
535 _S_whichbyte(size_t __pos ) _GLIBCXX_NOEXCEPT
536 { return (__pos % _GLIBCXX_BITSET_BITS_PER_WORD) / __CHAR_BIT__; }
538 static _GLIBCXX_CONSTEXPR size_t
539 _S_whichbit(size_t __pos ) _GLIBCXX_NOEXCEPT
540 { return __pos % _GLIBCXX_BITSET_BITS_PER_WORD; }
542 static _GLIBCXX_CONSTEXPR _WordT
543 _S_maskbit(size_t __pos ) _GLIBCXX_NOEXCEPT
544 { return (static_cast<_WordT>(1)) << _S_whichbit(__pos); }
546 // This would normally give access to the data. The bounds-checking
547 // in the bitset class will prevent the user from getting this far,
548 // but (1) it must still return an lvalue to compile, and (2) the
549 // user might call _Unchecked_set directly, in which case this /needs/
550 // to fail. Let's not penalize zero-length users unless they actually
551 // make an unchecked call; all the memory ugliness is therefore
552 // localized to this single should-never-get-this-far function.
554 _M_getword(size_t) _GLIBCXX_NOEXCEPT
556 __throw_out_of_range(__N("_Base_bitset::_M_getword"));
561 _M_getword(size_t __pos) const _GLIBCXX_NOEXCEPT
564 _GLIBCXX_CONSTEXPR _WordT
565 _M_hiword() const _GLIBCXX_NOEXCEPT
569 _M_do_and(const _Base_bitset<0>&) _GLIBCXX_NOEXCEPT
573 _M_do_or(const _Base_bitset<0>&) _GLIBCXX_NOEXCEPT
577 _M_do_xor(const _Base_bitset<0>&) _GLIBCXX_NOEXCEPT
581 _M_do_left_shift(size_t) _GLIBCXX_NOEXCEPT
585 _M_do_right_shift(size_t) _GLIBCXX_NOEXCEPT
589 _M_do_flip() _GLIBCXX_NOEXCEPT
593 _M_do_set() _GLIBCXX_NOEXCEPT
597 _M_do_reset() _GLIBCXX_NOEXCEPT
600 // Are all empty bitsets equal to each other? Are they equal to
601 // themselves? How to compare a thing which has no state? What is
602 // the sound of one zero-length bitset clapping?
604 _M_is_equal(const _Base_bitset<0>&) const _GLIBCXX_NOEXCEPT
608 _M_are_all_aux() const _GLIBCXX_NOEXCEPT
612 _M_is_any() const _GLIBCXX_NOEXCEPT
616 _M_do_count() const _GLIBCXX_NOEXCEPT
620 _M_do_to_ulong() const _GLIBCXX_NOEXCEPT
623 #ifdef __GXX_EXPERIMENTAL_CXX0X__
625 _M_do_to_ullong() const noexcept
629 // Normally "not found" is the size, but that could also be
630 // misinterpreted as an index in this corner case. Oh well.
632 _M_do_find_first(size_t) const _GLIBCXX_NOEXCEPT
636 _M_do_find_next(size_t, size_t) const _GLIBCXX_NOEXCEPT
641 // Helper class to zero out the unused high-order bits in the highest word.
642 template<size_t _Extrabits>
645 typedef unsigned long _WordT;
648 _S_do_sanitize(_WordT& __val) _GLIBCXX_NOEXCEPT
649 { __val &= ~((~static_cast<_WordT>(0)) << _Extrabits); }
655 typedef unsigned long _WordT;
658 _S_do_sanitize(_WordT) _GLIBCXX_NOEXCEPT { }
662 * @brief The %bitset class represents a @e fixed-size sequence of bits.
664 * @ingroup containers
666 * (Note that %bitset does @e not meet the formal requirements of a
667 * <a href="tables.html#65">container</a>. Mainly, it lacks iterators.)
669 * The template argument, @a Nb, may be any non-negative number,
670 * specifying the number of bits (e.g., "0", "12", "1024*1024").
672 * In the general unoptimized case, storage is allocated in word-sized
673 * blocks. Let B be the number of bits in a word, then (Nb+(B-1))/B
674 * words will be used for storage. B - Nb%B bits are unused. (They are
675 * the high-order bits in the highest word.) It is a class invariant
676 * that those unused bits are always zero.
678 * If you think of %bitset as <em>a simple array of bits</em>, be
679 * aware that your mental picture is reversed: a %bitset behaves
680 * the same way as bits in integers do, with the bit at index 0 in
681 * the <em>least significant / right-hand</em> position, and the bit at
682 * index Nb-1 in the <em>most significant / left-hand</em> position.
683 * Thus, unlike other containers, a %bitset's index <em>counts from
684 * right to left</em>, to put it very loosely.
686 * This behavior is preserved when translating to and from strings. For
687 * example, the first line of the following program probably prints
688 * <em>b('a') is 0001100001</em> on a modern ASCII system.
692 * #include <iostream>
695 * using namespace std;
702 * cout << "b('a') is " << b << endl;
706 * string str = s.str();
707 * cout << "index 3 in the string is " << str[3] << " but\n"
708 * << "index 3 in the bitset is " << b[3] << endl;
713 * http://gcc.gnu.org/onlinedocs/libstdc++/manual/bk01pt12ch33s02.html
714 * for a description of extensions.
716 * Most of the actual code isn't contained in %bitset<> itself, but in the
717 * base class _Base_bitset. The base class works with whole words, not with
718 * individual bits. This allows us to specialize _Base_bitset for the
719 * important special case where the %bitset is only a single word.
721 * Extra confusion can result due to the fact that the storage for
722 * _Base_bitset @e is a regular array, and is indexed as such. This is
723 * carefully encapsulated.
727 : private _Base_bitset<_GLIBCXX_BITSET_WORDS(_Nb)>
730 typedef _Base_bitset<_GLIBCXX_BITSET_WORDS(_Nb)> _Base;
731 typedef unsigned long _WordT;
734 _M_do_sanitize() _GLIBCXX_NOEXCEPT
736 typedef _Sanitize<_Nb % _GLIBCXX_BITSET_BITS_PER_WORD> __sanitize_type;
737 __sanitize_type::_S_do_sanitize(this->_M_hiword());
740 #ifdef __GXX_EXPERIMENTAL_CXX0X__
741 template<typename> friend class hash;
746 * This encapsulates the concept of a single bit. An instance of this
747 * class is a proxy for an actual bit; this way the individual bit
748 * operations are done as faster word-size bitwise instructions.
750 * Most users will never need to use this class directly; conversions
751 * to and from bool are automatic and should be transparent. Overloaded
752 * operators help to preserve the illusion.
754 * (On a typical system, this <em>bit %reference</em> is 64
755 * times the size of an actual bit. Ha.)
768 reference(bitset& __b, size_t __pos) _GLIBCXX_NOEXCEPT
770 _M_wp = &__b._M_getword(__pos);
771 _M_bpos = _Base::_S_whichbit(__pos);
774 ~reference() _GLIBCXX_NOEXCEPT
779 operator=(bool __x) _GLIBCXX_NOEXCEPT
782 *_M_wp |= _Base::_S_maskbit(_M_bpos);
784 *_M_wp &= ~_Base::_S_maskbit(_M_bpos);
788 // For b[i] = b[__j];
790 operator=(const reference& __j) _GLIBCXX_NOEXCEPT
792 if ((*(__j._M_wp) & _Base::_S_maskbit(__j._M_bpos)))
793 *_M_wp |= _Base::_S_maskbit(_M_bpos);
795 *_M_wp &= ~_Base::_S_maskbit(_M_bpos);
801 operator~() const _GLIBCXX_NOEXCEPT
802 { return (*(_M_wp) & _Base::_S_maskbit(_M_bpos)) == 0; }
805 operator bool() const _GLIBCXX_NOEXCEPT
806 { return (*(_M_wp) & _Base::_S_maskbit(_M_bpos)) != 0; }
810 flip() _GLIBCXX_NOEXCEPT
812 *_M_wp ^= _Base::_S_maskbit(_M_bpos);
816 friend class reference;
818 // 23.3.5.1 constructors:
819 /// All bits set to zero.
820 _GLIBCXX_CONSTEXPR bitset() _GLIBCXX_NOEXCEPT
823 /// Initial bits bitwise-copied from a single word (others set to zero).
824 #ifdef __GXX_EXPERIMENTAL_CXX0X__
825 constexpr bitset(unsigned long long __val) noexcept
828 bitset(unsigned long __val)
830 { _M_do_sanitize(); }
834 * @brief Use a subset of a string.
835 * @param s A string of @a 0 and @a 1 characters.
836 * @param position Index of the first character in @a s to use;
838 * @throw std::out_of_range If @a pos is bigger the size of @a s.
839 * @throw std::invalid_argument If a character appears in the string
840 * which is neither @a 0 nor @a 1.
842 template<class _CharT, class _Traits, class _Alloc>
844 bitset(const std::basic_string<_CharT, _Traits, _Alloc>& __s,
845 size_t __position = 0)
848 if (__position > __s.size())
849 __throw_out_of_range(__N("bitset::bitset initial position "
851 _M_copy_from_string(__s, __position,
852 std::basic_string<_CharT, _Traits, _Alloc>::npos,
853 _CharT('0'), _CharT('1'));
857 * @brief Use a subset of a string.
858 * @param s A string of @a 0 and @a 1 characters.
859 * @param position Index of the first character in @a s to use.
860 * @param n The number of characters to copy.
861 * @throw std::out_of_range If @a pos is bigger the size of @a s.
862 * @throw std::invalid_argument If a character appears in the string
863 * which is neither @a 0 nor @a 1.
865 template<class _CharT, class _Traits, class _Alloc>
866 bitset(const std::basic_string<_CharT, _Traits, _Alloc>& __s,
867 size_t __position, size_t __n)
870 if (__position > __s.size())
871 __throw_out_of_range(__N("bitset::bitset initial position "
873 _M_copy_from_string(__s, __position, __n, _CharT('0'), _CharT('1'));
876 // _GLIBCXX_RESOLVE_LIB_DEFECTS
877 // 396. what are characters zero and one.
878 template<class _CharT, class _Traits, class _Alloc>
879 bitset(const std::basic_string<_CharT, _Traits, _Alloc>& __s,
880 size_t __position, size_t __n,
881 _CharT __zero, _CharT __one = _CharT('1'))
884 if (__position > __s.size())
885 __throw_out_of_range(__N("bitset::bitset initial position "
887 _M_copy_from_string(__s, __position, __n, __zero, __one);
890 #ifdef __GXX_EXPERIMENTAL_CXX0X__
892 * @brief Construct from a character %array.
893 * @param str An %array of characters @a zero and @a one.
894 * @param n The number of characters to use.
895 * @param zero The character corresponding to the value 0.
896 * @param one The character corresponding to the value 1.
897 * @throw std::invalid_argument If a character appears in the string
898 * which is neither @a zero nor @a one.
900 template<typename _CharT>
902 bitset(const _CharT* __str,
903 typename std::basic_string<_CharT>::size_type __n
904 = std::basic_string<_CharT>::npos,
905 _CharT __zero = _CharT('0'), _CharT __one = _CharT('1'))
909 __throw_logic_error(__N("bitset::bitset(const _CharT*, ...)"));
911 if (__n == std::basic_string<_CharT>::npos)
912 __n = std::char_traits<_CharT>::length(__str);
913 _M_copy_from_ptr<_CharT, std::char_traits<_CharT>>(__str, __n, 0,
919 // 23.3.5.2 bitset operations:
922 * @brief Operations on bitsets.
923 * @param rhs A same-sized bitset.
925 * These should be self-explanatory.
928 operator&=(const bitset<_Nb>& __rhs) _GLIBCXX_NOEXCEPT
930 this->_M_do_and(__rhs);
935 operator|=(const bitset<_Nb>& __rhs) _GLIBCXX_NOEXCEPT
937 this->_M_do_or(__rhs);
942 operator^=(const bitset<_Nb>& __rhs) _GLIBCXX_NOEXCEPT
944 this->_M_do_xor(__rhs);
951 * @brief Operations on bitsets.
952 * @param position The number of places to shift.
954 * These should be self-explanatory.
957 operator<<=(size_t __position) _GLIBCXX_NOEXCEPT
959 if (__builtin_expect(__position < _Nb, 1))
961 this->_M_do_left_shift(__position);
962 this->_M_do_sanitize();
970 operator>>=(size_t __position) _GLIBCXX_NOEXCEPT
972 if (__builtin_expect(__position < _Nb, 1))
974 this->_M_do_right_shift(__position);
975 this->_M_do_sanitize();
985 * These versions of single-bit set, reset, flip, and test are
986 * extensions from the SGI version. They do no range checking.
987 * @ingroup SGIextensions
990 _Unchecked_set(size_t __pos) _GLIBCXX_NOEXCEPT
992 this->_M_getword(__pos) |= _Base::_S_maskbit(__pos);
997 _Unchecked_set(size_t __pos, int __val) _GLIBCXX_NOEXCEPT
1000 this->_M_getword(__pos) |= _Base::_S_maskbit(__pos);
1002 this->_M_getword(__pos) &= ~_Base::_S_maskbit(__pos);
1007 _Unchecked_reset(size_t __pos) _GLIBCXX_NOEXCEPT
1009 this->_M_getword(__pos) &= ~_Base::_S_maskbit(__pos);
1014 _Unchecked_flip(size_t __pos) _GLIBCXX_NOEXCEPT
1016 this->_M_getword(__pos) ^= _Base::_S_maskbit(__pos);
1021 _Unchecked_test(size_t __pos) const _GLIBCXX_NOEXCEPT
1022 { return ((this->_M_getword(__pos) & _Base::_S_maskbit(__pos))
1023 != static_cast<_WordT>(0)); }
1026 // Set, reset, and flip.
1028 * @brief Sets every bit to true.
1031 set() _GLIBCXX_NOEXCEPT
1034 this->_M_do_sanitize();
1039 * @brief Sets a given bit to a particular value.
1040 * @param position The index of the bit.
1041 * @param val Either true or false, defaults to true.
1042 * @throw std::out_of_range If @a pos is bigger the size of the %set.
1045 set(size_t __position, bool __val = true)
1047 if (__position >= _Nb)
1048 __throw_out_of_range(__N("bitset::set"));
1049 return _Unchecked_set(__position, __val);
1053 * @brief Sets every bit to false.
1056 reset() _GLIBCXX_NOEXCEPT
1058 this->_M_do_reset();
1063 * @brief Sets a given bit to false.
1064 * @param position The index of the bit.
1065 * @throw std::out_of_range If @a pos is bigger the size of the %set.
1067 * Same as writing @c set(pos,false).
1070 reset(size_t __position)
1072 if (__position >= _Nb)
1073 __throw_out_of_range(__N("bitset::reset"));
1074 return _Unchecked_reset(__position);
1078 * @brief Toggles every bit to its opposite value.
1081 flip() _GLIBCXX_NOEXCEPT
1084 this->_M_do_sanitize();
1089 * @brief Toggles a given bit to its opposite value.
1090 * @param position The index of the bit.
1091 * @throw std::out_of_range If @a pos is bigger the size of the %set.
1094 flip(size_t __position)
1096 if (__position >= _Nb)
1097 __throw_out_of_range(__N("bitset::flip"));
1098 return _Unchecked_flip(__position);
1101 /// See the no-argument flip().
1103 operator~() const _GLIBCXX_NOEXCEPT
1104 { return bitset<_Nb>(*this).flip(); }
1108 * @brief Array-indexing support.
1109 * @param position Index into the %bitset.
1110 * @return A bool for a <em>const %bitset</em>. For non-const
1111 * bitsets, an instance of the reference proxy class.
1112 * @note These operators do no range checking and throw no exceptions,
1113 * as required by DR 11 to the standard.
1115 * _GLIBCXX_RESOLVE_LIB_DEFECTS Note that this implementation already
1116 * resolves DR 11 (items 1 and 2), but does not do the range-checking
1117 * required by that DR's resolution. -pme
1118 * The DR has since been changed: range-checking is a precondition
1119 * (users' responsibility), and these functions must not throw. -pme
1122 operator[](size_t __position)
1123 { return reference(*this, __position); }
1126 operator[](size_t __position) const
1127 { return _Unchecked_test(__position); }
1131 * @brief Returns a numerical interpretation of the %bitset.
1132 * @return The integral equivalent of the bits.
1133 * @throw std::overflow_error If there are too many bits to be
1134 * represented in an @c unsigned @c long.
1138 { return this->_M_do_to_ulong(); }
1140 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1143 { return this->_M_do_to_ullong(); }
1147 * @brief Returns a character interpretation of the %bitset.
1148 * @return The string equivalent of the bits.
1150 * Note the ordering of the bits: decreasing character positions
1151 * correspond to increasing bit positions (see the main class notes for
1154 template<class _CharT, class _Traits, class _Alloc>
1155 std::basic_string<_CharT, _Traits, _Alloc>
1158 std::basic_string<_CharT, _Traits, _Alloc> __result;
1159 _M_copy_to_string(__result, _CharT('0'), _CharT('1'));
1163 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1164 // 396. what are characters zero and one.
1165 template<class _CharT, class _Traits, class _Alloc>
1166 std::basic_string<_CharT, _Traits, _Alloc>
1167 to_string(_CharT __zero, _CharT __one = _CharT('1')) const
1169 std::basic_string<_CharT, _Traits, _Alloc> __result;
1170 _M_copy_to_string(__result, __zero, __one);
1174 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1175 // 434. bitset::to_string() hard to use.
1176 template<class _CharT, class _Traits>
1177 std::basic_string<_CharT, _Traits, std::allocator<_CharT> >
1179 { return to_string<_CharT, _Traits, std::allocator<_CharT> >(); }
1181 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1182 // 853. to_string needs updating with zero and one.
1183 template<class _CharT, class _Traits>
1184 std::basic_string<_CharT, _Traits, std::allocator<_CharT> >
1185 to_string(_CharT __zero, _CharT __one = _CharT('1')) const
1186 { return to_string<_CharT, _Traits,
1187 std::allocator<_CharT> >(__zero, __one); }
1189 template<class _CharT>
1190 std::basic_string<_CharT, std::char_traits<_CharT>,
1191 std::allocator<_CharT> >
1194 return to_string<_CharT, std::char_traits<_CharT>,
1195 std::allocator<_CharT> >();
1198 template<class _CharT>
1199 std::basic_string<_CharT, std::char_traits<_CharT>,
1200 std::allocator<_CharT> >
1201 to_string(_CharT __zero, _CharT __one = _CharT('1')) const
1203 return to_string<_CharT, std::char_traits<_CharT>,
1204 std::allocator<_CharT> >(__zero, __one);
1207 std::basic_string<char, std::char_traits<char>, std::allocator<char> >
1210 return to_string<char, std::char_traits<char>,
1211 std::allocator<char> >();
1214 std::basic_string<char, std::char_traits<char>, std::allocator<char> >
1215 to_string(char __zero, char __one = '1') const
1217 return to_string<char, std::char_traits<char>,
1218 std::allocator<char> >(__zero, __one);
1221 // Helper functions for string operations.
1222 template<class _CharT, class _Traits>
1224 _M_copy_from_ptr(const _CharT*, size_t, size_t, size_t,
1227 template<class _CharT, class _Traits, class _Alloc>
1229 _M_copy_from_string(const std::basic_string<_CharT,
1230 _Traits, _Alloc>& __s, size_t __pos, size_t __n,
1231 _CharT __zero, _CharT __one)
1232 { _M_copy_from_ptr<_CharT, _Traits>(__s.data(), __s.size(), __pos, __n,
1235 template<class _CharT, class _Traits, class _Alloc>
1237 _M_copy_to_string(std::basic_string<_CharT, _Traits, _Alloc>&,
1238 _CharT, _CharT) const;
1240 // NB: Backward compat.
1241 template<class _CharT, class _Traits, class _Alloc>
1243 _M_copy_from_string(const std::basic_string<_CharT,
1244 _Traits, _Alloc>& __s, size_t __pos, size_t __n)
1245 { _M_copy_from_string(__s, __pos, __n, _CharT('0'), _CharT('1')); }
1247 template<class _CharT, class _Traits, class _Alloc>
1249 _M_copy_to_string(std::basic_string<_CharT, _Traits,_Alloc>& __s) const
1250 { _M_copy_to_string(__s, _CharT('0'), _CharT('1')); }
1252 /// Returns the number of bits which are set.
1254 count() const _GLIBCXX_NOEXCEPT
1255 { return this->_M_do_count(); }
1257 /// Returns the total number of bits.
1258 _GLIBCXX_CONSTEXPR size_t
1259 size() const _GLIBCXX_NOEXCEPT
1263 /// These comparisons for equality/inequality are, well, @e bitwise.
1265 operator==(const bitset<_Nb>& __rhs) const _GLIBCXX_NOEXCEPT
1266 { return this->_M_is_equal(__rhs); }
1269 operator!=(const bitset<_Nb>& __rhs) const _GLIBCXX_NOEXCEPT
1270 { return !this->_M_is_equal(__rhs); }
1274 * @brief Tests the value of a bit.
1275 * @param position The index of a bit.
1276 * @return The value at @a pos.
1277 * @throw std::out_of_range If @a pos is bigger the size of the %set.
1280 test(size_t __position) const
1282 if (__position >= _Nb)
1283 __throw_out_of_range(__N("bitset::test"));
1284 return _Unchecked_test(__position);
1287 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1288 // DR 693. std::bitset::all() missing.
1290 * @brief Tests whether all the bits are on.
1291 * @return True if all the bits are set.
1294 all() const _GLIBCXX_NOEXCEPT
1295 { return this->_M_are_all_aux() == _Nb; }
1298 * @brief Tests whether any of the bits are on.
1299 * @return True if at least one bit is set.
1302 any() const _GLIBCXX_NOEXCEPT
1303 { return this->_M_is_any(); }
1306 * @brief Tests whether any of the bits are on.
1307 * @return True if none of the bits are set.
1310 none() const _GLIBCXX_NOEXCEPT
1311 { return !this->_M_is_any(); }
1314 /// Self-explanatory.
1316 operator<<(size_t __position) const _GLIBCXX_NOEXCEPT
1317 { return bitset<_Nb>(*this) <<= __position; }
1320 operator>>(size_t __position) const _GLIBCXX_NOEXCEPT
1321 { return bitset<_Nb>(*this) >>= __position; }
1325 * @brief Finds the index of the first "on" bit.
1326 * @return The index of the first bit set, or size() if not found.
1327 * @ingroup SGIextensions
1331 _Find_first() const _GLIBCXX_NOEXCEPT
1332 { return this->_M_do_find_first(_Nb); }
1335 * @brief Finds the index of the next "on" bit after prev.
1336 * @return The index of the next bit set, or size() if not found.
1337 * @param prev Where to start searching.
1338 * @ingroup SGIextensions
1342 _Find_next(size_t __prev ) const _GLIBCXX_NOEXCEPT
1343 { return this->_M_do_find_next(__prev, _Nb); }
1346 // Definitions of non-inline member functions.
1347 template<size_t _Nb>
1348 template<class _CharT, class _Traits>
1351 _M_copy_from_ptr(const _CharT* __s, size_t __len,
1352 size_t __pos, size_t __n, _CharT __zero, _CharT __one)
1355 const size_t __nbits = std::min(_Nb, std::min(__n, __len - __pos));
1356 for (size_t __i = __nbits; __i > 0; --__i)
1358 const _CharT __c = __s[__pos + __nbits - __i];
1359 if (_Traits::eq(__c, __zero))
1361 else if (_Traits::eq(__c, __one))
1362 _Unchecked_set(__i - 1);
1364 __throw_invalid_argument(__N("bitset::_M_copy_from_ptr"));
1368 template<size_t _Nb>
1369 template<class _CharT, class _Traits, class _Alloc>
1372 _M_copy_to_string(std::basic_string<_CharT, _Traits, _Alloc>& __s,
1373 _CharT __zero, _CharT __one) const
1375 __s.assign(_Nb, __zero);
1376 for (size_t __i = _Nb; __i > 0; --__i)
1377 if (_Unchecked_test(__i - 1))
1378 _Traits::assign(__s[_Nb - __i], __one);
1381 // 23.3.5.3 bitset operations:
1384 * @brief Global bitwise operations on bitsets.
1385 * @param x A bitset.
1386 * @param y A bitset of the same size as @a x.
1387 * @return A new bitset.
1389 * These should be self-explanatory.
1391 template<size_t _Nb>
1393 operator&(const bitset<_Nb>& __x, const bitset<_Nb>& __y) _GLIBCXX_NOEXCEPT
1395 bitset<_Nb> __result(__x);
1400 template<size_t _Nb>
1402 operator|(const bitset<_Nb>& __x, const bitset<_Nb>& __y) _GLIBCXX_NOEXCEPT
1404 bitset<_Nb> __result(__x);
1409 template <size_t _Nb>
1411 operator^(const bitset<_Nb>& __x, const bitset<_Nb>& __y) _GLIBCXX_NOEXCEPT
1413 bitset<_Nb> __result(__x);
1421 * @brief Global I/O operators for bitsets.
1423 * Direct I/O between streams and bitsets is supported. Output is
1424 * straightforward. Input will skip whitespace, only accept @a 0 and @a 1
1425 * characters, and will only extract as many digits as the %bitset will
1428 template<class _CharT, class _Traits, size_t _Nb>
1429 std::basic_istream<_CharT, _Traits>&
1430 operator>>(std::basic_istream<_CharT, _Traits>& __is, bitset<_Nb>& __x)
1432 typedef typename _Traits::char_type char_type;
1433 typedef std::basic_istream<_CharT, _Traits> __istream_type;
1434 typedef typename __istream_type::ios_base __ios_base;
1436 std::basic_string<_CharT, _Traits> __tmp;
1439 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1440 // 303. Bitset input operator underspecified
1441 const char_type __zero = __is.widen('0');
1442 const char_type __one = __is.widen('1');
1444 typename __ios_base::iostate __state = __ios_base::goodbit;
1445 typename __istream_type::sentry __sentry(__is);
1450 for (size_t __i = _Nb; __i > 0; --__i)
1452 static typename _Traits::int_type __eof = _Traits::eof();
1454 typename _Traits::int_type __c1 = __is.rdbuf()->sbumpc();
1455 if (_Traits::eq_int_type(__c1, __eof))
1457 __state |= __ios_base::eofbit;
1462 const char_type __c2 = _Traits::to_char_type(__c1);
1463 if (_Traits::eq(__c2, __zero))
1464 __tmp.push_back(__zero);
1465 else if (_Traits::eq(__c2, __one))
1466 __tmp.push_back(__one);
1468 eq_int_type(__is.rdbuf()->sputbackc(__c2),
1471 __state |= __ios_base::failbit;
1477 __catch(__cxxabiv1::__forced_unwind&)
1479 __is._M_setstate(__ios_base::badbit);
1480 __throw_exception_again;
1483 { __is._M_setstate(__ios_base::badbit); }
1486 if (__tmp.empty() && _Nb)
1487 __state |= __ios_base::failbit;
1489 __x._M_copy_from_string(__tmp, static_cast<size_t>(0), _Nb,
1492 __is.setstate(__state);
1496 template <class _CharT, class _Traits, size_t _Nb>
1497 std::basic_ostream<_CharT, _Traits>&
1498 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1499 const bitset<_Nb>& __x)
1501 std::basic_string<_CharT, _Traits> __tmp;
1503 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1504 // 396. what are characters zero and one.
1505 const ctype<_CharT>& __ct = use_facet<ctype<_CharT> >(__os.getloc());
1506 __x._M_copy_to_string(__tmp, __ct.widen('0'), __ct.widen('1'));
1507 return __os << __tmp;
1511 _GLIBCXX_END_NAMESPACE_CONTAINER
1514 #undef _GLIBCXX_BITSET_WORDS
1515 #undef _GLIBCXX_BITSET_BITS_PER_WORD
1517 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1519 #include <bits/functional_hash.h>
1521 namespace std _GLIBCXX_VISIBILITY(default)
1523 _GLIBCXX_BEGIN_NAMESPACE_VERSION
1526 /// std::hash specialization for bitset.
1527 template<size_t _Nb>
1528 struct hash<_GLIBCXX_STD_C::bitset<_Nb>>
1529 : public __hash_base<size_t, _GLIBCXX_STD_C::bitset<_Nb>>
1532 operator()(const _GLIBCXX_STD_C::bitset<_Nb>& __b) const
1534 const size_t __clength = (_Nb + __CHAR_BIT__ - 1) / __CHAR_BIT__;
1535 return std::_Hash_impl::hash(__b._M_getdata(), __clength);
1540 struct hash<_GLIBCXX_STD_C::bitset<0>>
1541 : public __hash_base<size_t, _GLIBCXX_STD_C::bitset<0>>
1544 operator()(const _GLIBCXX_STD_C::bitset<0>&) const
1548 _GLIBCXX_END_NAMESPACE_VERSION
1551 #endif // __GXX_EXPERIMENTAL_CXX0X__
1553 #ifdef _GLIBCXX_DEBUG
1554 # include <debug/bitset>
1557 #ifdef _GLIBCXX_PROFILE
1558 # include <profile/bitset>
1561 #endif /* _GLIBCXX_BITSET */