2 //===----------------------------------------------------------------------===//
4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5 // See https://llvm.org/LICENSE.txt for license information.
6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
8 //===----------------------------------------------------------------------===//
10 #ifndef _LIBCPP_BITSET
11 #define _LIBCPP_BITSET
31 ~reference() noexcept;
32 reference& operator=(bool x) noexcept; // for b[i] = x;
33 reference& operator=(const reference&) noexcept; // for b[i] = b[j];
34 bool operator~() const noexcept; // flips the bit
35 operator bool() const noexcept; // for x = b[i];
36 reference& flip() noexcept; // for b[i].flip();
39 // 23.3.5.1 constructors:
40 constexpr bitset() noexcept;
41 constexpr bitset(unsigned long long val) noexcept;
42 template <class charT>
43 explicit bitset(const charT* str,
44 typename basic_string<charT>::size_type n = basic_string<charT>::npos,
45 charT zero = charT('0'), charT one = charT('1')); // constexpr since C++23
46 template<class charT, class traits, class Allocator>
47 explicit bitset(const basic_string<charT,traits,Allocator>& str,
48 typename basic_string<charT,traits,Allocator>::size_type pos = 0,
49 typename basic_string<charT,traits,Allocator>::size_type n =
50 basic_string<charT,traits,Allocator>::npos,
51 charT zero = charT('0'), charT one = charT('1')); // constexpr since C++23
53 // 23.3.5.2 bitset operations:
54 bitset& operator&=(const bitset& rhs) noexcept; // constexpr since C++23
55 bitset& operator|=(const bitset& rhs) noexcept; // constexpr since C++23
56 bitset& operator^=(const bitset& rhs) noexcept; // constexpr since C++23
57 bitset& operator<<=(size_t pos) noexcept; // constexpr since C++23
58 bitset& operator>>=(size_t pos) noexcept; // constexpr since C++23
59 bitset& set() noexcept; // constexpr since C++23
60 bitset& set(size_t pos, bool val = true); // constexpr since C++23
61 bitset& reset() noexcept; // constexpr since C++23
62 bitset& reset(size_t pos); // constexpr since C++23
63 bitset operator~() const noexcept; // constexpr since C++23
64 bitset& flip() noexcept; // constexpr since C++23
65 bitset& flip(size_t pos); // constexpr since C++23
68 constexpr bool operator[](size_t pos) const;
69 reference operator[](size_t pos); // constexpr since C++23
70 unsigned long to_ulong() const; // constexpr since C++23
71 unsigned long long to_ullong() const; // constexpr since C++23
72 template <class charT, class traits, class Allocator> // constexpr since C++23
73 basic_string<charT, traits, Allocator> to_string(charT zero = charT('0'), charT one = charT('1')) const;
74 template <class charT, class traits> // constexpr since C++23
75 basic_string<charT, traits, allocator<charT> > to_string(charT zero = charT('0'), charT one = charT('1')) const;
76 template <class charT> // constexpr since C++23
77 basic_string<charT, char_traits<charT>, allocator<charT> > to_string(charT zero = charT('0'), charT one = charT('1')) const;
78 basic_string<char, char_traits<char>, allocator<char> > to_string(char zero = '0', char one = '1') const; // constexpr since C++23
79 size_t count() const noexcept; // constexpr since C++23
80 constexpr size_t size() const noexcept; // constexpr since C++23
81 bool operator==(const bitset& rhs) const noexcept; // constexpr since C++23
82 bool operator!=(const bitset& rhs) const noexcept; // removed in C++20
83 bool test(size_t pos) const; // constexpr since C++23
84 bool all() const noexcept; // constexpr since C++23
85 bool any() const noexcept; // constexpr since C++23
86 bool none() const noexcept; // constexpr since C++23
87 bitset<N> operator<<(size_t pos) const noexcept; // constexpr since C++23
88 bitset<N> operator>>(size_t pos) const noexcept; // constexpr since C++23
91 // 23.3.5.3 bitset operators:
93 bitset<N> operator&(const bitset<N>&, const bitset<N>&) noexcept; // constexpr since C++23
96 bitset<N> operator|(const bitset<N>&, const bitset<N>&) noexcept; // constexpr since C++23
99 bitset<N> operator^(const bitset<N>&, const bitset<N>&) noexcept; // constexpr since C++23
101 template <class charT, class traits, size_t N>
102 basic_istream<charT, traits>&
103 operator>>(basic_istream<charT, traits>& is, bitset<N>& x);
105 template <class charT, class traits, size_t N>
106 basic_ostream<charT, traits>&
107 operator<<(basic_ostream<charT, traits>& os, const bitset<N>& x);
109 template <size_t N> struct hash<std::bitset<N>>;
115 #include <__algorithm/fill.h>
116 #include <__assert> // all public C++ headers provide the assertion handler
117 #include <__bit_reference>
119 #include <__functional/hash.h>
120 #include <__functional/unary_function.h>
121 #include <__type_traits/is_char_like_type.h>
125 #include <string_view>
128 // standard-mandated includes
134 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
135 # pragma GCC system_header
139 #include <__undef_macros>
142 _LIBCPP_BEGIN_NAMESPACE_STD
144 template <size_t _N_words, size_t _Size>
147 template <size_t _N_words, size_t _Size>
148 struct __has_storage_type<__bitset<_N_words, _Size> >
150 static const bool value = true;
153 template <size_t _N_words, size_t _Size>
157 typedef ptrdiff_t difference_type;
158 typedef size_t size_type;
159 typedef size_type __storage_type;
161 typedef __bitset __self;
162 typedef __storage_type* __storage_pointer;
163 typedef const __storage_type* __const_storage_pointer;
164 static const unsigned __bits_per_word = static_cast<unsigned>(sizeof(__storage_type) * CHAR_BIT);
166 friend class __bit_reference<__bitset>;
167 friend class __bit_const_reference<__bitset>;
168 friend class __bit_iterator<__bitset, false>;
169 friend class __bit_iterator<__bitset, true>;
170 friend struct __bit_array<__bitset>;
172 __storage_type __first_[_N_words];
174 typedef __bit_reference<__bitset> reference;
175 typedef __bit_const_reference<__bitset> const_reference;
176 typedef __bit_iterator<__bitset, false> iterator;
177 typedef __bit_iterator<__bitset, true> const_iterator;
179 _LIBCPP_INLINE_VISIBILITY
180 _LIBCPP_CONSTEXPR __bitset() _NOEXCEPT;
181 _LIBCPP_INLINE_VISIBILITY
182 explicit _LIBCPP_CONSTEXPR __bitset(unsigned long long __v) _NOEXCEPT;
184 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23 reference __make_ref(size_t __pos) _NOEXCEPT
185 {return reference(__first_ + __pos / __bits_per_word, __storage_type(1) << __pos % __bits_per_word);}
186 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR const_reference __make_ref(size_t __pos) const _NOEXCEPT
187 {return const_reference(__first_ + __pos / __bits_per_word, __storage_type(1) << __pos % __bits_per_word);}
188 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23 iterator __make_iter(size_t __pos) _NOEXCEPT
189 {return iterator(__first_ + __pos / __bits_per_word, __pos % __bits_per_word);}
190 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23 const_iterator __make_iter(size_t __pos) const _NOEXCEPT
191 {return const_iterator(__first_ + __pos / __bits_per_word, __pos % __bits_per_word);}
193 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23
194 void operator&=(const __bitset& __v) _NOEXCEPT;
195 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23
196 void operator|=(const __bitset& __v) _NOEXCEPT;
197 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23
198 void operator^=(const __bitset& __v) _NOEXCEPT;
200 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 void flip() _NOEXCEPT;
201 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long to_ulong() const
202 {return to_ulong(integral_constant<bool, _Size < sizeof(unsigned long) * CHAR_BIT>());}
203 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long long to_ullong() const
204 {return to_ullong(integral_constant<bool, _Size < sizeof(unsigned long long) * CHAR_BIT>());}
206 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bool all() const _NOEXCEPT;
207 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bool any() const _NOEXCEPT;
208 _LIBCPP_INLINE_VISIBILITY
209 size_t __hash_code() const _NOEXCEPT;
211 #ifdef _LIBCPP_CXX03_LANG
212 void __init(unsigned long long __v, false_type) _NOEXCEPT;
213 _LIBCPP_INLINE_VISIBILITY
214 void __init(unsigned long long __v, true_type) _NOEXCEPT;
215 #endif // _LIBCPP_CXX03_LANG
216 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23
217 unsigned long to_ulong(false_type) const;
218 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23
219 unsigned long to_ulong(true_type) const;
220 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23
221 unsigned long long to_ullong(false_type) const;
222 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23
223 unsigned long long to_ullong(true_type) const;
224 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23
225 unsigned long long to_ullong(true_type, false_type) const;
226 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23
227 unsigned long long to_ullong(true_type, true_type) const;
230 template <size_t _N_words, size_t _Size>
233 __bitset<_N_words, _Size>::__bitset() _NOEXCEPT
234 #ifndef _LIBCPP_CXX03_LANG
238 #ifdef _LIBCPP_CXX03_LANG
239 _VSTD::fill_n(__first_, _N_words, __storage_type(0));
243 #ifdef _LIBCPP_CXX03_LANG
245 template <size_t _N_words, size_t _Size>
247 __bitset<_N_words, _Size>::__init(unsigned long long __v, false_type) _NOEXCEPT
249 __storage_type __t[sizeof(unsigned long long) / sizeof(__storage_type)];
251 for (size_t __i = 0; __i < sizeof(__t)/sizeof(__t[0]); ++__i, __v >>= __bits_per_word, __sz -= __bits_per_word )
252 if ( __sz < __bits_per_word)
253 __t[__i] = static_cast<__storage_type>(__v) & ( 1ULL << __sz ) - 1;
255 __t[__i] = static_cast<__storage_type>(__v);
257 _VSTD::copy(__t, __t + sizeof(__t)/sizeof(__t[0]), __first_);
258 _VSTD::fill(__first_ + sizeof(__t)/sizeof(__t[0]), __first_ + sizeof(__first_)/sizeof(__first_[0]),
262 template <size_t _N_words, size_t _Size>
263 inline _LIBCPP_INLINE_VISIBILITY
265 __bitset<_N_words, _Size>::__init(unsigned long long __v, true_type) _NOEXCEPT
268 if (_Size < __bits_per_word)
269 __first_[0] &= ( 1ULL << _Size ) - 1;
271 _VSTD::fill(__first_ + 1, __first_ + sizeof(__first_)/sizeof(__first_[0]), __storage_type(0));
274 #endif // _LIBCPP_CXX03_LANG
276 template <size_t _N_words, size_t _Size>
279 __bitset<_N_words, _Size>::__bitset(unsigned long long __v) _NOEXCEPT
280 #ifndef _LIBCPP_CXX03_LANG
281 #if __SIZEOF_SIZE_T__ == 8
283 #elif __SIZEOF_SIZE_T__ == 4
284 : __first_{static_cast<__storage_type>(__v),
285 _Size >= 2 * __bits_per_word ? static_cast<__storage_type>(__v >> __bits_per_word)
286 : static_cast<__storage_type>((__v >> __bits_per_word) & (__storage_type(1) << (_Size - __bits_per_word)) - 1)}
288 #error This constructor has not been ported to this platform
292 #ifdef _LIBCPP_CXX03_LANG
293 __init(__v, integral_constant<bool, sizeof(unsigned long long) == sizeof(__storage_type)>());
297 template <size_t _N_words, size_t _Size>
299 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 void
300 __bitset<_N_words, _Size>::operator&=(const __bitset& __v) _NOEXCEPT
302 for (size_type __i = 0; __i < _N_words; ++__i)
303 __first_[__i] &= __v.__first_[__i];
306 template <size_t _N_words, size_t _Size>
308 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 void
309 __bitset<_N_words, _Size>::operator|=(const __bitset& __v) _NOEXCEPT
311 for (size_type __i = 0; __i < _N_words; ++__i)
312 __first_[__i] |= __v.__first_[__i];
315 template <size_t _N_words, size_t _Size>
317 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 void
318 __bitset<_N_words, _Size>::operator^=(const __bitset& __v) _NOEXCEPT
320 for (size_type __i = 0; __i < _N_words; ++__i)
321 __first_[__i] ^= __v.__first_[__i];
324 template <size_t _N_words, size_t _Size>
325 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 void
326 __bitset<_N_words, _Size>::flip() _NOEXCEPT
328 // do middle whole words
329 size_type __n = _Size;
330 __storage_pointer __p = __first_;
331 for (; __n >= __bits_per_word; ++__p, __n -= __bits_per_word)
333 // do last partial word
336 __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
337 __storage_type __b = *__p & __m;
343 template <size_t _N_words, size_t _Size>
344 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long
345 __bitset<_N_words, _Size>::to_ulong(false_type) const
347 const_iterator __e = __make_iter(_Size);
348 const_iterator __i = _VSTD::find(__make_iter(sizeof(unsigned long) * CHAR_BIT), __e, true);
350 __throw_overflow_error("bitset to_ulong overflow error");
355 template <size_t _N_words, size_t _Size>
357 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long
358 __bitset<_N_words, _Size>::to_ulong(true_type) const
363 template <size_t _N_words, size_t _Size>
364 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long long
365 __bitset<_N_words, _Size>::to_ullong(false_type) const
367 const_iterator __e = __make_iter(_Size);
368 const_iterator __i = _VSTD::find(__make_iter(sizeof(unsigned long long) * CHAR_BIT), __e, true);
370 __throw_overflow_error("bitset to_ullong overflow error");
372 return to_ullong(true_type());
375 template <size_t _N_words, size_t _Size>
377 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long long
378 __bitset<_N_words, _Size>::to_ullong(true_type) const
380 return to_ullong(true_type(), integral_constant<bool, sizeof(__storage_type) < sizeof(unsigned long long)>());
383 template <size_t _N_words, size_t _Size>
385 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long long
386 __bitset<_N_words, _Size>::to_ullong(true_type, false_type) const
391 template <size_t _N_words, size_t _Size>
392 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long long
393 __bitset<_N_words, _Size>::to_ullong(true_type, true_type) const
395 unsigned long long __r = __first_[0];
396 for (size_t __i = 1; __i < sizeof(unsigned long long) / sizeof(__storage_type); ++__i)
397 __r |= static_cast<unsigned long long>(__first_[__i]) << (sizeof(__storage_type) * CHAR_BIT);
401 template <size_t _N_words, size_t _Size>
402 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bool
403 __bitset<_N_words, _Size>::all() const _NOEXCEPT
405 // do middle whole words
406 size_type __n = _Size;
407 __const_storage_pointer __p = __first_;
408 for (; __n >= __bits_per_word; ++__p, __n -= __bits_per_word)
411 // do last partial word
414 __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
421 template <size_t _N_words, size_t _Size>
422 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bool
423 __bitset<_N_words, _Size>::any() const _NOEXCEPT
425 // do middle whole words
426 size_type __n = _Size;
427 __const_storage_pointer __p = __first_;
428 for (; __n >= __bits_per_word; ++__p, __n -= __bits_per_word)
431 // do last partial word
434 __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
441 template <size_t _N_words, size_t _Size>
444 __bitset<_N_words, _Size>::__hash_code() const _NOEXCEPT
447 for (size_type __i = 0; __i < _N_words; ++__i)
448 __h ^= __first_[__i];
452 template <size_t _Size>
453 class __bitset<1, _Size>
456 typedef ptrdiff_t difference_type;
457 typedef size_t size_type;
458 typedef size_type __storage_type;
460 typedef __bitset __self;
461 typedef __storage_type* __storage_pointer;
462 typedef const __storage_type* __const_storage_pointer;
463 static const unsigned __bits_per_word = static_cast<unsigned>(sizeof(__storage_type) * CHAR_BIT);
465 friend class __bit_reference<__bitset>;
466 friend class __bit_const_reference<__bitset>;
467 friend class __bit_iterator<__bitset, false>;
468 friend class __bit_iterator<__bitset, true>;
469 friend struct __bit_array<__bitset>;
471 __storage_type __first_;
473 typedef __bit_reference<__bitset> reference;
474 typedef __bit_const_reference<__bitset> const_reference;
475 typedef __bit_iterator<__bitset, false> iterator;
476 typedef __bit_iterator<__bitset, true> const_iterator;
478 _LIBCPP_INLINE_VISIBILITY
479 _LIBCPP_CONSTEXPR __bitset() _NOEXCEPT;
480 _LIBCPP_INLINE_VISIBILITY
481 explicit _LIBCPP_CONSTEXPR __bitset(unsigned long long __v) _NOEXCEPT;
483 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23 reference __make_ref(size_t __pos) _NOEXCEPT
484 {return reference(&__first_, __storage_type(1) << __pos);}
485 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR const_reference __make_ref(size_t __pos) const _NOEXCEPT
486 {return const_reference(&__first_, __storage_type(1) << __pos);}
487 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23 iterator __make_iter(size_t __pos) _NOEXCEPT
488 {return iterator(&__first_ + __pos / __bits_per_word, __pos % __bits_per_word);}
489 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23 const_iterator __make_iter(size_t __pos) const _NOEXCEPT
490 {return const_iterator(&__first_ + __pos / __bits_per_word, __pos % __bits_per_word);}
492 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23
493 void operator&=(const __bitset& __v) _NOEXCEPT;
494 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23
495 void operator|=(const __bitset& __v) _NOEXCEPT;
496 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23
497 void operator^=(const __bitset& __v) _NOEXCEPT;
499 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23
500 void flip() _NOEXCEPT;
502 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23
503 unsigned long to_ulong() const;
504 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23
505 unsigned long long to_ullong() const;
507 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23
508 bool all() const _NOEXCEPT;
509 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23
510 bool any() const _NOEXCEPT;
512 _LIBCPP_INLINE_VISIBILITY
513 size_t __hash_code() const _NOEXCEPT;
516 template <size_t _Size>
519 __bitset<1, _Size>::__bitset() _NOEXCEPT
524 template <size_t _Size>
527 __bitset<1, _Size>::__bitset(unsigned long long __v) _NOEXCEPT
529 _Size == __bits_per_word ? static_cast<__storage_type>(__v)
530 : static_cast<__storage_type>(__v) & ((__storage_type(1) << _Size) - 1)
535 template <size_t _Size>
537 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 void
538 __bitset<1, _Size>::operator&=(const __bitset& __v) _NOEXCEPT
540 __first_ &= __v.__first_;
543 template <size_t _Size>
545 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 void
546 __bitset<1, _Size>::operator|=(const __bitset& __v) _NOEXCEPT
548 __first_ |= __v.__first_;
551 template <size_t _Size>
553 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 void
554 __bitset<1, _Size>::operator^=(const __bitset& __v) _NOEXCEPT
556 __first_ ^= __v.__first_;
559 template <size_t _Size>
561 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 void
562 __bitset<1, _Size>::flip() _NOEXCEPT
564 __storage_type __m = ~__storage_type(0) >> (__bits_per_word - _Size);
565 __first_ = ~__first_;
569 template <size_t _Size>
571 _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long
572 __bitset<1, _Size>::to_ulong() const
577 template <size_t _Size>
579 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long long
580 __bitset<1, _Size>::to_ullong() const
585 template <size_t _Size>
587 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bool
588 __bitset<1, _Size>::all() const _NOEXCEPT
590 __storage_type __m = ~__storage_type(0) >> (__bits_per_word - _Size);
591 return !(~__first_ & __m);
594 template <size_t _Size>
596 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bool
597 __bitset<1, _Size>::any() const _NOEXCEPT
599 __storage_type __m = ~__storage_type(0) >> (__bits_per_word - _Size);
600 return __first_ & __m;
603 template <size_t _Size>
606 __bitset<1, _Size>::__hash_code() const _NOEXCEPT
615 typedef ptrdiff_t difference_type;
616 typedef size_t size_type;
617 typedef size_type __storage_type;
619 typedef __bitset __self;
620 typedef __storage_type* __storage_pointer;
621 typedef const __storage_type* __const_storage_pointer;
622 static const unsigned __bits_per_word = static_cast<unsigned>(sizeof(__storage_type) * CHAR_BIT);
624 friend class __bit_reference<__bitset>;
625 friend class __bit_const_reference<__bitset>;
626 friend class __bit_iterator<__bitset, false>;
627 friend class __bit_iterator<__bitset, true>;
628 friend struct __bit_array<__bitset>;
630 typedef __bit_reference<__bitset> reference;
631 typedef __bit_const_reference<__bitset> const_reference;
632 typedef __bit_iterator<__bitset, false> iterator;
633 typedef __bit_iterator<__bitset, true> const_iterator;
635 _LIBCPP_INLINE_VISIBILITY
636 _LIBCPP_CONSTEXPR __bitset() _NOEXCEPT;
637 _LIBCPP_INLINE_VISIBILITY
638 explicit _LIBCPP_CONSTEXPR __bitset(unsigned long long) _NOEXCEPT;
640 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23 reference __make_ref(size_t) _NOEXCEPT
641 {return reference(nullptr, 1);}
642 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR const_reference __make_ref(size_t) const _NOEXCEPT
643 {return const_reference(nullptr, 1);}
644 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23 iterator __make_iter(size_t) _NOEXCEPT
645 {return iterator(nullptr, 0);}
646 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23 const_iterator __make_iter(size_t) const _NOEXCEPT
647 {return const_iterator(nullptr, 0);}
649 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23 void operator&=(const __bitset&) _NOEXCEPT {}
650 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23 void operator|=(const __bitset&) _NOEXCEPT {}
651 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23 void operator^=(const __bitset&) _NOEXCEPT {}
653 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23 void flip() _NOEXCEPT {}
655 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long to_ulong() const {return 0;}
656 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long long to_ullong() const {return 0;}
658 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23 bool all() const _NOEXCEPT {return true;}
659 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23 bool any() const _NOEXCEPT {return false;}
661 _LIBCPP_INLINE_VISIBILITY size_t __hash_code() const _NOEXCEPT {return 0;}
666 __bitset<0, 0>::__bitset() _NOEXCEPT
672 __bitset<0, 0>::__bitset(unsigned long long) _NOEXCEPT
676 template <size_t _Size> class _LIBCPP_TEMPLATE_VIS bitset;
677 template <size_t _Size> struct hash<bitset<_Size> >;
679 template <size_t _Size>
680 class _LIBCPP_TEMPLATE_VIS bitset
681 : private __bitset<_Size == 0 ? 0 : (_Size - 1) / (sizeof(size_t) * CHAR_BIT) + 1, _Size>
684 static const unsigned __n_words = _Size == 0 ? 0 : (_Size - 1) / (sizeof(size_t) * CHAR_BIT) + 1;
685 typedef __bitset<__n_words, _Size> base;
688 typedef typename base::reference reference;
689 typedef typename base::const_reference const_reference;
691 // 23.3.5.1 constructors:
692 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR bitset() _NOEXCEPT {}
693 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
694 bitset(unsigned long long __v) _NOEXCEPT : base(__v) {}
695 template <class _CharT, class = __enable_if_t<_IsCharLikeType<_CharT>::value> >
696 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 explicit bitset(
698 typename basic_string<_CharT>::size_type __n = basic_string<_CharT>::npos,
699 _CharT __zero = _CharT('0'),
700 _CharT __one = _CharT('1')) {
702 size_t __rlen = std::min(__n, char_traits<_CharT>::length(__str));
703 __init_from_string_view(basic_string_view<_CharT>(__str, __rlen), __zero, __one);
705 template <class _CharT, class _Traits, class _Allocator>
706 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 explicit bitset(
707 const basic_string<_CharT, _Traits, _Allocator>& __str,
708 typename basic_string<_CharT, _Traits, _Allocator>::size_type __pos = 0,
709 typename basic_string<_CharT, _Traits, _Allocator>::size_type __n =
710 basic_string<_CharT, _Traits, _Allocator>::npos,
711 _CharT __zero = _CharT('0'),
712 _CharT __one = _CharT('1')) {
713 if (__pos > __str.size())
714 std::__throw_out_of_range("bitset string pos out of range");
716 size_t __rlen = std::min(__n, __str.size() - __pos);
717 __init_from_string_view(basic_string_view<_CharT, _Traits>(__str.data() + __pos, __rlen), __zero, __one);
720 // 23.3.5.2 bitset operations:
721 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23
722 bitset& operator&=(const bitset& __rhs) _NOEXCEPT;
723 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23
724 bitset& operator|=(const bitset& __rhs) _NOEXCEPT;
725 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23
726 bitset& operator^=(const bitset& __rhs) _NOEXCEPT;
727 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23
728 bitset& operator<<=(size_t __pos) _NOEXCEPT;
729 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23
730 bitset& operator>>=(size_t __pos) _NOEXCEPT;
731 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23
732 bitset& set() _NOEXCEPT;
733 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23
734 bitset& set(size_t __pos, bool __val = true);
735 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23
736 bitset& reset() _NOEXCEPT;
737 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23
738 bitset& reset(size_t __pos);
739 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23
740 bitset operator~() const _NOEXCEPT;
741 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23
742 bitset& flip() _NOEXCEPT;
743 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23
744 bitset& flip(size_t __pos);
747 #ifdef _LIBCPP_ABI_BITSET_VECTOR_BOOL_CONST_SUBSCRIPT_RETURN_BOOL
748 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR bool operator[](size_t __p) const {return base::__make_ref(__p);}
750 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR const_reference operator[](size_t __p) const {return base::__make_ref(__p);}
752 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 reference operator[](size_t __p) {return base::__make_ref(__p);}
753 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23
754 unsigned long to_ulong() const;
755 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23
756 unsigned long long to_ullong() const;
757 template <class _CharT, class _Traits, class _Allocator>
758 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23
759 basic_string<_CharT, _Traits, _Allocator> to_string(_CharT __zero = _CharT('0'),
760 _CharT __one = _CharT('1')) const;
761 template <class _CharT, class _Traits>
762 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23
763 basic_string<_CharT, _Traits, allocator<_CharT> > to_string(_CharT __zero = _CharT('0'),
764 _CharT __one = _CharT('1')) const;
765 template <class _CharT>
766 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23
767 basic_string<_CharT, char_traits<_CharT>, allocator<_CharT> > to_string(_CharT __zero = _CharT('0'),
768 _CharT __one = _CharT('1')) const;
769 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23
770 basic_string<char, char_traits<char>, allocator<char> > to_string(char __zero = '0',
771 char __one = '1') const;
772 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23
773 size_t count() const _NOEXCEPT;
774 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR size_t size() const _NOEXCEPT {return _Size;}
775 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23
776 bool operator==(const bitset& __rhs) const _NOEXCEPT;
777 #if _LIBCPP_STD_VER <= 17
778 _LIBCPP_INLINE_VISIBILITY
779 bool operator!=(const bitset& __rhs) const _NOEXCEPT;
781 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23
782 bool test(size_t __pos) const;
783 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23
784 bool all() const _NOEXCEPT;
785 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23
786 bool any() const _NOEXCEPT;
787 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23 bool none() const _NOEXCEPT {return !any();}
788 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23
789 bitset operator<<(size_t __pos) const _NOEXCEPT;
790 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23
791 bitset operator>>(size_t __pos) const _NOEXCEPT;
794 template <class _CharT, class _Traits>
795 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 void
796 __init_from_string_view(basic_string_view<_CharT, _Traits> __str, _CharT __zero, _CharT __one) {
798 for (size_t __i = 0; __i < __str.size(); ++__i)
799 if (!_Traits::eq(__str[__i], __zero) && !_Traits::eq(__str[__i], __one))
800 std::__throw_invalid_argument("bitset string ctor has invalid argument");
802 size_t __mp = std::min(__str.size(), _Size);
804 for (; __i < __mp; ++__i) {
805 _CharT __c = __str[__mp - 1 - __i];
806 (*this)[__i] = _Traits::eq(__c, __one);
808 std::fill(base::__make_iter(__i), base::__make_iter(_Size), false);
811 _LIBCPP_INLINE_VISIBILITY
812 size_t __hash_code() const _NOEXCEPT {return base::__hash_code();}
814 friend struct hash<bitset>;
817 template <size_t _Size>
819 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23
821 bitset<_Size>::operator&=(const bitset& __rhs) _NOEXCEPT
823 base::operator&=(__rhs);
827 template <size_t _Size>
829 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23
831 bitset<_Size>::operator|=(const bitset& __rhs) _NOEXCEPT
833 base::operator|=(__rhs);
837 template <size_t _Size>
839 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23
841 bitset<_Size>::operator^=(const bitset& __rhs) _NOEXCEPT
843 base::operator^=(__rhs);
847 template <size_t _Size>
848 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23
850 bitset<_Size>::operator<<=(size_t __pos) _NOEXCEPT
852 __pos = _VSTD::min(__pos, _Size);
853 _VSTD::copy_backward(base::__make_iter(0), base::__make_iter(_Size - __pos), base::__make_iter(_Size));
854 _VSTD::fill_n(base::__make_iter(0), __pos, false);
858 template <size_t _Size>
859 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23
861 bitset<_Size>::operator>>=(size_t __pos) _NOEXCEPT
863 __pos = _VSTD::min(__pos, _Size);
864 _VSTD::copy(base::__make_iter(__pos), base::__make_iter(_Size), base::__make_iter(0));
865 _VSTD::fill_n(base::__make_iter(_Size - __pos), __pos, false);
869 template <size_t _Size>
871 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23
873 bitset<_Size>::set() _NOEXCEPT
875 _VSTD::fill_n(base::__make_iter(0), _Size, true);
879 template <size_t _Size>
880 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23
882 bitset<_Size>::set(size_t __pos, bool __val)
885 __throw_out_of_range("bitset set argument out of range");
887 (*this)[__pos] = __val;
891 template <size_t _Size>
893 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23
895 bitset<_Size>::reset() _NOEXCEPT
897 _VSTD::fill_n(base::__make_iter(0), _Size, false);
901 template <size_t _Size>
902 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23
904 bitset<_Size>::reset(size_t __pos)
907 __throw_out_of_range("bitset reset argument out of range");
909 (*this)[__pos] = false;
913 template <size_t _Size>
915 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23
917 bitset<_Size>::operator~() const _NOEXCEPT
924 template <size_t _Size>
926 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23
928 bitset<_Size>::flip() _NOEXCEPT
934 template <size_t _Size>
935 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23
937 bitset<_Size>::flip(size_t __pos)
940 __throw_out_of_range("bitset flip argument out of range");
942 reference __r = base::__make_ref(__pos);
947 template <size_t _Size>
949 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23
951 bitset<_Size>::to_ulong() const
953 return base::to_ulong();
956 template <size_t _Size>
958 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23
960 bitset<_Size>::to_ullong() const
962 return base::to_ullong();
965 template <size_t _Size>
966 template <class _CharT, class _Traits, class _Allocator>
967 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23
968 basic_string<_CharT, _Traits, _Allocator>
969 bitset<_Size>::to_string(_CharT __zero, _CharT __one) const
971 basic_string<_CharT, _Traits, _Allocator> __r(_Size, __zero);
972 for (size_t __i = 0; __i != _Size; ++__i)
975 __r[_Size - 1 - __i] = __one;
980 template <size_t _Size>
981 template <class _CharT, class _Traits>
983 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23
984 basic_string<_CharT, _Traits, allocator<_CharT> >
985 bitset<_Size>::to_string(_CharT __zero, _CharT __one) const
987 return to_string<_CharT, _Traits, allocator<_CharT> >(__zero, __one);
990 template <size_t _Size>
991 template <class _CharT>
993 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23
994 basic_string<_CharT, char_traits<_CharT>, allocator<_CharT> >
995 bitset<_Size>::to_string(_CharT __zero, _CharT __one) const
997 return to_string<_CharT, char_traits<_CharT>, allocator<_CharT> >(__zero, __one);
1000 template <size_t _Size>
1002 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23
1003 basic_string<char, char_traits<char>, allocator<char> >
1004 bitset<_Size>::to_string(char __zero, char __one) const
1006 return to_string<char, char_traits<char>, allocator<char> >(__zero, __one);
1009 template <size_t _Size>
1011 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23
1013 bitset<_Size>::count() const _NOEXCEPT
1015 return static_cast<size_t>(_VSTD::__count_bool_true(base::__make_iter(0), _Size));
1018 template <size_t _Size>
1020 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23
1022 bitset<_Size>::operator==(const bitset& __rhs) const _NOEXCEPT
1024 return _VSTD::equal(base::__make_iter(0), base::__make_iter(_Size), __rhs.__make_iter(0));
1027 #if _LIBCPP_STD_VER <= 17
1029 template <size_t _Size>
1031 _LIBCPP_HIDE_FROM_ABI
1033 bitset<_Size>::operator!=(const bitset& __rhs) const _NOEXCEPT
1035 return !(*this == __rhs);
1040 template <size_t _Size>
1041 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23
1043 bitset<_Size>::test(size_t __pos) const
1046 __throw_out_of_range("bitset test argument out of range");
1048 return (*this)[__pos];
1051 template <size_t _Size>
1053 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23
1055 bitset<_Size>::all() const _NOEXCEPT
1060 template <size_t _Size>
1062 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23
1064 bitset<_Size>::any() const _NOEXCEPT
1069 template <size_t _Size>
1071 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23
1073 bitset<_Size>::operator<<(size_t __pos) const _NOEXCEPT
1080 template <size_t _Size>
1082 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23
1084 bitset<_Size>::operator>>(size_t __pos) const _NOEXCEPT
1091 template <size_t _Size>
1092 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23
1094 operator&(const bitset<_Size>& __x, const bitset<_Size>& __y) _NOEXCEPT
1096 bitset<_Size> __r = __x;
1101 template <size_t _Size>
1102 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23
1104 operator|(const bitset<_Size>& __x, const bitset<_Size>& __y) _NOEXCEPT
1106 bitset<_Size> __r = __x;
1111 template <size_t _Size>
1112 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23
1114 operator^(const bitset<_Size>& __x, const bitset<_Size>& __y) _NOEXCEPT
1116 bitset<_Size> __r = __x;
1121 template <size_t _Size>
1122 struct _LIBCPP_TEMPLATE_VIS hash<bitset<_Size> >
1123 : public __unary_function<bitset<_Size>, size_t>
1125 _LIBCPP_INLINE_VISIBILITY
1126 size_t operator()(const bitset<_Size>& __bs) const _NOEXCEPT
1127 {return __bs.__hash_code();}
1130 template <class _CharT, class _Traits, size_t _Size>
1131 _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
1132 operator>>(basic_istream<_CharT, _Traits>& __is, bitset<_Size>& __x);
1134 template <class _CharT, class _Traits, size_t _Size>
1135 _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
1136 operator<<(basic_ostream<_CharT, _Traits>& __os, const bitset<_Size>& __x);
1138 _LIBCPP_END_NAMESPACE_STD
1142 #if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
1143 # include <concepts>
1145 # include <type_traits>
1148 #endif // _LIBCPP_BITSET