OSDN Git Service

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