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