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