1 // <bitset> -*- C++ -*-
3 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006
4 // Free Software Foundation, Inc.
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)
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.
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
19 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
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.
33 * Silicon Graphics Computer Systems, Inc.
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.
44 /** @file include/bitset
45 * This is a Standard C++ Library header.
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) \
65 / _GLIBCXX_BITSET_BITS_PER_WORD)
67 _GLIBCXX_BEGIN_NESTED_NAMESPACE(std, _GLIBCXX_STD)
71 * Base class, general case. It is a class inveriant that _Nw will be
74 * See documentation for bitset.
80 typedef unsigned long _WordT;
82 /// 0 is the least significant word.
88 _Base_bitset(unsigned long __val)
95 _S_whichword(size_t __pos )
96 { return __pos / _GLIBCXX_BITSET_BITS_PER_WORD; }
99 _S_whichbyte(size_t __pos )
100 { return (__pos % _GLIBCXX_BITSET_BITS_PER_WORD) / __CHAR_BIT__; }
103 _S_whichbit(size_t __pos )
104 { return __pos % _GLIBCXX_BITSET_BITS_PER_WORD; }
107 _S_maskbit(size_t __pos )
108 { return (static_cast<_WordT>(1)) << _S_whichbit(__pos); }
111 _M_getword(size_t __pos)
112 { return _M_w[_S_whichword(__pos)]; }
115 _M_getword(size_t __pos) const
116 { return _M_w[_S_whichword(__pos)]; }
120 { return _M_w[_Nw - 1]; }
124 { return _M_w[_Nw - 1]; }
127 _M_do_and(const _Base_bitset<_Nw>& __x)
129 for (size_t __i = 0; __i < _Nw; __i++)
130 _M_w[__i] &= __x._M_w[__i];
134 _M_do_or(const _Base_bitset<_Nw>& __x)
136 for (size_t __i = 0; __i < _Nw; __i++)
137 _M_w[__i] |= __x._M_w[__i];
141 _M_do_xor(const _Base_bitset<_Nw>& __x)
143 for (size_t __i = 0; __i < _Nw; __i++)
144 _M_w[__i] ^= __x._M_w[__i];
148 _M_do_left_shift(size_t __shift);
151 _M_do_right_shift(size_t __shift);
156 for (size_t __i = 0; __i < _Nw; __i++)
157 _M_w[__i] = ~_M_w[__i];
163 for (size_t __i = 0; __i < _Nw; __i++)
164 _M_w[__i] = ~static_cast<_WordT>(0);
169 { std::memset(_M_w, 0, _Nw * sizeof(_WordT)); }
172 _M_is_equal(const _Base_bitset<_Nw>& __x) const
174 for (size_t __i = 0; __i < _Nw; ++__i)
176 if (_M_w[__i] != __x._M_w[__i])
185 for (size_t __i = 0; __i < _Nw; __i++)
187 if (_M_w[__i] != static_cast<_WordT>(0))
197 for (size_t __i = 0; __i < _Nw; __i++)
198 __result += __builtin_popcountl(_M_w[__i]);
203 _M_do_to_ulong() const;
205 // find first "on" bit
207 _M_do_find_first(size_t __not_found) const;
209 // find the next "on" bit that follows "prev"
211 _M_do_find_next(size_t __prev, size_t __not_found) const;
214 // Definitions of non-inline functions from _Base_bitset.
217 _Base_bitset<_Nw>::_M_do_left_shift(size_t __shift)
219 if (__builtin_expect(__shift != 0, 1))
221 const size_t __wshift = __shift / _GLIBCXX_BITSET_BITS_PER_WORD;
222 const size_t __offset = __shift % _GLIBCXX_BITSET_BITS_PER_WORD;
225 for (size_t __n = _Nw - 1; __n >= __wshift; --__n)
226 _M_w[__n] = _M_w[__n - __wshift];
229 const size_t __sub_offset = (_GLIBCXX_BITSET_BITS_PER_WORD
231 for (size_t __n = _Nw - 1; __n > __wshift; --__n)
232 _M_w[__n] = ((_M_w[__n - __wshift] << __offset)
233 | (_M_w[__n - __wshift - 1] >> __sub_offset));
234 _M_w[__wshift] = _M_w[0] << __offset;
237 std::fill(_M_w + 0, _M_w + __wshift, static_cast<_WordT>(0));
243 _Base_bitset<_Nw>::_M_do_right_shift(size_t __shift)
245 if (__builtin_expect(__shift != 0, 1))
247 const size_t __wshift = __shift / _GLIBCXX_BITSET_BITS_PER_WORD;
248 const size_t __offset = __shift % _GLIBCXX_BITSET_BITS_PER_WORD;
249 const size_t __limit = _Nw - __wshift - 1;
252 for (size_t __n = 0; __n <= __limit; ++__n)
253 _M_w[__n] = _M_w[__n + __wshift];
256 const size_t __sub_offset = (_GLIBCXX_BITSET_BITS_PER_WORD
258 for (size_t __n = 0; __n < __limit; ++__n)
259 _M_w[__n] = ((_M_w[__n + __wshift] >> __offset)
260 | (_M_w[__n + __wshift + 1] << __sub_offset));
261 _M_w[__limit] = _M_w[_Nw-1] >> __offset;
264 std::fill(_M_w + __limit + 1, _M_w + _Nw, static_cast<_WordT>(0));
270 _Base_bitset<_Nw>::_M_do_to_ulong() const
272 for (size_t __i = 1; __i < _Nw; ++__i)
274 __throw_overflow_error(__N("_Base_bitset::_M_do_to_ulong"));
280 _Base_bitset<_Nw>::_M_do_find_first(size_t __not_found) const
282 for (size_t __i = 0; __i < _Nw; __i++)
284 _WordT __thisword = _M_w[__i];
285 if (__thisword != static_cast<_WordT>(0))
286 return (__i * _GLIBCXX_BITSET_BITS_PER_WORD
287 + __builtin_ctzl(__thisword));
289 // not found, so return an indication of failure.
295 _Base_bitset<_Nw>::_M_do_find_next(size_t __prev, size_t __not_found) const
297 // make bound inclusive
300 // check out of bounds
301 if (__prev >= _Nw * _GLIBCXX_BITSET_BITS_PER_WORD)
305 size_t __i = _S_whichword(__prev);
306 _WordT __thisword = _M_w[__i];
308 // mask off bits below bound
309 __thisword &= (~static_cast<_WordT>(0)) << _S_whichbit(__prev);
311 if (__thisword != static_cast<_WordT>(0))
312 return (__i * _GLIBCXX_BITSET_BITS_PER_WORD
313 + __builtin_ctzl(__thisword));
315 // check subsequent words
317 for (; __i < _Nw; __i++)
319 __thisword = _M_w[__i];
320 if (__thisword != static_cast<_WordT>(0))
321 return (__i * _GLIBCXX_BITSET_BITS_PER_WORD
322 + __builtin_ctzl(__thisword));
324 // not found, so return an indication of failure.
326 } // end _M_do_find_next
330 * Base class, specialization for a single word.
332 * See documentation for bitset.
336 struct _Base_bitset<1>
338 typedef unsigned long _WordT;
345 _Base_bitset(unsigned long __val)
350 _S_whichword(size_t __pos )
351 { return __pos / _GLIBCXX_BITSET_BITS_PER_WORD; }
354 _S_whichbyte(size_t __pos )
355 { return (__pos % _GLIBCXX_BITSET_BITS_PER_WORD) / __CHAR_BIT__; }
358 _S_whichbit(size_t __pos )
359 { return __pos % _GLIBCXX_BITSET_BITS_PER_WORD; }
362 _S_maskbit(size_t __pos )
363 { return (static_cast<_WordT>(1)) << _S_whichbit(__pos); }
370 _M_getword(size_t) const
382 _M_do_and(const _Base_bitset<1>& __x)
383 { _M_w &= __x._M_w; }
386 _M_do_or(const _Base_bitset<1>& __x)
387 { _M_w |= __x._M_w; }
390 _M_do_xor(const _Base_bitset<1>& __x)
391 { _M_w ^= __x._M_w; }
394 _M_do_left_shift(size_t __shift)
395 { _M_w <<= __shift; }
398 _M_do_right_shift(size_t __shift)
399 { _M_w >>= __shift; }
407 { _M_w = ~static_cast<_WordT>(0); }
414 _M_is_equal(const _Base_bitset<1>& __x) const
415 { return _M_w == __x._M_w; }
419 { return _M_w != 0; }
423 { return __builtin_popcountl(_M_w); }
426 _M_do_to_ulong() const
430 _M_do_find_first(size_t __not_found) const
433 return __builtin_ctzl(_M_w);
438 // find the next "on" bit that follows "prev"
440 _M_do_find_next(size_t __prev, size_t __not_found) const
443 if (__prev >= ((size_t) _GLIBCXX_BITSET_BITS_PER_WORD))
446 _WordT __x = _M_w >> __prev;
448 return __builtin_ctzl(__x) + __prev;
456 * Base class, specialization for no storage (zero-length %bitset).
458 * See documentation for bitset.
462 struct _Base_bitset<0>
464 typedef unsigned long _WordT;
469 _Base_bitset(unsigned long)
473 _S_whichword(size_t __pos )
474 { return __pos / _GLIBCXX_BITSET_BITS_PER_WORD; }
477 _S_whichbyte(size_t __pos )
478 { return (__pos % _GLIBCXX_BITSET_BITS_PER_WORD) / __CHAR_BIT__; }
481 _S_whichbit(size_t __pos )
482 { return __pos % _GLIBCXX_BITSET_BITS_PER_WORD; }
485 _S_maskbit(size_t __pos )
486 { return (static_cast<_WordT>(1)) << _S_whichbit(__pos); }
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.
496 _M_getword(size_t) const
498 __throw_out_of_range(__N("_Base_bitset::_M_getword"));
507 _M_do_and(const _Base_bitset<0>&)
511 _M_do_or(const _Base_bitset<0>&)
515 _M_do_xor(const _Base_bitset<0>&)
519 _M_do_left_shift(size_t)
523 _M_do_right_shift(size_t)
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?
542 _M_is_equal(const _Base_bitset<0>&) const
554 _M_do_to_ulong() const
557 // Normally "not found" is the size, but that could also be
558 // misinterpreted as an index in this corner case. Oh well.
560 _M_do_find_first(size_t) const
564 _M_do_find_next(size_t, size_t) const
569 // Helper class to zero out the unused high-order bits in the highest word.
570 template<size_t _Extrabits>
573 static void _S_do_sanitize(unsigned long& __val)
574 { __val &= ~((~static_cast<unsigned long>(0)) << _Extrabits); }
579 { static void _S_do_sanitize(unsigned long) {} };
582 * @brief The %bitset class represents a @e fixed-size sequence of bits.
584 * @ingroup Containers
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.)
589 * The template argument, @a Nb, may be any non-negative number,
590 * specifying the number of bits (e.g., "0", "12", "1024*1024").
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.
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
601 * / right-hand" position, and the bit at index Nb-1 in the "most
602 * significant / left-hand" position. Thus, unlike other containers, a
603 * %bitset's index "counts from right to left," to put it very loosely.
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.
611 * #include <iostream>
614 * using namespace std;
621 * cout << "b('a') is " << b << endl;
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;
631 * Also see http://gcc.gnu.org/onlinedocs/libstdc++/ext/sgiexts.html#ch23
632 * for a description of extensions.
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.
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.
647 : private _Base_bitset<_GLIBCXX_BITSET_WORDS(_Nb)>
650 typedef _Base_bitset<_GLIBCXX_BITSET_WORDS(_Nb)> _Base;
651 typedef unsigned long _WordT;
656 _Sanitize<_Nb % _GLIBCXX_BITSET_BITS_PER_WORD>::
657 _S_do_sanitize(this->_M_hiword());
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.
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.
670 * (On a typical system, this "bit %reference" is 64 times the size of
671 * an actual bit. Ha.)
684 reference(bitset& __b, size_t __pos)
686 _M_wp = &__b._M_getword(__pos);
687 _M_bpos = _Base::_S_whichbit(__pos);
698 *_M_wp |= _Base::_S_maskbit(_M_bpos);
700 *_M_wp &= ~_Base::_S_maskbit(_M_bpos);
704 // For b[i] = b[__j];
706 operator=(const reference& __j)
708 if ((*(__j._M_wp) & _Base::_S_maskbit(__j._M_bpos)))
709 *_M_wp |= _Base::_S_maskbit(_M_bpos);
711 *_M_wp &= ~_Base::_S_maskbit(_M_bpos);
718 { return (*(_M_wp) & _Base::_S_maskbit(_M_bpos)) == 0; }
721 operator bool() const
722 { return (*(_M_wp) & _Base::_S_maskbit(_M_bpos)) != 0; }
728 *_M_wp ^= _Base::_S_maskbit(_M_bpos);
732 friend class reference;
734 // 23.3.5.1 constructors:
735 /// All bits set to zero.
739 /// Initial bits bitwise-copied from a single word (others set to zero).
740 bitset(unsigned long __val)
742 { _M_do_sanitize(); }
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;
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'.
753 template<class _CharT, class _Traits, class _Alloc>
755 bitset(const std::basic_string<_CharT, _Traits, _Alloc>& __s,
756 size_t __position = 0)
759 if (__position > __s.size())
760 __throw_out_of_range(__N("bitset::bitset initial position "
762 _M_copy_from_string(__s, __position,
763 std::basic_string<_CharT, _Traits, _Alloc>::npos);
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'.
775 template<class _CharT, class _Traits, class _Alloc>
776 bitset(const std::basic_string<_CharT, _Traits, _Alloc>& __s,
777 size_t __position, size_t __n)
780 if (__position > __s.size())
781 __throw_out_of_range(__N("bitset::bitset initial position "
783 _M_copy_from_string(__s, __position, __n);
786 // 23.3.5.2 bitset operations:
789 * @brief Operations on bitsets.
790 * @param rhs A same-sized bitset.
792 * These should be self-explanatory.
795 operator&=(const bitset<_Nb>& __rhs)
797 this->_M_do_and(__rhs);
802 operator|=(const bitset<_Nb>& __rhs)
804 this->_M_do_or(__rhs);
809 operator^=(const bitset<_Nb>& __rhs)
811 this->_M_do_xor(__rhs);
818 * @brief Operations on bitsets.
819 * @param position The number of places to shift.
821 * These should be self-explanatory.
824 operator<<=(size_t __position)
826 if (__builtin_expect(__position < _Nb, 1))
828 this->_M_do_left_shift(__position);
829 this->_M_do_sanitize();
837 operator>>=(size_t __position)
839 if (__builtin_expect(__position < _Nb, 1))
841 this->_M_do_right_shift(__position);
842 this->_M_do_sanitize();
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
857 _Unchecked_set(size_t __pos)
859 this->_M_getword(__pos) |= _Base::_S_maskbit(__pos);
864 _Unchecked_set(size_t __pos, int __val)
867 this->_M_getword(__pos) |= _Base::_S_maskbit(__pos);
869 this->_M_getword(__pos) &= ~_Base::_S_maskbit(__pos);
874 _Unchecked_reset(size_t __pos)
876 this->_M_getword(__pos) &= ~_Base::_S_maskbit(__pos);
881 _Unchecked_flip(size_t __pos)
883 this->_M_getword(__pos) ^= _Base::_S_maskbit(__pos);
888 _Unchecked_test(size_t __pos) const
889 { return ((this->_M_getword(__pos) & _Base::_S_maskbit(__pos))
890 != static_cast<_WordT>(0)); }
893 // Set, reset, and flip.
895 * @brief Sets every bit to true.
901 this->_M_do_sanitize();
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.
912 set(size_t __position, bool __val = true)
914 if (__position >= _Nb)
915 __throw_out_of_range(__N("bitset::set"));
916 return _Unchecked_set(__position, __val);
920 * @brief Sets every bit to false.
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.
934 * Same as writing @c set(pos,false).
937 reset(size_t __position)
939 if (__position >= _Nb)
940 __throw_out_of_range(__N("bitset::reset"));
941 return _Unchecked_reset(__position);
945 * @brief Toggles every bit to its opposite value.
951 this->_M_do_sanitize();
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.
961 flip(size_t __position)
963 if (__position >= _Nb)
964 __throw_out_of_range(__N("bitset::flip"));
965 return _Unchecked_flip(__position);
968 /// See the no-argument flip().
971 { return bitset<_Nb>(*this).flip(); }
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.
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
991 operator[](size_t __position)
992 { return reference(*this,__position); }
995 operator[](size_t __position) const
996 { return _Unchecked_test(__position); }
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.
1007 { return this->_M_do_to_ulong(); }
1010 * @brief Retuns a character interpretation of the %bitset.
1011 * @return The string equivalent of the bits.
1013 * Note the ordering of the bits: decreasing character positions
1014 * correspond to increasing bit positions (see the main class notes for
1017 template<class _CharT, class _Traits, class _Alloc>
1018 std::basic_string<_CharT, _Traits, _Alloc>
1021 std::basic_string<_CharT, _Traits, _Alloc> __result;
1022 _M_copy_to_string(__result);
1026 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1027 // 434. bitset::to_string() hard to use.
1028 template<class _CharT, class _Traits>
1029 std::basic_string<_CharT, _Traits, std::allocator<_CharT> >
1031 { return to_string<_CharT, _Traits, std::allocator<_CharT> >(); }
1033 template<class _CharT>
1034 std::basic_string<_CharT, std::char_traits<_CharT>,
1035 std::allocator<_CharT> >
1038 return to_string<_CharT, std::char_traits<_CharT>,
1039 std::allocator<_CharT> >();
1042 std::basic_string<char, std::char_traits<char>, std::allocator<char> >
1045 return to_string<char, std::char_traits<char>,
1046 std::allocator<char> >();
1049 // Helper functions for string operations.
1050 template<class _CharT, class _Traits, class _Alloc>
1052 _M_copy_from_string(const std::basic_string<_CharT,
1053 _Traits, _Alloc>& __s,
1056 template<class _CharT, class _Traits, class _Alloc>
1058 _M_copy_to_string(std::basic_string<_CharT, _Traits, _Alloc>&) const;
1060 /// Returns the number of bits which are set.
1063 { return this->_M_do_count(); }
1065 /// Returns the total number of bits.
1071 /// These comparisons for equality/inequality are, well, @e bitwise.
1073 operator==(const bitset<_Nb>& __rhs) const
1074 { return this->_M_is_equal(__rhs); }
1077 operator!=(const bitset<_Nb>& __rhs) const
1078 { return !this->_M_is_equal(__rhs); }
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.
1088 test(size_t __position) const
1090 if (__position >= _Nb)
1091 __throw_out_of_range(__N("bitset::test"));
1092 return _Unchecked_test(__position);
1096 * @brief Tests whether any of the bits are on.
1097 * @return True if at least one bit is set.
1101 { return this->_M_is_any(); }
1104 * @brief Tests whether any of the bits are on.
1105 * @return True if none of the bits are set.
1109 { return !this->_M_is_any(); }
1112 /// Self-explanatory.
1114 operator<<(size_t __position) const
1115 { return bitset<_Nb>(*this) <<= __position; }
1118 operator>>(size_t __position) const
1119 { return bitset<_Nb>(*this) >>= __position; }
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
1130 { return this->_M_do_find_first(_Nb); }
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
1140 _Find_next(size_t __prev ) const
1141 { return this->_M_do_find_next(__prev, _Nb); }
1144 // Definitions of non-inline member functions.
1145 template<size_t _Nb>
1146 template<class _CharT, class _Traits, class _Alloc>
1149 _M_copy_from_string(const std::basic_string<_CharT, _Traits,
1150 _Alloc>& __s, size_t __pos, size_t __n)
1153 const size_t __nbits = std::min(_Nb, std::min(__n, __s.size() - __pos));
1154 for (size_t __i = __nbits; __i > 0; --__i)
1156 switch(__s[__pos + __nbits - __i])
1161 _Unchecked_set(__i - 1);
1164 __throw_invalid_argument(__N("bitset::_M_copy_from_string"));
1169 template<size_t _Nb>
1170 template<class _CharT, class _Traits, class _Alloc>
1173 _M_copy_to_string(std::basic_string<_CharT, _Traits, _Alloc>& __s) const
1175 __s.assign(_Nb, '0');
1176 for (size_t __i = _Nb; __i > 0; --__i)
1177 if (_Unchecked_test(__i - 1))
1178 __s[_Nb - __i] = '1';
1181 // 23.3.5.3 bitset operations:
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.
1189 * These should be self-explanatory.
1191 template<size_t _Nb>
1193 operator&(const bitset<_Nb>& __x, const bitset<_Nb>& __y)
1195 bitset<_Nb> __result(__x);
1200 template<size_t _Nb>
1202 operator|(const bitset<_Nb>& __x, const bitset<_Nb>& __y)
1204 bitset<_Nb> __result(__x);
1209 template <size_t _Nb>
1211 operator^(const bitset<_Nb>& __x, const bitset<_Nb>& __y)
1213 bitset<_Nb> __result(__x);
1221 * @brief Global I/O operators for bitsets.
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
1228 template<class _CharT, class _Traits, size_t _Nb>
1229 std::basic_istream<_CharT, _Traits>&
1230 operator>>(std::basic_istream<_CharT, _Traits>& __is, bitset<_Nb>& __x)
1232 typedef typename _Traits::char_type char_type;
1233 std::basic_string<_CharT, _Traits> __tmp;
1236 std::ios_base::iostate __state = std::ios_base::goodbit;
1237 typename std::basic_istream<_CharT, _Traits>::sentry __sentry(__is);
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');
1247 for (size_t __i = _Nb; __i > 0; --__i)
1249 static typename _Traits::int_type __eof = _Traits::eof();
1251 typename _Traits::int_type __c1 = __buf->sbumpc();
1252 if (_Traits::eq_int_type(__c1, __eof))
1254 __state |= std::ios_base::eofbit;
1259 const char_type __c2 = _Traits::to_char_type(__c1);
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),
1267 __state |= std::ios_base::failbit;
1274 { __is._M_setstate(std::ios_base::badbit); }
1277 if (__tmp.empty() && _Nb)
1278 __state |= std::ios_base::failbit;
1280 __x._M_copy_from_string(__tmp, static_cast<size_t>(0), _Nb);
1282 __is.setstate(__state);
1286 template <class _CharT, class _Traits, size_t _Nb>
1287 std::basic_ostream<_CharT, _Traits>&
1288 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1289 const bitset<_Nb>& __x)
1291 std::basic_string<_CharT, _Traits> __tmp;
1292 __x._M_copy_to_string(__tmp);
1293 return __os << __tmp;
1296 // Specializations for zero-sized bitsets, to avoid "unsigned comparison
1297 // with zero" warnings.
1303 __throw_out_of_range(__N("bitset::set"));
1312 __throw_out_of_range(__N("bitset::reset"));
1321 __throw_out_of_range(__N("bitset::flip"));
1330 __throw_out_of_range(__N("bitset::test"));
1335 _GLIBCXX_END_NESTED_NAMESPACE
1337 #undef _GLIBCXX_BITSET_WORDS
1338 #undef _GLIBCXX_BITSET_BITS_PER_WORD
1340 #ifdef _GLIBCXX_DEBUG
1341 # include <debug/bitset>
1344 #endif /* _GLIBCXX_BITSET */