Ne.app NeANT
The Ne.app NeANT User module of the NeANT distribution.
bitset.cc
Go to the documentation of this file.
1
30
31
32#if 8 == _RWSTD_CHAR_BIT
33# define _RWSTD_LOG2_CHAR_BITS 3
34#elif 16 == _RWSTD_CHAR_BIT
35# define _RWSTD_LOG2_CHAR_BITS 4
36#elif 32 == _RWSTD_CHAR_BIT
37# define _RWSTD_LOG2_CHAR_BITS 5
38#elif 64 == _RWSTD_CHAR_BIT
39# define _RWSTD_LOG2_CHAR_BITS 6
40#endif
41
42#if 1 == _RWSTD_LONG_SIZE
43# define _RWSTD_LOG2_LONG_SIZE 0
44#elif 2 == _RWSTD_LONG_SIZE
45# define _RWSTD_LOG2_LONG_SIZE 1
46#elif 4 == _RWSTD_LONG_SIZE
47# define _RWSTD_LOG2_LONG_SIZE 2
48#elif 8 == _RWSTD_LONG_SIZE
49# define _RWSTD_LOG2_LONG_SIZE 3
50#elif 16 == _RWSTD_LONG_SIZE
51# define _RWSTD_LOG2_LONG_SIZE 4
52#endif
53
54
56template <class _CharT, class _Traits>
57void __rw_bitset (unsigned long *__bits, _RWSTD_SIZE_T __maxbits,
58 const _CharT *__str, _RWSTD_SIZE_T __slen,
59 const _Traits*, _CharT __b0, _CharT __b1,
60 _RWSTD_SIZE_T __pos, _RWSTD_SIZE_T __n,
61 const char *__file, const char *__fun)
62{
63 const _RWSTD_SIZE_T __wordbits = sizeof *__bits * _RWSTD_CHAR_BIT;
64
65 if (_RWSTD_SIZE_MAX == __slen)
66 __slen = _Traits::length (__str);
67
68 // verify that `pos' is a valid offset into `str'
69 _RWSTD_REQUIRES (__pos <= __slen,
70 (_RWSTD_ERROR_OUT_OF_RANGE, __file, __fun, __pos, __slen));
71
72 // compute the number of characters in `str' past the offset `pos'
73 // that will be checked (although not all of them may necessarily
74 // be used)
75 const _RWSTD_SIZE_T __nchars =
76 __n < (__slen - __pos) ? __n : __slen - __pos;
77
78 // compute the number of bits to initialize from `str'
79 const _RWSTD_SIZE_T __nbits = __nchars < __maxbits ? __nchars : __maxbits;
80
81 __str += __pos;
82
83 // compute the number of bytes occupied by `bits'
84#if defined (_RWSTD_LOG2_CHAR_BITS) && defined (_RWSTD_LOG2_LONG_SIZE)
85
86 const _RWSTD_SIZE_T __nbytes =
87 ((__maxbits >> (_RWSTD_LOG2_CHAR_BITS + _RWSTD_LOG2_LONG_SIZE)) +
88 (0 != (__maxbits & (__wordbits - 1)))) << _RWSTD_LOG2_LONG_SIZE;
89
90#else // #if !defined (_RWSTD_LOG2_CHAR_BITS) || !defined (_RWSTD_LOG2_LONG_SIZE)
91
92 const _RWSTD_SIZE_T __nbytes =
93 (__maxbits / __wordbits + (0 != __maxbits % __wordbits))
94 * sizeof (*__bits);
95
96#endif // _RWSTD_LOG2_CHAR_BITS && _RWSTD_LOG2_LONG_SIZE
97
98 _RWSTD_MEMSET (__bits, 0, __nbytes);
99
100 // set all bits but also check any extra characters as required
101 for (_RWSTD_SIZE_T __i = 0; __i != __nchars; ++__i) {
102
103 if (_Traits::eq (__b1, __str [__i])) {
104
105 const _RWSTD_SIZE_T __bitno = __nbits - __i - 1;
106
107 if (__i < __nbits)
108 __bits [__bitno / __wordbits] |= 1UL << __bitno % __wordbits;
109 }
110 else {
111 // verify that the character is valid
112 _RWSTD_REQUIRES (_Traits::eq (__b0, __str [__i]),
113 (_RWSTD_ERROR_INVALID_ARGUMENT, __file, __fun));
114 }
115 }
116}
117
118#undef _RWSTD_LOG2_CHAR_BITS
119#undef _RWSTD_LOG2_ULONG_SIZE
120
121
123template <_RWSTD_SIZE_T _Size, class _CharT, class _Traits>
124_STD::basic_istream<_CharT, _Traits>&
125__rw_extract_bitset (_STD::basic_istream<_CharT, _Traits> &__strm,
126 _STD::bitset<_Size> &__x)
127{
128 typedef _STD::basic_istream<_CharT, _Traits> _Istream;
129
130 typename _Istream::iostate __err (__strm.goodbit);
131
132
133 _STD::bitset<_Size> __tmp;
134
135 _RWSTD_SIZE_T __bit = _Size;
136
137 _TRY {
138
139 // sentry may throw an exception
140 const typename _Istream::sentry __ipfx (__strm);
141
142 if (__ipfx) {
143
144 // 23.3.5.3, p5 - extract at most _Size chars
145
146 // get next char without extracting
147 typename _Traits::int_type __c = __strm.rdbuf ()->sgetc ();
148
149 for ( ; __bit; --__bit) {
150
151 if (_Traits::eq_int_type (_Traits::eof (), __c)) {
152 __err = __strm.eofbit;
153 break;
154 }
155
156 // convert to char_type and narrow to char
157 const char __ch =
158 __strm.narrow (_Traits::to_char_type (__c), _CharT ());
159
160 if ('1' == __ch)
161 __tmp [__bit - 1] = 1;
162 else if ('0' != __ch)
163 break;
164
165 __c = __strm.rdbuf ()->snextc ();
166 }
167
168 // shift tmp right by the number of outstanding bits
169 __tmp >>= __bit;
170 }
171 }
172 _CATCH (...) {
173 __strm.setstate (__strm.badbit | _RW::__rw_rethrow);
174 }
175
176 if (_Size && _Size == __bit)
177 __err |= __strm.failbit;
178 else
179 __x = __tmp;
180
181 if (__err)
182 __strm.setstate (__err);
183
184 return __strm;
185}
186
187} // namespace __rw
#define _RWSTD_REQUIRES(pred, args)
Definition _defs.h:1132
#define _TRY
Definition _defs.h:850
#define _RWSTD_NAMESPACE(name)
Definition _defs.h:567
#define _EXPORT
Definition _defs.h:498
#define _CATCH(ex)
Definition _defs.h:851
#define _RWSTD_ERROR_INVALID_ARGUMENT
Definition _error.h:91
#define _RWSTD_ERROR_OUT_OF_RANGE
Definition _error.h:93
#define _RWSTD_MEMSET
Definition _traits.h:109