1 // <bitset> -*- C++ -*-
3 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
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 _Base_bitset(unsigned long __val)
86 _S_whichword(size_t __pos )
87 { return __pos / _GLIBCXX_BITSET_BITS_PER_WORD; }
90 _S_whichbyte(size_t __pos )
91 { return (__pos % _GLIBCXX_BITSET_BITS_PER_WORD) / __CHAR_BIT__; }
94 _S_whichbit(size_t __pos )
95 { return __pos % _GLIBCXX_BITSET_BITS_PER_WORD; }
98 _S_maskbit(size_t __pos )
99 { return (static_cast<_WordT>(1)) << _S_whichbit(__pos); }
102 _M_getword(size_t __pos)
103 { return _M_w[_S_whichword(__pos)]; }
106 _M_getword(size_t __pos) const
107 { return _M_w[_S_whichword(__pos)]; }
111 { return _M_w[_Nw - 1]; }
115 { return _M_w[_Nw - 1]; }
118 _M_do_and(const _Base_bitset<_Nw>& __x)
120 for (size_t __i = 0; __i < _Nw; __i++)
121 _M_w[__i] &= __x._M_w[__i];
125 _M_do_or(const _Base_bitset<_Nw>& __x)
127 for (size_t __i = 0; __i < _Nw; __i++)
128 _M_w[__i] |= __x._M_w[__i];
132 _M_do_xor(const _Base_bitset<_Nw>& __x)
134 for (size_t __i = 0; __i < _Nw; __i++)
135 _M_w[__i] ^= __x._M_w[__i];
139 _M_do_left_shift(size_t __shift);
142 _M_do_right_shift(size_t __shift);
147 for (size_t __i = 0; __i < _Nw; __i++)
148 _M_w[__i] = ~_M_w[__i];
154 for (size_t __i = 0; __i < _Nw; __i++)
155 _M_w[__i] = ~static_cast<_WordT>(0);
160 { __builtin_memset(_M_w, 0, _Nw * sizeof(_WordT)); }
163 _M_is_equal(const _Base_bitset<_Nw>& __x) const
165 for (size_t __i = 0; __i < _Nw; ++__i)
166 if (_M_w[__i] != __x._M_w[__i])
172 _M_are_all_aux() const
174 for (size_t __i = 0; __i < _Nw - 1; __i++)
175 if (_M_w[__i] != ~static_cast<_WordT>(0))
177 return ((_Nw - 1) * _GLIBCXX_BITSET_BITS_PER_WORD
178 + __builtin_popcountl(_M_hiword()));
184 for (size_t __i = 0; __i < _Nw; __i++)
185 if (_M_w[__i] != static_cast<_WordT>(0))
194 for (size_t __i = 0; __i < _Nw; __i++)
195 __result += __builtin_popcountl(_M_w[__i]);
200 _M_do_to_ulong() const;
202 // find first "on" bit
204 _M_do_find_first(size_t __not_found) const;
206 // find the next "on" bit that follows "prev"
208 _M_do_find_next(size_t __prev, size_t __not_found) const;
211 // Definitions of non-inline functions from _Base_bitset.
214 _Base_bitset<_Nw>::_M_do_left_shift(size_t __shift)
216 if (__builtin_expect(__shift != 0, 1))
218 const size_t __wshift = __shift / _GLIBCXX_BITSET_BITS_PER_WORD;
219 const size_t __offset = __shift % _GLIBCXX_BITSET_BITS_PER_WORD;
222 for (size_t __n = _Nw - 1; __n >= __wshift; --__n)
223 _M_w[__n] = _M_w[__n - __wshift];
226 const size_t __sub_offset = (_GLIBCXX_BITSET_BITS_PER_WORD
228 for (size_t __n = _Nw - 1; __n > __wshift; --__n)
229 _M_w[__n] = ((_M_w[__n - __wshift] << __offset)
230 | (_M_w[__n - __wshift - 1] >> __sub_offset));
231 _M_w[__wshift] = _M_w[0] << __offset;
234 std::fill(_M_w + 0, _M_w + __wshift, static_cast<_WordT>(0));
240 _Base_bitset<_Nw>::_M_do_right_shift(size_t __shift)
242 if (__builtin_expect(__shift != 0, 1))
244 const size_t __wshift = __shift / _GLIBCXX_BITSET_BITS_PER_WORD;
245 const size_t __offset = __shift % _GLIBCXX_BITSET_BITS_PER_WORD;
246 const size_t __limit = _Nw - __wshift - 1;
249 for (size_t __n = 0; __n <= __limit; ++__n)
250 _M_w[__n] = _M_w[__n + __wshift];
253 const size_t __sub_offset = (_GLIBCXX_BITSET_BITS_PER_WORD
255 for (size_t __n = 0; __n < __limit; ++__n)
256 _M_w[__n] = ((_M_w[__n + __wshift] >> __offset)
257 | (_M_w[__n + __wshift + 1] << __sub_offset));
258 _M_w[__limit] = _M_w[_Nw-1] >> __offset;
261 std::fill(_M_w + __limit + 1, _M_w + _Nw, static_cast<_WordT>(0));
267 _Base_bitset<_Nw>::_M_do_to_ulong() const
269 for (size_t __i = 1; __i < _Nw; ++__i)
271 __throw_overflow_error(__N("_Base_bitset::_M_do_to_ulong"));
277 _Base_bitset<_Nw>::_M_do_find_first(size_t __not_found) const
279 for (size_t __i = 0; __i < _Nw; __i++)
281 _WordT __thisword = _M_w[__i];
282 if (__thisword != static_cast<_WordT>(0))
283 return (__i * _GLIBCXX_BITSET_BITS_PER_WORD
284 + __builtin_ctzl(__thisword));
286 // not found, so return an indication of failure.
292 _Base_bitset<_Nw>::_M_do_find_next(size_t __prev, size_t __not_found) const
294 // make bound inclusive
297 // check out of bounds
298 if (__prev >= _Nw * _GLIBCXX_BITSET_BITS_PER_WORD)
302 size_t __i = _S_whichword(__prev);
303 _WordT __thisword = _M_w[__i];
305 // mask off bits below bound
306 __thisword &= (~static_cast<_WordT>(0)) << _S_whichbit(__prev);
308 if (__thisword != static_cast<_WordT>(0))
309 return (__i * _GLIBCXX_BITSET_BITS_PER_WORD
310 + __builtin_ctzl(__thisword));
312 // check subsequent words
314 for (; __i < _Nw; __i++)
316 __thisword = _M_w[__i];
317 if (__thisword != static_cast<_WordT>(0))
318 return (__i * _GLIBCXX_BITSET_BITS_PER_WORD
319 + __builtin_ctzl(__thisword));
321 // not found, so return an indication of failure.
323 } // end _M_do_find_next
326 * Base class, specialization for a single word.
328 * See documentation for bitset.
331 struct _Base_bitset<1>
333 typedef unsigned long _WordT;
340 _Base_bitset(unsigned long __val)
345 _S_whichword(size_t __pos )
346 { return __pos / _GLIBCXX_BITSET_BITS_PER_WORD; }
349 _S_whichbyte(size_t __pos )
350 { return (__pos % _GLIBCXX_BITSET_BITS_PER_WORD) / __CHAR_BIT__; }
353 _S_whichbit(size_t __pos )
354 { return __pos % _GLIBCXX_BITSET_BITS_PER_WORD; }
357 _S_maskbit(size_t __pos )
358 { return (static_cast<_WordT>(1)) << _S_whichbit(__pos); }
365 _M_getword(size_t) const
377 _M_do_and(const _Base_bitset<1>& __x)
378 { _M_w &= __x._M_w; }
381 _M_do_or(const _Base_bitset<1>& __x)
382 { _M_w |= __x._M_w; }
385 _M_do_xor(const _Base_bitset<1>& __x)
386 { _M_w ^= __x._M_w; }
389 _M_do_left_shift(size_t __shift)
390 { _M_w <<= __shift; }
393 _M_do_right_shift(size_t __shift)
394 { _M_w >>= __shift; }
402 { _M_w = ~static_cast<_WordT>(0); }
409 _M_is_equal(const _Base_bitset<1>& __x) const
410 { return _M_w == __x._M_w; }
413 _M_are_all_aux() const
414 { return __builtin_popcountl(_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;
454 * Base class, specialization for no storage (zero-length %bitset).
456 * See documentation for bitset.
459 struct _Base_bitset<0>
461 typedef unsigned long _WordT;
466 _Base_bitset(unsigned long)
470 _S_whichword(size_t __pos )
471 { return __pos / _GLIBCXX_BITSET_BITS_PER_WORD; }
474 _S_whichbyte(size_t __pos )
475 { return (__pos % _GLIBCXX_BITSET_BITS_PER_WORD) / __CHAR_BIT__; }
478 _S_whichbit(size_t __pos )
479 { return __pos % _GLIBCXX_BITSET_BITS_PER_WORD; }
482 _S_maskbit(size_t __pos )
483 { return (static_cast<_WordT>(1)) << _S_whichbit(__pos); }
485 // This would normally give access to the data. The bounds-checking
486 // in the bitset class will prevent the user from getting this far,
487 // but (1) it must still return an lvalue to compile, and (2) the
488 // user might call _Unchecked_set directly, in which case this /needs/
489 // to fail. Let's not penalize zero-length users unless they actually
490 // make an unchecked call; all the memory ugliness is therefore
491 // localized to this single should-never-get-this-far function.
493 _M_getword(size_t) const
495 __throw_out_of_range(__N("_Base_bitset::_M_getword"));
504 _M_do_and(const _Base_bitset<0>&)
508 _M_do_or(const _Base_bitset<0>&)
512 _M_do_xor(const _Base_bitset<0>&)
516 _M_do_left_shift(size_t)
520 _M_do_right_shift(size_t)
535 // Are all empty bitsets equal to each other? Are they equal to
536 // themselves? How to compare a thing which has no state? What is
537 // the sound of one zero-length bitset clapping?
539 _M_is_equal(const _Base_bitset<0>&) const
543 _M_are_all_aux() const
555 _M_do_to_ulong() const
558 // Normally "not found" is the size, but that could also be
559 // misinterpreted as an index in this corner case. Oh well.
561 _M_do_find_first(size_t) const
565 _M_do_find_next(size_t, size_t) const
570 // Helper class to zero out the unused high-order bits in the highest word.
571 template<size_t _Extrabits>
574 static void _S_do_sanitize(unsigned long& __val)
575 { __val &= ~((~static_cast<unsigned long>(0)) << _Extrabits); }
580 { static void _S_do_sanitize(unsigned long) {} };
583 * @brief The %bitset class represents a @e fixed-size sequence of bits.
585 * @ingroup containers
587 * (Note that %bitset does @e not meet the formal requirements of a
588 * <a href="tables.html#65">container</a>. Mainly, it lacks iterators.)
590 * The template argument, @a Nb, may be any non-negative number,
591 * specifying the number of bits (e.g., "0", "12", "1024*1024").
593 * In the general unoptimized case, storage is allocated in word-sized
594 * blocks. Let B be the number of bits in a word, then (Nb+(B-1))/B
595 * words will be used for storage. B - Nb%B bits are unused. (They are
596 * the high-order bits in the highest word.) It is a class invariant
597 * that those unused bits are always zero.
599 * If you think of %bitset as "a simple array of bits," be aware that
600 * your mental picture is reversed: a %bitset behaves the same way as
601 * bits in integers do, with the bit at index 0 in the "least significant
602 * / right-hand" position, and the bit at index Nb-1 in the "most
603 * significant / left-hand" position. Thus, unlike other containers, a
604 * %bitset's index "counts from right to left," to put it very loosely.
606 * This behavior is preserved when translating to and from strings. For
607 * example, the first line of the following program probably prints
608 * "b('a') is 0001100001" on a modern ASCII system.
612 * #include <iostream>
615 * using namespace std;
622 * cout << "b('a') is " << b << endl;
626 * string str = s.str();
627 * cout << "index 3 in the string is " << str[3] << " but\n"
628 * << "index 3 in the bitset is " << b[3] << endl;
633 * http://gcc.gnu.org/onlinedocs/libstdc++/manual/bk01pt12ch33s02.html
634 * for a description of extensions.
636 * Most of the actual code isn't contained in %bitset<> itself, but in the
637 * base class _Base_bitset. The base class works with whole words, not with
638 * individual bits. This allows us to specialize _Base_bitset for the
639 * important special case where the %bitset is only a single word.
641 * Extra confusion can result due to the fact that the storage for
642 * _Base_bitset @e is a regular array, and is indexed as such. This is
643 * 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,
764 _CharT('0'), _CharT('1'));
768 * @brief Use a subset of a string.
769 * @param s A string of '0' and '1' characters.
770 * @param position Index of the first character in @a s to use.
771 * @param n The number of characters to copy.
772 * @throw std::out_of_range If @a pos is bigger the size of @a s.
773 * @throw std::invalid_argument If a character appears in the string
774 * which is neither '0' nor '1'.
776 template<class _CharT, class _Traits, class _Alloc>
777 bitset(const std::basic_string<_CharT, _Traits, _Alloc>& __s,
778 size_t __position, size_t __n)
781 if (__position > __s.size())
782 __throw_out_of_range(__N("bitset::bitset initial position "
784 _M_copy_from_string(__s, __position, __n, _CharT('0'), _CharT('1'));
787 // _GLIBCXX_RESOLVE_LIB_DEFECTS
788 // 396. what are characters zero and one.
789 template<class _CharT, class _Traits, class _Alloc>
790 bitset(const std::basic_string<_CharT, _Traits, _Alloc>& __s,
791 size_t __position, size_t __n,
792 _CharT __zero, _CharT __one = _CharT('1'))
795 if (__position > __s.size())
796 __throw_out_of_range(__N("bitset::bitset initial position "
798 _M_copy_from_string(__s, __position, __n, __zero, __one);
801 // 23.3.5.2 bitset operations:
804 * @brief Operations on bitsets.
805 * @param rhs A same-sized bitset.
807 * These should be self-explanatory.
810 operator&=(const bitset<_Nb>& __rhs)
812 this->_M_do_and(__rhs);
817 operator|=(const bitset<_Nb>& __rhs)
819 this->_M_do_or(__rhs);
824 operator^=(const bitset<_Nb>& __rhs)
826 this->_M_do_xor(__rhs);
833 * @brief Operations on bitsets.
834 * @param position The number of places to shift.
836 * These should be self-explanatory.
839 operator<<=(size_t __position)
841 if (__builtin_expect(__position < _Nb, 1))
843 this->_M_do_left_shift(__position);
844 this->_M_do_sanitize();
852 operator>>=(size_t __position)
854 if (__builtin_expect(__position < _Nb, 1))
856 this->_M_do_right_shift(__position);
857 this->_M_do_sanitize();
867 * These versions of single-bit set, reset, flip, and test are
868 * extensions from the SGI version. They do no range checking.
869 * @ingroup SGIextensions
872 _Unchecked_set(size_t __pos)
874 this->_M_getword(__pos) |= _Base::_S_maskbit(__pos);
879 _Unchecked_set(size_t __pos, int __val)
882 this->_M_getword(__pos) |= _Base::_S_maskbit(__pos);
884 this->_M_getword(__pos) &= ~_Base::_S_maskbit(__pos);
889 _Unchecked_reset(size_t __pos)
891 this->_M_getword(__pos) &= ~_Base::_S_maskbit(__pos);
896 _Unchecked_flip(size_t __pos)
898 this->_M_getword(__pos) ^= _Base::_S_maskbit(__pos);
903 _Unchecked_test(size_t __pos) const
904 { return ((this->_M_getword(__pos) & _Base::_S_maskbit(__pos))
905 != static_cast<_WordT>(0)); }
908 // Set, reset, and flip.
910 * @brief Sets every bit to true.
916 this->_M_do_sanitize();
921 * @brief Sets a given bit to a particular value.
922 * @param position The index of the bit.
923 * @param val Either true or false, defaults to true.
924 * @throw std::out_of_range If @a pos is bigger the size of the %set.
927 set(size_t __position, bool __val = true)
929 if (__position >= _Nb)
930 __throw_out_of_range(__N("bitset::set"));
931 return _Unchecked_set(__position, __val);
935 * @brief Sets every bit to false.
945 * @brief Sets a given bit to false.
946 * @param position The index of the bit.
947 * @throw std::out_of_range If @a pos is bigger the size of the %set.
949 * Same as writing @c set(pos,false).
952 reset(size_t __position)
954 if (__position >= _Nb)
955 __throw_out_of_range(__N("bitset::reset"));
956 return _Unchecked_reset(__position);
960 * @brief Toggles every bit to its opposite value.
966 this->_M_do_sanitize();
971 * @brief Toggles a given bit to its opposite value.
972 * @param position The index of the bit.
973 * @throw std::out_of_range If @a pos is bigger the size of the %set.
976 flip(size_t __position)
978 if (__position >= _Nb)
979 __throw_out_of_range(__N("bitset::flip"));
980 return _Unchecked_flip(__position);
983 /// See the no-argument flip().
986 { return bitset<_Nb>(*this).flip(); }
990 * @brief Array-indexing support.
991 * @param position Index into the %bitset.
992 * @return A bool for a 'const %bitset'. For non-const bitsets, an
993 * instance of the reference proxy class.
994 * @note These operators do no range checking and throw no exceptions,
995 * as required by DR 11 to the standard.
997 * _GLIBCXX_RESOLVE_LIB_DEFECTS Note that this implementation already
998 * resolves DR 11 (items 1 and 2), but does not do the range-checking
999 * required by that DR's resolution. -pme
1000 * The DR has since been changed: range-checking is a precondition
1001 * (users' responsibility), and these functions must not throw. -pme
1004 operator[](size_t __position)
1005 { return reference(*this,__position); }
1008 operator[](size_t __position) const
1009 { return _Unchecked_test(__position); }
1013 * @brief Returns a numerical interpretation of the %bitset.
1014 * @return The integral equivalent of the bits.
1015 * @throw std::overflow_error If there are too many bits to be
1016 * represented in an @c unsigned @c long.
1020 { return this->_M_do_to_ulong(); }
1023 * @brief Returns a character interpretation of the %bitset.
1024 * @return The string equivalent of the bits.
1026 * Note the ordering of the bits: decreasing character positions
1027 * correspond to increasing bit positions (see the main class notes for
1030 template<class _CharT, class _Traits, class _Alloc>
1031 std::basic_string<_CharT, _Traits, _Alloc>
1034 std::basic_string<_CharT, _Traits, _Alloc> __result;
1035 _M_copy_to_string(__result, _CharT('0'), _CharT('1'));
1039 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1040 // 396. what are characters zero and one.
1041 template<class _CharT, class _Traits, class _Alloc>
1042 std::basic_string<_CharT, _Traits, _Alloc>
1043 to_string(_CharT __zero, _CharT __one = _CharT('1')) const
1045 std::basic_string<_CharT, _Traits, _Alloc> __result;
1046 _M_copy_to_string(__result, __zero, __one);
1050 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1051 // 434. bitset::to_string() hard to use.
1052 template<class _CharT, class _Traits>
1053 std::basic_string<_CharT, _Traits, std::allocator<_CharT> >
1055 { return to_string<_CharT, _Traits, std::allocator<_CharT> >(); }
1057 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1058 // 853. to_string needs updating with zero and one.
1059 template<class _CharT, class _Traits>
1060 std::basic_string<_CharT, _Traits, std::allocator<_CharT> >
1061 to_string(_CharT __zero, _CharT __one = _CharT('1')) const
1062 { return to_string<_CharT, _Traits,
1063 std::allocator<_CharT> >(__zero, __one); }
1065 template<class _CharT>
1066 std::basic_string<_CharT, std::char_traits<_CharT>,
1067 std::allocator<_CharT> >
1070 return to_string<_CharT, std::char_traits<_CharT>,
1071 std::allocator<_CharT> >();
1074 template<class _CharT>
1075 std::basic_string<_CharT, std::char_traits<_CharT>,
1076 std::allocator<_CharT> >
1077 to_string(_CharT __zero, _CharT __one = _CharT('1')) const
1079 return to_string<_CharT, std::char_traits<_CharT>,
1080 std::allocator<_CharT> >(__zero, __one);
1083 std::basic_string<char, std::char_traits<char>, std::allocator<char> >
1086 return to_string<char, std::char_traits<char>,
1087 std::allocator<char> >();
1090 std::basic_string<char, std::char_traits<char>, std::allocator<char> >
1091 to_string(char __zero, char __one = '1') const
1093 return to_string<char, std::char_traits<char>,
1094 std::allocator<char> >(__zero, __one);
1097 // Helper functions for string operations.
1098 template<class _CharT, class _Traits>
1100 _M_copy_from_ptr(const _CharT*, size_t, size_t, size_t,
1103 template<class _CharT, class _Traits, class _Alloc>
1105 _M_copy_from_string(const std::basic_string<_CharT,
1106 _Traits, _Alloc>& __s, size_t __pos, size_t __n,
1107 _CharT __zero, _CharT __one)
1108 { _M_copy_from_ptr<_CharT, _Traits>(__s.data(), __s.size(), __pos, __n,
1111 template<class _CharT, class _Traits, class _Alloc>
1113 _M_copy_to_string(std::basic_string<_CharT, _Traits, _Alloc>&,
1114 _CharT, _CharT) const;
1116 // NB: Backward compat.
1117 template<class _CharT, class _Traits, class _Alloc>
1119 _M_copy_from_string(const std::basic_string<_CharT,
1120 _Traits, _Alloc>& __s, size_t __pos, size_t __n)
1121 { _M_copy_from_string(__s, __pos, __n, _CharT('0'), _CharT('1')); }
1123 template<class _CharT, class _Traits, class _Alloc>
1125 _M_copy_to_string(std::basic_string<_CharT, _Traits,_Alloc>& __s) const
1126 { _M_copy_to_string(__s, _CharT('0'), _CharT('1')); }
1128 /// Returns the number of bits which are set.
1131 { return this->_M_do_count(); }
1133 /// Returns the total number of bits.
1139 /// These comparisons for equality/inequality are, well, @e bitwise.
1141 operator==(const bitset<_Nb>& __rhs) const
1142 { return this->_M_is_equal(__rhs); }
1145 operator!=(const bitset<_Nb>& __rhs) const
1146 { return !this->_M_is_equal(__rhs); }
1150 * @brief Tests the value of a bit.
1151 * @param position The index of a bit.
1152 * @return The value at @a pos.
1153 * @throw std::out_of_range If @a pos is bigger the size of the %set.
1156 test(size_t __position) const
1158 if (__position >= _Nb)
1159 __throw_out_of_range(__N("bitset::test"));
1160 return _Unchecked_test(__position);
1163 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1164 // DR 693. std::bitset::all() missing.
1166 * @brief Tests whether all the bits are on.
1167 * @return True if all the bits are set.
1171 { return this->_M_are_all_aux() == _Nb; }
1174 * @brief Tests whether any of the bits are on.
1175 * @return True if at least one bit is set.
1179 { return this->_M_is_any(); }
1182 * @brief Tests whether any of the bits are on.
1183 * @return True if none of the bits are set.
1187 { return !this->_M_is_any(); }
1190 /// Self-explanatory.
1192 operator<<(size_t __position) const
1193 { return bitset<_Nb>(*this) <<= __position; }
1196 operator>>(size_t __position) const
1197 { return bitset<_Nb>(*this) >>= __position; }
1201 * @brief Finds the index of the first "on" bit.
1202 * @return The index of the first bit set, or size() if not found.
1203 * @ingroup SGIextensions
1208 { return this->_M_do_find_first(_Nb); }
1211 * @brief Finds the index of the next "on" bit after prev.
1212 * @return The index of the next bit set, or size() if not found.
1213 * @param prev Where to start searching.
1214 * @ingroup SGIextensions
1218 _Find_next(size_t __prev ) const
1219 { return this->_M_do_find_next(__prev, _Nb); }
1222 // Definitions of non-inline member functions.
1223 template<size_t _Nb>
1224 template<class _CharT, class _Traits>
1227 _M_copy_from_ptr(const _CharT* __s, size_t __len,
1228 size_t __pos, size_t __n, _CharT __zero, _CharT __one)
1231 const size_t __nbits = std::min(_Nb, std::min(__n, __len - __pos));
1232 for (size_t __i = __nbits; __i > 0; --__i)
1234 const _CharT __c = __s[__pos + __nbits - __i];
1235 if (_Traits::eq(__c, __zero))
1237 else if (_Traits::eq(__c, __one))
1238 _Unchecked_set(__i - 1);
1240 __throw_invalid_argument(__N("bitset::_M_copy_from_ptr"));
1244 template<size_t _Nb>
1245 template<class _CharT, class _Traits, class _Alloc>
1248 _M_copy_to_string(std::basic_string<_CharT, _Traits, _Alloc>& __s,
1249 _CharT __zero, _CharT __one) const
1251 __s.assign(_Nb, __zero);
1252 for (size_t __i = _Nb; __i > 0; --__i)
1253 if (_Unchecked_test(__i - 1))
1254 _Traits::assign(__s[_Nb - __i], __one);
1257 // 23.3.5.3 bitset operations:
1260 * @brief Global bitwise operations on bitsets.
1261 * @param x A bitset.
1262 * @param y A bitset of the same size as @a x.
1263 * @return A new bitset.
1265 * These should be self-explanatory.
1267 template<size_t _Nb>
1269 operator&(const bitset<_Nb>& __x, const bitset<_Nb>& __y)
1271 bitset<_Nb> __result(__x);
1276 template<size_t _Nb>
1278 operator|(const bitset<_Nb>& __x, const bitset<_Nb>& __y)
1280 bitset<_Nb> __result(__x);
1285 template <size_t _Nb>
1287 operator^(const bitset<_Nb>& __x, const bitset<_Nb>& __y)
1289 bitset<_Nb> __result(__x);
1297 * @brief Global I/O operators for bitsets.
1299 * Direct I/O between streams and bitsets is supported. Output is
1300 * straightforward. Input will skip whitespace, only accept '0' and '1'
1301 * characters, and will only extract as many digits as the %bitset will
1304 template<class _CharT, class _Traits, size_t _Nb>
1305 std::basic_istream<_CharT, _Traits>&
1306 operator>>(std::basic_istream<_CharT, _Traits>& __is, bitset<_Nb>& __x)
1308 typedef typename _Traits::char_type char_type;
1309 typedef std::basic_istream<_CharT, _Traits> __istream_type;
1310 typedef typename __istream_type::ios_base __ios_base;
1312 std::basic_string<_CharT, _Traits> __tmp;
1315 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1316 // 303. Bitset input operator underspecified
1317 const char_type __zero = __is.widen('0');
1318 const char_type __one = __is.widen('1');
1320 typename __ios_base::iostate __state = __ios_base::goodbit;
1321 typename __istream_type::sentry __sentry(__is);
1326 for (size_t __i = _Nb; __i > 0; --__i)
1328 static typename _Traits::int_type __eof = _Traits::eof();
1330 typename _Traits::int_type __c1 = __is.rdbuf()->sbumpc();
1331 if (_Traits::eq_int_type(__c1, __eof))
1333 __state |= __ios_base::eofbit;
1338 const char_type __c2 = _Traits::to_char_type(__c1);
1339 if (_Traits::eq(__c2, __zero))
1340 __tmp.push_back(__zero);
1341 else if (_Traits::eq(__c2, __one))
1342 __tmp.push_back(__one);
1344 eq_int_type(__is.rdbuf()->sputbackc(__c2),
1347 __state |= __ios_base::failbit;
1353 __catch(__cxxabiv1::__forced_unwind&)
1355 __is._M_setstate(__ios_base::badbit);
1356 __throw_exception_again;
1359 { __is._M_setstate(__ios_base::badbit); }
1362 if (__tmp.empty() && _Nb)
1363 __state |= __ios_base::failbit;
1365 __x._M_copy_from_string(__tmp, static_cast<size_t>(0), _Nb,
1368 __is.setstate(__state);
1372 template <class _CharT, class _Traits, size_t _Nb>
1373 std::basic_ostream<_CharT, _Traits>&
1374 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1375 const bitset<_Nb>& __x)
1377 std::basic_string<_CharT, _Traits> __tmp;
1379 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1380 // 396. what are characters zero and one.
1381 const ctype<_CharT>& __ct = use_facet<ctype<_CharT> >(__os.getloc());
1382 __x._M_copy_to_string(__tmp, __ct.widen('0'), __ct.widen('1'));
1383 return __os << __tmp;
1387 _GLIBCXX_END_NESTED_NAMESPACE
1389 #undef _GLIBCXX_BITSET_WORDS
1390 #undef _GLIBCXX_BITSET_BITS_PER_WORD
1392 #ifdef _GLIBCXX_DEBUG
1393 # include <debug/bitset>
1396 #endif /* _GLIBCXX_BITSET */