* include/c_std/cmath.tcc: Use _GLIBCXX_ prefix on file guard.
[official-gcc.git] / libstdc++-v3 / include / std / std_bitset.h
blobab4ca236bd7f33b4dfa6569f21b9324f71cbed98
1 // <bitset> -*- C++ -*-
3 // Copyright (C) 2001, 2002, 2003 Free Software Foundation, Inc.
4 //
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 2, or (at your option)
9 // any later version.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING. If not, write to the Free
18 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
19 // USA.
21 // As a special exception, you may use this file as part of a free software
22 // library without restriction. Specifically, if other files instantiate
23 // templates or use macros or inline functions from this file, or you compile
24 // this file and link it with other files to produce an executable, this
25 // file does not by itself cause the resulting executable to be covered by
26 // the GNU General Public License. This exception does not however
27 // invalidate any other reasons why the executable file might be covered by
28 // the GNU General Public License.
31 * Copyright (c) 1998
32 * Silicon Graphics Computer Systems, Inc.
34 * Permission to use, copy, modify, distribute and sell this software
35 * and its documentation for any purpose is hereby granted without fee,
36 * provided that the above copyright notice appear in all copies and
37 * that both that copyright notice and this permission notice appear
38 * in supporting documentation. Silicon Graphics makes no
39 * representations about the suitability of this software for any
40 * purpose. It is provided "as is" without express or implied warranty.
43 /** @file bitset
44 * This is a Standard C++ Library header. You should @c #include this header
45 * in your programs, rather than any of the "st[dl]_*.h" implementation files.
48 #ifndef _GLIBCXX_BITSET
49 #define _GLIBCXX_BITSET 1
51 #pragma GCC system_header
53 #include <cstddef> // for size_t
54 #include <cstring> // for memset
55 #include <limits> // for numeric_limits
56 #include <string>
57 #include <bits/functexcept.h> // for invalid_argument, out_of_range,
58 // overflow_error
59 #include <ostream> // for ostream (operator<<)
60 #include <istream> // for istream (operator>>)
63 #define _GLIBCXX_BITSET_BITS_PER_WORD numeric_limits<unsigned long>::digits
64 #define _GLIBCXX_BITSET_WORDS(__n) \
65 ((__n) < 1 ? 0 : ((__n) + _GLIBCXX_BITSET_BITS_PER_WORD - 1)/_GLIBCXX_BITSET_BITS_PER_WORD)
67 namespace std
69 /**
70 * @if maint
71 * Base class, general case. It is a class inveriant that _Nw will be
72 * nonnegative.
74 * See documentation for bitset.
75 * @endif
77 template<size_t _Nw>
78 struct _Base_bitset
80 typedef unsigned long _WordT;
82 /// 0 is the least significant word.
83 _WordT _M_w[_Nw];
85 _Base_bitset() { _M_do_reset(); }
86 _Base_bitset(unsigned long __val)
88 _M_do_reset();
89 _M_w[0] = __val;
92 static size_t
93 _S_whichword(size_t __pos )
94 { return __pos / _GLIBCXX_BITSET_BITS_PER_WORD; }
96 static size_t
97 _S_whichbyte(size_t __pos )
98 { return (__pos % _GLIBCXX_BITSET_BITS_PER_WORD) / __CHAR_BIT__; }
100 static size_t
101 _S_whichbit(size_t __pos )
102 { return __pos % _GLIBCXX_BITSET_BITS_PER_WORD; }
104 static _WordT
105 _S_maskbit(size_t __pos )
106 { return (static_cast<_WordT>(1)) << _S_whichbit(__pos); }
108 _WordT&
109 _M_getword(size_t __pos)
110 { return _M_w[_S_whichword(__pos)]; }
112 _WordT
113 _M_getword(size_t __pos) const
114 { return _M_w[_S_whichword(__pos)]; }
116 _WordT&
117 _M_hiword() { return _M_w[_Nw - 1]; }
119 _WordT
120 _M_hiword() const { return _M_w[_Nw - 1]; }
122 void
123 _M_do_and(const _Base_bitset<_Nw>& __x)
125 for (size_t __i = 0; __i < _Nw; __i++)
126 _M_w[__i] &= __x._M_w[__i];
129 void
130 _M_do_or(const _Base_bitset<_Nw>& __x)
132 for (size_t __i = 0; __i < _Nw; __i++)
133 _M_w[__i] |= __x._M_w[__i];
136 void
137 _M_do_xor(const _Base_bitset<_Nw>& __x)
139 for (size_t __i = 0; __i < _Nw; __i++)
140 _M_w[__i] ^= __x._M_w[__i];
143 void
144 _M_do_left_shift(size_t __shift);
146 void
147 _M_do_right_shift(size_t __shift);
149 void
150 _M_do_flip()
152 for (size_t __i = 0; __i < _Nw; __i++)
153 _M_w[__i] = ~_M_w[__i];
156 void
157 _M_do_set()
159 for (size_t __i = 0; __i < _Nw; __i++)
160 _M_w[__i] = ~static_cast<_WordT>(0);
163 void
164 _M_do_reset() { memset(_M_w, 0, _Nw * sizeof(_WordT)); }
166 bool
167 _M_is_equal(const _Base_bitset<_Nw>& __x) const
169 for (size_t __i = 0; __i < _Nw; ++__i)
171 if (_M_w[__i] != __x._M_w[__i])
172 return false;
174 return true;
177 bool
178 _M_is_any() const
180 for (size_t __i = 0; __i < _Nw; __i++)
182 if (_M_w[__i] != static_cast<_WordT>(0))
183 return true;
185 return false;
188 size_t
189 _M_do_count() const
191 size_t __result = 0;
192 for (size_t __i = 0; __i < _Nw; __i++)
193 __result += __builtin_popcountl(_M_w[__i]);
194 return __result;
197 unsigned long
198 _M_do_to_ulong() const;
200 // find first "on" bit
201 size_t
202 _M_do_find_first(size_t __not_found) const;
204 // find the next "on" bit that follows "prev"
205 size_t
206 _M_do_find_next(size_t __prev, size_t __not_found) const;
209 // Definitions of non-inline functions from _Base_bitset.
210 template<size_t _Nw>
211 void
212 _Base_bitset<_Nw>::_M_do_left_shift(size_t __shift)
214 if (__builtin_expect(__shift != 0, 1))
216 const size_t __wshift = __shift / _GLIBCXX_BITSET_BITS_PER_WORD;
217 const size_t __offset = __shift % _GLIBCXX_BITSET_BITS_PER_WORD;
219 if (__offset == 0)
220 for (size_t __n = _Nw - 1; __n >= __wshift; --__n)
221 _M_w[__n] = _M_w[__n - __wshift];
222 else
224 const size_t __sub_offset = _GLIBCXX_BITSET_BITS_PER_WORD - __offset;
225 for (size_t __n = _Nw - 1; __n > __wshift; --__n)
226 _M_w[__n] = (_M_w[__n - __wshift] << __offset) |
227 (_M_w[__n - __wshift - 1] >> __sub_offset);
228 _M_w[__wshift] = _M_w[0] << __offset;
231 std::fill(_M_w + 0, _M_w + __wshift, static_cast<_WordT>(0));
235 template<size_t _Nw>
236 void
237 _Base_bitset<_Nw>::_M_do_right_shift(size_t __shift)
239 if (__builtin_expect(__shift != 0, 1))
241 const size_t __wshift = __shift / _GLIBCXX_BITSET_BITS_PER_WORD;
242 const size_t __offset = __shift % _GLIBCXX_BITSET_BITS_PER_WORD;
243 const size_t __limit = _Nw - __wshift - 1;
245 if (__offset == 0)
246 for (size_t __n = 0; __n <= __limit; ++__n)
247 _M_w[__n] = _M_w[__n + __wshift];
248 else
250 const size_t __sub_offset = _GLIBCXX_BITSET_BITS_PER_WORD - __offset;
251 for (size_t __n = 0; __n < __limit; ++__n)
252 _M_w[__n] = (_M_w[__n + __wshift] >> __offset) |
253 (_M_w[__n + __wshift + 1] << __sub_offset);
254 _M_w[__limit] = _M_w[_Nw-1] >> __offset;
257 std::fill(_M_w + __limit + 1, _M_w + _Nw, static_cast<_WordT>(0));
261 template<size_t _Nw>
262 unsigned long
263 _Base_bitset<_Nw>::_M_do_to_ulong() const
265 for (size_t __i = 1; __i < _Nw; ++__i)
266 if (_M_w[__i])
267 __throw_overflow_error(__N("_Base_bitset::_M_do_to_ulong"));
268 return _M_w[0];
271 template<size_t _Nw>
272 size_t
273 _Base_bitset<_Nw>::_M_do_find_first(size_t __not_found) const
275 for (size_t __i = 0; __i < _Nw; __i++)
277 _WordT __thisword = _M_w[__i];
278 if (__thisword != static_cast<_WordT>(0))
279 return __i * _GLIBCXX_BITSET_BITS_PER_WORD
280 + __builtin_ctzl(__thisword);
282 // not found, so return an indication of failure.
283 return __not_found;
286 template<size_t _Nw>
287 size_t
288 _Base_bitset<_Nw>::_M_do_find_next(size_t __prev, size_t __not_found) const
290 // make bound inclusive
291 ++__prev;
293 // check out of bounds
294 if ( __prev >= _Nw * _GLIBCXX_BITSET_BITS_PER_WORD )
295 return __not_found;
297 // search first word
298 size_t __i = _S_whichword(__prev);
299 _WordT __thisword = _M_w[__i];
301 // mask off bits below bound
302 __thisword >>= __prev + 1;
304 if (__thisword != static_cast<_WordT>(0))
305 return __i * _GLIBCXX_BITSET_BITS_PER_WORD
306 + __builtin_ctzl(__thisword);
308 // check subsequent words
309 __i++;
310 for ( ; __i < _Nw; __i++ )
312 __thisword = _M_w[__i];
313 if (__thisword != static_cast<_WordT>(0))
314 return __i * _GLIBCXX_BITSET_BITS_PER_WORD
315 + __builtin_ctzl(__thisword);
317 // not found, so return an indication of failure.
318 return __not_found;
319 } // end _M_do_find_next
323 * @if maint
324 * Base class, specialization for a single word.
326 * See documentation for bitset.
327 * @endif
329 template<>
330 struct _Base_bitset<1>
332 typedef unsigned long _WordT;
333 _WordT _M_w;
335 _Base_bitset( void ) : _M_w(0) {}
336 _Base_bitset(unsigned long __val) : _M_w(__val) {}
338 static size_t
339 _S_whichword(size_t __pos )
340 { return __pos / _GLIBCXX_BITSET_BITS_PER_WORD; }
342 static size_t
343 _S_whichbyte(size_t __pos )
344 { return (__pos % _GLIBCXX_BITSET_BITS_PER_WORD) / __CHAR_BIT__; }
346 static size_t
347 _S_whichbit(size_t __pos )
348 { return __pos % _GLIBCXX_BITSET_BITS_PER_WORD; }
350 static _WordT
351 _S_maskbit(size_t __pos )
352 { return (static_cast<_WordT>(1)) << _S_whichbit(__pos); }
354 _WordT&
355 _M_getword(size_t) { return _M_w; }
357 _WordT
358 _M_getword(size_t) const { return _M_w; }
360 _WordT&
361 _M_hiword() { return _M_w; }
363 _WordT
364 _M_hiword() const { return _M_w; }
366 void
367 _M_do_and(const _Base_bitset<1>& __x) { _M_w &= __x._M_w; }
369 void
370 _M_do_or(const _Base_bitset<1>& __x) { _M_w |= __x._M_w; }
372 void
373 _M_do_xor(const _Base_bitset<1>& __x) { _M_w ^= __x._M_w; }
375 void
376 _M_do_left_shift(size_t __shift) { _M_w <<= __shift; }
378 void
379 _M_do_right_shift(size_t __shift) { _M_w >>= __shift; }
381 void
382 _M_do_flip() { _M_w = ~_M_w; }
384 void
385 _M_do_set() { _M_w = ~static_cast<_WordT>(0); }
387 void
388 _M_do_reset() { _M_w = 0; }
390 bool
391 _M_is_equal(const _Base_bitset<1>& __x) const
392 { return _M_w == __x._M_w; }
394 bool
395 _M_is_any() const { return _M_w != 0; }
397 size_t
398 _M_do_count() const { return __builtin_popcountl(_M_w); }
400 unsigned long
401 _M_do_to_ulong() const { return _M_w; }
403 size_t
404 _M_do_find_first(size_t __not_found) const
406 if (_M_w != 0)
407 return __builtin_ctzl(_M_w);
408 else
409 return __not_found;
412 // find the next "on" bit that follows "prev"
413 size_t
414 _M_do_find_next(size_t __prev, size_t __not_found) const
416 ++__prev;
417 if (__prev >= ((size_t) _GLIBCXX_BITSET_BITS_PER_WORD))
418 return __not_found;
420 _WordT __x = _M_w >> __prev;
421 if (__x != 0)
422 return __builtin_ctzl(__x) + __prev;
423 else
424 return __not_found;
430 * @if maint
431 * Base class, specialization for no storage (zero-length %bitset).
433 * See documentation for bitset.
434 * @endif
436 template<>
437 struct _Base_bitset<0>
439 typedef unsigned long _WordT;
441 _Base_bitset() {}
442 _Base_bitset(unsigned long) {}
444 static size_t
445 _S_whichword(size_t __pos )
446 { return __pos / _GLIBCXX_BITSET_BITS_PER_WORD; }
448 static size_t
449 _S_whichbyte(size_t __pos )
450 { return (__pos % _GLIBCXX_BITSET_BITS_PER_WORD) / __CHAR_BIT__; }
452 static size_t
453 _S_whichbit(size_t __pos )
454 { return __pos % _GLIBCXX_BITSET_BITS_PER_WORD; }
456 static _WordT
457 _S_maskbit(size_t __pos )
458 { return (static_cast<_WordT>(1)) << _S_whichbit(__pos); }
460 // This would normally give access to the data. The bounds-checking
461 // in the bitset class will prevent the user from getting this far,
462 // but (1) it must still return an lvalue to compile, and (2) the
463 // user might call _Unchecked_set directly, in which case this /needs/
464 // to fail. Let's not penalize zero-length users unless they actually
465 // make an unchecked call; all the memory ugliness is therefore
466 // localized to this single should-never-get-this-far function.
467 _WordT&
468 _M_getword(size_t) const
470 __throw_out_of_range(__N("_Base_bitset::_M_getword"));
471 return *new _WordT;
474 _WordT
475 _M_hiword() const { return 0; }
477 void
478 _M_do_and(const _Base_bitset<0>&) { }
480 void
481 _M_do_or(const _Base_bitset<0>&) { }
483 void
484 _M_do_xor(const _Base_bitset<0>&) { }
486 void
487 _M_do_left_shift(size_t) { }
489 void
490 _M_do_right_shift(size_t) { }
492 void
493 _M_do_flip() { }
495 void
496 _M_do_set() { }
498 void
499 _M_do_reset() { }
501 // Are all empty bitsets equal to each other? Are they equal to
502 // themselves? How to compare a thing which has no state? What is
503 // the sound of one zero-length bitset clapping?
504 bool
505 _M_is_equal(const _Base_bitset<0>&) const { return true; }
507 bool
508 _M_is_any() const { return false; }
510 size_t
511 _M_do_count() const { return 0; }
513 unsigned long
514 _M_do_to_ulong() const { return 0; }
516 // Normally "not found" is the size, but that could also be
517 // misinterpreted as an index in this corner case. Oh well.
518 size_t
519 _M_do_find_first(size_t) const { return 0; }
521 size_t
522 _M_do_find_next(size_t, size_t) const { return 0; }
526 // Helper class to zero out the unused high-order bits in the highest word.
527 template<size_t _Extrabits>
528 struct _Sanitize
530 static void _S_do_sanitize(unsigned long& __val)
531 { __val &= ~((~static_cast<unsigned long>(0)) << _Extrabits); }
534 template<>
535 struct _Sanitize<0>
536 { static void _S_do_sanitize(unsigned long) { } };
540 * @brief The %bitset class represents a @e fixed-size sequence of bits.
542 * @ingroup Containers
544 * (Note that %bitset does @e not meet the formal requirements of a
545 * <a href="tables.html#65">container</a>. Mainly, it lacks iterators.)
547 * The template argument, @a Nb, may be any non-negative number,
548 * specifying the number of bits (e.g., "0", "12", "1024*1024").
550 * In the general unoptimized case, storage is allocated in word-sized
551 * blocks. Let B be the number of bits in a word, then (Nb+(B-1))/B
552 * words will be used for storage. B - Nb%B bits are unused. (They are
553 * the high-order bits in the highest word.) It is a class invariant
554 * that those unused bits are always zero.
556 * If you think of %bitset as "a simple array of bits," be aware that
557 * your mental picture is reversed: a %bitset behaves the same way as
558 * bits in integers do, with the bit at index 0 in the "least significant
559 * / right-hand" position, and the bit at index Nb-1 in the "most
560 * significant / left-hand" position. Thus, unlike other containers, a
561 * %bitset's index "counts from right to left," to put it very loosely.
563 * This behavior is preserved when translating to and from strings. For
564 * example, the first line of the following program probably prints
565 * "b('a') is 0001100001" on a modern ASCII system.
567 * @code
568 * #include <bitset>
569 * #include <iostream>
570 * #include <sstream>
572 * using namespace std;
574 * int main()
576 * long a = 'a';
577 * bitset<10> b(a);
579 * cout << "b('a') is " << b << endl;
581 * ostringstream s;
582 * s << b;
583 * string str = s.str();
584 * cout << "index 3 in the string is " << str[3] << " but\n"
585 * << "index 3 in the bitset is " << b[3] << endl;
587 * @endcode
589 * Also see http://gcc.gnu.org/onlinedocs/libstdc++/ext/sgiexts.html#ch23
590 * for a description of extensions.
592 * @if maint
593 * Most of the actual code isn't contained in %bitset<> itself, but in the
594 * base class _Base_bitset. The base class works with whole words, not with
595 * individual bits. This allows us to specialize _Base_bitset for the
596 * important special case where the %bitset is only a single word.
598 * Extra confusion can result due to the fact that the storage for
599 * _Base_bitset @e is a regular array, and is indexed as such. This is
600 * carefully encapsulated.
601 * @endif
603 template<size_t _Nb>
604 class bitset : private _Base_bitset<_GLIBCXX_BITSET_WORDS(_Nb)>
606 private:
607 typedef _Base_bitset<_GLIBCXX_BITSET_WORDS(_Nb)> _Base;
608 typedef unsigned long _WordT;
610 void
611 _M_do_sanitize()
613 _Sanitize<_Nb%_GLIBCXX_BITSET_BITS_PER_WORD>::
614 _S_do_sanitize(this->_M_hiword());
617 public:
619 * This encapsulates the concept of a single bit. An instance of this
620 * class is a proxy for an actual bit; this way the individual bit
621 * operations are done as faster word-size bitwise instructions.
623 * Most users will never need to use this class directly; conversions
624 * to and from bool are automatic and should be transparent. Overloaded
625 * operators help to preserve the illusion.
627 * (On a typical system, this "bit %reference" is 64 times the size of
628 * an actual bit. Ha.)
630 class reference
632 friend class bitset;
634 _WordT *_M_wp;
635 size_t _M_bpos;
637 // left undefined
638 reference();
640 public:
641 reference(bitset& __b, size_t __pos)
643 _M_wp = &__b._M_getword(__pos);
644 _M_bpos = _Base::_S_whichbit(__pos);
647 ~reference() { }
649 // for b[i] = __x;
650 reference&
651 operator=(bool __x)
653 if ( __x )
654 *_M_wp |= _Base::_S_maskbit(_M_bpos);
655 else
656 *_M_wp &= ~_Base::_S_maskbit(_M_bpos);
657 return *this;
660 // for b[i] = b[__j];
661 reference&
662 operator=(const reference& __j)
664 if ( (*(__j._M_wp) & _Base::_S_maskbit(__j._M_bpos)) )
665 *_M_wp |= _Base::_S_maskbit(_M_bpos);
666 else
667 *_M_wp &= ~_Base::_S_maskbit(_M_bpos);
668 return *this;
671 // flips the bit
672 bool
673 operator~() const
674 { return (*(_M_wp) & _Base::_S_maskbit(_M_bpos)) == 0; }
676 // for __x = b[i];
677 operator bool() const
678 { return (*(_M_wp) & _Base::_S_maskbit(_M_bpos)) != 0; }
680 // for b[i].flip();
681 reference&
682 flip()
684 *_M_wp ^= _Base::_S_maskbit(_M_bpos);
685 return *this;
688 friend class reference;
690 // 23.3.5.1 constructors:
691 /// All bits set to zero.
692 bitset() { }
694 /// Initial bits bitwise-copied from a single word (others set to zero).
695 bitset(unsigned long __val) : _Base(__val)
696 { _M_do_sanitize(); }
699 * @brief Use a subset of a string.
700 * @param s A string of '0' and '1' characters.
701 * @param position Index of the first character in @a s to use; defaults
702 * to zero.
703 * @throw std::out_of_range If @a pos is bigger the size of @a s.
704 * @throw std::invalid_argument If a character appears in the string
705 * which is neither '0' nor '1'.
707 template<class _CharT, class _Traits, class _Alloc>
708 explicit bitset(const basic_string<_CharT, _Traits, _Alloc>& __s,
709 size_t __position = 0) : _Base()
711 if (__position > __s.size())
712 __throw_out_of_range("bitset::bitset initial position not valid");
713 _M_copy_from_string(__s, __position,
714 basic_string<_CharT, _Traits, _Alloc>::npos);
718 * @brief Use a subset of a string.
719 * @param s A string of '0' and '1' characters.
720 * @param position Index of the first character in @a s to use.
721 * @param n The number of characters to copy.
722 * @throw std::out_of_range If @a pos is bigger the size of @a s.
723 * @throw std::invalid_argument If a character appears in the string
724 * which is neither '0' nor '1'.
726 template<class _CharT, class _Traits, class _Alloc>
727 bitset(const basic_string<_CharT, _Traits, _Alloc>& __s,
728 size_t __position, size_t __n) : _Base()
730 if (__position > __s.size())
731 __throw_out_of_range("bitset::bitset initial position not valid");
732 _M_copy_from_string(__s, __position, __n);
735 // 23.3.5.2 bitset operations:
736 //@{
738 * @brief Operations on bitsets.
739 * @param rhs A same-sized bitset.
741 * These should be self-explanatory.
743 bitset<_Nb>&
744 operator&=(const bitset<_Nb>& __rhs)
746 this->_M_do_and(__rhs);
747 return *this;
750 bitset<_Nb>&
751 operator|=(const bitset<_Nb>& __rhs)
753 this->_M_do_or(__rhs);
754 return *this;
757 bitset<_Nb>&
758 operator^=(const bitset<_Nb>& __rhs)
760 this->_M_do_xor(__rhs);
761 return *this;
763 //@}
765 //@{
767 * @brief Operations on bitsets.
768 * @param position The number of places to shift.
770 * These should be self-explanatory.
772 bitset<_Nb>&
773 operator<<=(size_t __position)
775 if (__builtin_expect(__position < _Nb, 1))
777 this->_M_do_left_shift(__position);
778 this->_M_do_sanitize();
780 else
781 this->_M_do_reset();
782 return *this;
785 bitset<_Nb>&
786 operator>>=(size_t __position)
788 if (__builtin_expect(__position < _Nb, 1))
790 this->_M_do_right_shift(__position);
791 this->_M_do_sanitize();
793 else
794 this->_M_do_reset();
795 return *this;
797 //@}
799 //@{
801 * These versions of single-bit set, reset, flip, and test are
802 * extensions from the SGI version. They do no range checking.
803 * @ingroup SGIextensions
805 bitset<_Nb>&
806 _Unchecked_set(size_t __pos)
808 this->_M_getword(__pos) |= _Base::_S_maskbit(__pos);
809 return *this;
812 bitset<_Nb>&
813 _Unchecked_set(size_t __pos, int __val)
815 if (__val)
816 this->_M_getword(__pos) |= _Base::_S_maskbit(__pos);
817 else
818 this->_M_getword(__pos) &= ~_Base::_S_maskbit(__pos);
819 return *this;
822 bitset<_Nb>&
823 _Unchecked_reset(size_t __pos)
825 this->_M_getword(__pos) &= ~_Base::_S_maskbit(__pos);
826 return *this;
829 bitset<_Nb>&
830 _Unchecked_flip(size_t __pos)
832 this->_M_getword(__pos) ^= _Base::_S_maskbit(__pos);
833 return *this;
836 bool
837 _Unchecked_test(size_t __pos) const
839 return (this->_M_getword(__pos) & _Base::_S_maskbit(__pos))
840 != static_cast<_WordT>(0);
842 //@}
844 // Set, reset, and flip.
846 * @brief Sets every bit to true.
848 bitset<_Nb>&
849 set()
851 this->_M_do_set();
852 this->_M_do_sanitize();
853 return *this;
857 * @brief Sets a given bit to a particular value.
858 * @param position The index of the bit.
859 * @param val Either true or false, defaults to true.
860 * @throw std::out_of_range If @a pos is bigger the size of the %set.
862 bitset<_Nb>&
863 set(size_t __position, bool __val = true)
865 if (__position >= _Nb)
866 __throw_out_of_range(__N("bitset::set"));
867 return _Unchecked_set(__position, __val);
871 * @brief Sets every bit to false.
873 bitset<_Nb>&
874 reset()
876 this->_M_do_reset();
877 return *this;
881 * @brief Sets a given bit to false.
882 * @param position The index of the bit.
883 * @throw std::out_of_range If @a pos is bigger the size of the %set.
885 * Same as writing @c set(pos,false).
887 bitset<_Nb>&
888 reset(size_t __position)
890 if (__position >= _Nb)
891 __throw_out_of_range(__N("bitset::reset"));
892 return _Unchecked_reset(__position);
896 * @brief Toggles every bit to its opposite value.
898 bitset<_Nb>&
899 flip()
901 this->_M_do_flip();
902 this->_M_do_sanitize();
903 return *this;
907 * @brief Toggles a given bit to its opposite value.
908 * @param position The index of the bit.
909 * @throw std::out_of_range If @a pos is bigger the size of the %set.
911 bitset<_Nb>&
912 flip(size_t __position)
914 if (__position >= _Nb)
915 __throw_out_of_range(__N("bitset::flip"));
916 return _Unchecked_flip(__position);
919 /// See the no-argument flip().
920 bitset<_Nb>
921 operator~() const { return bitset<_Nb>(*this).flip(); }
923 //@{
925 * @brief Array-indexing support.
926 * @param position Index into the %bitset.
927 * @return A bool for a 'const %bitset'. For non-const bitsets, an
928 * instance of the reference proxy class.
929 * @note These operators do no range checking and throw no exceptions,
930 * as required by DR 11 to the standard.
932 * @if maint
933 * _GLIBCXX_RESOLVE_LIB_DEFECTS Note that this implementation already
934 * resolves DR 11 (items 1 and 2), but does not do the range-checking
935 * required by that DR's resolution. -pme
936 * The DR has since been changed: range-checking is a precondition
937 * (users' responsibility), and these functions must not throw. -pme
938 * @endif
940 reference
941 operator[](size_t __position) { return reference(*this,__position); }
943 bool
944 operator[](size_t __position) const { return _Unchecked_test(__position); }
945 //@}
948 * @brief Retuns a numerical interpretation of the %bitset.
949 * @return The integral equivalent of the bits.
950 * @throw std::overflow_error If there are too many bits to be
951 * represented in an @c unsigned @c long.
953 unsigned long
954 to_ulong() const { return this->_M_do_to_ulong(); }
957 * @brief Retuns a character interpretation of the %bitset.
958 * @return The string equivalent of the bits.
960 * Note the ordering of the bits: decreasing character positions
961 * correspond to increasing bit positions (see the main class notes for
962 * an example).
964 * Also note that you must specify the string's template parameters
965 * explicitly. Given a bitset @c bs and a string @s:
966 * @code
967 * s = bs.to_string<char,char_traits<char>,allocator<char> >();
968 * @endcode
970 template<class _CharT, class _Traits, class _Alloc>
971 basic_string<_CharT, _Traits, _Alloc>
972 to_string() const
974 basic_string<_CharT, _Traits, _Alloc> __result;
975 _M_copy_to_string(__result);
976 return __result;
979 // Helper functions for string operations.
980 template<class _CharT, class _Traits, class _Alloc>
981 void
982 _M_copy_from_string(const basic_string<_CharT,_Traits,_Alloc>& __s,
983 size_t, size_t);
985 template<class _CharT, class _Traits, class _Alloc>
986 void
987 _M_copy_to_string(basic_string<_CharT,_Traits,_Alloc>&) const;
989 /// Returns the number of bits which are set.
990 size_t
991 count() const { return this->_M_do_count(); }
993 /// Returns the total number of bits.
994 size_t
995 size() const { return _Nb; }
997 //@{
998 /// These comparisons for equality/inequality are, well, @e bitwise.
999 bool
1000 operator==(const bitset<_Nb>& __rhs) const
1001 { return this->_M_is_equal(__rhs); }
1003 bool
1004 operator!=(const bitset<_Nb>& __rhs) const
1005 { return !this->_M_is_equal(__rhs); }
1006 //@}
1009 * @brief Tests the value of a bit.
1010 * @param position The index of a bit.
1011 * @return The value at @a pos.
1012 * @throw std::out_of_range If @a pos is bigger the size of the %set.
1014 bool
1015 test(size_t __position) const
1017 if (__position >= _Nb)
1018 __throw_out_of_range(__N("bitset::test"));
1019 return _Unchecked_test(__position);
1023 * @brief Tests whether any of the bits are on.
1024 * @return True if at least one bit is set.
1026 bool
1027 any() const { return this->_M_is_any(); }
1030 * @brief Tests whether any of the bits are on.
1031 * @return True if none of the bits are set.
1033 bool
1034 none() const { return !this->_M_is_any(); }
1036 //@{
1037 /// Self-explanatory.
1038 bitset<_Nb>
1039 operator<<(size_t __position) const
1040 { return bitset<_Nb>(*this) <<= __position; }
1042 bitset<_Nb>
1043 operator>>(size_t __position) const
1044 { return bitset<_Nb>(*this) >>= __position; }
1045 //@}
1048 * @brief Finds the index of the first "on" bit.
1049 * @return The index of the first bit set, or size() if not found.
1050 * @ingroup SGIextensions
1051 * @sa _Find_next
1053 size_t
1054 _Find_first() const
1055 { return this->_M_do_find_first(_Nb); }
1058 * @brief Finds the index of the next "on" bit after prev.
1059 * @return The index of the next bit set, or size() if not found.
1060 * @param prev Where to start searching.
1061 * @ingroup SGIextensions
1062 * @sa _Find_first
1064 size_t
1065 _Find_next(size_t __prev ) const
1066 { return this->_M_do_find_next(__prev, _Nb); }
1069 // Definitions of non-inline member functions.
1070 template<size_t _Nb>
1071 template<class _CharT, class _Traits, class _Alloc>
1072 void
1073 bitset<_Nb>::_M_copy_from_string(const basic_string<_CharT,_Traits,_Alloc>& __s, size_t __pos, size_t __n)
1075 reset();
1076 const size_t __nbits = std::min(_Nb, std::min(__n, __s.size() - __pos));
1077 for (size_t __i = 0; __i < __nbits; ++__i)
1079 switch(__s[__pos + __nbits - __i - 1])
1081 case '0':
1082 break;
1083 case '1':
1084 set(__i);
1085 break;
1086 default:
1087 __throw_invalid_argument("bitset::_M_copy_from_string");
1092 template<size_t _Nb>
1093 template<class _CharT, class _Traits, class _Alloc>
1094 void
1095 bitset<_Nb>::_M_copy_to_string(basic_string<_CharT, _Traits, _Alloc>& __s) const
1097 __s.assign(_Nb, '0');
1098 for (size_t __i = 0; __i < _Nb; ++__i)
1099 if (_Unchecked_test(__i))
1100 __s[_Nb - 1 - __i] = '1';
1103 // 23.3.5.3 bitset operations:
1104 //@{
1106 * @brief Global bitwise operations on bitsets.
1107 * @param x A bitset.
1108 * @param y A bitset of the same size as @a x.
1109 * @return A new bitset.
1111 * These should be self-explanatory.
1113 template<size_t _Nb>
1114 inline bitset<_Nb>
1115 operator&(const bitset<_Nb>& __x, const bitset<_Nb>& __y)
1117 bitset<_Nb> __result(__x);
1118 __result &= __y;
1119 return __result;
1122 template<size_t _Nb>
1123 inline bitset<_Nb>
1124 operator|(const bitset<_Nb>& __x, const bitset<_Nb>& __y)
1126 bitset<_Nb> __result(__x);
1127 __result |= __y;
1128 return __result;
1131 template <size_t _Nb>
1132 inline bitset<_Nb>
1133 operator^(const bitset<_Nb>& __x, const bitset<_Nb>& __y)
1135 bitset<_Nb> __result(__x);
1136 __result ^= __y;
1137 return __result;
1139 //@}
1141 //@{
1143 * @brief Global I/O operators for bitsets.
1145 * Direct I/O between streams and bitsets is supported. Output is
1146 * straightforward. Input will skip whitespace, only accept '0' and '1'
1147 * characters, and will only extract as many digits as the %bitset will
1148 * hold.
1150 template<class _CharT, class _Traits, size_t _Nb>
1151 basic_istream<_CharT, _Traits>&
1152 operator>>(basic_istream<_CharT, _Traits>& __is, bitset<_Nb>& __x)
1154 typedef typename _Traits::char_type char_type;
1155 basic_string<_CharT, _Traits> __tmp;
1156 __tmp.reserve(_Nb);
1158 // Skip whitespace
1159 typename basic_istream<_CharT, _Traits>::sentry __sentry(__is);
1160 if (__sentry)
1162 ios_base::iostate __state = ios_base::goodbit;
1163 basic_streambuf<_CharT, _Traits>* __buf = __is.rdbuf();
1164 for (size_t __i = 0; __i < _Nb; ++__i)
1166 static typename _Traits::int_type __eof = _Traits::eof();
1168 typename _Traits::int_type __c1 = __buf->sbumpc();
1169 if (_Traits::eq_int_type(__c1, __eof))
1171 __state |= ios_base::eofbit;
1172 break;
1174 else
1176 char_type __c2 = _Traits::to_char_type(__c1);
1177 char_type __c = __is.narrow(__c2, '*');
1179 if (__c == '0' || __c == '1')
1180 __tmp.push_back(__c);
1181 else if (_Traits::eq_int_type(__buf->sputbackc(__c2), __eof))
1183 __state |= ios_base::failbit;
1184 break;
1189 if (__tmp.empty() && !_Nb)
1190 __state |= ios_base::failbit;
1191 else
1192 __x._M_copy_from_string(__tmp, static_cast<size_t>(0), _Nb);
1194 if (__state != ios_base::goodbit)
1195 __is.setstate(__state); // may throw an exception
1198 return __is;
1201 template <class _CharT, class _Traits, size_t _Nb>
1202 basic_ostream<_CharT, _Traits>&
1203 operator<<(basic_ostream<_CharT, _Traits>& __os, const bitset<_Nb>& __x)
1205 basic_string<_CharT, _Traits> __tmp;
1206 __x._M_copy_to_string(__tmp);
1207 return __os << __tmp;
1209 //@}
1210 } // namespace std
1212 #undef _GLIBCXX_BITSET_WORDS
1213 #undef _GLIBCXX_BITSET_BITS_PER_WORD
1215 #endif /* _GLIBCXX_BITSET */