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