Home | History | Annotate | Line # | Download | only in std
      1       1.1  mrg // <bitset> -*- C++ -*-
      2       1.1  mrg 
      3  1.1.1.14  mrg // Copyright (C) 2001-2024 Free Software Foundation, Inc.
      4       1.1  mrg //
      5       1.1  mrg // This file is part of the GNU ISO C++ Library.  This library is free
      6       1.1  mrg // software; you can redistribute it and/or modify it under the
      7       1.1  mrg // terms of the GNU General Public License as published by the
      8       1.1  mrg // Free Software Foundation; either version 3, or (at your option)
      9       1.1  mrg // any later version.
     10       1.1  mrg 
     11       1.1  mrg // This library is distributed in the hope that it will be useful,
     12       1.1  mrg // but WITHOUT ANY WARRANTY; without even the implied warranty of
     13       1.1  mrg // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     14       1.1  mrg // GNU General Public License for more details.
     15       1.1  mrg 
     16       1.1  mrg // Under Section 7 of GPL version 3, you are granted additional
     17       1.1  mrg // permissions described in the GCC Runtime Library Exception, version
     18       1.1  mrg // 3.1, as published by the Free Software Foundation.
     19       1.1  mrg 
     20       1.1  mrg // You should have received a copy of the GNU General Public License and
     21       1.1  mrg // a copy of the GCC Runtime Library Exception along with this program;
     22       1.1  mrg // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
     23       1.1  mrg // <http://www.gnu.org/licenses/>.
     24       1.1  mrg 
     25       1.1  mrg /*
     26       1.1  mrg  * Copyright (c) 1998
     27       1.1  mrg  * Silicon Graphics Computer Systems, Inc.
     28       1.1  mrg  *
     29       1.1  mrg  * Permission to use, copy, modify, distribute and sell this software
     30       1.1  mrg  * and its documentation for any purpose is hereby granted without fee,
     31       1.1  mrg  * provided that the above copyright notice appear in all copies and
     32       1.1  mrg  * that both that copyright notice and this permission notice appear
     33       1.1  mrg  * in supporting documentation.  Silicon Graphics makes no
     34       1.1  mrg  * representations about the suitability of this software for any
     35       1.1  mrg  * purpose.  It is provided "as is" without express or implied warranty.
     36       1.1  mrg  */
     37       1.1  mrg 
     38       1.1  mrg /** @file include/bitset
     39       1.1  mrg  *  This is a Standard C++ Library header.
     40       1.1  mrg  */
     41       1.1  mrg 
     42       1.1  mrg #ifndef _GLIBCXX_BITSET
     43       1.1  mrg #define _GLIBCXX_BITSET 1
     44       1.1  mrg 
     45       1.1  mrg #pragma GCC system_header
     46       1.1  mrg 
     47       1.1  mrg #include <bits/functexcept.h>   // For invalid_argument, out_of_range,
     48       1.1  mrg                                 // overflow_error
     49  1.1.1.14  mrg #include <bits/stl_algobase.h>  // For std::fill
     50  1.1.1.14  mrg 
     51  1.1.1.14  mrg #if _GLIBCXX_HOSTED
     52  1.1.1.14  mrg # include <string>
     53  1.1.1.14  mrg # include <iosfwd>
     54  1.1.1.14  mrg # include <bits/cxxabi_forced.h>
     55  1.1.1.14  mrg #endif
     56       1.1  mrg 
     57   1.1.1.9  mrg #if __cplusplus >= 201103L
     58   1.1.1.9  mrg # include <bits/functional_hash.h>
     59   1.1.1.9  mrg #endif
     60   1.1.1.9  mrg 
     61  1.1.1.14  mrg #define __glibcxx_want_constexpr_bitset
     62  1.1.1.14  mrg #include <bits/version.h>
     63  1.1.1.14  mrg 
     64   1.1.1.2  mrg #define _GLIBCXX_BITSET_BITS_PER_WORD  (__CHAR_BIT__ * __SIZEOF_LONG__)
     65       1.1  mrg #define _GLIBCXX_BITSET_WORDS(__n) \
     66   1.1.1.2  mrg   ((__n) / _GLIBCXX_BITSET_BITS_PER_WORD + \
     67   1.1.1.2  mrg    ((__n) % _GLIBCXX_BITSET_BITS_PER_WORD == 0 ? 0 : 1))
     68       1.1  mrg 
     69   1.1.1.2  mrg #define _GLIBCXX_BITSET_BITS_PER_ULL (__CHAR_BIT__ * __SIZEOF_LONG_LONG__)
     70   1.1.1.2  mrg 
     71   1.1.1.2  mrg namespace std _GLIBCXX_VISIBILITY(default)
     72   1.1.1.2  mrg {
     73   1.1.1.2  mrg _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
     74       1.1  mrg 
     75       1.1  mrg   /**
     76       1.1  mrg    *  Base class, general case.  It is a class invariant that _Nw will be
     77       1.1  mrg    *  nonnegative.
     78       1.1  mrg    *
     79       1.1  mrg    *  See documentation for bitset.
     80       1.1  mrg   */
     81       1.1  mrg   template<size_t _Nw>
     82       1.1  mrg     struct _Base_bitset
     83       1.1  mrg     {
     84       1.1  mrg       typedef unsigned long _WordT;
     85       1.1  mrg 
     86       1.1  mrg       /// 0 is the least significant word.
     87       1.1  mrg       _WordT 		_M_w[_Nw];
     88       1.1  mrg 
     89   1.1.1.2  mrg       _GLIBCXX_CONSTEXPR _Base_bitset() _GLIBCXX_NOEXCEPT
     90   1.1.1.2  mrg       : _M_w() { }
     91       1.1  mrg 
     92   1.1.1.2  mrg #if __cplusplus >= 201103L
     93   1.1.1.2  mrg       constexpr _Base_bitset(unsigned long long __val) noexcept
     94   1.1.1.2  mrg       : _M_w{ _WordT(__val)
     95   1.1.1.2  mrg #if __SIZEOF_LONG_LONG__ > __SIZEOF_LONG__
     96   1.1.1.2  mrg 	       , _WordT(__val >> _GLIBCXX_BITSET_BITS_PER_WORD)
     97   1.1.1.2  mrg #endif
     98   1.1.1.2  mrg        } { }
     99       1.1  mrg #else
    100       1.1  mrg       _Base_bitset(unsigned long __val)
    101   1.1.1.2  mrg       : _M_w()
    102   1.1.1.2  mrg       { _M_w[0] = __val; }
    103       1.1  mrg #endif
    104       1.1  mrg 
    105   1.1.1.2  mrg       static _GLIBCXX_CONSTEXPR size_t
    106   1.1.1.2  mrg       _S_whichword(size_t __pos) _GLIBCXX_NOEXCEPT
    107       1.1  mrg       { return __pos / _GLIBCXX_BITSET_BITS_PER_WORD; }
    108       1.1  mrg 
    109   1.1.1.2  mrg       static _GLIBCXX_CONSTEXPR size_t
    110   1.1.1.2  mrg       _S_whichbyte(size_t __pos) _GLIBCXX_NOEXCEPT
    111       1.1  mrg       { return (__pos % _GLIBCXX_BITSET_BITS_PER_WORD) / __CHAR_BIT__; }
    112       1.1  mrg 
    113   1.1.1.2  mrg       static _GLIBCXX_CONSTEXPR size_t
    114   1.1.1.2  mrg       _S_whichbit(size_t __pos) _GLIBCXX_NOEXCEPT
    115       1.1  mrg       { return __pos % _GLIBCXX_BITSET_BITS_PER_WORD; }
    116       1.1  mrg 
    117   1.1.1.2  mrg       static _GLIBCXX_CONSTEXPR _WordT
    118   1.1.1.2  mrg       _S_maskbit(size_t __pos) _GLIBCXX_NOEXCEPT
    119       1.1  mrg       { return (static_cast<_WordT>(1)) << _S_whichbit(__pos); }
    120       1.1  mrg 
    121  1.1.1.14  mrg       _GLIBCXX14_CONSTEXPR _WordT&
    122   1.1.1.2  mrg       _M_getword(size_t __pos) _GLIBCXX_NOEXCEPT
    123       1.1  mrg       { return _M_w[_S_whichword(__pos)]; }
    124       1.1  mrg 
    125   1.1.1.2  mrg       _GLIBCXX_CONSTEXPR _WordT
    126   1.1.1.2  mrg       _M_getword(size_t __pos) const _GLIBCXX_NOEXCEPT
    127       1.1  mrg       { return _M_w[_S_whichword(__pos)]; }
    128       1.1  mrg 
    129   1.1.1.2  mrg #if __cplusplus >= 201103L
    130  1.1.1.14  mrg       constexpr const _WordT*
    131   1.1.1.2  mrg       _M_getdata() const noexcept
    132       1.1  mrg       { return _M_w; }
    133       1.1  mrg #endif
    134       1.1  mrg 
    135  1.1.1.14  mrg       _GLIBCXX23_CONSTEXPR _WordT&
    136   1.1.1.2  mrg       _M_hiword() _GLIBCXX_NOEXCEPT
    137       1.1  mrg       { return _M_w[_Nw - 1]; }
    138       1.1  mrg 
    139   1.1.1.2  mrg       _GLIBCXX_CONSTEXPR _WordT
    140   1.1.1.2  mrg       _M_hiword() const _GLIBCXX_NOEXCEPT
    141       1.1  mrg       { return _M_w[_Nw - 1]; }
    142       1.1  mrg 
    143  1.1.1.14  mrg       _GLIBCXX23_CONSTEXPR void
    144   1.1.1.2  mrg       _M_do_and(const _Base_bitset<_Nw>& __x) _GLIBCXX_NOEXCEPT
    145       1.1  mrg       {
    146       1.1  mrg 	for (size_t __i = 0; __i < _Nw; __i++)
    147       1.1  mrg 	  _M_w[__i] &= __x._M_w[__i];
    148       1.1  mrg       }
    149       1.1  mrg 
    150  1.1.1.14  mrg       _GLIBCXX14_CONSTEXPR void
    151   1.1.1.2  mrg       _M_do_or(const _Base_bitset<_Nw>& __x) _GLIBCXX_NOEXCEPT
    152       1.1  mrg       {
    153       1.1  mrg 	for (size_t __i = 0; __i < _Nw; __i++)
    154       1.1  mrg 	  _M_w[__i] |= __x._M_w[__i];
    155       1.1  mrg       }
    156       1.1  mrg 
    157  1.1.1.14  mrg       _GLIBCXX14_CONSTEXPR void
    158   1.1.1.2  mrg       _M_do_xor(const _Base_bitset<_Nw>& __x) _GLIBCXX_NOEXCEPT
    159       1.1  mrg       {
    160       1.1  mrg 	for (size_t __i = 0; __i < _Nw; __i++)
    161       1.1  mrg 	  _M_w[__i] ^= __x._M_w[__i];
    162       1.1  mrg       }
    163       1.1  mrg 
    164  1.1.1.14  mrg       _GLIBCXX14_CONSTEXPR void
    165   1.1.1.2  mrg       _M_do_left_shift(size_t __shift) _GLIBCXX_NOEXCEPT;
    166       1.1  mrg 
    167  1.1.1.14  mrg       _GLIBCXX14_CONSTEXPR void
    168   1.1.1.2  mrg       _M_do_right_shift(size_t __shift) _GLIBCXX_NOEXCEPT;
    169       1.1  mrg 
    170  1.1.1.14  mrg       _GLIBCXX14_CONSTEXPR void
    171   1.1.1.2  mrg       _M_do_flip() _GLIBCXX_NOEXCEPT
    172       1.1  mrg       {
    173       1.1  mrg 	for (size_t __i = 0; __i < _Nw; __i++)
    174       1.1  mrg 	  _M_w[__i] = ~_M_w[__i];
    175       1.1  mrg       }
    176       1.1  mrg 
    177  1.1.1.14  mrg       _GLIBCXX14_CONSTEXPR void
    178   1.1.1.2  mrg       _M_do_set() _GLIBCXX_NOEXCEPT
    179       1.1  mrg       {
    180  1.1.1.14  mrg #if __cplusplus >= 201402L
    181  1.1.1.14  mrg 	if (__builtin_is_constant_evaluated())
    182  1.1.1.14  mrg 	  {
    183  1.1.1.14  mrg 	    for (_WordT& __w : _M_w)
    184  1.1.1.14  mrg 	      __w = ~static_cast<_WordT>(0);;
    185  1.1.1.14  mrg 	    return;
    186  1.1.1.14  mrg 	  }
    187  1.1.1.14  mrg #endif
    188  1.1.1.14  mrg 	__builtin_memset(_M_w, 0xFF, _Nw * sizeof(_WordT));
    189       1.1  mrg       }
    190       1.1  mrg 
    191  1.1.1.14  mrg       _GLIBCXX14_CONSTEXPR void
    192   1.1.1.2  mrg       _M_do_reset() _GLIBCXX_NOEXCEPT
    193  1.1.1.14  mrg       {
    194  1.1.1.14  mrg #if __cplusplus >= 201402L
    195  1.1.1.14  mrg 	if (__builtin_is_constant_evaluated())
    196  1.1.1.14  mrg 	  {
    197  1.1.1.14  mrg 	    for (_WordT& __w : _M_w)
    198  1.1.1.14  mrg 	      __w = 0;
    199  1.1.1.14  mrg 	    return;
    200  1.1.1.14  mrg 	  }
    201  1.1.1.14  mrg #endif
    202  1.1.1.14  mrg 	__builtin_memset(_M_w, 0, _Nw * sizeof(_WordT));
    203  1.1.1.14  mrg       }
    204       1.1  mrg 
    205  1.1.1.14  mrg       _GLIBCXX14_CONSTEXPR bool
    206   1.1.1.2  mrg       _M_is_equal(const _Base_bitset<_Nw>& __x) const _GLIBCXX_NOEXCEPT
    207       1.1  mrg       {
    208       1.1  mrg 	for (size_t __i = 0; __i < _Nw; ++__i)
    209       1.1  mrg 	  if (_M_w[__i] != __x._M_w[__i])
    210       1.1  mrg 	    return false;
    211       1.1  mrg 	return true;
    212       1.1  mrg       }
    213       1.1  mrg 
    214   1.1.1.2  mrg       template<size_t _Nb>
    215  1.1.1.14  mrg         _GLIBCXX14_CONSTEXPR bool
    216   1.1.1.2  mrg         _M_are_all() const _GLIBCXX_NOEXCEPT
    217   1.1.1.2  mrg         {
    218   1.1.1.2  mrg 	  for (size_t __i = 0; __i < _Nw - 1; __i++)
    219   1.1.1.2  mrg 	    if (_M_w[__i] != ~static_cast<_WordT>(0))
    220   1.1.1.2  mrg 	      return false;
    221   1.1.1.2  mrg 	  return _M_hiword() == (~static_cast<_WordT>(0)
    222   1.1.1.2  mrg 				 >> (_Nw * _GLIBCXX_BITSET_BITS_PER_WORD
    223   1.1.1.2  mrg 				     - _Nb));
    224   1.1.1.2  mrg 	}
    225       1.1  mrg 
    226  1.1.1.14  mrg       _GLIBCXX14_CONSTEXPR bool
    227   1.1.1.2  mrg       _M_is_any() const _GLIBCXX_NOEXCEPT
    228       1.1  mrg       {
    229       1.1  mrg 	for (size_t __i = 0; __i < _Nw; __i++)
    230       1.1  mrg 	  if (_M_w[__i] != static_cast<_WordT>(0))
    231       1.1  mrg 	    return true;
    232       1.1  mrg 	return false;
    233       1.1  mrg       }
    234       1.1  mrg 
    235  1.1.1.14  mrg       _GLIBCXX14_CONSTEXPR size_t
    236   1.1.1.2  mrg       _M_do_count() const _GLIBCXX_NOEXCEPT
    237       1.1  mrg       {
    238       1.1  mrg 	size_t __result = 0;
    239       1.1  mrg 	for (size_t __i = 0; __i < _Nw; __i++)
    240       1.1  mrg 	  __result += __builtin_popcountl(_M_w[__i]);
    241       1.1  mrg 	return __result;
    242       1.1  mrg       }
    243       1.1  mrg 
    244  1.1.1.14  mrg       _GLIBCXX14_CONSTEXPR unsigned long
    245       1.1  mrg       _M_do_to_ulong() const;
    246       1.1  mrg 
    247   1.1.1.2  mrg #if __cplusplus >= 201103L
    248  1.1.1.14  mrg       _GLIBCXX14_CONSTEXPR unsigned long long
    249       1.1  mrg       _M_do_to_ullong() const;
    250       1.1  mrg #endif
    251       1.1  mrg 
    252       1.1  mrg       // find first "on" bit
    253  1.1.1.14  mrg       _GLIBCXX14_CONSTEXPR size_t
    254   1.1.1.2  mrg       _M_do_find_first(size_t) const _GLIBCXX_NOEXCEPT;
    255       1.1  mrg 
    256       1.1  mrg       // find the next "on" bit that follows "prev"
    257  1.1.1.14  mrg       _GLIBCXX14_CONSTEXPR size_t
    258   1.1.1.2  mrg       _M_do_find_next(size_t, size_t) const _GLIBCXX_NOEXCEPT;
    259       1.1  mrg     };
    260       1.1  mrg 
    261       1.1  mrg   // Definitions of non-inline functions from _Base_bitset.
    262       1.1  mrg   template<size_t _Nw>
    263  1.1.1.14  mrg     _GLIBCXX14_CONSTEXPR void
    264   1.1.1.2  mrg     _Base_bitset<_Nw>::_M_do_left_shift(size_t __shift) _GLIBCXX_NOEXCEPT
    265       1.1  mrg     {
    266       1.1  mrg       if (__builtin_expect(__shift != 0, 1))
    267       1.1  mrg 	{
    268       1.1  mrg 	  const size_t __wshift = __shift / _GLIBCXX_BITSET_BITS_PER_WORD;
    269       1.1  mrg 	  const size_t __offset = __shift % _GLIBCXX_BITSET_BITS_PER_WORD;
    270       1.1  mrg 
    271       1.1  mrg 	  if (__offset == 0)
    272       1.1  mrg 	    for (size_t __n = _Nw - 1; __n >= __wshift; --__n)
    273       1.1  mrg 	      _M_w[__n] = _M_w[__n - __wshift];
    274       1.1  mrg 	  else
    275       1.1  mrg 	    {
    276   1.1.1.6  mrg 	      const size_t __sub_offset = (_GLIBCXX_BITSET_BITS_PER_WORD
    277       1.1  mrg 					   - __offset);
    278       1.1  mrg 	      for (size_t __n = _Nw - 1; __n > __wshift; --__n)
    279       1.1  mrg 		_M_w[__n] = ((_M_w[__n - __wshift] << __offset)
    280       1.1  mrg 			     | (_M_w[__n - __wshift - 1] >> __sub_offset));
    281       1.1  mrg 	      _M_w[__wshift] = _M_w[0] << __offset;
    282       1.1  mrg 	    }
    283       1.1  mrg 
    284       1.1  mrg 	  std::fill(_M_w + 0, _M_w + __wshift, static_cast<_WordT>(0));
    285       1.1  mrg 	}
    286       1.1  mrg     }
    287       1.1  mrg 
    288       1.1  mrg   template<size_t _Nw>
    289  1.1.1.14  mrg     _GLIBCXX14_CONSTEXPR void
    290   1.1.1.2  mrg     _Base_bitset<_Nw>::_M_do_right_shift(size_t __shift) _GLIBCXX_NOEXCEPT
    291       1.1  mrg     {
    292       1.1  mrg       if (__builtin_expect(__shift != 0, 1))
    293       1.1  mrg 	{
    294       1.1  mrg 	  const size_t __wshift = __shift / _GLIBCXX_BITSET_BITS_PER_WORD;
    295       1.1  mrg 	  const size_t __offset = __shift % _GLIBCXX_BITSET_BITS_PER_WORD;
    296       1.1  mrg 	  const size_t __limit = _Nw - __wshift - 1;
    297       1.1  mrg 
    298       1.1  mrg 	  if (__offset == 0)
    299       1.1  mrg 	    for (size_t __n = 0; __n <= __limit; ++__n)
    300       1.1  mrg 	      _M_w[__n] = _M_w[__n + __wshift];
    301       1.1  mrg 	  else
    302       1.1  mrg 	    {
    303       1.1  mrg 	      const size_t __sub_offset = (_GLIBCXX_BITSET_BITS_PER_WORD
    304       1.1  mrg 					   - __offset);
    305       1.1  mrg 	      for (size_t __n = 0; __n < __limit; ++__n)
    306       1.1  mrg 		_M_w[__n] = ((_M_w[__n + __wshift] >> __offset)
    307       1.1  mrg 			     | (_M_w[__n + __wshift + 1] << __sub_offset));
    308       1.1  mrg 	      _M_w[__limit] = _M_w[_Nw-1] >> __offset;
    309       1.1  mrg 	    }
    310   1.1.1.6  mrg 
    311       1.1  mrg 	  std::fill(_M_w + __limit + 1, _M_w + _Nw, static_cast<_WordT>(0));
    312       1.1  mrg 	}
    313       1.1  mrg     }
    314       1.1  mrg 
    315       1.1  mrg   template<size_t _Nw>
    316  1.1.1.14  mrg     _GLIBCXX14_CONSTEXPR unsigned long
    317       1.1  mrg     _Base_bitset<_Nw>::_M_do_to_ulong() const
    318       1.1  mrg     {
    319       1.1  mrg       for (size_t __i = 1; __i < _Nw; ++__i)
    320       1.1  mrg 	if (_M_w[__i])
    321       1.1  mrg 	  __throw_overflow_error(__N("_Base_bitset::_M_do_to_ulong"));
    322       1.1  mrg       return _M_w[0];
    323       1.1  mrg     }
    324       1.1  mrg 
    325   1.1.1.2  mrg #if __cplusplus >= 201103L
    326       1.1  mrg   template<size_t _Nw>
    327  1.1.1.14  mrg     _GLIBCXX14_CONSTEXPR unsigned long long
    328       1.1  mrg     _Base_bitset<_Nw>::_M_do_to_ullong() const
    329       1.1  mrg     {
    330  1.1.1.14  mrg #if __SIZEOF_LONG_LONG__ == __SIZEOF_LONG__
    331  1.1.1.14  mrg       return _M_do_to_ulong();
    332  1.1.1.14  mrg #else
    333  1.1.1.14  mrg       for (size_t __i = 2; __i < _Nw; ++__i)
    334       1.1  mrg 	if (_M_w[__i])
    335       1.1  mrg 	  __throw_overflow_error(__N("_Base_bitset::_M_do_to_ullong"));
    336       1.1  mrg 
    337  1.1.1.14  mrg       return _M_w[0] + (static_cast<unsigned long long>(_M_w[1])
    338       1.1  mrg 			  << _GLIBCXX_BITSET_BITS_PER_WORD);
    339       1.1  mrg #endif
    340  1.1.1.14  mrg     }
    341  1.1.1.14  mrg #endif // C++11
    342       1.1  mrg 
    343       1.1  mrg   template<size_t _Nw>
    344  1.1.1.14  mrg     _GLIBCXX14_CONSTEXPR size_t
    345   1.1.1.2  mrg     _Base_bitset<_Nw>::
    346   1.1.1.2  mrg     _M_do_find_first(size_t __not_found) const _GLIBCXX_NOEXCEPT
    347       1.1  mrg     {
    348       1.1  mrg       for (size_t __i = 0; __i < _Nw; __i++)
    349       1.1  mrg 	{
    350       1.1  mrg 	  _WordT __thisword = _M_w[__i];
    351       1.1  mrg 	  if (__thisword != static_cast<_WordT>(0))
    352       1.1  mrg 	    return (__i * _GLIBCXX_BITSET_BITS_PER_WORD
    353       1.1  mrg 		    + __builtin_ctzl(__thisword));
    354       1.1  mrg 	}
    355       1.1  mrg       // not found, so return an indication of failure.
    356       1.1  mrg       return __not_found;
    357       1.1  mrg     }
    358       1.1  mrg 
    359       1.1  mrg   template<size_t _Nw>
    360  1.1.1.14  mrg     _GLIBCXX14_CONSTEXPR size_t
    361   1.1.1.2  mrg     _Base_bitset<_Nw>::
    362   1.1.1.2  mrg     _M_do_find_next(size_t __prev, size_t __not_found) const _GLIBCXX_NOEXCEPT
    363       1.1  mrg     {
    364       1.1  mrg       // make bound inclusive
    365       1.1  mrg       ++__prev;
    366       1.1  mrg 
    367       1.1  mrg       // check out of bounds
    368       1.1  mrg       if (__prev >= _Nw * _GLIBCXX_BITSET_BITS_PER_WORD)
    369       1.1  mrg 	return __not_found;
    370       1.1  mrg 
    371       1.1  mrg       // search first word
    372       1.1  mrg       size_t __i = _S_whichword(__prev);
    373       1.1  mrg       _WordT __thisword = _M_w[__i];
    374       1.1  mrg 
    375       1.1  mrg       // mask off bits below bound
    376       1.1  mrg       __thisword &= (~static_cast<_WordT>(0)) << _S_whichbit(__prev);
    377       1.1  mrg 
    378       1.1  mrg       if (__thisword != static_cast<_WordT>(0))
    379       1.1  mrg 	return (__i * _GLIBCXX_BITSET_BITS_PER_WORD
    380       1.1  mrg 		+ __builtin_ctzl(__thisword));
    381       1.1  mrg 
    382       1.1  mrg       // check subsequent words
    383       1.1  mrg       __i++;
    384       1.1  mrg       for (; __i < _Nw; __i++)
    385       1.1  mrg 	{
    386       1.1  mrg 	  __thisword = _M_w[__i];
    387       1.1  mrg 	  if (__thisword != static_cast<_WordT>(0))
    388       1.1  mrg 	    return (__i * _GLIBCXX_BITSET_BITS_PER_WORD
    389       1.1  mrg 		    + __builtin_ctzl(__thisword));
    390       1.1  mrg 	}
    391       1.1  mrg       // not found, so return an indication of failure.
    392       1.1  mrg       return __not_found;
    393       1.1  mrg     } // end _M_do_find_next
    394       1.1  mrg 
    395       1.1  mrg   /**
    396       1.1  mrg    *  Base class, specialization for a single word.
    397       1.1  mrg    *
    398       1.1  mrg    *  See documentation for bitset.
    399       1.1  mrg   */
    400       1.1  mrg   template<>
    401       1.1  mrg     struct _Base_bitset<1>
    402       1.1  mrg     {
    403       1.1  mrg       typedef unsigned long _WordT;
    404       1.1  mrg       _WordT _M_w;
    405       1.1  mrg 
    406   1.1.1.2  mrg       _GLIBCXX_CONSTEXPR _Base_bitset() _GLIBCXX_NOEXCEPT
    407       1.1  mrg       : _M_w(0)
    408       1.1  mrg       { }
    409       1.1  mrg 
    410   1.1.1.2  mrg #if __cplusplus >= 201103L
    411   1.1.1.2  mrg       constexpr _Base_bitset(unsigned long long __val) noexcept
    412       1.1  mrg #else
    413       1.1  mrg       _Base_bitset(unsigned long __val)
    414       1.1  mrg #endif
    415       1.1  mrg       : _M_w(__val)
    416       1.1  mrg       { }
    417       1.1  mrg 
    418   1.1.1.2  mrg       static _GLIBCXX_CONSTEXPR size_t
    419   1.1.1.2  mrg       _S_whichword(size_t __pos) _GLIBCXX_NOEXCEPT
    420       1.1  mrg       { return __pos / _GLIBCXX_BITSET_BITS_PER_WORD; }
    421       1.1  mrg 
    422   1.1.1.2  mrg       static _GLIBCXX_CONSTEXPR size_t
    423   1.1.1.2  mrg       _S_whichbyte(size_t __pos) _GLIBCXX_NOEXCEPT
    424       1.1  mrg       { return (__pos % _GLIBCXX_BITSET_BITS_PER_WORD) / __CHAR_BIT__; }
    425       1.1  mrg 
    426   1.1.1.2  mrg       static _GLIBCXX_CONSTEXPR size_t
    427   1.1.1.2  mrg       _S_whichbit(size_t __pos) _GLIBCXX_NOEXCEPT
    428       1.1  mrg       {  return __pos % _GLIBCXX_BITSET_BITS_PER_WORD; }
    429       1.1  mrg 
    430   1.1.1.2  mrg       static _GLIBCXX_CONSTEXPR _WordT
    431   1.1.1.2  mrg       _S_maskbit(size_t __pos) _GLIBCXX_NOEXCEPT
    432       1.1  mrg       { return (static_cast<_WordT>(1)) << _S_whichbit(__pos); }
    433       1.1  mrg 
    434  1.1.1.14  mrg       _GLIBCXX14_CONSTEXPR _WordT&
    435   1.1.1.2  mrg       _M_getword(size_t) _GLIBCXX_NOEXCEPT
    436       1.1  mrg       { return _M_w; }
    437       1.1  mrg 
    438   1.1.1.2  mrg       _GLIBCXX_CONSTEXPR _WordT
    439   1.1.1.2  mrg       _M_getword(size_t) const _GLIBCXX_NOEXCEPT
    440       1.1  mrg       { return _M_w; }
    441       1.1  mrg 
    442   1.1.1.2  mrg #if __cplusplus >= 201103L
    443  1.1.1.14  mrg       constexpr const _WordT*
    444   1.1.1.2  mrg       _M_getdata() const noexcept
    445       1.1  mrg       { return &_M_w; }
    446       1.1  mrg #endif
    447       1.1  mrg 
    448  1.1.1.14  mrg       _GLIBCXX14_CONSTEXPR _WordT&
    449   1.1.1.2  mrg       _M_hiword() _GLIBCXX_NOEXCEPT
    450       1.1  mrg       { return _M_w; }
    451       1.1  mrg 
    452   1.1.1.2  mrg       _GLIBCXX_CONSTEXPR _WordT
    453   1.1.1.2  mrg       _M_hiword() const _GLIBCXX_NOEXCEPT
    454       1.1  mrg       { return _M_w; }
    455       1.1  mrg 
    456  1.1.1.14  mrg       _GLIBCXX14_CONSTEXPR void
    457   1.1.1.2  mrg       _M_do_and(const _Base_bitset<1>& __x) _GLIBCXX_NOEXCEPT
    458       1.1  mrg       { _M_w &= __x._M_w; }
    459       1.1  mrg 
    460  1.1.1.14  mrg       _GLIBCXX14_CONSTEXPR void
    461   1.1.1.2  mrg       _M_do_or(const _Base_bitset<1>& __x) _GLIBCXX_NOEXCEPT
    462       1.1  mrg       { _M_w |= __x._M_w; }
    463       1.1  mrg 
    464  1.1.1.14  mrg       _GLIBCXX14_CONSTEXPR void
    465   1.1.1.2  mrg       _M_do_xor(const _Base_bitset<1>& __x) _GLIBCXX_NOEXCEPT
    466       1.1  mrg       { _M_w ^= __x._M_w; }
    467       1.1  mrg 
    468  1.1.1.14  mrg       _GLIBCXX14_CONSTEXPR void
    469   1.1.1.2  mrg       _M_do_left_shift(size_t __shift) _GLIBCXX_NOEXCEPT
    470       1.1  mrg       { _M_w <<= __shift; }
    471       1.1  mrg 
    472  1.1.1.14  mrg       _GLIBCXX14_CONSTEXPR void
    473   1.1.1.2  mrg       _M_do_right_shift(size_t __shift) _GLIBCXX_NOEXCEPT
    474       1.1  mrg       { _M_w >>= __shift; }
    475       1.1  mrg 
    476  1.1.1.14  mrg       _GLIBCXX14_CONSTEXPR void
    477   1.1.1.2  mrg       _M_do_flip() _GLIBCXX_NOEXCEPT
    478       1.1  mrg       { _M_w = ~_M_w; }
    479       1.1  mrg 
    480  1.1.1.14  mrg       _GLIBCXX14_CONSTEXPR void
    481   1.1.1.2  mrg       _M_do_set() _GLIBCXX_NOEXCEPT
    482       1.1  mrg       { _M_w = ~static_cast<_WordT>(0); }
    483       1.1  mrg 
    484  1.1.1.14  mrg       _GLIBCXX14_CONSTEXPR void
    485   1.1.1.2  mrg       _M_do_reset() _GLIBCXX_NOEXCEPT
    486       1.1  mrg       { _M_w = 0; }
    487       1.1  mrg 
    488  1.1.1.14  mrg       _GLIBCXX14_CONSTEXPR bool
    489   1.1.1.2  mrg       _M_is_equal(const _Base_bitset<1>& __x) const _GLIBCXX_NOEXCEPT
    490       1.1  mrg       { return _M_w == __x._M_w; }
    491       1.1  mrg 
    492   1.1.1.2  mrg       template<size_t _Nb>
    493  1.1.1.14  mrg         _GLIBCXX14_CONSTEXPR bool
    494   1.1.1.2  mrg         _M_are_all() const _GLIBCXX_NOEXCEPT
    495   1.1.1.2  mrg         { return _M_w == (~static_cast<_WordT>(0)
    496   1.1.1.2  mrg 			  >> (_GLIBCXX_BITSET_BITS_PER_WORD - _Nb)); }
    497       1.1  mrg 
    498  1.1.1.14  mrg       _GLIBCXX14_CONSTEXPR bool
    499   1.1.1.2  mrg       _M_is_any() const _GLIBCXX_NOEXCEPT
    500       1.1  mrg       { return _M_w != 0; }
    501       1.1  mrg 
    502  1.1.1.14  mrg       _GLIBCXX14_CONSTEXPR size_t
    503   1.1.1.2  mrg       _M_do_count() const _GLIBCXX_NOEXCEPT
    504       1.1  mrg       { return __builtin_popcountl(_M_w); }
    505       1.1  mrg 
    506  1.1.1.14  mrg       _GLIBCXX14_CONSTEXPR unsigned long
    507   1.1.1.2  mrg       _M_do_to_ulong() const _GLIBCXX_NOEXCEPT
    508       1.1  mrg       { return _M_w; }
    509       1.1  mrg 
    510   1.1.1.2  mrg #if __cplusplus >= 201103L
    511  1.1.1.14  mrg       constexpr unsigned long long
    512   1.1.1.2  mrg       _M_do_to_ullong() const noexcept
    513       1.1  mrg       { return _M_w; }
    514       1.1  mrg #endif
    515       1.1  mrg 
    516  1.1.1.14  mrg       _GLIBCXX14_CONSTEXPR size_t
    517   1.1.1.2  mrg       _M_do_find_first(size_t __not_found) const _GLIBCXX_NOEXCEPT
    518       1.1  mrg       {
    519       1.1  mrg         if (_M_w != 0)
    520       1.1  mrg           return __builtin_ctzl(_M_w);
    521       1.1  mrg         else
    522       1.1  mrg           return __not_found;
    523       1.1  mrg       }
    524       1.1  mrg 
    525       1.1  mrg       // find the next "on" bit that follows "prev"
    526  1.1.1.14  mrg       _GLIBCXX14_CONSTEXPR size_t
    527       1.1  mrg       _M_do_find_next(size_t __prev, size_t __not_found) const
    528   1.1.1.2  mrg 	_GLIBCXX_NOEXCEPT
    529       1.1  mrg       {
    530       1.1  mrg 	++__prev;
    531       1.1  mrg 	if (__prev >= ((size_t) _GLIBCXX_BITSET_BITS_PER_WORD))
    532       1.1  mrg 	  return __not_found;
    533       1.1  mrg 
    534       1.1  mrg 	_WordT __x = _M_w >> __prev;
    535       1.1  mrg 	if (__x != 0)
    536       1.1  mrg 	  return __builtin_ctzl(__x) + __prev;
    537       1.1  mrg 	else
    538       1.1  mrg 	  return __not_found;
    539       1.1  mrg       }
    540       1.1  mrg     };
    541       1.1  mrg 
    542       1.1  mrg   /**
    543       1.1  mrg    *  Base class, specialization for no storage (zero-length %bitset).
    544       1.1  mrg    *
    545       1.1  mrg    *  See documentation for bitset.
    546       1.1  mrg   */
    547       1.1  mrg   template<>
    548       1.1  mrg     struct _Base_bitset<0>
    549       1.1  mrg     {
    550       1.1  mrg       typedef unsigned long _WordT;
    551       1.1  mrg 
    552   1.1.1.2  mrg       _GLIBCXX_CONSTEXPR _Base_bitset() _GLIBCXX_NOEXCEPT
    553       1.1  mrg       { }
    554       1.1  mrg 
    555   1.1.1.2  mrg #if __cplusplus >= 201103L
    556   1.1.1.2  mrg       constexpr _Base_bitset(unsigned long long) noexcept
    557       1.1  mrg #else
    558       1.1  mrg       _Base_bitset(unsigned long)
    559       1.1  mrg #endif
    560       1.1  mrg       { }
    561       1.1  mrg 
    562   1.1.1.2  mrg       static _GLIBCXX_CONSTEXPR size_t
    563   1.1.1.2  mrg       _S_whichword(size_t __pos) _GLIBCXX_NOEXCEPT
    564       1.1  mrg       { return __pos / _GLIBCXX_BITSET_BITS_PER_WORD; }
    565       1.1  mrg 
    566   1.1.1.2  mrg       static _GLIBCXX_CONSTEXPR size_t
    567   1.1.1.2  mrg       _S_whichbyte(size_t __pos) _GLIBCXX_NOEXCEPT
    568       1.1  mrg       { return (__pos % _GLIBCXX_BITSET_BITS_PER_WORD) / __CHAR_BIT__; }
    569       1.1  mrg 
    570   1.1.1.2  mrg       static _GLIBCXX_CONSTEXPR size_t
    571   1.1.1.2  mrg       _S_whichbit(size_t __pos) _GLIBCXX_NOEXCEPT
    572       1.1  mrg       {  return __pos % _GLIBCXX_BITSET_BITS_PER_WORD; }
    573       1.1  mrg 
    574   1.1.1.2  mrg       static _GLIBCXX_CONSTEXPR _WordT
    575   1.1.1.2  mrg       _S_maskbit(size_t __pos) _GLIBCXX_NOEXCEPT
    576       1.1  mrg       { return (static_cast<_WordT>(1)) << _S_whichbit(__pos); }
    577       1.1  mrg 
    578       1.1  mrg       // This would normally give access to the data.  The bounds-checking
    579       1.1  mrg       // in the bitset class will prevent the user from getting this far,
    580  1.1.1.14  mrg       // but this must fail if the user calls _Unchecked_set directly.
    581  1.1.1.14  mrg       // Let's not penalize zero-length users unless they actually
    582       1.1  mrg       // make an unchecked call; all the memory ugliness is therefore
    583       1.1  mrg       // localized to this single should-never-get-this-far function.
    584  1.1.1.14  mrg       __attribute__((__noreturn__))
    585       1.1  mrg       _WordT&
    586   1.1.1.2  mrg       _M_getword(size_t) _GLIBCXX_NOEXCEPT
    587  1.1.1.14  mrg       { __throw_out_of_range(__N("_Base_bitset::_M_getword")); }
    588       1.1  mrg 
    589   1.1.1.2  mrg       _GLIBCXX_CONSTEXPR _WordT
    590   1.1.1.9  mrg       _M_getword(size_t) const _GLIBCXX_NOEXCEPT
    591   1.1.1.2  mrg       { return 0; }
    592   1.1.1.2  mrg 
    593   1.1.1.2  mrg       _GLIBCXX_CONSTEXPR _WordT
    594   1.1.1.2  mrg       _M_hiword() const _GLIBCXX_NOEXCEPT
    595       1.1  mrg       { return 0; }
    596       1.1  mrg 
    597  1.1.1.14  mrg       _GLIBCXX14_CONSTEXPR void
    598   1.1.1.2  mrg       _M_do_and(const _Base_bitset<0>&) _GLIBCXX_NOEXCEPT
    599       1.1  mrg       { }
    600       1.1  mrg 
    601  1.1.1.14  mrg       _GLIBCXX14_CONSTEXPR void
    602   1.1.1.2  mrg       _M_do_or(const _Base_bitset<0>&) _GLIBCXX_NOEXCEPT
    603       1.1  mrg       { }
    604       1.1  mrg 
    605  1.1.1.14  mrg       _GLIBCXX14_CONSTEXPR void
    606   1.1.1.2  mrg       _M_do_xor(const _Base_bitset<0>&) _GLIBCXX_NOEXCEPT
    607       1.1  mrg       { }
    608       1.1  mrg 
    609  1.1.1.14  mrg       _GLIBCXX14_CONSTEXPR void
    610   1.1.1.2  mrg       _M_do_left_shift(size_t) _GLIBCXX_NOEXCEPT
    611       1.1  mrg       { }
    612       1.1  mrg 
    613  1.1.1.14  mrg       _GLIBCXX14_CONSTEXPR void
    614   1.1.1.2  mrg       _M_do_right_shift(size_t) _GLIBCXX_NOEXCEPT
    615       1.1  mrg       { }
    616       1.1  mrg 
    617  1.1.1.14  mrg       _GLIBCXX14_CONSTEXPR void
    618   1.1.1.2  mrg       _M_do_flip() _GLIBCXX_NOEXCEPT
    619       1.1  mrg       { }
    620       1.1  mrg 
    621  1.1.1.14  mrg       _GLIBCXX14_CONSTEXPR void
    622   1.1.1.2  mrg       _M_do_set() _GLIBCXX_NOEXCEPT
    623       1.1  mrg       { }
    624       1.1  mrg 
    625  1.1.1.14  mrg       _GLIBCXX14_CONSTEXPR void
    626   1.1.1.2  mrg       _M_do_reset() _GLIBCXX_NOEXCEPT
    627       1.1  mrg       { }
    628       1.1  mrg 
    629       1.1  mrg       // Are all empty bitsets equal to each other?  Are they equal to
    630       1.1  mrg       // themselves?  How to compare a thing which has no state?  What is
    631       1.1  mrg       // the sound of one zero-length bitset clapping?
    632  1.1.1.14  mrg       _GLIBCXX_CONSTEXPR bool
    633   1.1.1.2  mrg       _M_is_equal(const _Base_bitset<0>&) const _GLIBCXX_NOEXCEPT
    634       1.1  mrg       { return true; }
    635       1.1  mrg 
    636   1.1.1.2  mrg       template<size_t _Nb>
    637  1.1.1.14  mrg         _GLIBCXX_CONSTEXPR bool
    638   1.1.1.2  mrg         _M_are_all() const _GLIBCXX_NOEXCEPT
    639   1.1.1.2  mrg         { return true; }
    640       1.1  mrg 
    641  1.1.1.14  mrg       _GLIBCXX_CONSTEXPR bool
    642   1.1.1.2  mrg       _M_is_any() const _GLIBCXX_NOEXCEPT
    643       1.1  mrg       { return false; }
    644       1.1  mrg 
    645  1.1.1.14  mrg       _GLIBCXX_CONSTEXPR size_t
    646   1.1.1.2  mrg       _M_do_count() const _GLIBCXX_NOEXCEPT
    647       1.1  mrg       { return 0; }
    648       1.1  mrg 
    649  1.1.1.14  mrg       _GLIBCXX_CONSTEXPR unsigned long
    650   1.1.1.2  mrg       _M_do_to_ulong() const _GLIBCXX_NOEXCEPT
    651       1.1  mrg       { return 0; }
    652       1.1  mrg 
    653   1.1.1.2  mrg #if __cplusplus >= 201103L
    654  1.1.1.14  mrg       constexpr unsigned long long
    655   1.1.1.2  mrg       _M_do_to_ullong() const noexcept
    656       1.1  mrg       { return 0; }
    657       1.1  mrg #endif
    658       1.1  mrg 
    659       1.1  mrg       // Normally "not found" is the size, but that could also be
    660       1.1  mrg       // misinterpreted as an index in this corner case.  Oh well.
    661  1.1.1.14  mrg       _GLIBCXX_CONSTEXPR size_t
    662   1.1.1.2  mrg       _M_do_find_first(size_t) const _GLIBCXX_NOEXCEPT
    663       1.1  mrg       { return 0; }
    664       1.1  mrg 
    665  1.1.1.14  mrg       _GLIBCXX_CONSTEXPR size_t
    666   1.1.1.2  mrg       _M_do_find_next(size_t, size_t) const _GLIBCXX_NOEXCEPT
    667       1.1  mrg       { return 0; }
    668       1.1  mrg     };
    669       1.1  mrg 
    670       1.1  mrg 
    671       1.1  mrg   // Helper class to zero out the unused high-order bits in the highest word.
    672       1.1  mrg   template<size_t _Extrabits>
    673       1.1  mrg     struct _Sanitize
    674       1.1  mrg     {
    675   1.1.1.2  mrg       typedef unsigned long _WordT;
    676   1.1.1.2  mrg 
    677  1.1.1.14  mrg       static _GLIBCXX14_CONSTEXPR void
    678   1.1.1.2  mrg       _S_do_sanitize(_WordT& __val) _GLIBCXX_NOEXCEPT
    679   1.1.1.2  mrg       { __val &= ~((~static_cast<_WordT>(0)) << _Extrabits); }
    680       1.1  mrg     };
    681       1.1  mrg 
    682       1.1  mrg   template<>
    683       1.1  mrg     struct _Sanitize<0>
    684   1.1.1.2  mrg     {
    685   1.1.1.2  mrg       typedef unsigned long _WordT;
    686   1.1.1.2  mrg 
    687  1.1.1.14  mrg       static _GLIBCXX14_CONSTEXPR void
    688   1.1.1.6  mrg       _S_do_sanitize(_WordT) _GLIBCXX_NOEXCEPT { }
    689   1.1.1.2  mrg     };
    690   1.1.1.2  mrg 
    691   1.1.1.2  mrg #if __cplusplus >= 201103L
    692   1.1.1.4  mrg   template<size_t _Nb, bool = (_Nb < _GLIBCXX_BITSET_BITS_PER_ULL)>
    693   1.1.1.2  mrg     struct _Sanitize_val
    694   1.1.1.2  mrg     {
    695   1.1.1.2  mrg       static constexpr unsigned long long
    696   1.1.1.2  mrg       _S_do_sanitize_val(unsigned long long __val)
    697   1.1.1.2  mrg       { return __val; }
    698   1.1.1.2  mrg     };
    699   1.1.1.2  mrg 
    700   1.1.1.2  mrg   template<size_t _Nb>
    701   1.1.1.2  mrg     struct _Sanitize_val<_Nb, true>
    702   1.1.1.2  mrg     {
    703   1.1.1.2  mrg       static constexpr unsigned long long
    704   1.1.1.2  mrg       _S_do_sanitize_val(unsigned long long __val)
    705   1.1.1.2  mrg       { return __val & ~((~static_cast<unsigned long long>(0)) << _Nb); }
    706   1.1.1.2  mrg     };
    707  1.1.1.14  mrg 
    708  1.1.1.14  mrg   namespace __bitset
    709  1.1.1.14  mrg   {
    710  1.1.1.14  mrg #if _GLIBCXX_HOSTED
    711  1.1.1.14  mrg     template<typename _CharT>
    712  1.1.1.14  mrg       using __string = std::basic_string<_CharT>;
    713  1.1.1.14  mrg #else
    714  1.1.1.14  mrg     template<typename _CharT>
    715  1.1.1.14  mrg       struct __string
    716  1.1.1.14  mrg       {
    717  1.1.1.14  mrg 	using size_type = size_t;
    718  1.1.1.14  mrg 	static constexpr size_type npos = size_type(-1);
    719  1.1.1.14  mrg 
    720  1.1.1.14  mrg 	struct traits_type
    721  1.1.1.14  mrg 	{
    722  1.1.1.14  mrg 	  static _GLIBCXX14_CONSTEXPR size_t
    723  1.1.1.14  mrg 	  length(const _CharT* __s) noexcept
    724  1.1.1.14  mrg 	  {
    725  1.1.1.14  mrg 	    size_t __n = 0;
    726  1.1.1.14  mrg 	    while (__s[__n])
    727  1.1.1.14  mrg 	      __n++;
    728  1.1.1.14  mrg 	    return __n;
    729  1.1.1.14  mrg 	  }
    730  1.1.1.14  mrg 
    731  1.1.1.14  mrg 	  static constexpr bool
    732  1.1.1.14  mrg 	  eq(_CharT __l, _CharT __r) noexcept
    733  1.1.1.14  mrg 	  { return __l == __r; }
    734  1.1.1.14  mrg 	};
    735  1.1.1.14  mrg       };
    736  1.1.1.14  mrg #endif // HOSTED
    737  1.1.1.14  mrg   } // namespace __bitset
    738  1.1.1.14  mrg #endif // C++11
    739       1.1  mrg 
    740       1.1  mrg   /**
    741   1.1.1.3  mrg    *  @brief The %bitset class represents a @e fixed-size sequence of bits.
    742   1.1.1.3  mrg    *  @ingroup utilities
    743       1.1  mrg    *
    744       1.1  mrg    *  (Note that %bitset does @e not meet the formal requirements of a
    745       1.1  mrg    *  <a href="tables.html#65">container</a>.  Mainly, it lacks iterators.)
    746       1.1  mrg    *
    747       1.1  mrg    *  The template argument, @a Nb, may be any non-negative number,
    748       1.1  mrg    *  specifying the number of bits (e.g., "0", "12", "1024*1024").
    749       1.1  mrg    *
    750       1.1  mrg    *  In the general unoptimized case, storage is allocated in word-sized
    751       1.1  mrg    *  blocks.  Let B be the number of bits in a word, then (Nb+(B-1))/B
    752       1.1  mrg    *  words will be used for storage.  B - Nb%B bits are unused.  (They are
    753       1.1  mrg    *  the high-order bits in the highest word.)  It is a class invariant
    754       1.1  mrg    *  that those unused bits are always zero.
    755       1.1  mrg    *
    756       1.1  mrg    *  If you think of %bitset as <em>a simple array of bits</em>, be
    757       1.1  mrg    *  aware that your mental picture is reversed: a %bitset behaves
    758       1.1  mrg    *  the same way as bits in integers do, with the bit at index 0 in
    759       1.1  mrg    *  the <em>least significant / right-hand</em> position, and the bit at
    760       1.1  mrg    *  index Nb-1 in the <em>most significant / left-hand</em> position.
    761       1.1  mrg    *  Thus, unlike other containers, a %bitset's index <em>counts from
    762       1.1  mrg    *  right to left</em>, to put it very loosely.
    763       1.1  mrg    *
    764       1.1  mrg    *  This behavior is preserved when translating to and from strings.  For
    765       1.1  mrg    *  example, the first line of the following program probably prints
    766       1.1  mrg    *  <em>b(&apos;a&apos;) is 0001100001</em> on a modern ASCII system.
    767       1.1  mrg    *
    768       1.1  mrg    *  @code
    769       1.1  mrg    *     #include <bitset>
    770       1.1  mrg    *     #include <iostream>
    771       1.1  mrg    *     #include <sstream>
    772       1.1  mrg    *
    773       1.1  mrg    *     using namespace std;
    774       1.1  mrg    *
    775       1.1  mrg    *     int main()
    776       1.1  mrg    *     {
    777       1.1  mrg    *         long         a = 'a';
    778       1.1  mrg    *         bitset<10>   b(a);
    779       1.1  mrg    *
    780       1.1  mrg    *         cout << "b('a') is " << b << endl;
    781       1.1  mrg    *
    782       1.1  mrg    *         ostringstream s;
    783       1.1  mrg    *         s << b;
    784       1.1  mrg    *         string  str = s.str();
    785       1.1  mrg    *         cout << "index 3 in the string is " << str[3] << " but\n"
    786       1.1  mrg    *              << "index 3 in the bitset is " << b[3] << endl;
    787       1.1  mrg    *     }
    788       1.1  mrg    *  @endcode
    789       1.1  mrg    *
    790       1.1  mrg    *  Also see:
    791   1.1.1.3  mrg    *  https://gcc.gnu.org/onlinedocs/libstdc++/manual/ext_containers.html
    792       1.1  mrg    *  for a description of extensions.
    793       1.1  mrg    *
    794       1.1  mrg    *  Most of the actual code isn't contained in %bitset<> itself, but in the
    795       1.1  mrg    *  base class _Base_bitset.  The base class works with whole words, not with
    796       1.1  mrg    *  individual bits.  This allows us to specialize _Base_bitset for the
    797       1.1  mrg    *  important special case where the %bitset is only a single word.
    798       1.1  mrg    *
    799       1.1  mrg    *  Extra confusion can result due to the fact that the storage for
    800       1.1  mrg    *  _Base_bitset @e is a regular array, and is indexed as such.  This is
    801       1.1  mrg    *  carefully encapsulated.
    802       1.1  mrg   */
    803       1.1  mrg   template<size_t _Nb>
    804       1.1  mrg     class bitset
    805       1.1  mrg     : private _Base_bitset<_GLIBCXX_BITSET_WORDS(_Nb)>
    806       1.1  mrg     {
    807       1.1  mrg     private:
    808       1.1  mrg       typedef _Base_bitset<_GLIBCXX_BITSET_WORDS(_Nb)> _Base;
    809       1.1  mrg       typedef unsigned long _WordT;
    810       1.1  mrg 
    811  1.1.1.14  mrg #if _GLIBCXX_HOSTED
    812   1.1.1.3  mrg       template<class _CharT, class _Traits, class _Alloc>
    813  1.1.1.14  mrg       _GLIBCXX23_CONSTEXPR
    814   1.1.1.3  mrg       void
    815   1.1.1.3  mrg       _M_check_initial_position(const std::basic_string<_CharT, _Traits, _Alloc>& __s,
    816   1.1.1.3  mrg 				size_t __position) const
    817   1.1.1.3  mrg       {
    818   1.1.1.3  mrg 	if (__position > __s.size())
    819   1.1.1.3  mrg 	  __throw_out_of_range_fmt(__N("bitset::bitset: __position "
    820   1.1.1.3  mrg 				       "(which is %zu) > __s.size() "
    821   1.1.1.3  mrg 				       "(which is %zu)"),
    822   1.1.1.3  mrg 				   __position, __s.size());
    823   1.1.1.3  mrg       }
    824  1.1.1.14  mrg #endif // HOSTED
    825   1.1.1.3  mrg 
    826  1.1.1.14  mrg       _GLIBCXX23_CONSTEXPR
    827   1.1.1.3  mrg       void _M_check(size_t __position, const char *__s) const
    828   1.1.1.3  mrg       {
    829   1.1.1.3  mrg 	if (__position >= _Nb)
    830   1.1.1.3  mrg 	  __throw_out_of_range_fmt(__N("%s: __position (which is %zu) "
    831   1.1.1.3  mrg 				       ">= _Nb (which is %zu)"),
    832   1.1.1.3  mrg 				   __s, __position, _Nb);
    833   1.1.1.3  mrg       }
    834   1.1.1.3  mrg 
    835  1.1.1.14  mrg       _GLIBCXX23_CONSTEXPR
    836       1.1  mrg       void
    837   1.1.1.2  mrg       _M_do_sanitize() _GLIBCXX_NOEXCEPT
    838   1.1.1.6  mrg       {
    839   1.1.1.2  mrg 	typedef _Sanitize<_Nb % _GLIBCXX_BITSET_BITS_PER_WORD> __sanitize_type;
    840   1.1.1.2  mrg 	__sanitize_type::_S_do_sanitize(this->_M_hiword());
    841   1.1.1.2  mrg       }
    842       1.1  mrg 
    843   1.1.1.2  mrg #if __cplusplus >= 201103L
    844   1.1.1.9  mrg       friend struct std::hash<bitset>;
    845       1.1  mrg #endif
    846       1.1  mrg 
    847       1.1  mrg     public:
    848       1.1  mrg       /**
    849       1.1  mrg        *  This encapsulates the concept of a single bit.  An instance of this
    850       1.1  mrg        *  class is a proxy for an actual bit; this way the individual bit
    851       1.1  mrg        *  operations are done as faster word-size bitwise instructions.
    852       1.1  mrg        *
    853       1.1  mrg        *  Most users will never need to use this class directly; conversions
    854       1.1  mrg        *  to and from bool are automatic and should be transparent.  Overloaded
    855       1.1  mrg        *  operators help to preserve the illusion.
    856       1.1  mrg        *
    857       1.1  mrg        *  (On a typical system, this <em>bit %reference</em> is 64
    858       1.1  mrg        *  times the size of an actual bit.  Ha.)
    859       1.1  mrg        */
    860       1.1  mrg       class reference
    861       1.1  mrg       {
    862       1.1  mrg 	friend class bitset;
    863       1.1  mrg 
    864   1.1.1.2  mrg 	_WordT*	_M_wp;
    865   1.1.1.2  mrg 	size_t 	_M_bpos;
    866   1.1.1.6  mrg 
    867       1.1  mrg 	// left undefined
    868       1.1  mrg 	reference();
    869   1.1.1.6  mrg 
    870       1.1  mrg       public:
    871  1.1.1.14  mrg 	_GLIBCXX23_CONSTEXPR
    872   1.1.1.2  mrg 	reference(bitset& __b, size_t __pos) _GLIBCXX_NOEXCEPT
    873       1.1  mrg 	{
    874       1.1  mrg 	  _M_wp = &__b._M_getword(__pos);
    875       1.1  mrg 	  _M_bpos = _Base::_S_whichbit(__pos);
    876       1.1  mrg 	}
    877       1.1  mrg 
    878  1.1.1.10  mrg #if __cplusplus >= 201103L
    879  1.1.1.10  mrg 	reference(const reference&) = default;
    880  1.1.1.10  mrg #endif
    881  1.1.1.10  mrg 
    882  1.1.1.14  mrg #if __cplusplus > 202002L && __cpp_constexpr_dynamic_alloc
    883  1.1.1.14  mrg 	constexpr
    884  1.1.1.14  mrg #endif
    885   1.1.1.2  mrg 	~reference() _GLIBCXX_NOEXCEPT
    886       1.1  mrg 	{ }
    887       1.1  mrg 
    888       1.1  mrg 	// For b[i] = __x;
    889  1.1.1.14  mrg 	_GLIBCXX23_CONSTEXPR
    890       1.1  mrg 	reference&
    891   1.1.1.2  mrg 	operator=(bool __x) _GLIBCXX_NOEXCEPT
    892       1.1  mrg 	{
    893       1.1  mrg 	  if (__x)
    894       1.1  mrg 	    *_M_wp |= _Base::_S_maskbit(_M_bpos);
    895       1.1  mrg 	  else
    896       1.1  mrg 	    *_M_wp &= ~_Base::_S_maskbit(_M_bpos);
    897       1.1  mrg 	  return *this;
    898       1.1  mrg 	}
    899       1.1  mrg 
    900       1.1  mrg 	// For b[i] = b[__j];
    901  1.1.1.14  mrg 	_GLIBCXX23_CONSTEXPR
    902       1.1  mrg 	reference&
    903   1.1.1.2  mrg 	operator=(const reference& __j) _GLIBCXX_NOEXCEPT
    904       1.1  mrg 	{
    905       1.1  mrg 	  if ((*(__j._M_wp) & _Base::_S_maskbit(__j._M_bpos)))
    906       1.1  mrg 	    *_M_wp |= _Base::_S_maskbit(_M_bpos);
    907       1.1  mrg 	  else
    908       1.1  mrg 	    *_M_wp &= ~_Base::_S_maskbit(_M_bpos);
    909       1.1  mrg 	  return *this;
    910       1.1  mrg 	}
    911       1.1  mrg 
    912       1.1  mrg 	// Flips the bit
    913  1.1.1.14  mrg 	_GLIBCXX23_CONSTEXPR
    914       1.1  mrg 	bool
    915   1.1.1.2  mrg 	operator~() const _GLIBCXX_NOEXCEPT
    916       1.1  mrg 	{ return (*(_M_wp) & _Base::_S_maskbit(_M_bpos)) == 0; }
    917       1.1  mrg 
    918       1.1  mrg 	// For __x = b[i];
    919  1.1.1.14  mrg 	_GLIBCXX23_CONSTEXPR
    920   1.1.1.2  mrg 	operator bool() const _GLIBCXX_NOEXCEPT
    921       1.1  mrg 	{ return (*(_M_wp) & _Base::_S_maskbit(_M_bpos)) != 0; }
    922       1.1  mrg 
    923       1.1  mrg 	// For b[i].flip();
    924  1.1.1.14  mrg 	_GLIBCXX23_CONSTEXPR
    925       1.1  mrg 	reference&
    926   1.1.1.2  mrg 	flip() _GLIBCXX_NOEXCEPT
    927       1.1  mrg 	{
    928       1.1  mrg 	  *_M_wp ^= _Base::_S_maskbit(_M_bpos);
    929       1.1  mrg 	  return *this;
    930       1.1  mrg 	}
    931       1.1  mrg       };
    932       1.1  mrg       friend class reference;
    933       1.1  mrg 
    934       1.1  mrg       // 23.3.5.1 constructors:
    935       1.1  mrg       /// All bits set to zero.
    936   1.1.1.2  mrg       _GLIBCXX_CONSTEXPR bitset() _GLIBCXX_NOEXCEPT
    937       1.1  mrg       { }
    938       1.1  mrg 
    939       1.1  mrg       /// Initial bits bitwise-copied from a single word (others set to zero).
    940   1.1.1.2  mrg #if __cplusplus >= 201103L
    941   1.1.1.2  mrg       constexpr bitset(unsigned long long __val) noexcept
    942   1.1.1.2  mrg       : _Base(_Sanitize_val<_Nb>::_S_do_sanitize_val(__val)) { }
    943       1.1  mrg #else
    944       1.1  mrg       bitset(unsigned long __val)
    945       1.1  mrg       : _Base(__val)
    946       1.1  mrg       { _M_do_sanitize(); }
    947   1.1.1.2  mrg #endif
    948       1.1  mrg 
    949  1.1.1.14  mrg #if _GLIBCXX_HOSTED
    950       1.1  mrg       /**
    951   1.1.1.2  mrg        *  Use a subset of a string.
    952   1.1.1.2  mrg        *  @param  __s  A string of @a 0 and @a 1 characters.
    953   1.1.1.2  mrg        *  @param  __position  Index of the first character in @a __s to use;
    954       1.1  mrg        *                    defaults to zero.
    955   1.1.1.2  mrg        *  @throw  std::out_of_range  If @a pos is bigger the size of @a __s.
    956       1.1  mrg        *  @throw  std::invalid_argument  If a character appears in the string
    957       1.1  mrg        *                                 which is neither @a 0 nor @a 1.
    958       1.1  mrg        */
    959       1.1  mrg       template<class _CharT, class _Traits, class _Alloc>
    960  1.1.1.14  mrg 	_GLIBCXX23_CONSTEXPR
    961       1.1  mrg 	explicit
    962       1.1  mrg 	bitset(const std::basic_string<_CharT, _Traits, _Alloc>& __s,
    963       1.1  mrg 	       size_t __position = 0)
    964       1.1  mrg 	: _Base()
    965       1.1  mrg 	{
    966   1.1.1.3  mrg 	  _M_check_initial_position(__s, __position);
    967       1.1  mrg 	  _M_copy_from_string(__s, __position,
    968       1.1  mrg 			      std::basic_string<_CharT, _Traits, _Alloc>::npos,
    969       1.1  mrg 			      _CharT('0'), _CharT('1'));
    970       1.1  mrg 	}
    971       1.1  mrg 
    972       1.1  mrg       /**
    973   1.1.1.2  mrg        *  Use a subset of a string.
    974   1.1.1.2  mrg        *  @param  __s  A string of @a 0 and @a 1 characters.
    975   1.1.1.2  mrg        *  @param  __position  Index of the first character in @a __s to use.
    976   1.1.1.2  mrg        *  @param  __n    The number of characters to copy.
    977   1.1.1.2  mrg        *  @throw std::out_of_range If @a __position is bigger the size
    978   1.1.1.2  mrg        *  of @a __s.
    979       1.1  mrg        *  @throw  std::invalid_argument  If a character appears in the string
    980       1.1  mrg        *                                 which is neither @a 0 nor @a 1.
    981       1.1  mrg        */
    982       1.1  mrg       template<class _CharT, class _Traits, class _Alloc>
    983  1.1.1.14  mrg 	_GLIBCXX23_CONSTEXPR
    984       1.1  mrg 	bitset(const std::basic_string<_CharT, _Traits, _Alloc>& __s,
    985       1.1  mrg 	       size_t __position, size_t __n)
    986       1.1  mrg 	: _Base()
    987       1.1  mrg 	{
    988   1.1.1.3  mrg 	  _M_check_initial_position(__s, __position);
    989       1.1  mrg 	  _M_copy_from_string(__s, __position, __n, _CharT('0'), _CharT('1'));
    990       1.1  mrg 	}
    991       1.1  mrg 
    992       1.1  mrg       // _GLIBCXX_RESOLVE_LIB_DEFECTS
    993       1.1  mrg       // 396. what are characters zero and one.
    994       1.1  mrg       template<class _CharT, class _Traits, class _Alloc>
    995  1.1.1.14  mrg 	_GLIBCXX23_CONSTEXPR
    996       1.1  mrg 	bitset(const std::basic_string<_CharT, _Traits, _Alloc>& __s,
    997       1.1  mrg 	       size_t __position, size_t __n,
    998       1.1  mrg 	       _CharT __zero, _CharT __one = _CharT('1'))
    999       1.1  mrg 	: _Base()
   1000       1.1  mrg 	{
   1001   1.1.1.3  mrg 	  _M_check_initial_position(__s, __position);
   1002       1.1  mrg 	  _M_copy_from_string(__s, __position, __n, __zero, __one);
   1003       1.1  mrg 	}
   1004  1.1.1.14  mrg #endif // HOSTED
   1005       1.1  mrg 
   1006   1.1.1.2  mrg #if __cplusplus >= 201103L
   1007       1.1  mrg       /**
   1008   1.1.1.2  mrg        *  Construct from a character %array.
   1009   1.1.1.2  mrg        *  @param  __str  An %array of characters @a zero and @a one.
   1010   1.1.1.2  mrg        *  @param  __n    The number of characters to use.
   1011   1.1.1.2  mrg        *  @param  __zero The character corresponding to the value 0.
   1012   1.1.1.2  mrg        *  @param  __one  The character corresponding to the value 1.
   1013   1.1.1.2  mrg        *  @throw  std::invalid_argument If a character appears in the string
   1014   1.1.1.2  mrg        *                                which is neither @a __zero nor @a __one.
   1015   1.1.1.2  mrg        */
   1016   1.1.1.2  mrg       template<typename _CharT>
   1017  1.1.1.14  mrg 	[[__gnu__::__nonnull__]]
   1018  1.1.1.14  mrg 	_GLIBCXX23_CONSTEXPR
   1019   1.1.1.2  mrg         explicit
   1020  1.1.1.14  mrg 	bitset(const _CharT* __str,
   1021  1.1.1.14  mrg 	       typename __bitset::__string<_CharT>::size_type __n
   1022  1.1.1.14  mrg 		 = __bitset::__string<_CharT>::npos,
   1023   1.1.1.2  mrg 	       _CharT __zero = _CharT('0'), _CharT __one = _CharT('1'))
   1024   1.1.1.2  mrg         : _Base()
   1025   1.1.1.2  mrg         {
   1026  1.1.1.14  mrg #if _GLIBCXX_HOSTED
   1027   1.1.1.2  mrg 	  if (!__str)
   1028   1.1.1.2  mrg 	    __throw_logic_error(__N("bitset::bitset(const _CharT*, ...)"));
   1029  1.1.1.14  mrg #endif
   1030  1.1.1.14  mrg 	  using _Traits = typename __bitset::__string<_CharT>::traits_type;
   1031   1.1.1.2  mrg 
   1032  1.1.1.14  mrg 	  if (__n == __bitset::__string<_CharT>::npos)
   1033  1.1.1.14  mrg 	    __n = _Traits::length(__str);
   1034  1.1.1.14  mrg 	  _M_copy_from_ptr<_CharT, _Traits>(__str, __n, 0, __n, __zero, __one);
   1035   1.1.1.2  mrg 	}
   1036  1.1.1.14  mrg #endif // C++11
   1037       1.1  mrg 
   1038       1.1  mrg       // 23.3.5.2 bitset operations:
   1039  1.1.1.12  mrg       ///@{
   1040       1.1  mrg       /**
   1041   1.1.1.2  mrg        *  Operations on bitsets.
   1042   1.1.1.2  mrg        *  @param  __rhs  A same-sized bitset.
   1043       1.1  mrg        *
   1044       1.1  mrg        *  These should be self-explanatory.
   1045       1.1  mrg        */
   1046  1.1.1.14  mrg       _GLIBCXX23_CONSTEXPR
   1047       1.1  mrg       bitset<_Nb>&
   1048   1.1.1.2  mrg       operator&=(const bitset<_Nb>& __rhs) _GLIBCXX_NOEXCEPT
   1049       1.1  mrg       {
   1050       1.1  mrg 	this->_M_do_and(__rhs);
   1051       1.1  mrg 	return *this;
   1052       1.1  mrg       }
   1053       1.1  mrg 
   1054  1.1.1.14  mrg       _GLIBCXX23_CONSTEXPR
   1055       1.1  mrg       bitset<_Nb>&
   1056   1.1.1.2  mrg       operator|=(const bitset<_Nb>& __rhs) _GLIBCXX_NOEXCEPT
   1057       1.1  mrg       {
   1058       1.1  mrg 	this->_M_do_or(__rhs);
   1059       1.1  mrg 	return *this;
   1060       1.1  mrg       }
   1061       1.1  mrg 
   1062  1.1.1.14  mrg       _GLIBCXX23_CONSTEXPR
   1063       1.1  mrg       bitset<_Nb>&
   1064   1.1.1.2  mrg       operator^=(const bitset<_Nb>& __rhs) _GLIBCXX_NOEXCEPT
   1065       1.1  mrg       {
   1066       1.1  mrg 	this->_M_do_xor(__rhs);
   1067       1.1  mrg 	return *this;
   1068       1.1  mrg       }
   1069  1.1.1.12  mrg       ///@}
   1070   1.1.1.6  mrg 
   1071  1.1.1.12  mrg       ///@{
   1072       1.1  mrg       /**
   1073   1.1.1.2  mrg        *  Operations on bitsets.
   1074   1.1.1.2  mrg        *  @param  __position  The number of places to shift.
   1075       1.1  mrg        *
   1076       1.1  mrg        *  These should be self-explanatory.
   1077       1.1  mrg        */
   1078  1.1.1.14  mrg       _GLIBCXX23_CONSTEXPR
   1079       1.1  mrg       bitset<_Nb>&
   1080   1.1.1.2  mrg       operator<<=(size_t __position) _GLIBCXX_NOEXCEPT
   1081       1.1  mrg       {
   1082       1.1  mrg 	if (__builtin_expect(__position < _Nb, 1))
   1083       1.1  mrg 	  {
   1084       1.1  mrg 	    this->_M_do_left_shift(__position);
   1085       1.1  mrg 	    this->_M_do_sanitize();
   1086       1.1  mrg 	  }
   1087       1.1  mrg 	else
   1088       1.1  mrg 	  this->_M_do_reset();
   1089       1.1  mrg 	return *this;
   1090       1.1  mrg       }
   1091       1.1  mrg 
   1092  1.1.1.14  mrg       _GLIBCXX23_CONSTEXPR
   1093       1.1  mrg       bitset<_Nb>&
   1094   1.1.1.2  mrg       operator>>=(size_t __position) _GLIBCXX_NOEXCEPT
   1095       1.1  mrg       {
   1096       1.1  mrg 	if (__builtin_expect(__position < _Nb, 1))
   1097  1.1.1.14  mrg 	  this->_M_do_right_shift(__position);
   1098       1.1  mrg 	else
   1099       1.1  mrg 	  this->_M_do_reset();
   1100       1.1  mrg 	return *this;
   1101       1.1  mrg       }
   1102  1.1.1.12  mrg       ///@}
   1103   1.1.1.6  mrg 
   1104  1.1.1.12  mrg       ///@{
   1105       1.1  mrg       /**
   1106       1.1  mrg        *  These versions of single-bit set, reset, flip, and test are
   1107       1.1  mrg        *  extensions from the SGI version.  They do no range checking.
   1108       1.1  mrg        *  @ingroup SGIextensions
   1109       1.1  mrg        */
   1110  1.1.1.14  mrg       _GLIBCXX23_CONSTEXPR
   1111       1.1  mrg       bitset<_Nb>&
   1112   1.1.1.2  mrg       _Unchecked_set(size_t __pos) _GLIBCXX_NOEXCEPT
   1113       1.1  mrg       {
   1114       1.1  mrg 	this->_M_getword(__pos) |= _Base::_S_maskbit(__pos);
   1115       1.1  mrg 	return *this;
   1116       1.1  mrg       }
   1117       1.1  mrg 
   1118  1.1.1.14  mrg       _GLIBCXX23_CONSTEXPR
   1119       1.1  mrg       bitset<_Nb>&
   1120   1.1.1.2  mrg       _Unchecked_set(size_t __pos, int __val) _GLIBCXX_NOEXCEPT
   1121       1.1  mrg       {
   1122       1.1  mrg 	if (__val)
   1123       1.1  mrg 	  this->_M_getword(__pos) |= _Base::_S_maskbit(__pos);
   1124       1.1  mrg 	else
   1125       1.1  mrg 	  this->_M_getword(__pos) &= ~_Base::_S_maskbit(__pos);
   1126       1.1  mrg 	return *this;
   1127       1.1  mrg       }
   1128       1.1  mrg 
   1129  1.1.1.14  mrg       _GLIBCXX23_CONSTEXPR
   1130       1.1  mrg       bitset<_Nb>&
   1131   1.1.1.2  mrg       _Unchecked_reset(size_t __pos) _GLIBCXX_NOEXCEPT
   1132       1.1  mrg       {
   1133       1.1  mrg 	this->_M_getword(__pos) &= ~_Base::_S_maskbit(__pos);
   1134       1.1  mrg 	return *this;
   1135       1.1  mrg       }
   1136       1.1  mrg 
   1137  1.1.1.14  mrg       _GLIBCXX23_CONSTEXPR
   1138       1.1  mrg       bitset<_Nb>&
   1139   1.1.1.2  mrg       _Unchecked_flip(size_t __pos) _GLIBCXX_NOEXCEPT
   1140       1.1  mrg       {
   1141       1.1  mrg 	this->_M_getword(__pos) ^= _Base::_S_maskbit(__pos);
   1142       1.1  mrg 	return *this;
   1143       1.1  mrg       }
   1144       1.1  mrg 
   1145   1.1.1.2  mrg       _GLIBCXX_CONSTEXPR bool
   1146   1.1.1.2  mrg       _Unchecked_test(size_t __pos) const _GLIBCXX_NOEXCEPT
   1147       1.1  mrg       { return ((this->_M_getword(__pos) & _Base::_S_maskbit(__pos))
   1148       1.1  mrg 		!= static_cast<_WordT>(0)); }
   1149  1.1.1.12  mrg       ///@}
   1150   1.1.1.6  mrg 
   1151       1.1  mrg       // Set, reset, and flip.
   1152       1.1  mrg       /**
   1153       1.1  mrg        *  @brief Sets every bit to true.
   1154       1.1  mrg        */
   1155  1.1.1.14  mrg       _GLIBCXX23_CONSTEXPR
   1156       1.1  mrg       bitset<_Nb>&
   1157   1.1.1.2  mrg       set() _GLIBCXX_NOEXCEPT
   1158       1.1  mrg       {
   1159       1.1  mrg 	this->_M_do_set();
   1160       1.1  mrg 	this->_M_do_sanitize();
   1161       1.1  mrg 	return *this;
   1162       1.1  mrg       }
   1163       1.1  mrg 
   1164       1.1  mrg       /**
   1165       1.1  mrg        *  @brief Sets a given bit to a particular value.
   1166   1.1.1.2  mrg        *  @param  __position  The index of the bit.
   1167   1.1.1.2  mrg        *  @param  __val  Either true or false, defaults to true.
   1168       1.1  mrg        *  @throw  std::out_of_range  If @a pos is bigger the size of the %set.
   1169       1.1  mrg        */
   1170  1.1.1.14  mrg       _GLIBCXX23_CONSTEXPR
   1171       1.1  mrg       bitset<_Nb>&
   1172       1.1  mrg       set(size_t __position, bool __val = true)
   1173       1.1  mrg       {
   1174   1.1.1.3  mrg 	this->_M_check(__position, __N("bitset::set"));
   1175       1.1  mrg 	return _Unchecked_set(__position, __val);
   1176       1.1  mrg       }
   1177       1.1  mrg 
   1178       1.1  mrg       /**
   1179       1.1  mrg        *  @brief Sets every bit to false.
   1180       1.1  mrg        */
   1181  1.1.1.14  mrg       _GLIBCXX23_CONSTEXPR
   1182       1.1  mrg       bitset<_Nb>&
   1183   1.1.1.2  mrg       reset() _GLIBCXX_NOEXCEPT
   1184       1.1  mrg       {
   1185       1.1  mrg 	this->_M_do_reset();
   1186       1.1  mrg 	return *this;
   1187       1.1  mrg       }
   1188       1.1  mrg 
   1189       1.1  mrg       /**
   1190       1.1  mrg        *  @brief Sets a given bit to false.
   1191   1.1.1.2  mrg        *  @param  __position  The index of the bit.
   1192       1.1  mrg        *  @throw  std::out_of_range  If @a pos is bigger the size of the %set.
   1193       1.1  mrg        *
   1194       1.1  mrg        *  Same as writing @c set(pos,false).
   1195       1.1  mrg        */
   1196  1.1.1.14  mrg       _GLIBCXX23_CONSTEXPR
   1197       1.1  mrg       bitset<_Nb>&
   1198       1.1  mrg       reset(size_t __position)
   1199       1.1  mrg       {
   1200   1.1.1.3  mrg 	this->_M_check(__position, __N("bitset::reset"));
   1201       1.1  mrg 	return _Unchecked_reset(__position);
   1202       1.1  mrg       }
   1203   1.1.1.6  mrg 
   1204       1.1  mrg       /**
   1205       1.1  mrg        *  @brief Toggles every bit to its opposite value.
   1206       1.1  mrg        */
   1207  1.1.1.14  mrg       _GLIBCXX23_CONSTEXPR
   1208       1.1  mrg       bitset<_Nb>&
   1209   1.1.1.2  mrg       flip() _GLIBCXX_NOEXCEPT
   1210       1.1  mrg       {
   1211       1.1  mrg 	this->_M_do_flip();
   1212       1.1  mrg 	this->_M_do_sanitize();
   1213       1.1  mrg 	return *this;
   1214       1.1  mrg       }
   1215       1.1  mrg 
   1216       1.1  mrg       /**
   1217       1.1  mrg        *  @brief Toggles a given bit to its opposite value.
   1218   1.1.1.2  mrg        *  @param  __position  The index of the bit.
   1219       1.1  mrg        *  @throw  std::out_of_range  If @a pos is bigger the size of the %set.
   1220       1.1  mrg        */
   1221  1.1.1.14  mrg       _GLIBCXX23_CONSTEXPR
   1222       1.1  mrg       bitset<_Nb>&
   1223       1.1  mrg       flip(size_t __position)
   1224       1.1  mrg       {
   1225   1.1.1.3  mrg 	this->_M_check(__position, __N("bitset::flip"));
   1226       1.1  mrg 	return _Unchecked_flip(__position);
   1227       1.1  mrg       }
   1228   1.1.1.6  mrg 
   1229       1.1  mrg       /// See the no-argument flip().
   1230  1.1.1.14  mrg       _GLIBCXX23_CONSTEXPR
   1231       1.1  mrg       bitset<_Nb>
   1232   1.1.1.2  mrg       operator~() const _GLIBCXX_NOEXCEPT
   1233       1.1  mrg       { return bitset<_Nb>(*this).flip(); }
   1234       1.1  mrg 
   1235  1.1.1.12  mrg       ///@{
   1236       1.1  mrg       /**
   1237       1.1  mrg        *  @brief  Array-indexing support.
   1238   1.1.1.2  mrg        *  @param  __position  Index into the %bitset.
   1239   1.1.1.2  mrg        *  @return A bool for a <em>const %bitset</em>.  For non-const
   1240   1.1.1.2  mrg        *           bitsets, an instance of the reference proxy class.
   1241       1.1  mrg        *  @note  These operators do no range checking and throw no exceptions,
   1242       1.1  mrg        *         as required by DR 11 to the standard.
   1243       1.1  mrg        *
   1244       1.1  mrg        *  _GLIBCXX_RESOLVE_LIB_DEFECTS Note that this implementation already
   1245       1.1  mrg        *  resolves DR 11 (items 1 and 2), but does not do the range-checking
   1246       1.1  mrg        *  required by that DR's resolution.  -pme
   1247       1.1  mrg        *  The DR has since been changed:  range-checking is a precondition
   1248       1.1  mrg        *  (users' responsibility), and these functions must not throw.  -pme
   1249       1.1  mrg        */
   1250  1.1.1.14  mrg       _GLIBCXX23_CONSTEXPR
   1251       1.1  mrg       reference
   1252       1.1  mrg       operator[](size_t __position)
   1253   1.1.1.2  mrg       { return reference(*this, __position); }
   1254       1.1  mrg 
   1255   1.1.1.2  mrg       _GLIBCXX_CONSTEXPR bool
   1256       1.1  mrg       operator[](size_t __position) const
   1257       1.1  mrg       { return _Unchecked_test(__position); }
   1258  1.1.1.12  mrg       ///@}
   1259   1.1.1.6  mrg 
   1260       1.1  mrg       /**
   1261       1.1  mrg        *  @brief Returns a numerical interpretation of the %bitset.
   1262       1.1  mrg        *  @return  The integral equivalent of the bits.
   1263       1.1  mrg        *  @throw  std::overflow_error  If there are too many bits to be
   1264       1.1  mrg        *                               represented in an @c unsigned @c long.
   1265       1.1  mrg        */
   1266  1.1.1.14  mrg       _GLIBCXX23_CONSTEXPR
   1267       1.1  mrg       unsigned long
   1268       1.1  mrg       to_ulong() const
   1269       1.1  mrg       { return this->_M_do_to_ulong(); }
   1270       1.1  mrg 
   1271   1.1.1.2  mrg #if __cplusplus >= 201103L
   1272  1.1.1.14  mrg       _GLIBCXX23_CONSTEXPR
   1273       1.1  mrg       unsigned long long
   1274       1.1  mrg       to_ullong() const
   1275       1.1  mrg       { return this->_M_do_to_ullong(); }
   1276       1.1  mrg #endif
   1277       1.1  mrg 
   1278  1.1.1.14  mrg #if _GLIBCXX_HOSTED
   1279       1.1  mrg       /**
   1280       1.1  mrg        *  @brief Returns a character interpretation of the %bitset.
   1281       1.1  mrg        *  @return  The string equivalent of the bits.
   1282       1.1  mrg        *
   1283       1.1  mrg        *  Note the ordering of the bits:  decreasing character positions
   1284       1.1  mrg        *  correspond to increasing bit positions (see the main class notes for
   1285       1.1  mrg        *  an example).
   1286       1.1  mrg        */
   1287       1.1  mrg       template<class _CharT, class _Traits, class _Alloc>
   1288  1.1.1.14  mrg 	_GLIBCXX23_CONSTEXPR
   1289       1.1  mrg 	std::basic_string<_CharT, _Traits, _Alloc>
   1290       1.1  mrg 	to_string() const
   1291       1.1  mrg 	{
   1292       1.1  mrg 	  std::basic_string<_CharT, _Traits, _Alloc> __result;
   1293       1.1  mrg 	  _M_copy_to_string(__result, _CharT('0'), _CharT('1'));
   1294       1.1  mrg 	  return __result;
   1295       1.1  mrg 	}
   1296       1.1  mrg 
   1297       1.1  mrg       // _GLIBCXX_RESOLVE_LIB_DEFECTS
   1298       1.1  mrg       // 396. what are characters zero and one.
   1299       1.1  mrg       template<class _CharT, class _Traits, class _Alloc>
   1300  1.1.1.14  mrg 	_GLIBCXX23_CONSTEXPR
   1301       1.1  mrg 	std::basic_string<_CharT, _Traits, _Alloc>
   1302       1.1  mrg 	to_string(_CharT __zero, _CharT __one = _CharT('1')) const
   1303       1.1  mrg 	{
   1304       1.1  mrg 	  std::basic_string<_CharT, _Traits, _Alloc> __result;
   1305       1.1  mrg 	  _M_copy_to_string(__result, __zero, __one);
   1306       1.1  mrg 	  return __result;
   1307       1.1  mrg 	}
   1308       1.1  mrg 
   1309       1.1  mrg       // _GLIBCXX_RESOLVE_LIB_DEFECTS
   1310       1.1  mrg       // 434. bitset::to_string() hard to use.
   1311       1.1  mrg       template<class _CharT, class _Traits>
   1312  1.1.1.14  mrg 	_GLIBCXX23_CONSTEXPR
   1313       1.1  mrg 	std::basic_string<_CharT, _Traits, std::allocator<_CharT> >
   1314       1.1  mrg 	to_string() const
   1315       1.1  mrg 	{ return to_string<_CharT, _Traits, std::allocator<_CharT> >(); }
   1316       1.1  mrg 
   1317       1.1  mrg       // _GLIBCXX_RESOLVE_LIB_DEFECTS
   1318       1.1  mrg       // 853. to_string needs updating with zero and one.
   1319       1.1  mrg       template<class _CharT, class _Traits>
   1320  1.1.1.14  mrg 	_GLIBCXX23_CONSTEXPR
   1321       1.1  mrg 	std::basic_string<_CharT, _Traits, std::allocator<_CharT> >
   1322       1.1  mrg 	to_string(_CharT __zero, _CharT __one = _CharT('1')) const
   1323       1.1  mrg 	{ return to_string<_CharT, _Traits,
   1324       1.1  mrg 	                   std::allocator<_CharT> >(__zero, __one); }
   1325       1.1  mrg 
   1326       1.1  mrg       template<class _CharT>
   1327  1.1.1.14  mrg 	_GLIBCXX23_CONSTEXPR
   1328       1.1  mrg 	std::basic_string<_CharT, std::char_traits<_CharT>,
   1329       1.1  mrg 	                  std::allocator<_CharT> >
   1330       1.1  mrg 	to_string() const
   1331       1.1  mrg 	{
   1332       1.1  mrg 	  return to_string<_CharT, std::char_traits<_CharT>,
   1333       1.1  mrg 	                   std::allocator<_CharT> >();
   1334       1.1  mrg 	}
   1335       1.1  mrg 
   1336       1.1  mrg       template<class _CharT>
   1337  1.1.1.14  mrg 	_GLIBCXX23_CONSTEXPR
   1338       1.1  mrg 	std::basic_string<_CharT, std::char_traits<_CharT>,
   1339       1.1  mrg 	                  std::allocator<_CharT> >
   1340       1.1  mrg 	to_string(_CharT __zero, _CharT __one = _CharT('1')) const
   1341       1.1  mrg 	{
   1342       1.1  mrg 	  return to_string<_CharT, std::char_traits<_CharT>,
   1343       1.1  mrg 	                   std::allocator<_CharT> >(__zero, __one);
   1344       1.1  mrg 	}
   1345       1.1  mrg 
   1346  1.1.1.14  mrg       _GLIBCXX23_CONSTEXPR
   1347       1.1  mrg       std::basic_string<char, std::char_traits<char>, std::allocator<char> >
   1348       1.1  mrg       to_string() const
   1349       1.1  mrg       {
   1350       1.1  mrg 	return to_string<char, std::char_traits<char>,
   1351       1.1  mrg 	                 std::allocator<char> >();
   1352       1.1  mrg       }
   1353       1.1  mrg 
   1354  1.1.1.14  mrg       _GLIBCXX23_CONSTEXPR
   1355       1.1  mrg       std::basic_string<char, std::char_traits<char>, std::allocator<char> >
   1356       1.1  mrg       to_string(char __zero, char __one = '1') const
   1357       1.1  mrg       {
   1358       1.1  mrg 	return to_string<char, std::char_traits<char>,
   1359       1.1  mrg 	                 std::allocator<char> >(__zero, __one);
   1360       1.1  mrg       }
   1361  1.1.1.14  mrg #endif // HOSTED
   1362       1.1  mrg 
   1363       1.1  mrg       /// Returns the number of bits which are set.
   1364  1.1.1.14  mrg       _GLIBCXX23_CONSTEXPR
   1365       1.1  mrg       size_t
   1366   1.1.1.2  mrg       count() const _GLIBCXX_NOEXCEPT
   1367       1.1  mrg       { return this->_M_do_count(); }
   1368       1.1  mrg 
   1369       1.1  mrg       /// Returns the total number of bits.
   1370   1.1.1.2  mrg       _GLIBCXX_CONSTEXPR size_t
   1371   1.1.1.2  mrg       size() const _GLIBCXX_NOEXCEPT
   1372       1.1  mrg       { return _Nb; }
   1373       1.1  mrg 
   1374  1.1.1.12  mrg       ///@{
   1375       1.1  mrg       /// These comparisons for equality/inequality are, well, @e bitwise.
   1376  1.1.1.14  mrg       _GLIBCXX23_CONSTEXPR
   1377       1.1  mrg       bool
   1378   1.1.1.2  mrg       operator==(const bitset<_Nb>& __rhs) const _GLIBCXX_NOEXCEPT
   1379       1.1  mrg       { return this->_M_is_equal(__rhs); }
   1380       1.1  mrg 
   1381  1.1.1.11  mrg #if __cpp_impl_three_way_comparison < 201907L
   1382  1.1.1.14  mrg       _GLIBCXX23_CONSTEXPR
   1383       1.1  mrg       bool
   1384   1.1.1.2  mrg       operator!=(const bitset<_Nb>& __rhs) const _GLIBCXX_NOEXCEPT
   1385       1.1  mrg       { return !this->_M_is_equal(__rhs); }
   1386  1.1.1.11  mrg #endif
   1387  1.1.1.12  mrg       ///@}
   1388   1.1.1.6  mrg 
   1389       1.1  mrg       /**
   1390       1.1  mrg        *  @brief Tests the value of a bit.
   1391   1.1.1.2  mrg        *  @param  __position  The index of a bit.
   1392       1.1  mrg        *  @return  The value at @a pos.
   1393       1.1  mrg        *  @throw  std::out_of_range  If @a pos is bigger the size of the %set.
   1394       1.1  mrg        */
   1395  1.1.1.14  mrg       _GLIBCXX23_CONSTEXPR
   1396       1.1  mrg       bool
   1397       1.1  mrg       test(size_t __position) const
   1398       1.1  mrg       {
   1399   1.1.1.3  mrg 	this->_M_check(__position, __N("bitset::test"));
   1400       1.1  mrg 	return _Unchecked_test(__position);
   1401       1.1  mrg       }
   1402       1.1  mrg 
   1403       1.1  mrg       // _GLIBCXX_RESOLVE_LIB_DEFECTS
   1404       1.1  mrg       // DR 693. std::bitset::all() missing.
   1405       1.1  mrg       /**
   1406       1.1  mrg        *  @brief Tests whether all the bits are on.
   1407       1.1  mrg        *  @return  True if all the bits are set.
   1408       1.1  mrg        */
   1409  1.1.1.14  mrg       _GLIBCXX23_CONSTEXPR
   1410       1.1  mrg       bool
   1411   1.1.1.2  mrg       all() const _GLIBCXX_NOEXCEPT
   1412   1.1.1.2  mrg       { return this->template _M_are_all<_Nb>(); }
   1413       1.1  mrg 
   1414       1.1  mrg       /**
   1415       1.1  mrg        *  @brief Tests whether any of the bits are on.
   1416       1.1  mrg        *  @return  True if at least one bit is set.
   1417       1.1  mrg        */
   1418  1.1.1.14  mrg       _GLIBCXX23_CONSTEXPR
   1419       1.1  mrg       bool
   1420   1.1.1.2  mrg       any() const _GLIBCXX_NOEXCEPT
   1421       1.1  mrg       { return this->_M_is_any(); }
   1422       1.1  mrg 
   1423       1.1  mrg       /**
   1424       1.1  mrg        *  @brief Tests whether any of the bits are on.
   1425       1.1  mrg        *  @return  True if none of the bits are set.
   1426       1.1  mrg        */
   1427  1.1.1.14  mrg       _GLIBCXX23_CONSTEXPR
   1428       1.1  mrg       bool
   1429   1.1.1.2  mrg       none() const _GLIBCXX_NOEXCEPT
   1430       1.1  mrg       { return !this->_M_is_any(); }
   1431       1.1  mrg 
   1432  1.1.1.12  mrg       ///@{
   1433       1.1  mrg       /// Self-explanatory.
   1434  1.1.1.14  mrg       _GLIBCXX23_CONSTEXPR
   1435       1.1  mrg       bitset<_Nb>
   1436   1.1.1.2  mrg       operator<<(size_t __position) const _GLIBCXX_NOEXCEPT
   1437       1.1  mrg       { return bitset<_Nb>(*this) <<= __position; }
   1438       1.1  mrg 
   1439  1.1.1.14  mrg       _GLIBCXX23_CONSTEXPR
   1440       1.1  mrg       bitset<_Nb>
   1441   1.1.1.2  mrg       operator>>(size_t __position) const _GLIBCXX_NOEXCEPT
   1442       1.1  mrg       { return bitset<_Nb>(*this) >>= __position; }
   1443  1.1.1.12  mrg       ///@}
   1444   1.1.1.6  mrg 
   1445       1.1  mrg       /**
   1446       1.1  mrg        *  @brief  Finds the index of the first "on" bit.
   1447       1.1  mrg        *  @return  The index of the first bit set, or size() if not found.
   1448       1.1  mrg        *  @ingroup SGIextensions
   1449       1.1  mrg        *  @sa  _Find_next
   1450       1.1  mrg        */
   1451  1.1.1.14  mrg       _GLIBCXX23_CONSTEXPR
   1452       1.1  mrg       size_t
   1453   1.1.1.2  mrg       _Find_first() const _GLIBCXX_NOEXCEPT
   1454       1.1  mrg       { return this->_M_do_find_first(_Nb); }
   1455       1.1  mrg 
   1456       1.1  mrg       /**
   1457       1.1  mrg        *  @brief  Finds the index of the next "on" bit after prev.
   1458       1.1  mrg        *  @return  The index of the next bit set, or size() if not found.
   1459   1.1.1.2  mrg        *  @param  __prev  Where to start searching.
   1460       1.1  mrg        *  @ingroup SGIextensions
   1461       1.1  mrg        *  @sa  _Find_first
   1462       1.1  mrg        */
   1463  1.1.1.14  mrg       _GLIBCXX23_CONSTEXPR
   1464       1.1  mrg       size_t
   1465   1.1.1.2  mrg       _Find_next(size_t __prev) const _GLIBCXX_NOEXCEPT
   1466       1.1  mrg       { return this->_M_do_find_next(__prev, _Nb); }
   1467  1.1.1.14  mrg 
   1468  1.1.1.14  mrg     private:
   1469  1.1.1.14  mrg       // Helper functions for string operations.
   1470  1.1.1.14  mrg       template<class _CharT, class _Traits>
   1471  1.1.1.14  mrg 	_GLIBCXX23_CONSTEXPR
   1472  1.1.1.14  mrg         void
   1473  1.1.1.14  mrg         _M_copy_from_ptr(const _CharT*, size_t, size_t, size_t,
   1474  1.1.1.14  mrg 			 _CharT, _CharT);
   1475  1.1.1.14  mrg 
   1476  1.1.1.14  mrg #if _GLIBCXX_HOSTED
   1477  1.1.1.14  mrg       template<class _CharT, class _Traits, class _Alloc>
   1478  1.1.1.14  mrg 	_GLIBCXX23_CONSTEXPR
   1479  1.1.1.14  mrg 	void
   1480  1.1.1.14  mrg 	_M_copy_from_string(const std::basic_string<_CharT,
   1481  1.1.1.14  mrg 			    _Traits, _Alloc>& __s, size_t __pos, size_t __n,
   1482  1.1.1.14  mrg 			    _CharT __zero, _CharT __one)
   1483  1.1.1.14  mrg 	{ _M_copy_from_ptr<_CharT, _Traits>(__s.data(), __s.size(), __pos, __n,
   1484  1.1.1.14  mrg 					    __zero, __one); }
   1485  1.1.1.14  mrg 
   1486  1.1.1.14  mrg       template<class _CharT, class _Traits, class _Alloc>
   1487  1.1.1.14  mrg 	_GLIBCXX23_CONSTEXPR
   1488  1.1.1.14  mrg 	void
   1489  1.1.1.14  mrg         _M_copy_to_string(std::basic_string<_CharT, _Traits, _Alloc>&,
   1490  1.1.1.14  mrg 			  _CharT, _CharT) const;
   1491  1.1.1.14  mrg 
   1492  1.1.1.14  mrg       template<class _CharT, class _Traits, size_t _Nb2>
   1493  1.1.1.14  mrg 	friend std::basic_istream<_CharT, _Traits>&
   1494  1.1.1.14  mrg 	operator>>(std::basic_istream<_CharT, _Traits>&, bitset<_Nb2>&);
   1495  1.1.1.14  mrg 
   1496  1.1.1.14  mrg       template <class _CharT, class _Traits, size_t _Nb2>
   1497  1.1.1.14  mrg 	friend std::basic_ostream<_CharT, _Traits>&
   1498  1.1.1.14  mrg 	operator<<(std::basic_ostream<_CharT, _Traits>&, const bitset<_Nb2>&);
   1499  1.1.1.14  mrg #endif
   1500       1.1  mrg     };
   1501       1.1  mrg 
   1502       1.1  mrg   // Definitions of non-inline member functions.
   1503       1.1  mrg   template<size_t _Nb>
   1504       1.1  mrg     template<class _CharT, class _Traits>
   1505  1.1.1.14  mrg       _GLIBCXX23_CONSTEXPR
   1506       1.1  mrg       void
   1507       1.1  mrg       bitset<_Nb>::
   1508       1.1  mrg       _M_copy_from_ptr(const _CharT* __s, size_t __len,
   1509       1.1  mrg 		       size_t __pos, size_t __n, _CharT __zero, _CharT __one)
   1510       1.1  mrg       {
   1511       1.1  mrg 	reset();
   1512   1.1.1.2  mrg 	const size_t __nbits = std::min(_Nb, std::min(__n, size_t(__len - __pos)));
   1513       1.1  mrg 	for (size_t __i = __nbits; __i > 0; --__i)
   1514       1.1  mrg 	  {
   1515       1.1  mrg 	    const _CharT __c = __s[__pos + __nbits - __i];
   1516       1.1  mrg 	    if (_Traits::eq(__c, __zero))
   1517       1.1  mrg 	      ;
   1518       1.1  mrg 	    else if (_Traits::eq(__c, __one))
   1519       1.1  mrg 	      _Unchecked_set(__i - 1);
   1520       1.1  mrg 	    else
   1521       1.1  mrg 	      __throw_invalid_argument(__N("bitset::_M_copy_from_ptr"));
   1522       1.1  mrg 	  }
   1523       1.1  mrg       }
   1524       1.1  mrg 
   1525  1.1.1.14  mrg #if _GLIBCXX_HOSTED
   1526       1.1  mrg   template<size_t _Nb>
   1527       1.1  mrg     template<class _CharT, class _Traits, class _Alloc>
   1528  1.1.1.14  mrg       _GLIBCXX23_CONSTEXPR
   1529       1.1  mrg       void
   1530       1.1  mrg       bitset<_Nb>::
   1531       1.1  mrg       _M_copy_to_string(std::basic_string<_CharT, _Traits, _Alloc>& __s,
   1532       1.1  mrg 			_CharT __zero, _CharT __one) const
   1533       1.1  mrg       {
   1534       1.1  mrg 	__s.assign(_Nb, __zero);
   1535  1.1.1.13  mrg 	size_t __n = this->_Find_first();
   1536  1.1.1.13  mrg 	while (__n < _Nb)
   1537  1.1.1.13  mrg 	  {
   1538  1.1.1.13  mrg 	    __s[_Nb - __n - 1] = __one;
   1539  1.1.1.13  mrg 	    __n = _Find_next(__n);
   1540  1.1.1.13  mrg 	  }
   1541       1.1  mrg       }
   1542  1.1.1.14  mrg #endif // HOSTED
   1543       1.1  mrg 
   1544       1.1  mrg   // 23.3.5.3 bitset operations:
   1545  1.1.1.12  mrg   ///@{
   1546       1.1  mrg   /**
   1547       1.1  mrg    *  @brief  Global bitwise operations on bitsets.
   1548   1.1.1.2  mrg    *  @param  __x  A bitset.
   1549   1.1.1.2  mrg    *  @param  __y  A bitset of the same size as @a __x.
   1550       1.1  mrg    *  @return  A new bitset.
   1551       1.1  mrg    *
   1552       1.1  mrg    *  These should be self-explanatory.
   1553       1.1  mrg   */
   1554       1.1  mrg   template<size_t _Nb>
   1555  1.1.1.14  mrg     _GLIBCXX23_CONSTEXPR
   1556       1.1  mrg     inline bitset<_Nb>
   1557   1.1.1.2  mrg     operator&(const bitset<_Nb>& __x, const bitset<_Nb>& __y) _GLIBCXX_NOEXCEPT
   1558       1.1  mrg     {
   1559       1.1  mrg       bitset<_Nb> __result(__x);
   1560       1.1  mrg       __result &= __y;
   1561       1.1  mrg       return __result;
   1562       1.1  mrg     }
   1563       1.1  mrg 
   1564       1.1  mrg   template<size_t _Nb>
   1565  1.1.1.14  mrg     _GLIBCXX23_CONSTEXPR
   1566       1.1  mrg     inline bitset<_Nb>
   1567   1.1.1.2  mrg     operator|(const bitset<_Nb>& __x, const bitset<_Nb>& __y) _GLIBCXX_NOEXCEPT
   1568       1.1  mrg     {
   1569       1.1  mrg       bitset<_Nb> __result(__x);
   1570       1.1  mrg       __result |= __y;
   1571       1.1  mrg       return __result;
   1572       1.1  mrg     }
   1573       1.1  mrg 
   1574       1.1  mrg   template <size_t _Nb>
   1575  1.1.1.14  mrg     _GLIBCXX23_CONSTEXPR
   1576       1.1  mrg     inline bitset<_Nb>
   1577   1.1.1.2  mrg     operator^(const bitset<_Nb>& __x, const bitset<_Nb>& __y) _GLIBCXX_NOEXCEPT
   1578       1.1  mrg     {
   1579       1.1  mrg       bitset<_Nb> __result(__x);
   1580       1.1  mrg       __result ^= __y;
   1581       1.1  mrg       return __result;
   1582       1.1  mrg     }
   1583  1.1.1.12  mrg   ///@}
   1584       1.1  mrg 
   1585  1.1.1.14  mrg #if _GLIBCXX_HOSTED
   1586  1.1.1.12  mrg   ///@{
   1587       1.1  mrg   /**
   1588       1.1  mrg    *  @brief Global I/O operators for bitsets.
   1589       1.1  mrg    *
   1590       1.1  mrg    *  Direct I/O between streams and bitsets is supported.  Output is
   1591       1.1  mrg    *  straightforward.  Input will skip whitespace, only accept @a 0 and @a 1
   1592       1.1  mrg    *  characters, and will only extract as many digits as the %bitset will
   1593       1.1  mrg    *  hold.
   1594       1.1  mrg   */
   1595       1.1  mrg   template<class _CharT, class _Traits, size_t _Nb>
   1596       1.1  mrg     std::basic_istream<_CharT, _Traits>&
   1597       1.1  mrg     operator>>(std::basic_istream<_CharT, _Traits>& __is, bitset<_Nb>& __x)
   1598       1.1  mrg     {
   1599       1.1  mrg       typedef typename _Traits::char_type          char_type;
   1600       1.1  mrg       typedef std::basic_istream<_CharT, _Traits>  __istream_type;
   1601       1.1  mrg       typedef typename __istream_type::ios_base    __ios_base;
   1602       1.1  mrg 
   1603  1.1.1.14  mrg       struct _Buffer
   1604  1.1.1.14  mrg       {
   1605  1.1.1.14  mrg 	static _GLIBCXX_CONSTEXPR bool _S_use_alloca() { return _Nb <= 256; }
   1606  1.1.1.14  mrg 
   1607  1.1.1.14  mrg 	explicit _Buffer(_CharT* __p) : _M_ptr(__p) { }
   1608  1.1.1.14  mrg 
   1609  1.1.1.14  mrg 	~_Buffer()
   1610  1.1.1.14  mrg 	{
   1611  1.1.1.14  mrg 	  if _GLIBCXX17_CONSTEXPR (!_S_use_alloca())
   1612  1.1.1.14  mrg 	    delete[] _M_ptr;
   1613  1.1.1.14  mrg 	}
   1614  1.1.1.14  mrg 
   1615  1.1.1.14  mrg 	_CharT* const _M_ptr;
   1616  1.1.1.14  mrg       };
   1617  1.1.1.14  mrg       _CharT* __ptr;
   1618  1.1.1.14  mrg       if _GLIBCXX17_CONSTEXPR (_Buffer::_S_use_alloca())
   1619  1.1.1.14  mrg 	__ptr = (_CharT*)__builtin_alloca(_Nb);
   1620  1.1.1.14  mrg       else
   1621  1.1.1.14  mrg 	__ptr = new _CharT[_Nb];
   1622  1.1.1.14  mrg       const _Buffer __buf(__ptr);
   1623       1.1  mrg 
   1624       1.1  mrg       // _GLIBCXX_RESOLVE_LIB_DEFECTS
   1625       1.1  mrg       // 303. Bitset input operator underspecified
   1626       1.1  mrg       const char_type __zero = __is.widen('0');
   1627       1.1  mrg       const char_type __one = __is.widen('1');
   1628       1.1  mrg 
   1629       1.1  mrg       typename __ios_base::iostate __state = __ios_base::goodbit;
   1630       1.1  mrg       typename __istream_type::sentry __sentry(__is);
   1631       1.1  mrg       if (__sentry)
   1632       1.1  mrg 	{
   1633       1.1  mrg 	  __try
   1634       1.1  mrg 	    {
   1635       1.1  mrg 	      for (size_t __i = _Nb; __i > 0; --__i)
   1636       1.1  mrg 		{
   1637       1.1  mrg 		  static typename _Traits::int_type __eof = _Traits::eof();
   1638   1.1.1.6  mrg 
   1639       1.1  mrg 		  typename _Traits::int_type __c1 = __is.rdbuf()->sbumpc();
   1640       1.1  mrg 		  if (_Traits::eq_int_type(__c1, __eof))
   1641       1.1  mrg 		    {
   1642       1.1  mrg 		      __state |= __ios_base::eofbit;
   1643       1.1  mrg 		      break;
   1644       1.1  mrg 		    }
   1645       1.1  mrg 		  else
   1646       1.1  mrg 		    {
   1647       1.1  mrg 		      const char_type __c2 = _Traits::to_char_type(__c1);
   1648       1.1  mrg 		      if (_Traits::eq(__c2, __zero))
   1649  1.1.1.14  mrg 			*__ptr++ = __zero;
   1650       1.1  mrg 		      else if (_Traits::eq(__c2, __one))
   1651  1.1.1.14  mrg 			*__ptr++ = __one;
   1652       1.1  mrg 		      else if (_Traits::
   1653       1.1  mrg 			       eq_int_type(__is.rdbuf()->sputbackc(__c2),
   1654       1.1  mrg 					   __eof))
   1655       1.1  mrg 			{
   1656       1.1  mrg 			  __state |= __ios_base::failbit;
   1657       1.1  mrg 			  break;
   1658       1.1  mrg 			}
   1659       1.1  mrg 		    }
   1660       1.1  mrg 		}
   1661       1.1  mrg 	    }
   1662       1.1  mrg 	  __catch(__cxxabiv1::__forced_unwind&)
   1663       1.1  mrg 	    {
   1664   1.1.1.6  mrg 	      __is._M_setstate(__ios_base::badbit);
   1665       1.1  mrg 	      __throw_exception_again;
   1666       1.1  mrg 	    }
   1667       1.1  mrg 	  __catch(...)
   1668       1.1  mrg 	    { __is._M_setstate(__ios_base::badbit); }
   1669       1.1  mrg 	}
   1670       1.1  mrg 
   1671  1.1.1.14  mrg       if _GLIBCXX17_CONSTEXPR (_Nb)
   1672  1.1.1.14  mrg       {
   1673  1.1.1.14  mrg 	if (size_t __len = __ptr - __buf._M_ptr)
   1674  1.1.1.14  mrg 	  __x.template _M_copy_from_ptr<_CharT, _Traits>(__buf._M_ptr, __len,
   1675  1.1.1.14  mrg 							 0, __len,
   1676  1.1.1.14  mrg 							 __zero, __one);
   1677  1.1.1.14  mrg 	else
   1678  1.1.1.14  mrg 	  __state |= __ios_base::failbit;
   1679  1.1.1.14  mrg       }
   1680       1.1  mrg       if (__state)
   1681       1.1  mrg 	__is.setstate(__state);
   1682       1.1  mrg       return __is;
   1683       1.1  mrg     }
   1684       1.1  mrg 
   1685       1.1  mrg   template <class _CharT, class _Traits, size_t _Nb>
   1686       1.1  mrg     std::basic_ostream<_CharT, _Traits>&
   1687       1.1  mrg     operator<<(std::basic_ostream<_CharT, _Traits>& __os,
   1688       1.1  mrg 	       const bitset<_Nb>& __x)
   1689       1.1  mrg     {
   1690       1.1  mrg       std::basic_string<_CharT, _Traits> __tmp;
   1691       1.1  mrg 
   1692       1.1  mrg       // _GLIBCXX_RESOLVE_LIB_DEFECTS
   1693       1.1  mrg       // 396. what are characters zero and one.
   1694       1.1  mrg       const ctype<_CharT>& __ct = use_facet<ctype<_CharT> >(__os.getloc());
   1695       1.1  mrg       __x._M_copy_to_string(__tmp, __ct.widen('0'), __ct.widen('1'));
   1696       1.1  mrg       return __os << __tmp;
   1697       1.1  mrg     }
   1698  1.1.1.12  mrg   ///@}
   1699  1.1.1.14  mrg #endif // HOSTED
   1700       1.1  mrg 
   1701   1.1.1.2  mrg _GLIBCXX_END_NAMESPACE_CONTAINER
   1702   1.1.1.2  mrg } // namespace std
   1703       1.1  mrg 
   1704       1.1  mrg #undef _GLIBCXX_BITSET_WORDS
   1705       1.1  mrg #undef _GLIBCXX_BITSET_BITS_PER_WORD
   1706   1.1.1.2  mrg #undef _GLIBCXX_BITSET_BITS_PER_ULL
   1707       1.1  mrg 
   1708   1.1.1.2  mrg #if __cplusplus >= 201103L
   1709       1.1  mrg 
   1710   1.1.1.2  mrg namespace std _GLIBCXX_VISIBILITY(default)
   1711   1.1.1.2  mrg {
   1712   1.1.1.2  mrg _GLIBCXX_BEGIN_NAMESPACE_VERSION
   1713       1.1  mrg 
   1714       1.1  mrg   // DR 1182.
   1715       1.1  mrg   /// std::hash specialization for bitset.
   1716       1.1  mrg   template<size_t _Nb>
   1717   1.1.1.2  mrg     struct hash<_GLIBCXX_STD_C::bitset<_Nb>>
   1718   1.1.1.2  mrg     : public __hash_base<size_t, _GLIBCXX_STD_C::bitset<_Nb>>
   1719       1.1  mrg     {
   1720       1.1  mrg       size_t
   1721   1.1.1.2  mrg       operator()(const _GLIBCXX_STD_C::bitset<_Nb>& __b) const noexcept
   1722       1.1  mrg       {
   1723       1.1  mrg 	const size_t __clength = (_Nb + __CHAR_BIT__ - 1) / __CHAR_BIT__;
   1724   1.1.1.2  mrg 	return std::_Hash_impl::hash(__b._M_getdata(), __clength);
   1725       1.1  mrg       }
   1726       1.1  mrg     };
   1727       1.1  mrg 
   1728       1.1  mrg   template<>
   1729   1.1.1.2  mrg     struct hash<_GLIBCXX_STD_C::bitset<0>>
   1730   1.1.1.2  mrg     : public __hash_base<size_t, _GLIBCXX_STD_C::bitset<0>>
   1731       1.1  mrg     {
   1732       1.1  mrg       size_t
   1733   1.1.1.2  mrg       operator()(const _GLIBCXX_STD_C::bitset<0>&) const noexcept
   1734       1.1  mrg       { return 0; }
   1735       1.1  mrg     };
   1736       1.1  mrg 
   1737   1.1.1.2  mrg _GLIBCXX_END_NAMESPACE_VERSION
   1738   1.1.1.2  mrg } // namespace
   1739       1.1  mrg 
   1740   1.1.1.2  mrg #endif // C++11
   1741       1.1  mrg 
   1742  1.1.1.14  mrg #if defined _GLIBCXX_DEBUG && _GLIBCXX_HOSTED
   1743       1.1  mrg # include <debug/bitset>
   1744       1.1  mrg #endif
   1745       1.1  mrg 
   1746       1.1  mrg #endif /* _GLIBCXX_BITSET */
   1747