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