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