bitset

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