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