Merge from mainline (gomp-merge-2005-02-26).
[official-gcc.git] / libstdc++-v3 / include / std / std_bitset.h
blobdc677786a2de77f53a1979d0bb5b797502ab71f9
1 // <bitset> -*- C++ -*-
3 // Copyright (C) 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
4 //
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)
9 // any later version.
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,
19 // USA.
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.
31 * Copyright (c) 1998
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.
43 /** @file bitset
44 * This is a Standard C++ Library header.
47 #ifndef _GLIBCXX_BITSET
48 #define _GLIBCXX_BITSET 1
50 #pragma GCC system_header
52 #include <cstddef> // For size_t
53 #include <cstring> // For memset
54 #include <limits> // For numeric_limits
55 #include <string>
56 #include <bits/functexcept.h> // For invalid_argument, out_of_range,
57 // overflow_error
58 #include <ostream> // For ostream (operator<<)
59 #include <istream> // For istream (operator>>)
61 #define _GLIBCXX_BITSET_BITS_PER_WORD numeric_limits<unsigned long>::digits
62 #define _GLIBCXX_BITSET_WORDS(__n) \
63 ((__n) < 1 ? 0 : ((__n) + _GLIBCXX_BITSET_BITS_PER_WORD - 1) \
64 / _GLIBCXX_BITSET_BITS_PER_WORD)
66 namespace _GLIBCXX_STD
68 /**
69 * @if maint
70 * Base class, general case. It is a class inveriant that _Nw will be
71 * nonnegative.
73 * See documentation for bitset.
74 * @endif
76 template<size_t _Nw>
77 struct _Base_bitset
79 typedef unsigned long _WordT;
81 /// 0 is the least significant word.
82 _WordT _M_w[_Nw];
84 _Base_bitset()
85 { _M_do_reset(); }
87 _Base_bitset(unsigned long __val)
89 _M_do_reset();
90 _M_w[0] = __val;
93 static size_t
94 _S_whichword(size_t __pos )
95 { return __pos / _GLIBCXX_BITSET_BITS_PER_WORD; }
97 static size_t
98 _S_whichbyte(size_t __pos )
99 { return (__pos % _GLIBCXX_BITSET_BITS_PER_WORD) / __CHAR_BIT__; }
101 static size_t
102 _S_whichbit(size_t __pos )
103 { return __pos % _GLIBCXX_BITSET_BITS_PER_WORD; }
105 static _WordT
106 _S_maskbit(size_t __pos )
107 { return (static_cast<_WordT>(1)) << _S_whichbit(__pos); }
109 _WordT&
110 _M_getword(size_t __pos)
111 { return _M_w[_S_whichword(__pos)]; }
113 _WordT
114 _M_getword(size_t __pos) const
115 { return _M_w[_S_whichword(__pos)]; }
117 _WordT&
118 _M_hiword()
119 { return _M_w[_Nw - 1]; }
121 _WordT
122 _M_hiword() const
123 { return _M_w[_Nw - 1]; }
125 void
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];
132 void
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];
139 void
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];
146 void
147 _M_do_left_shift(size_t __shift);
149 void
150 _M_do_right_shift(size_t __shift);
152 void
153 _M_do_flip()
155 for (size_t __i = 0; __i < _Nw; __i++)
156 _M_w[__i] = ~_M_w[__i];
159 void
160 _M_do_set()
162 for (size_t __i = 0; __i < _Nw; __i++)
163 _M_w[__i] = ~static_cast<_WordT>(0);
166 void
167 _M_do_reset()
168 { std::memset(_M_w, 0, _Nw * sizeof(_WordT)); }
170 bool
171 _M_is_equal(const _Base_bitset<_Nw>& __x) const
173 for (size_t __i = 0; __i < _Nw; ++__i)
175 if (_M_w[__i] != __x._M_w[__i])
176 return false;
178 return true;
181 bool
182 _M_is_any() const
184 for (size_t __i = 0; __i < _Nw; __i++)
186 if (_M_w[__i] != static_cast<_WordT>(0))
187 return true;
189 return false;
192 size_t
193 _M_do_count() const
195 size_t __result = 0;
196 for (size_t __i = 0; __i < _Nw; __i++)
197 __result += __builtin_popcountl(_M_w[__i]);
198 return __result;
201 unsigned long
202 _M_do_to_ulong() const;
204 // find first "on" bit
205 size_t
206 _M_do_find_first(size_t __not_found) const;
208 // find the next "on" bit that follows "prev"
209 size_t
210 _M_do_find_next(size_t __prev, size_t __not_found) const;
213 // Definitions of non-inline functions from _Base_bitset.
214 template<size_t _Nw>
215 void
216 _Base_bitset<_Nw>::_M_do_left_shift(size_t __shift)
218 if (__builtin_expect(__shift != 0, 1))
220 const size_t __wshift = __shift / _GLIBCXX_BITSET_BITS_PER_WORD;
221 const size_t __offset = __shift % _GLIBCXX_BITSET_BITS_PER_WORD;
223 if (__offset == 0)
224 for (size_t __n = _Nw - 1; __n >= __wshift; --__n)
225 _M_w[__n] = _M_w[__n - __wshift];
226 else
228 const size_t __sub_offset = (_GLIBCXX_BITSET_BITS_PER_WORD
229 - __offset);
230 for (size_t __n = _Nw - 1; __n > __wshift; --__n)
231 _M_w[__n] = ((_M_w[__n - __wshift] << __offset)
232 | (_M_w[__n - __wshift - 1] >> __sub_offset));
233 _M_w[__wshift] = _M_w[0] << __offset;
236 std::fill(_M_w + 0, _M_w + __wshift, static_cast<_WordT>(0));
240 template<size_t _Nw>
241 void
242 _Base_bitset<_Nw>::_M_do_right_shift(size_t __shift)
244 if (__builtin_expect(__shift != 0, 1))
246 const size_t __wshift = __shift / _GLIBCXX_BITSET_BITS_PER_WORD;
247 const size_t __offset = __shift % _GLIBCXX_BITSET_BITS_PER_WORD;
248 const size_t __limit = _Nw - __wshift - 1;
250 if (__offset == 0)
251 for (size_t __n = 0; __n <= __limit; ++__n)
252 _M_w[__n] = _M_w[__n + __wshift];
253 else
255 const size_t __sub_offset = (_GLIBCXX_BITSET_BITS_PER_WORD
256 - __offset);
257 for (size_t __n = 0; __n < __limit; ++__n)
258 _M_w[__n] = ((_M_w[__n + __wshift] >> __offset)
259 | (_M_w[__n + __wshift + 1] << __sub_offset));
260 _M_w[__limit] = _M_w[_Nw-1] >> __offset;
263 std::fill(_M_w + __limit + 1, _M_w + _Nw, static_cast<_WordT>(0));
267 template<size_t _Nw>
268 unsigned long
269 _Base_bitset<_Nw>::_M_do_to_ulong() const
271 for (size_t __i = 1; __i < _Nw; ++__i)
272 if (_M_w[__i])
273 __throw_overflow_error(__N("_Base_bitset::_M_do_to_ulong"));
274 return _M_w[0];
277 template<size_t _Nw>
278 size_t
279 _Base_bitset<_Nw>::_M_do_find_first(size_t __not_found) const
281 for (size_t __i = 0; __i < _Nw; __i++)
283 _WordT __thisword = _M_w[__i];
284 if (__thisword != static_cast<_WordT>(0))
285 return (__i * _GLIBCXX_BITSET_BITS_PER_WORD
286 + __builtin_ctzl(__thisword));
288 // not found, so return an indication of failure.
289 return __not_found;
292 template<size_t _Nw>
293 size_t
294 _Base_bitset<_Nw>::_M_do_find_next(size_t __prev, size_t __not_found) const
296 // make bound inclusive
297 ++__prev;
299 // check out of bounds
300 if (__prev >= _Nw * _GLIBCXX_BITSET_BITS_PER_WORD)
301 return __not_found;
303 // search first word
304 size_t __i = _S_whichword(__prev);
305 _WordT __thisword = _M_w[__i];
307 // mask off bits below bound
308 __thisword &= (~static_cast<_WordT>(0)) << _S_whichbit(__prev);
310 if (__thisword != static_cast<_WordT>(0))
311 return (__i * _GLIBCXX_BITSET_BITS_PER_WORD
312 + __builtin_ctzl(__thisword));
314 // check subsequent words
315 __i++;
316 for (; __i < _Nw; __i++)
318 __thisword = _M_w[__i];
319 if (__thisword != static_cast<_WordT>(0))
320 return (__i * _GLIBCXX_BITSET_BITS_PER_WORD
321 + __builtin_ctzl(__thisword));
323 // not found, so return an indication of failure.
324 return __not_found;
325 } // end _M_do_find_next
328 * @if maint
329 * Base class, specialization for a single word.
331 * See documentation for bitset.
332 * @endif
334 template<>
335 struct _Base_bitset<1>
337 typedef unsigned long _WordT;
338 _WordT _M_w;
340 _Base_bitset(void)
341 : _M_w(0)
344 _Base_bitset(unsigned long __val)
345 : _M_w(__val)
348 static size_t
349 _S_whichword(size_t __pos )
350 { return __pos / _GLIBCXX_BITSET_BITS_PER_WORD; }
352 static size_t
353 _S_whichbyte(size_t __pos )
354 { return (__pos % _GLIBCXX_BITSET_BITS_PER_WORD) / __CHAR_BIT__; }
356 static size_t
357 _S_whichbit(size_t __pos )
358 { return __pos % _GLIBCXX_BITSET_BITS_PER_WORD; }
360 static _WordT
361 _S_maskbit(size_t __pos )
362 { return (static_cast<_WordT>(1)) << _S_whichbit(__pos); }
364 _WordT&
365 _M_getword(size_t)
366 { return _M_w; }
368 _WordT
369 _M_getword(size_t) const
370 { return _M_w; }
372 _WordT&
373 _M_hiword()
374 { return _M_w; }
376 _WordT
377 _M_hiword() const
378 { return _M_w; }
380 void
381 _M_do_and(const _Base_bitset<1>& __x)
382 { _M_w &= __x._M_w; }
384 void
385 _M_do_or(const _Base_bitset<1>& __x)
386 { _M_w |= __x._M_w; }
388 void
389 _M_do_xor(const _Base_bitset<1>& __x)
390 { _M_w ^= __x._M_w; }
392 void
393 _M_do_left_shift(size_t __shift)
394 { _M_w <<= __shift; }
396 void
397 _M_do_right_shift(size_t __shift)
398 { _M_w >>= __shift; }
400 void
401 _M_do_flip()
402 { _M_w = ~_M_w; }
404 void
405 _M_do_set()
406 { _M_w = ~static_cast<_WordT>(0); }
408 void
409 _M_do_reset()
410 { _M_w = 0; }
412 bool
413 _M_is_equal(const _Base_bitset<1>& __x) const
414 { return _M_w == __x._M_w; }
416 bool
417 _M_is_any() const
418 { return _M_w != 0; }
420 size_t
421 _M_do_count() const
422 { return __builtin_popcountl(_M_w); }
424 unsigned long
425 _M_do_to_ulong() const
426 { return _M_w; }
428 size_t
429 _M_do_find_first(size_t __not_found) const
431 if (_M_w != 0)
432 return __builtin_ctzl(_M_w);
433 else
434 return __not_found;
437 // find the next "on" bit that follows "prev"
438 size_t
439 _M_do_find_next(size_t __prev, size_t __not_found) const
441 ++__prev;
442 if (__prev >= ((size_t) _GLIBCXX_BITSET_BITS_PER_WORD))
443 return __not_found;
445 _WordT __x = _M_w >> __prev;
446 if (__x != 0)
447 return __builtin_ctzl(__x) + __prev;
448 else
449 return __not_found;
454 * @if maint
455 * Base class, specialization for no storage (zero-length %bitset).
457 * See documentation for bitset.
458 * @endif
460 template<>
461 struct _Base_bitset<0>
463 typedef unsigned long _WordT;
465 _Base_bitset()
468 _Base_bitset(unsigned long)
471 static size_t
472 _S_whichword(size_t __pos )
473 { return __pos / _GLIBCXX_BITSET_BITS_PER_WORD; }
475 static size_t
476 _S_whichbyte(size_t __pos )
477 { return (__pos % _GLIBCXX_BITSET_BITS_PER_WORD) / __CHAR_BIT__; }
479 static size_t
480 _S_whichbit(size_t __pos )
481 { return __pos % _GLIBCXX_BITSET_BITS_PER_WORD; }
483 static _WordT
484 _S_maskbit(size_t __pos )
485 { return (static_cast<_WordT>(1)) << _S_whichbit(__pos); }
487 // This would normally give access to the data. The bounds-checking
488 // in the bitset class will prevent the user from getting this far,
489 // but (1) it must still return an lvalue to compile, and (2) the
490 // user might call _Unchecked_set directly, in which case this /needs/
491 // to fail. Let's not penalize zero-length users unless they actually
492 // make an unchecked call; all the memory ugliness is therefore
493 // localized to this single should-never-get-this-far function.
494 _WordT&
495 _M_getword(size_t) const
497 __throw_out_of_range(__N("_Base_bitset::_M_getword"));
498 return *new _WordT;
501 _WordT
502 _M_hiword() const
503 { return 0; }
505 void
506 _M_do_and(const _Base_bitset<0>&)
509 void
510 _M_do_or(const _Base_bitset<0>&)
513 void
514 _M_do_xor(const _Base_bitset<0>&)
517 void
518 _M_do_left_shift(size_t)
521 void
522 _M_do_right_shift(size_t)
525 void
526 _M_do_flip()
529 void
530 _M_do_set()
533 void
534 _M_do_reset()
537 // Are all empty bitsets equal to each other? Are they equal to
538 // themselves? How to compare a thing which has no state? What is
539 // the sound of one zero-length bitset clapping?
540 bool
541 _M_is_equal(const _Base_bitset<0>&) const
542 { return true; }
544 bool
545 _M_is_any() const
546 { return false; }
548 size_t
549 _M_do_count() const
550 { return 0; }
552 unsigned long
553 _M_do_to_ulong() const
554 { return 0; }
556 // Normally "not found" is the size, but that could also be
557 // misinterpreted as an index in this corner case. Oh well.
558 size_t
559 _M_do_find_first(size_t) const
560 { return 0; }
562 size_t
563 _M_do_find_next(size_t, size_t) const
564 { return 0; }
568 // Helper class to zero out the unused high-order bits in the highest word.
569 template<size_t _Extrabits>
570 struct _Sanitize
572 static void _S_do_sanitize(unsigned long& __val)
573 { __val &= ~((~static_cast<unsigned long>(0)) << _Extrabits); }
576 template<>
577 struct _Sanitize<0>
578 { static void _S_do_sanitize(unsigned long) {} };
581 * @brief The %bitset class represents a @e fixed-size sequence of bits.
583 * @ingroup Containers
585 * (Note that %bitset does @e not meet the formal requirements of a
586 * <a href="tables.html#65">container</a>. Mainly, it lacks iterators.)
588 * The template argument, @a Nb, may be any non-negative number,
589 * specifying the number of bits (e.g., "0", "12", "1024*1024").
591 * In the general unoptimized case, storage is allocated in word-sized
592 * blocks. Let B be the number of bits in a word, then (Nb+(B-1))/B
593 * words will be used for storage. B - Nb%B bits are unused. (They are
594 * the high-order bits in the highest word.) It is a class invariant
595 * that those unused bits are always zero.
597 * If you think of %bitset as "a simple array of bits," be aware that
598 * your mental picture is reversed: a %bitset behaves the same way as
599 * bits in integers do, with the bit at index 0 in the "least significant
600 * / right-hand" position, and the bit at index Nb-1 in the "most
601 * significant / left-hand" position. Thus, unlike other containers, a
602 * %bitset's index "counts from right to left," to put it very loosely.
604 * This behavior is preserved when translating to and from strings. For
605 * example, the first line of the following program probably prints
606 * "b('a') is 0001100001" on a modern ASCII system.
608 * @code
609 * #include <bitset>
610 * #include <iostream>
611 * #include <sstream>
613 * using namespace std;
615 * int main()
617 * long a = 'a';
618 * bitset<10> b(a);
620 * cout << "b('a') is " << b << endl;
622 * ostringstream s;
623 * s << b;
624 * string str = s.str();
625 * cout << "index 3 in the string is " << str[3] << " but\n"
626 * << "index 3 in the bitset is " << b[3] << endl;
628 * @endcode
630 * Also see http://gcc.gnu.org/onlinedocs/libstdc++/ext/sgiexts.html#ch23
631 * for a description of extensions.
633 * @if maint
634 * Most of the actual code isn't contained in %bitset<> itself, but in the
635 * base class _Base_bitset. The base class works with whole words, not with
636 * individual bits. This allows us to specialize _Base_bitset for the
637 * important special case where the %bitset is only a single word.
639 * Extra confusion can result due to the fact that the storage for
640 * _Base_bitset @e is a regular array, and is indexed as such. This is
641 * carefully encapsulated.
642 * @endif
644 template<size_t _Nb>
645 class bitset
646 : private _Base_bitset<_GLIBCXX_BITSET_WORDS(_Nb)>
648 private:
649 typedef _Base_bitset<_GLIBCXX_BITSET_WORDS(_Nb)> _Base;
650 typedef unsigned long _WordT;
652 void
653 _M_do_sanitize()
655 _Sanitize<_Nb % _GLIBCXX_BITSET_BITS_PER_WORD>::
656 _S_do_sanitize(this->_M_hiword());
659 public:
661 * This encapsulates the concept of a single bit. An instance of this
662 * class is a proxy for an actual bit; this way the individual bit
663 * operations are done as faster word-size bitwise instructions.
665 * Most users will never need to use this class directly; conversions
666 * to and from bool are automatic and should be transparent. Overloaded
667 * operators help to preserve the illusion.
669 * (On a typical system, this "bit %reference" is 64 times the size of
670 * an actual bit. Ha.)
672 class reference
674 friend class bitset;
676 _WordT *_M_wp;
677 size_t _M_bpos;
679 // left undefined
680 reference();
682 public:
683 reference(bitset& __b, size_t __pos)
685 _M_wp = &__b._M_getword(__pos);
686 _M_bpos = _Base::_S_whichbit(__pos);
689 ~reference()
692 // For b[i] = __x;
693 reference&
694 operator=(bool __x)
696 if (__x)
697 *_M_wp |= _Base::_S_maskbit(_M_bpos);
698 else
699 *_M_wp &= ~_Base::_S_maskbit(_M_bpos);
700 return *this;
703 // For b[i] = b[__j];
704 reference&
705 operator=(const reference& __j)
707 if ((*(__j._M_wp) & _Base::_S_maskbit(__j._M_bpos)))
708 *_M_wp |= _Base::_S_maskbit(_M_bpos);
709 else
710 *_M_wp &= ~_Base::_S_maskbit(_M_bpos);
711 return *this;
714 // Flips the bit
715 bool
716 operator~() const
717 { return (*(_M_wp) & _Base::_S_maskbit(_M_bpos)) == 0; }
719 // For __x = b[i];
720 operator bool() const
721 { return (*(_M_wp) & _Base::_S_maskbit(_M_bpos)) != 0; }
723 // For b[i].flip();
724 reference&
725 flip()
727 *_M_wp ^= _Base::_S_maskbit(_M_bpos);
728 return *this;
731 friend class reference;
733 // 23.3.5.1 constructors:
734 /// All bits set to zero.
735 bitset()
738 /// Initial bits bitwise-copied from a single word (others set to zero).
739 bitset(unsigned long __val)
740 : _Base(__val)
741 { _M_do_sanitize(); }
744 * @brief Use a subset of a string.
745 * @param s A string of '0' and '1' characters.
746 * @param position Index of the first character in @a s to use;
747 * defaults to zero.
748 * @throw std::out_of_range If @a pos is bigger the size of @a s.
749 * @throw std::invalid_argument If a character appears in the string
750 * which is neither '0' nor '1'.
752 template<class _CharT, class _Traits, class _Alloc>
753 explicit
754 bitset(const basic_string<_CharT, _Traits, _Alloc>& __s,
755 size_t __position = 0)
756 : _Base()
758 if (__position > __s.size())
759 __throw_out_of_range(__N("bitset::bitset initial position "
760 "not valid"));
761 _M_copy_from_string(__s, __position,
762 basic_string<_CharT, _Traits, _Alloc>::npos);
766 * @brief Use a subset of a string.
767 * @param s A string of '0' and '1' characters.
768 * @param position Index of the first character in @a s to use.
769 * @param n The number of characters to copy.
770 * @throw std::out_of_range If @a pos is bigger the size of @a s.
771 * @throw std::invalid_argument If a character appears in the string
772 * which is neither '0' nor '1'.
774 template<class _CharT, class _Traits, class _Alloc>
775 bitset(const basic_string<_CharT, _Traits, _Alloc>& __s,
776 size_t __position, size_t __n)
777 : _Base()
779 if (__position > __s.size())
780 __throw_out_of_range(__N("bitset::bitset initial position "
781 "not valid"));
782 _M_copy_from_string(__s, __position, __n);
785 // 23.3.5.2 bitset operations:
786 //@{
788 * @brief Operations on bitsets.
789 * @param rhs A same-sized bitset.
791 * These should be self-explanatory.
793 bitset<_Nb>&
794 operator&=(const bitset<_Nb>& __rhs)
796 this->_M_do_and(__rhs);
797 return *this;
800 bitset<_Nb>&
801 operator|=(const bitset<_Nb>& __rhs)
803 this->_M_do_or(__rhs);
804 return *this;
807 bitset<_Nb>&
808 operator^=(const bitset<_Nb>& __rhs)
810 this->_M_do_xor(__rhs);
811 return *this;
813 //@}
815 //@{
817 * @brief Operations on bitsets.
818 * @param position The number of places to shift.
820 * These should be self-explanatory.
822 bitset<_Nb>&
823 operator<<=(size_t __position)
825 if (__builtin_expect(__position < _Nb, 1))
827 this->_M_do_left_shift(__position);
828 this->_M_do_sanitize();
830 else
831 this->_M_do_reset();
832 return *this;
835 bitset<_Nb>&
836 operator>>=(size_t __position)
838 if (__builtin_expect(__position < _Nb, 1))
840 this->_M_do_right_shift(__position);
841 this->_M_do_sanitize();
843 else
844 this->_M_do_reset();
845 return *this;
847 //@}
849 //@{
851 * These versions of single-bit set, reset, flip, and test are
852 * extensions from the SGI version. They do no range checking.
853 * @ingroup SGIextensions
855 bitset<_Nb>&
856 _Unchecked_set(size_t __pos)
858 this->_M_getword(__pos) |= _Base::_S_maskbit(__pos);
859 return *this;
862 bitset<_Nb>&
863 _Unchecked_set(size_t __pos, int __val)
865 if (__val)
866 this->_M_getword(__pos) |= _Base::_S_maskbit(__pos);
867 else
868 this->_M_getword(__pos) &= ~_Base::_S_maskbit(__pos);
869 return *this;
872 bitset<_Nb>&
873 _Unchecked_reset(size_t __pos)
875 this->_M_getword(__pos) &= ~_Base::_S_maskbit(__pos);
876 return *this;
879 bitset<_Nb>&
880 _Unchecked_flip(size_t __pos)
882 this->_M_getword(__pos) ^= _Base::_S_maskbit(__pos);
883 return *this;
886 bool
887 _Unchecked_test(size_t __pos) const
888 { return ((this->_M_getword(__pos) & _Base::_S_maskbit(__pos))
889 != static_cast<_WordT>(0)); }
890 //@}
892 // Set, reset, and flip.
894 * @brief Sets every bit to true.
896 bitset<_Nb>&
897 set()
899 this->_M_do_set();
900 this->_M_do_sanitize();
901 return *this;
905 * @brief Sets a given bit to a particular value.
906 * @param position The index of the bit.
907 * @param val Either true or false, defaults to true.
908 * @throw std::out_of_range If @a pos is bigger the size of the %set.
910 bitset<_Nb>&
911 set(size_t __position, bool __val = true)
913 if (__position >= _Nb)
914 __throw_out_of_range(__N("bitset::set"));
915 return _Unchecked_set(__position, __val);
919 * @brief Sets every bit to false.
921 bitset<_Nb>&
922 reset()
924 this->_M_do_reset();
925 return *this;
929 * @brief Sets a given bit to false.
930 * @param position The index of the bit.
931 * @throw std::out_of_range If @a pos is bigger the size of the %set.
933 * Same as writing @c set(pos,false).
935 bitset<_Nb>&
936 reset(size_t __position)
938 if (__position >= _Nb)
939 __throw_out_of_range(__N("bitset::reset"));
940 return _Unchecked_reset(__position);
944 * @brief Toggles every bit to its opposite value.
946 bitset<_Nb>&
947 flip()
949 this->_M_do_flip();
950 this->_M_do_sanitize();
951 return *this;
955 * @brief Toggles a given bit to its opposite value.
956 * @param position The index of the bit.
957 * @throw std::out_of_range If @a pos is bigger the size of the %set.
959 bitset<_Nb>&
960 flip(size_t __position)
962 if (__position >= _Nb)
963 __throw_out_of_range(__N("bitset::flip"));
964 return _Unchecked_flip(__position);
967 /// See the no-argument flip().
968 bitset<_Nb>
969 operator~() const
970 { return bitset<_Nb>(*this).flip(); }
972 //@{
974 * @brief Array-indexing support.
975 * @param position Index into the %bitset.
976 * @return A bool for a 'const %bitset'. For non-const bitsets, an
977 * instance of the reference proxy class.
978 * @note These operators do no range checking and throw no exceptions,
979 * as required by DR 11 to the standard.
981 * @if maint
982 * _GLIBCXX_RESOLVE_LIB_DEFECTS Note that this implementation already
983 * resolves DR 11 (items 1 and 2), but does not do the range-checking
984 * required by that DR's resolution. -pme
985 * The DR has since been changed: range-checking is a precondition
986 * (users' responsibility), and these functions must not throw. -pme
987 * @endif
989 reference
990 operator[](size_t __position)
991 { return reference(*this,__position); }
993 bool
994 operator[](size_t __position) const
995 { return _Unchecked_test(__position); }
996 //@}
999 * @brief Retuns a numerical interpretation of the %bitset.
1000 * @return The integral equivalent of the bits.
1001 * @throw std::overflow_error If there are too many bits to be
1002 * represented in an @c unsigned @c long.
1004 unsigned long
1005 to_ulong() const
1006 { return this->_M_do_to_ulong(); }
1009 * @brief Retuns a character interpretation of the %bitset.
1010 * @return The string equivalent of the bits.
1012 * Note the ordering of the bits: decreasing character positions
1013 * correspond to increasing bit positions (see the main class notes for
1014 * an example).
1016 template<class _CharT, class _Traits, class _Alloc>
1017 basic_string<_CharT, _Traits, _Alloc>
1018 to_string() const
1020 basic_string<_CharT, _Traits, _Alloc> __result;
1021 _M_copy_to_string(__result);
1022 return __result;
1025 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1026 // 434. bitset::to_string() hard to use.
1027 template<class _CharT, class _Traits>
1028 basic_string<_CharT, _Traits, allocator<_CharT> >
1029 to_string() const
1030 { return to_string<_CharT, _Traits, allocator<_CharT> >(); }
1032 template<class _CharT>
1033 basic_string<_CharT, char_traits<_CharT>, allocator<_CharT> >
1034 to_string() const
1035 { return to_string<_CharT, char_traits<_CharT>, allocator<_CharT> >(); }
1037 basic_string<char, char_traits<char>, allocator<char> >
1038 to_string() const
1039 { return to_string<char, char_traits<char>, allocator<char> >(); }
1041 // Helper functions for string operations.
1042 template<class _CharT, class _Traits, class _Alloc>
1043 void
1044 _M_copy_from_string(const basic_string<_CharT, _Traits, _Alloc>& __s,
1045 size_t, size_t);
1047 template<class _CharT, class _Traits, class _Alloc>
1048 void
1049 _M_copy_to_string(basic_string<_CharT, _Traits, _Alloc>&) const;
1051 /// Returns the number of bits which are set.
1052 size_t
1053 count() const
1054 { return this->_M_do_count(); }
1056 /// Returns the total number of bits.
1057 size_t
1058 size() const
1059 { return _Nb; }
1061 //@{
1062 /// These comparisons for equality/inequality are, well, @e bitwise.
1063 bool
1064 operator==(const bitset<_Nb>& __rhs) const
1065 { return this->_M_is_equal(__rhs); }
1067 bool
1068 operator!=(const bitset<_Nb>& __rhs) const
1069 { return !this->_M_is_equal(__rhs); }
1070 //@}
1073 * @brief Tests the value of a bit.
1074 * @param position The index of a bit.
1075 * @return The value at @a pos.
1076 * @throw std::out_of_range If @a pos is bigger the size of the %set.
1078 bool
1079 test(size_t __position) const
1081 if (__position >= _Nb)
1082 __throw_out_of_range(__N("bitset::test"));
1083 return _Unchecked_test(__position);
1087 * @brief Tests whether any of the bits are on.
1088 * @return True if at least one bit is set.
1090 bool
1091 any() const
1092 { return this->_M_is_any(); }
1095 * @brief Tests whether any of the bits are on.
1096 * @return True if none of the bits are set.
1098 bool
1099 none() const
1100 { return !this->_M_is_any(); }
1102 //@{
1103 /// Self-explanatory.
1104 bitset<_Nb>
1105 operator<<(size_t __position) const
1106 { return bitset<_Nb>(*this) <<= __position; }
1108 bitset<_Nb>
1109 operator>>(size_t __position) const
1110 { return bitset<_Nb>(*this) >>= __position; }
1111 //@}
1114 * @brief Finds the index of the first "on" bit.
1115 * @return The index of the first bit set, or size() if not found.
1116 * @ingroup SGIextensions
1117 * @sa _Find_next
1119 size_t
1120 _Find_first() const
1121 { return this->_M_do_find_first(_Nb); }
1124 * @brief Finds the index of the next "on" bit after prev.
1125 * @return The index of the next bit set, or size() if not found.
1126 * @param prev Where to start searching.
1127 * @ingroup SGIextensions
1128 * @sa _Find_first
1130 size_t
1131 _Find_next(size_t __prev ) const
1132 { return this->_M_do_find_next(__prev, _Nb); }
1135 // Definitions of non-inline member functions.
1136 template<size_t _Nb>
1137 template<class _CharT, class _Traits, class _Alloc>
1138 void
1139 bitset<_Nb>::_M_copy_from_string(const basic_string<_CharT, _Traits,
1140 _Alloc>& __s, size_t __pos, size_t __n)
1142 reset();
1143 const size_t __nbits = std::min(_Nb, std::min(__n, __s.size() - __pos));
1144 for (size_t __i = 0; __i < __nbits; ++__i)
1146 switch(__s[__pos + __nbits - __i - 1])
1148 case '0':
1149 break;
1150 case '1':
1151 set(__i);
1152 break;
1153 default:
1154 __throw_invalid_argument(__N("bitset::_M_copy_from_string"));
1159 template<size_t _Nb>
1160 template<class _CharT, class _Traits, class _Alloc>
1161 void
1162 bitset<_Nb>::_M_copy_to_string(basic_string<_CharT, _Traits,
1163 _Alloc>& __s) const
1165 __s.assign(_Nb, '0');
1166 for (size_t __i = 0; __i < _Nb; ++__i)
1167 if (_Unchecked_test(__i))
1168 __s[_Nb - 1 - __i] = '1';
1171 // 23.3.5.3 bitset operations:
1172 //@{
1174 * @brief Global bitwise operations on bitsets.
1175 * @param x A bitset.
1176 * @param y A bitset of the same size as @a x.
1177 * @return A new bitset.
1179 * These should be self-explanatory.
1181 template<size_t _Nb>
1182 inline bitset<_Nb>
1183 operator&(const bitset<_Nb>& __x, const bitset<_Nb>& __y)
1185 bitset<_Nb> __result(__x);
1186 __result &= __y;
1187 return __result;
1190 template<size_t _Nb>
1191 inline bitset<_Nb>
1192 operator|(const bitset<_Nb>& __x, const bitset<_Nb>& __y)
1194 bitset<_Nb> __result(__x);
1195 __result |= __y;
1196 return __result;
1199 template <size_t _Nb>
1200 inline bitset<_Nb>
1201 operator^(const bitset<_Nb>& __x, const bitset<_Nb>& __y)
1203 bitset<_Nb> __result(__x);
1204 __result ^= __y;
1205 return __result;
1207 //@}
1209 //@{
1211 * @brief Global I/O operators for bitsets.
1213 * Direct I/O between streams and bitsets is supported. Output is
1214 * straightforward. Input will skip whitespace, only accept '0' and '1'
1215 * characters, and will only extract as many digits as the %bitset will
1216 * hold.
1218 template<class _CharT, class _Traits, size_t _Nb>
1219 basic_istream<_CharT, _Traits>&
1220 operator>>(basic_istream<_CharT, _Traits>& __is, bitset<_Nb>& __x)
1222 typedef typename _Traits::char_type char_type;
1223 basic_string<_CharT, _Traits> __tmp;
1224 __tmp.reserve(_Nb);
1226 ios_base::iostate __state = ios_base::goodbit;
1227 typename basic_istream<_CharT, _Traits>::sentry __sentry(__is);
1228 if (__sentry)
1232 basic_streambuf<_CharT, _Traits>* __buf = __is.rdbuf();
1233 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1234 // 303. Bitset input operator underspecified
1235 const char_type __zero = __is.widen('0');
1236 const char_type __one = __is.widen('1');
1237 for (size_t __i = 0; __i < _Nb; ++__i)
1239 static typename _Traits::int_type __eof = _Traits::eof();
1241 typename _Traits::int_type __c1 = __buf->sbumpc();
1242 if (_Traits::eq_int_type(__c1, __eof))
1244 __state |= ios_base::eofbit;
1245 break;
1247 else
1249 const char_type __c2 = _Traits::to_char_type(__c1);
1250 if (__c2 == __zero)
1251 __tmp.push_back('0');
1252 else if (__c2 == __one)
1253 __tmp.push_back('1');
1254 else if (_Traits::eq_int_type(__buf->sputbackc(__c2),
1255 __eof))
1257 __state |= ios_base::failbit;
1258 break;
1263 catch(...)
1264 { __is._M_setstate(ios_base::badbit); }
1267 if (__tmp.empty() && _Nb)
1268 __state |= ios_base::failbit;
1269 else
1270 __x._M_copy_from_string(__tmp, static_cast<size_t>(0), _Nb);
1271 if (__state)
1272 __is.setstate(__state);
1273 return __is;
1276 template <class _CharT, class _Traits, size_t _Nb>
1277 basic_ostream<_CharT, _Traits>&
1278 operator<<(basic_ostream<_CharT, _Traits>& __os, const bitset<_Nb>& __x)
1280 basic_string<_CharT, _Traits> __tmp;
1281 __x._M_copy_to_string(__tmp);
1282 return __os << __tmp;
1284 //@}
1285 } // namespace std
1287 #undef _GLIBCXX_BITSET_WORDS
1288 #undef _GLIBCXX_BITSET_BITS_PER_WORD
1290 #ifdef _GLIBCXX_DEBUG
1291 # include <debug/bitset>
1292 #endif
1294 #endif /* _GLIBCXX_BITSET */