OSDN Git Service

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