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