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