OSDN Git Service

86f361c9867930ebb05fe7fa93e30947967e506a
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / include / std / bitset
1 // <bitset> -*- C++ -*-
2
3 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
4 // Free Software Foundation, Inc.
5 //
6 // This file is part of the GNU ISO C++ Library.  This library is free
7 // software; you can redistribute it and/or modify it under the
8 // terms of the GNU General Public License as published by the
9 // Free Software Foundation; either version 3, or (at your option)
10 // any later version.
11
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 // GNU General Public License for more details.
16
17 // Under Section 7 of GPL version 3, you are granted additional
18 // permissions described in the GCC Runtime Library Exception, version
19 // 3.1, as published by the Free Software Foundation.
20
21 // You should have received a copy of the GNU General Public License and
22 // a copy of the GCC Runtime Library Exception along with this program;
23 // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
24 // <http://www.gnu.org/licenses/>.
25
26 /*
27  * Copyright (c) 1998
28  * Silicon Graphics Computer Systems, Inc.
29  *
30  * Permission to use, copy, modify, distribute and sell this software
31  * and its documentation for any purpose is hereby granted without fee,
32  * provided that the above copyright notice appear in all copies and
33  * that both that copyright notice and this permission notice appear
34  * in supporting documentation.  Silicon Graphics makes no
35  * representations about the suitability of this software for any
36  * purpose.  It is provided "as is" without express or implied warranty.
37  */
38
39 /** @file include/bitset
40  *  This is a Standard C++ Library header.
41  */
42
43 #ifndef _GLIBCXX_BITSET
44 #define _GLIBCXX_BITSET 1
45
46 #pragma GCC system_header
47
48 #include <string>
49 #include <bits/functexcept.h>   // For invalid_argument, out_of_range,
50                                 // overflow_error
51 #include <iosfwd>
52 #include <cxxabi-forced.h>
53
54 #define _GLIBCXX_BITSET_BITS_PER_WORD  (__CHAR_BIT__ * sizeof(unsigned long))
55 #define _GLIBCXX_BITSET_WORDS(__n) \
56   ((__n) / _GLIBCXX_BITSET_BITS_PER_WORD + \
57    ((__n) % _GLIBCXX_BITSET_BITS_PER_WORD == 0 ? 0 : 1))
58
59 _GLIBCXX_BEGIN_NESTED_NAMESPACE(std, _GLIBCXX_STD_D)
60
61   /**
62    *  Base class, general case.  It is a class invariant that _Nw will be
63    *  nonnegative.
64    *
65    *  See documentation for bitset.
66   */
67   template<size_t _Nw>
68     struct _Base_bitset
69     {
70       typedef unsigned long _WordT;
71
72       /// 0 is the least significant word.
73       _WordT            _M_w[_Nw];
74
75       _GLIBCXX_CONSTEXPR _Base_bitset()
76       : _M_w() { }
77
78 #ifdef __GXX_EXPERIMENTAL_CXX0X__
79       constexpr _Base_bitset(unsigned long long __val)
80       : _M_w({ _WordT(__val)
81 #if __SIZEOF_LONG_LONG__ > __SIZEOF_LONG__
82                , _WordT(__val >> _GLIBCXX_BITSET_BITS_PER_WORD)
83 #endif
84        }) { }
85 #else
86       _Base_bitset(unsigned long __val)
87       : _M_w()
88       { _M_w[0] = __val; }
89 #endif
90
91       static _GLIBCXX_CONSTEXPR size_t
92       _S_whichword(size_t __pos )
93       { return __pos / _GLIBCXX_BITSET_BITS_PER_WORD; }
94
95       static _GLIBCXX_CONSTEXPR size_t
96       _S_whichbyte(size_t __pos )
97       { return (__pos % _GLIBCXX_BITSET_BITS_PER_WORD) / __CHAR_BIT__; }
98
99       static _GLIBCXX_CONSTEXPR size_t
100       _S_whichbit(size_t __pos )
101       { return __pos % _GLIBCXX_BITSET_BITS_PER_WORD; }
102
103       static _GLIBCXX_CONSTEXPR _WordT
104       _S_maskbit(size_t __pos )
105       { return (static_cast<_WordT>(1)) << _S_whichbit(__pos); }
106
107       _WordT&
108       _M_getword(size_t __pos)
109       { return _M_w[_S_whichword(__pos)]; }
110
111       _WordT
112       _M_getword(size_t __pos) const
113       { return _M_w[_S_whichword(__pos)]; }
114
115 #ifdef __GXX_EXPERIMENTAL_CXX0X__
116       const _WordT*
117       _M_getdata() const
118       { return _M_w; }
119 #endif
120
121       _WordT&
122       _M_hiword()
123       { return _M_w[_Nw - 1]; }
124
125       _GLIBCXX_CONSTEXPR _WordT
126       _M_hiword() const
127       { return _M_w[_Nw - 1]; }
128
129       void
130       _M_do_and(const _Base_bitset<_Nw>& __x)
131       {
132         for (size_t __i = 0; __i < _Nw; __i++)
133           _M_w[__i] &= __x._M_w[__i];
134       }
135
136       void
137       _M_do_or(const _Base_bitset<_Nw>& __x)
138       {
139         for (size_t __i = 0; __i < _Nw; __i++)
140           _M_w[__i] |= __x._M_w[__i];
141       }
142
143       void
144       _M_do_xor(const _Base_bitset<_Nw>& __x)
145       {
146         for (size_t __i = 0; __i < _Nw; __i++)
147           _M_w[__i] ^= __x._M_w[__i];
148       }
149
150       void
151       _M_do_left_shift(size_t __shift);
152
153       void
154       _M_do_right_shift(size_t __shift);
155
156       void
157       _M_do_flip()
158       {
159         for (size_t __i = 0; __i < _Nw; __i++)
160           _M_w[__i] = ~_M_w[__i];
161       }
162
163       void
164       _M_do_set()
165       {
166         for (size_t __i = 0; __i < _Nw; __i++)
167           _M_w[__i] = ~static_cast<_WordT>(0);
168       }
169
170       void
171       _M_do_reset()
172       { __builtin_memset(_M_w, 0, _Nw * sizeof(_WordT)); }
173
174       bool
175       _M_is_equal(const _Base_bitset<_Nw>& __x) const
176       {
177         for (size_t __i = 0; __i < _Nw; ++__i)
178           if (_M_w[__i] != __x._M_w[__i])
179             return false;
180         return true;
181       }
182
183       size_t
184       _M_are_all_aux() const
185       {
186         for (size_t __i = 0; __i < _Nw - 1; __i++)
187           if (_M_w[__i] != ~static_cast<_WordT>(0))
188             return 0;
189         return ((_Nw - 1) * _GLIBCXX_BITSET_BITS_PER_WORD
190                 + __builtin_popcountl(_M_hiword()));
191       }
192
193       bool
194       _M_is_any() const
195       {
196         for (size_t __i = 0; __i < _Nw; __i++)
197           if (_M_w[__i] != static_cast<_WordT>(0))
198             return true;
199         return false;
200       }
201
202       size_t
203       _M_do_count() const
204       {
205         size_t __result = 0;
206         for (size_t __i = 0; __i < _Nw; __i++)
207           __result += __builtin_popcountl(_M_w[__i]);
208         return __result;
209       }
210
211       unsigned long
212       _M_do_to_ulong() const;
213
214 #ifdef __GXX_EXPERIMENTAL_CXX0X__
215       unsigned long long
216       _M_do_to_ullong() const;
217 #endif
218
219       // find first "on" bit
220       size_t
221       _M_do_find_first(size_t __not_found) const;
222
223       // find the next "on" bit that follows "prev"
224       size_t
225       _M_do_find_next(size_t __prev, size_t __not_found) const;
226     };
227
228   // Definitions of non-inline functions from _Base_bitset.
229   template<size_t _Nw>
230     void
231     _Base_bitset<_Nw>::_M_do_left_shift(size_t __shift)
232     {
233       if (__builtin_expect(__shift != 0, 1))
234         {
235           const size_t __wshift = __shift / _GLIBCXX_BITSET_BITS_PER_WORD;
236           const size_t __offset = __shift % _GLIBCXX_BITSET_BITS_PER_WORD;
237
238           if (__offset == 0)
239             for (size_t __n = _Nw - 1; __n >= __wshift; --__n)
240               _M_w[__n] = _M_w[__n - __wshift];
241           else
242             {
243               const size_t __sub_offset = (_GLIBCXX_BITSET_BITS_PER_WORD 
244                                            - __offset);
245               for (size_t __n = _Nw - 1; __n > __wshift; --__n)
246                 _M_w[__n] = ((_M_w[__n - __wshift] << __offset)
247                              | (_M_w[__n - __wshift - 1] >> __sub_offset));
248               _M_w[__wshift] = _M_w[0] << __offset;
249             }
250
251           std::fill(_M_w + 0, _M_w + __wshift, static_cast<_WordT>(0));
252         }
253     }
254
255   template<size_t _Nw>
256     void
257     _Base_bitset<_Nw>::_M_do_right_shift(size_t __shift)
258     {
259       if (__builtin_expect(__shift != 0, 1))
260         {
261           const size_t __wshift = __shift / _GLIBCXX_BITSET_BITS_PER_WORD;
262           const size_t __offset = __shift % _GLIBCXX_BITSET_BITS_PER_WORD;
263           const size_t __limit = _Nw - __wshift - 1;
264
265           if (__offset == 0)
266             for (size_t __n = 0; __n <= __limit; ++__n)
267               _M_w[__n] = _M_w[__n + __wshift];
268           else
269             {
270               const size_t __sub_offset = (_GLIBCXX_BITSET_BITS_PER_WORD
271                                            - __offset);
272               for (size_t __n = 0; __n < __limit; ++__n)
273                 _M_w[__n] = ((_M_w[__n + __wshift] >> __offset)
274                              | (_M_w[__n + __wshift + 1] << __sub_offset));
275               _M_w[__limit] = _M_w[_Nw-1] >> __offset;
276             }
277           
278           std::fill(_M_w + __limit + 1, _M_w + _Nw, static_cast<_WordT>(0));
279         }
280     }
281
282   template<size_t _Nw>
283     unsigned long
284     _Base_bitset<_Nw>::_M_do_to_ulong() const
285     {
286       for (size_t __i = 1; __i < _Nw; ++__i)
287         if (_M_w[__i])
288           __throw_overflow_error(__N("_Base_bitset::_M_do_to_ulong"));
289       return _M_w[0];
290     }
291
292 #ifdef __GXX_EXPERIMENTAL_CXX0X__
293   template<size_t _Nw>
294     unsigned long long
295     _Base_bitset<_Nw>::_M_do_to_ullong() const
296     {
297       const bool __dw = sizeof(unsigned long long) > sizeof(unsigned long);
298       for (size_t __i = 1 + __dw; __i < _Nw; ++__i)
299         if (_M_w[__i])
300           __throw_overflow_error(__N("_Base_bitset::_M_do_to_ullong"));
301
302       if (__dw)
303         return _M_w[0] + (static_cast<unsigned long long>(_M_w[1])
304                           << _GLIBCXX_BITSET_BITS_PER_WORD);
305       return _M_w[0];
306     }
307 #endif
308
309   template<size_t _Nw>
310     size_t
311     _Base_bitset<_Nw>::_M_do_find_first(size_t __not_found) const
312     {
313       for (size_t __i = 0; __i < _Nw; __i++)
314         {
315           _WordT __thisword = _M_w[__i];
316           if (__thisword != static_cast<_WordT>(0))
317             return (__i * _GLIBCXX_BITSET_BITS_PER_WORD
318                     + __builtin_ctzl(__thisword));
319         }
320       // not found, so return an indication of failure.
321       return __not_found;
322     }
323
324   template<size_t _Nw>
325     size_t
326     _Base_bitset<_Nw>::_M_do_find_next(size_t __prev, size_t __not_found) const
327     {
328       // make bound inclusive
329       ++__prev;
330
331       // check out of bounds
332       if (__prev >= _Nw * _GLIBCXX_BITSET_BITS_PER_WORD)
333         return __not_found;
334
335       // search first word
336       size_t __i = _S_whichword(__prev);
337       _WordT __thisword = _M_w[__i];
338
339       // mask off bits below bound
340       __thisword &= (~static_cast<_WordT>(0)) << _S_whichbit(__prev);
341
342       if (__thisword != static_cast<_WordT>(0))
343         return (__i * _GLIBCXX_BITSET_BITS_PER_WORD
344                 + __builtin_ctzl(__thisword));
345
346       // check subsequent words
347       __i++;
348       for (; __i < _Nw; __i++)
349         {
350           __thisword = _M_w[__i];
351           if (__thisword != static_cast<_WordT>(0))
352             return (__i * _GLIBCXX_BITSET_BITS_PER_WORD
353                     + __builtin_ctzl(__thisword));
354         }
355       // not found, so return an indication of failure.
356       return __not_found;
357     } // end _M_do_find_next
358
359   /**
360    *  Base class, specialization for a single word.
361    *
362    *  See documentation for bitset.
363   */
364   template<>
365     struct _Base_bitset<1>
366     {
367       typedef unsigned long _WordT;
368       _WordT _M_w;
369
370       _GLIBCXX_CONSTEXPR _Base_bitset()
371       : _M_w(0)
372       { }
373
374 #ifdef __GXX_EXPERIMENTAL_CXX0X__
375       constexpr _Base_bitset(unsigned long long __val)
376 #else
377       _Base_bitset(unsigned long __val)
378 #endif
379       : _M_w(__val)
380       { }
381
382       static _GLIBCXX_CONSTEXPR size_t
383       _S_whichword(size_t __pos )
384       { return __pos / _GLIBCXX_BITSET_BITS_PER_WORD; }
385
386       static _GLIBCXX_CONSTEXPR size_t
387       _S_whichbyte(size_t __pos )
388       { return (__pos % _GLIBCXX_BITSET_BITS_PER_WORD) / __CHAR_BIT__; }
389
390       static _GLIBCXX_CONSTEXPR size_t
391       _S_whichbit(size_t __pos )
392       {  return __pos % _GLIBCXX_BITSET_BITS_PER_WORD; }
393
394       static _GLIBCXX_CONSTEXPR _WordT
395       _S_maskbit(size_t __pos )
396       { return (static_cast<_WordT>(1)) << _S_whichbit(__pos); }
397
398       _WordT&
399       _M_getword(size_t)
400       { return _M_w; }
401
402       _WordT
403       _M_getword(size_t) const
404       { return _M_w; }
405
406 #ifdef __GXX_EXPERIMENTAL_CXX0X__
407       const _WordT*
408       _M_getdata() const
409       { return &_M_w; }
410 #endif
411
412       _WordT&
413       _M_hiword()
414       { return _M_w; }
415
416       _GLIBCXX_CONSTEXPR _WordT
417       _M_hiword() const
418       { return _M_w; }
419
420       void
421       _M_do_and(const _Base_bitset<1>& __x)
422       { _M_w &= __x._M_w; }
423
424       void
425       _M_do_or(const _Base_bitset<1>& __x)
426       { _M_w |= __x._M_w; }
427
428       void
429       _M_do_xor(const _Base_bitset<1>& __x)
430       { _M_w ^= __x._M_w; }
431
432       void
433       _M_do_left_shift(size_t __shift)
434       { _M_w <<= __shift; }
435
436       void
437       _M_do_right_shift(size_t __shift)
438       { _M_w >>= __shift; }
439
440       void
441       _M_do_flip()
442       { _M_w = ~_M_w; }
443
444       void
445       _M_do_set()
446       { _M_w = ~static_cast<_WordT>(0); }
447
448       void
449       _M_do_reset()
450       { _M_w = 0; }
451
452       bool
453       _M_is_equal(const _Base_bitset<1>& __x) const
454       { return _M_w == __x._M_w; }
455
456       size_t
457       _M_are_all_aux() const
458       { return __builtin_popcountl(_M_w); }
459
460       bool
461       _M_is_any() const
462       { return _M_w != 0; }
463
464       size_t
465       _M_do_count() const
466       { return __builtin_popcountl(_M_w); }
467
468       unsigned long
469       _M_do_to_ulong() const
470       { return _M_w; }
471
472 #ifdef __GXX_EXPERIMENTAL_CXX0X__
473       unsigned long long
474       _M_do_to_ullong() const
475       { return _M_w; }
476 #endif
477
478       size_t
479       _M_do_find_first(size_t __not_found) const
480       {
481         if (_M_w != 0)
482           return __builtin_ctzl(_M_w);
483         else
484           return __not_found;
485       }
486
487       // find the next "on" bit that follows "prev"
488       size_t
489       _M_do_find_next(size_t __prev, size_t __not_found) const
490       {
491         ++__prev;
492         if (__prev >= ((size_t) _GLIBCXX_BITSET_BITS_PER_WORD))
493           return __not_found;
494
495         _WordT __x = _M_w >> __prev;
496         if (__x != 0)
497           return __builtin_ctzl(__x) + __prev;
498         else
499           return __not_found;
500       }
501     };
502
503   /**
504    *  Base class, specialization for no storage (zero-length %bitset).
505    *
506    *  See documentation for bitset.
507   */
508   template<>
509     struct _Base_bitset<0>
510     {
511       typedef unsigned long _WordT;
512
513       _GLIBCXX_CONSTEXPR _Base_bitset()
514       { }
515
516 #ifdef __GXX_EXPERIMENTAL_CXX0X__
517       constexpr _Base_bitset(unsigned long long)
518 #else
519       _Base_bitset(unsigned long)
520 #endif
521       { }
522
523       static _GLIBCXX_CONSTEXPR size_t
524       _S_whichword(size_t __pos )
525       { return __pos / _GLIBCXX_BITSET_BITS_PER_WORD; }
526
527       static _GLIBCXX_CONSTEXPR size_t
528       _S_whichbyte(size_t __pos )
529       { return (__pos % _GLIBCXX_BITSET_BITS_PER_WORD) / __CHAR_BIT__; }
530
531       static _GLIBCXX_CONSTEXPR size_t
532       _S_whichbit(size_t __pos )
533       {  return __pos % _GLIBCXX_BITSET_BITS_PER_WORD; }
534
535       static _GLIBCXX_CONSTEXPR _WordT
536       _S_maskbit(size_t __pos )
537       { return (static_cast<_WordT>(1)) << _S_whichbit(__pos); }
538
539       // This would normally give access to the data.  The bounds-checking
540       // in the bitset class will prevent the user from getting this far,
541       // but (1) it must still return an lvalue to compile, and (2) the
542       // user might call _Unchecked_set directly, in which case this /needs/
543       // to fail.  Let's not penalize zero-length users unless they actually
544       // make an unchecked call; all the memory ugliness is therefore
545       // localized to this single should-never-get-this-far function.
546       _WordT&
547       _M_getword(size_t)
548       { 
549         __throw_out_of_range(__N("_Base_bitset::_M_getword")); 
550         return *new _WordT; 
551       }
552
553       _WordT
554       _M_getword(size_t __pos) const
555       { return 0; }
556
557       _GLIBCXX_CONSTEXPR _WordT
558       _M_hiword() const
559       { return 0; }
560
561       void
562       _M_do_and(const _Base_bitset<0>&)
563       { }
564
565       void
566       _M_do_or(const _Base_bitset<0>&)
567       { }
568
569       void
570       _M_do_xor(const _Base_bitset<0>&)
571       { }
572
573       void
574       _M_do_left_shift(size_t)
575       { }
576
577       void
578       _M_do_right_shift(size_t)
579       { }
580
581       void
582       _M_do_flip()
583       { }
584
585       void
586       _M_do_set()
587       { }
588
589       void
590       _M_do_reset()
591       { }
592
593       // Are all empty bitsets equal to each other?  Are they equal to
594       // themselves?  How to compare a thing which has no state?  What is
595       // the sound of one zero-length bitset clapping?
596       bool
597       _M_is_equal(const _Base_bitset<0>&) const
598       { return true; }
599
600       size_t
601       _M_are_all_aux() const
602       { return 0; }
603
604       bool
605       _M_is_any() const
606       { return false; }
607
608       size_t
609       _M_do_count() const
610       { return 0; }
611
612       unsigned long
613       _M_do_to_ulong() const
614       { return 0; }
615
616 #ifdef __GXX_EXPERIMENTAL_CXX0X__
617       unsigned long long
618       _M_do_to_ullong() const
619       { return 0; }
620 #endif
621
622       // Normally "not found" is the size, but that could also be
623       // misinterpreted as an index in this corner case.  Oh well.
624       size_t
625       _M_do_find_first(size_t) const
626       { return 0; }
627
628       size_t
629       _M_do_find_next(size_t, size_t) const
630       { return 0; }
631     };
632
633
634   // Helper class to zero out the unused high-order bits in the highest word.
635   template<size_t _Extrabits>
636     struct _Sanitize
637     {
638       typedef unsigned long _WordT;
639
640       static void 
641       _S_do_sanitize(_WordT& __val)
642       { __val &= ~((~static_cast<_WordT>(0)) << _Extrabits); }
643     };
644
645   template<>
646     struct _Sanitize<0>
647     { 
648       typedef unsigned long _WordT;
649
650       static void 
651       _S_do_sanitize(_WordT) { } 
652     };
653
654   /**
655    *  @brief  The %bitset class represents a @e fixed-size sequence of bits.
656    *
657    *  @ingroup containers
658    *
659    *  (Note that %bitset does @e not meet the formal requirements of a
660    *  <a href="tables.html#65">container</a>.  Mainly, it lacks iterators.)
661    *
662    *  The template argument, @a Nb, may be any non-negative number,
663    *  specifying the number of bits (e.g., "0", "12", "1024*1024").
664    *
665    *  In the general unoptimized case, storage is allocated in word-sized
666    *  blocks.  Let B be the number of bits in a word, then (Nb+(B-1))/B
667    *  words will be used for storage.  B - Nb%B bits are unused.  (They are
668    *  the high-order bits in the highest word.)  It is a class invariant
669    *  that those unused bits are always zero.
670    *
671    *  If you think of %bitset as <em>a simple array of bits</em>, be
672    *  aware that your mental picture is reversed: a %bitset behaves
673    *  the same way as bits in integers do, with the bit at index 0 in
674    *  the <em>least significant / right-hand</em> position, and the bit at
675    *  index Nb-1 in the <em>most significant / left-hand</em> position.
676    *  Thus, unlike other containers, a %bitset's index <em>counts from
677    *  right to left</em>, to put it very loosely.
678    *
679    *  This behavior is preserved when translating to and from strings.  For
680    *  example, the first line of the following program probably prints
681    *  <em>b(&apos;a&apos;) is 0001100001</em> on a modern ASCII system.
682    *
683    *  @code
684    *     #include <bitset>
685    *     #include <iostream>
686    *     #include <sstream>
687    *
688    *     using namespace std;
689    *
690    *     int main()
691    *     {
692    *         long         a = 'a';
693    *         bitset<10>   b(a);
694    *
695    *         cout << "b('a') is " << b << endl;
696    *
697    *         ostringstream s;
698    *         s << b;
699    *         string  str = s.str();
700    *         cout << "index 3 in the string is " << str[3] << " but\n"
701    *              << "index 3 in the bitset is " << b[3] << endl;
702    *     }
703    *  @endcode
704    *
705    *  Also see:
706    *  http://gcc.gnu.org/onlinedocs/libstdc++/manual/bk01pt12ch33s02.html
707    *  for a description of extensions.
708    *
709    *  Most of the actual code isn't contained in %bitset<> itself, but in the
710    *  base class _Base_bitset.  The base class works with whole words, not with
711    *  individual bits.  This allows us to specialize _Base_bitset for the
712    *  important special case where the %bitset is only a single word.
713    *
714    *  Extra confusion can result due to the fact that the storage for
715    *  _Base_bitset @e is a regular array, and is indexed as such.  This is
716    *  carefully encapsulated.
717   */
718   template<size_t _Nb>
719     class bitset
720     : private _Base_bitset<_GLIBCXX_BITSET_WORDS(_Nb)>
721     {
722     private:
723       typedef _Base_bitset<_GLIBCXX_BITSET_WORDS(_Nb)> _Base;
724       typedef unsigned long _WordT;
725
726       void
727       _M_do_sanitize()
728       { 
729         typedef _Sanitize<_Nb % _GLIBCXX_BITSET_BITS_PER_WORD> __sanitize_type;
730         __sanitize_type::_S_do_sanitize(this->_M_hiword());
731       }
732
733 #ifdef __GXX_EXPERIMENTAL_CXX0X__
734       template<typename> friend class hash;
735 #endif
736
737     public:
738       /**
739        *  This encapsulates the concept of a single bit.  An instance of this
740        *  class is a proxy for an actual bit; this way the individual bit
741        *  operations are done as faster word-size bitwise instructions.
742        *
743        *  Most users will never need to use this class directly; conversions
744        *  to and from bool are automatic and should be transparent.  Overloaded
745        *  operators help to preserve the illusion.
746        *
747        *  (On a typical system, this <em>bit %reference</em> is 64
748        *  times the size of an actual bit.  Ha.)
749        */
750       class reference
751       {
752         friend class bitset;
753
754         _WordT* _M_wp;
755         size_t  _M_bpos;
756         
757         // left undefined
758         reference();
759         
760       public:
761         reference(bitset& __b, size_t __pos)
762         {
763           _M_wp = &__b._M_getword(__pos);
764           _M_bpos = _Base::_S_whichbit(__pos);
765         }
766
767         ~reference()
768         { }
769
770         // For b[i] = __x;
771         reference&
772         operator=(bool __x)
773         {
774           if (__x)
775             *_M_wp |= _Base::_S_maskbit(_M_bpos);
776           else
777             *_M_wp &= ~_Base::_S_maskbit(_M_bpos);
778           return *this;
779         }
780
781         // For b[i] = b[__j];
782         reference&
783         operator=(const reference& __j)
784         {
785           if ((*(__j._M_wp) & _Base::_S_maskbit(__j._M_bpos)))
786             *_M_wp |= _Base::_S_maskbit(_M_bpos);
787           else
788             *_M_wp &= ~_Base::_S_maskbit(_M_bpos);
789           return *this;
790         }
791
792         // Flips the bit
793         bool
794         operator~() const
795         { return (*(_M_wp) & _Base::_S_maskbit(_M_bpos)) == 0; }
796
797         // For __x = b[i];
798         operator bool() const
799         { return (*(_M_wp) & _Base::_S_maskbit(_M_bpos)) != 0; }
800
801         // For b[i].flip();
802         reference&
803         flip()
804         {
805           *_M_wp ^= _Base::_S_maskbit(_M_bpos);
806           return *this;
807         }
808       };
809       friend class reference;
810
811       // 23.3.5.1 constructors:
812       /// All bits set to zero.
813       _GLIBCXX_CONSTEXPR bitset()
814       { }
815
816       /// Initial bits bitwise-copied from a single word (others set to zero).
817 #ifdef __GXX_EXPERIMENTAL_CXX0X__
818       constexpr bitset(unsigned long long __val)
819       : _Base(__val) { }
820 #else
821       bitset(unsigned long __val)
822       : _Base(__val)
823       { _M_do_sanitize(); }
824 #endif
825
826       /**
827        *  @brief  Use a subset of a string.
828        *  @param  s  A string of @a 0 and @a 1 characters.
829        *  @param  position  Index of the first character in @a s to use;
830        *                    defaults to zero.
831        *  @throw  std::out_of_range  If @a pos is bigger the size of @a s.
832        *  @throw  std::invalid_argument  If a character appears in the string
833        *                                 which is neither @a 0 nor @a 1.
834        */
835       template<class _CharT, class _Traits, class _Alloc>
836         explicit
837         bitset(const std::basic_string<_CharT, _Traits, _Alloc>& __s,
838                size_t __position = 0)
839         : _Base()
840         {
841           if (__position > __s.size())
842             __throw_out_of_range(__N("bitset::bitset initial position "
843                                      "not valid"));
844           _M_copy_from_string(__s, __position,
845                               std::basic_string<_CharT, _Traits, _Alloc>::npos,
846                               _CharT('0'), _CharT('1'));
847         }
848
849       /**
850        *  @brief  Use a subset of a string.
851        *  @param  s  A string of @a 0 and @a 1 characters.
852        *  @param  position  Index of the first character in @a s to use.
853        *  @param  n    The number of characters to copy.
854        *  @throw  std::out_of_range  If @a pos is bigger the size of @a s.
855        *  @throw  std::invalid_argument  If a character appears in the string
856        *                                 which is neither @a 0 nor @a 1.
857        */
858       template<class _CharT, class _Traits, class _Alloc>
859         bitset(const std::basic_string<_CharT, _Traits, _Alloc>& __s,
860                size_t __position, size_t __n)
861         : _Base()
862         {
863           if (__position > __s.size())
864             __throw_out_of_range(__N("bitset::bitset initial position "
865                                      "not valid"));
866           _M_copy_from_string(__s, __position, __n, _CharT('0'), _CharT('1'));
867         }
868
869       // _GLIBCXX_RESOLVE_LIB_DEFECTS
870       // 396. what are characters zero and one.
871       template<class _CharT, class _Traits, class _Alloc>
872         bitset(const std::basic_string<_CharT, _Traits, _Alloc>& __s,
873                size_t __position, size_t __n,
874                _CharT __zero, _CharT __one = _CharT('1'))
875         : _Base()
876         {
877           if (__position > __s.size())
878             __throw_out_of_range(__N("bitset::bitset initial position "
879                                      "not valid"));
880           _M_copy_from_string(__s, __position, __n, __zero, __one);
881         }
882
883 #ifdef __GXX_EXPERIMENTAL_CXX0X__
884       /**
885        *  @brief  Construct from a character %array.
886        *  @param  str  An %array of characters @a zero and @a one.
887        *  @param  n    The number of characters to use.
888        *  @param  zero The character corresponding to the value 0.
889        *  @param  one  The character corresponding to the value 1.
890        *  @throw  std::invalid_argument  If a character appears in the string
891        *                                 which is neither @a zero nor @a one.
892        */
893       template<typename _CharT>
894         explicit
895         bitset(const _CharT* __str,
896                typename std::basic_string<_CharT>::size_type __n
897                = std::basic_string<_CharT>::npos,
898                _CharT __zero = _CharT('0'), _CharT __one = _CharT('1'))
899         : _Base()
900         {
901           if (!__str)
902             __throw_logic_error(__N("bitset::bitset(const _CharT*, ...)"));
903
904           if (__n == std::basic_string<_CharT>::npos)
905             __n = std::char_traits<_CharT>::length(__str);
906           _M_copy_from_ptr<_CharT, std::char_traits<_CharT>>(__str, __n, 0,
907                                                              __n, __zero,
908                                                              __one);
909         }
910 #endif
911
912       // 23.3.5.2 bitset operations:
913       //@{
914       /**
915        *  @brief  Operations on bitsets.
916        *  @param  rhs  A same-sized bitset.
917        *
918        *  These should be self-explanatory.
919        */
920       bitset<_Nb>&
921       operator&=(const bitset<_Nb>& __rhs)
922       {
923         this->_M_do_and(__rhs);
924         return *this;
925       }
926
927       bitset<_Nb>&
928       operator|=(const bitset<_Nb>& __rhs)
929       {
930         this->_M_do_or(__rhs);
931         return *this;
932       }
933
934       bitset<_Nb>&
935       operator^=(const bitset<_Nb>& __rhs)
936       {
937         this->_M_do_xor(__rhs);
938         return *this;
939       }
940       //@}
941       
942       //@{
943       /**
944        *  @brief  Operations on bitsets.
945        *  @param  position  The number of places to shift.
946        *
947        *  These should be self-explanatory.
948        */
949       bitset<_Nb>&
950       operator<<=(size_t __position)
951       {
952         if (__builtin_expect(__position < _Nb, 1))
953           {
954             this->_M_do_left_shift(__position);
955             this->_M_do_sanitize();
956           }
957         else
958           this->_M_do_reset();
959         return *this;
960       }
961
962       bitset<_Nb>&
963       operator>>=(size_t __position)
964       {
965         if (__builtin_expect(__position < _Nb, 1))
966           {
967             this->_M_do_right_shift(__position);
968             this->_M_do_sanitize();
969           }
970         else
971           this->_M_do_reset();
972         return *this;
973       }
974       //@}
975       
976       //@{
977       /**
978        *  These versions of single-bit set, reset, flip, and test are
979        *  extensions from the SGI version.  They do no range checking.
980        *  @ingroup SGIextensions
981        */
982       bitset<_Nb>&
983       _Unchecked_set(size_t __pos)
984       {
985         this->_M_getword(__pos) |= _Base::_S_maskbit(__pos);
986         return *this;
987       }
988
989       bitset<_Nb>&
990       _Unchecked_set(size_t __pos, int __val)
991       {
992         if (__val)
993           this->_M_getword(__pos) |= _Base::_S_maskbit(__pos);
994         else
995           this->_M_getword(__pos) &= ~_Base::_S_maskbit(__pos);
996         return *this;
997       }
998
999       bitset<_Nb>&
1000       _Unchecked_reset(size_t __pos)
1001       {
1002         this->_M_getword(__pos) &= ~_Base::_S_maskbit(__pos);
1003         return *this;
1004       }
1005
1006       bitset<_Nb>&
1007       _Unchecked_flip(size_t __pos)
1008       {
1009         this->_M_getword(__pos) ^= _Base::_S_maskbit(__pos);
1010         return *this;
1011       }
1012
1013       bool
1014       _Unchecked_test(size_t __pos) const
1015       { return ((this->_M_getword(__pos) & _Base::_S_maskbit(__pos))
1016                 != static_cast<_WordT>(0)); }
1017       //@}
1018       
1019       // Set, reset, and flip.
1020       /**
1021        *  @brief Sets every bit to true.
1022        */
1023       bitset<_Nb>&
1024       set()
1025       {
1026         this->_M_do_set();
1027         this->_M_do_sanitize();
1028         return *this;
1029       }
1030
1031       /**
1032        *  @brief Sets a given bit to a particular value.
1033        *  @param  position  The index of the bit.
1034        *  @param  val  Either true or false, defaults to true.
1035        *  @throw  std::out_of_range  If @a pos is bigger the size of the %set.
1036        */
1037       bitset<_Nb>&
1038       set(size_t __position, bool __val = true)
1039       {
1040         if (__position >= _Nb)
1041           __throw_out_of_range(__N("bitset::set"));
1042         return _Unchecked_set(__position, __val);
1043       }
1044
1045       /**
1046        *  @brief Sets every bit to false.
1047        */
1048       bitset<_Nb>&
1049       reset()
1050       {
1051         this->_M_do_reset();
1052         return *this;
1053       }
1054
1055       /**
1056        *  @brief Sets a given bit to false.
1057        *  @param  position  The index of the bit.
1058        *  @throw  std::out_of_range  If @a pos is bigger the size of the %set.
1059        *
1060        *  Same as writing @c set(pos,false).
1061        */
1062       bitset<_Nb>&
1063       reset(size_t __position)
1064       {
1065         if (__position >= _Nb)
1066           __throw_out_of_range(__N("bitset::reset"));
1067         return _Unchecked_reset(__position);
1068       }
1069       
1070       /**
1071        *  @brief Toggles every bit to its opposite value.
1072        */
1073       bitset<_Nb>&
1074       flip()
1075       {
1076         this->_M_do_flip();
1077         this->_M_do_sanitize();
1078         return *this;
1079       }
1080
1081       /**
1082        *  @brief Toggles a given bit to its opposite value.
1083        *  @param  position  The index of the bit.
1084        *  @throw  std::out_of_range  If @a pos is bigger the size of the %set.
1085        */
1086       bitset<_Nb>&
1087       flip(size_t __position)
1088       {
1089         if (__position >= _Nb)
1090           __throw_out_of_range(__N("bitset::flip"));
1091         return _Unchecked_flip(__position);
1092       }
1093       
1094       /// See the no-argument flip().
1095       bitset<_Nb>
1096       operator~() const
1097       { return bitset<_Nb>(*this).flip(); }
1098
1099       //@{
1100       /**
1101        *  @brief  Array-indexing support.
1102        *  @param  position  Index into the %bitset.
1103        *  @return A bool for a <em>const %bitset</em>.  For non-const
1104        *           bitsets, an instance of the reference proxy class.
1105        *  @note  These operators do no range checking and throw no exceptions,
1106        *         as required by DR 11 to the standard.
1107        *
1108        *  _GLIBCXX_RESOLVE_LIB_DEFECTS Note that this implementation already
1109        *  resolves DR 11 (items 1 and 2), but does not do the range-checking
1110        *  required by that DR's resolution.  -pme
1111        *  The DR has since been changed:  range-checking is a precondition
1112        *  (users' responsibility), and these functions must not throw.  -pme
1113        */
1114       reference
1115       operator[](size_t __position)
1116       { return reference(*this, __position); }
1117
1118       bool
1119       operator[](size_t __position) const
1120       { return _Unchecked_test(__position); }
1121       //@}
1122       
1123       /**
1124        *  @brief Returns a numerical interpretation of the %bitset.
1125        *  @return  The integral equivalent of the bits.
1126        *  @throw  std::overflow_error  If there are too many bits to be
1127        *                               represented in an @c unsigned @c long.
1128        */
1129       unsigned long
1130       to_ulong() const
1131       { return this->_M_do_to_ulong(); }
1132
1133 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1134       unsigned long long
1135       to_ullong() const
1136       { return this->_M_do_to_ullong(); }
1137 #endif
1138
1139       /**
1140        *  @brief Returns a character interpretation of the %bitset.
1141        *  @return  The string equivalent of the bits.
1142        *
1143        *  Note the ordering of the bits:  decreasing character positions
1144        *  correspond to increasing bit positions (see the main class notes for
1145        *  an example).
1146        */
1147       template<class _CharT, class _Traits, class _Alloc>
1148         std::basic_string<_CharT, _Traits, _Alloc>
1149         to_string() const
1150         {
1151           std::basic_string<_CharT, _Traits, _Alloc> __result;
1152           _M_copy_to_string(__result, _CharT('0'), _CharT('1'));
1153           return __result;
1154         }
1155
1156       // _GLIBCXX_RESOLVE_LIB_DEFECTS
1157       // 396. what are characters zero and one.
1158       template<class _CharT, class _Traits, class _Alloc>
1159         std::basic_string<_CharT, _Traits, _Alloc>
1160         to_string(_CharT __zero, _CharT __one = _CharT('1')) const
1161         {
1162           std::basic_string<_CharT, _Traits, _Alloc> __result;
1163           _M_copy_to_string(__result, __zero, __one);
1164           return __result;
1165         }
1166
1167       // _GLIBCXX_RESOLVE_LIB_DEFECTS
1168       // 434. bitset::to_string() hard to use.
1169       template<class _CharT, class _Traits>
1170         std::basic_string<_CharT, _Traits, std::allocator<_CharT> >
1171         to_string() const
1172         { return to_string<_CharT, _Traits, std::allocator<_CharT> >(); }
1173
1174       // _GLIBCXX_RESOLVE_LIB_DEFECTS
1175       // 853. to_string needs updating with zero and one.
1176       template<class _CharT, class _Traits>
1177         std::basic_string<_CharT, _Traits, std::allocator<_CharT> >
1178         to_string(_CharT __zero, _CharT __one = _CharT('1')) const
1179         { return to_string<_CharT, _Traits,
1180                            std::allocator<_CharT> >(__zero, __one); }
1181
1182       template<class _CharT>
1183         std::basic_string<_CharT, std::char_traits<_CharT>,
1184                           std::allocator<_CharT> >
1185         to_string() const
1186         {
1187           return to_string<_CharT, std::char_traits<_CharT>,
1188                            std::allocator<_CharT> >();
1189         }
1190
1191       template<class _CharT>
1192         std::basic_string<_CharT, std::char_traits<_CharT>,
1193                           std::allocator<_CharT> >
1194         to_string(_CharT __zero, _CharT __one = _CharT('1')) const
1195         {
1196           return to_string<_CharT, std::char_traits<_CharT>,
1197                            std::allocator<_CharT> >(__zero, __one);
1198         }
1199
1200       std::basic_string<char, std::char_traits<char>, std::allocator<char> >
1201       to_string() const
1202       {
1203         return to_string<char, std::char_traits<char>,
1204                          std::allocator<char> >();
1205       }
1206
1207       std::basic_string<char, std::char_traits<char>, std::allocator<char> >
1208       to_string(char __zero, char __one = '1') const
1209       {
1210         return to_string<char, std::char_traits<char>,
1211                          std::allocator<char> >(__zero, __one);
1212       }
1213
1214       // Helper functions for string operations.
1215       template<class _CharT, class _Traits>
1216         void
1217         _M_copy_from_ptr(const _CharT*, size_t, size_t, size_t,
1218                          _CharT, _CharT);
1219
1220       template<class _CharT, class _Traits, class _Alloc>
1221         void
1222         _M_copy_from_string(const std::basic_string<_CharT,
1223                             _Traits, _Alloc>& __s, size_t __pos, size_t __n,
1224                             _CharT __zero, _CharT __one)
1225         { _M_copy_from_ptr<_CharT, _Traits>(__s.data(), __s.size(), __pos, __n,
1226                                             __zero, __one); }
1227
1228       template<class _CharT, class _Traits, class _Alloc>
1229         void
1230         _M_copy_to_string(std::basic_string<_CharT, _Traits, _Alloc>&,
1231                           _CharT, _CharT) const;
1232
1233       // NB: Backward compat.
1234       template<class _CharT, class _Traits, class _Alloc>
1235         void
1236         _M_copy_from_string(const std::basic_string<_CharT,
1237                             _Traits, _Alloc>& __s, size_t __pos, size_t __n)
1238         { _M_copy_from_string(__s, __pos, __n, _CharT('0'), _CharT('1')); }
1239
1240       template<class _CharT, class _Traits, class _Alloc>
1241         void
1242         _M_copy_to_string(std::basic_string<_CharT, _Traits,_Alloc>& __s) const
1243         { _M_copy_to_string(__s, _CharT('0'), _CharT('1')); }
1244
1245       /// Returns the number of bits which are set.
1246       size_t
1247       count() const
1248       { return this->_M_do_count(); }
1249
1250       /// Returns the total number of bits.
1251       _GLIBCXX_CONSTEXPR size_t
1252       size() const
1253       { return _Nb; }
1254
1255       //@{
1256       /// These comparisons for equality/inequality are, well, @e bitwise.
1257       bool
1258       operator==(const bitset<_Nb>& __rhs) const
1259       { return this->_M_is_equal(__rhs); }
1260
1261       bool
1262       operator!=(const bitset<_Nb>& __rhs) const
1263       { return !this->_M_is_equal(__rhs); }
1264       //@}
1265       
1266       /**
1267        *  @brief Tests the value of a bit.
1268        *  @param  position  The index of a bit.
1269        *  @return  The value at @a pos.
1270        *  @throw  std::out_of_range  If @a pos is bigger the size of the %set.
1271        */
1272       bool
1273       test(size_t __position) const
1274       {
1275         if (__position >= _Nb)
1276           __throw_out_of_range(__N("bitset::test"));
1277         return _Unchecked_test(__position);
1278       }
1279
1280       // _GLIBCXX_RESOLVE_LIB_DEFECTS
1281       // DR 693. std::bitset::all() missing.
1282       /**
1283        *  @brief Tests whether all the bits are on.
1284        *  @return  True if all the bits are set.
1285        */
1286       bool
1287       all() const
1288       { return this->_M_are_all_aux() == _Nb; }
1289
1290       /**
1291        *  @brief Tests whether any of the bits are on.
1292        *  @return  True if at least one bit is set.
1293        */
1294       bool
1295       any() const
1296       { return this->_M_is_any(); }
1297
1298       /**
1299        *  @brief Tests whether any of the bits are on.
1300        *  @return  True if none of the bits are set.
1301        */
1302       bool
1303       none() const
1304       { return !this->_M_is_any(); }
1305
1306       //@{
1307       /// Self-explanatory.
1308       bitset<_Nb>
1309       operator<<(size_t __position) const
1310       { return bitset<_Nb>(*this) <<= __position; }
1311
1312       bitset<_Nb>
1313       operator>>(size_t __position) const
1314       { return bitset<_Nb>(*this) >>= __position; }
1315       //@}
1316       
1317       /**
1318        *  @brief  Finds the index of the first "on" bit.
1319        *  @return  The index of the first bit set, or size() if not found.
1320        *  @ingroup SGIextensions
1321        *  @sa  _Find_next
1322        */
1323       size_t
1324       _Find_first() const
1325       { return this->_M_do_find_first(_Nb); }
1326
1327       /**
1328        *  @brief  Finds the index of the next "on" bit after prev.
1329        *  @return  The index of the next bit set, or size() if not found.
1330        *  @param  prev  Where to start searching.
1331        *  @ingroup SGIextensions
1332        *  @sa  _Find_first
1333        */
1334       size_t
1335       _Find_next(size_t __prev ) const
1336       { return this->_M_do_find_next(__prev, _Nb); }
1337     };
1338
1339   // Definitions of non-inline member functions.
1340   template<size_t _Nb>
1341     template<class _CharT, class _Traits>
1342       void
1343       bitset<_Nb>::
1344       _M_copy_from_ptr(const _CharT* __s, size_t __len,
1345                        size_t __pos, size_t __n, _CharT __zero, _CharT __one)
1346       {
1347         reset();
1348         const size_t __nbits = std::min(_Nb, std::min(__n, __len - __pos));
1349         for (size_t __i = __nbits; __i > 0; --__i)
1350           {
1351             const _CharT __c = __s[__pos + __nbits - __i];
1352             if (_Traits::eq(__c, __zero))
1353               ;
1354             else if (_Traits::eq(__c, __one))
1355               _Unchecked_set(__i - 1);
1356             else
1357               __throw_invalid_argument(__N("bitset::_M_copy_from_ptr"));
1358           }
1359       }
1360
1361   template<size_t _Nb>
1362     template<class _CharT, class _Traits, class _Alloc>
1363       void
1364       bitset<_Nb>::
1365       _M_copy_to_string(std::basic_string<_CharT, _Traits, _Alloc>& __s,
1366                         _CharT __zero, _CharT __one) const
1367       {
1368         __s.assign(_Nb, __zero);
1369         for (size_t __i = _Nb; __i > 0; --__i)
1370           if (_Unchecked_test(__i - 1))
1371             _Traits::assign(__s[_Nb - __i], __one);
1372       }
1373
1374   // 23.3.5.3 bitset operations:
1375   //@{
1376   /**
1377    *  @brief  Global bitwise operations on bitsets.
1378    *  @param  x  A bitset.
1379    *  @param  y  A bitset of the same size as @a x.
1380    *  @return  A new bitset.
1381    *
1382    *  These should be self-explanatory.
1383   */
1384   template<size_t _Nb>
1385     inline bitset<_Nb>
1386     operator&(const bitset<_Nb>& __x, const bitset<_Nb>& __y)
1387     {
1388       bitset<_Nb> __result(__x);
1389       __result &= __y;
1390       return __result;
1391     }
1392
1393   template<size_t _Nb>
1394     inline bitset<_Nb>
1395     operator|(const bitset<_Nb>& __x, const bitset<_Nb>& __y)
1396     {
1397       bitset<_Nb> __result(__x);
1398       __result |= __y;
1399       return __result;
1400     }
1401
1402   template <size_t _Nb>
1403     inline bitset<_Nb>
1404     operator^(const bitset<_Nb>& __x, const bitset<_Nb>& __y)
1405     {
1406       bitset<_Nb> __result(__x);
1407       __result ^= __y;
1408       return __result;
1409     }
1410   //@}
1411
1412   //@{
1413   /**
1414    *  @brief Global I/O operators for bitsets.
1415    *
1416    *  Direct I/O between streams and bitsets is supported.  Output is
1417    *  straightforward.  Input will skip whitespace, only accept @a 0 and @a 1
1418    *  characters, and will only extract as many digits as the %bitset will
1419    *  hold.
1420   */
1421   template<class _CharT, class _Traits, size_t _Nb>
1422     std::basic_istream<_CharT, _Traits>&
1423     operator>>(std::basic_istream<_CharT, _Traits>& __is, bitset<_Nb>& __x)
1424     {
1425       typedef typename _Traits::char_type          char_type;
1426       typedef std::basic_istream<_CharT, _Traits>  __istream_type;
1427       typedef typename __istream_type::ios_base    __ios_base;
1428
1429       std::basic_string<_CharT, _Traits> __tmp;
1430       __tmp.reserve(_Nb);
1431
1432       // _GLIBCXX_RESOLVE_LIB_DEFECTS
1433       // 303. Bitset input operator underspecified
1434       const char_type __zero = __is.widen('0');
1435       const char_type __one = __is.widen('1');
1436
1437       typename __ios_base::iostate __state = __ios_base::goodbit;
1438       typename __istream_type::sentry __sentry(__is);
1439       if (__sentry)
1440         {
1441           __try
1442             {
1443               for (size_t __i = _Nb; __i > 0; --__i)
1444                 {
1445                   static typename _Traits::int_type __eof = _Traits::eof();
1446                   
1447                   typename _Traits::int_type __c1 = __is.rdbuf()->sbumpc();
1448                   if (_Traits::eq_int_type(__c1, __eof))
1449                     {
1450                       __state |= __ios_base::eofbit;
1451                       break;
1452                     }
1453                   else
1454                     {
1455                       const char_type __c2 = _Traits::to_char_type(__c1);
1456                       if (_Traits::eq(__c2, __zero))
1457                         __tmp.push_back(__zero);
1458                       else if (_Traits::eq(__c2, __one))
1459                         __tmp.push_back(__one);
1460                       else if (_Traits::
1461                                eq_int_type(__is.rdbuf()->sputbackc(__c2),
1462                                            __eof))
1463                         {
1464                           __state |= __ios_base::failbit;
1465                           break;
1466                         }
1467                     }
1468                 }
1469             }
1470           __catch(__cxxabiv1::__forced_unwind&)
1471             {
1472               __is._M_setstate(__ios_base::badbit);             
1473               __throw_exception_again;
1474             }
1475           __catch(...)
1476             { __is._M_setstate(__ios_base::badbit); }
1477         }
1478
1479       if (__tmp.empty() && _Nb)
1480         __state |= __ios_base::failbit;
1481       else
1482         __x._M_copy_from_string(__tmp, static_cast<size_t>(0), _Nb,
1483                                 __zero, __one);
1484       if (__state)
1485         __is.setstate(__state);
1486       return __is;
1487     }
1488
1489   template <class _CharT, class _Traits, size_t _Nb>
1490     std::basic_ostream<_CharT, _Traits>&
1491     operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1492                const bitset<_Nb>& __x)
1493     {
1494       std::basic_string<_CharT, _Traits> __tmp;
1495
1496       // _GLIBCXX_RESOLVE_LIB_DEFECTS
1497       // 396. what are characters zero and one.
1498       const ctype<_CharT>& __ct = use_facet<ctype<_CharT> >(__os.getloc());
1499       __x._M_copy_to_string(__tmp, __ct.widen('0'), __ct.widen('1'));
1500       return __os << __tmp;
1501     }
1502   //@}
1503
1504 _GLIBCXX_END_NESTED_NAMESPACE
1505
1506 #undef _GLIBCXX_BITSET_WORDS
1507 #undef _GLIBCXX_BITSET_BITS_PER_WORD
1508
1509 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1510
1511 #include <bits/functional_hash.h>
1512
1513 _GLIBCXX_BEGIN_NAMESPACE(std)
1514
1515   // DR 1182.
1516   /// std::hash specialization for bitset.
1517   template<size_t _Nb>
1518     struct hash<_GLIBCXX_STD_D::bitset<_Nb>>
1519     : public __hash_base<size_t, _GLIBCXX_STD_D::bitset<_Nb>>
1520     {
1521       size_t
1522       operator()(const _GLIBCXX_STD_D::bitset<_Nb>& __b) const
1523       {
1524         const size_t __clength = (_Nb + __CHAR_BIT__ - 1) / __CHAR_BIT__;
1525         return std::_Hash_impl::hash(__b._M_getdata(), __clength);
1526       }
1527     };
1528
1529   template<>
1530     struct hash<_GLIBCXX_STD_D::bitset<0>>
1531     : public __hash_base<size_t, _GLIBCXX_STD_D::bitset<0>>
1532     {
1533       size_t
1534       operator()(const _GLIBCXX_STD_D::bitset<0>&) const
1535       { return 0; }
1536     };
1537
1538 _GLIBCXX_END_NAMESPACE
1539
1540 #endif // __GXX_EXPERIMENTAL_CXX0X__
1541
1542 #ifdef _GLIBCXX_DEBUG
1543 # include <debug/bitset>
1544 #endif
1545
1546 #ifdef _GLIBCXX_PROFILE
1547 # include <profile/bitset>
1548 #endif
1549
1550 #endif /* _GLIBCXX_BITSET */