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