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