887 lines · plain
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-exception7//8//===----------------------------------------------------------------------===//9 10#ifndef _LIBCPP___CXX03_BITSET11#define _LIBCPP___CXX03_BITSET12 13// clang-format off14 15/*16 bitset synopsis17 18namespace std19{20 21namespace std {22 23template <size_t N>24class bitset25{26public:27 // bit reference:28 class reference29 {30 friend class bitset;31 reference() noexcept;32 public:33 ~reference() noexcept;34 reference& operator=(bool x) noexcept; // for b[i] = x;35 reference& operator=(const reference&) noexcept; // for b[i] = b[j];36 bool operator~() const noexcept; // flips the bit37 operator bool() const noexcept; // for x = b[i];38 reference& flip() noexcept; // for b[i].flip();39 };40 41 // 23.3.5.1 constructors:42 constexpr bitset() noexcept;43 constexpr bitset(unsigned long long val) noexcept;44 template <class charT>45 constexpr explicit bitset(const charT* str,46 typename basic_string<charT>::size_type n = basic_string<charT>::npos,47 charT zero = charT('0'), charT one = charT('1')); // until C++26, constexpr since C++2348 template <class charT>49 constexpr explicit bitset(const charT* str,50 typename basic_string_view<charT>::size_type n = basic_string_view<charT>::npos,51 charT zero = charT('0'), charT one = charT('1')); // since C++2652 template<class charT, class traits>53 explicit bitset(54 const basic_string_view<charT,traits>& str,55 typename basic_string_view<charT,traits>::size_type pos = 0,56 typename basic_string_view<charT,traits>::size_type n = basic_string_view<charT,traits>::npos,57 charT zero = charT('0'), charT one = charT('1')); // since C++2658 template<class charT, class traits, class Allocator>59 constexpr explicit bitset(60 const basic_string<charT,traits,Allocator>& str,61 typename basic_string<charT,traits,Allocator>::size_type pos = 0,62 typename basic_string<charT,traits,Allocator>::size_type n = basic_string<charT,traits,Allocator>::npos,63 charT zero = charT('0'), charT one = charT('1')); // constexpr since C++2364 65 // 23.3.5.2 bitset operations:66 bitset& operator&=(const bitset& rhs) noexcept; // constexpr since C++2367 bitset& operator|=(const bitset& rhs) noexcept; // constexpr since C++2368 bitset& operator^=(const bitset& rhs) noexcept; // constexpr since C++2369 bitset& operator<<=(size_t pos) noexcept; // constexpr since C++2370 bitset& operator>>=(size_t pos) noexcept; // constexpr since C++2371 bitset& set() noexcept; // constexpr since C++2372 bitset& set(size_t pos, bool val = true); // constexpr since C++2373 bitset& reset() noexcept; // constexpr since C++2374 bitset& reset(size_t pos); // constexpr since C++2375 bitset operator~() const noexcept; // constexpr since C++2376 bitset& flip() noexcept; // constexpr since C++2377 bitset& flip(size_t pos); // constexpr since C++2378 79 // element access:80 constexpr bool operator[](size_t pos) const;81 reference operator[](size_t pos); // constexpr since C++2382 unsigned long to_ulong() const; // constexpr since C++2383 unsigned long long to_ullong() const; // constexpr since C++2384 template <class charT, class traits, class Allocator> // constexpr since C++2385 basic_string<charT, traits, Allocator> to_string(charT zero = charT('0'), charT one = charT('1')) const;86 template <class charT, class traits> // constexpr since C++2387 basic_string<charT, traits, allocator<charT> > to_string(charT zero = charT('0'), charT one = charT('1')) const;88 template <class charT> // constexpr since C++2389 basic_string<charT, char_traits<charT>, allocator<charT> > to_string(charT zero = charT('0'), charT one = charT('1')) const;90 basic_string<char, char_traits<char>, allocator<char> > to_string(char zero = '0', char one = '1') const; // constexpr since C++2391 size_t count() const noexcept; // constexpr since C++2392 constexpr size_t size() const noexcept; // constexpr since C++2393 bool operator==(const bitset& rhs) const noexcept; // constexpr since C++2394 bool operator!=(const bitset& rhs) const noexcept; // removed in C++2095 bool test(size_t pos) const; // constexpr since C++2396 bool all() const noexcept; // constexpr since C++2397 bool any() const noexcept; // constexpr since C++2398 bool none() const noexcept; // constexpr since C++2399 bitset<N> operator<<(size_t pos) const noexcept; // constexpr since C++23100 bitset<N> operator>>(size_t pos) const noexcept; // constexpr since C++23101};102 103// 23.3.5.3 bitset operators:104template <size_t N>105bitset<N> operator&(const bitset<N>&, const bitset<N>&) noexcept; // constexpr since C++23106 107template <size_t N>108bitset<N> operator|(const bitset<N>&, const bitset<N>&) noexcept; // constexpr since C++23109 110template <size_t N>111bitset<N> operator^(const bitset<N>&, const bitset<N>&) noexcept; // constexpr since C++23112 113template <class charT, class traits, size_t N>114basic_istream<charT, traits>&115operator>>(basic_istream<charT, traits>& is, bitset<N>& x);116 117template <class charT, class traits, size_t N>118basic_ostream<charT, traits>&119operator<<(basic_ostream<charT, traits>& os, const bitset<N>& x);120 121template <size_t N> struct hash<std::bitset<N>>;122 123} // std124 125*/126 127// clang-format on128 129#include <__cxx03/__algorithm/count.h>130#include <__cxx03/__algorithm/fill.h>131#include <__cxx03/__algorithm/find.h>132#include <__cxx03/__bit_reference>133#include <__cxx03/__config>134#include <__cxx03/__functional/hash.h>135#include <__cxx03/__functional/unary_function.h>136#include <__cxx03/__type_traits/is_char_like_type.h>137#include <__cxx03/climits>138#include <__cxx03/cstddef>139#include <__cxx03/stdexcept>140#include <__cxx03/string_view>141#include <__cxx03/version>142 143// standard-mandated includes144 145// [bitset.syn]146#include <__cxx03/iosfwd>147#include <__cxx03/string>148 149#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)150# pragma GCC system_header151#endif152 153_LIBCPP_PUSH_MACROS154#include <__cxx03/__undef_macros>155 156_LIBCPP_BEGIN_NAMESPACE_STD157 158template <size_t _N_words, size_t _Size>159class __bitset;160 161template <size_t _N_words, size_t _Size>162struct __has_storage_type<__bitset<_N_words, _Size> > {163 static const bool value = true;164};165 166template <size_t _N_words, size_t _Size>167class __bitset {168public:169 typedef ptrdiff_t difference_type;170 typedef size_t size_type;171 typedef size_type __storage_type;172 173protected:174 typedef __bitset __self;175 typedef __storage_type* __storage_pointer;176 typedef const __storage_type* __const_storage_pointer;177 static const unsigned __bits_per_word = static_cast<unsigned>(sizeof(__storage_type) * CHAR_BIT);178 179 friend class __bit_reference<__bitset>;180 friend class __bit_const_reference<__bitset>;181 friend class __bit_iterator<__bitset, false>;182 friend class __bit_iterator<__bitset, true>;183 friend struct __bit_array<__bitset>;184 185 __storage_type __first_[_N_words];186 187 typedef __bit_reference<__bitset> reference;188 typedef __bit_const_reference<__bitset> __const_reference;189 typedef __bit_iterator<__bitset, false> __iterator;190 typedef __bit_iterator<__bitset, true> __const_iterator;191 192 _LIBCPP_HIDE_FROM_ABI __bitset() _NOEXCEPT;193 _LIBCPP_HIDE_FROM_ABI explicit __bitset(unsigned long long __v) _NOEXCEPT;194 195 _LIBCPP_HIDE_FROM_ABI reference __make_ref(size_t __pos) _NOEXCEPT {196 return reference(__first_ + __pos / __bits_per_word, __storage_type(1) << __pos % __bits_per_word);197 }198 _LIBCPP_HIDE_FROM_ABI __const_reference __make_ref(size_t __pos) const _NOEXCEPT {199 return __const_reference(__first_ + __pos / __bits_per_word, __storage_type(1) << __pos % __bits_per_word);200 }201 _LIBCPP_HIDE_FROM_ABI __iterator __make_iter(size_t __pos) _NOEXCEPT {202 return __iterator(__first_ + __pos / __bits_per_word, __pos % __bits_per_word);203 }204 _LIBCPP_HIDE_FROM_ABI __const_iterator __make_iter(size_t __pos) const _NOEXCEPT {205 return __const_iterator(__first_ + __pos / __bits_per_word, __pos % __bits_per_word);206 }207 208 _LIBCPP_HIDE_FROM_ABI void operator&=(const __bitset& __v) _NOEXCEPT;209 _LIBCPP_HIDE_FROM_ABI void operator|=(const __bitset& __v) _NOEXCEPT;210 _LIBCPP_HIDE_FROM_ABI void operator^=(const __bitset& __v) _NOEXCEPT;211 212 _LIBCPP_HIDE_FROM_ABI void flip() _NOEXCEPT;213 _LIBCPP_HIDE_FROM_ABI unsigned long to_ulong() const {214 return to_ulong(integral_constant < bool, _Size< sizeof(unsigned long) * CHAR_BIT>());215 }216 _LIBCPP_HIDE_FROM_ABI unsigned long long to_ullong() const {217 return to_ullong(integral_constant < bool, _Size< sizeof(unsigned long long) * CHAR_BIT>());218 }219 220 _LIBCPP_HIDE_FROM_ABI bool all() const _NOEXCEPT;221 _LIBCPP_HIDE_FROM_ABI bool any() const _NOEXCEPT;222 _LIBCPP_HIDE_FROM_ABI size_t __hash_code() const _NOEXCEPT;223 224private:225 void __init(unsigned long long __v, false_type) _NOEXCEPT;226 _LIBCPP_HIDE_FROM_ABI void __init(unsigned long long __v, true_type) _NOEXCEPT;227 _LIBCPP_HIDE_FROM_ABI unsigned long to_ulong(false_type) const;228 _LIBCPP_HIDE_FROM_ABI unsigned long to_ulong(true_type) const;229 _LIBCPP_HIDE_FROM_ABI unsigned long long to_ullong(false_type) const;230 _LIBCPP_HIDE_FROM_ABI unsigned long long to_ullong(true_type) const;231 _LIBCPP_HIDE_FROM_ABI unsigned long long to_ullong(true_type, false_type) const;232 _LIBCPP_HIDE_FROM_ABI unsigned long long to_ullong(true_type, true_type) const;233};234 235template <size_t _N_words, size_t _Size>236inline __bitset<_N_words, _Size>::__bitset() _NOEXCEPT {237 std::fill_n(__first_, _N_words, __storage_type(0));238}239 240template <size_t _N_words, size_t _Size>241void __bitset<_N_words, _Size>::__init(unsigned long long __v, false_type) _NOEXCEPT {242 __storage_type __t[sizeof(unsigned long long) / sizeof(__storage_type)];243 size_t __sz = _Size;244 for (size_t __i = 0; __i < sizeof(__t) / sizeof(__t[0]); ++__i, __v >>= __bits_per_word, __sz -= __bits_per_word)245 if (__sz < __bits_per_word)246 __t[__i] = static_cast<__storage_type>(__v) & (1ULL << __sz) - 1;247 else248 __t[__i] = static_cast<__storage_type>(__v);249 250 std::copy(__t, __t + sizeof(__t) / sizeof(__t[0]), __first_);251 std::fill(252 __first_ + sizeof(__t) / sizeof(__t[0]), __first_ + sizeof(__first_) / sizeof(__first_[0]), __storage_type(0));253}254 255template <size_t _N_words, size_t _Size>256inline _LIBCPP_HIDE_FROM_ABI void __bitset<_N_words, _Size>::__init(unsigned long long __v, true_type) _NOEXCEPT {257 __first_[0] = __v;258 if (_Size < __bits_per_word)259 __first_[0] &= (1ULL << _Size) - 1;260 261 std::fill(__first_ + 1, __first_ + sizeof(__first_) / sizeof(__first_[0]), __storage_type(0));262}263 264template <size_t _N_words, size_t _Size>265inline __bitset<_N_words, _Size>::__bitset(unsigned long long __v) _NOEXCEPT {266 __init(__v, integral_constant<bool, sizeof(unsigned long long) == sizeof(__storage_type)>());267}268 269template <size_t _N_words, size_t _Size>270inline _LIBCPP_HIDE_FROM_ABI void __bitset<_N_words, _Size>::operator&=(const __bitset& __v) _NOEXCEPT {271 for (size_type __i = 0; __i < _N_words; ++__i)272 __first_[__i] &= __v.__first_[__i];273}274 275template <size_t _N_words, size_t _Size>276inline _LIBCPP_HIDE_FROM_ABI void __bitset<_N_words, _Size>::operator|=(const __bitset& __v) _NOEXCEPT {277 for (size_type __i = 0; __i < _N_words; ++__i)278 __first_[__i] |= __v.__first_[__i];279}280 281template <size_t _N_words, size_t _Size>282inline _LIBCPP_HIDE_FROM_ABI void __bitset<_N_words, _Size>::operator^=(const __bitset& __v) _NOEXCEPT {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>288_LIBCPP_HIDE_FROM_ABI void __bitset<_N_words, _Size>::flip() _NOEXCEPT {289 // do middle whole words290 size_type __n = _Size;291 __storage_pointer __p = __first_;292 for (; __n >= __bits_per_word; ++__p, __n -= __bits_per_word)293 *__p = ~*__p;294 // do last partial word295 if (__n > 0) {296 __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);297 __storage_type __b = *__p & __m;298 *__p &= ~__m;299 *__p |= ~__b & __m;300 }301}302 303template <size_t _N_words, size_t _Size>304_LIBCPP_HIDE_FROM_ABI unsigned long __bitset<_N_words, _Size>::to_ulong(false_type) const {305 __const_iterator __e = __make_iter(_Size);306 __const_iterator __i = std::find(__make_iter(sizeof(unsigned long) * CHAR_BIT), __e, true);307 if (__i != __e)308 __throw_overflow_error("bitset to_ulong overflow error");309 310 return __first_[0];311}312 313template <size_t _N_words, size_t _Size>314inline _LIBCPP_HIDE_FROM_ABI unsigned long __bitset<_N_words, _Size>::to_ulong(true_type) const {315 return __first_[0];316}317 318template <size_t _N_words, size_t _Size>319_LIBCPP_HIDE_FROM_ABI unsigned long long __bitset<_N_words, _Size>::to_ullong(false_type) const {320 __const_iterator __e = __make_iter(_Size);321 __const_iterator __i = std::find(__make_iter(sizeof(unsigned long long) * CHAR_BIT), __e, true);322 if (__i != __e)323 __throw_overflow_error("bitset to_ullong overflow error");324 325 return to_ullong(true_type());326}327 328template <size_t _N_words, size_t _Size>329inline _LIBCPP_HIDE_FROM_ABI unsigned long long __bitset<_N_words, _Size>::to_ullong(true_type) const {330 return to_ullong(true_type(), integral_constant<bool, sizeof(__storage_type) < sizeof(unsigned long long)>());331}332 333template <size_t _N_words, size_t _Size>334inline _LIBCPP_HIDE_FROM_ABI unsigned long long __bitset<_N_words, _Size>::to_ullong(true_type, false_type) const {335 return __first_[0];336}337 338template <size_t _N_words, size_t _Size>339_LIBCPP_HIDE_FROM_ABI unsigned long long __bitset<_N_words, _Size>::to_ullong(true_type, true_type) const {340 unsigned long long __r = __first_[0];341 _LIBCPP_DIAGNOSTIC_PUSH342 _LIBCPP_GCC_DIAGNOSTIC_IGNORED("-Wshift-count-overflow")343 for (size_t __i = 1; __i < sizeof(unsigned long long) / sizeof(__storage_type); ++__i)344 __r |= static_cast<unsigned long long>(__first_[__i]) << (sizeof(__storage_type) * CHAR_BIT);345 _LIBCPP_DIAGNOSTIC_POP346 return __r;347}348 349template <size_t _N_words, size_t _Size>350_LIBCPP_HIDE_FROM_ABI bool __bitset<_N_words, _Size>::all() const _NOEXCEPT {351 // do middle whole words352 size_type __n = _Size;353 __const_storage_pointer __p = __first_;354 for (; __n >= __bits_per_word; ++__p, __n -= __bits_per_word)355 if (~*__p)356 return false;357 // do last partial word358 if (__n > 0) {359 __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);360 if (~*__p & __m)361 return false;362 }363 return true;364}365 366template <size_t _N_words, size_t _Size>367_LIBCPP_HIDE_FROM_ABI bool __bitset<_N_words, _Size>::any() const _NOEXCEPT {368 // do middle whole words369 size_type __n = _Size;370 __const_storage_pointer __p = __first_;371 for (; __n >= __bits_per_word; ++__p, __n -= __bits_per_word)372 if (*__p)373 return true;374 // do last partial word375 if (__n > 0) {376 __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);377 if (*__p & __m)378 return true;379 }380 return false;381}382 383template <size_t _N_words, size_t _Size>384inline size_t __bitset<_N_words, _Size>::__hash_code() const _NOEXCEPT {385 size_t __h = 0;386 for (size_type __i = 0; __i < _N_words; ++__i)387 __h ^= __first_[__i];388 return __h;389}390 391template <size_t _Size>392class __bitset<1, _Size> {393public:394 typedef ptrdiff_t difference_type;395 typedef size_t size_type;396 typedef size_type __storage_type;397 398protected:399 typedef __bitset __self;400 typedef __storage_type* __storage_pointer;401 typedef const __storage_type* __const_storage_pointer;402 static const unsigned __bits_per_word = static_cast<unsigned>(sizeof(__storage_type) * CHAR_BIT);403 404 friend class __bit_reference<__bitset>;405 friend class __bit_const_reference<__bitset>;406 friend class __bit_iterator<__bitset, false>;407 friend class __bit_iterator<__bitset, true>;408 friend struct __bit_array<__bitset>;409 410 __storage_type __first_;411 412 typedef __bit_reference<__bitset> reference;413 typedef __bit_const_reference<__bitset> __const_reference;414 typedef __bit_iterator<__bitset, false> __iterator;415 typedef __bit_iterator<__bitset, true> __const_iterator;416 417 _LIBCPP_HIDE_FROM_ABI __bitset() _NOEXCEPT;418 _LIBCPP_HIDE_FROM_ABI explicit __bitset(unsigned long long __v) _NOEXCEPT;419 420 _LIBCPP_HIDE_FROM_ABI reference __make_ref(size_t __pos) _NOEXCEPT {421 return reference(&__first_, __storage_type(1) << __pos);422 }423 _LIBCPP_HIDE_FROM_ABI __const_reference __make_ref(size_t __pos) const _NOEXCEPT {424 return __const_reference(&__first_, __storage_type(1) << __pos);425 }426 _LIBCPP_HIDE_FROM_ABI __iterator __make_iter(size_t __pos) _NOEXCEPT {427 return __iterator(&__first_ + __pos / __bits_per_word, __pos % __bits_per_word);428 }429 _LIBCPP_HIDE_FROM_ABI __const_iterator __make_iter(size_t __pos) const _NOEXCEPT {430 return __const_iterator(&__first_ + __pos / __bits_per_word, __pos % __bits_per_word);431 }432 433 _LIBCPP_HIDE_FROM_ABI void operator&=(const __bitset& __v) _NOEXCEPT;434 _LIBCPP_HIDE_FROM_ABI void operator|=(const __bitset& __v) _NOEXCEPT;435 _LIBCPP_HIDE_FROM_ABI void operator^=(const __bitset& __v) _NOEXCEPT;436 437 _LIBCPP_HIDE_FROM_ABI void flip() _NOEXCEPT;438 439 _LIBCPP_HIDE_FROM_ABI unsigned long to_ulong() const;440 _LIBCPP_HIDE_FROM_ABI unsigned long long to_ullong() const;441 442 _LIBCPP_HIDE_FROM_ABI bool all() const _NOEXCEPT;443 _LIBCPP_HIDE_FROM_ABI bool any() const _NOEXCEPT;444 445 _LIBCPP_HIDE_FROM_ABI size_t __hash_code() const _NOEXCEPT;446};447 448template <size_t _Size>449inline __bitset<1, _Size>::__bitset() _NOEXCEPT : __first_(0) {}450 451template <size_t _Size>452inline __bitset<1, _Size>::__bitset(unsigned long long __v) _NOEXCEPT453 : __first_(_Size == __bits_per_word ? static_cast<__storage_type>(__v)454 : static_cast<__storage_type>(__v) & ((__storage_type(1) << _Size) - 1)) {}455 456template <size_t _Size>457inline _LIBCPP_HIDE_FROM_ABI void __bitset<1, _Size>::operator&=(const __bitset& __v) _NOEXCEPT {458 __first_ &= __v.__first_;459}460 461template <size_t _Size>462inline _LIBCPP_HIDE_FROM_ABI void __bitset<1, _Size>::operator|=(const __bitset& __v) _NOEXCEPT {463 __first_ |= __v.__first_;464}465 466template <size_t _Size>467inline _LIBCPP_HIDE_FROM_ABI void __bitset<1, _Size>::operator^=(const __bitset& __v) _NOEXCEPT {468 __first_ ^= __v.__first_;469}470 471template <size_t _Size>472inline _LIBCPP_HIDE_FROM_ABI void __bitset<1, _Size>::flip() _NOEXCEPT {473 __storage_type __m = ~__storage_type(0) >> (__bits_per_word - _Size);474 __first_ = ~__first_;475 __first_ &= __m;476}477 478template <size_t _Size>479inline unsigned long __bitset<1, _Size>::to_ulong() const {480 return __first_;481}482 483template <size_t _Size>484inline _LIBCPP_HIDE_FROM_ABI unsigned long long __bitset<1, _Size>::to_ullong() const {485 return __first_;486}487 488template <size_t _Size>489inline _LIBCPP_HIDE_FROM_ABI bool __bitset<1, _Size>::all() const _NOEXCEPT {490 __storage_type __m = ~__storage_type(0) >> (__bits_per_word - _Size);491 return !(~__first_ & __m);492}493 494template <size_t _Size>495inline _LIBCPP_HIDE_FROM_ABI bool __bitset<1, _Size>::any() const _NOEXCEPT {496 __storage_type __m = ~__storage_type(0) >> (__bits_per_word - _Size);497 return __first_ & __m;498}499 500template <size_t _Size>501inline size_t __bitset<1, _Size>::__hash_code() const _NOEXCEPT {502 return __first_;503}504 505template <>506class __bitset<0, 0> {507public:508 typedef ptrdiff_t difference_type;509 typedef size_t size_type;510 typedef size_type __storage_type;511 512protected:513 typedef __bitset __self;514 typedef __storage_type* __storage_pointer;515 typedef const __storage_type* __const_storage_pointer;516 static const unsigned __bits_per_word = static_cast<unsigned>(sizeof(__storage_type) * CHAR_BIT);517 518 friend class __bit_reference<__bitset>;519 friend class __bit_const_reference<__bitset>;520 friend class __bit_iterator<__bitset, false>;521 friend class __bit_iterator<__bitset, true>;522 friend struct __bit_array<__bitset>;523 524 typedef __bit_reference<__bitset> reference;525 typedef __bit_const_reference<__bitset> __const_reference;526 typedef __bit_iterator<__bitset, false> __iterator;527 typedef __bit_iterator<__bitset, true> __const_iterator;528 529 _LIBCPP_HIDE_FROM_ABI __bitset() _NOEXCEPT;530 _LIBCPP_HIDE_FROM_ABI explicit __bitset(unsigned long long) _NOEXCEPT;531 532 _LIBCPP_HIDE_FROM_ABI reference __make_ref(size_t) _NOEXCEPT { return reference(nullptr, 1); }533 _LIBCPP_HIDE_FROM_ABI __const_reference __make_ref(size_t) const _NOEXCEPT { return __const_reference(nullptr, 1); }534 _LIBCPP_HIDE_FROM_ABI __iterator __make_iter(size_t) _NOEXCEPT { return __iterator(nullptr, 0); }535 _LIBCPP_HIDE_FROM_ABI __const_iterator __make_iter(size_t) const _NOEXCEPT { return __const_iterator(nullptr, 0); }536 537 _LIBCPP_HIDE_FROM_ABI void operator&=(const __bitset&) _NOEXCEPT {}538 _LIBCPP_HIDE_FROM_ABI void operator|=(const __bitset&) _NOEXCEPT {}539 _LIBCPP_HIDE_FROM_ABI void operator^=(const __bitset&) _NOEXCEPT {}540 541 _LIBCPP_HIDE_FROM_ABI void flip() _NOEXCEPT {}542 543 _LIBCPP_HIDE_FROM_ABI unsigned long to_ulong() const { return 0; }544 _LIBCPP_HIDE_FROM_ABI unsigned long long to_ullong() const { return 0; }545 546 _LIBCPP_HIDE_FROM_ABI bool all() const _NOEXCEPT { return true; }547 _LIBCPP_HIDE_FROM_ABI bool any() const _NOEXCEPT { return false; }548 549 _LIBCPP_HIDE_FROM_ABI size_t __hash_code() const _NOEXCEPT { return 0; }550};551 552inline __bitset<0, 0>::__bitset() _NOEXCEPT {}553 554inline __bitset<0, 0>::__bitset(unsigned long long) _NOEXCEPT {}555 556template <size_t _Size>557class _LIBCPP_TEMPLATE_VIS bitset;558template <size_t _Size>559struct hash<bitset<_Size> >;560 561template <size_t _Size>562class _LIBCPP_TEMPLATE_VIS bitset563 : private __bitset<_Size == 0 ? 0 : (_Size - 1) / (sizeof(size_t) * CHAR_BIT) + 1, _Size> {564public:565 static const unsigned __n_words = _Size == 0 ? 0 : (_Size - 1) / (sizeof(size_t) * CHAR_BIT) + 1;566 typedef __bitset<__n_words, _Size> __base;567 568public:569 typedef typename __base::reference reference;570 typedef typename __base::__const_reference __const_reference;571 572 // 23.3.5.1 constructors:573 _LIBCPP_HIDE_FROM_ABI bitset() _NOEXCEPT {}574 _LIBCPP_HIDE_FROM_ABI bitset(unsigned long long __v) _NOEXCEPT : __base(__v) {}575 template <class _CharT, __enable_if_t<_IsCharLikeType<_CharT>::value, int> = 0>576 _LIBCPP_HIDE_FROM_ABI explicit bitset(577 const _CharT* __str,578 typename basic_string<_CharT>::size_type __n = basic_string<_CharT>::npos,579 _CharT __zero = _CharT('0'),580 _CharT __one = _CharT('1')) {581 size_t __rlen = std::min(__n, char_traits<_CharT>::length(__str));582 __init_from_string_view(basic_string_view<_CharT>(__str, __rlen), __zero, __one);583 }584 585 template <class _CharT, class _Traits, class _Allocator>586 _LIBCPP_HIDE_FROM_ABI explicit bitset(587 const basic_string<_CharT, _Traits, _Allocator>& __str,588 typename basic_string<_CharT, _Traits, _Allocator>::size_type __pos = 0,589 typename basic_string<_CharT, _Traits, _Allocator>::size_type __n =590 basic_string<_CharT, _Traits, _Allocator>::npos,591 _CharT __zero = _CharT('0'),592 _CharT __one = _CharT('1')) {593 if (__pos > __str.size())594 std::__throw_out_of_range("bitset string pos out of range");595 596 size_t __rlen = std::min(__n, __str.size() - __pos);597 __init_from_string_view(basic_string_view<_CharT, _Traits>(__str.data() + __pos, __rlen), __zero, __one);598 }599 600 // 23.3.5.2 bitset operations:601 _LIBCPP_HIDE_FROM_ABI bitset& operator&=(const bitset& __rhs) _NOEXCEPT;602 _LIBCPP_HIDE_FROM_ABI bitset& operator|=(const bitset& __rhs) _NOEXCEPT;603 _LIBCPP_HIDE_FROM_ABI bitset& operator^=(const bitset& __rhs) _NOEXCEPT;604 _LIBCPP_HIDE_FROM_ABI bitset& operator<<=(size_t __pos) _NOEXCEPT;605 _LIBCPP_HIDE_FROM_ABI bitset& operator>>=(size_t __pos) _NOEXCEPT;606 _LIBCPP_HIDE_FROM_ABI bitset& set() _NOEXCEPT;607 _LIBCPP_HIDE_FROM_ABI bitset& set(size_t __pos, bool __val = true);608 _LIBCPP_HIDE_FROM_ABI bitset& reset() _NOEXCEPT;609 _LIBCPP_HIDE_FROM_ABI bitset& reset(size_t __pos);610 _LIBCPP_HIDE_FROM_ABI bitset operator~() const _NOEXCEPT;611 _LIBCPP_HIDE_FROM_ABI bitset& flip() _NOEXCEPT;612 _LIBCPP_HIDE_FROM_ABI bitset& flip(size_t __pos);613 614 // element access:615#ifdef _LIBCPP_ABI_BITSET_VECTOR_BOOL_CONST_SUBSCRIPT_RETURN_BOOL616 _LIBCPP_HIDE_FROM_ABI bool operator[](size_t __p) const { return __base::__make_ref(__p); }617#else618 _LIBCPP_HIDE_FROM_ABI __const_reference operator[](size_t __p) const { return __base::__make_ref(__p); }619#endif620 _LIBCPP_HIDE_FROM_ABI reference operator[](size_t __p) { return __base::__make_ref(__p); }621 _LIBCPP_HIDE_FROM_ABI unsigned long to_ulong() const;622 _LIBCPP_HIDE_FROM_ABI unsigned long long to_ullong() const;623 template <class _CharT, class _Traits, class _Allocator>624 _LIBCPP_HIDE_FROM_ABI basic_string<_CharT, _Traits, _Allocator>625 to_string(_CharT __zero = _CharT('0'), _CharT __one = _CharT('1')) const;626 template <class _CharT, class _Traits>627 _LIBCPP_HIDE_FROM_ABI basic_string<_CharT, _Traits, allocator<_CharT> >628 to_string(_CharT __zero = _CharT('0'), _CharT __one = _CharT('1')) const;629 template <class _CharT>630 _LIBCPP_HIDE_FROM_ABI basic_string<_CharT, char_traits<_CharT>, allocator<_CharT> >631 to_string(_CharT __zero = _CharT('0'), _CharT __one = _CharT('1')) const;632 _LIBCPP_HIDE_FROM_ABI basic_string<char, char_traits<char>, allocator<char> >633 to_string(char __zero = '0', char __one = '1') const;634 _LIBCPP_HIDE_FROM_ABI size_t count() const _NOEXCEPT;635 _LIBCPP_HIDE_FROM_ABI size_t size() const _NOEXCEPT { return _Size; }636 _LIBCPP_HIDE_FROM_ABI bool operator==(const bitset& __rhs) const _NOEXCEPT;637 _LIBCPP_HIDE_FROM_ABI bool operator!=(const bitset& __rhs) const _NOEXCEPT;638 _LIBCPP_HIDE_FROM_ABI bool test(size_t __pos) const;639 _LIBCPP_HIDE_FROM_ABI bool all() const _NOEXCEPT;640 _LIBCPP_HIDE_FROM_ABI bool any() const _NOEXCEPT;641 _LIBCPP_HIDE_FROM_ABI bool none() const _NOEXCEPT { return !any(); }642 _LIBCPP_HIDE_FROM_ABI bitset operator<<(size_t __pos) const _NOEXCEPT;643 _LIBCPP_HIDE_FROM_ABI bitset operator>>(size_t __pos) const _NOEXCEPT;644 645private:646 template <class _CharT, class _Traits>647 _LIBCPP_HIDE_FROM_ABI void648 __init_from_string_view(basic_string_view<_CharT, _Traits> __str, _CharT __zero, _CharT __one) {649 for (size_t __i = 0; __i < __str.size(); ++__i)650 if (!_Traits::eq(__str[__i], __zero) && !_Traits::eq(__str[__i], __one))651 std::__throw_invalid_argument("bitset string ctor has invalid argument");652 653 size_t __mp = std::min(__str.size(), _Size);654 size_t __i = 0;655 for (; __i < __mp; ++__i) {656 _CharT __c = __str[__mp - 1 - __i];657 (*this)[__i] = _Traits::eq(__c, __one);658 }659 std::fill(__base::__make_iter(__i), __base::__make_iter(_Size), false);660 }661 662 _LIBCPP_HIDE_FROM_ABI size_t __hash_code() const _NOEXCEPT { return __base::__hash_code(); }663 664 friend struct hash<bitset>;665};666 667template <size_t _Size>668inline _LIBCPP_HIDE_FROM_ABI bitset<_Size>& bitset<_Size>::operator&=(const bitset& __rhs) _NOEXCEPT {669 __base::operator&=(__rhs);670 return *this;671}672 673template <size_t _Size>674inline _LIBCPP_HIDE_FROM_ABI bitset<_Size>& bitset<_Size>::operator|=(const bitset& __rhs) _NOEXCEPT {675 __base::operator|=(__rhs);676 return *this;677}678 679template <size_t _Size>680inline _LIBCPP_HIDE_FROM_ABI bitset<_Size>& bitset<_Size>::operator^=(const bitset& __rhs) _NOEXCEPT {681 __base::operator^=(__rhs);682 return *this;683}684 685template <size_t _Size>686_LIBCPP_HIDE_FROM_ABI bitset<_Size>& bitset<_Size>::operator<<=(size_t __pos) _NOEXCEPT {687 __pos = std::min(__pos, _Size);688 std::copy_backward(__base::__make_iter(0), __base::__make_iter(_Size - __pos), __base::__make_iter(_Size));689 std::fill_n(__base::__make_iter(0), __pos, false);690 return *this;691}692 693template <size_t _Size>694_LIBCPP_HIDE_FROM_ABI bitset<_Size>& bitset<_Size>::operator>>=(size_t __pos) _NOEXCEPT {695 __pos = std::min(__pos, _Size);696 std::copy(__base::__make_iter(__pos), __base::__make_iter(_Size), __base::__make_iter(0));697 std::fill_n(__base::__make_iter(_Size - __pos), __pos, false);698 return *this;699}700 701template <size_t _Size>702inline _LIBCPP_HIDE_FROM_ABI bitset<_Size>& bitset<_Size>::set() _NOEXCEPT {703 std::fill_n(__base::__make_iter(0), _Size, true);704 return *this;705}706 707template <size_t _Size>708_LIBCPP_HIDE_FROM_ABI bitset<_Size>& bitset<_Size>::set(size_t __pos, bool __val) {709 if (__pos >= _Size)710 __throw_out_of_range("bitset set argument out of range");711 712 (*this)[__pos] = __val;713 return *this;714}715 716template <size_t _Size>717inline _LIBCPP_HIDE_FROM_ABI bitset<_Size>& bitset<_Size>::reset() _NOEXCEPT {718 std::fill_n(__base::__make_iter(0), _Size, false);719 return *this;720}721 722template <size_t _Size>723_LIBCPP_HIDE_FROM_ABI bitset<_Size>& bitset<_Size>::reset(size_t __pos) {724 if (__pos >= _Size)725 __throw_out_of_range("bitset reset argument out of range");726 727 (*this)[__pos] = false;728 return *this;729}730 731template <size_t _Size>732inline _LIBCPP_HIDE_FROM_ABI bitset<_Size> bitset<_Size>::operator~() const _NOEXCEPT {733 bitset __x(*this);734 __x.flip();735 return __x;736}737 738template <size_t _Size>739inline _LIBCPP_HIDE_FROM_ABI bitset<_Size>& bitset<_Size>::flip() _NOEXCEPT {740 __base::flip();741 return *this;742}743 744template <size_t _Size>745_LIBCPP_HIDE_FROM_ABI bitset<_Size>& bitset<_Size>::flip(size_t __pos) {746 if (__pos >= _Size)747 __throw_out_of_range("bitset flip argument out of range");748 749 reference __r = __base::__make_ref(__pos);750 __r = ~__r;751 return *this;752}753 754template <size_t _Size>755inline _LIBCPP_HIDE_FROM_ABI unsigned long bitset<_Size>::to_ulong() const {756 return __base::to_ulong();757}758 759template <size_t _Size>760inline _LIBCPP_HIDE_FROM_ABI unsigned long long bitset<_Size>::to_ullong() const {761 return __base::to_ullong();762}763 764template <size_t _Size>765template <class _CharT, class _Traits, class _Allocator>766_LIBCPP_HIDE_FROM_ABI basic_string<_CharT, _Traits, _Allocator>767bitset<_Size>::to_string(_CharT __zero, _CharT __one) const {768 basic_string<_CharT, _Traits, _Allocator> __r(_Size, __zero);769 for (size_t __i = 0; __i != _Size; ++__i) {770 if ((*this)[__i])771 __r[_Size - 1 - __i] = __one;772 }773 return __r;774}775 776template <size_t _Size>777template <class _CharT, class _Traits>778inline _LIBCPP_HIDE_FROM_ABI basic_string<_CharT, _Traits, allocator<_CharT> >779bitset<_Size>::to_string(_CharT __zero, _CharT __one) const {780 return to_string<_CharT, _Traits, allocator<_CharT> >(__zero, __one);781}782 783template <size_t _Size>784template <class _CharT>785inline _LIBCPP_HIDE_FROM_ABI basic_string<_CharT, char_traits<_CharT>, allocator<_CharT> >786bitset<_Size>::to_string(_CharT __zero, _CharT __one) const {787 return to_string<_CharT, char_traits<_CharT>, allocator<_CharT> >(__zero, __one);788}789 790template <size_t _Size>791inline _LIBCPP_HIDE_FROM_ABI basic_string<char, char_traits<char>, allocator<char> >792bitset<_Size>::to_string(char __zero, char __one) const {793 return to_string<char, char_traits<char>, allocator<char> >(__zero, __one);794}795 796template <size_t _Size>797inline _LIBCPP_HIDE_FROM_ABI size_t bitset<_Size>::count() const _NOEXCEPT {798 return static_cast<size_t>(std::count(__base::__make_iter(0), __base::__make_iter(_Size), true));799}800 801template <size_t _Size>802inline _LIBCPP_HIDE_FROM_ABI bool bitset<_Size>::operator==(const bitset& __rhs) const _NOEXCEPT {803 return std::equal(__base::__make_iter(0), __base::__make_iter(_Size), __rhs.__make_iter(0));804}805 806template <size_t _Size>807inline _LIBCPP_HIDE_FROM_ABI bool bitset<_Size>::operator!=(const bitset& __rhs) const _NOEXCEPT {808 return !(*this == __rhs);809}810 811template <size_t _Size>812_LIBCPP_HIDE_FROM_ABI bool bitset<_Size>::test(size_t __pos) const {813 if (__pos >= _Size)814 __throw_out_of_range("bitset test argument out of range");815 816 return (*this)[__pos];817}818 819template <size_t _Size>820inline _LIBCPP_HIDE_FROM_ABI bool bitset<_Size>::all() const _NOEXCEPT {821 return __base::all();822}823 824template <size_t _Size>825inline _LIBCPP_HIDE_FROM_ABI bool bitset<_Size>::any() const _NOEXCEPT {826 return __base::any();827}828 829template <size_t _Size>830inline _LIBCPP_HIDE_FROM_ABI bitset<_Size> bitset<_Size>::operator<<(size_t __pos) const _NOEXCEPT {831 bitset __r = *this;832 __r <<= __pos;833 return __r;834}835 836template <size_t _Size>837inline _LIBCPP_HIDE_FROM_ABI bitset<_Size> bitset<_Size>::operator>>(size_t __pos) const _NOEXCEPT {838 bitset __r = *this;839 __r >>= __pos;840 return __r;841}842 843template <size_t _Size>844inline _LIBCPP_HIDE_FROM_ABI bitset<_Size> operator&(const bitset<_Size>& __x, const bitset<_Size>& __y) _NOEXCEPT {845 bitset<_Size> __r = __x;846 __r &= __y;847 return __r;848}849 850template <size_t _Size>851inline _LIBCPP_HIDE_FROM_ABI bitset<_Size> operator|(const bitset<_Size>& __x, const bitset<_Size>& __y) _NOEXCEPT {852 bitset<_Size> __r = __x;853 __r |= __y;854 return __r;855}856 857template <size_t _Size>858inline _LIBCPP_HIDE_FROM_ABI bitset<_Size> operator^(const bitset<_Size>& __x, const bitset<_Size>& __y) _NOEXCEPT {859 bitset<_Size> __r = __x;860 __r ^= __y;861 return __r;862}863 864template <size_t _Size>865struct _LIBCPP_TEMPLATE_VIS hash<bitset<_Size> > : public __unary_function<bitset<_Size>, size_t> {866 _LIBCPP_HIDE_FROM_ABI size_t operator()(const bitset<_Size>& __bs) const _NOEXCEPT { return __bs.__hash_code(); }867};868 869template <class _CharT, class _Traits, size_t _Size>870_LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&871operator>>(basic_istream<_CharT, _Traits>& __is, bitset<_Size>& __x);872 873template <class _CharT, class _Traits, size_t _Size>874_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&875operator<<(basic_ostream<_CharT, _Traits>& __os, const bitset<_Size>& __x);876 877_LIBCPP_END_NAMESPACE_STD878 879_LIBCPP_POP_MACROS880 881#if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES)882# include <__cxx03/cstdlib>883# include <__cxx03/type_traits>884#endif885 886#endif // _LIBCPP___CXX03_BITSET887