1 /*
2  * Copyright (c) 1998
3  * Silicon Graphics Computer Systems, Inc.
4  *
5  * Copyright (c) 1999
6  * Boris Fomitchev
7  *
8  * This material is provided "as is", with absolutely no warranty expressed
9  * or implied. Any use is at your own risk.
10  *
11  * Permission to use or copy this software for any purpose is hereby granted
12  * without fee, provided the above notices are retained on all copies.
13  * Permission to modify the code and to distribute modified code is granted,
14  * provided the above notices are retained, and a notice that the code was
15  * modified is included with the above copyright notice.
16  *
17  */
18 
19 #ifndef _STLP_BITSET_H
20 #define _STLP_BITSET_H
21 
22 // A bitset of size N has N % (sizeof(unsigned long) * CHAR_BIT) unused
23 // bits.  (They are the high- order bits in the highest word.)  It is
24 // a class invariant of class bitset<> that those unused bits are
25 // always zero.
26 
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.
31 
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.
36 
37 
38 #ifndef _STLP_INTERNAL_ALGOBASE_H
39 #  include <stl/_algobase.h>
40 #endif
41 
42 #ifndef _STLP_INTERNAL_ALLOC_H
43 #  include <stl/_alloc.h>
44 #endif
45 
46 #ifndef _STLP_INTERNAL_ITERATOR_H
47 #  include <stl/_iterator.h>
48 #endif
49 
50 #ifndef _STLP_INTERNAL_UNINITIALIZED_H
51 #  include <stl/_uninitialized.h>
52 #endif
53 
54 #ifndef _STLP_RANGE_ERRORS_H
55 #  include <stl/_range_errors.h>
56 #endif
57 
58 #ifndef _STLP_INTERNAL_STRING_H
59 #  include <stl/_string.h>
60 #endif
61 
62 #define __BITS_PER_WORD (CHAR_BIT*sizeof(unsigned long))
63 #define __BITSET_WORDS(__n) ((__n + __BITS_PER_WORD - 1)/__BITS_PER_WORD)
64 
65 _STLP_BEGIN_NAMESPACE
66 
67 _STLP_MOVE_TO_PRIV_NAMESPACE
68 
69 // structure to aid in counting bits
70 class _STLP_CLASS_DECLSPEC _Bs_G
71 {
72   public:
73     //returns the number of bit set within the buffer between __beg and __end.
_S_count(const unsigned char * __beg,const unsigned char * __end)74     static size_t _S_count(const unsigned char *__beg, const unsigned char *__end)
75 #if defined (_STLP_USE_NO_IOSTREAMS)
76     {
77       size_t __result = 0;
78       for (; __beg != __end; ++__beg) {
79         for (size_t i = 0; i < (sizeof(unsigned char) * 8); ++i) {
80           if ((*__beg & (1 << i)) != 0) { ++__result; }
81         }
82       }
83       return __result;
84     }
85 #else
86       ;
87 #endif
88     // Mapping from 8 bit unsigned integers to the index of the first one bit set:
_S_first_one(unsigned char __x)89     static unsigned char _S_first_one(unsigned char __x)
90 #if defined (_STLP_USE_NO_IOSTREAMS)
91     {
92       for (unsigned char i = 0; i < (sizeof(unsigned char) * 8); ++i) {
93         if ((__x & (1 << i)) != 0) { return i; }
94       }
95       return 0;
96     }
97 #else
98       ;
99 #endif
100 };
101 
102 //
103 // Base class: general case.
104 //
105 
106 template<size_t _Nw>
107 struct _Base_bitset {
108   typedef unsigned long _WordT;
109 
110   _WordT _M_w[_Nw];                // 0 is the least significant word.
111 
_Base_bitset_Base_bitset112   _Base_bitset() { _M_do_reset(); }
113 
_Base_bitset_Base_bitset114   _Base_bitset(unsigned long __val) {
115     _M_do_reset();
116     _M_w[0] = __val;
117   }
118 
_S_whichword_Base_bitset119   static size_t _STLP_CALL _S_whichword( size_t __pos ) {
120     return __pos / __BITS_PER_WORD;
121   }
_S_whichbyte_Base_bitset122   static size_t _STLP_CALL _S_whichbyte( size_t __pos ) {
123     return (__pos % __BITS_PER_WORD) / CHAR_BIT;
124   }
_S_whichbit_Base_bitset125   static size_t _STLP_CALL _S_whichbit( size_t __pos ) {
126     return __pos % __BITS_PER_WORD;
127   }
_S_maskbit_Base_bitset128   static _WordT _STLP_CALL _S_maskbit( size_t __pos ) {
129     return __STATIC_CAST(_WordT,1) << _S_whichbit(__pos);
130   }
131 
_M_getword_Base_bitset132   _WordT& _M_getword(size_t __pos)       { return _M_w[_S_whichword(__pos)]; }
_M_getword_Base_bitset133   _WordT  _M_getword(size_t __pos) const { return _M_w[_S_whichword(__pos)]; }
134 
_M_hiword_Base_bitset135   _WordT& _M_hiword()       { return _M_w[_Nw - 1]; }
_M_hiword_Base_bitset136   _WordT  _M_hiword() const { return _M_w[_Nw - 1]; }
137 
_M_do_and_Base_bitset138   void _M_do_and(const _Base_bitset<_Nw>& __x) {
139     for ( size_t __i = 0; __i < _Nw; __i++ ) {
140       _M_w[__i] &= __x._M_w[__i];
141     }
142   }
143 
_M_do_or_Base_bitset144   void _M_do_or(const _Base_bitset<_Nw>& __x) {
145     for ( size_t __i = 0; __i < _Nw; __i++ ) {
146       _M_w[__i] |= __x._M_w[__i];
147     }
148   }
149 
_M_do_xor_Base_bitset150   void _M_do_xor(const _Base_bitset<_Nw>& __x) {
151     for ( size_t __i = 0; __i < _Nw; __i++ ) {
152       _M_w[__i] ^= __x._M_w[__i];
153     }
154   }
155 
156   void _M_do_left_shift(size_t __shift);
157 
158   void _M_do_right_shift(size_t __shift);
159 
_M_do_flip_Base_bitset160   void _M_do_flip() {
161     for ( size_t __i = 0; __i < _Nw; __i++ ) {
162       _M_w[__i] = ~_M_w[__i];
163     }
164   }
165 
_M_do_set_Base_bitset166   void _M_do_set() {
167     for ( size_t __i = 0; __i < _Nw; __i++ ) {
168       _M_w[__i] = ~__STATIC_CAST(_WordT,0);
169     }
170   }
171 
_M_do_reset_Base_bitset172   void _M_do_reset() { memset(_M_w, 0, _Nw * sizeof(_WordT)); }
173 
_M_is_equal_Base_bitset174   bool _M_is_equal(const _Base_bitset<_Nw>& __x) const {
175     for (size_t __i = 0; __i < _Nw; ++__i) {
176       if (_M_w[__i] != __x._M_w[__i])
177         return false;
178     }
179     return true;
180   }
181 
_M_is_any_Base_bitset182   bool _M_is_any() const {
183     for ( size_t __i = 0; __i < _Nw ; __i++ ) {
184       if ( _M_w[__i] != __STATIC_CAST(_WordT,0) )
185         return true;
186     }
187     return false;
188   }
189 
_M_do_count_Base_bitset190   size_t _M_do_count() const {
191     const unsigned char* __byte_ptr = (const unsigned char*)_M_w;
192     const unsigned char* __end_ptr = (const unsigned char*)(_M_w+_Nw);
193 
194     return _Bs_G::_S_count(__byte_ptr, __end_ptr);
195   }
196 
197   unsigned long _M_do_to_ulong() const;
198 
199   // find first "on" bit
200   size_t _M_do_find_first(size_t __not_found) const;
201 
202   // find the next "on" bit that follows "prev"
203   size_t _M_do_find_next(size_t __prev, size_t __not_found) const;
204 };
205 
206 //
207 // Base class: specialization for a single word.
208 //
209 _STLP_TEMPLATE_NULL
210 struct _Base_bitset<1UL> {
211   typedef unsigned long _WordT;
212   typedef _Base_bitset<1UL> _Self;
213 
214   _WordT _M_w;
215 
216   _Base_bitset( void ) : _M_w(0) {}
217   _Base_bitset(unsigned long __val) : _M_w(__val) {}
218 
219   static size_t _STLP_CALL _S_whichword( size_t __pos ) {
220     return __pos / __BITS_PER_WORD ;
221   }
222   static size_t _STLP_CALL _S_whichbyte( size_t __pos ) {
223     return (__pos % __BITS_PER_WORD) / CHAR_BIT;
224   }
225   static size_t _STLP_CALL _S_whichbit( size_t __pos ) {
226     return __pos % __BITS_PER_WORD;
227   }
228   static _WordT _STLP_CALL _S_maskbit( size_t __pos ) {
229     return (__STATIC_CAST(_WordT,1)) << _S_whichbit(__pos);
230   }
231 
232   _WordT& _M_getword(size_t)       { return _M_w; }
233   _WordT  _M_getword(size_t) const { return _M_w; }
234 
235   _WordT& _M_hiword()       { return _M_w; }
236   _WordT  _M_hiword() const { return _M_w; }
237 
238   void _M_do_and(const _Self& __x) { _M_w &= __x._M_w; }
239   void _M_do_or(const _Self& __x)  { _M_w |= __x._M_w; }
240   void _M_do_xor(const _Self& __x) { _M_w ^= __x._M_w; }
241   void _M_do_left_shift(size_t __shift)     { _M_w <<= __shift; }
242   void _M_do_right_shift(size_t __shift)    { _M_w >>= __shift; }
243   void _M_do_flip()                       { _M_w = ~_M_w; }
244   void _M_do_set()                        { _M_w = ~__STATIC_CAST(_WordT,0); }
245   void _M_do_reset()                      { _M_w = 0; }
246 
247   bool _M_is_equal(const _Self& __x) const {
248     return _M_w == __x._M_w;
249   }
250   bool _M_is_any() const {
251     return _M_w != 0;
252   }
253 
254   size_t _M_do_count() const {
255     const unsigned char* __byte_ptr = (const unsigned char*)&_M_w;
256     const unsigned char* __end_ptr = ((const unsigned char*)&_M_w)+sizeof(_M_w);
257     return _Bs_G::_S_count(__byte_ptr, __end_ptr);
258   }
259 
260   unsigned long _M_do_to_ulong() const { return _M_w; }
261 
262   inline size_t _M_do_find_first(size_t __not_found) const;
263 
264   // find the next "on" bit that follows "prev"
265   inline size_t _M_do_find_next(size_t __prev, size_t __not_found) const;
266 };
267 
268 
269 // ------------------------------------------------------------
270 //
271 // Definitions of should-be-non-inline functions from the single-word version of
272 //  _Base_bitset.
273 //
274 inline size_t
275 _Base_bitset<1UL>::_M_do_find_first(size_t __not_found) const {
276   //  typedef unsigned long _WordT;
277   _WordT __thisword = _M_w;
278 
279   if ( __thisword != __STATIC_CAST(_WordT,0) ) {
280     // find byte within word
281     for ( size_t __j = 0; __j < sizeof(_WordT); __j++ ) {
282       unsigned char __this_byte
283         = __STATIC_CAST(unsigned char,(__thisword & (~(unsigned char)0)));
284       if ( __this_byte )
285         return __j*CHAR_BIT + _Bs_G::_S_first_one(__this_byte);
286 
287       __thisword >>= CHAR_BIT;
288     }
289   }
290   // not found, so return a value that indicates failure.
291   return __not_found;
292 }
293 
294 inline size_t
295 _Base_bitset<1UL>::_M_do_find_next(size_t __prev,
296                                    size_t __not_found ) const {
297   // make bound inclusive
298   ++__prev;
299 
300   // check out of bounds
301   if ( __prev >= __BITS_PER_WORD )
302     return __not_found;
303 
304     // search first (and only) word
305   _WordT __thisword = _M_w;
306 
307   // mask off bits below bound
308   __thisword &= (~__STATIC_CAST(_WordT,0)) << _S_whichbit(__prev);
309 
310   if ( __thisword != __STATIC_CAST(_WordT,0) ) {
311     // find byte within word
312     // get first byte into place
313     __thisword >>= _S_whichbyte(__prev) * CHAR_BIT;
314     for ( size_t __j = _S_whichbyte(__prev); __j < sizeof(_WordT); __j++ ) {
315       unsigned char __this_byte
316         = __STATIC_CAST(unsigned char,(__thisword & (~(unsigned char)0)));
317       if ( __this_byte )
318         return __j*CHAR_BIT + _Bs_G::_S_first_one(__this_byte);
319 
320       __thisword >>= CHAR_BIT;
321     }
322   }
323 
324   // not found, so return a value that indicates failure.
325   return __not_found;
326 } // end _M_do_find_next
327 
328 
329 // ------------------------------------------------------------
330 // Helper class to zero out the unused high-order bits in the highest word.
331 
332 template <size_t _Extrabits> struct _Sanitize {
333   static void _STLP_CALL _M_do_sanitize(unsigned long& __val)
334   { __val &= ~((~__STATIC_CAST(unsigned long,0)) << _Extrabits); }
335 };
336 
337 _STLP_TEMPLATE_NULL struct _Sanitize<0UL> {
338   static void _STLP_CALL _M_do_sanitize(unsigned long) {}
339 };
340 
341 _STLP_MOVE_TO_STD_NAMESPACE
342 
343 // ------------------------------------------------------------
344 // Class bitset.
345 //   _Nb may be any nonzero number of type size_t.
346 template<size_t _Nb>
347 class bitset : public _STLP_PRIV _Base_bitset<__BITSET_WORDS(_Nb) > {
348 public:
349   enum { _Words = __BITSET_WORDS(_Nb) } ;
350 
351 private:
352   typedef _STLP_PRIV _Base_bitset< _Words > _Base;
353 
354   void _M_do_sanitize() {
355     _STLP_PRIV _Sanitize<_Nb%__BITS_PER_WORD >::_M_do_sanitize(this->_M_hiword());
356   }
357 public:
358   typedef unsigned long _WordT;
359   struct reference;
360   friend struct reference;
361 
362   // bit reference:
363   struct reference {
364   typedef _STLP_PRIV _Base_bitset<_Words > _Bitset_base;
365   typedef bitset<_Nb> _Bitset;
366     //    friend _Bitset;
367     _WordT *_M_wp;
368     size_t _M_bpos;
369 
370     // should be left undefined
371     reference() {}
372 
373     reference( _Bitset& __b, size_t __pos ) {
374       _M_wp = &__b._M_getword(__pos);
375       _M_bpos = _Bitset_base::_S_whichbit(__pos);
376     }
377 
378   public:
379     ~reference() {}
380 
381     // for b[i] = __x;
382     reference& operator=(bool __x) {
383       if ( __x )
384         *_M_wp |= _Bitset_base::_S_maskbit(_M_bpos);
385       else
386         *_M_wp &= ~_Bitset_base::_S_maskbit(_M_bpos);
387 
388       return *this;
389     }
390 
391     // for b[i] = b[__j];
392     reference& operator=(const reference& __j) {
393       if ( (*(__j._M_wp) & _Bitset_base::_S_maskbit(__j._M_bpos)) )
394         *_M_wp |= _Bitset_base::_S_maskbit(_M_bpos);
395       else
396         *_M_wp &= ~_Bitset_base::_S_maskbit(_M_bpos);
397 
398       return *this;
399     }
400 
401     // flips the bit
402     bool operator~() const { return (*(_M_wp) & _Bitset_base::_S_maskbit(_M_bpos)) == 0; }
403 
404     // for __x = b[i];
405     operator bool() const { return (*(_M_wp) & _Bitset_base::_S_maskbit(_M_bpos)) != 0; }
406 
407     // for b[i].flip();
408     reference& flip() {
409       *_M_wp ^= _Bitset_base::_S_maskbit(_M_bpos);
410       return *this;
411     }
412   };
413 
414   // 23.3.5.1 constructors:
415   bitset() {}
416 
417   bitset(unsigned long __val) : _STLP_PRIV _Base_bitset<_Words>(__val) { _M_do_sanitize(); }
418 
419 #if defined (_STLP_MEMBER_TEMPLATES)
420   template<class _CharT, class _Traits, class _Alloc>
421   explicit bitset(const basic_string<_CharT,_Traits,_Alloc>& __s,
422                   size_t __pos = 0)
423     : _STLP_PRIV _Base_bitset<_Words >() {
424     if (__pos > __s.size())
425       __stl_throw_out_of_range("bitset");
426     _M_copy_from_string(__s, __pos,
427                         basic_string<_CharT, _Traits, _Alloc>::npos);
428   }
429   template<class _CharT, class _Traits, class _Alloc>
430   bitset(const basic_string<_CharT, _Traits, _Alloc>& __s,
431           size_t __pos,
432           size_t __n)
433   : _STLP_PRIV _Base_bitset<_Words >() {
434     if (__pos > __s.size())
435       __stl_throw_out_of_range("bitset");
436     _M_copy_from_string(__s, __pos, __n);
437   }
438 #else /* _STLP_MEMBER_TEMPLATES */
439   explicit bitset(const string& __s,
440                   size_t __pos = 0,
441                   size_t __n = (size_t)-1)
442     : _STLP_PRIV _Base_bitset<_Words >() {
443     if (__pos > __s.size())
444       __stl_throw_out_of_range("bitset");
445     _M_copy_from_string(__s, __pos, __n);
446   }
447 #endif /* _STLP_MEMBER_TEMPLATES */
448 
449   // 23.3.5.2 bitset operations:
450   bitset<_Nb>& operator&=(const bitset<_Nb>& __rhs) {
451     this->_M_do_and(__rhs);
452     return *this;
453   }
454 
455   bitset<_Nb>& operator|=(const bitset<_Nb>& __rhs) {
456     this->_M_do_or(__rhs);
457     return *this;
458   }
459 
460   bitset<_Nb>& operator^=(const bitset<_Nb>& __rhs) {
461     this->_M_do_xor(__rhs);
462     return *this;
463   }
464 
465   bitset<_Nb>& operator<<=(size_t __pos) {
466     this->_M_do_left_shift(__pos);
467     this->_M_do_sanitize();
468     return *this;
469   }
470 
471   bitset<_Nb>& operator>>=(size_t __pos) {
472     this->_M_do_right_shift(__pos);
473     this->_M_do_sanitize();
474     return *this;
475   }
476 
477   //
478   // Extension:
479   // Versions of single-bit set, reset, flip, test with no range checking.
480   //
481 
482   bitset<_Nb>& _Unchecked_set(size_t __pos) {
483     this->_M_getword(__pos) |= _STLP_PRIV _Base_bitset<_Words > ::_S_maskbit(__pos);
484     return *this;
485   }
486 
487   bitset<_Nb>& _Unchecked_set(size_t __pos, int __val) {
488     if (__val)
489       this->_M_getword(__pos) |= this->_S_maskbit(__pos);
490     else
491       this->_M_getword(__pos) &= ~ this->_S_maskbit(__pos);
492 
493     return *this;
494   }
495 
496   bitset<_Nb>& _Unchecked_reset(size_t __pos) {
497     this->_M_getword(__pos) &= ~ this->_S_maskbit(__pos);
498     return *this;
499   }
500 
501   bitset<_Nb>& _Unchecked_flip(size_t __pos) {
502     this->_M_getword(__pos) ^= this->_S_maskbit(__pos);
503     return *this;
504   }
505 
506   bool _Unchecked_test(size_t __pos) const {
507     return (this->_M_getword(__pos) & this->_S_maskbit(__pos)) != __STATIC_CAST(_WordT,0);
508   }
509 
510   // Set, reset, and flip.
511 
512   bitset<_Nb>& set() {
513     this->_M_do_set();
514     this->_M_do_sanitize();
515     return *this;
516   }
517 
518   bitset<_Nb>& set(size_t __pos) {
519     if (__pos >= _Nb)
520       __stl_throw_out_of_range("bitset");
521     return _Unchecked_set(__pos);
522   }
523 
524   bitset<_Nb>& set(size_t __pos, int __val) {
525     if (__pos >= _Nb)
526       __stl_throw_out_of_range("bitset");
527     return _Unchecked_set(__pos, __val);
528   }
529 
530   bitset<_Nb>& reset() {
531     this->_M_do_reset();
532     return *this;
533   }
534 
535   bitset<_Nb>& reset(size_t __pos) {
536     if (__pos >= _Nb)
537       __stl_throw_out_of_range("bitset");
538 
539     return _Unchecked_reset(__pos);
540   }
541 
542   bitset<_Nb>& flip() {
543     this->_M_do_flip();
544     this->_M_do_sanitize();
545     return *this;
546   }
547 
548   bitset<_Nb>& flip(size_t __pos) {
549     if (__pos >= _Nb)
550       __stl_throw_out_of_range("bitset");
551 
552     return _Unchecked_flip(__pos);
553   }
554 
555   bitset<_Nb> operator~() const {
556     return bitset<_Nb>(*this).flip();
557   }
558 
559   // element access:
560   //for b[i];
561   reference operator[](size_t __pos) { return reference(*this,__pos); }
562   bool operator[](size_t __pos) const { return _Unchecked_test(__pos); }
563 
564   unsigned long to_ulong() const { return this->_M_do_to_ulong(); }
565 
566 #if defined (_STLP_MEMBER_TEMPLATES) && !defined (_STLP_NO_EXPLICIT_FUNCTION_TMPL_ARGS)
567   template <class _CharT, class _Traits, class _Alloc>
568   basic_string<_CharT, _Traits, _Alloc> to_string() const {
569     basic_string<_CharT, _Traits, _Alloc> __result;
570     _M_copy_to_string(__result);
571     return __result;
572   }
573 #else
574   string to_string() const {
575     string __result;
576     _M_copy_to_string(__result);
577     return __result;
578   }
579 #endif /* _STLP_EXPLICIT_FUNCTION_TMPL_ARGS */
580 
581   size_t count() const { return this->_M_do_count(); }
582 
583   size_t size() const { return _Nb; }
584 
585   bool operator==(const bitset<_Nb>& __rhs) const {
586     return this->_M_is_equal(__rhs);
587   }
588   bool operator!=(const bitset<_Nb>& __rhs) const {
589     return !this->_M_is_equal(__rhs);
590   }
591 
592   bool test(size_t __pos) const {
593     if (__pos >= _Nb)
594       __stl_throw_out_of_range("bitset");
595 
596     return _Unchecked_test(__pos);
597   }
598 
599   bool any() const { return this->_M_is_any(); }
600   bool none() const { return !this->_M_is_any(); }
601 
602   bitset<_Nb> operator<<(size_t __pos) const {
603     bitset<_Nb> __result(*this);
604     __result <<= __pos ;  return __result;
605   }
606   bitset<_Nb> operator>>(size_t __pos) const {
607     bitset<_Nb> __result(*this);
608     __result >>= __pos ;  return __result;
609   }
610 
611 #if !defined (_STLP_NO_EXTENSIONS)
612   //
613   // EXTENSIONS: bit-find operations.  These operations are
614   // experimental, and are subject to change or removal in future
615   // versions.
616   //
617 
618   // find the index of the first "on" bit
619   size_t _Find_first() const
620     { return this->_M_do_find_first(_Nb); }
621 
622   // find the index of the next "on" bit after prev
623   size_t _Find_next( size_t __prev ) const
624     { return this->_M_do_find_next(__prev, _Nb); }
625 #endif
626 
627 //
628 // Definitions of should-be non-inline member functions.
629 //
630 #if defined (_STLP_MEMBER_TEMPLATES)
631   template<class _CharT, class _Traits, class _Alloc>
632   void _M_copy_from_string(const basic_string<_CharT,_Traits,_Alloc>& __s,
633                            size_t __pos, size_t __n) {
634 #else
635   void _M_copy_from_string(const string& __s,
636                            size_t __pos, size_t __n) {
637     typedef typename string::traits_type _Traits;
638 #endif
639     reset();
640     size_t __tmp = _Nb;
641     const size_t __Nbits = (min) (__tmp, (min) (__n, __s.size() - __pos));
642     for ( size_t __i= 0; __i < __Nbits; ++__i) {
643       typename _Traits::int_type __k = _Traits::to_int_type(__s[__pos + __Nbits - __i - 1]);
644       // boris : widen() ?
645       if (__k == '1')
646         set(__i);
647       else if (__k != '0')
648         __stl_throw_invalid_argument("bitset");
649     }
650   }
651 
652 #if defined (_STLP_MEMBER_TEMPLATES)
653   template <class _CharT, class _Traits, class _Alloc>
654   void _M_copy_to_string(basic_string<_CharT, _Traits, _Alloc>& __s) const
655 #else
656   void _M_copy_to_string(string& __s) const
657 #endif
658   {
659     __s.assign(_Nb, '0');
660 
661     for (size_t __i = 0; __i < _Nb; ++__i) {
662       if (_Unchecked_test(__i))
663         __s[_Nb - 1 - __i] = '1';
664     }
665   }
666 
667 #if !defined (_STLP_MEMBER_TEMPLATES) && !defined (_STLP_NO_WCHAR_T)
668   void _M_copy_to_string(wstring& __s) const {
669     __s.assign(_Nb, '0');
670 
671     for (size_t __i = 0; __i < _Nb; ++__i) {
672       if (_Unchecked_test(__i))
673         __s[_Nb - 1 - __i] = '1';
674     }
675   }
676 #endif
677 
678 #if defined (_STLP_NON_TYPE_TMPL_PARAM_BUG)
679   bitset<_Nb> operator&(const bitset<_Nb>& __y) const {
680     bitset<_Nb> __result(*this);
681     __result &= __y;
682     return __result;
683   }
684   bitset<_Nb> operator|(const bitset<_Nb>& __y) const {
685     bitset<_Nb> __result(*this);
686     __result |= __y;
687     return __result;
688   }
689   bitset<_Nb> operator^(const bitset<_Nb>& __y) const {
690     bitset<_Nb> __result(*this);
691     __result ^= __y;
692     return __result;
693   }
694 #endif
695 };
696 
697 // ------------------------------------------------------------
698 //
699 // 23.3.5.3 bitset operations:
700 //
701 #if ! defined (_STLP_NON_TYPE_TMPL_PARAM_BUG)
702 template <size_t _Nb>
703 inline bitset<_Nb>  _STLP_CALL
704 operator&(const bitset<_Nb>& __x,
705           const bitset<_Nb>& __y) {
706   bitset<_Nb> __result(__x);
707   __result &= __y;
708   return __result;
709 }
710 
711 
712 template <size_t _Nb>
713 inline bitset<_Nb>  _STLP_CALL
714 operator|(const bitset<_Nb>& __x,
715           const bitset<_Nb>& __y) {
716   bitset<_Nb> __result(__x);
717   __result |= __y;
718   return __result;
719 }
720 
721 template <size_t _Nb>
722 inline bitset<_Nb>  _STLP_CALL
723 operator^(const bitset<_Nb>& __x,
724           const bitset<_Nb>& __y) {
725   bitset<_Nb> __result(__x);
726   __result ^= __y;
727   return __result;
728 }
729 
730 #if !defined (_STLP_USE_NO_IOSTREAMS)
731 
732 _STLP_END_NAMESPACE
733 
734 #  if !(defined (_STLP_MSVC) && (_STLP_MSVC < 1300)) && \
735       !(defined(__SUNPRO_CC) && (__SUNPRO_CC < 0x500))
736 
737 #ifndef _STLP_INTERNAL_IOSFWD
738 #  include <stl/_iosfwd.h>
739 #endif
740 
741 _STLP_BEGIN_NAMESPACE
742 
743 template <class _CharT, class _Traits, size_t _Nb>
744 basic_istream<_CharT, _Traits>&  _STLP_CALL
745 operator>>(basic_istream<_CharT, _Traits>& __is, bitset<_Nb>& __x);
746 
747 template <class _CharT, class _Traits, size_t _Nb>
748 basic_ostream<_CharT, _Traits>& _STLP_CALL
749 operator<<(basic_ostream<_CharT, _Traits>& __os, const bitset<_Nb>& __x);
750 
751 #  else
752 
753 #ifndef _STLP_STRING_IO_H
754 #  include <stl/_string_io.h> //includes _istream.h and _ostream.h
755 #endif
756 
757 _STLP_BEGIN_NAMESPACE
758 
759 template <size_t _Nb>
760 istream&  _STLP_CALL
761 operator>>(istream& __is, bitset<_Nb>& __x) {
762   typedef typename string::traits_type _Traits;
763   string __tmp;
764   __tmp.reserve(_Nb);
765 
766   // Skip whitespace
767   typename istream::sentry __sentry(__is);
768   if (__sentry) {
769     streambuf* __buf = __is.rdbuf();
770     for (size_t __i = 0; __i < _Nb; ++__i) {
771       static typename _Traits::int_type __eof = _Traits::eof();
772 
773       typename _Traits::int_type __c1 = __buf->sbumpc();
774       if (_Traits::eq_int_type(__c1, __eof)) {
775         __is.setstate(ios_base::eofbit);
776         break;
777       }
778       else {
779         typename _Traits::char_type __c2 = _Traits::to_char_type(__c1);
780         char __c  = __is.narrow(__c2, '*');
781 
782         if (__c == '0' || __c == '1')
783           __tmp.push_back(__c);
784         else if (_Traits::eq_int_type(__buf->sputbackc(__c2), __eof)) {
785           __is.setstate(ios_base::failbit);
786           break;
787         }
788       }
789     }
790 
791     if (__tmp.empty())
792       __is.setstate(ios_base::failbit);
793     else
794       __x._M_copy_from_string(__tmp, __STATIC_CAST(size_t,0), _Nb);
795   }
796 
797   return __is;
798 }
799 
800 template <size_t _Nb>
801 ostream& _STLP_CALL
802 operator<<(ostream& __os, const bitset<_Nb>& __x) {
803   string __tmp;
804   __x._M_copy_to_string(__tmp);
805   return __os << __tmp;
806 }
807 
808 #    if !defined (_STLP_NO_WCHAR_T)
809 
810 template <size_t _Nb>
811 wistream&  _STLP_CALL
812 operator>>(wistream& __is, bitset<_Nb>& __x) {
813   typedef typename wstring::traits_type _Traits;
814   wstring __tmp;
815   __tmp.reserve(_Nb);
816 
817   // Skip whitespace
818   typename wistream::sentry __sentry(__is);
819   if (__sentry) {
820     wstreambuf* __buf = __is.rdbuf();
821     for (size_t __i = 0; __i < _Nb; ++__i) {
822       static typename _Traits::int_type __eof = _Traits::eof();
823 
824       typename _Traits::int_type __c1 = __buf->sbumpc();
825       if (_Traits::eq_int_type(__c1, __eof)) {
826         __is.setstate(ios_base::eofbit);
827         break;
828       }
829       else {
830         typename _Traits::char_type __c2 = _Traits::to_char_type(__c1);
831         char __c  = __is.narrow(__c2, '*');
832 
833         if (__c == '0' || __c == '1')
834           __tmp.push_back(__c);
835         else if (_Traits::eq_int_type(__buf->sputbackc(__c2), __eof)) {
836           __is.setstate(ios_base::failbit);
837           break;
838         }
839       }
840     }
841 
842     if (__tmp.empty())
843       __is.setstate(ios_base::failbit);
844     else
845       __x._M_copy_from_string(__tmp, __STATIC_CAST(size_t,0), _Nb);
846   }
847 
848   return __is;
849 }
850 
851 template <size_t _Nb>
852 wostream& _STLP_CALL
853 operator<<(wostream& __os, const bitset<_Nb>& __x) {
854   wstring __tmp;
855   __x._M_copy_to_string(__tmp);
856   return __os << __tmp;
857 }
858 
859 #    endif /* _STLP_NO_WCHAR_T */
860 #  endif
861 #endif
862 
863 #endif /* _STLP_NON_TYPE_TMPL_PARAM_BUG */
864 
865 #undef  bitset
866 
867 _STLP_END_NAMESPACE
868 
869 #undef __BITS_PER_WORD
870 #undef __BITSET_WORDS
871 
872 #if !defined (_STLP_LINK_TIME_INSTANTIATION)
873 #  include <stl/_bitset.c>
874 #endif
875 
876 #endif /* _STLP_BITSET_H */
877 
878 // Local Variables:
879 // mode:C++
880 // End:
881