00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043 #ifndef _GLIBCXX_BITSET
00044 #define _GLIBCXX_BITSET 1
00045
00046 #pragma GCC system_header
00047
00048 #include <cstddef>
00049 #include <string>
00050 #include <bits/functexcept.h>
00051
00052 #include <iosfwd>
00053 #include <cxxabi-forced.h>
00054
00055 #define _GLIBCXX_BITSET_BITS_PER_WORD (__CHAR_BIT__ * sizeof(unsigned long))
00056 #define _GLIBCXX_BITSET_WORDS(__n) \
00057 ((__n) < 1 ? 0 : ((__n) + _GLIBCXX_BITSET_BITS_PER_WORD - 1) \
00058 / _GLIBCXX_BITSET_BITS_PER_WORD)
00059
00060 _GLIBCXX_BEGIN_NESTED_NAMESPACE(std, _GLIBCXX_STD_D)
00061
00062
00063
00064
00065
00066
00067
00068 template<size_t _Nw>
00069 struct _Base_bitset
00070 {
00071 typedef unsigned long _WordT;
00072
00073
00074 _WordT _M_w[_Nw];
00075
00076 _Base_bitset()
00077 { _M_do_reset(); }
00078
00079 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00080 _Base_bitset(unsigned long long __val)
00081 #else
00082 _Base_bitset(unsigned long __val)
00083 #endif
00084 {
00085 _M_do_reset();
00086 _M_w[0] = __val;
00087 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00088 if (sizeof(unsigned long long) > sizeof(unsigned long))
00089 _M_w[1] = __val >> _GLIBCXX_BITSET_BITS_PER_WORD;
00090 #endif
00091 }
00092
00093 static size_t
00094 _S_whichword(size_t __pos )
00095 { return __pos / _GLIBCXX_BITSET_BITS_PER_WORD; }
00096
00097 static size_t
00098 _S_whichbyte(size_t __pos )
00099 { return (__pos % _GLIBCXX_BITSET_BITS_PER_WORD) / __CHAR_BIT__; }
00100
00101 static size_t
00102 _S_whichbit(size_t __pos )
00103 { return __pos % _GLIBCXX_BITSET_BITS_PER_WORD; }
00104
00105 static _WordT
00106 _S_maskbit(size_t __pos )
00107 { return (static_cast<_WordT>(1)) << _S_whichbit(__pos); }
00108
00109 _WordT&
00110 _M_getword(size_t __pos)
00111 { return _M_w[_S_whichword(__pos)]; }
00112
00113 _WordT
00114 _M_getword(size_t __pos) const
00115 { return _M_w[_S_whichword(__pos)]; }
00116
00117 _WordT&
00118 _M_hiword()
00119 { return _M_w[_Nw - 1]; }
00120
00121 _WordT
00122 _M_hiword() const
00123 { return _M_w[_Nw - 1]; }
00124
00125 void
00126 _M_do_and(const _Base_bitset<_Nw>& __x)
00127 {
00128 for (size_t __i = 0; __i < _Nw; __i++)
00129 _M_w[__i] &= __x._M_w[__i];
00130 }
00131
00132 void
00133 _M_do_or(const _Base_bitset<_Nw>& __x)
00134 {
00135 for (size_t __i = 0; __i < _Nw; __i++)
00136 _M_w[__i] |= __x._M_w[__i];
00137 }
00138
00139 void
00140 _M_do_xor(const _Base_bitset<_Nw>& __x)
00141 {
00142 for (size_t __i = 0; __i < _Nw; __i++)
00143 _M_w[__i] ^= __x._M_w[__i];
00144 }
00145
00146 void
00147 _M_do_left_shift(size_t __shift);
00148
00149 void
00150 _M_do_right_shift(size_t __shift);
00151
00152 void
00153 _M_do_flip()
00154 {
00155 for (size_t __i = 0; __i < _Nw; __i++)
00156 _M_w[__i] = ~_M_w[__i];
00157 }
00158
00159 void
00160 _M_do_set()
00161 {
00162 for (size_t __i = 0; __i < _Nw; __i++)
00163 _M_w[__i] = ~static_cast<_WordT>(0);
00164 }
00165
00166 void
00167 _M_do_reset()
00168 { __builtin_memset(_M_w, 0, _Nw * sizeof(_WordT)); }
00169
00170 bool
00171 _M_is_equal(const _Base_bitset<_Nw>& __x) const
00172 {
00173 for (size_t __i = 0; __i < _Nw; ++__i)
00174 if (_M_w[__i] != __x._M_w[__i])
00175 return false;
00176 return true;
00177 }
00178
00179 size_t
00180 _M_are_all_aux() const
00181 {
00182 for (size_t __i = 0; __i < _Nw - 1; __i++)
00183 if (_M_w[__i] != ~static_cast<_WordT>(0))
00184 return 0;
00185 return ((_Nw - 1) * _GLIBCXX_BITSET_BITS_PER_WORD
00186 + __builtin_popcountl(_M_hiword()));
00187 }
00188
00189 bool
00190 _M_is_any() const
00191 {
00192 for (size_t __i = 0; __i < _Nw; __i++)
00193 if (_M_w[__i] != static_cast<_WordT>(0))
00194 return true;
00195 return false;
00196 }
00197
00198 size_t
00199 _M_do_count() const
00200 {
00201 size_t __result = 0;
00202 for (size_t __i = 0; __i < _Nw; __i++)
00203 __result += __builtin_popcountl(_M_w[__i]);
00204 return __result;
00205 }
00206
00207 unsigned long
00208 _M_do_to_ulong() const;
00209
00210 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00211 unsigned long long
00212 _M_do_to_ullong() const;
00213 #endif
00214
00215
00216 size_t
00217 _M_do_find_first(size_t __not_found) const;
00218
00219
00220 size_t
00221 _M_do_find_next(size_t __prev, size_t __not_found) const;
00222 };
00223
00224
00225 template<size_t _Nw>
00226 void
00227 _Base_bitset<_Nw>::_M_do_left_shift(size_t __shift)
00228 {
00229 if (__builtin_expect(__shift != 0, 1))
00230 {
00231 const size_t __wshift = __shift / _GLIBCXX_BITSET_BITS_PER_WORD;
00232 const size_t __offset = __shift % _GLIBCXX_BITSET_BITS_PER_WORD;
00233
00234 if (__offset == 0)
00235 for (size_t __n = _Nw - 1; __n >= __wshift; --__n)
00236 _M_w[__n] = _M_w[__n - __wshift];
00237 else
00238 {
00239 const size_t __sub_offset = (_GLIBCXX_BITSET_BITS_PER_WORD
00240 - __offset);
00241 for (size_t __n = _Nw - 1; __n > __wshift; --__n)
00242 _M_w[__n] = ((_M_w[__n - __wshift] << __offset)
00243 | (_M_w[__n - __wshift - 1] >> __sub_offset));
00244 _M_w[__wshift] = _M_w[0] << __offset;
00245 }
00246
00247 std::fill(_M_w + 0, _M_w + __wshift, static_cast<_WordT>(0));
00248 }
00249 }
00250
00251 template<size_t _Nw>
00252 void
00253 _Base_bitset<_Nw>::_M_do_right_shift(size_t __shift)
00254 {
00255 if (__builtin_expect(__shift != 0, 1))
00256 {
00257 const size_t __wshift = __shift / _GLIBCXX_BITSET_BITS_PER_WORD;
00258 const size_t __offset = __shift % _GLIBCXX_BITSET_BITS_PER_WORD;
00259 const size_t __limit = _Nw - __wshift - 1;
00260
00261 if (__offset == 0)
00262 for (size_t __n = 0; __n <= __limit; ++__n)
00263 _M_w[__n] = _M_w[__n + __wshift];
00264 else
00265 {
00266 const size_t __sub_offset = (_GLIBCXX_BITSET_BITS_PER_WORD
00267 - __offset);
00268 for (size_t __n = 0; __n < __limit; ++__n)
00269 _M_w[__n] = ((_M_w[__n + __wshift] >> __offset)
00270 | (_M_w[__n + __wshift + 1] << __sub_offset));
00271 _M_w[__limit] = _M_w[_Nw-1] >> __offset;
00272 }
00273
00274 std::fill(_M_w + __limit + 1, _M_w + _Nw, static_cast<_WordT>(0));
00275 }
00276 }
00277
00278 template<size_t _Nw>
00279 unsigned long
00280 _Base_bitset<_Nw>::_M_do_to_ulong() const
00281 {
00282 for (size_t __i = 1; __i < _Nw; ++__i)
00283 if (_M_w[__i])
00284 __throw_overflow_error(__N("_Base_bitset::_M_do_to_ulong"));
00285 return _M_w[0];
00286 }
00287
00288 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00289 template<size_t _Nw>
00290 unsigned long long
00291 _Base_bitset<_Nw>::_M_do_to_ullong() const
00292 {
00293 const bool __dw = sizeof(unsigned long long) > sizeof(unsigned long);
00294 for (size_t __i = 1 + __dw; __i < _Nw; ++__i)
00295 if (_M_w[__i])
00296 __throw_overflow_error(__N("_Base_bitset::_M_do_to_ullong"));
00297
00298 if (__dw)
00299 return _M_w[0] + (static_cast<unsigned long long>(_M_w[1])
00300 << _GLIBCXX_BITSET_BITS_PER_WORD);
00301 return _M_w[0];
00302 }
00303 #endif
00304
00305 template<size_t _Nw>
00306 size_t
00307 _Base_bitset<_Nw>::_M_do_find_first(size_t __not_found) const
00308 {
00309 for (size_t __i = 0; __i < _Nw; __i++)
00310 {
00311 _WordT __thisword = _M_w[__i];
00312 if (__thisword != static_cast<_WordT>(0))
00313 return (__i * _GLIBCXX_BITSET_BITS_PER_WORD
00314 + __builtin_ctzl(__thisword));
00315 }
00316
00317 return __not_found;
00318 }
00319
00320 template<size_t _Nw>
00321 size_t
00322 _Base_bitset<_Nw>::_M_do_find_next(size_t __prev, size_t __not_found) const
00323 {
00324
00325 ++__prev;
00326
00327
00328 if (__prev >= _Nw * _GLIBCXX_BITSET_BITS_PER_WORD)
00329 return __not_found;
00330
00331
00332 size_t __i = _S_whichword(__prev);
00333 _WordT __thisword = _M_w[__i];
00334
00335
00336 __thisword &= (~static_cast<_WordT>(0)) << _S_whichbit(__prev);
00337
00338 if (__thisword != static_cast<_WordT>(0))
00339 return (__i * _GLIBCXX_BITSET_BITS_PER_WORD
00340 + __builtin_ctzl(__thisword));
00341
00342
00343 __i++;
00344 for (; __i < _Nw; __i++)
00345 {
00346 __thisword = _M_w[__i];
00347 if (__thisword != static_cast<_WordT>(0))
00348 return (__i * _GLIBCXX_BITSET_BITS_PER_WORD
00349 + __builtin_ctzl(__thisword));
00350 }
00351
00352 return __not_found;
00353 }
00354
00355
00356
00357
00358
00359
00360 template<>
00361 struct _Base_bitset<1>
00362 {
00363 typedef unsigned long _WordT;
00364 _WordT _M_w;
00365
00366 _Base_bitset(void)
00367 : _M_w(0)
00368 { }
00369
00370 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00371 _Base_bitset(unsigned long long __val)
00372 #else
00373 _Base_bitset(unsigned long __val)
00374 #endif
00375 : _M_w(__val)
00376 { }
00377
00378 static size_t
00379 _S_whichword(size_t __pos )
00380 { return __pos / _GLIBCXX_BITSET_BITS_PER_WORD; }
00381
00382 static size_t
00383 _S_whichbyte(size_t __pos )
00384 { return (__pos % _GLIBCXX_BITSET_BITS_PER_WORD) / __CHAR_BIT__; }
00385
00386 static size_t
00387 _S_whichbit(size_t __pos )
00388 { return __pos % _GLIBCXX_BITSET_BITS_PER_WORD; }
00389
00390 static _WordT
00391 _S_maskbit(size_t __pos )
00392 { return (static_cast<_WordT>(1)) << _S_whichbit(__pos); }
00393
00394 _WordT&
00395 _M_getword(size_t)
00396 { return _M_w; }
00397
00398 _WordT
00399 _M_getword(size_t) const
00400 { return _M_w; }
00401
00402 _WordT&
00403 _M_hiword()
00404 { return _M_w; }
00405
00406 _WordT
00407 _M_hiword() const
00408 { return _M_w; }
00409
00410 void
00411 _M_do_and(const _Base_bitset<1>& __x)
00412 { _M_w &= __x._M_w; }
00413
00414 void
00415 _M_do_or(const _Base_bitset<1>& __x)
00416 { _M_w |= __x._M_w; }
00417
00418 void
00419 _M_do_xor(const _Base_bitset<1>& __x)
00420 { _M_w ^= __x._M_w; }
00421
00422 void
00423 _M_do_left_shift(size_t __shift)
00424 { _M_w <<= __shift; }
00425
00426 void
00427 _M_do_right_shift(size_t __shift)
00428 { _M_w >>= __shift; }
00429
00430 void
00431 _M_do_flip()
00432 { _M_w = ~_M_w; }
00433
00434 void
00435 _M_do_set()
00436 { _M_w = ~static_cast<_WordT>(0); }
00437
00438 void
00439 _M_do_reset()
00440 { _M_w = 0; }
00441
00442 bool
00443 _M_is_equal(const _Base_bitset<1>& __x) const
00444 { return _M_w == __x._M_w; }
00445
00446 size_t
00447 _M_are_all_aux() const
00448 { return __builtin_popcountl(_M_w); }
00449
00450 bool
00451 _M_is_any() const
00452 { return _M_w != 0; }
00453
00454 size_t
00455 _M_do_count() const
00456 { return __builtin_popcountl(_M_w); }
00457
00458 unsigned long
00459 _M_do_to_ulong() const
00460 { return _M_w; }
00461
00462 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00463 unsigned long long
00464 _M_do_to_ullong() const
00465 { return _M_w; }
00466 #endif
00467
00468 size_t
00469 _M_do_find_first(size_t __not_found) const
00470 {
00471 if (_M_w != 0)
00472 return __builtin_ctzl(_M_w);
00473 else
00474 return __not_found;
00475 }
00476
00477
00478 size_t
00479 _M_do_find_next(size_t __prev, size_t __not_found) const
00480 {
00481 ++__prev;
00482 if (__prev >= ((size_t) _GLIBCXX_BITSET_BITS_PER_WORD))
00483 return __not_found;
00484
00485 _WordT __x = _M_w >> __prev;
00486 if (__x != 0)
00487 return __builtin_ctzl(__x) + __prev;
00488 else
00489 return __not_found;
00490 }
00491 };
00492
00493
00494
00495
00496
00497
00498 template<>
00499 struct _Base_bitset<0>
00500 {
00501 typedef unsigned long _WordT;
00502
00503 _Base_bitset()
00504 { }
00505
00506 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00507 _Base_bitset(unsigned long long)
00508 #else
00509 _Base_bitset(unsigned long)
00510 #endif
00511 { }
00512
00513 static size_t
00514 _S_whichword(size_t __pos )
00515 { return __pos / _GLIBCXX_BITSET_BITS_PER_WORD; }
00516
00517 static size_t
00518 _S_whichbyte(size_t __pos )
00519 { return (__pos % _GLIBCXX_BITSET_BITS_PER_WORD) / __CHAR_BIT__; }
00520
00521 static size_t
00522 _S_whichbit(size_t __pos )
00523 { return __pos % _GLIBCXX_BITSET_BITS_PER_WORD; }
00524
00525 static _WordT
00526 _S_maskbit(size_t __pos )
00527 { return (static_cast<_WordT>(1)) << _S_whichbit(__pos); }
00528
00529
00530
00531
00532
00533
00534
00535
00536 _WordT&
00537 _M_getword(size_t) const
00538 {
00539 __throw_out_of_range(__N("_Base_bitset::_M_getword"));
00540 return *new _WordT;
00541 }
00542
00543 _WordT
00544 _M_hiword() const
00545 { return 0; }
00546
00547 void
00548 _M_do_and(const _Base_bitset<0>&)
00549 { }
00550
00551 void
00552 _M_do_or(const _Base_bitset<0>&)
00553 { }
00554
00555 void
00556 _M_do_xor(const _Base_bitset<0>&)
00557 { }
00558
00559 void
00560 _M_do_left_shift(size_t)
00561 { }
00562
00563 void
00564 _M_do_right_shift(size_t)
00565 { }
00566
00567 void
00568 _M_do_flip()
00569 { }
00570
00571 void
00572 _M_do_set()
00573 { }
00574
00575 void
00576 _M_do_reset()
00577 { }
00578
00579
00580
00581
00582 bool
00583 _M_is_equal(const _Base_bitset<0>&) const
00584 { return true; }
00585
00586 size_t
00587 _M_are_all_aux() const
00588 { return 0; }
00589
00590 bool
00591 _M_is_any() const
00592 { return false; }
00593
00594 size_t
00595 _M_do_count() const
00596 { return 0; }
00597
00598 unsigned long
00599 _M_do_to_ulong() const
00600 { return 0; }
00601
00602 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00603 unsigned long long
00604 _M_do_to_ullong() const
00605 { return 0; }
00606 #endif
00607
00608
00609
00610 size_t
00611 _M_do_find_first(size_t) const
00612 { return 0; }
00613
00614 size_t
00615 _M_do_find_next(size_t, size_t) const
00616 { return 0; }
00617 };
00618
00619
00620
00621 template<size_t _Extrabits>
00622 struct _Sanitize
00623 {
00624 static void _S_do_sanitize(unsigned long& __val)
00625 { __val &= ~((~static_cast<unsigned long>(0)) << _Extrabits); }
00626 };
00627
00628 template<>
00629 struct _Sanitize<0>
00630 { static void _S_do_sanitize(unsigned long) {} };
00631
00632
00633
00634
00635
00636
00637
00638
00639
00640
00641
00642
00643
00644
00645
00646
00647
00648
00649
00650
00651
00652
00653
00654
00655
00656
00657
00658
00659
00660
00661
00662
00663
00664
00665
00666
00667
00668
00669
00670
00671
00672
00673
00674
00675
00676
00677
00678
00679
00680
00681
00682
00683
00684
00685
00686
00687
00688
00689
00690
00691
00692
00693
00694
00695 template<size_t _Nb>
00696 class bitset
00697 : private _Base_bitset<_GLIBCXX_BITSET_WORDS(_Nb)>
00698 {
00699 private:
00700 typedef _Base_bitset<_GLIBCXX_BITSET_WORDS(_Nb)> _Base;
00701 typedef unsigned long _WordT;
00702
00703 void
00704 _M_do_sanitize()
00705 {
00706 _Sanitize<_Nb % _GLIBCXX_BITSET_BITS_PER_WORD>::
00707 _S_do_sanitize(this->_M_hiword());
00708 }
00709
00710 public:
00711
00712
00713
00714
00715
00716
00717
00718
00719
00720
00721
00722
00723 class reference
00724 {
00725 friend class bitset;
00726
00727 _WordT *_M_wp;
00728 size_t _M_bpos;
00729
00730
00731 reference();
00732
00733 public:
00734 reference(bitset& __b, size_t __pos)
00735 {
00736 _M_wp = &__b._M_getword(__pos);
00737 _M_bpos = _Base::_S_whichbit(__pos);
00738 }
00739
00740 ~reference()
00741 { }
00742
00743
00744 reference&
00745 operator=(bool __x)
00746 {
00747 if (__x)
00748 *_M_wp |= _Base::_S_maskbit(_M_bpos);
00749 else
00750 *_M_wp &= ~_Base::_S_maskbit(_M_bpos);
00751 return *this;
00752 }
00753
00754
00755 reference&
00756 operator=(const reference& __j)
00757 {
00758 if ((*(__j._M_wp) & _Base::_S_maskbit(__j._M_bpos)))
00759 *_M_wp |= _Base::_S_maskbit(_M_bpos);
00760 else
00761 *_M_wp &= ~_Base::_S_maskbit(_M_bpos);
00762 return *this;
00763 }
00764
00765
00766 bool
00767 operator~() const
00768 { return (*(_M_wp) & _Base::_S_maskbit(_M_bpos)) == 0; }
00769
00770
00771 operator bool() const
00772 { return (*(_M_wp) & _Base::_S_maskbit(_M_bpos)) != 0; }
00773
00774
00775 reference&
00776 flip()
00777 {
00778 *_M_wp ^= _Base::_S_maskbit(_M_bpos);
00779 return *this;
00780 }
00781 };
00782 friend class reference;
00783
00784
00785
00786 bitset()
00787 { }
00788
00789
00790 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00791 bitset(unsigned long long __val)
00792 #else
00793 bitset(unsigned long __val)
00794 #endif
00795 : _Base(__val)
00796 { _M_do_sanitize(); }
00797
00798
00799
00800
00801
00802
00803
00804
00805
00806
00807 template<class _CharT, class _Traits, class _Alloc>
00808 explicit
00809 bitset(const std::basic_string<_CharT, _Traits, _Alloc>& __s,
00810 size_t __position = 0)
00811 : _Base()
00812 {
00813 if (__position > __s.size())
00814 __throw_out_of_range(__N("bitset::bitset initial position "
00815 "not valid"));
00816 _M_copy_from_string(__s, __position,
00817 std::basic_string<_CharT, _Traits, _Alloc>::npos,
00818 _CharT('0'), _CharT('1'));
00819 }
00820
00821
00822
00823
00824
00825
00826
00827
00828
00829
00830 template<class _CharT, class _Traits, class _Alloc>
00831 bitset(const std::basic_string<_CharT, _Traits, _Alloc>& __s,
00832 size_t __position, size_t __n)
00833 : _Base()
00834 {
00835 if (__position > __s.size())
00836 __throw_out_of_range(__N("bitset::bitset initial position "
00837 "not valid"));
00838 _M_copy_from_string(__s, __position, __n, _CharT('0'), _CharT('1'));
00839 }
00840
00841
00842
00843 template<class _CharT, class _Traits, class _Alloc>
00844 bitset(const std::basic_string<_CharT, _Traits, _Alloc>& __s,
00845 size_t __position, size_t __n,
00846 _CharT __zero, _CharT __one = _CharT('1'))
00847 : _Base()
00848 {
00849 if (__position > __s.size())
00850 __throw_out_of_range(__N("bitset::bitset initial position "
00851 "not valid"));
00852 _M_copy_from_string(__s, __position, __n, __zero, __one);
00853 }
00854
00855 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00856
00857
00858
00859
00860
00861
00862 explicit
00863 bitset(const char* __str)
00864 : _Base()
00865 {
00866 if (!__str)
00867 __throw_logic_error(__N("bitset::bitset(const char*)"));
00868
00869 const size_t __len = __builtin_strlen(__str);
00870 _M_copy_from_ptr<char, std::char_traits<char>>(__str, __len, 0,
00871 __len, '0', '1');
00872 }
00873 #endif
00874
00875
00876
00877
00878
00879
00880
00881
00882
00883 bitset<_Nb>&
00884 operator&=(const bitset<_Nb>& __rhs)
00885 {
00886 this->_M_do_and(__rhs);
00887 return *this;
00888 }
00889
00890 bitset<_Nb>&
00891 operator|=(const bitset<_Nb>& __rhs)
00892 {
00893 this->_M_do_or(__rhs);
00894 return *this;
00895 }
00896
00897 bitset<_Nb>&
00898 operator^=(const bitset<_Nb>& __rhs)
00899 {
00900 this->_M_do_xor(__rhs);
00901 return *this;
00902 }
00903
00904
00905
00906
00907
00908
00909
00910
00911
00912 bitset<_Nb>&
00913 operator<<=(size_t __position)
00914 {
00915 if (__builtin_expect(__position < _Nb, 1))
00916 {
00917 this->_M_do_left_shift(__position);
00918 this->_M_do_sanitize();
00919 }
00920 else
00921 this->_M_do_reset();
00922 return *this;
00923 }
00924
00925 bitset<_Nb>&
00926 operator>>=(size_t __position)
00927 {
00928 if (__builtin_expect(__position < _Nb, 1))
00929 {
00930 this->_M_do_right_shift(__position);
00931 this->_M_do_sanitize();
00932 }
00933 else
00934 this->_M_do_reset();
00935 return *this;
00936 }
00937
00938
00939
00940
00941
00942
00943
00944
00945 bitset<_Nb>&
00946 _Unchecked_set(size_t __pos)
00947 {
00948 this->_M_getword(__pos) |= _Base::_S_maskbit(__pos);
00949 return *this;
00950 }
00951
00952 bitset<_Nb>&
00953 _Unchecked_set(size_t __pos, int __val)
00954 {
00955 if (__val)
00956 this->_M_getword(__pos) |= _Base::_S_maskbit(__pos);
00957 else
00958 this->_M_getword(__pos) &= ~_Base::_S_maskbit(__pos);
00959 return *this;
00960 }
00961
00962 bitset<_Nb>&
00963 _Unchecked_reset(size_t __pos)
00964 {
00965 this->_M_getword(__pos) &= ~_Base::_S_maskbit(__pos);
00966 return *this;
00967 }
00968
00969 bitset<_Nb>&
00970 _Unchecked_flip(size_t __pos)
00971 {
00972 this->_M_getword(__pos) ^= _Base::_S_maskbit(__pos);
00973 return *this;
00974 }
00975
00976 bool
00977 _Unchecked_test(size_t __pos) const
00978 { return ((this->_M_getword(__pos) & _Base::_S_maskbit(__pos))
00979 != static_cast<_WordT>(0)); }
00980
00981
00982
00983
00984
00985
00986 bitset<_Nb>&
00987 set()
00988 {
00989 this->_M_do_set();
00990 this->_M_do_sanitize();
00991 return *this;
00992 }
00993
00994
00995
00996
00997
00998
00999
01000 bitset<_Nb>&
01001 set(size_t __position, bool __val = true)
01002 {
01003 if (__position >= _Nb)
01004 __throw_out_of_range(__N("bitset::set"));
01005 return _Unchecked_set(__position, __val);
01006 }
01007
01008
01009
01010
01011 bitset<_Nb>&
01012 reset()
01013 {
01014 this->_M_do_reset();
01015 return *this;
01016 }
01017
01018
01019
01020
01021
01022
01023
01024
01025 bitset<_Nb>&
01026 reset(size_t __position)
01027 {
01028 if (__position >= _Nb)
01029 __throw_out_of_range(__N("bitset::reset"));
01030 return _Unchecked_reset(__position);
01031 }
01032
01033
01034
01035
01036 bitset<_Nb>&
01037 flip()
01038 {
01039 this->_M_do_flip();
01040 this->_M_do_sanitize();
01041 return *this;
01042 }
01043
01044
01045
01046
01047
01048
01049 bitset<_Nb>&
01050 flip(size_t __position)
01051 {
01052 if (__position >= _Nb)
01053 __throw_out_of_range(__N("bitset::flip"));
01054 return _Unchecked_flip(__position);
01055 }
01056
01057
01058 bitset<_Nb>
01059 operator~() const
01060 { return bitset<_Nb>(*this).flip(); }
01061
01062
01063
01064
01065
01066
01067
01068
01069
01070
01071
01072
01073
01074
01075
01076
01077 reference
01078 operator[](size_t __position)
01079 { return reference(*this,__position); }
01080
01081 bool
01082 operator[](size_t __position) const
01083 { return _Unchecked_test(__position); }
01084
01085
01086
01087
01088
01089
01090
01091
01092 unsigned long
01093 to_ulong() const
01094 { return this->_M_do_to_ulong(); }
01095
01096 #ifdef __GXX_EXPERIMENTAL_CXX0X__
01097 unsigned long long
01098 to_ullong() const
01099 { return this->_M_do_to_ullong(); }
01100 #endif
01101
01102
01103
01104
01105
01106
01107
01108
01109
01110 template<class _CharT, class _Traits, class _Alloc>
01111 std::basic_string<_CharT, _Traits, _Alloc>
01112 to_string() const
01113 {
01114 std::basic_string<_CharT, _Traits, _Alloc> __result;
01115 _M_copy_to_string(__result, _CharT('0'), _CharT('1'));
01116 return __result;
01117 }
01118
01119
01120
01121 template<class _CharT, class _Traits, class _Alloc>
01122 std::basic_string<_CharT, _Traits, _Alloc>
01123 to_string(_CharT __zero, _CharT __one = _CharT('1')) const
01124 {
01125 std::basic_string<_CharT, _Traits, _Alloc> __result;
01126 _M_copy_to_string(__result, __zero, __one);
01127 return __result;
01128 }
01129
01130
01131
01132 template<class _CharT, class _Traits>
01133 std::basic_string<_CharT, _Traits, std::allocator<_CharT> >
01134 to_string() const
01135 { return to_string<_CharT, _Traits, std::allocator<_CharT> >(); }
01136
01137
01138
01139 template<class _CharT, class _Traits>
01140 std::basic_string<_CharT, _Traits, std::allocator<_CharT> >
01141 to_string(_CharT __zero, _CharT __one = _CharT('1')) const
01142 { return to_string<_CharT, _Traits,
01143 std::allocator<_CharT> >(__zero, __one); }
01144
01145 template<class _CharT>
01146 std::basic_string<_CharT, std::char_traits<_CharT>,
01147 std::allocator<_CharT> >
01148 to_string() const
01149 {
01150 return to_string<_CharT, std::char_traits<_CharT>,
01151 std::allocator<_CharT> >();
01152 }
01153
01154 template<class _CharT>
01155 std::basic_string<_CharT, std::char_traits<_CharT>,
01156 std::allocator<_CharT> >
01157 to_string(_CharT __zero, _CharT __one = _CharT('1')) const
01158 {
01159 return to_string<_CharT, std::char_traits<_CharT>,
01160 std::allocator<_CharT> >(__zero, __one);
01161 }
01162
01163 std::basic_string<char, std::char_traits<char>, std::allocator<char> >
01164 to_string() const
01165 {
01166 return to_string<char, std::char_traits<char>,
01167 std::allocator<char> >();
01168 }
01169
01170 std::basic_string<char, std::char_traits<char>, std::allocator<char> >
01171 to_string(char __zero, char __one = '1') const
01172 {
01173 return to_string<char, std::char_traits<char>,
01174 std::allocator<char> >(__zero, __one);
01175 }
01176
01177
01178 template<class _CharT, class _Traits>
01179 void
01180 _M_copy_from_ptr(const _CharT*, size_t, size_t, size_t,
01181 _CharT, _CharT);
01182
01183 template<class _CharT, class _Traits, class _Alloc>
01184 void
01185 _M_copy_from_string(const std::basic_string<_CharT,
01186 _Traits, _Alloc>& __s, size_t __pos, size_t __n,
01187 _CharT __zero, _CharT __one)
01188 { _M_copy_from_ptr<_CharT, _Traits>(__s.data(), __s.size(), __pos, __n,
01189 __zero, __one); }
01190
01191 template<class _CharT, class _Traits, class _Alloc>
01192 void
01193 _M_copy_to_string(std::basic_string<_CharT, _Traits, _Alloc>&,
01194 _CharT, _CharT) const;
01195
01196
01197 template<class _CharT, class _Traits, class _Alloc>
01198 void
01199 _M_copy_from_string(const std::basic_string<_CharT,
01200 _Traits, _Alloc>& __s, size_t __pos, size_t __n)
01201 { _M_copy_from_string(__s, __pos, __n, _CharT('0'), _CharT('1')); }
01202
01203 template<class _CharT, class _Traits, class _Alloc>
01204 void
01205 _M_copy_to_string(std::basic_string<_CharT, _Traits,_Alloc>& __s) const
01206 { _M_copy_to_string(__s, _CharT('0'), _CharT('1')); }
01207
01208
01209 size_t
01210 count() const
01211 { return this->_M_do_count(); }
01212
01213
01214 size_t
01215 size() const
01216 { return _Nb; }
01217
01218
01219
01220 bool
01221 operator==(const bitset<_Nb>& __rhs) const
01222 { return this->_M_is_equal(__rhs); }
01223
01224 bool
01225 operator!=(const bitset<_Nb>& __rhs) const
01226 { return !this->_M_is_equal(__rhs); }
01227
01228
01229
01230
01231
01232
01233
01234
01235 bool
01236 test(size_t __position) const
01237 {
01238 if (__position >= _Nb)
01239 __throw_out_of_range(__N("bitset::test"));
01240 return _Unchecked_test(__position);
01241 }
01242
01243
01244
01245
01246
01247
01248
01249 bool
01250 all() const
01251 { return this->_M_are_all_aux() == _Nb; }
01252
01253
01254
01255
01256
01257 bool
01258 any() const
01259 { return this->_M_is_any(); }
01260
01261
01262
01263
01264
01265 bool
01266 none() const
01267 { return !this->_M_is_any(); }
01268
01269
01270
01271 bitset<_Nb>
01272 operator<<(size_t __position) const
01273 { return bitset<_Nb>(*this) <<= __position; }
01274
01275 bitset<_Nb>
01276 operator>>(size_t __position) const
01277 { return bitset<_Nb>(*this) >>= __position; }
01278
01279
01280
01281
01282
01283
01284
01285
01286 size_t
01287 _Find_first() const
01288 { return this->_M_do_find_first(_Nb); }
01289
01290
01291
01292
01293
01294
01295
01296
01297 size_t
01298 _Find_next(size_t __prev ) const
01299 { return this->_M_do_find_next(__prev, _Nb); }
01300 };
01301
01302
01303 template<size_t _Nb>
01304 template<class _CharT, class _Traits>
01305 void
01306 bitset<_Nb>::
01307 _M_copy_from_ptr(const _CharT* __s, size_t __len,
01308 size_t __pos, size_t __n, _CharT __zero, _CharT __one)
01309 {
01310 reset();
01311 const size_t __nbits = std::min(_Nb, std::min(__n, __len - __pos));
01312 for (size_t __i = __nbits; __i > 0; --__i)
01313 {
01314 const _CharT __c = __s[__pos + __nbits - __i];
01315 if (_Traits::eq(__c, __zero))
01316 ;
01317 else if (_Traits::eq(__c, __one))
01318 _Unchecked_set(__i - 1);
01319 else
01320 __throw_invalid_argument(__N("bitset::_M_copy_from_ptr"));
01321 }
01322 }
01323
01324 template<size_t _Nb>
01325 template<class _CharT, class _Traits, class _Alloc>
01326 void
01327 bitset<_Nb>::
01328 _M_copy_to_string(std::basic_string<_CharT, _Traits, _Alloc>& __s,
01329 _CharT __zero, _CharT __one) const
01330 {
01331 __s.assign(_Nb, __zero);
01332 for (size_t __i = _Nb; __i > 0; --__i)
01333 if (_Unchecked_test(__i - 1))
01334 _Traits::assign(__s[_Nb - __i], __one);
01335 }
01336
01337
01338
01339
01340
01341
01342
01343
01344
01345
01346
01347 template<size_t _Nb>
01348 inline bitset<_Nb>
01349 operator&(const bitset<_Nb>& __x, const bitset<_Nb>& __y)
01350 {
01351 bitset<_Nb> __result(__x);
01352 __result &= __y;
01353 return __result;
01354 }
01355
01356 template<size_t _Nb>
01357 inline bitset<_Nb>
01358 operator|(const bitset<_Nb>& __x, const bitset<_Nb>& __y)
01359 {
01360 bitset<_Nb> __result(__x);
01361 __result |= __y;
01362 return __result;
01363 }
01364
01365 template <size_t _Nb>
01366 inline bitset<_Nb>
01367 operator^(const bitset<_Nb>& __x, const bitset<_Nb>& __y)
01368 {
01369 bitset<_Nb> __result(__x);
01370 __result ^= __y;
01371 return __result;
01372 }
01373
01374
01375
01376
01377
01378
01379
01380
01381
01382
01383
01384 template<class _CharT, class _Traits, size_t _Nb>
01385 std::basic_istream<_CharT, _Traits>&
01386 operator>>(std::basic_istream<_CharT, _Traits>& __is, bitset<_Nb>& __x)
01387 {
01388 typedef typename _Traits::char_type char_type;
01389 typedef std::basic_istream<_CharT, _Traits> __istream_type;
01390 typedef typename __istream_type::ios_base __ios_base;
01391
01392 std::basic_string<_CharT, _Traits> __tmp;
01393 __tmp.reserve(_Nb);
01394
01395
01396
01397 const char_type __zero = __is.widen('0');
01398 const char_type __one = __is.widen('1');
01399
01400 typename __ios_base::iostate __state = __ios_base::goodbit;
01401 typename __istream_type::sentry __sentry(__is);
01402 if (__sentry)
01403 {
01404 __try
01405 {
01406 for (size_t __i = _Nb; __i > 0; --__i)
01407 {
01408 static typename _Traits::int_type __eof = _Traits::eof();
01409
01410 typename _Traits::int_type __c1 = __is.rdbuf()->sbumpc();
01411 if (_Traits::eq_int_type(__c1, __eof))
01412 {
01413 __state |= __ios_base::eofbit;
01414 break;
01415 }
01416 else
01417 {
01418 const char_type __c2 = _Traits::to_char_type(__c1);
01419 if (_Traits::eq(__c2, __zero))
01420 __tmp.push_back(__zero);
01421 else if (_Traits::eq(__c2, __one))
01422 __tmp.push_back(__one);
01423 else if (_Traits::
01424 eq_int_type(__is.rdbuf()->sputbackc(__c2),
01425 __eof))
01426 {
01427 __state |= __ios_base::failbit;
01428 break;
01429 }
01430 }
01431 }
01432 }
01433 __catch(__cxxabiv1::__forced_unwind&)
01434 {
01435 __is._M_setstate(__ios_base::badbit);
01436 __throw_exception_again;
01437 }
01438 __catch(...)
01439 { __is._M_setstate(__ios_base::badbit); }
01440 }
01441
01442 if (__tmp.empty() && _Nb)
01443 __state |= __ios_base::failbit;
01444 else
01445 __x._M_copy_from_string(__tmp, static_cast<size_t>(0), _Nb,
01446 __zero, __one);
01447 if (__state)
01448 __is.setstate(__state);
01449 return __is;
01450 }
01451
01452 template <class _CharT, class _Traits, size_t _Nb>
01453 std::basic_ostream<_CharT, _Traits>&
01454 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
01455 const bitset<_Nb>& __x)
01456 {
01457 std::basic_string<_CharT, _Traits> __tmp;
01458
01459
01460
01461 const ctype<_CharT>& __ct = use_facet<ctype<_CharT> >(__os.getloc());
01462 __x._M_copy_to_string(__tmp, __ct.widen('0'), __ct.widen('1'));
01463 return __os << __tmp;
01464 }
01465
01466
01467 _GLIBCXX_END_NESTED_NAMESPACE
01468
01469 #undef _GLIBCXX_BITSET_WORDS
01470 #undef _GLIBCXX_BITSET_BITS_PER_WORD
01471
01472 #ifdef _GLIBCXX_DEBUG
01473 # include <debug/bitset>
01474 #endif
01475
01476 #ifdef _GLIBCXX_PROFILE
01477 # include <profile/bitset>
01478 #endif
01479
01480 #endif