2 //===---------------------------- bitset ----------------------------------===//
4 // The LLVM Compiler Infrastructure
6 // This file is dual licensed under the MIT and the University of Illinois Open
7 // Source Licenses. See LICENSE.TXT for details.
9 //===----------------------------------------------------------------------===//
11 #ifndef _LIBCPP_BITSET
12 #define _LIBCPP_BITSET
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();
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'));
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);
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;
92 // 23.3.5.3 bitset operators:
94 bitset<N> operator&(const bitset<N>&, const bitset<N>&) noexcept;
97 bitset<N> operator|(const bitset<N>&, const bitset<N>&) noexcept;
100 bitset<N> operator^(const bitset<N>&, const bitset<N>&) noexcept;
102 template <class charT, class traits, size_t N>
103 basic_istream<charT, traits>&
104 operator>>(basic_istream<charT, traits>& is, bitset<N>& x);
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);
110 template <size_t N> struct hash<std::bitset<N>>;
116 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
117 #pragma GCC system_header
121 #include <__bit_reference>
127 #include <__functional_base>
128 #if defined(_LIBCPP_NO_EXCEPTIONS)
132 #include <__undef_min_max>
134 _LIBCPP_BEGIN_NAMESPACE_STD
136 template <size_t _N_words, size_t _Size>
139 template <size_t _N_words, size_t _Size>
140 struct __has_storage_type<__bitset<_N_words, _Size> >
142 static const bool value = true;
145 template <size_t _N_words, size_t _Size>
149 typedef ptrdiff_t difference_type;
150 typedef size_t size_type;
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);
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>;
164 __storage_type __first_[_N_words];
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;
171 __bitset() _NOEXCEPT;
172 explicit __bitset(unsigned long long __v) _NOEXCEPT;
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);}
183 void operator&=(const __bitset& __v) _NOEXCEPT;
184 void operator|=(const __bitset& __v) _NOEXCEPT;
185 void operator^=(const __bitset& __v) _NOEXCEPT;
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>());}
193 bool all() const _NOEXCEPT;
194 bool any() const _NOEXCEPT;
195 size_t __hash_code() const _NOEXCEPT;
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;
207 template <size_t _N_words, size_t _Size>
208 inline _LIBCPP_INLINE_VISIBILITY
209 __bitset<_N_words, _Size>::__bitset() _NOEXCEPT
211 _VSTD::fill_n(__first_, _N_words, __storage_type(0));
214 template <size_t _N_words, size_t _Size>
216 __bitset<_N_words, _Size>::__init(unsigned long long __v, false_type) _NOEXCEPT
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]),
226 template <size_t _N_words, size_t _Size>
227 inline _LIBCPP_INLINE_VISIBILITY
229 __bitset<_N_words, _Size>::__init(unsigned long long __v, true_type) _NOEXCEPT
232 _VSTD::fill(__first_ + 1, __first_ + sizeof(__first_)/sizeof(__first_[0]), __storage_type(0));
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
239 __init(__v, integral_constant<bool, sizeof(unsigned long long) == sizeof(__storage_type)>());
242 template <size_t _N_words, size_t _Size>
243 inline _LIBCPP_INLINE_VISIBILITY
245 __bitset<_N_words, _Size>::operator&=(const __bitset& __v) _NOEXCEPT
247 for (size_type __i = 0; __i < _N_words; ++__i)
248 __first_[__i] &= __v.__first_[__i];
251 template <size_t _N_words, size_t _Size>
252 inline _LIBCPP_INLINE_VISIBILITY
254 __bitset<_N_words, _Size>::operator|=(const __bitset& __v) _NOEXCEPT
256 for (size_type __i = 0; __i < _N_words; ++__i)
257 __first_[__i] |= __v.__first_[__i];
260 template <size_t _N_words, size_t _Size>
261 inline _LIBCPP_INLINE_VISIBILITY
263 __bitset<_N_words, _Size>::operator^=(const __bitset& __v) _NOEXCEPT
265 for (size_type __i = 0; __i < _N_words; ++__i)
266 __first_[__i] ^= __v.__first_[__i];
269 template <size_t _N_words, size_t _Size>
271 __bitset<_N_words, _Size>::flip() _NOEXCEPT
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)
278 // do last partial word
281 __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
282 __storage_type __b = *__p & __m;
288 template <size_t _N_words, size_t _Size>
290 __bitset<_N_words, _Size>::to_ulong(false_type) const
292 const_iterator __e = __make_iter(_Size);
293 const_iterator __i = _VSTD::find(__make_iter(sizeof(unsigned long) * CHAR_BIT), __e, true);
295 #ifndef _LIBCPP_NO_EXCEPTIONS
296 throw overflow_error("bitset to_ulong overflow error");
298 assert(!"bitset to_ulong overflow error");
303 template <size_t _N_words, size_t _Size>
304 inline _LIBCPP_INLINE_VISIBILITY
306 __bitset<_N_words, _Size>::to_ulong(true_type) const
311 template <size_t _N_words, size_t _Size>
313 __bitset<_N_words, _Size>::to_ullong(false_type) const
315 const_iterator __e = __make_iter(_Size);
316 const_iterator __i = _VSTD::find(__make_iter(sizeof(unsigned long long) * CHAR_BIT), __e, true);
318 #ifndef _LIBCPP_NO_EXCEPTIONS
319 throw overflow_error("bitset to_ullong overflow error");
321 assert(!"bitset to_ullong overflow error");
323 return to_ullong(true_type());
326 template <size_t _N_words, size_t _Size>
327 inline _LIBCPP_INLINE_VISIBILITY
329 __bitset<_N_words, _Size>::to_ullong(true_type) const
331 return to_ullong(true_type(), integral_constant<bool, sizeof(__storage_type) < sizeof(unsigned long long)>());
334 template <size_t _N_words, size_t _Size>
335 inline _LIBCPP_INLINE_VISIBILITY
337 __bitset<_N_words, _Size>::to_ullong(true_type, false_type) const
342 template <size_t _N_words, size_t _Size>
344 __bitset<_N_words, _Size>::to_ullong(true_type, true_type) const
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);
352 template <size_t _N_words, size_t _Size>
354 __bitset<_N_words, _Size>::all() const _NOEXCEPT
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)
362 // do last partial word
365 __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
372 template <size_t _N_words, size_t _Size>
374 __bitset<_N_words, _Size>::any() const _NOEXCEPT
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)
382 // do last partial word
385 __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
392 template <size_t _N_words, size_t _Size>
393 inline _LIBCPP_INLINE_VISIBILITY
395 __bitset<_N_words, _Size>::__hash_code() const _NOEXCEPT
398 for (size_type __i = 0; __i < _N_words; ++__i)
399 __h ^= __first_[__i];
403 template <size_t _Size>
404 class __bitset<1, _Size>
407 typedef ptrdiff_t difference_type;
408 typedef size_t size_type;
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);
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>;
422 __storage_type __first_;
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;
429 __bitset() _NOEXCEPT;
430 explicit __bitset(unsigned long long __v) _NOEXCEPT;
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);}
441 void operator&=(const __bitset& __v) _NOEXCEPT;
442 void operator|=(const __bitset& __v) _NOEXCEPT;
443 void operator^=(const __bitset& __v) _NOEXCEPT;
445 void flip() _NOEXCEPT;
447 unsigned long to_ulong() const;
448 unsigned long long to_ullong() const;
450 bool all() const _NOEXCEPT;
451 bool any() const _NOEXCEPT;
453 size_t __hash_code() const _NOEXCEPT;
456 template <size_t _Size>
457 inline _LIBCPP_INLINE_VISIBILITY
458 __bitset<1, _Size>::__bitset() _NOEXCEPT
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))
470 template <size_t _Size>
471 inline _LIBCPP_INLINE_VISIBILITY
473 __bitset<1, _Size>::operator&=(const __bitset& __v) _NOEXCEPT
475 __first_ &= __v.__first_;
478 template <size_t _Size>
479 inline _LIBCPP_INLINE_VISIBILITY
481 __bitset<1, _Size>::operator|=(const __bitset& __v) _NOEXCEPT
483 __first_ |= __v.__first_;
486 template <size_t _Size>
487 inline _LIBCPP_INLINE_VISIBILITY
489 __bitset<1, _Size>::operator^=(const __bitset& __v) _NOEXCEPT
491 __first_ ^= __v.__first_;
494 template <size_t _Size>
495 inline _LIBCPP_INLINE_VISIBILITY
497 __bitset<1, _Size>::flip() _NOEXCEPT
499 __storage_type __m = ~__storage_type(0) >> (__bits_per_word - _Size);
500 __first_ = ~__first_;
504 template <size_t _Size>
505 inline _LIBCPP_INLINE_VISIBILITY
507 __bitset<1, _Size>::to_ulong() const
512 template <size_t _Size>
513 inline _LIBCPP_INLINE_VISIBILITY
515 __bitset<1, _Size>::to_ullong() const
520 template <size_t _Size>
521 inline _LIBCPP_INLINE_VISIBILITY
523 __bitset<1, _Size>::all() const _NOEXCEPT
525 __storage_type __m = ~__storage_type(0) >> (__bits_per_word - _Size);
526 return !(~__first_ & __m);
529 template <size_t _Size>
530 inline _LIBCPP_INLINE_VISIBILITY
532 __bitset<1, _Size>::any() const _NOEXCEPT
534 __storage_type __m = ~__storage_type(0) >> (__bits_per_word - _Size);
535 return __first_ & __m;
538 template <size_t _Size>
539 inline _LIBCPP_INLINE_VISIBILITY
541 __bitset<1, _Size>::__hash_code() const _NOEXCEPT
550 typedef ptrdiff_t difference_type;
551 typedef size_t size_type;
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);
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>;
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;
570 __bitset() _NOEXCEPT;
571 explicit __bitset(unsigned long long) _NOEXCEPT;
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);}
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 {}
586 _LIBCPP_INLINE_VISIBILITY void flip() _NOEXCEPT {}
588 _LIBCPP_INLINE_VISIBILITY unsigned long to_ulong() const {return 0;}
589 _LIBCPP_INLINE_VISIBILITY unsigned long long to_ullong() const {return 0;}
591 _LIBCPP_INLINE_VISIBILITY bool all() const _NOEXCEPT {return true;}
592 _LIBCPP_INLINE_VISIBILITY bool any() const _NOEXCEPT {return false;}
594 _LIBCPP_INLINE_VISIBILITY size_t __hash_code() const _NOEXCEPT {return 0;}
597 inline _LIBCPP_INLINE_VISIBILITY
598 __bitset<0, 0>::__bitset() _NOEXCEPT
602 inline _LIBCPP_INLINE_VISIBILITY
603 __bitset<0, 0>::__bitset(unsigned long long) _NOEXCEPT
607 template <size_t _Size> class bitset;
608 template <size_t _Size> struct hash<bitset<_Size> >;
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>
614 static const unsigned __n_words = _Size == 0 ? 0 : (_Size - 1) / (sizeof(size_t) * CHAR_BIT) + 1;
615 typedef __bitset<__n_words, _Size> base;
618 typedef typename base::reference reference;
619 typedef typename base::const_reference const_reference;
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'));
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);
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;
678 _LIBCPP_INLINE_VISIBILITY
679 size_t __hash_code() const _NOEXCEPT {return base::__hash_code();}
681 friend struct hash<bitset>;
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)
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");
696 assert(!"bitset string ctor has invalid argument");
698 size_t _Mp = _VSTD::min(__rlen, _Size);
700 for (; __i < _Mp; ++__i)
702 _CharT __c = __str[_Mp - 1 - __i];
704 (*this)[__i] = false;
708 _VSTD::fill(base::__make_iter(__i), base::__make_iter(_Size), false);
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)
718 if (__pos > __str.size())
719 #ifndef _LIBCPP_NO_EXCEPTIONS
720 throw out_of_range("bitset string pos out of range");
722 assert(!"bitset string pos out of range");
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");
730 assert(!"bitset string ctor has invalid argument");
732 size_t _Mp = _VSTD::min(__rlen, _Size);
734 for (; __i < _Mp; ++__i)
736 _CharT __c = __str[__pos + _Mp - 1 - __i];
737 if (_Traits::eq(__c, __zero))
738 (*this)[__i] = false;
742 _VSTD::fill(base::__make_iter(__i), base::__make_iter(_Size), false);
745 template <size_t _Size>
746 inline _LIBCPP_INLINE_VISIBILITY
748 bitset<_Size>::operator&=(const bitset& __rhs) _NOEXCEPT
750 base::operator&=(__rhs);
754 template <size_t _Size>
755 inline _LIBCPP_INLINE_VISIBILITY
757 bitset<_Size>::operator|=(const bitset& __rhs) _NOEXCEPT
759 base::operator|=(__rhs);
763 template <size_t _Size>
764 inline _LIBCPP_INLINE_VISIBILITY
766 bitset<_Size>::operator^=(const bitset& __rhs) _NOEXCEPT
768 base::operator^=(__rhs);
772 template <size_t _Size>
774 bitset<_Size>::operator<<=(size_t __pos) _NOEXCEPT
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);
782 template <size_t _Size>
784 bitset<_Size>::operator>>=(size_t __pos) _NOEXCEPT
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);
792 template <size_t _Size>
793 inline _LIBCPP_INLINE_VISIBILITY
795 bitset<_Size>::set() _NOEXCEPT
797 _VSTD::fill_n(base::__make_iter(0), _Size, true);
801 template <size_t _Size>
803 bitset<_Size>::set(size_t __pos, bool __val)
806 #ifndef _LIBCPP_NO_EXCEPTIONS
807 throw out_of_range("bitset set argument out of range");
809 assert(!"bitset set argument out of range");
811 (*this)[__pos] = __val;
815 template <size_t _Size>
816 inline _LIBCPP_INLINE_VISIBILITY
818 bitset<_Size>::reset() _NOEXCEPT
820 _VSTD::fill_n(base::__make_iter(0), _Size, false);
824 template <size_t _Size>
826 bitset<_Size>::reset(size_t __pos)
829 #ifndef _LIBCPP_NO_EXCEPTIONS
830 throw out_of_range("bitset reset argument out of range");
832 assert(!"bitset reset argument out of range");
834 (*this)[__pos] = false;
838 template <size_t _Size>
839 inline _LIBCPP_INLINE_VISIBILITY
841 bitset<_Size>::operator~() const _NOEXCEPT
848 template <size_t _Size>
849 inline _LIBCPP_INLINE_VISIBILITY
851 bitset<_Size>::flip() _NOEXCEPT
857 template <size_t _Size>
859 bitset<_Size>::flip(size_t __pos)
862 #ifndef _LIBCPP_NO_EXCEPTIONS
863 throw out_of_range("bitset flip argument out of range");
865 assert(!"bitset flip argument out of range");
867 reference r = base::__make_ref(__pos);
872 template <size_t _Size>
873 inline _LIBCPP_INLINE_VISIBILITY
875 bitset<_Size>::to_ulong() const
877 return base::to_ulong();
880 template <size_t _Size>
881 inline _LIBCPP_INLINE_VISIBILITY
883 bitset<_Size>::to_ullong() const
885 return base::to_ullong();
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
893 basic_string<_CharT, _Traits, _Allocator> __r(_Size, __zero);
894 for (size_t __i = 0; __i < _Size; ++__i)
897 __r[_Size - 1 - __i] = __one;
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
908 return to_string<_CharT, _Traits, allocator<_CharT> >(__zero, __one);
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
917 return to_string<_CharT, char_traits<_CharT>, allocator<_CharT> >(__zero, __one);
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
925 return to_string<char, char_traits<char>, allocator<char> >(__zero, __one);
928 template <size_t _Size>
929 inline _LIBCPP_INLINE_VISIBILITY
931 bitset<_Size>::count() const _NOEXCEPT
933 return static_cast<size_t>(_VSTD::count(base::__make_iter(0), base::__make_iter(_Size), true));
936 template <size_t _Size>
937 inline _LIBCPP_INLINE_VISIBILITY
939 bitset<_Size>::operator==(const bitset& __rhs) const _NOEXCEPT
941 return _VSTD::equal(base::__make_iter(0), base::__make_iter(_Size), __rhs.__make_iter(0));
944 template <size_t _Size>
945 inline _LIBCPP_INLINE_VISIBILITY
947 bitset<_Size>::operator!=(const bitset& __rhs) const _NOEXCEPT
949 return !(*this == __rhs);
952 template <size_t _Size>
954 bitset<_Size>::test(size_t __pos) const
957 #ifndef _LIBCPP_NO_EXCEPTIONS
958 throw out_of_range("bitset test argument out of range");
960 assert(!"bitset test argument out of range");
962 return (*this)[__pos];
965 template <size_t _Size>
966 inline _LIBCPP_INLINE_VISIBILITY
968 bitset<_Size>::all() const _NOEXCEPT
973 template <size_t _Size>
974 inline _LIBCPP_INLINE_VISIBILITY
976 bitset<_Size>::any() const _NOEXCEPT
981 template <size_t _Size>
982 inline _LIBCPP_INLINE_VISIBILITY
984 bitset<_Size>::operator<<(size_t __pos) const _NOEXCEPT
991 template <size_t _Size>
992 inline _LIBCPP_INLINE_VISIBILITY
994 bitset<_Size>::operator>>(size_t __pos) const _NOEXCEPT
1001 template <size_t _Size>
1002 inline _LIBCPP_INLINE_VISIBILITY
1004 operator&(const bitset<_Size>& __x, const bitset<_Size>& __y) _NOEXCEPT
1006 bitset<_Size> __r = __x;
1011 template <size_t _Size>
1012 inline _LIBCPP_INLINE_VISIBILITY
1014 operator|(const bitset<_Size>& __x, const bitset<_Size>& __y) _NOEXCEPT
1016 bitset<_Size> __r = __x;
1021 template <size_t _Size>
1022 inline _LIBCPP_INLINE_VISIBILITY
1024 operator^(const bitset<_Size>& __x, const bitset<_Size>& __y) _NOEXCEPT
1026 bitset<_Size> __r = __x;
1031 template <size_t _Size>
1032 struct _LIBCPP_VISIBLE hash<bitset<_Size> >
1033 : public unary_function<bitset<_Size>, size_t>
1035 _LIBCPP_INLINE_VISIBILITY
1036 size_t operator()(const bitset<_Size>& __bs) const _NOEXCEPT
1037 {return __bs.__hash_code();}
1040 template <class _CharT, class _Traits, size_t _Size>
1041 basic_istream<_CharT, _Traits>&
1042 operator>>(basic_istream<_CharT, _Traits>& __is, bitset<_Size>& __x);
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);
1048 _LIBCPP_END_NAMESPACE_STD
1050 #endif // _LIBCPP_BITSET