libstdc++
bitset
Go to the documentation of this file.
00001 // <bitset> -*- C++ -*-
00002 
00003 // Copyright (C) 2001-2015 Free Software Foundation, Inc.
00004 //
00005 // This file is part of the GNU ISO C++ Library.  This library is free
00006 // software; you can redistribute it and/or modify it under the
00007 // terms of the GNU General Public License as published by the
00008 // Free Software Foundation; either version 3, or (at your option)
00009 // any later version.
00010 
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 // GNU General Public License for more details.
00015 
00016 // Under Section 7 of GPL version 3, you are granted additional
00017 // permissions described in the GCC Runtime Library Exception, version
00018 // 3.1, as published by the Free Software Foundation.
00019 
00020 // You should have received a copy of the GNU General Public License and
00021 // a copy of the GCC Runtime Library Exception along with this program;
00022 // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
00023 // <http://www.gnu.org/licenses/>.
00024 
00025 /*
00026  * Copyright (c) 1998
00027  * Silicon Graphics Computer Systems, Inc.
00028  *
00029  * Permission to use, copy, modify, distribute and sell this software
00030  * and its documentation for any purpose is hereby granted without fee,
00031  * provided that the above copyright notice appear in all copies and
00032  * that both that copyright notice and this permission notice appear
00033  * in supporting documentation.  Silicon Graphics makes no
00034  * representations about the suitability of this software for any
00035  * purpose.  It is provided "as is" without express or implied warranty.
00036  */
00037 
00038 /** @file include/bitset
00039  *  This is a Standard C++ Library header.
00040  */
00041 
00042 #ifndef _GLIBCXX_BITSET
00043 #define _GLIBCXX_BITSET 1
00044 
00045 #pragma GCC system_header
00046 
00047 #include <string>
00048 #include <bits/functexcept.h>   // For invalid_argument, out_of_range,
00049                                 // overflow_error
00050 #include <iosfwd>
00051 #include <bits/cxxabi_forced.h>
00052 
00053 #define _GLIBCXX_BITSET_BITS_PER_WORD  (__CHAR_BIT__ * __SIZEOF_LONG__)
00054 #define _GLIBCXX_BITSET_WORDS(__n) \
00055   ((__n) / _GLIBCXX_BITSET_BITS_PER_WORD + \
00056    ((__n) % _GLIBCXX_BITSET_BITS_PER_WORD == 0 ? 0 : 1))
00057 
00058 #define _GLIBCXX_BITSET_BITS_PER_ULL (__CHAR_BIT__ * __SIZEOF_LONG_LONG__)
00059 
00060 namespace std _GLIBCXX_VISIBILITY(default)
00061 {
00062 _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
00063 
00064   /**
00065    *  Base class, general case.  It is a class invariant that _Nw will be
00066    *  nonnegative.
00067    *
00068    *  See documentation for bitset.
00069   */
00070   template<size_t _Nw>
00071     struct _Base_bitset
00072     {
00073       typedef unsigned long _WordT;
00074 
00075       /// 0 is the least significant word.
00076       _WordT            _M_w[_Nw];
00077 
00078       _GLIBCXX_CONSTEXPR _Base_bitset() _GLIBCXX_NOEXCEPT
00079       : _M_w() { }
00080 
00081 #if __cplusplus >= 201103L
00082       constexpr _Base_bitset(unsigned long long __val) noexcept
00083       : _M_w{ _WordT(__val)
00084 #if __SIZEOF_LONG_LONG__ > __SIZEOF_LONG__
00085                , _WordT(__val >> _GLIBCXX_BITSET_BITS_PER_WORD)
00086 #endif
00087        } { }
00088 #else
00089       _Base_bitset(unsigned long __val)
00090       : _M_w()
00091       { _M_w[0] = __val; }
00092 #endif
00093 
00094       static _GLIBCXX_CONSTEXPR size_t
00095       _S_whichword(size_t __pos) _GLIBCXX_NOEXCEPT
00096       { return __pos / _GLIBCXX_BITSET_BITS_PER_WORD; }
00097 
00098       static _GLIBCXX_CONSTEXPR size_t
00099       _S_whichbyte(size_t __pos) _GLIBCXX_NOEXCEPT
00100       { return (__pos % _GLIBCXX_BITSET_BITS_PER_WORD) / __CHAR_BIT__; }
00101 
00102       static _GLIBCXX_CONSTEXPR size_t
00103       _S_whichbit(size_t __pos) _GLIBCXX_NOEXCEPT
00104       { return __pos % _GLIBCXX_BITSET_BITS_PER_WORD; }
00105 
00106       static _GLIBCXX_CONSTEXPR _WordT
00107       _S_maskbit(size_t __pos) _GLIBCXX_NOEXCEPT
00108       { return (static_cast<_WordT>(1)) << _S_whichbit(__pos); }
00109 
00110       _WordT&
00111       _M_getword(size_t __pos) _GLIBCXX_NOEXCEPT
00112       { return _M_w[_S_whichword(__pos)]; }
00113 
00114       _GLIBCXX_CONSTEXPR _WordT
00115       _M_getword(size_t __pos) const _GLIBCXX_NOEXCEPT
00116       { return _M_w[_S_whichword(__pos)]; }
00117 
00118 #if __cplusplus >= 201103L
00119       const _WordT*
00120       _M_getdata() const noexcept
00121       { return _M_w; }
00122 #endif
00123 
00124       _WordT&
00125       _M_hiword() _GLIBCXX_NOEXCEPT
00126       { return _M_w[_Nw - 1]; }
00127 
00128       _GLIBCXX_CONSTEXPR _WordT
00129       _M_hiword() const _GLIBCXX_NOEXCEPT
00130       { return _M_w[_Nw - 1]; }
00131 
00132       void
00133       _M_do_and(const _Base_bitset<_Nw>& __x) _GLIBCXX_NOEXCEPT
00134       {
00135         for (size_t __i = 0; __i < _Nw; __i++)
00136           _M_w[__i] &= __x._M_w[__i];
00137       }
00138 
00139       void
00140       _M_do_or(const _Base_bitset<_Nw>& __x) _GLIBCXX_NOEXCEPT
00141       {
00142         for (size_t __i = 0; __i < _Nw; __i++)
00143           _M_w[__i] |= __x._M_w[__i];
00144       }
00145 
00146       void
00147       _M_do_xor(const _Base_bitset<_Nw>& __x) _GLIBCXX_NOEXCEPT
00148       {
00149         for (size_t __i = 0; __i < _Nw; __i++)
00150           _M_w[__i] ^= __x._M_w[__i];
00151       }
00152 
00153       void
00154       _M_do_left_shift(size_t __shift) _GLIBCXX_NOEXCEPT;
00155 
00156       void
00157       _M_do_right_shift(size_t __shift) _GLIBCXX_NOEXCEPT;
00158 
00159       void
00160       _M_do_flip() _GLIBCXX_NOEXCEPT
00161       {
00162         for (size_t __i = 0; __i < _Nw; __i++)
00163           _M_w[__i] = ~_M_w[__i];
00164       }
00165 
00166       void
00167       _M_do_set() _GLIBCXX_NOEXCEPT
00168       {
00169         for (size_t __i = 0; __i < _Nw; __i++)
00170           _M_w[__i] = ~static_cast<_WordT>(0);
00171       }
00172 
00173       void
00174       _M_do_reset() _GLIBCXX_NOEXCEPT
00175       { __builtin_memset(_M_w, 0, _Nw * sizeof(_WordT)); }
00176 
00177       bool
00178       _M_is_equal(const _Base_bitset<_Nw>& __x) const _GLIBCXX_NOEXCEPT
00179       {
00180         for (size_t __i = 0; __i < _Nw; ++__i)
00181           if (_M_w[__i] != __x._M_w[__i])
00182             return false;
00183         return true;
00184       }
00185 
00186       template<size_t _Nb>
00187         bool
00188         _M_are_all() const _GLIBCXX_NOEXCEPT
00189         {
00190           for (size_t __i = 0; __i < _Nw - 1; __i++)
00191             if (_M_w[__i] != ~static_cast<_WordT>(0))
00192               return false;
00193           return _M_hiword() == (~static_cast<_WordT>(0)
00194                                  >> (_Nw * _GLIBCXX_BITSET_BITS_PER_WORD
00195                                      - _Nb));
00196         }
00197 
00198       bool
00199       _M_is_any() const _GLIBCXX_NOEXCEPT
00200       {
00201         for (size_t __i = 0; __i < _Nw; __i++)
00202           if (_M_w[__i] != static_cast<_WordT>(0))
00203             return true;
00204         return false;
00205       }
00206 
00207       size_t
00208       _M_do_count() const _GLIBCXX_NOEXCEPT
00209       {
00210         size_t __result = 0;
00211         for (size_t __i = 0; __i < _Nw; __i++)
00212           __result += __builtin_popcountl(_M_w[__i]);
00213         return __result;
00214       }
00215 
00216       unsigned long
00217       _M_do_to_ulong() const;
00218 
00219 #if __cplusplus >= 201103L
00220       unsigned long long
00221       _M_do_to_ullong() const;
00222 #endif
00223 
00224       // find first "on" bit
00225       size_t
00226       _M_do_find_first(size_t) const _GLIBCXX_NOEXCEPT;
00227 
00228       // find the next "on" bit that follows "prev"
00229       size_t
00230       _M_do_find_next(size_t, size_t) const _GLIBCXX_NOEXCEPT;
00231     };
00232 
00233   // Definitions of non-inline functions from _Base_bitset.
00234   template<size_t _Nw>
00235     void
00236     _Base_bitset<_Nw>::_M_do_left_shift(size_t __shift) _GLIBCXX_NOEXCEPT
00237     {
00238       if (__builtin_expect(__shift != 0, 1))
00239         {
00240           const size_t __wshift = __shift / _GLIBCXX_BITSET_BITS_PER_WORD;
00241           const size_t __offset = __shift % _GLIBCXX_BITSET_BITS_PER_WORD;
00242 
00243           if (__offset == 0)
00244             for (size_t __n = _Nw - 1; __n >= __wshift; --__n)
00245               _M_w[__n] = _M_w[__n - __wshift];
00246           else
00247             {
00248               const size_t __sub_offset = (_GLIBCXX_BITSET_BITS_PER_WORD 
00249                                            - __offset);
00250               for (size_t __n = _Nw - 1; __n > __wshift; --__n)
00251                 _M_w[__n] = ((_M_w[__n - __wshift] << __offset)
00252                              | (_M_w[__n - __wshift - 1] >> __sub_offset));
00253               _M_w[__wshift] = _M_w[0] << __offset;
00254             }
00255 
00256           std::fill(_M_w + 0, _M_w + __wshift, static_cast<_WordT>(0));
00257         }
00258     }
00259 
00260   template<size_t _Nw>
00261     void
00262     _Base_bitset<_Nw>::_M_do_right_shift(size_t __shift) _GLIBCXX_NOEXCEPT
00263     {
00264       if (__builtin_expect(__shift != 0, 1))
00265         {
00266           const size_t __wshift = __shift / _GLIBCXX_BITSET_BITS_PER_WORD;
00267           const size_t __offset = __shift % _GLIBCXX_BITSET_BITS_PER_WORD;
00268           const size_t __limit = _Nw - __wshift - 1;
00269 
00270           if (__offset == 0)
00271             for (size_t __n = 0; __n <= __limit; ++__n)
00272               _M_w[__n] = _M_w[__n + __wshift];
00273           else
00274             {
00275               const size_t __sub_offset = (_GLIBCXX_BITSET_BITS_PER_WORD
00276                                            - __offset);
00277               for (size_t __n = 0; __n < __limit; ++__n)
00278                 _M_w[__n] = ((_M_w[__n + __wshift] >> __offset)
00279                              | (_M_w[__n + __wshift + 1] << __sub_offset));
00280               _M_w[__limit] = _M_w[_Nw-1] >> __offset;
00281             }
00282           
00283           std::fill(_M_w + __limit + 1, _M_w + _Nw, static_cast<_WordT>(0));
00284         }
00285     }
00286 
00287   template<size_t _Nw>
00288     unsigned long
00289     _Base_bitset<_Nw>::_M_do_to_ulong() const
00290     {
00291       for (size_t __i = 1; __i < _Nw; ++__i)
00292         if (_M_w[__i])
00293           __throw_overflow_error(__N("_Base_bitset::_M_do_to_ulong"));
00294       return _M_w[0];
00295     }
00296 
00297 #if __cplusplus >= 201103L
00298   template<size_t _Nw>
00299     unsigned long long
00300     _Base_bitset<_Nw>::_M_do_to_ullong() const
00301     {
00302       const bool __dw = sizeof(unsigned long long) > sizeof(unsigned long);
00303       for (size_t __i = 1 + __dw; __i < _Nw; ++__i)
00304         if (_M_w[__i])
00305           __throw_overflow_error(__N("_Base_bitset::_M_do_to_ullong"));
00306 
00307       if (__dw)
00308         return _M_w[0] + (static_cast<unsigned long long>(_M_w[1])
00309                           << _GLIBCXX_BITSET_BITS_PER_WORD);
00310       return _M_w[0];
00311     }
00312 #endif
00313 
00314   template<size_t _Nw>
00315     size_t
00316     _Base_bitset<_Nw>::
00317     _M_do_find_first(size_t __not_found) const _GLIBCXX_NOEXCEPT
00318     {
00319       for (size_t __i = 0; __i < _Nw; __i++)
00320         {
00321           _WordT __thisword = _M_w[__i];
00322           if (__thisword != static_cast<_WordT>(0))
00323             return (__i * _GLIBCXX_BITSET_BITS_PER_WORD
00324                     + __builtin_ctzl(__thisword));
00325         }
00326       // not found, so return an indication of failure.
00327       return __not_found;
00328     }
00329 
00330   template<size_t _Nw>
00331     size_t
00332     _Base_bitset<_Nw>::
00333     _M_do_find_next(size_t __prev, size_t __not_found) const _GLIBCXX_NOEXCEPT
00334     {
00335       // make bound inclusive
00336       ++__prev;
00337 
00338       // check out of bounds
00339       if (__prev >= _Nw * _GLIBCXX_BITSET_BITS_PER_WORD)
00340         return __not_found;
00341 
00342       // search first word
00343       size_t __i = _S_whichword(__prev);
00344       _WordT __thisword = _M_w[__i];
00345 
00346       // mask off bits below bound
00347       __thisword &= (~static_cast<_WordT>(0)) << _S_whichbit(__prev);
00348 
00349       if (__thisword != static_cast<_WordT>(0))
00350         return (__i * _GLIBCXX_BITSET_BITS_PER_WORD
00351                 + __builtin_ctzl(__thisword));
00352 
00353       // check subsequent words
00354       __i++;
00355       for (; __i < _Nw; __i++)
00356         {
00357           __thisword = _M_w[__i];
00358           if (__thisword != static_cast<_WordT>(0))
00359             return (__i * _GLIBCXX_BITSET_BITS_PER_WORD
00360                     + __builtin_ctzl(__thisword));
00361         }
00362       // not found, so return an indication of failure.
00363       return __not_found;
00364     } // end _M_do_find_next
00365 
00366   /**
00367    *  Base class, specialization for a single word.
00368    *
00369    *  See documentation for bitset.
00370   */
00371   template<>
00372     struct _Base_bitset<1>
00373     {
00374       typedef unsigned long _WordT;
00375       _WordT _M_w;
00376 
00377       _GLIBCXX_CONSTEXPR _Base_bitset() _GLIBCXX_NOEXCEPT
00378       : _M_w(0)
00379       { }
00380 
00381 #if __cplusplus >= 201103L
00382       constexpr _Base_bitset(unsigned long long __val) noexcept
00383 #else
00384       _Base_bitset(unsigned long __val)
00385 #endif
00386       : _M_w(__val)
00387       { }
00388 
00389       static _GLIBCXX_CONSTEXPR size_t
00390       _S_whichword(size_t __pos) _GLIBCXX_NOEXCEPT
00391       { return __pos / _GLIBCXX_BITSET_BITS_PER_WORD; }
00392 
00393       static _GLIBCXX_CONSTEXPR size_t
00394       _S_whichbyte(size_t __pos) _GLIBCXX_NOEXCEPT
00395       { return (__pos % _GLIBCXX_BITSET_BITS_PER_WORD) / __CHAR_BIT__; }
00396 
00397       static _GLIBCXX_CONSTEXPR size_t
00398       _S_whichbit(size_t __pos) _GLIBCXX_NOEXCEPT
00399       {  return __pos % _GLIBCXX_BITSET_BITS_PER_WORD; }
00400 
00401       static _GLIBCXX_CONSTEXPR _WordT
00402       _S_maskbit(size_t __pos) _GLIBCXX_NOEXCEPT
00403       { return (static_cast<_WordT>(1)) << _S_whichbit(__pos); }
00404 
00405       _WordT&
00406       _M_getword(size_t) _GLIBCXX_NOEXCEPT
00407       { return _M_w; }
00408 
00409       _GLIBCXX_CONSTEXPR _WordT
00410       _M_getword(size_t) const _GLIBCXX_NOEXCEPT
00411       { return _M_w; }
00412 
00413 #if __cplusplus >= 201103L
00414       const _WordT*
00415       _M_getdata() const noexcept
00416       { return &_M_w; }
00417 #endif
00418 
00419       _WordT&
00420       _M_hiword() _GLIBCXX_NOEXCEPT
00421       { return _M_w; }
00422 
00423       _GLIBCXX_CONSTEXPR _WordT
00424       _M_hiword() const _GLIBCXX_NOEXCEPT
00425       { return _M_w; }
00426 
00427       void
00428       _M_do_and(const _Base_bitset<1>& __x) _GLIBCXX_NOEXCEPT
00429       { _M_w &= __x._M_w; }
00430 
00431       void
00432       _M_do_or(const _Base_bitset<1>& __x) _GLIBCXX_NOEXCEPT
00433       { _M_w |= __x._M_w; }
00434 
00435       void
00436       _M_do_xor(const _Base_bitset<1>& __x) _GLIBCXX_NOEXCEPT
00437       { _M_w ^= __x._M_w; }
00438 
00439       void
00440       _M_do_left_shift(size_t __shift) _GLIBCXX_NOEXCEPT
00441       { _M_w <<= __shift; }
00442 
00443       void
00444       _M_do_right_shift(size_t __shift) _GLIBCXX_NOEXCEPT
00445       { _M_w >>= __shift; }
00446 
00447       void
00448       _M_do_flip() _GLIBCXX_NOEXCEPT
00449       { _M_w = ~_M_w; }
00450 
00451       void
00452       _M_do_set() _GLIBCXX_NOEXCEPT
00453       { _M_w = ~static_cast<_WordT>(0); }
00454 
00455       void
00456       _M_do_reset() _GLIBCXX_NOEXCEPT
00457       { _M_w = 0; }
00458 
00459       bool
00460       _M_is_equal(const _Base_bitset<1>& __x) const _GLIBCXX_NOEXCEPT
00461       { return _M_w == __x._M_w; }
00462 
00463       template<size_t _Nb>
00464         bool
00465         _M_are_all() const _GLIBCXX_NOEXCEPT
00466         { return _M_w == (~static_cast<_WordT>(0)
00467                           >> (_GLIBCXX_BITSET_BITS_PER_WORD - _Nb)); }
00468 
00469       bool
00470       _M_is_any() const _GLIBCXX_NOEXCEPT
00471       { return _M_w != 0; }
00472 
00473       size_t
00474       _M_do_count() const _GLIBCXX_NOEXCEPT
00475       { return __builtin_popcountl(_M_w); }
00476 
00477       unsigned long
00478       _M_do_to_ulong() const _GLIBCXX_NOEXCEPT
00479       { return _M_w; }
00480 
00481 #if __cplusplus >= 201103L
00482       unsigned long long
00483       _M_do_to_ullong() const noexcept
00484       { return _M_w; }
00485 #endif
00486 
00487       size_t
00488       _M_do_find_first(size_t __not_found) const _GLIBCXX_NOEXCEPT
00489       {
00490         if (_M_w != 0)
00491           return __builtin_ctzl(_M_w);
00492         else
00493           return __not_found;
00494       }
00495 
00496       // find the next "on" bit that follows "prev"
00497       size_t
00498       _M_do_find_next(size_t __prev, size_t __not_found) const
00499         _GLIBCXX_NOEXCEPT
00500       {
00501         ++__prev;
00502         if (__prev >= ((size_t) _GLIBCXX_BITSET_BITS_PER_WORD))
00503           return __not_found;
00504 
00505         _WordT __x = _M_w >> __prev;
00506         if (__x != 0)
00507           return __builtin_ctzl(__x) + __prev;
00508         else
00509           return __not_found;
00510       }
00511     };
00512 
00513   /**
00514    *  Base class, specialization for no storage (zero-length %bitset).
00515    *
00516    *  See documentation for bitset.
00517   */
00518   template<>
00519     struct _Base_bitset<0>
00520     {
00521       typedef unsigned long _WordT;
00522 
00523       _GLIBCXX_CONSTEXPR _Base_bitset() _GLIBCXX_NOEXCEPT
00524       { }
00525 
00526 #if __cplusplus >= 201103L
00527       constexpr _Base_bitset(unsigned long long) noexcept
00528 #else
00529       _Base_bitset(unsigned long)
00530 #endif
00531       { }
00532 
00533       static _GLIBCXX_CONSTEXPR size_t
00534       _S_whichword(size_t __pos) _GLIBCXX_NOEXCEPT
00535       { return __pos / _GLIBCXX_BITSET_BITS_PER_WORD; }
00536 
00537       static _GLIBCXX_CONSTEXPR size_t
00538       _S_whichbyte(size_t __pos) _GLIBCXX_NOEXCEPT
00539       { return (__pos % _GLIBCXX_BITSET_BITS_PER_WORD) / __CHAR_BIT__; }
00540 
00541       static _GLIBCXX_CONSTEXPR size_t
00542       _S_whichbit(size_t __pos) _GLIBCXX_NOEXCEPT
00543       {  return __pos % _GLIBCXX_BITSET_BITS_PER_WORD; }
00544 
00545       static _GLIBCXX_CONSTEXPR _WordT
00546       _S_maskbit(size_t __pos) _GLIBCXX_NOEXCEPT
00547       { return (static_cast<_WordT>(1)) << _S_whichbit(__pos); }
00548 
00549       // This would normally give access to the data.  The bounds-checking
00550       // in the bitset class will prevent the user from getting this far,
00551       // but (1) it must still return an lvalue to compile, and (2) the
00552       // user might call _Unchecked_set directly, in which case this /needs/
00553       // to fail.  Let's not penalize zero-length users unless they actually
00554       // make an unchecked call; all the memory ugliness is therefore
00555       // localized to this single should-never-get-this-far function.
00556       _WordT&
00557       _M_getword(size_t) _GLIBCXX_NOEXCEPT
00558       {
00559         __throw_out_of_range(__N("_Base_bitset::_M_getword")); 
00560         return *new _WordT;
00561       }
00562 
00563       _GLIBCXX_CONSTEXPR _WordT
00564       _M_getword(size_t __pos) const _GLIBCXX_NOEXCEPT
00565       { return 0; }
00566 
00567       _GLIBCXX_CONSTEXPR _WordT
00568       _M_hiword() const _GLIBCXX_NOEXCEPT
00569       { return 0; }
00570 
00571       void
00572       _M_do_and(const _Base_bitset<0>&) _GLIBCXX_NOEXCEPT
00573       { }
00574 
00575       void
00576       _M_do_or(const _Base_bitset<0>&) _GLIBCXX_NOEXCEPT
00577       { }
00578 
00579       void
00580       _M_do_xor(const _Base_bitset<0>&) _GLIBCXX_NOEXCEPT
00581       { }
00582 
00583       void
00584       _M_do_left_shift(size_t) _GLIBCXX_NOEXCEPT
00585       { }
00586 
00587       void
00588       _M_do_right_shift(size_t) _GLIBCXX_NOEXCEPT
00589       { }
00590 
00591       void
00592       _M_do_flip() _GLIBCXX_NOEXCEPT
00593       { }
00594 
00595       void
00596       _M_do_set() _GLIBCXX_NOEXCEPT
00597       { }
00598 
00599       void
00600       _M_do_reset() _GLIBCXX_NOEXCEPT
00601       { }
00602 
00603       // Are all empty bitsets equal to each other?  Are they equal to
00604       // themselves?  How to compare a thing which has no state?  What is
00605       // the sound of one zero-length bitset clapping?
00606       bool
00607       _M_is_equal(const _Base_bitset<0>&) const _GLIBCXX_NOEXCEPT
00608       { return true; }
00609 
00610       template<size_t _Nb>
00611         bool
00612         _M_are_all() const _GLIBCXX_NOEXCEPT
00613         { return true; }
00614 
00615       bool
00616       _M_is_any() const _GLIBCXX_NOEXCEPT
00617       { return false; }
00618 
00619       size_t
00620       _M_do_count() const _GLIBCXX_NOEXCEPT
00621       { return 0; }
00622 
00623       unsigned long
00624       _M_do_to_ulong() const _GLIBCXX_NOEXCEPT
00625       { return 0; }
00626 
00627 #if __cplusplus >= 201103L
00628       unsigned long long
00629       _M_do_to_ullong() const noexcept
00630       { return 0; }
00631 #endif
00632 
00633       // Normally "not found" is the size, but that could also be
00634       // misinterpreted as an index in this corner case.  Oh well.
00635       size_t
00636       _M_do_find_first(size_t) const _GLIBCXX_NOEXCEPT
00637       { return 0; }
00638 
00639       size_t
00640       _M_do_find_next(size_t, size_t) const _GLIBCXX_NOEXCEPT
00641       { return 0; }
00642     };
00643 
00644 
00645   // Helper class to zero out the unused high-order bits in the highest word.
00646   template<size_t _Extrabits>
00647     struct _Sanitize
00648     {
00649       typedef unsigned long _WordT;
00650 
00651       static void
00652       _S_do_sanitize(_WordT& __val) _GLIBCXX_NOEXCEPT
00653       { __val &= ~((~static_cast<_WordT>(0)) << _Extrabits); }
00654     };
00655 
00656   template<>
00657     struct _Sanitize<0>
00658     {
00659       typedef unsigned long _WordT;
00660 
00661       static void
00662       _S_do_sanitize(_WordT) _GLIBCXX_NOEXCEPT { } 
00663     };
00664 
00665 #if __cplusplus >= 201103L
00666   template<size_t _Nb, bool = _Nb < _GLIBCXX_BITSET_BITS_PER_ULL>
00667     struct _Sanitize_val
00668     {
00669       static constexpr unsigned long long
00670       _S_do_sanitize_val(unsigned long long __val)
00671       { return __val; }
00672     };
00673 
00674   template<size_t _Nb>
00675     struct _Sanitize_val<_Nb, true>
00676     {
00677       static constexpr unsigned long long
00678       _S_do_sanitize_val(unsigned long long __val)
00679       { return __val & ~((~static_cast<unsigned long long>(0)) << _Nb); }
00680     };
00681 #endif
00682 
00683   /**
00684    *  @class bitset <bitset>
00685    *
00686    *  @brief The %bitset class represents a @e fixed-size sequence of bits.
00687    *  @ingroup utilities
00688    *
00689    *  (Note that %bitset does @e not meet the formal requirements of a
00690    *  <a href="tables.html#65">container</a>.  Mainly, it lacks iterators.)
00691    *
00692    *  The template argument, @a Nb, may be any non-negative number,
00693    *  specifying the number of bits (e.g., "0", "12", "1024*1024").
00694    *
00695    *  In the general unoptimized case, storage is allocated in word-sized
00696    *  blocks.  Let B be the number of bits in a word, then (Nb+(B-1))/B
00697    *  words will be used for storage.  B - Nb%B bits are unused.  (They are
00698    *  the high-order bits in the highest word.)  It is a class invariant
00699    *  that those unused bits are always zero.
00700    *
00701    *  If you think of %bitset as <em>a simple array of bits</em>, be
00702    *  aware that your mental picture is reversed: a %bitset behaves
00703    *  the same way as bits in integers do, with the bit at index 0 in
00704    *  the <em>least significant / right-hand</em> position, and the bit at
00705    *  index Nb-1 in the <em>most significant / left-hand</em> position.
00706    *  Thus, unlike other containers, a %bitset's index <em>counts from
00707    *  right to left</em>, to put it very loosely.
00708    *
00709    *  This behavior is preserved when translating to and from strings.  For
00710    *  example, the first line of the following program probably prints
00711    *  <em>b(&apos;a&apos;) is 0001100001</em> on a modern ASCII system.
00712    *
00713    *  @code
00714    *     #include <bitset>
00715    *     #include <iostream>
00716    *     #include <sstream>
00717    *
00718    *     using namespace std;
00719    *
00720    *     int main()
00721    *     {
00722    *         long         a = 'a';
00723    *         bitset<10>   b(a);
00724    *
00725    *         cout << "b('a') is " << b << endl;
00726    *
00727    *         ostringstream s;
00728    *         s << b;
00729    *         string  str = s.str();
00730    *         cout << "index 3 in the string is " << str[3] << " but\n"
00731    *              << "index 3 in the bitset is " << b[3] << endl;
00732    *     }
00733    *  @endcode
00734    *
00735    *  Also see:
00736    *  https://gcc.gnu.org/onlinedocs/libstdc++/manual/ext_containers.html
00737    *  for a description of extensions.
00738    *
00739    *  Most of the actual code isn't contained in %bitset<> itself, but in the
00740    *  base class _Base_bitset.  The base class works with whole words, not with
00741    *  individual bits.  This allows us to specialize _Base_bitset for the
00742    *  important special case where the %bitset is only a single word.
00743    *
00744    *  Extra confusion can result due to the fact that the storage for
00745    *  _Base_bitset @e is a regular array, and is indexed as such.  This is
00746    *  carefully encapsulated.
00747   */
00748   template<size_t _Nb>
00749     class bitset
00750     : private _Base_bitset<_GLIBCXX_BITSET_WORDS(_Nb)>
00751     {
00752     private:
00753       typedef _Base_bitset<_GLIBCXX_BITSET_WORDS(_Nb)> _Base;
00754       typedef unsigned long _WordT;
00755 
00756       template<class _CharT, class _Traits, class _Alloc>
00757       void
00758       _M_check_initial_position(const std::basic_string<_CharT, _Traits, _Alloc>& __s,
00759                                 size_t __position) const
00760       {
00761         if (__position > __s.size())
00762           __throw_out_of_range_fmt(__N("bitset::bitset: __position "
00763                                        "(which is %zu) > __s.size() "
00764                                        "(which is %zu)"),
00765                                    __position, __s.size());
00766       }
00767 
00768       void _M_check(size_t __position, const char *__s) const
00769       {
00770         if (__position >= _Nb)
00771           __throw_out_of_range_fmt(__N("%s: __position (which is %zu) "
00772                                        ">= _Nb (which is %zu)"),
00773                                    __s, __position, _Nb);
00774       }
00775 
00776       void
00777       _M_do_sanitize() _GLIBCXX_NOEXCEPT
00778       { 
00779         typedef _Sanitize<_Nb % _GLIBCXX_BITSET_BITS_PER_WORD> __sanitize_type;
00780         __sanitize_type::_S_do_sanitize(this->_M_hiword());
00781       }
00782 
00783 #if __cplusplus >= 201103L
00784       template<typename> friend struct hash;
00785 #endif
00786 
00787     public:
00788       /**
00789        *  This encapsulates the concept of a single bit.  An instance of this
00790        *  class is a proxy for an actual bit; this way the individual bit
00791        *  operations are done as faster word-size bitwise instructions.
00792        *
00793        *  Most users will never need to use this class directly; conversions
00794        *  to and from bool are automatic and should be transparent.  Overloaded
00795        *  operators help to preserve the illusion.
00796        *
00797        *  (On a typical system, this <em>bit %reference</em> is 64
00798        *  times the size of an actual bit.  Ha.)
00799        */
00800       class reference
00801       {
00802         friend class bitset;
00803 
00804         _WordT* _M_wp;
00805         size_t  _M_bpos;
00806         
00807         // left undefined
00808         reference();
00809         
00810       public:
00811         reference(bitset& __b, size_t __pos) _GLIBCXX_NOEXCEPT
00812         {
00813           _M_wp = &__b._M_getword(__pos);
00814           _M_bpos = _Base::_S_whichbit(__pos);
00815         }
00816 
00817         ~reference() _GLIBCXX_NOEXCEPT
00818         { }
00819 
00820         // For b[i] = __x;
00821         reference&
00822         operator=(bool __x) _GLIBCXX_NOEXCEPT
00823         {
00824           if (__x)
00825             *_M_wp |= _Base::_S_maskbit(_M_bpos);
00826           else
00827             *_M_wp &= ~_Base::_S_maskbit(_M_bpos);
00828           return *this;
00829         }
00830 
00831         // For b[i] = b[__j];
00832         reference&
00833         operator=(const reference& __j) _GLIBCXX_NOEXCEPT
00834         {
00835           if ((*(__j._M_wp) & _Base::_S_maskbit(__j._M_bpos)))
00836             *_M_wp |= _Base::_S_maskbit(_M_bpos);
00837           else
00838             *_M_wp &= ~_Base::_S_maskbit(_M_bpos);
00839           return *this;
00840         }
00841 
00842         // Flips the bit
00843         bool
00844         operator~() const _GLIBCXX_NOEXCEPT
00845         { return (*(_M_wp) & _Base::_S_maskbit(_M_bpos)) == 0; }
00846 
00847         // For __x = b[i];
00848         operator bool() const _GLIBCXX_NOEXCEPT
00849         { return (*(_M_wp) & _Base::_S_maskbit(_M_bpos)) != 0; }
00850 
00851         // For b[i].flip();
00852         reference&
00853         flip() _GLIBCXX_NOEXCEPT
00854         {
00855           *_M_wp ^= _Base::_S_maskbit(_M_bpos);
00856           return *this;
00857         }
00858       };
00859       friend class reference;
00860 
00861       // 23.3.5.1 constructors:
00862       /// All bits set to zero.
00863       _GLIBCXX_CONSTEXPR bitset() _GLIBCXX_NOEXCEPT
00864       { }
00865 
00866       /// Initial bits bitwise-copied from a single word (others set to zero).
00867 #if __cplusplus >= 201103L
00868       constexpr bitset(unsigned long long __val) noexcept
00869       : _Base(_Sanitize_val<_Nb>::_S_do_sanitize_val(__val)) { }
00870 #else
00871       bitset(unsigned long __val)
00872       : _Base(__val)
00873       { _M_do_sanitize(); }
00874 #endif
00875 
00876       /**
00877        *  Use a subset of a string.
00878        *  @param  __s  A string of @a 0 and @a 1 characters.
00879        *  @param  __position  Index of the first character in @a __s to use;
00880        *                    defaults to zero.
00881        *  @throw  std::out_of_range  If @a pos is bigger the size of @a __s.
00882        *  @throw  std::invalid_argument  If a character appears in the string
00883        *                                 which is neither @a 0 nor @a 1.
00884        */
00885       template<class _CharT, class _Traits, class _Alloc>
00886         explicit
00887         bitset(const std::basic_string<_CharT, _Traits, _Alloc>& __s,
00888                size_t __position = 0)
00889         : _Base()
00890         {
00891           _M_check_initial_position(__s, __position);
00892           _M_copy_from_string(__s, __position,
00893                               std::basic_string<_CharT, _Traits, _Alloc>::npos,
00894                               _CharT('0'), _CharT('1'));
00895         }
00896 
00897       /**
00898        *  Use a subset of a string.
00899        *  @param  __s  A string of @a 0 and @a 1 characters.
00900        *  @param  __position  Index of the first character in @a __s to use.
00901        *  @param  __n    The number of characters to copy.
00902        *  @throw std::out_of_range If @a __position is bigger the size
00903        *  of @a __s.
00904        *  @throw  std::invalid_argument  If a character appears in the string
00905        *                                 which is neither @a 0 nor @a 1.
00906        */
00907       template<class _CharT, class _Traits, class _Alloc>
00908         bitset(const std::basic_string<_CharT, _Traits, _Alloc>& __s,
00909                size_t __position, size_t __n)
00910         : _Base()
00911         {
00912           _M_check_initial_position(__s, __position);
00913           _M_copy_from_string(__s, __position, __n, _CharT('0'), _CharT('1'));
00914         }
00915 
00916       // _GLIBCXX_RESOLVE_LIB_DEFECTS
00917       // 396. what are characters zero and one.
00918       template<class _CharT, class _Traits, class _Alloc>
00919         bitset(const std::basic_string<_CharT, _Traits, _Alloc>& __s,
00920                size_t __position, size_t __n,
00921                _CharT __zero, _CharT __one = _CharT('1'))
00922         : _Base()
00923         {
00924           _M_check_initial_position(__s, __position);
00925           _M_copy_from_string(__s, __position, __n, __zero, __one);
00926         }
00927 
00928 #if __cplusplus >= 201103L
00929       /**
00930        *  Construct from a character %array.
00931        *  @param  __str  An %array of characters @a zero and @a one.
00932        *  @param  __n    The number of characters to use.
00933        *  @param  __zero The character corresponding to the value 0.
00934        *  @param  __one  The character corresponding to the value 1.
00935        *  @throw  std::invalid_argument If a character appears in the string
00936        *                                which is neither @a __zero nor @a __one.
00937        */
00938       template<typename _CharT>
00939         explicit
00940         bitset(const _CharT* __str,
00941                typename std::basic_string<_CharT>::size_type __n
00942                = std::basic_string<_CharT>::npos,
00943                _CharT __zero = _CharT('0'), _CharT __one = _CharT('1'))
00944         : _Base()
00945         {
00946           if (!__str)
00947             __throw_logic_error(__N("bitset::bitset(const _CharT*, ...)"));
00948 
00949           if (__n == std::basic_string<_CharT>::npos)
00950             __n = std::char_traits<_CharT>::length(__str);
00951           _M_copy_from_ptr<_CharT, std::char_traits<_CharT>>(__str, __n, 0,
00952                                                              __n, __zero,
00953                                                              __one);
00954         }
00955 #endif
00956 
00957       // 23.3.5.2 bitset operations:
00958       //@{
00959       /**
00960        *  Operations on bitsets.
00961        *  @param  __rhs  A same-sized bitset.
00962        *
00963        *  These should be self-explanatory.
00964        */
00965       bitset<_Nb>&
00966       operator&=(const bitset<_Nb>& __rhs) _GLIBCXX_NOEXCEPT
00967       {
00968         this->_M_do_and(__rhs);
00969         return *this;
00970       }
00971 
00972       bitset<_Nb>&
00973       operator|=(const bitset<_Nb>& __rhs) _GLIBCXX_NOEXCEPT
00974       {
00975         this->_M_do_or(__rhs);
00976         return *this;
00977       }
00978 
00979       bitset<_Nb>&
00980       operator^=(const bitset<_Nb>& __rhs) _GLIBCXX_NOEXCEPT
00981       {
00982         this->_M_do_xor(__rhs);
00983         return *this;
00984       }
00985       //@}
00986       
00987       //@{
00988       /**
00989        *  Operations on bitsets.
00990        *  @param  __position  The number of places to shift.
00991        *
00992        *  These should be self-explanatory.
00993        */
00994       bitset<_Nb>&
00995       operator<<=(size_t __position) _GLIBCXX_NOEXCEPT
00996       {
00997         if (__builtin_expect(__position < _Nb, 1))
00998           {
00999             this->_M_do_left_shift(__position);
01000             this->_M_do_sanitize();
01001           }
01002         else
01003           this->_M_do_reset();
01004         return *this;
01005       }
01006 
01007       bitset<_Nb>&
01008       operator>>=(size_t __position) _GLIBCXX_NOEXCEPT
01009       {
01010         if (__builtin_expect(__position < _Nb, 1))
01011           {
01012             this->_M_do_right_shift(__position);
01013             this->_M_do_sanitize();
01014           }
01015         else
01016           this->_M_do_reset();
01017         return *this;
01018       }
01019       //@}
01020       
01021       //@{
01022       /**
01023        *  These versions of single-bit set, reset, flip, and test are
01024        *  extensions from the SGI version.  They do no range checking.
01025        *  @ingroup SGIextensions
01026        */
01027       bitset<_Nb>&
01028       _Unchecked_set(size_t __pos) _GLIBCXX_NOEXCEPT
01029       {
01030         this->_M_getword(__pos) |= _Base::_S_maskbit(__pos);
01031         return *this;
01032       }
01033 
01034       bitset<_Nb>&
01035       _Unchecked_set(size_t __pos, int __val) _GLIBCXX_NOEXCEPT
01036       {
01037         if (__val)
01038           this->_M_getword(__pos) |= _Base::_S_maskbit(__pos);
01039         else
01040           this->_M_getword(__pos) &= ~_Base::_S_maskbit(__pos);
01041         return *this;
01042       }
01043 
01044       bitset<_Nb>&
01045       _Unchecked_reset(size_t __pos) _GLIBCXX_NOEXCEPT
01046       {
01047         this->_M_getword(__pos) &= ~_Base::_S_maskbit(__pos);
01048         return *this;
01049       }
01050 
01051       bitset<_Nb>&
01052       _Unchecked_flip(size_t __pos) _GLIBCXX_NOEXCEPT
01053       {
01054         this->_M_getword(__pos) ^= _Base::_S_maskbit(__pos);
01055         return *this;
01056       }
01057 
01058       _GLIBCXX_CONSTEXPR bool
01059       _Unchecked_test(size_t __pos) const _GLIBCXX_NOEXCEPT
01060       { return ((this->_M_getword(__pos) & _Base::_S_maskbit(__pos))
01061                 != static_cast<_WordT>(0)); }
01062       //@}
01063       
01064       // Set, reset, and flip.
01065       /**
01066        *  @brief Sets every bit to true.
01067        */
01068       bitset<_Nb>&
01069       set() _GLIBCXX_NOEXCEPT
01070       {
01071         this->_M_do_set();
01072         this->_M_do_sanitize();
01073         return *this;
01074       }
01075 
01076       /**
01077        *  @brief Sets a given bit to a particular value.
01078        *  @param  __position  The index of the bit.
01079        *  @param  __val  Either true or false, defaults to true.
01080        *  @throw  std::out_of_range  If @a pos is bigger the size of the %set.
01081        */
01082       bitset<_Nb>&
01083       set(size_t __position, bool __val = true)
01084       {
01085         this->_M_check(__position, __N("bitset::set"));
01086         return _Unchecked_set(__position, __val);
01087       }
01088 
01089       /**
01090        *  @brief Sets every bit to false.
01091        */
01092       bitset<_Nb>&
01093       reset() _GLIBCXX_NOEXCEPT
01094       {
01095         this->_M_do_reset();
01096         return *this;
01097       }
01098 
01099       /**
01100        *  @brief Sets a given bit to false.
01101        *  @param  __position  The index of the bit.
01102        *  @throw  std::out_of_range  If @a pos is bigger the size of the %set.
01103        *
01104        *  Same as writing @c set(pos,false).
01105        */
01106       bitset<_Nb>&
01107       reset(size_t __position)
01108       {
01109         this->_M_check(__position, __N("bitset::reset"));
01110         return _Unchecked_reset(__position);
01111       }
01112       
01113       /**
01114        *  @brief Toggles every bit to its opposite value.
01115        */
01116       bitset<_Nb>&
01117       flip() _GLIBCXX_NOEXCEPT
01118       {
01119         this->_M_do_flip();
01120         this->_M_do_sanitize();
01121         return *this;
01122       }
01123 
01124       /**
01125        *  @brief Toggles a given bit to its opposite value.
01126        *  @param  __position  The index of the bit.
01127        *  @throw  std::out_of_range  If @a pos is bigger the size of the %set.
01128        */
01129       bitset<_Nb>&
01130       flip(size_t __position)
01131       {
01132         this->_M_check(__position, __N("bitset::flip"));
01133         return _Unchecked_flip(__position);
01134       }
01135       
01136       /// See the no-argument flip().
01137       bitset<_Nb>
01138       operator~() const _GLIBCXX_NOEXCEPT
01139       { return bitset<_Nb>(*this).flip(); }
01140 
01141       //@{
01142       /**
01143        *  @brief  Array-indexing support.
01144        *  @param  __position  Index into the %bitset.
01145        *  @return A bool for a <em>const %bitset</em>.  For non-const
01146        *           bitsets, an instance of the reference proxy class.
01147        *  @note  These operators do no range checking and throw no exceptions,
01148        *         as required by DR 11 to the standard.
01149        *
01150        *  _GLIBCXX_RESOLVE_LIB_DEFECTS Note that this implementation already
01151        *  resolves DR 11 (items 1 and 2), but does not do the range-checking
01152        *  required by that DR's resolution.  -pme
01153        *  The DR has since been changed:  range-checking is a precondition
01154        *  (users' responsibility), and these functions must not throw.  -pme
01155        */
01156       reference
01157       operator[](size_t __position)
01158       { return reference(*this, __position); }
01159 
01160       _GLIBCXX_CONSTEXPR bool
01161       operator[](size_t __position) const
01162       { return _Unchecked_test(__position); }
01163       //@}
01164       
01165       /**
01166        *  @brief Returns a numerical interpretation of the %bitset.
01167        *  @return  The integral equivalent of the bits.
01168        *  @throw  std::overflow_error  If there are too many bits to be
01169        *                               represented in an @c unsigned @c long.
01170        */
01171       unsigned long
01172       to_ulong() const
01173       { return this->_M_do_to_ulong(); }
01174 
01175 #if __cplusplus >= 201103L
01176       unsigned long long
01177       to_ullong() const
01178       { return this->_M_do_to_ullong(); }
01179 #endif
01180 
01181       /**
01182        *  @brief Returns a character interpretation of the %bitset.
01183        *  @return  The string equivalent of the bits.
01184        *
01185        *  Note the ordering of the bits:  decreasing character positions
01186        *  correspond to increasing bit positions (see the main class notes for
01187        *  an example).
01188        */
01189       template<class _CharT, class _Traits, class _Alloc>
01190         std::basic_string<_CharT, _Traits, _Alloc>
01191         to_string() const
01192         {
01193           std::basic_string<_CharT, _Traits, _Alloc> __result;
01194           _M_copy_to_string(__result, _CharT('0'), _CharT('1'));
01195           return __result;
01196         }
01197 
01198       // _GLIBCXX_RESOLVE_LIB_DEFECTS
01199       // 396. what are characters zero and one.
01200       template<class _CharT, class _Traits, class _Alloc>
01201         std::basic_string<_CharT, _Traits, _Alloc>
01202         to_string(_CharT __zero, _CharT __one = _CharT('1')) const
01203         {
01204           std::basic_string<_CharT, _Traits, _Alloc> __result;
01205           _M_copy_to_string(__result, __zero, __one);
01206           return __result;
01207         }
01208 
01209       // _GLIBCXX_RESOLVE_LIB_DEFECTS
01210       // 434. bitset::to_string() hard to use.
01211       template<class _CharT, class _Traits>
01212         std::basic_string<_CharT, _Traits, std::allocator<_CharT> >
01213         to_string() const
01214         { return to_string<_CharT, _Traits, std::allocator<_CharT> >(); }
01215 
01216       // _GLIBCXX_RESOLVE_LIB_DEFECTS
01217       // 853. to_string needs updating with zero and one.
01218       template<class _CharT, class _Traits>
01219         std::basic_string<_CharT, _Traits, std::allocator<_CharT> >
01220         to_string(_CharT __zero, _CharT __one = _CharT('1')) const
01221         { return to_string<_CharT, _Traits,
01222                            std::allocator<_CharT> >(__zero, __one); }
01223 
01224       template<class _CharT>
01225         std::basic_string<_CharT, std::char_traits<_CharT>,
01226                           std::allocator<_CharT> >
01227         to_string() const
01228         {
01229           return to_string<_CharT, std::char_traits<_CharT>,
01230                            std::allocator<_CharT> >();
01231         }
01232 
01233       template<class _CharT>
01234         std::basic_string<_CharT, std::char_traits<_CharT>,
01235                           std::allocator<_CharT> >
01236         to_string(_CharT __zero, _CharT __one = _CharT('1')) const
01237         {
01238           return to_string<_CharT, std::char_traits<_CharT>,
01239                            std::allocator<_CharT> >(__zero, __one);
01240         }
01241 
01242       std::basic_string<char, std::char_traits<char>, std::allocator<char> >
01243       to_string() const
01244       {
01245         return to_string<char, std::char_traits<char>,
01246                          std::allocator<char> >();
01247       }
01248 
01249       std::basic_string<char, std::char_traits<char>, std::allocator<char> >
01250       to_string(char __zero, char __one = '1') const
01251       {
01252         return to_string<char, std::char_traits<char>,
01253                          std::allocator<char> >(__zero, __one);
01254       }
01255 
01256       // Helper functions for string operations.
01257       template<class _CharT, class _Traits>
01258         void
01259         _M_copy_from_ptr(const _CharT*, size_t, size_t, size_t,
01260                          _CharT, _CharT);
01261 
01262       template<class _CharT, class _Traits, class _Alloc>
01263         void
01264         _M_copy_from_string(const std::basic_string<_CharT,
01265                             _Traits, _Alloc>& __s, size_t __pos, size_t __n,
01266                             _CharT __zero, _CharT __one)
01267         { _M_copy_from_ptr<_CharT, _Traits>(__s.data(), __s.size(), __pos, __n,
01268                                             __zero, __one); }
01269 
01270       template<class _CharT, class _Traits, class _Alloc>
01271         void
01272         _M_copy_to_string(std::basic_string<_CharT, _Traits, _Alloc>&,
01273                           _CharT, _CharT) const;
01274 
01275       // NB: Backward compat.
01276       template<class _CharT, class _Traits, class _Alloc>
01277         void
01278         _M_copy_from_string(const std::basic_string<_CharT,
01279                             _Traits, _Alloc>& __s, size_t __pos, size_t __n)
01280         { _M_copy_from_string(__s, __pos, __n, _CharT('0'), _CharT('1')); }
01281 
01282       template<class _CharT, class _Traits, class _Alloc>
01283         void
01284         _M_copy_to_string(std::basic_string<_CharT, _Traits,_Alloc>& __s) const
01285         { _M_copy_to_string(__s, _CharT('0'), _CharT('1')); }
01286 
01287       /// Returns the number of bits which are set.
01288       size_t
01289       count() const _GLIBCXX_NOEXCEPT
01290       { return this->_M_do_count(); }
01291 
01292       /// Returns the total number of bits.
01293       _GLIBCXX_CONSTEXPR size_t
01294       size() const _GLIBCXX_NOEXCEPT
01295       { return _Nb; }
01296 
01297       //@{
01298       /// These comparisons for equality/inequality are, well, @e bitwise.
01299       bool
01300       operator==(const bitset<_Nb>& __rhs) const _GLIBCXX_NOEXCEPT
01301       { return this->_M_is_equal(__rhs); }
01302 
01303       bool
01304       operator!=(const bitset<_Nb>& __rhs) const _GLIBCXX_NOEXCEPT
01305       { return !this->_M_is_equal(__rhs); }
01306       //@}
01307       
01308       /**
01309        *  @brief Tests the value of a bit.
01310        *  @param  __position  The index of a bit.
01311        *  @return  The value at @a pos.
01312        *  @throw  std::out_of_range  If @a pos is bigger the size of the %set.
01313        */
01314       bool
01315       test(size_t __position) const
01316       {
01317         this->_M_check(__position, __N("bitset::test"));
01318         return _Unchecked_test(__position);
01319       }
01320 
01321       // _GLIBCXX_RESOLVE_LIB_DEFECTS
01322       // DR 693. std::bitset::all() missing.
01323       /**
01324        *  @brief Tests whether all the bits are on.
01325        *  @return  True if all the bits are set.
01326        */
01327       bool
01328       all() const _GLIBCXX_NOEXCEPT
01329       { return this->template _M_are_all<_Nb>(); }
01330 
01331       /**
01332        *  @brief Tests whether any of the bits are on.
01333        *  @return  True if at least one bit is set.
01334        */
01335       bool
01336       any() const _GLIBCXX_NOEXCEPT
01337       { return this->_M_is_any(); }
01338 
01339       /**
01340        *  @brief Tests whether any of the bits are on.
01341        *  @return  True if none of the bits are set.
01342        */
01343       bool
01344       none() const _GLIBCXX_NOEXCEPT
01345       { return !this->_M_is_any(); }
01346 
01347       //@{
01348       /// Self-explanatory.
01349       bitset<_Nb>
01350       operator<<(size_t __position) const _GLIBCXX_NOEXCEPT
01351       { return bitset<_Nb>(*this) <<= __position; }
01352 
01353       bitset<_Nb>
01354       operator>>(size_t __position) const _GLIBCXX_NOEXCEPT
01355       { return bitset<_Nb>(*this) >>= __position; }
01356       //@}
01357       
01358       /**
01359        *  @brief  Finds the index of the first "on" bit.
01360        *  @return  The index of the first bit set, or size() if not found.
01361        *  @ingroup SGIextensions
01362        *  @sa  _Find_next
01363        */
01364       size_t
01365       _Find_first() const _GLIBCXX_NOEXCEPT
01366       { return this->_M_do_find_first(_Nb); }
01367 
01368       /**
01369        *  @brief  Finds the index of the next "on" bit after prev.
01370        *  @return  The index of the next bit set, or size() if not found.
01371        *  @param  __prev  Where to start searching.
01372        *  @ingroup SGIextensions
01373        *  @sa  _Find_first
01374        */
01375       size_t
01376       _Find_next(size_t __prev) const _GLIBCXX_NOEXCEPT
01377       { return this->_M_do_find_next(__prev, _Nb); }
01378     };
01379 
01380   // Definitions of non-inline member functions.
01381   template<size_t _Nb>
01382     template<class _CharT, class _Traits>
01383       void
01384       bitset<_Nb>::
01385       _M_copy_from_ptr(const _CharT* __s, size_t __len,
01386                        size_t __pos, size_t __n, _CharT __zero, _CharT __one)
01387       {
01388         reset();
01389         const size_t __nbits = std::min(_Nb, std::min(__n, size_t(__len - __pos)));
01390         for (size_t __i = __nbits; __i > 0; --__i)
01391           {
01392             const _CharT __c = __s[__pos + __nbits - __i];
01393             if (_Traits::eq(__c, __zero))
01394               ;
01395             else if (_Traits::eq(__c, __one))
01396               _Unchecked_set(__i - 1);
01397             else
01398               __throw_invalid_argument(__N("bitset::_M_copy_from_ptr"));
01399           }
01400       }
01401 
01402   template<size_t _Nb>
01403     template<class _CharT, class _Traits, class _Alloc>
01404       void
01405       bitset<_Nb>::
01406       _M_copy_to_string(std::basic_string<_CharT, _Traits, _Alloc>& __s,
01407                         _CharT __zero, _CharT __one) const
01408       {
01409         __s.assign(_Nb, __zero);
01410         for (size_t __i = _Nb; __i > 0; --__i)
01411           if (_Unchecked_test(__i - 1))
01412             _Traits::assign(__s[_Nb - __i], __one);
01413       }
01414 
01415   // 23.3.5.3 bitset operations:
01416   //@{
01417   /**
01418    *  @brief  Global bitwise operations on bitsets.
01419    *  @param  __x  A bitset.
01420    *  @param  __y  A bitset of the same size as @a __x.
01421    *  @return  A new bitset.
01422    *
01423    *  These should be self-explanatory.
01424   */
01425   template<size_t _Nb>
01426     inline bitset<_Nb>
01427     operator&(const bitset<_Nb>& __x, const bitset<_Nb>& __y) _GLIBCXX_NOEXCEPT
01428     {
01429       bitset<_Nb> __result(__x);
01430       __result &= __y;
01431       return __result;
01432     }
01433 
01434   template<size_t _Nb>
01435     inline bitset<_Nb>
01436     operator|(const bitset<_Nb>& __x, const bitset<_Nb>& __y) _GLIBCXX_NOEXCEPT
01437     {
01438       bitset<_Nb> __result(__x);
01439       __result |= __y;
01440       return __result;
01441     }
01442 
01443   template <size_t _Nb>
01444     inline bitset<_Nb>
01445     operator^(const bitset<_Nb>& __x, const bitset<_Nb>& __y) _GLIBCXX_NOEXCEPT
01446     {
01447       bitset<_Nb> __result(__x);
01448       __result ^= __y;
01449       return __result;
01450     }
01451   //@}
01452 
01453   //@{
01454   /**
01455    *  @brief Global I/O operators for bitsets.
01456    *
01457    *  Direct I/O between streams and bitsets is supported.  Output is
01458    *  straightforward.  Input will skip whitespace, only accept @a 0 and @a 1
01459    *  characters, and will only extract as many digits as the %bitset will
01460    *  hold.
01461   */
01462   template<class _CharT, class _Traits, size_t _Nb>
01463     std::basic_istream<_CharT, _Traits>&
01464     operator>>(std::basic_istream<_CharT, _Traits>& __is, bitset<_Nb>& __x)
01465     {
01466       typedef typename _Traits::char_type          char_type;
01467       typedef std::basic_istream<_CharT, _Traits>  __istream_type;
01468       typedef typename __istream_type::ios_base    __ios_base;
01469 
01470       std::basic_string<_CharT, _Traits> __tmp;
01471       __tmp.reserve(_Nb);
01472 
01473       // _GLIBCXX_RESOLVE_LIB_DEFECTS
01474       // 303. Bitset input operator underspecified
01475       const char_type __zero = __is.widen('0');
01476       const char_type __one = __is.widen('1');
01477 
01478       typename __ios_base::iostate __state = __ios_base::goodbit;
01479       typename __istream_type::sentry __sentry(__is);
01480       if (__sentry)
01481         {
01482           __try
01483             {
01484               for (size_t __i = _Nb; __i > 0; --__i)
01485                 {
01486                   static typename _Traits::int_type __eof = _Traits::eof();
01487                   
01488                   typename _Traits::int_type __c1 = __is.rdbuf()->sbumpc();
01489                   if (_Traits::eq_int_type(__c1, __eof))
01490                     {
01491                       __state |= __ios_base::eofbit;
01492                       break;
01493                     }
01494                   else
01495                     {
01496                       const char_type __c2 = _Traits::to_char_type(__c1);
01497                       if (_Traits::eq(__c2, __zero))
01498                         __tmp.push_back(__zero);
01499                       else if (_Traits::eq(__c2, __one))
01500                         __tmp.push_back(__one);
01501                       else if (_Traits::
01502                                eq_int_type(__is.rdbuf()->sputbackc(__c2),
01503                                            __eof))
01504                         {
01505                           __state |= __ios_base::failbit;
01506                           break;
01507                         }
01508                     }
01509                 }
01510             }
01511           __catch(__cxxabiv1::__forced_unwind&)
01512             {
01513               __is._M_setstate(__ios_base::badbit);             
01514               __throw_exception_again;
01515             }
01516           __catch(...)
01517             { __is._M_setstate(__ios_base::badbit); }
01518         }
01519 
01520       if (__tmp.empty() && _Nb)
01521         __state |= __ios_base::failbit;
01522       else
01523         __x._M_copy_from_string(__tmp, static_cast<size_t>(0), _Nb,
01524                                 __zero, __one);
01525       if (__state)
01526         __is.setstate(__state);
01527       return __is;
01528     }
01529 
01530   template <class _CharT, class _Traits, size_t _Nb>
01531     std::basic_ostream<_CharT, _Traits>&
01532     operator<<(std::basic_ostream<_CharT, _Traits>& __os,
01533                const bitset<_Nb>& __x)
01534     {
01535       std::basic_string<_CharT, _Traits> __tmp;
01536 
01537       // _GLIBCXX_RESOLVE_LIB_DEFECTS
01538       // 396. what are characters zero and one.
01539       const ctype<_CharT>& __ct = use_facet<ctype<_CharT> >(__os.getloc());
01540       __x._M_copy_to_string(__tmp, __ct.widen('0'), __ct.widen('1'));
01541       return __os << __tmp;
01542     }
01543   //@}
01544 
01545 _GLIBCXX_END_NAMESPACE_CONTAINER
01546 } // namespace std
01547 
01548 #undef _GLIBCXX_BITSET_WORDS
01549 #undef _GLIBCXX_BITSET_BITS_PER_WORD
01550 #undef _GLIBCXX_BITSET_BITS_PER_ULL
01551 
01552 #if __cplusplus >= 201103L
01553 
01554 #include <bits/functional_hash.h>
01555 
01556 namespace std _GLIBCXX_VISIBILITY(default)
01557 {
01558 _GLIBCXX_BEGIN_NAMESPACE_VERSION
01559 
01560   // DR 1182.
01561   /// std::hash specialization for bitset.
01562   template<size_t _Nb>
01563     struct hash<_GLIBCXX_STD_C::bitset<_Nb>>
01564     : public __hash_base<size_t, _GLIBCXX_STD_C::bitset<_Nb>>
01565     {
01566       size_t
01567       operator()(const _GLIBCXX_STD_C::bitset<_Nb>& __b) const noexcept
01568       {
01569         const size_t __clength = (_Nb + __CHAR_BIT__ - 1) / __CHAR_BIT__;
01570         return std::_Hash_impl::hash(__b._M_getdata(), __clength);
01571       }
01572     };
01573 
01574   template<>
01575     struct hash<_GLIBCXX_STD_C::bitset<0>>
01576     : public __hash_base<size_t, _GLIBCXX_STD_C::bitset<0>>
01577     {
01578       size_t
01579       operator()(const _GLIBCXX_STD_C::bitset<0>&) const noexcept
01580       { return 0; }
01581     };
01582 
01583 _GLIBCXX_END_NAMESPACE_VERSION
01584 } // namespace
01585 
01586 #endif // C++11
01587 
01588 #ifdef _GLIBCXX_DEBUG
01589 # include <debug/bitset>
01590 #endif
01591 
01592 #ifdef _GLIBCXX_PROFILE
01593 # include <profile/bitset>
01594 #endif
01595 
01596 #endif /* _GLIBCXX_BITSET */