libstdc++
|
00001 // <bitset> -*- C++ -*- 00002 00003 // Copyright (C) 2001-2014 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 template<class _CharT, class _Traits, class _Alloc> 00756 void 00757 _M_check_initial_position(const std::basic_string<_CharT, _Traits, _Alloc>& __s, 00758 size_t __position) const 00759 { 00760 if (__position > __s.size()) 00761 __throw_out_of_range_fmt(__N("bitset::bitset: __position " 00762 "(which is %zu) > __s.size() " 00763 "(which is %zu)"), 00764 __position, __s.size()); 00765 } 00766 00767 void _M_check(size_t __position, const char *__s) const 00768 { 00769 if (__position >= _Nb) 00770 __throw_out_of_range_fmt(__N("%s: __position (which is %zu) " 00771 ">= _Nb (which is %zu)"), 00772 __s, __position, _Nb); 00773 } 00774 00775 void 00776 _M_do_sanitize() _GLIBCXX_NOEXCEPT 00777 { 00778 typedef _Sanitize<_Nb % _GLIBCXX_BITSET_BITS_PER_WORD> __sanitize_type; 00779 __sanitize_type::_S_do_sanitize(this->_M_hiword()); 00780 } 00781 00782 #if __cplusplus >= 201103L 00783 template<typename> friend struct hash; 00784 #endif 00785 00786 public: 00787 /** 00788 * This encapsulates the concept of a single bit. An instance of this 00789 * class is a proxy for an actual bit; this way the individual bit 00790 * operations are done as faster word-size bitwise instructions. 00791 * 00792 * Most users will never need to use this class directly; conversions 00793 * to and from bool are automatic and should be transparent. Overloaded 00794 * operators help to preserve the illusion. 00795 * 00796 * (On a typical system, this <em>bit %reference</em> is 64 00797 * times the size of an actual bit. Ha.) 00798 */ 00799 class reference 00800 { 00801 friend class bitset; 00802 00803 _WordT* _M_wp; 00804 size_t _M_bpos; 00805 00806 // left undefined 00807 reference(); 00808 00809 public: 00810 reference(bitset& __b, size_t __pos) _GLIBCXX_NOEXCEPT 00811 { 00812 _M_wp = &__b._M_getword(__pos); 00813 _M_bpos = _Base::_S_whichbit(__pos); 00814 } 00815 00816 ~reference() _GLIBCXX_NOEXCEPT 00817 { } 00818 00819 // For b[i] = __x; 00820 reference& 00821 operator=(bool __x) _GLIBCXX_NOEXCEPT 00822 { 00823 if (__x) 00824 *_M_wp |= _Base::_S_maskbit(_M_bpos); 00825 else 00826 *_M_wp &= ~_Base::_S_maskbit(_M_bpos); 00827 return *this; 00828 } 00829 00830 // For b[i] = b[__j]; 00831 reference& 00832 operator=(const reference& __j) _GLIBCXX_NOEXCEPT 00833 { 00834 if ((*(__j._M_wp) & _Base::_S_maskbit(__j._M_bpos))) 00835 *_M_wp |= _Base::_S_maskbit(_M_bpos); 00836 else 00837 *_M_wp &= ~_Base::_S_maskbit(_M_bpos); 00838 return *this; 00839 } 00840 00841 // Flips the bit 00842 bool 00843 operator~() const _GLIBCXX_NOEXCEPT 00844 { return (*(_M_wp) & _Base::_S_maskbit(_M_bpos)) == 0; } 00845 00846 // For __x = b[i]; 00847 operator bool() const _GLIBCXX_NOEXCEPT 00848 { return (*(_M_wp) & _Base::_S_maskbit(_M_bpos)) != 0; } 00849 00850 // For b[i].flip(); 00851 reference& 00852 flip() _GLIBCXX_NOEXCEPT 00853 { 00854 *_M_wp ^= _Base::_S_maskbit(_M_bpos); 00855 return *this; 00856 } 00857 }; 00858 friend class reference; 00859 00860 // 23.3.5.1 constructors: 00861 /// All bits set to zero. 00862 _GLIBCXX_CONSTEXPR bitset() _GLIBCXX_NOEXCEPT 00863 { } 00864 00865 /// Initial bits bitwise-copied from a single word (others set to zero). 00866 #if __cplusplus >= 201103L 00867 constexpr bitset(unsigned long long __val) noexcept 00868 : _Base(_Sanitize_val<_Nb>::_S_do_sanitize_val(__val)) { } 00869 #else 00870 bitset(unsigned long __val) 00871 : _Base(__val) 00872 { _M_do_sanitize(); } 00873 #endif 00874 00875 /** 00876 * Use a subset of a string. 00877 * @param __s A string of @a 0 and @a 1 characters. 00878 * @param __position Index of the first character in @a __s to use; 00879 * defaults to zero. 00880 * @throw std::out_of_range If @a pos is bigger the size of @a __s. 00881 * @throw std::invalid_argument If a character appears in the string 00882 * which is neither @a 0 nor @a 1. 00883 */ 00884 template<class _CharT, class _Traits, class _Alloc> 00885 explicit 00886 bitset(const std::basic_string<_CharT, _Traits, _Alloc>& __s, 00887 size_t __position = 0) 00888 : _Base() 00889 { 00890 _M_check_initial_position(__s, __position); 00891 _M_copy_from_string(__s, __position, 00892 std::basic_string<_CharT, _Traits, _Alloc>::npos, 00893 _CharT('0'), _CharT('1')); 00894 } 00895 00896 /** 00897 * Use a subset of a string. 00898 * @param __s A string of @a 0 and @a 1 characters. 00899 * @param __position Index of the first character in @a __s to use. 00900 * @param __n The number of characters to copy. 00901 * @throw std::out_of_range If @a __position is bigger the size 00902 * of @a __s. 00903 * @throw std::invalid_argument If a character appears in the string 00904 * which is neither @a 0 nor @a 1. 00905 */ 00906 template<class _CharT, class _Traits, class _Alloc> 00907 bitset(const std::basic_string<_CharT, _Traits, _Alloc>& __s, 00908 size_t __position, size_t __n) 00909 : _Base() 00910 { 00911 _M_check_initial_position(__s, __position); 00912 _M_copy_from_string(__s, __position, __n, _CharT('0'), _CharT('1')); 00913 } 00914 00915 // _GLIBCXX_RESOLVE_LIB_DEFECTS 00916 // 396. what are characters zero and one. 00917 template<class _CharT, class _Traits, class _Alloc> 00918 bitset(const std::basic_string<_CharT, _Traits, _Alloc>& __s, 00919 size_t __position, size_t __n, 00920 _CharT __zero, _CharT __one = _CharT('1')) 00921 : _Base() 00922 { 00923 _M_check_initial_position(__s, __position); 00924 _M_copy_from_string(__s, __position, __n, __zero, __one); 00925 } 00926 00927 #if __cplusplus >= 201103L 00928 /** 00929 * Construct from a character %array. 00930 * @param __str An %array of characters @a zero and @a one. 00931 * @param __n The number of characters to use. 00932 * @param __zero The character corresponding to the value 0. 00933 * @param __one The character corresponding to the value 1. 00934 * @throw std::invalid_argument If a character appears in the string 00935 * which is neither @a __zero nor @a __one. 00936 */ 00937 template<typename _CharT> 00938 explicit 00939 bitset(const _CharT* __str, 00940 typename std::basic_string<_CharT>::size_type __n 00941 = std::basic_string<_CharT>::npos, 00942 _CharT __zero = _CharT('0'), _CharT __one = _CharT('1')) 00943 : _Base() 00944 { 00945 if (!__str) 00946 __throw_logic_error(__N("bitset::bitset(const _CharT*, ...)")); 00947 00948 if (__n == std::basic_string<_CharT>::npos) 00949 __n = std::char_traits<_CharT>::length(__str); 00950 _M_copy_from_ptr<_CharT, std::char_traits<_CharT>>(__str, __n, 0, 00951 __n, __zero, 00952 __one); 00953 } 00954 #endif 00955 00956 // 23.3.5.2 bitset operations: 00957 //@{ 00958 /** 00959 * Operations on bitsets. 00960 * @param __rhs A same-sized bitset. 00961 * 00962 * These should be self-explanatory. 00963 */ 00964 bitset<_Nb>& 00965 operator&=(const bitset<_Nb>& __rhs) _GLIBCXX_NOEXCEPT 00966 { 00967 this->_M_do_and(__rhs); 00968 return *this; 00969 } 00970 00971 bitset<_Nb>& 00972 operator|=(const bitset<_Nb>& __rhs) _GLIBCXX_NOEXCEPT 00973 { 00974 this->_M_do_or(__rhs); 00975 return *this; 00976 } 00977 00978 bitset<_Nb>& 00979 operator^=(const bitset<_Nb>& __rhs) _GLIBCXX_NOEXCEPT 00980 { 00981 this->_M_do_xor(__rhs); 00982 return *this; 00983 } 00984 //@} 00985 00986 //@{ 00987 /** 00988 * Operations on bitsets. 00989 * @param __position The number of places to shift. 00990 * 00991 * These should be self-explanatory. 00992 */ 00993 bitset<_Nb>& 00994 operator<<=(size_t __position) _GLIBCXX_NOEXCEPT 00995 { 00996 if (__builtin_expect(__position < _Nb, 1)) 00997 { 00998 this->_M_do_left_shift(__position); 00999 this->_M_do_sanitize(); 01000 } 01001 else 01002 this->_M_do_reset(); 01003 return *this; 01004 } 01005 01006 bitset<_Nb>& 01007 operator>>=(size_t __position) _GLIBCXX_NOEXCEPT 01008 { 01009 if (__builtin_expect(__position < _Nb, 1)) 01010 { 01011 this->_M_do_right_shift(__position); 01012 this->_M_do_sanitize(); 01013 } 01014 else 01015 this->_M_do_reset(); 01016 return *this; 01017 } 01018 //@} 01019 01020 //@{ 01021 /** 01022 * These versions of single-bit set, reset, flip, and test are 01023 * extensions from the SGI version. They do no range checking. 01024 * @ingroup SGIextensions 01025 */ 01026 bitset<_Nb>& 01027 _Unchecked_set(size_t __pos) _GLIBCXX_NOEXCEPT 01028 { 01029 this->_M_getword(__pos) |= _Base::_S_maskbit(__pos); 01030 return *this; 01031 } 01032 01033 bitset<_Nb>& 01034 _Unchecked_set(size_t __pos, int __val) _GLIBCXX_NOEXCEPT 01035 { 01036 if (__val) 01037 this->_M_getword(__pos) |= _Base::_S_maskbit(__pos); 01038 else 01039 this->_M_getword(__pos) &= ~_Base::_S_maskbit(__pos); 01040 return *this; 01041 } 01042 01043 bitset<_Nb>& 01044 _Unchecked_reset(size_t __pos) _GLIBCXX_NOEXCEPT 01045 { 01046 this->_M_getword(__pos) &= ~_Base::_S_maskbit(__pos); 01047 return *this; 01048 } 01049 01050 bitset<_Nb>& 01051 _Unchecked_flip(size_t __pos) _GLIBCXX_NOEXCEPT 01052 { 01053 this->_M_getword(__pos) ^= _Base::_S_maskbit(__pos); 01054 return *this; 01055 } 01056 01057 _GLIBCXX_CONSTEXPR bool 01058 _Unchecked_test(size_t __pos) const _GLIBCXX_NOEXCEPT 01059 { return ((this->_M_getword(__pos) & _Base::_S_maskbit(__pos)) 01060 != static_cast<_WordT>(0)); } 01061 //@} 01062 01063 // Set, reset, and flip. 01064 /** 01065 * @brief Sets every bit to true. 01066 */ 01067 bitset<_Nb>& 01068 set() _GLIBCXX_NOEXCEPT 01069 { 01070 this->_M_do_set(); 01071 this->_M_do_sanitize(); 01072 return *this; 01073 } 01074 01075 /** 01076 * @brief Sets a given bit to a particular value. 01077 * @param __position The index of the bit. 01078 * @param __val Either true or false, defaults to true. 01079 * @throw std::out_of_range If @a pos is bigger the size of the %set. 01080 */ 01081 bitset<_Nb>& 01082 set(size_t __position, bool __val = true) 01083 { 01084 this->_M_check(__position, __N("bitset::set")); 01085 return _Unchecked_set(__position, __val); 01086 } 01087 01088 /** 01089 * @brief Sets every bit to false. 01090 */ 01091 bitset<_Nb>& 01092 reset() _GLIBCXX_NOEXCEPT 01093 { 01094 this->_M_do_reset(); 01095 return *this; 01096 } 01097 01098 /** 01099 * @brief Sets a given bit to false. 01100 * @param __position The index of the bit. 01101 * @throw std::out_of_range If @a pos is bigger the size of the %set. 01102 * 01103 * Same as writing @c set(pos,false). 01104 */ 01105 bitset<_Nb>& 01106 reset(size_t __position) 01107 { 01108 this->_M_check(__position, __N("bitset::reset")); 01109 return _Unchecked_reset(__position); 01110 } 01111 01112 /** 01113 * @brief Toggles every bit to its opposite value. 01114 */ 01115 bitset<_Nb>& 01116 flip() _GLIBCXX_NOEXCEPT 01117 { 01118 this->_M_do_flip(); 01119 this->_M_do_sanitize(); 01120 return *this; 01121 } 01122 01123 /** 01124 * @brief Toggles a given bit to its opposite value. 01125 * @param __position The index of the bit. 01126 * @throw std::out_of_range If @a pos is bigger the size of the %set. 01127 */ 01128 bitset<_Nb>& 01129 flip(size_t __position) 01130 { 01131 this->_M_check(__position, __N("bitset::flip")); 01132 return _Unchecked_flip(__position); 01133 } 01134 01135 /// See the no-argument flip(). 01136 bitset<_Nb> 01137 operator~() const _GLIBCXX_NOEXCEPT 01138 { return bitset<_Nb>(*this).flip(); } 01139 01140 //@{ 01141 /** 01142 * @brief Array-indexing support. 01143 * @param __position Index into the %bitset. 01144 * @return A bool for a <em>const %bitset</em>. For non-const 01145 * bitsets, an instance of the reference proxy class. 01146 * @note These operators do no range checking and throw no exceptions, 01147 * as required by DR 11 to the standard. 01148 * 01149 * _GLIBCXX_RESOLVE_LIB_DEFECTS Note that this implementation already 01150 * resolves DR 11 (items 1 and 2), but does not do the range-checking 01151 * required by that DR's resolution. -pme 01152 * The DR has since been changed: range-checking is a precondition 01153 * (users' responsibility), and these functions must not throw. -pme 01154 */ 01155 reference 01156 operator[](size_t __position) 01157 { return reference(*this, __position); } 01158 01159 _GLIBCXX_CONSTEXPR bool 01160 operator[](size_t __position) const 01161 { return _Unchecked_test(__position); } 01162 //@} 01163 01164 /** 01165 * @brief Returns a numerical interpretation of the %bitset. 01166 * @return The integral equivalent of the bits. 01167 * @throw std::overflow_error If there are too many bits to be 01168 * represented in an @c unsigned @c long. 01169 */ 01170 unsigned long 01171 to_ulong() const 01172 { return this->_M_do_to_ulong(); } 01173 01174 #if __cplusplus >= 201103L 01175 unsigned long long 01176 to_ullong() const 01177 { return this->_M_do_to_ullong(); } 01178 #endif 01179 01180 /** 01181 * @brief Returns a character interpretation of the %bitset. 01182 * @return The string equivalent of the bits. 01183 * 01184 * Note the ordering of the bits: decreasing character positions 01185 * correspond to increasing bit positions (see the main class notes for 01186 * an example). 01187 */ 01188 template<class _CharT, class _Traits, class _Alloc> 01189 std::basic_string<_CharT, _Traits, _Alloc> 01190 to_string() const 01191 { 01192 std::basic_string<_CharT, _Traits, _Alloc> __result; 01193 _M_copy_to_string(__result, _CharT('0'), _CharT('1')); 01194 return __result; 01195 } 01196 01197 // _GLIBCXX_RESOLVE_LIB_DEFECTS 01198 // 396. what are characters zero and one. 01199 template<class _CharT, class _Traits, class _Alloc> 01200 std::basic_string<_CharT, _Traits, _Alloc> 01201 to_string(_CharT __zero, _CharT __one = _CharT('1')) const 01202 { 01203 std::basic_string<_CharT, _Traits, _Alloc> __result; 01204 _M_copy_to_string(__result, __zero, __one); 01205 return __result; 01206 } 01207 01208 // _GLIBCXX_RESOLVE_LIB_DEFECTS 01209 // 434. bitset::to_string() hard to use. 01210 template<class _CharT, class _Traits> 01211 std::basic_string<_CharT, _Traits, std::allocator<_CharT> > 01212 to_string() const 01213 { return to_string<_CharT, _Traits, std::allocator<_CharT> >(); } 01214 01215 // _GLIBCXX_RESOLVE_LIB_DEFECTS 01216 // 853. to_string needs updating with zero and one. 01217 template<class _CharT, class _Traits> 01218 std::basic_string<_CharT, _Traits, std::allocator<_CharT> > 01219 to_string(_CharT __zero, _CharT __one = _CharT('1')) const 01220 { return to_string<_CharT, _Traits, 01221 std::allocator<_CharT> >(__zero, __one); } 01222 01223 template<class _CharT> 01224 std::basic_string<_CharT, std::char_traits<_CharT>, 01225 std::allocator<_CharT> > 01226 to_string() const 01227 { 01228 return to_string<_CharT, std::char_traits<_CharT>, 01229 std::allocator<_CharT> >(); 01230 } 01231 01232 template<class _CharT> 01233 std::basic_string<_CharT, std::char_traits<_CharT>, 01234 std::allocator<_CharT> > 01235 to_string(_CharT __zero, _CharT __one = _CharT('1')) const 01236 { 01237 return to_string<_CharT, std::char_traits<_CharT>, 01238 std::allocator<_CharT> >(__zero, __one); 01239 } 01240 01241 std::basic_string<char, std::char_traits<char>, std::allocator<char> > 01242 to_string() const 01243 { 01244 return to_string<char, std::char_traits<char>, 01245 std::allocator<char> >(); 01246 } 01247 01248 std::basic_string<char, std::char_traits<char>, std::allocator<char> > 01249 to_string(char __zero, char __one = '1') const 01250 { 01251 return to_string<char, std::char_traits<char>, 01252 std::allocator<char> >(__zero, __one); 01253 } 01254 01255 // Helper functions for string operations. 01256 template<class _CharT, class _Traits> 01257 void 01258 _M_copy_from_ptr(const _CharT*, size_t, size_t, size_t, 01259 _CharT, _CharT); 01260 01261 template<class _CharT, class _Traits, class _Alloc> 01262 void 01263 _M_copy_from_string(const std::basic_string<_CharT, 01264 _Traits, _Alloc>& __s, size_t __pos, size_t __n, 01265 _CharT __zero, _CharT __one) 01266 { _M_copy_from_ptr<_CharT, _Traits>(__s.data(), __s.size(), __pos, __n, 01267 __zero, __one); } 01268 01269 template<class _CharT, class _Traits, class _Alloc> 01270 void 01271 _M_copy_to_string(std::basic_string<_CharT, _Traits, _Alloc>&, 01272 _CharT, _CharT) const; 01273 01274 // NB: Backward compat. 01275 template<class _CharT, class _Traits, class _Alloc> 01276 void 01277 _M_copy_from_string(const std::basic_string<_CharT, 01278 _Traits, _Alloc>& __s, size_t __pos, size_t __n) 01279 { _M_copy_from_string(__s, __pos, __n, _CharT('0'), _CharT('1')); } 01280 01281 template<class _CharT, class _Traits, class _Alloc> 01282 void 01283 _M_copy_to_string(std::basic_string<_CharT, _Traits,_Alloc>& __s) const 01284 { _M_copy_to_string(__s, _CharT('0'), _CharT('1')); } 01285 01286 /// Returns the number of bits which are set. 01287 size_t 01288 count() const _GLIBCXX_NOEXCEPT 01289 { return this->_M_do_count(); } 01290 01291 /// Returns the total number of bits. 01292 _GLIBCXX_CONSTEXPR size_t 01293 size() const _GLIBCXX_NOEXCEPT 01294 { return _Nb; } 01295 01296 //@{ 01297 /// These comparisons for equality/inequality are, well, @e bitwise. 01298 bool 01299 operator==(const bitset<_Nb>& __rhs) const _GLIBCXX_NOEXCEPT 01300 { return this->_M_is_equal(__rhs); } 01301 01302 bool 01303 operator!=(const bitset<_Nb>& __rhs) const _GLIBCXX_NOEXCEPT 01304 { return !this->_M_is_equal(__rhs); } 01305 //@} 01306 01307 /** 01308 * @brief Tests the value of a bit. 01309 * @param __position The index of a bit. 01310 * @return The value at @a pos. 01311 * @throw std::out_of_range If @a pos is bigger the size of the %set. 01312 */ 01313 bool 01314 test(size_t __position) const 01315 { 01316 this->_M_check(__position, __N("bitset::test")); 01317 return _Unchecked_test(__position); 01318 } 01319 01320 // _GLIBCXX_RESOLVE_LIB_DEFECTS 01321 // DR 693. std::bitset::all() missing. 01322 /** 01323 * @brief Tests whether all the bits are on. 01324 * @return True if all the bits are set. 01325 */ 01326 bool 01327 all() const _GLIBCXX_NOEXCEPT 01328 { return this->template _M_are_all<_Nb>(); } 01329 01330 /** 01331 * @brief Tests whether any of the bits are on. 01332 * @return True if at least one bit is set. 01333 */ 01334 bool 01335 any() const _GLIBCXX_NOEXCEPT 01336 { return this->_M_is_any(); } 01337 01338 /** 01339 * @brief Tests whether any of the bits are on. 01340 * @return True if none of the bits are set. 01341 */ 01342 bool 01343 none() const _GLIBCXX_NOEXCEPT 01344 { return !this->_M_is_any(); } 01345 01346 //@{ 01347 /// Self-explanatory. 01348 bitset<_Nb> 01349 operator<<(size_t __position) const _GLIBCXX_NOEXCEPT 01350 { return bitset<_Nb>(*this) <<= __position; } 01351 01352 bitset<_Nb> 01353 operator>>(size_t __position) const _GLIBCXX_NOEXCEPT 01354 { return bitset<_Nb>(*this) >>= __position; } 01355 //@} 01356 01357 /** 01358 * @brief Finds the index of the first "on" bit. 01359 * @return The index of the first bit set, or size() if not found. 01360 * @ingroup SGIextensions 01361 * @sa _Find_next 01362 */ 01363 size_t 01364 _Find_first() const _GLIBCXX_NOEXCEPT 01365 { return this->_M_do_find_first(_Nb); } 01366 01367 /** 01368 * @brief Finds the index of the next "on" bit after prev. 01369 * @return The index of the next bit set, or size() if not found. 01370 * @param __prev Where to start searching. 01371 * @ingroup SGIextensions 01372 * @sa _Find_first 01373 */ 01374 size_t 01375 _Find_next(size_t __prev) const _GLIBCXX_NOEXCEPT 01376 { return this->_M_do_find_next(__prev, _Nb); } 01377 }; 01378 01379 // Definitions of non-inline member functions. 01380 template<size_t _Nb> 01381 template<class _CharT, class _Traits> 01382 void 01383 bitset<_Nb>:: 01384 _M_copy_from_ptr(const _CharT* __s, size_t __len, 01385 size_t __pos, size_t __n, _CharT __zero, _CharT __one) 01386 { 01387 reset(); 01388 const size_t __nbits = std::min(_Nb, std::min(__n, size_t(__len - __pos))); 01389 for (size_t __i = __nbits; __i > 0; --__i) 01390 { 01391 const _CharT __c = __s[__pos + __nbits - __i]; 01392 if (_Traits::eq(__c, __zero)) 01393 ; 01394 else if (_Traits::eq(__c, __one)) 01395 _Unchecked_set(__i - 1); 01396 else 01397 __throw_invalid_argument(__N("bitset::_M_copy_from_ptr")); 01398 } 01399 } 01400 01401 template<size_t _Nb> 01402 template<class _CharT, class _Traits, class _Alloc> 01403 void 01404 bitset<_Nb>:: 01405 _M_copy_to_string(std::basic_string<_CharT, _Traits, _Alloc>& __s, 01406 _CharT __zero, _CharT __one) const 01407 { 01408 __s.assign(_Nb, __zero); 01409 for (size_t __i = _Nb; __i > 0; --__i) 01410 if (_Unchecked_test(__i - 1)) 01411 _Traits::assign(__s[_Nb - __i], __one); 01412 } 01413 01414 // 23.3.5.3 bitset operations: 01415 //@{ 01416 /** 01417 * @brief Global bitwise operations on bitsets. 01418 * @param __x A bitset. 01419 * @param __y A bitset of the same size as @a __x. 01420 * @return A new bitset. 01421 * 01422 * These should be self-explanatory. 01423 */ 01424 template<size_t _Nb> 01425 inline bitset<_Nb> 01426 operator&(const bitset<_Nb>& __x, const bitset<_Nb>& __y) _GLIBCXX_NOEXCEPT 01427 { 01428 bitset<_Nb> __result(__x); 01429 __result &= __y; 01430 return __result; 01431 } 01432 01433 template<size_t _Nb> 01434 inline bitset<_Nb> 01435 operator|(const bitset<_Nb>& __x, const bitset<_Nb>& __y) _GLIBCXX_NOEXCEPT 01436 { 01437 bitset<_Nb> __result(__x); 01438 __result |= __y; 01439 return __result; 01440 } 01441 01442 template <size_t _Nb> 01443 inline bitset<_Nb> 01444 operator^(const bitset<_Nb>& __x, const bitset<_Nb>& __y) _GLIBCXX_NOEXCEPT 01445 { 01446 bitset<_Nb> __result(__x); 01447 __result ^= __y; 01448 return __result; 01449 } 01450 //@} 01451 01452 //@{ 01453 /** 01454 * @brief Global I/O operators for bitsets. 01455 * 01456 * Direct I/O between streams and bitsets is supported. Output is 01457 * straightforward. Input will skip whitespace, only accept @a 0 and @a 1 01458 * characters, and will only extract as many digits as the %bitset will 01459 * hold. 01460 */ 01461 template<class _CharT, class _Traits, size_t _Nb> 01462 std::basic_istream<_CharT, _Traits>& 01463 operator>>(std::basic_istream<_CharT, _Traits>& __is, bitset<_Nb>& __x) 01464 { 01465 typedef typename _Traits::char_type char_type; 01466 typedef std::basic_istream<_CharT, _Traits> __istream_type; 01467 typedef typename __istream_type::ios_base __ios_base; 01468 01469 std::basic_string<_CharT, _Traits> __tmp; 01470 __tmp.reserve(_Nb); 01471 01472 // _GLIBCXX_RESOLVE_LIB_DEFECTS 01473 // 303. Bitset input operator underspecified 01474 const char_type __zero = __is.widen('0'); 01475 const char_type __one = __is.widen('1'); 01476 01477 typename __ios_base::iostate __state = __ios_base::goodbit; 01478 typename __istream_type::sentry __sentry(__is); 01479 if (__sentry) 01480 { 01481 __try 01482 { 01483 for (size_t __i = _Nb; __i > 0; --__i) 01484 { 01485 static typename _Traits::int_type __eof = _Traits::eof(); 01486 01487 typename _Traits::int_type __c1 = __is.rdbuf()->sbumpc(); 01488 if (_Traits::eq_int_type(__c1, __eof)) 01489 { 01490 __state |= __ios_base::eofbit; 01491 break; 01492 } 01493 else 01494 { 01495 const char_type __c2 = _Traits::to_char_type(__c1); 01496 if (_Traits::eq(__c2, __zero)) 01497 __tmp.push_back(__zero); 01498 else if (_Traits::eq(__c2, __one)) 01499 __tmp.push_back(__one); 01500 else if (_Traits:: 01501 eq_int_type(__is.rdbuf()->sputbackc(__c2), 01502 __eof)) 01503 { 01504 __state |= __ios_base::failbit; 01505 break; 01506 } 01507 } 01508 } 01509 } 01510 __catch(__cxxabiv1::__forced_unwind&) 01511 { 01512 __is._M_setstate(__ios_base::badbit); 01513 __throw_exception_again; 01514 } 01515 __catch(...) 01516 { __is._M_setstate(__ios_base::badbit); } 01517 } 01518 01519 if (__tmp.empty() && _Nb) 01520 __state |= __ios_base::failbit; 01521 else 01522 __x._M_copy_from_string(__tmp, static_cast<size_t>(0), _Nb, 01523 __zero, __one); 01524 if (__state) 01525 __is.setstate(__state); 01526 return __is; 01527 } 01528 01529 template <class _CharT, class _Traits, size_t _Nb> 01530 std::basic_ostream<_CharT, _Traits>& 01531 operator<<(std::basic_ostream<_CharT, _Traits>& __os, 01532 const bitset<_Nb>& __x) 01533 { 01534 std::basic_string<_CharT, _Traits> __tmp; 01535 01536 // _GLIBCXX_RESOLVE_LIB_DEFECTS 01537 // 396. what are characters zero and one. 01538 const ctype<_CharT>& __ct = use_facet<ctype<_CharT> >(__os.getloc()); 01539 __x._M_copy_to_string(__tmp, __ct.widen('0'), __ct.widen('1')); 01540 return __os << __tmp; 01541 } 01542 //@} 01543 01544 _GLIBCXX_END_NAMESPACE_CONTAINER 01545 } // namespace std 01546 01547 #undef _GLIBCXX_BITSET_WORDS 01548 #undef _GLIBCXX_BITSET_BITS_PER_WORD 01549 #undef _GLIBCXX_BITSET_BITS_PER_ULL 01550 01551 #if __cplusplus >= 201103L 01552 01553 #include <bits/functional_hash.h> 01554 01555 namespace std _GLIBCXX_VISIBILITY(default) 01556 { 01557 _GLIBCXX_BEGIN_NAMESPACE_VERSION 01558 01559 // DR 1182. 01560 /// std::hash specialization for bitset. 01561 template<size_t _Nb> 01562 struct hash<_GLIBCXX_STD_C::bitset<_Nb>> 01563 : public __hash_base<size_t, _GLIBCXX_STD_C::bitset<_Nb>> 01564 { 01565 size_t 01566 operator()(const _GLIBCXX_STD_C::bitset<_Nb>& __b) const noexcept 01567 { 01568 const size_t __clength = (_Nb + __CHAR_BIT__ - 1) / __CHAR_BIT__; 01569 return std::_Hash_impl::hash(__b._M_getdata(), __clength); 01570 } 01571 }; 01572 01573 template<> 01574 struct hash<_GLIBCXX_STD_C::bitset<0>> 01575 : public __hash_base<size_t, _GLIBCXX_STD_C::bitset<0>> 01576 { 01577 size_t 01578 operator()(const _GLIBCXX_STD_C::bitset<0>&) const noexcept 01579 { return 0; } 01580 }; 01581 01582 _GLIBCXX_END_NAMESPACE_VERSION 01583 } // namespace 01584 01585 #endif // C++11 01586 01587 #ifdef _GLIBCXX_DEBUG 01588 # include <debug/bitset> 01589 #endif 01590 01591 #ifdef _GLIBCXX_PROFILE 01592 # include <profile/bitset> 01593 #endif 01594 01595 #endif /* _GLIBCXX_BITSET */