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
00048 #ifndef _GLIBCPP_BITSET_H
00049 #define _GLIBCPP_BITSET_H
00050
00051 #pragma GCC system_header
00052
00053 #include <cstddef>
00054 #include <cstring>
00055 #include <string>
00056 #include <bits/functexcept.h>
00057
00058 #include <ostream>
00059 #include <istream>
00060
00061
00062 #define _GLIBCPP_BITSET_BITS_PER_WORD (CHAR_BIT*sizeof(unsigned long))
00063 #define _GLIBCPP_BITSET_WORDS(__n) \
00064 ((__n) < 1 ? 0 : ((__n) + _GLIBCPP_BITSET_BITS_PER_WORD - 1)/_GLIBCPP_BITSET_BITS_PER_WORD)
00065
00066 namespace std
00067 {
00068 extern unsigned char _S_bit_count[256];
00069 extern unsigned char _S_first_one[256];
00070
00079 template<size_t _Nw>
00080 struct _Base_bitset
00081 {
00082 typedef unsigned long _WordT;
00083
00085 _WordT _M_w[_Nw];
00086
00087 _Base_bitset() { _M_do_reset(); }
00088 _Base_bitset(unsigned long __val)
00089 {
00090 _M_do_reset();
00091 _M_w[0] = __val;
00092 }
00093
00094 static size_t
00095 _S_whichword(size_t __pos )
00096 { return __pos / _GLIBCPP_BITSET_BITS_PER_WORD; }
00097
00098 static size_t
00099 _S_whichbyte(size_t __pos )
00100 { return (__pos % _GLIBCPP_BITSET_BITS_PER_WORD) / CHAR_BIT; }
00101
00102 static size_t
00103 _S_whichbit(size_t __pos )
00104 { return __pos % _GLIBCPP_BITSET_BITS_PER_WORD; }
00105
00106 static _WordT
00107 _S_maskbit(size_t __pos )
00108 { return (static_cast<_WordT>(1)) << _S_whichbit(__pos); }
00109
00110 _WordT&
00111 _M_getword(size_t __pos)
00112 { return _M_w[_S_whichword(__pos)]; }
00113
00114 _WordT
00115 _M_getword(size_t __pos) const
00116 { return _M_w[_S_whichword(__pos)]; }
00117
00118 _WordT&
00119 _M_hiword() { return _M_w[_Nw - 1]; }
00120
00121 _WordT
00122 _M_hiword() const { return _M_w[_Nw - 1]; }
00123
00124 void
00125 _M_do_and(const _Base_bitset<_Nw>& __x)
00126 {
00127 for (size_t __i = 0; __i < _Nw; __i++)
00128 _M_w[__i] &= __x._M_w[__i];
00129 }
00130
00131 void
00132 _M_do_or(const _Base_bitset<_Nw>& __x)
00133 {
00134 for (size_t __i = 0; __i < _Nw; __i++)
00135 _M_w[__i] |= __x._M_w[__i];
00136 }
00137
00138 void
00139 _M_do_xor(const _Base_bitset<_Nw>& __x)
00140 {
00141 for (size_t __i = 0; __i < _Nw; __i++)
00142 _M_w[__i] ^= __x._M_w[__i];
00143 }
00144
00145 void
00146 _M_do_left_shift(size_t __shift);
00147
00148 void
00149 _M_do_right_shift(size_t __shift);
00150
00151 void
00152 _M_do_flip()
00153 {
00154 for (size_t __i = 0; __i < _Nw; __i++)
00155 _M_w[__i] = ~_M_w[__i];
00156 }
00157
00158 void
00159 _M_do_set()
00160 {
00161 for (size_t __i = 0; __i < _Nw; __i++)
00162 _M_w[__i] = ~static_cast<_WordT>(0);
00163 }
00164
00165 void
00166 _M_do_reset() { memset(_M_w, 0, _Nw * sizeof(_WordT)); }
00167
00168 bool
00169 _M_is_equal(const _Base_bitset<_Nw>& __x) const
00170 {
00171 for (size_t __i = 0; __i < _Nw; ++__i)
00172 {
00173 if (_M_w[__i] != __x._M_w[__i])
00174 return false;
00175 }
00176 return true;
00177 }
00178
00179 bool
00180 _M_is_any() const
00181 {
00182 for (size_t __i = 0; __i < _Nw; __i++)
00183 {
00184 if (_M_w[__i] != static_cast<_WordT>(0))
00185 return true;
00186 }
00187 return false;
00188 }
00189
00190 size_t
00191 _M_do_count() const
00192 {
00193 size_t __result = 0;
00194 const unsigned char* __byte_ptr = (const unsigned char*)_M_w;
00195 const unsigned char* __end_ptr = (const unsigned char*)(_M_w + _Nw);
00196
00197 while ( __byte_ptr < __end_ptr )
00198 {
00199 __result += _S_bit_count[*__byte_ptr];
00200 __byte_ptr++;
00201 }
00202 return __result;
00203 }
00204
00205 unsigned long
00206 _M_do_to_ulong() const;
00207
00208
00209 size_t
00210 _M_do_find_first(size_t __not_found) const;
00211
00212
00213 size_t
00214 _M_do_find_next(size_t __prev, size_t __not_found) const;
00215 };
00216
00217
00218 template<size_t _Nw>
00219 void
00220 _Base_bitset<_Nw>::_M_do_left_shift(size_t __shift)
00221 {
00222 if (__shift != 0)
00223 {
00224 const size_t __wshift = __shift / _GLIBCPP_BITSET_BITS_PER_WORD;
00225 const size_t __offset = __shift % _GLIBCPP_BITSET_BITS_PER_WORD;
00226
00227 if (__offset == 0)
00228 for (size_t __n = _Nw - 1; __n >= __wshift; --__n)
00229 _M_w[__n] = _M_w[__n - __wshift];
00230 else
00231 {
00232 const size_t __sub_offset = _GLIBCPP_BITSET_BITS_PER_WORD - __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 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 (__shift != 0)
00248 {
00249 const size_t __wshift = __shift / _GLIBCPP_BITSET_BITS_PER_WORD;
00250 const size_t __offset = __shift % _GLIBCPP_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 = _GLIBCPP_BITSET_BITS_PER_WORD - __offset;
00259 for (size_t __n = 0; __n < __limit; ++__n)
00260 _M_w[__n] = (_M_w[__n + __wshift] >> __offset) |
00261 (_M_w[__n + __wshift + 1] << __sub_offset);
00262 _M_w[__limit] = _M_w[_Nw-1] >> __offset;
00263 }
00264
00265 fill(_M_w + __limit + 1, _M_w + _Nw, static_cast<_WordT>(0));
00266 }
00267 }
00268
00269 template<size_t _Nw>
00270 unsigned long
00271 _Base_bitset<_Nw>::_M_do_to_ulong() const
00272 {
00273 for (size_t __i = 1; __i < _Nw; ++__i)
00274 if (_M_w[__i])
00275 __throw_overflow_error("bitset -- too large to fit in unsigned long");
00276 return _M_w[0];
00277 }
00278
00279 template<size_t _Nw>
00280 size_t
00281 _Base_bitset<_Nw>::_M_do_find_first(size_t __not_found) const
00282 {
00283 for (size_t __i = 0; __i < _Nw; __i++ )
00284 {
00285 _WordT __thisword = _M_w[__i];
00286 if ( __thisword != static_cast<_WordT>(0) )
00287 {
00288
00289 for (size_t __j = 0; __j < sizeof(_WordT); __j++ )
00290 {
00291 unsigned char __this_byte
00292 = static_cast<unsigned char>(__thisword & (~(unsigned char)0));
00293 if (__this_byte)
00294 return __i*_GLIBCPP_BITSET_BITS_PER_WORD + __j*CHAR_BIT +
00295 _S_first_one[__this_byte];
00296
00297 __thisword >>= CHAR_BIT;
00298 }
00299 }
00300 }
00301
00302 return __not_found;
00303 }
00304
00305 template<size_t _Nw>
00306 size_t
00307 _Base_bitset<_Nw>::_M_do_find_next(size_t __prev, size_t __not_found) const
00308 {
00309
00310 ++__prev;
00311
00312
00313 if ( __prev >= _Nw * _GLIBCPP_BITSET_BITS_PER_WORD )
00314 return __not_found;
00315
00316
00317 size_t __i = _S_whichword(__prev);
00318 _WordT __thisword = _M_w[__i];
00319
00320
00321 __thisword &= (~static_cast<_WordT>(0)) << _S_whichbit(__prev);
00322
00323 if ( __thisword != static_cast<_WordT>(0) )
00324 {
00325
00326
00327 __thisword >>= _S_whichbyte(__prev) * CHAR_BIT;
00328 for (size_t __j = _S_whichbyte(__prev); __j < sizeof(_WordT); __j++)
00329 {
00330 unsigned char __this_byte
00331 = static_cast<unsigned char>(__thisword & (~(unsigned char)0));
00332 if ( __this_byte )
00333 return __i*_GLIBCPP_BITSET_BITS_PER_WORD + __j*CHAR_BIT +
00334 _S_first_one[__this_byte];
00335
00336 __thisword >>= CHAR_BIT;
00337 }
00338 }
00339
00340
00341 __i++;
00342 for ( ; __i < _Nw; __i++ )
00343 {
00344 __thisword = _M_w[__i];
00345 if ( __thisword != static_cast<_WordT>(0) )
00346 {
00347
00348 for (size_t __j = 0; __j < sizeof(_WordT); __j++ )
00349 {
00350 unsigned char __this_byte
00351 = static_cast<unsigned char>(__thisword & (~(unsigned char)0));
00352 if ( __this_byte )
00353 return __i*_GLIBCPP_BITSET_BITS_PER_WORD + __j*CHAR_BIT +
00354 _S_first_one[__this_byte];
00355
00356 __thisword >>= CHAR_BIT;
00357 }
00358 }
00359 }
00360
00361 return __not_found;
00362 }
00363
00364
00372 template<>
00373 struct _Base_bitset<1>
00374 {
00375 typedef unsigned long _WordT;
00376 _WordT _M_w;
00377
00378 _Base_bitset( void ) : _M_w(0) {}
00379 _Base_bitset(unsigned long __val) : _M_w(__val) {}
00380
00381 static size_t
00382 _S_whichword(size_t __pos )
00383 { return __pos / _GLIBCPP_BITSET_BITS_PER_WORD; }
00384
00385 static size_t
00386 _S_whichbyte(size_t __pos )
00387 { return (__pos % _GLIBCPP_BITSET_BITS_PER_WORD) / CHAR_BIT; }
00388
00389 static size_t
00390 _S_whichbit(size_t __pos )
00391 { return __pos % _GLIBCPP_BITSET_BITS_PER_WORD; }
00392
00393 static _WordT
00394 _S_maskbit(size_t __pos )
00395 { return (static_cast<_WordT>(1)) << _S_whichbit(__pos); }
00396
00397 _WordT&
00398 _M_getword(size_t) { return _M_w; }
00399
00400 _WordT
00401 _M_getword(size_t) const { return _M_w; }
00402
00403 _WordT&
00404 _M_hiword() { return _M_w; }
00405
00406 _WordT
00407 _M_hiword() const { return _M_w; }
00408
00409 void
00410 _M_do_and(const _Base_bitset<1>& __x) { _M_w &= __x._M_w; }
00411
00412 void
00413 _M_do_or(const _Base_bitset<1>& __x) { _M_w |= __x._M_w; }
00414
00415 void
00416 _M_do_xor(const _Base_bitset<1>& __x) { _M_w ^= __x._M_w; }
00417
00418 void
00419 _M_do_left_shift(size_t __shift) { _M_w <<= __shift; }
00420
00421 void
00422 _M_do_right_shift(size_t __shift) { _M_w >>= __shift; }
00423
00424 void
00425 _M_do_flip() { _M_w = ~_M_w; }
00426
00427 void
00428 _M_do_set() { _M_w = ~static_cast<_WordT>(0); }
00429
00430 void
00431 _M_do_reset() { _M_w = 0; }
00432
00433 bool
00434 _M_is_equal(const _Base_bitset<1>& __x) const
00435 { return _M_w == __x._M_w; }
00436
00437 bool
00438 _M_is_any() const { return _M_w != 0; }
00439
00440 size_t
00441 _M_do_count() const
00442 {
00443 size_t __result = 0;
00444 const unsigned char* __byte_ptr = (const unsigned char*)&_M_w;
00445 const unsigned char* __end_ptr
00446 = ((const unsigned char*)&_M_w)+sizeof(_M_w);
00447 while ( __byte_ptr < __end_ptr )
00448 {
00449 __result += _S_bit_count[*__byte_ptr];
00450 __byte_ptr++;
00451 }
00452 return __result;
00453 }
00454
00455 unsigned long
00456 _M_do_to_ulong() const { return _M_w; }
00457
00458 size_t
00459 _M_do_find_first(size_t __not_found) const;
00460
00461
00462 size_t
00463 _M_do_find_next(size_t __prev, size_t __not_found) const;
00464 };
00465
00466
00474 template<>
00475 struct _Base_bitset<0>
00476 {
00477 typedef unsigned long _WordT;
00478
00479 _Base_bitset() {}
00480 _Base_bitset(unsigned long) {}
00481
00482 static size_t
00483 _S_whichword(size_t __pos )
00484 { return __pos / _GLIBCPP_BITSET_BITS_PER_WORD; }
00485
00486 static size_t
00487 _S_whichbyte(size_t __pos )
00488 { return (__pos % _GLIBCPP_BITSET_BITS_PER_WORD) / CHAR_BIT; }
00489
00490 static size_t
00491 _S_whichbit(size_t __pos )
00492 { return __pos % _GLIBCPP_BITSET_BITS_PER_WORD; }
00493
00494 static _WordT
00495 _S_maskbit(size_t __pos )
00496 { return (static_cast<_WordT>(1)) << _S_whichbit(__pos); }
00497
00498
00499
00500
00501
00502
00503
00504
00505 _WordT&
00506 _M_getword(size_t) const
00507 { __throw_out_of_range("bitset -- zero-length"); return *new _WordT; }
00508
00509 _WordT
00510 _M_hiword() const { return 0; }
00511
00512 void
00513 _M_do_and(const _Base_bitset<0>&) { }
00514
00515 void
00516 _M_do_or(const _Base_bitset<0>&) { }
00517
00518 void
00519 _M_do_xor(const _Base_bitset<0>&) { }
00520
00521 void
00522 _M_do_left_shift(size_t) { }
00523
00524 void
00525 _M_do_right_shift(size_t) { }
00526
00527 void
00528 _M_do_flip() { }
00529
00530 void
00531 _M_do_set() { }
00532
00533 void
00534 _M_do_reset() { }
00535
00536
00537
00538
00539 bool
00540 _M_is_equal(const _Base_bitset<0>&) const { return true; }
00541
00542 bool
00543 _M_is_any() const { return false; }
00544
00545 size_t
00546 _M_do_count() const { return 0; }
00547
00548 unsigned long
00549 _M_do_to_ulong() const { return 0; }
00550
00551
00552
00553 size_t
00554 _M_do_find_first(size_t) const { return 0; }
00555
00556 size_t
00557 _M_do_find_next(size_t, size_t) const { return 0; }
00558 };
00559
00560
00561
00562 template<size_t _Extrabits>
00563 struct _Sanitize
00564 {
00565 static void _S_do_sanitize(unsigned long& __val)
00566 { __val &= ~((~static_cast<unsigned long>(0)) << _Extrabits); }
00567 };
00568
00569 template<>
00570 struct _Sanitize<0>
00571 { static void _S_do_sanitize(unsigned long) { } };
00572
00636 template<size_t _Nb>
00637 class bitset : private _Base_bitset<_GLIBCPP_BITSET_WORDS(_Nb)>
00638 {
00639 private:
00640 typedef _Base_bitset<_GLIBCPP_BITSET_WORDS(_Nb)> _Base;
00641 typedef unsigned long _WordT;
00642
00643 void
00644 _M_do_sanitize()
00645 {
00646 _Sanitize<_Nb%_GLIBCPP_BITSET_BITS_PER_WORD>::
00647 _S_do_sanitize(this->_M_hiword());
00648 }
00649
00650 public:
00663 class reference
00664 {
00665 friend class bitset;
00666
00667 _WordT *_M_wp;
00668 size_t _M_bpos;
00669
00670
00671 reference();
00672
00673 public:
00674 reference(bitset& __b, size_t __pos)
00675 {
00676 _M_wp = &__b._M_getword(__pos);
00677 _M_bpos = _Base::_S_whichbit(__pos);
00678 }
00679
00680 ~reference() { }
00681
00682
00683 reference&
00684 operator=(bool __x)
00685 {
00686 if ( __x )
00687 *_M_wp |= _Base::_S_maskbit(_M_bpos);
00688 else
00689 *_M_wp &= ~_Base::_S_maskbit(_M_bpos);
00690 return *this;
00691 }
00692
00693
00694 reference&
00695 operator=(const reference& __j)
00696 {
00697 if ( (*(__j._M_wp) & _Base::_S_maskbit(__j._M_bpos)) )
00698 *_M_wp |= _Base::_S_maskbit(_M_bpos);
00699 else
00700 *_M_wp &= ~_Base::_S_maskbit(_M_bpos);
00701 return *this;
00702 }
00703
00704
00705 bool
00706 operator~() const
00707 { return (*(_M_wp) & _Base::_S_maskbit(_M_bpos)) == 0; }
00708
00709
00710 operator bool() const
00711 { return (*(_M_wp) & _Base::_S_maskbit(_M_bpos)) != 0; }
00712
00713
00714 reference&
00715 flip()
00716 {
00717 *_M_wp ^= _Base::_S_maskbit(_M_bpos);
00718 return *this;
00719 }
00720 };
00721 friend class reference;
00722
00723
00725
00726
00728 bitset(unsigned long __val) : _Base(__val)
00729 { _M_do_sanitize(); }
00730
00740 template<class _CharT, class _Traits, class _Alloc>
00741 explicit bitset(const basic_string<_CharT, _Traits, _Alloc>& __s,
00742 size_t __pos = 0) : _Base()
00743 {
00744 if (__pos > __s.size())
00745 __throw_out_of_range("bitset -- initial position is larger than "
00746 "the string itself");
00747 _M_copy_from_string(__s, __pos,
00748 basic_string<_CharT, _Traits, _Alloc>::npos);
00749 }
00750
00760 template<class _CharT, class _Traits, class _Alloc>
00761 bitset(const basic_string<_CharT, _Traits, _Alloc>& __s,
00762 size_t __pos, size_t __n) : _Base()
00763 {
00764 if (__pos > __s.size())
00765 __throw_out_of_range("bitset -- initial position is larger than "
00766 "the string itself");
00767 _M_copy_from_string(__s, __pos, __n);
00768 }
00769
00770
00772
00778 bitset<_Nb>&
00779 operator&=(const bitset<_Nb>& __rhs)
00780 {
00781 this->_M_do_and(__rhs);
00782 return *this;
00783 }
00784
00785 bitset<_Nb>&
00786 operator|=(const bitset<_Nb>& __rhs)
00787 {
00788 this->_M_do_or(__rhs);
00789 return *this;
00790 }
00791
00792 bitset<_Nb>&
00793 operator^=(const bitset<_Nb>& __rhs)
00794 {
00795 this->_M_do_xor(__rhs);
00796 return *this;
00797 }
00799
00801
00807 bitset<_Nb>&
00808 operator<<=(size_t __pos)
00809 {
00810 this->_M_do_left_shift(__pos);
00811 this->_M_do_sanitize();
00812 return *this;
00813 }
00814
00815 bitset<_Nb>&
00816 operator>>=(size_t __pos)
00817 {
00818 this->_M_do_right_shift(__pos);
00819 this->_M_do_sanitize();
00820 return *this;
00821 }
00823
00825
00830 bitset<_Nb>&
00831 _Unchecked_set(size_t __pos)
00832 {
00833 this->_M_getword(__pos) |= _Base::_S_maskbit(__pos);
00834 return *this;
00835 }
00836
00837 bitset<_Nb>&
00838 _Unchecked_set(size_t __pos, int __val)
00839 {
00840 if (__val)
00841 this->_M_getword(__pos) |= _Base::_S_maskbit(__pos);
00842 else
00843 this->_M_getword(__pos) &= ~_Base::_S_maskbit(__pos);
00844 return *this;
00845 }
00846
00847 bitset<_Nb>&
00848 _Unchecked_reset(size_t __pos)
00849 {
00850 this->_M_getword(__pos) &= ~_Base::_S_maskbit(__pos);
00851 return *this;
00852 }
00853
00854 bitset<_Nb>&
00855 _Unchecked_flip(size_t __pos)
00856 {
00857 this->_M_getword(__pos) ^= _Base::_S_maskbit(__pos);
00858 return *this;
00859 }
00860
00861 bool
00862 _Unchecked_test(size_t __pos) const
00863 {
00864 return (this->_M_getword(__pos) & _Base::_S_maskbit(__pos))
00865 != static_cast<_WordT>(0);
00866 }
00868
00869
00873 bitset<_Nb>&
00874 set()
00875 {
00876 this->_M_do_set();
00877 this->_M_do_sanitize();
00878 return *this;
00879 }
00880
00887 bitset<_Nb>&
00888 set(size_t __pos, bool __val = true)
00889 {
00890 if (__pos >= _Nb)
00891 __throw_out_of_range("bitset -- set() argument too large");
00892 return _Unchecked_set(__pos, __val);
00893 }
00894
00898 bitset<_Nb>&
00899 reset()
00900 {
00901 this->_M_do_reset();
00902 return *this;
00903 }
00904
00912 bitset<_Nb>&
00913 reset(size_t __pos)
00914 {
00915 if (__pos >= _Nb)
00916 __throw_out_of_range("bitset -- reset() argument too large");
00917 return _Unchecked_reset(__pos);
00918 }
00919
00923 bitset<_Nb>&
00924 flip()
00925 {
00926 this->_M_do_flip();
00927 this->_M_do_sanitize();
00928 return *this;
00929 }
00930
00936 bitset<_Nb>&
00937 flip(size_t __pos)
00938 {
00939 if (__pos >= _Nb)
00940 __throw_out_of_range("bitset -- flip() argument too large");
00941 return _Unchecked_flip(__pos);
00942 }
00943
00945 bitset<_Nb>
00946 operator~() const { return bitset<_Nb>(*this).flip(); }
00947
00949
00965 reference
00966 operator[](size_t __pos) { return reference(*this,__pos); }
00967
00968 bool
00969 operator[](size_t __pos) const { return _Unchecked_test(__pos); }
00971
00978 unsigned long
00979 to_ulong() const { return this->_M_do_to_ulong(); }
00980
00995 template<class _CharT, class _Traits, class _Alloc>
00996 basic_string<_CharT, _Traits, _Alloc>
00997 to_string() const
00998 {
00999 basic_string<_CharT, _Traits, _Alloc> __result;
01000 _M_copy_to_string(__result);
01001 return __result;
01002 }
01003
01004
01005 template<class _CharT, class _Traits, class _Alloc>
01006 void
01007 _M_copy_from_string(const basic_string<_CharT,_Traits,_Alloc>& __s,
01008 size_t, size_t);
01009
01010 template<class _CharT, class _Traits, class _Alloc>
01011 void
01012 _M_copy_to_string(basic_string<_CharT,_Traits,_Alloc>&) const;
01013
01015 size_t
01016 count() const { return this->_M_do_count(); }
01017
01019 size_t
01020 size() const { return _Nb; }
01021
01023
01024 bool
01025 operator==(const bitset<_Nb>& __rhs) const
01026 { return this->_M_is_equal(__rhs); }
01027
01028 bool
01029 operator!=(const bitset<_Nb>& __rhs) const
01030 { return !this->_M_is_equal(__rhs); }
01032
01039 bool
01040 test(size_t __pos) const
01041 {
01042 if (__pos >= _Nb)
01043 __throw_out_of_range("bitset -- test() argument too large");
01044 return _Unchecked_test(__pos);
01045 }
01046
01051 bool
01052 any() const { return this->_M_is_any(); }
01053
01058 bool
01059 none() const { return !this->_M_is_any(); }
01060
01062
01063 bitset<_Nb>
01064 operator<<(size_t __pos) const
01065 { return bitset<_Nb>(*this) <<= __pos; }
01066
01067 bitset<_Nb>
01068 operator>>(size_t __pos) const
01069 { return bitset<_Nb>(*this) >>= __pos; }
01071
01078 size_t
01079 _Find_first() const
01080 { return this->_M_do_find_first(_Nb); }
01081
01089 size_t
01090 _Find_next(size_t __prev ) const
01091 { return this->_M_do_find_next(__prev, _Nb); }
01092 };
01093
01094
01095 template<size_t _Nb>
01096 template<class _CharT, class _Traits, class _Alloc>
01097 void
01098 bitset<_Nb>::_M_copy_from_string(const basic_string<_CharT,_Traits,_Alloc>& __s, size_t __pos, size_t __n)
01099 {
01100 reset();
01101 const size_t __nbits = min(_Nb, min(__n, __s.size() - __pos));
01102 for (size_t __i = 0; __i < __nbits; ++__i)
01103 {
01104 switch(__s[__pos + __nbits - __i - 1])
01105 {
01106 case '0':
01107 break;
01108 case '1':
01109 set(__i);
01110 break;
01111 default:
01112 __throw_invalid_argument("bitset -- string contains characters "
01113 "which are neither 0 nor 1");
01114 }
01115 }
01116 }
01117
01118 template<size_t _Nb>
01119 template<class _CharT, class _Traits, class _Alloc>
01120 void
01121 bitset<_Nb>::_M_copy_to_string(basic_string<_CharT, _Traits, _Alloc>& __s) const
01122 {
01123 __s.assign(_Nb, '0');
01124 for (size_t __i = 0; __i < _Nb; ++__i)
01125 if (_Unchecked_test(__i))
01126 __s[_Nb - 1 - __i] = '1';
01127 }
01128
01129
01131
01139 template<size_t _Nb>
01140 inline bitset<_Nb>
01141 operator&(const bitset<_Nb>& __x, const bitset<_Nb>& __y)
01142 {
01143 bitset<_Nb> __result(__x);
01144 __result &= __y;
01145 return __result;
01146 }
01147
01148 template<size_t _Nb>
01149 inline bitset<_Nb>
01150 operator|(const bitset<_Nb>& __x, const bitset<_Nb>& __y)
01151 {
01152 bitset<_Nb> __result(__x);
01153 __result |= __y;
01154 return __result;
01155 }
01156
01157 template <size_t _Nb>
01158 inline bitset<_Nb>
01159 operator^(const bitset<_Nb>& __x, const bitset<_Nb>& __y)
01160 {
01161 bitset<_Nb> __result(__x);
01162 __result ^= __y;
01163 return __result;
01164 }
01166
01168
01176 template<class _CharT, class _Traits, size_t _Nb>
01177 basic_istream<_CharT, _Traits>&
01178 operator>>(basic_istream<_CharT, _Traits>& __is, bitset<_Nb>& __x)
01179 {
01180 typedef typename _Traits::char_type char_type;
01181 basic_string<_CharT, _Traits> __tmp;
01182 __tmp.reserve(_Nb);
01183
01184
01185 typename basic_istream<_CharT, _Traits>::sentry __sentry(__is);
01186 if (__sentry)
01187 {
01188 basic_streambuf<_CharT, _Traits>* __buf = __is.rdbuf();
01189 for (size_t __i = 0; __i < _Nb; ++__i)
01190 {
01191 static typename _Traits::int_type __eof = _Traits::eof();
01192
01193 typename _Traits::int_type __c1 = __buf->sbumpc();
01194 if (_Traits::eq_int_type(__c1, __eof))
01195 {
01196 __is.setstate(ios_base::eofbit);
01197 break;
01198 }
01199 else
01200 {
01201 char_type __c2 = _Traits::to_char_type(__c1);
01202 char_type __c = __is.narrow(__c2, '*');
01203
01204 if (__c == '0' || __c == '1')
01205 __tmp.push_back(__c);
01206 else if (_Traits::eq_int_type(__buf->sputbackc(__c2), __eof))
01207 {
01208 __is.setstate(ios_base::failbit);
01209 break;
01210 }
01211 }
01212 }
01213
01214 if (__tmp.empty() && !_Nb)
01215 __is.setstate(ios_base::failbit);
01216 else
01217 __x._M_copy_from_string(__tmp, static_cast<size_t>(0), _Nb);
01218 }
01219
01220 return __is;
01221 }
01222
01223
01224
01225
01226
01227
01228
01229
01230
01231
01232
01233 template <class _CharT, class _Traits, size_t _Nb>
01234 basic_ostream<_CharT, _Traits>&
01235 operator<<(basic_ostream<_CharT, _Traits>& __os, const bitset<_Nb>& __x)
01236 {
01237 basic_string<_CharT, _Traits> __tmp;
01238 __x._M_copy_to_string(__tmp);
01239 return __os << __tmp;
01240 }
01242 }
01243
01244 #undef _GLIBCPP_BITSET_WORDS
01245
01246 #endif