1 1.1 mrg // <bitset> -*- C++ -*- 2 1.1 mrg 3 1.12 mrg // Copyright (C) 2001-2022 Free Software Foundation, Inc. 4 1.1 mrg // 5 1.1 mrg // This file is part of the GNU ISO C++ Library. This library is free 6 1.1 mrg // software; you can redistribute it and/or modify it under the 7 1.1 mrg // terms of the GNU General Public License as published by the 8 1.1 mrg // Free Software Foundation; either version 3, or (at your option) 9 1.1 mrg // any later version. 10 1.1 mrg 11 1.1 mrg // This library is distributed in the hope that it will be useful, 12 1.1 mrg // but WITHOUT ANY WARRANTY; without even the implied warranty of 13 1.1 mrg // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 1.1 mrg // GNU General Public License for more details. 15 1.1 mrg 16 1.1 mrg // Under Section 7 of GPL version 3, you are granted additional 17 1.1 mrg // permissions described in the GCC Runtime Library Exception, version 18 1.1 mrg // 3.1, as published by the Free Software Foundation. 19 1.1 mrg 20 1.1 mrg // You should have received a copy of the GNU General Public License and 21 1.1 mrg // a copy of the GCC Runtime Library Exception along with this program; 22 1.1 mrg // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see 23 1.1 mrg // <http://www.gnu.org/licenses/>. 24 1.1 mrg 25 1.1 mrg /* 26 1.1 mrg * Copyright (c) 1998 27 1.1 mrg * Silicon Graphics Computer Systems, Inc. 28 1.1 mrg * 29 1.1 mrg * Permission to use, copy, modify, distribute and sell this software 30 1.1 mrg * and its documentation for any purpose is hereby granted without fee, 31 1.1 mrg * provided that the above copyright notice appear in all copies and 32 1.1 mrg * that both that copyright notice and this permission notice appear 33 1.1 mrg * in supporting documentation. Silicon Graphics makes no 34 1.1 mrg * representations about the suitability of this software for any 35 1.1 mrg * purpose. It is provided "as is" without express or implied warranty. 36 1.1 mrg */ 37 1.1 mrg 38 1.1 mrg /** @file include/bitset 39 1.1 mrg * This is a Standard C++ Library header. 40 1.1 mrg */ 41 1.1 mrg 42 1.1 mrg #ifndef _GLIBCXX_BITSET 43 1.1 mrg #define _GLIBCXX_BITSET 1 44 1.1 mrg 45 1.1 mrg #pragma GCC system_header 46 1.1 mrg 47 1.1 mrg #include <string> 48 1.1 mrg #include <bits/functexcept.h> // For invalid_argument, out_of_range, 49 1.1 mrg // overflow_error 50 1.1 mrg #include <iosfwd> 51 1.3 mrg #include <bits/cxxabi_forced.h> 52 1.1 mrg 53 1.9 mrg #if __cplusplus >= 201103L 54 1.9 mrg # include <bits/functional_hash.h> 55 1.9 mrg #endif 56 1.9 mrg 57 1.3 mrg #define _GLIBCXX_BITSET_BITS_PER_WORD (__CHAR_BIT__ * __SIZEOF_LONG__) 58 1.1 mrg #define _GLIBCXX_BITSET_WORDS(__n) \ 59 1.3 mrg ((__n) / _GLIBCXX_BITSET_BITS_PER_WORD + \ 60 1.3 mrg ((__n) % _GLIBCXX_BITSET_BITS_PER_WORD == 0 ? 0 : 1)) 61 1.1 mrg 62 1.3 mrg #define _GLIBCXX_BITSET_BITS_PER_ULL (__CHAR_BIT__ * __SIZEOF_LONG_LONG__) 63 1.3 mrg 64 1.3 mrg namespace std _GLIBCXX_VISIBILITY(default) 65 1.3 mrg { 66 1.3 mrg _GLIBCXX_BEGIN_NAMESPACE_CONTAINER 67 1.1 mrg 68 1.1 mrg /** 69 1.1 mrg * Base class, general case. It is a class invariant that _Nw will be 70 1.1 mrg * nonnegative. 71 1.1 mrg * 72 1.1 mrg * See documentation for bitset. 73 1.1 mrg */ 74 1.1 mrg template<size_t _Nw> 75 1.1 mrg struct _Base_bitset 76 1.1 mrg { 77 1.1 mrg typedef unsigned long _WordT; 78 1.1 mrg 79 1.1 mrg /// 0 is the least significant word. 80 1.1 mrg _WordT _M_w[_Nw]; 81 1.1 mrg 82 1.3 mrg _GLIBCXX_CONSTEXPR _Base_bitset() _GLIBCXX_NOEXCEPT 83 1.3 mrg : _M_w() { } 84 1.1 mrg 85 1.3 mrg #if __cplusplus >= 201103L 86 1.3 mrg constexpr _Base_bitset(unsigned long long __val) noexcept 87 1.3 mrg : _M_w{ _WordT(__val) 88 1.3 mrg #if __SIZEOF_LONG_LONG__ > __SIZEOF_LONG__ 89 1.3 mrg , _WordT(__val >> _GLIBCXX_BITSET_BITS_PER_WORD) 90 1.3 mrg #endif 91 1.3 mrg } { } 92 1.1 mrg #else 93 1.1 mrg _Base_bitset(unsigned long __val) 94 1.3 mrg : _M_w() 95 1.3 mrg { _M_w[0] = __val; } 96 1.1 mrg #endif 97 1.1 mrg 98 1.3 mrg static _GLIBCXX_CONSTEXPR size_t 99 1.3 mrg _S_whichword(size_t __pos) _GLIBCXX_NOEXCEPT 100 1.1 mrg { return __pos / _GLIBCXX_BITSET_BITS_PER_WORD; } 101 1.1 mrg 102 1.3 mrg static _GLIBCXX_CONSTEXPR size_t 103 1.3 mrg _S_whichbyte(size_t __pos) _GLIBCXX_NOEXCEPT 104 1.1 mrg { return (__pos % _GLIBCXX_BITSET_BITS_PER_WORD) / __CHAR_BIT__; } 105 1.1 mrg 106 1.3 mrg static _GLIBCXX_CONSTEXPR size_t 107 1.3 mrg _S_whichbit(size_t __pos) _GLIBCXX_NOEXCEPT 108 1.1 mrg { return __pos % _GLIBCXX_BITSET_BITS_PER_WORD; } 109 1.1 mrg 110 1.3 mrg static _GLIBCXX_CONSTEXPR _WordT 111 1.3 mrg _S_maskbit(size_t __pos) _GLIBCXX_NOEXCEPT 112 1.1 mrg { return (static_cast<_WordT>(1)) << _S_whichbit(__pos); } 113 1.1 mrg 114 1.1 mrg _WordT& 115 1.3 mrg _M_getword(size_t __pos) _GLIBCXX_NOEXCEPT 116 1.1 mrg { return _M_w[_S_whichword(__pos)]; } 117 1.1 mrg 118 1.3 mrg _GLIBCXX_CONSTEXPR _WordT 119 1.3 mrg _M_getword(size_t __pos) const _GLIBCXX_NOEXCEPT 120 1.1 mrg { return _M_w[_S_whichword(__pos)]; } 121 1.1 mrg 122 1.3 mrg #if __cplusplus >= 201103L 123 1.1 mrg const _WordT* 124 1.3 mrg _M_getdata() const noexcept 125 1.1 mrg { return _M_w; } 126 1.1 mrg #endif 127 1.1 mrg 128 1.1 mrg _WordT& 129 1.3 mrg _M_hiword() _GLIBCXX_NOEXCEPT 130 1.1 mrg { return _M_w[_Nw - 1]; } 131 1.1 mrg 132 1.3 mrg _GLIBCXX_CONSTEXPR _WordT 133 1.3 mrg _M_hiword() const _GLIBCXX_NOEXCEPT 134 1.1 mrg { return _M_w[_Nw - 1]; } 135 1.1 mrg 136 1.1 mrg void 137 1.3 mrg _M_do_and(const _Base_bitset<_Nw>& __x) _GLIBCXX_NOEXCEPT 138 1.1 mrg { 139 1.1 mrg for (size_t __i = 0; __i < _Nw; __i++) 140 1.1 mrg _M_w[__i] &= __x._M_w[__i]; 141 1.1 mrg } 142 1.1 mrg 143 1.1 mrg void 144 1.3 mrg _M_do_or(const _Base_bitset<_Nw>& __x) _GLIBCXX_NOEXCEPT 145 1.1 mrg { 146 1.1 mrg for (size_t __i = 0; __i < _Nw; __i++) 147 1.1 mrg _M_w[__i] |= __x._M_w[__i]; 148 1.1 mrg } 149 1.1 mrg 150 1.1 mrg void 151 1.3 mrg _M_do_xor(const _Base_bitset<_Nw>& __x) _GLIBCXX_NOEXCEPT 152 1.1 mrg { 153 1.1 mrg for (size_t __i = 0; __i < _Nw; __i++) 154 1.1 mrg _M_w[__i] ^= __x._M_w[__i]; 155 1.1 mrg } 156 1.1 mrg 157 1.1 mrg void 158 1.3 mrg _M_do_left_shift(size_t __shift) _GLIBCXX_NOEXCEPT; 159 1.1 mrg 160 1.1 mrg void 161 1.3 mrg _M_do_right_shift(size_t __shift) _GLIBCXX_NOEXCEPT; 162 1.1 mrg 163 1.1 mrg void 164 1.3 mrg _M_do_flip() _GLIBCXX_NOEXCEPT 165 1.1 mrg { 166 1.1 mrg for (size_t __i = 0; __i < _Nw; __i++) 167 1.1 mrg _M_w[__i] = ~_M_w[__i]; 168 1.1 mrg } 169 1.1 mrg 170 1.1 mrg void 171 1.3 mrg _M_do_set() _GLIBCXX_NOEXCEPT 172 1.1 mrg { 173 1.1 mrg for (size_t __i = 0; __i < _Nw; __i++) 174 1.1 mrg _M_w[__i] = ~static_cast<_WordT>(0); 175 1.1 mrg } 176 1.1 mrg 177 1.1 mrg void 178 1.3 mrg _M_do_reset() _GLIBCXX_NOEXCEPT 179 1.1 mrg { __builtin_memset(_M_w, 0, _Nw * sizeof(_WordT)); } 180 1.1 mrg 181 1.1 mrg bool 182 1.3 mrg _M_is_equal(const _Base_bitset<_Nw>& __x) const _GLIBCXX_NOEXCEPT 183 1.1 mrg { 184 1.1 mrg for (size_t __i = 0; __i < _Nw; ++__i) 185 1.1 mrg if (_M_w[__i] != __x._M_w[__i]) 186 1.1 mrg return false; 187 1.1 mrg return true; 188 1.1 mrg } 189 1.1 mrg 190 1.3 mrg template<size_t _Nb> 191 1.3 mrg bool 192 1.3 mrg _M_are_all() const _GLIBCXX_NOEXCEPT 193 1.3 mrg { 194 1.3 mrg for (size_t __i = 0; __i < _Nw - 1; __i++) 195 1.3 mrg if (_M_w[__i] != ~static_cast<_WordT>(0)) 196 1.3 mrg return false; 197 1.3 mrg return _M_hiword() == (~static_cast<_WordT>(0) 198 1.3 mrg >> (_Nw * _GLIBCXX_BITSET_BITS_PER_WORD 199 1.3 mrg - _Nb)); 200 1.3 mrg } 201 1.1 mrg 202 1.1 mrg bool 203 1.3 mrg _M_is_any() const _GLIBCXX_NOEXCEPT 204 1.1 mrg { 205 1.1 mrg for (size_t __i = 0; __i < _Nw; __i++) 206 1.1 mrg if (_M_w[__i] != static_cast<_WordT>(0)) 207 1.1 mrg return true; 208 1.1 mrg return false; 209 1.1 mrg } 210 1.1 mrg 211 1.1 mrg size_t 212 1.3 mrg _M_do_count() const _GLIBCXX_NOEXCEPT 213 1.1 mrg { 214 1.1 mrg size_t __result = 0; 215 1.1 mrg for (size_t __i = 0; __i < _Nw; __i++) 216 1.1 mrg __result += __builtin_popcountl(_M_w[__i]); 217 1.1 mrg return __result; 218 1.1 mrg } 219 1.1 mrg 220 1.1 mrg unsigned long 221 1.1 mrg _M_do_to_ulong() const; 222 1.1 mrg 223 1.3 mrg #if __cplusplus >= 201103L 224 1.1 mrg unsigned long long 225 1.1 mrg _M_do_to_ullong() const; 226 1.1 mrg #endif 227 1.1 mrg 228 1.1 mrg // find first "on" bit 229 1.1 mrg size_t 230 1.3 mrg _M_do_find_first(size_t) const _GLIBCXX_NOEXCEPT; 231 1.1 mrg 232 1.1 mrg // find the next "on" bit that follows "prev" 233 1.1 mrg size_t 234 1.3 mrg _M_do_find_next(size_t, size_t) const _GLIBCXX_NOEXCEPT; 235 1.1 mrg }; 236 1.1 mrg 237 1.1 mrg // Definitions of non-inline functions from _Base_bitset. 238 1.1 mrg template<size_t _Nw> 239 1.1 mrg void 240 1.3 mrg _Base_bitset<_Nw>::_M_do_left_shift(size_t __shift) _GLIBCXX_NOEXCEPT 241 1.1 mrg { 242 1.1 mrg if (__builtin_expect(__shift != 0, 1)) 243 1.1 mrg { 244 1.1 mrg const size_t __wshift = __shift / _GLIBCXX_BITSET_BITS_PER_WORD; 245 1.1 mrg const size_t __offset = __shift % _GLIBCXX_BITSET_BITS_PER_WORD; 246 1.1 mrg 247 1.1 mrg if (__offset == 0) 248 1.1 mrg for (size_t __n = _Nw - 1; __n >= __wshift; --__n) 249 1.1 mrg _M_w[__n] = _M_w[__n - __wshift]; 250 1.1 mrg else 251 1.1 mrg { 252 1.8 mrg const size_t __sub_offset = (_GLIBCXX_BITSET_BITS_PER_WORD 253 1.1 mrg - __offset); 254 1.1 mrg for (size_t __n = _Nw - 1; __n > __wshift; --__n) 255 1.1 mrg _M_w[__n] = ((_M_w[__n - __wshift] << __offset) 256 1.1 mrg | (_M_w[__n - __wshift - 1] >> __sub_offset)); 257 1.1 mrg _M_w[__wshift] = _M_w[0] << __offset; 258 1.1 mrg } 259 1.1 mrg 260 1.1 mrg std::fill(_M_w + 0, _M_w + __wshift, static_cast<_WordT>(0)); 261 1.1 mrg } 262 1.1 mrg } 263 1.1 mrg 264 1.1 mrg template<size_t _Nw> 265 1.1 mrg void 266 1.3 mrg _Base_bitset<_Nw>::_M_do_right_shift(size_t __shift) _GLIBCXX_NOEXCEPT 267 1.1 mrg { 268 1.1 mrg if (__builtin_expect(__shift != 0, 1)) 269 1.1 mrg { 270 1.1 mrg const size_t __wshift = __shift / _GLIBCXX_BITSET_BITS_PER_WORD; 271 1.1 mrg const size_t __offset = __shift % _GLIBCXX_BITSET_BITS_PER_WORD; 272 1.1 mrg const size_t __limit = _Nw - __wshift - 1; 273 1.1 mrg 274 1.1 mrg if (__offset == 0) 275 1.1 mrg for (size_t __n = 0; __n <= __limit; ++__n) 276 1.1 mrg _M_w[__n] = _M_w[__n + __wshift]; 277 1.1 mrg else 278 1.1 mrg { 279 1.1 mrg const size_t __sub_offset = (_GLIBCXX_BITSET_BITS_PER_WORD 280 1.1 mrg - __offset); 281 1.1 mrg for (size_t __n = 0; __n < __limit; ++__n) 282 1.1 mrg _M_w[__n] = ((_M_w[__n + __wshift] >> __offset) 283 1.1 mrg | (_M_w[__n + __wshift + 1] << __sub_offset)); 284 1.1 mrg _M_w[__limit] = _M_w[_Nw-1] >> __offset; 285 1.1 mrg } 286 1.8 mrg 287 1.1 mrg std::fill(_M_w + __limit + 1, _M_w + _Nw, static_cast<_WordT>(0)); 288 1.1 mrg } 289 1.1 mrg } 290 1.1 mrg 291 1.1 mrg template<size_t _Nw> 292 1.1 mrg unsigned long 293 1.1 mrg _Base_bitset<_Nw>::_M_do_to_ulong() const 294 1.1 mrg { 295 1.1 mrg for (size_t __i = 1; __i < _Nw; ++__i) 296 1.1 mrg if (_M_w[__i]) 297 1.1 mrg __throw_overflow_error(__N("_Base_bitset::_M_do_to_ulong")); 298 1.1 mrg return _M_w[0]; 299 1.1 mrg } 300 1.1 mrg 301 1.3 mrg #if __cplusplus >= 201103L 302 1.1 mrg template<size_t _Nw> 303 1.1 mrg unsigned long long 304 1.1 mrg _Base_bitset<_Nw>::_M_do_to_ullong() const 305 1.1 mrg { 306 1.1 mrg const bool __dw = sizeof(unsigned long long) > sizeof(unsigned long); 307 1.1 mrg for (size_t __i = 1 + __dw; __i < _Nw; ++__i) 308 1.1 mrg if (_M_w[__i]) 309 1.1 mrg __throw_overflow_error(__N("_Base_bitset::_M_do_to_ullong")); 310 1.1 mrg 311 1.1 mrg if (__dw) 312 1.1 mrg return _M_w[0] + (static_cast<unsigned long long>(_M_w[1]) 313 1.1 mrg << _GLIBCXX_BITSET_BITS_PER_WORD); 314 1.1 mrg return _M_w[0]; 315 1.1 mrg } 316 1.1 mrg #endif 317 1.1 mrg 318 1.1 mrg template<size_t _Nw> 319 1.1 mrg size_t 320 1.3 mrg _Base_bitset<_Nw>:: 321 1.3 mrg _M_do_find_first(size_t __not_found) const _GLIBCXX_NOEXCEPT 322 1.1 mrg { 323 1.1 mrg for (size_t __i = 0; __i < _Nw; __i++) 324 1.1 mrg { 325 1.1 mrg _WordT __thisword = _M_w[__i]; 326 1.1 mrg if (__thisword != static_cast<_WordT>(0)) 327 1.1 mrg return (__i * _GLIBCXX_BITSET_BITS_PER_WORD 328 1.1 mrg + __builtin_ctzl(__thisword)); 329 1.1 mrg } 330 1.1 mrg // not found, so return an indication of failure. 331 1.1 mrg return __not_found; 332 1.1 mrg } 333 1.1 mrg 334 1.1 mrg template<size_t _Nw> 335 1.1 mrg size_t 336 1.3 mrg _Base_bitset<_Nw>:: 337 1.3 mrg _M_do_find_next(size_t __prev, size_t __not_found) const _GLIBCXX_NOEXCEPT 338 1.1 mrg { 339 1.1 mrg // make bound inclusive 340 1.1 mrg ++__prev; 341 1.1 mrg 342 1.1 mrg // check out of bounds 343 1.1 mrg if (__prev >= _Nw * _GLIBCXX_BITSET_BITS_PER_WORD) 344 1.1 mrg return __not_found; 345 1.1 mrg 346 1.1 mrg // search first word 347 1.1 mrg size_t __i = _S_whichword(__prev); 348 1.1 mrg _WordT __thisword = _M_w[__i]; 349 1.1 mrg 350 1.1 mrg // mask off bits below bound 351 1.1 mrg __thisword &= (~static_cast<_WordT>(0)) << _S_whichbit(__prev); 352 1.1 mrg 353 1.1 mrg if (__thisword != static_cast<_WordT>(0)) 354 1.1 mrg return (__i * _GLIBCXX_BITSET_BITS_PER_WORD 355 1.1 mrg + __builtin_ctzl(__thisword)); 356 1.1 mrg 357 1.1 mrg // check subsequent words 358 1.1 mrg __i++; 359 1.1 mrg for (; __i < _Nw; __i++) 360 1.1 mrg { 361 1.1 mrg __thisword = _M_w[__i]; 362 1.1 mrg if (__thisword != static_cast<_WordT>(0)) 363 1.1 mrg return (__i * _GLIBCXX_BITSET_BITS_PER_WORD 364 1.1 mrg + __builtin_ctzl(__thisword)); 365 1.1 mrg } 366 1.1 mrg // not found, so return an indication of failure. 367 1.1 mrg return __not_found; 368 1.1 mrg } // end _M_do_find_next 369 1.1 mrg 370 1.1 mrg /** 371 1.1 mrg * Base class, specialization for a single word. 372 1.1 mrg * 373 1.1 mrg * See documentation for bitset. 374 1.1 mrg */ 375 1.1 mrg template<> 376 1.1 mrg struct _Base_bitset<1> 377 1.1 mrg { 378 1.1 mrg typedef unsigned long _WordT; 379 1.1 mrg _WordT _M_w; 380 1.1 mrg 381 1.3 mrg _GLIBCXX_CONSTEXPR _Base_bitset() _GLIBCXX_NOEXCEPT 382 1.1 mrg : _M_w(0) 383 1.1 mrg { } 384 1.1 mrg 385 1.3 mrg #if __cplusplus >= 201103L 386 1.3 mrg constexpr _Base_bitset(unsigned long long __val) noexcept 387 1.1 mrg #else 388 1.1 mrg _Base_bitset(unsigned long __val) 389 1.1 mrg #endif 390 1.1 mrg : _M_w(__val) 391 1.1 mrg { } 392 1.1 mrg 393 1.3 mrg static _GLIBCXX_CONSTEXPR size_t 394 1.3 mrg _S_whichword(size_t __pos) _GLIBCXX_NOEXCEPT 395 1.1 mrg { return __pos / _GLIBCXX_BITSET_BITS_PER_WORD; } 396 1.1 mrg 397 1.3 mrg static _GLIBCXX_CONSTEXPR size_t 398 1.3 mrg _S_whichbyte(size_t __pos) _GLIBCXX_NOEXCEPT 399 1.1 mrg { return (__pos % _GLIBCXX_BITSET_BITS_PER_WORD) / __CHAR_BIT__; } 400 1.1 mrg 401 1.3 mrg static _GLIBCXX_CONSTEXPR size_t 402 1.3 mrg _S_whichbit(size_t __pos) _GLIBCXX_NOEXCEPT 403 1.1 mrg { return __pos % _GLIBCXX_BITSET_BITS_PER_WORD; } 404 1.1 mrg 405 1.3 mrg static _GLIBCXX_CONSTEXPR _WordT 406 1.3 mrg _S_maskbit(size_t __pos) _GLIBCXX_NOEXCEPT 407 1.1 mrg { return (static_cast<_WordT>(1)) << _S_whichbit(__pos); } 408 1.1 mrg 409 1.1 mrg _WordT& 410 1.3 mrg _M_getword(size_t) _GLIBCXX_NOEXCEPT 411 1.1 mrg { return _M_w; } 412 1.1 mrg 413 1.3 mrg _GLIBCXX_CONSTEXPR _WordT 414 1.3 mrg _M_getword(size_t) const _GLIBCXX_NOEXCEPT 415 1.1 mrg { return _M_w; } 416 1.1 mrg 417 1.3 mrg #if __cplusplus >= 201103L 418 1.1 mrg const _WordT* 419 1.3 mrg _M_getdata() const noexcept 420 1.1 mrg { return &_M_w; } 421 1.1 mrg #endif 422 1.1 mrg 423 1.1 mrg _WordT& 424 1.3 mrg _M_hiword() _GLIBCXX_NOEXCEPT 425 1.1 mrg { return _M_w; } 426 1.1 mrg 427 1.3 mrg _GLIBCXX_CONSTEXPR _WordT 428 1.3 mrg _M_hiword() const _GLIBCXX_NOEXCEPT 429 1.1 mrg { return _M_w; } 430 1.1 mrg 431 1.1 mrg void 432 1.3 mrg _M_do_and(const _Base_bitset<1>& __x) _GLIBCXX_NOEXCEPT 433 1.1 mrg { _M_w &= __x._M_w; } 434 1.1 mrg 435 1.1 mrg void 436 1.3 mrg _M_do_or(const _Base_bitset<1>& __x) _GLIBCXX_NOEXCEPT 437 1.1 mrg { _M_w |= __x._M_w; } 438 1.1 mrg 439 1.1 mrg void 440 1.3 mrg _M_do_xor(const _Base_bitset<1>& __x) _GLIBCXX_NOEXCEPT 441 1.1 mrg { _M_w ^= __x._M_w; } 442 1.1 mrg 443 1.1 mrg void 444 1.3 mrg _M_do_left_shift(size_t __shift) _GLIBCXX_NOEXCEPT 445 1.1 mrg { _M_w <<= __shift; } 446 1.1 mrg 447 1.1 mrg void 448 1.3 mrg _M_do_right_shift(size_t __shift) _GLIBCXX_NOEXCEPT 449 1.1 mrg { _M_w >>= __shift; } 450 1.1 mrg 451 1.1 mrg void 452 1.3 mrg _M_do_flip() _GLIBCXX_NOEXCEPT 453 1.1 mrg { _M_w = ~_M_w; } 454 1.1 mrg 455 1.1 mrg void 456 1.3 mrg _M_do_set() _GLIBCXX_NOEXCEPT 457 1.1 mrg { _M_w = ~static_cast<_WordT>(0); } 458 1.1 mrg 459 1.1 mrg void 460 1.3 mrg _M_do_reset() _GLIBCXX_NOEXCEPT 461 1.1 mrg { _M_w = 0; } 462 1.1 mrg 463 1.1 mrg bool 464 1.3 mrg _M_is_equal(const _Base_bitset<1>& __x) const _GLIBCXX_NOEXCEPT 465 1.1 mrg { return _M_w == __x._M_w; } 466 1.1 mrg 467 1.3 mrg template<size_t _Nb> 468 1.3 mrg bool 469 1.3 mrg _M_are_all() const _GLIBCXX_NOEXCEPT 470 1.3 mrg { return _M_w == (~static_cast<_WordT>(0) 471 1.3 mrg >> (_GLIBCXX_BITSET_BITS_PER_WORD - _Nb)); } 472 1.1 mrg 473 1.1 mrg bool 474 1.3 mrg _M_is_any() const _GLIBCXX_NOEXCEPT 475 1.1 mrg { return _M_w != 0; } 476 1.1 mrg 477 1.1 mrg size_t 478 1.3 mrg _M_do_count() const _GLIBCXX_NOEXCEPT 479 1.1 mrg { return __builtin_popcountl(_M_w); } 480 1.1 mrg 481 1.1 mrg unsigned long 482 1.3 mrg _M_do_to_ulong() const _GLIBCXX_NOEXCEPT 483 1.1 mrg { return _M_w; } 484 1.1 mrg 485 1.3 mrg #if __cplusplus >= 201103L 486 1.1 mrg unsigned long long 487 1.3 mrg _M_do_to_ullong() const noexcept 488 1.1 mrg { return _M_w; } 489 1.1 mrg #endif 490 1.1 mrg 491 1.1 mrg size_t 492 1.3 mrg _M_do_find_first(size_t __not_found) const _GLIBCXX_NOEXCEPT 493 1.1 mrg { 494 1.1 mrg if (_M_w != 0) 495 1.1 mrg return __builtin_ctzl(_M_w); 496 1.1 mrg else 497 1.1 mrg return __not_found; 498 1.1 mrg } 499 1.1 mrg 500 1.1 mrg // find the next "on" bit that follows "prev" 501 1.1 mrg size_t 502 1.1 mrg _M_do_find_next(size_t __prev, size_t __not_found) const 503 1.3 mrg _GLIBCXX_NOEXCEPT 504 1.1 mrg { 505 1.1 mrg ++__prev; 506 1.1 mrg if (__prev >= ((size_t) _GLIBCXX_BITSET_BITS_PER_WORD)) 507 1.1 mrg return __not_found; 508 1.1 mrg 509 1.1 mrg _WordT __x = _M_w >> __prev; 510 1.1 mrg if (__x != 0) 511 1.1 mrg return __builtin_ctzl(__x) + __prev; 512 1.1 mrg else 513 1.1 mrg return __not_found; 514 1.1 mrg } 515 1.1 mrg }; 516 1.1 mrg 517 1.1 mrg /** 518 1.1 mrg * Base class, specialization for no storage (zero-length %bitset). 519 1.1 mrg * 520 1.1 mrg * See documentation for bitset. 521 1.1 mrg */ 522 1.1 mrg template<> 523 1.1 mrg struct _Base_bitset<0> 524 1.1 mrg { 525 1.1 mrg typedef unsigned long _WordT; 526 1.1 mrg 527 1.3 mrg _GLIBCXX_CONSTEXPR _Base_bitset() _GLIBCXX_NOEXCEPT 528 1.1 mrg { } 529 1.1 mrg 530 1.3 mrg #if __cplusplus >= 201103L 531 1.3 mrg constexpr _Base_bitset(unsigned long long) noexcept 532 1.1 mrg #else 533 1.1 mrg _Base_bitset(unsigned long) 534 1.1 mrg #endif 535 1.1 mrg { } 536 1.1 mrg 537 1.3 mrg static _GLIBCXX_CONSTEXPR size_t 538 1.3 mrg _S_whichword(size_t __pos) _GLIBCXX_NOEXCEPT 539 1.1 mrg { return __pos / _GLIBCXX_BITSET_BITS_PER_WORD; } 540 1.1 mrg 541 1.3 mrg static _GLIBCXX_CONSTEXPR size_t 542 1.3 mrg _S_whichbyte(size_t __pos) _GLIBCXX_NOEXCEPT 543 1.1 mrg { return (__pos % _GLIBCXX_BITSET_BITS_PER_WORD) / __CHAR_BIT__; } 544 1.1 mrg 545 1.3 mrg static _GLIBCXX_CONSTEXPR size_t 546 1.3 mrg _S_whichbit(size_t __pos) _GLIBCXX_NOEXCEPT 547 1.1 mrg { return __pos % _GLIBCXX_BITSET_BITS_PER_WORD; } 548 1.1 mrg 549 1.3 mrg static _GLIBCXX_CONSTEXPR _WordT 550 1.3 mrg _S_maskbit(size_t __pos) _GLIBCXX_NOEXCEPT 551 1.1 mrg { return (static_cast<_WordT>(1)) << _S_whichbit(__pos); } 552 1.1 mrg 553 1.1 mrg // This would normally give access to the data. The bounds-checking 554 1.1 mrg // in the bitset class will prevent the user from getting this far, 555 1.1 mrg // but (1) it must still return an lvalue to compile, and (2) the 556 1.1 mrg // user might call _Unchecked_set directly, in which case this /needs/ 557 1.1 mrg // to fail. Let's not penalize zero-length users unless they actually 558 1.1 mrg // make an unchecked call; all the memory ugliness is therefore 559 1.1 mrg // localized to this single should-never-get-this-far function. 560 1.1 mrg _WordT& 561 1.3 mrg _M_getword(size_t) _GLIBCXX_NOEXCEPT 562 1.3 mrg { 563 1.8 mrg __throw_out_of_range(__N("_Base_bitset::_M_getword")); 564 1.3 mrg return *new _WordT; 565 1.1 mrg } 566 1.1 mrg 567 1.3 mrg _GLIBCXX_CONSTEXPR _WordT 568 1.9 mrg _M_getword(size_t) const _GLIBCXX_NOEXCEPT 569 1.3 mrg { return 0; } 570 1.3 mrg 571 1.3 mrg _GLIBCXX_CONSTEXPR _WordT 572 1.3 mrg _M_hiword() const _GLIBCXX_NOEXCEPT 573 1.1 mrg { return 0; } 574 1.1 mrg 575 1.1 mrg void 576 1.3 mrg _M_do_and(const _Base_bitset<0>&) _GLIBCXX_NOEXCEPT 577 1.1 mrg { } 578 1.1 mrg 579 1.1 mrg void 580 1.3 mrg _M_do_or(const _Base_bitset<0>&) _GLIBCXX_NOEXCEPT 581 1.1 mrg { } 582 1.1 mrg 583 1.1 mrg void 584 1.3 mrg _M_do_xor(const _Base_bitset<0>&) _GLIBCXX_NOEXCEPT 585 1.1 mrg { } 586 1.1 mrg 587 1.1 mrg void 588 1.3 mrg _M_do_left_shift(size_t) _GLIBCXX_NOEXCEPT 589 1.1 mrg { } 590 1.1 mrg 591 1.1 mrg void 592 1.3 mrg _M_do_right_shift(size_t) _GLIBCXX_NOEXCEPT 593 1.1 mrg { } 594 1.1 mrg 595 1.1 mrg void 596 1.3 mrg _M_do_flip() _GLIBCXX_NOEXCEPT 597 1.1 mrg { } 598 1.1 mrg 599 1.1 mrg void 600 1.3 mrg _M_do_set() _GLIBCXX_NOEXCEPT 601 1.1 mrg { } 602 1.1 mrg 603 1.1 mrg void 604 1.3 mrg _M_do_reset() _GLIBCXX_NOEXCEPT 605 1.1 mrg { } 606 1.1 mrg 607 1.1 mrg // Are all empty bitsets equal to each other? Are they equal to 608 1.1 mrg // themselves? How to compare a thing which has no state? What is 609 1.1 mrg // the sound of one zero-length bitset clapping? 610 1.1 mrg bool 611 1.3 mrg _M_is_equal(const _Base_bitset<0>&) const _GLIBCXX_NOEXCEPT 612 1.1 mrg { return true; } 613 1.1 mrg 614 1.3 mrg template<size_t _Nb> 615 1.3 mrg bool 616 1.3 mrg _M_are_all() const _GLIBCXX_NOEXCEPT 617 1.3 mrg { return true; } 618 1.1 mrg 619 1.1 mrg bool 620 1.3 mrg _M_is_any() const _GLIBCXX_NOEXCEPT 621 1.1 mrg { return false; } 622 1.1 mrg 623 1.1 mrg size_t 624 1.3 mrg _M_do_count() const _GLIBCXX_NOEXCEPT 625 1.1 mrg { return 0; } 626 1.1 mrg 627 1.1 mrg unsigned long 628 1.3 mrg _M_do_to_ulong() const _GLIBCXX_NOEXCEPT 629 1.1 mrg { return 0; } 630 1.1 mrg 631 1.3 mrg #if __cplusplus >= 201103L 632 1.1 mrg unsigned long long 633 1.3 mrg _M_do_to_ullong() const noexcept 634 1.1 mrg { return 0; } 635 1.1 mrg #endif 636 1.1 mrg 637 1.1 mrg // Normally "not found" is the size, but that could also be 638 1.1 mrg // misinterpreted as an index in this corner case. Oh well. 639 1.1 mrg size_t 640 1.3 mrg _M_do_find_first(size_t) const _GLIBCXX_NOEXCEPT 641 1.1 mrg { return 0; } 642 1.1 mrg 643 1.1 mrg size_t 644 1.3 mrg _M_do_find_next(size_t, size_t) const _GLIBCXX_NOEXCEPT 645 1.1 mrg { return 0; } 646 1.1 mrg }; 647 1.1 mrg 648 1.1 mrg 649 1.1 mrg // Helper class to zero out the unused high-order bits in the highest word. 650 1.1 mrg template<size_t _Extrabits> 651 1.1 mrg struct _Sanitize 652 1.1 mrg { 653 1.3 mrg typedef unsigned long _WordT; 654 1.3 mrg 655 1.3 mrg static void 656 1.3 mrg _S_do_sanitize(_WordT& __val) _GLIBCXX_NOEXCEPT 657 1.3 mrg { __val &= ~((~static_cast<_WordT>(0)) << _Extrabits); } 658 1.1 mrg }; 659 1.1 mrg 660 1.1 mrg template<> 661 1.1 mrg struct _Sanitize<0> 662 1.3 mrg { 663 1.3 mrg typedef unsigned long _WordT; 664 1.3 mrg 665 1.3 mrg static void 666 1.8 mrg _S_do_sanitize(_WordT) _GLIBCXX_NOEXCEPT { } 667 1.3 mrg }; 668 1.3 mrg 669 1.3 mrg #if __cplusplus >= 201103L 670 1.5 mrg template<size_t _Nb, bool = (_Nb < _GLIBCXX_BITSET_BITS_PER_ULL)> 671 1.3 mrg struct _Sanitize_val 672 1.3 mrg { 673 1.3 mrg static constexpr unsigned long long 674 1.3 mrg _S_do_sanitize_val(unsigned long long __val) 675 1.3 mrg { return __val; } 676 1.3 mrg }; 677 1.3 mrg 678 1.3 mrg template<size_t _Nb> 679 1.3 mrg struct _Sanitize_val<_Nb, true> 680 1.3 mrg { 681 1.3 mrg static constexpr unsigned long long 682 1.3 mrg _S_do_sanitize_val(unsigned long long __val) 683 1.3 mrg { return __val & ~((~static_cast<unsigned long long>(0)) << _Nb); } 684 1.3 mrg }; 685 1.3 mrg #endif 686 1.1 mrg 687 1.1 mrg /** 688 1.5 mrg * @brief The %bitset class represents a @e fixed-size sequence of bits. 689 1.5 mrg * @ingroup utilities 690 1.1 mrg * 691 1.1 mrg * (Note that %bitset does @e not meet the formal requirements of a 692 1.1 mrg * <a href="tables.html#65">container</a>. Mainly, it lacks iterators.) 693 1.1 mrg * 694 1.1 mrg * The template argument, @a Nb, may be any non-negative number, 695 1.1 mrg * specifying the number of bits (e.g., "0", "12", "1024*1024"). 696 1.1 mrg * 697 1.1 mrg * In the general unoptimized case, storage is allocated in word-sized 698 1.1 mrg * blocks. Let B be the number of bits in a word, then (Nb+(B-1))/B 699 1.1 mrg * words will be used for storage. B - Nb%B bits are unused. (They are 700 1.1 mrg * the high-order bits in the highest word.) It is a class invariant 701 1.1 mrg * that those unused bits are always zero. 702 1.1 mrg * 703 1.1 mrg * If you think of %bitset as <em>a simple array of bits</em>, be 704 1.1 mrg * aware that your mental picture is reversed: a %bitset behaves 705 1.1 mrg * the same way as bits in integers do, with the bit at index 0 in 706 1.1 mrg * the <em>least significant / right-hand</em> position, and the bit at 707 1.1 mrg * index Nb-1 in the <em>most significant / left-hand</em> position. 708 1.1 mrg * Thus, unlike other containers, a %bitset's index <em>counts from 709 1.1 mrg * right to left</em>, to put it very loosely. 710 1.1 mrg * 711 1.1 mrg * This behavior is preserved when translating to and from strings. For 712 1.1 mrg * example, the first line of the following program probably prints 713 1.1 mrg * <em>b('a') is 0001100001</em> on a modern ASCII system. 714 1.1 mrg * 715 1.1 mrg * @code 716 1.1 mrg * #include <bitset> 717 1.1 mrg * #include <iostream> 718 1.1 mrg * #include <sstream> 719 1.1 mrg * 720 1.1 mrg * using namespace std; 721 1.1 mrg * 722 1.1 mrg * int main() 723 1.1 mrg * { 724 1.1 mrg * long a = 'a'; 725 1.1 mrg * bitset<10> b(a); 726 1.1 mrg * 727 1.1 mrg * cout << "b('a') is " << b << endl; 728 1.1 mrg * 729 1.1 mrg * ostringstream s; 730 1.1 mrg * s << b; 731 1.1 mrg * string str = s.str(); 732 1.1 mrg * cout << "index 3 in the string is " << str[3] << " but\n" 733 1.1 mrg * << "index 3 in the bitset is " << b[3] << endl; 734 1.1 mrg * } 735 1.1 mrg * @endcode 736 1.1 mrg * 737 1.1 mrg * Also see: 738 1.5 mrg * https://gcc.gnu.org/onlinedocs/libstdc++/manual/ext_containers.html 739 1.1 mrg * for a description of extensions. 740 1.1 mrg * 741 1.1 mrg * Most of the actual code isn't contained in %bitset<> itself, but in the 742 1.1 mrg * base class _Base_bitset. The base class works with whole words, not with 743 1.1 mrg * individual bits. This allows us to specialize _Base_bitset for the 744 1.1 mrg * important special case where the %bitset is only a single word. 745 1.1 mrg * 746 1.1 mrg * Extra confusion can result due to the fact that the storage for 747 1.1 mrg * _Base_bitset @e is a regular array, and is indexed as such. This is 748 1.1 mrg * carefully encapsulated. 749 1.1 mrg */ 750 1.1 mrg template<size_t _Nb> 751 1.1 mrg class bitset 752 1.1 mrg : private _Base_bitset<_GLIBCXX_BITSET_WORDS(_Nb)> 753 1.1 mrg { 754 1.1 mrg private: 755 1.1 mrg typedef _Base_bitset<_GLIBCXX_BITSET_WORDS(_Nb)> _Base; 756 1.1 mrg typedef unsigned long _WordT; 757 1.1 mrg 758 1.5 mrg template<class _CharT, class _Traits, class _Alloc> 759 1.5 mrg void 760 1.5 mrg _M_check_initial_position(const std::basic_string<_CharT, _Traits, _Alloc>& __s, 761 1.5 mrg size_t __position) const 762 1.5 mrg { 763 1.5 mrg if (__position > __s.size()) 764 1.5 mrg __throw_out_of_range_fmt(__N("bitset::bitset: __position " 765 1.5 mrg "(which is %zu) > __s.size() " 766 1.5 mrg "(which is %zu)"), 767 1.5 mrg __position, __s.size()); 768 1.5 mrg } 769 1.5 mrg 770 1.5 mrg void _M_check(size_t __position, const char *__s) const 771 1.5 mrg { 772 1.5 mrg if (__position >= _Nb) 773 1.5 mrg __throw_out_of_range_fmt(__N("%s: __position (which is %zu) " 774 1.5 mrg ">= _Nb (which is %zu)"), 775 1.5 mrg __s, __position, _Nb); 776 1.5 mrg } 777 1.5 mrg 778 1.1 mrg void 779 1.3 mrg _M_do_sanitize() _GLIBCXX_NOEXCEPT 780 1.8 mrg { 781 1.3 mrg typedef _Sanitize<_Nb % _GLIBCXX_BITSET_BITS_PER_WORD> __sanitize_type; 782 1.3 mrg __sanitize_type::_S_do_sanitize(this->_M_hiword()); 783 1.3 mrg } 784 1.1 mrg 785 1.3 mrg #if __cplusplus >= 201103L 786 1.9 mrg friend struct std::hash<bitset>; 787 1.1 mrg #endif 788 1.1 mrg 789 1.1 mrg public: 790 1.1 mrg /** 791 1.1 mrg * This encapsulates the concept of a single bit. An instance of this 792 1.1 mrg * class is a proxy for an actual bit; this way the individual bit 793 1.1 mrg * operations are done as faster word-size bitwise instructions. 794 1.1 mrg * 795 1.1 mrg * Most users will never need to use this class directly; conversions 796 1.1 mrg * to and from bool are automatic and should be transparent. Overloaded 797 1.1 mrg * operators help to preserve the illusion. 798 1.1 mrg * 799 1.1 mrg * (On a typical system, this <em>bit %reference</em> is 64 800 1.1 mrg * times the size of an actual bit. Ha.) 801 1.1 mrg */ 802 1.1 mrg class reference 803 1.1 mrg { 804 1.1 mrg friend class bitset; 805 1.1 mrg 806 1.3 mrg _WordT* _M_wp; 807 1.3 mrg size_t _M_bpos; 808 1.8 mrg 809 1.1 mrg // left undefined 810 1.1 mrg reference(); 811 1.8 mrg 812 1.1 mrg public: 813 1.3 mrg reference(bitset& __b, size_t __pos) _GLIBCXX_NOEXCEPT 814 1.1 mrg { 815 1.1 mrg _M_wp = &__b._M_getword(__pos); 816 1.1 mrg _M_bpos = _Base::_S_whichbit(__pos); 817 1.1 mrg } 818 1.1 mrg 819 1.10 mrg #if __cplusplus >= 201103L 820 1.10 mrg reference(const reference&) = default; 821 1.10 mrg #endif 822 1.10 mrg 823 1.3 mrg ~reference() _GLIBCXX_NOEXCEPT 824 1.1 mrg { } 825 1.1 mrg 826 1.1 mrg // For b[i] = __x; 827 1.1 mrg reference& 828 1.3 mrg operator=(bool __x) _GLIBCXX_NOEXCEPT 829 1.1 mrg { 830 1.1 mrg if (__x) 831 1.1 mrg *_M_wp |= _Base::_S_maskbit(_M_bpos); 832 1.1 mrg else 833 1.1 mrg *_M_wp &= ~_Base::_S_maskbit(_M_bpos); 834 1.1 mrg return *this; 835 1.1 mrg } 836 1.1 mrg 837 1.1 mrg // For b[i] = b[__j]; 838 1.1 mrg reference& 839 1.3 mrg operator=(const reference& __j) _GLIBCXX_NOEXCEPT 840 1.1 mrg { 841 1.1 mrg if ((*(__j._M_wp) & _Base::_S_maskbit(__j._M_bpos))) 842 1.1 mrg *_M_wp |= _Base::_S_maskbit(_M_bpos); 843 1.1 mrg else 844 1.1 mrg *_M_wp &= ~_Base::_S_maskbit(_M_bpos); 845 1.1 mrg return *this; 846 1.1 mrg } 847 1.1 mrg 848 1.1 mrg // Flips the bit 849 1.1 mrg bool 850 1.3 mrg operator~() const _GLIBCXX_NOEXCEPT 851 1.1 mrg { return (*(_M_wp) & _Base::_S_maskbit(_M_bpos)) == 0; } 852 1.1 mrg 853 1.1 mrg // For __x = b[i]; 854 1.3 mrg operator bool() const _GLIBCXX_NOEXCEPT 855 1.1 mrg { return (*(_M_wp) & _Base::_S_maskbit(_M_bpos)) != 0; } 856 1.1 mrg 857 1.1 mrg // For b[i].flip(); 858 1.1 mrg reference& 859 1.3 mrg flip() _GLIBCXX_NOEXCEPT 860 1.1 mrg { 861 1.1 mrg *_M_wp ^= _Base::_S_maskbit(_M_bpos); 862 1.1 mrg return *this; 863 1.1 mrg } 864 1.1 mrg }; 865 1.1 mrg friend class reference; 866 1.1 mrg 867 1.1 mrg // 23.3.5.1 constructors: 868 1.1 mrg /// All bits set to zero. 869 1.3 mrg _GLIBCXX_CONSTEXPR bitset() _GLIBCXX_NOEXCEPT 870 1.1 mrg { } 871 1.1 mrg 872 1.1 mrg /// Initial bits bitwise-copied from a single word (others set to zero). 873 1.3 mrg #if __cplusplus >= 201103L 874 1.3 mrg constexpr bitset(unsigned long long __val) noexcept 875 1.3 mrg : _Base(_Sanitize_val<_Nb>::_S_do_sanitize_val(__val)) { } 876 1.1 mrg #else 877 1.1 mrg bitset(unsigned long __val) 878 1.1 mrg : _Base(__val) 879 1.1 mrg { _M_do_sanitize(); } 880 1.3 mrg #endif 881 1.1 mrg 882 1.1 mrg /** 883 1.3 mrg * Use a subset of a string. 884 1.3 mrg * @param __s A string of @a 0 and @a 1 characters. 885 1.3 mrg * @param __position Index of the first character in @a __s to use; 886 1.1 mrg * defaults to zero. 887 1.3 mrg * @throw std::out_of_range If @a pos is bigger the size of @a __s. 888 1.1 mrg * @throw std::invalid_argument If a character appears in the string 889 1.1 mrg * which is neither @a 0 nor @a 1. 890 1.1 mrg */ 891 1.1 mrg template<class _CharT, class _Traits, class _Alloc> 892 1.1 mrg explicit 893 1.1 mrg bitset(const std::basic_string<_CharT, _Traits, _Alloc>& __s, 894 1.1 mrg size_t __position = 0) 895 1.1 mrg : _Base() 896 1.1 mrg { 897 1.5 mrg _M_check_initial_position(__s, __position); 898 1.1 mrg _M_copy_from_string(__s, __position, 899 1.1 mrg std::basic_string<_CharT, _Traits, _Alloc>::npos, 900 1.1 mrg _CharT('0'), _CharT('1')); 901 1.1 mrg } 902 1.1 mrg 903 1.1 mrg /** 904 1.3 mrg * Use a subset of a string. 905 1.3 mrg * @param __s A string of @a 0 and @a 1 characters. 906 1.3 mrg * @param __position Index of the first character in @a __s to use. 907 1.3 mrg * @param __n The number of characters to copy. 908 1.3 mrg * @throw std::out_of_range If @a __position is bigger the size 909 1.3 mrg * of @a __s. 910 1.1 mrg * @throw std::invalid_argument If a character appears in the string 911 1.1 mrg * which is neither @a 0 nor @a 1. 912 1.1 mrg */ 913 1.1 mrg template<class _CharT, class _Traits, class _Alloc> 914 1.1 mrg bitset(const std::basic_string<_CharT, _Traits, _Alloc>& __s, 915 1.1 mrg size_t __position, size_t __n) 916 1.1 mrg : _Base() 917 1.1 mrg { 918 1.5 mrg _M_check_initial_position(__s, __position); 919 1.1 mrg _M_copy_from_string(__s, __position, __n, _CharT('0'), _CharT('1')); 920 1.1 mrg } 921 1.1 mrg 922 1.1 mrg // _GLIBCXX_RESOLVE_LIB_DEFECTS 923 1.1 mrg // 396. what are characters zero and one. 924 1.1 mrg template<class _CharT, class _Traits, class _Alloc> 925 1.1 mrg bitset(const std::basic_string<_CharT, _Traits, _Alloc>& __s, 926 1.1 mrg size_t __position, size_t __n, 927 1.1 mrg _CharT __zero, _CharT __one = _CharT('1')) 928 1.1 mrg : _Base() 929 1.1 mrg { 930 1.5 mrg _M_check_initial_position(__s, __position); 931 1.1 mrg _M_copy_from_string(__s, __position, __n, __zero, __one); 932 1.1 mrg } 933 1.1 mrg 934 1.3 mrg #if __cplusplus >= 201103L 935 1.1 mrg /** 936 1.3 mrg * Construct from a character %array. 937 1.3 mrg * @param __str An %array of characters @a zero and @a one. 938 1.3 mrg * @param __n The number of characters to use. 939 1.3 mrg * @param __zero The character corresponding to the value 0. 940 1.3 mrg * @param __one The character corresponding to the value 1. 941 1.3 mrg * @throw std::invalid_argument If a character appears in the string 942 1.3 mrg * which is neither @a __zero nor @a __one. 943 1.3 mrg */ 944 1.3 mrg template<typename _CharT> 945 1.3 mrg explicit 946 1.3 mrg bitset(const _CharT* __str, 947 1.3 mrg typename std::basic_string<_CharT>::size_type __n 948 1.3 mrg = std::basic_string<_CharT>::npos, 949 1.3 mrg _CharT __zero = _CharT('0'), _CharT __one = _CharT('1')) 950 1.3 mrg : _Base() 951 1.3 mrg { 952 1.3 mrg if (!__str) 953 1.3 mrg __throw_logic_error(__N("bitset::bitset(const _CharT*, ...)")); 954 1.3 mrg 955 1.3 mrg if (__n == std::basic_string<_CharT>::npos) 956 1.3 mrg __n = std::char_traits<_CharT>::length(__str); 957 1.3 mrg _M_copy_from_ptr<_CharT, std::char_traits<_CharT>>(__str, __n, 0, 958 1.3 mrg __n, __zero, 959 1.3 mrg __one); 960 1.3 mrg } 961 1.1 mrg #endif 962 1.1 mrg 963 1.1 mrg // 23.3.5.2 bitset operations: 964 1.11 mrg ///@{ 965 1.1 mrg /** 966 1.3 mrg * Operations on bitsets. 967 1.3 mrg * @param __rhs A same-sized bitset. 968 1.1 mrg * 969 1.1 mrg * These should be self-explanatory. 970 1.1 mrg */ 971 1.1 mrg bitset<_Nb>& 972 1.3 mrg operator&=(const bitset<_Nb>& __rhs) _GLIBCXX_NOEXCEPT 973 1.1 mrg { 974 1.1 mrg this->_M_do_and(__rhs); 975 1.1 mrg return *this; 976 1.1 mrg } 977 1.1 mrg 978 1.1 mrg bitset<_Nb>& 979 1.3 mrg operator|=(const bitset<_Nb>& __rhs) _GLIBCXX_NOEXCEPT 980 1.1 mrg { 981 1.1 mrg this->_M_do_or(__rhs); 982 1.1 mrg return *this; 983 1.1 mrg } 984 1.1 mrg 985 1.1 mrg bitset<_Nb>& 986 1.3 mrg operator^=(const bitset<_Nb>& __rhs) _GLIBCXX_NOEXCEPT 987 1.1 mrg { 988 1.1 mrg this->_M_do_xor(__rhs); 989 1.1 mrg return *this; 990 1.1 mrg } 991 1.11 mrg ///@} 992 1.8 mrg 993 1.11 mrg ///@{ 994 1.1 mrg /** 995 1.3 mrg * Operations on bitsets. 996 1.3 mrg * @param __position The number of places to shift. 997 1.1 mrg * 998 1.1 mrg * These should be self-explanatory. 999 1.1 mrg */ 1000 1.1 mrg bitset<_Nb>& 1001 1.3 mrg operator<<=(size_t __position) _GLIBCXX_NOEXCEPT 1002 1.1 mrg { 1003 1.1 mrg if (__builtin_expect(__position < _Nb, 1)) 1004 1.1 mrg { 1005 1.1 mrg this->_M_do_left_shift(__position); 1006 1.1 mrg this->_M_do_sanitize(); 1007 1.1 mrg } 1008 1.1 mrg else 1009 1.1 mrg this->_M_do_reset(); 1010 1.1 mrg return *this; 1011 1.1 mrg } 1012 1.1 mrg 1013 1.1 mrg bitset<_Nb>& 1014 1.3 mrg operator>>=(size_t __position) _GLIBCXX_NOEXCEPT 1015 1.1 mrg { 1016 1.1 mrg if (__builtin_expect(__position < _Nb, 1)) 1017 1.1 mrg { 1018 1.1 mrg this->_M_do_right_shift(__position); 1019 1.1 mrg this->_M_do_sanitize(); 1020 1.1 mrg } 1021 1.1 mrg else 1022 1.1 mrg this->_M_do_reset(); 1023 1.1 mrg return *this; 1024 1.1 mrg } 1025 1.11 mrg ///@} 1026 1.8 mrg 1027 1.11 mrg ///@{ 1028 1.1 mrg /** 1029 1.1 mrg * These versions of single-bit set, reset, flip, and test are 1030 1.1 mrg * extensions from the SGI version. They do no range checking. 1031 1.1 mrg * @ingroup SGIextensions 1032 1.1 mrg */ 1033 1.1 mrg bitset<_Nb>& 1034 1.3 mrg _Unchecked_set(size_t __pos) _GLIBCXX_NOEXCEPT 1035 1.1 mrg { 1036 1.1 mrg this->_M_getword(__pos) |= _Base::_S_maskbit(__pos); 1037 1.1 mrg return *this; 1038 1.1 mrg } 1039 1.1 mrg 1040 1.1 mrg bitset<_Nb>& 1041 1.3 mrg _Unchecked_set(size_t __pos, int __val) _GLIBCXX_NOEXCEPT 1042 1.1 mrg { 1043 1.1 mrg if (__val) 1044 1.1 mrg this->_M_getword(__pos) |= _Base::_S_maskbit(__pos); 1045 1.1 mrg else 1046 1.1 mrg this->_M_getword(__pos) &= ~_Base::_S_maskbit(__pos); 1047 1.1 mrg return *this; 1048 1.1 mrg } 1049 1.1 mrg 1050 1.1 mrg bitset<_Nb>& 1051 1.3 mrg _Unchecked_reset(size_t __pos) _GLIBCXX_NOEXCEPT 1052 1.1 mrg { 1053 1.1 mrg this->_M_getword(__pos) &= ~_Base::_S_maskbit(__pos); 1054 1.1 mrg return *this; 1055 1.1 mrg } 1056 1.1 mrg 1057 1.1 mrg bitset<_Nb>& 1058 1.3 mrg _Unchecked_flip(size_t __pos) _GLIBCXX_NOEXCEPT 1059 1.1 mrg { 1060 1.1 mrg this->_M_getword(__pos) ^= _Base::_S_maskbit(__pos); 1061 1.1 mrg return *this; 1062 1.1 mrg } 1063 1.1 mrg 1064 1.3 mrg _GLIBCXX_CONSTEXPR bool 1065 1.3 mrg _Unchecked_test(size_t __pos) const _GLIBCXX_NOEXCEPT 1066 1.1 mrg { return ((this->_M_getword(__pos) & _Base::_S_maskbit(__pos)) 1067 1.1 mrg != static_cast<_WordT>(0)); } 1068 1.11 mrg ///@} 1069 1.8 mrg 1070 1.1 mrg // Set, reset, and flip. 1071 1.1 mrg /** 1072 1.1 mrg * @brief Sets every bit to true. 1073 1.1 mrg */ 1074 1.1 mrg bitset<_Nb>& 1075 1.3 mrg set() _GLIBCXX_NOEXCEPT 1076 1.1 mrg { 1077 1.1 mrg this->_M_do_set(); 1078 1.1 mrg this->_M_do_sanitize(); 1079 1.1 mrg return *this; 1080 1.1 mrg } 1081 1.1 mrg 1082 1.1 mrg /** 1083 1.1 mrg * @brief Sets a given bit to a particular value. 1084 1.3 mrg * @param __position The index of the bit. 1085 1.3 mrg * @param __val Either true or false, defaults to true. 1086 1.1 mrg * @throw std::out_of_range If @a pos is bigger the size of the %set. 1087 1.1 mrg */ 1088 1.1 mrg bitset<_Nb>& 1089 1.1 mrg set(size_t __position, bool __val = true) 1090 1.1 mrg { 1091 1.5 mrg this->_M_check(__position, __N("bitset::set")); 1092 1.1 mrg return _Unchecked_set(__position, __val); 1093 1.1 mrg } 1094 1.1 mrg 1095 1.1 mrg /** 1096 1.1 mrg * @brief Sets every bit to false. 1097 1.1 mrg */ 1098 1.1 mrg bitset<_Nb>& 1099 1.3 mrg reset() _GLIBCXX_NOEXCEPT 1100 1.1 mrg { 1101 1.1 mrg this->_M_do_reset(); 1102 1.1 mrg return *this; 1103 1.1 mrg } 1104 1.1 mrg 1105 1.1 mrg /** 1106 1.1 mrg * @brief Sets a given bit to false. 1107 1.3 mrg * @param __position The index of the bit. 1108 1.1 mrg * @throw std::out_of_range If @a pos is bigger the size of the %set. 1109 1.1 mrg * 1110 1.1 mrg * Same as writing @c set(pos,false). 1111 1.1 mrg */ 1112 1.1 mrg bitset<_Nb>& 1113 1.1 mrg reset(size_t __position) 1114 1.1 mrg { 1115 1.5 mrg this->_M_check(__position, __N("bitset::reset")); 1116 1.1 mrg return _Unchecked_reset(__position); 1117 1.1 mrg } 1118 1.8 mrg 1119 1.1 mrg /** 1120 1.1 mrg * @brief Toggles every bit to its opposite value. 1121 1.1 mrg */ 1122 1.1 mrg bitset<_Nb>& 1123 1.3 mrg flip() _GLIBCXX_NOEXCEPT 1124 1.1 mrg { 1125 1.1 mrg this->_M_do_flip(); 1126 1.1 mrg this->_M_do_sanitize(); 1127 1.1 mrg return *this; 1128 1.1 mrg } 1129 1.1 mrg 1130 1.1 mrg /** 1131 1.1 mrg * @brief Toggles a given bit to its opposite value. 1132 1.3 mrg * @param __position The index of the bit. 1133 1.1 mrg * @throw std::out_of_range If @a pos is bigger the size of the %set. 1134 1.1 mrg */ 1135 1.1 mrg bitset<_Nb>& 1136 1.1 mrg flip(size_t __position) 1137 1.1 mrg { 1138 1.5 mrg this->_M_check(__position, __N("bitset::flip")); 1139 1.1 mrg return _Unchecked_flip(__position); 1140 1.1 mrg } 1141 1.8 mrg 1142 1.1 mrg /// See the no-argument flip(). 1143 1.1 mrg bitset<_Nb> 1144 1.3 mrg operator~() const _GLIBCXX_NOEXCEPT 1145 1.1 mrg { return bitset<_Nb>(*this).flip(); } 1146 1.1 mrg 1147 1.11 mrg ///@{ 1148 1.1 mrg /** 1149 1.1 mrg * @brief Array-indexing support. 1150 1.3 mrg * @param __position Index into the %bitset. 1151 1.3 mrg * @return A bool for a <em>const %bitset</em>. For non-const 1152 1.3 mrg * bitsets, an instance of the reference proxy class. 1153 1.1 mrg * @note These operators do no range checking and throw no exceptions, 1154 1.1 mrg * as required by DR 11 to the standard. 1155 1.1 mrg * 1156 1.1 mrg * _GLIBCXX_RESOLVE_LIB_DEFECTS Note that this implementation already 1157 1.1 mrg * resolves DR 11 (items 1 and 2), but does not do the range-checking 1158 1.1 mrg * required by that DR's resolution. -pme 1159 1.1 mrg * The DR has since been changed: range-checking is a precondition 1160 1.1 mrg * (users' responsibility), and these functions must not throw. -pme 1161 1.1 mrg */ 1162 1.1 mrg reference 1163 1.1 mrg operator[](size_t __position) 1164 1.3 mrg { return reference(*this, __position); } 1165 1.1 mrg 1166 1.3 mrg _GLIBCXX_CONSTEXPR bool 1167 1.1 mrg operator[](size_t __position) const 1168 1.1 mrg { return _Unchecked_test(__position); } 1169 1.11 mrg ///@} 1170 1.8 mrg 1171 1.1 mrg /** 1172 1.1 mrg * @brief Returns a numerical interpretation of the %bitset. 1173 1.1 mrg * @return The integral equivalent of the bits. 1174 1.1 mrg * @throw std::overflow_error If there are too many bits to be 1175 1.1 mrg * represented in an @c unsigned @c long. 1176 1.1 mrg */ 1177 1.1 mrg unsigned long 1178 1.1 mrg to_ulong() const 1179 1.1 mrg { return this->_M_do_to_ulong(); } 1180 1.1 mrg 1181 1.3 mrg #if __cplusplus >= 201103L 1182 1.1 mrg unsigned long long 1183 1.1 mrg to_ullong() const 1184 1.1 mrg { return this->_M_do_to_ullong(); } 1185 1.1 mrg #endif 1186 1.1 mrg 1187 1.1 mrg /** 1188 1.1 mrg * @brief Returns a character interpretation of the %bitset. 1189 1.1 mrg * @return The string equivalent of the bits. 1190 1.1 mrg * 1191 1.1 mrg * Note the ordering of the bits: decreasing character positions 1192 1.1 mrg * correspond to increasing bit positions (see the main class notes for 1193 1.1 mrg * an example). 1194 1.1 mrg */ 1195 1.1 mrg template<class _CharT, class _Traits, class _Alloc> 1196 1.1 mrg std::basic_string<_CharT, _Traits, _Alloc> 1197 1.1 mrg to_string() const 1198 1.1 mrg { 1199 1.1 mrg std::basic_string<_CharT, _Traits, _Alloc> __result; 1200 1.1 mrg _M_copy_to_string(__result, _CharT('0'), _CharT('1')); 1201 1.1 mrg return __result; 1202 1.1 mrg } 1203 1.1 mrg 1204 1.1 mrg // _GLIBCXX_RESOLVE_LIB_DEFECTS 1205 1.1 mrg // 396. what are characters zero and one. 1206 1.1 mrg template<class _CharT, class _Traits, class _Alloc> 1207 1.1 mrg std::basic_string<_CharT, _Traits, _Alloc> 1208 1.1 mrg to_string(_CharT __zero, _CharT __one = _CharT('1')) const 1209 1.1 mrg { 1210 1.1 mrg std::basic_string<_CharT, _Traits, _Alloc> __result; 1211 1.1 mrg _M_copy_to_string(__result, __zero, __one); 1212 1.1 mrg return __result; 1213 1.1 mrg } 1214 1.1 mrg 1215 1.1 mrg // _GLIBCXX_RESOLVE_LIB_DEFECTS 1216 1.1 mrg // 434. bitset::to_string() hard to use. 1217 1.1 mrg template<class _CharT, class _Traits> 1218 1.1 mrg std::basic_string<_CharT, _Traits, std::allocator<_CharT> > 1219 1.1 mrg to_string() const 1220 1.1 mrg { return to_string<_CharT, _Traits, std::allocator<_CharT> >(); } 1221 1.1 mrg 1222 1.1 mrg // _GLIBCXX_RESOLVE_LIB_DEFECTS 1223 1.1 mrg // 853. to_string needs updating with zero and one. 1224 1.1 mrg template<class _CharT, class _Traits> 1225 1.1 mrg std::basic_string<_CharT, _Traits, std::allocator<_CharT> > 1226 1.1 mrg to_string(_CharT __zero, _CharT __one = _CharT('1')) const 1227 1.1 mrg { return to_string<_CharT, _Traits, 1228 1.1 mrg std::allocator<_CharT> >(__zero, __one); } 1229 1.1 mrg 1230 1.1 mrg template<class _CharT> 1231 1.1 mrg std::basic_string<_CharT, std::char_traits<_CharT>, 1232 1.1 mrg std::allocator<_CharT> > 1233 1.1 mrg to_string() const 1234 1.1 mrg { 1235 1.1 mrg return to_string<_CharT, std::char_traits<_CharT>, 1236 1.1 mrg std::allocator<_CharT> >(); 1237 1.1 mrg } 1238 1.1 mrg 1239 1.1 mrg template<class _CharT> 1240 1.1 mrg std::basic_string<_CharT, std::char_traits<_CharT>, 1241 1.1 mrg std::allocator<_CharT> > 1242 1.1 mrg to_string(_CharT __zero, _CharT __one = _CharT('1')) const 1243 1.1 mrg { 1244 1.1 mrg return to_string<_CharT, std::char_traits<_CharT>, 1245 1.1 mrg std::allocator<_CharT> >(__zero, __one); 1246 1.1 mrg } 1247 1.1 mrg 1248 1.1 mrg std::basic_string<char, std::char_traits<char>, std::allocator<char> > 1249 1.1 mrg to_string() const 1250 1.1 mrg { 1251 1.1 mrg return to_string<char, std::char_traits<char>, 1252 1.1 mrg std::allocator<char> >(); 1253 1.1 mrg } 1254 1.1 mrg 1255 1.1 mrg std::basic_string<char, std::char_traits<char>, std::allocator<char> > 1256 1.1 mrg to_string(char __zero, char __one = '1') const 1257 1.1 mrg { 1258 1.1 mrg return to_string<char, std::char_traits<char>, 1259 1.1 mrg std::allocator<char> >(__zero, __one); 1260 1.1 mrg } 1261 1.1 mrg 1262 1.1 mrg // Helper functions for string operations. 1263 1.1 mrg template<class _CharT, class _Traits> 1264 1.1 mrg void 1265 1.1 mrg _M_copy_from_ptr(const _CharT*, size_t, size_t, size_t, 1266 1.1 mrg _CharT, _CharT); 1267 1.1 mrg 1268 1.1 mrg template<class _CharT, class _Traits, class _Alloc> 1269 1.1 mrg void 1270 1.1 mrg _M_copy_from_string(const std::basic_string<_CharT, 1271 1.1 mrg _Traits, _Alloc>& __s, size_t __pos, size_t __n, 1272 1.1 mrg _CharT __zero, _CharT __one) 1273 1.1 mrg { _M_copy_from_ptr<_CharT, _Traits>(__s.data(), __s.size(), __pos, __n, 1274 1.1 mrg __zero, __one); } 1275 1.1 mrg 1276 1.1 mrg template<class _CharT, class _Traits, class _Alloc> 1277 1.1 mrg void 1278 1.1 mrg _M_copy_to_string(std::basic_string<_CharT, _Traits, _Alloc>&, 1279 1.1 mrg _CharT, _CharT) const; 1280 1.1 mrg 1281 1.1 mrg // NB: Backward compat. 1282 1.1 mrg template<class _CharT, class _Traits, class _Alloc> 1283 1.1 mrg void 1284 1.1 mrg _M_copy_from_string(const std::basic_string<_CharT, 1285 1.1 mrg _Traits, _Alloc>& __s, size_t __pos, size_t __n) 1286 1.1 mrg { _M_copy_from_string(__s, __pos, __n, _CharT('0'), _CharT('1')); } 1287 1.1 mrg 1288 1.1 mrg template<class _CharT, class _Traits, class _Alloc> 1289 1.1 mrg void 1290 1.1 mrg _M_copy_to_string(std::basic_string<_CharT, _Traits,_Alloc>& __s) const 1291 1.1 mrg { _M_copy_to_string(__s, _CharT('0'), _CharT('1')); } 1292 1.1 mrg 1293 1.1 mrg /// Returns the number of bits which are set. 1294 1.1 mrg size_t 1295 1.3 mrg count() const _GLIBCXX_NOEXCEPT 1296 1.1 mrg { return this->_M_do_count(); } 1297 1.1 mrg 1298 1.1 mrg /// Returns the total number of bits. 1299 1.3 mrg _GLIBCXX_CONSTEXPR size_t 1300 1.3 mrg size() const _GLIBCXX_NOEXCEPT 1301 1.1 mrg { return _Nb; } 1302 1.1 mrg 1303 1.11 mrg ///@{ 1304 1.1 mrg /// These comparisons for equality/inequality are, well, @e bitwise. 1305 1.1 mrg bool 1306 1.3 mrg operator==(const bitset<_Nb>& __rhs) const _GLIBCXX_NOEXCEPT 1307 1.1 mrg { return this->_M_is_equal(__rhs); } 1308 1.1 mrg 1309 1.11 mrg #if __cpp_impl_three_way_comparison < 201907L 1310 1.1 mrg bool 1311 1.3 mrg operator!=(const bitset<_Nb>& __rhs) const _GLIBCXX_NOEXCEPT 1312 1.1 mrg { return !this->_M_is_equal(__rhs); } 1313 1.11 mrg #endif 1314 1.11 mrg ///@} 1315 1.8 mrg 1316 1.1 mrg /** 1317 1.1 mrg * @brief Tests the value of a bit. 1318 1.3 mrg * @param __position The index of a bit. 1319 1.1 mrg * @return The value at @a pos. 1320 1.1 mrg * @throw std::out_of_range If @a pos is bigger the size of the %set. 1321 1.1 mrg */ 1322 1.1 mrg bool 1323 1.1 mrg test(size_t __position) const 1324 1.1 mrg { 1325 1.5 mrg this->_M_check(__position, __N("bitset::test")); 1326 1.1 mrg return _Unchecked_test(__position); 1327 1.1 mrg } 1328 1.1 mrg 1329 1.1 mrg // _GLIBCXX_RESOLVE_LIB_DEFECTS 1330 1.1 mrg // DR 693. std::bitset::all() missing. 1331 1.1 mrg /** 1332 1.1 mrg * @brief Tests whether all the bits are on. 1333 1.1 mrg * @return True if all the bits are set. 1334 1.1 mrg */ 1335 1.1 mrg bool 1336 1.3 mrg all() const _GLIBCXX_NOEXCEPT 1337 1.3 mrg { return this->template _M_are_all<_Nb>(); } 1338 1.1 mrg 1339 1.1 mrg /** 1340 1.1 mrg * @brief Tests whether any of the bits are on. 1341 1.1 mrg * @return True if at least one bit is set. 1342 1.1 mrg */ 1343 1.1 mrg bool 1344 1.3 mrg any() const _GLIBCXX_NOEXCEPT 1345 1.1 mrg { return this->_M_is_any(); } 1346 1.1 mrg 1347 1.1 mrg /** 1348 1.1 mrg * @brief Tests whether any of the bits are on. 1349 1.1 mrg * @return True if none of the bits are set. 1350 1.1 mrg */ 1351 1.1 mrg bool 1352 1.3 mrg none() const _GLIBCXX_NOEXCEPT 1353 1.1 mrg { return !this->_M_is_any(); } 1354 1.1 mrg 1355 1.11 mrg ///@{ 1356 1.1 mrg /// Self-explanatory. 1357 1.1 mrg bitset<_Nb> 1358 1.3 mrg operator<<(size_t __position) const _GLIBCXX_NOEXCEPT 1359 1.1 mrg { return bitset<_Nb>(*this) <<= __position; } 1360 1.1 mrg 1361 1.1 mrg bitset<_Nb> 1362 1.3 mrg operator>>(size_t __position) const _GLIBCXX_NOEXCEPT 1363 1.1 mrg { return bitset<_Nb>(*this) >>= __position; } 1364 1.11 mrg ///@} 1365 1.8 mrg 1366 1.1 mrg /** 1367 1.1 mrg * @brief Finds the index of the first "on" bit. 1368 1.1 mrg * @return The index of the first bit set, or size() if not found. 1369 1.1 mrg * @ingroup SGIextensions 1370 1.1 mrg * @sa _Find_next 1371 1.1 mrg */ 1372 1.1 mrg size_t 1373 1.3 mrg _Find_first() const _GLIBCXX_NOEXCEPT 1374 1.1 mrg { return this->_M_do_find_first(_Nb); } 1375 1.1 mrg 1376 1.1 mrg /** 1377 1.1 mrg * @brief Finds the index of the next "on" bit after prev. 1378 1.1 mrg * @return The index of the next bit set, or size() if not found. 1379 1.3 mrg * @param __prev Where to start searching. 1380 1.1 mrg * @ingroup SGIextensions 1381 1.1 mrg * @sa _Find_first 1382 1.1 mrg */ 1383 1.1 mrg size_t 1384 1.3 mrg _Find_next(size_t __prev) const _GLIBCXX_NOEXCEPT 1385 1.1 mrg { return this->_M_do_find_next(__prev, _Nb); } 1386 1.1 mrg }; 1387 1.1 mrg 1388 1.1 mrg // Definitions of non-inline member functions. 1389 1.1 mrg template<size_t _Nb> 1390 1.1 mrg template<class _CharT, class _Traits> 1391 1.1 mrg void 1392 1.1 mrg bitset<_Nb>:: 1393 1.1 mrg _M_copy_from_ptr(const _CharT* __s, size_t __len, 1394 1.1 mrg size_t __pos, size_t __n, _CharT __zero, _CharT __one) 1395 1.1 mrg { 1396 1.1 mrg reset(); 1397 1.3 mrg const size_t __nbits = std::min(_Nb, std::min(__n, size_t(__len - __pos))); 1398 1.1 mrg for (size_t __i = __nbits; __i > 0; --__i) 1399 1.1 mrg { 1400 1.1 mrg const _CharT __c = __s[__pos + __nbits - __i]; 1401 1.1 mrg if (_Traits::eq(__c, __zero)) 1402 1.1 mrg ; 1403 1.1 mrg else if (_Traits::eq(__c, __one)) 1404 1.1 mrg _Unchecked_set(__i - 1); 1405 1.1 mrg else 1406 1.1 mrg __throw_invalid_argument(__N("bitset::_M_copy_from_ptr")); 1407 1.1 mrg } 1408 1.1 mrg } 1409 1.1 mrg 1410 1.1 mrg template<size_t _Nb> 1411 1.1 mrg template<class _CharT, class _Traits, class _Alloc> 1412 1.1 mrg void 1413 1.1 mrg bitset<_Nb>:: 1414 1.1 mrg _M_copy_to_string(std::basic_string<_CharT, _Traits, _Alloc>& __s, 1415 1.1 mrg _CharT __zero, _CharT __one) const 1416 1.1 mrg { 1417 1.1 mrg __s.assign(_Nb, __zero); 1418 1.12 mrg size_t __n = this->_Find_first(); 1419 1.12 mrg while (__n < _Nb) 1420 1.12 mrg { 1421 1.12 mrg __s[_Nb - __n - 1] = __one; 1422 1.12 mrg __n = _Find_next(__n); 1423 1.12 mrg } 1424 1.1 mrg } 1425 1.1 mrg 1426 1.1 mrg // 23.3.5.3 bitset operations: 1427 1.11 mrg ///@{ 1428 1.1 mrg /** 1429 1.1 mrg * @brief Global bitwise operations on bitsets. 1430 1.3 mrg * @param __x A bitset. 1431 1.3 mrg * @param __y A bitset of the same size as @a __x. 1432 1.1 mrg * @return A new bitset. 1433 1.1 mrg * 1434 1.1 mrg * These should be self-explanatory. 1435 1.1 mrg */ 1436 1.1 mrg template<size_t _Nb> 1437 1.1 mrg inline bitset<_Nb> 1438 1.3 mrg operator&(const bitset<_Nb>& __x, const bitset<_Nb>& __y) _GLIBCXX_NOEXCEPT 1439 1.1 mrg { 1440 1.1 mrg bitset<_Nb> __result(__x); 1441 1.1 mrg __result &= __y; 1442 1.1 mrg return __result; 1443 1.1 mrg } 1444 1.1 mrg 1445 1.1 mrg template<size_t _Nb> 1446 1.1 mrg inline bitset<_Nb> 1447 1.3 mrg operator|(const bitset<_Nb>& __x, const bitset<_Nb>& __y) _GLIBCXX_NOEXCEPT 1448 1.1 mrg { 1449 1.1 mrg bitset<_Nb> __result(__x); 1450 1.1 mrg __result |= __y; 1451 1.1 mrg return __result; 1452 1.1 mrg } 1453 1.1 mrg 1454 1.1 mrg template <size_t _Nb> 1455 1.1 mrg inline bitset<_Nb> 1456 1.3 mrg operator^(const bitset<_Nb>& __x, const bitset<_Nb>& __y) _GLIBCXX_NOEXCEPT 1457 1.1 mrg { 1458 1.1 mrg bitset<_Nb> __result(__x); 1459 1.1 mrg __result ^= __y; 1460 1.1 mrg return __result; 1461 1.1 mrg } 1462 1.11 mrg ///@} 1463 1.1 mrg 1464 1.11 mrg ///@{ 1465 1.1 mrg /** 1466 1.1 mrg * @brief Global I/O operators for bitsets. 1467 1.1 mrg * 1468 1.1 mrg * Direct I/O between streams and bitsets is supported. Output is 1469 1.1 mrg * straightforward. Input will skip whitespace, only accept @a 0 and @a 1 1470 1.1 mrg * characters, and will only extract as many digits as the %bitset will 1471 1.1 mrg * hold. 1472 1.1 mrg */ 1473 1.1 mrg template<class _CharT, class _Traits, size_t _Nb> 1474 1.1 mrg std::basic_istream<_CharT, _Traits>& 1475 1.1 mrg operator>>(std::basic_istream<_CharT, _Traits>& __is, bitset<_Nb>& __x) 1476 1.1 mrg { 1477 1.1 mrg typedef typename _Traits::char_type char_type; 1478 1.1 mrg typedef std::basic_istream<_CharT, _Traits> __istream_type; 1479 1.1 mrg typedef typename __istream_type::ios_base __ios_base; 1480 1.1 mrg 1481 1.1 mrg std::basic_string<_CharT, _Traits> __tmp; 1482 1.1 mrg __tmp.reserve(_Nb); 1483 1.1 mrg 1484 1.1 mrg // _GLIBCXX_RESOLVE_LIB_DEFECTS 1485 1.1 mrg // 303. Bitset input operator underspecified 1486 1.1 mrg const char_type __zero = __is.widen('0'); 1487 1.1 mrg const char_type __one = __is.widen('1'); 1488 1.1 mrg 1489 1.1 mrg typename __ios_base::iostate __state = __ios_base::goodbit; 1490 1.1 mrg typename __istream_type::sentry __sentry(__is); 1491 1.1 mrg if (__sentry) 1492 1.1 mrg { 1493 1.1 mrg __try 1494 1.1 mrg { 1495 1.1 mrg for (size_t __i = _Nb; __i > 0; --__i) 1496 1.1 mrg { 1497 1.1 mrg static typename _Traits::int_type __eof = _Traits::eof(); 1498 1.8 mrg 1499 1.1 mrg typename _Traits::int_type __c1 = __is.rdbuf()->sbumpc(); 1500 1.1 mrg if (_Traits::eq_int_type(__c1, __eof)) 1501 1.1 mrg { 1502 1.1 mrg __state |= __ios_base::eofbit; 1503 1.1 mrg break; 1504 1.1 mrg } 1505 1.1 mrg else 1506 1.1 mrg { 1507 1.1 mrg const char_type __c2 = _Traits::to_char_type(__c1); 1508 1.1 mrg if (_Traits::eq(__c2, __zero)) 1509 1.1 mrg __tmp.push_back(__zero); 1510 1.1 mrg else if (_Traits::eq(__c2, __one)) 1511 1.1 mrg __tmp.push_back(__one); 1512 1.1 mrg else if (_Traits:: 1513 1.1 mrg eq_int_type(__is.rdbuf()->sputbackc(__c2), 1514 1.1 mrg __eof)) 1515 1.1 mrg { 1516 1.1 mrg __state |= __ios_base::failbit; 1517 1.1 mrg break; 1518 1.1 mrg } 1519 1.1 mrg } 1520 1.1 mrg } 1521 1.1 mrg } 1522 1.1 mrg __catch(__cxxabiv1::__forced_unwind&) 1523 1.1 mrg { 1524 1.8 mrg __is._M_setstate(__ios_base::badbit); 1525 1.1 mrg __throw_exception_again; 1526 1.1 mrg } 1527 1.1 mrg __catch(...) 1528 1.1 mrg { __is._M_setstate(__ios_base::badbit); } 1529 1.1 mrg } 1530 1.1 mrg 1531 1.1 mrg if (__tmp.empty() && _Nb) 1532 1.1 mrg __state |= __ios_base::failbit; 1533 1.1 mrg else 1534 1.1 mrg __x._M_copy_from_string(__tmp, static_cast<size_t>(0), _Nb, 1535 1.1 mrg __zero, __one); 1536 1.1 mrg if (__state) 1537 1.1 mrg __is.setstate(__state); 1538 1.1 mrg return __is; 1539 1.1 mrg } 1540 1.1 mrg 1541 1.1 mrg template <class _CharT, class _Traits, size_t _Nb> 1542 1.1 mrg std::basic_ostream<_CharT, _Traits>& 1543 1.1 mrg operator<<(std::basic_ostream<_CharT, _Traits>& __os, 1544 1.1 mrg const bitset<_Nb>& __x) 1545 1.1 mrg { 1546 1.1 mrg std::basic_string<_CharT, _Traits> __tmp; 1547 1.1 mrg 1548 1.1 mrg // _GLIBCXX_RESOLVE_LIB_DEFECTS 1549 1.1 mrg // 396. what are characters zero and one. 1550 1.1 mrg const ctype<_CharT>& __ct = use_facet<ctype<_CharT> >(__os.getloc()); 1551 1.1 mrg __x._M_copy_to_string(__tmp, __ct.widen('0'), __ct.widen('1')); 1552 1.1 mrg return __os << __tmp; 1553 1.1 mrg } 1554 1.11 mrg ///@} 1555 1.1 mrg 1556 1.3 mrg _GLIBCXX_END_NAMESPACE_CONTAINER 1557 1.3 mrg } // namespace std 1558 1.1 mrg 1559 1.1 mrg #undef _GLIBCXX_BITSET_WORDS 1560 1.1 mrg #undef _GLIBCXX_BITSET_BITS_PER_WORD 1561 1.3 mrg #undef _GLIBCXX_BITSET_BITS_PER_ULL 1562 1.1 mrg 1563 1.3 mrg #if __cplusplus >= 201103L 1564 1.1 mrg 1565 1.3 mrg namespace std _GLIBCXX_VISIBILITY(default) 1566 1.3 mrg { 1567 1.3 mrg _GLIBCXX_BEGIN_NAMESPACE_VERSION 1568 1.1 mrg 1569 1.1 mrg // DR 1182. 1570 1.1 mrg /// std::hash specialization for bitset. 1571 1.1 mrg template<size_t _Nb> 1572 1.3 mrg struct hash<_GLIBCXX_STD_C::bitset<_Nb>> 1573 1.3 mrg : public __hash_base<size_t, _GLIBCXX_STD_C::bitset<_Nb>> 1574 1.1 mrg { 1575 1.1 mrg size_t 1576 1.3 mrg operator()(const _GLIBCXX_STD_C::bitset<_Nb>& __b) const noexcept 1577 1.1 mrg { 1578 1.1 mrg const size_t __clength = (_Nb + __CHAR_BIT__ - 1) / __CHAR_BIT__; 1579 1.3 mrg return std::_Hash_impl::hash(__b._M_getdata(), __clength); 1580 1.1 mrg } 1581 1.1 mrg }; 1582 1.1 mrg 1583 1.1 mrg template<> 1584 1.3 mrg struct hash<_GLIBCXX_STD_C::bitset<0>> 1585 1.3 mrg : public __hash_base<size_t, _GLIBCXX_STD_C::bitset<0>> 1586 1.1 mrg { 1587 1.1 mrg size_t 1588 1.3 mrg operator()(const _GLIBCXX_STD_C::bitset<0>&) const noexcept 1589 1.1 mrg { return 0; } 1590 1.1 mrg }; 1591 1.1 mrg 1592 1.3 mrg _GLIBCXX_END_NAMESPACE_VERSION 1593 1.3 mrg } // namespace 1594 1.1 mrg 1595 1.3 mrg #endif // C++11 1596 1.1 mrg 1597 1.1 mrg #ifdef _GLIBCXX_DEBUG 1598 1.1 mrg # include <debug/bitset> 1599 1.1 mrg #endif 1600 1.1 mrg 1601 1.1 mrg #endif /* _GLIBCXX_BITSET */ 1602