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