libstdc++
bitset
Go to the documentation of this file.
1// <bitset> -*- C++ -*-
2
3// Copyright (C) 2001-2026 Free Software Foundation, Inc.
4//
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)
9// any later version.
10
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.
15
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.
19
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/>.
24
25/*
26 * Copyright (c) 1998
27 * Silicon Graphics Computer Systems, Inc.
28 *
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.
36 */
37
38/** @file include/bitset
39 * This is a Standard C++ Library header.
40 */
41
42#ifndef _GLIBCXX_BITSET
43#define _GLIBCXX_BITSET 1
44
45#ifdef _GLIBCXX_SYSHDR
46#pragma GCC system_header
47#endif
48
49#include <bits/stdexcept_throw.h> // For invalid_argument, out_of_range,
50 // overflow_error
51#include <bits/stl_algobase.h> // For std::fill
52
53#if _GLIBCXX_HOSTED
54# include <string>
55# include <iosfwd>
56# include <bits/cxxabi_forced.h>
57#endif
58
59#if __cplusplus >= 201103L
60# include <bits/functional_hash.h>
61#endif
62
63#define __glibcxx_want_constexpr_bitset
64#define __glibcxx_want_bitset // ...construct from string_view
65#define __glibcxx_want_hardened_bitset
66#include <bits/version.h>
67
68#ifdef __cpp_lib_bitset // ...construct from string_view
69# include <string_view>
70#endif
71
72#define _GLIBCXX_BITSET_BITS_PER_WORD (__CHAR_BIT__ * __SIZEOF_LONG__)
73#define _GLIBCXX_BITSET_WORDS(__n) \
74 ((__n) / _GLIBCXX_BITSET_BITS_PER_WORD + \
75 ((__n) % _GLIBCXX_BITSET_BITS_PER_WORD == 0 ? 0 : 1))
76
77#define _GLIBCXX_BITSET_BITS_PER_ULL (__CHAR_BIT__ * __SIZEOF_LONG_LONG__)
78
79namespace std _GLIBCXX_VISIBILITY(default)
80{
81_GLIBCXX_BEGIN_NAMESPACE_CONTAINER
82
83 /**
84 * Base class, general case. It is a class invariant that _Nw will be
85 * nonnegative.
86 *
87 * See documentation for bitset.
88 */
89 template<size_t _Nw>
90 struct _Base_bitset
91 {
92 typedef unsigned long _WordT;
93
94 /// 0 is the least significant word.
95 _WordT _M_w[_Nw];
96
97 _GLIBCXX_CONSTEXPR _Base_bitset() _GLIBCXX_NOEXCEPT
98 : _M_w() { }
99
100#if __cplusplus >= 201103L
101 constexpr _Base_bitset(unsigned long long __val) noexcept
102 : _M_w{ _WordT(__val)
103#if __SIZEOF_LONG_LONG__ > __SIZEOF_LONG__
104 , _WordT(__val >> _GLIBCXX_BITSET_BITS_PER_WORD)
105#endif
106 } { }
107#else
108 _Base_bitset(unsigned long __val)
109 : _M_w()
110 { _M_w[0] = __val; }
111#endif
112
113 static _GLIBCXX_CONSTEXPR size_t
114 _S_whichword(size_t __pos) _GLIBCXX_NOEXCEPT
115 { return __pos / _GLIBCXX_BITSET_BITS_PER_WORD; }
116
117 static _GLIBCXX_CONSTEXPR size_t
118 _S_whichbyte(size_t __pos) _GLIBCXX_NOEXCEPT
119 { return (__pos % _GLIBCXX_BITSET_BITS_PER_WORD) / __CHAR_BIT__; }
120
121 static _GLIBCXX_CONSTEXPR size_t
122 _S_whichbit(size_t __pos) _GLIBCXX_NOEXCEPT
123 { return __pos % _GLIBCXX_BITSET_BITS_PER_WORD; }
124
125 static _GLIBCXX_CONSTEXPR _WordT
126 _S_maskbit(size_t __pos) _GLIBCXX_NOEXCEPT
127 { return (static_cast<_WordT>(1)) << _S_whichbit(__pos); }
128
129 _GLIBCXX14_CONSTEXPR _WordT&
130 _M_getword(size_t __pos) _GLIBCXX_NOEXCEPT
131 { return _M_w[_S_whichword(__pos)]; }
132
133 _GLIBCXX_CONSTEXPR _WordT
134 _M_getword(size_t __pos) const _GLIBCXX_NOEXCEPT
135 { return _M_w[_S_whichword(__pos)]; }
136
137#if __cplusplus >= 201103L
138 constexpr const _WordT*
139 _M_getdata() const noexcept
140 { return _M_w; }
141#endif
142
143 _GLIBCXX23_CONSTEXPR _WordT&
144 _M_hiword() _GLIBCXX_NOEXCEPT
145 { return _M_w[_Nw - 1]; }
146
147 _GLIBCXX_CONSTEXPR _WordT
148 _M_hiword() const _GLIBCXX_NOEXCEPT
149 { return _M_w[_Nw - 1]; }
150
151 _GLIBCXX23_CONSTEXPR void
152 _M_do_and(const _Base_bitset<_Nw>& __x) _GLIBCXX_NOEXCEPT
153 {
154 for (size_t __i = 0; __i < _Nw; __i++)
155 _M_w[__i] &= __x._M_w[__i];
156 }
157
158 _GLIBCXX14_CONSTEXPR void
159 _M_do_or(const _Base_bitset<_Nw>& __x) _GLIBCXX_NOEXCEPT
160 {
161 for (size_t __i = 0; __i < _Nw; __i++)
162 _M_w[__i] |= __x._M_w[__i];
163 }
164
165 _GLIBCXX14_CONSTEXPR void
166 _M_do_xor(const _Base_bitset<_Nw>& __x) _GLIBCXX_NOEXCEPT
167 {
168 for (size_t __i = 0; __i < _Nw; __i++)
169 _M_w[__i] ^= __x._M_w[__i];
170 }
171
172 _GLIBCXX14_CONSTEXPR void
173 _M_do_left_shift(size_t __shift) _GLIBCXX_NOEXCEPT;
174
175 _GLIBCXX14_CONSTEXPR void
176 _M_do_right_shift(size_t __shift) _GLIBCXX_NOEXCEPT;
177
178 _GLIBCXX14_CONSTEXPR void
179 _M_do_flip() _GLIBCXX_NOEXCEPT
180 {
181 for (size_t __i = 0; __i < _Nw; __i++)
182 _M_w[__i] = ~_M_w[__i];
183 }
184
185 _GLIBCXX14_CONSTEXPR void
186 _M_do_set() _GLIBCXX_NOEXCEPT
187 {
188#if __cplusplus >= 201402L
189 if (__builtin_is_constant_evaluated())
190 {
191 for (_WordT& __w : _M_w)
192 __w = ~static_cast<_WordT>(0);;
193 return;
194 }
195#endif
196 __builtin_memset(_M_w, 0xFF, _Nw * sizeof(_WordT));
197 }
198
199 _GLIBCXX14_CONSTEXPR void
200 _M_do_reset() _GLIBCXX_NOEXCEPT
201 {
202#if __cplusplus >= 201402L
203 if (__builtin_is_constant_evaluated())
204 {
205 for (_WordT& __w : _M_w)
206 __w = 0;
207 return;
208 }
209#endif
210 __builtin_memset(_M_w, 0, _Nw * sizeof(_WordT));
211 }
212
213 _GLIBCXX14_CONSTEXPR bool
214 _M_is_equal(const _Base_bitset<_Nw>& __x) const _GLIBCXX_NOEXCEPT
215 {
216#if __cplusplus >= 201402L
217 if (__builtin_is_constant_evaluated())
218 {
219 for (size_t __i = 0; __i < _Nw; ++__i)
220 if (_M_w[__i] != __x._M_w[__i])
221 return false;
222 return true;
223 }
224#endif
225 return !__builtin_memcmp(_M_w, __x._M_w, _Nw * sizeof(_WordT));
226 }
227
228 template<size_t _Nb>
229 _GLIBCXX14_CONSTEXPR bool
230 _M_are_all() const _GLIBCXX_NOEXCEPT
231 {
232 for (size_t __i = 0; __i < _Nw - 1; __i++)
233 if (_M_w[__i] != ~static_cast<_WordT>(0))
234 return false;
235 return _M_hiword() == (~static_cast<_WordT>(0)
236 >> (_Nw * _GLIBCXX_BITSET_BITS_PER_WORD
237 - _Nb));
238 }
239
240 _GLIBCXX14_CONSTEXPR bool
241 _M_is_any() const _GLIBCXX_NOEXCEPT
242 {
243 for (size_t __i = 0; __i < _Nw; __i++)
244 if (_M_w[__i] != static_cast<_WordT>(0))
245 return true;
246 return false;
247 }
248
249 _GLIBCXX14_CONSTEXPR size_t
250 _M_do_count() const _GLIBCXX_NOEXCEPT
251 {
252 size_t __result = 0;
253 for (size_t __i = 0; __i < _Nw; __i++)
254 __result += __builtin_popcountl(_M_w[__i]);
255 return __result;
256 }
257
258 _GLIBCXX14_CONSTEXPR unsigned long
259 _M_do_to_ulong() const;
260
261#if __cplusplus >= 201103L
262 _GLIBCXX14_CONSTEXPR unsigned long long
263 _M_do_to_ullong() const;
264#endif
265
266 // find first "on" bit
267 _GLIBCXX14_CONSTEXPR size_t
268 _M_do_find_first(size_t) const _GLIBCXX_NOEXCEPT;
269
270 // find the next "on" bit that follows "prev"
271 _GLIBCXX14_CONSTEXPR size_t
272 _M_do_find_next(size_t, size_t) const _GLIBCXX_NOEXCEPT;
273 };
274
275 // Definitions of non-inline functions from _Base_bitset.
276 template<size_t _Nw>
277 _GLIBCXX14_CONSTEXPR void
278 _Base_bitset<_Nw>::_M_do_left_shift(size_t __shift) _GLIBCXX_NOEXCEPT
279 {
280 if (__builtin_expect(__shift != 0, 1))
281 {
282 const size_t __wshift = __shift / _GLIBCXX_BITSET_BITS_PER_WORD;
283 const size_t __offset = __shift % _GLIBCXX_BITSET_BITS_PER_WORD;
284
285 if (__offset == 0)
286 for (size_t __n = _Nw - 1; __n >= __wshift; --__n)
287 _M_w[__n] = _M_w[__n - __wshift];
288 else
289 {
290 const size_t __sub_offset = (_GLIBCXX_BITSET_BITS_PER_WORD
291 - __offset);
292 for (size_t __n = _Nw - 1; __n > __wshift; --__n)
293 _M_w[__n] = ((_M_w[__n - __wshift] << __offset)
294 | (_M_w[__n - __wshift - 1] >> __sub_offset));
295 _M_w[__wshift] = _M_w[0] << __offset;
296 }
297
298 std::fill(_M_w + 0, _M_w + __wshift, static_cast<_WordT>(0));
299 }
300 }
301
302 template<size_t _Nw>
303 _GLIBCXX14_CONSTEXPR void
304 _Base_bitset<_Nw>::_M_do_right_shift(size_t __shift) _GLIBCXX_NOEXCEPT
305 {
306 if (__builtin_expect(__shift != 0, 1))
307 {
308 const size_t __wshift = __shift / _GLIBCXX_BITSET_BITS_PER_WORD;
309 const size_t __offset = __shift % _GLIBCXX_BITSET_BITS_PER_WORD;
310 const size_t __limit = _Nw - __wshift - 1;
311
312 if (__offset == 0)
313 for (size_t __n = 0; __n <= __limit; ++__n)
314 _M_w[__n] = _M_w[__n + __wshift];
315 else
316 {
317 const size_t __sub_offset = (_GLIBCXX_BITSET_BITS_PER_WORD
318 - __offset);
319 for (size_t __n = 0; __n < __limit; ++__n)
320 _M_w[__n] = ((_M_w[__n + __wshift] >> __offset)
321 | (_M_w[__n + __wshift + 1] << __sub_offset));
322 _M_w[__limit] = _M_w[_Nw-1] >> __offset;
323 }
324
325 std::fill(_M_w + __limit + 1, _M_w + _Nw, static_cast<_WordT>(0));
326 }
327 }
328
329 template<size_t _Nw>
330 _GLIBCXX14_CONSTEXPR unsigned long
331 _Base_bitset<_Nw>::_M_do_to_ulong() const
332 {
333 for (size_t __i = 1; __i < _Nw; ++__i)
334 if (_M_w[__i])
335 __throw_overflow_error(__N("_Base_bitset::_M_do_to_ulong"));
336 return _M_w[0];
337 }
338
339#if __cplusplus >= 201103L
340 template<size_t _Nw>
341 _GLIBCXX14_CONSTEXPR unsigned long long
342 _Base_bitset<_Nw>::_M_do_to_ullong() const
343 {
344#if __SIZEOF_LONG_LONG__ == __SIZEOF_LONG__
345 return _M_do_to_ulong();
346#else
347 for (size_t __i = 2; __i < _Nw; ++__i)
348 if (_M_w[__i])
349 __throw_overflow_error(__N("_Base_bitset::_M_do_to_ullong"));
350
351 return _M_w[0] + (static_cast<unsigned long long>(_M_w[1])
352 << _GLIBCXX_BITSET_BITS_PER_WORD);
353#endif
354 }
355#endif // C++11
356
357 template<size_t _Nw>
358 _GLIBCXX14_CONSTEXPR size_t
360 _M_do_find_first(size_t __not_found) const _GLIBCXX_NOEXCEPT
361 {
362 for (size_t __i = 0; __i < _Nw; __i++)
363 {
364 _WordT __thisword = _M_w[__i];
365 if (__thisword != static_cast<_WordT>(0))
366 return (__i * _GLIBCXX_BITSET_BITS_PER_WORD
367 + __builtin_ctzl(__thisword));
368 }
369 // not found, so return an indication of failure.
370 return __not_found;
371 }
372
373 template<size_t _Nw>
374 _GLIBCXX14_CONSTEXPR size_t
376 _M_do_find_next(size_t __prev, size_t __not_found) const _GLIBCXX_NOEXCEPT
377 {
378 // make bound inclusive
379 ++__prev;
380
381 // check out of bounds
382 if (__prev >= _Nw * _GLIBCXX_BITSET_BITS_PER_WORD)
383 return __not_found;
384
385 // search first word
386 size_t __i = _S_whichword(__prev);
387 _WordT __thisword = _M_w[__i];
388
389 // mask off bits below bound
390 __thisword &= (~static_cast<_WordT>(0)) << _S_whichbit(__prev);
391
392 if (__thisword != static_cast<_WordT>(0))
393 return (__i * _GLIBCXX_BITSET_BITS_PER_WORD
394 + __builtin_ctzl(__thisword));
395
396 // check subsequent words
397 __i++;
398 for (; __i < _Nw; __i++)
399 {
400 __thisword = _M_w[__i];
401 if (__thisword != static_cast<_WordT>(0))
402 return (__i * _GLIBCXX_BITSET_BITS_PER_WORD
403 + __builtin_ctzl(__thisword));
404 }
405 // not found, so return an indication of failure.
406 return __not_found;
407 } // end _M_do_find_next
408
409 /**
410 * Base class, specialization for a single word.
411 *
412 * See documentation for bitset.
413 */
414 template<>
415 struct _Base_bitset<1>
416 {
417 typedef unsigned long _WordT;
418 _WordT _M_w;
419
420 _GLIBCXX_CONSTEXPR _Base_bitset() _GLIBCXX_NOEXCEPT
421 : _M_w(0)
422 { }
423
424#if __cplusplus >= 201103L
425 constexpr _Base_bitset(unsigned long long __val) noexcept
426#else
427 _Base_bitset(unsigned long __val)
428#endif
429 : _M_w(__val)
430 { }
431
432 static _GLIBCXX_CONSTEXPR size_t
433 _S_whichword(size_t __pos) _GLIBCXX_NOEXCEPT
434 { return __pos / _GLIBCXX_BITSET_BITS_PER_WORD; }
435
436 static _GLIBCXX_CONSTEXPR size_t
437 _S_whichbyte(size_t __pos) _GLIBCXX_NOEXCEPT
438 { return (__pos % _GLIBCXX_BITSET_BITS_PER_WORD) / __CHAR_BIT__; }
439
440 static _GLIBCXX_CONSTEXPR size_t
441 _S_whichbit(size_t __pos) _GLIBCXX_NOEXCEPT
442 { return __pos % _GLIBCXX_BITSET_BITS_PER_WORD; }
443
444 static _GLIBCXX_CONSTEXPR _WordT
445 _S_maskbit(size_t __pos) _GLIBCXX_NOEXCEPT
446 { return (static_cast<_WordT>(1)) << _S_whichbit(__pos); }
447
448 _GLIBCXX14_CONSTEXPR _WordT&
449 _M_getword(size_t) _GLIBCXX_NOEXCEPT
450 { return _M_w; }
451
452 _GLIBCXX_CONSTEXPR _WordT
453 _M_getword(size_t) const _GLIBCXX_NOEXCEPT
454 { return _M_w; }
455
456#if __cplusplus >= 201103L
457 constexpr const _WordT*
458 _M_getdata() const noexcept
459 { return &_M_w; }
460#endif
461
462 _GLIBCXX14_CONSTEXPR _WordT&
463 _M_hiword() _GLIBCXX_NOEXCEPT
464 { return _M_w; }
465
466 _GLIBCXX_CONSTEXPR _WordT
467 _M_hiword() const _GLIBCXX_NOEXCEPT
468 { return _M_w; }
469
470 _GLIBCXX14_CONSTEXPR void
471 _M_do_and(const _Base_bitset<1>& __x) _GLIBCXX_NOEXCEPT
472 { _M_w &= __x._M_w; }
473
474 _GLIBCXX14_CONSTEXPR void
475 _M_do_or(const _Base_bitset<1>& __x) _GLIBCXX_NOEXCEPT
476 { _M_w |= __x._M_w; }
477
478 _GLIBCXX14_CONSTEXPR void
479 _M_do_xor(const _Base_bitset<1>& __x) _GLIBCXX_NOEXCEPT
480 { _M_w ^= __x._M_w; }
481
482 _GLIBCXX14_CONSTEXPR void
483 _M_do_left_shift(size_t __shift) _GLIBCXX_NOEXCEPT
484 { _M_w <<= __shift; }
485
486 _GLIBCXX14_CONSTEXPR void
487 _M_do_right_shift(size_t __shift) _GLIBCXX_NOEXCEPT
488 { _M_w >>= __shift; }
489
490 _GLIBCXX14_CONSTEXPR void
491 _M_do_flip() _GLIBCXX_NOEXCEPT
492 { _M_w = ~_M_w; }
493
494 _GLIBCXX14_CONSTEXPR void
495 _M_do_set() _GLIBCXX_NOEXCEPT
496 { _M_w = ~static_cast<_WordT>(0); }
497
498 _GLIBCXX14_CONSTEXPR void
499 _M_do_reset() _GLIBCXX_NOEXCEPT
500 { _M_w = 0; }
501
502 _GLIBCXX14_CONSTEXPR bool
503 _M_is_equal(const _Base_bitset<1>& __x) const _GLIBCXX_NOEXCEPT
504 { return _M_w == __x._M_w; }
505
506 template<size_t _Nb>
507 _GLIBCXX14_CONSTEXPR bool
508 _M_are_all() const _GLIBCXX_NOEXCEPT
509 { return _M_w == (~static_cast<_WordT>(0)
510 >> (_GLIBCXX_BITSET_BITS_PER_WORD - _Nb)); }
511
512 _GLIBCXX14_CONSTEXPR bool
513 _M_is_any() const _GLIBCXX_NOEXCEPT
514 { return _M_w != 0; }
515
516 _GLIBCXX14_CONSTEXPR size_t
517 _M_do_count() const _GLIBCXX_NOEXCEPT
518 { return __builtin_popcountl(_M_w); }
519
520 _GLIBCXX14_CONSTEXPR unsigned long
521 _M_do_to_ulong() const _GLIBCXX_NOEXCEPT
522 { return _M_w; }
523
524#if __cplusplus >= 201103L
525 constexpr unsigned long long
526 _M_do_to_ullong() const noexcept
527 { return _M_w; }
528#endif
529
530 _GLIBCXX14_CONSTEXPR size_t
531 _M_do_find_first(size_t __not_found) const _GLIBCXX_NOEXCEPT
532 {
533 if (_M_w != 0)
534 return __builtin_ctzl(_M_w);
535 else
536 return __not_found;
537 }
538
539 // find the next "on" bit that follows "prev"
540 _GLIBCXX14_CONSTEXPR size_t
541 _M_do_find_next(size_t __prev, size_t __not_found) const
542 _GLIBCXX_NOEXCEPT
543 {
544 ++__prev;
545 if (__prev >= ((size_t) _GLIBCXX_BITSET_BITS_PER_WORD))
546 return __not_found;
547
548 _WordT __x = _M_w >> __prev;
549 if (__x != 0)
550 return __builtin_ctzl(__x) + __prev;
551 else
552 return __not_found;
553 }
554 };
555
556 /**
557 * Base class, specialization for no storage (zero-length %bitset).
558 *
559 * See documentation for bitset.
560 */
561 template<>
562 struct _Base_bitset<0>
563 {
564 typedef unsigned long _WordT;
565
566 _GLIBCXX_CONSTEXPR _Base_bitset() _GLIBCXX_NOEXCEPT
567 { }
568
569#if __cplusplus >= 201103L
570 constexpr _Base_bitset(unsigned long long) noexcept
571#else
572 _Base_bitset(unsigned long)
573#endif
574 { }
575
576 static _GLIBCXX_CONSTEXPR size_t
577 _S_whichword(size_t __pos) _GLIBCXX_NOEXCEPT
578 { return __pos / _GLIBCXX_BITSET_BITS_PER_WORD; }
579
580 static _GLIBCXX_CONSTEXPR size_t
581 _S_whichbyte(size_t __pos) _GLIBCXX_NOEXCEPT
582 { return (__pos % _GLIBCXX_BITSET_BITS_PER_WORD) / __CHAR_BIT__; }
583
584 static _GLIBCXX_CONSTEXPR size_t
585 _S_whichbit(size_t __pos) _GLIBCXX_NOEXCEPT
586 { return __pos % _GLIBCXX_BITSET_BITS_PER_WORD; }
587
588 static _GLIBCXX_CONSTEXPR _WordT
589 _S_maskbit(size_t __pos) _GLIBCXX_NOEXCEPT
590 { return (static_cast<_WordT>(1)) << _S_whichbit(__pos); }
591
592 // This would normally give access to the data. The bounds-checking
593 // in the bitset class will prevent the user from getting this far,
594 // but this must fail if the user calls _Unchecked_set directly.
595 // Let's not penalize zero-length users unless they actually
596 // make an unchecked call; all the memory ugliness is therefore
597 // localized to this single should-never-get-this-far function.
598 __attribute__((__noreturn__))
599 _WordT&
600 _M_getword(size_t) _GLIBCXX_NOEXCEPT
601 { __throw_out_of_range(__N("_Base_bitset::_M_getword")); }
602
603 _GLIBCXX_CONSTEXPR _WordT
604 _M_getword(size_t) const _GLIBCXX_NOEXCEPT
605 { return 0; }
606
607 _GLIBCXX_CONSTEXPR _WordT
608 _M_hiword() const _GLIBCXX_NOEXCEPT
609 { return 0; }
610
611 _GLIBCXX14_CONSTEXPR void
612 _M_do_and(const _Base_bitset<0>&) _GLIBCXX_NOEXCEPT
613 { }
614
615 _GLIBCXX14_CONSTEXPR void
616 _M_do_or(const _Base_bitset<0>&) _GLIBCXX_NOEXCEPT
617 { }
618
619 _GLIBCXX14_CONSTEXPR void
620 _M_do_xor(const _Base_bitset<0>&) _GLIBCXX_NOEXCEPT
621 { }
622
623 _GLIBCXX14_CONSTEXPR void
624 _M_do_left_shift(size_t) _GLIBCXX_NOEXCEPT
625 { }
626
627 _GLIBCXX14_CONSTEXPR void
628 _M_do_right_shift(size_t) _GLIBCXX_NOEXCEPT
629 { }
630
631 _GLIBCXX14_CONSTEXPR void
632 _M_do_flip() _GLIBCXX_NOEXCEPT
633 { }
634
635 _GLIBCXX14_CONSTEXPR void
636 _M_do_set() _GLIBCXX_NOEXCEPT
637 { }
638
639 _GLIBCXX14_CONSTEXPR void
640 _M_do_reset() _GLIBCXX_NOEXCEPT
641 { }
642
643 // Are all empty bitsets equal to each other? Are they equal to
644 // themselves? How to compare a thing which has no state? What is
645 // the sound of one zero-length bitset clapping?
646 _GLIBCXX_CONSTEXPR bool
647 _M_is_equal(const _Base_bitset<0>&) const _GLIBCXX_NOEXCEPT
648 { return true; }
649
650 template<size_t _Nb>
651 _GLIBCXX_CONSTEXPR bool
652 _M_are_all() const _GLIBCXX_NOEXCEPT
653 { return true; }
654
655 _GLIBCXX_CONSTEXPR bool
656 _M_is_any() const _GLIBCXX_NOEXCEPT
657 { return false; }
658
659 _GLIBCXX_CONSTEXPR size_t
660 _M_do_count() const _GLIBCXX_NOEXCEPT
661 { return 0; }
662
663 _GLIBCXX_CONSTEXPR unsigned long
664 _M_do_to_ulong() const _GLIBCXX_NOEXCEPT
665 { return 0; }
666
667#if __cplusplus >= 201103L
668 constexpr unsigned long long
669 _M_do_to_ullong() const noexcept
670 { return 0; }
671#endif
672
673 // Normally "not found" is the size, but that could also be
674 // misinterpreted as an index in this corner case. Oh well.
675 _GLIBCXX_CONSTEXPR size_t
676 _M_do_find_first(size_t) const _GLIBCXX_NOEXCEPT
677 { return 0; }
678
679 _GLIBCXX_CONSTEXPR size_t
680 _M_do_find_next(size_t, size_t) const _GLIBCXX_NOEXCEPT
681 { return 0; }
682 };
683
684
685 // Helper class to zero out the unused high-order bits in the highest word.
686 template<size_t _Extrabits>
687 struct _Sanitize
688 {
689 typedef unsigned long _WordT;
690
691 static _GLIBCXX14_CONSTEXPR void
692 _S_do_sanitize(_WordT& __val) _GLIBCXX_NOEXCEPT
693 { __val &= ~((~static_cast<_WordT>(0)) << _Extrabits); }
694 };
695
696 template<>
697 struct _Sanitize<0>
698 {
699 typedef unsigned long _WordT;
700
701 static _GLIBCXX14_CONSTEXPR void
702 _S_do_sanitize(_WordT) _GLIBCXX_NOEXCEPT { }
703 };
704
705#if __cplusplus >= 201103L
706 template<size_t _Nb, bool = (_Nb < _GLIBCXX_BITSET_BITS_PER_ULL)>
707 struct _Sanitize_val
708 {
709 static constexpr unsigned long long
710 _S_do_sanitize_val(unsigned long long __val)
711 { return __val; }
712 };
713
714 template<size_t _Nb>
715 struct _Sanitize_val<_Nb, true>
716 {
717 static constexpr unsigned long long
718 _S_do_sanitize_val(unsigned long long __val)
719 { return __val & ~((~static_cast<unsigned long long>(0)) << _Nb); }
720 };
721
722 namespace __bitset
723 {
724#ifdef __cpp_lib_bitset // ...construct from string_view
725 template<typename _CharT>
726 using __string = std::basic_string_view<_CharT>;
727#elif _GLIBCXX_HOSTED
728 template<typename _CharT>
729 using __string = std::basic_string<_CharT>;
730#else
731 template<typename _CharT>
732 struct __string
733 {
734 using size_type = size_t;
735 static constexpr size_type npos = size_type(-1);
736
737 struct traits_type
738 {
739 static _GLIBCXX14_CONSTEXPR size_t
740 length(const _CharT* __s) noexcept
741 {
742 size_t __n = 0;
743 while (__s[__n])
744 __n++;
745 return __n;
746 }
747
748 static constexpr bool
749 eq(_CharT __l, _CharT __r) noexcept
750 { return __l == __r; }
751 };
752 };
753#endif // HOSTED
754 } // namespace __bitset
755#endif // C++11
756
757 /**
758 * @brief The %bitset class represents a @e fixed-size sequence of bits.
759 * @ingroup utilities
760 *
761 * (Note that %bitset does @e not meet the formal requirements of a
762 * <a href="tables.html#65">container</a>. Mainly, it lacks iterators.)
763 *
764 * The template argument, `Nb`, may be any non-negative number,
765 * specifying the number of bits (e.g., "0", "12", "1024*1024").
766 *
767 * In the general unoptimized case, storage is allocated in word-sized
768 * blocks. Let B be the number of bits in a word, then (Nb+(B-1))/B
769 * words will be used for storage. B - Nb%B bits are unused. (They are
770 * the high-order bits in the highest word.) It is a class invariant
771 * that those unused bits are always zero.
772 *
773 * If you think of %bitset as <em>a simple array of bits</em>, be
774 * aware that your mental picture is reversed: a %bitset behaves
775 * the same way as bits in integers do, with the bit at index 0 in
776 * the <em>least significant / right-hand</em> position, and the bit at
777 * index Nb-1 in the <em>most significant / left-hand</em> position.
778 * Thus, unlike other containers, a %bitset's index <em>counts from
779 * right to left</em>, to put it very loosely.
780 *
781 * This behavior is preserved when translating to and from strings. For
782 * example, the first line of the following program probably prints
783 * <em>b(&apos;a&apos;) is 0001100001</em> on a modern ASCII system.
784 *
785 * @code
786 * #include <bitset>
787 * #include <iostream>
788 * #include <sstream>
789 *
790 * using namespace std;
791 *
792 * int main()
793 * {
794 * long a = 'a';
795 * bitset<10> b(a);
796 *
797 * cout << "b('a') is " << b << endl;
798 *
799 * ostringstream s;
800 * s << b;
801 * string str = s.str();
802 * cout << "index 3 in the string is " << str[3] << " but\n"
803 * << "index 3 in the bitset is " << b[3] << endl;
804 * }
805 * @endcode
806 *
807 * Also see:
808 * https://gcc.gnu.org/onlinedocs/libstdc++/manual/ext_containers.html
809 * for a description of extensions.
810 *
811 * Most of the actual code isn't contained in %bitset<> itself, but in the
812 * base class _Base_bitset. The base class works with whole words, not with
813 * individual bits. This allows us to specialize _Base_bitset for the
814 * important special case where the %bitset is only a single word.
815 *
816 * Extra confusion can result due to the fact that the storage for
817 * _Base_bitset @e is a regular array, and is indexed as such. This is
818 * carefully encapsulated.
819 */
820 template<size_t _Nb>
821 class bitset
822 : private _Base_bitset<_GLIBCXX_BITSET_WORDS(_Nb)>
823 {
824 private:
825 typedef _Base_bitset<_GLIBCXX_BITSET_WORDS(_Nb)> _Base;
826 typedef unsigned long _WordT;
827
828 template<class _Str>
829 _GLIBCXX23_CONSTEXPR void
830 _M_check_initial_position(
831 const _Str& __s, typename _Str::size_type __position) const
832 {
833 if (__position > __s.size())
834 __throw_out_of_range_fmt(
835 __N("bitset::bitset:"
836 " __position (which is %zu) > __s.size() (which is %zu)"),
837 size_t(__position), size_t(__s.size()));
838 }
839
840 _GLIBCXX23_CONSTEXPR
841 void _M_check(size_t __position, const char *__s) const
842 {
843 if (__position >= _Nb)
844 __throw_out_of_range_fmt(
845 __N("%s: __position (which is %zu) >= _Nb (which is %zu)"),
846 __s, size_t(__position), size_t(_Nb));
847 }
848
849 _GLIBCXX23_CONSTEXPR
850 void
851 _M_do_sanitize() _GLIBCXX_NOEXCEPT
852 {
853 typedef _Sanitize<_Nb % _GLIBCXX_BITSET_BITS_PER_WORD> __sanitize_type;
854 __sanitize_type::_S_do_sanitize(this->_M_hiword());
855 }
856
857#if __cplusplus >= 201103L
858 friend struct std::hash<bitset>;
859#endif
860
861 public:
862 /**
863 * This encapsulates the concept of a single bit. An instance of this
864 * class is a proxy for an actual bit; this way the individual bit
865 * operations are done as faster word-size bitwise instructions.
866 *
867 * Most users will never need to use this class directly; conversions
868 * to and from bool are automatic and should be transparent. Overloaded
869 * operators help to preserve the illusion.
870 *
871 * (On a typical system, this <em>bit %reference</em> is 64
872 * times the size of an actual bit. Ha.)
873 */
874 class reference
875 {
876 friend class bitset;
877
878 _WordT* _M_wp;
879 size_t _M_bpos;
880
881 _GLIBCXX23_CONSTEXPR
882 reference(bitset& __b, size_t __pos) _GLIBCXX_NOEXCEPT
883 {
884 _M_wp = &__b._M_getword(__pos);
885 _M_bpos = _Base::_S_whichbit(__pos);
886 }
887
888 public:
889#if __cplusplus >= 201103L
890 reference(const reference&) = default;
891#endif
892
893#if __cplusplus > 202002L && __cpp_constexpr_dynamic_alloc
894 constexpr
895#endif
896 ~reference() _GLIBCXX_NOEXCEPT
897 { }
898
899 // For b[i] = __x;
900 _GLIBCXX23_CONSTEXPR
901 reference&
902 operator=(bool __x) _GLIBCXX_NOEXCEPT
903 {
904 if (__x)
905 *_M_wp |= _Base::_S_maskbit(_M_bpos);
906 else
907 *_M_wp &= ~_Base::_S_maskbit(_M_bpos);
908 return *this;
909 }
910
911 // For b[i] = b[__j];
912 _GLIBCXX23_CONSTEXPR
913 reference&
914 operator=(const reference& __j) _GLIBCXX_NOEXCEPT
915 {
916 if ((*(__j._M_wp) & _Base::_S_maskbit(__j._M_bpos)))
917 *_M_wp |= _Base::_S_maskbit(_M_bpos);
918 else
919 *_M_wp &= ~_Base::_S_maskbit(_M_bpos);
920 return *this;
921 }
922
923 // Flips the bit
924 _GLIBCXX23_CONSTEXPR
925 bool
926 operator~() const _GLIBCXX_NOEXCEPT
927 { return (*(_M_wp) & _Base::_S_maskbit(_M_bpos)) == 0; }
928
929 // For __x = b[i];
930 _GLIBCXX23_CONSTEXPR
931 operator bool() const _GLIBCXX_NOEXCEPT
932 { return (*(_M_wp) & _Base::_S_maskbit(_M_bpos)) != 0; }
933
934 // For b[i].flip();
935 _GLIBCXX23_CONSTEXPR
936 reference&
937 flip() _GLIBCXX_NOEXCEPT
938 {
939 *_M_wp ^= _Base::_S_maskbit(_M_bpos);
940 return *this;
941 }
942 };
943 friend class reference;
944
945 // 23.3.5.1 constructors:
946 /// All bits set to zero.
947 _GLIBCXX_CONSTEXPR bitset() _GLIBCXX_NOEXCEPT
948 { }
949
950 /// Initial bits bitwise-copied from a single word (others set to zero).
951#if __cplusplus >= 201103L
952 constexpr bitset(unsigned long long __val) noexcept
953 : _Base(_Sanitize_val<_Nb>::_S_do_sanitize_val(__val)) { }
954#else
955 bitset(unsigned long __val)
956 : _Base(__val)
957 { _M_do_sanitize(); }
958#endif
959
960#if _GLIBCXX_HOSTED
961 /**
962 * Use a subset of a string.
963 * @param __s A string of `0` and `1` characters.
964 * @param __position Index of the first character in `__s` to use;
965 * defaults to zero.
966 * @throw std::out_of_range If `__position > __s.size()`.
967 * @throw std::invalid_argument If a character appears in the string
968 * which is neither `0` nor `1`.
969 */
970 template<class _CharT, class _Traits, class _Alloc>
971 _GLIBCXX23_CONSTEXPR
972 explicit
974 size_t __position = 0)
975 : _Base()
976 {
977 _M_check_initial_position(__s, __position);
978 _M_copy_from_string(__s, __position,
980 _CharT('0'), _CharT('1'));
981 }
982
983 /**
984 * Use a subset of a string.
985 * @param __s A string of `0` and `1` characters.
986 * @param __position Index of the first character in `__s` to use.
987 * @param __n The number of characters to copy.
988 * @throw std::out_of_range If `__position > __s.size()`.
989 * @throw std::invalid_argument If a character appears in the string
990 * which is neither `0` nor `1`.
991 */
992 template<class _CharT, class _Traits, class _Alloc>
993 _GLIBCXX23_CONSTEXPR
995 size_t __position, size_t __n)
996 : _Base()
997 {
998 _M_check_initial_position(__s, __position);
999 _M_copy_from_string(__s, __position, __n, _CharT('0'), _CharT('1'));
1000 }
1001
1002 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1003 // 396. what are characters zero and one.
1004 template<class _CharT, class _Traits, class _Alloc>
1005 _GLIBCXX23_CONSTEXPR
1007 size_t __position, size_t __n,
1008 _CharT __zero, _CharT __one = _CharT('1'))
1009 : _Base()
1010 {
1011 _M_check_initial_position(__s, __position);
1012 _M_copy_from_string(__s, __position, __n, __zero, __one);
1013 }
1014#endif // HOSTED
1015
1016#ifdef __cpp_lib_bitset // C++ >= 23
1017 /**
1018 * Use a subset of a string view.
1019 * @param __s A `string_view` of a sequence of `0` and `1` characters.
1020 * @param __position Index of the first character in `__s` to use.
1021 * @param __n The maximum number of characters from `__s` to use.
1022 * @param __zero The character corresponding to the value 0.
1023 * @param __one The character corresponding to the value 1.
1024 * @throw std::out_of_range If `__position > __s.size()`.
1025 * @throw std::invalid_argument If a character appears in `__s`
1026 * which is neither `0` nor `1`.
1027 */
1028 template<class _CharT, class _Traits>
1029 constexpr explicit
1030 bitset(basic_string_view<_CharT, _Traits> __s,
1031 basic_string_view<_CharT, _Traits>::size_type __position = 0,
1032 basic_string_view<_CharT, _Traits>::size_type __n =
1033 basic_string_view<_CharT, _Traits>::npos,
1034 _CharT __zero = _CharT('0'), _CharT __one = _CharT('1'))
1035 : _Base()
1036 {
1037 _M_check_initial_position(__s, __position);
1038 _M_copy_from_ptr<_CharT, _Traits>(
1039 __s.data(), __s.size(), __position, __n, __zero, __one);
1040 }
1041#endif
1042
1043#if __cplusplus >= 201103L
1044 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1045 // 4294. bitset(const CharT*) constructor needs to be constrained
1046 /**
1047 * Construct from a character %array.
1048 * @param __str An %array of characters `__zero` and `__one`.
1049 * @param __n The number of characters to use.
1050 * @param __zero The character corresponding to the value 0.
1051 * @param __one The character corresponding to the value 1.
1052 * @throw std::invalid_argument If a character appears in the string
1053 * which is neither `__zero` nor `__one`.
1054 */
1055 template<typename _CharT,
1056 typename = _Require<is_trivially_copyable<_CharT>,
1059 __not_<is_array<_CharT>>>>
1060 [[__gnu__::__nonnull__]]
1061 _GLIBCXX23_CONSTEXPR
1062 explicit
1063 bitset(const _CharT* __str,
1064 typename __bitset::__string<_CharT>::size_type __n
1066 _CharT __zero = _CharT('0'), _CharT __one = _CharT('1'))
1067 : _Base()
1068 {
1069 if (!__str)
1070 __throw_logic_error(__N("bitset::bitset(const _CharT*, ...)"));
1071 using _Traits = typename __bitset::__string<_CharT>::traits_type;
1072
1074 __n = _Traits::length(__str);
1075 _M_copy_from_ptr<_CharT, _Traits>(__str, __n, 0, __n, __zero, __one);
1076 }
1077#endif // C++11
1078
1079 // 23.3.5.2 bitset operations:
1080 ///@{
1081 /**
1082 * Operations on bitsets.
1083 * @param __rhs A same-sized bitset.
1084 *
1085 * These should be self-explanatory.
1086 */
1087 _GLIBCXX23_CONSTEXPR
1089 operator&=(const bitset<_Nb>& __rhs) _GLIBCXX_NOEXCEPT
1090 {
1091 this->_M_do_and(__rhs);
1092 return *this;
1093 }
1094
1095 _GLIBCXX23_CONSTEXPR
1097 operator|=(const bitset<_Nb>& __rhs) _GLIBCXX_NOEXCEPT
1098 {
1099 this->_M_do_or(__rhs);
1100 return *this;
1101 }
1102
1103 _GLIBCXX23_CONSTEXPR
1105 operator^=(const bitset<_Nb>& __rhs) _GLIBCXX_NOEXCEPT
1106 {
1107 this->_M_do_xor(__rhs);
1108 return *this;
1109 }
1110 ///@}
1111
1112 ///@{
1113 /**
1114 * Operations on bitsets.
1115 * @param __position The number of places to shift.
1116 *
1117 * These should be self-explanatory.
1118 */
1119 _GLIBCXX23_CONSTEXPR
1121 operator<<=(size_t __position) _GLIBCXX_NOEXCEPT
1122 {
1123 if (__builtin_expect(__position < _Nb, 1))
1124 {
1125 this->_M_do_left_shift(__position);
1126 this->_M_do_sanitize();
1127 }
1128 else
1129 this->_M_do_reset();
1130 return *this;
1131 }
1132
1133 _GLIBCXX23_CONSTEXPR
1135 operator>>=(size_t __position) _GLIBCXX_NOEXCEPT
1136 {
1137 if (__builtin_expect(__position < _Nb, 1))
1138 this->_M_do_right_shift(__position);
1139 else
1140 this->_M_do_reset();
1141 return *this;
1142 }
1143 ///@}
1144
1145 ///@{
1146 /**
1147 * These versions of single-bit set, reset, flip, and test are
1148 * extensions from the SGI version. They do no range checking.
1149 * @ingroup SGIextensions
1150 */
1151 _GLIBCXX23_CONSTEXPR
1153 _Unchecked_set(size_t __pos) _GLIBCXX_NOEXCEPT
1154 {
1155 this->_M_getword(__pos) |= _Base::_S_maskbit(__pos);
1156 return *this;
1157 }
1158
1159 _GLIBCXX23_CONSTEXPR
1161 _Unchecked_set(size_t __pos, int __val) _GLIBCXX_NOEXCEPT
1162 {
1163 if (__val)
1164 this->_M_getword(__pos) |= _Base::_S_maskbit(__pos);
1165 else
1166 this->_M_getword(__pos) &= ~_Base::_S_maskbit(__pos);
1167 return *this;
1168 }
1169
1170 _GLIBCXX23_CONSTEXPR
1172 _Unchecked_reset(size_t __pos) _GLIBCXX_NOEXCEPT
1173 {
1174 this->_M_getword(__pos) &= ~_Base::_S_maskbit(__pos);
1175 return *this;
1176 }
1177
1178 _GLIBCXX23_CONSTEXPR
1180 _Unchecked_flip(size_t __pos) _GLIBCXX_NOEXCEPT
1181 {
1182 this->_M_getword(__pos) ^= _Base::_S_maskbit(__pos);
1183 return *this;
1184 }
1185
1186 _GLIBCXX_CONSTEXPR bool
1187 _Unchecked_test(size_t __pos) const _GLIBCXX_NOEXCEPT
1188 { return ((this->_M_getword(__pos) & _Base::_S_maskbit(__pos))
1189 != static_cast<_WordT>(0)); }
1190 ///@}
1191
1192 // Set, reset, and flip.
1193 /**
1194 * @brief Sets every bit to true.
1195 */
1196 _GLIBCXX23_CONSTEXPR
1198 set() _GLIBCXX_NOEXCEPT
1199 {
1200 this->_M_do_set();
1201 this->_M_do_sanitize();
1202 return *this;
1203 }
1204
1205 /**
1206 * @brief Sets a given bit to a particular value.
1207 * @param __position The index of the bit.
1208 * @param __val Either true or false, defaults to true.
1209 * @throw std::out_of_range If @a pos is bigger the size of the %set.
1210 */
1211 _GLIBCXX23_CONSTEXPR
1213 set(size_t __position, bool __val = true)
1214 {
1215 this->_M_check(__position, __N("bitset::set"));
1216 return _Unchecked_set(__position, __val);
1217 }
1218
1219 /**
1220 * @brief Sets every bit to false.
1221 */
1222 _GLIBCXX23_CONSTEXPR
1224 reset() _GLIBCXX_NOEXCEPT
1225 {
1226 this->_M_do_reset();
1227 return *this;
1228 }
1229
1230 /**
1231 * @brief Sets a given bit to false.
1232 * @param __position The index of the bit.
1233 * @throw std::out_of_range If @a pos is bigger the size of the %set.
1234 *
1235 * Same as writing @c set(pos,false).
1236 */
1237 _GLIBCXX23_CONSTEXPR
1239 reset(size_t __position)
1240 {
1241 this->_M_check(__position, __N("bitset::reset"));
1242 return _Unchecked_reset(__position);
1243 }
1244
1245 /**
1246 * @brief Toggles every bit to its opposite value.
1247 */
1248 _GLIBCXX23_CONSTEXPR
1250 flip() _GLIBCXX_NOEXCEPT
1251 {
1252 this->_M_do_flip();
1253 this->_M_do_sanitize();
1254 return *this;
1255 }
1256
1257 /**
1258 * @brief Toggles a given bit to its opposite value.
1259 * @param __position The index of the bit.
1260 * @throw std::out_of_range If @a pos is bigger the size of the %set.
1261 */
1262 _GLIBCXX23_CONSTEXPR
1264 flip(size_t __position)
1265 {
1266 this->_M_check(__position, __N("bitset::flip"));
1267 return _Unchecked_flip(__position);
1268 }
1269
1270 /// See the no-argument flip().
1271 _GLIBCXX23_CONSTEXPR
1273 operator~() const _GLIBCXX_NOEXCEPT
1274 { return bitset<_Nb>(*this).flip(); }
1275
1276 ///@{
1277 /**
1278 * @brief Array-indexing support.
1279 * @param __position Index into the %bitset.
1280 * @return A bool for a <em>const %bitset</em>. For non-const
1281 * bitsets, an instance of the reference proxy class.
1282 * @note These operators do no range checking and throw no exceptions,
1283 * as required by DR 11 to the standard.
1284 *
1285 * _GLIBCXX_RESOLVE_LIB_DEFECTS Note that this implementation already
1286 * resolves DR 11 (items 1 and 2), but does not do the range-checking
1287 * required by that DR's resolution. -pme
1288 * The DR has since been changed: range-checking is a precondition
1289 * (users' responsibility), and these functions must not throw. -pme
1290 */
1291 _GLIBCXX23_CONSTEXPR
1292 reference
1293 operator[](size_t __position)
1294 {
1295 __glibcxx_assert(__position < _Nb);
1296 return reference(*this, __position);
1297 }
1298
1299 _GLIBCXX_CONSTEXPR bool
1300 operator[](size_t __position) const
1301 {
1302#if __cplusplus != 201103L
1303 __glibcxx_assert(__position < _Nb);
1304 return _Unchecked_test(__position);
1305#elif defined(_GLIBCXX_ASSERTIONS)
1306 // C++11 forbids a compound stmt in a constexpr function.
1307 return __position < _Nb ? _Unchecked_test(__position)
1308 : (__builtin_trap(), false);
1309#else
1310 return _Unchecked_test(__position);
1311#endif
1312 }
1313 ///@}
1314
1315 /**
1316 * @brief Returns a numerical interpretation of the %bitset.
1317 * @return The integral equivalent of the bits.
1318 * @throw std::overflow_error If there are too many bits to be
1319 * represented in an @c unsigned @c long.
1320 */
1321 _GLIBCXX23_CONSTEXPR
1322 unsigned long
1323 to_ulong() const
1324 { return this->_M_do_to_ulong(); }
1325
1326#if __cplusplus >= 201103L
1327 _GLIBCXX23_CONSTEXPR
1328 unsigned long long
1329 to_ullong() const
1330 { return this->_M_do_to_ullong(); }
1331#endif
1332
1333#if _GLIBCXX_HOSTED
1334 /**
1335 * @brief Returns a character interpretation of the %bitset.
1336 * @return The string equivalent of the bits.
1337 *
1338 * Note the ordering of the bits: decreasing character positions
1339 * correspond to increasing bit positions (see the main class notes for
1340 * an example).
1341 */
1342 template<class _CharT, class _Traits, class _Alloc>
1343 _GLIBCXX23_CONSTEXPR
1346 {
1348 _M_copy_to_string(__result, _CharT('0'), _CharT('1'));
1349 return __result;
1350 }
1351
1352 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1353 // 396. what are characters zero and one.
1354 template<class _CharT, class _Traits, class _Alloc>
1355 _GLIBCXX23_CONSTEXPR
1357 to_string(_CharT __zero, _CharT __one = _CharT('1')) const
1358 {
1360 _M_copy_to_string(__result, __zero, __one);
1361 return __result;
1362 }
1363
1364 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1365 // 434. bitset::to_string() hard to use.
1366 template<class _CharT, class _Traits>
1367 _GLIBCXX23_CONSTEXPR
1369 to_string() const
1370 { return to_string<_CharT, _Traits, std::allocator<_CharT> >(); }
1371
1372 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1373 // 853. to_string needs updating with zero and one.
1374 template<class _CharT, class _Traits>
1375 _GLIBCXX23_CONSTEXPR
1376 std::basic_string<_CharT, _Traits, std::allocator<_CharT> >
1377 to_string(_CharT __zero, _CharT __one = _CharT('1')) const
1378 { return to_string<_CharT, _Traits,
1379 std::allocator<_CharT> >(__zero, __one); }
1380
1381 template<class _CharT>
1382 _GLIBCXX23_CONSTEXPR
1383 std::basic_string<_CharT, std::char_traits<_CharT>,
1384 std::allocator<_CharT> >
1385 to_string() const
1386 {
1387 return to_string<_CharT, std::char_traits<_CharT>,
1388 std::allocator<_CharT> >();
1389 }
1390
1391 template<class _CharT>
1392 _GLIBCXX23_CONSTEXPR
1393 std::basic_string<_CharT, std::char_traits<_CharT>,
1394 std::allocator<_CharT> >
1395 to_string(_CharT __zero, _CharT __one = _CharT('1')) const
1396 {
1397 return to_string<_CharT, std::char_traits<_CharT>,
1398 std::allocator<_CharT> >(__zero, __one);
1399 }
1400
1401 _GLIBCXX23_CONSTEXPR
1402 std::basic_string<char, std::char_traits<char>, std::allocator<char> >
1403 to_string() const
1404 {
1405 return to_string<char, std::char_traits<char>,
1406 std::allocator<char> >();
1407 }
1408
1409 _GLIBCXX23_CONSTEXPR
1410 std::basic_string<char, std::char_traits<char>, std::allocator<char> >
1411 to_string(char __zero, char __one = '1') const
1412 {
1413 return to_string<char, std::char_traits<char>,
1414 std::allocator<char> >(__zero, __one);
1415 }
1416#endif // HOSTED
1417
1418 /// Returns the number of bits which are set.
1419 _GLIBCXX23_CONSTEXPR
1420 size_t
1421 count() const _GLIBCXX_NOEXCEPT
1422 { return this->_M_do_count(); }
1423
1424 /// Returns the total number of bits.
1425 _GLIBCXX_CONSTEXPR size_t
1426 size() const _GLIBCXX_NOEXCEPT
1427 { return _Nb; }
1428
1429 ///@{
1430 /// These comparisons for equality/inequality are, well, @e bitwise.
1431 _GLIBCXX23_CONSTEXPR
1432 bool
1433 operator==(const bitset<_Nb>& __rhs) const _GLIBCXX_NOEXCEPT
1434 { return this->_M_is_equal(__rhs); }
1435
1436#if __cpp_impl_three_way_comparison < 201907L
1437 _GLIBCXX23_CONSTEXPR
1438 bool
1439 operator!=(const bitset<_Nb>& __rhs) const _GLIBCXX_NOEXCEPT
1440 { return !this->_M_is_equal(__rhs); }
1441#endif
1442 ///@}
1443
1444 /**
1445 * @brief Tests the value of a bit.
1446 * @param __position The index of a bit.
1447 * @return The value at @a pos.
1448 * @throw std::out_of_range If @a pos is bigger the size of the %set.
1449 */
1450 _GLIBCXX23_CONSTEXPR
1451 bool
1452 test(size_t __position) const
1453 {
1454 this->_M_check(__position, __N("bitset::test"));
1455 return _Unchecked_test(__position);
1456 }
1457
1458 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1459 // DR 693. std::bitset::all() missing.
1460 /**
1461 * @brief Tests whether all the bits are on.
1462 * @return True if all the bits are set.
1463 */
1464 _GLIBCXX23_CONSTEXPR
1465 bool
1466 all() const _GLIBCXX_NOEXCEPT
1467 { return this->template _M_are_all<_Nb>(); }
1468
1469 /**
1470 * @brief Tests whether any of the bits are on.
1471 * @return True if at least one bit is set.
1472 */
1473 _GLIBCXX23_CONSTEXPR
1474 bool
1475 any() const _GLIBCXX_NOEXCEPT
1476 { return this->_M_is_any(); }
1477
1478 /**
1479 * @brief Tests whether any of the bits are on.
1480 * @return True if none of the bits are set.
1481 */
1482 _GLIBCXX23_CONSTEXPR
1483 bool
1484 none() const _GLIBCXX_NOEXCEPT
1485 { return !this->_M_is_any(); }
1486
1487 ///@{
1488 /// Self-explanatory.
1489 _GLIBCXX23_CONSTEXPR
1491 operator<<(size_t __position) const _GLIBCXX_NOEXCEPT
1492 { return bitset<_Nb>(*this) <<= __position; }
1493
1494 _GLIBCXX23_CONSTEXPR
1496 operator>>(size_t __position) const _GLIBCXX_NOEXCEPT
1497 { return bitset<_Nb>(*this) >>= __position; }
1498 ///@}
1499
1500 /**
1501 * @brief Finds the index of the first "on" bit.
1502 * @return The index of the first bit set, or size() if not found.
1503 * @ingroup SGIextensions
1504 * @sa _Find_next
1505 */
1506 _GLIBCXX23_CONSTEXPR
1507 size_t
1508 _Find_first() const _GLIBCXX_NOEXCEPT
1509 { return this->_M_do_find_first(_Nb); }
1510
1511 /**
1512 * @brief Finds the index of the next "on" bit after prev.
1513 * @return The index of the next bit set, or size() if not found.
1514 * @param __prev Where to start searching.
1515 * @ingroup SGIextensions
1516 * @sa _Find_first
1517 */
1518 _GLIBCXX23_CONSTEXPR
1519 size_t
1520 _Find_next(size_t __prev) const _GLIBCXX_NOEXCEPT
1521 { return this->_M_do_find_next(__prev, _Nb); }
1522
1523 private:
1524 // Helper functions for string operations.
1525 template<class _CharT, class _Traits>
1526 _GLIBCXX23_CONSTEXPR
1527 void
1528 _M_copy_from_ptr(const _CharT*, size_t, size_t, size_t,
1529 _CharT, _CharT);
1530
1531#if _GLIBCXX_HOSTED
1532 template<class _CharT, class _Traits, class _Alloc>
1533 _GLIBCXX23_CONSTEXPR
1534 void
1535 _M_copy_from_string(const std::basic_string<_CharT,
1536 _Traits, _Alloc>& __s, size_t __pos, size_t __n,
1537 _CharT __zero, _CharT __one)
1538 { _M_copy_from_ptr<_CharT, _Traits>(__s.data(), __s.size(), __pos, __n,
1539 __zero, __one); }
1540
1541 template<class _CharT, class _Traits, class _Alloc>
1542 _GLIBCXX23_CONSTEXPR
1543 void
1545 _CharT, _CharT) const;
1546
1547 template<class _CharT, class _Traits, size_t _Nb2>
1550
1551 template <class _CharT, class _Traits, size_t _Nb2>
1553 operator<<(std::basic_ostream<_CharT, _Traits>&, const bitset<_Nb2>&);
1554#endif
1555 };
1556
1557 // Definitions of non-inline member functions.
1558 template<size_t _Nb>
1559 template<class _CharT, class _Traits>
1560 _GLIBCXX23_CONSTEXPR
1561 void
1563 _M_copy_from_ptr(const _CharT* __s, size_t __len,
1564 size_t __pos, size_t __n, _CharT __zero, _CharT __one)
1565 {
1566 reset();
1567 const size_t __rlen = std::min(__n, size_t(__len - __pos));
1568 const size_t __nbits = std::min(_Nb, __rlen);
1569 for (size_t __i = __rlen - __nbits; __i > 0; --__i)
1570 {
1571 const _CharT __c = __s[__pos + __rlen - __i];
1572 if (!_Traits::eq(__c, __zero) && !_Traits::eq(__c, __one))
1573 __throw_invalid_argument(__N("bitset::_M_copy_from_ptr"));
1574 }
1575 for (size_t __i = __nbits; __i > 0; --__i)
1576 {
1577 const _CharT __c = __s[__pos + __nbits - __i];
1578 if (_Traits::eq(__c, __zero))
1579 ;
1580 else if (_Traits::eq(__c, __one))
1581 _Unchecked_set(__i - 1);
1582 else
1583 __throw_invalid_argument(__N("bitset::_M_copy_from_ptr"));
1584 }
1585 }
1586
1587#if _GLIBCXX_HOSTED
1588 template<size_t _Nb>
1589 template<class _CharT, class _Traits, class _Alloc>
1590 _GLIBCXX23_CONSTEXPR
1591 void
1593 _M_copy_to_string(std::basic_string<_CharT, _Traits, _Alloc>& __s,
1594 _CharT __zero, _CharT __one) const
1595 {
1596 __s.assign(_Nb, __zero);
1597 size_t __n = this->_Find_first();
1598 while (__n < _Nb)
1599 {
1600 __s[_Nb - __n - 1] = __one;
1601 __n = _Find_next(__n);
1602 }
1603 }
1604#endif // HOSTED
1605
1606 // 23.3.5.3 bitset operations:
1607 ///@{
1608 /**
1609 * @brief Global bitwise operations on bitsets.
1610 * @param __x A bitset.
1611 * @param __y A bitset of the same size as @a __x.
1612 * @return A new bitset.
1613 *
1614 * These should be self-explanatory.
1615 */
1616 template<size_t _Nb>
1617 _GLIBCXX23_CONSTEXPR
1618 inline bitset<_Nb>
1619 operator&(const bitset<_Nb>& __x, const bitset<_Nb>& __y) _GLIBCXX_NOEXCEPT
1620 {
1621 bitset<_Nb> __result(__x);
1622 __result &= __y;
1623 return __result;
1624 }
1625
1626 template<size_t _Nb>
1627 _GLIBCXX23_CONSTEXPR
1628 inline bitset<_Nb>
1629 operator|(const bitset<_Nb>& __x, const bitset<_Nb>& __y) _GLIBCXX_NOEXCEPT
1630 {
1631 bitset<_Nb> __result(__x);
1632 __result |= __y;
1633 return __result;
1634 }
1635
1636 template <size_t _Nb>
1637 _GLIBCXX23_CONSTEXPR
1638 inline bitset<_Nb>
1639 operator^(const bitset<_Nb>& __x, const bitset<_Nb>& __y) _GLIBCXX_NOEXCEPT
1640 {
1641 bitset<_Nb> __result(__x);
1642 __result ^= __y;
1643 return __result;
1644 }
1645 ///@}
1646
1647#if _GLIBCXX_HOSTED
1648 ///@{
1649 /**
1650 * @brief Global I/O operators for bitsets.
1651 *
1652 * Direct I/O between streams and bitsets is supported. Output is
1653 * straightforward. Input will skip whitespace, only accept @a 0 and @a 1
1654 * characters, and will only extract as many digits as the %bitset will
1655 * hold.
1656 */
1657 template<class _CharT, class _Traits, size_t _Nb>
1660 {
1661 typedef typename _Traits::char_type char_type;
1662 typedef std::basic_istream<_CharT, _Traits> __istream_type;
1663 typedef typename __istream_type::ios_base __ios_base;
1664
1665#pragma GCC diagnostic push
1666#pragma GCC diagnostic ignored "-Wc++17-extensions" // if constexpr
1667 struct _Buffer
1668 {
1669 static _GLIBCXX_CONSTEXPR bool _S_use_alloca() { return _Nb <= 256; }
1670
1671 explicit _Buffer(_CharT* __p) : _M_ptr(__p) { }
1672
1673 ~_Buffer()
1674 {
1675 if _GLIBCXX_CONSTEXPR (!_S_use_alloca())
1676 delete[] _M_ptr;
1677 }
1678
1679 _CharT* const _M_ptr;
1680 };
1681 _CharT* __ptr;
1682 if _GLIBCXX_CONSTEXPR (_Buffer::_S_use_alloca())
1683 __ptr = (_CharT*)__builtin_alloca(_Nb);
1684 else
1685 __ptr = new _CharT[_Nb];
1686 const _Buffer __buf(__ptr);
1687#pragma GCC diagnostic pop
1688
1689 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1690 // 303. Bitset input operator underspecified
1691 const char_type __zero = __is.widen('0');
1692 const char_type __one = __is.widen('1');
1693
1694 typename __ios_base::iostate __state = __ios_base::goodbit;
1695 typename __istream_type::sentry __sentry(__is);
1696 if (__sentry)
1697 {
1698 __try
1699 {
1700 for (size_t __i = _Nb; __i > 0; --__i)
1701 {
1702 static typename _Traits::int_type __eof = _Traits::eof();
1703
1704 typename _Traits::int_type __c1 = __is.rdbuf()->sbumpc();
1705 if (_Traits::eq_int_type(__c1, __eof))
1706 {
1707 __state |= __ios_base::eofbit;
1708 break;
1709 }
1710 else
1711 {
1712 const char_type __c2 = _Traits::to_char_type(__c1);
1713 if (_Traits::eq(__c2, __zero))
1714 *__ptr++ = __zero;
1715 else if (_Traits::eq(__c2, __one))
1716 *__ptr++ = __one;
1717 else if (_Traits::
1718 eq_int_type(__is.rdbuf()->sputbackc(__c2),
1719 __eof))
1720 {
1721 __state |= __ios_base::failbit;
1722 break;
1723 }
1724 }
1725 }
1726 }
1728 {
1729 __is._M_setstate(__ios_base::badbit);
1730 __throw_exception_again;
1731 }
1732 __catch(...)
1733 { __is._M_setstate(__ios_base::badbit); }
1734 }
1735
1736#pragma GCC diagnostic push
1737#pragma GCC diagnostic ignored "-Wc++17-extensions" // if constexpr
1738 if _GLIBCXX_CONSTEXPR (_Nb)
1739 {
1740 if (size_t __len = __ptr - __buf._M_ptr)
1741 __x.template _M_copy_from_ptr<_CharT, _Traits>(__buf._M_ptr, __len,
1742 0, __len,
1743 __zero, __one);
1744 else
1745 __state |= __ios_base::failbit;
1746 }
1747#pragma GCC diagnostic pop
1748 if (__state)
1749 __is.setstate(__state);
1750 return __is;
1751 }
1752
1753 template <class _CharT, class _Traits, size_t _Nb>
1756 const bitset<_Nb>& __x)
1757 {
1759
1760 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1761 // 396. what are characters zero and one.
1762 const ctype<_CharT>& __ct = use_facet<ctype<_CharT> >(__os.getloc());
1763 __x._M_copy_to_string(__tmp, __ct.widen('0'), __ct.widen('1'));
1764 return __os << __tmp;
1765 }
1766 ///@}
1767#endif // HOSTED
1768
1769_GLIBCXX_END_NAMESPACE_CONTAINER
1770} // namespace std
1771
1772#undef _GLIBCXX_BITSET_WORDS
1773#undef _GLIBCXX_BITSET_BITS_PER_WORD
1774#undef _GLIBCXX_BITSET_BITS_PER_ULL
1775
1776#if __cplusplus >= 201103L
1777
1778namespace std _GLIBCXX_VISIBILITY(default)
1779{
1780_GLIBCXX_BEGIN_NAMESPACE_VERSION
1781
1782 // DR 1182.
1783 /// std::hash specialization for bitset.
1784 template<size_t _Nb>
1785 struct hash<_GLIBCXX_STD_C::bitset<_Nb>>
1786 : public __hash_base<size_t, _GLIBCXX_STD_C::bitset<_Nb>>
1787 {
1788 size_t
1789 operator()(const _GLIBCXX_STD_C::bitset<_Nb>& __b) const noexcept
1790 {
1791 const size_t __clength = (_Nb + __CHAR_BIT__ - 1) / __CHAR_BIT__;
1792 return std::_Hash_impl::hash(__b._M_getdata(), __clength);
1793 }
1794 };
1795
1796 template<>
1797 struct hash<_GLIBCXX_STD_C::bitset<0>>
1798 : public __hash_base<size_t, _GLIBCXX_STD_C::bitset<0>>
1799 {
1800 size_t
1801 operator()(const _GLIBCXX_STD_C::bitset<0>&) const noexcept
1802 { return 0; }
1803 };
1804
1805_GLIBCXX_END_NAMESPACE_VERSION
1806} // namespace
1807
1808#endif // C++11
1809
1810#if defined _GLIBCXX_DEBUG && _GLIBCXX_HOSTED
1811# include <debug/bitset>
1812#endif
1813
1814#endif /* _GLIBCXX_BITSET */
constexpr bitset< _Nb > & _Unchecked_reset(size_t __pos) noexcept
Definition bitset:1172
constexpr bitset< _Nb > & _Unchecked_flip(size_t __pos) noexcept
Definition bitset:1180
constexpr size_t _Find_first() const noexcept
Finds the index of the first "on" bit.
Definition bitset:1508
constexpr bool _Unchecked_test(size_t __pos) const noexcept
Definition bitset:1187
constexpr bitset< _Nb > & _Unchecked_set(size_t __pos) noexcept
Definition bitset:1153
constexpr bitset< _Nb > & _Unchecked_set(size_t __pos, int __val) noexcept
Definition bitset:1161
constexpr size_t _Find_next(size_t __prev) const noexcept
Finds the index of the next "on" bit after prev.
Definition bitset:1520
constexpr const _Tp & min(const _Tp &, const _Tp &)
This does what you think it does.
const _Facet & use_facet(const locale &__loc)
Return a facet.
ISO C++ entities toplevel namespace is std.
constexpr bitset< _Nb > operator^(const bitset< _Nb > &__x, const bitset< _Nb > &__y) noexcept
Global bitwise operations on bitsets.
Definition bitset:1639
std::basic_istream< _CharT, _Traits > & operator>>(std::basic_istream< _CharT, _Traits > &__is, bitset< _Nb > &__x)
Global I/O operators for bitsets.
Definition bitset:1659
std::basic_ostream< _CharT, _Traits > & operator<<(std::basic_ostream< _CharT, _Traits > &__os, const bitset< _Nb > &__x)
Global I/O operators for bitsets.
Definition bitset:1755
constexpr bitset< _Nb > operator|(const bitset< _Nb > &__x, const bitset< _Nb > &__y) noexcept
Global bitwise operations on bitsets.
Definition bitset:1629
constexpr bitset< _Nb > operator&(const bitset< _Nb > &__x, const bitset< _Nb > &__y) noexcept
Global bitwise operations on bitsets.
Definition bitset:1619
The bitset class represents a fixed-size sequence of bits.
Definition bitset:823
constexpr reference operator[](size_t __position)
Array-indexing support.
Definition bitset:1293
constexpr bitset< _Nb > & operator>>=(size_t __position) noexcept
Definition bitset:1135
constexpr bitset< _Nb > & flip() noexcept
Toggles every bit to its opposite value.
Definition bitset:1250
constexpr bitset< _Nb > & operator&=(const bitset< _Nb > &__rhs) noexcept
Definition bitset:1089
constexpr bitset(const _CharT *__str, typename __bitset::__string< _CharT >::size_type __n=__bitset::__string< _CharT >::npos, _CharT __zero=_CharT('0'), _CharT __one=_CharT('1'))
Definition bitset:1063
constexpr bitset< _Nb > operator>>(size_t __position) const noexcept
Self-explanatory.
Definition bitset:1496
constexpr unsigned long to_ulong() const
Returns a numerical interpretation of the bitset.
Definition bitset:1323
constexpr bool any() const noexcept
Tests whether any of the bits are on.
Definition bitset:1475
constexpr bitset() noexcept
All bits set to zero.
Definition bitset:947
constexpr bitset< _Nb > & operator^=(const bitset< _Nb > &__rhs) noexcept
Definition bitset:1105
constexpr bool test(size_t __position) const
Tests the value of a bit.
Definition bitset:1452
constexpr bitset< _Nb > operator~() const noexcept
See the no-argument flip().
Definition bitset:1273
constexpr bitset< _Nb > & operator|=(const bitset< _Nb > &__rhs) noexcept
Definition bitset:1097
constexpr bitset< _Nb > & set(size_t __position, bool __val=true)
Sets a given bit to a particular value.
Definition bitset:1213
constexpr size_t count() const noexcept
Returns the number of bits which are set.
Definition bitset:1421
constexpr bitset< _Nb > & operator<<=(size_t __position) noexcept
Definition bitset:1121
constexpr std::basic_string< _CharT, _Traits, _Alloc > to_string() const
Returns a character interpretation of the bitset.
Definition bitset:1345
constexpr bitset< _Nb > & reset() noexcept
Sets every bit to false.
Definition bitset:1224
constexpr bitset< _Nb > & set() noexcept
Sets every bit to true.
Definition bitset:1198
constexpr bitset(const std::basic_string< _CharT, _Traits, _Alloc > &__s, size_t __position=0)
Definition bitset:973
constexpr bitset(const std::basic_string< _CharT, _Traits, _Alloc > &__s, size_t __position, size_t __n)
Definition bitset:994
constexpr bitset(unsigned long long __val) noexcept
Initial bits bitwise-copied from a single word (others set to zero).
Definition bitset:952
constexpr size_t size() const noexcept
Returns the total number of bits.
Definition bitset:1426
constexpr bool all() const noexcept
Tests whether all the bits are on.
Definition bitset:1466
constexpr bitset< _Nb > & reset(size_t __position)
Sets a given bit to false.
Definition bitset:1239
constexpr bool none() const noexcept
Tests whether any of the bits are on.
Definition bitset:1484
constexpr bool operator==(const bitset< _Nb > &__rhs) const noexcept
These comparisons for equality/inequality are, well, bitwise.
Definition bitset:1433
constexpr bool operator[](size_t __position) const
Array-indexing support.
Definition bitset:1300
constexpr bitset< _Nb > & flip(size_t __position)
Toggles a given bit to its opposite value.
Definition bitset:1264
Template class basic_istream.
Definition istream:73
Primary class template hash.
is_standard_layout
Definition type_traits:1006
is_trivially_default_constructible
Definition type_traits:1462
void setstate(iostate __state)
Sets additional flags in the error state.
Definition basic_ios.h:167
char_type widen(char __c) const
Widens characters.
Definition basic_ios.h:465
basic_streambuf< _CharT, _Traits > * rdbuf() const
Accessing the underlying buffer.
Definition basic_ios.h:338
Managing sequences of characters and character-like objects.
constexpr basic_string & assign(const basic_string &__str)
Set value to contents of another string.
static const size_type npos
Value returned by various member functions when they fail.
Thrown as part of forced unwinding.
locale getloc() const
Locale access.
Definition ios_base.h:841
Template class basic_ostream.
Definition ostream.h:72