bitset

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

Generated on 11 Jan 2010 for libstdc++ by  doxygen 1.6.1