1 // <bitset> -*- C++ -*-
3 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
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 3, 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 // Under Section 7 of GPL version 3, you are granted additional
18 // permissions described in the GCC Runtime Library Exception, version
19 // 3.1, as published by the Free Software Foundation.
21 // You should have received a copy of the GNU General Public License and
22 // a copy of the GCC Runtime Library Exception along with this program;
23 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24 // <http://www.gnu.org/licenses/>.
28 * Silicon Graphics Computer Systems, Inc.
30 * Permission to use, copy, modify, distribute and sell this software
31 * and its documentation for any purpose is hereby granted without fee,
32 * provided that the above copyright notice appear in all copies and
33 * that both that copyright notice and this permission notice appear
34 * in supporting documentation. Silicon Graphics makes no
35 * representations about the suitability of this software for any
36 * purpose. It is provided "as is" without express or implied warranty.
39 /** @file include/bitset
40 * This is a Standard C++ Library header.
43 #ifndef _GLIBCXX_BITSET
44 #define _GLIBCXX_BITSET 1
46 #pragma GCC system_header
48 #include <cstddef> // For size_t
50 #include <bits/functexcept.h> // For invalid_argument, out_of_range,
53 #include <cxxabi-forced.h>
55 #define _GLIBCXX_BITSET_BITS_PER_WORD (__CHAR_BIT__ * sizeof(unsigned long))
56 #define _GLIBCXX_BITSET_WORDS(__n) \
57 ((__n) < 1 ? 0 : ((__n) + _GLIBCXX_BITSET_BITS_PER_WORD - 1) \
58 / _GLIBCXX_BITSET_BITS_PER_WORD)
60 _GLIBCXX_BEGIN_NESTED_NAMESPACE(std, _GLIBCXX_STD_D)
63 * Base class, general case. It is a class invariant that _Nw will be
66 * See documentation for bitset.
71 typedef unsigned long _WordT;
73 /// 0 is the least significant word.
79 #ifdef __GXX_EXPERIMENTAL_CXX0X__
80 _Base_bitset(unsigned long long __val)
82 _Base_bitset(unsigned long __val)
87 #ifdef __GXX_EXPERIMENTAL_CXX0X__
88 if (sizeof(unsigned long long) > sizeof(unsigned long))
89 _M_w[1] = __val >> _GLIBCXX_BITSET_BITS_PER_WORD;
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 { __builtin_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)
174 if (_M_w[__i] != __x._M_w[__i])
180 _M_are_all_aux() const
182 for (size_t __i = 0; __i < _Nw - 1; __i++)
183 if (_M_w[__i] != ~static_cast<_WordT>(0))
185 return ((_Nw - 1) * _GLIBCXX_BITSET_BITS_PER_WORD
186 + __builtin_popcountl(_M_hiword()));
192 for (size_t __i = 0; __i < _Nw; __i++)
193 if (_M_w[__i] != static_cast<_WordT>(0))
202 for (size_t __i = 0; __i < _Nw; __i++)
203 __result += __builtin_popcountl(_M_w[__i]);
208 _M_do_to_ulong() const;
210 #ifdef __GXX_EXPERIMENTAL_CXX0X__
212 _M_do_to_ullong() const;
215 // find first "on" bit
217 _M_do_find_first(size_t __not_found) const;
219 // find the next "on" bit that follows "prev"
221 _M_do_find_next(size_t __prev, size_t __not_found) const;
224 // Definitions of non-inline functions from _Base_bitset.
227 _Base_bitset<_Nw>::_M_do_left_shift(size_t __shift)
229 if (__builtin_expect(__shift != 0, 1))
231 const size_t __wshift = __shift / _GLIBCXX_BITSET_BITS_PER_WORD;
232 const size_t __offset = __shift % _GLIBCXX_BITSET_BITS_PER_WORD;
235 for (size_t __n = _Nw - 1; __n >= __wshift; --__n)
236 _M_w[__n] = _M_w[__n - __wshift];
239 const size_t __sub_offset = (_GLIBCXX_BITSET_BITS_PER_WORD
241 for (size_t __n = _Nw - 1; __n > __wshift; --__n)
242 _M_w[__n] = ((_M_w[__n - __wshift] << __offset)
243 | (_M_w[__n - __wshift - 1] >> __sub_offset));
244 _M_w[__wshift] = _M_w[0] << __offset;
247 std::fill(_M_w + 0, _M_w + __wshift, static_cast<_WordT>(0));
253 _Base_bitset<_Nw>::_M_do_right_shift(size_t __shift)
255 if (__builtin_expect(__shift != 0, 1))
257 const size_t __wshift = __shift / _GLIBCXX_BITSET_BITS_PER_WORD;
258 const size_t __offset = __shift % _GLIBCXX_BITSET_BITS_PER_WORD;
259 const size_t __limit = _Nw - __wshift - 1;
262 for (size_t __n = 0; __n <= __limit; ++__n)
263 _M_w[__n] = _M_w[__n + __wshift];
266 const size_t __sub_offset = (_GLIBCXX_BITSET_BITS_PER_WORD
268 for (size_t __n = 0; __n < __limit; ++__n)
269 _M_w[__n] = ((_M_w[__n + __wshift] >> __offset)
270 | (_M_w[__n + __wshift + 1] << __sub_offset));
271 _M_w[__limit] = _M_w[_Nw-1] >> __offset;
274 std::fill(_M_w + __limit + 1, _M_w + _Nw, static_cast<_WordT>(0));
280 _Base_bitset<_Nw>::_M_do_to_ulong() const
282 for (size_t __i = 1; __i < _Nw; ++__i)
284 __throw_overflow_error(__N("_Base_bitset::_M_do_to_ulong"));
288 #ifdef __GXX_EXPERIMENTAL_CXX0X__
291 _Base_bitset<_Nw>::_M_do_to_ullong() const
293 const bool __dw = sizeof(unsigned long long) > sizeof(unsigned long);
294 for (size_t __i = 1 + __dw; __i < _Nw; ++__i)
296 __throw_overflow_error(__N("_Base_bitset::_M_do_to_ullong"));
299 return _M_w[0] + (static_cast<unsigned long long>(_M_w[1])
300 << _GLIBCXX_BITSET_BITS_PER_WORD);
307 _Base_bitset<_Nw>::_M_do_find_first(size_t __not_found) const
309 for (size_t __i = 0; __i < _Nw; __i++)
311 _WordT __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.
322 _Base_bitset<_Nw>::_M_do_find_next(size_t __prev, size_t __not_found) const
324 // make bound inclusive
327 // check out of bounds
328 if (__prev >= _Nw * _GLIBCXX_BITSET_BITS_PER_WORD)
332 size_t __i = _S_whichword(__prev);
333 _WordT __thisword = _M_w[__i];
335 // mask off bits below bound
336 __thisword &= (~static_cast<_WordT>(0)) << _S_whichbit(__prev);
338 if (__thisword != static_cast<_WordT>(0))
339 return (__i * _GLIBCXX_BITSET_BITS_PER_WORD
340 + __builtin_ctzl(__thisword));
342 // check subsequent words
344 for (; __i < _Nw; __i++)
346 __thisword = _M_w[__i];
347 if (__thisword != static_cast<_WordT>(0))
348 return (__i * _GLIBCXX_BITSET_BITS_PER_WORD
349 + __builtin_ctzl(__thisword));
351 // not found, so return an indication of failure.
353 } // end _M_do_find_next
356 * Base class, specialization for a single word.
358 * See documentation for bitset.
361 struct _Base_bitset<1>
363 typedef unsigned long _WordT;
370 #ifdef __GXX_EXPERIMENTAL_CXX0X__
371 _Base_bitset(unsigned long long __val)
373 _Base_bitset(unsigned long __val)
379 _S_whichword(size_t __pos )
380 { return __pos / _GLIBCXX_BITSET_BITS_PER_WORD; }
383 _S_whichbyte(size_t __pos )
384 { return (__pos % _GLIBCXX_BITSET_BITS_PER_WORD) / __CHAR_BIT__; }
387 _S_whichbit(size_t __pos )
388 { return __pos % _GLIBCXX_BITSET_BITS_PER_WORD; }
391 _S_maskbit(size_t __pos )
392 { return (static_cast<_WordT>(1)) << _S_whichbit(__pos); }
399 _M_getword(size_t) const
411 _M_do_and(const _Base_bitset<1>& __x)
412 { _M_w &= __x._M_w; }
415 _M_do_or(const _Base_bitset<1>& __x)
416 { _M_w |= __x._M_w; }
419 _M_do_xor(const _Base_bitset<1>& __x)
420 { _M_w ^= __x._M_w; }
423 _M_do_left_shift(size_t __shift)
424 { _M_w <<= __shift; }
427 _M_do_right_shift(size_t __shift)
428 { _M_w >>= __shift; }
436 { _M_w = ~static_cast<_WordT>(0); }
443 _M_is_equal(const _Base_bitset<1>& __x) const
444 { return _M_w == __x._M_w; }
447 _M_are_all_aux() const
448 { return __builtin_popcountl(_M_w); }
452 { return _M_w != 0; }
456 { return __builtin_popcountl(_M_w); }
459 _M_do_to_ulong() const
462 #ifdef __GXX_EXPERIMENTAL_CXX0X__
464 _M_do_to_ullong() const
469 _M_do_find_first(size_t __not_found) const
472 return __builtin_ctzl(_M_w);
477 // find the next "on" bit that follows "prev"
479 _M_do_find_next(size_t __prev, size_t __not_found) const
482 if (__prev >= ((size_t) _GLIBCXX_BITSET_BITS_PER_WORD))
485 _WordT __x = _M_w >> __prev;
487 return __builtin_ctzl(__x) + __prev;
494 * Base class, specialization for no storage (zero-length %bitset).
496 * See documentation for bitset.
499 struct _Base_bitset<0>
501 typedef unsigned long _WordT;
506 #ifdef __GXX_EXPERIMENTAL_CXX0X__
507 _Base_bitset(unsigned long long)
509 _Base_bitset(unsigned long)
514 _S_whichword(size_t __pos )
515 { return __pos / _GLIBCXX_BITSET_BITS_PER_WORD; }
518 _S_whichbyte(size_t __pos )
519 { return (__pos % _GLIBCXX_BITSET_BITS_PER_WORD) / __CHAR_BIT__; }
522 _S_whichbit(size_t __pos )
523 { return __pos % _GLIBCXX_BITSET_BITS_PER_WORD; }
526 _S_maskbit(size_t __pos )
527 { return (static_cast<_WordT>(1)) << _S_whichbit(__pos); }
529 // This would normally give access to the data. The bounds-checking
530 // in the bitset class will prevent the user from getting this far,
531 // but (1) it must still return an lvalue to compile, and (2) the
532 // user might call _Unchecked_set directly, in which case this /needs/
533 // to fail. Let's not penalize zero-length users unless they actually
534 // make an unchecked call; all the memory ugliness is therefore
535 // localized to this single should-never-get-this-far function.
537 _M_getword(size_t) const
539 __throw_out_of_range(__N("_Base_bitset::_M_getword"));
548 _M_do_and(const _Base_bitset<0>&)
552 _M_do_or(const _Base_bitset<0>&)
556 _M_do_xor(const _Base_bitset<0>&)
560 _M_do_left_shift(size_t)
564 _M_do_right_shift(size_t)
579 // Are all empty bitsets equal to each other? Are they equal to
580 // themselves? How to compare a thing which has no state? What is
581 // the sound of one zero-length bitset clapping?
583 _M_is_equal(const _Base_bitset<0>&) const
587 _M_are_all_aux() const
599 _M_do_to_ulong() const
602 #ifdef __GXX_EXPERIMENTAL_CXX0X__
604 _M_do_to_ullong() const
608 // Normally "not found" is the size, but that could also be
609 // misinterpreted as an index in this corner case. Oh well.
611 _M_do_find_first(size_t) const
615 _M_do_find_next(size_t, size_t) const
620 // Helper class to zero out the unused high-order bits in the highest word.
621 template<size_t _Extrabits>
624 static void _S_do_sanitize(unsigned long& __val)
625 { __val &= ~((~static_cast<unsigned long>(0)) << _Extrabits); }
630 { static void _S_do_sanitize(unsigned long) {} };
633 * @brief The %bitset class represents a @e fixed-size sequence of bits.
635 * @ingroup containers
637 * (Note that %bitset does @e not meet the formal requirements of a
638 * <a href="tables.html#65">container</a>. Mainly, it lacks iterators.)
640 * The template argument, @a Nb, may be any non-negative number,
641 * specifying the number of bits (e.g., "0", "12", "1024*1024").
643 * In the general unoptimized case, storage is allocated in word-sized
644 * blocks. Let B be the number of bits in a word, then (Nb+(B-1))/B
645 * words will be used for storage. B - Nb%B bits are unused. (They are
646 * the high-order bits in the highest word.) It is a class invariant
647 * that those unused bits are always zero.
649 * If you think of %bitset as <em>a simple array of bits</em>, be
650 * aware that your mental picture is reversed: a %bitset behaves
651 * the same way as bits in integers do, with the bit at index 0 in
652 * the <em>least significant / right-hand</em> position, and the bit at
653 * index Nb-1 in the <em>most significant / left-hand</em> position.
654 * Thus, unlike other containers, a %bitset's index <em>counts from
655 * right to left</em>, to put it very loosely.
657 * This behavior is preserved when translating to and from strings. For
658 * example, the first line of the following program probably prints
659 * <em>b('a') is 0001100001</em> on a modern ASCII system.
663 * #include <iostream>
666 * using namespace std;
673 * cout << "b('a') is " << b << endl;
677 * string str = s.str();
678 * cout << "index 3 in the string is " << str[3] << " but\n"
679 * << "index 3 in the bitset is " << b[3] << endl;
684 * http://gcc.gnu.org/onlinedocs/libstdc++/manual/bk01pt12ch33s02.html
685 * for a description of extensions.
687 * Most of the actual code isn't contained in %bitset<> itself, but in the
688 * base class _Base_bitset. The base class works with whole words, not with
689 * individual bits. This allows us to specialize _Base_bitset for the
690 * important special case where the %bitset is only a single word.
692 * Extra confusion can result due to the fact that the storage for
693 * _Base_bitset @e is a regular array, and is indexed as such. This is
694 * carefully encapsulated.
698 : private _Base_bitset<_GLIBCXX_BITSET_WORDS(_Nb)>
701 typedef _Base_bitset<_GLIBCXX_BITSET_WORDS(_Nb)> _Base;
702 typedef unsigned long _WordT;
707 _Sanitize<_Nb % _GLIBCXX_BITSET_BITS_PER_WORD>::
708 _S_do_sanitize(this->_M_hiword());
713 * This encapsulates the concept of a single bit. An instance of this
714 * class is a proxy for an actual bit; this way the individual bit
715 * operations are done as faster word-size bitwise instructions.
717 * Most users will never need to use this class directly; conversions
718 * to and from bool are automatic and should be transparent. Overloaded
719 * operators help to preserve the illusion.
721 * (On a typical system, this <em>bit %reference</em> is 64
722 * times the size of an actual bit. Ha.)
735 reference(bitset& __b, size_t __pos)
737 _M_wp = &__b._M_getword(__pos);
738 _M_bpos = _Base::_S_whichbit(__pos);
749 *_M_wp |= _Base::_S_maskbit(_M_bpos);
751 *_M_wp &= ~_Base::_S_maskbit(_M_bpos);
755 // For b[i] = b[__j];
757 operator=(const reference& __j)
759 if ((*(__j._M_wp) & _Base::_S_maskbit(__j._M_bpos)))
760 *_M_wp |= _Base::_S_maskbit(_M_bpos);
762 *_M_wp &= ~_Base::_S_maskbit(_M_bpos);
769 { return (*(_M_wp) & _Base::_S_maskbit(_M_bpos)) == 0; }
772 operator bool() const
773 { return (*(_M_wp) & _Base::_S_maskbit(_M_bpos)) != 0; }
779 *_M_wp ^= _Base::_S_maskbit(_M_bpos);
783 friend class reference;
785 // 23.3.5.1 constructors:
786 /// All bits set to zero.
790 /// Initial bits bitwise-copied from a single word (others set to zero).
791 #ifdef __GXX_EXPERIMENTAL_CXX0X__
792 bitset(unsigned long long __val)
794 bitset(unsigned long __val)
797 { _M_do_sanitize(); }
800 * @brief Use a subset of a string.
801 * @param s A string of @a 0 and @a 1 characters.
802 * @param position Index of the first character in @a s to use;
804 * @throw std::out_of_range If @a pos is bigger the size of @a s.
805 * @throw std::invalid_argument If a character appears in the string
806 * which is neither @a 0 nor @a 1.
808 template<class _CharT, class _Traits, class _Alloc>
810 bitset(const std::basic_string<_CharT, _Traits, _Alloc>& __s,
811 size_t __position = 0)
814 if (__position > __s.size())
815 __throw_out_of_range(__N("bitset::bitset initial position "
817 _M_copy_from_string(__s, __position,
818 std::basic_string<_CharT, _Traits, _Alloc>::npos,
819 _CharT('0'), _CharT('1'));
823 * @brief Use a subset of a string.
824 * @param s A string of @a 0 and @a 1 characters.
825 * @param position Index of the first character in @a s to use.
826 * @param n The number of characters to copy.
827 * @throw std::out_of_range If @a pos is bigger the size of @a s.
828 * @throw std::invalid_argument If a character appears in the string
829 * which is neither @a 0 nor @a 1.
831 template<class _CharT, class _Traits, class _Alloc>
832 bitset(const std::basic_string<_CharT, _Traits, _Alloc>& __s,
833 size_t __position, size_t __n)
836 if (__position > __s.size())
837 __throw_out_of_range(__N("bitset::bitset initial position "
839 _M_copy_from_string(__s, __position, __n, _CharT('0'), _CharT('1'));
842 // _GLIBCXX_RESOLVE_LIB_DEFECTS
843 // 396. what are characters zero and one.
844 template<class _CharT, class _Traits, class _Alloc>
845 bitset(const std::basic_string<_CharT, _Traits, _Alloc>& __s,
846 size_t __position, size_t __n,
847 _CharT __zero, _CharT __one = _CharT('1'))
850 if (__position > __s.size())
851 __throw_out_of_range(__N("bitset::bitset initial position "
853 _M_copy_from_string(__s, __position, __n, __zero, __one);
856 #ifdef __GXX_EXPERIMENTAL_CXX0X__
858 * @brief Construct from a string.
859 * @param str A string of @a 0 and @a 1 characters.
860 * @throw std::invalid_argument If a character appears in the string
861 * which is neither @a 0 nor @a 1.
864 bitset(const char* __str)
868 __throw_logic_error(__N("bitset::bitset(const char*)"));
870 const size_t __len = __builtin_strlen(__str);
871 _M_copy_from_ptr<char, std::char_traits<char>>(__str, __len, 0,
876 // 23.3.5.2 bitset operations:
879 * @brief Operations on bitsets.
880 * @param rhs A same-sized bitset.
882 * These should be self-explanatory.
885 operator&=(const bitset<_Nb>& __rhs)
887 this->_M_do_and(__rhs);
892 operator|=(const bitset<_Nb>& __rhs)
894 this->_M_do_or(__rhs);
899 operator^=(const bitset<_Nb>& __rhs)
901 this->_M_do_xor(__rhs);
908 * @brief Operations on bitsets.
909 * @param position The number of places to shift.
911 * These should be self-explanatory.
914 operator<<=(size_t __position)
916 if (__builtin_expect(__position < _Nb, 1))
918 this->_M_do_left_shift(__position);
919 this->_M_do_sanitize();
927 operator>>=(size_t __position)
929 if (__builtin_expect(__position < _Nb, 1))
931 this->_M_do_right_shift(__position);
932 this->_M_do_sanitize();
942 * These versions of single-bit set, reset, flip, and test are
943 * extensions from the SGI version. They do no range checking.
944 * @ingroup SGIextensions
947 _Unchecked_set(size_t __pos)
949 this->_M_getword(__pos) |= _Base::_S_maskbit(__pos);
954 _Unchecked_set(size_t __pos, int __val)
957 this->_M_getword(__pos) |= _Base::_S_maskbit(__pos);
959 this->_M_getword(__pos) &= ~_Base::_S_maskbit(__pos);
964 _Unchecked_reset(size_t __pos)
966 this->_M_getword(__pos) &= ~_Base::_S_maskbit(__pos);
971 _Unchecked_flip(size_t __pos)
973 this->_M_getword(__pos) ^= _Base::_S_maskbit(__pos);
978 _Unchecked_test(size_t __pos) const
979 { return ((this->_M_getword(__pos) & _Base::_S_maskbit(__pos))
980 != static_cast<_WordT>(0)); }
983 // Set, reset, and flip.
985 * @brief Sets every bit to true.
991 this->_M_do_sanitize();
996 * @brief Sets a given bit to a particular value.
997 * @param position The index of the bit.
998 * @param val Either true or false, defaults to true.
999 * @throw std::out_of_range If @a pos is bigger the size of the %set.
1002 set(size_t __position, bool __val = true)
1004 if (__position >= _Nb)
1005 __throw_out_of_range(__N("bitset::set"));
1006 return _Unchecked_set(__position, __val);
1010 * @brief Sets every bit to false.
1015 this->_M_do_reset();
1020 * @brief Sets a given bit to false.
1021 * @param position The index of the bit.
1022 * @throw std::out_of_range If @a pos is bigger the size of the %set.
1024 * Same as writing @c set(pos,false).
1027 reset(size_t __position)
1029 if (__position >= _Nb)
1030 __throw_out_of_range(__N("bitset::reset"));
1031 return _Unchecked_reset(__position);
1035 * @brief Toggles every bit to its opposite value.
1041 this->_M_do_sanitize();
1046 * @brief Toggles a given bit to its opposite value.
1047 * @param position The index of the bit.
1048 * @throw std::out_of_range If @a pos is bigger the size of the %set.
1051 flip(size_t __position)
1053 if (__position >= _Nb)
1054 __throw_out_of_range(__N("bitset::flip"));
1055 return _Unchecked_flip(__position);
1058 /// See the no-argument flip().
1061 { return bitset<_Nb>(*this).flip(); }
1065 * @brief Array-indexing support.
1066 * @param position Index into the %bitset.
1067 * @return A bool for a <em>const %bitset</em>. For non-const bitsets, an
1068 * instance of the reference proxy class.
1069 * @note These operators do no range checking and throw no exceptions,
1070 * as required by DR 11 to the standard.
1072 * _GLIBCXX_RESOLVE_LIB_DEFECTS Note that this implementation already
1073 * resolves DR 11 (items 1 and 2), but does not do the range-checking
1074 * required by that DR's resolution. -pme
1075 * The DR has since been changed: range-checking is a precondition
1076 * (users' responsibility), and these functions must not throw. -pme
1079 operator[](size_t __position)
1080 { return reference(*this,__position); }
1083 operator[](size_t __position) const
1084 { return _Unchecked_test(__position); }
1088 * @brief Returns a numerical interpretation of the %bitset.
1089 * @return The integral equivalent of the bits.
1090 * @throw std::overflow_error If there are too many bits to be
1091 * represented in an @c unsigned @c long.
1095 { return this->_M_do_to_ulong(); }
1097 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1100 { return this->_M_do_to_ullong(); }
1104 * @brief Returns a character interpretation of the %bitset.
1105 * @return The string equivalent of the bits.
1107 * Note the ordering of the bits: decreasing character positions
1108 * correspond to increasing bit positions (see the main class notes for
1111 template<class _CharT, class _Traits, class _Alloc>
1112 std::basic_string<_CharT, _Traits, _Alloc>
1115 std::basic_string<_CharT, _Traits, _Alloc> __result;
1116 _M_copy_to_string(__result, _CharT('0'), _CharT('1'));
1120 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1121 // 396. what are characters zero and one.
1122 template<class _CharT, class _Traits, class _Alloc>
1123 std::basic_string<_CharT, _Traits, _Alloc>
1124 to_string(_CharT __zero, _CharT __one = _CharT('1')) const
1126 std::basic_string<_CharT, _Traits, _Alloc> __result;
1127 _M_copy_to_string(__result, __zero, __one);
1131 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1132 // 434. bitset::to_string() hard to use.
1133 template<class _CharT, class _Traits>
1134 std::basic_string<_CharT, _Traits, std::allocator<_CharT> >
1136 { return to_string<_CharT, _Traits, std::allocator<_CharT> >(); }
1138 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1139 // 853. to_string needs updating with zero and one.
1140 template<class _CharT, class _Traits>
1141 std::basic_string<_CharT, _Traits, std::allocator<_CharT> >
1142 to_string(_CharT __zero, _CharT __one = _CharT('1')) const
1143 { return to_string<_CharT, _Traits,
1144 std::allocator<_CharT> >(__zero, __one); }
1146 template<class _CharT>
1147 std::basic_string<_CharT, std::char_traits<_CharT>,
1148 std::allocator<_CharT> >
1151 return to_string<_CharT, std::char_traits<_CharT>,
1152 std::allocator<_CharT> >();
1155 template<class _CharT>
1156 std::basic_string<_CharT, std::char_traits<_CharT>,
1157 std::allocator<_CharT> >
1158 to_string(_CharT __zero, _CharT __one = _CharT('1')) const
1160 return to_string<_CharT, std::char_traits<_CharT>,
1161 std::allocator<_CharT> >(__zero, __one);
1164 std::basic_string<char, std::char_traits<char>, std::allocator<char> >
1167 return to_string<char, std::char_traits<char>,
1168 std::allocator<char> >();
1171 std::basic_string<char, std::char_traits<char>, std::allocator<char> >
1172 to_string(char __zero, char __one = '1') const
1174 return to_string<char, std::char_traits<char>,
1175 std::allocator<char> >(__zero, __one);
1178 // Helper functions for string operations.
1179 template<class _CharT, class _Traits>
1181 _M_copy_from_ptr(const _CharT*, size_t, size_t, size_t,
1184 template<class _CharT, class _Traits, class _Alloc>
1186 _M_copy_from_string(const std::basic_string<_CharT,
1187 _Traits, _Alloc>& __s, size_t __pos, size_t __n,
1188 _CharT __zero, _CharT __one)
1189 { _M_copy_from_ptr<_CharT, _Traits>(__s.data(), __s.size(), __pos, __n,
1192 template<class _CharT, class _Traits, class _Alloc>
1194 _M_copy_to_string(std::basic_string<_CharT, _Traits, _Alloc>&,
1195 _CharT, _CharT) const;
1197 // NB: Backward compat.
1198 template<class _CharT, class _Traits, class _Alloc>
1200 _M_copy_from_string(const std::basic_string<_CharT,
1201 _Traits, _Alloc>& __s, size_t __pos, size_t __n)
1202 { _M_copy_from_string(__s, __pos, __n, _CharT('0'), _CharT('1')); }
1204 template<class _CharT, class _Traits, class _Alloc>
1206 _M_copy_to_string(std::basic_string<_CharT, _Traits,_Alloc>& __s) const
1207 { _M_copy_to_string(__s, _CharT('0'), _CharT('1')); }
1209 /// Returns the number of bits which are set.
1212 { return this->_M_do_count(); }
1214 /// Returns the total number of bits.
1220 /// These comparisons for equality/inequality are, well, @e bitwise.
1222 operator==(const bitset<_Nb>& __rhs) const
1223 { return this->_M_is_equal(__rhs); }
1226 operator!=(const bitset<_Nb>& __rhs) const
1227 { return !this->_M_is_equal(__rhs); }
1231 * @brief Tests the value of a bit.
1232 * @param position The index of a bit.
1233 * @return The value at @a pos.
1234 * @throw std::out_of_range If @a pos is bigger the size of the %set.
1237 test(size_t __position) const
1239 if (__position >= _Nb)
1240 __throw_out_of_range(__N("bitset::test"));
1241 return _Unchecked_test(__position);
1244 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1245 // DR 693. std::bitset::all() missing.
1247 * @brief Tests whether all the bits are on.
1248 * @return True if all the bits are set.
1252 { return this->_M_are_all_aux() == _Nb; }
1255 * @brief Tests whether any of the bits are on.
1256 * @return True if at least one bit is set.
1260 { return this->_M_is_any(); }
1263 * @brief Tests whether any of the bits are on.
1264 * @return True if none of the bits are set.
1268 { return !this->_M_is_any(); }
1271 /// Self-explanatory.
1273 operator<<(size_t __position) const
1274 { return bitset<_Nb>(*this) <<= __position; }
1277 operator>>(size_t __position) const
1278 { return bitset<_Nb>(*this) >>= __position; }
1282 * @brief Finds the index of the first "on" bit.
1283 * @return The index of the first bit set, or size() if not found.
1284 * @ingroup SGIextensions
1289 { return this->_M_do_find_first(_Nb); }
1292 * @brief Finds the index of the next "on" bit after prev.
1293 * @return The index of the next bit set, or size() if not found.
1294 * @param prev Where to start searching.
1295 * @ingroup SGIextensions
1299 _Find_next(size_t __prev ) const
1300 { return this->_M_do_find_next(__prev, _Nb); }
1303 // Definitions of non-inline member functions.
1304 template<size_t _Nb>
1305 template<class _CharT, class _Traits>
1308 _M_copy_from_ptr(const _CharT* __s, size_t __len,
1309 size_t __pos, size_t __n, _CharT __zero, _CharT __one)
1312 const size_t __nbits = std::min(_Nb, std::min(__n, __len - __pos));
1313 for (size_t __i = __nbits; __i > 0; --__i)
1315 const _CharT __c = __s[__pos + __nbits - __i];
1316 if (_Traits::eq(__c, __zero))
1318 else if (_Traits::eq(__c, __one))
1319 _Unchecked_set(__i - 1);
1321 __throw_invalid_argument(__N("bitset::_M_copy_from_ptr"));
1325 template<size_t _Nb>
1326 template<class _CharT, class _Traits, class _Alloc>
1329 _M_copy_to_string(std::basic_string<_CharT, _Traits, _Alloc>& __s,
1330 _CharT __zero, _CharT __one) const
1332 __s.assign(_Nb, __zero);
1333 for (size_t __i = _Nb; __i > 0; --__i)
1334 if (_Unchecked_test(__i - 1))
1335 _Traits::assign(__s[_Nb - __i], __one);
1338 // 23.3.5.3 bitset operations:
1341 * @brief Global bitwise operations on bitsets.
1342 * @param x A bitset.
1343 * @param y A bitset of the same size as @a x.
1344 * @return A new bitset.
1346 * These should be self-explanatory.
1348 template<size_t _Nb>
1350 operator&(const bitset<_Nb>& __x, const bitset<_Nb>& __y)
1352 bitset<_Nb> __result(__x);
1357 template<size_t _Nb>
1359 operator|(const bitset<_Nb>& __x, const bitset<_Nb>& __y)
1361 bitset<_Nb> __result(__x);
1366 template <size_t _Nb>
1368 operator^(const bitset<_Nb>& __x, const bitset<_Nb>& __y)
1370 bitset<_Nb> __result(__x);
1378 * @brief Global I/O operators for bitsets.
1380 * Direct I/O between streams and bitsets is supported. Output is
1381 * straightforward. Input will skip whitespace, only accept @a 0 and @a 1
1382 * characters, and will only extract as many digits as the %bitset will
1385 template<class _CharT, class _Traits, size_t _Nb>
1386 std::basic_istream<_CharT, _Traits>&
1387 operator>>(std::basic_istream<_CharT, _Traits>& __is, bitset<_Nb>& __x)
1389 typedef typename _Traits::char_type char_type;
1390 typedef std::basic_istream<_CharT, _Traits> __istream_type;
1391 typedef typename __istream_type::ios_base __ios_base;
1393 std::basic_string<_CharT, _Traits> __tmp;
1396 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1397 // 303. Bitset input operator underspecified
1398 const char_type __zero = __is.widen('0');
1399 const char_type __one = __is.widen('1');
1401 typename __ios_base::iostate __state = __ios_base::goodbit;
1402 typename __istream_type::sentry __sentry(__is);
1407 for (size_t __i = _Nb; __i > 0; --__i)
1409 static typename _Traits::int_type __eof = _Traits::eof();
1411 typename _Traits::int_type __c1 = __is.rdbuf()->sbumpc();
1412 if (_Traits::eq_int_type(__c1, __eof))
1414 __state |= __ios_base::eofbit;
1419 const char_type __c2 = _Traits::to_char_type(__c1);
1420 if (_Traits::eq(__c2, __zero))
1421 __tmp.push_back(__zero);
1422 else if (_Traits::eq(__c2, __one))
1423 __tmp.push_back(__one);
1425 eq_int_type(__is.rdbuf()->sputbackc(__c2),
1428 __state |= __ios_base::failbit;
1434 __catch(__cxxabiv1::__forced_unwind&)
1436 __is._M_setstate(__ios_base::badbit);
1437 __throw_exception_again;
1440 { __is._M_setstate(__ios_base::badbit); }
1443 if (__tmp.empty() && _Nb)
1444 __state |= __ios_base::failbit;
1446 __x._M_copy_from_string(__tmp, static_cast<size_t>(0), _Nb,
1449 __is.setstate(__state);
1453 template <class _CharT, class _Traits, size_t _Nb>
1454 std::basic_ostream<_CharT, _Traits>&
1455 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1456 const bitset<_Nb>& __x)
1458 std::basic_string<_CharT, _Traits> __tmp;
1460 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1461 // 396. what are characters zero and one.
1462 const ctype<_CharT>& __ct = use_facet<ctype<_CharT> >(__os.getloc());
1463 __x._M_copy_to_string(__tmp, __ct.widen('0'), __ct.widen('1'));
1464 return __os << __tmp;
1468 _GLIBCXX_END_NESTED_NAMESPACE
1470 #undef _GLIBCXX_BITSET_WORDS
1471 #undef _GLIBCXX_BITSET_BITS_PER_WORD
1473 #ifdef _GLIBCXX_DEBUG
1474 # include <debug/bitset>
1477 #ifdef _GLIBCXX_PROFILE
1478 # include <profile/bitset>
1481 #endif /* _GLIBCXX_BITSET */