1 // <bitset> -*- C++ -*-
3 // Copyright (C) 2001-2024 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
47 #include <bits/functexcept.h> // For invalid_argument, out_of_range,
49 #include <bits/stl_algobase.h> // For std::fill
54 # include <bits/cxxabi_forced.h>
57 #if __cplusplus >= 201103L
58 # include <bits/functional_hash.h>
61 #define __glibcxx_want_constexpr_bitset
62 #include <bits/version.h>
64 #define _GLIBCXX_BITSET_BITS_PER_WORD (__CHAR_BIT__ * __SIZEOF_LONG__)
65 #define _GLIBCXX_BITSET_WORDS(__n) \
66 ((__n) / _GLIBCXX_BITSET_BITS_PER_WORD + \
67 ((__n) % _GLIBCXX_BITSET_BITS_PER_WORD == 0 ? 0 : 1))
69 #define _GLIBCXX_BITSET_BITS_PER_ULL (__CHAR_BIT__ * __SIZEOF_LONG_LONG__)
71 namespace std _GLIBCXX_VISIBILITY(default)
73 _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
76 * Base class, general case. It is a class invariant that _Nw will be
79 * See documentation for bitset.
84 typedef unsigned long _WordT;
86 /// 0 is the least significant word.
89 _GLIBCXX_CONSTEXPR _Base_bitset() _GLIBCXX_NOEXCEPT
92 #if __cplusplus >= 201103L
93 constexpr _Base_bitset(unsigned long long __val) noexcept
95 #if __SIZEOF_LONG_LONG__ > __SIZEOF_LONG__
96 , _WordT(__val >> _GLIBCXX_BITSET_BITS_PER_WORD)
100 _Base_bitset(unsigned long __val)
105 static _GLIBCXX_CONSTEXPR size_t
106 _S_whichword(size_t __pos) _GLIBCXX_NOEXCEPT
107 { return __pos / _GLIBCXX_BITSET_BITS_PER_WORD; }
109 static _GLIBCXX_CONSTEXPR size_t
110 _S_whichbyte(size_t __pos) _GLIBCXX_NOEXCEPT
111 { return (__pos % _GLIBCXX_BITSET_BITS_PER_WORD) / __CHAR_BIT__; }
113 static _GLIBCXX_CONSTEXPR size_t
114 _S_whichbit(size_t __pos) _GLIBCXX_NOEXCEPT
115 { return __pos % _GLIBCXX_BITSET_BITS_PER_WORD; }
117 static _GLIBCXX_CONSTEXPR _WordT
118 _S_maskbit(size_t __pos) _GLIBCXX_NOEXCEPT
119 { return (static_cast<_WordT>(1)) << _S_whichbit(__pos); }
121 _GLIBCXX14_CONSTEXPR _WordT&
122 _M_getword(size_t __pos) _GLIBCXX_NOEXCEPT
123 { return _M_w[_S_whichword(__pos)]; }
125 _GLIBCXX_CONSTEXPR _WordT
126 _M_getword(size_t __pos) const _GLIBCXX_NOEXCEPT
127 { return _M_w[_S_whichword(__pos)]; }
129 #if __cplusplus >= 201103L
130 constexpr const _WordT*
131 _M_getdata() const noexcept
135 _GLIBCXX23_CONSTEXPR _WordT&
136 _M_hiword() _GLIBCXX_NOEXCEPT
137 { return _M_w[_Nw - 1]; }
139 _GLIBCXX_CONSTEXPR _WordT
140 _M_hiword() const _GLIBCXX_NOEXCEPT
141 { return _M_w[_Nw - 1]; }
143 _GLIBCXX23_CONSTEXPR void
144 _M_do_and(const _Base_bitset<_Nw>& __x) _GLIBCXX_NOEXCEPT
146 for (size_t __i = 0; __i < _Nw; __i++)
147 _M_w[__i] &= __x._M_w[__i];
150 _GLIBCXX14_CONSTEXPR void
151 _M_do_or(const _Base_bitset<_Nw>& __x) _GLIBCXX_NOEXCEPT
153 for (size_t __i = 0; __i < _Nw; __i++)
154 _M_w[__i] |= __x._M_w[__i];
157 _GLIBCXX14_CONSTEXPR void
158 _M_do_xor(const _Base_bitset<_Nw>& __x) _GLIBCXX_NOEXCEPT
160 for (size_t __i = 0; __i < _Nw; __i++)
161 _M_w[__i] ^= __x._M_w[__i];
164 _GLIBCXX14_CONSTEXPR void
165 _M_do_left_shift(size_t __shift) _GLIBCXX_NOEXCEPT;
167 _GLIBCXX14_CONSTEXPR void
168 _M_do_right_shift(size_t __shift) _GLIBCXX_NOEXCEPT;
170 _GLIBCXX14_CONSTEXPR void
171 _M_do_flip() _GLIBCXX_NOEXCEPT
173 for (size_t __i = 0; __i < _Nw; __i++)
174 _M_w[__i] = ~_M_w[__i];
177 _GLIBCXX14_CONSTEXPR void
178 _M_do_set() _GLIBCXX_NOEXCEPT
180 #if __cplusplus >= 201402L
181 if (__builtin_is_constant_evaluated())
183 for (_WordT& __w : _M_w)
184 __w = ~static_cast<_WordT>(0);;
188 __builtin_memset(_M_w, 0xFF, _Nw * sizeof(_WordT));
191 _GLIBCXX14_CONSTEXPR void
192 _M_do_reset() _GLIBCXX_NOEXCEPT
194 #if __cplusplus >= 201402L
195 if (__builtin_is_constant_evaluated())
197 for (_WordT& __w : _M_w)
202 __builtin_memset(_M_w, 0, _Nw * sizeof(_WordT));
205 _GLIBCXX14_CONSTEXPR bool
206 _M_is_equal(const _Base_bitset<_Nw>& __x) const _GLIBCXX_NOEXCEPT
208 for (size_t __i = 0; __i < _Nw; ++__i)
209 if (_M_w[__i] != __x._M_w[__i])
215 _GLIBCXX14_CONSTEXPR bool
216 _M_are_all() const _GLIBCXX_NOEXCEPT
218 for (size_t __i = 0; __i < _Nw - 1; __i++)
219 if (_M_w[__i] != ~static_cast<_WordT>(0))
221 return _M_hiword() == (~static_cast<_WordT>(0)
222 >> (_Nw * _GLIBCXX_BITSET_BITS_PER_WORD
226 _GLIBCXX14_CONSTEXPR bool
227 _M_is_any() const _GLIBCXX_NOEXCEPT
229 for (size_t __i = 0; __i < _Nw; __i++)
230 if (_M_w[__i] != static_cast<_WordT>(0))
235 _GLIBCXX14_CONSTEXPR size_t
236 _M_do_count() const _GLIBCXX_NOEXCEPT
239 for (size_t __i = 0; __i < _Nw; __i++)
240 __result += __builtin_popcountl(_M_w[__i]);
244 _GLIBCXX14_CONSTEXPR unsigned long
245 _M_do_to_ulong() const;
247 #if __cplusplus >= 201103L
248 _GLIBCXX14_CONSTEXPR unsigned long long
249 _M_do_to_ullong() const;
252 // find first "on" bit
253 _GLIBCXX14_CONSTEXPR size_t
254 _M_do_find_first(size_t) const _GLIBCXX_NOEXCEPT;
256 // find the next "on" bit that follows "prev"
257 _GLIBCXX14_CONSTEXPR size_t
258 _M_do_find_next(size_t, size_t) const _GLIBCXX_NOEXCEPT;
261 // Definitions of non-inline functions from _Base_bitset.
263 _GLIBCXX14_CONSTEXPR void
264 _Base_bitset<_Nw>::_M_do_left_shift(size_t __shift) _GLIBCXX_NOEXCEPT
266 if (__builtin_expect(__shift != 0, 1))
268 const size_t __wshift = __shift / _GLIBCXX_BITSET_BITS_PER_WORD;
269 const size_t __offset = __shift % _GLIBCXX_BITSET_BITS_PER_WORD;
272 for (size_t __n = _Nw - 1; __n >= __wshift; --__n)
273 _M_w[__n] = _M_w[__n - __wshift];
276 const size_t __sub_offset = (_GLIBCXX_BITSET_BITS_PER_WORD
278 for (size_t __n = _Nw - 1; __n > __wshift; --__n)
279 _M_w[__n] = ((_M_w[__n - __wshift] << __offset)
280 | (_M_w[__n - __wshift - 1] >> __sub_offset));
281 _M_w[__wshift] = _M_w[0] << __offset;
284 std::fill(_M_w + 0, _M_w + __wshift, static_cast<_WordT>(0));
289 _GLIBCXX14_CONSTEXPR void
290 _Base_bitset<_Nw>::_M_do_right_shift(size_t __shift) _GLIBCXX_NOEXCEPT
292 if (__builtin_expect(__shift != 0, 1))
294 const size_t __wshift = __shift / _GLIBCXX_BITSET_BITS_PER_WORD;
295 const size_t __offset = __shift % _GLIBCXX_BITSET_BITS_PER_WORD;
296 const size_t __limit = _Nw - __wshift - 1;
299 for (size_t __n = 0; __n <= __limit; ++__n)
300 _M_w[__n] = _M_w[__n + __wshift];
303 const size_t __sub_offset = (_GLIBCXX_BITSET_BITS_PER_WORD
305 for (size_t __n = 0; __n < __limit; ++__n)
306 _M_w[__n] = ((_M_w[__n + __wshift] >> __offset)
307 | (_M_w[__n + __wshift + 1] << __sub_offset));
308 _M_w[__limit] = _M_w[_Nw-1] >> __offset;
311 std::fill(_M_w + __limit + 1, _M_w + _Nw, static_cast<_WordT>(0));
316 _GLIBCXX14_CONSTEXPR unsigned long
317 _Base_bitset<_Nw>::_M_do_to_ulong() const
319 for (size_t __i = 1; __i < _Nw; ++__i)
321 __throw_overflow_error(__N("_Base_bitset::_M_do_to_ulong"));
325 #if __cplusplus >= 201103L
327 _GLIBCXX14_CONSTEXPR unsigned long long
328 _Base_bitset<_Nw>::_M_do_to_ullong() const
330 #if __SIZEOF_LONG_LONG__ == __SIZEOF_LONG__
331 return _M_do_to_ulong();
333 for (size_t __i = 2; __i < _Nw; ++__i)
335 __throw_overflow_error(__N("_Base_bitset::_M_do_to_ullong"));
337 return _M_w[0] + (static_cast<unsigned long long>(_M_w[1])
338 << _GLIBCXX_BITSET_BITS_PER_WORD);
344 _GLIBCXX14_CONSTEXPR size_t
346 _M_do_find_first(size_t __not_found) const _GLIBCXX_NOEXCEPT
348 for (size_t __i = 0; __i < _Nw; __i++)
350 _WordT __thisword = _M_w[__i];
351 if (__thisword != static_cast<_WordT>(0))
352 return (__i * _GLIBCXX_BITSET_BITS_PER_WORD
353 + __builtin_ctzl(__thisword));
355 // not found, so return an indication of failure.
360 _GLIBCXX14_CONSTEXPR size_t
362 _M_do_find_next(size_t __prev, size_t __not_found) const _GLIBCXX_NOEXCEPT
364 // make bound inclusive
367 // check out of bounds
368 if (__prev >= _Nw * _GLIBCXX_BITSET_BITS_PER_WORD)
372 size_t __i = _S_whichword(__prev);
373 _WordT __thisword = _M_w[__i];
375 // mask off bits below bound
376 __thisword &= (~static_cast<_WordT>(0)) << _S_whichbit(__prev);
378 if (__thisword != static_cast<_WordT>(0))
379 return (__i * _GLIBCXX_BITSET_BITS_PER_WORD
380 + __builtin_ctzl(__thisword));
382 // check subsequent words
384 for (; __i < _Nw; __i++)
386 __thisword = _M_w[__i];
387 if (__thisword != static_cast<_WordT>(0))
388 return (__i * _GLIBCXX_BITSET_BITS_PER_WORD
389 + __builtin_ctzl(__thisword));
391 // not found, so return an indication of failure.
393 } // end _M_do_find_next
396 * Base class, specialization for a single word.
398 * See documentation for bitset.
401 struct _Base_bitset<1>
403 typedef unsigned long _WordT;
406 _GLIBCXX_CONSTEXPR _Base_bitset() _GLIBCXX_NOEXCEPT
410 #if __cplusplus >= 201103L
411 constexpr _Base_bitset(unsigned long long __val) noexcept
413 _Base_bitset(unsigned long __val)
418 static _GLIBCXX_CONSTEXPR size_t
419 _S_whichword(size_t __pos) _GLIBCXX_NOEXCEPT
420 { return __pos / _GLIBCXX_BITSET_BITS_PER_WORD; }
422 static _GLIBCXX_CONSTEXPR size_t
423 _S_whichbyte(size_t __pos) _GLIBCXX_NOEXCEPT
424 { return (__pos % _GLIBCXX_BITSET_BITS_PER_WORD) / __CHAR_BIT__; }
426 static _GLIBCXX_CONSTEXPR size_t
427 _S_whichbit(size_t __pos) _GLIBCXX_NOEXCEPT
428 { return __pos % _GLIBCXX_BITSET_BITS_PER_WORD; }
430 static _GLIBCXX_CONSTEXPR _WordT
431 _S_maskbit(size_t __pos) _GLIBCXX_NOEXCEPT
432 { return (static_cast<_WordT>(1)) << _S_whichbit(__pos); }
434 _GLIBCXX14_CONSTEXPR _WordT&
435 _M_getword(size_t) _GLIBCXX_NOEXCEPT
438 _GLIBCXX_CONSTEXPR _WordT
439 _M_getword(size_t) const _GLIBCXX_NOEXCEPT
442 #if __cplusplus >= 201103L
443 constexpr const _WordT*
444 _M_getdata() const noexcept
448 _GLIBCXX14_CONSTEXPR _WordT&
449 _M_hiword() _GLIBCXX_NOEXCEPT
452 _GLIBCXX_CONSTEXPR _WordT
453 _M_hiword() const _GLIBCXX_NOEXCEPT
456 _GLIBCXX14_CONSTEXPR void
457 _M_do_and(const _Base_bitset<1>& __x) _GLIBCXX_NOEXCEPT
458 { _M_w &= __x._M_w; }
460 _GLIBCXX14_CONSTEXPR void
461 _M_do_or(const _Base_bitset<1>& __x) _GLIBCXX_NOEXCEPT
462 { _M_w |= __x._M_w; }
464 _GLIBCXX14_CONSTEXPR void
465 _M_do_xor(const _Base_bitset<1>& __x) _GLIBCXX_NOEXCEPT
466 { _M_w ^= __x._M_w; }
468 _GLIBCXX14_CONSTEXPR void
469 _M_do_left_shift(size_t __shift) _GLIBCXX_NOEXCEPT
470 { _M_w <<= __shift; }
472 _GLIBCXX14_CONSTEXPR void
473 _M_do_right_shift(size_t __shift) _GLIBCXX_NOEXCEPT
474 { _M_w >>= __shift; }
476 _GLIBCXX14_CONSTEXPR void
477 _M_do_flip() _GLIBCXX_NOEXCEPT
480 _GLIBCXX14_CONSTEXPR void
481 _M_do_set() _GLIBCXX_NOEXCEPT
482 { _M_w = ~static_cast<_WordT>(0); }
484 _GLIBCXX14_CONSTEXPR void
485 _M_do_reset() _GLIBCXX_NOEXCEPT
488 _GLIBCXX14_CONSTEXPR bool
489 _M_is_equal(const _Base_bitset<1>& __x) const _GLIBCXX_NOEXCEPT
490 { return _M_w == __x._M_w; }
493 _GLIBCXX14_CONSTEXPR bool
494 _M_are_all() const _GLIBCXX_NOEXCEPT
495 { return _M_w == (~static_cast<_WordT>(0)
496 >> (_GLIBCXX_BITSET_BITS_PER_WORD - _Nb)); }
498 _GLIBCXX14_CONSTEXPR bool
499 _M_is_any() const _GLIBCXX_NOEXCEPT
500 { return _M_w != 0; }
502 _GLIBCXX14_CONSTEXPR size_t
503 _M_do_count() const _GLIBCXX_NOEXCEPT
504 { return __builtin_popcountl(_M_w); }
506 _GLIBCXX14_CONSTEXPR unsigned long
507 _M_do_to_ulong() const _GLIBCXX_NOEXCEPT
510 #if __cplusplus >= 201103L
511 constexpr unsigned long long
512 _M_do_to_ullong() const noexcept
516 _GLIBCXX14_CONSTEXPR size_t
517 _M_do_find_first(size_t __not_found) const _GLIBCXX_NOEXCEPT
520 return __builtin_ctzl(_M_w);
525 // find the next "on" bit that follows "prev"
526 _GLIBCXX14_CONSTEXPR size_t
527 _M_do_find_next(size_t __prev, size_t __not_found) const
531 if (__prev >= ((size_t) _GLIBCXX_BITSET_BITS_PER_WORD))
534 _WordT __x = _M_w >> __prev;
536 return __builtin_ctzl(__x) + __prev;
543 * Base class, specialization for no storage (zero-length %bitset).
545 * See documentation for bitset.
548 struct _Base_bitset<0>
550 typedef unsigned long _WordT;
552 _GLIBCXX_CONSTEXPR _Base_bitset() _GLIBCXX_NOEXCEPT
555 #if __cplusplus >= 201103L
556 constexpr _Base_bitset(unsigned long long) noexcept
558 _Base_bitset(unsigned long)
562 static _GLIBCXX_CONSTEXPR size_t
563 _S_whichword(size_t __pos) _GLIBCXX_NOEXCEPT
564 { return __pos / _GLIBCXX_BITSET_BITS_PER_WORD; }
566 static _GLIBCXX_CONSTEXPR size_t
567 _S_whichbyte(size_t __pos) _GLIBCXX_NOEXCEPT
568 { return (__pos % _GLIBCXX_BITSET_BITS_PER_WORD) / __CHAR_BIT__; }
570 static _GLIBCXX_CONSTEXPR size_t
571 _S_whichbit(size_t __pos) _GLIBCXX_NOEXCEPT
572 { return __pos % _GLIBCXX_BITSET_BITS_PER_WORD; }
574 static _GLIBCXX_CONSTEXPR _WordT
575 _S_maskbit(size_t __pos) _GLIBCXX_NOEXCEPT
576 { return (static_cast<_WordT>(1)) << _S_whichbit(__pos); }
578 // This would normally give access to the data. The bounds-checking
579 // in the bitset class will prevent the user from getting this far,
580 // but this must fail if the user calls _Unchecked_set directly.
581 // Let's not penalize zero-length users unless they actually
582 // make an unchecked call; all the memory ugliness is therefore
583 // localized to this single should-never-get-this-far function.
584 __attribute__((__noreturn__))
586 _M_getword(size_t) _GLIBCXX_NOEXCEPT
587 { __throw_out_of_range(__N("_Base_bitset::_M_getword")); }
589 _GLIBCXX_CONSTEXPR _WordT
590 _M_getword(size_t) const _GLIBCXX_NOEXCEPT
593 _GLIBCXX_CONSTEXPR _WordT
594 _M_hiword() const _GLIBCXX_NOEXCEPT
597 _GLIBCXX14_CONSTEXPR void
598 _M_do_and(const _Base_bitset<0>&) _GLIBCXX_NOEXCEPT
601 _GLIBCXX14_CONSTEXPR void
602 _M_do_or(const _Base_bitset<0>&) _GLIBCXX_NOEXCEPT
605 _GLIBCXX14_CONSTEXPR void
606 _M_do_xor(const _Base_bitset<0>&) _GLIBCXX_NOEXCEPT
609 _GLIBCXX14_CONSTEXPR void
610 _M_do_left_shift(size_t) _GLIBCXX_NOEXCEPT
613 _GLIBCXX14_CONSTEXPR void
614 _M_do_right_shift(size_t) _GLIBCXX_NOEXCEPT
617 _GLIBCXX14_CONSTEXPR void
618 _M_do_flip() _GLIBCXX_NOEXCEPT
621 _GLIBCXX14_CONSTEXPR void
622 _M_do_set() _GLIBCXX_NOEXCEPT
625 _GLIBCXX14_CONSTEXPR void
626 _M_do_reset() _GLIBCXX_NOEXCEPT
629 // Are all empty bitsets equal to each other? Are they equal to
630 // themselves? How to compare a thing which has no state? What is
631 // the sound of one zero-length bitset clapping?
632 _GLIBCXX_CONSTEXPR bool
633 _M_is_equal(const _Base_bitset<0>&) const _GLIBCXX_NOEXCEPT
637 _GLIBCXX_CONSTEXPR bool
638 _M_are_all() const _GLIBCXX_NOEXCEPT
641 _GLIBCXX_CONSTEXPR bool
642 _M_is_any() const _GLIBCXX_NOEXCEPT
645 _GLIBCXX_CONSTEXPR size_t
646 _M_do_count() const _GLIBCXX_NOEXCEPT
649 _GLIBCXX_CONSTEXPR unsigned long
650 _M_do_to_ulong() const _GLIBCXX_NOEXCEPT
653 #if __cplusplus >= 201103L
654 constexpr unsigned long long
655 _M_do_to_ullong() const noexcept
659 // Normally "not found" is the size, but that could also be
660 // misinterpreted as an index in this corner case. Oh well.
661 _GLIBCXX_CONSTEXPR size_t
662 _M_do_find_first(size_t) const _GLIBCXX_NOEXCEPT
665 _GLIBCXX_CONSTEXPR size_t
666 _M_do_find_next(size_t, size_t) const _GLIBCXX_NOEXCEPT
671 // Helper class to zero out the unused high-order bits in the highest word.
672 template<size_t _Extrabits>
675 typedef unsigned long _WordT;
677 static _GLIBCXX14_CONSTEXPR void
678 _S_do_sanitize(_WordT& __val) _GLIBCXX_NOEXCEPT
679 { __val &= ~((~static_cast<_WordT>(0)) << _Extrabits); }
685 typedef unsigned long _WordT;
687 static _GLIBCXX14_CONSTEXPR void
688 _S_do_sanitize(_WordT) _GLIBCXX_NOEXCEPT { }
691 #if __cplusplus >= 201103L
692 template<size_t _Nb, bool = (_Nb < _GLIBCXX_BITSET_BITS_PER_ULL)>
695 static constexpr unsigned long long
696 _S_do_sanitize_val(unsigned long long __val)
701 struct _Sanitize_val<_Nb, true>
703 static constexpr unsigned long long
704 _S_do_sanitize_val(unsigned long long __val)
705 { return __val & ~((~static_cast<unsigned long long>(0)) << _Nb); }
711 template<typename _CharT>
712 using __string = std::basic_string<_CharT>;
714 template<typename _CharT>
717 using size_type = size_t;
718 static constexpr size_type npos = size_type(-1);
722 static _GLIBCXX14_CONSTEXPR size_t
723 length(const _CharT* __s) noexcept
731 static constexpr bool
732 eq(_CharT __l, _CharT __r) noexcept
733 { return __l == __r; }
737 } // namespace __bitset
741 * @brief The %bitset class represents a @e fixed-size sequence of bits.
744 * (Note that %bitset does @e not meet the formal requirements of a
745 * <a href="tables.html#65">container</a>. Mainly, it lacks iterators.)
747 * The template argument, @a Nb, may be any non-negative number,
748 * specifying the number of bits (e.g., "0", "12", "1024*1024").
750 * In the general unoptimized case, storage is allocated in word-sized
751 * blocks. Let B be the number of bits in a word, then (Nb+(B-1))/B
752 * words will be used for storage. B - Nb%B bits are unused. (They are
753 * the high-order bits in the highest word.) It is a class invariant
754 * that those unused bits are always zero.
756 * If you think of %bitset as <em>a simple array of bits</em>, be
757 * aware that your mental picture is reversed: a %bitset behaves
758 * the same way as bits in integers do, with the bit at index 0 in
759 * the <em>least significant / right-hand</em> position, and the bit at
760 * index Nb-1 in the <em>most significant / left-hand</em> position.
761 * Thus, unlike other containers, a %bitset's index <em>counts from
762 * right to left</em>, to put it very loosely.
764 * This behavior is preserved when translating to and from strings. For
765 * example, the first line of the following program probably prints
766 * <em>b('a') is 0001100001</em> on a modern ASCII system.
770 * #include <iostream>
773 * using namespace std;
780 * cout << "b('a') is " << b << endl;
784 * string str = s.str();
785 * cout << "index 3 in the string is " << str[3] << " but\n"
786 * << "index 3 in the bitset is " << b[3] << endl;
791 * https://gcc.gnu.org/onlinedocs/libstdc++/manual/ext_containers.html
792 * for a description of extensions.
794 * Most of the actual code isn't contained in %bitset<> itself, but in the
795 * base class _Base_bitset. The base class works with whole words, not with
796 * individual bits. This allows us to specialize _Base_bitset for the
797 * important special case where the %bitset is only a single word.
799 * Extra confusion can result due to the fact that the storage for
800 * _Base_bitset @e is a regular array, and is indexed as such. This is
801 * carefully encapsulated.
805 : private _Base_bitset<_GLIBCXX_BITSET_WORDS(_Nb)>
808 typedef _Base_bitset<_GLIBCXX_BITSET_WORDS(_Nb)> _Base;
809 typedef unsigned long _WordT;
812 template<class _CharT, class _Traits, class _Alloc>
815 _M_check_initial_position(const std::basic_string<_CharT, _Traits, _Alloc>& __s,
816 size_t __position) const
818 if (__position > __s.size())
819 __throw_out_of_range_fmt(__N("bitset::bitset: __position "
820 "(which is %zu) > __s.size() "
822 __position, __s.size());
827 void _M_check(size_t __position, const char *__s) const
829 if (__position >= _Nb)
830 __throw_out_of_range_fmt(__N("%s: __position (which is %zu) "
831 ">= _Nb (which is %zu)"),
832 __s, __position, _Nb);
837 _M_do_sanitize() _GLIBCXX_NOEXCEPT
839 typedef _Sanitize<_Nb % _GLIBCXX_BITSET_BITS_PER_WORD> __sanitize_type;
840 __sanitize_type::_S_do_sanitize(this->_M_hiword());
843 #if __cplusplus >= 201103L
844 friend struct std::hash<bitset>;
849 * This encapsulates the concept of a single bit. An instance of this
850 * class is a proxy for an actual bit; this way the individual bit
851 * operations are done as faster word-size bitwise instructions.
853 * Most users will never need to use this class directly; conversions
854 * to and from bool are automatic and should be transparent. Overloaded
855 * operators help to preserve the illusion.
857 * (On a typical system, this <em>bit %reference</em> is 64
858 * times the size of an actual bit. Ha.)
872 reference(bitset& __b, size_t __pos) _GLIBCXX_NOEXCEPT
874 _M_wp = &__b._M_getword(__pos);
875 _M_bpos = _Base::_S_whichbit(__pos);
878 #if __cplusplus >= 201103L
879 reference(const reference&) = default;
882 #if __cplusplus > 202002L && __cpp_constexpr_dynamic_alloc
885 ~reference() _GLIBCXX_NOEXCEPT
891 operator=(bool __x) _GLIBCXX_NOEXCEPT
894 *_M_wp |= _Base::_S_maskbit(_M_bpos);
896 *_M_wp &= ~_Base::_S_maskbit(_M_bpos);
900 // For b[i] = b[__j];
903 operator=(const reference& __j) _GLIBCXX_NOEXCEPT
905 if ((*(__j._M_wp) & _Base::_S_maskbit(__j._M_bpos)))
906 *_M_wp |= _Base::_S_maskbit(_M_bpos);
908 *_M_wp &= ~_Base::_S_maskbit(_M_bpos);
915 operator~() const _GLIBCXX_NOEXCEPT
916 { return (*(_M_wp) & _Base::_S_maskbit(_M_bpos)) == 0; }
920 operator bool() const _GLIBCXX_NOEXCEPT
921 { return (*(_M_wp) & _Base::_S_maskbit(_M_bpos)) != 0; }
926 flip() _GLIBCXX_NOEXCEPT
928 *_M_wp ^= _Base::_S_maskbit(_M_bpos);
932 friend class reference;
934 // 23.3.5.1 constructors:
935 /// All bits set to zero.
936 _GLIBCXX_CONSTEXPR bitset() _GLIBCXX_NOEXCEPT
939 /// Initial bits bitwise-copied from a single word (others set to zero).
940 #if __cplusplus >= 201103L
941 constexpr bitset(unsigned long long __val) noexcept
942 : _Base(_Sanitize_val<_Nb>::_S_do_sanitize_val(__val)) { }
944 bitset(unsigned long __val)
946 { _M_do_sanitize(); }
951 * Use a subset of a string.
952 * @param __s A string of @a 0 and @a 1 characters.
953 * @param __position Index of the first character in @a __s to use;
955 * @throw std::out_of_range If @a pos is bigger the size of @a __s.
956 * @throw std::invalid_argument If a character appears in the string
957 * which is neither @a 0 nor @a 1.
959 template<class _CharT, class _Traits, class _Alloc>
962 bitset(const std::basic_string<_CharT, _Traits, _Alloc>& __s,
963 size_t __position = 0)
966 _M_check_initial_position(__s, __position);
967 _M_copy_from_string(__s, __position,
968 std::basic_string<_CharT, _Traits, _Alloc>::npos,
969 _CharT('0'), _CharT('1'));
973 * Use a subset of a string.
974 * @param __s A string of @a 0 and @a 1 characters.
975 * @param __position Index of the first character in @a __s to use.
976 * @param __n The number of characters to copy.
977 * @throw std::out_of_range If @a __position is bigger the size
979 * @throw std::invalid_argument If a character appears in the string
980 * which is neither @a 0 nor @a 1.
982 template<class _CharT, class _Traits, class _Alloc>
984 bitset(const std::basic_string<_CharT, _Traits, _Alloc>& __s,
985 size_t __position, size_t __n)
988 _M_check_initial_position(__s, __position);
989 _M_copy_from_string(__s, __position, __n, _CharT('0'), _CharT('1'));
992 // _GLIBCXX_RESOLVE_LIB_DEFECTS
993 // 396. what are characters zero and one.
994 template<class _CharT, class _Traits, class _Alloc>
996 bitset(const std::basic_string<_CharT, _Traits, _Alloc>& __s,
997 size_t __position, size_t __n,
998 _CharT __zero, _CharT __one = _CharT('1'))
1001 _M_check_initial_position(__s, __position);
1002 _M_copy_from_string(__s, __position, __n, __zero, __one);
1006 #if __cplusplus >= 201103L
1008 * Construct from a character %array.
1009 * @param __str An %array of characters @a zero and @a one.
1010 * @param __n The number of characters to use.
1011 * @param __zero The character corresponding to the value 0.
1012 * @param __one The character corresponding to the value 1.
1013 * @throw std::invalid_argument If a character appears in the string
1014 * which is neither @a __zero nor @a __one.
1016 template<typename _CharT>
1017 [[__gnu__::__nonnull__]]
1018 _GLIBCXX23_CONSTEXPR
1020 bitset(const _CharT* __str,
1021 typename __bitset::__string<_CharT>::size_type __n
1022 = __bitset::__string<_CharT>::npos,
1023 _CharT __zero = _CharT('0'), _CharT __one = _CharT('1'))
1028 __throw_logic_error(__N("bitset::bitset(const _CharT*, ...)"));
1030 using _Traits = typename __bitset::__string<_CharT>::traits_type;
1032 if (__n == __bitset::__string<_CharT>::npos)
1033 __n = _Traits::length(__str);
1034 _M_copy_from_ptr<_CharT, _Traits>(__str, __n, 0, __n, __zero, __one);
1038 // 23.3.5.2 bitset operations:
1041 * Operations on bitsets.
1042 * @param __rhs A same-sized bitset.
1044 * These should be self-explanatory.
1046 _GLIBCXX23_CONSTEXPR
1048 operator&=(const bitset<_Nb>& __rhs) _GLIBCXX_NOEXCEPT
1050 this->_M_do_and(__rhs);
1054 _GLIBCXX23_CONSTEXPR
1056 operator|=(const bitset<_Nb>& __rhs) _GLIBCXX_NOEXCEPT
1058 this->_M_do_or(__rhs);
1062 _GLIBCXX23_CONSTEXPR
1064 operator^=(const bitset<_Nb>& __rhs) _GLIBCXX_NOEXCEPT
1066 this->_M_do_xor(__rhs);
1073 * Operations on bitsets.
1074 * @param __position The number of places to shift.
1076 * These should be self-explanatory.
1078 _GLIBCXX23_CONSTEXPR
1080 operator<<=(size_t __position) _GLIBCXX_NOEXCEPT
1082 if (__builtin_expect(__position < _Nb, 1))
1084 this->_M_do_left_shift(__position);
1085 this->_M_do_sanitize();
1088 this->_M_do_reset();
1092 _GLIBCXX23_CONSTEXPR
1094 operator>>=(size_t __position) _GLIBCXX_NOEXCEPT
1096 if (__builtin_expect(__position < _Nb, 1))
1097 this->_M_do_right_shift(__position);
1099 this->_M_do_reset();
1106 * These versions of single-bit set, reset, flip, and test are
1107 * extensions from the SGI version. They do no range checking.
1108 * @ingroup SGIextensions
1110 _GLIBCXX23_CONSTEXPR
1112 _Unchecked_set(size_t __pos) _GLIBCXX_NOEXCEPT
1114 this->_M_getword(__pos) |= _Base::_S_maskbit(__pos);
1118 _GLIBCXX23_CONSTEXPR
1120 _Unchecked_set(size_t __pos, int __val) _GLIBCXX_NOEXCEPT
1123 this->_M_getword(__pos) |= _Base::_S_maskbit(__pos);
1125 this->_M_getword(__pos) &= ~_Base::_S_maskbit(__pos);
1129 _GLIBCXX23_CONSTEXPR
1131 _Unchecked_reset(size_t __pos) _GLIBCXX_NOEXCEPT
1133 this->_M_getword(__pos) &= ~_Base::_S_maskbit(__pos);
1137 _GLIBCXX23_CONSTEXPR
1139 _Unchecked_flip(size_t __pos) _GLIBCXX_NOEXCEPT
1141 this->_M_getword(__pos) ^= _Base::_S_maskbit(__pos);
1145 _GLIBCXX_CONSTEXPR bool
1146 _Unchecked_test(size_t __pos) const _GLIBCXX_NOEXCEPT
1147 { return ((this->_M_getword(__pos) & _Base::_S_maskbit(__pos))
1148 != static_cast<_WordT>(0)); }
1151 // Set, reset, and flip.
1153 * @brief Sets every bit to true.
1155 _GLIBCXX23_CONSTEXPR
1157 set() _GLIBCXX_NOEXCEPT
1160 this->_M_do_sanitize();
1165 * @brief Sets a given bit to a particular value.
1166 * @param __position The index of the bit.
1167 * @param __val Either true or false, defaults to true.
1168 * @throw std::out_of_range If @a pos is bigger the size of the %set.
1170 _GLIBCXX23_CONSTEXPR
1172 set(size_t __position, bool __val = true)
1174 this->_M_check(__position, __N("bitset::set"));
1175 return _Unchecked_set(__position, __val);
1179 * @brief Sets every bit to false.
1181 _GLIBCXX23_CONSTEXPR
1183 reset() _GLIBCXX_NOEXCEPT
1185 this->_M_do_reset();
1190 * @brief Sets a given bit to false.
1191 * @param __position The index of the bit.
1192 * @throw std::out_of_range If @a pos is bigger the size of the %set.
1194 * Same as writing @c set(pos,false).
1196 _GLIBCXX23_CONSTEXPR
1198 reset(size_t __position)
1200 this->_M_check(__position, __N("bitset::reset"));
1201 return _Unchecked_reset(__position);
1205 * @brief Toggles every bit to its opposite value.
1207 _GLIBCXX23_CONSTEXPR
1209 flip() _GLIBCXX_NOEXCEPT
1212 this->_M_do_sanitize();
1217 * @brief Toggles a given bit to its opposite value.
1218 * @param __position The index of the bit.
1219 * @throw std::out_of_range If @a pos is bigger the size of the %set.
1221 _GLIBCXX23_CONSTEXPR
1223 flip(size_t __position)
1225 this->_M_check(__position, __N("bitset::flip"));
1226 return _Unchecked_flip(__position);
1229 /// See the no-argument flip().
1230 _GLIBCXX23_CONSTEXPR
1232 operator~() const _GLIBCXX_NOEXCEPT
1233 { return bitset<_Nb>(*this).flip(); }
1237 * @brief Array-indexing support.
1238 * @param __position Index into the %bitset.
1239 * @return A bool for a <em>const %bitset</em>. For non-const
1240 * bitsets, an instance of the reference proxy class.
1241 * @note These operators do no range checking and throw no exceptions,
1242 * as required by DR 11 to the standard.
1244 * _GLIBCXX_RESOLVE_LIB_DEFECTS Note that this implementation already
1245 * resolves DR 11 (items 1 and 2), but does not do the range-checking
1246 * required by that DR's resolution. -pme
1247 * The DR has since been changed: range-checking is a precondition
1248 * (users' responsibility), and these functions must not throw. -pme
1250 _GLIBCXX23_CONSTEXPR
1252 operator[](size_t __position)
1253 { return reference(*this, __position); }
1255 _GLIBCXX_CONSTEXPR bool
1256 operator[](size_t __position) const
1257 { return _Unchecked_test(__position); }
1261 * @brief Returns a numerical interpretation of the %bitset.
1262 * @return The integral equivalent of the bits.
1263 * @throw std::overflow_error If there are too many bits to be
1264 * represented in an @c unsigned @c long.
1266 _GLIBCXX23_CONSTEXPR
1269 { return this->_M_do_to_ulong(); }
1271 #if __cplusplus >= 201103L
1272 _GLIBCXX23_CONSTEXPR
1275 { return this->_M_do_to_ullong(); }
1280 * @brief Returns a character interpretation of the %bitset.
1281 * @return The string equivalent of the bits.
1283 * Note the ordering of the bits: decreasing character positions
1284 * correspond to increasing bit positions (see the main class notes for
1287 template<class _CharT, class _Traits, class _Alloc>
1288 _GLIBCXX23_CONSTEXPR
1289 std::basic_string<_CharT, _Traits, _Alloc>
1292 std::basic_string<_CharT, _Traits, _Alloc> __result;
1293 _M_copy_to_string(__result, _CharT('0'), _CharT('1'));
1297 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1298 // 396. what are characters zero and one.
1299 template<class _CharT, class _Traits, class _Alloc>
1300 _GLIBCXX23_CONSTEXPR
1301 std::basic_string<_CharT, _Traits, _Alloc>
1302 to_string(_CharT __zero, _CharT __one = _CharT('1')) const
1304 std::basic_string<_CharT, _Traits, _Alloc> __result;
1305 _M_copy_to_string(__result, __zero, __one);
1309 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1310 // 434. bitset::to_string() hard to use.
1311 template<class _CharT, class _Traits>
1312 _GLIBCXX23_CONSTEXPR
1313 std::basic_string<_CharT, _Traits, std::allocator<_CharT> >
1315 { return to_string<_CharT, _Traits, std::allocator<_CharT> >(); }
1317 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1318 // 853. to_string needs updating with zero and one.
1319 template<class _CharT, class _Traits>
1320 _GLIBCXX23_CONSTEXPR
1321 std::basic_string<_CharT, _Traits, std::allocator<_CharT> >
1322 to_string(_CharT __zero, _CharT __one = _CharT('1')) const
1323 { return to_string<_CharT, _Traits,
1324 std::allocator<_CharT> >(__zero, __one); }
1326 template<class _CharT>
1327 _GLIBCXX23_CONSTEXPR
1328 std::basic_string<_CharT, std::char_traits<_CharT>,
1329 std::allocator<_CharT> >
1332 return to_string<_CharT, std::char_traits<_CharT>,
1333 std::allocator<_CharT> >();
1336 template<class _CharT>
1337 _GLIBCXX23_CONSTEXPR
1338 std::basic_string<_CharT, std::char_traits<_CharT>,
1339 std::allocator<_CharT> >
1340 to_string(_CharT __zero, _CharT __one = _CharT('1')) const
1342 return to_string<_CharT, std::char_traits<_CharT>,
1343 std::allocator<_CharT> >(__zero, __one);
1346 _GLIBCXX23_CONSTEXPR
1347 std::basic_string<char, std::char_traits<char>, std::allocator<char> >
1350 return to_string<char, std::char_traits<char>,
1351 std::allocator<char> >();
1354 _GLIBCXX23_CONSTEXPR
1355 std::basic_string<char, std::char_traits<char>, std::allocator<char> >
1356 to_string(char __zero, char __one = '1') const
1358 return to_string<char, std::char_traits<char>,
1359 std::allocator<char> >(__zero, __one);
1363 /// Returns the number of bits which are set.
1364 _GLIBCXX23_CONSTEXPR
1366 count() const _GLIBCXX_NOEXCEPT
1367 { return this->_M_do_count(); }
1369 /// Returns the total number of bits.
1370 _GLIBCXX_CONSTEXPR size_t
1371 size() const _GLIBCXX_NOEXCEPT
1375 /// These comparisons for equality/inequality are, well, @e bitwise.
1376 _GLIBCXX23_CONSTEXPR
1378 operator==(const bitset<_Nb>& __rhs) const _GLIBCXX_NOEXCEPT
1379 { return this->_M_is_equal(__rhs); }
1381 #if __cpp_impl_three_way_comparison < 201907L
1382 _GLIBCXX23_CONSTEXPR
1384 operator!=(const bitset<_Nb>& __rhs) const _GLIBCXX_NOEXCEPT
1385 { return !this->_M_is_equal(__rhs); }
1390 * @brief Tests the value of a bit.
1391 * @param __position The index of a bit.
1392 * @return The value at @a pos.
1393 * @throw std::out_of_range If @a pos is bigger the size of the %set.
1395 _GLIBCXX23_CONSTEXPR
1397 test(size_t __position) const
1399 this->_M_check(__position, __N("bitset::test"));
1400 return _Unchecked_test(__position);
1403 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1404 // DR 693. std::bitset::all() missing.
1406 * @brief Tests whether all the bits are on.
1407 * @return True if all the bits are set.
1409 _GLIBCXX23_CONSTEXPR
1411 all() const _GLIBCXX_NOEXCEPT
1412 { return this->template _M_are_all<_Nb>(); }
1415 * @brief Tests whether any of the bits are on.
1416 * @return True if at least one bit is set.
1418 _GLIBCXX23_CONSTEXPR
1420 any() const _GLIBCXX_NOEXCEPT
1421 { return this->_M_is_any(); }
1424 * @brief Tests whether any of the bits are on.
1425 * @return True if none of the bits are set.
1427 _GLIBCXX23_CONSTEXPR
1429 none() const _GLIBCXX_NOEXCEPT
1430 { return !this->_M_is_any(); }
1433 /// Self-explanatory.
1434 _GLIBCXX23_CONSTEXPR
1436 operator<<(size_t __position) const _GLIBCXX_NOEXCEPT
1437 { return bitset<_Nb>(*this) <<= __position; }
1439 _GLIBCXX23_CONSTEXPR
1441 operator>>(size_t __position) const _GLIBCXX_NOEXCEPT
1442 { return bitset<_Nb>(*this) >>= __position; }
1446 * @brief Finds the index of the first "on" bit.
1447 * @return The index of the first bit set, or size() if not found.
1448 * @ingroup SGIextensions
1451 _GLIBCXX23_CONSTEXPR
1453 _Find_first() const _GLIBCXX_NOEXCEPT
1454 { return this->_M_do_find_first(_Nb); }
1457 * @brief Finds the index of the next "on" bit after prev.
1458 * @return The index of the next bit set, or size() if not found.
1459 * @param __prev Where to start searching.
1460 * @ingroup SGIextensions
1463 _GLIBCXX23_CONSTEXPR
1465 _Find_next(size_t __prev) const _GLIBCXX_NOEXCEPT
1466 { return this->_M_do_find_next(__prev, _Nb); }
1469 // Helper functions for string operations.
1470 template<class _CharT, class _Traits>
1471 _GLIBCXX23_CONSTEXPR
1473 _M_copy_from_ptr(const _CharT*, size_t, size_t, size_t,
1477 template<class _CharT, class _Traits, class _Alloc>
1478 _GLIBCXX23_CONSTEXPR
1480 _M_copy_from_string(const std::basic_string<_CharT,
1481 _Traits, _Alloc>& __s, size_t __pos, size_t __n,
1482 _CharT __zero, _CharT __one)
1483 { _M_copy_from_ptr<_CharT, _Traits>(__s.data(), __s.size(), __pos, __n,
1486 template<class _CharT, class _Traits, class _Alloc>
1487 _GLIBCXX23_CONSTEXPR
1489 _M_copy_to_string(std::basic_string<_CharT, _Traits, _Alloc>&,
1490 _CharT, _CharT) const;
1492 template<class _CharT, class _Traits, size_t _Nb2>
1493 friend std::basic_istream<_CharT, _Traits>&
1494 operator>>(std::basic_istream<_CharT, _Traits>&, bitset<_Nb2>&);
1496 template <class _CharT, class _Traits, size_t _Nb2>
1497 friend std::basic_ostream<_CharT, _Traits>&
1498 operator<<(std::basic_ostream<_CharT, _Traits>&, const bitset<_Nb2>&);
1502 // Definitions of non-inline member functions.
1503 template<size_t _Nb>
1504 template<class _CharT, class _Traits>
1505 _GLIBCXX23_CONSTEXPR
1508 _M_copy_from_ptr(const _CharT* __s, size_t __len,
1509 size_t __pos, size_t __n, _CharT __zero, _CharT __one)
1512 const size_t __nbits = std::min(_Nb, std::min(__n, size_t(__len - __pos)));
1513 for (size_t __i = __nbits; __i > 0; --__i)
1515 const _CharT __c = __s[__pos + __nbits - __i];
1516 if (_Traits::eq(__c, __zero))
1518 else if (_Traits::eq(__c, __one))
1519 _Unchecked_set(__i - 1);
1521 __throw_invalid_argument(__N("bitset::_M_copy_from_ptr"));
1526 template<size_t _Nb>
1527 template<class _CharT, class _Traits, class _Alloc>
1528 _GLIBCXX23_CONSTEXPR
1531 _M_copy_to_string(std::basic_string<_CharT, _Traits, _Alloc>& __s,
1532 _CharT __zero, _CharT __one) const
1534 __s.assign(_Nb, __zero);
1535 size_t __n = this->_Find_first();
1538 __s[_Nb - __n - 1] = __one;
1539 __n = _Find_next(__n);
1544 // 23.3.5.3 bitset operations:
1547 * @brief Global bitwise operations on bitsets.
1548 * @param __x A bitset.
1549 * @param __y A bitset of the same size as @a __x.
1550 * @return A new bitset.
1552 * These should be self-explanatory.
1554 template<size_t _Nb>
1555 _GLIBCXX23_CONSTEXPR
1557 operator&(const bitset<_Nb>& __x, const bitset<_Nb>& __y) _GLIBCXX_NOEXCEPT
1559 bitset<_Nb> __result(__x);
1564 template<size_t _Nb>
1565 _GLIBCXX23_CONSTEXPR
1567 operator|(const bitset<_Nb>& __x, const bitset<_Nb>& __y) _GLIBCXX_NOEXCEPT
1569 bitset<_Nb> __result(__x);
1574 template <size_t _Nb>
1575 _GLIBCXX23_CONSTEXPR
1577 operator^(const bitset<_Nb>& __x, const bitset<_Nb>& __y) _GLIBCXX_NOEXCEPT
1579 bitset<_Nb> __result(__x);
1588 * @brief Global I/O operators for bitsets.
1590 * Direct I/O between streams and bitsets is supported. Output is
1591 * straightforward. Input will skip whitespace, only accept @a 0 and @a 1
1592 * characters, and will only extract as many digits as the %bitset will
1595 template<class _CharT, class _Traits, size_t _Nb>
1596 std::basic_istream<_CharT, _Traits>&
1597 operator>>(std::basic_istream<_CharT, _Traits>& __is, bitset<_Nb>& __x)
1599 typedef typename _Traits::char_type char_type;
1600 typedef std::basic_istream<_CharT, _Traits> __istream_type;
1601 typedef typename __istream_type::ios_base __ios_base;
1605 static _GLIBCXX_CONSTEXPR bool _S_use_alloca() { return _Nb <= 256; }
1607 explicit _Buffer(_CharT* __p) : _M_ptr(__p) { }
1611 if _GLIBCXX17_CONSTEXPR (!_S_use_alloca())
1615 _CharT* const _M_ptr;
1618 if _GLIBCXX17_CONSTEXPR (_Buffer::_S_use_alloca())
1619 __ptr = (_CharT*)__builtin_alloca(_Nb);
1621 __ptr = new _CharT[_Nb];
1622 const _Buffer __buf(__ptr);
1624 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1625 // 303. Bitset input operator underspecified
1626 const char_type __zero = __is.widen('0');
1627 const char_type __one = __is.widen('1');
1629 typename __ios_base::iostate __state = __ios_base::goodbit;
1630 typename __istream_type::sentry __sentry(__is);
1635 for (size_t __i = _Nb; __i > 0; --__i)
1637 static typename _Traits::int_type __eof = _Traits::eof();
1639 typename _Traits::int_type __c1 = __is.rdbuf()->sbumpc();
1640 if (_Traits::eq_int_type(__c1, __eof))
1642 __state |= __ios_base::eofbit;
1647 const char_type __c2 = _Traits::to_char_type(__c1);
1648 if (_Traits::eq(__c2, __zero))
1650 else if (_Traits::eq(__c2, __one))
1653 eq_int_type(__is.rdbuf()->sputbackc(__c2),
1656 __state |= __ios_base::failbit;
1662 __catch(__cxxabiv1::__forced_unwind&)
1664 __is._M_setstate(__ios_base::badbit);
1665 __throw_exception_again;
1668 { __is._M_setstate(__ios_base::badbit); }
1671 if _GLIBCXX17_CONSTEXPR (_Nb)
1673 if (size_t __len = __ptr - __buf._M_ptr)
1674 __x.template _M_copy_from_ptr<_CharT, _Traits>(__buf._M_ptr, __len,
1678 __state |= __ios_base::failbit;
1681 __is.setstate(__state);
1685 template <class _CharT, class _Traits, size_t _Nb>
1686 std::basic_ostream<_CharT, _Traits>&
1687 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1688 const bitset<_Nb>& __x)
1690 std::basic_string<_CharT, _Traits> __tmp;
1692 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1693 // 396. what are characters zero and one.
1694 const ctype<_CharT>& __ct = use_facet<ctype<_CharT> >(__os.getloc());
1695 __x._M_copy_to_string(__tmp, __ct.widen('0'), __ct.widen('1'));
1696 return __os << __tmp;
1701 _GLIBCXX_END_NAMESPACE_CONTAINER
1704 #undef _GLIBCXX_BITSET_WORDS
1705 #undef _GLIBCXX_BITSET_BITS_PER_WORD
1706 #undef _GLIBCXX_BITSET_BITS_PER_ULL
1708 #if __cplusplus >= 201103L
1710 namespace std _GLIBCXX_VISIBILITY(default)
1712 _GLIBCXX_BEGIN_NAMESPACE_VERSION
1715 /// std::hash specialization for bitset.
1716 template<size_t _Nb>
1717 struct hash<_GLIBCXX_STD_C::bitset<_Nb>>
1718 : public __hash_base<size_t, _GLIBCXX_STD_C::bitset<_Nb>>
1721 operator()(const _GLIBCXX_STD_C::bitset<_Nb>& __b) const noexcept
1723 const size_t __clength = (_Nb + __CHAR_BIT__ - 1) / __CHAR_BIT__;
1724 return std::_Hash_impl::hash(__b._M_getdata(), __clength);
1729 struct hash<_GLIBCXX_STD_C::bitset<0>>
1730 : public __hash_base<size_t, _GLIBCXX_STD_C::bitset<0>>
1733 operator()(const _GLIBCXX_STD_C::bitset<0>&) const noexcept
1737 _GLIBCXX_END_NAMESPACE_VERSION
1742 #if defined _GLIBCXX_DEBUG && _GLIBCXX_HOSTED
1743 # include <debug/bitset>
1746 #endif /* _GLIBCXX_BITSET */