epoc32/include/stdapis/stlportv5/stl/_bitset.h
author William Roberts <williamr@symbian.org>
Wed, 31 Mar 2010 12:33:34 +0100
branchSymbian3
changeset 4 837f303aceeb
parent 3 e1b950c65cb4
permissions -rw-r--r--
Current Symbian^3 public API header files (from PDK 3.0.h)
This is the epoc32/include tree with the "platform" subtrees removed, and
all but a selected few mbg and rsg files removed.
     1 /*
     2  * Copyright (c) 1998
     3  * Silicon Graphics Computer Systems, Inc.
     4  *
     5  * Copyright (c) 1999
     6  * Boris Fomitchev
     7  *
     8  * This material is provided "as is", with absolutely no warranty expressed
     9  * or implied. Any use is at your own risk.
    10  *
    11  * Permission to use or copy this software for any purpose is hereby granted
    12  * without fee, provided the above notices are retained on all copies.
    13  * Permission to modify the code and to distribute modified code is granted,
    14  * provided the above notices are retained, and a notice that the code was
    15  * modified is included with the above copyright notice.
    16  *
    17  */
    18 
    19 #ifndef _STLP_BITSET_H
    20 #define _STLP_BITSET_H
    21 
    22 // A bitset of size N has N % (sizeof(unsigned long) * CHAR_BIT) unused
    23 // bits.  (They are the high- order bits in the highest word.)  It is
    24 // a class invariant of class bitset<> that those unused bits are
    25 // always zero.
    26 
    27 // Most of the actual code isn't contained in bitset<> itself, but in the
    28 // base class _Base_bitset.  The base class works with whole words, not with
    29 // individual bits.  This allows us to specialize _Base_bitset for the
    30 // important special case where the bitset is only a single word.
    31 
    32 // The C++ standard does not define the precise semantics of operator[].
    33 // In this implementation the const version of operator[] is equivalent
    34 // to test(), except that it does no range checking.  The non-const version
    35 // returns a reference to a bit, again without doing any range checking.
    36 
    37 
    38 #ifndef _STLP_INTERNAL_ALGOBASE_H
    39 #  include <stl/_algobase.h>
    40 #endif
    41 
    42 #ifndef _STLP_INTERNAL_ALLOC_H
    43 #  include <stl/_alloc.h>
    44 #endif
    45 
    46 #ifndef _STLP_INTERNAL_ITERATOR_H
    47 #  include <stl/_iterator.h>
    48 #endif
    49 
    50 #ifndef _STLP_INTERNAL_UNINITIALIZED_H
    51 #  include <stl/_uninitialized.h>
    52 #endif
    53 
    54 #ifndef _STLP_RANGE_ERRORS_H
    55 #  include <stl/_range_errors.h>
    56 #endif
    57 
    58 #ifndef _STLP_INTERNAL_STRING_H
    59 #  include <stl/_string.h>
    60 #endif
    61 
    62 #define __BITS_PER_WORD (CHAR_BIT*sizeof(unsigned long))
    63 #define __BITSET_WORDS(__n) ((__n + __BITS_PER_WORD - 1)/__BITS_PER_WORD)
    64 
    65 _STLP_BEGIN_NAMESPACE
    66 
    67 _STLP_MOVE_TO_PRIV_NAMESPACE
    68 
    69 // structure to aid in counting bits
    70 class _STLP_CLASS_DECLSPEC _Bs_G
    71 {
    72   public:
    73     //returns the number of bit set within the buffer between __beg and __end.
    74    #if defined (__SYMBIAN32__)
    75     _STLP_DECLSPEC 
    76 #endif 
    77 	  static size_t _S_count(const unsigned char *__beg, const unsigned char *__end)
    78 #if defined (_STLP_USE_NO_IOSTREAMS)
    79     {
    80       size_t __result = 0;
    81       for (; __beg != __end; ++__beg) {
    82         for (size_t i = 0; i < (sizeof(unsigned char) * 8); ++i) {
    83           if ((*__beg & (1 << i)) != 0) { ++__result; }
    84         }
    85       }
    86       return __result;
    87     }
    88 #else
    89       ;
    90 #endif
    91     // Mapping from 8 bit unsigned integers to the index of the first one bit set:
    92 #if defined (__SYMBIAN32__)
    93     _STLP_DECLSPEC 
    94 #endif
    95 	static unsigned char _S_first_one(unsigned char __x)
    96 #if defined (_STLP_USE_NO_IOSTREAMS)
    97     {
    98       for (unsigned char i = 0; i < (sizeof(unsigned char) * 8); ++i) {
    99         if ((__x & (1 << i)) != 0) { return i; }
   100       }
   101       return 0;
   102     }
   103 #else
   104       ;
   105 #endif
   106 };
   107 
   108 //
   109 // Base class: general case.
   110 //
   111 
   112 template<size_t _Nw>
   113 struct _Base_bitset {
   114   typedef unsigned long _WordT;
   115 
   116   _WordT _M_w[_Nw];                // 0 is the least significant word.
   117 
   118   _Base_bitset() { _M_do_reset(); }
   119 
   120   _Base_bitset(unsigned long __val) {
   121     _M_do_reset();
   122     _M_w[0] = __val;
   123   }
   124 
   125   static size_t _STLP_CALL _S_whichword( size_t __pos ) {
   126     return __pos / __BITS_PER_WORD;
   127   }
   128   static size_t _STLP_CALL _S_whichbyte( size_t __pos ) {
   129     return (__pos % __BITS_PER_WORD) / CHAR_BIT;
   130   }
   131   static size_t _STLP_CALL _S_whichbit( size_t __pos ) {
   132     return __pos % __BITS_PER_WORD;
   133   }
   134   static _WordT _STLP_CALL _S_maskbit( size_t __pos ) {
   135     return __STATIC_CAST(_WordT,1) << _S_whichbit(__pos);
   136   }
   137 
   138   _WordT& _M_getword(size_t __pos)       { return _M_w[_S_whichword(__pos)]; }
   139   _WordT  _M_getword(size_t __pos) const { return _M_w[_S_whichword(__pos)]; }
   140 
   141   _WordT& _M_hiword()       { return _M_w[_Nw - 1]; }
   142   _WordT  _M_hiword() const { return _M_w[_Nw - 1]; }
   143 
   144   void _M_do_and(const _Base_bitset<_Nw>& __x) {
   145     for ( size_t __i = 0; __i < _Nw; __i++ ) {
   146       _M_w[__i] &= __x._M_w[__i];
   147     }
   148   }
   149 
   150   void _M_do_or(const _Base_bitset<_Nw>& __x) {
   151     for ( size_t __i = 0; __i < _Nw; __i++ ) {
   152       _M_w[__i] |= __x._M_w[__i];
   153     }
   154   }
   155 
   156   void _M_do_xor(const _Base_bitset<_Nw>& __x) {
   157     for ( size_t __i = 0; __i < _Nw; __i++ ) {
   158       _M_w[__i] ^= __x._M_w[__i];
   159     }
   160   }
   161 
   162   void _M_do_left_shift(size_t __shift);
   163 
   164   void _M_do_right_shift(size_t __shift);
   165 
   166   void _M_do_flip() {
   167     for ( size_t __i = 0; __i < _Nw; __i++ ) {
   168       _M_w[__i] = ~_M_w[__i];
   169     }
   170   }
   171 
   172   void _M_do_set() {
   173     for ( size_t __i = 0; __i < _Nw; __i++ ) {
   174       _M_w[__i] = ~__STATIC_CAST(_WordT,0);
   175     }
   176   }
   177 
   178   void _M_do_reset() { memset(_M_w, 0, _Nw * sizeof(_WordT)); }
   179 
   180   bool _M_is_equal(const _Base_bitset<_Nw>& __x) const {
   181     for (size_t __i = 0; __i < _Nw; ++__i) {
   182       if (_M_w[__i] != __x._M_w[__i])
   183         return false;
   184     }
   185     return true;
   186   }
   187 
   188   bool _M_is_any() const {
   189     for ( size_t __i = 0; __i < _Nw ; __i++ ) {
   190       if ( _M_w[__i] != __STATIC_CAST(_WordT,0) )
   191         return true;
   192     }
   193     return false;
   194   }
   195 
   196   size_t _M_do_count() const {
   197     const unsigned char* __byte_ptr = (const unsigned char*)_M_w;
   198     const unsigned char* __end_ptr = (const unsigned char*)(_M_w+_Nw);
   199 
   200     return _Bs_G::_S_count(__byte_ptr, __end_ptr);
   201   }
   202 
   203   unsigned long _M_do_to_ulong() const;
   204 
   205   // find first "on" bit
   206   size_t _M_do_find_first(size_t __not_found) const;
   207 
   208   // find the next "on" bit that follows "prev"
   209   size_t _M_do_find_next(size_t __prev, size_t __not_found) const;
   210 };
   211 
   212 //
   213 // Base class: specialization for a single word.
   214 //
   215 _STLP_TEMPLATE_NULL
   216 struct _Base_bitset<1UL> {
   217   typedef unsigned long _WordT;
   218   typedef _Base_bitset<1UL> _Self;
   219 
   220   _WordT _M_w;
   221 
   222   _Base_bitset( void ) : _M_w(0) {}
   223   _Base_bitset(unsigned long __val) : _M_w(__val) {}
   224 
   225   static size_t _STLP_CALL _S_whichword( size_t __pos ) {
   226     return __pos / __BITS_PER_WORD ;
   227   }
   228   static size_t _STLP_CALL _S_whichbyte( size_t __pos ) {
   229     return (__pos % __BITS_PER_WORD) / CHAR_BIT;
   230   }
   231   static size_t _STLP_CALL _S_whichbit( size_t __pos ) {
   232     return __pos % __BITS_PER_WORD;
   233   }
   234   static _WordT _STLP_CALL _S_maskbit( size_t __pos ) {
   235     return (__STATIC_CAST(_WordT,1)) << _S_whichbit(__pos);
   236   }
   237 
   238   _WordT& _M_getword(size_t)       { return _M_w; }
   239   _WordT  _M_getword(size_t) const { return _M_w; }
   240 
   241   _WordT& _M_hiword()       { return _M_w; }
   242   _WordT  _M_hiword() const { return _M_w; }
   243 
   244   void _M_do_and(const _Self& __x) { _M_w &= __x._M_w; }
   245   void _M_do_or(const _Self& __x)  { _M_w |= __x._M_w; }
   246   void _M_do_xor(const _Self& __x) { _M_w ^= __x._M_w; }
   247   void _M_do_left_shift(size_t __shift)     { _M_w <<= __shift; }
   248   void _M_do_right_shift(size_t __shift)    { _M_w >>= __shift; }
   249   void _M_do_flip()                       { _M_w = ~_M_w; }
   250   void _M_do_set()                        { _M_w = ~__STATIC_CAST(_WordT,0); }
   251   void _M_do_reset()                      { _M_w = 0; }
   252 
   253   bool _M_is_equal(const _Self& __x) const {
   254     return _M_w == __x._M_w;
   255   }
   256   bool _M_is_any() const {
   257     return _M_w != 0;
   258   }
   259 
   260   size_t _M_do_count() const {
   261     const unsigned char* __byte_ptr = (const unsigned char*)&_M_w;
   262     const unsigned char* __end_ptr = ((const unsigned char*)&_M_w)+sizeof(_M_w);
   263     return _Bs_G::_S_count(__byte_ptr, __end_ptr);
   264   }
   265 
   266   unsigned long _M_do_to_ulong() const { return _M_w; }
   267 
   268   inline size_t _M_do_find_first(size_t __not_found) const;
   269 
   270   // find the next "on" bit that follows "prev"
   271   inline size_t _M_do_find_next(size_t __prev, size_t __not_found) const;
   272 };
   273 
   274 
   275 // ------------------------------------------------------------
   276 //
   277 // Definitions of should-be-non-inline functions from the single-word version of
   278 //  _Base_bitset.
   279 //
   280 inline size_t
   281 _Base_bitset<1UL>::_M_do_find_first(size_t __not_found) const {
   282   //  typedef unsigned long _WordT;
   283   _WordT __thisword = _M_w;
   284 
   285   if ( __thisword != __STATIC_CAST(_WordT,0) ) {
   286     // find byte within word
   287     for ( size_t __j = 0; __j < sizeof(_WordT); __j++ ) {
   288       unsigned char __this_byte
   289         = __STATIC_CAST(unsigned char,(__thisword & (~(unsigned char)0)));
   290       if ( __this_byte )
   291         return __j*CHAR_BIT + _Bs_G::_S_first_one(__this_byte);
   292 
   293       __thisword >>= CHAR_BIT;
   294     }
   295   }
   296   // not found, so return a value that indicates failure.
   297   return __not_found;
   298 }
   299 
   300 inline size_t
   301 _Base_bitset<1UL>::_M_do_find_next(size_t __prev,
   302                                    size_t __not_found ) const {
   303   // make bound inclusive
   304   ++__prev;
   305 
   306   // check out of bounds
   307   if ( __prev >= __BITS_PER_WORD )
   308     return __not_found;
   309 
   310     // search first (and only) word
   311   _WordT __thisword = _M_w;
   312 
   313   // mask off bits below bound
   314   __thisword &= (~__STATIC_CAST(_WordT,0)) << _S_whichbit(__prev);
   315 
   316   if ( __thisword != __STATIC_CAST(_WordT,0) ) {
   317     // find byte within word
   318     // get first byte into place
   319     __thisword >>= _S_whichbyte(__prev) * CHAR_BIT;
   320     for ( size_t __j = _S_whichbyte(__prev); __j < sizeof(_WordT); __j++ ) {
   321       unsigned char __this_byte
   322         = __STATIC_CAST(unsigned char,(__thisword & (~(unsigned char)0)));
   323       if ( __this_byte )
   324         return __j*CHAR_BIT + _Bs_G::_S_first_one(__this_byte);
   325 
   326       __thisword >>= CHAR_BIT;
   327     }
   328   }
   329 
   330   // not found, so return a value that indicates failure.
   331   return __not_found;
   332 } // end _M_do_find_next
   333 
   334 
   335 // ------------------------------------------------------------
   336 // Helper class to zero out the unused high-order bits in the highest word.
   337 
   338 template <size_t _Extrabits> struct _Sanitize {
   339   static void _STLP_CALL _M_do_sanitize(unsigned long& __val)
   340   { __val &= ~((~__STATIC_CAST(unsigned long,0)) << _Extrabits); }
   341 };
   342 
   343 _STLP_TEMPLATE_NULL struct _Sanitize<0UL> {
   344   static void _STLP_CALL _M_do_sanitize(unsigned long) {}
   345 };
   346 
   347 _STLP_MOVE_TO_STD_NAMESPACE
   348 
   349 // ------------------------------------------------------------
   350 // Class bitset.
   351 //   _Nb may be any nonzero number of type size_t.
   352 template<size_t _Nb>
   353 class bitset : public _STLP_PRIV _Base_bitset<__BITSET_WORDS(_Nb) > {
   354 public:
   355   enum { _Words = __BITSET_WORDS(_Nb) } ;
   356 
   357 private:
   358   typedef _STLP_PRIV _Base_bitset< _Words > _Base;
   359 
   360   void _M_do_sanitize() {
   361     _STLP_PRIV _Sanitize<_Nb%__BITS_PER_WORD >::_M_do_sanitize(this->_M_hiword());
   362   }
   363 public:
   364   typedef unsigned long _WordT;
   365   struct reference;
   366   friend struct reference;
   367 
   368   // bit reference:
   369   struct reference {
   370   typedef _STLP_PRIV _Base_bitset<_Words > _Bitset_base;
   371   typedef bitset<_Nb> _Bitset;
   372     //    friend _Bitset;
   373     _WordT *_M_wp;
   374     size_t _M_bpos;
   375 
   376     // should be left undefined
   377     reference() {}
   378 
   379     reference( _Bitset& __b, size_t __pos ) {
   380       _M_wp = &__b._M_getword(__pos);
   381       _M_bpos = _Bitset_base::_S_whichbit(__pos);
   382     }
   383 
   384   public:
   385     ~reference() {}
   386 
   387     // for b[i] = __x;
   388     reference& operator=(bool __x) {
   389       if ( __x )
   390         *_M_wp |= _Bitset_base::_S_maskbit(_M_bpos);
   391       else
   392         *_M_wp &= ~_Bitset_base::_S_maskbit(_M_bpos);
   393 
   394       return *this;
   395     }
   396 
   397     // for b[i] = b[__j];
   398     reference& operator=(const reference& __j) {
   399       if ( (*(__j._M_wp) & _Bitset_base::_S_maskbit(__j._M_bpos)) )
   400         *_M_wp |= _Bitset_base::_S_maskbit(_M_bpos);
   401       else
   402         *_M_wp &= ~_Bitset_base::_S_maskbit(_M_bpos);
   403 
   404       return *this;
   405     }
   406 
   407     // flips the bit
   408     bool operator~() const { return (*(_M_wp) & _Bitset_base::_S_maskbit(_M_bpos)) == 0; }
   409 
   410     // for __x = b[i];
   411     operator bool() const { return (*(_M_wp) & _Bitset_base::_S_maskbit(_M_bpos)) != 0; }
   412 
   413     // for b[i].flip();
   414     reference& flip() {
   415       *_M_wp ^= _Bitset_base::_S_maskbit(_M_bpos);
   416       return *this;
   417     }
   418   };
   419 
   420   // 23.3.5.1 constructors:
   421   bitset() {}
   422 
   423   bitset(unsigned long __val) : _STLP_PRIV _Base_bitset<_Words>(__val) { _M_do_sanitize(); }
   424 
   425 #if defined (_STLP_MEMBER_TEMPLATES)
   426   template<class _CharT, class _Traits, class _Alloc>
   427   explicit bitset(const basic_string<_CharT,_Traits,_Alloc>& __s,
   428                   size_t __pos = 0)
   429     : _STLP_PRIV _Base_bitset<_Words >() {
   430     if (__pos > __s.size())
   431       __stl_throw_out_of_range("bitset");
   432     _M_copy_from_string(__s, __pos,
   433                         basic_string<_CharT, _Traits, _Alloc>::npos);
   434   }
   435   template<class _CharT, class _Traits, class _Alloc>
   436   bitset(const basic_string<_CharT, _Traits, _Alloc>& __s,
   437           size_t __pos,
   438           size_t __n)
   439   : _STLP_PRIV _Base_bitset<_Words >() {
   440     if (__pos > __s.size())
   441       __stl_throw_out_of_range("bitset");
   442     _M_copy_from_string(__s, __pos, __n);
   443   }
   444 #else /* _STLP_MEMBER_TEMPLATES */
   445   explicit bitset(const string& __s,
   446                   size_t __pos = 0,
   447                   size_t __n = (size_t)-1)
   448     : _STLP_PRIV _Base_bitset<_Words >() {
   449     if (__pos > __s.size())
   450       __stl_throw_out_of_range("bitset");
   451     _M_copy_from_string(__s, __pos, __n);
   452   }
   453 #endif /* _STLP_MEMBER_TEMPLATES */
   454 
   455   // 23.3.5.2 bitset operations:
   456   bitset<_Nb>& operator&=(const bitset<_Nb>& __rhs) {
   457     this->_M_do_and(__rhs);
   458     return *this;
   459   }
   460 
   461   bitset<_Nb>& operator|=(const bitset<_Nb>& __rhs) {
   462     this->_M_do_or(__rhs);
   463     return *this;
   464   }
   465 
   466   bitset<_Nb>& operator^=(const bitset<_Nb>& __rhs) {
   467     this->_M_do_xor(__rhs);
   468     return *this;
   469   }
   470 
   471   bitset<_Nb>& operator<<=(size_t __pos) {
   472     this->_M_do_left_shift(__pos);
   473     this->_M_do_sanitize();
   474     return *this;
   475   }
   476 
   477   bitset<_Nb>& operator>>=(size_t __pos) {
   478     this->_M_do_right_shift(__pos);
   479     this->_M_do_sanitize();
   480     return *this;
   481   }
   482 
   483   //
   484   // Extension:
   485   // Versions of single-bit set, reset, flip, test with no range checking.
   486   //
   487 
   488   bitset<_Nb>& _Unchecked_set(size_t __pos) {
   489     this->_M_getword(__pos) |= _STLP_PRIV _Base_bitset<_Words > ::_S_maskbit(__pos);
   490     return *this;
   491   }
   492 
   493   bitset<_Nb>& _Unchecked_set(size_t __pos, int __val) {
   494     if (__val)
   495       this->_M_getword(__pos) |= this->_S_maskbit(__pos);
   496     else
   497       this->_M_getword(__pos) &= ~ this->_S_maskbit(__pos);
   498 
   499     return *this;
   500   }
   501 
   502   bitset<_Nb>& _Unchecked_reset(size_t __pos) {
   503     this->_M_getword(__pos) &= ~ this->_S_maskbit(__pos);
   504     return *this;
   505   }
   506 
   507   bitset<_Nb>& _Unchecked_flip(size_t __pos) {
   508     this->_M_getword(__pos) ^= this->_S_maskbit(__pos);
   509     return *this;
   510   }
   511 
   512   bool _Unchecked_test(size_t __pos) const {
   513     return (this->_M_getword(__pos) & this->_S_maskbit(__pos)) != __STATIC_CAST(_WordT,0);
   514   }
   515 
   516   // Set, reset, and flip.
   517 
   518   bitset<_Nb>& set() {
   519     this->_M_do_set();
   520     this->_M_do_sanitize();
   521     return *this;
   522   }
   523 
   524   bitset<_Nb>& set(size_t __pos) {
   525     if (__pos >= _Nb)
   526       __stl_throw_out_of_range("bitset");
   527     return _Unchecked_set(__pos);
   528   }
   529 
   530   bitset<_Nb>& set(size_t __pos, int __val) {
   531     if (__pos >= _Nb)
   532       __stl_throw_out_of_range("bitset");
   533     return _Unchecked_set(__pos, __val);
   534   }
   535 
   536   bitset<_Nb>& reset() {
   537     this->_M_do_reset();
   538     return *this;
   539   }
   540 
   541   bitset<_Nb>& reset(size_t __pos) {
   542     if (__pos >= _Nb)
   543       __stl_throw_out_of_range("bitset");
   544 
   545     return _Unchecked_reset(__pos);
   546   }
   547 
   548   bitset<_Nb>& flip() {
   549     this->_M_do_flip();
   550     this->_M_do_sanitize();
   551     return *this;
   552   }
   553 
   554   bitset<_Nb>& flip(size_t __pos) {
   555     if (__pos >= _Nb)
   556       __stl_throw_out_of_range("bitset");
   557 
   558     return _Unchecked_flip(__pos);
   559   }
   560 
   561   bitset<_Nb> operator~() const {
   562     return bitset<_Nb>(*this).flip();
   563   }
   564 
   565   // element access:
   566   //for b[i];
   567   reference operator[](size_t __pos) { return reference(*this,__pos); }
   568   bool operator[](size_t __pos) const { return _Unchecked_test(__pos); }
   569 
   570   unsigned long to_ulong() const { return this->_M_do_to_ulong(); }
   571 
   572 #if defined (_STLP_MEMBER_TEMPLATES) && !defined (_STLP_NO_EXPLICIT_FUNCTION_TMPL_ARGS)
   573   template <class _CharT, class _Traits, class _Alloc>
   574   basic_string<_CharT, _Traits, _Alloc> to_string() const {
   575     basic_string<_CharT, _Traits, _Alloc> __result;
   576     _M_copy_to_string(__result);
   577     return __result;
   578   }
   579 #else
   580   string to_string() const {
   581     string __result;
   582     _M_copy_to_string(__result);
   583     return __result;
   584   }
   585 #endif /* _STLP_EXPLICIT_FUNCTION_TMPL_ARGS */
   586 
   587   size_t count() const { return this->_M_do_count(); }
   588 
   589   size_t size() const { return _Nb; }
   590 
   591   bool operator==(const bitset<_Nb>& __rhs) const {
   592     return this->_M_is_equal(__rhs);
   593   }
   594   bool operator!=(const bitset<_Nb>& __rhs) const {
   595     return !this->_M_is_equal(__rhs);
   596   }
   597 
   598   bool test(size_t __pos) const {
   599     if (__pos >= _Nb)
   600       __stl_throw_out_of_range("bitset");
   601 
   602     return _Unchecked_test(__pos);
   603   }
   604 
   605   bool any() const { return this->_M_is_any(); }
   606   bool none() const { return !this->_M_is_any(); }
   607 
   608   bitset<_Nb> operator<<(size_t __pos) const {
   609     bitset<_Nb> __result(*this);
   610     __result <<= __pos ;  return __result;
   611   }
   612   bitset<_Nb> operator>>(size_t __pos) const {
   613     bitset<_Nb> __result(*this);
   614     __result >>= __pos ;  return __result;
   615   }
   616 
   617 #if !defined (_STLP_NO_EXTENSIONS)
   618   //
   619   // EXTENSIONS: bit-find operations.  These operations are
   620   // experimental, and are subject to change or removal in future
   621   // versions.
   622   //
   623 
   624   // find the index of the first "on" bit
   625   size_t _Find_first() const
   626     { return this->_M_do_find_first(_Nb); }
   627 
   628   // find the index of the next "on" bit after prev
   629   size_t _Find_next( size_t __prev ) const
   630     { return this->_M_do_find_next(__prev, _Nb); }
   631 #endif
   632 
   633 //
   634 // Definitions of should-be non-inline member functions.
   635 //
   636 #if defined (_STLP_MEMBER_TEMPLATES)
   637   template<class _CharT, class _Traits, class _Alloc>
   638   void _M_copy_from_string(const basic_string<_CharT,_Traits,_Alloc>& __s,
   639                            size_t __pos, size_t __n) {
   640 #else
   641   void _M_copy_from_string(const string& __s,
   642                            size_t __pos, size_t __n) {
   643     typedef typename string::traits_type _Traits;
   644 #endif
   645     reset();
   646     size_t __tmp = _Nb;
   647     const size_t __Nbits = (min) (__tmp, (min) (__n, __s.size() - __pos));
   648     for ( size_t __i= 0; __i < __Nbits; ++__i) {
   649       typename _Traits::int_type __k = _Traits::to_int_type(__s[__pos + __Nbits - __i - 1]);
   650       // boris : widen() ?
   651       if (__k == '1')
   652         set(__i);
   653       else if (__k != '0')
   654         __stl_throw_invalid_argument("bitset");
   655     }
   656   }
   657 
   658 #if defined (_STLP_MEMBER_TEMPLATES)
   659   template <class _CharT, class _Traits, class _Alloc>
   660   void _M_copy_to_string(basic_string<_CharT, _Traits, _Alloc>& __s) const
   661 #else
   662   void _M_copy_to_string(string& __s) const
   663 #endif
   664   {
   665     __s.assign(_Nb, '0');
   666 
   667     for (size_t __i = 0; __i < _Nb; ++__i) {
   668       if (_Unchecked_test(__i))
   669         __s[_Nb - 1 - __i] = '1';
   670     }
   671   }
   672 
   673 #if !defined (_STLP_MEMBER_TEMPLATES) && !defined (_STLP_NO_WCHAR_T)
   674   void _M_copy_to_string(wstring& __s) const {
   675     __s.assign(_Nb, '0');
   676 
   677     for (size_t __i = 0; __i < _Nb; ++__i) {
   678       if (_Unchecked_test(__i))
   679         __s[_Nb - 1 - __i] = '1';
   680     }
   681   }
   682 #endif
   683 
   684 #if defined (_STLP_NON_TYPE_TMPL_PARAM_BUG)
   685   bitset<_Nb> operator&(const bitset<_Nb>& __y) const {
   686     bitset<_Nb> __result(*this);
   687     __result &= __y;
   688     return __result;
   689   }
   690   bitset<_Nb> operator|(const bitset<_Nb>& __y) const {
   691     bitset<_Nb> __result(*this);
   692     __result |= __y;
   693     return __result;
   694   }
   695   bitset<_Nb> operator^(const bitset<_Nb>& __y) const {
   696     bitset<_Nb> __result(*this);
   697     __result ^= __y;
   698     return __result;
   699   }
   700 #endif
   701 };
   702 
   703 // ------------------------------------------------------------
   704 //
   705 // 23.3.5.3 bitset operations:
   706 //
   707 #if ! defined (_STLP_NON_TYPE_TMPL_PARAM_BUG)
   708 template <size_t _Nb>
   709 inline bitset<_Nb>  _STLP_CALL
   710 operator&(const bitset<_Nb>& __x,
   711           const bitset<_Nb>& __y) {
   712   bitset<_Nb> __result(__x);
   713   __result &= __y;
   714   return __result;
   715 }
   716 
   717 
   718 template <size_t _Nb>
   719 inline bitset<_Nb>  _STLP_CALL
   720 operator|(const bitset<_Nb>& __x,
   721           const bitset<_Nb>& __y) {
   722   bitset<_Nb> __result(__x);
   723   __result |= __y;
   724   return __result;
   725 }
   726 
   727 template <size_t _Nb>
   728 inline bitset<_Nb>  _STLP_CALL
   729 operator^(const bitset<_Nb>& __x,
   730           const bitset<_Nb>& __y) {
   731   bitset<_Nb> __result(__x);
   732   __result ^= __y;
   733   return __result;
   734 }
   735 
   736 #if !defined (_STLP_USE_NO_IOSTREAMS)
   737 
   738 _STLP_END_NAMESPACE
   739 
   740 #  if !(defined (_STLP_MSVC) && (_STLP_MSVC < 1300)) && \
   741       !(defined(__SUNPRO_CC) && (__SUNPRO_CC < 0x500))
   742 
   743 #ifndef _STLP_INTERNAL_IOSFWD
   744 #  include <stl/_iosfwd.h>
   745 #endif
   746 
   747 _STLP_BEGIN_NAMESPACE
   748 
   749 template <class _CharT, class _Traits, size_t _Nb>
   750 basic_istream<_CharT, _Traits>&  _STLP_CALL
   751 operator>>(basic_istream<_CharT, _Traits>& __is, bitset<_Nb>& __x);
   752 
   753 template <class _CharT, class _Traits, size_t _Nb>
   754 basic_ostream<_CharT, _Traits>& _STLP_CALL
   755 operator<<(basic_ostream<_CharT, _Traits>& __os, const bitset<_Nb>& __x);
   756 
   757 #  else
   758 
   759 #ifndef _STLP_STRING_IO_H
   760 #  include <stl/_string_io.h> //includes _istream.h and _ostream.h
   761 #endif
   762 
   763 _STLP_BEGIN_NAMESPACE
   764 
   765 template <size_t _Nb>
   766 istream&  _STLP_CALL
   767 operator>>(istream& __is, bitset<_Nb>& __x) {
   768   typedef typename string::traits_type _Traits;
   769   string __tmp;
   770   __tmp.reserve(_Nb);
   771 
   772   // Skip whitespace
   773   typename istream::sentry __sentry(__is);
   774   if (__sentry) {
   775     streambuf* __buf = __is.rdbuf();
   776     for (size_t __i = 0; __i < _Nb; ++__i) {
   777       static typename _Traits::int_type __eof = _Traits::eof();
   778 
   779       typename _Traits::int_type __c1 = __buf->sbumpc();
   780       if (_Traits::eq_int_type(__c1, __eof)) {
   781         __is.setstate(ios_base::eofbit);
   782         break;
   783       }
   784       else {
   785         typename _Traits::char_type __c2 = _Traits::to_char_type(__c1);
   786         char __c  = __is.narrow(__c2, '*');
   787 
   788         if (__c == '0' || __c == '1')
   789           __tmp.push_back(__c);
   790         else if (_Traits::eq_int_type(__buf->sputbackc(__c2), __eof)) {
   791           __is.setstate(ios_base::failbit);
   792           break;
   793         }
   794       }
   795     }
   796 
   797     if (__tmp.empty())
   798       __is.setstate(ios_base::failbit);
   799     else
   800       __x._M_copy_from_string(__tmp, __STATIC_CAST(size_t,0), _Nb);
   801   }
   802 
   803   return __is;
   804 }
   805 
   806 template <size_t _Nb>
   807 ostream& _STLP_CALL
   808 operator<<(ostream& __os, const bitset<_Nb>& __x) {
   809   string __tmp;
   810   __x._M_copy_to_string(__tmp);
   811   return __os << __tmp;
   812 }
   813 
   814 #    if !defined (_STLP_NO_WCHAR_T)
   815 
   816 template <size_t _Nb>
   817 wistream&  _STLP_CALL
   818 operator>>(wistream& __is, bitset<_Nb>& __x) {
   819   typedef typename wstring::traits_type _Traits;
   820   wstring __tmp;
   821   __tmp.reserve(_Nb);
   822 
   823   // Skip whitespace
   824   typename wistream::sentry __sentry(__is);
   825   if (__sentry) {
   826     wstreambuf* __buf = __is.rdbuf();
   827     for (size_t __i = 0; __i < _Nb; ++__i) {
   828       static typename _Traits::int_type __eof = _Traits::eof();
   829 
   830       typename _Traits::int_type __c1 = __buf->sbumpc();
   831       if (_Traits::eq_int_type(__c1, __eof)) {
   832         __is.setstate(ios_base::eofbit);
   833         break;
   834       }
   835       else {
   836         typename _Traits::char_type __c2 = _Traits::to_char_type(__c1);
   837         char __c  = __is.narrow(__c2, '*');
   838 
   839         if (__c == '0' || __c == '1')
   840           __tmp.push_back(__c);
   841         else if (_Traits::eq_int_type(__buf->sputbackc(__c2), __eof)) {
   842           __is.setstate(ios_base::failbit);
   843           break;
   844         }
   845       }
   846     }
   847 
   848     if (__tmp.empty())
   849       __is.setstate(ios_base::failbit);
   850     else
   851       __x._M_copy_from_string(__tmp, __STATIC_CAST(size_t,0), _Nb);
   852   }
   853 
   854   return __is;
   855 }
   856 
   857 template <size_t _Nb>
   858 wostream& _STLP_CALL
   859 operator<<(wostream& __os, const bitset<_Nb>& __x) {
   860   wstring __tmp;
   861   __x._M_copy_to_string(__tmp);
   862   return __os << __tmp;
   863 }
   864 
   865 #    endif /* _STLP_NO_WCHAR_T */
   866 #  endif
   867 #endif
   868 
   869 #endif /* _STLP_NON_TYPE_TMPL_PARAM_BUG */
   870 
   871 #undef  bitset
   872 
   873 _STLP_END_NAMESPACE
   874 
   875 #undef __BITS_PER_WORD
   876 #undef __BITSET_WORDS
   877 
   878 #if !defined (_STLP_LINK_TIME_INSTANTIATION)
   879 #  include <stl/_bitset.c>
   880 #endif
   881 
   882 #endif /* _STLP_BITSET_H */
   883 
   884 // Local Variables:
   885 // mode:C++
   886 // End: