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