/home/ntakagi/work/STLport-5.1.5/stlport/stl/_bitset.hGo to the documentation of this file.00001 /* 00002 * Copyright (c) 1998 00003 * Silicon Graphics Computer Systems, Inc. 00004 * 00005 * Copyright (c) 1999 00006 * Boris Fomitchev 00007 * 00008 * This material is provided "as is", with absolutely no warranty expressed 00009 * or implied. Any use is at your own risk. 00010 * 00011 * Permission to use or copy this software for any purpose is hereby granted 00012 * without fee, provided the above notices are retained on all copies. 00013 * Permission to modify the code and to distribute modified code is granted, 00014 * provided the above notices are retained, and a notice that the code was 00015 * modified is included with the above copyright notice. 00016 * 00017 */ 00018 00019 #ifndef _STLP_BITSET_H 00020 #define _STLP_BITSET_H 00021 00022 // A bitset of size N has N % (sizeof(unsigned long) * CHAR_BIT) unused 00023 // bits. (They are the high- order bits in the highest word.) It is 00024 // a class invariant of class bitset<> that those unused bits are 00025 // always zero. 00026 00027 // Most of the actual code isn't contained in bitset<> itself, but in the 00028 // base class _Base_bitset. The base class works with whole words, not with 00029 // individual bits. This allows us to specialize _Base_bitset for the 00030 // important special case where the bitset is only a single word. 00031 00032 // The C++ standard does not define the precise semantics of operator[]. 00033 // In this implementation the const version of operator[] is equivalent 00034 // to test(), except that it does no range checking. The non-const version 00035 // returns a reference to a bit, again without doing any range checking. 00036 00037 00038 #ifndef _STLP_INTERNAL_ALGOBASE_H 00039 # include <stl/_algobase.h> 00040 #endif 00041 00042 #ifndef _STLP_INTERNAL_ALLOC_H 00043 # include <stl/_alloc.h> 00044 #endif 00045 00046 #ifndef _STLP_INTERNAL_ITERATOR_H 00047 # include <stl/_iterator.h> 00048 #endif 00049 00050 #ifndef _STLP_INTERNAL_UNINITIALIZED_H 00051 # include <stl/_uninitialized.h> 00052 #endif 00053 00054 #ifndef _STLP_RANGE_ERRORS_H 00055 # include <stl/_range_errors.h> 00056 #endif 00057 00058 #ifndef _STLP_INTERNAL_STRING_H 00059 # include <stl/_string.h> 00060 #endif 00061 00062 #define __BITS_PER_WORD (CHAR_BIT*sizeof(unsigned long)) 00063 #define __BITSET_WORDS(__n) ((__n + __BITS_PER_WORD - 1)/__BITS_PER_WORD) 00064 00065 _STLP_BEGIN_NAMESPACE 00066 00067 _STLP_MOVE_TO_PRIV_NAMESPACE 00068 00069 // structure to aid in counting bits 00070 class _STLP_CLASS_DECLSPEC _Bs_G 00071 { 00072 public: 00073 //returns the number of bit set within the buffer between __beg and __end. 00074 static size_t _S_count(const unsigned char *__beg, const unsigned char *__end) 00075 #if defined (_STLP_USE_NO_IOSTREAMS) 00076 { 00077 size_t __result = 0; 00078 for (; __beg != __end; ++__beg) { 00079 for (size_t i = 0; i < (sizeof(unsigned char) * 8); ++i) { 00080 if ((*__beg & (1 << i)) != 0) { ++__result; } 00081 } 00082 } 00083 return __result; 00084 } 00085 #else 00086 ; 00087 #endif 00088 // Mapping from 8 bit unsigned integers to the index of the first one bit set: 00089 static unsigned char _S_first_one(unsigned char __x) 00090 #if defined (_STLP_USE_NO_IOSTREAMS) 00091 { 00092 for (unsigned char i = 0; i < (sizeof(unsigned char) * 8); ++i) { 00093 if ((__x & (1 << i)) != 0) { return i; } 00094 } 00095 return 0; 00096 } 00097 #else 00098 ; 00099 #endif 00100 }; 00101 00102 // 00103 // Base class: general case. 00104 // 00105 00106 template<size_t _Nw> 00107 struct _Base_bitset { 00108 typedef unsigned long _WordT; 00109 00110 _WordT _M_w[_Nw]; // 0 is the least significant word. 00111 00112 _Base_bitset() { _M_do_reset(); } 00113 00114 _Base_bitset(unsigned long __val) { 00115 _M_do_reset(); 00116 _M_w[0] = __val; 00117 } 00118 00119 static size_t _STLP_CALL _S_whichword( size_t __pos ) { 00120 return __pos / __BITS_PER_WORD; 00121 } 00122 static size_t _STLP_CALL _S_whichbyte( size_t __pos ) { 00123 return (__pos % __BITS_PER_WORD) / CHAR_BIT; 00124 } 00125 static size_t _STLP_CALL _S_whichbit( size_t __pos ) { 00126 return __pos % __BITS_PER_WORD; 00127 } 00128 static _WordT _STLP_CALL _S_maskbit( size_t __pos ) { 00129 return __STATIC_CAST(_WordT,1) << _S_whichbit(__pos); 00130 } 00131 00132 _WordT& _M_getword(size_t __pos) { return _M_w[_S_whichword(__pos)]; } 00133 _WordT _M_getword(size_t __pos) const { return _M_w[_S_whichword(__pos)]; } 00134 00135 _WordT& _M_hiword() { return _M_w[_Nw - 1]; } 00136 _WordT _M_hiword() const { return _M_w[_Nw - 1]; } 00137 00138 void _M_do_and(const _Base_bitset<_Nw>& __x) { 00139 for ( size_t __i = 0; __i < _Nw; __i++ ) { 00140 _M_w[__i] &= __x._M_w[__i]; 00141 } 00142 } 00143 00144 void _M_do_or(const _Base_bitset<_Nw>& __x) { 00145 for ( size_t __i = 0; __i < _Nw; __i++ ) { 00146 _M_w[__i] |= __x._M_w[__i]; 00147 } 00148 } 00149 00150 void _M_do_xor(const _Base_bitset<_Nw>& __x) { 00151 for ( size_t __i = 0; __i < _Nw; __i++ ) { 00152 _M_w[__i] ^= __x._M_w[__i]; 00153 } 00154 } 00155 00156 void _M_do_left_shift(size_t __shift); 00157 00158 void _M_do_right_shift(size_t __shift); 00159 00160 void _M_do_flip() { 00161 for ( size_t __i = 0; __i < _Nw; __i++ ) { 00162 _M_w[__i] = ~_M_w[__i]; 00163 } 00164 } 00165 00166 void _M_do_set() { 00167 for ( size_t __i = 0; __i < _Nw; __i++ ) { 00168 _M_w[__i] = ~__STATIC_CAST(_WordT,0); 00169 } 00170 } 00171 00172 void _M_do_reset() { memset(_M_w, 0, _Nw * sizeof(_WordT)); } 00173 00174 bool _M_is_equal(const _Base_bitset<_Nw>& __x) const { 00175 for (size_t __i = 0; __i < _Nw; ++__i) { 00176 if (_M_w[__i] != __x._M_w[__i]) 00177 return false; 00178 } 00179 return true; 00180 } 00181 00182 bool _M_is_any() const { 00183 for ( size_t __i = 0; __i < _Nw ; __i++ ) { 00184 if ( _M_w[__i] != __STATIC_CAST(_WordT,0) ) 00185 return true; 00186 } 00187 return false; 00188 } 00189 00190 size_t _M_do_count() const { 00191 const unsigned char* __byte_ptr = (const unsigned char*)_M_w; 00192 const unsigned char* __end_ptr = (const unsigned char*)(_M_w+_Nw); 00193 00194 return _Bs_G::_S_count(__byte_ptr, __end_ptr); 00195 } 00196 00197 unsigned long _M_do_to_ulong() const; 00198 00199 // find first "on" bit 00200 size_t _M_do_find_first(size_t __not_found) const; 00201 00202 // find the next "on" bit that follows "prev" 00203 size_t _M_do_find_next(size_t __prev, size_t __not_found) const; 00204 }; 00205 00206 // 00207 // Base class: specialization for a single word. 00208 // 00209 _STLP_TEMPLATE_NULL 00210 struct _Base_bitset<1UL> { 00211 typedef unsigned long _WordT; 00212 typedef _Base_bitset<1UL> _Self; 00213 00214 _WordT _M_w; 00215 00216 _Base_bitset( void ) : _M_w(0) {} 00217 _Base_bitset(unsigned long __val) : _M_w(__val) {} 00218 00219 static size_t _STLP_CALL _S_whichword( size_t __pos ) { 00220 return __pos / __BITS_PER_WORD ; 00221 } 00222 static size_t _STLP_CALL _S_whichbyte( size_t __pos ) { 00223 return (__pos % __BITS_PER_WORD) / CHAR_BIT; 00224 } 00225 static size_t _STLP_CALL _S_whichbit( size_t __pos ) { 00226 return __pos % __BITS_PER_WORD; 00227 } 00228 static _WordT _STLP_CALL _S_maskbit( size_t __pos ) { 00229 return (__STATIC_CAST(_WordT,1)) << _S_whichbit(__pos); 00230 } 00231 00232 _WordT& _M_getword(size_t) { return _M_w; } 00233 _WordT _M_getword(size_t) const { return _M_w; } 00234 00235 _WordT& _M_hiword() { return _M_w; } 00236 _WordT _M_hiword() const { return _M_w; } 00237 00238 void _M_do_and(const _Self& __x) { _M_w &= __x._M_w; } 00239 void _M_do_or(const _Self& __x) { _M_w |= __x._M_w; } 00240 void _M_do_xor(const _Self& __x) { _M_w ^= __x._M_w; } 00241 void _M_do_left_shift(size_t __shift) { _M_w <<= __shift; } 00242 void _M_do_right_shift(size_t __shift) { _M_w >>= __shift; } 00243 void _M_do_flip() { _M_w = ~_M_w; } 00244 void _M_do_set() { _M_w = ~__STATIC_CAST(_WordT,0); } 00245 void _M_do_reset() { _M_w = 0; } 00246 00247 bool _M_is_equal(const _Self& __x) const { 00248 return _M_w == __x._M_w; 00249 } 00250 bool _M_is_any() const { 00251 return _M_w != 0; 00252 } 00253 00254 size_t _M_do_count() const { 00255 const unsigned char* __byte_ptr = (const unsigned char*)&_M_w; 00256 const unsigned char* __end_ptr = ((const unsigned char*)&_M_w)+sizeof(_M_w); 00257 return _Bs_G::_S_count(__byte_ptr, __end_ptr); 00258 } 00259 00260 unsigned long _M_do_to_ulong() const { return _M_w; } 00261 00262 inline size_t _M_do_find_first(size_t __not_found) const; 00263 00264 // find the next "on" bit that follows "prev" 00265 inline size_t _M_do_find_next(size_t __prev, size_t __not_found) const; 00266 }; 00267 00268 00269 // ------------------------------------------------------------ 00270 // 00271 // Definitions of should-be-non-inline functions from the single-word version of 00272 // _Base_bitset. 00273 // 00274 inline size_t 00275 _Base_bitset<1UL>::_M_do_find_first(size_t __not_found) const { 00276 // typedef unsigned long _WordT; 00277 _WordT __thisword = _M_w; 00278 00279 if ( __thisword != __STATIC_CAST(_WordT,0) ) { 00280 // find byte within word 00281 for ( size_t __j = 0; __j < sizeof(_WordT); __j++ ) { 00282 unsigned char __this_byte 00283 = __STATIC_CAST(unsigned char,(__thisword & (~(unsigned char)0))); 00284 if ( __this_byte ) 00285 return __j*CHAR_BIT + _Bs_G::_S_first_one(__this_byte); 00286 00287 __thisword >>= CHAR_BIT; 00288 } 00289 } 00290 // not found, so return a value that indicates failure. 00291 return __not_found; 00292 } 00293 00294 inline size_t 00295 _Base_bitset<1UL>::_M_do_find_next(size_t __prev, 00296 size_t __not_found ) const { 00297 // make bound inclusive 00298 ++__prev; 00299 00300 // check out of bounds 00301 if ( __prev >= __BITS_PER_WORD ) 00302 return __not_found; 00303 00304 // search first (and only) word 00305 _WordT __thisword = _M_w; 00306 00307 // mask off bits below bound 00308 __thisword &= (~__STATIC_CAST(_WordT,0)) << _S_whichbit(__prev); 00309 00310 if ( __thisword != __STATIC_CAST(_WordT,0) ) { 00311 // find byte within word 00312 // get first byte into place 00313 __thisword >>= _S_whichbyte(__prev) * CHAR_BIT; 00314 for ( size_t __j = _S_whichbyte(__prev); __j < sizeof(_WordT); __j++ ) { 00315 unsigned char __this_byte 00316 = __STATIC_CAST(unsigned char,(__thisword & (~(unsigned char)0))); 00317 if ( __this_byte ) 00318 return __j*CHAR_BIT + _Bs_G::_S_first_one(__this_byte); 00319 00320 __thisword >>= CHAR_BIT; 00321 } 00322 } 00323 00324 // not found, so return a value that indicates failure. 00325 return __not_found; 00326 } // end _M_do_find_next 00327 00328 00329 // ------------------------------------------------------------ 00330 // Helper class to zero out the unused high-order bits in the highest word. 00331 00332 template <size_t _Extrabits> struct _Sanitize { 00333 static void _STLP_CALL _M_do_sanitize(unsigned long& __val) 00334 { __val &= ~((~__STATIC_CAST(unsigned long,0)) << _Extrabits); } 00335 }; 00336 00337 _STLP_TEMPLATE_NULL struct _Sanitize<0UL> { 00338 static void _STLP_CALL _M_do_sanitize(unsigned long) {} 00339 }; 00340 00341 _STLP_MOVE_TO_STD_NAMESPACE 00342 00343 // ------------------------------------------------------------ 00344 // Class bitset. 00345 // _Nb may be any nonzero number of type size_t. 00346 template<size_t _Nb> 00347 class bitset : public _STLP_PRIV _Base_bitset<__BITSET_WORDS(_Nb) > { 00348 public: 00349 enum { _Words = __BITSET_WORDS(_Nb) } ; 00350 00351 private: 00352 typedef _STLP_PRIV _Base_bitset< _Words > _Base; 00353 00354 void _M_do_sanitize() { 00355 _STLP_PRIV _Sanitize<_Nb%__BITS_PER_WORD >::_M_do_sanitize(this->_M_hiword()); 00356 } 00357 public: 00358 typedef unsigned long _WordT; 00359 struct reference; 00360 friend struct reference; 00361 00362 // bit reference: 00363 struct reference { 00364 typedef _STLP_PRIV _Base_bitset<_Words > _Bitset_base; 00365 typedef bitset<_Nb> _Bitset; 00366 // friend _Bitset; 00367 _WordT *_M_wp; 00368 size_t _M_bpos; 00369 00370 // should be left undefined 00371 reference() {} 00372 00373 reference( _Bitset& __b, size_t __pos ) { 00374 _M_wp = &__b._M_getword(__pos); 00375 _M_bpos = _Bitset_base::_S_whichbit(__pos); 00376 } 00377 00378 public: 00379 ~reference() {} 00380 00381 // for b[i] = __x; 00382 reference& operator=(bool __x) { 00383 if ( __x ) 00384 *_M_wp |= _Bitset_base::_S_maskbit(_M_bpos); 00385 else 00386 *_M_wp &= ~_Bitset_base::_S_maskbit(_M_bpos); 00387 00388 return *this; 00389 } 00390 00391 // for b[i] = b[__j]; 00392 reference& operator=(const reference& __j) { 00393 if ( (*(__j._M_wp) & _Bitset_base::_S_maskbit(__j._M_bpos)) ) 00394 *_M_wp |= _Bitset_base::_S_maskbit(_M_bpos); 00395 else 00396 *_M_wp &= ~_Bitset_base::_S_maskbit(_M_bpos); 00397 00398 return *this; 00399 } 00400 00401 // flips the bit 00402 bool operator~() const { return (*(_M_wp) & _Bitset_base::_S_maskbit(_M_bpos)) == 0; } 00403 00404 // for __x = b[i]; 00405 operator bool() const { return (*(_M_wp) & _Bitset_base::_S_maskbit(_M_bpos)) != 0; } 00406 00407 // for b[i].flip(); 00408 reference& flip() { 00409 *_M_wp ^= _Bitset_base::_S_maskbit(_M_bpos); 00410 return *this; 00411 } 00412 }; 00413 00414 // 23.3.5.1 constructors: 00415 bitset() {} 00416 00417 bitset(unsigned long __val) : _STLP_PRIV _Base_bitset<_Words>(__val) { _M_do_sanitize(); } 00418 00419 #if defined (_STLP_MEMBER_TEMPLATES) 00420 template<class _CharT, class _Traits, class _Alloc> 00421 explicit bitset(const basic_string<_CharT,_Traits,_Alloc>& __s, 00422 size_t __pos = 0) 00423 : _STLP_PRIV _Base_bitset<_Words >() { 00424 if (__pos > __s.size()) 00425 __stl_throw_out_of_range("bitset"); 00426 _M_copy_from_string(__s, __pos, 00427 basic_string<_CharT, _Traits, _Alloc>::npos); 00428 } 00429 template<class _CharT, class _Traits, class _Alloc> 00430 bitset(const basic_string<_CharT, _Traits, _Alloc>& __s, 00431 size_t __pos, 00432 size_t __n) 00433 : _STLP_PRIV _Base_bitset<_Words >() { 00434 if (__pos > __s.size()) 00435 __stl_throw_out_of_range("bitset"); 00436 _M_copy_from_string(__s, __pos, __n); 00437 } 00438 #else /* _STLP_MEMBER_TEMPLATES */ 00439 explicit bitset(const string& __s, 00440 size_t __pos = 0, 00441 size_t __n = (size_t)-1) 00442 : _STLP_PRIV _Base_bitset<_Words >() { 00443 if (__pos > __s.size()) 00444 __stl_throw_out_of_range("bitset"); 00445 _M_copy_from_string(__s, __pos, __n); 00446 } 00447 #endif /* _STLP_MEMBER_TEMPLATES */ 00448 00449 // 23.3.5.2 bitset operations: 00450 bitset<_Nb>& operator&=(const bitset<_Nb>& __rhs) { 00451 this->_M_do_and(__rhs); 00452 return *this; 00453 } 00454 00455 bitset<_Nb>& operator|=(const bitset<_Nb>& __rhs) { 00456 this->_M_do_or(__rhs); 00457 return *this; 00458 } 00459 00460 bitset<_Nb>& operator^=(const bitset<_Nb>& __rhs) { 00461 this->_M_do_xor(__rhs); 00462 return *this; 00463 } 00464 00465 bitset<_Nb>& operator<<=(size_t __pos) { 00466 this->_M_do_left_shift(__pos); 00467 this->_M_do_sanitize(); 00468 return *this; 00469 } 00470 00471 bitset<_Nb>& operator>>=(size_t __pos) { 00472 this->_M_do_right_shift(__pos); 00473 this->_M_do_sanitize(); 00474 return *this; 00475 } 00476 00477 // 00478 // Extension: 00479 // Versions of single-bit set, reset, flip, test with no range checking. 00480 // 00481 00482 bitset<_Nb>& _Unchecked_set(size_t __pos) { 00483 this->_M_getword(__pos) |= _STLP_PRIV _Base_bitset<_Words > ::_S_maskbit(__pos); 00484 return *this; 00485 } 00486 00487 bitset<_Nb>& _Unchecked_set(size_t __pos, int __val) { 00488 if (__val) 00489 this->_M_getword(__pos) |= this->_S_maskbit(__pos); 00490 else 00491 this->_M_getword(__pos) &= ~ this->_S_maskbit(__pos); 00492 00493 return *this; 00494 } 00495 00496 bitset<_Nb>& _Unchecked_reset(size_t __pos) { 00497 this->_M_getword(__pos) &= ~ this->_S_maskbit(__pos); 00498 return *this; 00499 } 00500 00501 bitset<_Nb>& _Unchecked_flip(size_t __pos) { 00502 this->_M_getword(__pos) ^= this->_S_maskbit(__pos); 00503 return *this; 00504 } 00505 00506 bool _Unchecked_test(size_t __pos) const { 00507 return (this->_M_getword(__pos) & this->_S_maskbit(__pos)) != __STATIC_CAST(_WordT,0); 00508 } 00509 00510 // Set, reset, and flip. 00511 00512 bitset<_Nb>& set() { 00513 this->_M_do_set(); 00514 this->_M_do_sanitize(); 00515 return *this; 00516 } 00517 00518 bitset<_Nb>& set(size_t __pos) { 00519 if (__pos >= _Nb) 00520 __stl_throw_out_of_range("bitset"); 00521 return _Unchecked_set(__pos); 00522 } 00523 00524 bitset<_Nb>& set(size_t __pos, int __val) { 00525 if (__pos >= _Nb) 00526 __stl_throw_out_of_range("bitset"); 00527 return _Unchecked_set(__pos, __val); 00528 } 00529 00530 bitset<_Nb>& reset() { 00531 this->_M_do_reset(); 00532 return *this; 00533 } 00534 00535 bitset<_Nb>& reset(size_t __pos) { 00536 if (__pos >= _Nb) 00537 __stl_throw_out_of_range("bitset"); 00538 00539 return _Unchecked_reset(__pos); 00540 } 00541 00542 bitset<_Nb>& flip() { 00543 this->_M_do_flip(); 00544 this->_M_do_sanitize(); 00545 return *this; 00546 } 00547 00548 bitset<_Nb>& flip(size_t __pos) { 00549 if (__pos >= _Nb) 00550 __stl_throw_out_of_range("bitset"); 00551 00552 return _Unchecked_flip(__pos); 00553 } 00554 00555 bitset<_Nb> operator~() const { 00556 return bitset<_Nb>(*this).flip(); 00557 } 00558 00559 // element access: 00560 //for b[i]; 00561 reference operator[](size_t __pos) { return reference(*this,__pos); } 00562 bool operator[](size_t __pos) const { return _Unchecked_test(__pos); } 00563 00564 unsigned long to_ulong() const { return this->_M_do_to_ulong(); } 00565 00566 #if defined (_STLP_MEMBER_TEMPLATES) && !defined (_STLP_NO_EXPLICIT_FUNCTION_TMPL_ARGS) 00567 template <class _CharT, class _Traits, class _Alloc> 00568 basic_string<_CharT, _Traits, _Alloc> to_string() const { 00569 basic_string<_CharT, _Traits, _Alloc> __result; 00570 _M_copy_to_string(__result); 00571 return __result; 00572 } 00573 #else 00574 string to_string() const { 00575 string __result; 00576 _M_copy_to_string(__result); 00577 return __result; 00578 } 00579 #endif /* _STLP_EXPLICIT_FUNCTION_TMPL_ARGS */ 00580 00581 size_t count() const { return this->_M_do_count(); } 00582 00583 size_t size() const { return _Nb; } 00584 00585 bool operator==(const bitset<_Nb>& __rhs) const { 00586 return this->_M_is_equal(__rhs); 00587 } 00588 bool operator!=(const bitset<_Nb>& __rhs) const { 00589 return !this->_M_is_equal(__rhs); 00590 } 00591 00592 bool test(size_t __pos) const { 00593 if (__pos >= _Nb) 00594 __stl_throw_out_of_range("bitset"); 00595 00596 return _Unchecked_test(__pos); 00597 } 00598 00599 bool any() const { return this->_M_is_any(); } 00600 bool none() const { return !this->_M_is_any(); } 00601 00602 bitset<_Nb> operator<<(size_t __pos) const { 00603 bitset<_Nb> __result(*this); 00604 __result <<= __pos ; return __result; 00605 } 00606 bitset<_Nb> operator>>(size_t __pos) const { 00607 bitset<_Nb> __result(*this); 00608 __result >>= __pos ; return __result; 00609 } 00610 00611 #if !defined (_STLP_NO_EXTENSIONS) 00612 // 00613 // EXTENSIONS: bit-find operations. These operations are 00614 // experimental, and are subject to change or removal in future 00615 // versions. 00616 // 00617 00618 // find the index of the first "on" bit 00619 size_t _Find_first() const 00620 { return this->_M_do_find_first(_Nb); } 00621 00622 // find the index of the next "on" bit after prev 00623 size_t _Find_next( size_t __prev ) const 00624 { return this->_M_do_find_next(__prev, _Nb); } 00625 #endif 00626 00627 // 00628 // Definitions of should-be non-inline member functions. 00629 // 00630 #if defined (_STLP_MEMBER_TEMPLATES) 00631 template<class _CharT, class _Traits, class _Alloc> 00632 void _M_copy_from_string(const basic_string<_CharT,_Traits,_Alloc>& __s, 00633 size_t __pos, size_t __n) { 00634 #else 00635 void _M_copy_from_string(const string& __s, 00636 size_t __pos, size_t __n) { 00637 typedef typename string::traits_type _Traits; 00638 #endif 00639 reset(); 00640 size_t __tmp = _Nb; 00641 const size_t __Nbits = (min) (__tmp, (min) (__n, __s.size() - __pos)); 00642 for ( size_t __i= 0; __i < __Nbits; ++__i) { 00643 typename _Traits::int_type __k = _Traits::to_int_type(__s[__pos + __Nbits - __i - 1]); 00644 // boris : widen() ? 00645 if (__k == '1') 00646 set(__i); 00647 else if (__k != '0') 00648 __stl_throw_invalid_argument("bitset"); 00649 } 00650 } 00651 00652 #if defined (_STLP_MEMBER_TEMPLATES) 00653 template <class _CharT, class _Traits, class _Alloc> 00654 void _M_copy_to_string(basic_string<_CharT, _Traits, _Alloc>& __s) const 00655 #else 00656 void _M_copy_to_string(string& __s) const 00657 #endif 00658 { 00659 __s.assign(_Nb, '0'); 00660 00661 for (size_t __i = 0; __i < _Nb; ++__i) { 00662 if (_Unchecked_test(__i)) 00663 __s[_Nb - 1 - __i] = '1'; 00664 } 00665 } 00666 00667 #if !defined (_STLP_MEMBER_TEMPLATES) && !defined (_STLP_NO_WCHAR_T) 00668 void _M_copy_to_string(wstring& __s) const { 00669 __s.assign(_Nb, '0'); 00670 00671 for (size_t __i = 0; __i < _Nb; ++__i) { 00672 if (_Unchecked_test(__i)) 00673 __s[_Nb - 1 - __i] = '1'; 00674 } 00675 } 00676 #endif 00677 00678 #if defined (_STLP_NON_TYPE_TMPL_PARAM_BUG) 00679 bitset<_Nb> operator&(const bitset<_Nb>& __y) const { 00680 bitset<_Nb> __result(*this); 00681 __result &= __y; 00682 return __result; 00683 } 00684 bitset<_Nb> operator|(const bitset<_Nb>& __y) const { 00685 bitset<_Nb> __result(*this); 00686 __result |= __y; 00687 return __result; 00688 } 00689 bitset<_Nb> operator^(const bitset<_Nb>& __y) const { 00690 bitset<_Nb> __result(*this); 00691 __result ^= __y; 00692 return __result; 00693 } 00694 #endif 00695 }; 00696 00697 // ------------------------------------------------------------ 00698 // 00699 // 23.3.5.3 bitset operations: 00700 // 00701 #if ! defined (_STLP_NON_TYPE_TMPL_PARAM_BUG) 00702 template <size_t _Nb> 00703 inline bitset<_Nb> _STLP_CALL 00704 operator&(const bitset<_Nb>& __x, 00705 const bitset<_Nb>& __y) { 00706 bitset<_Nb> __result(__x); 00707 __result &= __y; 00708 return __result; 00709 } 00710 00711 00712 template <size_t _Nb> 00713 inline bitset<_Nb> _STLP_CALL 00714 operator|(const bitset<_Nb>& __x, 00715 const bitset<_Nb>& __y) { 00716 bitset<_Nb> __result(__x); 00717 __result |= __y; 00718 return __result; 00719 } 00720 00721 template <size_t _Nb> 00722 inline bitset<_Nb> _STLP_CALL 00723 operator^(const bitset<_Nb>& __x, 00724 const bitset<_Nb>& __y) { 00725 bitset<_Nb> __result(__x); 00726 __result ^= __y; 00727 return __result; 00728 } 00729 00730 #if !defined (_STLP_USE_NO_IOSTREAMS) 00731 00732 _STLP_END_NAMESPACE 00733 00734 # if !(defined (_STLP_MSVC) && (_STLP_MSVC < 1300)) && \ 00735 !(defined(__SUNPRO_CC) && (__SUNPRO_CC < 0x500)) 00736 00737 #ifndef _STLP_INTERNAL_IOSFWD 00738 # include <stl/_iosfwd.h> 00739 #endif 00740 00741 _STLP_BEGIN_NAMESPACE 00742 00743 template <class _CharT, class _Traits, size_t _Nb> 00744 basic_istream<_CharT, _Traits>& _STLP_CALL 00745 operator>>(basic_istream<_CharT, _Traits>& __is, bitset<_Nb>& __x); 00746 00747 template <class _CharT, class _Traits, size_t _Nb> 00748 basic_ostream<_CharT, _Traits>& _STLP_CALL 00749 operator<<(basic_ostream<_CharT, _Traits>& __os, const bitset<_Nb>& __x); 00750 00751 # else 00752 00753 #ifndef _STLP_STRING_IO_H 00754 # include <stl/_string_io.h> //includes _istream.h and _ostream.h 00755 #endif 00756 00757 _STLP_BEGIN_NAMESPACE 00758 00759 template <size_t _Nb> 00760 istream& _STLP_CALL 00761 operator>>(istream& __is, bitset<_Nb>& __x) { 00762 typedef typename string::traits_type _Traits; 00763 string __tmp; 00764 __tmp.reserve(_Nb); 00765 00766 // Skip whitespace 00767 typename istream::sentry __sentry(__is); 00768 if (__sentry) { 00769 streambuf* __buf = __is.rdbuf(); 00770 for (size_t __i = 0; __i < _Nb; ++__i) { 00771 static typename _Traits::int_type __eof = _Traits::eof(); 00772 00773 typename _Traits::int_type __c1 = __buf->sbumpc(); 00774 if (_Traits::eq_int_type(__c1, __eof)) { 00775 __is.setstate(ios_base::eofbit); 00776 break; 00777 } 00778 else { 00779 typename _Traits::char_type __c2 = _Traits::to_char_type(__c1); 00780 char __c = __is.narrow(__c2, '*'); 00781 00782 if (__c == '0' || __c == '1') 00783 __tmp.push_back(__c); 00784 else if (_Traits::eq_int_type(__buf->sputbackc(__c2), __eof)) { 00785 __is.setstate(ios_base::failbit); 00786 break; 00787 } 00788 } 00789 } 00790 00791 if (__tmp.empty()) 00792 __is.setstate(ios_base::failbit); 00793 else 00794 __x._M_copy_from_string(__tmp, __STATIC_CAST(size_t,0), _Nb); 00795 } 00796 00797 return __is; 00798 } 00799 00800 template <size_t _Nb> 00801 ostream& _STLP_CALL 00802 operator<<(ostream& __os, const bitset<_Nb>& __x) { 00803 string __tmp; 00804 __x._M_copy_to_string(__tmp); 00805 return __os << __tmp; 00806 } 00807 00808 # if !defined (_STLP_NO_WCHAR_T) 00809 00810 template <size_t _Nb> 00811 wistream& _STLP_CALL 00812 operator>>(wistream& __is, bitset<_Nb>& __x) { 00813 typedef typename wstring::traits_type _Traits; 00814 wstring __tmp; 00815 __tmp.reserve(_Nb); 00816 00817 // Skip whitespace 00818 typename wistream::sentry __sentry(__is); 00819 if (__sentry) { 00820 wstreambuf* __buf = __is.rdbuf(); 00821 for (size_t __i = 0; __i < _Nb; ++__i) { 00822 static typename _Traits::int_type __eof = _Traits::eof(); 00823 00824 typename _Traits::int_type __c1 = __buf->sbumpc(); 00825 if (_Traits::eq_int_type(__c1, __eof)) { 00826 __is.setstate(ios_base::eofbit); 00827 break; 00828 } 00829 else { 00830 typename _Traits::char_type __c2 = _Traits::to_char_type(__c1); 00831 char __c = __is.narrow(__c2, '*'); 00832 00833 if (__c == '0' || __c == '1') 00834 __tmp.push_back(__c); 00835 else if (_Traits::eq_int_type(__buf->sputbackc(__c2), __eof)) { 00836 __is.setstate(ios_base::failbit); 00837 break; 00838 } 00839 } 00840 } 00841 00842 if (__tmp.empty()) 00843 __is.setstate(ios_base::failbit); 00844 else 00845 __x._M_copy_from_string(__tmp, __STATIC_CAST(size_t,0), _Nb); 00846 } 00847 00848 return __is; 00849 } 00850 00851 template <size_t _Nb> 00852 wostream& _STLP_CALL 00853 operator<<(wostream& __os, const bitset<_Nb>& __x) { 00854 wstring __tmp; 00855 __x._M_copy_to_string(__tmp); 00856 return __os << __tmp; 00857 } 00858 00859 # endif /* _STLP_NO_WCHAR_T */ 00860 # endif 00861 #endif 00862 00863 #endif /* _STLP_NON_TYPE_TMPL_PARAM_BUG */ 00864 00865 #undef bitset 00866 00867 _STLP_END_NAMESPACE 00868 00869 #undef __BITS_PER_WORD 00870 #undef __BITSET_WORDS 00871 00872 #if !defined (_STLP_LINK_TIME_INSTANTIATION) 00873 # include <stl/_bitset.c> 00874 #endif 00875 00876 #endif /* _STLP_BITSET_H */ 00877 00878 // Local Variables: 00879 // mode:C++ 00880 // End:
Generated on Mon Mar 10 15:32:19 2008 by ![]() |