/home/ntakagi/work/STLport-5.1.5/stlport/stl/_bitset.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 1998
00003  * Silicon Graphics Computer Systems, Inc.
00004  *
00005  * Copyright (c) 1999
00006  * Boris Fomitchev
00007  *
00008  * This material is provided "as is", with absolutely no warranty expressed
00009  * or implied. Any use is at your own risk.
00010  *
00011  * Permission to use or copy this software for any purpose is hereby granted
00012  * without fee, provided the above notices are retained on all copies.
00013  * Permission to modify the code and to distribute modified code is granted,
00014  * provided the above notices are retained, and a notice that the code was
00015  * modified is included with the above copyright notice.
00016  *
00017  */
00018 
00019 #ifndef _STLP_BITSET_C
00020 #define _STLP_BITSET_C
00021 
00022 #ifndef _STLP_BITSET_H
00023 #  include <stl/_bitset.h>
00024 #endif
00025 
00026 #define __BITS_PER_WORD (CHAR_BIT * sizeof(unsigned long))
00027 
00028 _STLP_BEGIN_NAMESPACE
00029 
00030 _STLP_MOVE_TO_PRIV_NAMESPACE
00031 //
00032 // Definitions of non-inline functions from _Base_bitset.
00033 //
00034 template<size_t _Nw>
00035 void _Base_bitset<_Nw>::_M_do_left_shift(size_t __shift) {
00036   if (__shift != 0) {
00037     const size_t __wshift = __shift / __BITS_PER_WORD;
00038     const size_t __offset = __shift % __BITS_PER_WORD;
00039 
00040     if (__offset == 0)
00041       for (size_t __n = _Nw - 1; __n >= __wshift; --__n)
00042         _M_w[__n] = _M_w[__n - __wshift];
00043 
00044     else {
00045       const size_t __sub_offset = __BITS_PER_WORD - __offset;
00046       for (size_t __n = _Nw - 1; __n > __wshift; --__n)
00047         _M_w[__n] = (_M_w[__n - __wshift] << __offset) |
00048                     (_M_w[__n - __wshift - 1] >> __sub_offset);
00049       _M_w[__wshift] = _M_w[0] << __offset;
00050     }
00051 
00052     fill(_M_w + 0, _M_w + __wshift, __STATIC_CAST(_WordT,0));
00053   }
00054 }
00055 
00056 template<size_t _Nw>
00057 void _Base_bitset<_Nw>::_M_do_right_shift(size_t __shift) {
00058   if (__shift != 0) {
00059     const size_t __wshift = __shift / __BITS_PER_WORD;
00060     const size_t __offset = __shift % __BITS_PER_WORD;
00061     const size_t __limit = _Nw - __wshift - 1;
00062 
00063     if (__offset == 0)
00064       for (size_t __n = 0; __n <= __limit; ++__n)
00065         _M_w[__n] = _M_w[__n + __wshift];
00066 
00067     else {
00068       const size_t __sub_offset = __BITS_PER_WORD - __offset;
00069       for (size_t __n = 0; __n < __limit; ++__n)
00070         _M_w[__n] = (_M_w[__n + __wshift] >> __offset) |
00071                     (_M_w[__n + __wshift + 1] << __sub_offset);
00072       _M_w[__limit] = _M_w[_Nw-1] >> __offset;
00073     }
00074 
00075     fill(_M_w + __limit + 1, _M_w + _Nw, __STATIC_CAST(_WordT,0));
00076   }
00077 }
00078 
00079 template<size_t _Nw>
00080 unsigned long _Base_bitset<_Nw>::_M_do_to_ulong() const {
00081   for (size_t __i = 1; __i < _Nw; ++__i)
00082     if (_M_w[__i])
00083       __stl_throw_overflow_error("bitset");
00084   return _M_w[0];
00085 } // End _M_do_to_ulong
00086 
00087 template<size_t _Nw>
00088 size_t _Base_bitset<_Nw>::_M_do_find_first(size_t __not_found) const {
00089   for ( size_t __i = 0; __i < _Nw; __i++ ) {
00090     _WordT __thisword = _M_w[__i];
00091     if ( __thisword != __STATIC_CAST(_WordT,0) ) {
00092       // find byte within word
00093       for ( size_t __j = 0; __j < sizeof(_WordT); __j++ ) {
00094         unsigned char __this_byte
00095           = __STATIC_CAST(unsigned char,(__thisword & (~(unsigned char)0)));
00096         if ( __this_byte )
00097           return __i*__BITS_PER_WORD + __j*CHAR_BIT +
00098             _Bs_G::_S_first_one(__this_byte);
00099 
00100         __thisword >>= CHAR_BIT;
00101       }
00102     }
00103   }
00104   // not found, so return an indication of failure.
00105   return __not_found;
00106 }
00107 
00108 template<size_t _Nw>
00109 size_t
00110 _Base_bitset<_Nw>::_M_do_find_next(size_t __prev,
00111                                    size_t __not_found) const {
00112   // make bound inclusive
00113   ++__prev;
00114 
00115   // check out of bounds
00116   if ( __prev >= _Nw * __BITS_PER_WORD )
00117     return __not_found;
00118 
00119     // search first word
00120   size_t __i = _S_whichword(__prev);
00121   _WordT __thisword = _M_w[__i];
00122 
00123     // mask off bits below bound
00124   __thisword &= (~__STATIC_CAST(_WordT,0)) << _S_whichbit(__prev);
00125 
00126   if ( __thisword != __STATIC_CAST(_WordT,0) ) {
00127     // find byte within word
00128     // get first byte into place
00129     __thisword >>= _S_whichbyte(__prev) * CHAR_BIT;
00130     for ( size_t __j = _S_whichbyte(__prev); __j < sizeof(_WordT); ++__j ) {
00131       unsigned char __this_byte
00132         = __STATIC_CAST(unsigned char,(__thisword & (~(unsigned char)0)));
00133       if ( __this_byte )
00134         return __i*__BITS_PER_WORD + __j*CHAR_BIT +
00135           _Bs_G::_S_first_one(__this_byte);
00136 
00137       __thisword >>= CHAR_BIT;
00138     }
00139   }
00140 
00141   // check subsequent words
00142   ++__i;
00143   for ( ; __i < _Nw; ++__i ) {
00144     /* _WordT */ __thisword = _M_w[__i];
00145     if ( __thisword != __STATIC_CAST(_WordT,0) ) {
00146       // find byte within word
00147       for ( size_t __j = 0; __j < sizeof(_WordT); ++__j ) {
00148         unsigned char __this_byte
00149           = __STATIC_CAST(unsigned char,(__thisword & (~(unsigned char)0)));
00150         if ( __this_byte )
00151           return __i*__BITS_PER_WORD + __j*CHAR_BIT +
00152             _Bs_G::_S_first_one(__this_byte);
00153 
00154         __thisword >>= CHAR_BIT;
00155       }
00156     }
00157   }
00158 
00159   // not found, so return an indication of failure.
00160   return __not_found;
00161 } // end _M_do_find_next
00162 
00163 _STLP_MOVE_TO_STD_NAMESPACE
00164 
00165 #if !defined (_STLP_NON_TYPE_TMPL_PARAM_BUG)
00166 
00167 #  if !defined (_STLP_USE_NO_IOSTREAMS)
00168 
00169 _STLP_END_NAMESPACE
00170 
00171 #ifndef _STLP_STRING_IO_H
00172 #  include <stl/_string_io.h> //includes _istream.h and _ostream.h
00173 #endif
00174 
00175 _STLP_BEGIN_NAMESPACE
00176 
00177 template <class _CharT, class _Traits, size_t _Nb>
00178 basic_istream<_CharT, _Traits>& _STLP_CALL
00179 operator>>(basic_istream<_CharT, _Traits>& __is, bitset<_Nb>& __x) {
00180   basic_string<_CharT, _Traits> __tmp;
00181   __tmp.reserve(_Nb);
00182 
00183   // Skip whitespace
00184   typename basic_istream<_CharT, _Traits>::sentry __sentry(__is);
00185   if (__sentry) {
00186     basic_streambuf<_CharT, _Traits>* __buf = __is.rdbuf();
00187     for (size_t __i = 0; __i < _Nb; ++__i) {
00188       static typename _Traits::int_type __eof = _Traits::eof();
00189 
00190       typename _Traits::int_type __c1 = __buf->sbumpc();
00191       if (_Traits::eq_int_type(__c1, __eof)) {
00192         __is.setstate(ios_base::eofbit);
00193         break;
00194       }
00195       else {
00196         typename _Traits::char_type __c2 = _Traits::to_char_type(__c1);
00197         char __c = __is.narrow(__c2, '*');
00198 
00199         if (__c == '0' || __c == '1')
00200           __tmp.push_back(__c);
00201         else if (_Traits::eq_int_type(__buf->sputbackc(__c2), __eof)) {
00202           __is.setstate(ios_base::failbit);
00203           break;
00204         }
00205       }
00206     }
00207 
00208     if (__tmp.empty())
00209       __is.setstate(ios_base::failbit);
00210     else
00211       __x._M_copy_from_string(__tmp, __STATIC_CAST(size_t,0), _Nb);
00212   }
00213 
00214   return __is;
00215 }
00216 
00217 template <class _CharT, class _Traits, size_t _Nb>
00218 basic_ostream<_CharT, _Traits>& _STLP_CALL
00219 operator<<(basic_ostream<_CharT, _Traits>& __os,
00220            const bitset<_Nb>& __x) {
00221   basic_string<_CharT, _Traits> __tmp;
00222   __x._M_copy_to_string(__tmp);
00223   return __os << __tmp;
00224 }
00225 
00226 #  endif /* !_STLP_USE_NO_IOSTREAMS */
00227 
00228 #endif /* _STLP_NON_TYPE_TMPL_PARAM_BUG */
00229 
00230 _STLP_END_NAMESPACE
00231 
00232 #undef __BITS_PER_WORD
00233 #undef bitset
00234 
00235 #endif /*  _STLP_BITSET_C */



Generated on Mon Mar 10 15:32:19 2008 by  doxygen 1.5.1