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 <__algorithm/find.h>
117 #include <__assert> // all public C++ headers provide the assertion handler
118 #include <__bit_reference>
120 #include <__functional/hash.h>
121 #include <__functional/unary_function.h>
122 #include <__type_traits/is_char_like_type.h>
126 #include <string_view>
129 // standard-mandated includes
135 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
136 # pragma GCC system_header
140 #include <__undef_macros>
143 _LIBCPP_BEGIN_NAMESPACE_STD
145 template <size_t _N_words, size_t _Size>
148 template <size_t _N_words, size_t _Size>
149 struct __has_storage_type<__bitset<_N_words, _Size> >
151 static const bool value = true;
154 template <size_t _N_words, size_t _Size>
158 typedef ptrdiff_t difference_type;
159 typedef size_t size_type;
160 typedef size_type __storage_type;
162 typedef __bitset __self;
163 typedef __storage_type* __storage_pointer;
164 typedef const __storage_type* __const_storage_pointer;
165 static const unsigned __bits_per_word = static_cast<unsigned>(sizeof(__storage_type) * CHAR_BIT);
167 friend class __bit_reference<__bitset>;
168 friend class __bit_const_reference<__bitset>;
169 friend class __bit_iterator<__bitset, false>;
170 friend class __bit_iterator<__bitset, true>;
171 friend struct __bit_array<__bitset>;
173 __storage_type __first_[_N_words];
175 typedef __bit_reference<__bitset> reference;
176 typedef __bit_const_reference<__bitset> const_reference;
177 typedef __bit_iterator<__bitset, false> iterator;
178 typedef __bit_iterator<__bitset, true> const_iterator;
180 _LIBCPP_INLINE_VISIBILITY
181 _LIBCPP_CONSTEXPR __bitset() _NOEXCEPT;
182 _LIBCPP_INLINE_VISIBILITY
183 explicit _LIBCPP_CONSTEXPR __bitset(unsigned long long __v) _NOEXCEPT;
185 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23 reference __make_ref(size_t __pos) _NOEXCEPT
186 {return reference(__first_ + __pos / __bits_per_word, __storage_type(1) << __pos % __bits_per_word);}
187 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR const_reference __make_ref(size_t __pos) const _NOEXCEPT
188 {return const_reference(__first_ + __pos / __bits_per_word, __storage_type(1) << __pos % __bits_per_word);}
189 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23 iterator __make_iter(size_t __pos) _NOEXCEPT
190 {return iterator(__first_ + __pos / __bits_per_word, __pos % __bits_per_word);}
191 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23 const_iterator __make_iter(size_t __pos) const _NOEXCEPT
192 {return const_iterator(__first_ + __pos / __bits_per_word, __pos % __bits_per_word);}
194 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23
195 void operator&=(const __bitset& __v) _NOEXCEPT;
196 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23
197 void operator|=(const __bitset& __v) _NOEXCEPT;
198 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23
199 void operator^=(const __bitset& __v) _NOEXCEPT;
201 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 void flip() _NOEXCEPT;
202 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long to_ulong() const
203 {return to_ulong(integral_constant<bool, _Size < sizeof(unsigned long) * CHAR_BIT>());}
204 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long long to_ullong() const
205 {return to_ullong(integral_constant<bool, _Size < sizeof(unsigned long long) * CHAR_BIT>());}
207 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bool all() const _NOEXCEPT;
208 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bool any() const _NOEXCEPT;
209 _LIBCPP_INLINE_VISIBILITY
210 size_t __hash_code() const _NOEXCEPT;
212 #ifdef _LIBCPP_CXX03_LANG
213 void __init(unsigned long long __v, false_type) _NOEXCEPT;
214 _LIBCPP_INLINE_VISIBILITY
215 void __init(unsigned long long __v, true_type) _NOEXCEPT;
216 #endif // _LIBCPP_CXX03_LANG
217 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23
218 unsigned long to_ulong(false_type) const;
219 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23
220 unsigned long to_ulong(true_type) const;
221 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23
222 unsigned long long to_ullong(false_type) const;
223 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23
224 unsigned long long to_ullong(true_type) const;
225 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23
226 unsigned long long to_ullong(true_type, false_type) const;
227 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23
228 unsigned long long to_ullong(true_type, true_type) const;
231 template <size_t _N_words, size_t _Size>
234 __bitset<_N_words, _Size>::__bitset() _NOEXCEPT
235 #ifndef _LIBCPP_CXX03_LANG
239 #ifdef _LIBCPP_CXX03_LANG
240 _VSTD::fill_n(__first_, _N_words, __storage_type(0));
244 #ifdef _LIBCPP_CXX03_LANG
246 template <size_t _N_words, size_t _Size>
248 __bitset<_N_words, _Size>::__init(unsigned long long __v, false_type) _NOEXCEPT
250 __storage_type __t[sizeof(unsigned long long) / sizeof(__storage_type)];
252 for (size_t __i = 0; __i < sizeof(__t)/sizeof(__t[0]); ++__i, __v >>= __bits_per_word, __sz -= __bits_per_word )
253 if ( __sz < __bits_per_word)
254 __t[__i] = static_cast<__storage_type>(__v) & ( 1ULL << __sz ) - 1;
256 __t[__i] = static_cast<__storage_type>(__v);
258 _VSTD::copy(__t, __t + sizeof(__t)/sizeof(__t[0]), __first_);
259 _VSTD::fill(__first_ + sizeof(__t)/sizeof(__t[0]), __first_ + sizeof(__first_)/sizeof(__first_[0]),
263 template <size_t _N_words, size_t _Size>
264 inline _LIBCPP_INLINE_VISIBILITY
266 __bitset<_N_words, _Size>::__init(unsigned long long __v, true_type) _NOEXCEPT
269 if (_Size < __bits_per_word)
270 __first_[0] &= ( 1ULL << _Size ) - 1;
272 _VSTD::fill(__first_ + 1, __first_ + sizeof(__first_)/sizeof(__first_[0]), __storage_type(0));
275 #endif // _LIBCPP_CXX03_LANG
277 template <size_t _N_words, size_t _Size>
280 __bitset<_N_words, _Size>::__bitset(unsigned long long __v) _NOEXCEPT
281 #ifndef _LIBCPP_CXX03_LANG
282 #if __SIZEOF_SIZE_T__ == 8
284 #elif __SIZEOF_SIZE_T__ == 4
285 : __first_{static_cast<__storage_type>(__v),
286 _Size >= 2 * __bits_per_word ? static_cast<__storage_type>(__v >> __bits_per_word)
287 : static_cast<__storage_type>((__v >> __bits_per_word) & (__storage_type(1) << (_Size - __bits_per_word)) - 1)}
289 #error This constructor has not been ported to this platform
293 #ifdef _LIBCPP_CXX03_LANG
294 __init(__v, integral_constant<bool, sizeof(unsigned long long) == sizeof(__storage_type)>());
298 template <size_t _N_words, size_t _Size>
300 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 void
301 __bitset<_N_words, _Size>::operator&=(const __bitset& __v) _NOEXCEPT
303 for (size_type __i = 0; __i < _N_words; ++__i)
304 __first_[__i] &= __v.__first_[__i];
307 template <size_t _N_words, size_t _Size>
309 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 void
310 __bitset<_N_words, _Size>::operator|=(const __bitset& __v) _NOEXCEPT
312 for (size_type __i = 0; __i < _N_words; ++__i)
313 __first_[__i] |= __v.__first_[__i];
316 template <size_t _N_words, size_t _Size>
318 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 void
319 __bitset<_N_words, _Size>::operator^=(const __bitset& __v) _NOEXCEPT
321 for (size_type __i = 0; __i < _N_words; ++__i)
322 __first_[__i] ^= __v.__first_[__i];
325 template <size_t _N_words, size_t _Size>
326 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 void
327 __bitset<_N_words, _Size>::flip() _NOEXCEPT
329 // do middle whole words
330 size_type __n = _Size;
331 __storage_pointer __p = __first_;
332 for (; __n >= __bits_per_word; ++__p, __n -= __bits_per_word)
334 // do last partial word
337 __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
338 __storage_type __b = *__p & __m;
344 template <size_t _N_words, size_t _Size>
345 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long
346 __bitset<_N_words, _Size>::to_ulong(false_type) const
348 const_iterator __e = __make_iter(_Size);
349 const_iterator __i = _VSTD::find(__make_iter(sizeof(unsigned long) * CHAR_BIT), __e, true);
351 __throw_overflow_error("bitset to_ulong overflow error");
356 template <size_t _N_words, size_t _Size>
358 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long
359 __bitset<_N_words, _Size>::to_ulong(true_type) const
364 template <size_t _N_words, size_t _Size>
365 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long long
366 __bitset<_N_words, _Size>::to_ullong(false_type) const
368 const_iterator __e = __make_iter(_Size);
369 const_iterator __i = _VSTD::find(__make_iter(sizeof(unsigned long long) * CHAR_BIT), __e, true);
371 __throw_overflow_error("bitset to_ullong overflow error");
373 return to_ullong(true_type());
376 template <size_t _N_words, size_t _Size>
378 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long long
379 __bitset<_N_words, _Size>::to_ullong(true_type) const
381 return to_ullong(true_type(), integral_constant<bool, sizeof(__storage_type) < sizeof(unsigned long long)>());
384 template <size_t _N_words, size_t _Size>
386 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long long
387 __bitset<_N_words, _Size>::to_ullong(true_type, false_type) const
392 template <size_t _N_words, size_t _Size>
393 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long long
394 __bitset<_N_words, _Size>::to_ullong(true_type, true_type) const
396 unsigned long long __r = __first_[0];
397 for (size_t __i = 1; __i < sizeof(unsigned long long) / sizeof(__storage_type); ++__i)
398 __r |= static_cast<unsigned long long>(__first_[__i]) << (sizeof(__storage_type) * CHAR_BIT);
402 template <size_t _N_words, size_t _Size>
403 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bool
404 __bitset<_N_words, _Size>::all() const _NOEXCEPT
406 // do middle whole words
407 size_type __n = _Size;
408 __const_storage_pointer __p = __first_;
409 for (; __n >= __bits_per_word; ++__p, __n -= __bits_per_word)
412 // do last partial word
415 __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
422 template <size_t _N_words, size_t _Size>
423 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bool
424 __bitset<_N_words, _Size>::any() const _NOEXCEPT
426 // do middle whole words
427 size_type __n = _Size;
428 __const_storage_pointer __p = __first_;
429 for (; __n >= __bits_per_word; ++__p, __n -= __bits_per_word)
432 // do last partial word
435 __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
442 template <size_t _N_words, size_t _Size>
445 __bitset<_N_words, _Size>::__hash_code() const _NOEXCEPT
448 for (size_type __i = 0; __i < _N_words; ++__i)
449 __h ^= __first_[__i];
453 template <size_t _Size>
454 class __bitset<1, _Size>
457 typedef ptrdiff_t difference_type;
458 typedef size_t size_type;
459 typedef size_type __storage_type;
461 typedef __bitset __self;
462 typedef __storage_type* __storage_pointer;
463 typedef const __storage_type* __const_storage_pointer;
464 static const unsigned __bits_per_word = static_cast<unsigned>(sizeof(__storage_type) * CHAR_BIT);
466 friend class __bit_reference<__bitset>;
467 friend class __bit_const_reference<__bitset>;
468 friend class __bit_iterator<__bitset, false>;
469 friend class __bit_iterator<__bitset, true>;
470 friend struct __bit_array<__bitset>;
472 __storage_type __first_;
474 typedef __bit_reference<__bitset> reference;
475 typedef __bit_const_reference<__bitset> const_reference;
476 typedef __bit_iterator<__bitset, false> iterator;
477 typedef __bit_iterator<__bitset, true> const_iterator;
479 _LIBCPP_INLINE_VISIBILITY
480 _LIBCPP_CONSTEXPR __bitset() _NOEXCEPT;
481 _LIBCPP_INLINE_VISIBILITY
482 explicit _LIBCPP_CONSTEXPR __bitset(unsigned long long __v) _NOEXCEPT;
484 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23 reference __make_ref(size_t __pos) _NOEXCEPT
485 {return reference(&__first_, __storage_type(1) << __pos);}
486 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR const_reference __make_ref(size_t __pos) const _NOEXCEPT
487 {return const_reference(&__first_, __storage_type(1) << __pos);}
488 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23 iterator __make_iter(size_t __pos) _NOEXCEPT
489 {return iterator(&__first_ + __pos / __bits_per_word, __pos % __bits_per_word);}
490 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23 const_iterator __make_iter(size_t __pos) const _NOEXCEPT
491 {return const_iterator(&__first_ + __pos / __bits_per_word, __pos % __bits_per_word);}
493 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23
494 void operator&=(const __bitset& __v) _NOEXCEPT;
495 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23
496 void operator|=(const __bitset& __v) _NOEXCEPT;
497 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23
498 void operator^=(const __bitset& __v) _NOEXCEPT;
500 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23
501 void flip() _NOEXCEPT;
503 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23
504 unsigned long to_ulong() const;
505 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23
506 unsigned long long to_ullong() const;
508 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23
509 bool all() const _NOEXCEPT;
510 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23
511 bool any() const _NOEXCEPT;
513 _LIBCPP_INLINE_VISIBILITY
514 size_t __hash_code() const _NOEXCEPT;
517 template <size_t _Size>
520 __bitset<1, _Size>::__bitset() _NOEXCEPT
525 template <size_t _Size>
528 __bitset<1, _Size>::__bitset(unsigned long long __v) _NOEXCEPT
530 _Size == __bits_per_word ? static_cast<__storage_type>(__v)
531 : static_cast<__storage_type>(__v) & ((__storage_type(1) << _Size) - 1)
536 template <size_t _Size>
538 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 void
539 __bitset<1, _Size>::operator&=(const __bitset& __v) _NOEXCEPT
541 __first_ &= __v.__first_;
544 template <size_t _Size>
546 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 void
547 __bitset<1, _Size>::operator|=(const __bitset& __v) _NOEXCEPT
549 __first_ |= __v.__first_;
552 template <size_t _Size>
554 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 void
555 __bitset<1, _Size>::operator^=(const __bitset& __v) _NOEXCEPT
557 __first_ ^= __v.__first_;
560 template <size_t _Size>
562 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 void
563 __bitset<1, _Size>::flip() _NOEXCEPT
565 __storage_type __m = ~__storage_type(0) >> (__bits_per_word - _Size);
566 __first_ = ~__first_;
570 template <size_t _Size>
572 _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long
573 __bitset<1, _Size>::to_ulong() const
578 template <size_t _Size>
580 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long long
581 __bitset<1, _Size>::to_ullong() const
586 template <size_t _Size>
588 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bool
589 __bitset<1, _Size>::all() const _NOEXCEPT
591 __storage_type __m = ~__storage_type(0) >> (__bits_per_word - _Size);
592 return !(~__first_ & __m);
595 template <size_t _Size>
597 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bool
598 __bitset<1, _Size>::any() const _NOEXCEPT
600 __storage_type __m = ~__storage_type(0) >> (__bits_per_word - _Size);
601 return __first_ & __m;
604 template <size_t _Size>
607 __bitset<1, _Size>::__hash_code() const _NOEXCEPT
616 typedef ptrdiff_t difference_type;
617 typedef size_t size_type;
618 typedef size_type __storage_type;
620 typedef __bitset __self;
621 typedef __storage_type* __storage_pointer;
622 typedef const __storage_type* __const_storage_pointer;
623 static const unsigned __bits_per_word = static_cast<unsigned>(sizeof(__storage_type) * CHAR_BIT);
625 friend class __bit_reference<__bitset>;
626 friend class __bit_const_reference<__bitset>;
627 friend class __bit_iterator<__bitset, false>;
628 friend class __bit_iterator<__bitset, true>;
629 friend struct __bit_array<__bitset>;
631 typedef __bit_reference<__bitset> reference;
632 typedef __bit_const_reference<__bitset> const_reference;
633 typedef __bit_iterator<__bitset, false> iterator;
634 typedef __bit_iterator<__bitset, true> const_iterator;
636 _LIBCPP_INLINE_VISIBILITY
637 _LIBCPP_CONSTEXPR __bitset() _NOEXCEPT;
638 _LIBCPP_INLINE_VISIBILITY
639 explicit _LIBCPP_CONSTEXPR __bitset(unsigned long long) _NOEXCEPT;
641 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23 reference __make_ref(size_t) _NOEXCEPT
642 {return reference(nullptr, 1);}
643 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR const_reference __make_ref(size_t) const _NOEXCEPT
644 {return const_reference(nullptr, 1);}
645 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23 iterator __make_iter(size_t) _NOEXCEPT
646 {return iterator(nullptr, 0);}
647 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23 const_iterator __make_iter(size_t) const _NOEXCEPT
648 {return const_iterator(nullptr, 0);}
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 {}
652 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23 void operator^=(const __bitset&) _NOEXCEPT {}
654 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23 void flip() _NOEXCEPT {}
656 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long to_ulong() const {return 0;}
657 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long long to_ullong() const {return 0;}
659 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23 bool all() const _NOEXCEPT {return true;}
660 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23 bool any() const _NOEXCEPT {return false;}
662 _LIBCPP_INLINE_VISIBILITY size_t __hash_code() const _NOEXCEPT {return 0;}
667 __bitset<0, 0>::__bitset() _NOEXCEPT
673 __bitset<0, 0>::__bitset(unsigned long long) _NOEXCEPT
677 template <size_t _Size> class _LIBCPP_TEMPLATE_VIS bitset;
678 template <size_t _Size> struct hash<bitset<_Size> >;
680 template <size_t _Size>
681 class _LIBCPP_TEMPLATE_VIS bitset
682 : private __bitset<_Size == 0 ? 0 : (_Size - 1) / (sizeof(size_t) * CHAR_BIT) + 1, _Size>
685 static const unsigned __n_words = _Size == 0 ? 0 : (_Size - 1) / (sizeof(size_t) * CHAR_BIT) + 1;
686 typedef __bitset<__n_words, _Size> base;
689 typedef typename base::reference reference;
690 typedef typename base::const_reference const_reference;
692 // 23.3.5.1 constructors:
693 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR bitset() _NOEXCEPT {}
694 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
695 bitset(unsigned long long __v) _NOEXCEPT : base(__v) {}
696 template <class _CharT, class = __enable_if_t<_IsCharLikeType<_CharT>::value> >
697 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 explicit bitset(
699 typename basic_string<_CharT>::size_type __n = basic_string<_CharT>::npos,
700 _CharT __zero = _CharT('0'),
701 _CharT __one = _CharT('1')) {
703 size_t __rlen = std::min(__n, char_traits<_CharT>::length(__str));
704 __init_from_string_view(basic_string_view<_CharT>(__str, __rlen), __zero, __one);
706 template <class _CharT, class _Traits, class _Allocator>
707 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 explicit bitset(
708 const basic_string<_CharT, _Traits, _Allocator>& __str,
709 typename basic_string<_CharT, _Traits, _Allocator>::size_type __pos = 0,
710 typename basic_string<_CharT, _Traits, _Allocator>::size_type __n =
711 basic_string<_CharT, _Traits, _Allocator>::npos,
712 _CharT __zero = _CharT('0'),
713 _CharT __one = _CharT('1')) {
714 if (__pos > __str.size())
715 std::__throw_out_of_range("bitset string pos out of range");
717 size_t __rlen = std::min(__n, __str.size() - __pos);
718 __init_from_string_view(basic_string_view<_CharT, _Traits>(__str.data() + __pos, __rlen), __zero, __one);
721 // 23.3.5.2 bitset operations:
722 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23
723 bitset& operator&=(const bitset& __rhs) _NOEXCEPT;
724 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23
725 bitset& operator|=(const bitset& __rhs) _NOEXCEPT;
726 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23
727 bitset& operator^=(const bitset& __rhs) _NOEXCEPT;
728 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23
729 bitset& operator<<=(size_t __pos) _NOEXCEPT;
730 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23
731 bitset& operator>>=(size_t __pos) _NOEXCEPT;
732 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23
733 bitset& set() _NOEXCEPT;
734 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23
735 bitset& set(size_t __pos, bool __val = true);
736 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23
737 bitset& reset() _NOEXCEPT;
738 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23
739 bitset& reset(size_t __pos);
740 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23
741 bitset operator~() const _NOEXCEPT;
742 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23
743 bitset& flip() _NOEXCEPT;
744 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23
745 bitset& flip(size_t __pos);
748 #ifdef _LIBCPP_ABI_BITSET_VECTOR_BOOL_CONST_SUBSCRIPT_RETURN_BOOL
749 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR bool operator[](size_t __p) const {return base::__make_ref(__p);}
751 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR const_reference operator[](size_t __p) const {return base::__make_ref(__p);}
753 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 reference operator[](size_t __p) {return base::__make_ref(__p);}
754 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23
755 unsigned long to_ulong() const;
756 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23
757 unsigned long long to_ullong() const;
758 template <class _CharT, class _Traits, class _Allocator>
759 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23
760 basic_string<_CharT, _Traits, _Allocator> to_string(_CharT __zero = _CharT('0'),
761 _CharT __one = _CharT('1')) const;
762 template <class _CharT, class _Traits>
763 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23
764 basic_string<_CharT, _Traits, allocator<_CharT> > to_string(_CharT __zero = _CharT('0'),
765 _CharT __one = _CharT('1')) const;
766 template <class _CharT>
767 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23
768 basic_string<_CharT, char_traits<_CharT>, allocator<_CharT> > to_string(_CharT __zero = _CharT('0'),
769 _CharT __one = _CharT('1')) const;
770 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23
771 basic_string<char, char_traits<char>, allocator<char> > to_string(char __zero = '0',
772 char __one = '1') const;
773 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23
774 size_t count() const _NOEXCEPT;
775 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR size_t size() const _NOEXCEPT {return _Size;}
776 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23
777 bool operator==(const bitset& __rhs) const _NOEXCEPT;
778 #if _LIBCPP_STD_VER <= 17
779 _LIBCPP_INLINE_VISIBILITY
780 bool operator!=(const bitset& __rhs) const _NOEXCEPT;
782 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23
783 bool test(size_t __pos) const;
784 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23
785 bool all() const _NOEXCEPT;
786 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23
787 bool any() const _NOEXCEPT;
788 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23 bool none() const _NOEXCEPT {return !any();}
789 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23
790 bitset operator<<(size_t __pos) const _NOEXCEPT;
791 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23
792 bitset operator>>(size_t __pos) const _NOEXCEPT;
795 template <class _CharT, class _Traits>
796 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 void
797 __init_from_string_view(basic_string_view<_CharT, _Traits> __str, _CharT __zero, _CharT __one) {
799 for (size_t __i = 0; __i < __str.size(); ++__i)
800 if (!_Traits::eq(__str[__i], __zero) && !_Traits::eq(__str[__i], __one))
801 std::__throw_invalid_argument("bitset string ctor has invalid argument");
803 size_t __mp = std::min(__str.size(), _Size);
805 for (; __i < __mp; ++__i) {
806 _CharT __c = __str[__mp - 1 - __i];
807 (*this)[__i] = _Traits::eq(__c, __one);
809 std::fill(base::__make_iter(__i), base::__make_iter(_Size), false);
812 _LIBCPP_INLINE_VISIBILITY
813 size_t __hash_code() const _NOEXCEPT {return base::__hash_code();}
815 friend struct hash<bitset>;
818 template <size_t _Size>
820 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23
822 bitset<_Size>::operator&=(const bitset& __rhs) _NOEXCEPT
824 base::operator&=(__rhs);
828 template <size_t _Size>
830 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23
832 bitset<_Size>::operator|=(const bitset& __rhs) _NOEXCEPT
834 base::operator|=(__rhs);
838 template <size_t _Size>
840 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23
842 bitset<_Size>::operator^=(const bitset& __rhs) _NOEXCEPT
844 base::operator^=(__rhs);
848 template <size_t _Size>
849 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23
851 bitset<_Size>::operator<<=(size_t __pos) _NOEXCEPT
853 __pos = _VSTD::min(__pos, _Size);
854 _VSTD::copy_backward(base::__make_iter(0), base::__make_iter(_Size - __pos), base::__make_iter(_Size));
855 _VSTD::fill_n(base::__make_iter(0), __pos, false);
859 template <size_t _Size>
860 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23
862 bitset<_Size>::operator>>=(size_t __pos) _NOEXCEPT
864 __pos = _VSTD::min(__pos, _Size);
865 _VSTD::copy(base::__make_iter(__pos), base::__make_iter(_Size), base::__make_iter(0));
866 _VSTD::fill_n(base::__make_iter(_Size - __pos), __pos, false);
870 template <size_t _Size>
872 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23
874 bitset<_Size>::set() _NOEXCEPT
876 _VSTD::fill_n(base::__make_iter(0), _Size, true);
880 template <size_t _Size>
881 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23
883 bitset<_Size>::set(size_t __pos, bool __val)
886 __throw_out_of_range("bitset set argument out of range");
888 (*this)[__pos] = __val;
892 template <size_t _Size>
894 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23
896 bitset<_Size>::reset() _NOEXCEPT
898 _VSTD::fill_n(base::__make_iter(0), _Size, false);
902 template <size_t _Size>
903 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23
905 bitset<_Size>::reset(size_t __pos)
908 __throw_out_of_range("bitset reset argument out of range");
910 (*this)[__pos] = false;
914 template <size_t _Size>
916 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23
918 bitset<_Size>::operator~() const _NOEXCEPT
925 template <size_t _Size>
927 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23
929 bitset<_Size>::flip() _NOEXCEPT
935 template <size_t _Size>
936 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23
938 bitset<_Size>::flip(size_t __pos)
941 __throw_out_of_range("bitset flip argument out of range");
943 reference __r = base::__make_ref(__pos);
948 template <size_t _Size>
950 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23
952 bitset<_Size>::to_ulong() const
954 return base::to_ulong();
957 template <size_t _Size>
959 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23
961 bitset<_Size>::to_ullong() const
963 return base::to_ullong();
966 template <size_t _Size>
967 template <class _CharT, class _Traits, class _Allocator>
968 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23
969 basic_string<_CharT, _Traits, _Allocator>
970 bitset<_Size>::to_string(_CharT __zero, _CharT __one) const
972 basic_string<_CharT, _Traits, _Allocator> __r(_Size, __zero);
973 for (size_t __i = 0; __i != _Size; ++__i)
976 __r[_Size - 1 - __i] = __one;
981 template <size_t _Size>
982 template <class _CharT, class _Traits>
984 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23
985 basic_string<_CharT, _Traits, allocator<_CharT> >
986 bitset<_Size>::to_string(_CharT __zero, _CharT __one) const
988 return to_string<_CharT, _Traits, allocator<_CharT> >(__zero, __one);
991 template <size_t _Size>
992 template <class _CharT>
994 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23
995 basic_string<_CharT, char_traits<_CharT>, allocator<_CharT> >
996 bitset<_Size>::to_string(_CharT __zero, _CharT __one) const
998 return to_string<_CharT, char_traits<_CharT>, allocator<_CharT> >(__zero, __one);
1001 template <size_t _Size>
1003 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23
1004 basic_string<char, char_traits<char>, allocator<char> >
1005 bitset<_Size>::to_string(char __zero, char __one) const
1007 return to_string<char, char_traits<char>, allocator<char> >(__zero, __one);
1010 template <size_t _Size>
1012 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23
1014 bitset<_Size>::count() const _NOEXCEPT
1016 return static_cast<size_t>(_VSTD::__count_bool<true>(base::__make_iter(0), _Size));
1019 template <size_t _Size>
1021 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23
1023 bitset<_Size>::operator==(const bitset& __rhs) const _NOEXCEPT
1025 return _VSTD::equal(base::__make_iter(0), base::__make_iter(_Size), __rhs.__make_iter(0));
1028 #if _LIBCPP_STD_VER <= 17
1030 template <size_t _Size>
1032 _LIBCPP_HIDE_FROM_ABI
1034 bitset<_Size>::operator!=(const bitset& __rhs) const _NOEXCEPT
1036 return !(*this == __rhs);
1041 template <size_t _Size>
1042 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23
1044 bitset<_Size>::test(size_t __pos) const
1047 __throw_out_of_range("bitset test argument out of range");
1049 return (*this)[__pos];
1052 template <size_t _Size>
1054 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23
1056 bitset<_Size>::all() const _NOEXCEPT
1061 template <size_t _Size>
1063 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23
1065 bitset<_Size>::any() const _NOEXCEPT
1070 template <size_t _Size>
1072 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23
1074 bitset<_Size>::operator<<(size_t __pos) const _NOEXCEPT
1081 template <size_t _Size>
1083 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23
1085 bitset<_Size>::operator>>(size_t __pos) const _NOEXCEPT
1092 template <size_t _Size>
1093 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23
1095 operator&(const bitset<_Size>& __x, const bitset<_Size>& __y) _NOEXCEPT
1097 bitset<_Size> __r = __x;
1102 template <size_t _Size>
1103 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23
1105 operator|(const bitset<_Size>& __x, const bitset<_Size>& __y) _NOEXCEPT
1107 bitset<_Size> __r = __x;
1112 template <size_t _Size>
1113 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23
1115 operator^(const bitset<_Size>& __x, const bitset<_Size>& __y) _NOEXCEPT
1117 bitset<_Size> __r = __x;
1122 template <size_t _Size>
1123 struct _LIBCPP_TEMPLATE_VIS hash<bitset<_Size> >
1124 : public __unary_function<bitset<_Size>, size_t>
1126 _LIBCPP_INLINE_VISIBILITY
1127 size_t operator()(const bitset<_Size>& __bs) const _NOEXCEPT
1128 {return __bs.__hash_code();}
1131 template <class _CharT, class _Traits, size_t _Size>
1132 _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
1133 operator>>(basic_istream<_CharT, _Traits>& __is, bitset<_Size>& __x);
1135 template <class _CharT, class _Traits, size_t _Size>
1136 _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
1137 operator<<(basic_ostream<_CharT, _Traits>& __os, const bitset<_Size>& __x);
1139 _LIBCPP_END_NAMESPACE_STD
1143 #if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
1144 # include <concepts>
1146 # include <type_traits>
1149 #endif // _LIBCPP_BITSET