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