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