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