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