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