epoc32/include/stdapis/stlportv5/stl/_bitset.c
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_C
williamr@4
    20
#define _STLP_BITSET_C
williamr@2
    21
williamr@4
    22
#ifndef _STLP_BITSET_H
williamr@2
    23
#  include <stl/_bitset.h>
williamr@4
    24
#endif
williamr@2
    25
williamr@4
    26
#define __BITS_PER_WORD (CHAR_BIT * sizeof(unsigned long))
williamr@2
    27
williamr@2
    28
_STLP_BEGIN_NAMESPACE
williamr@2
    29
williamr@4
    30
_STLP_MOVE_TO_PRIV_NAMESPACE
williamr@2
    31
//
williamr@2
    32
// Definitions of non-inline functions from _Base_bitset.
williamr@4
    33
//
williamr@2
    34
template<size_t _Nw>
williamr@4
    35
void _Base_bitset<_Nw>::_M_do_left_shift(size_t __shift) {
williamr@2
    36
  if (__shift != 0) {
williamr@2
    37
    const size_t __wshift = __shift / __BITS_PER_WORD;
williamr@2
    38
    const size_t __offset = __shift % __BITS_PER_WORD;
williamr@2
    39
williamr@2
    40
    if (__offset == 0)
williamr@2
    41
      for (size_t __n = _Nw - 1; __n >= __wshift; --__n)
williamr@2
    42
        _M_w[__n] = _M_w[__n - __wshift];
williamr@2
    43
williamr@2
    44
    else {
williamr@2
    45
      const size_t __sub_offset = __BITS_PER_WORD - __offset;
williamr@2
    46
      for (size_t __n = _Nw - 1; __n > __wshift; --__n)
williamr@4
    47
        _M_w[__n] = (_M_w[__n - __wshift] << __offset) |
williamr@2
    48
                    (_M_w[__n - __wshift - 1] >> __sub_offset);
williamr@2
    49
      _M_w[__wshift] = _M_w[0] << __offset;
williamr@2
    50
    }
williamr@2
    51
williamr@2
    52
    fill(_M_w + 0, _M_w + __wshift, __STATIC_CAST(_WordT,0));
williamr@2
    53
  }
williamr@2
    54
}
williamr@2
    55
williamr@2
    56
template<size_t _Nw>
williamr@4
    57
void _Base_bitset<_Nw>::_M_do_right_shift(size_t __shift) {
williamr@2
    58
  if (__shift != 0) {
williamr@2
    59
    const size_t __wshift = __shift / __BITS_PER_WORD;
williamr@2
    60
    const size_t __offset = __shift % __BITS_PER_WORD;
williamr@2
    61
    const size_t __limit = _Nw - __wshift - 1;
williamr@2
    62
williamr@2
    63
    if (__offset == 0)
williamr@2
    64
      for (size_t __n = 0; __n <= __limit; ++__n)
williamr@2
    65
        _M_w[__n] = _M_w[__n + __wshift];
williamr@2
    66
williamr@2
    67
    else {
williamr@2
    68
      const size_t __sub_offset = __BITS_PER_WORD - __offset;
williamr@2
    69
      for (size_t __n = 0; __n < __limit; ++__n)
williamr@2
    70
        _M_w[__n] = (_M_w[__n + __wshift] >> __offset) |
williamr@2
    71
                    (_M_w[__n + __wshift + 1] << __sub_offset);
williamr@2
    72
      _M_w[__limit] = _M_w[_Nw-1] >> __offset;
williamr@2
    73
    }
williamr@2
    74
williamr@2
    75
    fill(_M_w + __limit + 1, _M_w + _Nw, __STATIC_CAST(_WordT,0));
williamr@2
    76
  }
williamr@2
    77
}
williamr@2
    78
williamr@2
    79
template<size_t _Nw>
williamr@4
    80
unsigned long _Base_bitset<_Nw>::_M_do_to_ulong() const {
williamr@4
    81
  for (size_t __i = 1; __i < _Nw; ++__i)
williamr@4
    82
    if (_M_w[__i])
williamr@2
    83
      __stl_throw_overflow_error("bitset");
williamr@2
    84
  return _M_w[0];
williamr@2
    85
} // End _M_do_to_ulong
williamr@2
    86
williamr@2
    87
template<size_t _Nw>
williamr@4
    88
size_t _Base_bitset<_Nw>::_M_do_find_first(size_t __not_found) const {
williamr@2
    89
  for ( size_t __i = 0; __i < _Nw; __i++ ) {
williamr@2
    90
    _WordT __thisword = _M_w[__i];
williamr@2
    91
    if ( __thisword != __STATIC_CAST(_WordT,0) ) {
williamr@2
    92
      // find byte within word
williamr@2
    93
      for ( size_t __j = 0; __j < sizeof(_WordT); __j++ ) {
williamr@2
    94
        unsigned char __this_byte
williamr@2
    95
          = __STATIC_CAST(unsigned char,(__thisword & (~(unsigned char)0)));
williamr@2
    96
        if ( __this_byte )
williamr@2
    97
          return __i*__BITS_PER_WORD + __j*CHAR_BIT +
williamr@4
    98
            _Bs_G::_S_first_one(__this_byte);
williamr@2
    99
williamr@2
   100
        __thisword >>= CHAR_BIT;
williamr@2
   101
      }
williamr@2
   102
    }
williamr@2
   103
  }
williamr@2
   104
  // not found, so return an indication of failure.
williamr@2
   105
  return __not_found;
williamr@2
   106
}
williamr@2
   107
williamr@2
   108
template<size_t _Nw>
williamr@2
   109
size_t
williamr@4
   110
_Base_bitset<_Nw>::_M_do_find_next(size_t __prev,
williamr@4
   111
                                   size_t __not_found) const {
williamr@2
   112
  // make bound inclusive
williamr@2
   113
  ++__prev;
williamr@2
   114
williamr@2
   115
  // check out of bounds
williamr@2
   116
  if ( __prev >= _Nw * __BITS_PER_WORD )
williamr@2
   117
    return __not_found;
williamr@2
   118
williamr@2
   119
    // search first word
williamr@2
   120
  size_t __i = _S_whichword(__prev);
williamr@2
   121
  _WordT __thisword = _M_w[__i];
williamr@2
   122
williamr@2
   123
    // mask off bits below bound
williamr@2
   124
  __thisword &= (~__STATIC_CAST(_WordT,0)) << _S_whichbit(__prev);
williamr@2
   125
williamr@2
   126
  if ( __thisword != __STATIC_CAST(_WordT,0) ) {
williamr@2
   127
    // find byte within word
williamr@2
   128
    // get first byte into place
williamr@2
   129
    __thisword >>= _S_whichbyte(__prev) * CHAR_BIT;
williamr@4
   130
    for ( size_t __j = _S_whichbyte(__prev); __j < sizeof(_WordT); ++__j ) {
williamr@2
   131
      unsigned char __this_byte
williamr@2
   132
        = __STATIC_CAST(unsigned char,(__thisword & (~(unsigned char)0)));
williamr@2
   133
      if ( __this_byte )
williamr@2
   134
        return __i*__BITS_PER_WORD + __j*CHAR_BIT +
williamr@4
   135
          _Bs_G::_S_first_one(__this_byte);
williamr@2
   136
williamr@2
   137
      __thisword >>= CHAR_BIT;
williamr@2
   138
    }
williamr@2
   139
  }
williamr@2
   140
williamr@2
   141
  // check subsequent words
williamr@4
   142
  ++__i;
williamr@4
   143
  for ( ; __i < _Nw; ++__i ) {
williamr@2
   144
    /* _WordT */ __thisword = _M_w[__i];
williamr@2
   145
    if ( __thisword != __STATIC_CAST(_WordT,0) ) {
williamr@2
   146
      // find byte within word
williamr@4
   147
      for ( size_t __j = 0; __j < sizeof(_WordT); ++__j ) {
williamr@2
   148
        unsigned char __this_byte
williamr@2
   149
          = __STATIC_CAST(unsigned char,(__thisword & (~(unsigned char)0)));
williamr@2
   150
        if ( __this_byte )
williamr@2
   151
          return __i*__BITS_PER_WORD + __j*CHAR_BIT +
williamr@4
   152
            _Bs_G::_S_first_one(__this_byte);
williamr@2
   153
williamr@2
   154
        __thisword >>= CHAR_BIT;
williamr@2
   155
      }
williamr@2
   156
    }
williamr@2
   157
  }
williamr@2
   158
williamr@2
   159
  // not found, so return an indication of failure.
williamr@2
   160
  return __not_found;
williamr@2
   161
} // end _M_do_find_next
williamr@2
   162
williamr@4
   163
_STLP_MOVE_TO_STD_NAMESPACE
williamr@2
   164
williamr@4
   165
#if !defined (_STLP_NON_TYPE_TMPL_PARAM_BUG)
williamr@2
   166
williamr@4
   167
#  if !defined (_STLP_USE_NO_IOSTREAMS)
williamr@2
   168
williamr@4
   169
_STLP_END_NAMESPACE
williamr@4
   170
williamr@4
   171
#ifndef _STLP_STRING_IO_H
williamr@4
   172
#  include <stl/_string_io.h> //includes _istream.h and _ostream.h
williamr@4
   173
#endif
williamr@4
   174
williamr@4
   175
_STLP_BEGIN_NAMESPACE
williamr@2
   176
williamr@2
   177
template <class _CharT, class _Traits, size_t _Nb>
williamr@2
   178
basic_istream<_CharT, _Traits>& _STLP_CALL
williamr@4
   179
operator>>(basic_istream<_CharT, _Traits>& __is, bitset<_Nb>& __x) {
williamr@2
   180
  basic_string<_CharT, _Traits> __tmp;
williamr@2
   181
  __tmp.reserve(_Nb);
williamr@2
   182
williamr@2
   183
  // Skip whitespace
williamr@2
   184
  typename basic_istream<_CharT, _Traits>::sentry __sentry(__is);
williamr@2
   185
  if (__sentry) {
williamr@2
   186
    basic_streambuf<_CharT, _Traits>* __buf = __is.rdbuf();
williamr@2
   187
    for (size_t __i = 0; __i < _Nb; ++__i) {
williamr@2
   188
      static typename _Traits::int_type __eof = _Traits::eof();
williamr@2
   189
williamr@2
   190
      typename _Traits::int_type __c1 = __buf->sbumpc();
williamr@2
   191
      if (_Traits::eq_int_type(__c1, __eof)) {
williamr@2
   192
        __is.setstate(ios_base::eofbit);
williamr@2
   193
        break;
williamr@2
   194
      }
williamr@2
   195
      else {
williamr@4
   196
        typename _Traits::char_type __c2 = _Traits::to_char_type(__c1);
williamr@4
   197
        char __c = __is.narrow(__c2, '*');
williamr@2
   198
williamr@2
   199
        if (__c == '0' || __c == '1')
williamr@2
   200
          __tmp.push_back(__c);
williamr@2
   201
        else if (_Traits::eq_int_type(__buf->sputbackc(__c2), __eof)) {
williamr@2
   202
          __is.setstate(ios_base::failbit);
williamr@2
   203
          break;
williamr@2
   204
        }
williamr@2
   205
      }
williamr@2
   206
    }
williamr@2
   207
williamr@2
   208
    if (__tmp.empty())
williamr@2
   209
      __is.setstate(ios_base::failbit);
williamr@2
   210
    else
williamr@2
   211
      __x._M_copy_from_string(__tmp, __STATIC_CAST(size_t,0), _Nb);
williamr@2
   212
  }
williamr@2
   213
williamr@2
   214
  return __is;
williamr@2
   215
}
williamr@2
   216
williamr@2
   217
template <class _CharT, class _Traits, size_t _Nb>
williamr@2
   218
basic_ostream<_CharT, _Traits>& _STLP_CALL
williamr@2
   219
operator<<(basic_ostream<_CharT, _Traits>& __os,
williamr@4
   220
           const bitset<_Nb>& __x) {
williamr@2
   221
  basic_string<_CharT, _Traits> __tmp;
williamr@2
   222
  __x._M_copy_to_string(__tmp);
williamr@2
   223
  return __os << __tmp;
williamr@2
   224
}
williamr@2
   225
williamr@4
   226
#  endif /* !_STLP_USE_NO_IOSTREAMS */
williamr@2
   227
williamr@4
   228
#endif /* _STLP_NON_TYPE_TMPL_PARAM_BUG */
williamr@2
   229
williamr@2
   230
_STLP_END_NAMESPACE
williamr@2
   231
williamr@4
   232
#undef __BITS_PER_WORD
williamr@4
   233
#undef bitset
williamr@2
   234
williamr@2
   235
#endif /*  _STLP_BITSET_C */