]> CyberLeo.Net >> Repos - FreeBSD/stable/9.git/blob - contrib/libc++/include/bitset
Merged libcxxrt and libc++. Now available for testing on 9-stable with
[FreeBSD/stable/9.git] / contrib / libc++ / include / bitset
1 // -*- C++ -*-
2 //===---------------------------- bitset ----------------------------------===//
3 //
4 //                     The LLVM Compiler Infrastructure
5 //
6 // This file is dual licensed under the MIT and the University of Illinois Open
7 // Source Licenses. See LICENSE.TXT for details.
8 //
9 //===----------------------------------------------------------------------===//
10
11 #ifndef _LIBCPP_BITSET
12 #define _LIBCPP_BITSET
13
14 /*
15     bitset synopsis
16
17 namespace std
18 {
19
20 namespace std {
21
22 template <size_t N>
23 class bitset
24 {
25 public:
26     // bit reference:
27     class reference
28     {
29         friend class bitset;
30         reference() noexcept;
31     public:
32         ~reference() noexcept;
33         reference& operator=(bool x) noexcept;           // for b[i] = x;
34         reference& operator=(const reference&) noexcept; // for b[i] = b[j];
35         bool operator~() const noexcept;                 // flips the bit
36         operator bool() const noexcept;                  // for x = b[i];
37         reference& flip() noexcept;                      // for b[i].flip();
38     };
39
40     // 23.3.5.1 constructors:
41     constexpr bitset() noexcept;
42     constexpr bitset(unsigned long long val) noexcept;
43     template <class charT>
44         explicit bitset(const charT* str,
45                         typename basic_string<charT>::size_type n = basic_string<charT>::npos,
46                         charT zero = charT('0'), charT one = charT('1'));
47     template<class charT, class traits, class Allocator>
48         explicit bitset(const basic_string<charT,traits,Allocator>& str,
49                         typename basic_string<charT,traits,Allocator>::size_type pos = 0,
50                         typename basic_string<charT,traits,Allocator>::size_type n =
51                                  basic_string<charT,traits,Allocator>::npos,
52                         charT zero = charT('0'), charT one = charT('1'));
53
54     // 23.3.5.2 bitset operations:
55     bitset& operator&=(const bitset& rhs) noexcept;
56     bitset& operator|=(const bitset& rhs) noexcept;
57     bitset& operator^=(const bitset& rhs) noexcept;
58     bitset& operator<<=(size_t pos) noexcept;
59     bitset& operator>>=(size_t pos) noexcept;
60     bitset& set() noexcept;
61     bitset& set(size_t pos, bool val = true);
62     bitset& reset() noexcept;
63     bitset& reset(size_t pos);
64     bitset operator~() const noexcept;
65     bitset& flip() noexcept;
66     bitset& flip(size_t pos);
67
68     // element access:
69     constexpr bool operator[](size_t pos) const; // for b[i];
70     reference operator[](size_t pos);            // for b[i];
71     unsigned long to_ulong() const;
72     unsigned long long to_ullong() const;
73     template <class charT, class traits, class Allocator>
74         basic_string<charT, traits, Allocator> to_string(charT zero = charT('0'), charT one = charT('1')) const;
75     template <class charT, class traits>
76         basic_string<charT, traits, allocator<charT> > to_string(charT zero = charT('0'), charT one = charT('1')) const;
77     template <class charT>
78         basic_string<charT, char_traits<charT>, allocator<charT> > to_string(charT zero = charT('0'), charT one = charT('1')) const;
79     basic_string<char, char_traits<char>, allocator<char> > to_string(char zero = '0', char one = '1') const;
80     size_t count() const noexcept;
81     constexpr size_t size() const noexcept;
82     bool operator==(const bitset& rhs) const noexcept;
83     bool operator!=(const bitset& rhs) const noexcept;
84     bool test(size_t pos) const;
85     bool all() const noexcept;
86     bool any() const noexcept;
87     bool none() const noexcept;
88     bitset operator<<(size_t pos) const noexcept;
89     bitset operator>>(size_t pos) const noexcept;
90 };
91
92 // 23.3.5.3 bitset operators:
93 template <size_t N>
94 bitset<N> operator&(const bitset<N>&, const bitset<N>&) noexcept;
95
96 template <size_t N>
97 bitset<N> operator|(const bitset<N>&, const bitset<N>&) noexcept;
98
99 template <size_t N>
100 bitset<N> operator^(const bitset<N>&, const bitset<N>&) noexcept;
101
102 template <class charT, class traits, size_t N>
103 basic_istream<charT, traits>&
104 operator>>(basic_istream<charT, traits>& is, bitset<N>& x);
105
106 template <class charT, class traits, size_t N>
107 basic_ostream<charT, traits>&
108 operator<<(basic_ostream<charT, traits>& os, const bitset<N>& x);
109
110 template <size_t N> struct hash<std::bitset<N>>;
111
112 }  // std
113
114 */
115
116 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
117 #pragma GCC system_header
118 #endif
119
120 #include <__config>
121 #include <__bit_reference>
122 #include <cstddef>
123 #include <climits>
124 #include <string>
125 #include <stdexcept>
126 #include <iosfwd>
127 #include <__functional_base>
128 #if defined(_LIBCPP_NO_EXCEPTIONS)
129     #include <cassert>
130 #endif
131
132 #include <__undef_min_max>
133
134 _LIBCPP_BEGIN_NAMESPACE_STD
135
136 template <size_t _N_words, size_t _Size>
137 class __bitset;
138
139 template <size_t _N_words, size_t _Size>
140 struct __has_storage_type<__bitset<_N_words, _Size> >
141 {
142     static const bool value = true;
143 };
144
145 template <size_t _N_words, size_t _Size>
146 class __bitset
147 {
148 public:
149     typedef ptrdiff_t              difference_type;
150     typedef size_t                 size_type;
151 protected:
152     typedef __bitset __self;
153     typedef size_type              __storage_type;
154     typedef       __storage_type*  __storage_pointer;
155     typedef const __storage_type*  __const_storage_pointer;
156     static const unsigned __bits_per_word = static_cast<unsigned>(sizeof(__storage_type) * CHAR_BIT);
157
158     friend class __bit_reference<__bitset>;
159     friend class __bit_const_reference<__bitset>;
160     friend class __bit_iterator<__bitset, false>;
161     friend class __bit_iterator<__bitset, true>;
162     friend class __bit_array<__bitset>;
163
164     __storage_type __first_[_N_words];
165
166     typedef __bit_reference<__bitset>                  reference;
167     typedef __bit_const_reference<__bitset>            const_reference;
168     typedef __bit_iterator<__bitset, false>            iterator;
169     typedef __bit_iterator<__bitset, true>             const_iterator;
170
171     __bitset() _NOEXCEPT;
172     explicit __bitset(unsigned long long __v) _NOEXCEPT;
173
174     _LIBCPP_INLINE_VISIBILITY reference __make_ref(size_t __pos) _NOEXCEPT
175         {return reference(__first_ + __pos / __bits_per_word, __storage_type(1) << __pos % __bits_per_word);}
176     _LIBCPP_INLINE_VISIBILITY const_reference __make_ref(size_t __pos) const _NOEXCEPT
177         {return const_reference(__first_ + __pos / __bits_per_word, __storage_type(1) << __pos % __bits_per_word);}
178     _LIBCPP_INLINE_VISIBILITY iterator __make_iter(size_t __pos) _NOEXCEPT
179         {return iterator(__first_ + __pos / __bits_per_word, __pos % __bits_per_word);}
180     _LIBCPP_INLINE_VISIBILITY const_iterator __make_iter(size_t __pos) const _NOEXCEPT
181         {return const_iterator(__first_ + __pos / __bits_per_word, __pos % __bits_per_word);}
182
183     void operator&=(const __bitset& __v) _NOEXCEPT;
184     void operator|=(const __bitset& __v) _NOEXCEPT;
185     void operator^=(const __bitset& __v) _NOEXCEPT;
186
187     void flip() _NOEXCEPT;
188     _LIBCPP_INLINE_VISIBILITY unsigned long to_ulong() const
189         {return to_ulong(integral_constant<bool, _Size < sizeof(unsigned long) * CHAR_BIT>());}
190     _LIBCPP_INLINE_VISIBILITY unsigned long long to_ullong() const
191         {return to_ullong(integral_constant<bool, _Size < sizeof(unsigned long long) * CHAR_BIT>());}
192
193     bool all() const _NOEXCEPT;
194     bool any() const _NOEXCEPT;
195     size_t __hash_code() const _NOEXCEPT;
196 private:
197     void __init(unsigned long long __v, false_type) _NOEXCEPT;
198     void __init(unsigned long long __v, true_type) _NOEXCEPT;
199     unsigned long to_ulong(false_type) const;
200     unsigned long to_ulong(true_type) const;
201     unsigned long long to_ullong(false_type) const;
202     unsigned long long to_ullong(true_type) const;
203     unsigned long long to_ullong(true_type, false_type) const;
204     unsigned long long to_ullong(true_type, true_type) const;
205 };
206
207 template <size_t _N_words, size_t _Size>
208 inline _LIBCPP_INLINE_VISIBILITY
209 __bitset<_N_words, _Size>::__bitset() _NOEXCEPT
210 {
211     _VSTD::fill_n(__first_, _N_words, __storage_type(0));
212 }
213
214 template <size_t _N_words, size_t _Size>
215 void
216 __bitset<_N_words, _Size>::__init(unsigned long long __v, false_type) _NOEXCEPT
217 {
218     __storage_type __t[sizeof(unsigned long long) / sizeof(__storage_type)];
219     for (size_t __i = 0; __i < sizeof(__t)/sizeof(__t[0]); ++__i, __v >>= __bits_per_word)
220         __t[__i] = static_cast<__storage_type>(__v);
221     _VSTD::copy(__t, __t + sizeof(__t)/sizeof(__t[0]), __first_);
222     _VSTD::fill(__first_ + sizeof(__t)/sizeof(__t[0]), __first_ + sizeof(__first_)/sizeof(__first_[0]),
223                __storage_type(0));
224 }
225
226 template <size_t _N_words, size_t _Size>
227 inline _LIBCPP_INLINE_VISIBILITY
228 void
229 __bitset<_N_words, _Size>::__init(unsigned long long __v, true_type) _NOEXCEPT
230 {
231     __first_[0] = __v;
232     _VSTD::fill(__first_ + 1, __first_ + sizeof(__first_)/sizeof(__first_[0]), __storage_type(0));
233 }
234
235 template <size_t _N_words, size_t _Size>
236 inline _LIBCPP_INLINE_VISIBILITY
237 __bitset<_N_words, _Size>::__bitset(unsigned long long __v) _NOEXCEPT
238 {
239     __init(__v, integral_constant<bool, sizeof(unsigned long long) == sizeof(__storage_type)>());
240 }
241
242 template <size_t _N_words, size_t _Size>
243 inline _LIBCPP_INLINE_VISIBILITY
244 void
245 __bitset<_N_words, _Size>::operator&=(const __bitset& __v) _NOEXCEPT
246 {
247     for (size_type __i = 0; __i < _N_words; ++__i)
248         __first_[__i] &= __v.__first_[__i];
249 }
250
251 template <size_t _N_words, size_t _Size>
252 inline _LIBCPP_INLINE_VISIBILITY
253 void
254 __bitset<_N_words, _Size>::operator|=(const __bitset& __v) _NOEXCEPT
255 {
256     for (size_type __i = 0; __i < _N_words; ++__i)
257         __first_[__i] |= __v.__first_[__i];
258 }
259
260 template <size_t _N_words, size_t _Size>
261 inline _LIBCPP_INLINE_VISIBILITY
262 void
263 __bitset<_N_words, _Size>::operator^=(const __bitset& __v) _NOEXCEPT
264 {
265     for (size_type __i = 0; __i < _N_words; ++__i)
266         __first_[__i] ^= __v.__first_[__i];
267 }
268
269 template <size_t _N_words, size_t _Size>
270 void
271 __bitset<_N_words, _Size>::flip() _NOEXCEPT
272 {
273     // do middle whole words
274     size_type __n = _Size;
275     __storage_pointer __p = __first_;
276     for (; __n >= __bits_per_word; ++__p, __n -= __bits_per_word)
277         *__p = ~*__p;
278     // do last partial word
279     if (__n > 0)
280     {
281         __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
282         __storage_type __b = *__p & __m;
283         *__p &= ~__m;
284         *__p |= ~__b & __m;
285     }
286 }
287
288 template <size_t _N_words, size_t _Size>
289 unsigned long
290 __bitset<_N_words, _Size>::to_ulong(false_type) const
291 {
292     const_iterator __e = __make_iter(_Size);
293     const_iterator __i = _VSTD::find(__make_iter(sizeof(unsigned long) * CHAR_BIT), __e, true);
294     if (__i != __e)
295 #ifndef _LIBCPP_NO_EXCEPTIONS
296         throw overflow_error("bitset to_ulong overflow error");
297 #else
298         assert(!"bitset to_ulong overflow error");
299 #endif
300     return __first_[0];
301 }
302
303 template <size_t _N_words, size_t _Size>
304 inline _LIBCPP_INLINE_VISIBILITY
305 unsigned long
306 __bitset<_N_words, _Size>::to_ulong(true_type) const
307 {
308     return __first_[0];
309 }
310
311 template <size_t _N_words, size_t _Size>
312 unsigned long long
313 __bitset<_N_words, _Size>::to_ullong(false_type) const
314 {
315     const_iterator __e = __make_iter(_Size);
316     const_iterator __i = _VSTD::find(__make_iter(sizeof(unsigned long long) * CHAR_BIT), __e, true);
317     if (__i != __e)
318 #ifndef _LIBCPP_NO_EXCEPTIONS
319         throw overflow_error("bitset to_ullong overflow error");
320 #else
321         assert(!"bitset to_ullong overflow error");
322 #endif
323     return to_ullong(true_type());
324 }
325
326 template <size_t _N_words, size_t _Size>
327 inline _LIBCPP_INLINE_VISIBILITY
328 unsigned long long
329 __bitset<_N_words, _Size>::to_ullong(true_type) const
330 {
331     return to_ullong(true_type(), integral_constant<bool, sizeof(__storage_type) < sizeof(unsigned long long)>());
332 }
333
334 template <size_t _N_words, size_t _Size>
335 inline _LIBCPP_INLINE_VISIBILITY
336 unsigned long long
337 __bitset<_N_words, _Size>::to_ullong(true_type, false_type) const
338 {
339     return __first_[0];
340 }
341
342 template <size_t _N_words, size_t _Size>
343 unsigned long long
344 __bitset<_N_words, _Size>::to_ullong(true_type, true_type) const
345 {
346     unsigned long long __r = __first_[0];
347     for (std::size_t __i = 1; __i < sizeof(unsigned long long) / sizeof(__storage_type); ++__i)
348         __r |= static_cast<unsigned long long>(__first_[__i]) << (sizeof(__storage_type) * CHAR_BIT);
349     return __r;
350 }
351
352 template <size_t _N_words, size_t _Size>
353 bool
354 __bitset<_N_words, _Size>::all() const _NOEXCEPT
355 {
356     // do middle whole words
357     size_type __n = _Size;
358     __const_storage_pointer __p = __first_;
359     for (; __n >= __bits_per_word; ++__p, __n -= __bits_per_word)
360         if (~*__p)
361             return false;
362     // do last partial word
363     if (__n > 0)
364     {
365         __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
366         if (~*__p & __m)
367             return false;
368     }
369     return true;
370 }
371
372 template <size_t _N_words, size_t _Size>
373 bool
374 __bitset<_N_words, _Size>::any() const _NOEXCEPT
375 {
376     // do middle whole words
377     size_type __n = _Size;
378     __const_storage_pointer __p = __first_;
379     for (; __n >= __bits_per_word; ++__p, __n -= __bits_per_word)
380         if (*__p)
381             return true;
382     // do last partial word
383     if (__n > 0)
384     {
385         __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
386         if (*__p & __m)
387             return true;
388     }
389     return false;
390 }
391
392 template <size_t _N_words, size_t _Size>
393 inline _LIBCPP_INLINE_VISIBILITY
394 size_t
395 __bitset<_N_words, _Size>::__hash_code() const _NOEXCEPT
396 {
397     size_t __h = 0;
398     for (size_type __i = 0; __i < _N_words; ++__i)
399         __h ^= __first_[__i];
400     return __h;
401 }
402
403 template <size_t _Size>
404 class __bitset<1, _Size>
405 {
406 public:
407     typedef ptrdiff_t              difference_type;
408     typedef size_t                 size_type;
409 protected:
410     typedef __bitset __self;
411     typedef size_type              __storage_type;
412     typedef       __storage_type*  __storage_pointer;
413     typedef const __storage_type*  __const_storage_pointer;
414     static const unsigned __bits_per_word = static_cast<unsigned>(sizeof(__storage_type) * CHAR_BIT);
415
416     friend class __bit_reference<__bitset>;
417     friend class __bit_const_reference<__bitset>;
418     friend class __bit_iterator<__bitset, false>;
419     friend class __bit_iterator<__bitset, true>;
420     friend class __bit_array<__bitset>;
421
422     __storage_type __first_;
423
424     typedef __bit_reference<__bitset>                  reference;
425     typedef __bit_const_reference<__bitset>            const_reference;
426     typedef __bit_iterator<__bitset, false>            iterator;
427     typedef __bit_iterator<__bitset, true>             const_iterator;
428
429     __bitset() _NOEXCEPT;
430     explicit __bitset(unsigned long long __v) _NOEXCEPT;
431
432     _LIBCPP_INLINE_VISIBILITY reference __make_ref(size_t __pos) _NOEXCEPT
433         {return reference(&__first_, __storage_type(1) << __pos);}
434     _LIBCPP_INLINE_VISIBILITY const_reference __make_ref(size_t __pos) const _NOEXCEPT
435         {return const_reference(&__first_, __storage_type(1) << __pos);}
436     _LIBCPP_INLINE_VISIBILITY iterator __make_iter(size_t __pos) _NOEXCEPT
437         {return iterator(&__first_ + __pos / __bits_per_word, __pos % __bits_per_word);}
438     _LIBCPP_INLINE_VISIBILITY const_iterator __make_iter(size_t __pos) const _NOEXCEPT
439         {return const_iterator(&__first_ + __pos / __bits_per_word, __pos % __bits_per_word);}
440
441     void operator&=(const __bitset& __v) _NOEXCEPT;
442     void operator|=(const __bitset& __v) _NOEXCEPT;
443     void operator^=(const __bitset& __v) _NOEXCEPT;
444
445     void flip() _NOEXCEPT;
446
447     unsigned long to_ulong() const;
448     unsigned long long to_ullong() const;
449
450     bool all() const _NOEXCEPT;
451     bool any() const _NOEXCEPT;
452
453     size_t __hash_code() const _NOEXCEPT;
454 };
455
456 template <size_t _Size>
457 inline _LIBCPP_INLINE_VISIBILITY
458 __bitset<1, _Size>::__bitset() _NOEXCEPT
459     : __first_(0)
460 {
461 }
462
463 template <size_t _Size>
464 inline _LIBCPP_INLINE_VISIBILITY
465 __bitset<1, _Size>::__bitset(unsigned long long __v) _NOEXCEPT
466     : __first_(static_cast<__storage_type>(__v))
467 {
468 }
469
470 template <size_t _Size>
471 inline _LIBCPP_INLINE_VISIBILITY
472 void
473 __bitset<1, _Size>::operator&=(const __bitset& __v) _NOEXCEPT
474 {
475     __first_ &= __v.__first_;
476 }
477
478 template <size_t _Size>
479 inline _LIBCPP_INLINE_VISIBILITY
480 void
481 __bitset<1, _Size>::operator|=(const __bitset& __v) _NOEXCEPT
482 {
483     __first_ |= __v.__first_;
484 }
485
486 template <size_t _Size>
487 inline _LIBCPP_INLINE_VISIBILITY
488 void
489 __bitset<1, _Size>::operator^=(const __bitset& __v) _NOEXCEPT
490 {
491     __first_ ^= __v.__first_;
492 }
493
494 template <size_t _Size>
495 inline _LIBCPP_INLINE_VISIBILITY
496 void
497 __bitset<1, _Size>::flip() _NOEXCEPT
498 {
499     __storage_type __m = ~__storage_type(0) >> (__bits_per_word - _Size);
500     __first_ = ~__first_;
501     __first_ &= __m;
502 }
503
504 template <size_t _Size>
505 inline _LIBCPP_INLINE_VISIBILITY
506 unsigned long
507 __bitset<1, _Size>::to_ulong() const
508 {
509     return __first_;
510 }
511
512 template <size_t _Size>
513 inline _LIBCPP_INLINE_VISIBILITY
514 unsigned long long
515 __bitset<1, _Size>::to_ullong() const
516 {
517     return __first_;
518 }
519
520 template <size_t _Size>
521 inline _LIBCPP_INLINE_VISIBILITY
522 bool
523 __bitset<1, _Size>::all() const _NOEXCEPT
524 {
525     __storage_type __m = ~__storage_type(0) >> (__bits_per_word - _Size);
526     return !(~__first_ & __m);
527 }
528
529 template <size_t _Size>
530 inline _LIBCPP_INLINE_VISIBILITY
531 bool
532 __bitset<1, _Size>::any() const _NOEXCEPT
533 {
534     __storage_type __m = ~__storage_type(0) >> (__bits_per_word - _Size);
535     return __first_ & __m;
536 }
537
538 template <size_t _Size>
539 inline _LIBCPP_INLINE_VISIBILITY
540 size_t
541 __bitset<1, _Size>::__hash_code() const _NOEXCEPT
542 {
543     return __first_;
544 }
545
546 template <>
547 class __bitset<0, 0>
548 {
549 public:
550     typedef ptrdiff_t              difference_type;
551     typedef size_t                 size_type;
552 protected:
553     typedef __bitset __self;
554     typedef size_type              __storage_type;
555     typedef       __storage_type*  __storage_pointer;
556     typedef const __storage_type*  __const_storage_pointer;
557     static const unsigned __bits_per_word = static_cast<unsigned>(sizeof(__storage_type) * CHAR_BIT);
558
559     friend class __bit_reference<__bitset>;
560     friend class __bit_const_reference<__bitset>;
561     friend class __bit_iterator<__bitset, false>;
562     friend class __bit_iterator<__bitset, true>;
563     friend struct __bit_array<__bitset>;
564
565     typedef __bit_reference<__bitset>                  reference;
566     typedef __bit_const_reference<__bitset>            const_reference;
567     typedef __bit_iterator<__bitset, false>            iterator;
568     typedef __bit_iterator<__bitset, true>             const_iterator;
569
570     __bitset() _NOEXCEPT;
571     explicit __bitset(unsigned long long) _NOEXCEPT;
572
573     _LIBCPP_INLINE_VISIBILITY reference __make_ref(size_t) _NOEXCEPT
574         {return reference(0, 1);}
575     _LIBCPP_INLINE_VISIBILITY const_reference __make_ref(size_t) const _NOEXCEPT
576         {return const_reference(0, 1);}
577     _LIBCPP_INLINE_VISIBILITY iterator __make_iter(size_t) _NOEXCEPT
578         {return iterator(0, 0);}
579     _LIBCPP_INLINE_VISIBILITY const_iterator __make_iter(size_t) const _NOEXCEPT
580         {return const_iterator(0, 0);}
581
582     _LIBCPP_INLINE_VISIBILITY void operator&=(const __bitset&) _NOEXCEPT {}
583     _LIBCPP_INLINE_VISIBILITY void operator|=(const __bitset&) _NOEXCEPT {}
584     _LIBCPP_INLINE_VISIBILITY void operator^=(const __bitset&) _NOEXCEPT {}
585
586     _LIBCPP_INLINE_VISIBILITY void flip() _NOEXCEPT {}
587
588     _LIBCPP_INLINE_VISIBILITY unsigned long to_ulong() const {return 0;}
589     _LIBCPP_INLINE_VISIBILITY unsigned long long to_ullong() const {return 0;}
590
591     _LIBCPP_INLINE_VISIBILITY bool all() const _NOEXCEPT {return true;}
592     _LIBCPP_INLINE_VISIBILITY bool any() const _NOEXCEPT {return false;}
593
594     _LIBCPP_INLINE_VISIBILITY size_t __hash_code() const _NOEXCEPT {return 0;}
595 };
596
597 inline _LIBCPP_INLINE_VISIBILITY
598 __bitset<0, 0>::__bitset() _NOEXCEPT
599 {
600 }
601
602 inline _LIBCPP_INLINE_VISIBILITY
603 __bitset<0, 0>::__bitset(unsigned long long) _NOEXCEPT
604 {
605 }
606
607 template <size_t _Size> class bitset;
608 template <size_t _Size> struct hash<bitset<_Size> >;
609
610 template <size_t _Size>
611 class _LIBCPP_VISIBLE bitset
612     : private __bitset<_Size == 0 ? 0 : (_Size - 1) / (sizeof(size_t) * CHAR_BIT) + 1, _Size>
613 {
614     static const unsigned __n_words = _Size == 0 ? 0 : (_Size - 1) / (sizeof(size_t) * CHAR_BIT) + 1;
615     typedef __bitset<__n_words, _Size> base;
616
617 public:
618     typedef typename base::reference       reference;
619     typedef typename base::const_reference const_reference;
620
621     // 23.3.5.1 constructors:
622     /*constexpr*/ _LIBCPP_INLINE_VISIBILITY bitset() _NOEXCEPT {}
623     /*constexpr*/ _LIBCPP_INLINE_VISIBILITY bitset(unsigned long long __v) _NOEXCEPT : base(__v) {}
624     template<class _CharT>
625         explicit bitset(const _CharT* __str,
626                         typename basic_string<_CharT>::size_type __n = basic_string<_CharT>::npos,
627                         _CharT __zero = _CharT('0'), _CharT __one = _CharT('1'));
628     template<class _CharT, class _Traits, class _Allocator>
629         explicit bitset(const basic_string<_CharT,_Traits,_Allocator>& __str,
630                         typename basic_string<_CharT,_Traits,_Allocator>::size_type __pos = 0,
631                         typename basic_string<_CharT,_Traits,_Allocator>::size_type __n =
632                                 (basic_string<_CharT,_Traits,_Allocator>::npos),
633                         _CharT __zero = _CharT('0'), _CharT __one = _CharT('1'));
634
635     // 23.3.5.2 bitset operations:
636     bitset& operator&=(const bitset& __rhs) _NOEXCEPT;
637     bitset& operator|=(const bitset& __rhs) _NOEXCEPT;
638     bitset& operator^=(const bitset& __rhs) _NOEXCEPT;
639     bitset& operator<<=(size_t __pos) _NOEXCEPT;
640     bitset& operator>>=(size_t __pos) _NOEXCEPT;
641     bitset& set() _NOEXCEPT;
642     bitset& set(size_t __pos, bool __val = true);
643     bitset& reset() _NOEXCEPT;
644     bitset& reset(size_t __pos);
645     bitset  operator~() const _NOEXCEPT;
646     bitset& flip() _NOEXCEPT;
647     bitset& flip(size_t __pos);
648
649     // element access:
650     _LIBCPP_INLINE_VISIBILITY const_reference operator[](size_t __p) const {return base::__make_ref(__p);}
651     _LIBCPP_INLINE_VISIBILITY       reference operator[](size_t __p)       {return base::__make_ref(__p);}
652     unsigned long to_ulong() const;
653     unsigned long long to_ullong() const;
654     template <class _CharT, class _Traits, class _Allocator>
655         basic_string<_CharT, _Traits, _Allocator> to_string(_CharT __zero = _CharT('0'),
656                                                             _CharT __one = _CharT('1')) const;
657     template <class _CharT, class _Traits>
658         basic_string<_CharT, _Traits, allocator<_CharT> > to_string(_CharT __zero = _CharT('0'),
659                                                                     _CharT __one = _CharT('1')) const;
660     template <class _CharT>
661         basic_string<_CharT, char_traits<_CharT>, allocator<_CharT> > to_string(_CharT __zero = _CharT('0'),
662                                                                                 _CharT __one = _CharT('1')) const;
663     basic_string<char, char_traits<char>, allocator<char> > to_string(char __zero = '0',
664                                                                       char __one = '1') const;
665     size_t count() const _NOEXCEPT;
666     /*constexpr*/ _LIBCPP_INLINE_VISIBILITY size_t size() const _NOEXCEPT {return _Size;}
667     bool operator==(const bitset& __rhs) const _NOEXCEPT;
668     bool operator!=(const bitset& __rhs) const _NOEXCEPT;
669     bool test(size_t __pos) const;
670     bool all() const _NOEXCEPT;
671     bool any() const _NOEXCEPT;
672     _LIBCPP_INLINE_VISIBILITY bool none() const _NOEXCEPT {return !any();}
673     bitset operator<<(size_t __pos) const _NOEXCEPT;
674     bitset operator>>(size_t __pos) const _NOEXCEPT;
675
676 private:
677
678     _LIBCPP_INLINE_VISIBILITY
679     size_t __hash_code() const _NOEXCEPT {return base::__hash_code();}
680
681     friend struct hash<bitset>;
682 };
683
684 template <size_t _Size>
685 template<class _CharT>
686 bitset<_Size>::bitset(const _CharT* __str,
687                       typename basic_string<_CharT>::size_type __n,
688                       _CharT __zero, _CharT __one)
689 {
690     size_t __rlen = _VSTD::min(__n, char_traits<_CharT>::length(__str));
691     for (size_t __i = 0; __i < __rlen; ++__i)
692         if (__str[__i] != __zero && __str[__i] != __one)
693 #ifndef _LIBCPP_NO_EXCEPTIONS
694             throw invalid_argument("bitset string ctor has invalid argument");
695 #else
696             assert(!"bitset string ctor has invalid argument");
697 #endif
698     size_t _Mp = _VSTD::min(__rlen, _Size);
699     size_t __i = 0;
700     for (; __i < _Mp; ++__i)
701     {
702         _CharT __c = __str[_Mp - 1 - __i];
703         if (__c == __zero)
704             (*this)[__i] = false;
705         else
706             (*this)[__i] = true;
707     }
708     _VSTD::fill(base::__make_iter(__i), base::__make_iter(_Size), false);
709 }
710
711 template <size_t _Size>
712 template<class _CharT, class _Traits, class _Allocator>
713 bitset<_Size>::bitset(const basic_string<_CharT,_Traits,_Allocator>& __str,
714        typename basic_string<_CharT,_Traits,_Allocator>::size_type __pos,
715        typename basic_string<_CharT,_Traits,_Allocator>::size_type __n,
716        _CharT __zero, _CharT __one)
717 {
718     if (__pos > __str.size())
719 #ifndef _LIBCPP_NO_EXCEPTIONS
720         throw out_of_range("bitset string pos out of range");
721 #else
722         assert(!"bitset string pos out of range");
723 #endif
724     size_t __rlen = _VSTD::min(__n, __str.size() - __pos);
725     for (size_t __i = __pos; __i < __pos + __rlen; ++__i)
726         if (!_Traits::eq(__str[__i], __zero) && !_Traits::eq(__str[__i], __one))
727 #ifndef _LIBCPP_NO_EXCEPTIONS
728             throw invalid_argument("bitset string ctor has invalid argument");
729 #else
730             assert(!"bitset string ctor has invalid argument");
731 #endif
732     size_t _Mp = _VSTD::min(__rlen, _Size);
733     size_t __i = 0;
734     for (; __i < _Mp; ++__i)
735     {
736         _CharT __c = __str[__pos + _Mp - 1 - __i];
737         if (_Traits::eq(__c, __zero))
738             (*this)[__i] = false;
739         else
740             (*this)[__i] = true;
741     }
742     _VSTD::fill(base::__make_iter(__i), base::__make_iter(_Size), false);
743 }
744
745 template <size_t _Size>
746 inline _LIBCPP_INLINE_VISIBILITY
747 bitset<_Size>&
748 bitset<_Size>::operator&=(const bitset& __rhs) _NOEXCEPT
749 {
750     base::operator&=(__rhs);
751     return *this;
752 }
753
754 template <size_t _Size>
755 inline _LIBCPP_INLINE_VISIBILITY
756 bitset<_Size>&
757 bitset<_Size>::operator|=(const bitset& __rhs) _NOEXCEPT
758 {
759     base::operator|=(__rhs);
760     return *this;
761 }
762
763 template <size_t _Size>
764 inline _LIBCPP_INLINE_VISIBILITY
765 bitset<_Size>&
766 bitset<_Size>::operator^=(const bitset& __rhs) _NOEXCEPT
767 {
768     base::operator^=(__rhs);
769     return *this;
770 }
771
772 template <size_t _Size>
773 bitset<_Size>&
774 bitset<_Size>::operator<<=(size_t __pos) _NOEXCEPT
775 {
776     __pos = _VSTD::min(__pos, _Size);
777     _VSTD::copy_backward(base::__make_iter(0), base::__make_iter(_Size - __pos), base::__make_iter(_Size));
778     _VSTD::fill_n(base::__make_iter(0), __pos, false);
779     return *this;
780 }
781
782 template <size_t _Size>
783 bitset<_Size>&
784 bitset<_Size>::operator>>=(size_t __pos) _NOEXCEPT
785 {
786     __pos = _VSTD::min(__pos, _Size);
787     _VSTD::copy(base::__make_iter(__pos), base::__make_iter(_Size), base::__make_iter(0));
788     _VSTD::fill_n(base::__make_iter(_Size - __pos), __pos, false);
789     return *this;
790 }
791
792 template <size_t _Size>
793 inline _LIBCPP_INLINE_VISIBILITY
794 bitset<_Size>&
795 bitset<_Size>::set() _NOEXCEPT
796 {
797     _VSTD::fill_n(base::__make_iter(0), _Size, true);
798     return *this;
799 }
800
801 template <size_t _Size>
802 bitset<_Size>&
803 bitset<_Size>::set(size_t __pos, bool __val)
804 {
805     if (__pos >= _Size)
806 #ifndef _LIBCPP_NO_EXCEPTIONS
807         throw out_of_range("bitset set argument out of range");
808 #else
809         assert(!"bitset set argument out of range");
810 #endif
811     (*this)[__pos] = __val;
812     return *this;
813 }
814
815 template <size_t _Size>
816 inline _LIBCPP_INLINE_VISIBILITY
817 bitset<_Size>&
818 bitset<_Size>::reset() _NOEXCEPT
819 {
820     _VSTD::fill_n(base::__make_iter(0), _Size, false);
821     return *this;
822 }
823
824 template <size_t _Size>
825 bitset<_Size>&
826 bitset<_Size>::reset(size_t __pos)
827 {
828     if (__pos >= _Size)
829 #ifndef _LIBCPP_NO_EXCEPTIONS
830         throw out_of_range("bitset reset argument out of range");
831 #else
832         assert(!"bitset reset argument out of range");
833 #endif
834     (*this)[__pos] = false;
835     return *this;
836 }
837
838 template <size_t _Size>
839 inline _LIBCPP_INLINE_VISIBILITY
840 bitset<_Size>
841 bitset<_Size>::operator~() const _NOEXCEPT
842 {
843     bitset __x(*this);
844     __x.flip();
845     return __x;
846 }
847
848 template <size_t _Size>
849 inline _LIBCPP_INLINE_VISIBILITY
850 bitset<_Size>&
851 bitset<_Size>::flip() _NOEXCEPT
852 {
853     base::flip();
854     return *this;
855 }
856
857 template <size_t _Size>
858 bitset<_Size>&
859 bitset<_Size>::flip(size_t __pos)
860 {
861     if (__pos >= _Size)
862 #ifndef _LIBCPP_NO_EXCEPTIONS
863         throw out_of_range("bitset flip argument out of range");
864 #else
865         assert(!"bitset flip argument out of range");
866 #endif
867     reference r = base::__make_ref(__pos);
868     r = ~r;
869     return *this;
870 }
871
872 template <size_t _Size>
873 inline _LIBCPP_INLINE_VISIBILITY
874 unsigned long
875 bitset<_Size>::to_ulong() const
876 {
877     return base::to_ulong();
878 }
879
880 template <size_t _Size>
881 inline _LIBCPP_INLINE_VISIBILITY
882 unsigned long long
883 bitset<_Size>::to_ullong() const
884 {
885     return base::to_ullong();
886 }
887
888 template <size_t _Size>
889 template <class _CharT, class _Traits, class _Allocator>
890 basic_string<_CharT, _Traits, _Allocator>
891 bitset<_Size>::to_string(_CharT __zero, _CharT __one) const
892 {
893     basic_string<_CharT, _Traits, _Allocator> __r(_Size, __zero);
894     for (size_t __i = 0; __i < _Size; ++__i)
895     {
896         if ((*this)[__i])
897             __r[_Size - 1 - __i] = __one;
898     }
899     return __r;
900 }
901
902 template <size_t _Size>
903 template <class _CharT, class _Traits>
904 inline _LIBCPP_INLINE_VISIBILITY
905 basic_string<_CharT, _Traits, allocator<_CharT> >
906 bitset<_Size>::to_string(_CharT __zero, _CharT __one) const
907 {
908     return to_string<_CharT, _Traits, allocator<_CharT> >(__zero, __one);
909 }
910
911 template <size_t _Size>
912 template <class _CharT>
913 inline _LIBCPP_INLINE_VISIBILITY
914 basic_string<_CharT, char_traits<_CharT>, allocator<_CharT> >
915 bitset<_Size>::to_string(_CharT __zero, _CharT __one) const
916 {
917     return to_string<_CharT, char_traits<_CharT>, allocator<_CharT> >(__zero, __one);
918 }
919
920 template <size_t _Size>
921 inline _LIBCPP_INLINE_VISIBILITY
922 basic_string<char, char_traits<char>, allocator<char> >
923 bitset<_Size>::to_string(char __zero, char __one) const
924 {
925     return to_string<char, char_traits<char>, allocator<char> >(__zero, __one);
926 }
927
928 template <size_t _Size>
929 inline _LIBCPP_INLINE_VISIBILITY
930 size_t
931 bitset<_Size>::count() const _NOEXCEPT
932 {
933     return static_cast<size_t>(_VSTD::count(base::__make_iter(0), base::__make_iter(_Size), true));
934 }
935
936 template <size_t _Size>
937 inline _LIBCPP_INLINE_VISIBILITY
938 bool
939 bitset<_Size>::operator==(const bitset& __rhs) const _NOEXCEPT
940 {
941     return _VSTD::equal(base::__make_iter(0), base::__make_iter(_Size), __rhs.__make_iter(0));
942 }
943
944 template <size_t _Size>
945 inline _LIBCPP_INLINE_VISIBILITY
946 bool
947 bitset<_Size>::operator!=(const bitset& __rhs) const _NOEXCEPT
948 {
949     return !(*this == __rhs);
950 }
951
952 template <size_t _Size>
953 bool
954 bitset<_Size>::test(size_t __pos) const
955 {
956     if (__pos >= _Size)
957 #ifndef _LIBCPP_NO_EXCEPTIONS
958         throw out_of_range("bitset test argument out of range");
959 #else
960         assert(!"bitset test argument out of range");
961 #endif
962     return (*this)[__pos];
963 }
964
965 template <size_t _Size>
966 inline _LIBCPP_INLINE_VISIBILITY
967 bool
968 bitset<_Size>::all() const _NOEXCEPT
969 {
970     return base::all();
971 }
972
973 template <size_t _Size>
974 inline _LIBCPP_INLINE_VISIBILITY
975 bool
976 bitset<_Size>::any() const _NOEXCEPT
977 {
978     return base::any();
979 }
980
981 template <size_t _Size>
982 inline _LIBCPP_INLINE_VISIBILITY
983 bitset<_Size>
984 bitset<_Size>::operator<<(size_t __pos) const _NOEXCEPT
985 {
986     bitset __r = *this;
987     __r <<= __pos;
988     return __r;
989 }
990
991 template <size_t _Size>
992 inline _LIBCPP_INLINE_VISIBILITY
993 bitset<_Size>
994 bitset<_Size>::operator>>(size_t __pos) const _NOEXCEPT
995 {
996     bitset __r = *this;
997     __r >>= __pos;
998     return __r;
999 }
1000
1001 template <size_t _Size>
1002 inline _LIBCPP_INLINE_VISIBILITY
1003 bitset<_Size>
1004 operator&(const bitset<_Size>& __x, const bitset<_Size>& __y) _NOEXCEPT
1005 {
1006     bitset<_Size> __r = __x;
1007     __r &= __y;
1008     return __r;
1009 }
1010
1011 template <size_t _Size>
1012 inline _LIBCPP_INLINE_VISIBILITY
1013 bitset<_Size>
1014 operator|(const bitset<_Size>& __x, const bitset<_Size>& __y) _NOEXCEPT
1015 {
1016     bitset<_Size> __r = __x;
1017     __r |= __y;
1018     return __r;
1019 }
1020
1021 template <size_t _Size>
1022 inline _LIBCPP_INLINE_VISIBILITY
1023 bitset<_Size>
1024 operator^(const bitset<_Size>& __x, const bitset<_Size>& __y) _NOEXCEPT
1025 {
1026     bitset<_Size> __r = __x;
1027     __r ^= __y;
1028     return __r;
1029 }
1030
1031 template <size_t _Size>
1032 struct _LIBCPP_VISIBLE hash<bitset<_Size> >
1033     : public unary_function<bitset<_Size>, size_t>
1034 {
1035     _LIBCPP_INLINE_VISIBILITY
1036     size_t operator()(const bitset<_Size>& __bs) const _NOEXCEPT
1037         {return __bs.__hash_code();}
1038 };
1039
1040 template <class _CharT, class _Traits, size_t _Size>
1041 basic_istream<_CharT, _Traits>&
1042 operator>>(basic_istream<_CharT, _Traits>& __is, bitset<_Size>& __x);
1043
1044 template <class _CharT, class _Traits, size_t _Size>
1045 basic_ostream<_CharT, _Traits>&
1046 operator<<(basic_ostream<_CharT, _Traits>& __os, const bitset<_Size>& __x);
1047
1048 _LIBCPP_END_NAMESPACE_STD
1049
1050 #endif  // _LIBCPP_BITSET