_bitset.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /*
  2. * Copyright (c) 1998
  3. * Silicon Graphics Computer Systems, Inc.
  4. *
  5. * Copyright (c) 1999
  6. * Boris Fomitchev
  7. *
  8. * This material is provided "as is", with absolutely no warranty expressed
  9. * or implied. Any use is at your own risk.
  10. *
  11. * Permission to use or copy this software for any purpose is hereby granted
  12. * without fee, provided the above notices are retained on all copies.
  13. * Permission to modify the code and to distribute modified code is granted,
  14. * provided the above notices are retained, and a notice that the code was
  15. * modified is included with the above copyright notice.
  16. *
  17. */
  18. #ifndef _STLP_BITSET_C
  19. #define _STLP_BITSET_C
  20. #ifndef _STLP_BITSET_H
  21. # include <stl/_bitset.h>
  22. #endif
  23. #define __BITS_PER_WORD (CHAR_BIT * sizeof(unsigned long))
  24. _STLP_BEGIN_NAMESPACE
  25. _STLP_MOVE_TO_PRIV_NAMESPACE
  26. //
  27. // Definitions of non-inline functions from _Base_bitset.
  28. //
  29. template<size_t _Nw>
  30. void _Base_bitset<_Nw>::_M_do_left_shift(size_t __shift) {
  31. if (__shift != 0) {
  32. const size_t __wshift = __shift / __BITS_PER_WORD;
  33. const size_t __offset = __shift % __BITS_PER_WORD;
  34. if (__offset == 0)
  35. for (size_t __n = _Nw - 1; __n >= __wshift; --__n)
  36. _M_w[__n] = _M_w[__n - __wshift];
  37. else {
  38. const size_t __sub_offset = __BITS_PER_WORD - __offset;
  39. for (size_t __n = _Nw - 1; __n > __wshift; --__n)
  40. _M_w[__n] = (_M_w[__n - __wshift] << __offset) |
  41. (_M_w[__n - __wshift - 1] >> __sub_offset);
  42. _M_w[__wshift] = _M_w[0] << __offset;
  43. }
  44. fill(_M_w + 0, _M_w + __wshift, __STATIC_CAST(_WordT,0));
  45. }
  46. }
  47. template<size_t _Nw>
  48. void _Base_bitset<_Nw>::_M_do_right_shift(size_t __shift) {
  49. if (__shift != 0) {
  50. const size_t __wshift = __shift / __BITS_PER_WORD;
  51. const size_t __offset = __shift % __BITS_PER_WORD;
  52. const size_t __limit = _Nw - __wshift - 1;
  53. if (__offset == 0)
  54. for (size_t __n = 0; __n <= __limit; ++__n)
  55. _M_w[__n] = _M_w[__n + __wshift];
  56. else {
  57. const size_t __sub_offset = __BITS_PER_WORD - __offset;
  58. for (size_t __n = 0; __n < __limit; ++__n)
  59. _M_w[__n] = (_M_w[__n + __wshift] >> __offset) |
  60. (_M_w[__n + __wshift + 1] << __sub_offset);
  61. _M_w[__limit] = _M_w[_Nw-1] >> __offset;
  62. }
  63. fill(_M_w + __limit + 1, _M_w + _Nw, __STATIC_CAST(_WordT,0));
  64. }
  65. }
  66. template<size_t _Nw>
  67. unsigned long _Base_bitset<_Nw>::_M_do_to_ulong() const {
  68. for (size_t __i = 1; __i < _Nw; ++__i)
  69. if (_M_w[__i])
  70. __stl_throw_overflow_error("bitset");
  71. return _M_w[0];
  72. } // End _M_do_to_ulong
  73. template<size_t _Nw>
  74. size_t _Base_bitset<_Nw>::_M_do_find_first(size_t __not_found) const {
  75. for ( size_t __i = 0; __i < _Nw; __i++ ) {
  76. _WordT __thisword = _M_w[__i];
  77. if ( __thisword != __STATIC_CAST(_WordT,0) ) {
  78. // find byte within word
  79. for ( size_t __j = 0; __j < sizeof(_WordT); __j++ ) {
  80. unsigned char __this_byte
  81. = __STATIC_CAST(unsigned char,(__thisword & (~(unsigned char)0)));
  82. if ( __this_byte )
  83. return __i*__BITS_PER_WORD + __j*CHAR_BIT +
  84. _Bs_G::_S_first_one(__this_byte);
  85. __thisword >>= CHAR_BIT;
  86. }
  87. }
  88. }
  89. // not found, so return an indication of failure.
  90. return __not_found;
  91. }
  92. template<size_t _Nw>
  93. size_t
  94. _Base_bitset<_Nw>::_M_do_find_next(size_t __prev,
  95. size_t __not_found) const {
  96. // make bound inclusive
  97. ++__prev;
  98. // check out of bounds
  99. if ( __prev >= _Nw * __BITS_PER_WORD )
  100. return __not_found;
  101. // search first word
  102. size_t __i = _S_whichword(__prev);
  103. _WordT __thisword = _M_w[__i];
  104. // mask off bits below bound
  105. __thisword &= (~__STATIC_CAST(_WordT,0)) << _S_whichbit(__prev);
  106. if ( __thisword != __STATIC_CAST(_WordT,0) ) {
  107. // find byte within word
  108. // get first byte into place
  109. __thisword >>= _S_whichbyte(__prev) * CHAR_BIT;
  110. for ( size_t __j = _S_whichbyte(__prev); __j < sizeof(_WordT); ++__j ) {
  111. unsigned char __this_byte
  112. = __STATIC_CAST(unsigned char,(__thisword & (~(unsigned char)0)));
  113. if ( __this_byte )
  114. return __i*__BITS_PER_WORD + __j*CHAR_BIT +
  115. _Bs_G::_S_first_one(__this_byte);
  116. __thisword >>= CHAR_BIT;
  117. }
  118. }
  119. // check subsequent words
  120. ++__i;
  121. for ( ; __i < _Nw; ++__i ) {
  122. /* _WordT */ __thisword = _M_w[__i];
  123. if ( __thisword != __STATIC_CAST(_WordT,0) ) {
  124. // find byte within word
  125. for ( size_t __j = 0; __j < sizeof(_WordT); ++__j ) {
  126. unsigned char __this_byte
  127. = __STATIC_CAST(unsigned char,(__thisword & (~(unsigned char)0)));
  128. if ( __this_byte )
  129. return __i*__BITS_PER_WORD + __j*CHAR_BIT +
  130. _Bs_G::_S_first_one(__this_byte);
  131. __thisword >>= CHAR_BIT;
  132. }
  133. }
  134. }
  135. // not found, so return an indication of failure.
  136. return __not_found;
  137. } // end _M_do_find_next
  138. _STLP_MOVE_TO_STD_NAMESPACE
  139. #if !defined (_STLP_NON_TYPE_TMPL_PARAM_BUG)
  140. # if !defined (_STLP_USE_NO_IOSTREAMS)
  141. _STLP_END_NAMESPACE
  142. #ifndef _STLP_STRING_IO_H
  143. # include <stl/_string_io.h> //includes _istream.h and _ostream.h
  144. #endif
  145. _STLP_BEGIN_NAMESPACE
  146. template <class _CharT, class _Traits, size_t _Nb>
  147. basic_istream<_CharT, _Traits>& _STLP_CALL
  148. operator>>(basic_istream<_CharT, _Traits>& __is, bitset<_Nb>& __x) {
  149. basic_string<_CharT, _Traits> __tmp;
  150. __tmp.reserve(_Nb);
  151. // Skip whitespace
  152. typename basic_istream<_CharT, _Traits>::sentry __sentry(__is);
  153. if (__sentry) {
  154. basic_streambuf<_CharT, _Traits>* __buf = __is.rdbuf();
  155. for (size_t __i = 0; __i < _Nb; ++__i) {
  156. static typename _Traits::int_type __eof = _Traits::eof();
  157. typename _Traits::int_type __c1 = __buf->sbumpc();
  158. if (_Traits::eq_int_type(__c1, __eof)) {
  159. __is.setstate(ios_base::eofbit);
  160. break;
  161. }
  162. else {
  163. typename _Traits::char_type __c2 = _Traits::to_char_type(__c1);
  164. char __c = __is.narrow(__c2, '*');
  165. if (__c == '0' || __c == '1')
  166. __tmp.push_back(__c);
  167. else if (_Traits::eq_int_type(__buf->sputbackc(__c2), __eof)) {
  168. __is.setstate(ios_base::failbit);
  169. break;
  170. }
  171. }
  172. }
  173. if (__tmp.empty())
  174. __is.setstate(ios_base::failbit);
  175. else
  176. __x._M_copy_from_string(__tmp, __STATIC_CAST(size_t,0), _Nb);
  177. }
  178. return __is;
  179. }
  180. template <class _CharT, class _Traits, size_t _Nb>
  181. basic_ostream<_CharT, _Traits>& _STLP_CALL
  182. operator<<(basic_ostream<_CharT, _Traits>& __os,
  183. const bitset<_Nb>& __x) {
  184. basic_string<_CharT, _Traits> __tmp;
  185. __x._M_copy_to_string(__tmp);
  186. return __os << __tmp;
  187. }
  188. # endif /* !_STLP_USE_NO_IOSTREAMS */
  189. #endif /* _STLP_NON_TYPE_TMPL_PARAM_BUG */
  190. _STLP_END_NAMESPACE
  191. #undef __BITS_PER_WORD
  192. #undef bitset
  193. #endif /* _STLP_BITSET_C */