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>
129 #include <__undef_min_max>
131 _LIBCPP_BEGIN_NAMESPACE_STD
133 template <size_t _N_words, size_t _Size>
136 template <size_t _N_words, size_t _Size>
137 struct __has_storage_type<__bitset<_N_words, _Size> >
139 static const bool value = true;
142 template <size_t _N_words, size_t _Size>
146 typedef ptrdiff_t difference_type;
147 typedef size_t size_type;
148 typedef size_type __storage_type;
150 typedef __bitset __self;
151 typedef __storage_type* __storage_pointer;
152 typedef const __storage_type* __const_storage_pointer;
153 static const unsigned __bits_per_word = static_cast<unsigned>(sizeof(__storage_type) * CHAR_BIT);
155 friend class __bit_reference<__bitset>;
156 friend class __bit_const_reference<__bitset>;
157 friend class __bit_iterator<__bitset, false>;
158 friend class __bit_iterator<__bitset, true>;
159 friend struct __bit_array<__bitset>;
161 __storage_type __first_[_N_words];
163 typedef __bit_reference<__bitset> reference;
164 typedef __bit_const_reference<__bitset> const_reference;
165 typedef __bit_iterator<__bitset, false> iterator;
166 typedef __bit_iterator<__bitset, true> const_iterator;
168 _LIBCPP_INLINE_VISIBILITY
169 _LIBCPP_CONSTEXPR __bitset() _NOEXCEPT;
170 _LIBCPP_INLINE_VISIBILITY
171 explicit _LIBCPP_CONSTEXPR __bitset(unsigned long long __v) _NOEXCEPT;
173 _LIBCPP_INLINE_VISIBILITY reference __make_ref(size_t __pos) _NOEXCEPT
174 {return reference(__first_ + __pos / __bits_per_word, __storage_type(1) << __pos % __bits_per_word);}
175 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR const_reference __make_ref(size_t __pos) const _NOEXCEPT
176 {return const_reference(__first_ + __pos / __bits_per_word, __storage_type(1) << __pos % __bits_per_word);}
177 _LIBCPP_INLINE_VISIBILITY iterator __make_iter(size_t __pos) _NOEXCEPT
178 {return iterator(__first_ + __pos / __bits_per_word, __pos % __bits_per_word);}
179 _LIBCPP_INLINE_VISIBILITY const_iterator __make_iter(size_t __pos) const _NOEXCEPT
180 {return const_iterator(__first_ + __pos / __bits_per_word, __pos % __bits_per_word);}
182 _LIBCPP_INLINE_VISIBILITY
183 void operator&=(const __bitset& __v) _NOEXCEPT;
184 _LIBCPP_INLINE_VISIBILITY
185 void operator|=(const __bitset& __v) _NOEXCEPT;
186 _LIBCPP_INLINE_VISIBILITY
187 void operator^=(const __bitset& __v) _NOEXCEPT;
189 void flip() _NOEXCEPT;
190 _LIBCPP_INLINE_VISIBILITY unsigned long to_ulong() const
191 {return to_ulong(integral_constant<bool, _Size < sizeof(unsigned long) * CHAR_BIT>());}
192 _LIBCPP_INLINE_VISIBILITY unsigned long long to_ullong() const
193 {return to_ullong(integral_constant<bool, _Size < sizeof(unsigned long long) * CHAR_BIT>());}
195 bool all() const _NOEXCEPT;
196 bool any() const _NOEXCEPT;
197 _LIBCPP_INLINE_VISIBILITY
198 size_t __hash_code() const _NOEXCEPT;
200 #ifdef _LIBCPP_HAS_NO_CONSTEXPR
201 void __init(unsigned long long __v, false_type) _NOEXCEPT;
202 _LIBCPP_INLINE_VISIBILITY
203 void __init(unsigned long long __v, true_type) _NOEXCEPT;
204 #endif // _LIBCPP_HAS_NO_CONSTEXPR
205 unsigned long to_ulong(false_type) const;
206 _LIBCPP_INLINE_VISIBILITY
207 unsigned long to_ulong(true_type) const;
208 unsigned long long to_ullong(false_type) const;
209 _LIBCPP_INLINE_VISIBILITY
210 unsigned long long to_ullong(true_type) const;
211 _LIBCPP_INLINE_VISIBILITY
212 unsigned long long to_ullong(true_type, false_type) const;
213 unsigned long long to_ullong(true_type, true_type) const;
216 template <size_t _N_words, size_t _Size>
219 __bitset<_N_words, _Size>::__bitset() _NOEXCEPT
220 #ifndef _LIBCPP_HAS_NO_CONSTEXPR
224 #ifdef _LIBCPP_HAS_NO_CONSTEXPR
225 _VSTD::fill_n(__first_, _N_words, __storage_type(0));
229 #ifdef _LIBCPP_HAS_NO_CONSTEXPR
231 template <size_t _N_words, size_t _Size>
233 __bitset<_N_words, _Size>::__init(unsigned long long __v, false_type) _NOEXCEPT
235 __storage_type __t[sizeof(unsigned long long) / sizeof(__storage_type)];
236 for (size_t __i = 0; __i < sizeof(__t)/sizeof(__t[0]); ++__i, __v >>= __bits_per_word)
237 __t[__i] = static_cast<__storage_type>(__v);
238 _VSTD::copy(__t, __t + sizeof(__t)/sizeof(__t[0]), __first_);
239 _VSTD::fill(__first_ + sizeof(__t)/sizeof(__t[0]), __first_ + sizeof(__first_)/sizeof(__first_[0]),
243 template <size_t _N_words, size_t _Size>
244 inline _LIBCPP_INLINE_VISIBILITY
246 __bitset<_N_words, _Size>::__init(unsigned long long __v, true_type) _NOEXCEPT
249 _VSTD::fill(__first_ + 1, __first_ + sizeof(__first_)/sizeof(__first_[0]), __storage_type(0));
252 #endif // _LIBCPP_HAS_NO_CONSTEXPR
254 template <size_t _N_words, size_t _Size>
257 __bitset<_N_words, _Size>::__bitset(unsigned long long __v) _NOEXCEPT
258 #ifndef _LIBCPP_HAS_NO_CONSTEXPR
259 #if __SIZEOF_SIZE_T__ == 8
261 #elif __SIZEOF_SIZE_T__ == 4
262 : __first_{static_cast<__storage_type>(__v), static_cast<__storage_type>(__v >> __bits_per_word)}
264 #error This constructor has not been ported to this platform
268 #ifdef _LIBCPP_HAS_NO_CONSTEXPR
269 __init(__v, integral_constant<bool, sizeof(unsigned long long) == sizeof(__storage_type)>());
273 template <size_t _N_words, size_t _Size>
276 __bitset<_N_words, _Size>::operator&=(const __bitset& __v) _NOEXCEPT
278 for (size_type __i = 0; __i < _N_words; ++__i)
279 __first_[__i] &= __v.__first_[__i];
282 template <size_t _N_words, size_t _Size>
285 __bitset<_N_words, _Size>::operator|=(const __bitset& __v) _NOEXCEPT
287 for (size_type __i = 0; __i < _N_words; ++__i)
288 __first_[__i] |= __v.__first_[__i];
291 template <size_t _N_words, size_t _Size>
294 __bitset<_N_words, _Size>::operator^=(const __bitset& __v) _NOEXCEPT
296 for (size_type __i = 0; __i < _N_words; ++__i)
297 __first_[__i] ^= __v.__first_[__i];
300 template <size_t _N_words, size_t _Size>
302 __bitset<_N_words, _Size>::flip() _NOEXCEPT
304 // do middle whole words
305 size_type __n = _Size;
306 __storage_pointer __p = __first_;
307 for (; __n >= __bits_per_word; ++__p, __n -= __bits_per_word)
309 // do last partial word
312 __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
313 __storage_type __b = *__p & __m;
319 template <size_t _N_words, size_t _Size>
321 __bitset<_N_words, _Size>::to_ulong(false_type) const
323 const_iterator __e = __make_iter(_Size);
324 const_iterator __i = _VSTD::find(__make_iter(sizeof(unsigned long) * CHAR_BIT), __e, true);
326 __throw_overflow_error("bitset to_ulong overflow error");
331 template <size_t _N_words, size_t _Size>
334 __bitset<_N_words, _Size>::to_ulong(true_type) const
339 template <size_t _N_words, size_t _Size>
341 __bitset<_N_words, _Size>::to_ullong(false_type) const
343 const_iterator __e = __make_iter(_Size);
344 const_iterator __i = _VSTD::find(__make_iter(sizeof(unsigned long long) * CHAR_BIT), __e, true);
346 __throw_overflow_error("bitset to_ullong overflow error");
348 return to_ullong(true_type());
351 template <size_t _N_words, size_t _Size>
354 __bitset<_N_words, _Size>::to_ullong(true_type) const
356 return to_ullong(true_type(), integral_constant<bool, sizeof(__storage_type) < sizeof(unsigned long long)>());
359 template <size_t _N_words, size_t _Size>
362 __bitset<_N_words, _Size>::to_ullong(true_type, false_type) const
367 template <size_t _N_words, size_t _Size>
369 __bitset<_N_words, _Size>::to_ullong(true_type, true_type) const
371 unsigned long long __r = __first_[0];
372 for (std::size_t __i = 1; __i < sizeof(unsigned long long) / sizeof(__storage_type); ++__i)
373 __r |= static_cast<unsigned long long>(__first_[__i]) << (sizeof(__storage_type) * CHAR_BIT);
377 template <size_t _N_words, size_t _Size>
379 __bitset<_N_words, _Size>::all() const _NOEXCEPT
381 // do middle whole words
382 size_type __n = _Size;
383 __const_storage_pointer __p = __first_;
384 for (; __n >= __bits_per_word; ++__p, __n -= __bits_per_word)
387 // do last partial word
390 __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
397 template <size_t _N_words, size_t _Size>
399 __bitset<_N_words, _Size>::any() const _NOEXCEPT
401 // do middle whole words
402 size_type __n = _Size;
403 __const_storage_pointer __p = __first_;
404 for (; __n >= __bits_per_word; ++__p, __n -= __bits_per_word)
407 // do last partial word
410 __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
417 template <size_t _N_words, size_t _Size>
420 __bitset<_N_words, _Size>::__hash_code() const _NOEXCEPT
423 for (size_type __i = 0; __i < _N_words; ++__i)
424 __h ^= __first_[__i];
428 template <size_t _Size>
429 class __bitset<1, _Size>
432 typedef ptrdiff_t difference_type;
433 typedef size_t size_type;
434 typedef size_type __storage_type;
436 typedef __bitset __self;
437 typedef __storage_type* __storage_pointer;
438 typedef const __storage_type* __const_storage_pointer;
439 static const unsigned __bits_per_word = static_cast<unsigned>(sizeof(__storage_type) * CHAR_BIT);
441 friend class __bit_reference<__bitset>;
442 friend class __bit_const_reference<__bitset>;
443 friend class __bit_iterator<__bitset, false>;
444 friend class __bit_iterator<__bitset, true>;
445 friend struct __bit_array<__bitset>;
447 __storage_type __first_;
449 typedef __bit_reference<__bitset> reference;
450 typedef __bit_const_reference<__bitset> const_reference;
451 typedef __bit_iterator<__bitset, false> iterator;
452 typedef __bit_iterator<__bitset, true> const_iterator;
454 _LIBCPP_INLINE_VISIBILITY
455 _LIBCPP_CONSTEXPR __bitset() _NOEXCEPT;
456 _LIBCPP_INLINE_VISIBILITY
457 explicit _LIBCPP_CONSTEXPR __bitset(unsigned long long __v) _NOEXCEPT;
459 _LIBCPP_INLINE_VISIBILITY reference __make_ref(size_t __pos) _NOEXCEPT
460 {return reference(&__first_, __storage_type(1) << __pos);}
461 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR const_reference __make_ref(size_t __pos) const _NOEXCEPT
462 {return const_reference(&__first_, __storage_type(1) << __pos);}
463 _LIBCPP_INLINE_VISIBILITY iterator __make_iter(size_t __pos) _NOEXCEPT
464 {return iterator(&__first_ + __pos / __bits_per_word, __pos % __bits_per_word);}
465 _LIBCPP_INLINE_VISIBILITY const_iterator __make_iter(size_t __pos) const _NOEXCEPT
466 {return const_iterator(&__first_ + __pos / __bits_per_word, __pos % __bits_per_word);}
468 _LIBCPP_INLINE_VISIBILITY
469 void operator&=(const __bitset& __v) _NOEXCEPT;
470 _LIBCPP_INLINE_VISIBILITY
471 void operator|=(const __bitset& __v) _NOEXCEPT;
472 _LIBCPP_INLINE_VISIBILITY
473 void operator^=(const __bitset& __v) _NOEXCEPT;
475 _LIBCPP_INLINE_VISIBILITY
476 void flip() _NOEXCEPT;
478 _LIBCPP_INLINE_VISIBILITY
479 unsigned long to_ulong() const;
480 _LIBCPP_INLINE_VISIBILITY
481 unsigned long long to_ullong() const;
483 _LIBCPP_INLINE_VISIBILITY
484 bool all() const _NOEXCEPT;
485 _LIBCPP_INLINE_VISIBILITY
486 bool any() const _NOEXCEPT;
488 _LIBCPP_INLINE_VISIBILITY
489 size_t __hash_code() const _NOEXCEPT;
492 template <size_t _Size>
495 __bitset<1, _Size>::__bitset() _NOEXCEPT
500 template <size_t _Size>
503 __bitset<1, _Size>::__bitset(unsigned long long __v) _NOEXCEPT
504 : __first_(static_cast<__storage_type>(__v))
508 template <size_t _Size>
511 __bitset<1, _Size>::operator&=(const __bitset& __v) _NOEXCEPT
513 __first_ &= __v.__first_;
516 template <size_t _Size>
519 __bitset<1, _Size>::operator|=(const __bitset& __v) _NOEXCEPT
521 __first_ |= __v.__first_;
524 template <size_t _Size>
527 __bitset<1, _Size>::operator^=(const __bitset& __v) _NOEXCEPT
529 __first_ ^= __v.__first_;
532 template <size_t _Size>
535 __bitset<1, _Size>::flip() _NOEXCEPT
537 __storage_type __m = ~__storage_type(0) >> (__bits_per_word - _Size);
538 __first_ = ~__first_;
542 template <size_t _Size>
545 __bitset<1, _Size>::to_ulong() const
550 template <size_t _Size>
553 __bitset<1, _Size>::to_ullong() const
558 template <size_t _Size>
561 __bitset<1, _Size>::all() const _NOEXCEPT
563 __storage_type __m = ~__storage_type(0) >> (__bits_per_word - _Size);
564 return !(~__first_ & __m);
567 template <size_t _Size>
570 __bitset<1, _Size>::any() const _NOEXCEPT
572 __storage_type __m = ~__storage_type(0) >> (__bits_per_word - _Size);
573 return __first_ & __m;
576 template <size_t _Size>
579 __bitset<1, _Size>::__hash_code() const _NOEXCEPT
588 typedef ptrdiff_t difference_type;
589 typedef size_t size_type;
590 typedef size_type __storage_type;
592 typedef __bitset __self;
593 typedef __storage_type* __storage_pointer;
594 typedef const __storage_type* __const_storage_pointer;
595 static const unsigned __bits_per_word = static_cast<unsigned>(sizeof(__storage_type) * CHAR_BIT);
597 friend class __bit_reference<__bitset>;
598 friend class __bit_const_reference<__bitset>;
599 friend class __bit_iterator<__bitset, false>;
600 friend class __bit_iterator<__bitset, true>;
601 friend struct __bit_array<__bitset>;
603 typedef __bit_reference<__bitset> reference;
604 typedef __bit_const_reference<__bitset> const_reference;
605 typedef __bit_iterator<__bitset, false> iterator;
606 typedef __bit_iterator<__bitset, true> const_iterator;
608 _LIBCPP_INLINE_VISIBILITY
609 _LIBCPP_CONSTEXPR __bitset() _NOEXCEPT;
610 _LIBCPP_INLINE_VISIBILITY
611 explicit _LIBCPP_CONSTEXPR __bitset(unsigned long long) _NOEXCEPT;
613 _LIBCPP_INLINE_VISIBILITY reference __make_ref(size_t) _NOEXCEPT
614 {return reference(0, 1);}
615 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR const_reference __make_ref(size_t) const _NOEXCEPT
616 {return const_reference(0, 1);}
617 _LIBCPP_INLINE_VISIBILITY iterator __make_iter(size_t) _NOEXCEPT
618 {return iterator(0, 0);}
619 _LIBCPP_INLINE_VISIBILITY const_iterator __make_iter(size_t) const _NOEXCEPT
620 {return const_iterator(0, 0);}
622 _LIBCPP_INLINE_VISIBILITY void operator&=(const __bitset&) _NOEXCEPT {}
623 _LIBCPP_INLINE_VISIBILITY void operator|=(const __bitset&) _NOEXCEPT {}
624 _LIBCPP_INLINE_VISIBILITY void operator^=(const __bitset&) _NOEXCEPT {}
626 _LIBCPP_INLINE_VISIBILITY void flip() _NOEXCEPT {}
628 _LIBCPP_INLINE_VISIBILITY unsigned long to_ulong() const {return 0;}
629 _LIBCPP_INLINE_VISIBILITY unsigned long long to_ullong() const {return 0;}
631 _LIBCPP_INLINE_VISIBILITY bool all() const _NOEXCEPT {return true;}
632 _LIBCPP_INLINE_VISIBILITY bool any() const _NOEXCEPT {return false;}
634 _LIBCPP_INLINE_VISIBILITY size_t __hash_code() const _NOEXCEPT {return 0;}
639 __bitset<0, 0>::__bitset() _NOEXCEPT
645 __bitset<0, 0>::__bitset(unsigned long long) _NOEXCEPT
649 template <size_t _Size> class _LIBCPP_TEMPLATE_VIS bitset;
650 template <size_t _Size> struct hash<bitset<_Size> >;
652 template <size_t _Size>
653 class _LIBCPP_TEMPLATE_VIS bitset
654 : private __bitset<_Size == 0 ? 0 : (_Size - 1) / (sizeof(size_t) * CHAR_BIT) + 1, _Size>
657 static const unsigned __n_words = _Size == 0 ? 0 : (_Size - 1) / (sizeof(size_t) * CHAR_BIT) + 1;
658 typedef __bitset<__n_words, _Size> base;
661 typedef typename base::reference reference;
662 typedef typename base::const_reference const_reference;
664 // 23.3.5.1 constructors:
665 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR bitset() _NOEXCEPT {}
666 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
667 bitset(unsigned long long __v) _NOEXCEPT : base(__v) {}
668 template<class _CharT>
669 explicit bitset(const _CharT* __str,
670 typename basic_string<_CharT>::size_type __n = basic_string<_CharT>::npos,
671 _CharT __zero = _CharT('0'), _CharT __one = _CharT('1'));
672 template<class _CharT, class _Traits, class _Allocator>
673 explicit bitset(const basic_string<_CharT,_Traits,_Allocator>& __str,
674 typename basic_string<_CharT,_Traits,_Allocator>::size_type __pos = 0,
675 typename basic_string<_CharT,_Traits,_Allocator>::size_type __n =
676 (basic_string<_CharT,_Traits,_Allocator>::npos),
677 _CharT __zero = _CharT('0'), _CharT __one = _CharT('1'));
679 // 23.3.5.2 bitset operations:
680 _LIBCPP_INLINE_VISIBILITY
681 bitset& operator&=(const bitset& __rhs) _NOEXCEPT;
682 _LIBCPP_INLINE_VISIBILITY
683 bitset& operator|=(const bitset& __rhs) _NOEXCEPT;
684 _LIBCPP_INLINE_VISIBILITY
685 bitset& operator^=(const bitset& __rhs) _NOEXCEPT;
686 bitset& operator<<=(size_t __pos) _NOEXCEPT;
687 bitset& operator>>=(size_t __pos) _NOEXCEPT;
688 _LIBCPP_INLINE_VISIBILITY
689 bitset& set() _NOEXCEPT;
690 bitset& set(size_t __pos, bool __val = true);
691 _LIBCPP_INLINE_VISIBILITY
692 bitset& reset() _NOEXCEPT;
693 bitset& reset(size_t __pos);
694 _LIBCPP_INLINE_VISIBILITY
695 bitset operator~() const _NOEXCEPT;
696 _LIBCPP_INLINE_VISIBILITY
697 bitset& flip() _NOEXCEPT;
698 bitset& flip(size_t __pos);
701 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
702 const_reference operator[](size_t __p) const {return base::__make_ref(__p);}
703 _LIBCPP_INLINE_VISIBILITY reference operator[](size_t __p) {return base::__make_ref(__p);}
704 _LIBCPP_INLINE_VISIBILITY
705 unsigned long to_ulong() const;
706 _LIBCPP_INLINE_VISIBILITY
707 unsigned long long to_ullong() const;
708 template <class _CharT, class _Traits, class _Allocator>
709 basic_string<_CharT, _Traits, _Allocator> to_string(_CharT __zero = _CharT('0'),
710 _CharT __one = _CharT('1')) const;
711 template <class _CharT, class _Traits>
712 _LIBCPP_INLINE_VISIBILITY
713 basic_string<_CharT, _Traits, allocator<_CharT> > to_string(_CharT __zero = _CharT('0'),
714 _CharT __one = _CharT('1')) const;
715 template <class _CharT>
716 _LIBCPP_INLINE_VISIBILITY
717 basic_string<_CharT, char_traits<_CharT>, allocator<_CharT> > to_string(_CharT __zero = _CharT('0'),
718 _CharT __one = _CharT('1')) const;
719 _LIBCPP_INLINE_VISIBILITY
720 basic_string<char, char_traits<char>, allocator<char> > to_string(char __zero = '0',
721 char __one = '1') const;
722 _LIBCPP_INLINE_VISIBILITY
723 size_t count() const _NOEXCEPT;
724 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR size_t size() const _NOEXCEPT {return _Size;}
725 _LIBCPP_INLINE_VISIBILITY
726 bool operator==(const bitset& __rhs) const _NOEXCEPT;
727 _LIBCPP_INLINE_VISIBILITY
728 bool operator!=(const bitset& __rhs) const _NOEXCEPT;
729 bool test(size_t __pos) const;
730 _LIBCPP_INLINE_VISIBILITY
731 bool all() const _NOEXCEPT;
732 _LIBCPP_INLINE_VISIBILITY
733 bool any() const _NOEXCEPT;
734 _LIBCPP_INLINE_VISIBILITY bool none() const _NOEXCEPT {return !any();}
735 _LIBCPP_INLINE_VISIBILITY
736 bitset operator<<(size_t __pos) const _NOEXCEPT;
737 _LIBCPP_INLINE_VISIBILITY
738 bitset operator>>(size_t __pos) const _NOEXCEPT;
742 _LIBCPP_INLINE_VISIBILITY
743 size_t __hash_code() const _NOEXCEPT {return base::__hash_code();}
745 friend struct hash<bitset>;
748 template <size_t _Size>
749 template<class _CharT>
750 bitset<_Size>::bitset(const _CharT* __str,
751 typename basic_string<_CharT>::size_type __n,
752 _CharT __zero, _CharT __one)
754 size_t __rlen = _VSTD::min(__n, char_traits<_CharT>::length(__str));
755 for (size_t __i = 0; __i < __rlen; ++__i)
756 if (__str[__i] != __zero && __str[__i] != __one)
757 __throw_invalid_argument("bitset string ctor has invalid argument");
759 size_t _Mp = _VSTD::min(__rlen, _Size);
761 for (; __i < _Mp; ++__i)
763 _CharT __c = __str[_Mp - 1 - __i];
765 (*this)[__i] = false;
769 _VSTD::fill(base::__make_iter(__i), base::__make_iter(_Size), false);
772 template <size_t _Size>
773 template<class _CharT, class _Traits, class _Allocator>
774 bitset<_Size>::bitset(const basic_string<_CharT,_Traits,_Allocator>& __str,
775 typename basic_string<_CharT,_Traits,_Allocator>::size_type __pos,
776 typename basic_string<_CharT,_Traits,_Allocator>::size_type __n,
777 _CharT __zero, _CharT __one)
779 if (__pos > __str.size())
780 __throw_out_of_range("bitset string pos out of range");
782 size_t __rlen = _VSTD::min(__n, __str.size() - __pos);
783 for (size_t __i = __pos; __i < __pos + __rlen; ++__i)
784 if (!_Traits::eq(__str[__i], __zero) && !_Traits::eq(__str[__i], __one))
785 __throw_invalid_argument("bitset string ctor has invalid argument");
787 size_t _Mp = _VSTD::min(__rlen, _Size);
789 for (; __i < _Mp; ++__i)
791 _CharT __c = __str[__pos + _Mp - 1 - __i];
792 if (_Traits::eq(__c, __zero))
793 (*this)[__i] = false;
797 _VSTD::fill(base::__make_iter(__i), base::__make_iter(_Size), false);
800 template <size_t _Size>
803 bitset<_Size>::operator&=(const bitset& __rhs) _NOEXCEPT
805 base::operator&=(__rhs);
809 template <size_t _Size>
812 bitset<_Size>::operator|=(const bitset& __rhs) _NOEXCEPT
814 base::operator|=(__rhs);
818 template <size_t _Size>
821 bitset<_Size>::operator^=(const bitset& __rhs) _NOEXCEPT
823 base::operator^=(__rhs);
827 template <size_t _Size>
829 bitset<_Size>::operator<<=(size_t __pos) _NOEXCEPT
831 __pos = _VSTD::min(__pos, _Size);
832 _VSTD::copy_backward(base::__make_iter(0), base::__make_iter(_Size - __pos), base::__make_iter(_Size));
833 _VSTD::fill_n(base::__make_iter(0), __pos, false);
837 template <size_t _Size>
839 bitset<_Size>::operator>>=(size_t __pos) _NOEXCEPT
841 __pos = _VSTD::min(__pos, _Size);
842 _VSTD::copy(base::__make_iter(__pos), base::__make_iter(_Size), base::__make_iter(0));
843 _VSTD::fill_n(base::__make_iter(_Size - __pos), __pos, false);
847 template <size_t _Size>
850 bitset<_Size>::set() _NOEXCEPT
852 _VSTD::fill_n(base::__make_iter(0), _Size, true);
856 template <size_t _Size>
858 bitset<_Size>::set(size_t __pos, bool __val)
861 __throw_out_of_range("bitset set argument out of range");
863 (*this)[__pos] = __val;
867 template <size_t _Size>
870 bitset<_Size>::reset() _NOEXCEPT
872 _VSTD::fill_n(base::__make_iter(0), _Size, false);
876 template <size_t _Size>
878 bitset<_Size>::reset(size_t __pos)
881 __throw_out_of_range("bitset reset argument out of range");
883 (*this)[__pos] = false;
887 template <size_t _Size>
890 bitset<_Size>::operator~() const _NOEXCEPT
897 template <size_t _Size>
900 bitset<_Size>::flip() _NOEXCEPT
906 template <size_t _Size>
908 bitset<_Size>::flip(size_t __pos)
911 __throw_out_of_range("bitset flip argument out of range");
913 reference r = base::__make_ref(__pos);
918 template <size_t _Size>
921 bitset<_Size>::to_ulong() const
923 return base::to_ulong();
926 template <size_t _Size>
929 bitset<_Size>::to_ullong() const
931 return base::to_ullong();
934 template <size_t _Size>
935 template <class _CharT, class _Traits, class _Allocator>
936 basic_string<_CharT, _Traits, _Allocator>
937 bitset<_Size>::to_string(_CharT __zero, _CharT __one) const
939 basic_string<_CharT, _Traits, _Allocator> __r(_Size, __zero);
940 for (size_t __i = 0; __i < _Size; ++__i)
943 __r[_Size - 1 - __i] = __one;
948 template <size_t _Size>
949 template <class _CharT, class _Traits>
951 basic_string<_CharT, _Traits, allocator<_CharT> >
952 bitset<_Size>::to_string(_CharT __zero, _CharT __one) const
954 return to_string<_CharT, _Traits, allocator<_CharT> >(__zero, __one);
957 template <size_t _Size>
958 template <class _CharT>
960 basic_string<_CharT, char_traits<_CharT>, allocator<_CharT> >
961 bitset<_Size>::to_string(_CharT __zero, _CharT __one) const
963 return to_string<_CharT, char_traits<_CharT>, allocator<_CharT> >(__zero, __one);
966 template <size_t _Size>
968 basic_string<char, char_traits<char>, allocator<char> >
969 bitset<_Size>::to_string(char __zero, char __one) const
971 return to_string<char, char_traits<char>, allocator<char> >(__zero, __one);
974 template <size_t _Size>
977 bitset<_Size>::count() const _NOEXCEPT
979 return static_cast<size_t>(_VSTD::count(base::__make_iter(0), base::__make_iter(_Size), true));
982 template <size_t _Size>
985 bitset<_Size>::operator==(const bitset& __rhs) const _NOEXCEPT
987 return _VSTD::equal(base::__make_iter(0), base::__make_iter(_Size), __rhs.__make_iter(0));
990 template <size_t _Size>
993 bitset<_Size>::operator!=(const bitset& __rhs) const _NOEXCEPT
995 return !(*this == __rhs);
998 template <size_t _Size>
1000 bitset<_Size>::test(size_t __pos) const
1003 __throw_out_of_range("bitset test argument out of range");
1005 return (*this)[__pos];
1008 template <size_t _Size>
1011 bitset<_Size>::all() const _NOEXCEPT
1016 template <size_t _Size>
1019 bitset<_Size>::any() const _NOEXCEPT
1024 template <size_t _Size>
1027 bitset<_Size>::operator<<(size_t __pos) const _NOEXCEPT
1034 template <size_t _Size>
1037 bitset<_Size>::operator>>(size_t __pos) const _NOEXCEPT
1044 template <size_t _Size>
1045 inline _LIBCPP_INLINE_VISIBILITY
1047 operator&(const bitset<_Size>& __x, const bitset<_Size>& __y) _NOEXCEPT
1049 bitset<_Size> __r = __x;
1054 template <size_t _Size>
1055 inline _LIBCPP_INLINE_VISIBILITY
1057 operator|(const bitset<_Size>& __x, const bitset<_Size>& __y) _NOEXCEPT
1059 bitset<_Size> __r = __x;
1064 template <size_t _Size>
1065 inline _LIBCPP_INLINE_VISIBILITY
1067 operator^(const bitset<_Size>& __x, const bitset<_Size>& __y) _NOEXCEPT
1069 bitset<_Size> __r = __x;
1074 template <size_t _Size>
1075 struct _LIBCPP_TEMPLATE_VIS hash<bitset<_Size> >
1076 : public unary_function<bitset<_Size>, size_t>
1078 _LIBCPP_INLINE_VISIBILITY
1079 size_t operator()(const bitset<_Size>& __bs) const _NOEXCEPT
1080 {return __bs.__hash_code();}
1083 template <class _CharT, class _Traits, size_t _Size>
1084 basic_istream<_CharT, _Traits>&
1085 operator>>(basic_istream<_CharT, _Traits>& __is, bitset<_Size>& __x);
1087 template <class _CharT, class _Traits, size_t _Size>
1088 basic_ostream<_CharT, _Traits>&
1089 operator<<(basic_ostream<_CharT, _Traits>& __os, const bitset<_Size>& __x);
1091 _LIBCPP_END_NAMESPACE_STD
1093 #endif // _LIBCPP_BITSET