]>
Commit | Line | Data |
---|---|---|
54c1bf78 | 1 | // <bitset> -*- C++ -*- |
de96ac46 | 2 | |
8fecd28c PC |
3 | // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006 |
4 | // Free Software Foundation, Inc. | |
de96ac46 BK |
5 | // |
6 | // This file is part of the GNU ISO C++ Library. This library is free | |
7 | // software; you can redistribute it and/or modify it under the | |
8 | // terms of the GNU General Public License as published by the | |
9 | // Free Software Foundation; either version 2, or (at your option) | |
10 | // any later version. | |
11 | ||
12 | // This library is distributed in the hope that it will be useful, | |
13 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | // GNU General Public License for more details. | |
16 | ||
17 | // You should have received a copy of the GNU General Public License along | |
18 | // with this library; see the file COPYING. If not, write to the Free | |
83f51799 | 19 | // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, |
de96ac46 BK |
20 | // USA. |
21 | ||
22 | // As a special exception, you may use this file as part of a free software | |
23 | // library without restriction. Specifically, if other files instantiate | |
24 | // templates or use macros or inline functions from this file, or you compile | |
25 | // this file and link it with other files to produce an executable, this | |
26 | // file does not by itself cause the resulting executable to be covered by | |
27 | // the GNU General Public License. This exception does not however | |
28 | // invalidate any other reasons why the executable file might be covered by | |
29 | // the GNU General Public License. | |
30 | ||
54c1bf78 BK |
31 | /* |
32 | * Copyright (c) 1998 | |
33 | * Silicon Graphics Computer Systems, Inc. | |
34 | * | |
35 | * Permission to use, copy, modify, distribute and sell this software | |
36 | * and its documentation for any purpose is hereby granted without fee, | |
37 | * provided that the above copyright notice appear in all copies and | |
38 | * that both that copyright notice and this permission notice appear | |
39 | * in supporting documentation. Silicon Graphics makes no | |
40 | * representations about the suitability of this software for any | |
41 | * purpose. It is provided "as is" without express or implied warranty. | |
b963aad8 | 42 | */ |
54c1bf78 | 43 | |
78a53887 | 44 | /** @file |
0aa06b18 | 45 | * This is a Standard C++ Library header. |
2f9d51b8 PE |
46 | */ |
47 | ||
1143680e SE |
48 | #ifndef _GLIBCXX_BITSET |
49 | #define _GLIBCXX_BITSET 1 | |
54c1bf78 BK |
50 | |
51 | #pragma GCC system_header | |
52 | ||
285b36d6 BK |
53 | #include <cstddef> // For size_t |
54 | #include <cstring> // For memset | |
55 | #include <limits> // For numeric_limits | |
54c1bf78 | 56 | #include <string> |
285b36d6 | 57 | #include <bits/functexcept.h> // For invalid_argument, out_of_range, |
b963aad8 | 58 | // overflow_error |
285b36d6 BK |
59 | #include <ostream> // For ostream (operator<<) |
60 | #include <istream> // For istream (operator>>) | |
54c1bf78 | 61 | |
3d7c150e BK |
62 | #define _GLIBCXX_BITSET_BITS_PER_WORD numeric_limits<unsigned long>::digits |
63 | #define _GLIBCXX_BITSET_WORDS(__n) \ | |
5c33bb62 PC |
64 | ((__n) < 1 ? 0 : ((__n) + _GLIBCXX_BITSET_BITS_PER_WORD - 1) \ |
65 | / _GLIBCXX_BITSET_BITS_PER_WORD) | |
54c1bf78 | 66 | |
3cbc7af0 BK |
67 | _GLIBCXX_BEGIN_NESTED_NAMESPACE(std, _GLIBCXX_STD) |
68 | ||
b963aad8 PE |
69 | /** |
70 | * @if maint | |
71 | * Base class, general case. It is a class inveriant that _Nw will be | |
72 | * nonnegative. | |
73 | * | |
74 | * See documentation for bitset. | |
75 | * @endif | |
76 | */ | |
1cb7f91f | 77 | template<size_t _Nw> |
b963aad8 | 78 | struct _Base_bitset |
1cb7f91f BK |
79 | { |
80 | typedef unsigned long _WordT; | |
81 | ||
b963aad8 PE |
82 | /// 0 is the least significant word. |
83 | _WordT _M_w[_Nw]; | |
84 | ||
5c33bb62 PC |
85 | _Base_bitset() |
86 | { _M_do_reset(); } | |
87 | ||
b963aad8 | 88 | _Base_bitset(unsigned long __val) |
1cb7f91f BK |
89 | { |
90 | _M_do_reset(); | |
91 | _M_w[0] = __val; | |
92 | } | |
54c1bf78 | 93 | |
b963aad8 | 94 | static size_t |
1cb7f91f | 95 | _S_whichword(size_t __pos ) |
3d7c150e | 96 | { return __pos / _GLIBCXX_BITSET_BITS_PER_WORD; } |
54c1bf78 | 97 | |
b963aad8 | 98 | static size_t |
1cb7f91f | 99 | _S_whichbyte(size_t __pos ) |
3d7c150e | 100 | { return (__pos % _GLIBCXX_BITSET_BITS_PER_WORD) / __CHAR_BIT__; } |
54c1bf78 | 101 | |
b963aad8 | 102 | static size_t |
1cb7f91f | 103 | _S_whichbit(size_t __pos ) |
3d7c150e | 104 | { return __pos % _GLIBCXX_BITSET_BITS_PER_WORD; } |
54c1bf78 | 105 | |
b963aad8 | 106 | static _WordT |
1cb7f91f BK |
107 | _S_maskbit(size_t __pos ) |
108 | { return (static_cast<_WordT>(1)) << _S_whichbit(__pos); } | |
54c1bf78 | 109 | |
b963aad8 PE |
110 | _WordT& |
111 | _M_getword(size_t __pos) | |
1cb7f91f | 112 | { return _M_w[_S_whichword(__pos)]; } |
54c1bf78 | 113 | |
b963aad8 PE |
114 | _WordT |
115 | _M_getword(size_t __pos) const | |
1cb7f91f | 116 | { return _M_w[_S_whichword(__pos)]; } |
54c1bf78 | 117 | |
b963aad8 | 118 | _WordT& |
5c33bb62 PC |
119 | _M_hiword() |
120 | { return _M_w[_Nw - 1]; } | |
54c1bf78 | 121 | |
b963aad8 | 122 | _WordT |
5c33bb62 PC |
123 | _M_hiword() const |
124 | { return _M_w[_Nw - 1]; } | |
54c1bf78 | 125 | |
b963aad8 PE |
126 | void |
127 | _M_do_and(const _Base_bitset<_Nw>& __x) | |
1cb7f91f | 128 | { |
b963aad8 | 129 | for (size_t __i = 0; __i < _Nw; __i++) |
1cb7f91f BK |
130 | _M_w[__i] &= __x._M_w[__i]; |
131 | } | |
54c1bf78 | 132 | |
b963aad8 PE |
133 | void |
134 | _M_do_or(const _Base_bitset<_Nw>& __x) | |
1cb7f91f | 135 | { |
b963aad8 | 136 | for (size_t __i = 0; __i < _Nw; __i++) |
1cb7f91f BK |
137 | _M_w[__i] |= __x._M_w[__i]; |
138 | } | |
54c1bf78 | 139 | |
b963aad8 PE |
140 | void |
141 | _M_do_xor(const _Base_bitset<_Nw>& __x) | |
1cb7f91f BK |
142 | { |
143 | for (size_t __i = 0; __i < _Nw; __i++) | |
144 | _M_w[__i] ^= __x._M_w[__i]; | |
145 | } | |
54c1bf78 | 146 | |
b963aad8 | 147 | void |
1cb7f91f BK |
148 | _M_do_left_shift(size_t __shift); |
149 | ||
b963aad8 | 150 | void |
1cb7f91f | 151 | _M_do_right_shift(size_t __shift); |
b963aad8 PE |
152 | |
153 | void | |
154 | _M_do_flip() | |
1cb7f91f | 155 | { |
b963aad8 | 156 | for (size_t __i = 0; __i < _Nw; __i++) |
1cb7f91f BK |
157 | _M_w[__i] = ~_M_w[__i]; |
158 | } | |
54c1bf78 | 159 | |
b963aad8 PE |
160 | void |
161 | _M_do_set() | |
1cb7f91f BK |
162 | { |
163 | for (size_t __i = 0; __i < _Nw; __i++) | |
164 | _M_w[__i] = ~static_cast<_WordT>(0); | |
165 | } | |
54c1bf78 | 166 | |
b963aad8 | 167 | void |
5c33bb62 PC |
168 | _M_do_reset() |
169 | { std::memset(_M_w, 0, _Nw * sizeof(_WordT)); } | |
1cb7f91f | 170 | |
b963aad8 PE |
171 | bool |
172 | _M_is_equal(const _Base_bitset<_Nw>& __x) const | |
1cb7f91f | 173 | { |
b963aad8 | 174 | for (size_t __i = 0; __i < _Nw; ++__i) |
1cb7f91f BK |
175 | { |
176 | if (_M_w[__i] != __x._M_w[__i]) | |
177 | return false; | |
178 | } | |
179 | return true; | |
180 | } | |
54c1bf78 | 181 | |
b963aad8 PE |
182 | bool |
183 | _M_is_any() const | |
1cb7f91f | 184 | { |
b963aad8 | 185 | for (size_t __i = 0; __i < _Nw; __i++) |
1cb7f91f BK |
186 | { |
187 | if (_M_w[__i] != static_cast<_WordT>(0)) | |
188 | return true; | |
189 | } | |
190 | return false; | |
191 | } | |
54c1bf78 | 192 | |
b963aad8 PE |
193 | size_t |
194 | _M_do_count() const | |
1cb7f91f BK |
195 | { |
196 | size_t __result = 0; | |
348b0c31 FH |
197 | for (size_t __i = 0; __i < _Nw; __i++) |
198 | __result += __builtin_popcountl(_M_w[__i]); | |
1cb7f91f BK |
199 | return __result; |
200 | } | |
54c1bf78 | 201 | |
b963aad8 PE |
202 | unsigned long |
203 | _M_do_to_ulong() const; | |
1cb7f91f BK |
204 | |
205 | // find first "on" bit | |
b963aad8 | 206 | size_t |
1cb7f91f BK |
207 | _M_do_find_first(size_t __not_found) const; |
208 | ||
209 | // find the next "on" bit that follows "prev" | |
b963aad8 | 210 | size_t |
1cb7f91f BK |
211 | _M_do_find_next(size_t __prev, size_t __not_found) const; |
212 | }; | |
213 | ||
214 | // Definitions of non-inline functions from _Base_bitset. | |
215 | template<size_t _Nw> | |
b963aad8 PE |
216 | void |
217 | _Base_bitset<_Nw>::_M_do_left_shift(size_t __shift) | |
1cb7f91f | 218 | { |
3bbfb3d9 | 219 | if (__builtin_expect(__shift != 0, 1)) |
1cb7f91f | 220 | { |
3d7c150e BK |
221 | const size_t __wshift = __shift / _GLIBCXX_BITSET_BITS_PER_WORD; |
222 | const size_t __offset = __shift % _GLIBCXX_BITSET_BITS_PER_WORD; | |
b963aad8 | 223 | |
1cb7f91f BK |
224 | if (__offset == 0) |
225 | for (size_t __n = _Nw - 1; __n >= __wshift; --__n) | |
226 | _M_w[__n] = _M_w[__n - __wshift]; | |
b963aad8 | 227 | else |
1cb7f91f | 228 | { |
5c33bb62 PC |
229 | const size_t __sub_offset = (_GLIBCXX_BITSET_BITS_PER_WORD |
230 | - __offset); | |
1cb7f91f | 231 | for (size_t __n = _Nw - 1; __n > __wshift; --__n) |
5c33bb62 PC |
232 | _M_w[__n] = ((_M_w[__n - __wshift] << __offset) |
233 | | (_M_w[__n - __wshift - 1] >> __sub_offset)); | |
1cb7f91f BK |
234 | _M_w[__wshift] = _M_w[0] << __offset; |
235 | } | |
b963aad8 | 236 | |
a8cad3e1 | 237 | std::fill(_M_w + 0, _M_w + __wshift, static_cast<_WordT>(0)); |
1cb7f91f | 238 | } |
54c1bf78 | 239 | } |
b963aad8 | 240 | |
1cb7f91f | 241 | template<size_t _Nw> |
b963aad8 PE |
242 | void |
243 | _Base_bitset<_Nw>::_M_do_right_shift(size_t __shift) | |
1cb7f91f | 244 | { |
3bbfb3d9 | 245 | if (__builtin_expect(__shift != 0, 1)) |
1cb7f91f | 246 | { |
3d7c150e BK |
247 | const size_t __wshift = __shift / _GLIBCXX_BITSET_BITS_PER_WORD; |
248 | const size_t __offset = __shift % _GLIBCXX_BITSET_BITS_PER_WORD; | |
1cb7f91f | 249 | const size_t __limit = _Nw - __wshift - 1; |
b963aad8 | 250 | |
1cb7f91f BK |
251 | if (__offset == 0) |
252 | for (size_t __n = 0; __n <= __limit; ++__n) | |
253 | _M_w[__n] = _M_w[__n + __wshift]; | |
b963aad8 | 254 | else |
1cb7f91f | 255 | { |
5c33bb62 PC |
256 | const size_t __sub_offset = (_GLIBCXX_BITSET_BITS_PER_WORD |
257 | - __offset); | |
1cb7f91f | 258 | for (size_t __n = 0; __n < __limit; ++__n) |
5c33bb62 PC |
259 | _M_w[__n] = ((_M_w[__n + __wshift] >> __offset) |
260 | | (_M_w[__n + __wshift + 1] << __sub_offset)); | |
1cb7f91f BK |
261 | _M_w[__limit] = _M_w[_Nw-1] >> __offset; |
262 | } | |
a8cad3e1 PC |
263 | |
264 | std::fill(_M_w + __limit + 1, _M_w + _Nw, static_cast<_WordT>(0)); | |
1cb7f91f | 265 | } |
54c1bf78 | 266 | } |
54c1bf78 | 267 | |
1cb7f91f | 268 | template<size_t _Nw> |
b963aad8 | 269 | unsigned long |
1cb7f91f BK |
270 | _Base_bitset<_Nw>::_M_do_to_ulong() const |
271 | { | |
b963aad8 PE |
272 | for (size_t __i = 1; __i < _Nw; ++__i) |
273 | if (_M_w[__i]) | |
988ad90d | 274 | __throw_overflow_error(__N("_Base_bitset::_M_do_to_ulong")); |
1cb7f91f | 275 | return _M_w[0]; |
54c1bf78 | 276 | } |
54c1bf78 | 277 | |
1cb7f91f | 278 | template<size_t _Nw> |
b963aad8 PE |
279 | size_t |
280 | _Base_bitset<_Nw>::_M_do_find_first(size_t __not_found) const | |
1cb7f91f | 281 | { |
348b0c31 | 282 | for (size_t __i = 0; __i < _Nw; __i++) |
1cb7f91f BK |
283 | { |
284 | _WordT __thisword = _M_w[__i]; | |
348b0c31 | 285 | if (__thisword != static_cast<_WordT>(0)) |
5c33bb62 PC |
286 | return (__i * _GLIBCXX_BITSET_BITS_PER_WORD |
287 | + __builtin_ctzl(__thisword)); | |
1cb7f91f BK |
288 | } |
289 | // not found, so return an indication of failure. | |
290 | return __not_found; | |
54c1bf78 BK |
291 | } |
292 | ||
1cb7f91f BK |
293 | template<size_t _Nw> |
294 | size_t | |
295 | _Base_bitset<_Nw>::_M_do_find_next(size_t __prev, size_t __not_found) const | |
b963aad8 | 296 | { |
1cb7f91f BK |
297 | // make bound inclusive |
298 | ++__prev; | |
b963aad8 | 299 | |
1cb7f91f | 300 | // check out of bounds |
394ef95e | 301 | if (__prev >= _Nw * _GLIBCXX_BITSET_BITS_PER_WORD) |
1cb7f91f | 302 | return __not_found; |
b963aad8 | 303 | |
1cb7f91f BK |
304 | // search first word |
305 | size_t __i = _S_whichword(__prev); | |
306 | _WordT __thisword = _M_w[__i]; | |
b963aad8 | 307 | |
1cb7f91f | 308 | // mask off bits below bound |
394ef95e | 309 | __thisword &= (~static_cast<_WordT>(0)) << _S_whichbit(__prev); |
b963aad8 | 310 | |
348b0c31 | 311 | if (__thisword != static_cast<_WordT>(0)) |
5c33bb62 PC |
312 | return (__i * _GLIBCXX_BITSET_BITS_PER_WORD |
313 | + __builtin_ctzl(__thisword)); | |
b963aad8 | 314 | |
1cb7f91f BK |
315 | // check subsequent words |
316 | __i++; | |
5c33bb62 | 317 | for (; __i < _Nw; __i++) |
1cb7f91f BK |
318 | { |
319 | __thisword = _M_w[__i]; | |
348b0c31 | 320 | if (__thisword != static_cast<_WordT>(0)) |
5c33bb62 PC |
321 | return (__i * _GLIBCXX_BITSET_BITS_PER_WORD |
322 | + __builtin_ctzl(__thisword)); | |
1cb7f91f BK |
323 | } |
324 | // not found, so return an indication of failure. | |
325 | return __not_found; | |
326 | } // end _M_do_find_next | |
54c1bf78 | 327 | |
b963aad8 PE |
328 | /** |
329 | * @if maint | |
330 | * Base class, specialization for a single word. | |
331 | * | |
332 | * See documentation for bitset. | |
333 | * @endif | |
334 | */ | |
335 | template<> | |
336 | struct _Base_bitset<1> | |
1cb7f91f BK |
337 | { |
338 | typedef unsigned long _WordT; | |
339 | _WordT _M_w; | |
54c1bf78 | 340 | |
5c33bb62 PC |
341 | _Base_bitset(void) |
342 | : _M_w(0) | |
5a4db26d | 343 | { } |
5c33bb62 PC |
344 | |
345 | _Base_bitset(unsigned long __val) | |
346 | : _M_w(__val) | |
5a4db26d | 347 | { } |
54c1bf78 | 348 | |
b963aad8 | 349 | static size_t |
1cb7f91f | 350 | _S_whichword(size_t __pos ) |
3d7c150e | 351 | { return __pos / _GLIBCXX_BITSET_BITS_PER_WORD; } |
54c1bf78 | 352 | |
b963aad8 | 353 | static size_t |
1cb7f91f | 354 | _S_whichbyte(size_t __pos ) |
3d7c150e | 355 | { return (__pos % _GLIBCXX_BITSET_BITS_PER_WORD) / __CHAR_BIT__; } |
54c1bf78 | 356 | |
b963aad8 | 357 | static size_t |
1cb7f91f | 358 | _S_whichbit(size_t __pos ) |
3d7c150e | 359 | { return __pos % _GLIBCXX_BITSET_BITS_PER_WORD; } |
54c1bf78 | 360 | |
b963aad8 | 361 | static _WordT |
1cb7f91f BK |
362 | _S_maskbit(size_t __pos ) |
363 | { return (static_cast<_WordT>(1)) << _S_whichbit(__pos); } | |
54c1bf78 | 364 | |
b963aad8 | 365 | _WordT& |
5c33bb62 PC |
366 | _M_getword(size_t) |
367 | { return _M_w; } | |
54c1bf78 | 368 | |
b963aad8 | 369 | _WordT |
5c33bb62 PC |
370 | _M_getword(size_t) const |
371 | { return _M_w; } | |
54c1bf78 | 372 | |
b963aad8 | 373 | _WordT& |
5c33bb62 PC |
374 | _M_hiword() |
375 | { return _M_w; } | |
54c1bf78 | 376 | |
b963aad8 | 377 | _WordT |
5c33bb62 PC |
378 | _M_hiword() const |
379 | { return _M_w; } | |
54c1bf78 | 380 | |
b963aad8 | 381 | void |
5c33bb62 PC |
382 | _M_do_and(const _Base_bitset<1>& __x) |
383 | { _M_w &= __x._M_w; } | |
54c1bf78 | 384 | |
b963aad8 | 385 | void |
5c33bb62 PC |
386 | _M_do_or(const _Base_bitset<1>& __x) |
387 | { _M_w |= __x._M_w; } | |
54c1bf78 | 388 | |
b963aad8 | 389 | void |
5c33bb62 PC |
390 | _M_do_xor(const _Base_bitset<1>& __x) |
391 | { _M_w ^= __x._M_w; } | |
54c1bf78 | 392 | |
b963aad8 | 393 | void |
5c33bb62 PC |
394 | _M_do_left_shift(size_t __shift) |
395 | { _M_w <<= __shift; } | |
54c1bf78 | 396 | |
b963aad8 | 397 | void |
5c33bb62 PC |
398 | _M_do_right_shift(size_t __shift) |
399 | { _M_w >>= __shift; } | |
54c1bf78 | 400 | |
b963aad8 | 401 | void |
5c33bb62 PC |
402 | _M_do_flip() |
403 | { _M_w = ~_M_w; } | |
54c1bf78 | 404 | |
b963aad8 | 405 | void |
5c33bb62 PC |
406 | _M_do_set() |
407 | { _M_w = ~static_cast<_WordT>(0); } | |
54c1bf78 | 408 | |
b963aad8 | 409 | void |
5c33bb62 PC |
410 | _M_do_reset() |
411 | { _M_w = 0; } | |
54c1bf78 | 412 | |
b963aad8 | 413 | bool |
1cb7f91f BK |
414 | _M_is_equal(const _Base_bitset<1>& __x) const |
415 | { return _M_w == __x._M_w; } | |
54c1bf78 | 416 | |
b963aad8 | 417 | bool |
5c33bb62 PC |
418 | _M_is_any() const |
419 | { return _M_w != 0; } | |
54c1bf78 | 420 | |
b963aad8 | 421 | size_t |
5c33bb62 PC |
422 | _M_do_count() const |
423 | { return __builtin_popcountl(_M_w); } | |
54c1bf78 | 424 | |
b963aad8 | 425 | unsigned long |
5c33bb62 PC |
426 | _M_do_to_ulong() const |
427 | { return _M_w; } | |
1cb7f91f | 428 | |
b963aad8 | 429 | size_t |
348b0c31 FH |
430 | _M_do_find_first(size_t __not_found) const |
431 | { | |
432 | if (_M_w != 0) | |
433 | return __builtin_ctzl(_M_w); | |
434 | else | |
435 | return __not_found; | |
436 | } | |
1cb7f91f BK |
437 | |
438 | // find the next "on" bit that follows "prev" | |
439 | size_t | |
348b0c31 FH |
440 | _M_do_find_next(size_t __prev, size_t __not_found) const |
441 | { | |
442 | ++__prev; | |
3d7c150e | 443 | if (__prev >= ((size_t) _GLIBCXX_BITSET_BITS_PER_WORD)) |
348b0c31 FH |
444 | return __not_found; |
445 | ||
446 | _WordT __x = _M_w >> __prev; | |
447 | if (__x != 0) | |
448 | return __builtin_ctzl(__x) + __prev; | |
449 | else | |
450 | return __not_found; | |
451 | } | |
1cb7f91f BK |
452 | }; |
453 | ||
bb12c809 PE |
454 | /** |
455 | * @if maint | |
456 | * Base class, specialization for no storage (zero-length %bitset). | |
457 | * | |
458 | * See documentation for bitset. | |
459 | * @endif | |
460 | */ | |
461 | template<> | |
462 | struct _Base_bitset<0> | |
463 | { | |
464 | typedef unsigned long _WordT; | |
465 | ||
5c33bb62 | 466 | _Base_bitset() |
5a4db26d | 467 | { } |
5c33bb62 PC |
468 | |
469 | _Base_bitset(unsigned long) | |
5a4db26d | 470 | { } |
bb12c809 PE |
471 | |
472 | static size_t | |
473 | _S_whichword(size_t __pos ) | |
3d7c150e | 474 | { return __pos / _GLIBCXX_BITSET_BITS_PER_WORD; } |
bb12c809 PE |
475 | |
476 | static size_t | |
477 | _S_whichbyte(size_t __pos ) | |
3d7c150e | 478 | { return (__pos % _GLIBCXX_BITSET_BITS_PER_WORD) / __CHAR_BIT__; } |
bb12c809 PE |
479 | |
480 | static size_t | |
481 | _S_whichbit(size_t __pos ) | |
3d7c150e | 482 | { return __pos % _GLIBCXX_BITSET_BITS_PER_WORD; } |
bb12c809 PE |
483 | |
484 | static _WordT | |
485 | _S_maskbit(size_t __pos ) | |
486 | { return (static_cast<_WordT>(1)) << _S_whichbit(__pos); } | |
487 | ||
488 | // This would normally give access to the data. The bounds-checking | |
489 | // in the bitset class will prevent the user from getting this far, | |
490 | // but (1) it must still return an lvalue to compile, and (2) the | |
491 | // user might call _Unchecked_set directly, in which case this /needs/ | |
492 | // to fail. Let's not penalize zero-length users unless they actually | |
493 | // make an unchecked call; all the memory ugliness is therefore | |
494 | // localized to this single should-never-get-this-far function. | |
495 | _WordT& | |
496 | _M_getword(size_t) const | |
988ad90d BK |
497 | { |
498 | __throw_out_of_range(__N("_Base_bitset::_M_getword")); | |
499 | return *new _WordT; | |
500 | } | |
bb12c809 PE |
501 | |
502 | _WordT | |
5c33bb62 PC |
503 | _M_hiword() const |
504 | { return 0; } | |
bb12c809 PE |
505 | |
506 | void | |
5c33bb62 | 507 | _M_do_and(const _Base_bitset<0>&) |
5a4db26d | 508 | { } |
bb12c809 PE |
509 | |
510 | void | |
5c33bb62 | 511 | _M_do_or(const _Base_bitset<0>&) |
5a4db26d | 512 | { } |
bb12c809 PE |
513 | |
514 | void | |
5c33bb62 | 515 | _M_do_xor(const _Base_bitset<0>&) |
5a4db26d | 516 | { } |
bb12c809 PE |
517 | |
518 | void | |
5c33bb62 | 519 | _M_do_left_shift(size_t) |
5a4db26d | 520 | { } |
bb12c809 PE |
521 | |
522 | void | |
5c33bb62 | 523 | _M_do_right_shift(size_t) |
5a4db26d | 524 | { } |
bb12c809 PE |
525 | |
526 | void | |
5c33bb62 | 527 | _M_do_flip() |
5a4db26d | 528 | { } |
bb12c809 PE |
529 | |
530 | void | |
5c33bb62 | 531 | _M_do_set() |
5a4db26d | 532 | { } |
bb12c809 PE |
533 | |
534 | void | |
5c33bb62 | 535 | _M_do_reset() |
5a4db26d | 536 | { } |
bb12c809 PE |
537 | |
538 | // Are all empty bitsets equal to each other? Are they equal to | |
539 | // themselves? How to compare a thing which has no state? What is | |
540 | // the sound of one zero-length bitset clapping? | |
541 | bool | |
5c33bb62 PC |
542 | _M_is_equal(const _Base_bitset<0>&) const |
543 | { return true; } | |
bb12c809 PE |
544 | |
545 | bool | |
5c33bb62 PC |
546 | _M_is_any() const |
547 | { return false; } | |
bb12c809 PE |
548 | |
549 | size_t | |
5c33bb62 PC |
550 | _M_do_count() const |
551 | { return 0; } | |
bb12c809 PE |
552 | |
553 | unsigned long | |
5c33bb62 PC |
554 | _M_do_to_ulong() const |
555 | { return 0; } | |
bb12c809 PE |
556 | |
557 | // Normally "not found" is the size, but that could also be | |
558 | // misinterpreted as an index in this corner case. Oh well. | |
559 | size_t | |
5c33bb62 PC |
560 | _M_do_find_first(size_t) const |
561 | { return 0; } | |
bb12c809 PE |
562 | |
563 | size_t | |
5c33bb62 PC |
564 | _M_do_find_next(size_t, size_t) const |
565 | { return 0; } | |
bb12c809 PE |
566 | }; |
567 | ||
568 | ||
1cb7f91f | 569 | // Helper class to zero out the unused high-order bits in the highest word. |
b963aad8 PE |
570 | template<size_t _Extrabits> |
571 | struct _Sanitize | |
1cb7f91f BK |
572 | { |
573 | static void _S_do_sanitize(unsigned long& __val) | |
574 | { __val &= ~((~static_cast<unsigned long>(0)) << _Extrabits); } | |
575 | }; | |
576 | ||
b963aad8 PE |
577 | template<> |
578 | struct _Sanitize<0> | |
5c33bb62 | 579 | { static void _S_do_sanitize(unsigned long) {} }; |
70e12fb9 | 580 | |
b963aad8 PE |
581 | /** |
582 | * @brief The %bitset class represents a @e fixed-size sequence of bits. | |
583 | * | |
584 | * @ingroup Containers | |
585 | * | |
e92a4045 PE |
586 | * (Note that %bitset does @e not meet the formal requirements of a |
587 | * <a href="tables.html#65">container</a>. Mainly, it lacks iterators.) | |
b963aad8 | 588 | * |
70e12fb9 PE |
589 | * The template argument, @a Nb, may be any non-negative number, |
590 | * specifying the number of bits (e.g., "0", "12", "1024*1024"). | |
b963aad8 | 591 | * |
70e12fb9 PE |
592 | * In the general unoptimized case, storage is allocated in word-sized |
593 | * blocks. Let B be the number of bits in a word, then (Nb+(B-1))/B | |
594 | * words will be used for storage. B - Nb%B bits are unused. (They are | |
595 | * the high-order bits in the highest word.) It is a class invariant | |
596 | * that those unused bits are always zero. | |
b963aad8 PE |
597 | * |
598 | * If you think of %bitset as "a simple array of bits," be aware that | |
599 | * your mental picture is reversed: a %bitset behaves the same way as | |
600 | * bits in integers do, with the bit at index 0 in the "least significant | |
70e12fb9 | 601 | * / right-hand" position, and the bit at index Nb-1 in the "most |
b963aad8 PE |
602 | * significant / left-hand" position. Thus, unlike other containers, a |
603 | * %bitset's index "counts from right to left," to put it very loosely. | |
604 | * | |
605 | * This behavior is preserved when translating to and from strings. For | |
606 | * example, the first line of the following program probably prints | |
607 | * "b('a') is 0001100001" on a modern ASCII system. | |
608 | * | |
609 | * @code | |
610 | * #include <bitset> | |
611 | * #include <iostream> | |
612 | * #include <sstream> | |
613 | * | |
614 | * using namespace std; | |
615 | * | |
616 | * int main() | |
617 | * { | |
618 | * long a = 'a'; | |
619 | * bitset<10> b(a); | |
620 | * | |
621 | * cout << "b('a') is " << b << endl; | |
622 | * | |
623 | * ostringstream s; | |
624 | * s << b; | |
625 | * string str = s.str(); | |
626 | * cout << "index 3 in the string is " << str[3] << " but\n" | |
627 | * << "index 3 in the bitset is " << b[3] << endl; | |
628 | * } | |
629 | * @endcode | |
630 | * | |
631 | * Also see http://gcc.gnu.org/onlinedocs/libstdc++/ext/sgiexts.html#ch23 | |
70e12fb9 | 632 | * for a description of extensions. |
b963aad8 PE |
633 | * |
634 | * @if maint | |
635 | * Most of the actual code isn't contained in %bitset<> itself, but in the | |
636 | * base class _Base_bitset. The base class works with whole words, not with | |
637 | * individual bits. This allows us to specialize _Base_bitset for the | |
638 | * important special case where the %bitset is only a single word. | |
639 | * | |
640 | * Extra confusion can result due to the fact that the storage for | |
641 | * _Base_bitset @e is a regular array, and is indexed as such. This is | |
642 | * carefully encapsulated. | |
643 | * @endif | |
644 | */ | |
1cb7f91f | 645 | template<size_t _Nb> |
5c33bb62 PC |
646 | class bitset |
647 | : private _Base_bitset<_GLIBCXX_BITSET_WORDS(_Nb)> | |
1cb7f91f | 648 | { |
5c33bb62 PC |
649 | private: |
650 | typedef _Base_bitset<_GLIBCXX_BITSET_WORDS(_Nb)> _Base; | |
651 | typedef unsigned long _WordT; | |
54c1bf78 | 652 | |
5c33bb62 PC |
653 | void |
654 | _M_do_sanitize() | |
655 | { | |
656 | _Sanitize<_Nb % _GLIBCXX_BITSET_BITS_PER_WORD>:: | |
657 | _S_do_sanitize(this->_M_hiword()); | |
658 | } | |
b963aad8 | 659 | |
5c33bb62 PC |
660 | public: |
661 | /** | |
662 | * This encapsulates the concept of a single bit. An instance of this | |
663 | * class is a proxy for an actual bit; this way the individual bit | |
664 | * operations are done as faster word-size bitwise instructions. | |
665 | * | |
666 | * Most users will never need to use this class directly; conversions | |
667 | * to and from bool are automatic and should be transparent. Overloaded | |
668 | * operators help to preserve the illusion. | |
669 | * | |
670 | * (On a typical system, this "bit %reference" is 64 times the size of | |
671 | * an actual bit. Ha.) | |
672 | */ | |
673 | class reference | |
674 | { | |
675 | friend class bitset; | |
676 | ||
677 | _WordT *_M_wp; | |
678 | size_t _M_bpos; | |
679 | ||
680 | // left undefined | |
681 | reference(); | |
682 | ||
683 | public: | |
684 | reference(bitset& __b, size_t __pos) | |
685 | { | |
686 | _M_wp = &__b._M_getword(__pos); | |
687 | _M_bpos = _Base::_S_whichbit(__pos); | |
688 | } | |
b963aad8 | 689 | |
5c33bb62 PC |
690 | ~reference() |
691 | { } | |
b963aad8 | 692 | |
5c33bb62 PC |
693 | // For b[i] = __x; |
694 | reference& | |
695 | operator=(bool __x) | |
696 | { | |
697 | if (__x) | |
698 | *_M_wp |= _Base::_S_maskbit(_M_bpos); | |
699 | else | |
700 | *_M_wp &= ~_Base::_S_maskbit(_M_bpos); | |
701 | return *this; | |
702 | } | |
703 | ||
704 | // For b[i] = b[__j]; | |
705 | reference& | |
706 | operator=(const reference& __j) | |
707 | { | |
708 | if ((*(__j._M_wp) & _Base::_S_maskbit(__j._M_bpos))) | |
709 | *_M_wp |= _Base::_S_maskbit(_M_bpos); | |
710 | else | |
711 | *_M_wp &= ~_Base::_S_maskbit(_M_bpos); | |
712 | return *this; | |
713 | } | |
714 | ||
715 | // Flips the bit | |
716 | bool | |
717 | operator~() const | |
718 | { return (*(_M_wp) & _Base::_S_maskbit(_M_bpos)) == 0; } | |
719 | ||
720 | // For __x = b[i]; | |
721 | operator bool() const | |
722 | { return (*(_M_wp) & _Base::_S_maskbit(_M_bpos)) != 0; } | |
723 | ||
724 | // For b[i].flip(); | |
725 | reference& | |
726 | flip() | |
727 | { | |
728 | *_M_wp ^= _Base::_S_maskbit(_M_bpos); | |
729 | return *this; | |
730 | } | |
731 | }; | |
732 | friend class reference; | |
733 | ||
734 | // 23.3.5.1 constructors: | |
735 | /// All bits set to zero. | |
736 | bitset() | |
737 | { } | |
738 | ||
739 | /// Initial bits bitwise-copied from a single word (others set to zero). | |
740 | bitset(unsigned long __val) | |
741 | : _Base(__val) | |
742 | { _M_do_sanitize(); } | |
743 | ||
744 | /** | |
745 | * @brief Use a subset of a string. | |
746 | * @param s A string of '0' and '1' characters. | |
747 | * @param position Index of the first character in @a s to use; | |
748 | * defaults to zero. | |
749 | * @throw std::out_of_range If @a pos is bigger the size of @a s. | |
750 | * @throw std::invalid_argument If a character appears in the string | |
751 | * which is neither '0' nor '1'. | |
752 | */ | |
753 | template<class _CharT, class _Traits, class _Alloc> | |
754 | explicit | |
6323b34e | 755 | bitset(const std::basic_string<_CharT, _Traits, _Alloc>& __s, |
5c33bb62 PC |
756 | size_t __position = 0) |
757 | : _Base() | |
758 | { | |
759 | if (__position > __s.size()) | |
760 | __throw_out_of_range(__N("bitset::bitset initial position " | |
761 | "not valid")); | |
762 | _M_copy_from_string(__s, __position, | |
6323b34e | 763 | std::basic_string<_CharT, _Traits, _Alloc>::npos); |
5c33bb62 PC |
764 | } |
765 | ||
766 | /** | |
767 | * @brief Use a subset of a string. | |
768 | * @param s A string of '0' and '1' characters. | |
769 | * @param position Index of the first character in @a s to use. | |
770 | * @param n The number of characters to copy. | |
771 | * @throw std::out_of_range If @a pos is bigger the size of @a s. | |
772 | * @throw std::invalid_argument If a character appears in the string | |
773 | * which is neither '0' nor '1'. | |
774 | */ | |
775 | template<class _CharT, class _Traits, class _Alloc> | |
6323b34e | 776 | bitset(const std::basic_string<_CharT, _Traits, _Alloc>& __s, |
5c33bb62 PC |
777 | size_t __position, size_t __n) |
778 | : _Base() | |
779 | { | |
780 | if (__position > __s.size()) | |
781 | __throw_out_of_range(__N("bitset::bitset initial position " | |
782 | "not valid")); | |
783 | _M_copy_from_string(__s, __position, __n); | |
784 | } | |
785 | ||
786 | // 23.3.5.2 bitset operations: | |
787 | //@{ | |
788 | /** | |
789 | * @brief Operations on bitsets. | |
790 | * @param rhs A same-sized bitset. | |
791 | * | |
792 | * These should be self-explanatory. | |
793 | */ | |
794 | bitset<_Nb>& | |
795 | operator&=(const bitset<_Nb>& __rhs) | |
1cb7f91f | 796 | { |
5c33bb62 PC |
797 | this->_M_do_and(__rhs); |
798 | return *this; | |
1cb7f91f | 799 | } |
54c1bf78 | 800 | |
5c33bb62 PC |
801 | bitset<_Nb>& |
802 | operator|=(const bitset<_Nb>& __rhs) | |
803 | { | |
804 | this->_M_do_or(__rhs); | |
805 | return *this; | |
806 | } | |
1cb7f91f | 807 | |
5c33bb62 PC |
808 | bitset<_Nb>& |
809 | operator^=(const bitset<_Nb>& __rhs) | |
1cb7f91f | 810 | { |
5c33bb62 PC |
811 | this->_M_do_xor(__rhs); |
812 | return *this; | |
813 | } | |
814 | //@} | |
815 | ||
816 | //@{ | |
817 | /** | |
818 | * @brief Operations on bitsets. | |
819 | * @param position The number of places to shift. | |
820 | * | |
821 | * These should be self-explanatory. | |
822 | */ | |
823 | bitset<_Nb>& | |
824 | operator<<=(size_t __position) | |
825 | { | |
826 | if (__builtin_expect(__position < _Nb, 1)) | |
827 | { | |
828 | this->_M_do_left_shift(__position); | |
829 | this->_M_do_sanitize(); | |
830 | } | |
1cb7f91f | 831 | else |
5c33bb62 | 832 | this->_M_do_reset(); |
1cb7f91f BK |
833 | return *this; |
834 | } | |
b963aad8 | 835 | |
5c33bb62 PC |
836 | bitset<_Nb>& |
837 | operator>>=(size_t __position) | |
1cb7f91f | 838 | { |
5c33bb62 PC |
839 | if (__builtin_expect(__position < _Nb, 1)) |
840 | { | |
841 | this->_M_do_right_shift(__position); | |
842 | this->_M_do_sanitize(); | |
843 | } | |
1cb7f91f | 844 | else |
5c33bb62 | 845 | this->_M_do_reset(); |
1cb7f91f BK |
846 | return *this; |
847 | } | |
5c33bb62 PC |
848 | //@} |
849 | ||
850 | //@{ | |
851 | /** | |
852 | * These versions of single-bit set, reset, flip, and test are | |
853 | * extensions from the SGI version. They do no range checking. | |
854 | * @ingroup SGIextensions | |
855 | */ | |
856 | bitset<_Nb>& | |
857 | _Unchecked_set(size_t __pos) | |
1cb7f91f | 858 | { |
5c33bb62 | 859 | this->_M_getword(__pos) |= _Base::_S_maskbit(__pos); |
1cb7f91f BK |
860 | return *this; |
861 | } | |
5c33bb62 PC |
862 | |
863 | bitset<_Nb>& | |
864 | _Unchecked_set(size_t __pos, int __val) | |
1cb7f91f | 865 | { |
5c33bb62 PC |
866 | if (__val) |
867 | this->_M_getword(__pos) |= _Base::_S_maskbit(__pos); | |
868 | else | |
869 | this->_M_getword(__pos) &= ~_Base::_S_maskbit(__pos); | |
870 | return *this; | |
1cb7f91f | 871 | } |
54c1bf78 | 872 | |
5c33bb62 PC |
873 | bitset<_Nb>& |
874 | _Unchecked_reset(size_t __pos) | |
1cb7f91f | 875 | { |
5c33bb62 PC |
876 | this->_M_getword(__pos) &= ~_Base::_S_maskbit(__pos); |
877 | return *this; | |
1cb7f91f | 878 | } |
54c1bf78 | 879 | |
5c33bb62 PC |
880 | bitset<_Nb>& |
881 | _Unchecked_flip(size_t __pos) | |
882 | { | |
883 | this->_M_getword(__pos) ^= _Base::_S_maskbit(__pos); | |
884 | return *this; | |
885 | } | |
54c1bf78 | 886 | |
5c33bb62 PC |
887 | bool |
888 | _Unchecked_test(size_t __pos) const | |
889 | { return ((this->_M_getword(__pos) & _Base::_S_maskbit(__pos)) | |
890 | != static_cast<_WordT>(0)); } | |
891 | //@} | |
892 | ||
893 | // Set, reset, and flip. | |
894 | /** | |
895 | * @brief Sets every bit to true. | |
896 | */ | |
897 | bitset<_Nb>& | |
898 | set() | |
899 | { | |
900 | this->_M_do_set(); | |
901 | this->_M_do_sanitize(); | |
902 | return *this; | |
903 | } | |
54c1bf78 | 904 | |
5c33bb62 PC |
905 | /** |
906 | * @brief Sets a given bit to a particular value. | |
907 | * @param position The index of the bit. | |
908 | * @param val Either true or false, defaults to true. | |
909 | * @throw std::out_of_range If @a pos is bigger the size of the %set. | |
910 | */ | |
911 | bitset<_Nb>& | |
912 | set(size_t __position, bool __val = true) | |
913 | { | |
914 | if (__position >= _Nb) | |
915 | __throw_out_of_range(__N("bitset::set")); | |
916 | return _Unchecked_set(__position, __val); | |
917 | } | |
54c1bf78 | 918 | |
5c33bb62 PC |
919 | /** |
920 | * @brief Sets every bit to false. | |
921 | */ | |
922 | bitset<_Nb>& | |
923 | reset() | |
924 | { | |
3bbfb3d9 | 925 | this->_M_do_reset(); |
5c33bb62 PC |
926 | return *this; |
927 | } | |
54c1bf78 | 928 | |
5c33bb62 PC |
929 | /** |
930 | * @brief Sets a given bit to false. | |
931 | * @param position The index of the bit. | |
932 | * @throw std::out_of_range If @a pos is bigger the size of the %set. | |
933 | * | |
934 | * Same as writing @c set(pos,false). | |
935 | */ | |
936 | bitset<_Nb>& | |
937 | reset(size_t __position) | |
938 | { | |
939 | if (__position >= _Nb) | |
940 | __throw_out_of_range(__N("bitset::reset")); | |
941 | return _Unchecked_reset(__position); | |
942 | } | |
943 | ||
944 | /** | |
945 | * @brief Toggles every bit to its opposite value. | |
946 | */ | |
947 | bitset<_Nb>& | |
948 | flip() | |
949 | { | |
950 | this->_M_do_flip(); | |
951 | this->_M_do_sanitize(); | |
952 | return *this; | |
953 | } | |
54c1bf78 | 954 | |
5c33bb62 PC |
955 | /** |
956 | * @brief Toggles a given bit to its opposite value. | |
957 | * @param position The index of the bit. | |
958 | * @throw std::out_of_range If @a pos is bigger the size of the %set. | |
959 | */ | |
960 | bitset<_Nb>& | |
961 | flip(size_t __position) | |
962 | { | |
963 | if (__position >= _Nb) | |
964 | __throw_out_of_range(__N("bitset::flip")); | |
965 | return _Unchecked_flip(__position); | |
966 | } | |
967 | ||
968 | /// See the no-argument flip(). | |
969 | bitset<_Nb> | |
970 | operator~() const | |
971 | { return bitset<_Nb>(*this).flip(); } | |
972 | ||
973 | //@{ | |
974 | /** | |
975 | * @brief Array-indexing support. | |
976 | * @param position Index into the %bitset. | |
977 | * @return A bool for a 'const %bitset'. For non-const bitsets, an | |
978 | * instance of the reference proxy class. | |
979 | * @note These operators do no range checking and throw no exceptions, | |
980 | * as required by DR 11 to the standard. | |
981 | * | |
982 | * @if maint | |
983 | * _GLIBCXX_RESOLVE_LIB_DEFECTS Note that this implementation already | |
984 | * resolves DR 11 (items 1 and 2), but does not do the range-checking | |
985 | * required by that DR's resolution. -pme | |
986 | * The DR has since been changed: range-checking is a precondition | |
987 | * (users' responsibility), and these functions must not throw. -pme | |
988 | * @endif | |
989 | */ | |
990 | reference | |
991 | operator[](size_t __position) | |
992 | { return reference(*this,__position); } | |
54c1bf78 | 993 | |
5c33bb62 PC |
994 | bool |
995 | operator[](size_t __position) const | |
996 | { return _Unchecked_test(__position); } | |
997 | //@} | |
998 | ||
999 | /** | |
1000 | * @brief Retuns a numerical interpretation of the %bitset. | |
1001 | * @return The integral equivalent of the bits. | |
1002 | * @throw std::overflow_error If there are too many bits to be | |
1003 | * represented in an @c unsigned @c long. | |
1004 | */ | |
1005 | unsigned long | |
1006 | to_ulong() const | |
1007 | { return this->_M_do_to_ulong(); } | |
1008 | ||
1009 | /** | |
1010 | * @brief Retuns a character interpretation of the %bitset. | |
1011 | * @return The string equivalent of the bits. | |
1012 | * | |
1013 | * Note the ordering of the bits: decreasing character positions | |
1014 | * correspond to increasing bit positions (see the main class notes for | |
1015 | * an example). | |
5c33bb62 PC |
1016 | */ |
1017 | template<class _CharT, class _Traits, class _Alloc> | |
6323b34e | 1018 | std::basic_string<_CharT, _Traits, _Alloc> |
5c33bb62 PC |
1019 | to_string() const |
1020 | { | |
6323b34e | 1021 | std::basic_string<_CharT, _Traits, _Alloc> __result; |
5c33bb62 PC |
1022 | _M_copy_to_string(__result); |
1023 | return __result; | |
1024 | } | |
54c1bf78 | 1025 | |
14492f0b PC |
1026 | // _GLIBCXX_RESOLVE_LIB_DEFECTS |
1027 | // 434. bitset::to_string() hard to use. | |
1028 | template<class _CharT, class _Traits> | |
6323b34e | 1029 | std::basic_string<_CharT, _Traits, std::allocator<_CharT> > |
14492f0b | 1030 | to_string() const |
6323b34e | 1031 | { return to_string<_CharT, _Traits, std::allocator<_CharT> >(); } |
14492f0b PC |
1032 | |
1033 | template<class _CharT> | |
6323b34e PC |
1034 | std::basic_string<_CharT, std::char_traits<_CharT>, |
1035 | std::allocator<_CharT> > | |
14492f0b | 1036 | to_string() const |
6323b34e PC |
1037 | { |
1038 | return to_string<_CharT, std::char_traits<_CharT>, | |
1039 | std::allocator<_CharT> >(); | |
1040 | } | |
14492f0b | 1041 | |
6323b34e | 1042 | std::basic_string<char, std::char_traits<char>, std::allocator<char> > |
14492f0b | 1043 | to_string() const |
6323b34e PC |
1044 | { |
1045 | return to_string<char, std::char_traits<char>, | |
1046 | std::allocator<char> >(); | |
1047 | } | |
14492f0b | 1048 | |
5c33bb62 PC |
1049 | // Helper functions for string operations. |
1050 | template<class _CharT, class _Traits, class _Alloc> | |
1051 | void | |
6323b34e PC |
1052 | _M_copy_from_string(const std::basic_string<_CharT, |
1053 | _Traits, _Alloc>& __s, | |
5c33bb62 | 1054 | size_t, size_t); |
54c1bf78 | 1055 | |
5c33bb62 PC |
1056 | template<class _CharT, class _Traits, class _Alloc> |
1057 | void | |
6323b34e | 1058 | _M_copy_to_string(std::basic_string<_CharT, _Traits, _Alloc>&) const; |
54c1bf78 | 1059 | |
5c33bb62 PC |
1060 | /// Returns the number of bits which are set. |
1061 | size_t | |
1062 | count() const | |
1063 | { return this->_M_do_count(); } | |
54c1bf78 | 1064 | |
5c33bb62 PC |
1065 | /// Returns the total number of bits. |
1066 | size_t | |
1067 | size() const | |
1068 | { return _Nb; } | |
54c1bf78 | 1069 | |
5c33bb62 PC |
1070 | //@{ |
1071 | /// These comparisons for equality/inequality are, well, @e bitwise. | |
1072 | bool | |
1073 | operator==(const bitset<_Nb>& __rhs) const | |
1074 | { return this->_M_is_equal(__rhs); } | |
54c1bf78 | 1075 | |
5c33bb62 PC |
1076 | bool |
1077 | operator!=(const bitset<_Nb>& __rhs) const | |
1078 | { return !this->_M_is_equal(__rhs); } | |
1079 | //@} | |
1080 | ||
1081 | /** | |
1082 | * @brief Tests the value of a bit. | |
1083 | * @param position The index of a bit. | |
1084 | * @return The value at @a pos. | |
1085 | * @throw std::out_of_range If @a pos is bigger the size of the %set. | |
1086 | */ | |
1087 | bool | |
1088 | test(size_t __position) const | |
1cb7f91f | 1089 | { |
5c33bb62 PC |
1090 | if (__position >= _Nb) |
1091 | __throw_out_of_range(__N("bitset::test")); | |
1092 | return _Unchecked_test(__position); | |
1cb7f91f | 1093 | } |
5c33bb62 PC |
1094 | |
1095 | /** | |
1096 | * @brief Tests whether any of the bits are on. | |
1097 | * @return True if at least one bit is set. | |
1098 | */ | |
1099 | bool | |
1100 | any() const | |
1101 | { return this->_M_is_any(); } | |
54c1bf78 | 1102 | |
5c33bb62 PC |
1103 | /** |
1104 | * @brief Tests whether any of the bits are on. | |
1105 | * @return True if none of the bits are set. | |
1106 | */ | |
1107 | bool | |
1108 | none() const | |
1109 | { return !this->_M_is_any(); } | |
1110 | ||
1111 | //@{ | |
1112 | /// Self-explanatory. | |
1113 | bitset<_Nb> | |
1114 | operator<<(size_t __position) const | |
1115 | { return bitset<_Nb>(*this) <<= __position; } | |
1116 | ||
1117 | bitset<_Nb> | |
1118 | operator>>(size_t __position) const | |
1119 | { return bitset<_Nb>(*this) >>= __position; } | |
1120 | //@} | |
1121 | ||
1122 | /** | |
1123 | * @brief Finds the index of the first "on" bit. | |
1124 | * @return The index of the first bit set, or size() if not found. | |
1125 | * @ingroup SGIextensions | |
1126 | * @sa _Find_next | |
1127 | */ | |
1128 | size_t | |
1129 | _Find_first() const | |
1130 | { return this->_M_do_find_first(_Nb); } | |
1131 | ||
1132 | /** | |
1133 | * @brief Finds the index of the next "on" bit after prev. | |
1134 | * @return The index of the next bit set, or size() if not found. | |
1135 | * @param prev Where to start searching. | |
1136 | * @ingroup SGIextensions | |
1137 | * @sa _Find_first | |
1138 | */ | |
1139 | size_t | |
1140 | _Find_next(size_t __prev ) const | |
1141 | { return this->_M_do_find_next(__prev, _Nb); } | |
1142 | }; | |
54c1bf78 | 1143 | |
1cb7f91f BK |
1144 | // Definitions of non-inline member functions. |
1145 | template<size_t _Nb> | |
1146 | template<class _CharT, class _Traits, class _Alloc> | |
5c33bb62 | 1147 | void |
e7c59a0e PC |
1148 | bitset<_Nb>:: |
1149 | _M_copy_from_string(const std::basic_string<_CharT, _Traits, | |
1150 | _Alloc>& __s, size_t __pos, size_t __n) | |
5c33bb62 PC |
1151 | { |
1152 | reset(); | |
1153 | const size_t __nbits = std::min(_Nb, std::min(__n, __s.size() - __pos)); | |
e7c59a0e | 1154 | for (size_t __i = __nbits; __i > 0; --__i) |
5c33bb62 | 1155 | { |
e7c59a0e | 1156 | switch(__s[__pos + __nbits - __i]) |
5c33bb62 PC |
1157 | { |
1158 | case '0': | |
1159 | break; | |
1160 | case '1': | |
8fecd28c | 1161 | _Unchecked_set(__i - 1); |
5c33bb62 PC |
1162 | break; |
1163 | default: | |
1164 | __throw_invalid_argument(__N("bitset::_M_copy_from_string")); | |
1165 | } | |
1166 | } | |
1167 | } | |
54c1bf78 | 1168 | |
1cb7f91f BK |
1169 | template<size_t _Nb> |
1170 | template<class _CharT, class _Traits, class _Alloc> | |
5c33bb62 | 1171 | void |
e7c59a0e PC |
1172 | bitset<_Nb>:: |
1173 | _M_copy_to_string(std::basic_string<_CharT, _Traits, _Alloc>& __s) const | |
5c33bb62 PC |
1174 | { |
1175 | __s.assign(_Nb, '0'); | |
e7c59a0e PC |
1176 | for (size_t __i = _Nb; __i > 0; --__i) |
1177 | if (_Unchecked_test(__i - 1)) | |
1178 | __s[_Nb - __i] = '1'; | |
5c33bb62 | 1179 | } |
54c1bf78 | 1180 | |
1cb7f91f | 1181 | // 23.3.5.3 bitset operations: |
b963aad8 PE |
1182 | //@{ |
1183 | /** | |
1184 | * @brief Global bitwise operations on bitsets. | |
1185 | * @param x A bitset. | |
1186 | * @param y A bitset of the same size as @a x. | |
1187 | * @return A new bitset. | |
1188 | * | |
1189 | * These should be self-explanatory. | |
1190 | */ | |
1cb7f91f | 1191 | template<size_t _Nb> |
b963aad8 PE |
1192 | inline bitset<_Nb> |
1193 | operator&(const bitset<_Nb>& __x, const bitset<_Nb>& __y) | |
1cb7f91f BK |
1194 | { |
1195 | bitset<_Nb> __result(__x); | |
1196 | __result &= __y; | |
1197 | return __result; | |
54c1bf78 BK |
1198 | } |
1199 | ||
1cb7f91f | 1200 | template<size_t _Nb> |
b963aad8 PE |
1201 | inline bitset<_Nb> |
1202 | operator|(const bitset<_Nb>& __x, const bitset<_Nb>& __y) | |
1cb7f91f BK |
1203 | { |
1204 | bitset<_Nb> __result(__x); | |
1205 | __result |= __y; | |
1206 | return __result; | |
1207 | } | |
54c1bf78 | 1208 | |
1cb7f91f | 1209 | template <size_t _Nb> |
b963aad8 PE |
1210 | inline bitset<_Nb> |
1211 | operator^(const bitset<_Nb>& __x, const bitset<_Nb>& __y) | |
1cb7f91f BK |
1212 | { |
1213 | bitset<_Nb> __result(__x); | |
1214 | __result ^= __y; | |
1215 | return __result; | |
1216 | } | |
b963aad8 PE |
1217 | //@} |
1218 | ||
1219 | //@{ | |
1220 | /** | |
1221 | * @brief Global I/O operators for bitsets. | |
1222 | * | |
1223 | * Direct I/O between streams and bitsets is supported. Output is | |
1224 | * straightforward. Input will skip whitespace, only accept '0' and '1' | |
1225 | * characters, and will only extract as many digits as the %bitset will | |
1226 | * hold. | |
1227 | */ | |
1cb7f91f | 1228 | template<class _CharT, class _Traits, size_t _Nb> |
6323b34e PC |
1229 | std::basic_istream<_CharT, _Traits>& |
1230 | operator>>(std::basic_istream<_CharT, _Traits>& __is, bitset<_Nb>& __x) | |
1cb7f91f BK |
1231 | { |
1232 | typedef typename _Traits::char_type char_type; | |
6323b34e | 1233 | std::basic_string<_CharT, _Traits> __tmp; |
1cb7f91f | 1234 | __tmp.reserve(_Nb); |
b963aad8 | 1235 | |
6323b34e PC |
1236 | std::ios_base::iostate __state = std::ios_base::goodbit; |
1237 | typename std::basic_istream<_CharT, _Traits>::sentry __sentry(__is); | |
b963aad8 | 1238 | if (__sentry) |
1cb7f91f | 1239 | { |
7f1156ed | 1240 | try |
1cb7f91f | 1241 | { |
7f1156ed PC |
1242 | basic_streambuf<_CharT, _Traits>* __buf = __is.rdbuf(); |
1243 | // _GLIBCXX_RESOLVE_LIB_DEFECTS | |
1244 | // 303. Bitset input operator underspecified | |
1245 | const char_type __zero = __is.widen('0'); | |
1246 | const char_type __one = __is.widen('1'); | |
e7c59a0e | 1247 | for (size_t __i = _Nb; __i > 0; --__i) |
1cb7f91f | 1248 | { |
7f1156ed PC |
1249 | static typename _Traits::int_type __eof = _Traits::eof(); |
1250 | ||
1251 | typename _Traits::int_type __c1 = __buf->sbumpc(); | |
1252 | if (_Traits::eq_int_type(__c1, __eof)) | |
1cb7f91f | 1253 | { |
6323b34e | 1254 | __state |= std::ios_base::eofbit; |
1cb7f91f BK |
1255 | break; |
1256 | } | |
7f1156ed PC |
1257 | else |
1258 | { | |
5c33bb62 | 1259 | const char_type __c2 = _Traits::to_char_type(__c1); |
7f1156ed PC |
1260 | if (__c2 == __zero) |
1261 | __tmp.push_back('0'); | |
1262 | else if (__c2 == __one) | |
1263 | __tmp.push_back('1'); | |
1264 | else if (_Traits::eq_int_type(__buf->sputbackc(__c2), | |
1265 | __eof)) | |
1266 | { | |
6323b34e | 1267 | __state |= std::ios_base::failbit; |
7f1156ed PC |
1268 | break; |
1269 | } | |
1270 | } | |
1cb7f91f BK |
1271 | } |
1272 | } | |
7f1156ed | 1273 | catch(...) |
6323b34e | 1274 | { __is._M_setstate(std::ios_base::badbit); } |
1cb7f91f BK |
1275 | } |
1276 | ||
7f1156ed | 1277 | if (__tmp.empty() && _Nb) |
6323b34e | 1278 | __state |= std::ios_base::failbit; |
7f1156ed PC |
1279 | else |
1280 | __x._M_copy_from_string(__tmp, static_cast<size_t>(0), _Nb); | |
1281 | if (__state) | |
1282 | __is.setstate(__state); | |
1cb7f91f BK |
1283 | return __is; |
1284 | } | |
54c1bf78 | 1285 | |
1cb7f91f | 1286 | template <class _CharT, class _Traits, size_t _Nb> |
6323b34e PC |
1287 | std::basic_ostream<_CharT, _Traits>& |
1288 | operator<<(std::basic_ostream<_CharT, _Traits>& __os, | |
1289 | const bitset<_Nb>& __x) | |
1cb7f91f | 1290 | { |
6323b34e | 1291 | std::basic_string<_CharT, _Traits> __tmp; |
1cb7f91f BK |
1292 | __x._M_copy_to_string(__tmp); |
1293 | return __os << __tmp; | |
1294 | } | |
e7c59a0e PC |
1295 | |
1296 | // Specializations for zero-sized bitsets, to avoid "unsigned comparison | |
1297 | // with zero" warnings. | |
1298 | template<> | |
1299 | inline bitset<0>& | |
1300 | bitset<0>:: | |
1301 | set(size_t, bool) | |
1302 | { | |
1303 | __throw_out_of_range(__N("bitset::set")); | |
1304 | return *this; | |
1305 | } | |
1306 | ||
1307 | template<> | |
1308 | inline bitset<0>& | |
1309 | bitset<0>:: | |
1310 | reset(size_t) | |
1311 | { | |
1312 | __throw_out_of_range(__N("bitset::reset")); | |
1313 | return *this; | |
1314 | } | |
1315 | ||
1316 | template<> | |
1317 | inline bitset<0>& | |
1318 | bitset<0>:: | |
1319 | flip(size_t) | |
1320 | { | |
1321 | __throw_out_of_range(__N("bitset::flip")); | |
1322 | return *this; | |
1323 | } | |
1324 | ||
1325 | template<> | |
1326 | inline bool | |
1327 | bitset<0>:: | |
1328 | test(size_t) const | |
1329 | { | |
1330 | __throw_out_of_range(__N("bitset::test")); | |
1331 | return false; | |
1332 | } | |
b963aad8 | 1333 | //@} |
3cbc7af0 BK |
1334 | |
1335 | _GLIBCXX_END_NESTED_NAMESPACE | |
54c1bf78 | 1336 | |
3d7c150e BK |
1337 | #undef _GLIBCXX_BITSET_WORDS |
1338 | #undef _GLIBCXX_BITSET_BITS_PER_WORD | |
54c1bf78 | 1339 | |
285b36d6 BK |
1340 | #ifdef _GLIBCXX_DEBUG |
1341 | # include <debug/bitset> | |
1342 | #endif | |
1343 | ||
1143680e | 1344 | #endif /* _GLIBCXX_BITSET */ |