Expect error messages from cross targets as well as native targets.
[official-gcc.git] / libstdc++ / stl / bitset
blobe26845ed0455a455f6e9bcfc41b30fcf8c4dcc78
1 /*
2  * Copyright (c) 1998
3  * Silicon Graphics Computer Systems, Inc.
4  *
5  * Permission to use, copy, modify, distribute and sell this software
6  * and its documentation for any purpose is hereby granted without fee,
7  * provided that the above copyright notice appear in all copies and
8  * that both that copyright notice and this permission notice appear
9  * in supporting documentation.  Silicon Graphics makes no
10  * representations about the suitability of this software for any
11  * purpose.  It is provided "as is" without express or implied warranty.
12  */
14 #ifndef __SGI_STL_BITSET
15 #define __SGI_STL_BITSET
17 // This implementation of bitset<> has a second template parameter,
18 // _WordT, which defaults to unsigned long.  *YOU SHOULD NOT USE
19 // THIS FEATURE*.  It is experimental, and it may be removed in
20 // future releases.
22 // A bitset of size N, using words of type _WordT, will have
23 // N % (sizeof(_WordT) * CHAR_BIT) unused bits.  (They are the high-
24 // order bits in the highest word.)  It is a class invariant
25 // of class bitset<> that those unused bits are always zero.
27 // Most of the actual code isn't contained in bitset<> itself, but in the
28 // base class _Base_bitset.  The base class works with whole words, not with
29 // individual bits.  This allows us to specialize _Base_bitset for the
30 // important special case where the bitset is only a single word.
32 // The C++ standard does not define the precise semantics of operator[].
33 // In this implementation the const version of operator[] is equivalent
34 // to test(), except that it does no range checking.  The non-const version
35 // returns a reference to a bit, again without doing any range checking.
38 #include <stddef.h>     // for size_t
39 #include <string>
40 #include <stdexcept>    // for invalid_argument, out_of_range, overflow_error
41 #include <iostream.h>   // for istream, ostream
43 #define __BITS_PER_WORDT(__wt) (CHAR_BIT*sizeof(__wt))
44 #define __BITSET_WORDS(__n,__wt) \
45  ((__n) < 1 ? 1 : ((__n) + __BITS_PER_WORDT(__wt) - 1)/__BITS_PER_WORDT(__wt))
47 __STL_BEGIN_NAMESPACE
49 #if defined(__sgi) && !defined(__GNUC__) && (_MIPS_SIM != _MIPS_SIM_ABI32)
50 #pragma set woff 1209
51 #endif
53 // structure to aid in counting bits
54 template<bool __dummy>
55 struct _Bit_count {
56   static unsigned char _S_bit_count[256];
59 // Mapping from 8 bit unsigned integers to the index of the first one
60 // bit:
61 template<bool __dummy>
62 struct _First_one {
63   static unsigned char _S_first_one[256];
67 // Base class: general case.
70 template<size_t _Nw, class _WordT>
71 struct _Base_bitset {
72   _WordT _M_w[_Nw];                // 0 is the least significant word.
74   _Base_bitset( void ) { _M_do_reset(); }
76   _Base_bitset(unsigned long __val);
78   static size_t _S_whichword( size_t __pos ) {
79     return __pos / __BITS_PER_WORDT(_WordT);
80   }
81   static size_t _S_whichbyte( size_t __pos ) {
82     return (__pos % __BITS_PER_WORDT(_WordT)) / CHAR_BIT;
83   }
84   static size_t _S_whichbit( size_t __pos ) {
85     return __pos % __BITS_PER_WORDT(_WordT);
86   }
87   static _WordT _S_maskbit( size_t __pos ) {
88     return (static_cast<_WordT>(1)) << _S_whichbit(__pos);
89   }
91   _WordT& _M_getword(size_t __pos)       { return _M_w[_S_whichword(__pos)]; }
92   _WordT  _M_getword(size_t __pos) const { return _M_w[_S_whichword(__pos)]; }
94   _WordT& _M_hiword()       { return _M_w[_Nw - 1]; }
95   _WordT  _M_hiword() const { return _M_w[_Nw - 1]; }
97   void _M_do_and(const _Base_bitset<_Nw,_WordT>& __x) {
98     for ( size_t __i = 0; __i < _Nw; __i++ ) {
99       _M_w[__i] &= __x._M_w[__i];
100     }
101   }
103   void _M_do_or(const _Base_bitset<_Nw,_WordT>& __x) {
104     for ( size_t __i = 0; __i < _Nw; __i++ ) {
105       _M_w[__i] |= __x._M_w[__i];
106     }
107   }
109   void _M_do_xor(const _Base_bitset<_Nw,_WordT>& __x) {
110     for ( size_t __i = 0; __i < _Nw; __i++ ) {
111       _M_w[__i] ^= __x._M_w[__i];
112     }
113   }
115   void _M_do_left_shift(size_t __shift);
117   void _M_do_right_shift(size_t __shift);
119   void _M_do_flip() {
120     for ( size_t __i = 0; __i < _Nw; __i++ ) {
121       _M_w[__i] = ~_M_w[__i];
122     }
123   }
125   void _M_do_set() {
126     for ( size_t __i = 0; __i < _Nw; __i++ ) {
127       _M_w[__i] = ~static_cast<_WordT>(0);
128     }
129   }
131   void _M_do_reset() {
132     for ( size_t __i = 0; __i < _Nw; __i++ ) {
133       _M_w[__i] = 0;
134     }
135   }
137   bool _M_is_equal(const _Base_bitset<_Nw,_WordT>& __x) const {
138     for (size_t __i = 0; __i < _Nw; ++__i) {
139       if (_M_w[__i] != __x._M_w[__i])
140         return false;
141     }
142     return true;
143   }
145   bool _M_is_any() const {
146     for ( size_t __i = 0; __i < __BITSET_WORDS(_Nw,_WordT); __i++ ) {
147       if ( _M_w[__i] != static_cast<_WordT>(0) )
148         return true;
149     }
150     return false;
151   }
153   size_t _M_do_count() const {
154     size_t __result = 0;
155     const unsigned char* __byte_ptr = (const unsigned char*)_M_w;
156     const unsigned char* __end_ptr = (const unsigned char*)(_M_w+_Nw);
158     while ( __byte_ptr < __end_ptr ) {
159       __result += _Bit_count<true>::_S_bit_count[*__byte_ptr];
160       __byte_ptr++;
161     }
162     return __result;
163   }
165   unsigned long _M_do_to_ulong() const;
167   // find first "on" bit
168   size_t _M_do_find_first(size_t __not_found) const;
170   // find the next "on" bit that follows "prev"
171   size_t _M_do_find_next(size_t __prev, size_t __not_found) const;
175 // Definitions of non-inline functions from _Base_bitset.
178 template<size_t _Nw, class _WordT>
179 _Base_bitset<_Nw, _WordT>::_Base_bitset(unsigned long __val)
181   _M_do_reset();
182   const size_t __n = min(sizeof(unsigned long)*CHAR_BIT,
183                          __BITS_PER_WORDT(_WordT)*_Nw);
184   for(size_t __i = 0; __i < __n; ++__i, __val >>= 1)
185     if ( __val & 0x1 )
186       _M_getword(__i) |= _S_maskbit(__i);
189 template<size_t _Nw, class _WordT>
190 void _Base_bitset<_Nw, _WordT>::_M_do_left_shift(size_t __shift)
192   if (__shift != 0) {
193     const size_t __wshift = __shift / __BITS_PER_WORDT(_WordT);
194     const size_t __offset = __shift % __BITS_PER_WORDT(_WordT);
195     const size_t __sub_offset = __BITS_PER_WORDT(_WordT) - __offset;
196     size_t __n = _Nw - 1;
197     for ( ; __n > __wshift; --__n)
198       _M_w[__n] = (_M_w[__n - __wshift] << __offset) |
199                 (_M_w[__n - __wshift - 1] >> __sub_offset);
200     if (__n == __wshift)
201       _M_w[__n] = _M_w[0] << __offset;
202     for (size_t __n1 = 0; __n1 < __n; ++__n1)
203       _M_w[__n1] = static_cast<_WordT>(0);
204   }
207 template<size_t _Nw, class _WordT>
208 void _Base_bitset<_Nw, _WordT>::_M_do_right_shift(size_t __shift)
210   if (__shift != 0) {
211     const size_t __wshift = __shift / __BITS_PER_WORDT(_WordT);
212     const size_t __offset = __shift % __BITS_PER_WORDT(_WordT);
213     const size_t __sub_offset = __BITS_PER_WORDT(_WordT) - __offset;
214     const size_t __limit = _Nw - __wshift - 1;
215     size_t __n = 0;
216     for ( ; __n < __limit; ++__n)
217       _M_w[__n] = (_M_w[__n + __wshift] >> __offset) |
218                   (_M_w[__n + __wshift + 1] << __sub_offset);
219     _M_w[__limit] = _M_w[_Nw-1] >> __offset;
220     for (size_t __n1 = __limit + 1; __n1 < _Nw; ++__n1)
221       _M_w[__n1] = static_cast<_WordT>(0);
222   }
225 template<size_t _Nw, class _WordT>
226 unsigned long _Base_bitset<_Nw, _WordT>::_M_do_to_ulong() const
228   const overflow_error __overflow("bitset");
230   if (sizeof(_WordT) >= sizeof(unsigned long)) {
231     for (size_t __i = 1; __i < _Nw; ++__i)
232       if (_M_w[__i])
233         __STL_THROW(__overflow);
235     const _WordT __mask = static_cast<_WordT>(static_cast<unsigned long>(-1));
236     if (_M_w[0] & ~__mask)
237       __STL_THROW(__overflow);
239     return static_cast<unsigned long>(_M_w[0] & __mask);
240   }
241   else {                      // sizeof(_WordT) < sizeof(unsigned long).
242     const size_t __nwords =
243       (sizeof(unsigned long) + sizeof(_WordT) - 1) / sizeof(_WordT);
245     size_t __min_nwords = __nwords;
246     if (_Nw > __nwords) {
247       for (size_t __i = __nwords; __i < _Nw; ++__i)
248         if (_M_w[__i])
249           __STL_THROW(__overflow);
250     }
251     else
252       __min_nwords = _Nw;
254     // If unsigned long is 8 bytes and _WordT is 6 bytes, then an unsigned
255     // long consists of all of one word plus 2 bytes from another word.
256     const size_t __part = sizeof(unsigned long) % sizeof(_WordT);
258     if (__part != 0 && __nwords <= _Nw &&
259         (_M_w[__min_nwords - 1] >> ((sizeof(_WordT) - __part) * CHAR_BIT)) != 0)
260       __STL_THROW(__overflow);
262     unsigned long __result = 0;
263     for (size_t __i = 0; __i < __min_nwords; ++__i) {
264       __result |= static_cast<unsigned long>(
265          _M_w[__i]) << (__i * sizeof(_WordT) * CHAR_BIT);
266     }
267     return __result;
268   }
269 } // End _M_do_to_ulong
271 template<size_t _Nw, class _WordT>
272 size_t _Base_bitset<_Nw, _WordT>::_M_do_find_first(size_t __not_found) const
274   for ( size_t __i = 0; __i < _Nw; __i++ ) {
275     _WordT __thisword = _M_w[__i];
276     if ( __thisword != static_cast<_WordT>(0) ) {
277       // find byte within word
278       for ( size_t __j = 0; __j < sizeof(_WordT); __j++ ) {
279         unsigned char __this_byte
280           = static_cast<unsigned char>(__thisword & (~(unsigned char)0));
281         if ( __this_byte )
282           return __i*__BITS_PER_WORDT(_WordT) + __j*CHAR_BIT +
283             _First_one<true>::_S_first_one[__this_byte];
285         __thisword >>= CHAR_BIT;
286       }
287     }
288   }
289   // not found, so return an indication of failure.
290   return __not_found;
293 template<size_t _Nw, class _WordT>
294 size_t
295 _Base_bitset<_Nw, _WordT>::_M_do_find_next(size_t __prev,
296                                            size_t __not_found) const
298   // make bound inclusive
299   ++__prev;
301   // check out of bounds
302   if ( __prev >= _Nw * __BITS_PER_WORDT(_WordT) )
303     return __not_found;
305     // search first word
306   size_t __i = _S_whichword(__prev);
307   _WordT __thisword = _M_w[__i];
309     // mask off bits below bound
310   __thisword &= (~static_cast<_WordT>(0)) << _S_whichbit(__prev);
312   if ( __thisword != static_cast<_WordT>(0) ) {
313     // find byte within word
314     // get first byte into place
315     __thisword >>= _S_whichbyte(__prev) * CHAR_BIT;
316     for ( size_t __j = _S_whichbyte(__prev); __j < sizeof(_WordT); __j++ ) {
317       unsigned char __this_byte
318         = static_cast<unsigned char>(__thisword & (~(unsigned char)0));
319       if ( __this_byte )
320         return __i*__BITS_PER_WORDT(_WordT) + __j*CHAR_BIT +
321           _First_one<true>::_S_first_one[__this_byte];
323       __thisword >>= CHAR_BIT;
324     }
325   }
327   // check subsequent words
328   __i++;
329   for ( ; __i < _Nw; __i++ ) {
330     _WordT __thisword = _M_w[__i];
331     if ( __thisword != static_cast<_WordT>(0) ) {
332       // find byte within word
333       for ( size_t __j = 0; __j < sizeof(_WordT); __j++ ) {
334         unsigned char __this_byte
335           = static_cast<unsigned char>(__thisword & (~(unsigned char)0));
336         if ( __this_byte )
337           return __i*__BITS_PER_WORDT(_WordT) + __j*CHAR_BIT +
338             _First_one<true>::_S_first_one[__this_byte];
340         __thisword >>= CHAR_BIT;
341       }
342     }
343   }
345   // not found, so return an indication of failure.
346   return __not_found;
347 } // end _M_do_find_next
350 // ------------------------------------------------------------
353 // Base class: specialization for a single word.
356 template<class _WordT>
357 struct _Base_bitset<1, _WordT> {
358   _WordT _M_w;
360   _Base_bitset( void ) { _M_do_reset(); }
362   _Base_bitset(unsigned long __val);
364   static size_t _S_whichword( size_t __pos ) {
365     return __pos / __BITS_PER_WORDT(_WordT);
366   }
367   static size_t _S_whichbyte( size_t __pos ) {
368     return (__pos % __BITS_PER_WORDT(_WordT)) / CHAR_BIT;
369   }
370   static size_t _S_whichbit( size_t __pos ) {
371     return __pos % __BITS_PER_WORDT(_WordT);
372   }
373   static _WordT _S_maskbit( size_t __pos ) {
374     return (static_cast<_WordT>(1)) << _S_whichbit(__pos);
375   }
377   _WordT& _M_getword(size_t)       { return _M_w; }
378   _WordT  _M_getword(size_t) const { return _M_w; }
380   _WordT& _M_hiword()       { return _M_w; }
381   _WordT  _M_hiword() const { return _M_w; }
383   void _M_do_and(const _Base_bitset<1,_WordT>& __x) { _M_w &= __x._M_w; }
384   void _M_do_or(const _Base_bitset<1,_WordT>& __x)  { _M_w |= __x._M_w; }
385   void _M_do_xor(const _Base_bitset<1,_WordT>& __x) { _M_w ^= __x._M_w; }
386   void _M_do_left_shift(size_t __shift)     { _M_w <<= __shift; }
387   void _M_do_right_shift(size_t __shift)    { _M_w >>= __shift; }
388   void _M_do_flip()                       { _M_w = ~_M_w; }
389   void _M_do_set()                        { _M_w = ~static_cast<_WordT>(0); }
390   void _M_do_reset()                      { _M_w = 0; }
392   bool _M_is_equal(const _Base_bitset<1,_WordT>& __x) const {
393     return _M_w == __x._M_w;
394   }
395   bool _M_is_any() const {
396     return _M_w != 0;
397   }
399   size_t _M_do_count() const {
400     size_t __result = 0;
401     const unsigned char* __byte_ptr = (const unsigned char*)&_M_w;
402     const unsigned char* __end_ptr = ((const unsigned char*)&_M_w)+sizeof(_M_w);
403     while ( __byte_ptr < __end_ptr ) {
404       __result += _Bit_count<true>::_S_bit_count[*__byte_ptr];
405       __byte_ptr++;
406     }
407     return __result;
408   }
410   unsigned long _M_do_to_ulong() const {
411     if (sizeof(_WordT) <= sizeof(unsigned long))
412         return static_cast<unsigned long>(_M_w);
413     else {
414       const _WordT __mask = static_cast<_WordT>(static_cast<unsigned long>(-1));
415       if (_M_w & ~__mask)
416         __STL_THROW(overflow_error("bitset"));
417       return static_cast<unsigned long>(_M_w);
418     }
419   }
421   size_t _M_do_find_first(size_t __not_found) const;
423   // find the next "on" bit that follows "prev"
424   size_t _M_do_find_next(size_t __prev, size_t __not_found) const;
429 // Definitions of non-inline functions from the single-word version of
430 //  _Base_bitset.
433 template <class _WordT>
434 _Base_bitset<1, _WordT>::_Base_bitset(unsigned long __val)
436   _M_do_reset();
437   const size_t __n = min(sizeof(unsigned long)*CHAR_BIT,
438                          __BITS_PER_WORDT(_WordT)*_Nw);
439   for(size_t __i = 0; __i < __n; ++__i, __val >>= 1)
440     if ( __val & 0x1 )
441       _M_w |= _S_maskbit(__i);
444 template <class _WordT>
445 size_t _Base_bitset<1, _WordT>::_M_do_find_first(size_t __not_found) const
447   _WordT __thisword = _M_w;
449   if ( __thisword != static_cast<_WordT>(0) ) {
450     // find byte within word
451     for ( size_t __j = 0; __j < sizeof(_WordT); __j++ ) {
452       unsigned char __this_byte
453         = static_cast<unsigned char>(__thisword & (~(unsigned char)0));
454       if ( __this_byte )
455         return __j*CHAR_BIT + _First_one<true>::_S_first_one[__this_byte];
457       __thisword >>= CHAR_BIT;
458     }
459   }
460   // not found, so return a value that indicates failure.
461   return __not_found;
464 template <class _WordT>
465 size_t
466 _Base_bitset<1, _WordT>::_M_do_find_next(size_t __prev,
467                                          size_t __not_found ) const
469   // make bound inclusive
470   ++__prev;
472   // check out of bounds
473   if ( __prev >= __BITS_PER_WORDT(_WordT) )
474     return __not_found;
476     // search first (and only) word
477   _WordT __thisword = _M_w;
479   // mask off bits below bound
480   __thisword &= (~static_cast<_WordT>(0)) << _S_whichbit(__prev);
482   if ( __thisword != static_cast<_WordT>(0) ) {
483     // find byte within word
484     // get first byte into place
485     __thisword >>= _S_whichbyte(__prev) * CHAR_BIT;
486     for ( size_t __j = _S_whichbyte(__prev); __j < sizeof(_WordT); __j++ ) {
487       unsigned char __this_byte
488         = static_cast<unsigned char>(__thisword & (~(unsigned char)0));
489       if ( __this_byte )
490         return __j*CHAR_BIT + _First_one<true>::_S_first_one[__this_byte];
492       __thisword >>= CHAR_BIT;
493     }
494   }
496   // not found, so return a value that indicates failure.
497   return __not_found;
498 } // end _M_do_find_next
501 // One last specialization: _M_do_to_ulong() and the constructor from
502 // unsigned long are very simple if the bitset consists of a single
503 // word of type unsigned long.
506 template<>
507 inline unsigned long
508 _Base_bitset<1, unsigned long>::_M_do_to_ulong() const { return _M_w; }
510 template<>
511 inline _Base_bitset<1, unsigned long>::_Base_bitset(unsigned long __val) {
512   _M_w = __val;
516 // ------------------------------------------------------------
517 // Helper class to zero out the unused high-order bits in the highest word.
519 template <class _WordT, size_t _Extrabits> struct _Sanitize {
520   static void _M_do_sanitize(_WordT& __val)
521     { __val &= ~((~static_cast<_WordT>(0)) << _Extrabits); }
524 template <class _WordT> struct _Sanitize<_WordT, 0> {
525   static void _M_do_sanitize(_WordT) {}
528 // ------------------------------------------------------------
529 // Class bitset.
530 //   _Nb may be any nonzero number of type size_t.
531 //   Type _WordT may be any unsigned integral type.
533 template<size_t _Nb, class _WordT = unsigned long>
534 class bitset : private _Base_bitset<__BITSET_WORDS(_Nb,_WordT), _WordT>
536 private:
537   typedef _Base_bitset<__BITSET_WORDS(_Nb,_WordT), _WordT> _Base;
539   // Import base's protected interface.  Necessary because of new template
540   // name resolution rules.
541   using _Base::_S_whichword;
542   using _Base::_S_whichbyte;
543   using _Base::_S_whichbit;
544   using _Base::_S_maskbit;
545   using _Base::_M_getword;
546   using _Base::_M_hiword;
547   using _Base::_M_do_and;
548   using _Base::_M_do_or;
549   using _Base::_M_do_xor;
550   using _Base::_M_do_left_shift;
551   using _Base::_M_do_right_shift;
552   using _Base::_M_do_flip;
553   using _Base::_M_do_set;
554   using _Base::_M_do_reset;
555   using _Base::_M_is_equal;
556   using _Base::_M_is_any;
557   using _Base::_M_do_count;
558   using _Base::_M_do_to_ulong;
559   using _Base::_M_do_find_first;
560   using _Base::_M_do_find_next;
562 private:
563   void _M_do_sanitize() {
564     _Sanitize<_WordT,_Nb%__BITS_PER_WORDT(_WordT) >
565       ::_M_do_sanitize(_M_hiword());
566   }
568 public:
570   // bit reference:
571   class reference {
572     friend class bitset;
574     _WordT *_M_wp;
575     size_t _M_bpos;
577     // left undefined
578     reference();
580     reference( bitset& __b, size_t __pos ) {
581       _M_wp = &__b._M_getword(__pos);
582       _M_bpos = _S_whichbit(__pos);
583     }
585   public:
586     ~reference() {}
588     // for b[i] = __x;
589     reference& operator=(bool __x) {
590       if ( __x )
591         *_M_wp |= _S_maskbit(_M_bpos);
592       else
593         *_M_wp &= ~_S_maskbit(_M_bpos);
595       return *this;
596     }
598     // for b[i] = b[__j];
599     reference& operator=(const reference& __j) {
600       if ( (*(__j._M_wp) & _S_maskbit(__j._M_bpos)) )
601         *_M_wp |= _S_maskbit(_M_bpos);
602       else
603         *_M_wp &= ~_S_maskbit(_M_bpos);
605       return *this;
606     }
608     // flips the bit
609     bool operator~() const { return (*(_M_wp) & _S_maskbit(_M_bpos)) == 0; }
611     // for __x = b[i];
612     operator bool() const { return (*(_M_wp) & _S_maskbit(_M_bpos)) != 0; }
614     // for b[i].flip();
615     reference& flip() {
616       *_M_wp ^= _S_maskbit(_M_bpos);
617       return *this;
618     }
619   };
621   // 23.3.5.1 constructors:
622   bitset() {}
623   bitset(unsigned long __val) :
624     _Base_bitset<__BITSET_WORDS(_Nb,_WordT), _WordT>(__val) {}
626   template<class _CharT, class _Traits, class _Alloc>
627   explicit bitset(const basic_string<_CharT,_Traits,_Alloc>& __s,
628                   size_t __pos = 0,
629                   size_t __n = size_t(basic_string<_CharT,_Traits,_Alloc>::npos))
630     : _Base()
631   {
632     if (__pos > __s.size())
633       __STL_THROW(out_of_range("bitset"));
634     _M_copy_from_string(__s, __pos, __n);
635   }
637   // 23.3.5.2 bitset operations:
638   bitset<_Nb,_WordT>& operator&=(const bitset<_Nb,_WordT>& __rhs) {
639     _M_do_and(__rhs);
640     return *this;
641   }
643   bitset<_Nb,_WordT>& operator|=(const bitset<_Nb,_WordT>& __rhs) {
644     _M_do_or(__rhs);
645     return *this;
646   }
648   bitset<_Nb,_WordT>& operator^=(const bitset<_Nb,_WordT>& __rhs) {
649     _M_do_xor(__rhs);
650     return *this;
651   }
653   bitset<_Nb,_WordT>& operator<<=(size_t __pos) {
654     _M_do_left_shift(__pos);
655     _M_do_sanitize();
656     return *this;
657   }
659   bitset<_Nb,_WordT>& operator>>=(size_t __pos) {
660     _M_do_right_shift(__pos);
661     _M_do_sanitize();
662     return *this;
663   }
665   //
666   // Extension:
667   // Versions of single-bit set, reset, flip, test with no range checking.
668   //
670   bitset<_Nb,_WordT>& _Unchecked_set(size_t __pos) {
671     _M_getword(__pos) |= _S_maskbit(__pos);
672     return *this;
673   }
675   bitset<_Nb,_WordT>& _Unchecked_set(size_t __pos, int __val) {
676     if (__val)
677       _M_getword(__pos) |= _S_maskbit(__pos);
678     else
679       _M_getword(__pos) &= ~_S_maskbit(__pos);
681     return *this;
682   }
684   bitset<_Nb,_WordT>& _Unchecked_reset(size_t __pos) {
685     _M_getword(__pos) &= ~_S_maskbit(__pos);
686     return *this;
687   }
689   bitset<_Nb,_WordT>& _Unchecked_flip(size_t __pos) {
690     _M_getword(__pos) ^= _S_maskbit(__pos);
691     return *this;
692   }
694   bool _Unchecked_test(size_t __pos) const {
695     return (_M_getword(__pos) & _S_maskbit(__pos)) != static_cast<_WordT>(0);
696   }
698   // Set, reset, and flip.
700   bitset<_Nb,_WordT>& set() {
701     _M_do_set();
702     _M_do_sanitize();
703     return *this;
704   }
706   bitset<_Nb,_WordT>& set(size_t __pos) {
707     if (__pos >= _Nb)
708       __STL_THROW(out_of_range("bitset"));
710     return _Unchecked_set(__pos);
711   }
713   bitset<_Nb,_WordT>& set(size_t __pos, int __val) {
714     if (__pos >= _Nb)
715       __STL_THROW(out_of_range("bitset"));
717     return _Unchecked_set(__pos, __val);
718   }
720   bitset<_Nb,_WordT>& reset() {
721     _M_do_reset();
722     return *this;
723   }
725   bitset<_Nb,_WordT>& reset(size_t __pos) {
726     if (__pos >= _Nb)
727       __STL_THROW(out_of_range("bitset"));
729     return _Unchecked_reset(__pos);
730   }
732   bitset<_Nb,_WordT>& flip() {
733     _M_do_flip();
734     _M_do_sanitize();
735     return *this;
736   }
738   bitset<_Nb,_WordT>& flip(size_t __pos) {
739     if (__pos >= _Nb)
740       __STL_THROW(out_of_range("bitset"));
742     return _Unchecked_flip(__pos);
743   }
745   bitset<_Nb,_WordT> operator~() const {
746     return bitset<_Nb,_WordT>(*this).flip();
747   }
749   // element access:
750   //for b[i];
751   reference operator[](size_t __pos) { return reference(*this,__pos); }
752   bool operator[](size_t __pos) const { return _Unchecked_test(__pos); }
754   unsigned long to_ulong() const { return _M_do_to_ulong(); }
756 #ifdef __STL_EXPLICIT_FUNCTION_TMPL_ARGS
757   template <class _CharT, class _Traits, class _Alloc>
758   basic_string<_CharT, _Traits, _Alloc> to_string() const {
759     basic_string<_CharT, _Traits, _Alloc> __result;
760     _M_copy_to_string(__result);
761     return __result;
762   }
763 #endif /* __STL_EXPLICIT_FUNCTION_TMPL_ARGS */
765   // Helper functions for string operations.
766   template<class _CharT, class _Traits, class _Alloc>
767   void _M_copy_from_string(const basic_string<_CharT,_Traits,_Alloc>& __s,
768                           size_t,
769                           size_t);
771   // Helper functions for string operations.
772   template<class _CharT, class _Traits, class _Alloc>
773   void _M_copy_to_string(basic_string<_CharT,_Traits,_Alloc>&) const;
775   size_t count() const { return _M_do_count(); }
777   size_t size() const { return _Nb; }
779   bool operator==(const bitset<_Nb,_WordT>& __rhs) const {
780     return _M_is_equal(__rhs);
781   }
782   bool operator!=(const bitset<_Nb,_WordT>& __rhs) const {
783     return !_M_is_equal(__rhs);
784   }
786   bool test(size_t __pos) const {
787     if (__pos > _Nb)
788       __STL_THROW(out_of_range("bitset"));
790     return _Unchecked_test(__pos);
791   }
793   bool any() const { return _M_is_any(); }
794   bool none() const { return !_M_is_any(); }
796   bitset<_Nb,_WordT> operator<<(size_t __pos) const
797     { return bitset<_Nb,_WordT>(*this) <<= __pos; }
798   bitset<_Nb,_WordT> operator>>(size_t __pos) const
799     { return bitset<_Nb,_WordT>(*this) >>= __pos; }
801   //
802   // EXTENSIONS: bit-find operations.  These operations are
803   // experimental, and are subject to change or removal in future
804   // versions.
805   //
807   // find the index of the first "on" bit
808   size_t _Find_first() const
809     { return _M_do_find_first(_Nb); }
811   // find the index of the next "on" bit after prev
812   size_t _Find_next( size_t __prev ) const
813     { return _M_do_find_next(__prev, _Nb); }
818 // Definitions of non-inline member functions.
821 template <size_t _Nb, class _WordT>
822 template<class _CharT, class _Traits, class _Alloc>
823 void bitset<_Nb, _WordT>
824   ::_M_copy_from_string(const basic_string<_CharT,_Traits,_Alloc>& __s,
825                         size_t __pos,
826                         size_t __n)
828   reset();
829   const size_t __nbits = min(_Nb, min(__n, __s.size() - __pos));
830   for (size_t __i = 0; __i < __nbits; ++__i) {
831     switch(__s[__pos + __nbits - __i - 1]) {
832     case '0':
833       break;
834     case '1':
835       set(__i);
836       break;
837     default:
838       __STL_THROW(invalid_argument("bitset"));
839     }
840   }
843 template <size_t _Nb, class _WordT>
844 template <class _CharT, class _Traits, class _Alloc>
845 void bitset<_Nb, _WordT>
846   ::_M_copy_to_string(basic_string<_CharT, _Traits, _Alloc>& __s) const
848   __s.assign(_Nb, '0');
850   for (size_t __i = 0; __i < _Nb; ++__i)
851     if (_Unchecked_test(__i))
852       __s[_Nb - 1 - __i] = '1';
855 // ------------------------------------------------------------
858 // 23.3.5.3 bitset operations:
861 template <size_t _Nb, class _WordT>
862 inline bitset<_Nb,_WordT> operator&(const bitset<_Nb,_WordT>& __x,
863                                     const bitset<_Nb,_WordT>& __y) {
864   bitset<_Nb,_WordT> __result(__x);
865   __result &= __y;
866   return __result;
870 template <size_t _Nb, class _WordT>
871 inline bitset<_Nb,_WordT> operator|(const bitset<_Nb,_WordT>& __x,
872                                     const bitset<_Nb,_WordT>& __y) {
873   bitset<_Nb,_WordT> __result(__x);
874   __result |= __y;
875   return __result;
878 template <size_t _Nb, class _WordT>
879 inline bitset<_Nb,_WordT> operator^(const bitset<_Nb,_WordT>& __x,
880                                     const bitset<_Nb,_WordT>& __y) {
881   bitset<_Nb,_WordT> __result(__x);
882   __result ^= __y;
883   return __result;
886 // NOTE: these must be rewritten once we have templatized iostreams.
888 template <size_t _Nb, class _WordT>
889 istream&
890 operator>>(istream& __is, bitset<_Nb,_WordT>& __x) {
891   string __tmp;
892   __tmp.reserve(_Nb);
894   // In new templatized iostreams, use istream::sentry
895   if (__is.flags() & ios::skipws) {
896     char __c;
897     do
898       __is.get(__c);
899     while (__is && isspace(__c));
900     if (__is)
901       __is.putback(__c);
902   }
904   for (size_t __i = 0; __i < _Nb; ++__i) {
905     char __c;
906     __is.get(__c);
908     if (!__is)
909       break;
910     else if (__c != '0' && __c != '1') {
911       __is.putback(__c);
912       break;
913     }
914     else
915       __tmp.push_back(__c);
916   }
918   if (__tmp.empty())
919     __is.clear(__is.rdstate() | ios::failbit);
920   else
921     __x._M_copy_from_string(__tmp, static_cast<size_t>(0), _Nb);
923   return __is;
926 template <size_t _Nb, class _WordT>
927 ostream& operator<<(ostream& __os, const bitset<_Nb,_WordT>& __x) {
928   string __tmp;
929   __x._M_copy_to_string(__tmp);
930   return __os << __tmp;
933 // ------------------------------------------------------------
934 // Lookup tables for find and count operations.
936 template<bool __dummy>
937 unsigned char _Bit_count<__dummy>::_S_bit_count[] = {
938   0, /*   0 */ 1, /*   1 */ 1, /*   2 */ 2, /*   3 */ 1, /*   4 */
939   2, /*   5 */ 2, /*   6 */ 3, /*   7 */ 1, /*   8 */ 2, /*   9 */
940   2, /*  10 */ 3, /*  11 */ 2, /*  12 */ 3, /*  13 */ 3, /*  14 */
941   4, /*  15 */ 1, /*  16 */ 2, /*  17 */ 2, /*  18 */ 3, /*  19 */
942   2, /*  20 */ 3, /*  21 */ 3, /*  22 */ 4, /*  23 */ 2, /*  24 */
943   3, /*  25 */ 3, /*  26 */ 4, /*  27 */ 3, /*  28 */ 4, /*  29 */
944   4, /*  30 */ 5, /*  31 */ 1, /*  32 */ 2, /*  33 */ 2, /*  34 */
945   3, /*  35 */ 2, /*  36 */ 3, /*  37 */ 3, /*  38 */ 4, /*  39 */
946   2, /*  40 */ 3, /*  41 */ 3, /*  42 */ 4, /*  43 */ 3, /*  44 */
947   4, /*  45 */ 4, /*  46 */ 5, /*  47 */ 2, /*  48 */ 3, /*  49 */
948   3, /*  50 */ 4, /*  51 */ 3, /*  52 */ 4, /*  53 */ 4, /*  54 */
949   5, /*  55 */ 3, /*  56 */ 4, /*  57 */ 4, /*  58 */ 5, /*  59 */
950   4, /*  60 */ 5, /*  61 */ 5, /*  62 */ 6, /*  63 */ 1, /*  64 */
951   2, /*  65 */ 2, /*  66 */ 3, /*  67 */ 2, /*  68 */ 3, /*  69 */
952   3, /*  70 */ 4, /*  71 */ 2, /*  72 */ 3, /*  73 */ 3, /*  74 */
953   4, /*  75 */ 3, /*  76 */ 4, /*  77 */ 4, /*  78 */ 5, /*  79 */
954   2, /*  80 */ 3, /*  81 */ 3, /*  82 */ 4, /*  83 */ 3, /*  84 */
955   4, /*  85 */ 4, /*  86 */ 5, /*  87 */ 3, /*  88 */ 4, /*  89 */
956   4, /*  90 */ 5, /*  91 */ 4, /*  92 */ 5, /*  93 */ 5, /*  94 */
957   6, /*  95 */ 2, /*  96 */ 3, /*  97 */ 3, /*  98 */ 4, /*  99 */
958   3, /* 100 */ 4, /* 101 */ 4, /* 102 */ 5, /* 103 */ 3, /* 104 */
959   4, /* 105 */ 4, /* 106 */ 5, /* 107 */ 4, /* 108 */ 5, /* 109 */
960   5, /* 110 */ 6, /* 111 */ 3, /* 112 */ 4, /* 113 */ 4, /* 114 */
961   5, /* 115 */ 4, /* 116 */ 5, /* 117 */ 5, /* 118 */ 6, /* 119 */
962   4, /* 120 */ 5, /* 121 */ 5, /* 122 */ 6, /* 123 */ 5, /* 124 */
963   6, /* 125 */ 6, /* 126 */ 7, /* 127 */ 1, /* 128 */ 2, /* 129 */
964   2, /* 130 */ 3, /* 131 */ 2, /* 132 */ 3, /* 133 */ 3, /* 134 */
965   4, /* 135 */ 2, /* 136 */ 3, /* 137 */ 3, /* 138 */ 4, /* 139 */
966   3, /* 140 */ 4, /* 141 */ 4, /* 142 */ 5, /* 143 */ 2, /* 144 */
967   3, /* 145 */ 3, /* 146 */ 4, /* 147 */ 3, /* 148 */ 4, /* 149 */
968   4, /* 150 */ 5, /* 151 */ 3, /* 152 */ 4, /* 153 */ 4, /* 154 */
969   5, /* 155 */ 4, /* 156 */ 5, /* 157 */ 5, /* 158 */ 6, /* 159 */
970   2, /* 160 */ 3, /* 161 */ 3, /* 162 */ 4, /* 163 */ 3, /* 164 */
971   4, /* 165 */ 4, /* 166 */ 5, /* 167 */ 3, /* 168 */ 4, /* 169 */
972   4, /* 170 */ 5, /* 171 */ 4, /* 172 */ 5, /* 173 */ 5, /* 174 */
973   6, /* 175 */ 3, /* 176 */ 4, /* 177 */ 4, /* 178 */ 5, /* 179 */
974   4, /* 180 */ 5, /* 181 */ 5, /* 182 */ 6, /* 183 */ 4, /* 184 */
975   5, /* 185 */ 5, /* 186 */ 6, /* 187 */ 5, /* 188 */ 6, /* 189 */
976   6, /* 190 */ 7, /* 191 */ 2, /* 192 */ 3, /* 193 */ 3, /* 194 */
977   4, /* 195 */ 3, /* 196 */ 4, /* 197 */ 4, /* 198 */ 5, /* 199 */
978   3, /* 200 */ 4, /* 201 */ 4, /* 202 */ 5, /* 203 */ 4, /* 204 */
979   5, /* 205 */ 5, /* 206 */ 6, /* 207 */ 3, /* 208 */ 4, /* 209 */
980   4, /* 210 */ 5, /* 211 */ 4, /* 212 */ 5, /* 213 */ 5, /* 214 */
981   6, /* 215 */ 4, /* 216 */ 5, /* 217 */ 5, /* 218 */ 6, /* 219 */
982   5, /* 220 */ 6, /* 221 */ 6, /* 222 */ 7, /* 223 */ 3, /* 224 */
983   4, /* 225 */ 4, /* 226 */ 5, /* 227 */ 4, /* 228 */ 5, /* 229 */
984   5, /* 230 */ 6, /* 231 */ 4, /* 232 */ 5, /* 233 */ 5, /* 234 */
985   6, /* 235 */ 5, /* 236 */ 6, /* 237 */ 6, /* 238 */ 7, /* 239 */
986   4, /* 240 */ 5, /* 241 */ 5, /* 242 */ 6, /* 243 */ 5, /* 244 */
987   6, /* 245 */ 6, /* 246 */ 7, /* 247 */ 5, /* 248 */ 6, /* 249 */
988   6, /* 250 */ 7, /* 251 */ 6, /* 252 */ 7, /* 253 */ 7, /* 254 */
989   8  /* 255 */
990 }; // end _Bit_count
992 template<bool __dummy>
993 unsigned char _First_one<__dummy>::_S_first_one[] = {
994   0, /*   0 */ 0, /*   1 */ 1, /*   2 */ 0, /*   3 */ 2, /*   4 */
995   0, /*   5 */ 1, /*   6 */ 0, /*   7 */ 3, /*   8 */ 0, /*   9 */
996   1, /*  10 */ 0, /*  11 */ 2, /*  12 */ 0, /*  13 */ 1, /*  14 */
997   0, /*  15 */ 4, /*  16 */ 0, /*  17 */ 1, /*  18 */ 0, /*  19 */
998   2, /*  20 */ 0, /*  21 */ 1, /*  22 */ 0, /*  23 */ 3, /*  24 */
999   0, /*  25 */ 1, /*  26 */ 0, /*  27 */ 2, /*  28 */ 0, /*  29 */
1000   1, /*  30 */ 0, /*  31 */ 5, /*  32 */ 0, /*  33 */ 1, /*  34 */
1001   0, /*  35 */ 2, /*  36 */ 0, /*  37 */ 1, /*  38 */ 0, /*  39 */
1002   3, /*  40 */ 0, /*  41 */ 1, /*  42 */ 0, /*  43 */ 2, /*  44 */
1003   0, /*  45 */ 1, /*  46 */ 0, /*  47 */ 4, /*  48 */ 0, /*  49 */
1004   1, /*  50 */ 0, /*  51 */ 2, /*  52 */ 0, /*  53 */ 1, /*  54 */
1005   0, /*  55 */ 3, /*  56 */ 0, /*  57 */ 1, /*  58 */ 0, /*  59 */
1006   2, /*  60 */ 0, /*  61 */ 1, /*  62 */ 0, /*  63 */ 6, /*  64 */
1007   0, /*  65 */ 1, /*  66 */ 0, /*  67 */ 2, /*  68 */ 0, /*  69 */
1008   1, /*  70 */ 0, /*  71 */ 3, /*  72 */ 0, /*  73 */ 1, /*  74 */
1009   0, /*  75 */ 2, /*  76 */ 0, /*  77 */ 1, /*  78 */ 0, /*  79 */
1010   4, /*  80 */ 0, /*  81 */ 1, /*  82 */ 0, /*  83 */ 2, /*  84 */
1011   0, /*  85 */ 1, /*  86 */ 0, /*  87 */ 3, /*  88 */ 0, /*  89 */
1012   1, /*  90 */ 0, /*  91 */ 2, /*  92 */ 0, /*  93 */ 1, /*  94 */
1013   0, /*  95 */ 5, /*  96 */ 0, /*  97 */ 1, /*  98 */ 0, /*  99 */
1014   2, /* 100 */ 0, /* 101 */ 1, /* 102 */ 0, /* 103 */ 3, /* 104 */
1015   0, /* 105 */ 1, /* 106 */ 0, /* 107 */ 2, /* 108 */ 0, /* 109 */
1016   1, /* 110 */ 0, /* 111 */ 4, /* 112 */ 0, /* 113 */ 1, /* 114 */
1017   0, /* 115 */ 2, /* 116 */ 0, /* 117 */ 1, /* 118 */ 0, /* 119 */
1018   3, /* 120 */ 0, /* 121 */ 1, /* 122 */ 0, /* 123 */ 2, /* 124 */
1019   0, /* 125 */ 1, /* 126 */ 0, /* 127 */ 7, /* 128 */ 0, /* 129 */
1020   1, /* 130 */ 0, /* 131 */ 2, /* 132 */ 0, /* 133 */ 1, /* 134 */
1021   0, /* 135 */ 3, /* 136 */ 0, /* 137 */ 1, /* 138 */ 0, /* 139 */
1022   2, /* 140 */ 0, /* 141 */ 1, /* 142 */ 0, /* 143 */ 4, /* 144 */
1023   0, /* 145 */ 1, /* 146 */ 0, /* 147 */ 2, /* 148 */ 0, /* 149 */
1024   1, /* 150 */ 0, /* 151 */ 3, /* 152 */ 0, /* 153 */ 1, /* 154 */
1025   0, /* 155 */ 2, /* 156 */ 0, /* 157 */ 1, /* 158 */ 0, /* 159 */
1026   5, /* 160 */ 0, /* 161 */ 1, /* 162 */ 0, /* 163 */ 2, /* 164 */
1027   0, /* 165 */ 1, /* 166 */ 0, /* 167 */ 3, /* 168 */ 0, /* 169 */
1028   1, /* 170 */ 0, /* 171 */ 2, /* 172 */ 0, /* 173 */ 1, /* 174 */
1029   0, /* 175 */ 4, /* 176 */ 0, /* 177 */ 1, /* 178 */ 0, /* 179 */
1030   2, /* 180 */ 0, /* 181 */ 1, /* 182 */ 0, /* 183 */ 3, /* 184 */
1031   0, /* 185 */ 1, /* 186 */ 0, /* 187 */ 2, /* 188 */ 0, /* 189 */
1032   1, /* 190 */ 0, /* 191 */ 6, /* 192 */ 0, /* 193 */ 1, /* 194 */
1033   0, /* 195 */ 2, /* 196 */ 0, /* 197 */ 1, /* 198 */ 0, /* 199 */
1034   3, /* 200 */ 0, /* 201 */ 1, /* 202 */ 0, /* 203 */ 2, /* 204 */
1035   0, /* 205 */ 1, /* 206 */ 0, /* 207 */ 4, /* 208 */ 0, /* 209 */
1036   1, /* 210 */ 0, /* 211 */ 2, /* 212 */ 0, /* 213 */ 1, /* 214 */
1037   0, /* 215 */ 3, /* 216 */ 0, /* 217 */ 1, /* 218 */ 0, /* 219 */
1038   2, /* 220 */ 0, /* 221 */ 1, /* 222 */ 0, /* 223 */ 5, /* 224 */
1039   0, /* 225 */ 1, /* 226 */ 0, /* 227 */ 2, /* 228 */ 0, /* 229 */
1040   1, /* 230 */ 0, /* 231 */ 3, /* 232 */ 0, /* 233 */ 1, /* 234 */
1041   0, /* 235 */ 2, /* 236 */ 0, /* 237 */ 1, /* 238 */ 0, /* 239 */
1042   4, /* 240 */ 0, /* 241 */ 1, /* 242 */ 0, /* 243 */ 2, /* 244 */
1043   0, /* 245 */ 1, /* 246 */ 0, /* 247 */ 3, /* 248 */ 0, /* 249 */
1044   1, /* 250 */ 0, /* 251 */ 2, /* 252 */ 0, /* 253 */ 1, /* 254 */
1045   0, /* 255 */
1046 }; // end _First_one
1048 #if defined(__sgi) && !defined(__GNUC__) && (_MIPS_SIM != _MIPS_SIM_ABI32)
1049 #pragma reset woff 1209
1050 #endif
1052 __STL_END_NAMESPACE
1055 #undef __BITS_PER_WORDT
1056 #undef __BITSET_WORDS
1058 #endif /* __SGI_STL_BITSET */
1061 // Local Variables:
1062 // mode:C++
1063 // End: