1 // <bitset> -*- C++ -*-
3 // Copyright (C) 2001, 2002, 2003, 2004, 2005 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, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
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.
47 #ifndef _GLIBCXX_BITSET
48 #define _GLIBCXX_BITSET 1
50 #pragma GCC system_header
52 #include <cstddef> // For size_t
53 #include <cstring> // For memset
54 #include <limits> // For numeric_limits
56 #include <bits/functexcept.h> // For invalid_argument, out_of_range,
58 #include <ostream> // For ostream (operator<<)
59 #include <istream> // For istream (operator>>)
61 #define _GLIBCXX_BITSET_BITS_PER_WORD numeric_limits<unsigned long>::digits
62 #define _GLIBCXX_BITSET_WORDS(__n) \
63 ((__n) < 1 ? 0 : ((__n) + _GLIBCXX_BITSET_BITS_PER_WORD - 1) \
64 / _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.
87 _Base_bitset(unsigned long __val
)
94 _S_whichword(size_t __pos
)
95 { return __pos
/ _GLIBCXX_BITSET_BITS_PER_WORD
; }
98 _S_whichbyte(size_t __pos
)
99 { return (__pos
% _GLIBCXX_BITSET_BITS_PER_WORD
) / __CHAR_BIT__
; }
102 _S_whichbit(size_t __pos
)
103 { return __pos
% _GLIBCXX_BITSET_BITS_PER_WORD
; }
106 _S_maskbit(size_t __pos
)
107 { return (static_cast<_WordT
>(1)) << _S_whichbit(__pos
); }
110 _M_getword(size_t __pos
)
111 { return _M_w
[_S_whichword(__pos
)]; }
114 _M_getword(size_t __pos
) const
115 { return _M_w
[_S_whichword(__pos
)]; }
119 { return _M_w
[_Nw
- 1]; }
123 { return _M_w
[_Nw
- 1]; }
126 _M_do_and(const _Base_bitset
<_Nw
>& __x
)
128 for (size_t __i
= 0; __i
< _Nw
; __i
++)
129 _M_w
[__i
] &= __x
._M_w
[__i
];
133 _M_do_or(const _Base_bitset
<_Nw
>& __x
)
135 for (size_t __i
= 0; __i
< _Nw
; __i
++)
136 _M_w
[__i
] |= __x
._M_w
[__i
];
140 _M_do_xor(const _Base_bitset
<_Nw
>& __x
)
142 for (size_t __i
= 0; __i
< _Nw
; __i
++)
143 _M_w
[__i
] ^= __x
._M_w
[__i
];
147 _M_do_left_shift(size_t __shift
);
150 _M_do_right_shift(size_t __shift
);
155 for (size_t __i
= 0; __i
< _Nw
; __i
++)
156 _M_w
[__i
] = ~_M_w
[__i
];
162 for (size_t __i
= 0; __i
< _Nw
; __i
++)
163 _M_w
[__i
] = ~static_cast<_WordT
>(0);
168 { std::memset(_M_w
, 0, _Nw
* sizeof(_WordT
)); }
171 _M_is_equal(const _Base_bitset
<_Nw
>& __x
) const
173 for (size_t __i
= 0; __i
< _Nw
; ++__i
)
175 if (_M_w
[__i
] != __x
._M_w
[__i
])
184 for (size_t __i
= 0; __i
< _Nw
; __i
++)
186 if (_M_w
[__i
] != static_cast<_WordT
>(0))
196 for (size_t __i
= 0; __i
< _Nw
; __i
++)
197 __result
+= __builtin_popcountl(_M_w
[__i
]);
202 _M_do_to_ulong() const;
204 // find first "on" bit
206 _M_do_find_first(size_t __not_found
) const;
208 // find the next "on" bit that follows "prev"
210 _M_do_find_next(size_t __prev
, size_t __not_found
) const;
213 // Definitions of non-inline functions from _Base_bitset.
216 _Base_bitset
<_Nw
>::_M_do_left_shift(size_t __shift
)
218 if (__builtin_expect(__shift
!= 0, 1))
220 const size_t __wshift
= __shift
/ _GLIBCXX_BITSET_BITS_PER_WORD
;
221 const size_t __offset
= __shift
% _GLIBCXX_BITSET_BITS_PER_WORD
;
224 for (size_t __n
= _Nw
- 1; __n
>= __wshift
; --__n
)
225 _M_w
[__n
] = _M_w
[__n
- __wshift
];
228 const size_t __sub_offset
= (_GLIBCXX_BITSET_BITS_PER_WORD
230 for (size_t __n
= _Nw
- 1; __n
> __wshift
; --__n
)
231 _M_w
[__n
] = ((_M_w
[__n
- __wshift
] << __offset
)
232 | (_M_w
[__n
- __wshift
- 1] >> __sub_offset
));
233 _M_w
[__wshift
] = _M_w
[0] << __offset
;
236 std::fill(_M_w
+ 0, _M_w
+ __wshift
, static_cast<_WordT
>(0));
242 _Base_bitset
<_Nw
>::_M_do_right_shift(size_t __shift
)
244 if (__builtin_expect(__shift
!= 0, 1))
246 const size_t __wshift
= __shift
/ _GLIBCXX_BITSET_BITS_PER_WORD
;
247 const size_t __offset
= __shift
% _GLIBCXX_BITSET_BITS_PER_WORD
;
248 const size_t __limit
= _Nw
- __wshift
- 1;
251 for (size_t __n
= 0; __n
<= __limit
; ++__n
)
252 _M_w
[__n
] = _M_w
[__n
+ __wshift
];
255 const size_t __sub_offset
= (_GLIBCXX_BITSET_BITS_PER_WORD
257 for (size_t __n
= 0; __n
< __limit
; ++__n
)
258 _M_w
[__n
] = ((_M_w
[__n
+ __wshift
] >> __offset
)
259 | (_M_w
[__n
+ __wshift
+ 1] << __sub_offset
));
260 _M_w
[__limit
] = _M_w
[_Nw
-1] >> __offset
;
263 std::fill(_M_w
+ __limit
+ 1, _M_w
+ _Nw
, static_cast<_WordT
>(0));
269 _Base_bitset
<_Nw
>::_M_do_to_ulong() const
271 for (size_t __i
= 1; __i
< _Nw
; ++__i
)
273 __throw_overflow_error(__N("_Base_bitset::_M_do_to_ulong"));
279 _Base_bitset
<_Nw
>::_M_do_find_first(size_t __not_found
) const
281 for (size_t __i
= 0; __i
< _Nw
; __i
++)
283 _WordT __thisword
= _M_w
[__i
];
284 if (__thisword
!= static_cast<_WordT
>(0))
285 return (__i
* _GLIBCXX_BITSET_BITS_PER_WORD
286 + __builtin_ctzl(__thisword
));
288 // not found, so return an indication of failure.
294 _Base_bitset
<_Nw
>::_M_do_find_next(size_t __prev
, size_t __not_found
) const
296 // make bound inclusive
299 // check out of bounds
300 if (__prev
>= _Nw
* _GLIBCXX_BITSET_BITS_PER_WORD
)
304 size_t __i
= _S_whichword(__prev
);
305 _WordT __thisword
= _M_w
[__i
];
307 // mask off bits below bound
308 __thisword
&= (~static_cast<_WordT
>(0)) << _S_whichbit(__prev
);
310 if (__thisword
!= static_cast<_WordT
>(0))
311 return (__i
* _GLIBCXX_BITSET_BITS_PER_WORD
312 + __builtin_ctzl(__thisword
));
314 // check subsequent words
316 for (; __i
< _Nw
; __i
++)
318 __thisword
= _M_w
[__i
];
319 if (__thisword
!= static_cast<_WordT
>(0))
320 return (__i
* _GLIBCXX_BITSET_BITS_PER_WORD
321 + __builtin_ctzl(__thisword
));
323 // not found, so return an indication of failure.
325 } // end _M_do_find_next
329 * Base class, specialization for a single word.
331 * See documentation for bitset.
335 struct _Base_bitset
<1>
337 typedef unsigned long _WordT
;
344 _Base_bitset(unsigned long __val
)
349 _S_whichword(size_t __pos
)
350 { return __pos
/ _GLIBCXX_BITSET_BITS_PER_WORD
; }
353 _S_whichbyte(size_t __pos
)
354 { return (__pos
% _GLIBCXX_BITSET_BITS_PER_WORD
) / __CHAR_BIT__
; }
357 _S_whichbit(size_t __pos
)
358 { return __pos
% _GLIBCXX_BITSET_BITS_PER_WORD
; }
361 _S_maskbit(size_t __pos
)
362 { return (static_cast<_WordT
>(1)) << _S_whichbit(__pos
); }
369 _M_getword(size_t) const
381 _M_do_and(const _Base_bitset
<1>& __x
)
382 { _M_w
&= __x
._M_w
; }
385 _M_do_or(const _Base_bitset
<1>& __x
)
386 { _M_w
|= __x
._M_w
; }
389 _M_do_xor(const _Base_bitset
<1>& __x
)
390 { _M_w
^= __x
._M_w
; }
393 _M_do_left_shift(size_t __shift
)
394 { _M_w
<<= __shift
; }
397 _M_do_right_shift(size_t __shift
)
398 { _M_w
>>= __shift
; }
406 { _M_w
= ~static_cast<_WordT
>(0); }
413 _M_is_equal(const _Base_bitset
<1>& __x
) const
414 { return _M_w
== __x
._M_w
; }
418 { return _M_w
!= 0; }
422 { return __builtin_popcountl(_M_w
); }
425 _M_do_to_ulong() const
429 _M_do_find_first(size_t __not_found
) const
432 return __builtin_ctzl(_M_w
);
437 // find the next "on" bit that follows "prev"
439 _M_do_find_next(size_t __prev
, size_t __not_found
) const
442 if (__prev
>= ((size_t) _GLIBCXX_BITSET_BITS_PER_WORD
))
445 _WordT __x
= _M_w
>> __prev
;
447 return __builtin_ctzl(__x
) + __prev
;
455 * Base class, specialization for no storage (zero-length %bitset).
457 * See documentation for bitset.
461 struct _Base_bitset
<0>
463 typedef unsigned long _WordT
;
468 _Base_bitset(unsigned long)
472 _S_whichword(size_t __pos
)
473 { return __pos
/ _GLIBCXX_BITSET_BITS_PER_WORD
; }
476 _S_whichbyte(size_t __pos
)
477 { return (__pos
% _GLIBCXX_BITSET_BITS_PER_WORD
) / __CHAR_BIT__
; }
480 _S_whichbit(size_t __pos
)
481 { return __pos
% _GLIBCXX_BITSET_BITS_PER_WORD
; }
484 _S_maskbit(size_t __pos
)
485 { return (static_cast<_WordT
>(1)) << _S_whichbit(__pos
); }
487 // This would normally give access to the data. The bounds-checking
488 // in the bitset class will prevent the user from getting this far,
489 // but (1) it must still return an lvalue to compile, and (2) the
490 // user might call _Unchecked_set directly, in which case this /needs/
491 // to fail. Let's not penalize zero-length users unless they actually
492 // make an unchecked call; all the memory ugliness is therefore
493 // localized to this single should-never-get-this-far function.
495 _M_getword(size_t) const
497 __throw_out_of_range(__N("_Base_bitset::_M_getword"));
506 _M_do_and(const _Base_bitset
<0>&)
510 _M_do_or(const _Base_bitset
<0>&)
514 _M_do_xor(const _Base_bitset
<0>&)
518 _M_do_left_shift(size_t)
522 _M_do_right_shift(size_t)
537 // Are all empty bitsets equal to each other? Are they equal to
538 // themselves? How to compare a thing which has no state? What is
539 // the sound of one zero-length bitset clapping?
541 _M_is_equal(const _Base_bitset
<0>&) const
553 _M_do_to_ulong() const
556 // Normally "not found" is the size, but that could also be
557 // misinterpreted as an index in this corner case. Oh well.
559 _M_do_find_first(size_t) const
563 _M_do_find_next(size_t, size_t) const
568 // Helper class to zero out the unused high-order bits in the highest word.
569 template<size_t _Extrabits
>
572 static void _S_do_sanitize(unsigned long& __val
)
573 { __val
&= ~((~static_cast<unsigned long>(0)) << _Extrabits
); }
578 { static void _S_do_sanitize(unsigned long) {} };
581 * @brief The %bitset class represents a @e fixed-size sequence of bits.
583 * @ingroup Containers
585 * (Note that %bitset does @e not meet the formal requirements of a
586 * <a href="tables.html#65">container</a>. Mainly, it lacks iterators.)
588 * The template argument, @a Nb, may be any non-negative number,
589 * specifying the number of bits (e.g., "0", "12", "1024*1024").
591 * In the general unoptimized case, storage is allocated in word-sized
592 * blocks. Let B be the number of bits in a word, then (Nb+(B-1))/B
593 * words will be used for storage. B - Nb%B bits are unused. (They are
594 * the high-order bits in the highest word.) It is a class invariant
595 * that those unused bits are always zero.
597 * If you think of %bitset as "a simple array of bits," be aware that
598 * your mental picture is reversed: a %bitset behaves the same way as
599 * bits in integers do, with the bit at index 0 in the "least significant
600 * / right-hand" position, and the bit at index Nb-1 in the "most
601 * significant / left-hand" position. Thus, unlike other containers, a
602 * %bitset's index "counts from right to left," to put it very loosely.
604 * This behavior is preserved when translating to and from strings. For
605 * example, the first line of the following program probably prints
606 * "b('a') is 0001100001" on a modern ASCII system.
610 * #include <iostream>
613 * using namespace std;
620 * cout << "b('a') is " << b << endl;
624 * string str = s.str();
625 * cout << "index 3 in the string is " << str[3] << " but\n"
626 * << "index 3 in the bitset is " << b[3] << endl;
630 * Also see http://gcc.gnu.org/onlinedocs/libstdc++/ext/sgiexts.html#ch23
631 * for a description of extensions.
634 * Most of the actual code isn't contained in %bitset<> itself, but in the
635 * base class _Base_bitset. The base class works with whole words, not with
636 * individual bits. This allows us to specialize _Base_bitset for the
637 * important special case where the %bitset is only a single word.
639 * Extra confusion can result due to the fact that the storage for
640 * _Base_bitset @e is a regular array, and is indexed as such. This is
641 * carefully encapsulated.
646 : private _Base_bitset
<_GLIBCXX_BITSET_WORDS(_Nb
)>
649 typedef _Base_bitset
<_GLIBCXX_BITSET_WORDS(_Nb
)> _Base
;
650 typedef unsigned long _WordT
;
655 _Sanitize
<_Nb
% _GLIBCXX_BITSET_BITS_PER_WORD
>::
656 _S_do_sanitize(this->_M_hiword());
661 * This encapsulates the concept of a single bit. An instance of this
662 * class is a proxy for an actual bit; this way the individual bit
663 * operations are done as faster word-size bitwise instructions.
665 * Most users will never need to use this class directly; conversions
666 * to and from bool are automatic and should be transparent. Overloaded
667 * operators help to preserve the illusion.
669 * (On a typical system, this "bit %reference" is 64 times the size of
670 * an actual bit. Ha.)
683 reference(bitset
& __b
, size_t __pos
)
685 _M_wp
= &__b
._M_getword(__pos
);
686 _M_bpos
= _Base::_S_whichbit(__pos
);
697 *_M_wp
|= _Base::_S_maskbit(_M_bpos
);
699 *_M_wp
&= ~_Base::_S_maskbit(_M_bpos
);
703 // For b[i] = b[__j];
705 operator=(const reference
& __j
)
707 if ((*(__j
._M_wp
) & _Base::_S_maskbit(__j
._M_bpos
)))
708 *_M_wp
|= _Base::_S_maskbit(_M_bpos
);
710 *_M_wp
&= ~_Base::_S_maskbit(_M_bpos
);
717 { return (*(_M_wp
) & _Base::_S_maskbit(_M_bpos
)) == 0; }
720 operator bool() const
721 { return (*(_M_wp
) & _Base::_S_maskbit(_M_bpos
)) != 0; }
727 *_M_wp
^= _Base::_S_maskbit(_M_bpos
);
731 friend class reference
;
733 // 23.3.5.1 constructors:
734 /// All bits set to zero.
738 /// Initial bits bitwise-copied from a single word (others set to zero).
739 bitset(unsigned long __val
)
741 { _M_do_sanitize(); }
744 * @brief Use a subset of a string.
745 * @param s A string of '0' and '1' characters.
746 * @param position Index of the first character in @a s to use;
748 * @throw std::out_of_range If @a pos is bigger the size of @a s.
749 * @throw std::invalid_argument If a character appears in the string
750 * which is neither '0' nor '1'.
752 template<class _CharT
, class _Traits
, class _Alloc
>
754 bitset(const std::basic_string
<_CharT
, _Traits
, _Alloc
>& __s
,
755 size_t __position
= 0)
758 if (__position
> __s
.size())
759 __throw_out_of_range(__N("bitset::bitset initial position "
761 _M_copy_from_string(__s
, __position
,
762 std::basic_string
<_CharT
, _Traits
, _Alloc
>::npos
);
766 * @brief Use a subset of a string.
767 * @param s A string of '0' and '1' characters.
768 * @param position Index of the first character in @a s to use.
769 * @param n The number of characters to copy.
770 * @throw std::out_of_range If @a pos is bigger the size of @a s.
771 * @throw std::invalid_argument If a character appears in the string
772 * which is neither '0' nor '1'.
774 template<class _CharT
, class _Traits
, class _Alloc
>
775 bitset(const std::basic_string
<_CharT
, _Traits
, _Alloc
>& __s
,
776 size_t __position
, size_t __n
)
779 if (__position
> __s
.size())
780 __throw_out_of_range(__N("bitset::bitset initial position "
782 _M_copy_from_string(__s
, __position
, __n
);
785 // 23.3.5.2 bitset operations:
788 * @brief Operations on bitsets.
789 * @param rhs A same-sized bitset.
791 * These should be self-explanatory.
794 operator&=(const bitset
<_Nb
>& __rhs
)
796 this->_M_do_and(__rhs
);
801 operator|=(const bitset
<_Nb
>& __rhs
)
803 this->_M_do_or(__rhs
);
808 operator^=(const bitset
<_Nb
>& __rhs
)
810 this->_M_do_xor(__rhs
);
817 * @brief Operations on bitsets.
818 * @param position The number of places to shift.
820 * These should be self-explanatory.
823 operator<<=(size_t __position
)
825 if (__builtin_expect(__position
< _Nb
, 1))
827 this->_M_do_left_shift(__position
);
828 this->_M_do_sanitize();
836 operator>>=(size_t __position
)
838 if (__builtin_expect(__position
< _Nb
, 1))
840 this->_M_do_right_shift(__position
);
841 this->_M_do_sanitize();
851 * These versions of single-bit set, reset, flip, and test are
852 * extensions from the SGI version. They do no range checking.
853 * @ingroup SGIextensions
856 _Unchecked_set(size_t __pos
)
858 this->_M_getword(__pos
) |= _Base::_S_maskbit(__pos
);
863 _Unchecked_set(size_t __pos
, int __val
)
866 this->_M_getword(__pos
) |= _Base::_S_maskbit(__pos
);
868 this->_M_getword(__pos
) &= ~_Base::_S_maskbit(__pos
);
873 _Unchecked_reset(size_t __pos
)
875 this->_M_getword(__pos
) &= ~_Base::_S_maskbit(__pos
);
880 _Unchecked_flip(size_t __pos
)
882 this->_M_getword(__pos
) ^= _Base::_S_maskbit(__pos
);
887 _Unchecked_test(size_t __pos
) const
888 { return ((this->_M_getword(__pos
) & _Base::_S_maskbit(__pos
))
889 != static_cast<_WordT
>(0)); }
892 // Set, reset, and flip.
894 * @brief Sets every bit to true.
900 this->_M_do_sanitize();
905 * @brief Sets a given bit to a particular value.
906 * @param position The index of the bit.
907 * @param val Either true or false, defaults to true.
908 * @throw std::out_of_range If @a pos is bigger the size of the %set.
911 set(size_t __position
, bool __val
= true)
913 if (__position
>= _Nb
)
914 __throw_out_of_range(__N("bitset::set"));
915 return _Unchecked_set(__position
, __val
);
919 * @brief Sets every bit to false.
929 * @brief Sets a given bit to false.
930 * @param position The index of the bit.
931 * @throw std::out_of_range If @a pos is bigger the size of the %set.
933 * Same as writing @c set(pos,false).
936 reset(size_t __position
)
938 if (__position
>= _Nb
)
939 __throw_out_of_range(__N("bitset::reset"));
940 return _Unchecked_reset(__position
);
944 * @brief Toggles every bit to its opposite value.
950 this->_M_do_sanitize();
955 * @brief Toggles a given bit to its opposite value.
956 * @param position The index of the bit.
957 * @throw std::out_of_range If @a pos is bigger the size of the %set.
960 flip(size_t __position
)
962 if (__position
>= _Nb
)
963 __throw_out_of_range(__N("bitset::flip"));
964 return _Unchecked_flip(__position
);
967 /// See the no-argument flip().
970 { return bitset
<_Nb
>(*this).flip(); }
974 * @brief Array-indexing support.
975 * @param position Index into the %bitset.
976 * @return A bool for a 'const %bitset'. For non-const bitsets, an
977 * instance of the reference proxy class.
978 * @note These operators do no range checking and throw no exceptions,
979 * as required by DR 11 to the standard.
982 * _GLIBCXX_RESOLVE_LIB_DEFECTS Note that this implementation already
983 * resolves DR 11 (items 1 and 2), but does not do the range-checking
984 * required by that DR's resolution. -pme
985 * The DR has since been changed: range-checking is a precondition
986 * (users' responsibility), and these functions must not throw. -pme
990 operator[](size_t __position
)
991 { return reference(*this,__position
); }
994 operator[](size_t __position
) const
995 { return _Unchecked_test(__position
); }
999 * @brief Retuns a numerical interpretation of the %bitset.
1000 * @return The integral equivalent of the bits.
1001 * @throw std::overflow_error If there are too many bits to be
1002 * represented in an @c unsigned @c long.
1006 { return this->_M_do_to_ulong(); }
1009 * @brief Retuns a character interpretation of the %bitset.
1010 * @return The string equivalent of the bits.
1012 * Note the ordering of the bits: decreasing character positions
1013 * correspond to increasing bit positions (see the main class notes for
1016 template<class _CharT
, class _Traits
, class _Alloc
>
1017 std::basic_string
<_CharT
, _Traits
, _Alloc
>
1020 std::basic_string
<_CharT
, _Traits
, _Alloc
> __result
;
1021 _M_copy_to_string(__result
);
1025 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1026 // 434. bitset::to_string() hard to use.
1027 template<class _CharT
, class _Traits
>
1028 std::basic_string
<_CharT
, _Traits
, std::allocator
<_CharT
> >
1030 { return to_string
<_CharT
, _Traits
, std::allocator
<_CharT
> >(); }
1032 template<class _CharT
>
1033 std::basic_string
<_CharT
, std::char_traits
<_CharT
>,
1034 std::allocator
<_CharT
> >
1037 return to_string
<_CharT
, std::char_traits
<_CharT
>,
1038 std::allocator
<_CharT
> >();
1041 std::basic_string
<char, std::char_traits
<char>, std::allocator
<char> >
1044 return to_string
<char, std::char_traits
<char>,
1045 std::allocator
<char> >();
1048 // Helper functions for string operations.
1049 template<class _CharT
, class _Traits
, class _Alloc
>
1051 _M_copy_from_string(const std::basic_string
<_CharT
,
1052 _Traits
, _Alloc
>& __s
,
1055 template<class _CharT
, class _Traits
, class _Alloc
>
1057 _M_copy_to_string(std::basic_string
<_CharT
, _Traits
, _Alloc
>&) const;
1059 /// Returns the number of bits which are set.
1062 { return this->_M_do_count(); }
1064 /// Returns the total number of bits.
1070 /// These comparisons for equality/inequality are, well, @e bitwise.
1072 operator==(const bitset
<_Nb
>& __rhs
) const
1073 { return this->_M_is_equal(__rhs
); }
1076 operator!=(const bitset
<_Nb
>& __rhs
) const
1077 { return !this->_M_is_equal(__rhs
); }
1081 * @brief Tests the value of a bit.
1082 * @param position The index of a bit.
1083 * @return The value at @a pos.
1084 * @throw std::out_of_range If @a pos is bigger the size of the %set.
1087 test(size_t __position
) const
1089 if (__position
>= _Nb
)
1090 __throw_out_of_range(__N("bitset::test"));
1091 return _Unchecked_test(__position
);
1095 * @brief Tests whether any of the bits are on.
1096 * @return True if at least one bit is set.
1100 { return this->_M_is_any(); }
1103 * @brief Tests whether any of the bits are on.
1104 * @return True if none of the bits are set.
1108 { return !this->_M_is_any(); }
1111 /// Self-explanatory.
1113 operator<<(size_t __position
) const
1114 { return bitset
<_Nb
>(*this) <<= __position
; }
1117 operator>>(size_t __position
) const
1118 { return bitset
<_Nb
>(*this) >>= __position
; }
1122 * @brief Finds the index of the first "on" bit.
1123 * @return The index of the first bit set, or size() if not found.
1124 * @ingroup SGIextensions
1129 { return this->_M_do_find_first(_Nb
); }
1132 * @brief Finds the index of the next "on" bit after prev.
1133 * @return The index of the next bit set, or size() if not found.
1134 * @param prev Where to start searching.
1135 * @ingroup SGIextensions
1139 _Find_next(size_t __prev
) const
1140 { return this->_M_do_find_next(__prev
, _Nb
); }
1143 // Definitions of non-inline member functions.
1144 template<size_t _Nb
>
1145 template<class _CharT
, class _Traits
, class _Alloc
>
1147 bitset
<_Nb
>::_M_copy_from_string(const std::basic_string
<_CharT
, _Traits
,
1148 _Alloc
>& __s
, size_t __pos
, size_t __n
)
1151 const size_t __nbits
= std::min(_Nb
, std::min(__n
, __s
.size() - __pos
));
1152 for (size_t __i
= 0; __i
< __nbits
; ++__i
)
1154 switch(__s
[__pos
+ __nbits
- __i
- 1])
1162 __throw_invalid_argument(__N("bitset::_M_copy_from_string"));
1167 template<size_t _Nb
>
1168 template<class _CharT
, class _Traits
, class _Alloc
>
1170 bitset
<_Nb
>::_M_copy_to_string(std::basic_string
<_CharT
, _Traits
,
1173 __s
.assign(_Nb
, '0');
1174 for (size_t __i
= 0; __i
< _Nb
; ++__i
)
1175 if (_Unchecked_test(__i
))
1176 __s
[_Nb
- 1 - __i
] = '1';
1179 // 23.3.5.3 bitset operations:
1182 * @brief Global bitwise operations on bitsets.
1183 * @param x A bitset.
1184 * @param y A bitset of the same size as @a x.
1185 * @return A new bitset.
1187 * These should be self-explanatory.
1189 template<size_t _Nb
>
1191 operator&(const bitset
<_Nb
>& __x
, const bitset
<_Nb
>& __y
)
1193 bitset
<_Nb
> __result(__x
);
1198 template<size_t _Nb
>
1200 operator|(const bitset
<_Nb
>& __x
, const bitset
<_Nb
>& __y
)
1202 bitset
<_Nb
> __result(__x
);
1207 template <size_t _Nb
>
1209 operator^(const bitset
<_Nb
>& __x
, const bitset
<_Nb
>& __y
)
1211 bitset
<_Nb
> __result(__x
);
1219 * @brief Global I/O operators for bitsets.
1221 * Direct I/O between streams and bitsets is supported. Output is
1222 * straightforward. Input will skip whitespace, only accept '0' and '1'
1223 * characters, and will only extract as many digits as the %bitset will
1226 template<class _CharT
, class _Traits
, size_t _Nb
>
1227 std::basic_istream
<_CharT
, _Traits
>&
1228 operator>>(std::basic_istream
<_CharT
, _Traits
>& __is
, bitset
<_Nb
>& __x
)
1230 typedef typename
_Traits::char_type char_type
;
1231 std::basic_string
<_CharT
, _Traits
> __tmp
;
1234 std::ios_base::iostate __state
= std::ios_base::goodbit
;
1235 typename
std::basic_istream
<_CharT
, _Traits
>::sentry
__sentry(__is
);
1240 basic_streambuf
<_CharT
, _Traits
>* __buf
= __is
.rdbuf();
1241 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1242 // 303. Bitset input operator underspecified
1243 const char_type __zero
= __is
.widen('0');
1244 const char_type __one
= __is
.widen('1');
1245 for (size_t __i
= 0; __i
< _Nb
; ++__i
)
1247 static typename
_Traits::int_type __eof
= _Traits::eof();
1249 typename
_Traits::int_type __c1
= __buf
->sbumpc();
1250 if (_Traits::eq_int_type(__c1
, __eof
))
1252 __state
|= std::ios_base::eofbit
;
1257 const char_type __c2
= _Traits::to_char_type(__c1
);
1259 __tmp
.push_back('0');
1260 else if (__c2
== __one
)
1261 __tmp
.push_back('1');
1262 else if (_Traits::eq_int_type(__buf
->sputbackc(__c2
),
1265 __state
|= std::ios_base::failbit
;
1272 { __is
._M_setstate(std::ios_base::badbit
); }
1275 if (__tmp
.empty() && _Nb
)
1276 __state
|= std::ios_base::failbit
;
1278 __x
._M_copy_from_string(__tmp
, static_cast<size_t>(0), _Nb
);
1280 __is
.setstate(__state
);
1284 template <class _CharT
, class _Traits
, size_t _Nb
>
1285 std::basic_ostream
<_CharT
, _Traits
>&
1286 operator<<(std::basic_ostream
<_CharT
, _Traits
>& __os
,
1287 const bitset
<_Nb
>& __x
)
1289 std::basic_string
<_CharT
, _Traits
> __tmp
;
1290 __x
._M_copy_to_string(__tmp
);
1291 return __os
<< __tmp
;
1296 #undef _GLIBCXX_BITSET_WORDS
1297 #undef _GLIBCXX_BITSET_BITS_PER_WORD
1299 #ifdef _GLIBCXX_DEBUG
1300 # include <debug/bitset>
1303 #endif /* _GLIBCXX_BITSET */