libstdc++
|
00001 // <bitset> -*- C++ -*- 00002 00003 // Copyright (C) 2001-2013 Free Software Foundation, Inc. 00004 // 00005 // This file is part of the GNU ISO C++ Library. This library is free 00006 // software; you can redistribute it and/or modify it under the 00007 // terms of the GNU General Public License as published by the 00008 // Free Software Foundation; either version 3, or (at your option) 00009 // any later version. 00010 00011 // This library is distributed in the hope that it will be useful, 00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 // GNU General Public License for more details. 00015 00016 // Under Section 7 of GPL version 3, you are granted additional 00017 // permissions described in the GCC Runtime Library Exception, version 00018 // 3.1, as published by the Free Software Foundation. 00019 00020 // You should have received a copy of the GNU General Public License and 00021 // a copy of the GCC Runtime Library Exception along with this program; 00022 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see 00023 // <http://www.gnu.org/licenses/>. 00024 00025 /* 00026 * Copyright (c) 1998 00027 * Silicon Graphics Computer Systems, Inc. 00028 * 00029 * Permission to use, copy, modify, distribute and sell this software 00030 * and its documentation for any purpose is hereby granted without fee, 00031 * provided that the above copyright notice appear in all copies and 00032 * that both that copyright notice and this permission notice appear 00033 * in supporting documentation. Silicon Graphics makes no 00034 * representations about the suitability of this software for any 00035 * purpose. It is provided "as is" without express or implied warranty. 00036 */ 00037 00038 /** @file include/bitset 00039 * This is a Standard C++ Library header. 00040 */ 00041 00042 #ifndef _GLIBCXX_BITSET 00043 #define _GLIBCXX_BITSET 1 00044 00045 #pragma GCC system_header 00046 00047 #include <string> 00048 #include <bits/functexcept.h> // For invalid_argument, out_of_range, 00049 // overflow_error 00050 #include <iosfwd> 00051 #include <bits/cxxabi_forced.h> 00052 00053 #define _GLIBCXX_BITSET_BITS_PER_WORD (__CHAR_BIT__ * __SIZEOF_LONG__) 00054 #define _GLIBCXX_BITSET_WORDS(__n) \ 00055 ((__n) / _GLIBCXX_BITSET_BITS_PER_WORD + \ 00056 ((__n) % _GLIBCXX_BITSET_BITS_PER_WORD == 0 ? 0 : 1)) 00057 00058 #define _GLIBCXX_BITSET_BITS_PER_ULL (__CHAR_BIT__ * __SIZEOF_LONG_LONG__) 00059 00060 namespace std _GLIBCXX_VISIBILITY(default) 00061 { 00062 _GLIBCXX_BEGIN_NAMESPACE_CONTAINER 00063 00064 /** 00065 * Base class, general case. It is a class invariant that _Nw will be 00066 * nonnegative. 00067 * 00068 * See documentation for bitset. 00069 */ 00070 template<size_t _Nw> 00071 struct _Base_bitset 00072 { 00073 typedef unsigned long _WordT; 00074 00075 /// 0 is the least significant word. 00076 _WordT _M_w[_Nw]; 00077 00078 _GLIBCXX_CONSTEXPR _Base_bitset() _GLIBCXX_NOEXCEPT 00079 : _M_w() { } 00080 00081 #if __cplusplus >= 201103L 00082 constexpr _Base_bitset(unsigned long long __val) noexcept 00083 : _M_w{ _WordT(__val) 00084 #if __SIZEOF_LONG_LONG__ > __SIZEOF_LONG__ 00085 , _WordT(__val >> _GLIBCXX_BITSET_BITS_PER_WORD) 00086 #endif 00087 } { } 00088 #else 00089 _Base_bitset(unsigned long __val) 00090 : _M_w() 00091 { _M_w[0] = __val; } 00092 #endif 00093 00094 static _GLIBCXX_CONSTEXPR size_t 00095 _S_whichword(size_t __pos) _GLIBCXX_NOEXCEPT 00096 { return __pos / _GLIBCXX_BITSET_BITS_PER_WORD; } 00097 00098 static _GLIBCXX_CONSTEXPR size_t 00099 _S_whichbyte(size_t __pos) _GLIBCXX_NOEXCEPT 00100 { return (__pos % _GLIBCXX_BITSET_BITS_PER_WORD) / __CHAR_BIT__; } 00101 00102 static _GLIBCXX_CONSTEXPR size_t 00103 _S_whichbit(size_t __pos) _GLIBCXX_NOEXCEPT 00104 { return __pos % _GLIBCXX_BITSET_BITS_PER_WORD; } 00105 00106 static _GLIBCXX_CONSTEXPR _WordT 00107 _S_maskbit(size_t __pos) _GLIBCXX_NOEXCEPT 00108 { return (static_cast<_WordT>(1)) << _S_whichbit(__pos); } 00109 00110 _WordT& 00111 _M_getword(size_t __pos) _GLIBCXX_NOEXCEPT 00112 { return _M_w[_S_whichword(__pos)]; } 00113 00114 _GLIBCXX_CONSTEXPR _WordT 00115 _M_getword(size_t __pos) const _GLIBCXX_NOEXCEPT 00116 { return _M_w[_S_whichword(__pos)]; } 00117 00118 #if __cplusplus >= 201103L 00119 const _WordT* 00120 _M_getdata() const noexcept 00121 { return _M_w; } 00122 #endif 00123 00124 _WordT& 00125 _M_hiword() _GLIBCXX_NOEXCEPT 00126 { return _M_w[_Nw - 1]; } 00127 00128 _GLIBCXX_CONSTEXPR _WordT 00129 _M_hiword() const _GLIBCXX_NOEXCEPT 00130 { return _M_w[_Nw - 1]; } 00131 00132 void 00133 _M_do_and(const _Base_bitset<_Nw>& __x) _GLIBCXX_NOEXCEPT 00134 { 00135 for (size_t __i = 0; __i < _Nw; __i++) 00136 _M_w[__i] &= __x._M_w[__i]; 00137 } 00138 00139 void 00140 _M_do_or(const _Base_bitset<_Nw>& __x) _GLIBCXX_NOEXCEPT 00141 { 00142 for (size_t __i = 0; __i < _Nw; __i++) 00143 _M_w[__i] |= __x._M_w[__i]; 00144 } 00145 00146 void 00147 _M_do_xor(const _Base_bitset<_Nw>& __x) _GLIBCXX_NOEXCEPT 00148 { 00149 for (size_t __i = 0; __i < _Nw; __i++) 00150 _M_w[__i] ^= __x._M_w[__i]; 00151 } 00152 00153 void 00154 _M_do_left_shift(size_t __shift) _GLIBCXX_NOEXCEPT; 00155 00156 void 00157 _M_do_right_shift(size_t __shift) _GLIBCXX_NOEXCEPT; 00158 00159 void 00160 _M_do_flip() _GLIBCXX_NOEXCEPT 00161 { 00162 for (size_t __i = 0; __i < _Nw; __i++) 00163 _M_w[__i] = ~_M_w[__i]; 00164 } 00165 00166 void 00167 _M_do_set() _GLIBCXX_NOEXCEPT 00168 { 00169 for (size_t __i = 0; __i < _Nw; __i++) 00170 _M_w[__i] = ~static_cast<_WordT>(0); 00171 } 00172 00173 void 00174 _M_do_reset() _GLIBCXX_NOEXCEPT 00175 { __builtin_memset(_M_w, 0, _Nw * sizeof(_WordT)); } 00176 00177 bool 00178 _M_is_equal(const _Base_bitset<_Nw>& __x) const _GLIBCXX_NOEXCEPT 00179 { 00180 for (size_t __i = 0; __i < _Nw; ++__i) 00181 if (_M_w[__i] != __x._M_w[__i]) 00182 return false; 00183 return true; 00184 } 00185 00186 template<size_t _Nb> 00187 bool 00188 _M_are_all() const _GLIBCXX_NOEXCEPT 00189 { 00190 for (size_t __i = 0; __i < _Nw - 1; __i++) 00191 if (_M_w[__i] != ~static_cast<_WordT>(0)) 00192 return false; 00193 return _M_hiword() == (~static_cast<_WordT>(0) 00194 >> (_Nw * _GLIBCXX_BITSET_BITS_PER_WORD 00195 - _Nb)); 00196 } 00197 00198 bool 00199 _M_is_any() const _GLIBCXX_NOEXCEPT 00200 { 00201 for (size_t __i = 0; __i < _Nw; __i++) 00202 if (_M_w[__i] != static_cast<_WordT>(0)) 00203 return true; 00204 return false; 00205 } 00206 00207 size_t 00208 _M_do_count() const _GLIBCXX_NOEXCEPT 00209 { 00210 size_t __result = 0; 00211 for (size_t __i = 0; __i < _Nw; __i++) 00212 __result += __builtin_popcountl(_M_w[__i]); 00213 return __result; 00214 } 00215 00216 unsigned long 00217 _M_do_to_ulong() const; 00218 00219 #if __cplusplus >= 201103L 00220 unsigned long long 00221 _M_do_to_ullong() const; 00222 #endif 00223 00224 // find first "on" bit 00225 size_t 00226 _M_do_find_first(size_t) const _GLIBCXX_NOEXCEPT; 00227 00228 // find the next "on" bit that follows "prev" 00229 size_t 00230 _M_do_find_next(size_t, size_t) const _GLIBCXX_NOEXCEPT; 00231 }; 00232 00233 // Definitions of non-inline functions from _Base_bitset. 00234 template<size_t _Nw> 00235 void 00236 _Base_bitset<_Nw>::_M_do_left_shift(size_t __shift) _GLIBCXX_NOEXCEPT 00237 { 00238 if (__builtin_expect(__shift != 0, 1)) 00239 { 00240 const size_t __wshift = __shift / _GLIBCXX_BITSET_BITS_PER_WORD; 00241 const size_t __offset = __shift % _GLIBCXX_BITSET_BITS_PER_WORD; 00242 00243 if (__offset == 0) 00244 for (size_t __n = _Nw - 1; __n >= __wshift; --__n) 00245 _M_w[__n] = _M_w[__n - __wshift]; 00246 else 00247 { 00248 const size_t __sub_offset = (_GLIBCXX_BITSET_BITS_PER_WORD 00249 - __offset); 00250 for (size_t __n = _Nw - 1; __n > __wshift; --__n) 00251 _M_w[__n] = ((_M_w[__n - __wshift] << __offset) 00252 | (_M_w[__n - __wshift - 1] >> __sub_offset)); 00253 _M_w[__wshift] = _M_w[0] << __offset; 00254 } 00255 00256 std::fill(_M_w + 0, _M_w + __wshift, static_cast<_WordT>(0)); 00257 } 00258 } 00259 00260 template<size_t _Nw> 00261 void 00262 _Base_bitset<_Nw>::_M_do_right_shift(size_t __shift) _GLIBCXX_NOEXCEPT 00263 { 00264 if (__builtin_expect(__shift != 0, 1)) 00265 { 00266 const size_t __wshift = __shift / _GLIBCXX_BITSET_BITS_PER_WORD; 00267 const size_t __offset = __shift % _GLIBCXX_BITSET_BITS_PER_WORD; 00268 const size_t __limit = _Nw - __wshift - 1; 00269 00270 if (__offset == 0) 00271 for (size_t __n = 0; __n <= __limit; ++__n) 00272 _M_w[__n] = _M_w[__n + __wshift]; 00273 else 00274 { 00275 const size_t __sub_offset = (_GLIBCXX_BITSET_BITS_PER_WORD 00276 - __offset); 00277 for (size_t __n = 0; __n < __limit; ++__n) 00278 _M_w[__n] = ((_M_w[__n + __wshift] >> __offset) 00279 | (_M_w[__n + __wshift + 1] << __sub_offset)); 00280 _M_w[__limit] = _M_w[_Nw-1] >> __offset; 00281 } 00282 00283 std::fill(_M_w + __limit + 1, _M_w + _Nw, static_cast<_WordT>(0)); 00284 } 00285 } 00286 00287 template<size_t _Nw> 00288 unsigned long 00289 _Base_bitset<_Nw>::_M_do_to_ulong() const 00290 { 00291 for (size_t __i = 1; __i < _Nw; ++__i) 00292 if (_M_w[__i]) 00293 __throw_overflow_error(__N("_Base_bitset::_M_do_to_ulong")); 00294 return _M_w[0]; 00295 } 00296 00297 #if __cplusplus >= 201103L 00298 template<size_t _Nw> 00299 unsigned long long 00300 _Base_bitset<_Nw>::_M_do_to_ullong() const 00301 { 00302 const bool __dw = sizeof(unsigned long long) > sizeof(unsigned long); 00303 for (size_t __i = 1 + __dw; __i < _Nw; ++__i) 00304 if (_M_w[__i]) 00305 __throw_overflow_error(__N("_Base_bitset::_M_do_to_ullong")); 00306 00307 if (__dw) 00308 return _M_w[0] + (static_cast<unsigned long long>(_M_w[1]) 00309 << _GLIBCXX_BITSET_BITS_PER_WORD); 00310 return _M_w[0]; 00311 } 00312 #endif 00313 00314 template<size_t _Nw> 00315 size_t 00316 _Base_bitset<_Nw>:: 00317 _M_do_find_first(size_t __not_found) const _GLIBCXX_NOEXCEPT 00318 { 00319 for (size_t __i = 0; __i < _Nw; __i++) 00320 { 00321 _WordT __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 // not found, so return an indication of failure. 00327 return __not_found; 00328 } 00329 00330 template<size_t _Nw> 00331 size_t 00332 _Base_bitset<_Nw>:: 00333 _M_do_find_next(size_t __prev, size_t __not_found) const _GLIBCXX_NOEXCEPT 00334 { 00335 // make bound inclusive 00336 ++__prev; 00337 00338 // check out of bounds 00339 if (__prev >= _Nw * _GLIBCXX_BITSET_BITS_PER_WORD) 00340 return __not_found; 00341 00342 // search first word 00343 size_t __i = _S_whichword(__prev); 00344 _WordT __thisword = _M_w[__i]; 00345 00346 // mask off bits below bound 00347 __thisword &= (~static_cast<_WordT>(0)) << _S_whichbit(__prev); 00348 00349 if (__thisword != static_cast<_WordT>(0)) 00350 return (__i * _GLIBCXX_BITSET_BITS_PER_WORD 00351 + __builtin_ctzl(__thisword)); 00352 00353 // check subsequent words 00354 __i++; 00355 for (; __i < _Nw; __i++) 00356 { 00357 __thisword = _M_w[__i]; 00358 if (__thisword != static_cast<_WordT>(0)) 00359 return (__i * _GLIBCXX_BITSET_BITS_PER_WORD 00360 + __builtin_ctzl(__thisword)); 00361 } 00362 // not found, so return an indication of failure. 00363 return __not_found; 00364 } // end _M_do_find_next 00365 00366 /** 00367 * Base class, specialization for a single word. 00368 * 00369 * See documentation for bitset. 00370 */ 00371 template<> 00372 struct _Base_bitset<1> 00373 { 00374 typedef unsigned long _WordT; 00375 _WordT _M_w; 00376 00377 _GLIBCXX_CONSTEXPR _Base_bitset() _GLIBCXX_NOEXCEPT 00378 : _M_w(0) 00379 { } 00380 00381 #if __cplusplus >= 201103L 00382 constexpr _Base_bitset(unsigned long long __val) noexcept 00383 #else 00384 _Base_bitset(unsigned long __val) 00385 #endif 00386 : _M_w(__val) 00387 { } 00388 00389 static _GLIBCXX_CONSTEXPR size_t 00390 _S_whichword(size_t __pos) _GLIBCXX_NOEXCEPT 00391 { return __pos / _GLIBCXX_BITSET_BITS_PER_WORD; } 00392 00393 static _GLIBCXX_CONSTEXPR size_t 00394 _S_whichbyte(size_t __pos) _GLIBCXX_NOEXCEPT 00395 { return (__pos % _GLIBCXX_BITSET_BITS_PER_WORD) / __CHAR_BIT__; } 00396 00397 static _GLIBCXX_CONSTEXPR size_t 00398 _S_whichbit(size_t __pos) _GLIBCXX_NOEXCEPT 00399 { return __pos % _GLIBCXX_BITSET_BITS_PER_WORD; } 00400 00401 static _GLIBCXX_CONSTEXPR _WordT 00402 _S_maskbit(size_t __pos) _GLIBCXX_NOEXCEPT 00403 { return (static_cast<_WordT>(1)) << _S_whichbit(__pos); } 00404 00405 _WordT& 00406 _M_getword(size_t) _GLIBCXX_NOEXCEPT 00407 { return _M_w; } 00408 00409 _GLIBCXX_CONSTEXPR _WordT 00410 _M_getword(size_t) const _GLIBCXX_NOEXCEPT 00411 { return _M_w; } 00412 00413 #if __cplusplus >= 201103L 00414 const _WordT* 00415 _M_getdata() const noexcept 00416 { return &_M_w; } 00417 #endif 00418 00419 _WordT& 00420 _M_hiword() _GLIBCXX_NOEXCEPT 00421 { return _M_w; } 00422 00423 _GLIBCXX_CONSTEXPR _WordT 00424 _M_hiword() const _GLIBCXX_NOEXCEPT 00425 { return _M_w; } 00426 00427 void 00428 _M_do_and(const _Base_bitset<1>& __x) _GLIBCXX_NOEXCEPT 00429 { _M_w &= __x._M_w; } 00430 00431 void 00432 _M_do_or(const _Base_bitset<1>& __x) _GLIBCXX_NOEXCEPT 00433 { _M_w |= __x._M_w; } 00434 00435 void 00436 _M_do_xor(const _Base_bitset<1>& __x) _GLIBCXX_NOEXCEPT 00437 { _M_w ^= __x._M_w; } 00438 00439 void 00440 _M_do_left_shift(size_t __shift) _GLIBCXX_NOEXCEPT 00441 { _M_w <<= __shift; } 00442 00443 void 00444 _M_do_right_shift(size_t __shift) _GLIBCXX_NOEXCEPT 00445 { _M_w >>= __shift; } 00446 00447 void 00448 _M_do_flip() _GLIBCXX_NOEXCEPT 00449 { _M_w = ~_M_w; } 00450 00451 void 00452 _M_do_set() _GLIBCXX_NOEXCEPT 00453 { _M_w = ~static_cast<_WordT>(0); } 00454 00455 void 00456 _M_do_reset() _GLIBCXX_NOEXCEPT 00457 { _M_w = 0; } 00458 00459 bool 00460 _M_is_equal(const _Base_bitset<1>& __x) const _GLIBCXX_NOEXCEPT 00461 { return _M_w == __x._M_w; } 00462 00463 template<size_t _Nb> 00464 bool 00465 _M_are_all() const _GLIBCXX_NOEXCEPT 00466 { return _M_w == (~static_cast<_WordT>(0) 00467 >> (_GLIBCXX_BITSET_BITS_PER_WORD - _Nb)); } 00468 00469 bool 00470 _M_is_any() const _GLIBCXX_NOEXCEPT 00471 { return _M_w != 0; } 00472 00473 size_t 00474 _M_do_count() const _GLIBCXX_NOEXCEPT 00475 { return __builtin_popcountl(_M_w); } 00476 00477 unsigned long 00478 _M_do_to_ulong() const _GLIBCXX_NOEXCEPT 00479 { return _M_w; } 00480 00481 #if __cplusplus >= 201103L 00482 unsigned long long 00483 _M_do_to_ullong() const noexcept 00484 { return _M_w; } 00485 #endif 00486 00487 size_t 00488 _M_do_find_first(size_t __not_found) const _GLIBCXX_NOEXCEPT 00489 { 00490 if (_M_w != 0) 00491 return __builtin_ctzl(_M_w); 00492 else 00493 return __not_found; 00494 } 00495 00496 // find the next "on" bit that follows "prev" 00497 size_t 00498 _M_do_find_next(size_t __prev, size_t __not_found) const 00499 _GLIBCXX_NOEXCEPT 00500 { 00501 ++__prev; 00502 if (__prev >= ((size_t) _GLIBCXX_BITSET_BITS_PER_WORD)) 00503 return __not_found; 00504 00505 _WordT __x = _M_w >> __prev; 00506 if (__x != 0) 00507 return __builtin_ctzl(__x) + __prev; 00508 else 00509 return __not_found; 00510 } 00511 }; 00512 00513 /** 00514 * Base class, specialization for no storage (zero-length %bitset). 00515 * 00516 * See documentation for bitset. 00517 */ 00518 template<> 00519 struct _Base_bitset<0> 00520 { 00521 typedef unsigned long _WordT; 00522 00523 _GLIBCXX_CONSTEXPR _Base_bitset() _GLIBCXX_NOEXCEPT 00524 { } 00525 00526 #if __cplusplus >= 201103L 00527 constexpr _Base_bitset(unsigned long long) noexcept 00528 #else 00529 _Base_bitset(unsigned long) 00530 #endif 00531 { } 00532 00533 static _GLIBCXX_CONSTEXPR size_t 00534 _S_whichword(size_t __pos) _GLIBCXX_NOEXCEPT 00535 { return __pos / _GLIBCXX_BITSET_BITS_PER_WORD; } 00536 00537 static _GLIBCXX_CONSTEXPR size_t 00538 _S_whichbyte(size_t __pos) _GLIBCXX_NOEXCEPT 00539 { return (__pos % _GLIBCXX_BITSET_BITS_PER_WORD) / __CHAR_BIT__; } 00540 00541 static _GLIBCXX_CONSTEXPR size_t 00542 _S_whichbit(size_t __pos) _GLIBCXX_NOEXCEPT 00543 { return __pos % _GLIBCXX_BITSET_BITS_PER_WORD; } 00544 00545 static _GLIBCXX_CONSTEXPR _WordT 00546 _S_maskbit(size_t __pos) _GLIBCXX_NOEXCEPT 00547 { return (static_cast<_WordT>(1)) << _S_whichbit(__pos); } 00548 00549 // This would normally give access to the data. The bounds-checking 00550 // in the bitset class will prevent the user from getting this far, 00551 // but (1) it must still return an lvalue to compile, and (2) the 00552 // user might call _Unchecked_set directly, in which case this /needs/ 00553 // to fail. Let's not penalize zero-length users unless they actually 00554 // make an unchecked call; all the memory ugliness is therefore 00555 // localized to this single should-never-get-this-far function. 00556 _WordT& 00557 _M_getword(size_t) _GLIBCXX_NOEXCEPT 00558 { 00559 __throw_out_of_range(__N("_Base_bitset::_M_getword")); 00560 return *new _WordT; 00561 } 00562 00563 _GLIBCXX_CONSTEXPR _WordT 00564 _M_getword(size_t __pos) const _GLIBCXX_NOEXCEPT 00565 { return 0; } 00566 00567 _GLIBCXX_CONSTEXPR _WordT 00568 _M_hiword() const _GLIBCXX_NOEXCEPT 00569 { return 0; } 00570 00571 void 00572 _M_do_and(const _Base_bitset<0>&) _GLIBCXX_NOEXCEPT 00573 { } 00574 00575 void 00576 _M_do_or(const _Base_bitset<0>&) _GLIBCXX_NOEXCEPT 00577 { } 00578 00579 void 00580 _M_do_xor(const _Base_bitset<0>&) _GLIBCXX_NOEXCEPT 00581 { } 00582 00583 void 00584 _M_do_left_shift(size_t) _GLIBCXX_NOEXCEPT 00585 { } 00586 00587 void 00588 _M_do_right_shift(size_t) _GLIBCXX_NOEXCEPT 00589 { } 00590 00591 void 00592 _M_do_flip() _GLIBCXX_NOEXCEPT 00593 { } 00594 00595 void 00596 _M_do_set() _GLIBCXX_NOEXCEPT 00597 { } 00598 00599 void 00600 _M_do_reset() _GLIBCXX_NOEXCEPT 00601 { } 00602 00603 // Are all empty bitsets equal to each other? Are they equal to 00604 // themselves? How to compare a thing which has no state? What is 00605 // the sound of one zero-length bitset clapping? 00606 bool 00607 _M_is_equal(const _Base_bitset<0>&) const _GLIBCXX_NOEXCEPT 00608 { return true; } 00609 00610 template<size_t _Nb> 00611 bool 00612 _M_are_all() const _GLIBCXX_NOEXCEPT 00613 { return true; } 00614 00615 bool 00616 _M_is_any() const _GLIBCXX_NOEXCEPT 00617 { return false; } 00618 00619 size_t 00620 _M_do_count() const _GLIBCXX_NOEXCEPT 00621 { return 0; } 00622 00623 unsigned long 00624 _M_do_to_ulong() const _GLIBCXX_NOEXCEPT 00625 { return 0; } 00626 00627 #if __cplusplus >= 201103L 00628 unsigned long long 00629 _M_do_to_ullong() const noexcept 00630 { return 0; } 00631 #endif 00632 00633 // Normally "not found" is the size, but that could also be 00634 // misinterpreted as an index in this corner case. Oh well. 00635 size_t 00636 _M_do_find_first(size_t) const _GLIBCXX_NOEXCEPT 00637 { return 0; } 00638 00639 size_t 00640 _M_do_find_next(size_t, size_t) const _GLIBCXX_NOEXCEPT 00641 { return 0; } 00642 }; 00643 00644 00645 // Helper class to zero out the unused high-order bits in the highest word. 00646 template<size_t _Extrabits> 00647 struct _Sanitize 00648 { 00649 typedef unsigned long _WordT; 00650 00651 static void 00652 _S_do_sanitize(_WordT& __val) _GLIBCXX_NOEXCEPT 00653 { __val &= ~((~static_cast<_WordT>(0)) << _Extrabits); } 00654 }; 00655 00656 template<> 00657 struct _Sanitize<0> 00658 { 00659 typedef unsigned long _WordT; 00660 00661 static void 00662 _S_do_sanitize(_WordT) _GLIBCXX_NOEXCEPT { } 00663 }; 00664 00665 #if __cplusplus >= 201103L 00666 template<size_t _Nb, bool = _Nb < _GLIBCXX_BITSET_BITS_PER_ULL> 00667 struct _Sanitize_val 00668 { 00669 static constexpr unsigned long long 00670 _S_do_sanitize_val(unsigned long long __val) 00671 { return __val; } 00672 }; 00673 00674 template<size_t _Nb> 00675 struct _Sanitize_val<_Nb, true> 00676 { 00677 static constexpr unsigned long long 00678 _S_do_sanitize_val(unsigned long long __val) 00679 { return __val & ~((~static_cast<unsigned long long>(0)) << _Nb); } 00680 }; 00681 #endif 00682 00683 /** 00684 * The %bitset class represents a @e fixed-size sequence of bits. 00685 * 00686 * @ingroup containers 00687 * 00688 * (Note that %bitset does @e not meet the formal requirements of a 00689 * <a href="tables.html#65">container</a>. Mainly, it lacks iterators.) 00690 * 00691 * The template argument, @a Nb, may be any non-negative number, 00692 * specifying the number of bits (e.g., "0", "12", "1024*1024"). 00693 * 00694 * In the general unoptimized case, storage is allocated in word-sized 00695 * blocks. Let B be the number of bits in a word, then (Nb+(B-1))/B 00696 * words will be used for storage. B - Nb%B bits are unused. (They are 00697 * the high-order bits in the highest word.) It is a class invariant 00698 * that those unused bits are always zero. 00699 * 00700 * If you think of %bitset as <em>a simple array of bits</em>, be 00701 * aware that your mental picture is reversed: a %bitset behaves 00702 * the same way as bits in integers do, with the bit at index 0 in 00703 * the <em>least significant / right-hand</em> position, and the bit at 00704 * index Nb-1 in the <em>most significant / left-hand</em> position. 00705 * Thus, unlike other containers, a %bitset's index <em>counts from 00706 * right to left</em>, to put it very loosely. 00707 * 00708 * This behavior is preserved when translating to and from strings. For 00709 * example, the first line of the following program probably prints 00710 * <em>b('a') is 0001100001</em> on a modern ASCII system. 00711 * 00712 * @code 00713 * #include <bitset> 00714 * #include <iostream> 00715 * #include <sstream> 00716 * 00717 * using namespace std; 00718 * 00719 * int main() 00720 * { 00721 * long a = 'a'; 00722 * bitset<10> b(a); 00723 * 00724 * cout << "b('a') is " << b << endl; 00725 * 00726 * ostringstream s; 00727 * s << b; 00728 * string str = s.str(); 00729 * cout << "index 3 in the string is " << str[3] << " but\n" 00730 * << "index 3 in the bitset is " << b[3] << endl; 00731 * } 00732 * @endcode 00733 * 00734 * Also see: 00735 * http://gcc.gnu.org/onlinedocs/libstdc++/manual/bk01pt12ch33s02.html 00736 * for a description of extensions. 00737 * 00738 * Most of the actual code isn't contained in %bitset<> itself, but in the 00739 * base class _Base_bitset. The base class works with whole words, not with 00740 * individual bits. This allows us to specialize _Base_bitset for the 00741 * important special case where the %bitset is only a single word. 00742 * 00743 * Extra confusion can result due to the fact that the storage for 00744 * _Base_bitset @e is a regular array, and is indexed as such. This is 00745 * carefully encapsulated. 00746 */ 00747 template<size_t _Nb> 00748 class bitset 00749 : private _Base_bitset<_GLIBCXX_BITSET_WORDS(_Nb)> 00750 { 00751 private: 00752 typedef _Base_bitset<_GLIBCXX_BITSET_WORDS(_Nb)> _Base; 00753 typedef unsigned long _WordT; 00754 00755 void 00756 _M_do_sanitize() _GLIBCXX_NOEXCEPT 00757 { 00758 typedef _Sanitize<_Nb % _GLIBCXX_BITSET_BITS_PER_WORD> __sanitize_type; 00759 __sanitize_type::_S_do_sanitize(this->_M_hiword()); 00760 } 00761 00762 #if __cplusplus >= 201103L 00763 template<typename> friend class hash; 00764 #endif 00765 00766 public: 00767 /** 00768 * This encapsulates the concept of a single bit. An instance of this 00769 * class is a proxy for an actual bit; this way the individual bit 00770 * operations are done as faster word-size bitwise instructions. 00771 * 00772 * Most users will never need to use this class directly; conversions 00773 * to and from bool are automatic and should be transparent. Overloaded 00774 * operators help to preserve the illusion. 00775 * 00776 * (On a typical system, this <em>bit %reference</em> is 64 00777 * times the size of an actual bit. Ha.) 00778 */ 00779 class reference 00780 { 00781 friend class bitset; 00782 00783 _WordT* _M_wp; 00784 size_t _M_bpos; 00785 00786 // left undefined 00787 reference(); 00788 00789 public: 00790 reference(bitset& __b, size_t __pos) _GLIBCXX_NOEXCEPT 00791 { 00792 _M_wp = &__b._M_getword(__pos); 00793 _M_bpos = _Base::_S_whichbit(__pos); 00794 } 00795 00796 ~reference() _GLIBCXX_NOEXCEPT 00797 { } 00798 00799 // For b[i] = __x; 00800 reference& 00801 operator=(bool __x) _GLIBCXX_NOEXCEPT 00802 { 00803 if (__x) 00804 *_M_wp |= _Base::_S_maskbit(_M_bpos); 00805 else 00806 *_M_wp &= ~_Base::_S_maskbit(_M_bpos); 00807 return *this; 00808 } 00809 00810 // For b[i] = b[__j]; 00811 reference& 00812 operator=(const reference& __j) _GLIBCXX_NOEXCEPT 00813 { 00814 if ((*(__j._M_wp) & _Base::_S_maskbit(__j._M_bpos))) 00815 *_M_wp |= _Base::_S_maskbit(_M_bpos); 00816 else 00817 *_M_wp &= ~_Base::_S_maskbit(_M_bpos); 00818 return *this; 00819 } 00820 00821 // Flips the bit 00822 bool 00823 operator~() const _GLIBCXX_NOEXCEPT 00824 { return (*(_M_wp) & _Base::_S_maskbit(_M_bpos)) == 0; } 00825 00826 // For __x = b[i]; 00827 operator bool() const _GLIBCXX_NOEXCEPT 00828 { return (*(_M_wp) & _Base::_S_maskbit(_M_bpos)) != 0; } 00829 00830 // For b[i].flip(); 00831 reference& 00832 flip() _GLIBCXX_NOEXCEPT 00833 { 00834 *_M_wp ^= _Base::_S_maskbit(_M_bpos); 00835 return *this; 00836 } 00837 }; 00838 friend class reference; 00839 00840 // 23.3.5.1 constructors: 00841 /// All bits set to zero. 00842 _GLIBCXX_CONSTEXPR bitset() _GLIBCXX_NOEXCEPT 00843 { } 00844 00845 /// Initial bits bitwise-copied from a single word (others set to zero). 00846 #if __cplusplus >= 201103L 00847 constexpr bitset(unsigned long long __val) noexcept 00848 : _Base(_Sanitize_val<_Nb>::_S_do_sanitize_val(__val)) { } 00849 #else 00850 bitset(unsigned long __val) 00851 : _Base(__val) 00852 { _M_do_sanitize(); } 00853 #endif 00854 00855 /** 00856 * Use a subset of a string. 00857 * @param __s A string of @a 0 and @a 1 characters. 00858 * @param __position Index of the first character in @a __s to use; 00859 * defaults to zero. 00860 * @throw std::out_of_range If @a pos is bigger the size of @a __s. 00861 * @throw std::invalid_argument If a character appears in the string 00862 * which is neither @a 0 nor @a 1. 00863 */ 00864 template<class _CharT, class _Traits, class _Alloc> 00865 explicit 00866 bitset(const std::basic_string<_CharT, _Traits, _Alloc>& __s, 00867 size_t __position = 0) 00868 : _Base() 00869 { 00870 if (__position > __s.size()) 00871 __throw_out_of_range(__N("bitset::bitset initial position " 00872 "not valid")); 00873 _M_copy_from_string(__s, __position, 00874 std::basic_string<_CharT, _Traits, _Alloc>::npos, 00875 _CharT('0'), _CharT('1')); 00876 } 00877 00878 /** 00879 * Use a subset of a string. 00880 * @param __s A string of @a 0 and @a 1 characters. 00881 * @param __position Index of the first character in @a __s to use. 00882 * @param __n The number of characters to copy. 00883 * @throw std::out_of_range If @a __position is bigger the size 00884 * of @a __s. 00885 * @throw std::invalid_argument If a character appears in the string 00886 * which is neither @a 0 nor @a 1. 00887 */ 00888 template<class _CharT, class _Traits, class _Alloc> 00889 bitset(const std::basic_string<_CharT, _Traits, _Alloc>& __s, 00890 size_t __position, size_t __n) 00891 : _Base() 00892 { 00893 if (__position > __s.size()) 00894 __throw_out_of_range(__N("bitset::bitset initial position " 00895 "not valid")); 00896 _M_copy_from_string(__s, __position, __n, _CharT('0'), _CharT('1')); 00897 } 00898 00899 // _GLIBCXX_RESOLVE_LIB_DEFECTS 00900 // 396. what are characters zero and one. 00901 template<class _CharT, class _Traits, class _Alloc> 00902 bitset(const std::basic_string<_CharT, _Traits, _Alloc>& __s, 00903 size_t __position, size_t __n, 00904 _CharT __zero, _CharT __one = _CharT('1')) 00905 : _Base() 00906 { 00907 if (__position > __s.size()) 00908 __throw_out_of_range(__N("bitset::bitset initial position " 00909 "not valid")); 00910 _M_copy_from_string(__s, __position, __n, __zero, __one); 00911 } 00912 00913 #if __cplusplus >= 201103L 00914 /** 00915 * Construct from a character %array. 00916 * @param __str An %array of characters @a zero and @a one. 00917 * @param __n The number of characters to use. 00918 * @param __zero The character corresponding to the value 0. 00919 * @param __one The character corresponding to the value 1. 00920 * @throw std::invalid_argument If a character appears in the string 00921 * which is neither @a __zero nor @a __one. 00922 */ 00923 template<typename _CharT> 00924 explicit 00925 bitset(const _CharT* __str, 00926 typename std::basic_string<_CharT>::size_type __n 00927 = std::basic_string<_CharT>::npos, 00928 _CharT __zero = _CharT('0'), _CharT __one = _CharT('1')) 00929 : _Base() 00930 { 00931 if (!__str) 00932 __throw_logic_error(__N("bitset::bitset(const _CharT*, ...)")); 00933 00934 if (__n == std::basic_string<_CharT>::npos) 00935 __n = std::char_traits<_CharT>::length(__str); 00936 _M_copy_from_ptr<_CharT, std::char_traits<_CharT>>(__str, __n, 0, 00937 __n, __zero, 00938 __one); 00939 } 00940 #endif 00941 00942 // 23.3.5.2 bitset operations: 00943 //@{ 00944 /** 00945 * Operations on bitsets. 00946 * @param __rhs A same-sized bitset. 00947 * 00948 * These should be self-explanatory. 00949 */ 00950 bitset<_Nb>& 00951 operator&=(const bitset<_Nb>& __rhs) _GLIBCXX_NOEXCEPT 00952 { 00953 this->_M_do_and(__rhs); 00954 return *this; 00955 } 00956 00957 bitset<_Nb>& 00958 operator|=(const bitset<_Nb>& __rhs) _GLIBCXX_NOEXCEPT 00959 { 00960 this->_M_do_or(__rhs); 00961 return *this; 00962 } 00963 00964 bitset<_Nb>& 00965 operator^=(const bitset<_Nb>& __rhs) _GLIBCXX_NOEXCEPT 00966 { 00967 this->_M_do_xor(__rhs); 00968 return *this; 00969 } 00970 //@} 00971 00972 //@{ 00973 /** 00974 * Operations on bitsets. 00975 * @param __position The number of places to shift. 00976 * 00977 * These should be self-explanatory. 00978 */ 00979 bitset<_Nb>& 00980 operator<<=(size_t __position) _GLIBCXX_NOEXCEPT 00981 { 00982 if (__builtin_expect(__position < _Nb, 1)) 00983 { 00984 this->_M_do_left_shift(__position); 00985 this->_M_do_sanitize(); 00986 } 00987 else 00988 this->_M_do_reset(); 00989 return *this; 00990 } 00991 00992 bitset<_Nb>& 00993 operator>>=(size_t __position) _GLIBCXX_NOEXCEPT 00994 { 00995 if (__builtin_expect(__position < _Nb, 1)) 00996 { 00997 this->_M_do_right_shift(__position); 00998 this->_M_do_sanitize(); 00999 } 01000 else 01001 this->_M_do_reset(); 01002 return *this; 01003 } 01004 //@} 01005 01006 //@{ 01007 /** 01008 * These versions of single-bit set, reset, flip, and test are 01009 * extensions from the SGI version. They do no range checking. 01010 * @ingroup SGIextensions 01011 */ 01012 bitset<_Nb>& 01013 _Unchecked_set(size_t __pos) _GLIBCXX_NOEXCEPT 01014 { 01015 this->_M_getword(__pos) |= _Base::_S_maskbit(__pos); 01016 return *this; 01017 } 01018 01019 bitset<_Nb>& 01020 _Unchecked_set(size_t __pos, int __val) _GLIBCXX_NOEXCEPT 01021 { 01022 if (__val) 01023 this->_M_getword(__pos) |= _Base::_S_maskbit(__pos); 01024 else 01025 this->_M_getword(__pos) &= ~_Base::_S_maskbit(__pos); 01026 return *this; 01027 } 01028 01029 bitset<_Nb>& 01030 _Unchecked_reset(size_t __pos) _GLIBCXX_NOEXCEPT 01031 { 01032 this->_M_getword(__pos) &= ~_Base::_S_maskbit(__pos); 01033 return *this; 01034 } 01035 01036 bitset<_Nb>& 01037 _Unchecked_flip(size_t __pos) _GLIBCXX_NOEXCEPT 01038 { 01039 this->_M_getword(__pos) ^= _Base::_S_maskbit(__pos); 01040 return *this; 01041 } 01042 01043 _GLIBCXX_CONSTEXPR bool 01044 _Unchecked_test(size_t __pos) const _GLIBCXX_NOEXCEPT 01045 { return ((this->_M_getword(__pos) & _Base::_S_maskbit(__pos)) 01046 != static_cast<_WordT>(0)); } 01047 //@} 01048 01049 // Set, reset, and flip. 01050 /** 01051 * @brief Sets every bit to true. 01052 */ 01053 bitset<_Nb>& 01054 set() _GLIBCXX_NOEXCEPT 01055 { 01056 this->_M_do_set(); 01057 this->_M_do_sanitize(); 01058 return *this; 01059 } 01060 01061 /** 01062 * @brief Sets a given bit to a particular value. 01063 * @param __position The index of the bit. 01064 * @param __val Either true or false, defaults to true. 01065 * @throw std::out_of_range If @a pos is bigger the size of the %set. 01066 */ 01067 bitset<_Nb>& 01068 set(size_t __position, bool __val = true) 01069 { 01070 if (__position >= _Nb) 01071 __throw_out_of_range(__N("bitset::set")); 01072 return _Unchecked_set(__position, __val); 01073 } 01074 01075 /** 01076 * @brief Sets every bit to false. 01077 */ 01078 bitset<_Nb>& 01079 reset() _GLIBCXX_NOEXCEPT 01080 { 01081 this->_M_do_reset(); 01082 return *this; 01083 } 01084 01085 /** 01086 * @brief Sets a given bit to false. 01087 * @param __position The index of the bit. 01088 * @throw std::out_of_range If @a pos is bigger the size of the %set. 01089 * 01090 * Same as writing @c set(pos,false). 01091 */ 01092 bitset<_Nb>& 01093 reset(size_t __position) 01094 { 01095 if (__position >= _Nb) 01096 __throw_out_of_range(__N("bitset::reset")); 01097 return _Unchecked_reset(__position); 01098 } 01099 01100 /** 01101 * @brief Toggles every bit to its opposite value. 01102 */ 01103 bitset<_Nb>& 01104 flip() _GLIBCXX_NOEXCEPT 01105 { 01106 this->_M_do_flip(); 01107 this->_M_do_sanitize(); 01108 return *this; 01109 } 01110 01111 /** 01112 * @brief Toggles a given bit to its opposite value. 01113 * @param __position The index of the bit. 01114 * @throw std::out_of_range If @a pos is bigger the size of the %set. 01115 */ 01116 bitset<_Nb>& 01117 flip(size_t __position) 01118 { 01119 if (__position >= _Nb) 01120 __throw_out_of_range(__N("bitset::flip")); 01121 return _Unchecked_flip(__position); 01122 } 01123 01124 /// See the no-argument flip(). 01125 bitset<_Nb> 01126 operator~() const _GLIBCXX_NOEXCEPT 01127 { return bitset<_Nb>(*this).flip(); } 01128 01129 //@{ 01130 /** 01131 * @brief Array-indexing support. 01132 * @param __position Index into the %bitset. 01133 * @return A bool for a <em>const %bitset</em>. For non-const 01134 * bitsets, an instance of the reference proxy class. 01135 * @note These operators do no range checking and throw no exceptions, 01136 * as required by DR 11 to the standard. 01137 * 01138 * _GLIBCXX_RESOLVE_LIB_DEFECTS Note that this implementation already 01139 * resolves DR 11 (items 1 and 2), but does not do the range-checking 01140 * required by that DR's resolution. -pme 01141 * The DR has since been changed: range-checking is a precondition 01142 * (users' responsibility), and these functions must not throw. -pme 01143 */ 01144 reference 01145 operator[](size_t __position) 01146 { return reference(*this, __position); } 01147 01148 _GLIBCXX_CONSTEXPR bool 01149 operator[](size_t __position) const 01150 { return _Unchecked_test(__position); } 01151 //@} 01152 01153 /** 01154 * @brief Returns a numerical interpretation of the %bitset. 01155 * @return The integral equivalent of the bits. 01156 * @throw std::overflow_error If there are too many bits to be 01157 * represented in an @c unsigned @c long. 01158 */ 01159 unsigned long 01160 to_ulong() const 01161 { return this->_M_do_to_ulong(); } 01162 01163 #if __cplusplus >= 201103L 01164 unsigned long long 01165 to_ullong() const 01166 { return this->_M_do_to_ullong(); } 01167 #endif 01168 01169 /** 01170 * @brief Returns a character interpretation of the %bitset. 01171 * @return The string equivalent of the bits. 01172 * 01173 * Note the ordering of the bits: decreasing character positions 01174 * correspond to increasing bit positions (see the main class notes for 01175 * an example). 01176 */ 01177 template<class _CharT, class _Traits, class _Alloc> 01178 std::basic_string<_CharT, _Traits, _Alloc> 01179 to_string() const 01180 { 01181 std::basic_string<_CharT, _Traits, _Alloc> __result; 01182 _M_copy_to_string(__result, _CharT('0'), _CharT('1')); 01183 return __result; 01184 } 01185 01186 // _GLIBCXX_RESOLVE_LIB_DEFECTS 01187 // 396. what are characters zero and one. 01188 template<class _CharT, class _Traits, class _Alloc> 01189 std::basic_string<_CharT, _Traits, _Alloc> 01190 to_string(_CharT __zero, _CharT __one = _CharT('1')) const 01191 { 01192 std::basic_string<_CharT, _Traits, _Alloc> __result; 01193 _M_copy_to_string(__result, __zero, __one); 01194 return __result; 01195 } 01196 01197 // _GLIBCXX_RESOLVE_LIB_DEFECTS 01198 // 434. bitset::to_string() hard to use. 01199 template<class _CharT, class _Traits> 01200 std::basic_string<_CharT, _Traits, std::allocator<_CharT> > 01201 to_string() const 01202 { return to_string<_CharT, _Traits, std::allocator<_CharT> >(); } 01203 01204 // _GLIBCXX_RESOLVE_LIB_DEFECTS 01205 // 853. to_string needs updating with zero and one. 01206 template<class _CharT, class _Traits> 01207 std::basic_string<_CharT, _Traits, std::allocator<_CharT> > 01208 to_string(_CharT __zero, _CharT __one = _CharT('1')) const 01209 { return to_string<_CharT, _Traits, 01210 std::allocator<_CharT> >(__zero, __one); } 01211 01212 template<class _CharT> 01213 std::basic_string<_CharT, std::char_traits<_CharT>, 01214 std::allocator<_CharT> > 01215 to_string() const 01216 { 01217 return to_string<_CharT, std::char_traits<_CharT>, 01218 std::allocator<_CharT> >(); 01219 } 01220 01221 template<class _CharT> 01222 std::basic_string<_CharT, std::char_traits<_CharT>, 01223 std::allocator<_CharT> > 01224 to_string(_CharT __zero, _CharT __one = _CharT('1')) const 01225 { 01226 return to_string<_CharT, std::char_traits<_CharT>, 01227 std::allocator<_CharT> >(__zero, __one); 01228 } 01229 01230 std::basic_string<char, std::char_traits<char>, std::allocator<char> > 01231 to_string() const 01232 { 01233 return to_string<char, std::char_traits<char>, 01234 std::allocator<char> >(); 01235 } 01236 01237 std::basic_string<char, std::char_traits<char>, std::allocator<char> > 01238 to_string(char __zero, char __one = '1') const 01239 { 01240 return to_string<char, std::char_traits<char>, 01241 std::allocator<char> >(__zero, __one); 01242 } 01243 01244 // Helper functions for string operations. 01245 template<class _CharT, class _Traits> 01246 void 01247 _M_copy_from_ptr(const _CharT*, size_t, size_t, size_t, 01248 _CharT, _CharT); 01249 01250 template<class _CharT, class _Traits, class _Alloc> 01251 void 01252 _M_copy_from_string(const std::basic_string<_CharT, 01253 _Traits, _Alloc>& __s, size_t __pos, size_t __n, 01254 _CharT __zero, _CharT __one) 01255 { _M_copy_from_ptr<_CharT, _Traits>(__s.data(), __s.size(), __pos, __n, 01256 __zero, __one); } 01257 01258 template<class _CharT, class _Traits, class _Alloc> 01259 void 01260 _M_copy_to_string(std::basic_string<_CharT, _Traits, _Alloc>&, 01261 _CharT, _CharT) const; 01262 01263 // NB: Backward compat. 01264 template<class _CharT, class _Traits, class _Alloc> 01265 void 01266 _M_copy_from_string(const std::basic_string<_CharT, 01267 _Traits, _Alloc>& __s, size_t __pos, size_t __n) 01268 { _M_copy_from_string(__s, __pos, __n, _CharT('0'), _CharT('1')); } 01269 01270 template<class _CharT, class _Traits, class _Alloc> 01271 void 01272 _M_copy_to_string(std::basic_string<_CharT, _Traits,_Alloc>& __s) const 01273 { _M_copy_to_string(__s, _CharT('0'), _CharT('1')); } 01274 01275 /// Returns the number of bits which are set. 01276 size_t 01277 count() const _GLIBCXX_NOEXCEPT 01278 { return this->_M_do_count(); } 01279 01280 /// Returns the total number of bits. 01281 _GLIBCXX_CONSTEXPR size_t 01282 size() const _GLIBCXX_NOEXCEPT 01283 { return _Nb; } 01284 01285 //@{ 01286 /// These comparisons for equality/inequality are, well, @e bitwise. 01287 bool 01288 operator==(const bitset<_Nb>& __rhs) const _GLIBCXX_NOEXCEPT 01289 { return this->_M_is_equal(__rhs); } 01290 01291 bool 01292 operator!=(const bitset<_Nb>& __rhs) const _GLIBCXX_NOEXCEPT 01293 { return !this->_M_is_equal(__rhs); } 01294 //@} 01295 01296 /** 01297 * @brief Tests the value of a bit. 01298 * @param __position The index of a bit. 01299 * @return The value at @a pos. 01300 * @throw std::out_of_range If @a pos is bigger the size of the %set. 01301 */ 01302 bool 01303 test(size_t __position) const 01304 { 01305 if (__position >= _Nb) 01306 __throw_out_of_range(__N("bitset::test")); 01307 return _Unchecked_test(__position); 01308 } 01309 01310 // _GLIBCXX_RESOLVE_LIB_DEFECTS 01311 // DR 693. std::bitset::all() missing. 01312 /** 01313 * @brief Tests whether all the bits are on. 01314 * @return True if all the bits are set. 01315 */ 01316 bool 01317 all() const _GLIBCXX_NOEXCEPT 01318 { return this->template _M_are_all<_Nb>(); } 01319 01320 /** 01321 * @brief Tests whether any of the bits are on. 01322 * @return True if at least one bit is set. 01323 */ 01324 bool 01325 any() const _GLIBCXX_NOEXCEPT 01326 { return this->_M_is_any(); } 01327 01328 /** 01329 * @brief Tests whether any of the bits are on. 01330 * @return True if none of the bits are set. 01331 */ 01332 bool 01333 none() const _GLIBCXX_NOEXCEPT 01334 { return !this->_M_is_any(); } 01335 01336 //@{ 01337 /// Self-explanatory. 01338 bitset<_Nb> 01339 operator<<(size_t __position) const _GLIBCXX_NOEXCEPT 01340 { return bitset<_Nb>(*this) <<= __position; } 01341 01342 bitset<_Nb> 01343 operator>>(size_t __position) const _GLIBCXX_NOEXCEPT 01344 { return bitset<_Nb>(*this) >>= __position; } 01345 //@} 01346 01347 /** 01348 * @brief Finds the index of the first "on" bit. 01349 * @return The index of the first bit set, or size() if not found. 01350 * @ingroup SGIextensions 01351 * @sa _Find_next 01352 */ 01353 size_t 01354 _Find_first() const _GLIBCXX_NOEXCEPT 01355 { return this->_M_do_find_first(_Nb); } 01356 01357 /** 01358 * @brief Finds the index of the next "on" bit after prev. 01359 * @return The index of the next bit set, or size() if not found. 01360 * @param __prev Where to start searching. 01361 * @ingroup SGIextensions 01362 * @sa _Find_first 01363 */ 01364 size_t 01365 _Find_next(size_t __prev) const _GLIBCXX_NOEXCEPT 01366 { return this->_M_do_find_next(__prev, _Nb); } 01367 }; 01368 01369 // Definitions of non-inline member functions. 01370 template<size_t _Nb> 01371 template<class _CharT, class _Traits> 01372 void 01373 bitset<_Nb>:: 01374 _M_copy_from_ptr(const _CharT* __s, size_t __len, 01375 size_t __pos, size_t __n, _CharT __zero, _CharT __one) 01376 { 01377 reset(); 01378 const size_t __nbits = std::min(_Nb, std::min(__n, size_t(__len - __pos))); 01379 for (size_t __i = __nbits; __i > 0; --__i) 01380 { 01381 const _CharT __c = __s[__pos + __nbits - __i]; 01382 if (_Traits::eq(__c, __zero)) 01383 ; 01384 else if (_Traits::eq(__c, __one)) 01385 _Unchecked_set(__i - 1); 01386 else 01387 __throw_invalid_argument(__N("bitset::_M_copy_from_ptr")); 01388 } 01389 } 01390 01391 template<size_t _Nb> 01392 template<class _CharT, class _Traits, class _Alloc> 01393 void 01394 bitset<_Nb>:: 01395 _M_copy_to_string(std::basic_string<_CharT, _Traits, _Alloc>& __s, 01396 _CharT __zero, _CharT __one) const 01397 { 01398 __s.assign(_Nb, __zero); 01399 for (size_t __i = _Nb; __i > 0; --__i) 01400 if (_Unchecked_test(__i - 1)) 01401 _Traits::assign(__s[_Nb - __i], __one); 01402 } 01403 01404 // 23.3.5.3 bitset operations: 01405 //@{ 01406 /** 01407 * @brief Global bitwise operations on bitsets. 01408 * @param __x A bitset. 01409 * @param __y A bitset of the same size as @a __x. 01410 * @return A new bitset. 01411 * 01412 * These should be self-explanatory. 01413 */ 01414 template<size_t _Nb> 01415 inline bitset<_Nb> 01416 operator&(const bitset<_Nb>& __x, const bitset<_Nb>& __y) _GLIBCXX_NOEXCEPT 01417 { 01418 bitset<_Nb> __result(__x); 01419 __result &= __y; 01420 return __result; 01421 } 01422 01423 template<size_t _Nb> 01424 inline bitset<_Nb> 01425 operator|(const bitset<_Nb>& __x, const bitset<_Nb>& __y) _GLIBCXX_NOEXCEPT 01426 { 01427 bitset<_Nb> __result(__x); 01428 __result |= __y; 01429 return __result; 01430 } 01431 01432 template <size_t _Nb> 01433 inline bitset<_Nb> 01434 operator^(const bitset<_Nb>& __x, const bitset<_Nb>& __y) _GLIBCXX_NOEXCEPT 01435 { 01436 bitset<_Nb> __result(__x); 01437 __result ^= __y; 01438 return __result; 01439 } 01440 //@} 01441 01442 //@{ 01443 /** 01444 * @brief Global I/O operators for bitsets. 01445 * 01446 * Direct I/O between streams and bitsets is supported. Output is 01447 * straightforward. Input will skip whitespace, only accept @a 0 and @a 1 01448 * characters, and will only extract as many digits as the %bitset will 01449 * hold. 01450 */ 01451 template<class _CharT, class _Traits, size_t _Nb> 01452 std::basic_istream<_CharT, _Traits>& 01453 operator>>(std::basic_istream<_CharT, _Traits>& __is, bitset<_Nb>& __x) 01454 { 01455 typedef typename _Traits::char_type char_type; 01456 typedef std::basic_istream<_CharT, _Traits> __istream_type; 01457 typedef typename __istream_type::ios_base __ios_base; 01458 01459 std::basic_string<_CharT, _Traits> __tmp; 01460 __tmp.reserve(_Nb); 01461 01462 // _GLIBCXX_RESOLVE_LIB_DEFECTS 01463 // 303. Bitset input operator underspecified 01464 const char_type __zero = __is.widen('0'); 01465 const char_type __one = __is.widen('1'); 01466 01467 typename __ios_base::iostate __state = __ios_base::goodbit; 01468 typename __istream_type::sentry __sentry(__is); 01469 if (__sentry) 01470 { 01471 __try 01472 { 01473 for (size_t __i = _Nb; __i > 0; --__i) 01474 { 01475 static typename _Traits::int_type __eof = _Traits::eof(); 01476 01477 typename _Traits::int_type __c1 = __is.rdbuf()->sbumpc(); 01478 if (_Traits::eq_int_type(__c1, __eof)) 01479 { 01480 __state |= __ios_base::eofbit; 01481 break; 01482 } 01483 else 01484 { 01485 const char_type __c2 = _Traits::to_char_type(__c1); 01486 if (_Traits::eq(__c2, __zero)) 01487 __tmp.push_back(__zero); 01488 else if (_Traits::eq(__c2, __one)) 01489 __tmp.push_back(__one); 01490 else if (_Traits:: 01491 eq_int_type(__is.rdbuf()->sputbackc(__c2), 01492 __eof)) 01493 { 01494 __state |= __ios_base::failbit; 01495 break; 01496 } 01497 } 01498 } 01499 } 01500 __catch(__cxxabiv1::__forced_unwind&) 01501 { 01502 __is._M_setstate(__ios_base::badbit); 01503 __throw_exception_again; 01504 } 01505 __catch(...) 01506 { __is._M_setstate(__ios_base::badbit); } 01507 } 01508 01509 if (__tmp.empty() && _Nb) 01510 __state |= __ios_base::failbit; 01511 else 01512 __x._M_copy_from_string(__tmp, static_cast<size_t>(0), _Nb, 01513 __zero, __one); 01514 if (__state) 01515 __is.setstate(__state); 01516 return __is; 01517 } 01518 01519 template <class _CharT, class _Traits, size_t _Nb> 01520 std::basic_ostream<_CharT, _Traits>& 01521 operator<<(std::basic_ostream<_CharT, _Traits>& __os, 01522 const bitset<_Nb>& __x) 01523 { 01524 std::basic_string<_CharT, _Traits> __tmp; 01525 01526 // _GLIBCXX_RESOLVE_LIB_DEFECTS 01527 // 396. what are characters zero and one. 01528 const ctype<_CharT>& __ct = use_facet<ctype<_CharT> >(__os.getloc()); 01529 __x._M_copy_to_string(__tmp, __ct.widen('0'), __ct.widen('1')); 01530 return __os << __tmp; 01531 } 01532 //@} 01533 01534 _GLIBCXX_END_NAMESPACE_CONTAINER 01535 } // namespace std 01536 01537 #undef _GLIBCXX_BITSET_WORDS 01538 #undef _GLIBCXX_BITSET_BITS_PER_WORD 01539 #undef _GLIBCXX_BITSET_BITS_PER_ULL 01540 01541 #if __cplusplus >= 201103L 01542 01543 #include <bits/functional_hash.h> 01544 01545 namespace std _GLIBCXX_VISIBILITY(default) 01546 { 01547 _GLIBCXX_BEGIN_NAMESPACE_VERSION 01548 01549 // DR 1182. 01550 /// std::hash specialization for bitset. 01551 template<size_t _Nb> 01552 struct hash<_GLIBCXX_STD_C::bitset<_Nb>> 01553 : public __hash_base<size_t, _GLIBCXX_STD_C::bitset<_Nb>> 01554 { 01555 size_t 01556 operator()(const _GLIBCXX_STD_C::bitset<_Nb>& __b) const noexcept 01557 { 01558 const size_t __clength = (_Nb + __CHAR_BIT__ - 1) / __CHAR_BIT__; 01559 return std::_Hash_impl::hash(__b._M_getdata(), __clength); 01560 } 01561 }; 01562 01563 template<> 01564 struct hash<_GLIBCXX_STD_C::bitset<0>> 01565 : public __hash_base<size_t, _GLIBCXX_STD_C::bitset<0>> 01566 { 01567 size_t 01568 operator()(const _GLIBCXX_STD_C::bitset<0>&) const noexcept 01569 { return 0; } 01570 }; 01571 01572 _GLIBCXX_END_NAMESPACE_VERSION 01573 } // namespace 01574 01575 #endif // C++11 01576 01577 #ifdef _GLIBCXX_DEBUG 01578 # include <debug/bitset> 01579 #endif 01580 01581 #ifdef _GLIBCXX_PROFILE 01582 # include <profile/bitset> 01583 #endif 01584 01585 #endif /* _GLIBCXX_BITSET */