OSDN Git Service

5829726c5b02ed906289b42b248e33e35a8c0940
[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 __position is bigger the size
861        *  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       _GLIBCXX_CONSTEXPR 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       _GLIBCXX_CONSTEXPR 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 */