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 _GLIBCPP_BITSET_H
49 #define _GLIBCPP_BITSET_H
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 _GLIBCPP_BITSET_BITS_PER_WORD numeric_limits<unsigned long>::digits
64 #define _GLIBCPP_BITSET_WORDS(__n) \
65 ((__n) < 1 ? 0 : ((__n) + _GLIBCPP_BITSET_BITS_PER_WORD - 1)/_GLIBCPP_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
/ _GLIBCPP_BITSET_BITS_PER_WORD
; }
97 _S_whichbyte(size_t __pos
)
98 { return (__pos
% _GLIBCPP_BITSET_BITS_PER_WORD
) / __CHAR_BIT__
; }
101 _S_whichbit(size_t __pos
)
102 { return __pos
% _GLIBCPP_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
/ _GLIBCPP_BITSET_BITS_PER_WORD
;
217 const size_t __offset
= __shift
% _GLIBCPP_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
= _GLIBCPP_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 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
/ _GLIBCPP_BITSET_BITS_PER_WORD
;
242 const size_t __offset
= __shift
% _GLIBCPP_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
= _GLIBCPP_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 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("bitset value is too large to fit in unsigned long"));
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
* _GLIBCPP_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
* _GLIBCPP_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
* _GLIBCPP_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
* _GLIBCPP_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
/ _GLIBCPP_BITSET_BITS_PER_WORD
; }
343 _S_whichbyte(size_t __pos
)
344 { return (__pos
% _GLIBCPP_BITSET_BITS_PER_WORD
) / __CHAR_BIT__
; }
347 _S_whichbit(size_t __pos
)
348 { return __pos
% _GLIBCPP_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) _GLIBCPP_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
/ _GLIBCPP_BITSET_BITS_PER_WORD
; }
449 _S_whichbyte(size_t __pos
)
450 { return (__pos
% _GLIBCPP_BITSET_BITS_PER_WORD
) / __CHAR_BIT__
; }
453 _S_whichbit(size_t __pos
)
454 { return __pos
% _GLIBCPP_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
469 { __throw_out_of_range(__N("bitset is zero-length")); return *new _WordT
; }
472 _M_hiword() const { return 0; }
475 _M_do_and(const _Base_bitset
<0>&) { }
478 _M_do_or(const _Base_bitset
<0>&) { }
481 _M_do_xor(const _Base_bitset
<0>&) { }
484 _M_do_left_shift(size_t) { }
487 _M_do_right_shift(size_t) { }
498 // Are all empty bitsets equal to each other? Are they equal to
499 // themselves? How to compare a thing which has no state? What is
500 // the sound of one zero-length bitset clapping?
502 _M_is_equal(const _Base_bitset
<0>&) const { return true; }
505 _M_is_any() const { return false; }
508 _M_do_count() const { return 0; }
511 _M_do_to_ulong() const { return 0; }
513 // Normally "not found" is the size, but that could also be
514 // misinterpreted as an index in this corner case. Oh well.
516 _M_do_find_first(size_t) const { return 0; }
519 _M_do_find_next(size_t, size_t) const { return 0; }
523 // Helper class to zero out the unused high-order bits in the highest word.
524 template<size_t _Extrabits
>
527 static void _S_do_sanitize(unsigned long& __val
)
528 { __val
&= ~((~static_cast<unsigned long>(0)) << _Extrabits
); }
533 { static void _S_do_sanitize(unsigned long) { } };
537 * @brief The %bitset class represents a @e fixed-size sequence of bits.
539 * @ingroup Containers
541 * (Note that %bitset does @e not meet the formal requirements of a
542 * <a href="tables.html#65">container</a>. Mainly, it lacks iterators.)
544 * The template argument, @a Nb, may be any non-negative number,
545 * specifying the number of bits (e.g., "0", "12", "1024*1024").
547 * In the general unoptimized case, storage is allocated in word-sized
548 * blocks. Let B be the number of bits in a word, then (Nb+(B-1))/B
549 * words will be used for storage. B - Nb%B bits are unused. (They are
550 * the high-order bits in the highest word.) It is a class invariant
551 * that those unused bits are always zero.
553 * If you think of %bitset as "a simple array of bits," be aware that
554 * your mental picture is reversed: a %bitset behaves the same way as
555 * bits in integers do, with the bit at index 0 in the "least significant
556 * / right-hand" position, and the bit at index Nb-1 in the "most
557 * significant / left-hand" position. Thus, unlike other containers, a
558 * %bitset's index "counts from right to left," to put it very loosely.
560 * This behavior is preserved when translating to and from strings. For
561 * example, the first line of the following program probably prints
562 * "b('a') is 0001100001" on a modern ASCII system.
566 * #include <iostream>
569 * using namespace std;
576 * cout << "b('a') is " << b << endl;
580 * string str = s.str();
581 * cout << "index 3 in the string is " << str[3] << " but\n"
582 * << "index 3 in the bitset is " << b[3] << endl;
586 * Also see http://gcc.gnu.org/onlinedocs/libstdc++/ext/sgiexts.html#ch23
587 * for a description of extensions.
590 * Most of the actual code isn't contained in %bitset<> itself, but in the
591 * base class _Base_bitset. The base class works with whole words, not with
592 * individual bits. This allows us to specialize _Base_bitset for the
593 * important special case where the %bitset is only a single word.
595 * Extra confusion can result due to the fact that the storage for
596 * _Base_bitset @e is a regular array, and is indexed as such. This is
597 * carefully encapsulated.
601 class bitset
: private _Base_bitset
<_GLIBCPP_BITSET_WORDS(_Nb
)>
604 typedef _Base_bitset
<_GLIBCPP_BITSET_WORDS(_Nb
)> _Base
;
605 typedef unsigned long _WordT
;
610 _Sanitize
<_Nb
%_GLIBCPP_BITSET_BITS_PER_WORD
>::
611 _S_do_sanitize(this->_M_hiword());
616 * This encapsulates the concept of a single bit. An instance of this
617 * class is a proxy for an actual bit; this way the individual bit
618 * operations are done as faster word-size bitwise instructions.
620 * Most users will never need to use this class directly; conversions
621 * to and from bool are automatic and should be transparent. Overloaded
622 * operators help to preserve the illusion.
624 * (On a typical system, this "bit %reference" is 64 times the size of
625 * an actual bit. Ha.)
638 reference(bitset
& __b
, size_t __pos
)
640 _M_wp
= &__b
._M_getword(__pos
);
641 _M_bpos
= _Base::_S_whichbit(__pos
);
651 *_M_wp
|= _Base::_S_maskbit(_M_bpos
);
653 *_M_wp
&= ~_Base::_S_maskbit(_M_bpos
);
657 // for b[i] = b[__j];
659 operator=(const reference
& __j
)
661 if ( (*(__j
._M_wp
) & _Base::_S_maskbit(__j
._M_bpos
)) )
662 *_M_wp
|= _Base::_S_maskbit(_M_bpos
);
664 *_M_wp
&= ~_Base::_S_maskbit(_M_bpos
);
671 { return (*(_M_wp
) & _Base::_S_maskbit(_M_bpos
)) == 0; }
674 operator bool() const
675 { return (*(_M_wp
) & _Base::_S_maskbit(_M_bpos
)) != 0; }
681 *_M_wp
^= _Base::_S_maskbit(_M_bpos
);
685 friend class reference
;
687 // 23.3.5.1 constructors:
688 /// All bits set to zero.
691 /// Initial bits bitwise-copied from a single word (others set to zero).
692 bitset(unsigned long __val
) : _Base(__val
)
693 { _M_do_sanitize(); }
696 * @brief Use a subset of a string.
697 * @param s A string of '0' and '1' characters.
698 * @param pos Index of the first character in @a s to use; defaults
700 * @throw std::out_of_range If @a pos is bigger the size of @a s.
701 * @throw std::invalid_argument If a character appears in the string
702 * which is neither '0' nor '1'.
704 template<class _CharT
, class _Traits
, class _Alloc
>
705 explicit bitset(const basic_string
<_CharT
, _Traits
, _Alloc
>& __s
,
706 size_t __pos
= 0) : _Base()
708 if (__pos
> __s
.size())
709 __throw_out_of_range("bitset -- initial position is larger than "
710 "the string itself");
711 _M_copy_from_string(__s
, __pos
,
712 basic_string
<_CharT
, _Traits
, _Alloc
>::npos
);
716 * @brief Use a subset of a string.
717 * @param s A string of '0' and '1' characters.
718 * @param pos Index of the first character in @a s to use.
719 * @param n The number of characters to copy.
720 * @throw std::out_of_range If @a pos is bigger the size of @a s.
721 * @throw std::invalid_argument If a character appears in the string
722 * which is neither '0' nor '1'.
724 template<class _CharT
, class _Traits
, class _Alloc
>
725 bitset(const basic_string
<_CharT
, _Traits
, _Alloc
>& __s
,
726 size_t __pos
, size_t __n
) : _Base()
728 if (__pos
> __s
.size())
729 __throw_out_of_range("bitset -- initial position is larger than "
730 "the string itself");
731 _M_copy_from_string(__s
, __pos
, __n
);
734 // 23.3.5.2 bitset operations:
737 * @brief Operations on bitsets.
738 * @param rhs A same-sized bitset.
740 * These should be self-explanatory.
743 operator&=(const bitset
<_Nb
>& __rhs
)
745 this->_M_do_and(__rhs
);
750 operator|=(const bitset
<_Nb
>& __rhs
)
752 this->_M_do_or(__rhs
);
757 operator^=(const bitset
<_Nb
>& __rhs
)
759 this->_M_do_xor(__rhs
);
766 * @brief Operations on bitsets.
767 * @param pos The number of places to shift.
769 * These should be self-explanatory.
772 operator<<=(size_t __pos
)
774 if (__builtin_expect(__pos
< _Nb
, 1))
776 this->_M_do_left_shift(__pos
);
777 this->_M_do_sanitize();
785 operator>>=(size_t __pos
)
787 if (__builtin_expect(__pos
< _Nb
, 1))
789 this->_M_do_right_shift(__pos
);
790 this->_M_do_sanitize();
800 * These versions of single-bit set, reset, flip, and test are
801 * extensions from the SGI version. They do no range checking.
802 * @ingroup SGIextensions
805 _Unchecked_set(size_t __pos
)
807 this->_M_getword(__pos
) |= _Base::_S_maskbit(__pos
);
812 _Unchecked_set(size_t __pos
, int __val
)
815 this->_M_getword(__pos
) |= _Base::_S_maskbit(__pos
);
817 this->_M_getword(__pos
) &= ~_Base::_S_maskbit(__pos
);
822 _Unchecked_reset(size_t __pos
)
824 this->_M_getword(__pos
) &= ~_Base::_S_maskbit(__pos
);
829 _Unchecked_flip(size_t __pos
)
831 this->_M_getword(__pos
) ^= _Base::_S_maskbit(__pos
);
836 _Unchecked_test(size_t __pos
) const
838 return (this->_M_getword(__pos
) & _Base::_S_maskbit(__pos
))
839 != static_cast<_WordT
>(0);
843 // Set, reset, and flip.
845 * @brief Sets every bit to true.
851 this->_M_do_sanitize();
856 * @brief Sets a given bit to a particular value.
857 * @param pos The index of the bit.
858 * @param val Either true or false, defaults to true.
859 * @throw std::out_of_range If @a pos is bigger the size of the %set.
862 set(size_t __pos
, bool __val
= true)
865 __throw_out_of_range(__N("bitset::set() argument too large"));
866 return _Unchecked_set(__pos
, __val
);
870 * @brief Sets every bit to false.
880 * @brief Sets a given bit to false.
881 * @param pos The index of the bit.
882 * @throw std::out_of_range If @a pos is bigger the size of the %set.
884 * Same as writing @c set(pos,false).
890 __throw_out_of_range(__N("bitset::reset() argument too large"));
891 return _Unchecked_reset(__pos
);
895 * @brief Toggles every bit to its opposite value.
901 this->_M_do_sanitize();
906 * @brief Toggles a given bit to its opposite value.
907 * @param pos The index of the bit.
908 * @throw std::out_of_range If @a pos is bigger the size of the %set.
914 __throw_out_of_range(__N("bitset::flip() argument too large"));
915 return _Unchecked_flip(__pos
);
918 /// See the no-argument flip().
920 operator~() const { return bitset
<_Nb
>(*this).flip(); }
924 * @brief Array-indexing support.
925 * @param pos Index into the %bitset.
926 * @return A bool for a 'const %bitset'. For non-const bitsets, an
927 * instance of the reference proxy class.
928 * @note These operators do no range checking and throw no exceptions,
929 * as required by DR 11 to the standard.
932 * _GLIBCPP_RESOLVE_LIB_DEFECTS Note that this implementation already
933 * resolves DR 11 (items 1 and 2), but does not do the range-checking
934 * required by that DR's resolution. -pme
935 * The DR has since been changed: range-checking is a precondition
936 * (users' responsibility), and these functions must not throw. -pme
940 operator[](size_t __pos
) { return reference(*this,__pos
); }
943 operator[](size_t __pos
) const { return _Unchecked_test(__pos
); }
947 * @brief Retuns a numerical interpretation of the %bitset.
948 * @return The integral equivalent of the bits.
949 * @throw std::overflow_error If there are too many bits to be
950 * represented in an @c unsigned @c long.
953 to_ulong() const { return this->_M_do_to_ulong(); }
956 * @brief Retuns a character interpretation of the %bitset.
957 * @return The string equivalent of the bits.
959 * Note the ordering of the bits: decreasing character positions
960 * correspond to increasing bit positions (see the main class notes for
963 * Also note that you must specify the string's template parameters
964 * explicitly. Given a bitset @c bs and a string @s:
966 * s = bs.to_string<char,char_traits<char>,allocator<char> >();
969 template<class _CharT
, class _Traits
, class _Alloc
>
970 basic_string
<_CharT
, _Traits
, _Alloc
>
973 basic_string
<_CharT
, _Traits
, _Alloc
> __result
;
974 _M_copy_to_string(__result
);
978 // Helper functions for string operations.
979 template<class _CharT
, class _Traits
, class _Alloc
>
981 _M_copy_from_string(const basic_string
<_CharT
,_Traits
,_Alloc
>& __s
,
984 template<class _CharT
, class _Traits
, class _Alloc
>
986 _M_copy_to_string(basic_string
<_CharT
,_Traits
,_Alloc
>&) const;
988 /// Returns the number of bits which are set.
990 count() const { return this->_M_do_count(); }
992 /// Returns the total number of bits.
994 size() const { return _Nb
; }
997 /// These comparisons for equality/inequality are, well, @e bitwise.
999 operator==(const bitset
<_Nb
>& __rhs
) const
1000 { return this->_M_is_equal(__rhs
); }
1003 operator!=(const bitset
<_Nb
>& __rhs
) const
1004 { return !this->_M_is_equal(__rhs
); }
1008 * @brief Tests the value of a bit.
1009 * @param pos The index of a bit.
1010 * @return The value at @a pos.
1011 * @throw std::out_of_range If @a pos is bigger the size of the %set.
1014 test(size_t __pos
) const
1017 __throw_out_of_range(__N("bitset::test() argument too large"));
1018 return _Unchecked_test(__pos
);
1022 * @brief Tests whether any of the bits are on.
1023 * @return True if at least one bit is set.
1026 any() const { return this->_M_is_any(); }
1029 * @brief Tests whether any of the bits are on.
1030 * @return True if none of the bits are set.
1033 none() const { return !this->_M_is_any(); }
1036 /// Self-explanatory.
1038 operator<<(size_t __pos
) const
1039 { return bitset
<_Nb
>(*this) <<= __pos
; }
1042 operator>>(size_t __pos
) const
1043 { return bitset
<_Nb
>(*this) >>= __pos
; }
1047 * @brief Finds the index of the first "on" bit.
1048 * @return The index of the first bit set, or size() if not found.
1049 * @ingroup SGIextensions
1054 { return this->_M_do_find_first(_Nb
); }
1057 * @brief Finds the index of the next "on" bit after prev.
1058 * @return The index of the next bit set, or size() if not found.
1059 * @param prev Where to start searching.
1060 * @ingroup SGIextensions
1064 _Find_next(size_t __prev
) const
1065 { return this->_M_do_find_next(__prev
, _Nb
); }
1068 // Definitions of non-inline member functions.
1069 template<size_t _Nb
>
1070 template<class _CharT
, class _Traits
, class _Alloc
>
1072 bitset
<_Nb
>::_M_copy_from_string(const basic_string
<_CharT
,_Traits
,_Alloc
>& __s
, size_t __pos
, size_t __n
)
1075 const size_t __nbits
= std::min(_Nb
, std::min(__n
, __s
.size() - __pos
));
1076 for (size_t __i
= 0; __i
< __nbits
; ++__i
)
1078 switch(__s
[__pos
+ __nbits
- __i
- 1])
1086 __throw_invalid_argument("bitset -- string contains characters "
1087 "which are neither 0 nor 1");
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 _GLIBCPP_BITSET_WORDS
1213 #undef _GLIBCPP_BITSET_BITS_PER_WORD
1215 #endif /* _GLIBCPP_BITSET_H */