1 // <bitset> -*- C++ -*-
3 // Copyright (C) 2001, 2002, 2003 Free Software Foundation, Inc.
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 2, or (at your option)
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING. If not, write to the Free
18 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
21 // As a special exception, you may use this file as part of a free software
22 // library without restriction. Specifically, if other files instantiate
23 // templates or use macros or inline functions from this file, or you compile
24 // this file and link it with other files to produce an executable, this
25 // file does not by itself cause the resulting executable to be covered by
26 // the GNU General Public License. This exception does not however
27 // invalidate any other reasons why the executable file might be covered by
28 // the GNU General Public License.
32 * Silicon Graphics Computer Systems, Inc.
34 * Permission to use, copy, modify, distribute and sell this software
35 * and its documentation for any purpose is hereby granted without fee,
36 * provided that the above copyright notice appear in all copies and
37 * that both that copyright notice and this permission notice appear
38 * in supporting documentation. Silicon Graphics makes no
39 * representations about the suitability of this software for any
40 * purpose. It is provided "as is" without express or implied warranty.
44 * This is a Standard C++ Library header. You should @c #include this header
45 * in your programs, rather than any of the "st[dl]_*.h" implementation files.
48 #ifndef _GLIBCXX_BITSET
49 #define _GLIBCXX_BITSET 1
51 #pragma GCC system_header
53 #include <cstddef> // for size_t
54 #include <cstring> // for memset
55 #include <limits> // for numeric_limits
57 #include <bits/functexcept.h> // for invalid_argument, out_of_range,
59 #include <ostream> // for ostream (operator<<)
60 #include <istream> // for istream (operator>>)
63 #define _GLIBCXX_BITSET_BITS_PER_WORD numeric_limits<unsigned long>::digits
64 #define _GLIBCXX_BITSET_WORDS(__n) \
65 ((__n) < 1 ? 0 : ((__n) + _GLIBCXX_BITSET_BITS_PER_WORD - 1)/_GLIBCXX_BITSET_BITS_PER_WORD)
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.
85 _Base_bitset() { _M_do_reset(); }
86 _Base_bitset(unsigned long __val
)
93 _S_whichword(size_t __pos
)
94 { return __pos
/ _GLIBCXX_BITSET_BITS_PER_WORD
; }
97 _S_whichbyte(size_t __pos
)
98 { return (__pos
% _GLIBCXX_BITSET_BITS_PER_WORD
) / __CHAR_BIT__
; }
101 _S_whichbit(size_t __pos
)
102 { return __pos
% _GLIBCXX_BITSET_BITS_PER_WORD
; }
105 _S_maskbit(size_t __pos
)
106 { return (static_cast<_WordT
>(1)) << _S_whichbit(__pos
); }
109 _M_getword(size_t __pos
)
110 { return _M_w
[_S_whichword(__pos
)]; }
113 _M_getword(size_t __pos
) const
114 { return _M_w
[_S_whichword(__pos
)]; }
117 _M_hiword() { return _M_w
[_Nw
- 1]; }
120 _M_hiword() const { return _M_w
[_Nw
- 1]; }
123 _M_do_and(const _Base_bitset
<_Nw
>& __x
)
125 for (size_t __i
= 0; __i
< _Nw
; __i
++)
126 _M_w
[__i
] &= __x
._M_w
[__i
];
130 _M_do_or(const _Base_bitset
<_Nw
>& __x
)
132 for (size_t __i
= 0; __i
< _Nw
; __i
++)
133 _M_w
[__i
] |= __x
._M_w
[__i
];
137 _M_do_xor(const _Base_bitset
<_Nw
>& __x
)
139 for (size_t __i
= 0; __i
< _Nw
; __i
++)
140 _M_w
[__i
] ^= __x
._M_w
[__i
];
144 _M_do_left_shift(size_t __shift
);
147 _M_do_right_shift(size_t __shift
);
152 for (size_t __i
= 0; __i
< _Nw
; __i
++)
153 _M_w
[__i
] = ~_M_w
[__i
];
159 for (size_t __i
= 0; __i
< _Nw
; __i
++)
160 _M_w
[__i
] = ~static_cast<_WordT
>(0);
164 _M_do_reset() { memset(_M_w
, 0, _Nw
* sizeof(_WordT
)); }
167 _M_is_equal(const _Base_bitset
<_Nw
>& __x
) const
169 for (size_t __i
= 0; __i
< _Nw
; ++__i
)
171 if (_M_w
[__i
] != __x
._M_w
[__i
])
180 for (size_t __i
= 0; __i
< _Nw
; __i
++)
182 if (_M_w
[__i
] != static_cast<_WordT
>(0))
192 for (size_t __i
= 0; __i
< _Nw
; __i
++)
193 __result
+= __builtin_popcountl(_M_w
[__i
]);
198 _M_do_to_ulong() const;
200 // find first "on" bit
202 _M_do_find_first(size_t __not_found
) const;
204 // find the next "on" bit that follows "prev"
206 _M_do_find_next(size_t __prev
, size_t __not_found
) const;
209 // Definitions of non-inline functions from _Base_bitset.
212 _Base_bitset
<_Nw
>::_M_do_left_shift(size_t __shift
)
214 if (__builtin_expect(__shift
!= 0, 1))
216 const size_t __wshift
= __shift
/ _GLIBCXX_BITSET_BITS_PER_WORD
;
217 const size_t __offset
= __shift
% _GLIBCXX_BITSET_BITS_PER_WORD
;
220 for (size_t __n
= _Nw
- 1; __n
>= __wshift
; --__n
)
221 _M_w
[__n
] = _M_w
[__n
- __wshift
];
224 const size_t __sub_offset
= _GLIBCXX_BITSET_BITS_PER_WORD
- __offset
;
225 for (size_t __n
= _Nw
- 1; __n
> __wshift
; --__n
)
226 _M_w
[__n
] = (_M_w
[__n
- __wshift
] << __offset
) |
227 (_M_w
[__n
- __wshift
- 1] >> __sub_offset
);
228 _M_w
[__wshift
] = _M_w
[0] << __offset
;
231 std::fill(_M_w
+ 0, _M_w
+ __wshift
, static_cast<_WordT
>(0));
237 _Base_bitset
<_Nw
>::_M_do_right_shift(size_t __shift
)
239 if (__builtin_expect(__shift
!= 0, 1))
241 const size_t __wshift
= __shift
/ _GLIBCXX_BITSET_BITS_PER_WORD
;
242 const size_t __offset
= __shift
% _GLIBCXX_BITSET_BITS_PER_WORD
;
243 const size_t __limit
= _Nw
- __wshift
- 1;
246 for (size_t __n
= 0; __n
<= __limit
; ++__n
)
247 _M_w
[__n
] = _M_w
[__n
+ __wshift
];
250 const size_t __sub_offset
= _GLIBCXX_BITSET_BITS_PER_WORD
- __offset
;
251 for (size_t __n
= 0; __n
< __limit
; ++__n
)
252 _M_w
[__n
] = (_M_w
[__n
+ __wshift
] >> __offset
) |
253 (_M_w
[__n
+ __wshift
+ 1] << __sub_offset
);
254 _M_w
[__limit
] = _M_w
[_Nw
-1] >> __offset
;
257 std::fill(_M_w
+ __limit
+ 1, _M_w
+ _Nw
, static_cast<_WordT
>(0));
263 _Base_bitset
<_Nw
>::_M_do_to_ulong() const
265 for (size_t __i
= 1; __i
< _Nw
; ++__i
)
267 __throw_overflow_error(__N("_Base_bitset::_M_do_to_ulong"));
273 _Base_bitset
<_Nw
>::_M_do_find_first(size_t __not_found
) const
275 for (size_t __i
= 0; __i
< _Nw
; __i
++)
277 _WordT __thisword
= _M_w
[__i
];
278 if (__thisword
!= static_cast<_WordT
>(0))
279 return __i
* _GLIBCXX_BITSET_BITS_PER_WORD
280 + __builtin_ctzl(__thisword
);
282 // not found, so return an indication of failure.
288 _Base_bitset
<_Nw
>::_M_do_find_next(size_t __prev
, size_t __not_found
) const
290 // make bound inclusive
293 // check out of bounds
294 if ( __prev
>= _Nw
* _GLIBCXX_BITSET_BITS_PER_WORD
)
298 size_t __i
= _S_whichword(__prev
);
299 _WordT __thisword
= _M_w
[__i
];
301 // mask off bits below bound
302 __thisword
>>= __prev
+ 1;
304 if (__thisword
!= static_cast<_WordT
>(0))
305 return __i
* _GLIBCXX_BITSET_BITS_PER_WORD
306 + __builtin_ctzl(__thisword
);
308 // check subsequent words
310 for ( ; __i
< _Nw
; __i
++ )
312 __thisword
= _M_w
[__i
];
313 if (__thisword
!= static_cast<_WordT
>(0))
314 return __i
* _GLIBCXX_BITSET_BITS_PER_WORD
315 + __builtin_ctzl(__thisword
);
317 // not found, so return an indication of failure.
319 } // end _M_do_find_next
324 * Base class, specialization for a single word.
326 * See documentation for bitset.
330 struct _Base_bitset
<1>
332 typedef unsigned long _WordT
;
335 _Base_bitset( void ) : _M_w(0) {}
336 _Base_bitset(unsigned long __val
) : _M_w(__val
) {}
339 _S_whichword(size_t __pos
)
340 { return __pos
/ _GLIBCXX_BITSET_BITS_PER_WORD
; }
343 _S_whichbyte(size_t __pos
)
344 { return (__pos
% _GLIBCXX_BITSET_BITS_PER_WORD
) / __CHAR_BIT__
; }
347 _S_whichbit(size_t __pos
)
348 { return __pos
% _GLIBCXX_BITSET_BITS_PER_WORD
; }
351 _S_maskbit(size_t __pos
)
352 { return (static_cast<_WordT
>(1)) << _S_whichbit(__pos
); }
355 _M_getword(size_t) { return _M_w
; }
358 _M_getword(size_t) const { return _M_w
; }
361 _M_hiword() { return _M_w
; }
364 _M_hiword() const { return _M_w
; }
367 _M_do_and(const _Base_bitset
<1>& __x
) { _M_w
&= __x
._M_w
; }
370 _M_do_or(const _Base_bitset
<1>& __x
) { _M_w
|= __x
._M_w
; }
373 _M_do_xor(const _Base_bitset
<1>& __x
) { _M_w
^= __x
._M_w
; }
376 _M_do_left_shift(size_t __shift
) { _M_w
<<= __shift
; }
379 _M_do_right_shift(size_t __shift
) { _M_w
>>= __shift
; }
382 _M_do_flip() { _M_w
= ~_M_w
; }
385 _M_do_set() { _M_w
= ~static_cast<_WordT
>(0); }
388 _M_do_reset() { _M_w
= 0; }
391 _M_is_equal(const _Base_bitset
<1>& __x
) const
392 { return _M_w
== __x
._M_w
; }
395 _M_is_any() const { return _M_w
!= 0; }
398 _M_do_count() const { return __builtin_popcountl(_M_w
); }
401 _M_do_to_ulong() const { return _M_w
; }
404 _M_do_find_first(size_t __not_found
) const
407 return __builtin_ctzl(_M_w
);
412 // find the next "on" bit that follows "prev"
414 _M_do_find_next(size_t __prev
, size_t __not_found
) const
417 if (__prev
>= ((size_t) _GLIBCXX_BITSET_BITS_PER_WORD
))
420 _WordT __x
= _M_w
>> __prev
;
422 return __builtin_ctzl(__x
) + __prev
;
431 * Base class, specialization for no storage (zero-length %bitset).
433 * See documentation for bitset.
437 struct _Base_bitset
<0>
439 typedef unsigned long _WordT
;
442 _Base_bitset(unsigned long) {}
445 _S_whichword(size_t __pos
)
446 { return __pos
/ _GLIBCXX_BITSET_BITS_PER_WORD
; }
449 _S_whichbyte(size_t __pos
)
450 { return (__pos
% _GLIBCXX_BITSET_BITS_PER_WORD
) / __CHAR_BIT__
; }
453 _S_whichbit(size_t __pos
)
454 { return __pos
% _GLIBCXX_BITSET_BITS_PER_WORD
; }
457 _S_maskbit(size_t __pos
)
458 { return (static_cast<_WordT
>(1)) << _S_whichbit(__pos
); }
460 // This would normally give access to the data. The bounds-checking
461 // in the bitset class will prevent the user from getting this far,
462 // but (1) it must still return an lvalue to compile, and (2) the
463 // user might call _Unchecked_set directly, in which case this /needs/
464 // to fail. Let's not penalize zero-length users unless they actually
465 // make an unchecked call; all the memory ugliness is therefore
466 // localized to this single should-never-get-this-far function.
468 _M_getword(size_t) const
470 __throw_out_of_range(__N("_Base_bitset::_M_getword"));
475 _M_hiword() const { return 0; }
478 _M_do_and(const _Base_bitset
<0>&) { }
481 _M_do_or(const _Base_bitset
<0>&) { }
484 _M_do_xor(const _Base_bitset
<0>&) { }
487 _M_do_left_shift(size_t) { }
490 _M_do_right_shift(size_t) { }
501 // Are all empty bitsets equal to each other? Are they equal to
502 // themselves? How to compare a thing which has no state? What is
503 // the sound of one zero-length bitset clapping?
505 _M_is_equal(const _Base_bitset
<0>&) const { return true; }
508 _M_is_any() const { return false; }
511 _M_do_count() const { return 0; }
514 _M_do_to_ulong() const { return 0; }
516 // Normally "not found" is the size, but that could also be
517 // misinterpreted as an index in this corner case. Oh well.
519 _M_do_find_first(size_t) const { return 0; }
522 _M_do_find_next(size_t, size_t) const { return 0; }
526 // Helper class to zero out the unused high-order bits in the highest word.
527 template<size_t _Extrabits
>
530 static void _S_do_sanitize(unsigned long& __val
)
531 { __val
&= ~((~static_cast<unsigned long>(0)) << _Extrabits
); }
536 { static void _S_do_sanitize(unsigned long) { } };
540 * @brief The %bitset class represents a @e fixed-size sequence of bits.
542 * @ingroup Containers
544 * (Note that %bitset does @e not meet the formal requirements of a
545 * <a href="tables.html#65">container</a>. Mainly, it lacks iterators.)
547 * The template argument, @a Nb, may be any non-negative number,
548 * specifying the number of bits (e.g., "0", "12", "1024*1024").
550 * In the general unoptimized case, storage is allocated in word-sized
551 * blocks. Let B be the number of bits in a word, then (Nb+(B-1))/B
552 * words will be used for storage. B - Nb%B bits are unused. (They are
553 * the high-order bits in the highest word.) It is a class invariant
554 * that those unused bits are always zero.
556 * If you think of %bitset as "a simple array of bits," be aware that
557 * your mental picture is reversed: a %bitset behaves the same way as
558 * bits in integers do, with the bit at index 0 in the "least significant
559 * / right-hand" position, and the bit at index Nb-1 in the "most
560 * significant / left-hand" position. Thus, unlike other containers, a
561 * %bitset's index "counts from right to left," to put it very loosely.
563 * This behavior is preserved when translating to and from strings. For
564 * example, the first line of the following program probably prints
565 * "b('a') is 0001100001" on a modern ASCII system.
569 * #include <iostream>
572 * using namespace std;
579 * cout << "b('a') is " << b << endl;
583 * string str = s.str();
584 * cout << "index 3 in the string is " << str[3] << " but\n"
585 * << "index 3 in the bitset is " << b[3] << endl;
589 * Also see http://gcc.gnu.org/onlinedocs/libstdc++/ext/sgiexts.html#ch23
590 * for a description of extensions.
593 * Most of the actual code isn't contained in %bitset<> itself, but in the
594 * base class _Base_bitset. The base class works with whole words, not with
595 * individual bits. This allows us to specialize _Base_bitset for the
596 * important special case where the %bitset is only a single word.
598 * Extra confusion can result due to the fact that the storage for
599 * _Base_bitset @e is a regular array, and is indexed as such. This is
600 * carefully encapsulated.
604 class bitset
: private _Base_bitset
<_GLIBCXX_BITSET_WORDS(_Nb
)>
607 typedef _Base_bitset
<_GLIBCXX_BITSET_WORDS(_Nb
)> _Base
;
608 typedef unsigned long _WordT
;
613 _Sanitize
<_Nb
%_GLIBCXX_BITSET_BITS_PER_WORD
>::
614 _S_do_sanitize(this->_M_hiword());
619 * This encapsulates the concept of a single bit. An instance of this
620 * class is a proxy for an actual bit; this way the individual bit
621 * operations are done as faster word-size bitwise instructions.
623 * Most users will never need to use this class directly; conversions
624 * to and from bool are automatic and should be transparent. Overloaded
625 * operators help to preserve the illusion.
627 * (On a typical system, this "bit %reference" is 64 times the size of
628 * an actual bit. Ha.)
641 reference(bitset
& __b
, size_t __pos
)
643 _M_wp
= &__b
._M_getword(__pos
);
644 _M_bpos
= _Base::_S_whichbit(__pos
);
654 *_M_wp
|= _Base::_S_maskbit(_M_bpos
);
656 *_M_wp
&= ~_Base::_S_maskbit(_M_bpos
);
660 // for b[i] = b[__j];
662 operator=(const reference
& __j
)
664 if ( (*(__j
._M_wp
) & _Base::_S_maskbit(__j
._M_bpos
)) )
665 *_M_wp
|= _Base::_S_maskbit(_M_bpos
);
667 *_M_wp
&= ~_Base::_S_maskbit(_M_bpos
);
674 { return (*(_M_wp
) & _Base::_S_maskbit(_M_bpos
)) == 0; }
677 operator bool() const
678 { return (*(_M_wp
) & _Base::_S_maskbit(_M_bpos
)) != 0; }
684 *_M_wp
^= _Base::_S_maskbit(_M_bpos
);
688 friend class reference
;
690 // 23.3.5.1 constructors:
691 /// All bits set to zero.
694 /// Initial bits bitwise-copied from a single word (others set to zero).
695 bitset(unsigned long __val
) : _Base(__val
)
696 { _M_do_sanitize(); }
699 * @brief Use a subset of a string.
700 * @param s A string of '0' and '1' characters.
701 * @param position Index of the first character in @a s to use; defaults
703 * @throw std::out_of_range If @a pos is bigger the size of @a s.
704 * @throw std::invalid_argument If a character appears in the string
705 * which is neither '0' nor '1'.
707 template<class _CharT
, class _Traits
, class _Alloc
>
708 explicit bitset(const basic_string
<_CharT
, _Traits
, _Alloc
>& __s
,
709 size_t __position
= 0) : _Base()
711 if (__position
> __s
.size())
712 __throw_out_of_range("bitset::bitset initial position not valid");
713 _M_copy_from_string(__s
, __position
,
714 basic_string
<_CharT
, _Traits
, _Alloc
>::npos
);
718 * @brief Use a subset of a string.
719 * @param s A string of '0' and '1' characters.
720 * @param position Index of the first character in @a s to use.
721 * @param n The number of characters to copy.
722 * @throw std::out_of_range If @a pos is bigger the size of @a s.
723 * @throw std::invalid_argument If a character appears in the string
724 * which is neither '0' nor '1'.
726 template<class _CharT
, class _Traits
, class _Alloc
>
727 bitset(const basic_string
<_CharT
, _Traits
, _Alloc
>& __s
,
728 size_t __position
, size_t __n
) : _Base()
730 if (__position
> __s
.size())
731 __throw_out_of_range("bitset::bitset initial position not valid");
732 _M_copy_from_string(__s
, __position
, __n
);
735 // 23.3.5.2 bitset operations:
738 * @brief Operations on bitsets.
739 * @param rhs A same-sized bitset.
741 * These should be self-explanatory.
744 operator&=(const bitset
<_Nb
>& __rhs
)
746 this->_M_do_and(__rhs
);
751 operator|=(const bitset
<_Nb
>& __rhs
)
753 this->_M_do_or(__rhs
);
758 operator^=(const bitset
<_Nb
>& __rhs
)
760 this->_M_do_xor(__rhs
);
767 * @brief Operations on bitsets.
768 * @param position The number of places to shift.
770 * These should be self-explanatory.
773 operator<<=(size_t __position
)
775 if (__builtin_expect(__position
< _Nb
, 1))
777 this->_M_do_left_shift(__position
);
778 this->_M_do_sanitize();
786 operator>>=(size_t __position
)
788 if (__builtin_expect(__position
< _Nb
, 1))
790 this->_M_do_right_shift(__position
);
791 this->_M_do_sanitize();
801 * These versions of single-bit set, reset, flip, and test are
802 * extensions from the SGI version. They do no range checking.
803 * @ingroup SGIextensions
806 _Unchecked_set(size_t __pos
)
808 this->_M_getword(__pos
) |= _Base::_S_maskbit(__pos
);
813 _Unchecked_set(size_t __pos
, int __val
)
816 this->_M_getword(__pos
) |= _Base::_S_maskbit(__pos
);
818 this->_M_getword(__pos
) &= ~_Base::_S_maskbit(__pos
);
823 _Unchecked_reset(size_t __pos
)
825 this->_M_getword(__pos
) &= ~_Base::_S_maskbit(__pos
);
830 _Unchecked_flip(size_t __pos
)
832 this->_M_getword(__pos
) ^= _Base::_S_maskbit(__pos
);
837 _Unchecked_test(size_t __pos
) const
839 return (this->_M_getword(__pos
) & _Base::_S_maskbit(__pos
))
840 != static_cast<_WordT
>(0);
844 // Set, reset, and flip.
846 * @brief Sets every bit to true.
852 this->_M_do_sanitize();
857 * @brief Sets a given bit to a particular value.
858 * @param position The index of the bit.
859 * @param val Either true or false, defaults to true.
860 * @throw std::out_of_range If @a pos is bigger the size of the %set.
863 set(size_t __position
, bool __val
= true)
865 if (__position
>= _Nb
)
866 __throw_out_of_range(__N("bitset::set"));
867 return _Unchecked_set(__position
, __val
);
871 * @brief Sets every bit to false.
881 * @brief Sets a given bit to false.
882 * @param position The index of the bit.
883 * @throw std::out_of_range If @a pos is bigger the size of the %set.
885 * Same as writing @c set(pos,false).
888 reset(size_t __position
)
890 if (__position
>= _Nb
)
891 __throw_out_of_range(__N("bitset::reset"));
892 return _Unchecked_reset(__position
);
896 * @brief Toggles every bit to its opposite value.
902 this->_M_do_sanitize();
907 * @brief Toggles a given bit to its opposite value.
908 * @param position The index of the bit.
909 * @throw std::out_of_range If @a pos is bigger the size of the %set.
912 flip(size_t __position
)
914 if (__position
>= _Nb
)
915 __throw_out_of_range(__N("bitset::flip"));
916 return _Unchecked_flip(__position
);
919 /// See the no-argument flip().
921 operator~() const { return bitset
<_Nb
>(*this).flip(); }
925 * @brief Array-indexing support.
926 * @param position Index into the %bitset.
927 * @return A bool for a 'const %bitset'. For non-const bitsets, an
928 * instance of the reference proxy class.
929 * @note These operators do no range checking and throw no exceptions,
930 * as required by DR 11 to the standard.
933 * _GLIBCXX_RESOLVE_LIB_DEFECTS Note that this implementation already
934 * resolves DR 11 (items 1 and 2), but does not do the range-checking
935 * required by that DR's resolution. -pme
936 * The DR has since been changed: range-checking is a precondition
937 * (users' responsibility), and these functions must not throw. -pme
941 operator[](size_t __position
) { return reference(*this,__position
); }
944 operator[](size_t __position
) const { return _Unchecked_test(__position
); }
948 * @brief Retuns a numerical interpretation of the %bitset.
949 * @return The integral equivalent of the bits.
950 * @throw std::overflow_error If there are too many bits to be
951 * represented in an @c unsigned @c long.
954 to_ulong() const { return this->_M_do_to_ulong(); }
957 * @brief Retuns a character interpretation of the %bitset.
958 * @return The string equivalent of the bits.
960 * Note the ordering of the bits: decreasing character positions
961 * correspond to increasing bit positions (see the main class notes for
964 * Also note that you must specify the string's template parameters
965 * explicitly. Given a bitset @c bs and a string @s:
967 * s = bs.to_string<char,char_traits<char>,allocator<char> >();
970 template<class _CharT
, class _Traits
, class _Alloc
>
971 basic_string
<_CharT
, _Traits
, _Alloc
>
974 basic_string
<_CharT
, _Traits
, _Alloc
> __result
;
975 _M_copy_to_string(__result
);
979 // Helper functions for string operations.
980 template<class _CharT
, class _Traits
, class _Alloc
>
982 _M_copy_from_string(const basic_string
<_CharT
,_Traits
,_Alloc
>& __s
,
985 template<class _CharT
, class _Traits
, class _Alloc
>
987 _M_copy_to_string(basic_string
<_CharT
,_Traits
,_Alloc
>&) const;
989 /// Returns the number of bits which are set.
991 count() const { return this->_M_do_count(); }
993 /// Returns the total number of bits.
995 size() const { return _Nb
; }
998 /// These comparisons for equality/inequality are, well, @e bitwise.
1000 operator==(const bitset
<_Nb
>& __rhs
) const
1001 { return this->_M_is_equal(__rhs
); }
1004 operator!=(const bitset
<_Nb
>& __rhs
) const
1005 { return !this->_M_is_equal(__rhs
); }
1009 * @brief Tests the value of a bit.
1010 * @param position The index of a bit.
1011 * @return The value at @a pos.
1012 * @throw std::out_of_range If @a pos is bigger the size of the %set.
1015 test(size_t __position
) const
1017 if (__position
>= _Nb
)
1018 __throw_out_of_range(__N("bitset::test"));
1019 return _Unchecked_test(__position
);
1023 * @brief Tests whether any of the bits are on.
1024 * @return True if at least one bit is set.
1027 any() const { return this->_M_is_any(); }
1030 * @brief Tests whether any of the bits are on.
1031 * @return True if none of the bits are set.
1034 none() const { return !this->_M_is_any(); }
1037 /// Self-explanatory.
1039 operator<<(size_t __position
) const
1040 { return bitset
<_Nb
>(*this) <<= __position
; }
1043 operator>>(size_t __position
) const
1044 { return bitset
<_Nb
>(*this) >>= __position
; }
1048 * @brief Finds the index of the first "on" bit.
1049 * @return The index of the first bit set, or size() if not found.
1050 * @ingroup SGIextensions
1055 { return this->_M_do_find_first(_Nb
); }
1058 * @brief Finds the index of the next "on" bit after prev.
1059 * @return The index of the next bit set, or size() if not found.
1060 * @param prev Where to start searching.
1061 * @ingroup SGIextensions
1065 _Find_next(size_t __prev
) const
1066 { return this->_M_do_find_next(__prev
, _Nb
); }
1069 // Definitions of non-inline member functions.
1070 template<size_t _Nb
>
1071 template<class _CharT
, class _Traits
, class _Alloc
>
1073 bitset
<_Nb
>::_M_copy_from_string(const basic_string
<_CharT
,_Traits
,_Alloc
>& __s
, size_t __pos
, size_t __n
)
1076 const size_t __nbits
= std::min(_Nb
, std::min(__n
, __s
.size() - __pos
));
1077 for (size_t __i
= 0; __i
< __nbits
; ++__i
)
1079 switch(__s
[__pos
+ __nbits
- __i
- 1])
1087 __throw_invalid_argument("bitset::_M_copy_from_string");
1092 template<size_t _Nb
>
1093 template<class _CharT
, class _Traits
, class _Alloc
>
1095 bitset
<_Nb
>::_M_copy_to_string(basic_string
<_CharT
, _Traits
, _Alloc
>& __s
) const
1097 __s
.assign(_Nb
, '0');
1098 for (size_t __i
= 0; __i
< _Nb
; ++__i
)
1099 if (_Unchecked_test(__i
))
1100 __s
[_Nb
- 1 - __i
] = '1';
1103 // 23.3.5.3 bitset operations:
1106 * @brief Global bitwise operations on bitsets.
1107 * @param x A bitset.
1108 * @param y A bitset of the same size as @a x.
1109 * @return A new bitset.
1111 * These should be self-explanatory.
1113 template<size_t _Nb
>
1115 operator&(const bitset
<_Nb
>& __x
, const bitset
<_Nb
>& __y
)
1117 bitset
<_Nb
> __result(__x
);
1122 template<size_t _Nb
>
1124 operator|(const bitset
<_Nb
>& __x
, const bitset
<_Nb
>& __y
)
1126 bitset
<_Nb
> __result(__x
);
1131 template <size_t _Nb
>
1133 operator^(const bitset
<_Nb
>& __x
, const bitset
<_Nb
>& __y
)
1135 bitset
<_Nb
> __result(__x
);
1143 * @brief Global I/O operators for bitsets.
1145 * Direct I/O between streams and bitsets is supported. Output is
1146 * straightforward. Input will skip whitespace, only accept '0' and '1'
1147 * characters, and will only extract as many digits as the %bitset will
1150 template<class _CharT
, class _Traits
, size_t _Nb
>
1151 basic_istream
<_CharT
, _Traits
>&
1152 operator>>(basic_istream
<_CharT
, _Traits
>& __is
, bitset
<_Nb
>& __x
)
1154 typedef typename
_Traits::char_type char_type
;
1155 basic_string
<_CharT
, _Traits
> __tmp
;
1159 typename basic_istream
<_CharT
, _Traits
>::sentry
__sentry(__is
);
1162 ios_base::iostate __state
= ios_base::goodbit
;
1163 basic_streambuf
<_CharT
, _Traits
>* __buf
= __is
.rdbuf();
1164 for (size_t __i
= 0; __i
< _Nb
; ++__i
)
1166 static typename
_Traits::int_type __eof
= _Traits::eof();
1168 typename
_Traits::int_type __c1
= __buf
->sbumpc();
1169 if (_Traits::eq_int_type(__c1
, __eof
))
1171 __state
|= ios_base::eofbit
;
1176 char_type __c2
= _Traits::to_char_type(__c1
);
1177 char_type __c
= __is
.narrow(__c2
, '*');
1179 if (__c
== '0' || __c
== '1')
1180 __tmp
.push_back(__c
);
1181 else if (_Traits::eq_int_type(__buf
->sputbackc(__c2
), __eof
))
1183 __state
|= ios_base::failbit
;
1189 if (__tmp
.empty() && !_Nb
)
1190 __state
|= ios_base::failbit
;
1192 __x
._M_copy_from_string(__tmp
, static_cast<size_t>(0), _Nb
);
1194 if (__state
!= ios_base::goodbit
)
1195 __is
.setstate(__state
); // may throw an exception
1201 template <class _CharT
, class _Traits
, size_t _Nb
>
1202 basic_ostream
<_CharT
, _Traits
>&
1203 operator<<(basic_ostream
<_CharT
, _Traits
>& __os
, const bitset
<_Nb
>& __x
)
1205 basic_string
<_CharT
, _Traits
> __tmp
;
1206 __x
._M_copy_to_string(__tmp
);
1207 return __os
<< __tmp
;
1212 #undef _GLIBCXX_BITSET_WORDS
1213 #undef _GLIBCXX_BITSET_BITS_PER_WORD
1215 #endif /* _GLIBCXX_BITSET */