1// -*- C++ -*- 2//===----------------------------------------------------------------------===// 3// 4// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 5// See https://llvm.org/LICENSE.txt for license information. 6// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 7// 8//===----------------------------------------------------------------------===// 9 10#ifndef _LIBCPP_BITSET 11#define _LIBCPP_BITSET 12 13/* 14 bitset synopsis 15 16namespace std 17{ 18 19namespace std { 20 21template <size_t N> 22class bitset 23{ 24public: 25 // bit reference: 26 class reference 27 { 28 friend class bitset; 29 reference() noexcept; 30 public: 31 ~reference() noexcept; 32 reference& operator=(bool x) noexcept; // for b[i] = x; 33 reference& operator=(const reference&) noexcept; // for b[i] = b[j]; 34 bool operator~() const noexcept; // flips the bit 35 operator bool() const noexcept; // for x = b[i]; 36 reference& flip() noexcept; // for b[i].flip(); 37 }; 38 39 // 23.3.5.1 constructors: 40 constexpr bitset() noexcept; 41 constexpr bitset(unsigned long long val) noexcept; 42 template <class charT> 43 explicit bitset(const charT* str, 44 typename basic_string<charT>::size_type n = basic_string<charT>::npos, 45 charT zero = charT('0'), charT one = charT('1')); 46 template<class charT, class traits, class Allocator> 47 explicit bitset(const basic_string<charT,traits,Allocator>& str, 48 typename basic_string<charT,traits,Allocator>::size_type pos = 0, 49 typename basic_string<charT,traits,Allocator>::size_type n = 50 basic_string<charT,traits,Allocator>::npos, 51 charT zero = charT('0'), charT one = charT('1')); 52 53 // 23.3.5.2 bitset operations: 54 bitset& operator&=(const bitset& rhs) noexcept; 55 bitset& operator|=(const bitset& rhs) noexcept; 56 bitset& operator^=(const bitset& rhs) noexcept; 57 bitset& operator<<=(size_t pos) noexcept; 58 bitset& operator>>=(size_t pos) noexcept; 59 bitset& set() noexcept; 60 bitset& set(size_t pos, bool val = true); 61 bitset& reset() noexcept; 62 bitset& reset(size_t pos); 63 bitset operator~() const noexcept; 64 bitset& flip() noexcept; 65 bitset& flip(size_t pos); 66 67 // element access: 68 constexpr bool operator[](size_t pos) const; // for b[i]; 69 reference operator[](size_t pos); // for b[i]; 70 unsigned long to_ulong() const; 71 unsigned long long to_ullong() const; 72 template <class charT, class traits, class Allocator> 73 basic_string<charT, traits, Allocator> to_string(charT zero = charT('0'), charT one = charT('1')) const; 74 template <class charT, class traits> 75 basic_string<charT, traits, allocator<charT> > to_string(charT zero = charT('0'), charT one = charT('1')) const; 76 template <class charT> 77 basic_string<charT, char_traits<charT>, allocator<charT> > to_string(charT zero = charT('0'), charT one = charT('1')) const; 78 basic_string<char, char_traits<char>, allocator<char> > to_string(char zero = '0', char one = '1') const; 79 size_t count() const noexcept; 80 constexpr size_t size() const noexcept; 81 bool operator==(const bitset& rhs) const noexcept; 82 bool operator!=(const bitset& rhs) const noexcept; 83 bool test(size_t pos) const; 84 bool all() const noexcept; 85 bool any() const noexcept; 86 bool none() const noexcept; 87 bitset operator<<(size_t pos) const noexcept; 88 bitset operator>>(size_t pos) const noexcept; 89}; 90 91// 23.3.5.3 bitset operators: 92template <size_t N> 93bitset<N> operator&(const bitset<N>&, const bitset<N>&) noexcept; 94 95template <size_t N> 96bitset<N> operator|(const bitset<N>&, const bitset<N>&) noexcept; 97 98template <size_t N> 99bitset<N> operator^(const bitset<N>&, const bitset<N>&) noexcept; 100 101template <class charT, class traits, size_t N> 102basic_istream<charT, traits>& 103operator>>(basic_istream<charT, traits>& is, bitset<N>& x); 104 105template <class charT, class traits, size_t N> 106basic_ostream<charT, traits>& 107operator<<(basic_ostream<charT, traits>& os, const bitset<N>& x); 108 109template <size_t N> struct hash<std::bitset<N>>; 110 111} // std 112 113*/ 114 115#include <__algorithm/fill.h> 116#include <__assert> // all public C++ headers provide the assertion handler 117#include <__bit_reference> 118#include <__config> 119#include <__functional/unary_function.h> 120#include <climits> 121#include <cstddef> 122#include <iosfwd> 123#include <stdexcept> 124#include <string> 125#include <version> 126 127#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 128# pragma GCC system_header 129#endif 130 131_LIBCPP_PUSH_MACROS 132#include <__undef_macros> 133 134 135_LIBCPP_BEGIN_NAMESPACE_STD 136 137template <size_t _N_words, size_t _Size> 138class __bitset; 139 140template <size_t _N_words, size_t _Size> 141struct __has_storage_type<__bitset<_N_words, _Size> > 142{ 143 static const bool value = true; 144}; 145 146template <size_t _N_words, size_t _Size> 147class __bitset 148{ 149public: 150 typedef ptrdiff_t difference_type; 151 typedef size_t size_type; 152 typedef size_type __storage_type; 153protected: 154 typedef __bitset __self; 155 typedef __storage_type* __storage_pointer; 156 typedef const __storage_type* __const_storage_pointer; 157 static const unsigned __bits_per_word = static_cast<unsigned>(sizeof(__storage_type) * CHAR_BIT); 158 159 friend class __bit_reference<__bitset>; 160 friend class __bit_const_reference<__bitset>; 161 friend class __bit_iterator<__bitset, false>; 162 friend class __bit_iterator<__bitset, true>; 163 friend struct __bit_array<__bitset>; 164 165 __storage_type __first_[_N_words]; 166 167 typedef __bit_reference<__bitset> reference; 168 typedef __bit_const_reference<__bitset> const_reference; 169 typedef __bit_iterator<__bitset, false> iterator; 170 typedef __bit_iterator<__bitset, true> const_iterator; 171 172 _LIBCPP_INLINE_VISIBILITY 173 _LIBCPP_CONSTEXPR __bitset() _NOEXCEPT; 174 _LIBCPP_INLINE_VISIBILITY 175 explicit _LIBCPP_CONSTEXPR __bitset(unsigned long long __v) _NOEXCEPT; 176 177 _LIBCPP_INLINE_VISIBILITY reference __make_ref(size_t __pos) _NOEXCEPT 178 {return reference(__first_ + __pos / __bits_per_word, __storage_type(1) << __pos % __bits_per_word);} 179 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR const_reference __make_ref(size_t __pos) const _NOEXCEPT 180 {return const_reference(__first_ + __pos / __bits_per_word, __storage_type(1) << __pos % __bits_per_word);} 181 _LIBCPP_INLINE_VISIBILITY iterator __make_iter(size_t __pos) _NOEXCEPT 182 {return iterator(__first_ + __pos / __bits_per_word, __pos % __bits_per_word);} 183 _LIBCPP_INLINE_VISIBILITY const_iterator __make_iter(size_t __pos) const _NOEXCEPT 184 {return const_iterator(__first_ + __pos / __bits_per_word, __pos % __bits_per_word);} 185 186 _LIBCPP_INLINE_VISIBILITY 187 void operator&=(const __bitset& __v) _NOEXCEPT; 188 _LIBCPP_INLINE_VISIBILITY 189 void operator|=(const __bitset& __v) _NOEXCEPT; 190 _LIBCPP_INLINE_VISIBILITY 191 void operator^=(const __bitset& __v) _NOEXCEPT; 192 193 void flip() _NOEXCEPT; 194 _LIBCPP_INLINE_VISIBILITY unsigned long to_ulong() const 195 {return to_ulong(integral_constant<bool, _Size < sizeof(unsigned long) * CHAR_BIT>());} 196 _LIBCPP_INLINE_VISIBILITY unsigned long long to_ullong() const 197 {return to_ullong(integral_constant<bool, _Size < sizeof(unsigned long long) * CHAR_BIT>());} 198 199 bool all() const _NOEXCEPT; 200 bool any() const _NOEXCEPT; 201 _LIBCPP_INLINE_VISIBILITY 202 size_t __hash_code() const _NOEXCEPT; 203private: 204#ifdef _LIBCPP_CXX03_LANG 205 void __init(unsigned long long __v, false_type) _NOEXCEPT; 206 _LIBCPP_INLINE_VISIBILITY 207 void __init(unsigned long long __v, true_type) _NOEXCEPT; 208#endif // _LIBCPP_CXX03_LANG 209 unsigned long to_ulong(false_type) const; 210 _LIBCPP_INLINE_VISIBILITY 211 unsigned long to_ulong(true_type) const; 212 unsigned long long to_ullong(false_type) const; 213 _LIBCPP_INLINE_VISIBILITY 214 unsigned long long to_ullong(true_type) const; 215 _LIBCPP_INLINE_VISIBILITY 216 unsigned long long to_ullong(true_type, false_type) const; 217 unsigned long long to_ullong(true_type, true_type) const; 218}; 219 220template <size_t _N_words, size_t _Size> 221inline 222_LIBCPP_CONSTEXPR 223__bitset<_N_words, _Size>::__bitset() _NOEXCEPT 224#ifndef _LIBCPP_CXX03_LANG 225 : __first_{0} 226#endif 227{ 228#ifdef _LIBCPP_CXX03_LANG 229 _VSTD::fill_n(__first_, _N_words, __storage_type(0)); 230#endif 231} 232 233#ifdef _LIBCPP_CXX03_LANG 234 235template <size_t _N_words, size_t _Size> 236void 237__bitset<_N_words, _Size>::__init(unsigned long long __v, false_type) _NOEXCEPT 238{ 239 __storage_type __t[sizeof(unsigned long long) / sizeof(__storage_type)]; 240 size_t __sz = _Size; 241 for (size_t __i = 0; __i < sizeof(__t)/sizeof(__t[0]); ++__i, __v >>= __bits_per_word, __sz -= __bits_per_word ) 242 if ( __sz < __bits_per_word) 243 __t[__i] = static_cast<__storage_type>(__v) & ( 1ULL << __sz ) - 1; 244 else 245 __t[__i] = static_cast<__storage_type>(__v); 246 247 _VSTD::copy(__t, __t + sizeof(__t)/sizeof(__t[0]), __first_); 248 _VSTD::fill(__first_ + sizeof(__t)/sizeof(__t[0]), __first_ + sizeof(__first_)/sizeof(__first_[0]), 249 __storage_type(0)); 250} 251 252template <size_t _N_words, size_t _Size> 253inline _LIBCPP_INLINE_VISIBILITY 254void 255__bitset<_N_words, _Size>::__init(unsigned long long __v, true_type) _NOEXCEPT 256{ 257 __first_[0] = __v; 258 if (_Size < __bits_per_word) 259 __first_[0] &= ( 1ULL << _Size ) - 1; 260 261 _VSTD::fill(__first_ + 1, __first_ + sizeof(__first_)/sizeof(__first_[0]), __storage_type(0)); 262} 263 264#endif // _LIBCPP_CXX03_LANG 265 266template <size_t _N_words, size_t _Size> 267inline 268_LIBCPP_CONSTEXPR 269__bitset<_N_words, _Size>::__bitset(unsigned long long __v) _NOEXCEPT 270#ifndef _LIBCPP_CXX03_LANG 271#if __SIZEOF_SIZE_T__ == 8 272 : __first_{__v} 273#elif __SIZEOF_SIZE_T__ == 4 274 : __first_{static_cast<__storage_type>(__v), 275 _Size >= 2 * __bits_per_word ? static_cast<__storage_type>(__v >> __bits_per_word) 276 : static_cast<__storage_type>((__v >> __bits_per_word) & (__storage_type(1) << (_Size - __bits_per_word)) - 1)} 277#else 278#error This constructor has not been ported to this platform 279#endif 280#endif 281{ 282#ifdef _LIBCPP_CXX03_LANG 283 __init(__v, integral_constant<bool, sizeof(unsigned long long) == sizeof(__storage_type)>()); 284#endif 285} 286 287template <size_t _N_words, size_t _Size> 288inline 289void 290__bitset<_N_words, _Size>::operator&=(const __bitset& __v) _NOEXCEPT 291{ 292 for (size_type __i = 0; __i < _N_words; ++__i) 293 __first_[__i] &= __v.__first_[__i]; 294} 295 296template <size_t _N_words, size_t _Size> 297inline 298void 299__bitset<_N_words, _Size>::operator|=(const __bitset& __v) _NOEXCEPT 300{ 301 for (size_type __i = 0; __i < _N_words; ++__i) 302 __first_[__i] |= __v.__first_[__i]; 303} 304 305template <size_t _N_words, size_t _Size> 306inline 307void 308__bitset<_N_words, _Size>::operator^=(const __bitset& __v) _NOEXCEPT 309{ 310 for (size_type __i = 0; __i < _N_words; ++__i) 311 __first_[__i] ^= __v.__first_[__i]; 312} 313 314template <size_t _N_words, size_t _Size> 315void 316__bitset<_N_words, _Size>::flip() _NOEXCEPT 317{ 318 // do middle whole words 319 size_type __n = _Size; 320 __storage_pointer __p = __first_; 321 for (; __n >= __bits_per_word; ++__p, __n -= __bits_per_word) 322 *__p = ~*__p; 323 // do last partial word 324 if (__n > 0) 325 { 326 __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n); 327 __storage_type __b = *__p & __m; 328 *__p &= ~__m; 329 *__p |= ~__b & __m; 330 } 331} 332 333template <size_t _N_words, size_t _Size> 334unsigned long 335__bitset<_N_words, _Size>::to_ulong(false_type) const 336{ 337 const_iterator __e = __make_iter(_Size); 338 const_iterator __i = _VSTD::find(__make_iter(sizeof(unsigned long) * CHAR_BIT), __e, true); 339 if (__i != __e) 340 __throw_overflow_error("bitset to_ulong overflow error"); 341 342 return __first_[0]; 343} 344 345template <size_t _N_words, size_t _Size> 346inline 347unsigned long 348__bitset<_N_words, _Size>::to_ulong(true_type) const 349{ 350 return __first_[0]; 351} 352 353template <size_t _N_words, size_t _Size> 354unsigned long long 355__bitset<_N_words, _Size>::to_ullong(false_type) const 356{ 357 const_iterator __e = __make_iter(_Size); 358 const_iterator __i = _VSTD::find(__make_iter(sizeof(unsigned long long) * CHAR_BIT), __e, true); 359 if (__i != __e) 360 __throw_overflow_error("bitset to_ullong overflow error"); 361 362 return to_ullong(true_type()); 363} 364 365template <size_t _N_words, size_t _Size> 366inline 367unsigned long long 368__bitset<_N_words, _Size>::to_ullong(true_type) const 369{ 370 return to_ullong(true_type(), integral_constant<bool, sizeof(__storage_type) < sizeof(unsigned long long)>()); 371} 372 373template <size_t _N_words, size_t _Size> 374inline 375unsigned long long 376__bitset<_N_words, _Size>::to_ullong(true_type, false_type) const 377{ 378 return __first_[0]; 379} 380 381template <size_t _N_words, size_t _Size> 382unsigned long long 383__bitset<_N_words, _Size>::to_ullong(true_type, true_type) const 384{ 385 unsigned long long __r = __first_[0]; 386 for (size_t __i = 1; __i < sizeof(unsigned long long) / sizeof(__storage_type); ++__i) 387 __r |= static_cast<unsigned long long>(__first_[__i]) << (sizeof(__storage_type) * CHAR_BIT); 388 return __r; 389} 390 391template <size_t _N_words, size_t _Size> 392bool 393__bitset<_N_words, _Size>::all() const _NOEXCEPT 394{ 395 // do middle whole words 396 size_type __n = _Size; 397 __const_storage_pointer __p = __first_; 398 for (; __n >= __bits_per_word; ++__p, __n -= __bits_per_word) 399 if (~*__p) 400 return false; 401 // do last partial word 402 if (__n > 0) 403 { 404 __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n); 405 if (~*__p & __m) 406 return false; 407 } 408 return true; 409} 410 411template <size_t _N_words, size_t _Size> 412bool 413__bitset<_N_words, _Size>::any() const _NOEXCEPT 414{ 415 // do middle whole words 416 size_type __n = _Size; 417 __const_storage_pointer __p = __first_; 418 for (; __n >= __bits_per_word; ++__p, __n -= __bits_per_word) 419 if (*__p) 420 return true; 421 // do last partial word 422 if (__n > 0) 423 { 424 __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n); 425 if (*__p & __m) 426 return true; 427 } 428 return false; 429} 430 431template <size_t _N_words, size_t _Size> 432inline 433size_t 434__bitset<_N_words, _Size>::__hash_code() const _NOEXCEPT 435{ 436 size_t __h = 0; 437 for (size_type __i = 0; __i < _N_words; ++__i) 438 __h ^= __first_[__i]; 439 return __h; 440} 441 442template <size_t _Size> 443class __bitset<1, _Size> 444{ 445public: 446 typedef ptrdiff_t difference_type; 447 typedef size_t size_type; 448 typedef size_type __storage_type; 449protected: 450 typedef __bitset __self; 451 typedef __storage_type* __storage_pointer; 452 typedef const __storage_type* __const_storage_pointer; 453 static const unsigned __bits_per_word = static_cast<unsigned>(sizeof(__storage_type) * CHAR_BIT); 454 455 friend class __bit_reference<__bitset>; 456 friend class __bit_const_reference<__bitset>; 457 friend class __bit_iterator<__bitset, false>; 458 friend class __bit_iterator<__bitset, true>; 459 friend struct __bit_array<__bitset>; 460 461 __storage_type __first_; 462 463 typedef __bit_reference<__bitset> reference; 464 typedef __bit_const_reference<__bitset> const_reference; 465 typedef __bit_iterator<__bitset, false> iterator; 466 typedef __bit_iterator<__bitset, true> const_iterator; 467 468 _LIBCPP_INLINE_VISIBILITY 469 _LIBCPP_CONSTEXPR __bitset() _NOEXCEPT; 470 _LIBCPP_INLINE_VISIBILITY 471 explicit _LIBCPP_CONSTEXPR __bitset(unsigned long long __v) _NOEXCEPT; 472 473 _LIBCPP_INLINE_VISIBILITY reference __make_ref(size_t __pos) _NOEXCEPT 474 {return reference(&__first_, __storage_type(1) << __pos);} 475 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR const_reference __make_ref(size_t __pos) const _NOEXCEPT 476 {return const_reference(&__first_, __storage_type(1) << __pos);} 477 _LIBCPP_INLINE_VISIBILITY iterator __make_iter(size_t __pos) _NOEXCEPT 478 {return iterator(&__first_ + __pos / __bits_per_word, __pos % __bits_per_word);} 479 _LIBCPP_INLINE_VISIBILITY const_iterator __make_iter(size_t __pos) const _NOEXCEPT 480 {return const_iterator(&__first_ + __pos / __bits_per_word, __pos % __bits_per_word);} 481 482 _LIBCPP_INLINE_VISIBILITY 483 void operator&=(const __bitset& __v) _NOEXCEPT; 484 _LIBCPP_INLINE_VISIBILITY 485 void operator|=(const __bitset& __v) _NOEXCEPT; 486 _LIBCPP_INLINE_VISIBILITY 487 void operator^=(const __bitset& __v) _NOEXCEPT; 488 489 _LIBCPP_INLINE_VISIBILITY 490 void flip() _NOEXCEPT; 491 492 _LIBCPP_INLINE_VISIBILITY 493 unsigned long to_ulong() const; 494 _LIBCPP_INLINE_VISIBILITY 495 unsigned long long to_ullong() const; 496 497 _LIBCPP_INLINE_VISIBILITY 498 bool all() const _NOEXCEPT; 499 _LIBCPP_INLINE_VISIBILITY 500 bool any() const _NOEXCEPT; 501 502 _LIBCPP_INLINE_VISIBILITY 503 size_t __hash_code() const _NOEXCEPT; 504}; 505 506template <size_t _Size> 507inline 508_LIBCPP_CONSTEXPR 509__bitset<1, _Size>::__bitset() _NOEXCEPT 510 : __first_(0) 511{ 512} 513 514template <size_t _Size> 515inline 516_LIBCPP_CONSTEXPR 517__bitset<1, _Size>::__bitset(unsigned long long __v) _NOEXCEPT 518 : __first_( 519 _Size == __bits_per_word ? static_cast<__storage_type>(__v) 520 : static_cast<__storage_type>(__v) & ((__storage_type(1) << _Size) - 1) 521 ) 522{ 523} 524 525template <size_t _Size> 526inline 527void 528__bitset<1, _Size>::operator&=(const __bitset& __v) _NOEXCEPT 529{ 530 __first_ &= __v.__first_; 531} 532 533template <size_t _Size> 534inline 535void 536__bitset<1, _Size>::operator|=(const __bitset& __v) _NOEXCEPT 537{ 538 __first_ |= __v.__first_; 539} 540 541template <size_t _Size> 542inline 543void 544__bitset<1, _Size>::operator^=(const __bitset& __v) _NOEXCEPT 545{ 546 __first_ ^= __v.__first_; 547} 548 549template <size_t _Size> 550inline 551void 552__bitset<1, _Size>::flip() _NOEXCEPT 553{ 554 __storage_type __m = ~__storage_type(0) >> (__bits_per_word - _Size); 555 __first_ = ~__first_; 556 __first_ &= __m; 557} 558 559template <size_t _Size> 560inline 561unsigned long 562__bitset<1, _Size>::to_ulong() const 563{ 564 return __first_; 565} 566 567template <size_t _Size> 568inline 569unsigned long long 570__bitset<1, _Size>::to_ullong() const 571{ 572 return __first_; 573} 574 575template <size_t _Size> 576inline 577bool 578__bitset<1, _Size>::all() const _NOEXCEPT 579{ 580 __storage_type __m = ~__storage_type(0) >> (__bits_per_word - _Size); 581 return !(~__first_ & __m); 582} 583 584template <size_t _Size> 585inline 586bool 587__bitset<1, _Size>::any() const _NOEXCEPT 588{ 589 __storage_type __m = ~__storage_type(0) >> (__bits_per_word - _Size); 590 return __first_ & __m; 591} 592 593template <size_t _Size> 594inline 595size_t 596__bitset<1, _Size>::__hash_code() const _NOEXCEPT 597{ 598 return __first_; 599} 600 601template <> 602class __bitset<0, 0> 603{ 604public: 605 typedef ptrdiff_t difference_type; 606 typedef size_t size_type; 607 typedef size_type __storage_type; 608protected: 609 typedef __bitset __self; 610 typedef __storage_type* __storage_pointer; 611 typedef const __storage_type* __const_storage_pointer; 612 static const unsigned __bits_per_word = static_cast<unsigned>(sizeof(__storage_type) * CHAR_BIT); 613 614 friend class __bit_reference<__bitset>; 615 friend class __bit_const_reference<__bitset>; 616 friend class __bit_iterator<__bitset, false>; 617 friend class __bit_iterator<__bitset, true>; 618 friend struct __bit_array<__bitset>; 619 620 typedef __bit_reference<__bitset> reference; 621 typedef __bit_const_reference<__bitset> const_reference; 622 typedef __bit_iterator<__bitset, false> iterator; 623 typedef __bit_iterator<__bitset, true> const_iterator; 624 625 _LIBCPP_INLINE_VISIBILITY 626 _LIBCPP_CONSTEXPR __bitset() _NOEXCEPT; 627 _LIBCPP_INLINE_VISIBILITY 628 explicit _LIBCPP_CONSTEXPR __bitset(unsigned long long) _NOEXCEPT; 629 630 _LIBCPP_INLINE_VISIBILITY reference __make_ref(size_t) _NOEXCEPT 631 {return reference(nullptr, 1);} 632 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR const_reference __make_ref(size_t) const _NOEXCEPT 633 {return const_reference(nullptr, 1);} 634 _LIBCPP_INLINE_VISIBILITY iterator __make_iter(size_t) _NOEXCEPT 635 {return iterator(nullptr, 0);} 636 _LIBCPP_INLINE_VISIBILITY const_iterator __make_iter(size_t) const _NOEXCEPT 637 {return const_iterator(nullptr, 0);} 638 639 _LIBCPP_INLINE_VISIBILITY void operator&=(const __bitset&) _NOEXCEPT {} 640 _LIBCPP_INLINE_VISIBILITY void operator|=(const __bitset&) _NOEXCEPT {} 641 _LIBCPP_INLINE_VISIBILITY void operator^=(const __bitset&) _NOEXCEPT {} 642 643 _LIBCPP_INLINE_VISIBILITY void flip() _NOEXCEPT {} 644 645 _LIBCPP_INLINE_VISIBILITY unsigned long to_ulong() const {return 0;} 646 _LIBCPP_INLINE_VISIBILITY unsigned long long to_ullong() const {return 0;} 647 648 _LIBCPP_INLINE_VISIBILITY bool all() const _NOEXCEPT {return true;} 649 _LIBCPP_INLINE_VISIBILITY bool any() const _NOEXCEPT {return false;} 650 651 _LIBCPP_INLINE_VISIBILITY size_t __hash_code() const _NOEXCEPT {return 0;} 652}; 653 654inline 655_LIBCPP_CONSTEXPR 656__bitset<0, 0>::__bitset() _NOEXCEPT 657{ 658} 659 660inline 661_LIBCPP_CONSTEXPR 662__bitset<0, 0>::__bitset(unsigned long long) _NOEXCEPT 663{ 664} 665 666template <size_t _Size> class _LIBCPP_TEMPLATE_VIS bitset; 667template <size_t _Size> struct hash<bitset<_Size> >; 668 669template <size_t _Size> 670class _LIBCPP_TEMPLATE_VIS bitset 671 : private __bitset<_Size == 0 ? 0 : (_Size - 1) / (sizeof(size_t) * CHAR_BIT) + 1, _Size> 672{ 673public: 674 static const unsigned __n_words = _Size == 0 ? 0 : (_Size - 1) / (sizeof(size_t) * CHAR_BIT) + 1; 675 typedef __bitset<__n_words, _Size> base; 676 677public: 678 typedef typename base::reference reference; 679 typedef typename base::const_reference const_reference; 680 681 // 23.3.5.1 constructors: 682 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR bitset() _NOEXCEPT {} 683 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR 684 bitset(unsigned long long __v) _NOEXCEPT : base(__v) {} 685 template<class _CharT, class = __enable_if_t<_IsCharLikeType<_CharT>::value> > 686 explicit bitset(const _CharT* __str, 687 typename basic_string<_CharT>::size_type __n = basic_string<_CharT>::npos, 688 _CharT __zero = _CharT('0'), _CharT __one = _CharT('1')); 689 template<class _CharT, class _Traits, class _Allocator> 690 explicit bitset(const basic_string<_CharT,_Traits,_Allocator>& __str, 691 typename basic_string<_CharT,_Traits,_Allocator>::size_type __pos = 0, 692 typename basic_string<_CharT,_Traits,_Allocator>::size_type __n = 693 (basic_string<_CharT,_Traits,_Allocator>::npos), 694 _CharT __zero = _CharT('0'), _CharT __one = _CharT('1')); 695 696 // 23.3.5.2 bitset operations: 697 _LIBCPP_INLINE_VISIBILITY 698 bitset& operator&=(const bitset& __rhs) _NOEXCEPT; 699 _LIBCPP_INLINE_VISIBILITY 700 bitset& operator|=(const bitset& __rhs) _NOEXCEPT; 701 _LIBCPP_INLINE_VISIBILITY 702 bitset& operator^=(const bitset& __rhs) _NOEXCEPT; 703 bitset& operator<<=(size_t __pos) _NOEXCEPT; 704 bitset& operator>>=(size_t __pos) _NOEXCEPT; 705 _LIBCPP_INLINE_VISIBILITY 706 bitset& set() _NOEXCEPT; 707 bitset& set(size_t __pos, bool __val = true); 708 _LIBCPP_INLINE_VISIBILITY 709 bitset& reset() _NOEXCEPT; 710 bitset& reset(size_t __pos); 711 _LIBCPP_INLINE_VISIBILITY 712 bitset operator~() const _NOEXCEPT; 713 _LIBCPP_INLINE_VISIBILITY 714 bitset& flip() _NOEXCEPT; 715 bitset& flip(size_t __pos); 716 717 // element access: 718 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR 719 const_reference operator[](size_t __p) const {return base::__make_ref(__p);} 720 _LIBCPP_INLINE_VISIBILITY reference operator[](size_t __p) {return base::__make_ref(__p);} 721 _LIBCPP_INLINE_VISIBILITY 722 unsigned long to_ulong() const; 723 _LIBCPP_INLINE_VISIBILITY 724 unsigned long long to_ullong() const; 725 template <class _CharT, class _Traits, class _Allocator> 726 basic_string<_CharT, _Traits, _Allocator> to_string(_CharT __zero = _CharT('0'), 727 _CharT __one = _CharT('1')) const; 728 template <class _CharT, class _Traits> 729 _LIBCPP_INLINE_VISIBILITY 730 basic_string<_CharT, _Traits, allocator<_CharT> > to_string(_CharT __zero = _CharT('0'), 731 _CharT __one = _CharT('1')) const; 732 template <class _CharT> 733 _LIBCPP_INLINE_VISIBILITY 734 basic_string<_CharT, char_traits<_CharT>, allocator<_CharT> > to_string(_CharT __zero = _CharT('0'), 735 _CharT __one = _CharT('1')) const; 736 _LIBCPP_INLINE_VISIBILITY 737 basic_string<char, char_traits<char>, allocator<char> > to_string(char __zero = '0', 738 char __one = '1') const; 739 _LIBCPP_INLINE_VISIBILITY 740 size_t count() const _NOEXCEPT; 741 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR size_t size() const _NOEXCEPT {return _Size;} 742 _LIBCPP_INLINE_VISIBILITY 743 bool operator==(const bitset& __rhs) const _NOEXCEPT; 744 _LIBCPP_INLINE_VISIBILITY 745 bool operator!=(const bitset& __rhs) const _NOEXCEPT; 746 bool test(size_t __pos) const; 747 _LIBCPP_INLINE_VISIBILITY 748 bool all() const _NOEXCEPT; 749 _LIBCPP_INLINE_VISIBILITY 750 bool any() const _NOEXCEPT; 751 _LIBCPP_INLINE_VISIBILITY bool none() const _NOEXCEPT {return !any();} 752 _LIBCPP_INLINE_VISIBILITY 753 bitset operator<<(size_t __pos) const _NOEXCEPT; 754 _LIBCPP_INLINE_VISIBILITY 755 bitset operator>>(size_t __pos) const _NOEXCEPT; 756 757private: 758 759 _LIBCPP_INLINE_VISIBILITY 760 size_t __hash_code() const _NOEXCEPT {return base::__hash_code();} 761 762 friend struct hash<bitset>; 763}; 764 765template <size_t _Size> 766template<class _CharT, class> 767bitset<_Size>::bitset(const _CharT* __str, 768 typename basic_string<_CharT>::size_type __n, 769 _CharT __zero, _CharT __one) 770{ 771 size_t __rlen = _VSTD::min(__n, char_traits<_CharT>::length(__str)); 772 for (size_t __i = 0; __i < __rlen; ++__i) 773 if (__str[__i] != __zero && __str[__i] != __one) 774 __throw_invalid_argument("bitset string ctor has invalid argument"); 775 776 size_t _Mp = _VSTD::min(__rlen, _Size); 777 size_t __i = 0; 778 for (; __i < _Mp; ++__i) 779 { 780 _CharT __c = __str[_Mp - 1 - __i]; 781 (*this)[__i] = (__c == __one); 782 } 783 _VSTD::fill(base::__make_iter(__i), base::__make_iter(_Size), false); 784} 785 786template <size_t _Size> 787template<class _CharT, class _Traits, class _Allocator> 788bitset<_Size>::bitset(const basic_string<_CharT,_Traits,_Allocator>& __str, 789 typename basic_string<_CharT,_Traits,_Allocator>::size_type __pos, 790 typename basic_string<_CharT,_Traits,_Allocator>::size_type __n, 791 _CharT __zero, _CharT __one) 792{ 793 if (__pos > __str.size()) 794 __throw_out_of_range("bitset string pos out of range"); 795 796 size_t __rlen = _VSTD::min(__n, __str.size() - __pos); 797 for (size_t __i = __pos; __i < __pos + __rlen; ++__i) 798 if (!_Traits::eq(__str[__i], __zero) && !_Traits::eq(__str[__i], __one)) 799 __throw_invalid_argument("bitset string ctor has invalid argument"); 800 801 size_t _Mp = _VSTD::min(__rlen, _Size); 802 size_t __i = 0; 803 for (; __i < _Mp; ++__i) 804 { 805 _CharT __c = __str[__pos + _Mp - 1 - __i]; 806 (*this)[__i] = _Traits::eq(__c, __one); 807 } 808 _VSTD::fill(base::__make_iter(__i), base::__make_iter(_Size), false); 809} 810 811template <size_t _Size> 812inline 813bitset<_Size>& 814bitset<_Size>::operator&=(const bitset& __rhs) _NOEXCEPT 815{ 816 base::operator&=(__rhs); 817 return *this; 818} 819 820template <size_t _Size> 821inline 822bitset<_Size>& 823bitset<_Size>::operator|=(const bitset& __rhs) _NOEXCEPT 824{ 825 base::operator|=(__rhs); 826 return *this; 827} 828 829template <size_t _Size> 830inline 831bitset<_Size>& 832bitset<_Size>::operator^=(const bitset& __rhs) _NOEXCEPT 833{ 834 base::operator^=(__rhs); 835 return *this; 836} 837 838template <size_t _Size> 839bitset<_Size>& 840bitset<_Size>::operator<<=(size_t __pos) _NOEXCEPT 841{ 842 __pos = _VSTD::min(__pos, _Size); 843 _VSTD::copy_backward(base::__make_iter(0), base::__make_iter(_Size - __pos), base::__make_iter(_Size)); 844 _VSTD::fill_n(base::__make_iter(0), __pos, false); 845 return *this; 846} 847 848template <size_t _Size> 849bitset<_Size>& 850bitset<_Size>::operator>>=(size_t __pos) _NOEXCEPT 851{ 852 __pos = _VSTD::min(__pos, _Size); 853 _VSTD::copy(base::__make_iter(__pos), base::__make_iter(_Size), base::__make_iter(0)); 854 _VSTD::fill_n(base::__make_iter(_Size - __pos), __pos, false); 855 return *this; 856} 857 858template <size_t _Size> 859inline 860bitset<_Size>& 861bitset<_Size>::set() _NOEXCEPT 862{ 863 _VSTD::fill_n(base::__make_iter(0), _Size, true); 864 return *this; 865} 866 867template <size_t _Size> 868bitset<_Size>& 869bitset<_Size>::set(size_t __pos, bool __val) 870{ 871 if (__pos >= _Size) 872 __throw_out_of_range("bitset set argument out of range"); 873 874 (*this)[__pos] = __val; 875 return *this; 876} 877 878template <size_t _Size> 879inline 880bitset<_Size>& 881bitset<_Size>::reset() _NOEXCEPT 882{ 883 _VSTD::fill_n(base::__make_iter(0), _Size, false); 884 return *this; 885} 886 887template <size_t _Size> 888bitset<_Size>& 889bitset<_Size>::reset(size_t __pos) 890{ 891 if (__pos >= _Size) 892 __throw_out_of_range("bitset reset argument out of range"); 893 894 (*this)[__pos] = false; 895 return *this; 896} 897 898template <size_t _Size> 899inline 900bitset<_Size> 901bitset<_Size>::operator~() const _NOEXCEPT 902{ 903 bitset __x(*this); 904 __x.flip(); 905 return __x; 906} 907 908template <size_t _Size> 909inline 910bitset<_Size>& 911bitset<_Size>::flip() _NOEXCEPT 912{ 913 base::flip(); 914 return *this; 915} 916 917template <size_t _Size> 918bitset<_Size>& 919bitset<_Size>::flip(size_t __pos) 920{ 921 if (__pos >= _Size) 922 __throw_out_of_range("bitset flip argument out of range"); 923 924 reference r = base::__make_ref(__pos); 925 r = ~r; 926 return *this; 927} 928 929template <size_t _Size> 930inline 931unsigned long 932bitset<_Size>::to_ulong() const 933{ 934 return base::to_ulong(); 935} 936 937template <size_t _Size> 938inline 939unsigned long long 940bitset<_Size>::to_ullong() const 941{ 942 return base::to_ullong(); 943} 944 945template <size_t _Size> 946template <class _CharT, class _Traits, class _Allocator> 947basic_string<_CharT, _Traits, _Allocator> 948bitset<_Size>::to_string(_CharT __zero, _CharT __one) const 949{ 950 basic_string<_CharT, _Traits, _Allocator> __r(_Size, __zero); 951 for (size_t __i = 0; __i != _Size; ++__i) 952 { 953 if ((*this)[__i]) 954 __r[_Size - 1 - __i] = __one; 955 } 956 return __r; 957} 958 959template <size_t _Size> 960template <class _CharT, class _Traits> 961inline 962basic_string<_CharT, _Traits, allocator<_CharT> > 963bitset<_Size>::to_string(_CharT __zero, _CharT __one) const 964{ 965 return to_string<_CharT, _Traits, allocator<_CharT> >(__zero, __one); 966} 967 968template <size_t _Size> 969template <class _CharT> 970inline 971basic_string<_CharT, char_traits<_CharT>, allocator<_CharT> > 972bitset<_Size>::to_string(_CharT __zero, _CharT __one) const 973{ 974 return to_string<_CharT, char_traits<_CharT>, allocator<_CharT> >(__zero, __one); 975} 976 977template <size_t _Size> 978inline 979basic_string<char, char_traits<char>, allocator<char> > 980bitset<_Size>::to_string(char __zero, char __one) const 981{ 982 return to_string<char, char_traits<char>, allocator<char> >(__zero, __one); 983} 984 985template <size_t _Size> 986inline 987size_t 988bitset<_Size>::count() const _NOEXCEPT 989{ 990 return static_cast<size_t>(_VSTD::__count_bool_true(base::__make_iter(0), _Size)); 991} 992 993template <size_t _Size> 994inline 995bool 996bitset<_Size>::operator==(const bitset& __rhs) const _NOEXCEPT 997{ 998 return _VSTD::equal(base::__make_iter(0), base::__make_iter(_Size), __rhs.__make_iter(0)); 999} 1000 1001template <size_t _Size> 1002inline 1003bool 1004bitset<_Size>::operator!=(const bitset& __rhs) const _NOEXCEPT 1005{ 1006 return !(*this == __rhs); 1007} 1008 1009template <size_t _Size> 1010bool 1011bitset<_Size>::test(size_t __pos) const 1012{ 1013 if (__pos >= _Size) 1014 __throw_out_of_range("bitset test argument out of range"); 1015 1016 return (*this)[__pos]; 1017} 1018 1019template <size_t _Size> 1020inline 1021bool 1022bitset<_Size>::all() const _NOEXCEPT 1023{ 1024 return base::all(); 1025} 1026 1027template <size_t _Size> 1028inline 1029bool 1030bitset<_Size>::any() const _NOEXCEPT 1031{ 1032 return base::any(); 1033} 1034 1035template <size_t _Size> 1036inline 1037bitset<_Size> 1038bitset<_Size>::operator<<(size_t __pos) const _NOEXCEPT 1039{ 1040 bitset __r = *this; 1041 __r <<= __pos; 1042 return __r; 1043} 1044 1045template <size_t _Size> 1046inline 1047bitset<_Size> 1048bitset<_Size>::operator>>(size_t __pos) const _NOEXCEPT 1049{ 1050 bitset __r = *this; 1051 __r >>= __pos; 1052 return __r; 1053} 1054 1055template <size_t _Size> 1056inline _LIBCPP_INLINE_VISIBILITY 1057bitset<_Size> 1058operator&(const bitset<_Size>& __x, const bitset<_Size>& __y) _NOEXCEPT 1059{ 1060 bitset<_Size> __r = __x; 1061 __r &= __y; 1062 return __r; 1063} 1064 1065template <size_t _Size> 1066inline _LIBCPP_INLINE_VISIBILITY 1067bitset<_Size> 1068operator|(const bitset<_Size>& __x, const bitset<_Size>& __y) _NOEXCEPT 1069{ 1070 bitset<_Size> __r = __x; 1071 __r |= __y; 1072 return __r; 1073} 1074 1075template <size_t _Size> 1076inline _LIBCPP_INLINE_VISIBILITY 1077bitset<_Size> 1078operator^(const bitset<_Size>& __x, const bitset<_Size>& __y) _NOEXCEPT 1079{ 1080 bitset<_Size> __r = __x; 1081 __r ^= __y; 1082 return __r; 1083} 1084 1085template <size_t _Size> 1086struct _LIBCPP_TEMPLATE_VIS hash<bitset<_Size> > 1087 : public unary_function<bitset<_Size>, size_t> 1088{ 1089 _LIBCPP_INLINE_VISIBILITY 1090 size_t operator()(const bitset<_Size>& __bs) const _NOEXCEPT 1091 {return __bs.__hash_code();} 1092}; 1093 1094template <class _CharT, class _Traits, size_t _Size> 1095basic_istream<_CharT, _Traits>& 1096operator>>(basic_istream<_CharT, _Traits>& __is, bitset<_Size>& __x); 1097 1098template <class _CharT, class _Traits, size_t _Size> 1099basic_ostream<_CharT, _Traits>& 1100operator<<(basic_ostream<_CharT, _Traits>& __os, const bitset<_Size>& __x); 1101 1102_LIBCPP_END_NAMESPACE_STD 1103 1104_LIBCPP_POP_MACROS 1105 1106#endif // _LIBCPP_BITSET 1107