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