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