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