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