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