williamr@4: /* williamr@4: * © Portions copyright (c) 2006-2007 Nokia Corporation. All rights reserved. williamr@4: * Copyright (c) 1998 williamr@4: * Silicon Graphics Computer Systems, Inc. williamr@4: * williamr@4: * Copyright (c) 1999 williamr@4: * Boris Fomitchev williamr@4: * williamr@4: * This material is provided "as is", with absolutely no warranty expressed williamr@4: * or implied. Any use is at your own risk. williamr@4: * williamr@4: * Permission to use or copy this software for any purpose is hereby granted williamr@4: * without fee, provided the above notices are retained on all copies. williamr@4: * Permission to modify the code and to distribute modified code is granted, williamr@4: * provided the above notices are retained, and a notice that the code was williamr@4: * modified is included with the above copyright notice. williamr@4: * williamr@4: */ williamr@4: williamr@4: #ifndef _STLP_BITSET_H williamr@4: #define _STLP_BITSET_H williamr@4: williamr@4: // A bitset of size N has N % (sizeof(unsigned long) * CHAR_BIT) unused williamr@4: // bits. (They are the high- order bits in the highest word.) It is williamr@4: // a class invariant of class bitset<> that those unused bits are williamr@4: // always zero. williamr@4: williamr@4: // Most of the actual code isn't contained in bitset<> itself, but in the williamr@4: // base class _Base_bitset. The base class works with whole words, not with williamr@4: // individual bits. This allows us to specialize _Base_bitset for the williamr@4: // important special case where the bitset is only a single word. williamr@4: williamr@4: // The C++ standard does not define the precise semantics of operator[]. williamr@4: // In this implementation the const version of operator[] is equivalent williamr@4: // to test(), except that it does no range checking. The non-const version williamr@4: // returns a reference to a bit, again without doing any range checking. williamr@4: williamr@4: williamr@4: # ifndef _STLP_INTERNAL_ALGOBASE_H williamr@4: # include williamr@4: # endif williamr@4: williamr@4: # ifndef _STLP_INTERNAL_ALLOC_H williamr@4: # include williamr@4: # endif williamr@4: williamr@4: # ifndef _STLP_INTERNAL_ITERATOR_H williamr@4: # include williamr@4: # endif williamr@4: williamr@4: # ifndef _STLP_INTERNAL_UNINITIALIZED_H williamr@4: # include williamr@4: # endif williamr@4: williamr@4: # ifndef _STLP_RANGE_ERRORS_H williamr@4: # include williamr@4: # endif williamr@4: williamr@4: # ifndef _STLP_STRING williamr@4: # include williamr@4: # endif williamr@4: williamr@4: # ifndef _STLP_ISTREAM williamr@4: # include williamr@4: # endif williamr@4: williamr@4: #define __BITS_PER_WORD (CHAR_BIT*sizeof(unsigned long)) williamr@4: #define __BITSET_WORDS(__n) ((__n + __BITS_PER_WORD - 1)/__BITS_PER_WORD) williamr@4: williamr@4: _STLP_BEGIN_NAMESPACE williamr@4: williamr@4: // structure to aid in counting bits williamr@4: template williamr@4: class _Bs_G { williamr@4: public: williamr@4: static const unsigned char _S_bit_count[256]; williamr@4: // Mapping from 8 bit unsigned integers to the index of the first one williamr@4: // bit: williamr@4: static const unsigned char _S_first_one[256]; williamr@4: }; williamr@4: williamr@4: // williamr@4: // Base class: general case. williamr@4: // williamr@4: williamr@4: template williamr@4: struct _Base_bitset { williamr@4: typedef unsigned long _WordT; williamr@4: williamr@4: _WordT _M_w[_Nw]; // 0 is the least significant word. williamr@4: williamr@4: _Base_bitset( void ) { _M_do_reset(); } williamr@4: williamr@4: _Base_bitset(unsigned long __val) { williamr@4: _M_do_reset(); williamr@4: _M_w[0] = __val; williamr@4: } williamr@4: williamr@4: static size_t _STLP_CALL _S_whichword( size_t __pos ) { williamr@4: return __pos / __BITS_PER_WORD; williamr@4: } williamr@4: static size_t _STLP_CALL _S_whichbyte( size_t __pos ) { williamr@4: return (__pos % __BITS_PER_WORD) / CHAR_BIT; williamr@4: } williamr@4: static size_t _STLP_CALL _S_whichbit( size_t __pos ) { williamr@4: return __pos % __BITS_PER_WORD; williamr@4: } williamr@4: static _WordT _STLP_CALL _S_maskbit( size_t __pos ) { williamr@4: return __STATIC_CAST(_WordT,1) << _S_whichbit(__pos); williamr@4: } williamr@4: williamr@4: _WordT& _M_getword(size_t __pos) { return _M_w[_S_whichword(__pos)]; } williamr@4: _WordT _M_getword(size_t __pos) const { return _M_w[_S_whichword(__pos)]; } williamr@4: williamr@4: _WordT& _M_hiword() { return _M_w[_Nw - 1]; } williamr@4: _WordT _M_hiword() const { return _M_w[_Nw - 1]; } williamr@4: williamr@4: void _M_do_and(const _Base_bitset<_Nw>& __x) { williamr@4: for ( size_t __i = 0; __i < _Nw; __i++ ) { williamr@4: _M_w[__i] &= __x._M_w[__i]; williamr@4: } williamr@4: } williamr@4: williamr@4: void _M_do_or(const _Base_bitset<_Nw>& __x) { williamr@4: for ( size_t __i = 0; __i < _Nw; __i++ ) { williamr@4: _M_w[__i] |= __x._M_w[__i]; williamr@4: } williamr@4: } williamr@4: williamr@4: void _M_do_xor(const _Base_bitset<_Nw>& __x) { williamr@4: for ( size_t __i = 0; __i < _Nw; __i++ ) { williamr@4: _M_w[__i] ^= __x._M_w[__i]; williamr@4: } williamr@4: } williamr@4: williamr@4: void _M_do_left_shift(size_t __shift); williamr@4: williamr@4: void _M_do_right_shift(size_t __shift); williamr@4: williamr@4: void _M_do_flip() { williamr@4: for ( size_t __i = 0; __i < _Nw; __i++ ) { williamr@4: _M_w[__i] = ~_M_w[__i]; williamr@4: } williamr@4: } williamr@4: williamr@4: void _M_do_set() { williamr@4: for ( size_t __i = 0; __i < _Nw; __i++ ) { williamr@4: _M_w[__i] = ~__STATIC_CAST(_WordT,0); williamr@4: } williamr@4: } williamr@4: williamr@4: williamr@4: void _M_do_reset() { memset(_M_w, 0, _Nw * sizeof(_WordT)); } williamr@4: williamr@4: bool _M_is_equal(const _Base_bitset<_Nw>& __x) const { williamr@4: for (size_t __i = 0; __i < _Nw; ++__i) { williamr@4: if (_M_w[__i] != __x._M_w[__i]) williamr@4: return false; williamr@4: } williamr@4: return true; williamr@4: } williamr@4: williamr@4: bool _M_is_any() const { williamr@4: for ( size_t __i = 0; __i < _Nw ; __i++ ) { williamr@4: if ( _M_w[__i] != __STATIC_CAST(_WordT,0) ) williamr@4: return true; williamr@4: } williamr@4: return false; williamr@4: } williamr@4: williamr@4: size_t _M_do_count() const { williamr@4: size_t __result = 0; williamr@4: const unsigned char* __byte_ptr = (const unsigned char*)_M_w; williamr@4: const unsigned char* __end_ptr = (const unsigned char*)(_M_w+_Nw); williamr@4: williamr@4: while ( __byte_ptr < __end_ptr ) { williamr@4: __result += _Bs_G::_S_bit_count[*__byte_ptr]; williamr@4: __byte_ptr++; williamr@4: } williamr@4: return __result; williamr@4: } williamr@4: williamr@4: unsigned long _M_do_to_ulong() const; williamr@4: williamr@4: // find first "on" bit williamr@4: size_t _M_do_find_first(size_t __not_found) const; williamr@4: williamr@4: // find the next "on" bit that follows "prev" williamr@4: size_t _M_do_find_next(size_t __prev, size_t __not_found) const; williamr@4: }; williamr@4: williamr@4: // williamr@4: // Base class: specialization for a single word. williamr@4: // williamr@4: williamr@4: _STLP_TEMPLATE_NULL williamr@4: struct _Base_bitset<1UL> { williamr@4: typedef unsigned long _WordT; williamr@4: typedef _Base_bitset<1UL> _Self; williamr@4: williamr@4: _WordT _M_w; williamr@4: williamr@4: _Base_bitset( void ) : _M_w(0) {} williamr@4: _Base_bitset(unsigned long __val) : _M_w(__val) {} williamr@4: williamr@4: static size_t _STLP_CALL _S_whichword( size_t __pos ) { williamr@4: return __pos / __BITS_PER_WORD ; williamr@4: } williamr@4: static size_t _STLP_CALL _S_whichbyte( size_t __pos ) { williamr@4: return (__pos % __BITS_PER_WORD) / CHAR_BIT; williamr@4: } williamr@4: static size_t _STLP_CALL _S_whichbit( size_t __pos ) { williamr@4: return __pos % __BITS_PER_WORD; williamr@4: } williamr@4: static _WordT _STLP_CALL _S_maskbit( size_t __pos ) { williamr@4: return (__STATIC_CAST(_WordT,1)) << _S_whichbit(__pos); williamr@4: } williamr@4: williamr@4: _WordT& _M_getword(size_t) { return _M_w; } williamr@4: _WordT _M_getword(size_t) const { return _M_w; } williamr@4: williamr@4: _WordT& _M_hiword() { return _M_w; } williamr@4: _WordT _M_hiword() const { return _M_w; } williamr@4: williamr@4: williamr@4: void _M_do_and(const _Self& __x) { _M_w &= __x._M_w; } williamr@4: void _M_do_or(const _Self& __x) { _M_w |= __x._M_w; } williamr@4: void _M_do_xor(const _Self& __x) { _M_w ^= __x._M_w; } williamr@4: void _M_do_left_shift(size_t __shift) { _M_w <<= __shift; } williamr@4: void _M_do_right_shift(size_t __shift) { _M_w >>= __shift; } williamr@4: void _M_do_flip() { _M_w = ~_M_w; } williamr@4: void _M_do_set() { _M_w = ~__STATIC_CAST(_WordT,0); } williamr@4: void _M_do_reset() { _M_w = 0; } williamr@4: williamr@4: bool _M_is_equal(const _Self& __x) const { williamr@4: return _M_w == __x._M_w; williamr@4: } williamr@4: bool _M_is_any() const { williamr@4: return _M_w != 0; williamr@4: } williamr@4: williamr@4: size_t _M_do_count() const { williamr@4: size_t __result = 0; williamr@4: const unsigned char* __byte_ptr = (const unsigned char*)&_M_w; williamr@4: const unsigned char* __end_ptr = ((const unsigned char*)&_M_w)+sizeof(_M_w); williamr@4: while ( __byte_ptr < __end_ptr ) { williamr@4: __result += _Bs_G::_S_bit_count[*__byte_ptr]; williamr@4: __byte_ptr++; williamr@4: } williamr@4: return __result; williamr@4: } williamr@4: williamr@4: unsigned long _M_do_to_ulong() const { return _M_w; } williamr@4: williamr@4: inline size_t _M_do_find_first(size_t __not_found) const; williamr@4: williamr@4: // find the next "on" bit that follows "prev" williamr@4: inline size_t _M_do_find_next(size_t __prev, size_t __not_found) const; williamr@4: williamr@4: }; williamr@4: williamr@4: williamr@4: // ------------------------------------------------------------ williamr@4: // williamr@4: // Definitions of should-be-non-inline functions from the single-word version of williamr@4: // _Base_bitset. williamr@4: // williamr@4: williamr@4: inline size_t williamr@4: _Base_bitset<1UL>::_M_do_find_first(size_t __not_found) const williamr@4: { williamr@4: // typedef unsigned long _WordT; williamr@4: _WordT __thisword = _M_w; williamr@4: williamr@4: if ( __thisword != __STATIC_CAST(_WordT,0) ) { williamr@4: // find byte within word williamr@4: for ( size_t __j = 0; __j < sizeof(_WordT); __j++ ) { williamr@4: unsigned char __this_byte williamr@4: = __STATIC_CAST(unsigned char,(__thisword & (~(unsigned char)0))); williamr@4: if ( __this_byte ) williamr@4: return __j*CHAR_BIT + _Bs_G::_S_first_one[__this_byte]; williamr@4: williamr@4: __thisword >>= CHAR_BIT; williamr@4: } williamr@4: } williamr@4: // not found, so return a value that indicates failure. williamr@4: return __not_found; williamr@4: } williamr@4: williamr@4: inline size_t williamr@4: _Base_bitset<1UL>::_M_do_find_next(size_t __prev, williamr@4: size_t __not_found ) const williamr@4: { williamr@4: // make bound inclusive williamr@4: ++__prev; williamr@4: williamr@4: // check out of bounds williamr@4: if ( __prev >= __BITS_PER_WORD ) williamr@4: return __not_found; williamr@4: williamr@4: // search first (and only) word williamr@4: _WordT __thisword = _M_w; williamr@4: williamr@4: // mask off bits below bound williamr@4: __thisword &= (~__STATIC_CAST(_WordT,0)) << _S_whichbit(__prev); williamr@4: williamr@4: if ( __thisword != __STATIC_CAST(_WordT,0) ) { williamr@4: // find byte within word williamr@4: // get first byte into place williamr@4: __thisword >>= _S_whichbyte(__prev) * CHAR_BIT; williamr@4: for ( size_t __j = _S_whichbyte(__prev); __j < sizeof(_WordT); __j++ ) { williamr@4: unsigned char __this_byte williamr@4: = __STATIC_CAST(unsigned char,(__thisword & (~(unsigned char)0))); williamr@4: if ( __this_byte ) williamr@4: return __j*CHAR_BIT + _Bs_G::_S_first_one[__this_byte]; williamr@4: williamr@4: __thisword >>= CHAR_BIT; williamr@4: } williamr@4: } williamr@4: williamr@4: // not found, so return a value that indicates failure. williamr@4: return __not_found; williamr@4: } // end _M_do_find_next williamr@4: williamr@4: williamr@4: // ------------------------------------------------------------ williamr@4: // Helper class to zero out the unused high-order bits in the highest word. williamr@4: williamr@4: template struct _Sanitize { williamr@4: static void _STLP_CALL _M_do_sanitize(unsigned long& __val) williamr@4: { __val &= ~((~__STATIC_CAST(unsigned long,0)) << _Extrabits); } williamr@4: }; williamr@4: williamr@4: _STLP_TEMPLATE_NULL struct _Sanitize<0UL> { williamr@4: static void _STLP_CALL _M_do_sanitize(unsigned long) {} williamr@4: }; williamr@4: williamr@4: // ------------------------------------------------------------ williamr@4: // Class bitset. williamr@4: // _Nb may be any nonzero number of type size_t. williamr@4: williamr@4: williamr@4: template williamr@4: class bitset : public _Base_bitset<__BITSET_WORDS(_Nb) > williamr@4: { williamr@4: public: williamr@4: enum { _Words = __BITSET_WORDS(_Nb) } ; williamr@4: williamr@4: private: williamr@4: typedef _Base_bitset< _Words > _Base; williamr@4: williamr@4: void _M_do_sanitize() { williamr@4: _Sanitize<_Nb%__BITS_PER_WORD >::_M_do_sanitize(this->_M_hiword()); williamr@4: } williamr@4: public: williamr@4: typedef unsigned long _WordT; williamr@4: struct reference; williamr@4: friend struct reference; williamr@4: williamr@4: // bit reference: williamr@4: struct reference { williamr@4: typedef _Base_bitset<_Words > _Bitset_base; williamr@4: typedef bitset<_Nb> _Bitset; williamr@4: // friend _Bitset; williamr@4: _WordT *_M_wp; williamr@4: size_t _M_bpos; williamr@4: williamr@4: // should be left undefined williamr@4: reference() {} williamr@4: williamr@4: reference( _Bitset& __b, size_t __pos ) { williamr@4: _M_wp = &__b._M_getword(__pos); williamr@4: _M_bpos = _Bitset_base::_S_whichbit(__pos); williamr@4: } williamr@4: williamr@4: public: williamr@4: ~reference() {} williamr@4: williamr@4: // for b[i] = __x; williamr@4: reference& operator=(bool __x) { williamr@4: if ( __x ) williamr@4: *_M_wp |= _Bitset_base::_S_maskbit(_M_bpos); williamr@4: else williamr@4: *_M_wp &= ~_Bitset_base::_S_maskbit(_M_bpos); williamr@4: williamr@4: return *this; williamr@4: } williamr@4: williamr@4: // for b[i] = b[__j]; williamr@4: reference& operator=(const reference& __j) { williamr@4: if ( (*(__j._M_wp) & _Bitset_base::_S_maskbit(__j._M_bpos)) ) williamr@4: *_M_wp |= _Bitset_base::_S_maskbit(_M_bpos); williamr@4: else williamr@4: *_M_wp &= ~_Bitset_base::_S_maskbit(_M_bpos); williamr@4: williamr@4: return *this; williamr@4: } williamr@4: williamr@4: // flips the bit williamr@4: bool operator~() const { return (*(_M_wp) & _Bitset_base::_S_maskbit(_M_bpos)) == 0; } williamr@4: williamr@4: // for __x = b[i]; williamr@4: operator bool() const { return (*(_M_wp) & _Bitset_base::_S_maskbit(_M_bpos)) != 0; } williamr@4: williamr@4: // for b[i].flip(); williamr@4: reference& flip() { williamr@4: *_M_wp ^= _Bitset_base::_S_maskbit(_M_bpos); williamr@4: return *this; williamr@4: } williamr@4: }; williamr@4: williamr@4: // 23.3.5.1 constructors: williamr@4: bitset() {} williamr@4: williamr@4: bitset(unsigned long __val) : _Base_bitset<_Words>(__val) { _M_do_sanitize(); } williamr@4: williamr@4: # ifdef _STLP_MEMBER_TEMPLATES williamr@4: template williamr@4: explicit bitset(const basic_string<_CharT,_Traits,_Alloc>& __s, williamr@4: size_t __pos = 0) williamr@4: : _Base_bitset<_Words >() williamr@4: { williamr@4: if (__pos > __s.size()) williamr@4: __stl_throw_out_of_range("bitset"); williamr@4: _M_copy_from_string(__s, __pos, williamr@4: basic_string<_CharT, _Traits, _Alloc>::npos); williamr@4: } williamr@4: template williamr@4: bitset(const basic_string<_CharT, _Traits, _Alloc>& __s, williamr@4: size_t __pos, williamr@4: size_t __n) williamr@4: : _Base_bitset<_Words >() williamr@4: { williamr@4: if (__pos > __s.size()) williamr@4: __stl_throw_out_of_range("bitset"); williamr@4: _M_copy_from_string(__s, __pos, __n); williamr@4: } williamr@4: #else /* _STLP_MEMBER_TEMPLATES */ williamr@4: explicit bitset(const string& __s, williamr@4: size_t __pos = 0, williamr@4: size_t __n = (size_t)-1) williamr@4: : _Base_bitset<_Words >() williamr@4: { williamr@4: if (__pos > __s.size()) williamr@4: __stl_throw_out_of_range("bitset"); williamr@4: _M_copy_from_string(__s, __pos, __n); williamr@4: } williamr@4: #endif /* _STLP_MEMBER_TEMPLATES */ williamr@4: williamr@4: // 23.3.5.2 bitset operations: williamr@4: bitset<_Nb>& operator&=(const bitset<_Nb>& __rhs) { williamr@4: this->_M_do_and(__rhs); williamr@4: return *this; williamr@4: } williamr@4: williamr@4: bitset<_Nb>& operator|=(const bitset<_Nb>& __rhs) { williamr@4: this->_M_do_or(__rhs); williamr@4: return *this; williamr@4: } williamr@4: williamr@4: bitset<_Nb>& operator^=(const bitset<_Nb>& __rhs) { williamr@4: this->_M_do_xor(__rhs); williamr@4: return *this; williamr@4: } williamr@4: williamr@4: bitset<_Nb>& operator<<=(size_t __pos) { williamr@4: #ifdef __SYMBIAN32__ williamr@4: if(__pos < _Nb) williamr@4: { williamr@4: this->_M_do_left_shift(__pos); williamr@4: this->_M_do_sanitize(); williamr@4: } williamr@4: else williamr@4: this->_M_do_reset(); williamr@4: #else williamr@4: this->_M_do_left_shift(__pos); williamr@4: this->_M_do_sanitize(); williamr@4: #endif williamr@4: williamr@4: return *this; williamr@4: } williamr@4: williamr@4: bitset<_Nb>& operator>>=(size_t __pos) { williamr@4: #ifdef __SYMBIAN32__ williamr@4: if(__pos < _Nb) williamr@4: { williamr@4: this->_M_do_right_shift(__pos); williamr@4: this->_M_do_sanitize(); williamr@4: } williamr@4: else williamr@4: this->_M_do_reset(); williamr@4: #else williamr@4: this->_M_do_right_shift(__pos); williamr@4: this->_M_do_sanitize(); williamr@4: #endif williamr@4: williamr@4: return *this; williamr@4: } williamr@4: williamr@4: // williamr@4: // Extension: williamr@4: // Versions of single-bit set, reset, flip, test with no range checking. williamr@4: // williamr@4: williamr@4: bitset<_Nb>& _Unchecked_set(size_t __pos) { williamr@4: this->_M_getword(__pos) |= _Base_bitset<_Words > ::_S_maskbit(__pos); williamr@4: return *this; williamr@4: } williamr@4: williamr@4: bitset<_Nb>& _Unchecked_set(size_t __pos, int __val) { williamr@4: if (__val) williamr@4: this->_M_getword(__pos) |= this->_S_maskbit(__pos); williamr@4: else williamr@4: this->_M_getword(__pos) &= ~ this->_S_maskbit(__pos); williamr@4: williamr@4: return *this; williamr@4: } williamr@4: williamr@4: bitset<_Nb>& _Unchecked_reset(size_t __pos) { williamr@4: this->_M_getword(__pos) &= ~ this->_S_maskbit(__pos); williamr@4: return *this; williamr@4: } williamr@4: williamr@4: bitset<_Nb>& _Unchecked_flip(size_t __pos) { williamr@4: this->_M_getword(__pos) ^= this->_S_maskbit(__pos); williamr@4: return *this; williamr@4: } williamr@4: williamr@4: bool _Unchecked_test(size_t __pos) const { williamr@4: return (this->_M_getword(__pos) & this->_S_maskbit(__pos)) != __STATIC_CAST(_WordT,0); williamr@4: } williamr@4: williamr@4: // Set, reset, and flip. williamr@4: williamr@4: bitset<_Nb>& set() { williamr@4: this->_M_do_set(); williamr@4: this->_M_do_sanitize(); williamr@4: return *this; williamr@4: } williamr@4: williamr@4: bitset<_Nb>& set(size_t __pos) { williamr@4: if (__pos >= _Nb) williamr@4: __stl_throw_out_of_range("bitset"); williamr@4: return _Unchecked_set(__pos); williamr@4: } williamr@4: williamr@4: bitset<_Nb>& set(size_t __pos, int __val) { williamr@4: if (__pos >= _Nb) williamr@4: __stl_throw_out_of_range("bitset"); williamr@4: return _Unchecked_set(__pos, __val); williamr@4: } williamr@4: williamr@4: bitset<_Nb>& reset() { williamr@4: this->_M_do_reset(); williamr@4: return *this; williamr@4: } williamr@4: williamr@4: bitset<_Nb>& reset(size_t __pos) { williamr@4: if (__pos >= _Nb) williamr@4: __stl_throw_out_of_range("bitset"); williamr@4: williamr@4: return _Unchecked_reset(__pos); williamr@4: } williamr@4: williamr@4: bitset<_Nb>& flip() { williamr@4: this->_M_do_flip(); williamr@4: this->_M_do_sanitize(); williamr@4: return *this; williamr@4: } williamr@4: williamr@4: bitset<_Nb>& flip(size_t __pos) { williamr@4: if (__pos >= _Nb) williamr@4: __stl_throw_out_of_range("bitset"); williamr@4: williamr@4: return _Unchecked_flip(__pos); williamr@4: } williamr@4: williamr@4: bitset<_Nb> operator~() const { williamr@4: return bitset<_Nb>(*this).flip(); williamr@4: } williamr@4: williamr@4: // element access: williamr@4: //for b[i]; williamr@4: reference operator[](size_t __pos) { return reference(*this,__pos); } williamr@4: bool operator[](size_t __pos) const { return _Unchecked_test(__pos); } williamr@4: williamr@4: unsigned long to_ulong() const { return this->_M_do_to_ulong(); } williamr@4: williamr@4: #if defined (_STLP_MEMBER_TEMPLATES) && ! defined (_STLP_NO_EXPLICIT_FUNCTION_TMPL_ARGS) williamr@4: template williamr@4: basic_string<_CharT, _Traits, _Alloc> to_string() const { williamr@4: basic_string<_CharT, _Traits, _Alloc> __result; williamr@4: _M_copy_to_string(__result); williamr@4: return __result; williamr@4: } williamr@4: #else williamr@4: string to_string() const { williamr@4: string __result; williamr@4: _M_copy_to_string(__result); williamr@4: return __result; williamr@4: } williamr@4: #endif /* _STLP_EXPLICIT_FUNCTION_TMPL_ARGS */ williamr@4: williamr@4: size_t count() const { return this->_M_do_count(); } williamr@4: williamr@4: size_t size() const { return _Nb; } williamr@4: williamr@4: bool operator==(const bitset<_Nb>& __rhs) const { williamr@4: return this->_M_is_equal(__rhs); williamr@4: } williamr@4: bool operator!=(const bitset<_Nb>& __rhs) const { williamr@4: return !this->_M_is_equal(__rhs); williamr@4: } williamr@4: williamr@4: bool test(size_t __pos) const { williamr@4: if (__pos >= _Nb) williamr@4: __stl_throw_out_of_range("bitset"); williamr@4: williamr@4: return _Unchecked_test(__pos); williamr@4: } williamr@4: williamr@4: bool any() const { return this->_M_is_any(); } williamr@4: bool none() const { return !this->_M_is_any(); } williamr@4: williamr@4: bitset<_Nb> operator<<(size_t __pos) const { williamr@4: bitset<_Nb> __result(*this); williamr@4: __result <<= __pos ; return __result; williamr@4: } williamr@4: bitset<_Nb> operator>>(size_t __pos) const { williamr@4: bitset<_Nb> __result(*this); williamr@4: __result >>= __pos ; return __result; williamr@4: } williamr@4: williamr@4: // williamr@4: // EXTENSIONS: bit-find operations. These operations are williamr@4: // experimental, and are subject to change or removal in future williamr@4: // versions. williamr@4: // williamr@4: williamr@4: // find the index of the first "on" bit williamr@4: size_t _Find_first() const williamr@4: { return this->_M_do_find_first(_Nb); } williamr@4: williamr@4: // find the index of the next "on" bit after prev williamr@4: size_t _Find_next( size_t __prev ) const williamr@4: { return this->_M_do_find_next(__prev, _Nb); } williamr@4: williamr@4: // williamr@4: // Definitions of should-be non-inline member functions. williamr@4: // williamr@4: # if defined (_STLP_MEMBER_TEMPLATES) williamr@4: template williamr@4: void _M_copy_from_string(const basic_string<_CharT,_Traits,_Alloc>& __s, williamr@4: size_t __pos, williamr@4: size_t __n) { williamr@4: #else williamr@4: void _M_copy_from_string(const string& __s, williamr@4: size_t __pos, williamr@4: size_t __n) { williamr@4: typedef char_traits _Traits; williamr@4: #endif williamr@4: reset(); williamr@4: size_t __tmp = _Nb; williamr@4: const size_t __Nbits = (min) (__tmp, (min) (__n, __s.size() - __pos)); williamr@4: for ( size_t __i= 0; __i < __Nbits; ++__i) { williamr@4: typename _Traits::int_type __k = _Traits::to_int_type(__s[__pos + __Nbits - __i - 1]); williamr@4: // boris : widen() ? williamr@4: if (__k == '1') williamr@4: set(__i); williamr@4: else if (__k !='0') williamr@4: __stl_throw_invalid_argument("bitset"); williamr@4: } williamr@4: } williamr@4: williamr@4: # if defined (_STLP_MEMBER_TEMPLATES) williamr@4: template williamr@4: void _M_copy_to_string(basic_string<_CharT, _Traits, _Alloc>& __s) const williamr@4: # else williamr@4: void _M_copy_to_string(string& __s) const williamr@4: # endif williamr@4: { williamr@4: __s.assign(_Nb, '0'); williamr@4: williamr@4: for (size_t __i = 0; __i < _Nb; ++__i) williamr@4: if (_Unchecked_test(__i)) williamr@4: __s[_Nb - 1 - __i] = '1'; williamr@4: } williamr@4: williamr@4: # if defined (_STLP_NON_TYPE_TMPL_PARAM_BUG) williamr@4: bitset<_Nb> operator&(const bitset<_Nb>& __y) const { williamr@4: bitset<_Nb> __result(*this); williamr@4: __result &= __y; williamr@4: return __result; williamr@4: } williamr@4: bitset<_Nb> operator|(const bitset<_Nb>& __y) const { williamr@4: bitset<_Nb> __result(*this); williamr@4: __result |= __y; williamr@4: return __result; williamr@4: } williamr@4: bitset<_Nb> operator^(const bitset<_Nb>& __y) const { williamr@4: bitset<_Nb> __result(*this); williamr@4: __result ^= __y; williamr@4: return __result; williamr@4: } williamr@4: # endif williamr@4: williamr@4: }; williamr@4: williamr@4: // ------------------------------------------------------------ williamr@4: // williamr@4: // 23.3.5.3 bitset operations: williamr@4: // williamr@4: williamr@4: # if ! defined (_STLP_NON_TYPE_TMPL_PARAM_BUG) williamr@4: williamr@4: template williamr@4: inline bitset<_Nb> _STLP_CALL williamr@4: operator&(const bitset<_Nb>& __x, williamr@4: const bitset<_Nb>& __y) { williamr@4: bitset<_Nb> __result(__x); williamr@4: __result &= __y; williamr@4: return __result; williamr@4: } williamr@4: williamr@4: williamr@4: template williamr@4: inline bitset<_Nb> _STLP_CALL williamr@4: operator|(const bitset<_Nb>& __x, williamr@4: const bitset<_Nb>& __y) { williamr@4: bitset<_Nb> __result(__x); williamr@4: __result |= __y; williamr@4: return __result; williamr@4: } williamr@4: williamr@4: template williamr@4: inline bitset<_Nb> _STLP_CALL williamr@4: operator^(const bitset<_Nb>& __x, williamr@4: const bitset<_Nb>& __y) { williamr@4: bitset<_Nb> __result(__x); williamr@4: __result ^= __y; williamr@4: return __result; williamr@4: } williamr@4: williamr@4: #if defined ( _STLP_USE_NEW_IOSTREAMS ) williamr@4: williamr@4: template williamr@4: basic_istream<_CharT, _Traits>& _STLP_CALL williamr@4: operator>>(basic_istream<_CharT, _Traits>& __is, bitset<_Nb>& __x); williamr@4: williamr@4: williamr@4: template williamr@4: basic_ostream<_CharT, _Traits>& _STLP_CALL williamr@4: operator<<(basic_ostream<_CharT, _Traits>& __os, const bitset<_Nb>& __x); williamr@4: williamr@4: #elif ! defined ( _STLP_USE_NO_IOSTREAMS ) williamr@4: williamr@4: // (reg) For Watcom IO, this tells if ostream class is in .exe or in .dll williamr@4: template williamr@4: _ISTREAM_DLL& _STLP_CALL williamr@4: operator>>(_ISTREAM_DLL& __is, bitset<_Nb>& __x); williamr@4: williamr@4: template williamr@4: inline _OSTREAM_DLL& _STLP_CALL operator<<(_OSTREAM_DLL& __os, const bitset<_Nb>& __x) { williamr@4: string __tmp; williamr@4: __x._M_copy_to_string(__tmp); williamr@4: return __os << __tmp; williamr@4: } williamr@4: williamr@4: #endif williamr@4: williamr@4: # endif /* _STLP_NON_TYPE_TMPL_PARAM_BUG */ williamr@4: williamr@4: # undef bitset williamr@4: williamr@4: williamr@4: _STLP_END_NAMESPACE williamr@4: williamr@4: # undef __BITS_PER_WORD williamr@4: # undef __BITSET_WORDS williamr@4: williamr@4: # if !defined (_STLP_LINK_TIME_INSTANTIATION) williamr@4: # include williamr@4: # endif williamr@4: williamr@4: #endif /* _STLP_BITSET_H */ williamr@4: williamr@4: williamr@4: // Local Variables: williamr@4: // mode:C++ williamr@4: // End: williamr@4: