OSDN Git Service

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