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