1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/epoc32/include/tools/stlport/stl/_bitset.h Wed Mar 31 12:33:34 2010 +0100
1.3 @@ -0,0 +1,869 @@
1.4 +/*
1.5 + * Copyright (c) 1998
1.6 + * Silicon Graphics Computer Systems, Inc.
1.7 + *
1.8 + * Copyright (c) 1999
1.9 + * Boris Fomitchev
1.10 + *
1.11 + * This material is provided "as is", with absolutely no warranty expressed
1.12 + * or implied. Any use is at your own risk.
1.13 + *
1.14 + * Permission to use or copy this software for any purpose is hereby granted
1.15 + * without fee, provided the above notices are retained on all copies.
1.16 + * Permission to modify the code and to distribute modified code is granted,
1.17 + * provided the above notices are retained, and a notice that the code was
1.18 + * modified is included with the above copyright notice.
1.19 + *
1.20 + */
1.21 +
1.22 +#ifndef _STLP_BITSET_H
1.23 +#define _STLP_BITSET_H
1.24 +
1.25 +// A bitset of size N has N % (sizeof(unsigned long) * CHAR_BIT) unused
1.26 +// bits. (They are the high- order bits in the highest word.) It is
1.27 +// a class invariant of class bitset<> that those unused bits are
1.28 +// always zero.
1.29 +
1.30 +// Most of the actual code isn't contained in bitset<> itself, but in the
1.31 +// base class _Base_bitset. The base class works with whole words, not with
1.32 +// individual bits. This allows us to specialize _Base_bitset for the
1.33 +// important special case where the bitset is only a single word.
1.34 +
1.35 +// The C++ standard does not define the precise semantics of operator[].
1.36 +// In this implementation the const version of operator[] is equivalent
1.37 +// to test(), except that it does no range checking. The non-const version
1.38 +// returns a reference to a bit, again without doing any range checking.
1.39 +
1.40 +
1.41 +#ifndef _STLP_INTERNAL_ALGOBASE_H
1.42 +# include <stl/_algobase.h>
1.43 +#endif
1.44 +
1.45 +#ifndef _STLP_INTERNAL_ALLOC_H
1.46 +# include <stl/_alloc.h>
1.47 +#endif
1.48 +
1.49 +#ifndef _STLP_INTERNAL_ITERATOR_H
1.50 +# include <stl/_iterator.h>
1.51 +#endif
1.52 +
1.53 +#ifndef _STLP_INTERNAL_UNINITIALIZED_H
1.54 +# include <stl/_uninitialized.h>
1.55 +#endif
1.56 +
1.57 +#ifndef _STLP_RANGE_ERRORS_H
1.58 +# include <stl/_range_errors.h>
1.59 +#endif
1.60 +
1.61 +#ifndef _STLP_INTERNAL_STRING_H
1.62 +# include <stl/_string.h>
1.63 +#endif
1.64 +
1.65 +#define __BITS_PER_WORD (CHAR_BIT*sizeof(unsigned long))
1.66 +#define __BITSET_WORDS(__n) ((__n + __BITS_PER_WORD - 1)/__BITS_PER_WORD)
1.67 +
1.68 +_STLP_BEGIN_NAMESPACE
1.69 +
1.70 +_STLP_MOVE_TO_PRIV_NAMESPACE
1.71 +
1.72 +// structure to aid in counting bits
1.73 +class _STLP_CLASS_DECLSPEC _Bs_G
1.74 +{
1.75 + public:
1.76 + //returns the number of bit set within the buffer between __beg and __end.
1.77 + static size_t _S_count(const unsigned char *__beg, const unsigned char *__end)
1.78 +#if defined (_STLP_USE_NO_IOSTREAMS)
1.79 + {
1.80 + size_t __result = 0;
1.81 + for (; __beg != __end; ++__beg) {
1.82 + for (size_t i = 0; i < (sizeof(unsigned char) * 8); ++i) {
1.83 + if ((*__beg & (1 << i)) != 0) { ++__result; }
1.84 + }
1.85 + }
1.86 + return __result;
1.87 + }
1.88 +#else
1.89 + ;
1.90 +#endif
1.91 + // Mapping from 8 bit unsigned integers to the index of the first one bit set:
1.92 + static unsigned char _S_first_one(unsigned char __x)
1.93 +#if defined (_STLP_USE_NO_IOSTREAMS)
1.94 + {
1.95 + for (unsigned char i = 0; i < (sizeof(unsigned char) * 8); ++i) {
1.96 + if ((__x & (1 << i)) != 0) { return i; }
1.97 + }
1.98 + return 0;
1.99 + }
1.100 +#else
1.101 + ;
1.102 +#endif
1.103 +};
1.104 +
1.105 +//
1.106 +// Base class: general case.
1.107 +//
1.108 +
1.109 +template<size_t _Nw>
1.110 +struct _Base_bitset {
1.111 + typedef unsigned long _WordT;
1.112 +
1.113 + _WordT _M_w[_Nw]; // 0 is the least significant word.
1.114 +
1.115 + _Base_bitset() { _M_do_reset(); }
1.116 +
1.117 + _Base_bitset(unsigned long __val) {
1.118 + _M_do_reset();
1.119 + _M_w[0] = __val;
1.120 + }
1.121 +
1.122 + static size_t _STLP_CALL _S_whichword( size_t __pos ) {
1.123 + return __pos / __BITS_PER_WORD;
1.124 + }
1.125 + static size_t _STLP_CALL _S_whichbyte( size_t __pos ) {
1.126 + return (__pos % __BITS_PER_WORD) / CHAR_BIT;
1.127 + }
1.128 + static size_t _STLP_CALL _S_whichbit( size_t __pos ) {
1.129 + return __pos % __BITS_PER_WORD;
1.130 + }
1.131 + static _WordT _STLP_CALL _S_maskbit( size_t __pos ) {
1.132 + return __STATIC_CAST(_WordT,1) << _S_whichbit(__pos);
1.133 + }
1.134 +
1.135 + _WordT& _M_getword(size_t __pos) { return _M_w[_S_whichword(__pos)]; }
1.136 + _WordT _M_getword(size_t __pos) const { return _M_w[_S_whichword(__pos)]; }
1.137 +
1.138 + _WordT& _M_hiword() { return _M_w[_Nw - 1]; }
1.139 + _WordT _M_hiword() const { return _M_w[_Nw - 1]; }
1.140 +
1.141 + void _M_do_and(const _Base_bitset<_Nw>& __x) {
1.142 + for ( size_t __i = 0; __i < _Nw; __i++ ) {
1.143 + _M_w[__i] &= __x._M_w[__i];
1.144 + }
1.145 + }
1.146 +
1.147 + void _M_do_or(const _Base_bitset<_Nw>& __x) {
1.148 + for ( size_t __i = 0; __i < _Nw; __i++ ) {
1.149 + _M_w[__i] |= __x._M_w[__i];
1.150 + }
1.151 + }
1.152 +
1.153 + void _M_do_xor(const _Base_bitset<_Nw>& __x) {
1.154 + for ( size_t __i = 0; __i < _Nw; __i++ ) {
1.155 + _M_w[__i] ^= __x._M_w[__i];
1.156 + }
1.157 + }
1.158 +
1.159 + void _M_do_left_shift(size_t __shift);
1.160 +
1.161 + void _M_do_right_shift(size_t __shift);
1.162 +
1.163 + void _M_do_flip() {
1.164 + for ( size_t __i = 0; __i < _Nw; __i++ ) {
1.165 + _M_w[__i] = ~_M_w[__i];
1.166 + }
1.167 + }
1.168 +
1.169 + void _M_do_set() {
1.170 + for ( size_t __i = 0; __i < _Nw; __i++ ) {
1.171 + _M_w[__i] = ~__STATIC_CAST(_WordT,0);
1.172 + }
1.173 + }
1.174 +
1.175 + void _M_do_reset() { memset(_M_w, 0, _Nw * sizeof(_WordT)); }
1.176 +
1.177 + bool _M_is_equal(const _Base_bitset<_Nw>& __x) const {
1.178 + for (size_t __i = 0; __i < _Nw; ++__i) {
1.179 + if (_M_w[__i] != __x._M_w[__i])
1.180 + return false;
1.181 + }
1.182 + return true;
1.183 + }
1.184 +
1.185 + bool _M_is_any() const {
1.186 + for ( size_t __i = 0; __i < _Nw ; __i++ ) {
1.187 + if ( _M_w[__i] != __STATIC_CAST(_WordT,0) )
1.188 + return true;
1.189 + }
1.190 + return false;
1.191 + }
1.192 +
1.193 + size_t _M_do_count() const {
1.194 + const unsigned char* __byte_ptr = (const unsigned char*)_M_w;
1.195 + const unsigned char* __end_ptr = (const unsigned char*)(_M_w+_Nw);
1.196 +
1.197 + return _Bs_G::_S_count(__byte_ptr, __end_ptr);
1.198 + }
1.199 +
1.200 + unsigned long _M_do_to_ulong() const;
1.201 +
1.202 + // find first "on" bit
1.203 + size_t _M_do_find_first(size_t __not_found) const;
1.204 +
1.205 + // find the next "on" bit that follows "prev"
1.206 + size_t _M_do_find_next(size_t __prev, size_t __not_found) const;
1.207 +};
1.208 +
1.209 +//
1.210 +// Base class: specialization for a single word.
1.211 +//
1.212 +_STLP_TEMPLATE_NULL
1.213 +struct _Base_bitset<1UL> {
1.214 + typedef unsigned long _WordT;
1.215 + typedef _Base_bitset<1UL> _Self;
1.216 +
1.217 + _WordT _M_w;
1.218 +
1.219 + _Base_bitset( void ) : _M_w(0) {}
1.220 + _Base_bitset(unsigned long __val) : _M_w(__val) {}
1.221 +
1.222 + static size_t _STLP_CALL _S_whichword( size_t __pos ) {
1.223 + return __pos / __BITS_PER_WORD ;
1.224 + }
1.225 + static size_t _STLP_CALL _S_whichbyte( size_t __pos ) {
1.226 + return (__pos % __BITS_PER_WORD) / CHAR_BIT;
1.227 + }
1.228 + static size_t _STLP_CALL _S_whichbit( size_t __pos ) {
1.229 + return __pos % __BITS_PER_WORD;
1.230 + }
1.231 + static _WordT _STLP_CALL _S_maskbit( size_t __pos ) {
1.232 + return (__STATIC_CAST(_WordT,1)) << _S_whichbit(__pos);
1.233 + }
1.234 +
1.235 + _WordT& _M_getword(size_t) { return _M_w; }
1.236 + _WordT _M_getword(size_t) const { return _M_w; }
1.237 +
1.238 + _WordT& _M_hiword() { return _M_w; }
1.239 + _WordT _M_hiword() const { return _M_w; }
1.240 +
1.241 + void _M_do_and(const _Self& __x) { _M_w &= __x._M_w; }
1.242 + void _M_do_or(const _Self& __x) { _M_w |= __x._M_w; }
1.243 + void _M_do_xor(const _Self& __x) { _M_w ^= __x._M_w; }
1.244 + void _M_do_left_shift(size_t __shift) { _M_w <<= __shift; }
1.245 + void _M_do_right_shift(size_t __shift) { _M_w >>= __shift; }
1.246 + void _M_do_flip() { _M_w = ~_M_w; }
1.247 + void _M_do_set() { _M_w = ~__STATIC_CAST(_WordT,0); }
1.248 + void _M_do_reset() { _M_w = 0; }
1.249 +
1.250 + bool _M_is_equal(const _Self& __x) const {
1.251 + return _M_w == __x._M_w;
1.252 + }
1.253 + bool _M_is_any() const {
1.254 + return _M_w != 0;
1.255 + }
1.256 +
1.257 + size_t _M_do_count() const {
1.258 + const unsigned char* __byte_ptr = (const unsigned char*)&_M_w;
1.259 + const unsigned char* __end_ptr = ((const unsigned char*)&_M_w)+sizeof(_M_w);
1.260 + return _Bs_G::_S_count(__byte_ptr, __end_ptr);
1.261 + }
1.262 +
1.263 + unsigned long _M_do_to_ulong() const { return _M_w; }
1.264 +
1.265 + inline size_t _M_do_find_first(size_t __not_found) const;
1.266 +
1.267 + // find the next "on" bit that follows "prev"
1.268 + inline size_t _M_do_find_next(size_t __prev, size_t __not_found) const;
1.269 +};
1.270 +
1.271 +
1.272 +// ------------------------------------------------------------
1.273 +//
1.274 +// Definitions of should-be-non-inline functions from the single-word version of
1.275 +// _Base_bitset.
1.276 +//
1.277 +inline size_t
1.278 +_Base_bitset<1UL>::_M_do_find_first(size_t __not_found) const {
1.279 + // typedef unsigned long _WordT;
1.280 + _WordT __thisword = _M_w;
1.281 +
1.282 + if ( __thisword != __STATIC_CAST(_WordT,0) ) {
1.283 + // find byte within word
1.284 + for ( size_t __j = 0; __j < sizeof(_WordT); __j++ ) {
1.285 + unsigned char __this_byte
1.286 + = __STATIC_CAST(unsigned char,(__thisword & (~(unsigned char)0)));
1.287 + if ( __this_byte )
1.288 + return __j*CHAR_BIT + _Bs_G::_S_first_one(__this_byte);
1.289 +
1.290 + __thisword >>= CHAR_BIT;
1.291 + }
1.292 + }
1.293 + // not found, so return a value that indicates failure.
1.294 + return __not_found;
1.295 +}
1.296 +
1.297 +inline size_t
1.298 +_Base_bitset<1UL>::_M_do_find_next(size_t __prev,
1.299 + size_t __not_found ) const {
1.300 + // make bound inclusive
1.301 + ++__prev;
1.302 +
1.303 + // check out of bounds
1.304 + if ( __prev >= __BITS_PER_WORD )
1.305 + return __not_found;
1.306 +
1.307 + // search first (and only) word
1.308 + _WordT __thisword = _M_w;
1.309 +
1.310 + // mask off bits below bound
1.311 + __thisword &= (~__STATIC_CAST(_WordT,0)) << _S_whichbit(__prev);
1.312 +
1.313 + if ( __thisword != __STATIC_CAST(_WordT,0) ) {
1.314 + // find byte within word
1.315 + // get first byte into place
1.316 + __thisword >>= _S_whichbyte(__prev) * CHAR_BIT;
1.317 + for ( size_t __j = _S_whichbyte(__prev); __j < sizeof(_WordT); __j++ ) {
1.318 + unsigned char __this_byte
1.319 + = __STATIC_CAST(unsigned char,(__thisword & (~(unsigned char)0)));
1.320 + if ( __this_byte )
1.321 + return __j*CHAR_BIT + _Bs_G::_S_first_one(__this_byte);
1.322 +
1.323 + __thisword >>= CHAR_BIT;
1.324 + }
1.325 + }
1.326 +
1.327 + // not found, so return a value that indicates failure.
1.328 + return __not_found;
1.329 +} // end _M_do_find_next
1.330 +
1.331 +
1.332 +// ------------------------------------------------------------
1.333 +// Helper class to zero out the unused high-order bits in the highest word.
1.334 +
1.335 +template <size_t _Extrabits> struct _Sanitize {
1.336 + static void _STLP_CALL _M_do_sanitize(unsigned long& __val)
1.337 + { __val &= ~((~__STATIC_CAST(unsigned long,0)) << _Extrabits); }
1.338 +};
1.339 +
1.340 +_STLP_TEMPLATE_NULL struct _Sanitize<0UL> {
1.341 + static void _STLP_CALL _M_do_sanitize(unsigned long) {}
1.342 +};
1.343 +
1.344 +_STLP_MOVE_TO_STD_NAMESPACE
1.345 +
1.346 +// ------------------------------------------------------------
1.347 +// Class bitset.
1.348 +// _Nb may be any nonzero number of type size_t.
1.349 +template<size_t _Nb>
1.350 +class bitset : public _STLP_PRIV _Base_bitset<__BITSET_WORDS(_Nb) > {
1.351 +public:
1.352 + enum { _Words = __BITSET_WORDS(_Nb) } ;
1.353 +
1.354 +private:
1.355 + typedef _STLP_PRIV _Base_bitset< _Words > _Base;
1.356 +
1.357 + void _M_do_sanitize() {
1.358 + _STLP_PRIV _Sanitize<_Nb%__BITS_PER_WORD >::_M_do_sanitize(this->_M_hiword());
1.359 + }
1.360 +public:
1.361 + typedef unsigned long _WordT;
1.362 + struct reference;
1.363 + friend struct reference;
1.364 +
1.365 + // bit reference:
1.366 + struct reference {
1.367 + typedef _STLP_PRIV _Base_bitset<_Words > _Bitset_base;
1.368 + typedef bitset<_Nb> _Bitset;
1.369 + // friend _Bitset;
1.370 + _WordT *_M_wp;
1.371 + size_t _M_bpos;
1.372 +
1.373 + // should be left undefined
1.374 + reference() {}
1.375 +
1.376 + reference( _Bitset& __b, size_t __pos ) {
1.377 + _M_wp = &__b._M_getword(__pos);
1.378 + _M_bpos = _Bitset_base::_S_whichbit(__pos);
1.379 + }
1.380 +
1.381 + public:
1.382 + ~reference() {}
1.383 +
1.384 + // for b[i] = __x;
1.385 + reference& operator=(bool __x) {
1.386 + if ( __x )
1.387 + *_M_wp |= _Bitset_base::_S_maskbit(_M_bpos);
1.388 + else
1.389 + *_M_wp &= ~_Bitset_base::_S_maskbit(_M_bpos);
1.390 +
1.391 + return *this;
1.392 + }
1.393 +
1.394 + // for b[i] = b[__j];
1.395 + reference& operator=(const reference& __j) {
1.396 + if ( (*(__j._M_wp) & _Bitset_base::_S_maskbit(__j._M_bpos)) )
1.397 + *_M_wp |= _Bitset_base::_S_maskbit(_M_bpos);
1.398 + else
1.399 + *_M_wp &= ~_Bitset_base::_S_maskbit(_M_bpos);
1.400 +
1.401 + return *this;
1.402 + }
1.403 +
1.404 + // flips the bit
1.405 + bool operator~() const { return (*(_M_wp) & _Bitset_base::_S_maskbit(_M_bpos)) == 0; }
1.406 +
1.407 + // for __x = b[i];
1.408 + operator bool() const { return (*(_M_wp) & _Bitset_base::_S_maskbit(_M_bpos)) != 0; }
1.409 +
1.410 + // for b[i].flip();
1.411 + reference& flip() {
1.412 + *_M_wp ^= _Bitset_base::_S_maskbit(_M_bpos);
1.413 + return *this;
1.414 + }
1.415 + };
1.416 +
1.417 + // 23.3.5.1 constructors:
1.418 + bitset() {}
1.419 +
1.420 + bitset(unsigned long __val) : _STLP_PRIV _Base_bitset<_Words>(__val) { _M_do_sanitize(); }
1.421 +
1.422 +#if defined (_STLP_MEMBER_TEMPLATES)
1.423 + template<class _CharT, class _Traits, class _Alloc>
1.424 + explicit bitset(const basic_string<_CharT,_Traits,_Alloc>& __s,
1.425 + size_t __pos = 0)
1.426 + : _STLP_PRIV _Base_bitset<_Words >() {
1.427 + if (__pos > __s.size())
1.428 + __stl_throw_out_of_range("bitset");
1.429 + _M_copy_from_string(__s, __pos,
1.430 + basic_string<_CharT, _Traits, _Alloc>::npos);
1.431 + }
1.432 + template<class _CharT, class _Traits, class _Alloc>
1.433 + bitset(const basic_string<_CharT, _Traits, _Alloc>& __s,
1.434 + size_t __pos,
1.435 + size_t __n)
1.436 + : _STLP_PRIV _Base_bitset<_Words >() {
1.437 + if (__pos > __s.size())
1.438 + __stl_throw_out_of_range("bitset");
1.439 + _M_copy_from_string(__s, __pos, __n);
1.440 + }
1.441 +#else /* _STLP_MEMBER_TEMPLATES */
1.442 + explicit bitset(const string& __s,
1.443 + size_t __pos = 0,
1.444 + size_t __n = (size_t)-1)
1.445 + : _STLP_PRIV _Base_bitset<_Words >() {
1.446 + if (__pos > __s.size())
1.447 + __stl_throw_out_of_range("bitset");
1.448 + _M_copy_from_string(__s, __pos, __n);
1.449 + }
1.450 +#endif /* _STLP_MEMBER_TEMPLATES */
1.451 +
1.452 + // 23.3.5.2 bitset operations:
1.453 + bitset<_Nb>& operator&=(const bitset<_Nb>& __rhs) {
1.454 + this->_M_do_and(__rhs);
1.455 + return *this;
1.456 + }
1.457 +
1.458 + bitset<_Nb>& operator|=(const bitset<_Nb>& __rhs) {
1.459 + this->_M_do_or(__rhs);
1.460 + return *this;
1.461 + }
1.462 +
1.463 + bitset<_Nb>& operator^=(const bitset<_Nb>& __rhs) {
1.464 + this->_M_do_xor(__rhs);
1.465 + return *this;
1.466 + }
1.467 +
1.468 + bitset<_Nb>& operator<<=(size_t __pos) {
1.469 + this->_M_do_left_shift(__pos);
1.470 + this->_M_do_sanitize();
1.471 + return *this;
1.472 + }
1.473 +
1.474 + bitset<_Nb>& operator>>=(size_t __pos) {
1.475 + this->_M_do_right_shift(__pos);
1.476 + this->_M_do_sanitize();
1.477 + return *this;
1.478 + }
1.479 +
1.480 + //
1.481 + // Extension:
1.482 + // Versions of single-bit set, reset, flip, test with no range checking.
1.483 + //
1.484 +
1.485 + bitset<_Nb>& _Unchecked_set(size_t __pos) {
1.486 + this->_M_getword(__pos) |= _STLP_PRIV _Base_bitset<_Words > ::_S_maskbit(__pos);
1.487 + return *this;
1.488 + }
1.489 +
1.490 + bitset<_Nb>& _Unchecked_set(size_t __pos, int __val) {
1.491 + if (__val)
1.492 + this->_M_getword(__pos) |= this->_S_maskbit(__pos);
1.493 + else
1.494 + this->_M_getword(__pos) &= ~ this->_S_maskbit(__pos);
1.495 +
1.496 + return *this;
1.497 + }
1.498 +
1.499 + bitset<_Nb>& _Unchecked_reset(size_t __pos) {
1.500 + this->_M_getword(__pos) &= ~ this->_S_maskbit(__pos);
1.501 + return *this;
1.502 + }
1.503 +
1.504 + bitset<_Nb>& _Unchecked_flip(size_t __pos) {
1.505 + this->_M_getword(__pos) ^= this->_S_maskbit(__pos);
1.506 + return *this;
1.507 + }
1.508 +
1.509 + bool _Unchecked_test(size_t __pos) const {
1.510 + return (this->_M_getword(__pos) & this->_S_maskbit(__pos)) != __STATIC_CAST(_WordT,0);
1.511 + }
1.512 +
1.513 + // Set, reset, and flip.
1.514 +
1.515 + bitset<_Nb>& set() {
1.516 + this->_M_do_set();
1.517 + this->_M_do_sanitize();
1.518 + return *this;
1.519 + }
1.520 +
1.521 + bitset<_Nb>& set(size_t __pos) {
1.522 + if (__pos >= _Nb)
1.523 + __stl_throw_out_of_range("bitset");
1.524 + return _Unchecked_set(__pos);
1.525 + }
1.526 +
1.527 + bitset<_Nb>& set(size_t __pos, int __val) {
1.528 + if (__pos >= _Nb)
1.529 + __stl_throw_out_of_range("bitset");
1.530 + return _Unchecked_set(__pos, __val);
1.531 + }
1.532 +
1.533 + bitset<_Nb>& reset() {
1.534 + this->_M_do_reset();
1.535 + return *this;
1.536 + }
1.537 +
1.538 + bitset<_Nb>& reset(size_t __pos) {
1.539 + if (__pos >= _Nb)
1.540 + __stl_throw_out_of_range("bitset");
1.541 +
1.542 + return _Unchecked_reset(__pos);
1.543 + }
1.544 +
1.545 + bitset<_Nb>& flip() {
1.546 + this->_M_do_flip();
1.547 + this->_M_do_sanitize();
1.548 + return *this;
1.549 + }
1.550 +
1.551 + bitset<_Nb>& flip(size_t __pos) {
1.552 + if (__pos >= _Nb)
1.553 + __stl_throw_out_of_range("bitset");
1.554 +
1.555 + return _Unchecked_flip(__pos);
1.556 + }
1.557 +
1.558 + bitset<_Nb> operator~() const {
1.559 + return bitset<_Nb>(*this).flip();
1.560 + }
1.561 +
1.562 + // element access:
1.563 + //for b[i];
1.564 + reference operator[](size_t __pos) { return reference(*this,__pos); }
1.565 + bool operator[](size_t __pos) const { return _Unchecked_test(__pos); }
1.566 +
1.567 + unsigned long to_ulong() const { return this->_M_do_to_ulong(); }
1.568 +
1.569 +#if defined (_STLP_MEMBER_TEMPLATES) && !defined (_STLP_NO_EXPLICIT_FUNCTION_TMPL_ARGS)
1.570 + template <class _CharT, class _Traits, class _Alloc>
1.571 + basic_string<_CharT, _Traits, _Alloc> to_string() const {
1.572 + basic_string<_CharT, _Traits, _Alloc> __result;
1.573 + _M_copy_to_string(__result);
1.574 + return __result;
1.575 + }
1.576 +#else
1.577 + string to_string() const {
1.578 + string __result;
1.579 + _M_copy_to_string(__result);
1.580 + return __result;
1.581 + }
1.582 +#endif /* _STLP_EXPLICIT_FUNCTION_TMPL_ARGS */
1.583 +
1.584 + size_t count() const { return this->_M_do_count(); }
1.585 +
1.586 + size_t size() const { return _Nb; }
1.587 +
1.588 + bool operator==(const bitset<_Nb>& __rhs) const {
1.589 + return this->_M_is_equal(__rhs);
1.590 + }
1.591 + bool operator!=(const bitset<_Nb>& __rhs) const {
1.592 + return !this->_M_is_equal(__rhs);
1.593 + }
1.594 +
1.595 + bool test(size_t __pos) const {
1.596 + if (__pos >= _Nb)
1.597 + __stl_throw_out_of_range("bitset");
1.598 +
1.599 + return _Unchecked_test(__pos);
1.600 + }
1.601 +
1.602 + bool any() const { return this->_M_is_any(); }
1.603 + bool none() const { return !this->_M_is_any(); }
1.604 +
1.605 + bitset<_Nb> operator<<(size_t __pos) const {
1.606 + bitset<_Nb> __result(*this);
1.607 + __result <<= __pos ; return __result;
1.608 + }
1.609 + bitset<_Nb> operator>>(size_t __pos) const {
1.610 + bitset<_Nb> __result(*this);
1.611 + __result >>= __pos ; return __result;
1.612 + }
1.613 +
1.614 +#if !defined (_STLP_NO_EXTENSIONS)
1.615 + //
1.616 + // EXTENSIONS: bit-find operations. These operations are
1.617 + // experimental, and are subject to change or removal in future
1.618 + // versions.
1.619 + //
1.620 +
1.621 + // find the index of the first "on" bit
1.622 + size_t _Find_first() const
1.623 + { return this->_M_do_find_first(_Nb); }
1.624 +
1.625 + // find the index of the next "on" bit after prev
1.626 + size_t _Find_next( size_t __prev ) const
1.627 + { return this->_M_do_find_next(__prev, _Nb); }
1.628 +#endif
1.629 +
1.630 +//
1.631 +// Definitions of should-be non-inline member functions.
1.632 +//
1.633 +#if defined (_STLP_MEMBER_TEMPLATES)
1.634 + template<class _CharT, class _Traits, class _Alloc>
1.635 + void _M_copy_from_string(const basic_string<_CharT,_Traits,_Alloc>& __s,
1.636 + size_t __pos, size_t __n) {
1.637 +#else
1.638 + void _M_copy_from_string(const string& __s,
1.639 + size_t __pos, size_t __n) {
1.640 + typedef typename string::traits_type _Traits;
1.641 +#endif
1.642 + reset();
1.643 + size_t __tmp = _Nb;
1.644 + const size_t __Nbits = (min) (__tmp, (min) (__n, __s.size() - __pos));
1.645 + for ( size_t __i= 0; __i < __Nbits; ++__i) {
1.646 + typename _Traits::int_type __k = _Traits::to_int_type(__s[__pos + __Nbits - __i - 1]);
1.647 + // boris : widen() ?
1.648 + if (__k == '1')
1.649 + set(__i);
1.650 + else if (__k != '0')
1.651 + __stl_throw_invalid_argument("bitset");
1.652 + }
1.653 + }
1.654 +
1.655 +#if defined (_STLP_MEMBER_TEMPLATES)
1.656 + template <class _CharT, class _Traits, class _Alloc>
1.657 + void _M_copy_to_string(basic_string<_CharT, _Traits, _Alloc>& __s) const
1.658 +#else
1.659 + void _M_copy_to_string(string& __s) const
1.660 +#endif
1.661 + {
1.662 + __s.assign(_Nb, '0');
1.663 +
1.664 + for (size_t __i = 0; __i < _Nb; ++__i) {
1.665 + if (_Unchecked_test(__i))
1.666 + __s[_Nb - 1 - __i] = '1';
1.667 + }
1.668 + }
1.669 +
1.670 +#if defined (_STLP_NON_TYPE_TMPL_PARAM_BUG)
1.671 + bitset<_Nb> operator&(const bitset<_Nb>& __y) const {
1.672 + bitset<_Nb> __result(*this);
1.673 + __result &= __y;
1.674 + return __result;
1.675 + }
1.676 + bitset<_Nb> operator|(const bitset<_Nb>& __y) const {
1.677 + bitset<_Nb> __result(*this);
1.678 + __result |= __y;
1.679 + return __result;
1.680 + }
1.681 + bitset<_Nb> operator^(const bitset<_Nb>& __y) const {
1.682 + bitset<_Nb> __result(*this);
1.683 + __result ^= __y;
1.684 + return __result;
1.685 + }
1.686 +#endif
1.687 +};
1.688 +
1.689 +// ------------------------------------------------------------
1.690 +//
1.691 +// 23.3.5.3 bitset operations:
1.692 +//
1.693 +#if ! defined (_STLP_NON_TYPE_TMPL_PARAM_BUG)
1.694 +template <size_t _Nb>
1.695 +inline bitset<_Nb> _STLP_CALL
1.696 +operator&(const bitset<_Nb>& __x,
1.697 + const bitset<_Nb>& __y) {
1.698 + bitset<_Nb> __result(__x);
1.699 + __result &= __y;
1.700 + return __result;
1.701 +}
1.702 +
1.703 +
1.704 +template <size_t _Nb>
1.705 +inline bitset<_Nb> _STLP_CALL
1.706 +operator|(const bitset<_Nb>& __x,
1.707 + const bitset<_Nb>& __y) {
1.708 + bitset<_Nb> __result(__x);
1.709 + __result |= __y;
1.710 + return __result;
1.711 +}
1.712 +
1.713 +template <size_t _Nb>
1.714 +inline bitset<_Nb> _STLP_CALL
1.715 +operator^(const bitset<_Nb>& __x,
1.716 + const bitset<_Nb>& __y) {
1.717 + bitset<_Nb> __result(__x);
1.718 + __result ^= __y;
1.719 + return __result;
1.720 +}
1.721 +
1.722 +#if !defined (_STLP_USE_NO_IOSTREAMS)
1.723 +
1.724 +_STLP_END_NAMESPACE
1.725 +
1.726 +# if !(defined (_STLP_MSVC) || (_STLP_MSVC < 1300)) && \
1.727 + !(defined(__SUNPRO_CC) || (__SUNPRO_CC < 0x500))
1.728 +
1.729 +#ifndef _STLP_INTERNAL_IOSFWD
1.730 +# include <stl/_iosfwd.h>
1.731 +#endif
1.732 +
1.733 +_STLP_BEGIN_NAMESPACE
1.734 +
1.735 +template <class _CharT, class _Traits, size_t _Nb>
1.736 +basic_istream<_CharT, _Traits>& _STLP_CALL
1.737 +operator>>(basic_istream<_CharT, _Traits>& __is, bitset<_Nb>& __x);
1.738 +
1.739 +template <class _CharT, class _Traits, size_t _Nb>
1.740 +basic_ostream<_CharT, _Traits>& _STLP_CALL
1.741 +operator<<(basic_ostream<_CharT, _Traits>& __os, const bitset<_Nb>& __x);
1.742 +
1.743 +# else
1.744 +
1.745 +#ifndef _STLP_STRING_IO_H
1.746 +# include <stl/_string_io.h> //includes _istream.h and _ostream.h
1.747 +#endif
1.748 +
1.749 +_STLP_BEGIN_NAMESPACE
1.750 +
1.751 +template <size_t _Nb>
1.752 +istream& _STLP_CALL
1.753 +operator>>(istream& __is, bitset<_Nb>& __x) {
1.754 + typedef typename string::traits_type _Traits;
1.755 + string __tmp;
1.756 + __tmp.reserve(_Nb);
1.757 +
1.758 + // Skip whitespace
1.759 + typename istream::sentry __sentry(__is);
1.760 + if (__sentry) {
1.761 + streambuf* __buf = __is.rdbuf();
1.762 + for (size_t __i = 0; __i < _Nb; ++__i) {
1.763 + static typename _Traits::int_type __eof = _Traits::eof();
1.764 +
1.765 + typename _Traits::int_type __c1 = __buf->sbumpc();
1.766 + if (_Traits::eq_int_type(__c1, __eof)) {
1.767 + __is.setstate(ios_base::eofbit);
1.768 + break;
1.769 + }
1.770 + else {
1.771 + typename _Traits::char_type __c2 = _Traits::to_char_type(__c1);
1.772 + char __c = __is.narrow(__c2, '*');
1.773 +
1.774 + if (__c == '0' || __c == '1')
1.775 + __tmp.push_back(__c);
1.776 + else if (_Traits::eq_int_type(__buf->sputbackc(__c2), __eof)) {
1.777 + __is.setstate(ios_base::failbit);
1.778 + break;
1.779 + }
1.780 + }
1.781 + }
1.782 +
1.783 + if (__tmp.empty())
1.784 + __is.setstate(ios_base::failbit);
1.785 + else
1.786 + __x._M_copy_from_string(__tmp, __STATIC_CAST(size_t,0), _Nb);
1.787 + }
1.788 +
1.789 + return __is;
1.790 +}
1.791 +
1.792 +template <size_t _Nb>
1.793 +ostream& _STLP_CALL
1.794 +operator<<(ostream& __os, const bitset<_Nb>& __x) {
1.795 + string __tmp;
1.796 + __x._M_copy_to_string(__tmp);
1.797 + return __os << __tmp;
1.798 +}
1.799 +
1.800 +# if !defined (_STLP_NO_WCHAR_T)
1.801 +
1.802 +template <size_t _Nb>
1.803 +wistream& _STLP_CALL
1.804 +operator>>(wistream& __is, bitset<_Nb>& __x) {
1.805 + typedef typename wstring::traits_type _Traits;
1.806 + wstring __tmp;
1.807 + __tmp.reserve(_Nb);
1.808 +
1.809 + // Skip whitespace
1.810 + typename wistream::sentry __sentry(__is);
1.811 + if (__sentry) {
1.812 + wstreambuf* __buf = __is.rdbuf();
1.813 + for (size_t __i = 0; __i < _Nb; ++__i) {
1.814 + static typename _Traits::int_type __eof = _Traits::eof();
1.815 +
1.816 + typename _Traits::int_type __c1 = __buf->sbumpc();
1.817 + if (_Traits::eq_int_type(__c1, __eof)) {
1.818 + __is.setstate(ios_base::eofbit);
1.819 + break;
1.820 + }
1.821 + else {
1.822 + typename _Traits::char_type __c2 = _Traits::to_char_type(__c1);
1.823 + char __c = __is.narrow(__c2, '*');
1.824 +
1.825 + if (__c == '0' || __c == '1')
1.826 + __tmp.push_back(__c);
1.827 + else if (_Traits::eq_int_type(__buf->sputbackc(__c2), __eof)) {
1.828 + __is.setstate(ios_base::failbit);
1.829 + break;
1.830 + }
1.831 + }
1.832 + }
1.833 +
1.834 + if (__tmp.empty())
1.835 + __is.setstate(ios_base::failbit);
1.836 + else
1.837 + __x._M_copy_from_string(__tmp, __STATIC_CAST(size_t,0), _Nb);
1.838 + }
1.839 +
1.840 + return __is;
1.841 +}
1.842 +
1.843 +template <size_t _Nb>
1.844 +wostream& _STLP_CALL
1.845 +operator<<(wostream& __os, const bitset<_Nb>& __x) {
1.846 + wstring __tmp;
1.847 + __x._M_copy_to_string(__tmp);
1.848 + return __os << __tmp;
1.849 +}
1.850 +
1.851 +# endif /* _STLP_NO_WCHAR_T */
1.852 +# endif
1.853 +#endif
1.854 +
1.855 +#endif /* _STLP_NON_TYPE_TMPL_PARAM_BUG */
1.856 +
1.857 +#undef bitset
1.858 +
1.859 +_STLP_END_NAMESPACE
1.860 +
1.861 +#undef __BITS_PER_WORD
1.862 +#undef __BITSET_WORDS
1.863 +
1.864 +#if !defined (_STLP_LINK_TIME_INSTANTIATION)
1.865 +# include <stl/_bitset.c>
1.866 +#endif
1.867 +
1.868 +#endif /* _STLP_BITSET_H */
1.869 +
1.870 +// Local Variables:
1.871 +// mode:C++
1.872 +// End: