]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm-project/libcxx/include/bitset
Merge llvm-project main llvmorg-17-init-19304-gd0b54bb50e51
[FreeBSD/FreeBSD.git] / contrib / llvm-project / libcxx / include / bitset
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
16 namespace std
17 {
18
19 namespace std {
20
21 template <size_t N>
22 class bitset
23 {
24 public:
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:
92 template <size_t N>
93 bitset<N> operator&(const bitset<N>&, const bitset<N>&) noexcept; // constexpr since C++23
94
95 template <size_t N>
96 bitset<N> operator|(const bitset<N>&, const bitset<N>&) noexcept; // constexpr since C++23
97
98 template <size_t N>
99 bitset<N> operator^(const bitset<N>&, const bitset<N>&) noexcept; // constexpr since C++23
100
101 template <class charT, class traits, size_t N>
102 basic_istream<charT, traits>&
103 operator>>(basic_istream<charT, traits>& is, bitset<N>& x);
104
105 template <class charT, class traits, size_t N>
106 basic_ostream<charT, traits>&
107 operator<<(basic_ostream<charT, traits>& os, const bitset<N>& x);
108
109 template <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
144 template <size_t _N_words, size_t _Size>
145 class __bitset;
146
147 template <size_t _N_words, size_t _Size>
148 struct __has_storage_type<__bitset<_N_words, _Size> >
149 {
150     static const bool value = true;
151 };
152
153 template <size_t _N_words, size_t _Size>
154 class __bitset
155 {
156 public:
157     typedef ptrdiff_t              difference_type;
158     typedef size_t                 size_type;
159     typedef size_type              __storage_type;
160 protected:
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;
210 private:
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
230 template <size_t _N_words, size_t _Size>
231 inline
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
245 template <size_t _N_words, size_t _Size>
246 void
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
262 template <size_t _N_words, size_t _Size>
263 inline _LIBCPP_INLINE_VISIBILITY
264 void
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
276 template <size_t _N_words, size_t _Size>
277 inline
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
297 template <size_t _N_words, size_t _Size>
298 inline
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
306 template <size_t _N_words, size_t _Size>
307 inline
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
315 template <size_t _N_words, size_t _Size>
316 inline
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
324 template <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
343 template <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
355 template <size_t _N_words, size_t _Size>
356 inline
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
363 template <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
375 template <size_t _N_words, size_t _Size>
376 inline
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
383 template <size_t _N_words, size_t _Size>
384 inline
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
391 template <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
401 template <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
421 template <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
441 template <size_t _N_words, size_t _Size>
442 inline
443 size_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
452 template <size_t _Size>
453 class __bitset<1, _Size>
454 {
455 public:
456     typedef ptrdiff_t              difference_type;
457     typedef size_t                 size_type;
458     typedef size_type              __storage_type;
459 protected:
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
516 template <size_t _Size>
517 inline
518 _LIBCPP_CONSTEXPR
519 __bitset<1, _Size>::__bitset() _NOEXCEPT
520     : __first_(0)
521 {
522 }
523
524 template <size_t _Size>
525 inline
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
535 template <size_t _Size>
536 inline
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
543 template <size_t _Size>
544 inline
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
551 template <size_t _Size>
552 inline
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
559 template <size_t _Size>
560 inline
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
569 template <size_t _Size>
570 inline
571 _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long
572 __bitset<1, _Size>::to_ulong() const
573 {
574     return __first_;
575 }
576
577 template <size_t _Size>
578 inline
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
585 template <size_t _Size>
586 inline
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
594 template <size_t _Size>
595 inline
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
603 template <size_t _Size>
604 inline
605 size_t
606 __bitset<1, _Size>::__hash_code() const _NOEXCEPT
607 {
608     return __first_;
609 }
610
611 template <>
612 class __bitset<0, 0>
613 {
614 public:
615     typedef ptrdiff_t              difference_type;
616     typedef size_t                 size_type;
617     typedef size_type              __storage_type;
618 protected:
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
664 inline
665 _LIBCPP_CONSTEXPR
666 __bitset<0, 0>::__bitset() _NOEXCEPT
667 {
668 }
669
670 inline
671 _LIBCPP_CONSTEXPR
672 __bitset<0, 0>::__bitset(unsigned long long) _NOEXCEPT
673 {
674 }
675
676 template <size_t _Size> class _LIBCPP_TEMPLATE_VIS bitset;
677 template <size_t _Size> struct hash<bitset<_Size> >;
678
679 template <size_t _Size>
680 class _LIBCPP_TEMPLATE_VIS bitset
681     : private __bitset<_Size == 0 ? 0 : (_Size - 1) / (sizeof(size_t) * CHAR_BIT) + 1, _Size>
682 {
683 public:
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
687 public:
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
793 private:
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
817 template <size_t _Size>
818 inline
819 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23
820 bitset<_Size>&
821 bitset<_Size>::operator&=(const bitset& __rhs) _NOEXCEPT
822 {
823     base::operator&=(__rhs);
824     return *this;
825 }
826
827 template <size_t _Size>
828 inline
829 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23
830 bitset<_Size>&
831 bitset<_Size>::operator|=(const bitset& __rhs) _NOEXCEPT
832 {
833     base::operator|=(__rhs);
834     return *this;
835 }
836
837 template <size_t _Size>
838 inline
839 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23
840 bitset<_Size>&
841 bitset<_Size>::operator^=(const bitset& __rhs) _NOEXCEPT
842 {
843     base::operator^=(__rhs);
844     return *this;
845 }
846
847 template <size_t _Size>
848 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23
849 bitset<_Size>&
850 bitset<_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
858 template <size_t _Size>
859 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23
860 bitset<_Size>&
861 bitset<_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
869 template <size_t _Size>
870 inline
871 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23
872 bitset<_Size>&
873 bitset<_Size>::set() _NOEXCEPT
874 {
875     _VSTD::fill_n(base::__make_iter(0), _Size, true);
876     return *this;
877 }
878
879 template <size_t _Size>
880 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23
881 bitset<_Size>&
882 bitset<_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
891 template <size_t _Size>
892 inline
893 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23
894 bitset<_Size>&
895 bitset<_Size>::reset() _NOEXCEPT
896 {
897     _VSTD::fill_n(base::__make_iter(0), _Size, false);
898     return *this;
899 }
900
901 template <size_t _Size>
902 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23
903 bitset<_Size>&
904 bitset<_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
913 template <size_t _Size>
914 inline
915 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23
916 bitset<_Size>
917 bitset<_Size>::operator~() const _NOEXCEPT
918 {
919     bitset __x(*this);
920     __x.flip();
921     return __x;
922 }
923
924 template <size_t _Size>
925 inline
926 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23
927 bitset<_Size>&
928 bitset<_Size>::flip() _NOEXCEPT
929 {
930     base::flip();
931     return *this;
932 }
933
934 template <size_t _Size>
935 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23
936 bitset<_Size>&
937 bitset<_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
947 template <size_t _Size>
948 inline
949 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23
950 unsigned long
951 bitset<_Size>::to_ulong() const
952 {
953     return base::to_ulong();
954 }
955
956 template <size_t _Size>
957 inline
958 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23
959 unsigned long long
960 bitset<_Size>::to_ullong() const
961 {
962     return base::to_ullong();
963 }
964
965 template <size_t _Size>
966 template <class _CharT, class _Traits, class _Allocator>
967 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23
968 basic_string<_CharT, _Traits, _Allocator>
969 bitset<_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
980 template <size_t _Size>
981 template <class _CharT, class _Traits>
982 inline
983 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23
984 basic_string<_CharT, _Traits, allocator<_CharT> >
985 bitset<_Size>::to_string(_CharT __zero, _CharT __one) const
986 {
987     return to_string<_CharT, _Traits, allocator<_CharT> >(__zero, __one);
988 }
989
990 template <size_t _Size>
991 template <class _CharT>
992 inline
993 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23
994 basic_string<_CharT, char_traits<_CharT>, allocator<_CharT> >
995 bitset<_Size>::to_string(_CharT __zero, _CharT __one) const
996 {
997     return to_string<_CharT, char_traits<_CharT>, allocator<_CharT> >(__zero, __one);
998 }
999
1000 template <size_t _Size>
1001 inline
1002 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23
1003 basic_string<char, char_traits<char>, allocator<char> >
1004 bitset<_Size>::to_string(char __zero, char __one) const
1005 {
1006     return to_string<char, char_traits<char>, allocator<char> >(__zero, __one);
1007 }
1008
1009 template <size_t _Size>
1010 inline
1011 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23
1012 size_t
1013 bitset<_Size>::count() const _NOEXCEPT
1014 {
1015     return static_cast<size_t>(_VSTD::__count_bool_true(base::__make_iter(0), _Size));
1016 }
1017
1018 template <size_t _Size>
1019 inline
1020 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23
1021 bool
1022 bitset<_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
1029 template <size_t _Size>
1030 inline
1031 _LIBCPP_HIDE_FROM_ABI
1032 bool
1033 bitset<_Size>::operator!=(const bitset& __rhs) const _NOEXCEPT
1034 {
1035     return !(*this == __rhs);
1036 }
1037
1038 #endif
1039
1040 template <size_t _Size>
1041 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23
1042 bool
1043 bitset<_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
1051 template <size_t _Size>
1052 inline
1053 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23
1054 bool
1055 bitset<_Size>::all() const _NOEXCEPT
1056 {
1057     return base::all();
1058 }
1059
1060 template <size_t _Size>
1061 inline
1062 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23
1063 bool
1064 bitset<_Size>::any() const _NOEXCEPT
1065 {
1066     return base::any();
1067 }
1068
1069 template <size_t _Size>
1070 inline
1071 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23
1072 bitset<_Size>
1073 bitset<_Size>::operator<<(size_t __pos) const _NOEXCEPT
1074 {
1075     bitset __r = *this;
1076     __r <<= __pos;
1077     return __r;
1078 }
1079
1080 template <size_t _Size>
1081 inline
1082 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23
1083 bitset<_Size>
1084 bitset<_Size>::operator>>(size_t __pos) const _NOEXCEPT
1085 {
1086     bitset __r = *this;
1087     __r >>= __pos;
1088     return __r;
1089 }
1090
1091 template <size_t _Size>
1092 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23
1093 bitset<_Size>
1094 operator&(const bitset<_Size>& __x, const bitset<_Size>& __y) _NOEXCEPT
1095 {
1096     bitset<_Size> __r = __x;
1097     __r &= __y;
1098     return __r;
1099 }
1100
1101 template <size_t _Size>
1102 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23
1103 bitset<_Size>
1104 operator|(const bitset<_Size>& __x, const bitset<_Size>& __y) _NOEXCEPT
1105 {
1106     bitset<_Size> __r = __x;
1107     __r |= __y;
1108     return __r;
1109 }
1110
1111 template <size_t _Size>
1112 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23
1113 bitset<_Size>
1114 operator^(const bitset<_Size>& __x, const bitset<_Size>& __y) _NOEXCEPT
1115 {
1116     bitset<_Size> __r = __x;
1117     __r ^= __y;
1118     return __r;
1119 }
1120
1121 template <size_t _Size>
1122 struct _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
1130 template <class _CharT, class _Traits, size_t _Size>
1131 _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
1132 operator>>(basic_istream<_CharT, _Traits>& __is, bitset<_Size>& __x);
1133
1134 template <class _CharT, class _Traits, size_t _Size>
1135 _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
1136 operator<<(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