xref: /freebsd/contrib/llvm-project/libcxx/include/bitset (revision 1631382cf2820245cc72965498ff174bb548dd63)
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/*
14    bitset synopsis
15
16namespace std
17{
18
19namespace std {
20
21template <size_t N>
22class bitset
23{
24public:
25    // bit reference:
26    class reference
27    {
28        friend class bitset;
29        reference() noexcept;
30    public:
31        ~reference() noexcept;
32        reference& operator=(bool x) noexcept;           // for b[i] = x;
33        reference& operator=(const reference&) noexcept; // for b[i] = b[j];
34        bool operator~() const noexcept;                 // flips the bit
35        operator bool() const noexcept;                  // for x = b[i];
36        reference& flip() noexcept;                      // for b[i].flip();
37    };
38
39    // 23.3.5.1 constructors:
40    constexpr bitset() noexcept;
41    constexpr bitset(unsigned long long val) noexcept;
42    template <class charT>
43        explicit bitset(const charT* str,
44                        typename basic_string<charT>::size_type n = basic_string<charT>::npos,
45                        charT zero = charT('0'), charT one = charT('1')); // constexpr since C++23
46    template<class charT, class traits, class Allocator>
47        explicit bitset(const basic_string<charT,traits,Allocator>& str,
48                        typename basic_string<charT,traits,Allocator>::size_type pos = 0,
49                        typename basic_string<charT,traits,Allocator>::size_type n =
50                                 basic_string<charT,traits,Allocator>::npos,
51                        charT zero = charT('0'), charT one = charT('1')); // constexpr since C++23
52
53    // 23.3.5.2 bitset operations:
54    bitset& operator&=(const bitset& rhs) noexcept; // constexpr since C++23
55    bitset& operator|=(const bitset& rhs) noexcept; // constexpr since C++23
56    bitset& operator^=(const bitset& rhs) noexcept; // constexpr since C++23
57    bitset& operator<<=(size_t pos) noexcept;       // constexpr since C++23
58    bitset& operator>>=(size_t pos) noexcept;       // constexpr since C++23
59    bitset& set() noexcept;                         // constexpr since C++23
60    bitset& set(size_t pos, bool val = true);       // constexpr since C++23
61    bitset& reset() noexcept;                       // constexpr since C++23
62    bitset& reset(size_t pos);                      // constexpr since C++23
63    bitset operator~() const noexcept;              // constexpr since C++23
64    bitset& flip() noexcept;                        // constexpr since C++23
65    bitset& flip(size_t pos);                       // constexpr since C++23
66
67    // element access:
68    constexpr bool operator[](size_t pos) const;
69    reference operator[](size_t pos);            // constexpr since C++23
70    unsigned long to_ulong() const;              // constexpr since C++23
71    unsigned long long to_ullong() const;        // constexpr since C++23
72    template <class charT, class traits, class Allocator> // constexpr since C++23
73        basic_string<charT, traits, Allocator> to_string(charT zero = charT('0'), charT one = charT('1')) const;
74    template <class charT, class traits> // constexpr since C++23
75        basic_string<charT, traits, allocator<charT> > to_string(charT zero = charT('0'), charT one = charT('1')) const;
76    template <class charT> // constexpr since C++23
77        basic_string<charT, char_traits<charT>, allocator<charT> > to_string(charT zero = charT('0'), charT one = charT('1')) const;
78    basic_string<char, char_traits<char>, allocator<char> > to_string(char zero = '0', char one = '1') const; // constexpr since C++23
79    size_t count() const noexcept;                     // constexpr since C++23
80    constexpr size_t size() const noexcept;            // constexpr since C++23
81    bool operator==(const bitset& rhs) const noexcept; // constexpr since C++23
82    bool operator!=(const bitset& rhs) const noexcept; // removed in C++20
83    bool test(size_t pos) const;                       // constexpr since C++23
84    bool all() const noexcept;                         // constexpr since C++23
85    bool any() const noexcept;                         // constexpr since C++23
86    bool none() const noexcept;                        // constexpr since C++23
87    bitset<N> operator<<(size_t pos) const noexcept;   // constexpr since C++23
88    bitset<N> operator>>(size_t pos) const noexcept;   // constexpr since C++23
89};
90
91// 23.3.5.3 bitset operators:
92template <size_t N>
93bitset<N> operator&(const bitset<N>&, const bitset<N>&) noexcept; // constexpr since C++23
94
95template <size_t N>
96bitset<N> operator|(const bitset<N>&, const bitset<N>&) noexcept; // constexpr since C++23
97
98template <size_t N>
99bitset<N> operator^(const bitset<N>&, const bitset<N>&) noexcept; // constexpr since C++23
100
101template <class charT, class traits, size_t N>
102basic_istream<charT, traits>&
103operator>>(basic_istream<charT, traits>& is, bitset<N>& x);
104
105template <class charT, class traits, size_t N>
106basic_ostream<charT, traits>&
107operator<<(basic_ostream<charT, traits>& os, const bitset<N>& x);
108
109template <size_t N> struct hash<std::bitset<N>>;
110
111}  // std
112
113*/
114
115#include <__algorithm/fill.h>
116#include <__assert> // all public C++ headers provide the assertion handler
117#include <__bit_reference>
118#include <__config>
119#include <__functional/hash.h>
120#include <__functional/unary_function.h>
121#include <__type_traits/is_char_like_type.h>
122#include <climits>
123#include <cstddef>
124#include <stdexcept>
125#include <string_view>
126#include <version>
127
128// standard-mandated includes
129
130// [bitset.syn]
131#include <iosfwd>
132#include <string>
133
134#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
135#  pragma GCC system_header
136#endif
137
138_LIBCPP_PUSH_MACROS
139#include <__undef_macros>
140
141
142_LIBCPP_BEGIN_NAMESPACE_STD
143
144template <size_t _N_words, size_t _Size>
145class __bitset;
146
147template <size_t _N_words, size_t _Size>
148struct __has_storage_type<__bitset<_N_words, _Size> >
149{
150    static const bool value = true;
151};
152
153template <size_t _N_words, size_t _Size>
154class __bitset
155{
156public:
157    typedef ptrdiff_t              difference_type;
158    typedef size_t                 size_type;
159    typedef size_type              __storage_type;
160protected:
161    typedef __bitset __self;
162    typedef       __storage_type*  __storage_pointer;
163    typedef const __storage_type*  __const_storage_pointer;
164    static const unsigned __bits_per_word = static_cast<unsigned>(sizeof(__storage_type) * CHAR_BIT);
165
166    friend class __bit_reference<__bitset>;
167    friend class __bit_const_reference<__bitset>;
168    friend class __bit_iterator<__bitset, false>;
169    friend class __bit_iterator<__bitset, true>;
170    friend struct __bit_array<__bitset>;
171
172    __storage_type __first_[_N_words];
173
174    typedef __bit_reference<__bitset>                  reference;
175    typedef __bit_const_reference<__bitset>            const_reference;
176    typedef __bit_iterator<__bitset, false>            iterator;
177    typedef __bit_iterator<__bitset, true>             const_iterator;
178
179    _LIBCPP_INLINE_VISIBILITY
180    _LIBCPP_CONSTEXPR __bitset() _NOEXCEPT;
181    _LIBCPP_INLINE_VISIBILITY
182    explicit _LIBCPP_CONSTEXPR __bitset(unsigned long long __v) _NOEXCEPT;
183
184    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23 reference __make_ref(size_t __pos) _NOEXCEPT
185        {return reference(__first_ + __pos / __bits_per_word, __storage_type(1) << __pos % __bits_per_word);}
186    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR const_reference __make_ref(size_t __pos) const _NOEXCEPT
187        {return const_reference(__first_ + __pos / __bits_per_word, __storage_type(1) << __pos % __bits_per_word);}
188    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23 iterator __make_iter(size_t __pos) _NOEXCEPT
189        {return iterator(__first_ + __pos / __bits_per_word, __pos % __bits_per_word);}
190    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23 const_iterator __make_iter(size_t __pos) const _NOEXCEPT
191        {return const_iterator(__first_ + __pos / __bits_per_word, __pos % __bits_per_word);}
192
193    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23
194    void operator&=(const __bitset& __v) _NOEXCEPT;
195    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23
196    void operator|=(const __bitset& __v) _NOEXCEPT;
197    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23
198    void operator^=(const __bitset& __v) _NOEXCEPT;
199
200    _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 void flip() _NOEXCEPT;
201    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long to_ulong() const
202        {return to_ulong(integral_constant<bool, _Size < sizeof(unsigned long) * CHAR_BIT>());}
203    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long long to_ullong() const
204        {return to_ullong(integral_constant<bool, _Size < sizeof(unsigned long long) * CHAR_BIT>());}
205
206    _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bool all() const _NOEXCEPT;
207    _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bool any() const _NOEXCEPT;
208    _LIBCPP_INLINE_VISIBILITY
209    size_t __hash_code() const _NOEXCEPT;
210private:
211#ifdef _LIBCPP_CXX03_LANG
212    void __init(unsigned long long __v, false_type) _NOEXCEPT;
213    _LIBCPP_INLINE_VISIBILITY
214    void __init(unsigned long long __v, true_type) _NOEXCEPT;
215#endif // _LIBCPP_CXX03_LANG
216    _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23
217    unsigned long to_ulong(false_type) const;
218    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23
219    unsigned long to_ulong(true_type) const;
220    _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23
221    unsigned long long to_ullong(false_type) const;
222    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23
223    unsigned long long to_ullong(true_type) const;
224    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23
225    unsigned long long to_ullong(true_type, false_type) const;
226    _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23
227    unsigned long long to_ullong(true_type, true_type) const;
228};
229
230template <size_t _N_words, size_t _Size>
231inline
232_LIBCPP_CONSTEXPR
233__bitset<_N_words, _Size>::__bitset() _NOEXCEPT
234#ifndef _LIBCPP_CXX03_LANG
235    : __first_{0}
236#endif
237{
238#ifdef _LIBCPP_CXX03_LANG
239    _VSTD::fill_n(__first_, _N_words, __storage_type(0));
240#endif
241}
242
243#ifdef _LIBCPP_CXX03_LANG
244
245template <size_t _N_words, size_t _Size>
246void
247__bitset<_N_words, _Size>::__init(unsigned long long __v, false_type) _NOEXCEPT
248{
249    __storage_type __t[sizeof(unsigned long long) / sizeof(__storage_type)];
250    size_t __sz = _Size;
251    for (size_t __i = 0; __i < sizeof(__t)/sizeof(__t[0]); ++__i, __v >>= __bits_per_word, __sz -= __bits_per_word )
252        if ( __sz < __bits_per_word)
253            __t[__i] = static_cast<__storage_type>(__v) & ( 1ULL << __sz ) - 1;
254        else
255            __t[__i] = static_cast<__storage_type>(__v);
256
257    _VSTD::copy(__t, __t + sizeof(__t)/sizeof(__t[0]), __first_);
258    _VSTD::fill(__first_ + sizeof(__t)/sizeof(__t[0]), __first_ + sizeof(__first_)/sizeof(__first_[0]),
259               __storage_type(0));
260}
261
262template <size_t _N_words, size_t _Size>
263inline _LIBCPP_INLINE_VISIBILITY
264void
265__bitset<_N_words, _Size>::__init(unsigned long long __v, true_type) _NOEXCEPT
266{
267    __first_[0] = __v;
268    if (_Size < __bits_per_word)
269        __first_[0] &= ( 1ULL << _Size ) - 1;
270
271    _VSTD::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
278_LIBCPP_CONSTEXPR
279__bitset<_N_words, _Size>::__bitset(unsigned long long __v) _NOEXCEPT
280#ifndef _LIBCPP_CXX03_LANG
281#if __SIZEOF_SIZE_T__ == 8
282    : __first_{__v}
283#elif __SIZEOF_SIZE_T__ == 4
284    : __first_{static_cast<__storage_type>(__v),
285                _Size >= 2 * __bits_per_word ? static_cast<__storage_type>(__v >> __bits_per_word)
286                : static_cast<__storage_type>((__v >> __bits_per_word) & (__storage_type(1) << (_Size - __bits_per_word)) - 1)}
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
299_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 void
300__bitset<_N_words, _Size>::operator&=(const __bitset& __v) _NOEXCEPT
301{
302    for (size_type __i = 0; __i < _N_words; ++__i)
303        __first_[__i] &= __v.__first_[__i];
304}
305
306template <size_t _N_words, size_t _Size>
307inline
308_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 void
309__bitset<_N_words, _Size>::operator|=(const __bitset& __v) _NOEXCEPT
310{
311    for (size_type __i = 0; __i < _N_words; ++__i)
312        __first_[__i] |= __v.__first_[__i];
313}
314
315template <size_t _N_words, size_t _Size>
316inline
317_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 void
318__bitset<_N_words, _Size>::operator^=(const __bitset& __v) _NOEXCEPT
319{
320    for (size_type __i = 0; __i < _N_words; ++__i)
321        __first_[__i] ^= __v.__first_[__i];
322}
323
324template <size_t _N_words, size_t _Size>
325_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 void
326__bitset<_N_words, _Size>::flip() _NOEXCEPT
327{
328    // do middle whole words
329    size_type __n = _Size;
330    __storage_pointer __p = __first_;
331    for (; __n >= __bits_per_word; ++__p, __n -= __bits_per_word)
332        *__p = ~*__p;
333    // do last partial word
334    if (__n > 0)
335    {
336        __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
337        __storage_type __b = *__p & __m;
338        *__p &= ~__m;
339        *__p |= ~__b & __m;
340    }
341}
342
343template <size_t _N_words, size_t _Size>
344_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long
345__bitset<_N_words, _Size>::to_ulong(false_type) const
346{
347    const_iterator __e = __make_iter(_Size);
348    const_iterator __i = _VSTD::find(__make_iter(sizeof(unsigned long) * CHAR_BIT), __e, true);
349    if (__i != __e)
350        __throw_overflow_error("bitset to_ulong overflow error");
351
352    return __first_[0];
353}
354
355template <size_t _N_words, size_t _Size>
356inline
357_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long
358__bitset<_N_words, _Size>::to_ulong(true_type) const
359{
360    return __first_[0];
361}
362
363template <size_t _N_words, size_t _Size>
364_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long long
365__bitset<_N_words, _Size>::to_ullong(false_type) const
366{
367    const_iterator __e = __make_iter(_Size);
368    const_iterator __i = _VSTD::find(__make_iter(sizeof(unsigned long long) * CHAR_BIT), __e, true);
369    if (__i != __e)
370        __throw_overflow_error("bitset to_ullong overflow error");
371
372    return to_ullong(true_type());
373}
374
375template <size_t _N_words, size_t _Size>
376inline
377_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long long
378__bitset<_N_words, _Size>::to_ullong(true_type) const
379{
380    return to_ullong(true_type(), integral_constant<bool, sizeof(__storage_type) < sizeof(unsigned long long)>());
381}
382
383template <size_t _N_words, size_t _Size>
384inline
385_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long long
386__bitset<_N_words, _Size>::to_ullong(true_type, false_type) const
387{
388    return __first_[0];
389}
390
391template <size_t _N_words, size_t _Size>
392_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long long
393__bitset<_N_words, _Size>::to_ullong(true_type, true_type) const
394{
395    unsigned long long __r = __first_[0];
396    for (size_t __i = 1; __i < sizeof(unsigned long long) / sizeof(__storage_type); ++__i)
397        __r |= static_cast<unsigned long long>(__first_[__i]) << (sizeof(__storage_type) * CHAR_BIT);
398    return __r;
399}
400
401template <size_t _N_words, size_t _Size>
402_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bool
403__bitset<_N_words, _Size>::all() const _NOEXCEPT
404{
405    // do middle whole words
406    size_type __n = _Size;
407    __const_storage_pointer __p = __first_;
408    for (; __n >= __bits_per_word; ++__p, __n -= __bits_per_word)
409        if (~*__p)
410            return false;
411    // do last partial word
412    if (__n > 0)
413    {
414        __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
415        if (~*__p & __m)
416            return false;
417    }
418    return true;
419}
420
421template <size_t _N_words, size_t _Size>
422_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bool
423__bitset<_N_words, _Size>::any() const _NOEXCEPT
424{
425    // do middle whole words
426    size_type __n = _Size;
427    __const_storage_pointer __p = __first_;
428    for (; __n >= __bits_per_word; ++__p, __n -= __bits_per_word)
429        if (*__p)
430            return true;
431    // do last partial word
432    if (__n > 0)
433    {
434        __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
435        if (*__p & __m)
436            return true;
437    }
438    return false;
439}
440
441template <size_t _N_words, size_t _Size>
442inline
443size_t
444__bitset<_N_words, _Size>::__hash_code() const _NOEXCEPT
445{
446    size_t __h = 0;
447    for (size_type __i = 0; __i < _N_words; ++__i)
448        __h ^= __first_[__i];
449    return __h;
450}
451
452template <size_t _Size>
453class __bitset<1, _Size>
454{
455public:
456    typedef ptrdiff_t              difference_type;
457    typedef size_t                 size_type;
458    typedef size_type              __storage_type;
459protected:
460    typedef __bitset __self;
461    typedef       __storage_type*  __storage_pointer;
462    typedef const __storage_type*  __const_storage_pointer;
463    static const unsigned __bits_per_word = static_cast<unsigned>(sizeof(__storage_type) * CHAR_BIT);
464
465    friend class __bit_reference<__bitset>;
466    friend class __bit_const_reference<__bitset>;
467    friend class __bit_iterator<__bitset, false>;
468    friend class __bit_iterator<__bitset, true>;
469    friend struct __bit_array<__bitset>;
470
471    __storage_type __first_;
472
473    typedef __bit_reference<__bitset>                  reference;
474    typedef __bit_const_reference<__bitset>            const_reference;
475    typedef __bit_iterator<__bitset, false>            iterator;
476    typedef __bit_iterator<__bitset, true>             const_iterator;
477
478    _LIBCPP_INLINE_VISIBILITY
479    _LIBCPP_CONSTEXPR __bitset() _NOEXCEPT;
480    _LIBCPP_INLINE_VISIBILITY
481    explicit _LIBCPP_CONSTEXPR __bitset(unsigned long long __v) _NOEXCEPT;
482
483    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23 reference __make_ref(size_t __pos) _NOEXCEPT
484        {return reference(&__first_, __storage_type(1) << __pos);}
485    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR const_reference __make_ref(size_t __pos) const _NOEXCEPT
486        {return const_reference(&__first_, __storage_type(1) << __pos);}
487    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23 iterator __make_iter(size_t __pos) _NOEXCEPT
488        {return iterator(&__first_ + __pos / __bits_per_word, __pos % __bits_per_word);}
489    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23 const_iterator __make_iter(size_t __pos) const _NOEXCEPT
490        {return const_iterator(&__first_ + __pos / __bits_per_word, __pos % __bits_per_word);}
491
492    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23
493    void operator&=(const __bitset& __v) _NOEXCEPT;
494    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23
495    void operator|=(const __bitset& __v) _NOEXCEPT;
496    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23
497    void operator^=(const __bitset& __v) _NOEXCEPT;
498
499    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23
500    void flip() _NOEXCEPT;
501
502    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23
503    unsigned long to_ulong() const;
504    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23
505    unsigned long long to_ullong() const;
506
507    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23
508    bool all() const _NOEXCEPT;
509    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23
510    bool any() const _NOEXCEPT;
511
512    _LIBCPP_INLINE_VISIBILITY
513    size_t __hash_code() const _NOEXCEPT;
514};
515
516template <size_t _Size>
517inline
518_LIBCPP_CONSTEXPR
519__bitset<1, _Size>::__bitset() _NOEXCEPT
520    : __first_(0)
521{
522}
523
524template <size_t _Size>
525inline
526_LIBCPP_CONSTEXPR
527__bitset<1, _Size>::__bitset(unsigned long long __v) _NOEXCEPT
528    : __first_(
529        _Size == __bits_per_word ? static_cast<__storage_type>(__v)
530                                 : static_cast<__storage_type>(__v) & ((__storage_type(1) << _Size) - 1)
531    )
532{
533}
534
535template <size_t _Size>
536inline
537_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 void
538__bitset<1, _Size>::operator&=(const __bitset& __v) _NOEXCEPT
539{
540    __first_ &= __v.__first_;
541}
542
543template <size_t _Size>
544inline
545_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 void
546__bitset<1, _Size>::operator|=(const __bitset& __v) _NOEXCEPT
547{
548    __first_ |= __v.__first_;
549}
550
551template <size_t _Size>
552inline
553_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 void
554__bitset<1, _Size>::operator^=(const __bitset& __v) _NOEXCEPT
555{
556    __first_ ^= __v.__first_;
557}
558
559template <size_t _Size>
560inline
561_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 void
562__bitset<1, _Size>::flip() _NOEXCEPT
563{
564    __storage_type __m = ~__storage_type(0) >> (__bits_per_word - _Size);
565    __first_ = ~__first_;
566    __first_ &= __m;
567}
568
569template <size_t _Size>
570inline
571_LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long
572__bitset<1, _Size>::to_ulong() const
573{
574    return __first_;
575}
576
577template <size_t _Size>
578inline
579_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long long
580__bitset<1, _Size>::to_ullong() const
581{
582    return __first_;
583}
584
585template <size_t _Size>
586inline
587_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bool
588__bitset<1, _Size>::all() const _NOEXCEPT
589{
590    __storage_type __m = ~__storage_type(0) >> (__bits_per_word - _Size);
591    return !(~__first_ & __m);
592}
593
594template <size_t _Size>
595inline
596_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bool
597__bitset<1, _Size>::any() const _NOEXCEPT
598{
599    __storage_type __m = ~__storage_type(0) >> (__bits_per_word - _Size);
600    return __first_ & __m;
601}
602
603template <size_t _Size>
604inline
605size_t
606__bitset<1, _Size>::__hash_code() const _NOEXCEPT
607{
608    return __first_;
609}
610
611template <>
612class __bitset<0, 0>
613{
614public:
615    typedef ptrdiff_t              difference_type;
616    typedef size_t                 size_type;
617    typedef size_type              __storage_type;
618protected:
619    typedef __bitset __self;
620    typedef       __storage_type*  __storage_pointer;
621    typedef const __storage_type*  __const_storage_pointer;
622    static const unsigned __bits_per_word = static_cast<unsigned>(sizeof(__storage_type) * CHAR_BIT);
623
624    friend class __bit_reference<__bitset>;
625    friend class __bit_const_reference<__bitset>;
626    friend class __bit_iterator<__bitset, false>;
627    friend class __bit_iterator<__bitset, true>;
628    friend struct __bit_array<__bitset>;
629
630    typedef __bit_reference<__bitset>                  reference;
631    typedef __bit_const_reference<__bitset>            const_reference;
632    typedef __bit_iterator<__bitset, false>            iterator;
633    typedef __bit_iterator<__bitset, true>             const_iterator;
634
635    _LIBCPP_INLINE_VISIBILITY
636    _LIBCPP_CONSTEXPR __bitset() _NOEXCEPT;
637    _LIBCPP_INLINE_VISIBILITY
638    explicit _LIBCPP_CONSTEXPR __bitset(unsigned long long) _NOEXCEPT;
639
640    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23 reference __make_ref(size_t) _NOEXCEPT
641        {return reference(nullptr, 1);}
642    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR const_reference __make_ref(size_t) const _NOEXCEPT
643        {return const_reference(nullptr, 1);}
644    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23 iterator __make_iter(size_t) _NOEXCEPT
645        {return iterator(nullptr, 0);}
646    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23 const_iterator __make_iter(size_t) const _NOEXCEPT
647        {return const_iterator(nullptr, 0);}
648
649    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23 void operator&=(const __bitset&) _NOEXCEPT {}
650    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23 void operator|=(const __bitset&) _NOEXCEPT {}
651    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23 void operator^=(const __bitset&) _NOEXCEPT {}
652
653    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23 void flip() _NOEXCEPT {}
654
655    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long to_ulong() const {return 0;}
656    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long long to_ullong() const {return 0;}
657
658    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23 bool all() const _NOEXCEPT {return true;}
659    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23 bool any() const _NOEXCEPT {return false;}
660
661    _LIBCPP_INLINE_VISIBILITY size_t __hash_code() const _NOEXCEPT {return 0;}
662};
663
664inline
665_LIBCPP_CONSTEXPR
666__bitset<0, 0>::__bitset() _NOEXCEPT
667{
668}
669
670inline
671_LIBCPP_CONSTEXPR
672__bitset<0, 0>::__bitset(unsigned long long) _NOEXCEPT
673{
674}
675
676template <size_t _Size> class _LIBCPP_TEMPLATE_VIS bitset;
677template <size_t _Size> struct hash<bitset<_Size> >;
678
679template <size_t _Size>
680class _LIBCPP_TEMPLATE_VIS bitset
681    : private __bitset<_Size == 0 ? 0 : (_Size - 1) / (sizeof(size_t) * CHAR_BIT) + 1, _Size>
682{
683public:
684    static const unsigned __n_words = _Size == 0 ? 0 : (_Size - 1) / (sizeof(size_t) * CHAR_BIT) + 1;
685    typedef __bitset<__n_words, _Size> base;
686
687public:
688    typedef typename base::reference       reference;
689    typedef typename base::const_reference const_reference;
690
691    // 23.3.5.1 constructors:
692    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR bitset() _NOEXCEPT {}
693    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
694        bitset(unsigned long long __v) _NOEXCEPT : base(__v) {}
695    template <class _CharT, class = __enable_if_t<_IsCharLikeType<_CharT>::value> >
696    _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 explicit bitset(
697        const _CharT* __str,
698        typename basic_string<_CharT>::size_type __n = basic_string<_CharT>::npos,
699        _CharT __zero                                = _CharT('0'),
700        _CharT __one                                 = _CharT('1')) {
701
702        size_t __rlen = std::min(__n, char_traits<_CharT>::length(__str));
703        __init_from_string_view(basic_string_view<_CharT>(__str, __rlen), __zero, __one);
704    }
705    template <class _CharT, class _Traits, class _Allocator>
706    _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 explicit bitset(
707        const basic_string<_CharT, _Traits, _Allocator>& __str,
708        typename basic_string<_CharT, _Traits, _Allocator>::size_type __pos = 0,
709        typename basic_string<_CharT, _Traits, _Allocator>::size_type __n =
710            basic_string<_CharT, _Traits, _Allocator>::npos,
711        _CharT __zero = _CharT('0'),
712        _CharT __one  = _CharT('1')) {
713        if (__pos > __str.size())
714            std::__throw_out_of_range("bitset string pos out of range");
715
716        size_t __rlen = std::min(__n, __str.size() - __pos);
717        __init_from_string_view(basic_string_view<_CharT, _Traits>(__str.data() + __pos, __rlen), __zero, __one);
718    }
719
720    // 23.3.5.2 bitset operations:
721    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23
722    bitset& operator&=(const bitset& __rhs) _NOEXCEPT;
723    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23
724    bitset& operator|=(const bitset& __rhs) _NOEXCEPT;
725    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23
726    bitset& operator^=(const bitset& __rhs) _NOEXCEPT;
727    _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23
728    bitset& operator<<=(size_t __pos) _NOEXCEPT;
729    _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23
730    bitset& operator>>=(size_t __pos) _NOEXCEPT;
731    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23
732    bitset& set() _NOEXCEPT;
733    _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23
734    bitset& set(size_t __pos, bool __val = true);
735    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23
736    bitset& reset() _NOEXCEPT;
737    _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23
738    bitset& reset(size_t __pos);
739    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23
740    bitset  operator~() const _NOEXCEPT;
741    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23
742    bitset& flip() _NOEXCEPT;
743    _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23
744    bitset& flip(size_t __pos);
745
746    // element access:
747#ifdef _LIBCPP_ABI_BITSET_VECTOR_BOOL_CONST_SUBSCRIPT_RETURN_BOOL
748    _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR            bool operator[](size_t __p) const {return base::__make_ref(__p);}
749#else
750    _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR const_reference operator[](size_t __p) const {return base::__make_ref(__p);}
751#endif
752    _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 reference operator[](size_t __p)       {return base::__make_ref(__p);}
753    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23
754    unsigned long to_ulong() const;
755    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23
756    unsigned long long to_ullong() const;
757    template <class _CharT, class _Traits, class _Allocator>
758    _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23
759        basic_string<_CharT, _Traits, _Allocator> to_string(_CharT __zero = _CharT('0'),
760                                                            _CharT __one = _CharT('1')) const;
761    template <class _CharT, class _Traits>
762        _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23
763        basic_string<_CharT, _Traits, allocator<_CharT> > to_string(_CharT __zero = _CharT('0'),
764                                                                    _CharT __one = _CharT('1')) const;
765    template <class _CharT>
766        _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23
767        basic_string<_CharT, char_traits<_CharT>, allocator<_CharT> > to_string(_CharT __zero = _CharT('0'),
768                                                                                _CharT __one = _CharT('1')) const;
769    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23
770    basic_string<char, char_traits<char>, allocator<char> > to_string(char __zero = '0',
771                                                                      char __one = '1') const;
772    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23
773    size_t count() const _NOEXCEPT;
774    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR size_t size() const _NOEXCEPT {return _Size;}
775    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23
776    bool operator==(const bitset& __rhs) const _NOEXCEPT;
777#if _LIBCPP_STD_VER <= 17
778    _LIBCPP_INLINE_VISIBILITY
779    bool operator!=(const bitset& __rhs) const _NOEXCEPT;
780#endif
781    _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23
782    bool test(size_t __pos) const;
783    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23
784    bool all() const _NOEXCEPT;
785    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23
786    bool any() const _NOEXCEPT;
787    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23 bool none() const _NOEXCEPT {return !any();}
788    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23
789    bitset operator<<(size_t __pos) const _NOEXCEPT;
790    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23
791    bitset operator>>(size_t __pos) const _NOEXCEPT;
792
793private:
794    template <class _CharT, class _Traits>
795    _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 void
796    __init_from_string_view(basic_string_view<_CharT, _Traits> __str, _CharT __zero, _CharT __one) {
797
798        for (size_t __i = 0; __i < __str.size(); ++__i)
799            if (!_Traits::eq(__str[__i], __zero) && !_Traits::eq(__str[__i], __one))
800              std::__throw_invalid_argument("bitset string ctor has invalid argument");
801
802        size_t __mp = std::min(__str.size(), _Size);
803        size_t __i  = 0;
804        for (; __i < __mp; ++__i) {
805            _CharT __c   = __str[__mp - 1 - __i];
806            (*this)[__i] = _Traits::eq(__c, __one);
807        }
808        std::fill(base::__make_iter(__i), base::__make_iter(_Size), false);
809    }
810
811    _LIBCPP_INLINE_VISIBILITY
812    size_t __hash_code() const _NOEXCEPT {return base::__hash_code();}
813
814    friend struct hash<bitset>;
815};
816
817template <size_t _Size>
818inline
819_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23
820bitset<_Size>&
821bitset<_Size>::operator&=(const bitset& __rhs) _NOEXCEPT
822{
823    base::operator&=(__rhs);
824    return *this;
825}
826
827template <size_t _Size>
828inline
829_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23
830bitset<_Size>&
831bitset<_Size>::operator|=(const bitset& __rhs) _NOEXCEPT
832{
833    base::operator|=(__rhs);
834    return *this;
835}
836
837template <size_t _Size>
838inline
839_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23
840bitset<_Size>&
841bitset<_Size>::operator^=(const bitset& __rhs) _NOEXCEPT
842{
843    base::operator^=(__rhs);
844    return *this;
845}
846
847template <size_t _Size>
848_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23
849bitset<_Size>&
850bitset<_Size>::operator<<=(size_t __pos) _NOEXCEPT
851{
852    __pos = _VSTD::min(__pos, _Size);
853    _VSTD::copy_backward(base::__make_iter(0), base::__make_iter(_Size - __pos), base::__make_iter(_Size));
854    _VSTD::fill_n(base::__make_iter(0), __pos, false);
855    return *this;
856}
857
858template <size_t _Size>
859_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23
860bitset<_Size>&
861bitset<_Size>::operator>>=(size_t __pos) _NOEXCEPT
862{
863    __pos = _VSTD::min(__pos, _Size);
864    _VSTD::copy(base::__make_iter(__pos), base::__make_iter(_Size), base::__make_iter(0));
865    _VSTD::fill_n(base::__make_iter(_Size - __pos), __pos, false);
866    return *this;
867}
868
869template <size_t _Size>
870inline
871_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23
872bitset<_Size>&
873bitset<_Size>::set() _NOEXCEPT
874{
875    _VSTD::fill_n(base::__make_iter(0), _Size, true);
876    return *this;
877}
878
879template <size_t _Size>
880_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23
881bitset<_Size>&
882bitset<_Size>::set(size_t __pos, bool __val)
883{
884    if (__pos >= _Size)
885        __throw_out_of_range("bitset set argument out of range");
886
887    (*this)[__pos] = __val;
888    return *this;
889}
890
891template <size_t _Size>
892inline
893_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23
894bitset<_Size>&
895bitset<_Size>::reset() _NOEXCEPT
896{
897    _VSTD::fill_n(base::__make_iter(0), _Size, false);
898    return *this;
899}
900
901template <size_t _Size>
902_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23
903bitset<_Size>&
904bitset<_Size>::reset(size_t __pos)
905{
906    if (__pos >= _Size)
907        __throw_out_of_range("bitset reset argument out of range");
908
909    (*this)[__pos] = false;
910    return *this;
911}
912
913template <size_t _Size>
914inline
915_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23
916bitset<_Size>
917bitset<_Size>::operator~() const _NOEXCEPT
918{
919    bitset __x(*this);
920    __x.flip();
921    return __x;
922}
923
924template <size_t _Size>
925inline
926_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23
927bitset<_Size>&
928bitset<_Size>::flip() _NOEXCEPT
929{
930    base::flip();
931    return *this;
932}
933
934template <size_t _Size>
935_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23
936bitset<_Size>&
937bitset<_Size>::flip(size_t __pos)
938{
939    if (__pos >= _Size)
940        __throw_out_of_range("bitset flip argument out of range");
941
942    reference __r = base::__make_ref(__pos);
943    __r = ~__r;
944    return *this;
945}
946
947template <size_t _Size>
948inline
949_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23
950unsigned long
951bitset<_Size>::to_ulong() const
952{
953    return base::to_ulong();
954}
955
956template <size_t _Size>
957inline
958_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23
959unsigned long long
960bitset<_Size>::to_ullong() const
961{
962    return base::to_ullong();
963}
964
965template <size_t _Size>
966template <class _CharT, class _Traits, class _Allocator>
967_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23
968basic_string<_CharT, _Traits, _Allocator>
969bitset<_Size>::to_string(_CharT __zero, _CharT __one) const
970{
971    basic_string<_CharT, _Traits, _Allocator> __r(_Size, __zero);
972    for (size_t __i = 0; __i != _Size; ++__i)
973    {
974        if ((*this)[__i])
975            __r[_Size - 1 - __i] = __one;
976    }
977    return __r;
978}
979
980template <size_t _Size>
981template <class _CharT, class _Traits>
982inline
983_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23
984basic_string<_CharT, _Traits, allocator<_CharT> >
985bitset<_Size>::to_string(_CharT __zero, _CharT __one) const
986{
987    return to_string<_CharT, _Traits, allocator<_CharT> >(__zero, __one);
988}
989
990template <size_t _Size>
991template <class _CharT>
992inline
993_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23
994basic_string<_CharT, char_traits<_CharT>, allocator<_CharT> >
995bitset<_Size>::to_string(_CharT __zero, _CharT __one) const
996{
997    return to_string<_CharT, char_traits<_CharT>, allocator<_CharT> >(__zero, __one);
998}
999
1000template <size_t _Size>
1001inline
1002_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23
1003basic_string<char, char_traits<char>, allocator<char> >
1004bitset<_Size>::to_string(char __zero, char __one) const
1005{
1006    return to_string<char, char_traits<char>, allocator<char> >(__zero, __one);
1007}
1008
1009template <size_t _Size>
1010inline
1011_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23
1012size_t
1013bitset<_Size>::count() const _NOEXCEPT
1014{
1015    return static_cast<size_t>(_VSTD::__count_bool_true(base::__make_iter(0), _Size));
1016}
1017
1018template <size_t _Size>
1019inline
1020_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23
1021bool
1022bitset<_Size>::operator==(const bitset& __rhs) const _NOEXCEPT
1023{
1024    return _VSTD::equal(base::__make_iter(0), base::__make_iter(_Size), __rhs.__make_iter(0));
1025}
1026
1027#if _LIBCPP_STD_VER <= 17
1028
1029template <size_t _Size>
1030inline
1031_LIBCPP_HIDE_FROM_ABI
1032bool
1033bitset<_Size>::operator!=(const bitset& __rhs) const _NOEXCEPT
1034{
1035    return !(*this == __rhs);
1036}
1037
1038#endif
1039
1040template <size_t _Size>
1041_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23
1042bool
1043bitset<_Size>::test(size_t __pos) const
1044{
1045    if (__pos >= _Size)
1046        __throw_out_of_range("bitset test argument out of range");
1047
1048    return (*this)[__pos];
1049}
1050
1051template <size_t _Size>
1052inline
1053_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23
1054bool
1055bitset<_Size>::all() const _NOEXCEPT
1056{
1057    return base::all();
1058}
1059
1060template <size_t _Size>
1061inline
1062_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23
1063bool
1064bitset<_Size>::any() const _NOEXCEPT
1065{
1066    return base::any();
1067}
1068
1069template <size_t _Size>
1070inline
1071_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23
1072bitset<_Size>
1073bitset<_Size>::operator<<(size_t __pos) const _NOEXCEPT
1074{
1075    bitset __r = *this;
1076    __r <<= __pos;
1077    return __r;
1078}
1079
1080template <size_t _Size>
1081inline
1082_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23
1083bitset<_Size>
1084bitset<_Size>::operator>>(size_t __pos) const _NOEXCEPT
1085{
1086    bitset __r = *this;
1087    __r >>= __pos;
1088    return __r;
1089}
1090
1091template <size_t _Size>
1092inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23
1093bitset<_Size>
1094operator&(const bitset<_Size>& __x, const bitset<_Size>& __y) _NOEXCEPT
1095{
1096    bitset<_Size> __r = __x;
1097    __r &= __y;
1098    return __r;
1099}
1100
1101template <size_t _Size>
1102inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23
1103bitset<_Size>
1104operator|(const bitset<_Size>& __x, const bitset<_Size>& __y) _NOEXCEPT
1105{
1106    bitset<_Size> __r = __x;
1107    __r |= __y;
1108    return __r;
1109}
1110
1111template <size_t _Size>
1112inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23
1113bitset<_Size>
1114operator^(const bitset<_Size>& __x, const bitset<_Size>& __y) _NOEXCEPT
1115{
1116    bitset<_Size> __r = __x;
1117    __r ^= __y;
1118    return __r;
1119}
1120
1121template <size_t _Size>
1122struct _LIBCPP_TEMPLATE_VIS hash<bitset<_Size> >
1123    : public __unary_function<bitset<_Size>, size_t>
1124{
1125    _LIBCPP_INLINE_VISIBILITY
1126    size_t operator()(const bitset<_Size>& __bs) const _NOEXCEPT
1127        {return __bs.__hash_code();}
1128};
1129
1130template <class _CharT, class _Traits, size_t _Size>
1131_LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
1132operator>>(basic_istream<_CharT, _Traits>& __is, bitset<_Size>& __x);
1133
1134template <class _CharT, class _Traits, size_t _Size>
1135_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
1136operator<<(basic_ostream<_CharT, _Traits>& __os, const bitset<_Size>& __x);
1137
1138_LIBCPP_END_NAMESPACE_STD
1139
1140_LIBCPP_POP_MACROS
1141
1142#if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
1143#  include <concepts>
1144#  include <cstdlib>
1145#  include <type_traits>
1146#endif
1147
1148#endif // _LIBCPP_BITSET
1149