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-exception
7//
8//===----------------------------------------------------------------------===//
9
10#ifndef _LIBCPP_BITSET
11#define _LIBCPP_BITSET
12
13// clang-format off
14
15/*
16 bitset synopsis
17
18namespace std
19{
20
21namespace std {
22
23template <size_t N>
24class bitset
25{
26public:
27 // bit reference:
28 class reference
29 {
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 bit
37 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++23
48 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++26
52 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++26
58 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++23
64
65 // 23.3.5.2 bitset operations:
66 bitset& operator&=(const bitset& rhs) noexcept; // constexpr since C++23
67 bitset& operator|=(const bitset& rhs) noexcept; // constexpr since C++23
68 bitset& operator^=(const bitset& rhs) noexcept; // constexpr since C++23
69 bitset& operator<<=(size_t pos) noexcept; // constexpr since C++23
70 bitset& operator>>=(size_t pos) noexcept; // constexpr since C++23
71 bitset& set() noexcept; // constexpr since C++23
72 bitset& set(size_t pos, bool val = true); // constexpr since C++23
73 bitset& reset() noexcept; // constexpr since C++23
74 bitset& reset(size_t pos); // constexpr since C++23
75 bitset operator~() const noexcept; // constexpr since C++23
76 bitset& flip() noexcept; // constexpr since C++23
77 bitset& flip(size_t pos); // constexpr since C++23
78
79 // element access:
80 constexpr bool operator[](size_t pos) const;
81 reference operator[](size_t pos); // constexpr since C++23
82 unsigned long to_ulong() const; // constexpr since C++23
83 unsigned long long to_ullong() const; // constexpr since C++23
84 template <class charT, class traits, class Allocator> // constexpr since C++23
85 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++23
87 basic_string<charT, traits, allocator<charT> > to_string(charT zero = charT('0'), charT one = charT('1')) const;
88 template <class charT> // constexpr since C++23
89 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++23
91 size_t count() const noexcept; // constexpr since C++23
92 constexpr size_t size() const noexcept; // constexpr since C++23
93 bool operator==(const bitset& rhs) const noexcept; // constexpr since C++23
94 bool operator!=(const bitset& rhs) const noexcept; // removed in C++20
95 bool test(size_t pos) const; // constexpr since C++23
96 bool all() const noexcept; // constexpr since C++23
97 bool any() const noexcept; // constexpr since C++23
98 bool none() const noexcept; // constexpr since C++23
99 bitset<N> operator<<(size_t pos) const noexcept; // constexpr since C++23
100 bitset<N> operator>>(size_t pos) const noexcept; // constexpr since C++23
101};
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++23
106
107template <size_t N>
108bitset<N> operator|(const bitset<N>&, const bitset<N>&) noexcept; // constexpr since C++23
109
110template <size_t N>
111bitset<N> operator^(const bitset<N>&, const bitset<N>&) noexcept; // constexpr since C++23
112
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} // std
124
125*/
126
127// clang-format on
128
129#if __cplusplus < 201103L && defined(_LIBCPP_USE_FROZEN_CXX03_HEADERS)
130# include <__cxx03/bitset>
131#else
132# include <__algorithm/copy.h>
133# include <__algorithm/copy_backward.h>
134# include <__algorithm/count.h>
135# include <__algorithm/equal.h>
136# include <__algorithm/fill.h>
137# include <__algorithm/fill_n.h>
138# include <__algorithm/find.h>
139# include <__algorithm/min.h>
140# include <__assert>
141# include <__bit/countr.h>
142# include <__bit/invert_if.h>
143# include <__bit_reference>
144# include <__config>
145# include <__cstddef/ptrdiff_t.h>
146# include <__cstddef/size_t.h>
147# include <__functional/hash.h>
148# include <__functional/identity.h>
149# include <__functional/unary_function.h>
150# include <__type_traits/enable_if.h>
151# include <__type_traits/integral_constant.h>
152# include <__type_traits/is_char_like_type.h>
153# include <__utility/integer_sequence.h>
154# include <climits>
155# include <stdexcept>
156# include <string_view>
157# include <version>
158
159// standard-mandated includes
160
161// [bitset.syn]
162# include <iosfwd>
163# include <string>
164
165# if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
166# pragma GCC system_header
167# endif
168
169_LIBCPP_PUSH_MACROS
170# include <__undef_macros>
171
172_LIBCPP_BEGIN_NAMESPACE_STD
173
174template <size_t _N_words, size_t _Size>
175class __bitset;
176
177template <size_t _N_words, size_t _Size>
178struct __has_storage_type<__bitset<_N_words, _Size> > {
179 static const bool value = true;
180};
181
182template <size_t _N_words, size_t _Size>
183class __bitset {
184public:
185 typedef size_t __storage_type;
186
187protected:
188 typedef __bitset __self;
189 typedef __storage_type* __storage_pointer;
190 typedef const __storage_type* __const_storage_pointer;
191 static const unsigned __bits_per_word = static_cast<unsigned>(sizeof(__storage_type) * CHAR_BIT);
192
193 friend class __bit_reference<__bitset>;
194 friend class __bit_const_reference<__bitset>;
195 friend class __bit_iterator<__bitset, false>;
196 friend class __bit_iterator<__bitset, true>;
197 friend struct __bit_array<__bitset>;
198
199 __storage_type __first_[_N_words];
200
201 typedef __bit_reference<__bitset> reference;
202 typedef __bit_const_reference<__bitset> __const_reference;
203 typedef __bit_iterator<__bitset, false> __iterator;
204 typedef __bit_iterator<__bitset, true> __const_iterator;
205
206 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR __bitset() _NOEXCEPT;
207 _LIBCPP_HIDE_FROM_ABI explicit _LIBCPP_CONSTEXPR __bitset(unsigned long long __v) _NOEXCEPT;
208
209 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 reference __make_ref(size_t __pos) _NOEXCEPT {
210 return reference(__first_ + __pos / __bits_per_word, __storage_type(1) << __pos % __bits_per_word);
211 }
212 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR __const_reference __make_ref(size_t __pos) const _NOEXCEPT {
213 return __const_reference(__first_ + __pos / __bits_per_word, __storage_type(1) << __pos % __bits_per_word);
214 }
215 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 __iterator __make_iter(size_t __pos) _NOEXCEPT {
216 return __iterator(__first_ + __pos / __bits_per_word, __pos % __bits_per_word);
217 }
218 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 __const_iterator __make_iter(size_t __pos) const _NOEXCEPT {
219 return __const_iterator(__first_ + __pos / __bits_per_word, __pos % __bits_per_word);
220 }
221
222 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 void operator&=(const __bitset& __v) _NOEXCEPT;
223 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 void operator|=(const __bitset& __v) _NOEXCEPT;
224 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 void operator^=(const __bitset& __v) _NOEXCEPT;
225
226 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 void flip() _NOEXCEPT;
227
228 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long to_ulong() const {
229 if _LIBCPP_CONSTEXPR (_Size > sizeof(unsigned long) * CHAR_BIT) {
230 if (auto __e = __make_iter(_Size); std::find(__make_iter(sizeof(unsigned long) * CHAR_BIT), __e, true) != __e)
231 std::__throw_overflow_error(msg: "__bitset<_N_words, _Size>::to_ulong overflow error");
232 }
233
234 static_assert(sizeof(__storage_type) >= sizeof(unsigned long),
235 "libc++ only supports platforms where sizeof(size_t) >= sizeof(unsigned long), such as 32-bit and "
236 "64-bit platforms. If you're interested in supporting a platform where that is not the case, please "
237 "contact the libc++ developers.");
238 return static_cast<unsigned long>(__first_[0]);
239 }
240
241 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long long to_ullong() const {
242 // Check for overflow if _Size does not fit in unsigned long long
243 if _LIBCPP_CONSTEXPR (_Size > sizeof(unsigned long long) * CHAR_BIT) {
244 if (auto __e = __make_iter(_Size);
245 std::find(__make_iter(sizeof(unsigned long long) * CHAR_BIT), __e, true) != __e)
246 std::__throw_overflow_error(msg: "__bitset<_N_words, _Size>::to_ullong overflow error");
247 }
248
249 // At this point, the effective bitset size (excluding leading zeros) fits in unsigned long long
250
251 if _LIBCPP_CONSTEXPR (sizeof(__storage_type) >= sizeof(unsigned long long)) {
252 // If __storage_type is at least as large as unsigned long long, the result spans only one word
253 return static_cast<unsigned long long>(__first_[0]);
254 } else {
255 // Otherwise, the result spans multiple words which are concatenated
256 const size_t __ull_words = (sizeof(unsigned long long) - 1) / sizeof(__storage_type) + 1;
257 const size_t __n_words = _N_words < __ull_words ? _N_words : __ull_words;
258 unsigned long long __r = static_cast<unsigned long long>(__first_[0]);
259 for (size_t __i = 1; __i < __n_words; ++__i)
260 __r |= static_cast<unsigned long long>(__first_[__i]) << (__bits_per_word * __i);
261 return __r;
262 }
263 }
264
265 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bool all() const _NOEXCEPT { return !__scan_bits(__bit_not()); }
266 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bool any() const _NOEXCEPT {
267 return __scan_bits(std::__identity());
268 }
269 _LIBCPP_HIDE_FROM_ABI size_t __hash_code() const _NOEXCEPT;
270
271 template <bool _Sparse, class _CharT, class _Traits, class _Allocator>
272 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 basic_string<_CharT, _Traits, _Allocator>
273 __to_string(_CharT __zero, _CharT __one) const {
274 basic_string<_CharT, _Traits, _Allocator> __r(_Size, _Sparse ? __zero : __one);
275 for (size_t __i = 0, __bits = 0; __i < _N_words; ++__i, __bits += __bits_per_word) {
276 __storage_type __word = std::__invert_if<!_Sparse>(__first_[__i]);
277 if (__i == _N_words - 1 && _Size - __bits < __bits_per_word)
278 __word &= (__storage_type(1) << (_Size - __bits)) - 1;
279 for (; __word; __word &= (__word - 1))
280 __r[_Size - 1 - (__bits + std::__countr_zero(t: __word))] = _Sparse ? __one : __zero;
281 }
282
283 return __r;
284 }
285
286private:
287 struct __bit_not {
288 template <class _Tp>
289 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR _Tp operator()(const _Tp& __x) const _NOEXCEPT {
290 return ~__x;
291 }
292 };
293
294 template <typename _Proj>
295 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 bool __scan_bits(_Proj __proj) const _NOEXCEPT {
296 size_t __n = _Size;
297 __const_storage_pointer __p = __first_;
298 // do middle whole words
299 for (; __n >= __bits_per_word; ++__p, __n -= __bits_per_word)
300 if (__proj(*__p))
301 return true;
302 // do last partial word
303 if (__n > 0) {
304 __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
305 if (__proj(*__p) & __m)
306 return true;
307 }
308 return false;
309 }
310
311# ifdef _LIBCPP_CXX03_LANG
312 void __init(unsigned long long __v, false_type) _NOEXCEPT;
313 _LIBCPP_HIDE_FROM_ABI void __init(unsigned long long __v, true_type) _NOEXCEPT;
314# else
315 template <size_t... _Indices>
316 _LIBCPP_HIDE_FROM_ABI constexpr __bitset(unsigned long long __v, __index_sequence<_Indices...>) _NOEXCEPT
317 : __first_{static_cast<__storage_type>(__v >> (_Indices * __bits_per_word))...} {}
318# endif // _LIBCPP_CXX03_LANG
319};
320
321template <size_t _N_words, size_t _Size>
322inline _LIBCPP_CONSTEXPR __bitset<_N_words, _Size>::__bitset() _NOEXCEPT
323# ifndef _LIBCPP_CXX03_LANG
324 : __first_{0}
325# endif
326{
327# ifdef _LIBCPP_CXX03_LANG
328 std::fill_n(__first_, _N_words, __storage_type(0));
329# endif
330}
331
332# ifdef _LIBCPP_CXX03_LANG
333
334template <size_t _N_words, size_t _Size>
335void __bitset<_N_words, _Size>::__init(unsigned long long __v, false_type) _NOEXCEPT {
336 const size_t __n_words = std::min((sizeof(unsigned long long) - 1) / sizeof(__storage_type) + 1, _N_words);
337 for (size_t __i = 0; __i < __n_words; ++__i, __v >>= __bits_per_word)
338 __first_[__i] = static_cast<__storage_type>(__v);
339 std::fill(__first_ + __n_words, __first_ + _N_words, __storage_type(0));
340}
341
342template <size_t _N_words, size_t _Size>
343inline _LIBCPP_HIDE_FROM_ABI void __bitset<_N_words, _Size>::__init(unsigned long long __v, true_type) _NOEXCEPT {
344 __first_[0] = __v;
345 std::fill(__first_ + 1, __first_ + _N_words, __storage_type(0));
346}
347
348# endif // _LIBCPP_CXX03_LANG
349
350template <size_t _N_words, size_t _Size>
351inline _LIBCPP_CONSTEXPR __bitset<_N_words, _Size>::__bitset(unsigned long long __v) _NOEXCEPT
352# ifndef _LIBCPP_CXX03_LANG
353 : __bitset(__v,
354 __make_index_sequence<(_N_words < (sizeof(unsigned long long) - 1) / sizeof(__storage_type) + 1)
355 ? _N_words
356 : (sizeof(unsigned long long) - 1) / sizeof(__storage_type) + 1>())
357# endif
358{
359# ifdef _LIBCPP_CXX03_LANG
360 __init(__v, _BoolConstant<sizeof(unsigned long long) <= sizeof(__storage_type)>());
361# endif
362}
363
364template <size_t _N_words, size_t _Size>
365inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 void
366__bitset<_N_words, _Size>::operator&=(const __bitset& __v) _NOEXCEPT {
367 for (size_t __i = 0; __i < _N_words; ++__i)
368 __first_[__i] &= __v.__first_[__i];
369}
370
371template <size_t _N_words, size_t _Size>
372inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 void
373__bitset<_N_words, _Size>::operator|=(const __bitset& __v) _NOEXCEPT {
374 for (size_t __i = 0; __i < _N_words; ++__i)
375 __first_[__i] |= __v.__first_[__i];
376}
377
378template <size_t _N_words, size_t _Size>
379inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 void
380__bitset<_N_words, _Size>::operator^=(const __bitset& __v) _NOEXCEPT {
381 for (size_t __i = 0; __i < _N_words; ++__i)
382 __first_[__i] ^= __v.__first_[__i];
383}
384
385template <size_t _N_words, size_t _Size>
386_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 void __bitset<_N_words, _Size>::flip() _NOEXCEPT {
387 // do middle whole words
388 size_t __n = _Size;
389 __storage_pointer __p = __first_;
390 for (; __n >= __bits_per_word; ++__p, __n -= __bits_per_word)
391 *__p = ~*__p;
392 // do last partial word
393 // Ensure trailing padding bits are zeroed as part of the ABI for consistent hashing behavior. std::hash<bitset>
394 // assumes trailing bits are zeroed; otherwise, identical bitsets could hash differently.
395 if (__n > 0)
396 *__p ^= (__storage_type(1) << __n) - 1;
397}
398
399template <size_t _N_words, size_t _Size>
400inline size_t __bitset<_N_words, _Size>::__hash_code() const _NOEXCEPT {
401 size_t __h = 0;
402 for (size_t __i = 0; __i < _N_words; ++__i)
403 __h ^= __first_[__i];
404 return __h;
405}
406
407template <size_t _Size>
408class __bitset<1, _Size> {
409public:
410 typedef size_t __storage_type;
411
412protected:
413 typedef __bitset __self;
414 typedef __storage_type* __storage_pointer;
415 typedef const __storage_type* __const_storage_pointer;
416 static const unsigned __bits_per_word = static_cast<unsigned>(sizeof(__storage_type) * CHAR_BIT);
417
418 friend class __bit_reference<__bitset>;
419 friend class __bit_const_reference<__bitset>;
420 friend class __bit_iterator<__bitset, false>;
421 friend class __bit_iterator<__bitset, true>;
422 friend struct __bit_array<__bitset>;
423
424 __storage_type __first_;
425
426 typedef __bit_reference<__bitset> reference;
427 typedef __bit_const_reference<__bitset> __const_reference;
428 typedef __bit_iterator<__bitset, false> __iterator;
429 typedef __bit_iterator<__bitset, true> __const_iterator;
430
431 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR __bitset() _NOEXCEPT;
432 _LIBCPP_HIDE_FROM_ABI explicit _LIBCPP_CONSTEXPR __bitset(unsigned long long __v) _NOEXCEPT;
433
434 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 reference __make_ref(size_t __pos) _NOEXCEPT {
435 return reference(&__first_, __storage_type(1) << __pos);
436 }
437 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR __const_reference __make_ref(size_t __pos) const _NOEXCEPT {
438 return __const_reference(&__first_, __storage_type(1) << __pos);
439 }
440 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 __iterator __make_iter(size_t __pos) _NOEXCEPT {
441 // Allow the == case to accommodate the past-the-end iterator.
442 _LIBCPP_ASSERT_INTERNAL(__pos <= __bits_per_word, "Out of bounds access in the single-word bitset implementation.");
443 return __pos != __bits_per_word ? __iterator(&__first_, __pos) : __iterator(&__first_ + 1, 0);
444 }
445 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 __const_iterator __make_iter(size_t __pos) const _NOEXCEPT {
446 // Allow the == case to accommodate the past-the-end iterator.
447 _LIBCPP_ASSERT_INTERNAL(__pos <= __bits_per_word, "Out of bounds access in the single-word bitset implementation.");
448 return __pos != __bits_per_word ? __const_iterator(&__first_, __pos) : __const_iterator(&__first_ + 1, 0);
449 }
450
451 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 void operator&=(const __bitset& __v) _NOEXCEPT;
452 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 void operator|=(const __bitset& __v) _NOEXCEPT;
453 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 void operator^=(const __bitset& __v) _NOEXCEPT;
454
455 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 void flip() _NOEXCEPT;
456
457 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long to_ulong() const {
458 if _LIBCPP_CONSTEXPR (_Size > sizeof(unsigned long) * CHAR_BIT) {
459 if (auto __e = __make_iter(_Size); std::find(__make_iter(sizeof(unsigned long) * CHAR_BIT), __e, true) != __e)
460 __throw_overflow_error(msg: "__bitset<1, _Size>::to_ulong overflow error");
461 }
462 return static_cast<unsigned long>(__first_);
463 }
464
465 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long long to_ullong() const {
466 // If _Size exceeds the size of unsigned long long, check for overflow
467 if _LIBCPP_CONSTEXPR (_Size > sizeof(unsigned long long) * CHAR_BIT) {
468 if (auto __e = __make_iter(_Size);
469 std::find(__make_iter(sizeof(unsigned long long) * CHAR_BIT), __e, true) != __e)
470 __throw_overflow_error(msg: "__bitset<1, _Size>::to_ullong overflow error");
471 }
472
473 // If _Size fits or no overflow, directly cast to unsigned long long
474 return static_cast<unsigned long long>(__first_);
475 }
476
477 template <bool _Sparse, class _CharT, class _Traits, class _Allocator>
478 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 basic_string<_CharT, _Traits, _Allocator>
479 __to_string(_CharT __zero, _CharT __one) const {
480 basic_string<_CharT, _Traits, _Allocator> __r(_Size, _Sparse ? __zero : __one);
481 __storage_type __word = std::__invert_if<!_Sparse>(__first_);
482 if (_Size < __bits_per_word)
483 __word &= (__storage_type(1) << _Size) - 1;
484 for (; __word; __word &= (__word - 1)) {
485 size_t __pos = std::__countr_zero(__word);
486 __r[_Size - 1 - __pos] = _Sparse ? __one : __zero;
487 }
488 return __r;
489 }
490
491 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bool all() const _NOEXCEPT;
492 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bool any() const _NOEXCEPT;
493
494 _LIBCPP_HIDE_FROM_ABI size_t __hash_code() const _NOEXCEPT;
495};
496
497template <size_t _Size>
498inline _LIBCPP_CONSTEXPR __bitset<1, _Size>::__bitset() _NOEXCEPT : __first_(0) {}
499
500template <size_t _Size>
501inline _LIBCPP_CONSTEXPR __bitset<1, _Size>::__bitset(unsigned long long __v) _NOEXCEPT
502 // TODO: We must refer to __bits_per_word in order to work around an issue with the GDB pretty-printers.
503 // Without it, the pretty-printers complain about a missing __bits_per_word member. This needs to
504 // be investigated further.
505 : __first_(_Size == __bits_per_word ? static_cast<__storage_type>(__v) : static_cast<__storage_type>(__v)) {}
506
507template <size_t _Size>
508inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 void
509__bitset<1, _Size>::operator&=(const __bitset& __v) _NOEXCEPT {
510 __first_ &= __v.__first_;
511}
512
513template <size_t _Size>
514inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 void
515__bitset<1, _Size>::operator|=(const __bitset& __v) _NOEXCEPT {
516 __first_ |= __v.__first_;
517}
518
519template <size_t _Size>
520inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 void
521__bitset<1, _Size>::operator^=(const __bitset& __v) _NOEXCEPT {
522 __first_ ^= __v.__first_;
523}
524
525template <size_t _Size>
526inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 void __bitset<1, _Size>::flip() _NOEXCEPT {
527 __first_ ^= ~__storage_type(0) >> (__bits_per_word - _Size);
528}
529
530template <size_t _Size>
531inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bool __bitset<1, _Size>::all() const _NOEXCEPT {
532 __storage_type __m = ~__storage_type(0) >> (__bits_per_word - _Size);
533 return !(~__first_ & __m);
534}
535
536template <size_t _Size>
537inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bool __bitset<1, _Size>::any() const _NOEXCEPT {
538 __storage_type __m = ~__storage_type(0) >> (__bits_per_word - _Size);
539 return __first_ & __m;
540}
541
542template <size_t _Size>
543inline size_t __bitset<1, _Size>::__hash_code() const _NOEXCEPT {
544 return __first_;
545}
546
547template <>
548class __bitset<0, 0> {
549public:
550 typedef size_t __storage_type;
551
552protected:
553 typedef __bitset __self;
554 typedef __storage_type* __storage_pointer;
555 typedef const __storage_type* __const_storage_pointer;
556 static const unsigned __bits_per_word = static_cast<unsigned>(sizeof(__storage_type) * CHAR_BIT);
557
558 friend class __bit_reference<__bitset>;
559 friend class __bit_const_reference<__bitset>;
560 friend class __bit_iterator<__bitset, false>;
561 friend class __bit_iterator<__bitset, true>;
562 friend struct __bit_array<__bitset>;
563
564 typedef __bit_reference<__bitset> reference;
565 typedef __bit_const_reference<__bitset> __const_reference;
566 typedef __bit_iterator<__bitset, false> __iterator;
567 typedef __bit_iterator<__bitset, true> __const_iterator;
568
569 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR __bitset() _NOEXCEPT;
570 _LIBCPP_HIDE_FROM_ABI explicit _LIBCPP_CONSTEXPR __bitset(unsigned long long) _NOEXCEPT;
571
572 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 reference __make_ref(size_t) _NOEXCEPT {
573 return reference(nullptr, 1);
574 }
575 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR __const_reference __make_ref(size_t) const _NOEXCEPT {
576 return __const_reference(nullptr, 1);
577 }
578 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 __iterator __make_iter(size_t) _NOEXCEPT {
579 return __iterator(nullptr, 0);
580 }
581 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 __const_iterator __make_iter(size_t) const _NOEXCEPT {
582 return __const_iterator(nullptr, 0);
583 }
584
585 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 void operator&=(const __bitset&) _NOEXCEPT {}
586 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 void operator|=(const __bitset&) _NOEXCEPT {}
587 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 void operator^=(const __bitset&) _NOEXCEPT {}
588
589 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 void flip() _NOEXCEPT {}
590
591 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long to_ulong() const { return 0; }
592 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long long to_ullong() const { return 0; }
593
594 template <bool _Sparse, class _CharT, class _Traits, class _Allocator>
595 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 basic_string<_CharT, _Traits, _Allocator>
596 __to_string(_CharT, _CharT) const {
597 return basic_string<_CharT, _Traits, _Allocator>();
598 }
599
600 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bool all() const _NOEXCEPT { return true; }
601 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bool any() const _NOEXCEPT { return false; }
602
603 _LIBCPP_HIDE_FROM_ABI size_t __hash_code() const _NOEXCEPT { return 0; }
604};
605
606inline _LIBCPP_CONSTEXPR __bitset<0, 0>::__bitset() _NOEXCEPT {}
607
608inline _LIBCPP_CONSTEXPR __bitset<0, 0>::__bitset(unsigned long long) _NOEXCEPT {}
609
610template <size_t _Size>
611class bitset;
612template <size_t _Size>
613struct hash<bitset<_Size> >;
614
615template <size_t _Size>
616class bitset : private __bitset<_Size == 0 ? 0 : (_Size - 1) / (sizeof(size_t) * CHAR_BIT) + 1, _Size> {
617public:
618 static const unsigned __n_words = _Size == 0 ? 0 : (_Size - 1) / (sizeof(size_t) * CHAR_BIT) + 1;
619 typedef __bitset<__n_words, _Size> __base;
620 typedef typename __base::reference reference;
621 typedef typename __base::__const_reference __const_reference;
622
623 // 23.3.5.1 constructors:
624 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR bitset() _NOEXCEPT {}
625 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR bitset(unsigned long long __v) _NOEXCEPT
626 : __base(sizeof(unsigned long long) * CHAR_BIT <= _Size ? __v : __v & ((1ULL << _Size) - 1)) {}
627 template <class _CharT, __enable_if_t<_IsCharLikeType<_CharT>::value, int> = 0>
628 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 explicit bitset(
629 const _CharT* __str,
630 size_t __n = basic_string<_CharT>::npos,
631 _CharT __zero = _CharT('0'),
632 _CharT __one = _CharT('1')) {
633 if (__n == basic_string<_CharT>::npos)
634 __init_from_string_view(basic_string_view<_CharT>(__str), __zero, __one);
635 else
636 __init_from_string_view(basic_string_view<_CharT>(__str, __n), __zero, __one);
637 }
638# if _LIBCPP_STD_VER >= 26
639 template <class _CharT, class _Traits>
640 _LIBCPP_HIDE_FROM_ABI constexpr explicit bitset(
641 basic_string_view<_CharT, _Traits> __str,
642 typename basic_string_view<_CharT, _Traits>::size_type __pos = 0,
643 typename basic_string_view<_CharT, _Traits>::size_type __n = basic_string_view<_CharT, _Traits>::npos,
644 _CharT __zero = _CharT('0'),
645 _CharT __one = _CharT('1')) {
646 if (__pos > __str.size())
647 std::__throw_out_of_range("bitset string pos out of range");
648
649 size_t __rlen = std::min(__n, __str.size() - __pos);
650 __init_from_string_view(basic_string_view<_CharT, _Traits>(__str.data() + __pos, __rlen), __zero, __one);
651 }
652# endif
653 template <class _CharT, class _Traits, class _Allocator>
654 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 explicit bitset(
655 const basic_string<_CharT, _Traits, _Allocator>& __str,
656 typename basic_string<_CharT, _Traits, _Allocator>::size_type __pos = 0,
657 typename basic_string<_CharT, _Traits, _Allocator>::size_type __n =
658 basic_string<_CharT, _Traits, _Allocator>::npos,
659 _CharT __zero = _CharT('0'),
660 _CharT __one = _CharT('1')) {
661 if (__pos > __str.size())
662 std::__throw_out_of_range(msg: "bitset string pos out of range");
663
664 size_t __rlen = std::min(__n, __str.size() - __pos);
665 __init_from_string_view(basic_string_view<_CharT, _Traits>(__str.data() + __pos, __rlen), __zero, __one);
666 }
667
668 // 23.3.5.2 bitset operations:
669 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bitset& operator&=(const bitset& __rhs) _NOEXCEPT;
670 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bitset& operator|=(const bitset& __rhs) _NOEXCEPT;
671 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bitset& operator^=(const bitset& __rhs) _NOEXCEPT;
672 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bitset& operator<<=(size_t __pos) _NOEXCEPT;
673 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bitset& operator>>=(size_t __pos) _NOEXCEPT;
674 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bitset& set() _NOEXCEPT;
675 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bitset& set(size_t __pos, bool __val = true);
676 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bitset& reset() _NOEXCEPT;
677 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bitset& reset(size_t __pos);
678 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bitset operator~() const _NOEXCEPT;
679 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bitset& flip() _NOEXCEPT;
680 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bitset& flip(size_t __pos);
681
682 // element access:
683 // TODO(LLVM 24): Remove the opt-out
684# ifndef _LIBCPP_DEPRECATED_ABI_BITSET_CONST_SUBSCRIPT_RETURN_REF
685 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR bool operator[](size_t __p) const {
686 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(__p < _Size, "bitset::operator[] index out of bounds");
687 return __base::__make_ref(__p);
688 }
689# else
690 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR __const_reference operator[](size_t __p) const {
691 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(__p < _Size, "bitset::operator[] index out of bounds");
692 return __base::__make_ref(__p);
693 }
694# endif
695 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 reference operator[](size_t __p) {
696 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(__p < _Size, "bitset::operator[] index out of bounds");
697 return __base::__make_ref(__p);
698 }
699 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long to_ulong() const {
700 return __base::to_ulong();
701 }
702 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long long to_ullong() const {
703 return __base::to_ullong();
704 }
705 template <class _CharT, class _Traits, class _Allocator>
706 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 basic_string<_CharT, _Traits, _Allocator>
707 to_string(_CharT __zero = _CharT('0'), _CharT __one = _CharT('1')) const;
708 template <class _CharT, class _Traits>
709 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI
710 _LIBCPP_CONSTEXPR_SINCE_CXX23 basic_string<_CharT, _Traits, allocator<_CharT> >
711 to_string(_CharT __zero = _CharT('0'), _CharT __one = _CharT('1')) const;
712 template <class _CharT>
713 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI
714 _LIBCPP_CONSTEXPR_SINCE_CXX23 basic_string<_CharT, char_traits<_CharT>, allocator<_CharT> >
715 to_string(_CharT __zero = _CharT('0'), _CharT __one = _CharT('1')) const;
716 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI
717 _LIBCPP_CONSTEXPR_SINCE_CXX23 basic_string<char, char_traits<char>, allocator<char> >
718 to_string(char __zero = '0', char __one = '1') const;
719 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 size_t count() const _NOEXCEPT;
720 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR size_t size() const _NOEXCEPT { return _Size; }
721 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bool operator==(const bitset& __rhs) const _NOEXCEPT;
722# if _LIBCPP_STD_VER <= 17
723 _LIBCPP_HIDE_FROM_ABI bool operator!=(const bitset& __rhs) const _NOEXCEPT;
724# endif
725 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bool test(size_t __pos) const;
726 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bool all() const _NOEXCEPT {
727 return __base::all();
728 }
729 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bool any() const _NOEXCEPT {
730 return __base::any();
731 }
732 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bool none() const _NOEXCEPT { return !any(); }
733 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bitset operator<<(size_t __pos) const _NOEXCEPT;
734 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bitset operator>>(size_t __pos) const _NOEXCEPT;
735
736private:
737 template <class _CharT, class _Traits>
738 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 void
739 __init_from_string_view(basic_string_view<_CharT, _Traits> __str, _CharT __zero, _CharT __one) {
740 for (size_t __i = 0; __i < __str.size(); ++__i)
741 if (!_Traits::eq(__str[__i], __zero) && !_Traits::eq(__str[__i], __one))
742 std::__throw_invalid_argument(msg: "bitset string ctor has invalid argument");
743
744 size_t __mp = std::min(__str.size(), _Size);
745 size_t __i = 0;
746 for (; __i < __mp; ++__i) {
747 _CharT __c = __str[__mp - 1 - __i];
748 (*this)[__i] = _Traits::eq(__c, __one);
749 }
750 }
751
752 _LIBCPP_HIDE_FROM_ABI size_t __hash_code() const _NOEXCEPT { return __base::__hash_code(); }
753
754 friend struct hash<bitset>;
755};
756
757template <size_t _Size>
758inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bitset<_Size>&
759bitset<_Size>::operator&=(const bitset& __rhs) _NOEXCEPT {
760 __base::operator&=(__rhs);
761 return *this;
762}
763
764template <size_t _Size>
765inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bitset<_Size>&
766bitset<_Size>::operator|=(const bitset& __rhs) _NOEXCEPT {
767 __base::operator|=(__rhs);
768 return *this;
769}
770
771template <size_t _Size>
772inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bitset<_Size>&
773bitset<_Size>::operator^=(const bitset& __rhs) _NOEXCEPT {
774 __base::operator^=(__rhs);
775 return *this;
776}
777
778template <size_t _Size>
779_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bitset<_Size>& bitset<_Size>::operator<<=(size_t __pos) _NOEXCEPT {
780 __pos = std::min(a: __pos, b: _Size);
781 std::copy_backward(__base::__make_iter(0), __base::__make_iter(_Size - __pos), __base::__make_iter(_Size));
782 std::fill_n(__base::__make_iter(0), __pos, false);
783 return *this;
784}
785
786template <size_t _Size>
787_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bitset<_Size>& bitset<_Size>::operator>>=(size_t __pos) _NOEXCEPT {
788 __pos = std::min(a: __pos, b: _Size);
789 std::copy(__base::__make_iter(__pos), __base::__make_iter(_Size), __base::__make_iter(0));
790 std::fill_n(__base::__make_iter(_Size - __pos), __pos, false);
791 return *this;
792}
793
794template <size_t _Size>
795inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bitset<_Size>& bitset<_Size>::set() _NOEXCEPT {
796 std::fill_n(__base::__make_iter(0), _Size, true);
797 return *this;
798}
799
800template <size_t _Size>
801_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bitset<_Size>& bitset<_Size>::set(size_t __pos, bool __val) {
802 if (__pos >= _Size)
803 std::__throw_out_of_range(msg: "bitset set argument out of range");
804
805 (*this)[__pos] = __val;
806 return *this;
807}
808
809template <size_t _Size>
810inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bitset<_Size>& bitset<_Size>::reset() _NOEXCEPT {
811 std::fill_n(__base::__make_iter(0), _Size, false);
812 return *this;
813}
814
815template <size_t _Size>
816_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bitset<_Size>& bitset<_Size>::reset(size_t __pos) {
817 if (__pos >= _Size)
818 std::__throw_out_of_range(msg: "bitset reset argument out of range");
819
820 (*this)[__pos] = false;
821 return *this;
822}
823
824template <size_t _Size>
825inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bitset<_Size> bitset<_Size>::operator~() const _NOEXCEPT {
826 bitset __x(*this);
827 __x.flip();
828 return __x;
829}
830
831template <size_t _Size>
832inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bitset<_Size>& bitset<_Size>::flip() _NOEXCEPT {
833 __base::flip();
834 return *this;
835}
836
837template <size_t _Size>
838_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bitset<_Size>& bitset<_Size>::flip(size_t __pos) {
839 if (__pos >= _Size)
840 std::__throw_out_of_range(msg: "bitset flip argument out of range");
841
842 reference __r = __base::__make_ref(__pos);
843 __r = ~__r;
844 return *this;
845}
846
847template <size_t _Size>
848template <class _CharT, class _Traits, class _Allocator>
849_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 basic_string<_CharT, _Traits, _Allocator>
850bitset<_Size>::to_string(_CharT __zero, _CharT __one) const {
851 bool __sparse = size_t(std::count(__base::__make_iter(0), __base::__make_iter(_Size), true)) < _Size / 2;
852 if (__sparse)
853 return __base::template __to_string<true, _CharT, _Traits, _Allocator>(__zero, __one);
854 else
855 return __base::template __to_string<false, _CharT, _Traits, _Allocator>(__zero, __one);
856}
857
858template <size_t _Size>
859template <class _CharT, class _Traits>
860inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 basic_string<_CharT, _Traits, allocator<_CharT> >
861bitset<_Size>::to_string(_CharT __zero, _CharT __one) const {
862 return to_string<_CharT, _Traits, allocator<_CharT> >(__zero, __one);
863}
864
865template <size_t _Size>
866template <class _CharT>
867inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 basic_string<_CharT, char_traits<_CharT>, allocator<_CharT> >
868bitset<_Size>::to_string(_CharT __zero, _CharT __one) const {
869 return to_string<_CharT, char_traits<_CharT>, allocator<_CharT> >(__zero, __one);
870}
871
872template <size_t _Size>
873inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 basic_string<char, char_traits<char>, allocator<char> >
874bitset<_Size>::to_string(char __zero, char __one) const {
875 return to_string<char, char_traits<char>, allocator<char> >(__zero, __one);
876}
877
878template <size_t _Size>
879inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 size_t bitset<_Size>::count() const _NOEXCEPT {
880# if defined(_LIBCPP_COMPILER_CLANG_BASED) && !defined(_LIBCPP_CXX03_LANG)
881 if constexpr (_Size == 0) {
882 return 0;
883 } else if constexpr (_Size <= __base::__bits_per_word) {
884 return __builtin_popcountg(static_cast<unsigned _BitInt(_Size)>(__base::__first_));
885 } else
886# endif
887 {
888 return static_cast<size_t>(std::count(__base::__make_iter(0), __base::__make_iter(_Size), true));
889 }
890}
891
892template <size_t _Size>
893inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bool
894bitset<_Size>::operator==(const bitset& __rhs) const _NOEXCEPT {
895 return std::equal(__base::__make_iter(0), __base::__make_iter(_Size), __rhs.__make_iter(0));
896}
897
898# if _LIBCPP_STD_VER <= 17
899
900template <size_t _Size>
901inline _LIBCPP_HIDE_FROM_ABI bool bitset<_Size>::operator!=(const bitset& __rhs) const _NOEXCEPT {
902 return !(*this == __rhs);
903}
904
905# endif
906
907template <size_t _Size>
908_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bool bitset<_Size>::test(size_t __pos) const {
909 if (__pos >= _Size)
910 std::__throw_out_of_range(msg: "bitset test argument out of range");
911
912 return (*this)[__pos];
913}
914
915template <size_t _Size>
916inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bitset<_Size>
917bitset<_Size>::operator<<(size_t __pos) const _NOEXCEPT {
918 bitset __r = *this;
919 __r <<= __pos;
920 return __r;
921}
922
923template <size_t _Size>
924inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bitset<_Size>
925bitset<_Size>::operator>>(size_t __pos) const _NOEXCEPT {
926 bitset __r = *this;
927 __r >>= __pos;
928 return __r;
929}
930
931template <size_t _Size>
932[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bitset<_Size>
933operator&(const bitset<_Size>& __x, const bitset<_Size>& __y) _NOEXCEPT {
934 bitset<_Size> __r = __x;
935 __r &= __y;
936 return __r;
937}
938
939template <size_t _Size>
940[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bitset<_Size>
941operator|(const bitset<_Size>& __x, const bitset<_Size>& __y) _NOEXCEPT {
942 bitset<_Size> __r = __x;
943 __r |= __y;
944 return __r;
945}
946
947template <size_t _Size>
948[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bitset<_Size>
949operator^(const bitset<_Size>& __x, const bitset<_Size>& __y) _NOEXCEPT {
950 bitset<_Size> __r = __x;
951 __r ^= __y;
952 return __r;
953}
954
955template <size_t _Size>
956struct hash<bitset<_Size> > : public __unary_function<bitset<_Size>, size_t> {
957 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI size_t operator()(const bitset<_Size>& __bs) const _NOEXCEPT {
958 return __bs.__hash_code();
959 }
960};
961
962template <class _CharT, class _Traits, size_t _Size>
963_LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
964operator>>(basic_istream<_CharT, _Traits>& __is, bitset<_Size>& __x);
965
966template <class _CharT, class _Traits, size_t _Size>
967_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
968operator<<(basic_ostream<_CharT, _Traits>& __os, const bitset<_Size>& __x);
969
970_LIBCPP_END_NAMESPACE_STD
971
972_LIBCPP_POP_MACROS
973
974# if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
975# include <concepts>
976# include <cstdlib>
977# include <optional>
978# include <type_traits>
979# endif
980#endif // __cplusplus < 201103L && defined(_LIBCPP_USE_FROZEN_CXX03_HEADERS)
981
982#endif // _LIBCPP_BITSET
983