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