1 // <bitset> -*- C++ -*-
3 // Copyright (C) 2001-2013 Free Software Foundation, Inc.
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
27 * Silicon Graphics Computer Systems, Inc.
29 * Permission to use, copy, modify, distribute and sell this software
30 * and its documentation for any purpose is hereby granted without fee,
31 * provided that the above copyright notice appear in all copies and
32 * that both that copyright notice and this permission notice appear
33 * in supporting documentation. Silicon Graphics makes no
34 * representations about the suitability of this software for any
35 * purpose. It is provided "as is" without express or implied warranty.
38 /** @file include/bitset
39 * This is a Standard C++ Library header.
42 #ifndef _GLIBCXX_BITSET
43 #define _GLIBCXX_BITSET 1
45 #pragma GCC system_header
48 #include <bits/functexcept.h> // For invalid_argument, out_of_range,
51 #include <bits/cxxabi_forced.h>
53 #define _GLIBCXX_BITSET_BITS_PER_WORD (__CHAR_BIT__ * __SIZEOF_LONG__)
54 #define _GLIBCXX_BITSET_WORDS(__n) \
55 ((__n) / _GLIBCXX_BITSET_BITS_PER_WORD + \
56 ((__n) % _GLIBCXX_BITSET_BITS_PER_WORD == 0 ? 0 : 1))
58 #define _GLIBCXX_BITSET_BITS_PER_ULL (__CHAR_BIT__ * __SIZEOF_LONG_LONG__)
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 #if __cplusplus >= 201103L
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 #if __cplusplus >= 201103L
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])
188 _M_are_all() const _GLIBCXX_NOEXCEPT
190 for (size_t __i = 0; __i < _Nw - 1; __i++)
191 if (_M_w[__i] != ~static_cast<_WordT>(0))
193 return _M_hiword() == (~static_cast<_WordT>(0)
194 >> (_Nw * _GLIBCXX_BITSET_BITS_PER_WORD
199 _M_is_any() const _GLIBCXX_NOEXCEPT
201 for (size_t __i = 0; __i < _Nw; __i++)
202 if (_M_w[__i] != static_cast<_WordT>(0))
208 _M_do_count() const _GLIBCXX_NOEXCEPT
211 for (size_t __i = 0; __i < _Nw; __i++)
212 __result += __builtin_popcountl(_M_w[__i]);
217 _M_do_to_ulong() const;
219 #if __cplusplus >= 201103L
221 _M_do_to_ullong() const;
224 // find first "on" bit
226 _M_do_find_first(size_t) const _GLIBCXX_NOEXCEPT;
228 // find the next "on" bit that follows "prev"
230 _M_do_find_next(size_t, size_t) const _GLIBCXX_NOEXCEPT;
233 // Definitions of non-inline functions from _Base_bitset.
236 _Base_bitset<_Nw>::_M_do_left_shift(size_t __shift) _GLIBCXX_NOEXCEPT
238 if (__builtin_expect(__shift != 0, 1))
240 const size_t __wshift = __shift / _GLIBCXX_BITSET_BITS_PER_WORD;
241 const size_t __offset = __shift % _GLIBCXX_BITSET_BITS_PER_WORD;
244 for (size_t __n = _Nw - 1; __n >= __wshift; --__n)
245 _M_w[__n] = _M_w[__n - __wshift];
248 const size_t __sub_offset = (_GLIBCXX_BITSET_BITS_PER_WORD
250 for (size_t __n = _Nw - 1; __n > __wshift; --__n)
251 _M_w[__n] = ((_M_w[__n - __wshift] << __offset)
252 | (_M_w[__n - __wshift - 1] >> __sub_offset));
253 _M_w[__wshift] = _M_w[0] << __offset;
256 std::fill(_M_w + 0, _M_w + __wshift, static_cast<_WordT>(0));
262 _Base_bitset<_Nw>::_M_do_right_shift(size_t __shift) _GLIBCXX_NOEXCEPT
264 if (__builtin_expect(__shift != 0, 1))
266 const size_t __wshift = __shift / _GLIBCXX_BITSET_BITS_PER_WORD;
267 const size_t __offset = __shift % _GLIBCXX_BITSET_BITS_PER_WORD;
268 const size_t __limit = _Nw - __wshift - 1;
271 for (size_t __n = 0; __n <= __limit; ++__n)
272 _M_w[__n] = _M_w[__n + __wshift];
275 const size_t __sub_offset = (_GLIBCXX_BITSET_BITS_PER_WORD
277 for (size_t __n = 0; __n < __limit; ++__n)
278 _M_w[__n] = ((_M_w[__n + __wshift] >> __offset)
279 | (_M_w[__n + __wshift + 1] << __sub_offset));
280 _M_w[__limit] = _M_w[_Nw-1] >> __offset;
283 std::fill(_M_w + __limit + 1, _M_w + _Nw, static_cast<_WordT>(0));
289 _Base_bitset<_Nw>::_M_do_to_ulong() const
291 for (size_t __i = 1; __i < _Nw; ++__i)
293 __throw_overflow_error(__N("_Base_bitset::_M_do_to_ulong"));
297 #if __cplusplus >= 201103L
300 _Base_bitset<_Nw>::_M_do_to_ullong() const
302 const bool __dw = sizeof(unsigned long long) > sizeof(unsigned long);
303 for (size_t __i = 1 + __dw; __i < _Nw; ++__i)
305 __throw_overflow_error(__N("_Base_bitset::_M_do_to_ullong"));
308 return _M_w[0] + (static_cast<unsigned long long>(_M_w[1])
309 << _GLIBCXX_BITSET_BITS_PER_WORD);
317 _M_do_find_first(size_t __not_found) const _GLIBCXX_NOEXCEPT
319 for (size_t __i = 0; __i < _Nw; __i++)
321 _WordT __thisword = _M_w[__i];
322 if (__thisword != static_cast<_WordT>(0))
323 return (__i * _GLIBCXX_BITSET_BITS_PER_WORD
324 + __builtin_ctzl(__thisword));
326 // not found, so return an indication of failure.
333 _M_do_find_next(size_t __prev, size_t __not_found) const _GLIBCXX_NOEXCEPT
335 // make bound inclusive
338 // check out of bounds
339 if (__prev >= _Nw * _GLIBCXX_BITSET_BITS_PER_WORD)
343 size_t __i = _S_whichword(__prev);
344 _WordT __thisword = _M_w[__i];
346 // mask off bits below bound
347 __thisword &= (~static_cast<_WordT>(0)) << _S_whichbit(__prev);
349 if (__thisword != static_cast<_WordT>(0))
350 return (__i * _GLIBCXX_BITSET_BITS_PER_WORD
351 + __builtin_ctzl(__thisword));
353 // check subsequent words
355 for (; __i < _Nw; __i++)
357 __thisword = _M_w[__i];
358 if (__thisword != static_cast<_WordT>(0))
359 return (__i * _GLIBCXX_BITSET_BITS_PER_WORD
360 + __builtin_ctzl(__thisword));
362 // not found, so return an indication of failure.
364 } // end _M_do_find_next
367 * Base class, specialization for a single word.
369 * See documentation for bitset.
372 struct _Base_bitset<1>
374 typedef unsigned long _WordT;
377 _GLIBCXX_CONSTEXPR _Base_bitset() _GLIBCXX_NOEXCEPT
381 #if __cplusplus >= 201103L
382 constexpr _Base_bitset(unsigned long long __val) noexcept
384 _Base_bitset(unsigned long __val)
389 static _GLIBCXX_CONSTEXPR size_t
390 _S_whichword(size_t __pos) _GLIBCXX_NOEXCEPT
391 { return __pos / _GLIBCXX_BITSET_BITS_PER_WORD; }
393 static _GLIBCXX_CONSTEXPR size_t
394 _S_whichbyte(size_t __pos) _GLIBCXX_NOEXCEPT
395 { return (__pos % _GLIBCXX_BITSET_BITS_PER_WORD) / __CHAR_BIT__; }
397 static _GLIBCXX_CONSTEXPR size_t
398 _S_whichbit(size_t __pos) _GLIBCXX_NOEXCEPT
399 { return __pos % _GLIBCXX_BITSET_BITS_PER_WORD; }
401 static _GLIBCXX_CONSTEXPR _WordT
402 _S_maskbit(size_t __pos) _GLIBCXX_NOEXCEPT
403 { return (static_cast<_WordT>(1)) << _S_whichbit(__pos); }
406 _M_getword(size_t) _GLIBCXX_NOEXCEPT
409 _GLIBCXX_CONSTEXPR _WordT
410 _M_getword(size_t) const _GLIBCXX_NOEXCEPT
413 #if __cplusplus >= 201103L
415 _M_getdata() const noexcept
420 _M_hiword() _GLIBCXX_NOEXCEPT
423 _GLIBCXX_CONSTEXPR _WordT
424 _M_hiword() const _GLIBCXX_NOEXCEPT
428 _M_do_and(const _Base_bitset<1>& __x) _GLIBCXX_NOEXCEPT
429 { _M_w &= __x._M_w; }
432 _M_do_or(const _Base_bitset<1>& __x) _GLIBCXX_NOEXCEPT
433 { _M_w |= __x._M_w; }
436 _M_do_xor(const _Base_bitset<1>& __x) _GLIBCXX_NOEXCEPT
437 { _M_w ^= __x._M_w; }
440 _M_do_left_shift(size_t __shift) _GLIBCXX_NOEXCEPT
441 { _M_w <<= __shift; }
444 _M_do_right_shift(size_t __shift) _GLIBCXX_NOEXCEPT
445 { _M_w >>= __shift; }
448 _M_do_flip() _GLIBCXX_NOEXCEPT
452 _M_do_set() _GLIBCXX_NOEXCEPT
453 { _M_w = ~static_cast<_WordT>(0); }
456 _M_do_reset() _GLIBCXX_NOEXCEPT
460 _M_is_equal(const _Base_bitset<1>& __x) const _GLIBCXX_NOEXCEPT
461 { return _M_w == __x._M_w; }
465 _M_are_all() const _GLIBCXX_NOEXCEPT
466 { return _M_w == (~static_cast<_WordT>(0)
467 >> (_GLIBCXX_BITSET_BITS_PER_WORD - _Nb)); }
470 _M_is_any() const _GLIBCXX_NOEXCEPT
471 { return _M_w != 0; }
474 _M_do_count() const _GLIBCXX_NOEXCEPT
475 { return __builtin_popcountl(_M_w); }
478 _M_do_to_ulong() const _GLIBCXX_NOEXCEPT
481 #if __cplusplus >= 201103L
483 _M_do_to_ullong() const noexcept
488 _M_do_find_first(size_t __not_found) const _GLIBCXX_NOEXCEPT
491 return __builtin_ctzl(_M_w);
496 // find the next "on" bit that follows "prev"
498 _M_do_find_next(size_t __prev, size_t __not_found) const
502 if (__prev >= ((size_t) _GLIBCXX_BITSET_BITS_PER_WORD))
505 _WordT __x = _M_w >> __prev;
507 return __builtin_ctzl(__x) + __prev;
514 * Base class, specialization for no storage (zero-length %bitset).
516 * See documentation for bitset.
519 struct _Base_bitset<0>
521 typedef unsigned long _WordT;
523 _GLIBCXX_CONSTEXPR _Base_bitset() _GLIBCXX_NOEXCEPT
526 #if __cplusplus >= 201103L
527 constexpr _Base_bitset(unsigned long long) noexcept
529 _Base_bitset(unsigned long)
533 static _GLIBCXX_CONSTEXPR size_t
534 _S_whichword(size_t __pos) _GLIBCXX_NOEXCEPT
535 { return __pos / _GLIBCXX_BITSET_BITS_PER_WORD; }
537 static _GLIBCXX_CONSTEXPR size_t
538 _S_whichbyte(size_t __pos) _GLIBCXX_NOEXCEPT
539 { return (__pos % _GLIBCXX_BITSET_BITS_PER_WORD) / __CHAR_BIT__; }
541 static _GLIBCXX_CONSTEXPR size_t
542 _S_whichbit(size_t __pos) _GLIBCXX_NOEXCEPT
543 { return __pos % _GLIBCXX_BITSET_BITS_PER_WORD; }
545 static _GLIBCXX_CONSTEXPR _WordT
546 _S_maskbit(size_t __pos) _GLIBCXX_NOEXCEPT
547 { return (static_cast<_WordT>(1)) << _S_whichbit(__pos); }
549 // This would normally give access to the data. The bounds-checking
550 // in the bitset class will prevent the user from getting this far,
551 // but (1) it must still return an lvalue to compile, and (2) the
552 // user might call _Unchecked_set directly, in which case this /needs/
553 // to fail. Let's not penalize zero-length users unless they actually
554 // make an unchecked call; all the memory ugliness is therefore
555 // localized to this single should-never-get-this-far function.
557 _M_getword(size_t) _GLIBCXX_NOEXCEPT
559 __throw_out_of_range(__N("_Base_bitset::_M_getword"));
563 _GLIBCXX_CONSTEXPR _WordT
564 _M_getword(size_t __pos) const _GLIBCXX_NOEXCEPT
567 _GLIBCXX_CONSTEXPR _WordT
568 _M_hiword() const _GLIBCXX_NOEXCEPT
572 _M_do_and(const _Base_bitset<0>&) _GLIBCXX_NOEXCEPT
576 _M_do_or(const _Base_bitset<0>&) _GLIBCXX_NOEXCEPT
580 _M_do_xor(const _Base_bitset<0>&) _GLIBCXX_NOEXCEPT
584 _M_do_left_shift(size_t) _GLIBCXX_NOEXCEPT
588 _M_do_right_shift(size_t) _GLIBCXX_NOEXCEPT
592 _M_do_flip() _GLIBCXX_NOEXCEPT
596 _M_do_set() _GLIBCXX_NOEXCEPT
600 _M_do_reset() _GLIBCXX_NOEXCEPT
603 // Are all empty bitsets equal to each other? Are they equal to
604 // themselves? How to compare a thing which has no state? What is
605 // the sound of one zero-length bitset clapping?
607 _M_is_equal(const _Base_bitset<0>&) const _GLIBCXX_NOEXCEPT
612 _M_are_all() const _GLIBCXX_NOEXCEPT
616 _M_is_any() const _GLIBCXX_NOEXCEPT
620 _M_do_count() const _GLIBCXX_NOEXCEPT
624 _M_do_to_ulong() const _GLIBCXX_NOEXCEPT
627 #if __cplusplus >= 201103L
629 _M_do_to_ullong() const noexcept
633 // Normally "not found" is the size, but that could also be
634 // misinterpreted as an index in this corner case. Oh well.
636 _M_do_find_first(size_t) const _GLIBCXX_NOEXCEPT
640 _M_do_find_next(size_t, size_t) const _GLIBCXX_NOEXCEPT
645 // Helper class to zero out the unused high-order bits in the highest word.
646 template<size_t _Extrabits>
649 typedef unsigned long _WordT;
652 _S_do_sanitize(_WordT& __val) _GLIBCXX_NOEXCEPT
653 { __val &= ~((~static_cast<_WordT>(0)) << _Extrabits); }
659 typedef unsigned long _WordT;
662 _S_do_sanitize(_WordT) _GLIBCXX_NOEXCEPT { }
665 #if __cplusplus >= 201103L
666 template<size_t _Nb, bool = _Nb < _GLIBCXX_BITSET_BITS_PER_ULL>
669 static constexpr unsigned long long
670 _S_do_sanitize_val(unsigned long long __val)
675 struct _Sanitize_val<_Nb, true>
677 static constexpr unsigned long long
678 _S_do_sanitize_val(unsigned long long __val)
679 { return __val & ~((~static_cast<unsigned long long>(0)) << _Nb); }
684 * The %bitset class represents a @e fixed-size sequence of bits.
686 * @ingroup containers
688 * (Note that %bitset does @e not meet the formal requirements of a
689 * <a href="tables.html#65">container</a>. Mainly, it lacks iterators.)
691 * The template argument, @a Nb, may be any non-negative number,
692 * specifying the number of bits (e.g., "0", "12", "1024*1024").
694 * In the general unoptimized case, storage is allocated in word-sized
695 * blocks. Let B be the number of bits in a word, then (Nb+(B-1))/B
696 * words will be used for storage. B - Nb%B bits are unused. (They are
697 * the high-order bits in the highest word.) It is a class invariant
698 * that those unused bits are always zero.
700 * If you think of %bitset as <em>a simple array of bits</em>, be
701 * aware that your mental picture is reversed: a %bitset behaves
702 * the same way as bits in integers do, with the bit at index 0 in
703 * the <em>least significant / right-hand</em> position, and the bit at
704 * index Nb-1 in the <em>most significant / left-hand</em> position.
705 * Thus, unlike other containers, a %bitset's index <em>counts from
706 * right to left</em>, to put it very loosely.
708 * This behavior is preserved when translating to and from strings. For
709 * example, the first line of the following program probably prints
710 * <em>b('a') is 0001100001</em> on a modern ASCII system.
714 * #include <iostream>
717 * using namespace std;
724 * cout << "b('a') is " << b << endl;
728 * string str = s.str();
729 * cout << "index 3 in the string is " << str[3] << " but\n"
730 * << "index 3 in the bitset is " << b[3] << endl;
735 * http://gcc.gnu.org/onlinedocs/libstdc++/manual/bk01pt12ch33s02.html
736 * for a description of extensions.
738 * Most of the actual code isn't contained in %bitset<> itself, but in the
739 * base class _Base_bitset. The base class works with whole words, not with
740 * individual bits. This allows us to specialize _Base_bitset for the
741 * important special case where the %bitset is only a single word.
743 * Extra confusion can result due to the fact that the storage for
744 * _Base_bitset @e is a regular array, and is indexed as such. This is
745 * carefully encapsulated.
749 : private _Base_bitset<_GLIBCXX_BITSET_WORDS(_Nb)>
752 typedef _Base_bitset<_GLIBCXX_BITSET_WORDS(_Nb)> _Base;
753 typedef unsigned long _WordT;
755 template<class _CharT, class _Traits, class _Alloc>
757 _M_check_initial_position(const std::basic_string<_CharT, _Traits, _Alloc>& __s,
758 size_t __position) const
760 if (__position > __s.size())
761 __throw_out_of_range_fmt(__N("bitset::bitset: __position "
762 "(which is %zu) > __s.size() "
764 __position, __s.size());
767 void _M_check(size_t __position, const char *__s) const
769 if (__position >= _Nb)
770 __throw_out_of_range_fmt(__N("%s: __position (which is %zu) "
771 ">= _Nb (which is %zu)"),
772 __s, __position, _Nb);
776 _M_do_sanitize() _GLIBCXX_NOEXCEPT
778 typedef _Sanitize<_Nb % _GLIBCXX_BITSET_BITS_PER_WORD> __sanitize_type;
779 __sanitize_type::_S_do_sanitize(this->_M_hiword());
782 #if __cplusplus >= 201103L
783 template<typename> friend struct hash;
788 * This encapsulates the concept of a single bit. An instance of this
789 * class is a proxy for an actual bit; this way the individual bit
790 * operations are done as faster word-size bitwise instructions.
792 * Most users will never need to use this class directly; conversions
793 * to and from bool are automatic and should be transparent. Overloaded
794 * operators help to preserve the illusion.
796 * (On a typical system, this <em>bit %reference</em> is 64
797 * times the size of an actual bit. Ha.)
810 reference(bitset& __b, size_t __pos) _GLIBCXX_NOEXCEPT
812 _M_wp = &__b._M_getword(__pos);
813 _M_bpos = _Base::_S_whichbit(__pos);
816 ~reference() _GLIBCXX_NOEXCEPT
821 operator=(bool __x) _GLIBCXX_NOEXCEPT
824 *_M_wp |= _Base::_S_maskbit(_M_bpos);
826 *_M_wp &= ~_Base::_S_maskbit(_M_bpos);
830 // For b[i] = b[__j];
832 operator=(const reference& __j) _GLIBCXX_NOEXCEPT
834 if ((*(__j._M_wp) & _Base::_S_maskbit(__j._M_bpos)))
835 *_M_wp |= _Base::_S_maskbit(_M_bpos);
837 *_M_wp &= ~_Base::_S_maskbit(_M_bpos);
843 operator~() const _GLIBCXX_NOEXCEPT
844 { return (*(_M_wp) & _Base::_S_maskbit(_M_bpos)) == 0; }
847 operator bool() const _GLIBCXX_NOEXCEPT
848 { return (*(_M_wp) & _Base::_S_maskbit(_M_bpos)) != 0; }
852 flip() _GLIBCXX_NOEXCEPT
854 *_M_wp ^= _Base::_S_maskbit(_M_bpos);
858 friend class reference;
860 // 23.3.5.1 constructors:
861 /// All bits set to zero.
862 _GLIBCXX_CONSTEXPR bitset() _GLIBCXX_NOEXCEPT
865 /// Initial bits bitwise-copied from a single word (others set to zero).
866 #if __cplusplus >= 201103L
867 constexpr bitset(unsigned long long __val) noexcept
868 : _Base(_Sanitize_val<_Nb>::_S_do_sanitize_val(__val)) { }
870 bitset(unsigned long __val)
872 { _M_do_sanitize(); }
876 * Use a subset of a string.
877 * @param __s A string of @a 0 and @a 1 characters.
878 * @param __position Index of the first character in @a __s to use;
880 * @throw std::out_of_range If @a pos is bigger the size of @a __s.
881 * @throw std::invalid_argument If a character appears in the string
882 * which is neither @a 0 nor @a 1.
884 template<class _CharT, class _Traits, class _Alloc>
886 bitset(const std::basic_string<_CharT, _Traits, _Alloc>& __s,
887 size_t __position = 0)
890 _M_check_initial_position(__s, __position);
891 _M_copy_from_string(__s, __position,
892 std::basic_string<_CharT, _Traits, _Alloc>::npos,
893 _CharT('0'), _CharT('1'));
897 * Use a subset of a string.
898 * @param __s A string of @a 0 and @a 1 characters.
899 * @param __position Index of the first character in @a __s to use.
900 * @param __n The number of characters to copy.
901 * @throw std::out_of_range If @a __position is bigger the size
903 * @throw std::invalid_argument If a character appears in the string
904 * which is neither @a 0 nor @a 1.
906 template<class _CharT, class _Traits, class _Alloc>
907 bitset(const std::basic_string<_CharT, _Traits, _Alloc>& __s,
908 size_t __position, size_t __n)
911 _M_check_initial_position(__s, __position);
912 _M_copy_from_string(__s, __position, __n, _CharT('0'), _CharT('1'));
915 // _GLIBCXX_RESOLVE_LIB_DEFECTS
916 // 396. what are characters zero and one.
917 template<class _CharT, class _Traits, class _Alloc>
918 bitset(const std::basic_string<_CharT, _Traits, _Alloc>& __s,
919 size_t __position, size_t __n,
920 _CharT __zero, _CharT __one = _CharT('1'))
923 _M_check_initial_position(__s, __position);
924 _M_copy_from_string(__s, __position, __n, __zero, __one);
927 #if __cplusplus >= 201103L
929 * Construct from a character %array.
930 * @param __str An %array of characters @a zero and @a one.
931 * @param __n The number of characters to use.
932 * @param __zero The character corresponding to the value 0.
933 * @param __one The character corresponding to the value 1.
934 * @throw std::invalid_argument If a character appears in the string
935 * which is neither @a __zero nor @a __one.
937 template<typename _CharT>
939 bitset(const _CharT* __str,
940 typename std::basic_string<_CharT>::size_type __n
941 = std::basic_string<_CharT>::npos,
942 _CharT __zero = _CharT('0'), _CharT __one = _CharT('1'))
946 __throw_logic_error(__N("bitset::bitset(const _CharT*, ...)"));
948 if (__n == std::basic_string<_CharT>::npos)
949 __n = std::char_traits<_CharT>::length(__str);
950 _M_copy_from_ptr<_CharT, std::char_traits<_CharT>>(__str, __n, 0,
956 // 23.3.5.2 bitset operations:
959 * Operations on bitsets.
960 * @param __rhs A same-sized bitset.
962 * These should be self-explanatory.
965 operator&=(const bitset<_Nb>& __rhs) _GLIBCXX_NOEXCEPT
967 this->_M_do_and(__rhs);
972 operator|=(const bitset<_Nb>& __rhs) _GLIBCXX_NOEXCEPT
974 this->_M_do_or(__rhs);
979 operator^=(const bitset<_Nb>& __rhs) _GLIBCXX_NOEXCEPT
981 this->_M_do_xor(__rhs);
988 * Operations on bitsets.
989 * @param __position The number of places to shift.
991 * These should be self-explanatory.
994 operator<<=(size_t __position) _GLIBCXX_NOEXCEPT
996 if (__builtin_expect(__position < _Nb, 1))
998 this->_M_do_left_shift(__position);
999 this->_M_do_sanitize();
1002 this->_M_do_reset();
1007 operator>>=(size_t __position) _GLIBCXX_NOEXCEPT
1009 if (__builtin_expect(__position < _Nb, 1))
1011 this->_M_do_right_shift(__position);
1012 this->_M_do_sanitize();
1015 this->_M_do_reset();
1022 * These versions of single-bit set, reset, flip, and test are
1023 * extensions from the SGI version. They do no range checking.
1024 * @ingroup SGIextensions
1027 _Unchecked_set(size_t __pos) _GLIBCXX_NOEXCEPT
1029 this->_M_getword(__pos) |= _Base::_S_maskbit(__pos);
1034 _Unchecked_set(size_t __pos, int __val) _GLIBCXX_NOEXCEPT
1037 this->_M_getword(__pos) |= _Base::_S_maskbit(__pos);
1039 this->_M_getword(__pos) &= ~_Base::_S_maskbit(__pos);
1044 _Unchecked_reset(size_t __pos) _GLIBCXX_NOEXCEPT
1046 this->_M_getword(__pos) &= ~_Base::_S_maskbit(__pos);
1051 _Unchecked_flip(size_t __pos) _GLIBCXX_NOEXCEPT
1053 this->_M_getword(__pos) ^= _Base::_S_maskbit(__pos);
1057 _GLIBCXX_CONSTEXPR bool
1058 _Unchecked_test(size_t __pos) const _GLIBCXX_NOEXCEPT
1059 { return ((this->_M_getword(__pos) & _Base::_S_maskbit(__pos))
1060 != static_cast<_WordT>(0)); }
1063 // Set, reset, and flip.
1065 * @brief Sets every bit to true.
1068 set() _GLIBCXX_NOEXCEPT
1071 this->_M_do_sanitize();
1076 * @brief Sets a given bit to a particular value.
1077 * @param __position The index of the bit.
1078 * @param __val Either true or false, defaults to true.
1079 * @throw std::out_of_range If @a pos is bigger the size of the %set.
1082 set(size_t __position, bool __val = true)
1084 this->_M_check(__position, __N("bitset::set"));
1085 return _Unchecked_set(__position, __val);
1089 * @brief Sets every bit to false.
1092 reset() _GLIBCXX_NOEXCEPT
1094 this->_M_do_reset();
1099 * @brief Sets a given bit to false.
1100 * @param __position The index of the bit.
1101 * @throw std::out_of_range If @a pos is bigger the size of the %set.
1103 * Same as writing @c set(pos,false).
1106 reset(size_t __position)
1108 this->_M_check(__position, __N("bitset::reset"));
1109 return _Unchecked_reset(__position);
1113 * @brief Toggles every bit to its opposite value.
1116 flip() _GLIBCXX_NOEXCEPT
1119 this->_M_do_sanitize();
1124 * @brief Toggles a given bit to its opposite value.
1125 * @param __position The index of the bit.
1126 * @throw std::out_of_range If @a pos is bigger the size of the %set.
1129 flip(size_t __position)
1131 this->_M_check(__position, __N("bitset::flip"));
1132 return _Unchecked_flip(__position);
1135 /// See the no-argument flip().
1137 operator~() const _GLIBCXX_NOEXCEPT
1138 { return bitset<_Nb>(*this).flip(); }
1142 * @brief Array-indexing support.
1143 * @param __position Index into the %bitset.
1144 * @return A bool for a <em>const %bitset</em>. For non-const
1145 * bitsets, an instance of the reference proxy class.
1146 * @note These operators do no range checking and throw no exceptions,
1147 * as required by DR 11 to the standard.
1149 * _GLIBCXX_RESOLVE_LIB_DEFECTS Note that this implementation already
1150 * resolves DR 11 (items 1 and 2), but does not do the range-checking
1151 * required by that DR's resolution. -pme
1152 * The DR has since been changed: range-checking is a precondition
1153 * (users' responsibility), and these functions must not throw. -pme
1156 operator[](size_t __position)
1157 { return reference(*this, __position); }
1159 _GLIBCXX_CONSTEXPR bool
1160 operator[](size_t __position) const
1161 { return _Unchecked_test(__position); }
1165 * @brief Returns a numerical interpretation of the %bitset.
1166 * @return The integral equivalent of the bits.
1167 * @throw std::overflow_error If there are too many bits to be
1168 * represented in an @c unsigned @c long.
1172 { return this->_M_do_to_ulong(); }
1174 #if __cplusplus >= 201103L
1177 { return this->_M_do_to_ullong(); }
1181 * @brief Returns a character interpretation of the %bitset.
1182 * @return The string equivalent of the bits.
1184 * Note the ordering of the bits: decreasing character positions
1185 * correspond to increasing bit positions (see the main class notes for
1188 template<class _CharT, class _Traits, class _Alloc>
1189 std::basic_string<_CharT, _Traits, _Alloc>
1192 std::basic_string<_CharT, _Traits, _Alloc> __result;
1193 _M_copy_to_string(__result, _CharT('0'), _CharT('1'));
1197 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1198 // 396. what are characters zero and one.
1199 template<class _CharT, class _Traits, class _Alloc>
1200 std::basic_string<_CharT, _Traits, _Alloc>
1201 to_string(_CharT __zero, _CharT __one = _CharT('1')) const
1203 std::basic_string<_CharT, _Traits, _Alloc> __result;
1204 _M_copy_to_string(__result, __zero, __one);
1208 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1209 // 434. bitset::to_string() hard to use.
1210 template<class _CharT, class _Traits>
1211 std::basic_string<_CharT, _Traits, std::allocator<_CharT> >
1213 { return to_string<_CharT, _Traits, std::allocator<_CharT> >(); }
1215 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1216 // 853. to_string needs updating with zero and one.
1217 template<class _CharT, class _Traits>
1218 std::basic_string<_CharT, _Traits, std::allocator<_CharT> >
1219 to_string(_CharT __zero, _CharT __one = _CharT('1')) const
1220 { return to_string<_CharT, _Traits,
1221 std::allocator<_CharT> >(__zero, __one); }
1223 template<class _CharT>
1224 std::basic_string<_CharT, std::char_traits<_CharT>,
1225 std::allocator<_CharT> >
1228 return to_string<_CharT, std::char_traits<_CharT>,
1229 std::allocator<_CharT> >();
1232 template<class _CharT>
1233 std::basic_string<_CharT, std::char_traits<_CharT>,
1234 std::allocator<_CharT> >
1235 to_string(_CharT __zero, _CharT __one = _CharT('1')) const
1237 return to_string<_CharT, std::char_traits<_CharT>,
1238 std::allocator<_CharT> >(__zero, __one);
1241 std::basic_string<char, std::char_traits<char>, std::allocator<char> >
1244 return to_string<char, std::char_traits<char>,
1245 std::allocator<char> >();
1248 std::basic_string<char, std::char_traits<char>, std::allocator<char> >
1249 to_string(char __zero, char __one = '1') const
1251 return to_string<char, std::char_traits<char>,
1252 std::allocator<char> >(__zero, __one);
1255 // Helper functions for string operations.
1256 template<class _CharT, class _Traits>
1258 _M_copy_from_ptr(const _CharT*, size_t, size_t, size_t,
1261 template<class _CharT, class _Traits, class _Alloc>
1263 _M_copy_from_string(const std::basic_string<_CharT,
1264 _Traits, _Alloc>& __s, size_t __pos, size_t __n,
1265 _CharT __zero, _CharT __one)
1266 { _M_copy_from_ptr<_CharT, _Traits>(__s.data(), __s.size(), __pos, __n,
1269 template<class _CharT, class _Traits, class _Alloc>
1271 _M_copy_to_string(std::basic_string<_CharT, _Traits, _Alloc>&,
1272 _CharT, _CharT) const;
1274 // NB: Backward compat.
1275 template<class _CharT, class _Traits, class _Alloc>
1277 _M_copy_from_string(const std::basic_string<_CharT,
1278 _Traits, _Alloc>& __s, size_t __pos, size_t __n)
1279 { _M_copy_from_string(__s, __pos, __n, _CharT('0'), _CharT('1')); }
1281 template<class _CharT, class _Traits, class _Alloc>
1283 _M_copy_to_string(std::basic_string<_CharT, _Traits,_Alloc>& __s) const
1284 { _M_copy_to_string(__s, _CharT('0'), _CharT('1')); }
1286 /// Returns the number of bits which are set.
1288 count() const _GLIBCXX_NOEXCEPT
1289 { return this->_M_do_count(); }
1291 /// Returns the total number of bits.
1292 _GLIBCXX_CONSTEXPR size_t
1293 size() const _GLIBCXX_NOEXCEPT
1297 /// These comparisons for equality/inequality are, well, @e bitwise.
1299 operator==(const bitset<_Nb>& __rhs) const _GLIBCXX_NOEXCEPT
1300 { return this->_M_is_equal(__rhs); }
1303 operator!=(const bitset<_Nb>& __rhs) const _GLIBCXX_NOEXCEPT
1304 { return !this->_M_is_equal(__rhs); }
1308 * @brief Tests the value of a bit.
1309 * @param __position The index of a bit.
1310 * @return The value at @a pos.
1311 * @throw std::out_of_range If @a pos is bigger the size of the %set.
1314 test(size_t __position) const
1316 this->_M_check(__position, __N("bitset::test"));
1317 return _Unchecked_test(__position);
1320 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1321 // DR 693. std::bitset::all() missing.
1323 * @brief Tests whether all the bits are on.
1324 * @return True if all the bits are set.
1327 all() const _GLIBCXX_NOEXCEPT
1328 { return this->template _M_are_all<_Nb>(); }
1331 * @brief Tests whether any of the bits are on.
1332 * @return True if at least one bit is set.
1335 any() const _GLIBCXX_NOEXCEPT
1336 { return this->_M_is_any(); }
1339 * @brief Tests whether any of the bits are on.
1340 * @return True if none of the bits are set.
1343 none() const _GLIBCXX_NOEXCEPT
1344 { return !this->_M_is_any(); }
1347 /// Self-explanatory.
1349 operator<<(size_t __position) const _GLIBCXX_NOEXCEPT
1350 { return bitset<_Nb>(*this) <<= __position; }
1353 operator>>(size_t __position) const _GLIBCXX_NOEXCEPT
1354 { return bitset<_Nb>(*this) >>= __position; }
1358 * @brief Finds the index of the first "on" bit.
1359 * @return The index of the first bit set, or size() if not found.
1360 * @ingroup SGIextensions
1364 _Find_first() const _GLIBCXX_NOEXCEPT
1365 { return this->_M_do_find_first(_Nb); }
1368 * @brief Finds the index of the next "on" bit after prev.
1369 * @return The index of the next bit set, or size() if not found.
1370 * @param __prev Where to start searching.
1371 * @ingroup SGIextensions
1375 _Find_next(size_t __prev) const _GLIBCXX_NOEXCEPT
1376 { return this->_M_do_find_next(__prev, _Nb); }
1379 // Definitions of non-inline member functions.
1380 template<size_t _Nb>
1381 template<class _CharT, class _Traits>
1384 _M_copy_from_ptr(const _CharT* __s, size_t __len,
1385 size_t __pos, size_t __n, _CharT __zero, _CharT __one)
1388 const size_t __nbits = std::min(_Nb, std::min(__n, size_t(__len - __pos)));
1389 for (size_t __i = __nbits; __i > 0; --__i)
1391 const _CharT __c = __s[__pos + __nbits - __i];
1392 if (_Traits::eq(__c, __zero))
1394 else if (_Traits::eq(__c, __one))
1395 _Unchecked_set(__i - 1);
1397 __throw_invalid_argument(__N("bitset::_M_copy_from_ptr"));
1401 template<size_t _Nb>
1402 template<class _CharT, class _Traits, class _Alloc>
1405 _M_copy_to_string(std::basic_string<_CharT, _Traits, _Alloc>& __s,
1406 _CharT __zero, _CharT __one) const
1408 __s.assign(_Nb, __zero);
1409 for (size_t __i = _Nb; __i > 0; --__i)
1410 if (_Unchecked_test(__i - 1))
1411 _Traits::assign(__s[_Nb - __i], __one);
1414 // 23.3.5.3 bitset operations:
1417 * @brief Global bitwise operations on bitsets.
1418 * @param __x A bitset.
1419 * @param __y A bitset of the same size as @a __x.
1420 * @return A new bitset.
1422 * These should be self-explanatory.
1424 template<size_t _Nb>
1426 operator&(const bitset<_Nb>& __x, const bitset<_Nb>& __y) _GLIBCXX_NOEXCEPT
1428 bitset<_Nb> __result(__x);
1433 template<size_t _Nb>
1435 operator|(const bitset<_Nb>& __x, const bitset<_Nb>& __y) _GLIBCXX_NOEXCEPT
1437 bitset<_Nb> __result(__x);
1442 template <size_t _Nb>
1444 operator^(const bitset<_Nb>& __x, const bitset<_Nb>& __y) _GLIBCXX_NOEXCEPT
1446 bitset<_Nb> __result(__x);
1454 * @brief Global I/O operators for bitsets.
1456 * Direct I/O between streams and bitsets is supported. Output is
1457 * straightforward. Input will skip whitespace, only accept @a 0 and @a 1
1458 * characters, and will only extract as many digits as the %bitset will
1461 template<class _CharT, class _Traits, size_t _Nb>
1462 std::basic_istream<_CharT, _Traits>&
1463 operator>>(std::basic_istream<_CharT, _Traits>& __is, bitset<_Nb>& __x)
1465 typedef typename _Traits::char_type char_type;
1466 typedef std::basic_istream<_CharT, _Traits> __istream_type;
1467 typedef typename __istream_type::ios_base __ios_base;
1469 std::basic_string<_CharT, _Traits> __tmp;
1472 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1473 // 303. Bitset input operator underspecified
1474 const char_type __zero = __is.widen('0');
1475 const char_type __one = __is.widen('1');
1477 typename __ios_base::iostate __state = __ios_base::goodbit;
1478 typename __istream_type::sentry __sentry(__is);
1483 for (size_t __i = _Nb; __i > 0; --__i)
1485 static typename _Traits::int_type __eof = _Traits::eof();
1487 typename _Traits::int_type __c1 = __is.rdbuf()->sbumpc();
1488 if (_Traits::eq_int_type(__c1, __eof))
1490 __state |= __ios_base::eofbit;
1495 const char_type __c2 = _Traits::to_char_type(__c1);
1496 if (_Traits::eq(__c2, __zero))
1497 __tmp.push_back(__zero);
1498 else if (_Traits::eq(__c2, __one))
1499 __tmp.push_back(__one);
1501 eq_int_type(__is.rdbuf()->sputbackc(__c2),
1504 __state |= __ios_base::failbit;
1510 __catch(__cxxabiv1::__forced_unwind&)
1512 __is._M_setstate(__ios_base::badbit);
1513 __throw_exception_again;
1516 { __is._M_setstate(__ios_base::badbit); }
1519 if (__tmp.empty() && _Nb)
1520 __state |= __ios_base::failbit;
1522 __x._M_copy_from_string(__tmp, static_cast<size_t>(0), _Nb,
1525 __is.setstate(__state);
1529 template <class _CharT, class _Traits, size_t _Nb>
1530 std::basic_ostream<_CharT, _Traits>&
1531 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1532 const bitset<_Nb>& __x)
1534 std::basic_string<_CharT, _Traits> __tmp;
1536 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1537 // 396. what are characters zero and one.
1538 const ctype<_CharT>& __ct = use_facet<ctype<_CharT> >(__os.getloc());
1539 __x._M_copy_to_string(__tmp, __ct.widen('0'), __ct.widen('1'));
1540 return __os << __tmp;
1544 _GLIBCXX_END_NAMESPACE_CONTAINER
1547 #undef _GLIBCXX_BITSET_WORDS
1548 #undef _GLIBCXX_BITSET_BITS_PER_WORD
1549 #undef _GLIBCXX_BITSET_BITS_PER_ULL
1551 #if __cplusplus >= 201103L
1553 #include <bits/functional_hash.h>
1555 namespace std _GLIBCXX_VISIBILITY(default)
1557 _GLIBCXX_BEGIN_NAMESPACE_VERSION
1560 /// std::hash specialization for bitset.
1561 template<size_t _Nb>
1562 struct hash<_GLIBCXX_STD_C::bitset<_Nb>>
1563 : public __hash_base<size_t, _GLIBCXX_STD_C::bitset<_Nb>>
1566 operator()(const _GLIBCXX_STD_C::bitset<_Nb>& __b) const noexcept
1568 const size_t __clength = (_Nb + __CHAR_BIT__ - 1) / __CHAR_BIT__;
1569 return std::_Hash_impl::hash(__b._M_getdata(), __clength);
1574 struct hash<_GLIBCXX_STD_C::bitset<0>>
1575 : public __hash_base<size_t, _GLIBCXX_STD_C::bitset<0>>
1578 operator()(const _GLIBCXX_STD_C::bitset<0>&) const noexcept
1582 _GLIBCXX_END_NAMESPACE_VERSION
1587 #ifdef _GLIBCXX_DEBUG
1588 # include <debug/bitset>
1591 #ifdef _GLIBCXX_PROFILE
1592 # include <profile/bitset>
1595 #endif /* _GLIBCXX_BITSET */