1 // <bitset> -*- C++ -*-
3 // Copyright (C) 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
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)
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,
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.
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.
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
57 #include <bits/functexcept.h> // For invalid_argument, out_of_range,
59 #include <ostream> // For ostream (operator<<)
60 #include <istream> // For istream (operator>>)
62 #define _GLIBCXX_BITSET_BITS_PER_WORD numeric_limits<unsigned long>::digits
63 #define _GLIBCXX_BITSET_WORDS(__n) \
64 ((__n) < 1 ? 0 : ((__n) + _GLIBCXX_BITSET_BITS_PER_WORD - 1)/_GLIBCXX_BITSET_BITS_PER_WORD)
66 namespace _GLIBCXX_STD
70 * Base class, general case. It is a class inveriant that _Nw will be
73 * See documentation for bitset.
79 typedef unsigned long _WordT
;
81 /// 0 is the least significant word.
84 _Base_bitset() { _M_do_reset(); }
85 _Base_bitset(unsigned long __val
)
92 _S_whichword(size_t __pos
)
93 { return __pos
/ _GLIBCXX_BITSET_BITS_PER_WORD
; }
96 _S_whichbyte(size_t __pos
)
97 { return (__pos
% _GLIBCXX_BITSET_BITS_PER_WORD
) / __CHAR_BIT__
; }
100 _S_whichbit(size_t __pos
)
101 { return __pos
% _GLIBCXX_BITSET_BITS_PER_WORD
; }
104 _S_maskbit(size_t __pos
)
105 { return (static_cast<_WordT
>(1)) << _S_whichbit(__pos
); }
108 _M_getword(size_t __pos
)
109 { return _M_w
[_S_whichword(__pos
)]; }
112 _M_getword(size_t __pos
) const
113 { return _M_w
[_S_whichword(__pos
)]; }
116 _M_hiword() { return _M_w
[_Nw
- 1]; }
119 _M_hiword() const { return _M_w
[_Nw
- 1]; }
122 _M_do_and(const _Base_bitset
<_Nw
>& __x
)
124 for (size_t __i
= 0; __i
< _Nw
; __i
++)
125 _M_w
[__i
] &= __x
._M_w
[__i
];
129 _M_do_or(const _Base_bitset
<_Nw
>& __x
)
131 for (size_t __i
= 0; __i
< _Nw
; __i
++)
132 _M_w
[__i
] |= __x
._M_w
[__i
];
136 _M_do_xor(const _Base_bitset
<_Nw
>& __x
)
138 for (size_t __i
= 0; __i
< _Nw
; __i
++)
139 _M_w
[__i
] ^= __x
._M_w
[__i
];
143 _M_do_left_shift(size_t __shift
);
146 _M_do_right_shift(size_t __shift
);
151 for (size_t __i
= 0; __i
< _Nw
; __i
++)
152 _M_w
[__i
] = ~_M_w
[__i
];
158 for (size_t __i
= 0; __i
< _Nw
; __i
++)
159 _M_w
[__i
] = ~static_cast<_WordT
>(0);
163 _M_do_reset() { memset(_M_w
, 0, _Nw
* sizeof(_WordT
)); }
166 _M_is_equal(const _Base_bitset
<_Nw
>& __x
) const
168 for (size_t __i
= 0; __i
< _Nw
; ++__i
)
170 if (_M_w
[__i
] != __x
._M_w
[__i
])
179 for (size_t __i
= 0; __i
< _Nw
; __i
++)
181 if (_M_w
[__i
] != static_cast<_WordT
>(0))
191 for (size_t __i
= 0; __i
< _Nw
; __i
++)
192 __result
+= __builtin_popcountl(_M_w
[__i
]);
197 _M_do_to_ulong() const;
199 // find first "on" bit
201 _M_do_find_first(size_t __not_found
) const;
203 // find the next "on" bit that follows "prev"
205 _M_do_find_next(size_t __prev
, size_t __not_found
) const;
208 // Definitions of non-inline functions from _Base_bitset.
211 _Base_bitset
<_Nw
>::_M_do_left_shift(size_t __shift
)
213 if (__builtin_expect(__shift
!= 0, 1))
215 const size_t __wshift
= __shift
/ _GLIBCXX_BITSET_BITS_PER_WORD
;
216 const size_t __offset
= __shift
% _GLIBCXX_BITSET_BITS_PER_WORD
;
219 for (size_t __n
= _Nw
- 1; __n
>= __wshift
; --__n
)
220 _M_w
[__n
] = _M_w
[__n
- __wshift
];
223 const size_t __sub_offset
= _GLIBCXX_BITSET_BITS_PER_WORD
- __offset
;
224 for (size_t __n
= _Nw
- 1; __n
> __wshift
; --__n
)
225 _M_w
[__n
] = (_M_w
[__n
- __wshift
] << __offset
) |
226 (_M_w
[__n
- __wshift
- 1] >> __sub_offset
);
227 _M_w
[__wshift
] = _M_w
[0] << __offset
;
230 std::fill(_M_w
+ 0, _M_w
+ __wshift
, static_cast<_WordT
>(0));
236 _Base_bitset
<_Nw
>::_M_do_right_shift(size_t __shift
)
238 if (__builtin_expect(__shift
!= 0, 1))
240 const size_t __wshift
= __shift
/ _GLIBCXX_BITSET_BITS_PER_WORD
;
241 const size_t __offset
= __shift
% _GLIBCXX_BITSET_BITS_PER_WORD
;
242 const size_t __limit
= _Nw
- __wshift
- 1;
245 for (size_t __n
= 0; __n
<= __limit
; ++__n
)
246 _M_w
[__n
] = _M_w
[__n
+ __wshift
];
249 const size_t __sub_offset
= _GLIBCXX_BITSET_BITS_PER_WORD
- __offset
;
250 for (size_t __n
= 0; __n
< __limit
; ++__n
)
251 _M_w
[__n
] = (_M_w
[__n
+ __wshift
] >> __offset
) |
252 (_M_w
[__n
+ __wshift
+ 1] << __sub_offset
);
253 _M_w
[__limit
] = _M_w
[_Nw
-1] >> __offset
;
256 std::fill(_M_w
+ __limit
+ 1, _M_w
+ _Nw
, static_cast<_WordT
>(0));
262 _Base_bitset
<_Nw
>::_M_do_to_ulong() const
264 for (size_t __i
= 1; __i
< _Nw
; ++__i
)
266 __throw_overflow_error(__N("_Base_bitset::_M_do_to_ulong"));
272 _Base_bitset
<_Nw
>::_M_do_find_first(size_t __not_found
) const
274 for (size_t __i
= 0; __i
< _Nw
; __i
++)
276 _WordT __thisword
= _M_w
[__i
];
277 if (__thisword
!= static_cast<_WordT
>(0))
278 return __i
* _GLIBCXX_BITSET_BITS_PER_WORD
279 + __builtin_ctzl(__thisword
);
281 // not found, so return an indication of failure.
287 _Base_bitset
<_Nw
>::_M_do_find_next(size_t __prev
, size_t __not_found
) const
289 // make bound inclusive
292 // check out of bounds
293 if (__prev
>= _Nw
* _GLIBCXX_BITSET_BITS_PER_WORD
)
297 size_t __i
= _S_whichword(__prev
);
298 _WordT __thisword
= _M_w
[__i
];
300 // mask off bits below bound
301 __thisword
&= (~static_cast<_WordT
>(0)) << _S_whichbit(__prev
);
303 if (__thisword
!= static_cast<_WordT
>(0))
304 return __i
* _GLIBCXX_BITSET_BITS_PER_WORD
305 + __builtin_ctzl(__thisword
);
307 // check subsequent words
309 for ( ; __i
< _Nw
; __i
++ )
311 __thisword
= _M_w
[__i
];
312 if (__thisword
!= static_cast<_WordT
>(0))
313 return __i
* _GLIBCXX_BITSET_BITS_PER_WORD
314 + __builtin_ctzl(__thisword
);
316 // not found, so return an indication of failure.
318 } // end _M_do_find_next
323 * Base class, specialization for a single word.
325 * See documentation for bitset.
329 struct _Base_bitset
<1>
331 typedef unsigned long _WordT
;
334 _Base_bitset( void ) : _M_w(0) {}
335 _Base_bitset(unsigned long __val
) : _M_w(__val
) {}
338 _S_whichword(size_t __pos
)
339 { return __pos
/ _GLIBCXX_BITSET_BITS_PER_WORD
; }
342 _S_whichbyte(size_t __pos
)
343 { return (__pos
% _GLIBCXX_BITSET_BITS_PER_WORD
) / __CHAR_BIT__
; }
346 _S_whichbit(size_t __pos
)
347 { return __pos
% _GLIBCXX_BITSET_BITS_PER_WORD
; }
350 _S_maskbit(size_t __pos
)
351 { return (static_cast<_WordT
>(1)) << _S_whichbit(__pos
); }
354 _M_getword(size_t) { return _M_w
; }
357 _M_getword(size_t) const { return _M_w
; }
360 _M_hiword() { return _M_w
; }
363 _M_hiword() const { return _M_w
; }
366 _M_do_and(const _Base_bitset
<1>& __x
) { _M_w
&= __x
._M_w
; }
369 _M_do_or(const _Base_bitset
<1>& __x
) { _M_w
|= __x
._M_w
; }
372 _M_do_xor(const _Base_bitset
<1>& __x
) { _M_w
^= __x
._M_w
; }
375 _M_do_left_shift(size_t __shift
) { _M_w
<<= __shift
; }
378 _M_do_right_shift(size_t __shift
) { _M_w
>>= __shift
; }
381 _M_do_flip() { _M_w
= ~_M_w
; }
384 _M_do_set() { _M_w
= ~static_cast<_WordT
>(0); }
387 _M_do_reset() { _M_w
= 0; }
390 _M_is_equal(const _Base_bitset
<1>& __x
) const
391 { return _M_w
== __x
._M_w
; }
394 _M_is_any() const { return _M_w
!= 0; }
397 _M_do_count() const { return __builtin_popcountl(_M_w
); }
400 _M_do_to_ulong() const { return _M_w
; }
403 _M_do_find_first(size_t __not_found
) const
406 return __builtin_ctzl(_M_w
);
411 // find the next "on" bit that follows "prev"
413 _M_do_find_next(size_t __prev
, size_t __not_found
) const
416 if (__prev
>= ((size_t) _GLIBCXX_BITSET_BITS_PER_WORD
))
419 _WordT __x
= _M_w
>> __prev
;
421 return __builtin_ctzl(__x
) + __prev
;
430 * Base class, specialization for no storage (zero-length %bitset).
432 * See documentation for bitset.
436 struct _Base_bitset
<0>
438 typedef unsigned long _WordT
;
441 _Base_bitset(unsigned long) {}
444 _S_whichword(size_t __pos
)
445 { return __pos
/ _GLIBCXX_BITSET_BITS_PER_WORD
; }
448 _S_whichbyte(size_t __pos
)
449 { return (__pos
% _GLIBCXX_BITSET_BITS_PER_WORD
) / __CHAR_BIT__
; }
452 _S_whichbit(size_t __pos
)
453 { return __pos
% _GLIBCXX_BITSET_BITS_PER_WORD
; }
456 _S_maskbit(size_t __pos
)
457 { return (static_cast<_WordT
>(1)) << _S_whichbit(__pos
); }
459 // This would normally give access to the data. The bounds-checking
460 // in the bitset class will prevent the user from getting this far,
461 // but (1) it must still return an lvalue to compile, and (2) the
462 // user might call _Unchecked_set directly, in which case this /needs/
463 // to fail. Let's not penalize zero-length users unless they actually
464 // make an unchecked call; all the memory ugliness is therefore
465 // localized to this single should-never-get-this-far function.
467 _M_getword(size_t) const
469 __throw_out_of_range(__N("_Base_bitset::_M_getword"));
474 _M_hiword() const { return 0; }
477 _M_do_and(const _Base_bitset
<0>&) { }
480 _M_do_or(const _Base_bitset
<0>&) { }
483 _M_do_xor(const _Base_bitset
<0>&) { }
486 _M_do_left_shift(size_t) { }
489 _M_do_right_shift(size_t) { }
500 // Are all empty bitsets equal to each other? Are they equal to
501 // themselves? How to compare a thing which has no state? What is
502 // the sound of one zero-length bitset clapping?
504 _M_is_equal(const _Base_bitset
<0>&) const { return true; }
507 _M_is_any() const { return false; }
510 _M_do_count() const { return 0; }
513 _M_do_to_ulong() const { return 0; }
515 // Normally "not found" is the size, but that could also be
516 // misinterpreted as an index in this corner case. Oh well.
518 _M_do_find_first(size_t) const { return 0; }
521 _M_do_find_next(size_t, size_t) const { return 0; }
525 // Helper class to zero out the unused high-order bits in the highest word.
526 template<size_t _Extrabits
>
529 static void _S_do_sanitize(unsigned long& __val
)
530 { __val
&= ~((~static_cast<unsigned long>(0)) << _Extrabits
); }
535 { static void _S_do_sanitize(unsigned long) { } };
539 * @brief The %bitset class represents a @e fixed-size sequence of bits.
541 * @ingroup Containers
543 * (Note that %bitset does @e not meet the formal requirements of a
544 * <a href="tables.html#65">container</a>. Mainly, it lacks iterators.)
546 * The template argument, @a Nb, may be any non-negative number,
547 * specifying the number of bits (e.g., "0", "12", "1024*1024").
549 * In the general unoptimized case, storage is allocated in word-sized
550 * blocks. Let B be the number of bits in a word, then (Nb+(B-1))/B
551 * words will be used for storage. B - Nb%B bits are unused. (They are
552 * the high-order bits in the highest word.) It is a class invariant
553 * that those unused bits are always zero.
555 * If you think of %bitset as "a simple array of bits," be aware that
556 * your mental picture is reversed: a %bitset behaves the same way as
557 * bits in integers do, with the bit at index 0 in the "least significant
558 * / right-hand" position, and the bit at index Nb-1 in the "most
559 * significant / left-hand" position. Thus, unlike other containers, a
560 * %bitset's index "counts from right to left," to put it very loosely.
562 * This behavior is preserved when translating to and from strings. For
563 * example, the first line of the following program probably prints
564 * "b('a') is 0001100001" on a modern ASCII system.
568 * #include <iostream>
571 * using namespace std;
578 * cout << "b('a') is " << b << endl;
582 * string str = s.str();
583 * cout << "index 3 in the string is " << str[3] << " but\n"
584 * << "index 3 in the bitset is " << b[3] << endl;
588 * Also see http://gcc.gnu.org/onlinedocs/libstdc++/ext/sgiexts.html#ch23
589 * for a description of extensions.
592 * Most of the actual code isn't contained in %bitset<> itself, but in the
593 * base class _Base_bitset. The base class works with whole words, not with
594 * individual bits. This allows us to specialize _Base_bitset for the
595 * important special case where the %bitset is only a single word.
597 * Extra confusion can result due to the fact that the storage for
598 * _Base_bitset @e is a regular array, and is indexed as such. This is
599 * carefully encapsulated.
603 class bitset
: private _Base_bitset
<_GLIBCXX_BITSET_WORDS(_Nb
)>
606 typedef _Base_bitset
<_GLIBCXX_BITSET_WORDS(_Nb
)> _Base
;
607 typedef unsigned long _WordT
;
612 _Sanitize
<_Nb
%_GLIBCXX_BITSET_BITS_PER_WORD
>::
613 _S_do_sanitize(this->_M_hiword());
618 * This encapsulates the concept of a single bit. An instance of this
619 * class is a proxy for an actual bit; this way the individual bit
620 * operations are done as faster word-size bitwise instructions.
622 * Most users will never need to use this class directly; conversions
623 * to and from bool are automatic and should be transparent. Overloaded
624 * operators help to preserve the illusion.
626 * (On a typical system, this "bit %reference" is 64 times the size of
627 * an actual bit. Ha.)
640 reference(bitset
& __b
, size_t __pos
)
642 _M_wp
= &__b
._M_getword(__pos
);
643 _M_bpos
= _Base::_S_whichbit(__pos
);
653 *_M_wp
|= _Base::_S_maskbit(_M_bpos
);
655 *_M_wp
&= ~_Base::_S_maskbit(_M_bpos
);
659 // For b[i] = b[__j];
661 operator=(const reference
& __j
)
663 if ( (*(__j
._M_wp
) & _Base::_S_maskbit(__j
._M_bpos
)) )
664 *_M_wp
|= _Base::_S_maskbit(_M_bpos
);
666 *_M_wp
&= ~_Base::_S_maskbit(_M_bpos
);
673 { return (*(_M_wp
) & _Base::_S_maskbit(_M_bpos
)) == 0; }
676 operator bool() const
677 { return (*(_M_wp
) & _Base::_S_maskbit(_M_bpos
)) != 0; }
683 *_M_wp
^= _Base::_S_maskbit(_M_bpos
);
687 friend class reference
;
689 // 23.3.5.1 constructors:
690 /// All bits set to zero.
693 /// Initial bits bitwise-copied from a single word (others set to zero).
694 bitset(unsigned long __val
) : _Base(__val
)
695 { _M_do_sanitize(); }
698 * @brief Use a subset of a string.
699 * @param s A string of '0' and '1' characters.
700 * @param position Index of the first character in @a s to use; defaults
702 * @throw std::out_of_range If @a pos is bigger the size of @a s.
703 * @throw std::invalid_argument If a character appears in the string
704 * which is neither '0' nor '1'.
706 template<class _CharT
, class _Traits
, class _Alloc
>
707 explicit bitset(const basic_string
<_CharT
, _Traits
, _Alloc
>& __s
,
708 size_t __position
= 0) : _Base()
710 if (__position
> __s
.size())
711 __throw_out_of_range(__N("bitset::bitset initial position "
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(__N("bitset::bitset initial position "
733 _M_copy_from_string(__s
, __position
, __n
);
736 // 23.3.5.2 bitset operations:
739 * @brief Operations on bitsets.
740 * @param rhs A same-sized bitset.
742 * These should be self-explanatory.
745 operator&=(const bitset
<_Nb
>& __rhs
)
747 this->_M_do_and(__rhs
);
752 operator|=(const bitset
<_Nb
>& __rhs
)
754 this->_M_do_or(__rhs
);
759 operator^=(const bitset
<_Nb
>& __rhs
)
761 this->_M_do_xor(__rhs
);
768 * @brief Operations on bitsets.
769 * @param position The number of places to shift.
771 * These should be self-explanatory.
774 operator<<=(size_t __position
)
776 if (__builtin_expect(__position
< _Nb
, 1))
778 this->_M_do_left_shift(__position
);
779 this->_M_do_sanitize();
787 operator>>=(size_t __position
)
789 if (__builtin_expect(__position
< _Nb
, 1))
791 this->_M_do_right_shift(__position
);
792 this->_M_do_sanitize();
802 * These versions of single-bit set, reset, flip, and test are
803 * extensions from the SGI version. They do no range checking.
804 * @ingroup SGIextensions
807 _Unchecked_set(size_t __pos
)
809 this->_M_getword(__pos
) |= _Base::_S_maskbit(__pos
);
814 _Unchecked_set(size_t __pos
, int __val
)
817 this->_M_getword(__pos
) |= _Base::_S_maskbit(__pos
);
819 this->_M_getword(__pos
) &= ~_Base::_S_maskbit(__pos
);
824 _Unchecked_reset(size_t __pos
)
826 this->_M_getword(__pos
) &= ~_Base::_S_maskbit(__pos
);
831 _Unchecked_flip(size_t __pos
)
833 this->_M_getword(__pos
) ^= _Base::_S_maskbit(__pos
);
838 _Unchecked_test(size_t __pos
) const
840 return (this->_M_getword(__pos
) & _Base::_S_maskbit(__pos
))
841 != static_cast<_WordT
>(0);
845 // Set, reset, and flip.
847 * @brief Sets every bit to true.
853 this->_M_do_sanitize();
858 * @brief Sets a given bit to a particular value.
859 * @param position The index of the bit.
860 * @param val Either true or false, defaults to true.
861 * @throw std::out_of_range If @a pos is bigger the size of the %set.
864 set(size_t __position
, bool __val
= true)
866 if (__position
>= _Nb
)
867 __throw_out_of_range(__N("bitset::set"));
868 return _Unchecked_set(__position
, __val
);
872 * @brief Sets every bit to false.
882 * @brief Sets a given bit to false.
883 * @param position The index of the bit.
884 * @throw std::out_of_range If @a pos is bigger the size of the %set.
886 * Same as writing @c set(pos,false).
889 reset(size_t __position
)
891 if (__position
>= _Nb
)
892 __throw_out_of_range(__N("bitset::reset"));
893 return _Unchecked_reset(__position
);
897 * @brief Toggles every bit to its opposite value.
903 this->_M_do_sanitize();
908 * @brief Toggles a given bit to its opposite value.
909 * @param position The index of the bit.
910 * @throw std::out_of_range If @a pos is bigger the size of the %set.
913 flip(size_t __position
)
915 if (__position
>= _Nb
)
916 __throw_out_of_range(__N("bitset::flip"));
917 return _Unchecked_flip(__position
);
920 /// See the no-argument flip().
922 operator~() const { return bitset
<_Nb
>(*this).flip(); }
926 * @brief Array-indexing support.
927 * @param position Index into the %bitset.
928 * @return A bool for a 'const %bitset'. For non-const bitsets, an
929 * instance of the reference proxy class.
930 * @note These operators do no range checking and throw no exceptions,
931 * as required by DR 11 to the standard.
934 * _GLIBCXX_RESOLVE_LIB_DEFECTS Note that this implementation already
935 * resolves DR 11 (items 1 and 2), but does not do the range-checking
936 * required by that DR's resolution. -pme
937 * The DR has since been changed: range-checking is a precondition
938 * (users' responsibility), and these functions must not throw. -pme
942 operator[](size_t __position
) { return reference(*this,__position
); }
945 operator[](size_t __position
) const { return _Unchecked_test(__position
); }
949 * @brief Retuns a numerical interpretation of the %bitset.
950 * @return The integral equivalent of the bits.
951 * @throw std::overflow_error If there are too many bits to be
952 * represented in an @c unsigned @c long.
955 to_ulong() const { return this->_M_do_to_ulong(); }
958 * @brief Retuns a character interpretation of the %bitset.
959 * @return The string equivalent of the bits.
961 * Note the ordering of the bits: decreasing character positions
962 * correspond to increasing bit positions (see the main class notes for
965 * Also note that you must specify the string's template parameters
966 * explicitly. Given a bitset @c bs and a string @s:
968 * s = bs.to_string<char,char_traits<char>,allocator<char> >();
971 template<class _CharT
, class _Traits
, class _Alloc
>
972 basic_string
<_CharT
, _Traits
, _Alloc
>
975 basic_string
<_CharT
, _Traits
, _Alloc
> __result
;
976 _M_copy_to_string(__result
);
980 // Helper functions for string operations.
981 template<class _CharT
, class _Traits
, class _Alloc
>
983 _M_copy_from_string(const basic_string
<_CharT
,_Traits
,_Alloc
>& __s
,
986 template<class _CharT
, class _Traits
, class _Alloc
>
988 _M_copy_to_string(basic_string
<_CharT
,_Traits
,_Alloc
>&) const;
990 /// Returns the number of bits which are set.
992 count() const { return this->_M_do_count(); }
994 /// Returns the total number of bits.
996 size() const { return _Nb
; }
999 /// These comparisons for equality/inequality are, well, @e bitwise.
1001 operator==(const bitset
<_Nb
>& __rhs
) const
1002 { return this->_M_is_equal(__rhs
); }
1005 operator!=(const bitset
<_Nb
>& __rhs
) const
1006 { return !this->_M_is_equal(__rhs
); }
1010 * @brief Tests the value of a bit.
1011 * @param position The index of a bit.
1012 * @return The value at @a pos.
1013 * @throw std::out_of_range If @a pos is bigger the size of the %set.
1016 test(size_t __position
) const
1018 if (__position
>= _Nb
)
1019 __throw_out_of_range(__N("bitset::test"));
1020 return _Unchecked_test(__position
);
1024 * @brief Tests whether any of the bits are on.
1025 * @return True if at least one bit is set.
1028 any() const { return this->_M_is_any(); }
1031 * @brief Tests whether any of the bits are on.
1032 * @return True if none of the bits are set.
1035 none() const { return !this->_M_is_any(); }
1038 /// Self-explanatory.
1040 operator<<(size_t __position
) const
1041 { return bitset
<_Nb
>(*this) <<= __position
; }
1044 operator>>(size_t __position
) const
1045 { return bitset
<_Nb
>(*this) >>= __position
; }
1049 * @brief Finds the index of the first "on" bit.
1050 * @return The index of the first bit set, or size() if not found.
1051 * @ingroup SGIextensions
1056 { return this->_M_do_find_first(_Nb
); }
1059 * @brief Finds the index of the next "on" bit after prev.
1060 * @return The index of the next bit set, or size() if not found.
1061 * @param prev Where to start searching.
1062 * @ingroup SGIextensions
1066 _Find_next(size_t __prev
) const
1067 { return this->_M_do_find_next(__prev
, _Nb
); }
1070 // Definitions of non-inline member functions.
1071 template<size_t _Nb
>
1072 template<class _CharT
, class _Traits
, class _Alloc
>
1074 bitset
<_Nb
>::_M_copy_from_string(const basic_string
<_CharT
, _Traits
,
1075 _Alloc
>& __s
, size_t __pos
, size_t __n
)
1078 const size_t __nbits
= std::min(_Nb
, std::min(__n
, __s
.size() - __pos
));
1079 for (size_t __i
= 0; __i
< __nbits
; ++__i
)
1081 switch(__s
[__pos
+ __nbits
- __i
- 1])
1089 __throw_invalid_argument(__N("bitset::_M_copy_from_string"));
1094 template<size_t _Nb
>
1095 template<class _CharT
, class _Traits
, class _Alloc
>
1097 bitset
<_Nb
>::_M_copy_to_string(basic_string
<_CharT
, _Traits
,
1100 __s
.assign(_Nb
, '0');
1101 for (size_t __i
= 0; __i
< _Nb
; ++__i
)
1102 if (_Unchecked_test(__i
))
1103 __s
[_Nb
- 1 - __i
] = '1';
1106 // 23.3.5.3 bitset operations:
1109 * @brief Global bitwise operations on bitsets.
1110 * @param x A bitset.
1111 * @param y A bitset of the same size as @a x.
1112 * @return A new bitset.
1114 * These should be self-explanatory.
1116 template<size_t _Nb
>
1118 operator&(const bitset
<_Nb
>& __x
, const bitset
<_Nb
>& __y
)
1120 bitset
<_Nb
> __result(__x
);
1125 template<size_t _Nb
>
1127 operator|(const bitset
<_Nb
>& __x
, const bitset
<_Nb
>& __y
)
1129 bitset
<_Nb
> __result(__x
);
1134 template <size_t _Nb
>
1136 operator^(const bitset
<_Nb
>& __x
, const bitset
<_Nb
>& __y
)
1138 bitset
<_Nb
> __result(__x
);
1146 * @brief Global I/O operators for bitsets.
1148 * Direct I/O between streams and bitsets is supported. Output is
1149 * straightforward. Input will skip whitespace, only accept '0' and '1'
1150 * characters, and will only extract as many digits as the %bitset will
1153 template<class _CharT
, class _Traits
, size_t _Nb
>
1154 basic_istream
<_CharT
, _Traits
>&
1155 operator>>(basic_istream
<_CharT
, _Traits
>& __is
, bitset
<_Nb
>& __x
)
1157 typedef typename
_Traits::char_type char_type
;
1158 basic_string
<_CharT
, _Traits
> __tmp
;
1161 ios_base::iostate __state
= ios_base::goodbit
;
1162 typename basic_istream
<_CharT
, _Traits
>::sentry
__sentry(__is
);
1167 basic_streambuf
<_CharT
, _Traits
>* __buf
= __is
.rdbuf();
1168 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1169 // 303. Bitset input operator underspecified
1170 const char_type __zero
= __is
.widen('0');
1171 const char_type __one
= __is
.widen('1');
1172 for (size_t __i
= 0; __i
< _Nb
; ++__i
)
1174 static typename
_Traits::int_type __eof
= _Traits::eof();
1176 typename
_Traits::int_type __c1
= __buf
->sbumpc();
1177 if (_Traits::eq_int_type(__c1
, __eof
))
1179 __state
|= ios_base::eofbit
;
1184 char_type __c2
= _Traits::to_char_type(__c1
);
1186 __tmp
.push_back('0');
1187 else if (__c2
== __one
)
1188 __tmp
.push_back('1');
1189 else if (_Traits::eq_int_type(__buf
->sputbackc(__c2
),
1192 __state
|= ios_base::failbit
;
1199 { __is
._M_setstate(ios_base::badbit
); }
1202 if (__tmp
.empty() && _Nb
)
1203 __state
|= ios_base::failbit
;
1205 __x
._M_copy_from_string(__tmp
, static_cast<size_t>(0), _Nb
);
1207 __is
.setstate(__state
);
1211 template <class _CharT
, class _Traits
, size_t _Nb
>
1212 basic_ostream
<_CharT
, _Traits
>&
1213 operator<<(basic_ostream
<_CharT
, _Traits
>& __os
, const bitset
<_Nb
>& __x
)
1215 basic_string
<_CharT
, _Traits
> __tmp
;
1216 __x
._M_copy_to_string(__tmp
);
1217 return __os
<< __tmp
;
1222 #undef _GLIBCXX_BITSET_WORDS
1223 #undef _GLIBCXX_BITSET_BITS_PER_WORD
1225 #ifdef _GLIBCXX_DEBUG
1226 # include <debug/bitset>
1229 #endif /* _GLIBCXX_BITSET */