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