]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/include/std/bitset
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / include / std / bitset
CommitLineData
54c1bf78 1// <bitset> -*- C++ -*-
de96ac46 2
a945c346 3// Copyright (C) 2001-2024 Free Software Foundation, Inc.
de96ac46
BK
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
748086b7 8// Free Software Foundation; either version 3, or (at your option)
de96ac46
BK
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
748086b7
JJ
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.
de96ac46 19
748086b7
JJ
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/>.
de96ac46 24
54c1bf78
BK
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.
b963aad8 36 */
54c1bf78 37
143c27b0 38/** @file include/bitset
0aa06b18 39 * This is a Standard C++ Library header.
2f9d51b8
PE
40 */
41
1143680e
SE
42#ifndef _GLIBCXX_BITSET
43#define _GLIBCXX_BITSET 1
54c1bf78
BK
44
45#pragma GCC system_header
46
285b36d6 47#include <bits/functexcept.h> // For invalid_argument, out_of_range,
b963aad8 48 // overflow_error
57707f38
AA
49#include <bits/stl_algobase.h> // For std::fill
50
9194c139
JW
51#if _GLIBCXX_HOSTED
52# include <string>
53# include <iosfwd>
54# include <bits/cxxabi_forced.h>
54fd7d81
AA
55#endif
56
57#if __cplusplus >= 201103L
58# include <bits/functional_hash.h>
5dfb5e5b
FD
59#endif
60
083b7f28
AA
61#define __glibcxx_want_constexpr_bitset
62#include <bits/version.h>
63
5da7fa30 64#define _GLIBCXX_BITSET_BITS_PER_WORD (__CHAR_BIT__ * __SIZEOF_LONG__)
3d7c150e 65#define _GLIBCXX_BITSET_WORDS(__n) \
fc99f780
LH
66 ((__n) / _GLIBCXX_BITSET_BITS_PER_WORD + \
67 ((__n) % _GLIBCXX_BITSET_BITS_PER_WORD == 0 ? 0 : 1))
54c1bf78 68
5da7fa30
PC
69#define _GLIBCXX_BITSET_BITS_PER_ULL (__CHAR_BIT__ * __SIZEOF_LONG_LONG__)
70
12ffa228
BK
71namespace std _GLIBCXX_VISIBILITY(default)
72{
73_GLIBCXX_BEGIN_NAMESPACE_CONTAINER
3cbc7af0 74
b963aad8 75 /**
28dac70a 76 * Base class, general case. It is a class invariant that _Nw will be
b963aad8
PE
77 * nonnegative.
78 *
79 * See documentation for bitset.
b963aad8 80 */
1cb7f91f 81 template<size_t _Nw>
b963aad8 82 struct _Base_bitset
1cb7f91f
BK
83 {
84 typedef unsigned long _WordT;
85
b963aad8
PE
86 /// 0 is the least significant word.
87 _WordT _M_w[_Nw];
88
5d861bf2 89 _GLIBCXX_CONSTEXPR _Base_bitset() _GLIBCXX_NOEXCEPT
b6710d1a 90 : _M_w() { }
94a86be0 91
734f5023 92#if __cplusplus >= 201103L
5d861bf2 93 constexpr _Base_bitset(unsigned long long __val) noexcept
9f1163b1 94 : _M_w{ _WordT(__val)
94a86be0 95#if __SIZEOF_LONG_LONG__ > __SIZEOF_LONG__
7350a361 96 , _WordT(__val >> _GLIBCXX_BITSET_BITS_PER_WORD)
94a86be0 97#endif
9f1163b1 98 } { }
94a86be0 99#else
b963aad8 100 _Base_bitset(unsigned long __val)
b6710d1a
PC
101 : _M_w()
102 { _M_w[0] = __val; }
94a86be0 103#endif
54c1bf78 104
94a86be0 105 static _GLIBCXX_CONSTEXPR size_t
6aa67e7b 106 _S_whichword(size_t __pos) _GLIBCXX_NOEXCEPT
3d7c150e 107 { return __pos / _GLIBCXX_BITSET_BITS_PER_WORD; }
54c1bf78 108
94a86be0 109 static _GLIBCXX_CONSTEXPR size_t
6aa67e7b 110 _S_whichbyte(size_t __pos) _GLIBCXX_NOEXCEPT
3d7c150e 111 { return (__pos % _GLIBCXX_BITSET_BITS_PER_WORD) / __CHAR_BIT__; }
54c1bf78 112
94a86be0 113 static _GLIBCXX_CONSTEXPR size_t
6aa67e7b 114 _S_whichbit(size_t __pos) _GLIBCXX_NOEXCEPT
3d7c150e 115 { return __pos % _GLIBCXX_BITSET_BITS_PER_WORD; }
54c1bf78 116
94a86be0 117 static _GLIBCXX_CONSTEXPR _WordT
6aa67e7b 118 _S_maskbit(size_t __pos) _GLIBCXX_NOEXCEPT
1cb7f91f 119 { return (static_cast<_WordT>(1)) << _S_whichbit(__pos); }
54c1bf78 120
9194c139 121 _GLIBCXX14_CONSTEXPR _WordT&
5d861bf2 122 _M_getword(size_t __pos) _GLIBCXX_NOEXCEPT
1cb7f91f 123 { return _M_w[_S_whichword(__pos)]; }
54c1bf78 124
07be6120 125 _GLIBCXX_CONSTEXPR _WordT
5d861bf2 126 _M_getword(size_t __pos) const _GLIBCXX_NOEXCEPT
1cb7f91f 127 { return _M_w[_S_whichword(__pos)]; }
54c1bf78 128
734f5023 129#if __cplusplus >= 201103L
9194c139 130 constexpr const _WordT*
5d861bf2 131 _M_getdata() const noexcept
055f6a47 132 { return _M_w; }
ec7058d6
PC
133#endif
134
9194c139 135 _GLIBCXX23_CONSTEXPR _WordT&
5d861bf2 136 _M_hiword() _GLIBCXX_NOEXCEPT
5c33bb62 137 { return _M_w[_Nw - 1]; }
54c1bf78 138
94a86be0 139 _GLIBCXX_CONSTEXPR _WordT
5d861bf2 140 _M_hiword() const _GLIBCXX_NOEXCEPT
5c33bb62 141 { return _M_w[_Nw - 1]; }
54c1bf78 142
9194c139 143 _GLIBCXX23_CONSTEXPR void
5d861bf2 144 _M_do_and(const _Base_bitset<_Nw>& __x) _GLIBCXX_NOEXCEPT
1cb7f91f 145 {
b963aad8 146 for (size_t __i = 0; __i < _Nw; __i++)
1cb7f91f
BK
147 _M_w[__i] &= __x._M_w[__i];
148 }
54c1bf78 149
9194c139 150 _GLIBCXX14_CONSTEXPR void
5d861bf2 151 _M_do_or(const _Base_bitset<_Nw>& __x) _GLIBCXX_NOEXCEPT
1cb7f91f 152 {
b963aad8 153 for (size_t __i = 0; __i < _Nw; __i++)
1cb7f91f
BK
154 _M_w[__i] |= __x._M_w[__i];
155 }
54c1bf78 156
9194c139 157 _GLIBCXX14_CONSTEXPR void
5d861bf2 158 _M_do_xor(const _Base_bitset<_Nw>& __x) _GLIBCXX_NOEXCEPT
1cb7f91f
BK
159 {
160 for (size_t __i = 0; __i < _Nw; __i++)
161 _M_w[__i] ^= __x._M_w[__i];
162 }
54c1bf78 163
9194c139 164 _GLIBCXX14_CONSTEXPR void
5d861bf2 165 _M_do_left_shift(size_t __shift) _GLIBCXX_NOEXCEPT;
1cb7f91f 166
9194c139 167 _GLIBCXX14_CONSTEXPR void
5d861bf2 168 _M_do_right_shift(size_t __shift) _GLIBCXX_NOEXCEPT;
b963aad8 169
9194c139 170 _GLIBCXX14_CONSTEXPR void
5d861bf2 171 _M_do_flip() _GLIBCXX_NOEXCEPT
1cb7f91f 172 {
b963aad8 173 for (size_t __i = 0; __i < _Nw; __i++)
1cb7f91f
BK
174 _M_w[__i] = ~_M_w[__i];
175 }
54c1bf78 176
9194c139 177 _GLIBCXX14_CONSTEXPR void
5d861bf2 178 _M_do_set() _GLIBCXX_NOEXCEPT
1cb7f91f
BK
179 {
180 for (size_t __i = 0; __i < _Nw; __i++)
181 _M_w[__i] = ~static_cast<_WordT>(0);
182 }
54c1bf78 183
9194c139 184 _GLIBCXX14_CONSTEXPR void
5d861bf2 185 _M_do_reset() _GLIBCXX_NOEXCEPT
9194c139 186 {
6904ed80 187#if __cplusplus >= 201402L
9194c139
JW
188 if (__builtin_is_constant_evaluated())
189 {
190 for (_WordT& __w : _M_w)
191 __w = 0;
192 return;
193 }
6904ed80 194#endif
9194c139
JW
195 __builtin_memset(_M_w, 0, _Nw * sizeof(_WordT));
196 }
197
198 _GLIBCXX14_CONSTEXPR bool
5d861bf2 199 _M_is_equal(const _Base_bitset<_Nw>& __x) const _GLIBCXX_NOEXCEPT
1cb7f91f 200 {
b963aad8 201 for (size_t __i = 0; __i < _Nw; ++__i)
b96817da
PC
202 if (_M_w[__i] != __x._M_w[__i])
203 return false;
1cb7f91f
BK
204 return true;
205 }
54c1bf78 206
0217ac04 207 template<size_t _Nb>
9194c139 208 _GLIBCXX14_CONSTEXPR bool
0217ac04
PC
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 }
b96817da 218
9194c139 219 _GLIBCXX14_CONSTEXPR bool
5d861bf2 220 _M_is_any() const _GLIBCXX_NOEXCEPT
1cb7f91f 221 {
b963aad8 222 for (size_t __i = 0; __i < _Nw; __i++)
b96817da
PC
223 if (_M_w[__i] != static_cast<_WordT>(0))
224 return true;
1cb7f91f
BK
225 return false;
226 }
54c1bf78 227
9194c139 228 _GLIBCXX14_CONSTEXPR size_t
5d861bf2 229 _M_do_count() const _GLIBCXX_NOEXCEPT
1cb7f91f
BK
230 {
231 size_t __result = 0;
348b0c31
FH
232 for (size_t __i = 0; __i < _Nw; __i++)
233 __result += __builtin_popcountl(_M_w[__i]);
1cb7f91f
BK
234 return __result;
235 }
54c1bf78 236
9194c139 237 _GLIBCXX14_CONSTEXPR unsigned long
b963aad8 238 _M_do_to_ulong() const;
1cb7f91f 239
734f5023 240#if __cplusplus >= 201103L
9194c139 241 _GLIBCXX14_CONSTEXPR unsigned long long
700d2899
PC
242 _M_do_to_ullong() const;
243#endif
244
1cb7f91f 245 // find first "on" bit
9194c139 246 _GLIBCXX14_CONSTEXPR size_t
07be6120 247 _M_do_find_first(size_t) const _GLIBCXX_NOEXCEPT;
1cb7f91f
BK
248
249 // find the next "on" bit that follows "prev"
9194c139 250 _GLIBCXX14_CONSTEXPR size_t
07be6120 251 _M_do_find_next(size_t, size_t) const _GLIBCXX_NOEXCEPT;
1cb7f91f
BK
252 };
253
254 // Definitions of non-inline functions from _Base_bitset.
255 template<size_t _Nw>
9194c139 256 _GLIBCXX14_CONSTEXPR void
5d861bf2 257 _Base_bitset<_Nw>::_M_do_left_shift(size_t __shift) _GLIBCXX_NOEXCEPT
1cb7f91f 258 {
3bbfb3d9 259 if (__builtin_expect(__shift != 0, 1))
1cb7f91f 260 {
3d7c150e
BK
261 const size_t __wshift = __shift / _GLIBCXX_BITSET_BITS_PER_WORD;
262 const size_t __offset = __shift % _GLIBCXX_BITSET_BITS_PER_WORD;
b963aad8 263
1cb7f91f
BK
264 if (__offset == 0)
265 for (size_t __n = _Nw - 1; __n >= __wshift; --__n)
266 _M_w[__n] = _M_w[__n - __wshift];
b963aad8 267 else
1cb7f91f 268 {
33ac58d5 269 const size_t __sub_offset = (_GLIBCXX_BITSET_BITS_PER_WORD
5c33bb62 270 - __offset);
1cb7f91f 271 for (size_t __n = _Nw - 1; __n > __wshift; --__n)
5c33bb62
PC
272 _M_w[__n] = ((_M_w[__n - __wshift] << __offset)
273 | (_M_w[__n - __wshift - 1] >> __sub_offset));
1cb7f91f
BK
274 _M_w[__wshift] = _M_w[0] << __offset;
275 }
b963aad8 276
a8cad3e1 277 std::fill(_M_w + 0, _M_w + __wshift, static_cast<_WordT>(0));
1cb7f91f 278 }
54c1bf78 279 }
b963aad8 280
1cb7f91f 281 template<size_t _Nw>
9194c139 282 _GLIBCXX14_CONSTEXPR void
5d861bf2 283 _Base_bitset<_Nw>::_M_do_right_shift(size_t __shift) _GLIBCXX_NOEXCEPT
1cb7f91f 284 {
3bbfb3d9 285 if (__builtin_expect(__shift != 0, 1))
1cb7f91f 286 {
3d7c150e
BK
287 const size_t __wshift = __shift / _GLIBCXX_BITSET_BITS_PER_WORD;
288 const size_t __offset = __shift % _GLIBCXX_BITSET_BITS_PER_WORD;
1cb7f91f 289 const size_t __limit = _Nw - __wshift - 1;
b963aad8 290
1cb7f91f
BK
291 if (__offset == 0)
292 for (size_t __n = 0; __n <= __limit; ++__n)
293 _M_w[__n] = _M_w[__n + __wshift];
b963aad8 294 else
1cb7f91f 295 {
5c33bb62
PC
296 const size_t __sub_offset = (_GLIBCXX_BITSET_BITS_PER_WORD
297 - __offset);
1cb7f91f 298 for (size_t __n = 0; __n < __limit; ++__n)
5c33bb62
PC
299 _M_w[__n] = ((_M_w[__n + __wshift] >> __offset)
300 | (_M_w[__n + __wshift + 1] << __sub_offset));
1cb7f91f
BK
301 _M_w[__limit] = _M_w[_Nw-1] >> __offset;
302 }
33ac58d5 303
a8cad3e1 304 std::fill(_M_w + __limit + 1, _M_w + _Nw, static_cast<_WordT>(0));
1cb7f91f 305 }
54c1bf78 306 }
54c1bf78 307
1cb7f91f 308 template<size_t _Nw>
9194c139 309 _GLIBCXX14_CONSTEXPR unsigned long
1cb7f91f
BK
310 _Base_bitset<_Nw>::_M_do_to_ulong() const
311 {
b963aad8
PE
312 for (size_t __i = 1; __i < _Nw; ++__i)
313 if (_M_w[__i])
988ad90d 314 __throw_overflow_error(__N("_Base_bitset::_M_do_to_ulong"));
1cb7f91f 315 return _M_w[0];
54c1bf78 316 }
54c1bf78 317
734f5023 318#if __cplusplus >= 201103L
700d2899 319 template<size_t _Nw>
9194c139 320 _GLIBCXX14_CONSTEXPR unsigned long long
700d2899
PC
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
1cb7f91f 335 template<size_t _Nw>
9194c139 336 _GLIBCXX14_CONSTEXPR size_t
5d861bf2
PC
337 _Base_bitset<_Nw>::
338 _M_do_find_first(size_t __not_found) const _GLIBCXX_NOEXCEPT
1cb7f91f 339 {
348b0c31 340 for (size_t __i = 0; __i < _Nw; __i++)
1cb7f91f
BK
341 {
342 _WordT __thisword = _M_w[__i];
348b0c31 343 if (__thisword != static_cast<_WordT>(0))
5c33bb62
PC
344 return (__i * _GLIBCXX_BITSET_BITS_PER_WORD
345 + __builtin_ctzl(__thisword));
1cb7f91f
BK
346 }
347 // not found, so return an indication of failure.
348 return __not_found;
54c1bf78
BK
349 }
350
1cb7f91f 351 template<size_t _Nw>
9194c139 352 _GLIBCXX14_CONSTEXPR size_t
5d861bf2
PC
353 _Base_bitset<_Nw>::
354 _M_do_find_next(size_t __prev, size_t __not_found) const _GLIBCXX_NOEXCEPT
b963aad8 355 {
1cb7f91f
BK
356 // make bound inclusive
357 ++__prev;
b963aad8 358
1cb7f91f 359 // check out of bounds
394ef95e 360 if (__prev >= _Nw * _GLIBCXX_BITSET_BITS_PER_WORD)
1cb7f91f 361 return __not_found;
b963aad8 362
1cb7f91f
BK
363 // search first word
364 size_t __i = _S_whichword(__prev);
365 _WordT __thisword = _M_w[__i];
b963aad8 366
1cb7f91f 367 // mask off bits below bound
394ef95e 368 __thisword &= (~static_cast<_WordT>(0)) << _S_whichbit(__prev);
b963aad8 369
348b0c31 370 if (__thisword != static_cast<_WordT>(0))
5c33bb62
PC
371 return (__i * _GLIBCXX_BITSET_BITS_PER_WORD
372 + __builtin_ctzl(__thisword));
b963aad8 373
1cb7f91f
BK
374 // check subsequent words
375 __i++;
5c33bb62 376 for (; __i < _Nw; __i++)
1cb7f91f
BK
377 {
378 __thisword = _M_w[__i];
348b0c31 379 if (__thisword != static_cast<_WordT>(0))
5c33bb62
PC
380 return (__i * _GLIBCXX_BITSET_BITS_PER_WORD
381 + __builtin_ctzl(__thisword));
1cb7f91f
BK
382 }
383 // not found, so return an indication of failure.
384 return __not_found;
385 } // end _M_do_find_next
54c1bf78 386
b963aad8 387 /**
b963aad8
PE
388 * Base class, specialization for a single word.
389 *
390 * See documentation for bitset.
b963aad8
PE
391 */
392 template<>
393 struct _Base_bitset<1>
1cb7f91f
BK
394 {
395 typedef unsigned long _WordT;
396 _WordT _M_w;
54c1bf78 397
5d861bf2 398 _GLIBCXX_CONSTEXPR _Base_bitset() _GLIBCXX_NOEXCEPT
5c33bb62 399 : _M_w(0)
5a4db26d 400 { }
5c33bb62 401
734f5023 402#if __cplusplus >= 201103L
5d861bf2 403 constexpr _Base_bitset(unsigned long long __val) noexcept
0d6f2a80 404#else
5c33bb62 405 _Base_bitset(unsigned long __val)
0d6f2a80 406#endif
5c33bb62 407 : _M_w(__val)
5a4db26d 408 { }
54c1bf78 409
94a86be0 410 static _GLIBCXX_CONSTEXPR size_t
6aa67e7b 411 _S_whichword(size_t __pos) _GLIBCXX_NOEXCEPT
3d7c150e 412 { return __pos / _GLIBCXX_BITSET_BITS_PER_WORD; }
54c1bf78 413
94a86be0 414 static _GLIBCXX_CONSTEXPR size_t
6aa67e7b 415 _S_whichbyte(size_t __pos) _GLIBCXX_NOEXCEPT
3d7c150e 416 { return (__pos % _GLIBCXX_BITSET_BITS_PER_WORD) / __CHAR_BIT__; }
54c1bf78 417
94a86be0 418 static _GLIBCXX_CONSTEXPR size_t
6aa67e7b 419 _S_whichbit(size_t __pos) _GLIBCXX_NOEXCEPT
3d7c150e 420 { return __pos % _GLIBCXX_BITSET_BITS_PER_WORD; }
54c1bf78 421
94a86be0 422 static _GLIBCXX_CONSTEXPR _WordT
6aa67e7b 423 _S_maskbit(size_t __pos) _GLIBCXX_NOEXCEPT
1cb7f91f 424 { return (static_cast<_WordT>(1)) << _S_whichbit(__pos); }
54c1bf78 425
9194c139 426 _GLIBCXX14_CONSTEXPR _WordT&
5d861bf2 427 _M_getword(size_t) _GLIBCXX_NOEXCEPT
5c33bb62 428 { return _M_w; }
54c1bf78 429
07be6120 430 _GLIBCXX_CONSTEXPR _WordT
5d861bf2 431 _M_getword(size_t) const _GLIBCXX_NOEXCEPT
5c33bb62 432 { return _M_w; }
54c1bf78 433
734f5023 434#if __cplusplus >= 201103L
9194c139 435 constexpr const _WordT*
5d861bf2 436 _M_getdata() const noexcept
055f6a47 437 { return &_M_w; }
ec7058d6
PC
438#endif
439
9194c139 440 _GLIBCXX14_CONSTEXPR _WordT&
5d861bf2 441 _M_hiword() _GLIBCXX_NOEXCEPT
5c33bb62 442 { return _M_w; }
54c1bf78 443
94a86be0 444 _GLIBCXX_CONSTEXPR _WordT
5d861bf2 445 _M_hiword() const _GLIBCXX_NOEXCEPT
5c33bb62 446 { return _M_w; }
54c1bf78 447
9194c139 448 _GLIBCXX14_CONSTEXPR void
5d861bf2 449 _M_do_and(const _Base_bitset<1>& __x) _GLIBCXX_NOEXCEPT
5c33bb62 450 { _M_w &= __x._M_w; }
54c1bf78 451
9194c139 452 _GLIBCXX14_CONSTEXPR void
5d861bf2 453 _M_do_or(const _Base_bitset<1>& __x) _GLIBCXX_NOEXCEPT
5c33bb62 454 { _M_w |= __x._M_w; }
54c1bf78 455
9194c139 456 _GLIBCXX14_CONSTEXPR void
5d861bf2 457 _M_do_xor(const _Base_bitset<1>& __x) _GLIBCXX_NOEXCEPT
5c33bb62 458 { _M_w ^= __x._M_w; }
54c1bf78 459
9194c139 460 _GLIBCXX14_CONSTEXPR void
5d861bf2 461 _M_do_left_shift(size_t __shift) _GLIBCXX_NOEXCEPT
5c33bb62 462 { _M_w <<= __shift; }
54c1bf78 463
9194c139 464 _GLIBCXX14_CONSTEXPR void
5d861bf2 465 _M_do_right_shift(size_t __shift) _GLIBCXX_NOEXCEPT
5c33bb62 466 { _M_w >>= __shift; }
54c1bf78 467
9194c139 468 _GLIBCXX14_CONSTEXPR void
5d861bf2 469 _M_do_flip() _GLIBCXX_NOEXCEPT
5c33bb62 470 { _M_w = ~_M_w; }
54c1bf78 471
9194c139 472 _GLIBCXX14_CONSTEXPR void
5d861bf2 473 _M_do_set() _GLIBCXX_NOEXCEPT
5c33bb62 474 { _M_w = ~static_cast<_WordT>(0); }
54c1bf78 475
9194c139 476 _GLIBCXX14_CONSTEXPR void
5d861bf2 477 _M_do_reset() _GLIBCXX_NOEXCEPT
5c33bb62 478 { _M_w = 0; }
54c1bf78 479
9194c139 480 _GLIBCXX14_CONSTEXPR bool
5d861bf2 481 _M_is_equal(const _Base_bitset<1>& __x) const _GLIBCXX_NOEXCEPT
1cb7f91f 482 { return _M_w == __x._M_w; }
54c1bf78 483
0217ac04 484 template<size_t _Nb>
9194c139 485 _GLIBCXX14_CONSTEXPR bool
0217ac04
PC
486 _M_are_all() const _GLIBCXX_NOEXCEPT
487 { return _M_w == (~static_cast<_WordT>(0)
488 >> (_GLIBCXX_BITSET_BITS_PER_WORD - _Nb)); }
b96817da 489
9194c139 490 _GLIBCXX14_CONSTEXPR bool
5d861bf2 491 _M_is_any() const _GLIBCXX_NOEXCEPT
5c33bb62 492 { return _M_w != 0; }
54c1bf78 493
9194c139 494 _GLIBCXX14_CONSTEXPR size_t
5d861bf2 495 _M_do_count() const _GLIBCXX_NOEXCEPT
5c33bb62 496 { return __builtin_popcountl(_M_w); }
54c1bf78 497
9194c139 498 _GLIBCXX14_CONSTEXPR unsigned long
5d861bf2 499 _M_do_to_ulong() const _GLIBCXX_NOEXCEPT
5c33bb62 500 { return _M_w; }
1cb7f91f 501
734f5023 502#if __cplusplus >= 201103L
9194c139 503 constexpr unsigned long long
5d861bf2 504 _M_do_to_ullong() const noexcept
700d2899
PC
505 { return _M_w; }
506#endif
507
9194c139 508 _GLIBCXX14_CONSTEXPR size_t
5d861bf2 509 _M_do_find_first(size_t __not_found) const _GLIBCXX_NOEXCEPT
348b0c31
FH
510 {
511 if (_M_w != 0)
512 return __builtin_ctzl(_M_w);
513 else
514 return __not_found;
515 }
1cb7f91f
BK
516
517 // find the next "on" bit that follows "prev"
9194c139 518 _GLIBCXX14_CONSTEXPR size_t
348b0c31 519 _M_do_find_next(size_t __prev, size_t __not_found) const
5d861bf2 520 _GLIBCXX_NOEXCEPT
348b0c31
FH
521 {
522 ++__prev;
3d7c150e 523 if (__prev >= ((size_t) _GLIBCXX_BITSET_BITS_PER_WORD))
348b0c31
FH
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 }
1cb7f91f
BK
532 };
533
bb12c809 534 /**
bb12c809
PE
535 * Base class, specialization for no storage (zero-length %bitset).
536 *
537 * See documentation for bitset.
bb12c809
PE
538 */
539 template<>
540 struct _Base_bitset<0>
541 {
542 typedef unsigned long _WordT;
543
5d861bf2 544 _GLIBCXX_CONSTEXPR _Base_bitset() _GLIBCXX_NOEXCEPT
5a4db26d 545 { }
5c33bb62 546
734f5023 547#if __cplusplus >= 201103L
5d861bf2 548 constexpr _Base_bitset(unsigned long long) noexcept
0d6f2a80 549#else
5c33bb62 550 _Base_bitset(unsigned long)
0d6f2a80 551#endif
5a4db26d 552 { }
bb12c809 553
94a86be0 554 static _GLIBCXX_CONSTEXPR size_t
6aa67e7b 555 _S_whichword(size_t __pos) _GLIBCXX_NOEXCEPT
3d7c150e 556 { return __pos / _GLIBCXX_BITSET_BITS_PER_WORD; }
bb12c809 557
94a86be0 558 static _GLIBCXX_CONSTEXPR size_t
6aa67e7b 559 _S_whichbyte(size_t __pos) _GLIBCXX_NOEXCEPT
3d7c150e 560 { return (__pos % _GLIBCXX_BITSET_BITS_PER_WORD) / __CHAR_BIT__; }
bb12c809 561
94a86be0 562 static _GLIBCXX_CONSTEXPR size_t
6aa67e7b 563 _S_whichbit(size_t __pos) _GLIBCXX_NOEXCEPT
3d7c150e 564 { return __pos % _GLIBCXX_BITSET_BITS_PER_WORD; }
bb12c809 565
94a86be0 566 static _GLIBCXX_CONSTEXPR _WordT
6aa67e7b 567 _S_maskbit(size_t __pos) _GLIBCXX_NOEXCEPT
bb12c809
PE
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,
9194c139
JW
572 // but this must fail if the user calls _Unchecked_set directly.
573 // Let's not penalize zero-length users unless they actually
bb12c809
PE
574 // make an unchecked call; all the memory ugliness is therefore
575 // localized to this single should-never-get-this-far function.
9194c139 576 __attribute__((__noreturn__))
bb12c809 577 _WordT&
5d861bf2 578 _M_getword(size_t) _GLIBCXX_NOEXCEPT
9194c139 579 { __throw_out_of_range(__N("_Base_bitset::_M_getword")); }
bb12c809 580
07be6120 581 _GLIBCXX_CONSTEXPR _WordT
9ed83a33 582 _M_getword(size_t) const _GLIBCXX_NOEXCEPT
94a86be0
BK
583 { return 0; }
584
585 _GLIBCXX_CONSTEXPR _WordT
5d861bf2 586 _M_hiword() const _GLIBCXX_NOEXCEPT
5c33bb62 587 { return 0; }
bb12c809 588
9194c139 589 _GLIBCXX14_CONSTEXPR void
5d861bf2 590 _M_do_and(const _Base_bitset<0>&) _GLIBCXX_NOEXCEPT
5a4db26d 591 { }
bb12c809 592
9194c139 593 _GLIBCXX14_CONSTEXPR void
5d861bf2 594 _M_do_or(const _Base_bitset<0>&) _GLIBCXX_NOEXCEPT
5a4db26d 595 { }
bb12c809 596
9194c139 597 _GLIBCXX14_CONSTEXPR void
5d861bf2 598 _M_do_xor(const _Base_bitset<0>&) _GLIBCXX_NOEXCEPT
5a4db26d 599 { }
bb12c809 600
9194c139 601 _GLIBCXX14_CONSTEXPR void
5d861bf2 602 _M_do_left_shift(size_t) _GLIBCXX_NOEXCEPT
5a4db26d 603 { }
bb12c809 604
9194c139 605 _GLIBCXX14_CONSTEXPR void
5d861bf2 606 _M_do_right_shift(size_t) _GLIBCXX_NOEXCEPT
5a4db26d 607 { }
bb12c809 608
9194c139 609 _GLIBCXX14_CONSTEXPR void
5d861bf2 610 _M_do_flip() _GLIBCXX_NOEXCEPT
5a4db26d 611 { }
bb12c809 612
9194c139 613 _GLIBCXX14_CONSTEXPR void
5d861bf2 614 _M_do_set() _GLIBCXX_NOEXCEPT
5a4db26d 615 { }
bb12c809 616
9194c139 617 _GLIBCXX14_CONSTEXPR void
5d861bf2 618 _M_do_reset() _GLIBCXX_NOEXCEPT
5a4db26d 619 { }
bb12c809
PE
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?
9194c139 624 _GLIBCXX_CONSTEXPR bool
5d861bf2 625 _M_is_equal(const _Base_bitset<0>&) const _GLIBCXX_NOEXCEPT
5c33bb62 626 { return true; }
bb12c809 627
0217ac04 628 template<size_t _Nb>
9194c139 629 _GLIBCXX_CONSTEXPR bool
0217ac04
PC
630 _M_are_all() const _GLIBCXX_NOEXCEPT
631 { return true; }
b96817da 632
9194c139 633 _GLIBCXX_CONSTEXPR bool
5d861bf2 634 _M_is_any() const _GLIBCXX_NOEXCEPT
5c33bb62 635 { return false; }
bb12c809 636
9194c139 637 _GLIBCXX_CONSTEXPR size_t
5d861bf2 638 _M_do_count() const _GLIBCXX_NOEXCEPT
5c33bb62 639 { return 0; }
bb12c809 640
9194c139 641 _GLIBCXX_CONSTEXPR unsigned long
5d861bf2 642 _M_do_to_ulong() const _GLIBCXX_NOEXCEPT
5c33bb62 643 { return 0; }
bb12c809 644
734f5023 645#if __cplusplus >= 201103L
9194c139 646 constexpr unsigned long long
5d861bf2 647 _M_do_to_ullong() const noexcept
700d2899
PC
648 { return 0; }
649#endif
650
bb12c809
PE
651 // Normally "not found" is the size, but that could also be
652 // misinterpreted as an index in this corner case. Oh well.
9194c139 653 _GLIBCXX_CONSTEXPR size_t
5d861bf2 654 _M_do_find_first(size_t) const _GLIBCXX_NOEXCEPT
5c33bb62 655 { return 0; }
bb12c809 656
9194c139 657 _GLIBCXX_CONSTEXPR size_t
5d861bf2 658 _M_do_find_next(size_t, size_t) const _GLIBCXX_NOEXCEPT
5c33bb62 659 { return 0; }
bb12c809
PE
660 };
661
662
1cb7f91f 663 // Helper class to zero out the unused high-order bits in the highest word.
b963aad8
PE
664 template<size_t _Extrabits>
665 struct _Sanitize
1cb7f91f 666 {
94a86be0
BK
667 typedef unsigned long _WordT;
668
9194c139 669 static _GLIBCXX14_CONSTEXPR void
5d861bf2 670 _S_do_sanitize(_WordT& __val) _GLIBCXX_NOEXCEPT
94a86be0 671 { __val &= ~((~static_cast<_WordT>(0)) << _Extrabits); }
1cb7f91f
BK
672 };
673
b963aad8
PE
674 template<>
675 struct _Sanitize<0>
6aa67e7b 676 {
94a86be0
BK
677 typedef unsigned long _WordT;
678
9194c139 679 static _GLIBCXX14_CONSTEXPR void
33ac58d5 680 _S_do_sanitize(_WordT) _GLIBCXX_NOEXCEPT { }
94a86be0 681 };
70e12fb9 682
734f5023 683#if __cplusplus >= 201103L
6b4f8906 684 template<size_t _Nb, bool = (_Nb < _GLIBCXX_BITSET_BITS_PER_ULL)>
5da7fa30
PC
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 };
7f38b5c4
JW
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
5da7fa30 731
b963aad8 732 /**
fb3bc977
JW
733 * @brief The %bitset class represents a @e fixed-size sequence of bits.
734 * @ingroup utilities
b963aad8 735 *
e92a4045
PE
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.)
b963aad8 738 *
70e12fb9
PE
739 * The template argument, @a Nb, may be any non-negative number,
740 * specifying the number of bits (e.g., "0", "12", "1024*1024").
b963aad8 741 *
70e12fb9
PE
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.
b963aad8 747 *
2a60a9f6
BK
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.
b963aad8
PE
755 *
756 * This behavior is preserved when translating to and from strings. For
757 * example, the first line of the following program probably prints
2a60a9f6 758 * <em>b(&apos;a&apos;) is 0001100001</em> on a modern ASCII system.
b963aad8
PE
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 *
a40fff0e 782 * Also see:
10d43d2f 783 * https://gcc.gnu.org/onlinedocs/libstdc++/manual/ext_containers.html
70e12fb9 784 * for a description of extensions.
b963aad8 785 *
b963aad8
PE
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.
b963aad8 794 */
1cb7f91f 795 template<size_t _Nb>
5c33bb62
PC
796 class bitset
797 : private _Base_bitset<_GLIBCXX_BITSET_WORDS(_Nb)>
1cb7f91f 798 {
5c33bb62
PC
799 private:
800 typedef _Base_bitset<_GLIBCXX_BITSET_WORDS(_Nb)> _Base;
801 typedef unsigned long _WordT;
54c1bf78 802
9194c139 803#if _GLIBCXX_HOSTED
9779c871 804 template<class _CharT, class _Traits, class _Alloc>
9194c139 805 _GLIBCXX23_CONSTEXPR
9779c871
PP
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 }
9194c139 816#endif // HOSTED
9779c871 817
9194c139 818 _GLIBCXX23_CONSTEXPR
9779c871
PP
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
9194c139 827 _GLIBCXX23_CONSTEXPR
5c33bb62 828 void
5d861bf2 829 _M_do_sanitize() _GLIBCXX_NOEXCEPT
33ac58d5 830 {
94a86be0
BK
831 typedef _Sanitize<_Nb % _GLIBCXX_BITSET_BITS_PER_WORD> __sanitize_type;
832 __sanitize_type::_S_do_sanitize(this->_M_hiword());
833 }
b963aad8 834
734f5023 835#if __cplusplus >= 201103L
5dfb5e5b 836 friend struct std::hash<bitset>;
ec7058d6
PC
837#endif
838
5c33bb62
PC
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 *
2a60a9f6
BK
849 * (On a typical system, this <em>bit %reference</em> is 64
850 * times the size of an actual bit. Ha.)
5c33bb62
PC
851 */
852 class reference
853 {
854 friend class bitset;
855
94a86be0
BK
856 _WordT* _M_wp;
857 size_t _M_bpos;
33ac58d5 858
5c33bb62
PC
859 // left undefined
860 reference();
33ac58d5 861
5c33bb62 862 public:
9194c139 863 _GLIBCXX23_CONSTEXPR
5d861bf2 864 reference(bitset& __b, size_t __pos) _GLIBCXX_NOEXCEPT
5c33bb62
PC
865 {
866 _M_wp = &__b._M_getword(__pos);
867 _M_bpos = _Base::_S_whichbit(__pos);
868 }
b963aad8 869
a1417556
JW
870#if __cplusplus >= 201103L
871 reference(const reference&) = default;
872#endif
873
9194c139
JW
874#if __cplusplus > 202002L && __cpp_constexpr_dynamic_alloc
875 constexpr
876#endif
5d861bf2 877 ~reference() _GLIBCXX_NOEXCEPT
5c33bb62 878 { }
b963aad8 879
5c33bb62 880 // For b[i] = __x;
9194c139 881 _GLIBCXX23_CONSTEXPR
5c33bb62 882 reference&
5d861bf2 883 operator=(bool __x) _GLIBCXX_NOEXCEPT
5c33bb62
PC
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];
9194c139 893 _GLIBCXX23_CONSTEXPR
5c33bb62 894 reference&
5d861bf2 895 operator=(const reference& __j) _GLIBCXX_NOEXCEPT
5c33bb62
PC
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
9194c139 905 _GLIBCXX23_CONSTEXPR
5c33bb62 906 bool
5d861bf2 907 operator~() const _GLIBCXX_NOEXCEPT
5c33bb62
PC
908 { return (*(_M_wp) & _Base::_S_maskbit(_M_bpos)) == 0; }
909
910 // For __x = b[i];
9194c139 911 _GLIBCXX23_CONSTEXPR
5d861bf2 912 operator bool() const _GLIBCXX_NOEXCEPT
5c33bb62
PC
913 { return (*(_M_wp) & _Base::_S_maskbit(_M_bpos)) != 0; }
914
915 // For b[i].flip();
9194c139 916 _GLIBCXX23_CONSTEXPR
5c33bb62 917 reference&
5d861bf2 918 flip() _GLIBCXX_NOEXCEPT
5c33bb62
PC
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.
5d861bf2 928 _GLIBCXX_CONSTEXPR bitset() _GLIBCXX_NOEXCEPT
5c33bb62
PC
929 { }
930
931 /// Initial bits bitwise-copied from a single word (others set to zero).
734f5023 932#if __cplusplus >= 201103L
5d861bf2 933 constexpr bitset(unsigned long long __val) noexcept
5da7fa30 934 : _Base(_Sanitize_val<_Nb>::_S_do_sanitize_val(__val)) { }
0d6f2a80 935#else
5c33bb62
PC
936 bitset(unsigned long __val)
937 : _Base(__val)
938 { _M_do_sanitize(); }
94a86be0 939#endif
5c33bb62 940
9194c139 941#if _GLIBCXX_HOSTED
5c33bb62 942 /**
7897a1c0 943 * Use a subset of a string.
93c66bc6
BK
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;
5c33bb62 946 * defaults to zero.
93c66bc6 947 * @throw std::out_of_range If @a pos is bigger the size of @a __s.
5c33bb62 948 * @throw std::invalid_argument If a character appears in the string
2a60a9f6 949 * which is neither @a 0 nor @a 1.
5c33bb62
PC
950 */
951 template<class _CharT, class _Traits, class _Alloc>
9194c139 952 _GLIBCXX23_CONSTEXPR
5c33bb62 953 explicit
6323b34e 954 bitset(const std::basic_string<_CharT, _Traits, _Alloc>& __s,
5c33bb62
PC
955 size_t __position = 0)
956 : _Base()
957 {
9779c871 958 _M_check_initial_position(__s, __position);
5c33bb62 959 _M_copy_from_string(__s, __position,
47cd1557
PC
960 std::basic_string<_CharT, _Traits, _Alloc>::npos,
961 _CharT('0'), _CharT('1'));
5c33bb62
PC
962 }
963
964 /**
7897a1c0 965 * Use a subset of a string.
93c66bc6
BK
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.
5c33bb62 971 * @throw std::invalid_argument If a character appears in the string
2a60a9f6 972 * which is neither @a 0 nor @a 1.
5c33bb62
PC
973 */
974 template<class _CharT, class _Traits, class _Alloc>
9194c139 975 _GLIBCXX23_CONSTEXPR
6323b34e 976 bitset(const std::basic_string<_CharT, _Traits, _Alloc>& __s,
5c33bb62
PC
977 size_t __position, size_t __n)
978 : _Base()
979 {
9779c871 980 _M_check_initial_position(__s, __position);
47cd1557
PC
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>
9194c139 987 _GLIBCXX23_CONSTEXPR
47cd1557
PC
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 {
9779c871 993 _M_check_initial_position(__s, __position);
47cd1557 994 _M_copy_from_string(__s, __position, __n, __zero, __one);
5c33bb62 995 }
7f38b5c4 996#endif // HOSTED
0fda18dd 997
734f5023 998#if __cplusplus >= 201103L
a1b418cb 999 /**
7897a1c0 1000 * Construct from a character %array.
93c66bc6
BK
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.
a1b418cb 1007 */
a0a2a399 1008 template<typename _CharT>
9194c139
JW
1009 [[__gnu__::__nonnull__]]
1010 _GLIBCXX23_CONSTEXPR
a0a2a399 1011 explicit
7f38b5c4
JW
1012 bitset(const _CharT* __str,
1013 typename __bitset::__string<_CharT>::size_type __n
1014 = __bitset::__string<_CharT>::npos,
a0a2a399
PC
1015 _CharT __zero = _CharT('0'), _CharT __one = _CharT('1'))
1016 : _Base()
1017 {
7f38b5c4 1018#if _GLIBCXX_HOSTED
a0a2a399
PC
1019 if (!__str)
1020 __throw_logic_error(__N("bitset::bitset(const _CharT*, ...)"));
7f38b5c4
JW
1021#endif
1022 using _Traits = typename __bitset::__string<_CharT>::traits_type;
a0a2a399 1023
7f38b5c4
JW
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);
a0a2a399 1027 }
9194c139 1028#endif // C++11
a1b418cb 1029
5c33bb62 1030 // 23.3.5.2 bitset operations:
f0b88346 1031 ///@{
5c33bb62 1032 /**
7897a1c0 1033 * Operations on bitsets.
93c66bc6 1034 * @param __rhs A same-sized bitset.
5c33bb62
PC
1035 *
1036 * These should be self-explanatory.
1037 */
9194c139 1038 _GLIBCXX23_CONSTEXPR
5c33bb62 1039 bitset<_Nb>&
5d861bf2 1040 operator&=(const bitset<_Nb>& __rhs) _GLIBCXX_NOEXCEPT
1cb7f91f 1041 {
5c33bb62
PC
1042 this->_M_do_and(__rhs);
1043 return *this;
1cb7f91f 1044 }
54c1bf78 1045
9194c139 1046 _GLIBCXX23_CONSTEXPR
5c33bb62 1047 bitset<_Nb>&
5d861bf2 1048 operator|=(const bitset<_Nb>& __rhs) _GLIBCXX_NOEXCEPT
5c33bb62
PC
1049 {
1050 this->_M_do_or(__rhs);
1051 return *this;
1052 }
1cb7f91f 1053
9194c139 1054 _GLIBCXX23_CONSTEXPR
5c33bb62 1055 bitset<_Nb>&
5d861bf2 1056 operator^=(const bitset<_Nb>& __rhs) _GLIBCXX_NOEXCEPT
1cb7f91f 1057 {
5c33bb62
PC
1058 this->_M_do_xor(__rhs);
1059 return *this;
1060 }
f0b88346 1061 ///@}
33ac58d5 1062
f0b88346 1063 ///@{
5c33bb62 1064 /**
7897a1c0 1065 * Operations on bitsets.
93c66bc6 1066 * @param __position The number of places to shift.
5c33bb62
PC
1067 *
1068 * These should be self-explanatory.
1069 */
9194c139 1070 _GLIBCXX23_CONSTEXPR
5c33bb62 1071 bitset<_Nb>&
5d861bf2 1072 operator<<=(size_t __position) _GLIBCXX_NOEXCEPT
5c33bb62
PC
1073 {
1074 if (__builtin_expect(__position < _Nb, 1))
1075 {
1076 this->_M_do_left_shift(__position);
1077 this->_M_do_sanitize();
1078 }
1cb7f91f 1079 else
5c33bb62 1080 this->_M_do_reset();
1cb7f91f
BK
1081 return *this;
1082 }
b963aad8 1083
9194c139 1084 _GLIBCXX23_CONSTEXPR
5c33bb62 1085 bitset<_Nb>&
5d861bf2 1086 operator>>=(size_t __position) _GLIBCXX_NOEXCEPT
1cb7f91f 1087 {
5c33bb62
PC
1088 if (__builtin_expect(__position < _Nb, 1))
1089 {
1090 this->_M_do_right_shift(__position);
1091 this->_M_do_sanitize();
1092 }
1cb7f91f 1093 else
5c33bb62 1094 this->_M_do_reset();
1cb7f91f
BK
1095 return *this;
1096 }
f0b88346 1097 ///@}
33ac58d5 1098
f0b88346 1099 ///@{
5c33bb62
PC
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 */
9194c139 1105 _GLIBCXX23_CONSTEXPR
5c33bb62 1106 bitset<_Nb>&
5d861bf2 1107 _Unchecked_set(size_t __pos) _GLIBCXX_NOEXCEPT
1cb7f91f 1108 {
5c33bb62 1109 this->_M_getword(__pos) |= _Base::_S_maskbit(__pos);
1cb7f91f
BK
1110 return *this;
1111 }
5c33bb62 1112
9194c139 1113 _GLIBCXX23_CONSTEXPR
5c33bb62 1114 bitset<_Nb>&
5d861bf2 1115 _Unchecked_set(size_t __pos, int __val) _GLIBCXX_NOEXCEPT
1cb7f91f 1116 {
5c33bb62
PC
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;
1cb7f91f 1122 }
54c1bf78 1123
9194c139 1124 _GLIBCXX23_CONSTEXPR
5c33bb62 1125 bitset<_Nb>&
5d861bf2 1126 _Unchecked_reset(size_t __pos) _GLIBCXX_NOEXCEPT
1cb7f91f 1127 {
5c33bb62
PC
1128 this->_M_getword(__pos) &= ~_Base::_S_maskbit(__pos);
1129 return *this;
1cb7f91f 1130 }
54c1bf78 1131
9194c139 1132 _GLIBCXX23_CONSTEXPR
5c33bb62 1133 bitset<_Nb>&
5d861bf2 1134 _Unchecked_flip(size_t __pos) _GLIBCXX_NOEXCEPT
5c33bb62
PC
1135 {
1136 this->_M_getword(__pos) ^= _Base::_S_maskbit(__pos);
1137 return *this;
1138 }
54c1bf78 1139
07be6120 1140 _GLIBCXX_CONSTEXPR bool
5d861bf2 1141 _Unchecked_test(size_t __pos) const _GLIBCXX_NOEXCEPT
5c33bb62
PC
1142 { return ((this->_M_getword(__pos) & _Base::_S_maskbit(__pos))
1143 != static_cast<_WordT>(0)); }
f0b88346 1144 ///@}
33ac58d5 1145
5c33bb62
PC
1146 // Set, reset, and flip.
1147 /**
1148 * @brief Sets every bit to true.
1149 */
9194c139 1150 _GLIBCXX23_CONSTEXPR
5c33bb62 1151 bitset<_Nb>&
5d861bf2 1152 set() _GLIBCXX_NOEXCEPT
5c33bb62
PC
1153 {
1154 this->_M_do_set();
1155 this->_M_do_sanitize();
1156 return *this;
1157 }
54c1bf78 1158
5c33bb62
PC
1159 /**
1160 * @brief Sets a given bit to a particular value.
93c66bc6
BK
1161 * @param __position The index of the bit.
1162 * @param __val Either true or false, defaults to true.
5c33bb62
PC
1163 * @throw std::out_of_range If @a pos is bigger the size of the %set.
1164 */
9194c139 1165 _GLIBCXX23_CONSTEXPR
5c33bb62
PC
1166 bitset<_Nb>&
1167 set(size_t __position, bool __val = true)
1168 {
9779c871 1169 this->_M_check(__position, __N("bitset::set"));
5c33bb62
PC
1170 return _Unchecked_set(__position, __val);
1171 }
54c1bf78 1172
5c33bb62
PC
1173 /**
1174 * @brief Sets every bit to false.
1175 */
9194c139 1176 _GLIBCXX23_CONSTEXPR
5c33bb62 1177 bitset<_Nb>&
5d861bf2 1178 reset() _GLIBCXX_NOEXCEPT
5c33bb62 1179 {
3bbfb3d9 1180 this->_M_do_reset();
5c33bb62
PC
1181 return *this;
1182 }
54c1bf78 1183
5c33bb62
PC
1184 /**
1185 * @brief Sets a given bit to false.
93c66bc6 1186 * @param __position The index of the bit.
5c33bb62
PC
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 */
9194c139 1191 _GLIBCXX23_CONSTEXPR
5c33bb62
PC
1192 bitset<_Nb>&
1193 reset(size_t __position)
1194 {
9779c871 1195 this->_M_check(__position, __N("bitset::reset"));
5c33bb62
PC
1196 return _Unchecked_reset(__position);
1197 }
33ac58d5 1198
5c33bb62
PC
1199 /**
1200 * @brief Toggles every bit to its opposite value.
1201 */
9194c139 1202 _GLIBCXX23_CONSTEXPR
5c33bb62 1203 bitset<_Nb>&
5d861bf2 1204 flip() _GLIBCXX_NOEXCEPT
5c33bb62
PC
1205 {
1206 this->_M_do_flip();
1207 this->_M_do_sanitize();
1208 return *this;
1209 }
54c1bf78 1210
5c33bb62
PC
1211 /**
1212 * @brief Toggles a given bit to its opposite value.
93c66bc6 1213 * @param __position The index of the bit.
5c33bb62
PC
1214 * @throw std::out_of_range If @a pos is bigger the size of the %set.
1215 */
9194c139 1216 _GLIBCXX23_CONSTEXPR
5c33bb62
PC
1217 bitset<_Nb>&
1218 flip(size_t __position)
1219 {
9779c871 1220 this->_M_check(__position, __N("bitset::flip"));
5c33bb62
PC
1221 return _Unchecked_flip(__position);
1222 }
33ac58d5 1223
5c33bb62 1224 /// See the no-argument flip().
9194c139 1225 _GLIBCXX23_CONSTEXPR
5c33bb62 1226 bitset<_Nb>
5d861bf2 1227 operator~() const _GLIBCXX_NOEXCEPT
5c33bb62
PC
1228 { return bitset<_Nb>(*this).flip(); }
1229
f0b88346 1230 ///@{
5c33bb62
PC
1231 /**
1232 * @brief Array-indexing support.
93c66bc6 1233 * @param __position Index into the %bitset.
94a86be0
BK
1234 * @return A bool for a <em>const %bitset</em>. For non-const
1235 * bitsets, an instance of the reference proxy class.
5c33bb62
PC
1236 * @note These operators do no range checking and throw no exceptions,
1237 * as required by DR 11 to the standard.
1238 *
5c33bb62
PC
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
5c33bb62 1244 */
9194c139 1245 _GLIBCXX23_CONSTEXPR
5c33bb62
PC
1246 reference
1247 operator[](size_t __position)
94a86be0 1248 { return reference(*this, __position); }
54c1bf78 1249
07be6120 1250 _GLIBCXX_CONSTEXPR bool
5c33bb62
PC
1251 operator[](size_t __position) const
1252 { return _Unchecked_test(__position); }
f0b88346 1253 ///@}
33ac58d5 1254
5c33bb62 1255 /**
28dac70a 1256 * @brief Returns a numerical interpretation of the %bitset.
5c33bb62
PC
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 */
9194c139 1261 _GLIBCXX23_CONSTEXPR
5c33bb62
PC
1262 unsigned long
1263 to_ulong() const
1264 { return this->_M_do_to_ulong(); }
1265
734f5023 1266#if __cplusplus >= 201103L
9194c139 1267 _GLIBCXX23_CONSTEXPR
700d2899
PC
1268 unsigned long long
1269 to_ullong() const
1270 { return this->_M_do_to_ullong(); }
1271#endif
1272
9194c139 1273#if _GLIBCXX_HOSTED
5c33bb62 1274 /**
28dac70a 1275 * @brief Returns a character interpretation of the %bitset.
5c33bb62
PC
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).
5c33bb62
PC
1281 */
1282 template<class _CharT, class _Traits, class _Alloc>
9194c139 1283 _GLIBCXX23_CONSTEXPR
6323b34e 1284 std::basic_string<_CharT, _Traits, _Alloc>
5c33bb62
PC
1285 to_string() const
1286 {
6323b34e 1287 std::basic_string<_CharT, _Traits, _Alloc> __result;
47cd1557
PC
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>
9194c139 1295 _GLIBCXX23_CONSTEXPR
47cd1557
PC
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);
5c33bb62
PC
1301 return __result;
1302 }
54c1bf78 1303
14492f0b
PC
1304 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1305 // 434. bitset::to_string() hard to use.
1306 template<class _CharT, class _Traits>
9194c139 1307 _GLIBCXX23_CONSTEXPR
6323b34e 1308 std::basic_string<_CharT, _Traits, std::allocator<_CharT> >
14492f0b 1309 to_string() const
6323b34e 1310 { return to_string<_CharT, _Traits, std::allocator<_CharT> >(); }
14492f0b 1311
47cd1557 1312 // _GLIBCXX_RESOLVE_LIB_DEFECTS
19a6a2ea 1313 // 853. to_string needs updating with zero and one.
47cd1557 1314 template<class _CharT, class _Traits>
9194c139 1315 _GLIBCXX23_CONSTEXPR
47cd1557
PC
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
14492f0b 1321 template<class _CharT>
9194c139 1322 _GLIBCXX23_CONSTEXPR
6323b34e
PC
1323 std::basic_string<_CharT, std::char_traits<_CharT>,
1324 std::allocator<_CharT> >
14492f0b 1325 to_string() const
6323b34e
PC
1326 {
1327 return to_string<_CharT, std::char_traits<_CharT>,
1328 std::allocator<_CharT> >();
1329 }
14492f0b 1330
47cd1557 1331 template<class _CharT>
9194c139 1332 _GLIBCXX23_CONSTEXPR
47cd1557
PC
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
9194c139 1341 _GLIBCXX23_CONSTEXPR
6323b34e 1342 std::basic_string<char, std::char_traits<char>, std::allocator<char> >
14492f0b 1343 to_string() const
6323b34e
PC
1344 {
1345 return to_string<char, std::char_traits<char>,
1346 std::allocator<char> >();
1347 }
14492f0b 1348
9194c139 1349 _GLIBCXX23_CONSTEXPR
47cd1557
PC
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 }
9194c139 1356#endif // HOSTED
54c1bf78 1357
5c33bb62 1358 /// Returns the number of bits which are set.
9194c139 1359 _GLIBCXX23_CONSTEXPR
5c33bb62 1360 size_t
5d861bf2 1361 count() const _GLIBCXX_NOEXCEPT
5c33bb62 1362 { return this->_M_do_count(); }
54c1bf78 1363
5c33bb62 1364 /// Returns the total number of bits.
94a86be0 1365 _GLIBCXX_CONSTEXPR size_t
5d861bf2 1366 size() const _GLIBCXX_NOEXCEPT
5c33bb62 1367 { return _Nb; }
54c1bf78 1368
f0b88346 1369 ///@{
5c33bb62 1370 /// These comparisons for equality/inequality are, well, @e bitwise.
9194c139 1371 _GLIBCXX23_CONSTEXPR
5c33bb62 1372 bool
5d861bf2 1373 operator==(const bitset<_Nb>& __rhs) const _GLIBCXX_NOEXCEPT
5c33bb62 1374 { return this->_M_is_equal(__rhs); }
54c1bf78 1375
596676d6 1376#if __cpp_impl_three_way_comparison < 201907L
9194c139 1377 _GLIBCXX23_CONSTEXPR
5c33bb62 1378 bool
5d861bf2 1379 operator!=(const bitset<_Nb>& __rhs) const _GLIBCXX_NOEXCEPT
5c33bb62 1380 { return !this->_M_is_equal(__rhs); }
596676d6 1381#endif
f0b88346 1382 ///@}
33ac58d5 1383
5c33bb62
PC
1384 /**
1385 * @brief Tests the value of a bit.
93c66bc6 1386 * @param __position The index of a bit.
5c33bb62
PC
1387 * @return The value at @a pos.
1388 * @throw std::out_of_range If @a pos is bigger the size of the %set.
1389 */
9194c139 1390 _GLIBCXX23_CONSTEXPR
5c33bb62
PC
1391 bool
1392 test(size_t __position) const
1cb7f91f 1393 {
9779c871 1394 this->_M_check(__position, __N("bitset::test"));
5c33bb62 1395 return _Unchecked_test(__position);
1cb7f91f 1396 }
b96817da
PC
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 */
9194c139 1404 _GLIBCXX23_CONSTEXPR
b96817da 1405 bool
5d861bf2 1406 all() const _GLIBCXX_NOEXCEPT
0217ac04 1407 { return this->template _M_are_all<_Nb>(); }
b96817da 1408
5c33bb62
PC
1409 /**
1410 * @brief Tests whether any of the bits are on.
1411 * @return True if at least one bit is set.
1412 */
9194c139 1413 _GLIBCXX23_CONSTEXPR
5c33bb62 1414 bool
5d861bf2 1415 any() const _GLIBCXX_NOEXCEPT
5c33bb62 1416 { return this->_M_is_any(); }
54c1bf78 1417
5c33bb62
PC
1418 /**
1419 * @brief Tests whether any of the bits are on.
1420 * @return True if none of the bits are set.
1421 */
9194c139 1422 _GLIBCXX23_CONSTEXPR
5c33bb62 1423 bool
5d861bf2 1424 none() const _GLIBCXX_NOEXCEPT
5c33bb62
PC
1425 { return !this->_M_is_any(); }
1426
f0b88346 1427 ///@{
5c33bb62 1428 /// Self-explanatory.
9194c139 1429 _GLIBCXX23_CONSTEXPR
5c33bb62 1430 bitset<_Nb>
5d861bf2 1431 operator<<(size_t __position) const _GLIBCXX_NOEXCEPT
5c33bb62
PC
1432 { return bitset<_Nb>(*this) <<= __position; }
1433
9194c139 1434 _GLIBCXX23_CONSTEXPR
5c33bb62 1435 bitset<_Nb>
5d861bf2 1436 operator>>(size_t __position) const _GLIBCXX_NOEXCEPT
5c33bb62 1437 { return bitset<_Nb>(*this) >>= __position; }
f0b88346 1438 ///@}
33ac58d5 1439
5c33bb62
PC
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 */
9194c139 1446 _GLIBCXX23_CONSTEXPR
5c33bb62 1447 size_t
5d861bf2 1448 _Find_first() const _GLIBCXX_NOEXCEPT
5c33bb62
PC
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.
93c66bc6 1454 * @param __prev Where to start searching.
5c33bb62
PC
1455 * @ingroup SGIextensions
1456 * @sa _Find_first
1457 */
9194c139 1458 _GLIBCXX23_CONSTEXPR
5c33bb62 1459 size_t
6aa67e7b 1460 _Find_next(size_t __prev) const _GLIBCXX_NOEXCEPT
5c33bb62 1461 { return this->_M_do_find_next(__prev, _Nb); }
4eb46f45
JW
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
5c33bb62 1495 };
54c1bf78 1496
1cb7f91f
BK
1497 // Definitions of non-inline member functions.
1498 template<size_t _Nb>
47cd1557 1499 template<class _CharT, class _Traits>
9194c139 1500 _GLIBCXX23_CONSTEXPR
5c33bb62 1501 void
e7c59a0e 1502 bitset<_Nb>::
0fda18dd 1503 _M_copy_from_ptr(const _CharT* __s, size_t __len,
47cd1557 1504 size_t __pos, size_t __n, _CharT __zero, _CharT __one)
5c33bb62
PC
1505 {
1506 reset();
58651854 1507 const size_t __nbits = std::min(_Nb, std::min(__n, size_t(__len - __pos)));
e7c59a0e 1508 for (size_t __i = __nbits; __i > 0; --__i)
5c33bb62 1509 {
47cd1557
PC
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
7d818988 1516 __throw_invalid_argument(__N("bitset::_M_copy_from_ptr"));
5c33bb62
PC
1517 }
1518 }
54c1bf78 1519
7f38b5c4 1520#if _GLIBCXX_HOSTED
1cb7f91f
BK
1521 template<size_t _Nb>
1522 template<class _CharT, class _Traits, class _Alloc>
9194c139 1523 _GLIBCXX23_CONSTEXPR
5c33bb62 1524 void
e7c59a0e 1525 bitset<_Nb>::
47cd1557
PC
1526 _M_copy_to_string(std::basic_string<_CharT, _Traits, _Alloc>& __s,
1527 _CharT __zero, _CharT __one) const
5c33bb62 1528 {
47cd1557 1529 __s.assign(_Nb, __zero);
ffb03fa1
JW
1530 size_t __n = this->_Find_first();
1531 while (__n < _Nb)
1532 {
1533 __s[_Nb - __n - 1] = __one;
1534 __n = _Find_next(__n);
1535 }
5c33bb62 1536 }
9194c139 1537#endif // HOSTED
54c1bf78 1538
1cb7f91f 1539 // 23.3.5.3 bitset operations:
f0b88346 1540 ///@{
b963aad8
PE
1541 /**
1542 * @brief Global bitwise operations on bitsets.
93c66bc6
BK
1543 * @param __x A bitset.
1544 * @param __y A bitset of the same size as @a __x.
b963aad8
PE
1545 * @return A new bitset.
1546 *
1547 * These should be self-explanatory.
1548 */
1cb7f91f 1549 template<size_t _Nb>
9194c139 1550 _GLIBCXX23_CONSTEXPR
b963aad8 1551 inline bitset<_Nb>
5d861bf2 1552 operator&(const bitset<_Nb>& __x, const bitset<_Nb>& __y) _GLIBCXX_NOEXCEPT
1cb7f91f
BK
1553 {
1554 bitset<_Nb> __result(__x);
1555 __result &= __y;
1556 return __result;
54c1bf78
BK
1557 }
1558
1cb7f91f 1559 template<size_t _Nb>
9194c139 1560 _GLIBCXX23_CONSTEXPR
b963aad8 1561 inline bitset<_Nb>
5d861bf2 1562 operator|(const bitset<_Nb>& __x, const bitset<_Nb>& __y) _GLIBCXX_NOEXCEPT
1cb7f91f
BK
1563 {
1564 bitset<_Nb> __result(__x);
1565 __result |= __y;
1566 return __result;
1567 }
54c1bf78 1568
1cb7f91f 1569 template <size_t _Nb>
9194c139 1570 _GLIBCXX23_CONSTEXPR
b963aad8 1571 inline bitset<_Nb>
5d861bf2 1572 operator^(const bitset<_Nb>& __x, const bitset<_Nb>& __y) _GLIBCXX_NOEXCEPT
1cb7f91f
BK
1573 {
1574 bitset<_Nb> __result(__x);
1575 __result ^= __y;
1576 return __result;
1577 }
f0b88346 1578 ///@}
b963aad8 1579
9194c139 1580#if _GLIBCXX_HOSTED
f0b88346 1581 ///@{
b963aad8
PE
1582 /**
1583 * @brief Global I/O operators for bitsets.
1584 *
1585 * Direct I/O between streams and bitsets is supported. Output is
2a60a9f6 1586 * straightforward. Input will skip whitespace, only accept @a 0 and @a 1
b963aad8
PE
1587 * characters, and will only extract as many digits as the %bitset will
1588 * hold.
1589 */
1cb7f91f 1590 template<class _CharT, class _Traits, size_t _Nb>
6323b34e
PC
1591 std::basic_istream<_CharT, _Traits>&
1592 operator>>(std::basic_istream<_CharT, _Traits>& __is, bitset<_Nb>& __x)
1cb7f91f 1593 {
f4e39278
PC
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
1c12a3cf
JW
1598 struct _Buffer
1599 {
553332c1
JW
1600 static _GLIBCXX_CONSTEXPR bool _S_use_alloca() { return _Nb <= 256; }
1601
1602 explicit _Buffer(_CharT* __p) : _M_ptr(__p) { }
1c12a3cf
JW
1603
1604 ~_Buffer()
1605 {
553332c1
JW
1606 if _GLIBCXX17_CONSTEXPR (!_S_use_alloca())
1607 delete[] _M_ptr;
1c12a3cf
JW
1608 }
1609
553332c1 1610 _CharT* const _M_ptr;
1c12a3cf 1611 };
553332c1
JW
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);
b963aad8 1618
47cd1557
PC
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
f4e39278
PC
1624 typename __ios_base::iostate __state = __ios_base::goodbit;
1625 typename __istream_type::sentry __sentry(__is);
b963aad8 1626 if (__sentry)
1cb7f91f 1627 {
bc2631e0 1628 __try
1cb7f91f 1629 {
e7c59a0e 1630 for (size_t __i = _Nb; __i > 0; --__i)
1cb7f91f 1631 {
7f1156ed 1632 static typename _Traits::int_type __eof = _Traits::eof();
33ac58d5 1633
11202768 1634 typename _Traits::int_type __c1 = __is.rdbuf()->sbumpc();
7f1156ed 1635 if (_Traits::eq_int_type(__c1, __eof))
1cb7f91f 1636 {
f4e39278 1637 __state |= __ios_base::eofbit;
1cb7f91f
BK
1638 break;
1639 }
7f1156ed
PC
1640 else
1641 {
5c33bb62 1642 const char_type __c2 = _Traits::to_char_type(__c1);
47cd1557 1643 if (_Traits::eq(__c2, __zero))
1c12a3cf 1644 *__ptr++ = __zero;
47cd1557 1645 else if (_Traits::eq(__c2, __one))
1c12a3cf 1646 *__ptr++ = __one;
11202768
PC
1647 else if (_Traits::
1648 eq_int_type(__is.rdbuf()->sputbackc(__c2),
1649 __eof))
7f1156ed 1650 {
f4e39278 1651 __state |= __ios_base::failbit;
7f1156ed
PC
1652 break;
1653 }
1654 }
1cb7f91f
BK
1655 }
1656 }
bc2631e0 1657 __catch(__cxxabiv1::__forced_unwind&)
d05f74f1 1658 {
33ac58d5 1659 __is._M_setstate(__ios_base::badbit);
d05f74f1
JM
1660 __throw_exception_again;
1661 }
bc2631e0 1662 __catch(...)
f4e39278 1663 { __is._M_setstate(__ios_base::badbit); }
1cb7f91f
BK
1664 }
1665
1c12a3cf
JW
1666 if _GLIBCXX17_CONSTEXPR (_Nb)
1667 {
553332c1
JW
1668 if (size_t __len = __ptr - __buf._M_ptr)
1669 __x.template _M_copy_from_ptr<_CharT, _Traits>(__buf._M_ptr, __len,
1c12a3cf
JW
1670 0, __len,
1671 __zero, __one);
1672 else
1673 __state |= __ios_base::failbit;
1674 }
7f1156ed
PC
1675 if (__state)
1676 __is.setstate(__state);
1cb7f91f
BK
1677 return __is;
1678 }
54c1bf78 1679
1cb7f91f 1680 template <class _CharT, class _Traits, size_t _Nb>
6323b34e
PC
1681 std::basic_ostream<_CharT, _Traits>&
1682 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1683 const bitset<_Nb>& __x)
1cb7f91f 1684 {
6323b34e 1685 std::basic_string<_CharT, _Traits> __tmp;
47cd1557
PC
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'));
1cb7f91f
BK
1691 return __os << __tmp;
1692 }
f0b88346 1693 ///@}
9194c139 1694#endif // HOSTED
3cbc7af0 1695
12ffa228
BK
1696_GLIBCXX_END_NAMESPACE_CONTAINER
1697} // namespace std
54c1bf78 1698
3d7c150e
BK
1699#undef _GLIBCXX_BITSET_WORDS
1700#undef _GLIBCXX_BITSET_BITS_PER_WORD
5da7fa30 1701#undef _GLIBCXX_BITSET_BITS_PER_ULL
54c1bf78 1702
54fd7d81 1703#if __cplusplus >= 201103L
4cd533a7 1704
12ffa228
BK
1705namespace std _GLIBCXX_VISIBILITY(default)
1706{
1707_GLIBCXX_BEGIN_NAMESPACE_VERSION
4cd533a7 1708
ec7058d6
PC
1709 // DR 1182.
1710 /// std::hash specialization for bitset.
1711 template<size_t _Nb>
12ffa228
BK
1712 struct hash<_GLIBCXX_STD_C::bitset<_Nb>>
1713 : public __hash_base<size_t, _GLIBCXX_STD_C::bitset<_Nb>>
ec7058d6
PC
1714 {
1715 size_t
72f1c34b 1716 operator()(const _GLIBCXX_STD_C::bitset<_Nb>& __b) const noexcept
ec7058d6 1717 {
055f6a47 1718 const size_t __clength = (_Nb + __CHAR_BIT__ - 1) / __CHAR_BIT__;
e7f72940 1719 return std::_Hash_impl::hash(__b._M_getdata(), __clength);
ec7058d6
PC
1720 }
1721 };
4cd533a7
PC
1722
1723 template<>
12ffa228
BK
1724 struct hash<_GLIBCXX_STD_C::bitset<0>>
1725 : public __hash_base<size_t, _GLIBCXX_STD_C::bitset<0>>
4cd533a7
PC
1726 {
1727 size_t
72f1c34b 1728 operator()(const _GLIBCXX_STD_C::bitset<0>&) const noexcept
4cd533a7
PC
1729 { return 0; }
1730 };
1731
12ffa228
BK
1732_GLIBCXX_END_NAMESPACE_VERSION
1733} // namespace
4cd533a7 1734
734f5023 1735#endif // C++11
ec7058d6 1736
6904ed80 1737#if defined _GLIBCXX_DEBUG && _GLIBCXX_HOSTED
285b36d6
BK
1738# include <debug/bitset>
1739#endif
1740
1143680e 1741#endif /* _GLIBCXX_BITSET */