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