blob: 827a901ffd712f79b709afdfea74bc4d813bd322 [file] [log] [blame]
Howard Hinnantbc8d3f92010-05-11 19:42:161// -*- C++ -*-
2//===---------------------------- bitset ----------------------------------===//
3//
Howard Hinnantf5256e12010-05-11 21:36:014// The LLVM Compiler Infrastructure
Howard Hinnantbc8d3f92010-05-11 19:42:165//
Howard Hinnantb64f8b02010-11-16 22:09:026// This file is dual licensed under the MIT and the University of Illinois Open
7// Source Licenses. See LICENSE.TXT for details.
Howard Hinnantbc8d3f92010-05-11 19:42:168//
9//===----------------------------------------------------------------------===//
10
11#ifndef _LIBCPP_BITSET
12#define _LIBCPP_BITSET
13
14/*
15 bitset synopsis
16
17namespace std
18{
19
20namespace std {
21
22template <size_t N>
23class bitset
24{
25public:
26 // bit reference:
27 class reference
28 {
29 friend class bitset;
Howard Hinnant10f25d22011-05-27 20:52:2830 reference() noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:1631 public:
Howard Hinnant10f25d22011-05-27 20:52:2832 ~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();
Howard Hinnantbc8d3f92010-05-11 19:42:1638 };
39
40 // 23.3.5.1 constructors:
Howard Hinnant10f25d22011-05-27 20:52:2841 constexpr bitset() noexcept;
42 constexpr bitset(unsigned long long val) noexcept;
Howard Hinnant34d6b192010-11-17 21:53:1443 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'));
Howard Hinnantbc8d3f92010-05-11 19:42:1647 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:
Howard Hinnant10f25d22011-05-27 20:52:2855 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;
Howard Hinnantbc8d3f92010-05-11 19:42:1661 bitset& set(size_t pos, bool val = true);
Howard Hinnant10f25d22011-05-27 20:52:2862 bitset& reset() noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:1663 bitset& reset(size_t pos);
Howard Hinnant10f25d22011-05-27 20:52:2864 bitset operator~() const noexcept;
65 bitset& flip() noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:1666 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;
Howard Hinnant10f25d22011-05-27 20:52:2880 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;
Howard Hinnantbc8d3f92010-05-11 19:42:1684 bool test(size_t pos) const;
Howard Hinnant10f25d22011-05-27 20:52:2885 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;
Howard Hinnantbc8d3f92010-05-11 19:42:1690};
91
92// 23.3.5.3 bitset operators:
93template <size_t N>
Howard Hinnant10f25d22011-05-27 20:52:2894bitset<N> operator&(const bitset<N>&, const bitset<N>&) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:1695
96template <size_t N>
Howard Hinnant10f25d22011-05-27 20:52:2897bitset<N> operator|(const bitset<N>&, const bitset<N>&) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:1698
99template <size_t N>
Howard Hinnant10f25d22011-05-27 20:52:28100bitset<N> operator^(const bitset<N>&, const bitset<N>&) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16101
102template <class charT, class traits, size_t N>
103basic_istream<charT, traits>&
104operator>>(basic_istream<charT, traits>& is, bitset<N>& x);
105
106template <class charT, class traits, size_t N>
107basic_ostream<charT, traits>&
108operator<<(basic_ostream<charT, traits>& os, const bitset<N>& x);
109
110template <size_t N> struct hash<std::bitset<N>>;
111
112} // std
113
114*/
115
Howard Hinnant08e17472011-10-17 20:05:10116#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantbc8d3f92010-05-11 19:42:16117#pragma GCC system_header
Howard Hinnant08e17472011-10-17 20:05:10118#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16119
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>
Howard Hinnantbc8d3f92010-05-11 19:42:16128
Howard Hinnant66c6f972011-11-29 16:45:27129#include <__undef_min_max>
130
Howard Hinnantbc8d3f92010-05-11 19:42:16131_LIBCPP_BEGIN_NAMESPACE_STD
132
133template <size_t _N_words, size_t _Size>
Howard Hinnantf03c3b42011-07-02 20:33:23134class __bitset;
135
136template <size_t _N_words, size_t _Size>
137struct __has_storage_type<__bitset<_N_words, _Size> >
138{
139 static const bool value = true;
140};
141
142template <size_t _N_words, size_t _Size>
Howard Hinnantbc8d3f92010-05-11 19:42:16143class __bitset
144{
145public:
146 typedef ptrdiff_t difference_type;
147 typedef size_t size_type;
Howard Hinnantf867f632012-05-07 16:50:38148 typedef size_type __storage_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16149protected:
150 typedef __bitset __self;
Howard Hinnantbc8d3f92010-05-11 19:42:16151 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);
154
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>;
Howard Hinnant4ae952a2012-08-17 17:10:18159 friend struct __bit_array<__bitset>;
Howard Hinnantbc8d3f92010-05-11 19:42:16160
161 __storage_type __first_[_N_words];
162
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;
167
Evgeniy Stepanova3b25f82015-11-07 01:22:13168 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant90d87232012-07-07 17:04:52169 _LIBCPP_CONSTEXPR __bitset() _NOEXCEPT;
Evgeniy Stepanova3b25f82015-11-07 01:22:13170 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant90d87232012-07-07 17:04:52171 explicit _LIBCPP_CONSTEXPR __bitset(unsigned long long __v) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16172
Howard Hinnant10f25d22011-05-27 20:52:28173 _LIBCPP_INLINE_VISIBILITY reference __make_ref(size_t __pos) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16174 {return reference(__first_ + __pos / __bits_per_word, __storage_type(1) << __pos % __bits_per_word);}
Howard Hinnant90d87232012-07-07 17:04:52175 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR const_reference __make_ref(size_t __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16176 {return const_reference(__first_ + __pos / __bits_per_word, __storage_type(1) << __pos % __bits_per_word);}
Howard Hinnant10f25d22011-05-27 20:52:28177 _LIBCPP_INLINE_VISIBILITY iterator __make_iter(size_t __pos) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16178 {return iterator(__first_ + __pos / __bits_per_word, __pos % __bits_per_word);}
Howard Hinnant10f25d22011-05-27 20:52:28179 _LIBCPP_INLINE_VISIBILITY const_iterator __make_iter(size_t __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16180 {return const_iterator(__first_ + __pos / __bits_per_word, __pos % __bits_per_word);}
181
Evgeniy Stepanova3b25f82015-11-07 01:22:13182 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant10f25d22011-05-27 20:52:28183 void operator&=(const __bitset& __v) _NOEXCEPT;
Evgeniy Stepanova3b25f82015-11-07 01:22:13184 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant10f25d22011-05-27 20:52:28185 void operator|=(const __bitset& __v) _NOEXCEPT;
Evgeniy Stepanova3b25f82015-11-07 01:22:13186 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant10f25d22011-05-27 20:52:28187 void operator^=(const __bitset& __v) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16188
Howard Hinnant10f25d22011-05-27 20:52:28189 void flip() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16190 _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>());}
194
Howard Hinnant10f25d22011-05-27 20:52:28195 bool all() const _NOEXCEPT;
196 bool any() const _NOEXCEPT;
Evgeniy Stepanova3b25f82015-11-07 01:22:13197 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant10f25d22011-05-27 20:52:28198 size_t __hash_code() const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16199private:
Howard Hinnant90d87232012-07-07 17:04:52200#ifdef _LIBCPP_HAS_NO_CONSTEXPR
Howard Hinnant10f25d22011-05-27 20:52:28201 void __init(unsigned long long __v, false_type) _NOEXCEPT;
Evgeniy Stepanov28c02db2015-12-09 22:32:36202 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant10f25d22011-05-27 20:52:28203 void __init(unsigned long long __v, true_type) _NOEXCEPT;
Howard Hinnant90d87232012-07-07 17:04:52204#endif // _LIBCPP_HAS_NO_CONSTEXPR
Howard Hinnantbc8d3f92010-05-11 19:42:16205 unsigned long to_ulong(false_type) const;
Evgeniy Stepanova3b25f82015-11-07 01:22:13206 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16207 unsigned long to_ulong(true_type) const;
208 unsigned long long to_ullong(false_type) const;
Evgeniy Stepanova3b25f82015-11-07 01:22:13209 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16210 unsigned long long to_ullong(true_type) const;
Evgeniy Stepanova3b25f82015-11-07 01:22:13211 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16212 unsigned long long to_ullong(true_type, false_type) const;
213 unsigned long long to_ullong(true_type, true_type) const;
214};
215
216template <size_t _N_words, size_t _Size>
Evgeniy Stepanova3b25f82015-11-07 01:22:13217inline
Howard Hinnant90d87232012-07-07 17:04:52218_LIBCPP_CONSTEXPR
Howard Hinnant10f25d22011-05-27 20:52:28219__bitset<_N_words, _Size>::__bitset() _NOEXCEPT
Howard Hinnant90d87232012-07-07 17:04:52220#ifndef _LIBCPP_HAS_NO_CONSTEXPR
221 : __first_{0}
222#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16223{
Howard Hinnant90d87232012-07-07 17:04:52224#ifdef _LIBCPP_HAS_NO_CONSTEXPR
Howard Hinnant0949eed2011-06-30 21:18:19225 _VSTD::fill_n(__first_, _N_words, __storage_type(0));
Howard Hinnant90d87232012-07-07 17:04:52226#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16227}
228
Howard Hinnant90d87232012-07-07 17:04:52229#ifdef _LIBCPP_HAS_NO_CONSTEXPR
230
Howard Hinnantbc8d3f92010-05-11 19:42:16231template <size_t _N_words, size_t _Size>
232void
Howard Hinnantec3773c2011-12-01 20:21:04233__bitset<_N_words, _Size>::__init(unsigned long long __v, false_type) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16234{
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);
Howard Hinnant0949eed2011-06-30 21:18:19238 _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]),
Howard Hinnantbc8d3f92010-05-11 19:42:16240 __storage_type(0));
241}
242
243template <size_t _N_words, size_t _Size>
244inline _LIBCPP_INLINE_VISIBILITY
245void
Howard Hinnantec3773c2011-12-01 20:21:04246__bitset<_N_words, _Size>::__init(unsigned long long __v, true_type) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16247{
248 __first_[0] = __v;
Howard Hinnant0949eed2011-06-30 21:18:19249 _VSTD::fill(__first_ + 1, __first_ + sizeof(__first_)/sizeof(__first_[0]), __storage_type(0));
Howard Hinnantbc8d3f92010-05-11 19:42:16250}
251
Howard Hinnant90d87232012-07-07 17:04:52252#endif // _LIBCPP_HAS_NO_CONSTEXPR
253
Howard Hinnantbc8d3f92010-05-11 19:42:16254template <size_t _N_words, size_t _Size>
Evgeniy Stepanova3b25f82015-11-07 01:22:13255inline
Howard Hinnant90d87232012-07-07 17:04:52256_LIBCPP_CONSTEXPR
Howard Hinnant10f25d22011-05-27 20:52:28257__bitset<_N_words, _Size>::__bitset(unsigned long long __v) _NOEXCEPT
Howard Hinnant90d87232012-07-07 17:04:52258#ifndef _LIBCPP_HAS_NO_CONSTEXPR
Nico Weber0211e862014-06-04 15:46:56259#if __SIZEOF_SIZE_T__ == 8
Howard Hinnant90d87232012-07-07 17:04:52260 : __first_{__v}
Nico Weber0211e862014-06-04 15:46:56261#elif __SIZEOF_SIZE_T__ == 4
Dimitry Andric5f8cb582016-09-02 21:02:11262 : __first_{static_cast<__storage_type>(__v), static_cast<__storage_type>(__v >> __bits_per_word)}
Howard Hinnant31014742013-03-06 18:16:12263#else
Howard Hinnant11a31d02013-03-06 17:30:26264#error This constructor has not been ported to this platform
265#endif
Howard Hinnant90d87232012-07-07 17:04:52266#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16267{
Howard Hinnant90d87232012-07-07 17:04:52268#ifdef _LIBCPP_HAS_NO_CONSTEXPR
Howard Hinnantbc8d3f92010-05-11 19:42:16269 __init(__v, integral_constant<bool, sizeof(unsigned long long) == sizeof(__storage_type)>());
Howard Hinnant90d87232012-07-07 17:04:52270#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16271}
272
273template <size_t _N_words, size_t _Size>
Evgeniy Stepanova3b25f82015-11-07 01:22:13274inline
Howard Hinnantbc8d3f92010-05-11 19:42:16275void
Howard Hinnant10f25d22011-05-27 20:52:28276__bitset<_N_words, _Size>::operator&=(const __bitset& __v) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16277{
278 for (size_type __i = 0; __i < _N_words; ++__i)
279 __first_[__i] &= __v.__first_[__i];
280}
281
282template <size_t _N_words, size_t _Size>
Evgeniy Stepanova3b25f82015-11-07 01:22:13283inline
Howard Hinnantbc8d3f92010-05-11 19:42:16284void
Howard Hinnant10f25d22011-05-27 20:52:28285__bitset<_N_words, _Size>::operator|=(const __bitset& __v) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16286{
287 for (size_type __i = 0; __i < _N_words; ++__i)
288 __first_[__i] |= __v.__first_[__i];
289}
290
291template <size_t _N_words, size_t _Size>
Evgeniy Stepanova3b25f82015-11-07 01:22:13292inline
Howard Hinnantbc8d3f92010-05-11 19:42:16293void
Howard Hinnant10f25d22011-05-27 20:52:28294__bitset<_N_words, _Size>::operator^=(const __bitset& __v) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16295{
296 for (size_type __i = 0; __i < _N_words; ++__i)
297 __first_[__i] ^= __v.__first_[__i];
298}
299
300template <size_t _N_words, size_t _Size>
301void
Howard Hinnant10f25d22011-05-27 20:52:28302__bitset<_N_words, _Size>::flip() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16303{
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)
308 *__p = ~*__p;
309 // do last partial word
310 if (__n > 0)
311 {
312 __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
313 __storage_type __b = *__p & __m;
314 *__p &= ~__m;
315 *__p |= ~__b & __m;
316 }
317}
318
319template <size_t _N_words, size_t _Size>
320unsigned long
321__bitset<_N_words, _Size>::to_ulong(false_type) const
322{
323 const_iterator __e = __make_iter(_Size);
Howard Hinnant0949eed2011-06-30 21:18:19324 const_iterator __i = _VSTD::find(__make_iter(sizeof(unsigned long) * CHAR_BIT), __e, true);
Howard Hinnantbc8d3f92010-05-11 19:42:16325 if (__i != __e)
Marshall Clow14c09a22016-08-25 15:09:01326 __throw_overflow_error("bitset to_ulong overflow error");
327
Howard Hinnantbc8d3f92010-05-11 19:42:16328 return __first_[0];
329}
330
331template <size_t _N_words, size_t _Size>
Evgeniy Stepanova3b25f82015-11-07 01:22:13332inline
Howard Hinnantbc8d3f92010-05-11 19:42:16333unsigned long
334__bitset<_N_words, _Size>::to_ulong(true_type) const
335{
336 return __first_[0];
337}
338
339template <size_t _N_words, size_t _Size>
340unsigned long long
341__bitset<_N_words, _Size>::to_ullong(false_type) const
342{
343 const_iterator __e = __make_iter(_Size);
Howard Hinnant0949eed2011-06-30 21:18:19344 const_iterator __i = _VSTD::find(__make_iter(sizeof(unsigned long long) * CHAR_BIT), __e, true);
Howard Hinnantbc8d3f92010-05-11 19:42:16345 if (__i != __e)
Marshall Clow14c09a22016-08-25 15:09:01346 __throw_overflow_error("bitset to_ullong overflow error");
347
Howard Hinnantbc8d3f92010-05-11 19:42:16348 return to_ullong(true_type());
349}
350
351template <size_t _N_words, size_t _Size>
Evgeniy Stepanova3b25f82015-11-07 01:22:13352inline
Howard Hinnantbc8d3f92010-05-11 19:42:16353unsigned long long
354__bitset<_N_words, _Size>::to_ullong(true_type) const
355{
356 return to_ullong(true_type(), integral_constant<bool, sizeof(__storage_type) < sizeof(unsigned long long)>());
357}
358
359template <size_t _N_words, size_t _Size>
Evgeniy Stepanova3b25f82015-11-07 01:22:13360inline
Howard Hinnantbc8d3f92010-05-11 19:42:16361unsigned long long
362__bitset<_N_words, _Size>::to_ullong(true_type, false_type) const
363{
364 return __first_[0];
365}
366
367template <size_t _N_words, size_t _Size>
368unsigned long long
369__bitset<_N_words, _Size>::to_ullong(true_type, true_type) const
370{
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);
374 return __r;
375}
376
377template <size_t _N_words, size_t _Size>
378bool
Howard Hinnant10f25d22011-05-27 20:52:28379__bitset<_N_words, _Size>::all() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16380{
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)
385 if (~*__p)
386 return false;
387 // do last partial word
388 if (__n > 0)
389 {
390 __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
391 if (~*__p & __m)
392 return false;
393 }
394 return true;
395}
396
397template <size_t _N_words, size_t _Size>
398bool
Howard Hinnant10f25d22011-05-27 20:52:28399__bitset<_N_words, _Size>::any() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16400{
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)
405 if (*__p)
406 return true;
407 // do last partial word
408 if (__n > 0)
409 {
410 __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
411 if (*__p & __m)
412 return true;
413 }
414 return false;
415}
416
417template <size_t _N_words, size_t _Size>
Evgeniy Stepanova3b25f82015-11-07 01:22:13418inline
Howard Hinnantbc8d3f92010-05-11 19:42:16419size_t
Howard Hinnant10f25d22011-05-27 20:52:28420__bitset<_N_words, _Size>::__hash_code() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16421{
422 size_t __h = 0;
423 for (size_type __i = 0; __i < _N_words; ++__i)
424 __h ^= __first_[__i];
425 return __h;
426}
427
428template <size_t _Size>
429class __bitset<1, _Size>
430{
431public:
432 typedef ptrdiff_t difference_type;
433 typedef size_t size_type;
Howard Hinnantf867f632012-05-07 16:50:38434 typedef size_type __storage_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16435protected:
436 typedef __bitset __self;
Howard Hinnantbc8d3f92010-05-11 19:42:16437 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);
440
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>;
Howard Hinnant4ae952a2012-08-17 17:10:18445 friend struct __bit_array<__bitset>;
Howard Hinnantbc8d3f92010-05-11 19:42:16446
447 __storage_type __first_;
448
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;
453
Evgeniy Stepanova3b25f82015-11-07 01:22:13454 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant90d87232012-07-07 17:04:52455 _LIBCPP_CONSTEXPR __bitset() _NOEXCEPT;
Evgeniy Stepanova3b25f82015-11-07 01:22:13456 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant90d87232012-07-07 17:04:52457 explicit _LIBCPP_CONSTEXPR __bitset(unsigned long long __v) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16458
Howard Hinnant10f25d22011-05-27 20:52:28459 _LIBCPP_INLINE_VISIBILITY reference __make_ref(size_t __pos) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16460 {return reference(&__first_, __storage_type(1) << __pos);}
Howard Hinnant90d87232012-07-07 17:04:52461 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR const_reference __make_ref(size_t __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16462 {return const_reference(&__first_, __storage_type(1) << __pos);}
Howard Hinnant10f25d22011-05-27 20:52:28463 _LIBCPP_INLINE_VISIBILITY iterator __make_iter(size_t __pos) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16464 {return iterator(&__first_ + __pos / __bits_per_word, __pos % __bits_per_word);}
Howard Hinnant10f25d22011-05-27 20:52:28465 _LIBCPP_INLINE_VISIBILITY const_iterator __make_iter(size_t __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16466 {return const_iterator(&__first_ + __pos / __bits_per_word, __pos % __bits_per_word);}
467
Evgeniy Stepanova3b25f82015-11-07 01:22:13468 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant10f25d22011-05-27 20:52:28469 void operator&=(const __bitset& __v) _NOEXCEPT;
Evgeniy Stepanova3b25f82015-11-07 01:22:13470 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant10f25d22011-05-27 20:52:28471 void operator|=(const __bitset& __v) _NOEXCEPT;
Evgeniy Stepanova3b25f82015-11-07 01:22:13472 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant10f25d22011-05-27 20:52:28473 void operator^=(const __bitset& __v) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16474
Evgeniy Stepanova3b25f82015-11-07 01:22:13475 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant10f25d22011-05-27 20:52:28476 void flip() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16477
Evgeniy Stepanova3b25f82015-11-07 01:22:13478 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16479 unsigned long to_ulong() const;
Evgeniy Stepanova3b25f82015-11-07 01:22:13480 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16481 unsigned long long to_ullong() const;
482
Evgeniy Stepanova3b25f82015-11-07 01:22:13483 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant10f25d22011-05-27 20:52:28484 bool all() const _NOEXCEPT;
Evgeniy Stepanova3b25f82015-11-07 01:22:13485 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant10f25d22011-05-27 20:52:28486 bool any() const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16487
Evgeniy Stepanova3b25f82015-11-07 01:22:13488 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant10f25d22011-05-27 20:52:28489 size_t __hash_code() const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16490};
491
492template <size_t _Size>
Evgeniy Stepanova3b25f82015-11-07 01:22:13493inline
Howard Hinnant90d87232012-07-07 17:04:52494_LIBCPP_CONSTEXPR
Howard Hinnant10f25d22011-05-27 20:52:28495__bitset<1, _Size>::__bitset() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16496 : __first_(0)
497{
498}
499
500template <size_t _Size>
Evgeniy Stepanova3b25f82015-11-07 01:22:13501inline
Howard Hinnant90d87232012-07-07 17:04:52502_LIBCPP_CONSTEXPR
Howard Hinnant10f25d22011-05-27 20:52:28503__bitset<1, _Size>::__bitset(unsigned long long __v) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16504 : __first_(static_cast<__storage_type>(__v))
505{
506}
507
508template <size_t _Size>
Evgeniy Stepanova3b25f82015-11-07 01:22:13509inline
Howard Hinnantbc8d3f92010-05-11 19:42:16510void
Howard Hinnant10f25d22011-05-27 20:52:28511__bitset<1, _Size>::operator&=(const __bitset& __v) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16512{
513 __first_ &= __v.__first_;
514}
515
516template <size_t _Size>
Evgeniy Stepanova3b25f82015-11-07 01:22:13517inline
Howard Hinnantbc8d3f92010-05-11 19:42:16518void
Howard Hinnant10f25d22011-05-27 20:52:28519__bitset<1, _Size>::operator|=(const __bitset& __v) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16520{
521 __first_ |= __v.__first_;
522}
523
524template <size_t _Size>
Evgeniy Stepanova3b25f82015-11-07 01:22:13525inline
Howard Hinnantbc8d3f92010-05-11 19:42:16526void
Howard Hinnant10f25d22011-05-27 20:52:28527__bitset<1, _Size>::operator^=(const __bitset& __v) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16528{
529 __first_ ^= __v.__first_;
530}
531
532template <size_t _Size>
Evgeniy Stepanova3b25f82015-11-07 01:22:13533inline
Howard Hinnantbc8d3f92010-05-11 19:42:16534void
Howard Hinnant10f25d22011-05-27 20:52:28535__bitset<1, _Size>::flip() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16536{
537 __storage_type __m = ~__storage_type(0) >> (__bits_per_word - _Size);
538 __first_ = ~__first_;
539 __first_ &= __m;
540}
541
542template <size_t _Size>
Evgeniy Stepanova3b25f82015-11-07 01:22:13543inline
Howard Hinnantbc8d3f92010-05-11 19:42:16544unsigned long
545__bitset<1, _Size>::to_ulong() const
546{
547 return __first_;
548}
549
550template <size_t _Size>
Evgeniy Stepanova3b25f82015-11-07 01:22:13551inline
Howard Hinnantbc8d3f92010-05-11 19:42:16552unsigned long long
553__bitset<1, _Size>::to_ullong() const
554{
555 return __first_;
556}
557
558template <size_t _Size>
Evgeniy Stepanova3b25f82015-11-07 01:22:13559inline
Howard Hinnantbc8d3f92010-05-11 19:42:16560bool
Howard Hinnant10f25d22011-05-27 20:52:28561__bitset<1, _Size>::all() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16562{
563 __storage_type __m = ~__storage_type(0) >> (__bits_per_word - _Size);
564 return !(~__first_ & __m);
565}
566
567template <size_t _Size>
Evgeniy Stepanova3b25f82015-11-07 01:22:13568inline
Howard Hinnantbc8d3f92010-05-11 19:42:16569bool
Howard Hinnant10f25d22011-05-27 20:52:28570__bitset<1, _Size>::any() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16571{
572 __storage_type __m = ~__storage_type(0) >> (__bits_per_word - _Size);
573 return __first_ & __m;
574}
575
576template <size_t _Size>
Evgeniy Stepanova3b25f82015-11-07 01:22:13577inline
Howard Hinnantbc8d3f92010-05-11 19:42:16578size_t
Howard Hinnant10f25d22011-05-27 20:52:28579__bitset<1, _Size>::__hash_code() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16580{
581 return __first_;
582}
583
584template <>
585class __bitset<0, 0>
586{
587public:
588 typedef ptrdiff_t difference_type;
589 typedef size_t size_type;
Howard Hinnantf867f632012-05-07 16:50:38590 typedef size_type __storage_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16591protected:
592 typedef __bitset __self;
Howard Hinnantbc8d3f92010-05-11 19:42:16593 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);
596
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>;
Howard Hinnantec3773c2011-12-01 20:21:04601 friend struct __bit_array<__bitset>;
Howard Hinnantbc8d3f92010-05-11 19:42:16602
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;
607
Evgeniy Stepanova3b25f82015-11-07 01:22:13608 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant90d87232012-07-07 17:04:52609 _LIBCPP_CONSTEXPR __bitset() _NOEXCEPT;
Evgeniy Stepanova3b25f82015-11-07 01:22:13610 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant90d87232012-07-07 17:04:52611 explicit _LIBCPP_CONSTEXPR __bitset(unsigned long long) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16612
Howard Hinnant10f25d22011-05-27 20:52:28613 _LIBCPP_INLINE_VISIBILITY reference __make_ref(size_t) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16614 {return reference(0, 1);}
Howard Hinnant90d87232012-07-07 17:04:52615 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR const_reference __make_ref(size_t) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16616 {return const_reference(0, 1);}
Howard Hinnantec3773c2011-12-01 20:21:04617 _LIBCPP_INLINE_VISIBILITY iterator __make_iter(size_t) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16618 {return iterator(0, 0);}
Howard Hinnantec3773c2011-12-01 20:21:04619 _LIBCPP_INLINE_VISIBILITY const_iterator __make_iter(size_t) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16620 {return const_iterator(0, 0);}
621
Howard Hinnant10f25d22011-05-27 20:52:28622 _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 {}
Howard Hinnantbc8d3f92010-05-11 19:42:16625
Howard Hinnant10f25d22011-05-27 20:52:28626 _LIBCPP_INLINE_VISIBILITY void flip() _NOEXCEPT {}
Howard Hinnantbc8d3f92010-05-11 19:42:16627
628 _LIBCPP_INLINE_VISIBILITY unsigned long to_ulong() const {return 0;}
629 _LIBCPP_INLINE_VISIBILITY unsigned long long to_ullong() const {return 0;}
630
Howard Hinnant10f25d22011-05-27 20:52:28631 _LIBCPP_INLINE_VISIBILITY bool all() const _NOEXCEPT {return true;}
632 _LIBCPP_INLINE_VISIBILITY bool any() const _NOEXCEPT {return false;}
Howard Hinnantbc8d3f92010-05-11 19:42:16633
Howard Hinnant10f25d22011-05-27 20:52:28634 _LIBCPP_INLINE_VISIBILITY size_t __hash_code() const _NOEXCEPT {return 0;}
Howard Hinnantbc8d3f92010-05-11 19:42:16635};
636
Evgeniy Stepanova3b25f82015-11-07 01:22:13637inline
Howard Hinnant90d87232012-07-07 17:04:52638_LIBCPP_CONSTEXPR
Howard Hinnant10f25d22011-05-27 20:52:28639__bitset<0, 0>::__bitset() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16640{
641}
642
Evgeniy Stepanova3b25f82015-11-07 01:22:13643inline
Howard Hinnant90d87232012-07-07 17:04:52644_LIBCPP_CONSTEXPR
Howard Hinnant10f25d22011-05-27 20:52:28645__bitset<0, 0>::__bitset(unsigned long long) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16646{
647}
648
Eric Fiselierc3589a82017-01-04 23:56:00649template <size_t _Size> class _LIBCPP_TEMPLATE_VIS bitset;
Eric Fiselier566bcb42016-04-21 22:54:21650template <size_t _Size> struct hash<bitset<_Size> >;
Howard Hinnantbc8d3f92010-05-11 19:42:16651
652template <size_t _Size>
Eric Fiselierc3589a82017-01-04 23:56:00653class _LIBCPP_TEMPLATE_VIS bitset
Howard Hinnantbc8d3f92010-05-11 19:42:16654 : private __bitset<_Size == 0 ? 0 : (_Size - 1) / (sizeof(size_t) * CHAR_BIT) + 1, _Size>
655{
Howard Hinnant11a31d02013-03-06 17:30:26656public:
Howard Hinnantbc8d3f92010-05-11 19:42:16657 static const unsigned __n_words = _Size == 0 ? 0 : (_Size - 1) / (sizeof(size_t) * CHAR_BIT) + 1;
658 typedef __bitset<__n_words, _Size> base;
659
Howard Hinnant324bb032010-08-22 00:02:43660public:
Howard Hinnantbc8d3f92010-05-11 19:42:16661 typedef typename base::reference reference;
662 typedef typename base::const_reference const_reference;
663
Howard Hinnant324bb032010-08-22 00:02:43664 // 23.3.5.1 constructors:
Howard Hinnant90d87232012-07-07 17:04:52665 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR bitset() _NOEXCEPT {}
666 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
667 bitset(unsigned long long __v) _NOEXCEPT : base(__v) {}
Howard Hinnant34d6b192010-11-17 21:53:14668 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'));
Howard Hinnant324bb032010-08-22 00:02:43672 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 =
Howard Hinnantbc8d3f92010-05-11 19:42:16676 (basic_string<_CharT,_Traits,_Allocator>::npos),
Howard Hinnant324bb032010-08-22 00:02:43677 _CharT __zero = _CharT('0'), _CharT __one = _CharT('1'));
Howard Hinnantbc8d3f92010-05-11 19:42:16678
Howard Hinnant324bb032010-08-22 00:02:43679 // 23.3.5.2 bitset operations:
Evgeniy Stepanova3b25f82015-11-07 01:22:13680 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant10f25d22011-05-27 20:52:28681 bitset& operator&=(const bitset& __rhs) _NOEXCEPT;
Evgeniy Stepanova3b25f82015-11-07 01:22:13682 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant10f25d22011-05-27 20:52:28683 bitset& operator|=(const bitset& __rhs) _NOEXCEPT;
Evgeniy Stepanova3b25f82015-11-07 01:22:13684 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant10f25d22011-05-27 20:52:28685 bitset& operator^=(const bitset& __rhs) _NOEXCEPT;
686 bitset& operator<<=(size_t __pos) _NOEXCEPT;
687 bitset& operator>>=(size_t __pos) _NOEXCEPT;
Evgeniy Stepanova3b25f82015-11-07 01:22:13688 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant10f25d22011-05-27 20:52:28689 bitset& set() _NOEXCEPT;
Howard Hinnant324bb032010-08-22 00:02:43690 bitset& set(size_t __pos, bool __val = true);
Evgeniy Stepanova3b25f82015-11-07 01:22:13691 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant10f25d22011-05-27 20:52:28692 bitset& reset() _NOEXCEPT;
Howard Hinnant324bb032010-08-22 00:02:43693 bitset& reset(size_t __pos);
Evgeniy Stepanova3b25f82015-11-07 01:22:13694 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant10f25d22011-05-27 20:52:28695 bitset operator~() const _NOEXCEPT;
Evgeniy Stepanova3b25f82015-11-07 01:22:13696 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant10f25d22011-05-27 20:52:28697 bitset& flip() _NOEXCEPT;
Howard Hinnant324bb032010-08-22 00:02:43698 bitset& flip(size_t __pos);
Howard Hinnantbc8d3f92010-05-11 19:42:16699
Howard Hinnant324bb032010-08-22 00:02:43700 // element access:
Howard Hinnant90d87232012-07-07 17:04:52701 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
702 const_reference operator[](size_t __p) const {return base::__make_ref(__p);}
Howard Hinnantbc8d3f92010-05-11 19:42:16703 _LIBCPP_INLINE_VISIBILITY reference operator[](size_t __p) {return base::__make_ref(__p);}
Evgeniy Stepanova3b25f82015-11-07 01:22:13704 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant324bb032010-08-22 00:02:43705 unsigned long to_ulong() const;
Evgeniy Stepanova3b25f82015-11-07 01:22:13706 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant324bb032010-08-22 00:02:43707 unsigned long long to_ullong() const;
708 template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16709 basic_string<_CharT, _Traits, _Allocator> to_string(_CharT __zero = _CharT('0'),
Howard Hinnant324bb032010-08-22 00:02:43710 _CharT __one = _CharT('1')) const;
711 template <class _CharT, class _Traits>
Evgeniy Stepanova3b25f82015-11-07 01:22:13712 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16713 basic_string<_CharT, _Traits, allocator<_CharT> > to_string(_CharT __zero = _CharT('0'),
Howard Hinnant324bb032010-08-22 00:02:43714 _CharT __one = _CharT('1')) const;
715 template <class _CharT>
Evgeniy Stepanova3b25f82015-11-07 01:22:13716 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16717 basic_string<_CharT, char_traits<_CharT>, allocator<_CharT> > to_string(_CharT __zero = _CharT('0'),
Howard Hinnant324bb032010-08-22 00:02:43718 _CharT __one = _CharT('1')) const;
Evgeniy Stepanova3b25f82015-11-07 01:22:13719 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16720 basic_string<char, char_traits<char>, allocator<char> > to_string(char __zero = '0',
Howard Hinnant324bb032010-08-22 00:02:43721 char __one = '1') const;
Evgeniy Stepanova3b25f82015-11-07 01:22:13722 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant10f25d22011-05-27 20:52:28723 size_t count() const _NOEXCEPT;
Howard Hinnant90d87232012-07-07 17:04:52724 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR size_t size() const _NOEXCEPT {return _Size;}
Evgeniy Stepanova3b25f82015-11-07 01:22:13725 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant10f25d22011-05-27 20:52:28726 bool operator==(const bitset& __rhs) const _NOEXCEPT;
Evgeniy Stepanova3b25f82015-11-07 01:22:13727 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant10f25d22011-05-27 20:52:28728 bool operator!=(const bitset& __rhs) const _NOEXCEPT;
Howard Hinnant324bb032010-08-22 00:02:43729 bool test(size_t __pos) const;
Evgeniy Stepanova3b25f82015-11-07 01:22:13730 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant10f25d22011-05-27 20:52:28731 bool all() const _NOEXCEPT;
Evgeniy Stepanova3b25f82015-11-07 01:22:13732 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant10f25d22011-05-27 20:52:28733 bool any() const _NOEXCEPT;
734 _LIBCPP_INLINE_VISIBILITY bool none() const _NOEXCEPT {return !any();}
Evgeniy Stepanova3b25f82015-11-07 01:22:13735 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant10f25d22011-05-27 20:52:28736 bitset operator<<(size_t __pos) const _NOEXCEPT;
Evgeniy Stepanova3b25f82015-11-07 01:22:13737 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant10f25d22011-05-27 20:52:28738 bitset operator>>(size_t __pos) const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16739
740private:
741
Howard Hinnant422a53f2010-09-21 21:28:23742 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant10f25d22011-05-27 20:52:28743 size_t __hash_code() const _NOEXCEPT {return base::__hash_code();}
Howard Hinnantbc8d3f92010-05-11 19:42:16744
745 friend struct hash<bitset>;
746};
747
748template <size_t _Size>
Howard Hinnant34d6b192010-11-17 21:53:14749template<class _CharT>
750bitset<_Size>::bitset(const _CharT* __str,
751 typename basic_string<_CharT>::size_type __n,
752 _CharT __zero, _CharT __one)
Howard Hinnantbc8d3f92010-05-11 19:42:16753{
Howard Hinnant0949eed2011-06-30 21:18:19754 size_t __rlen = _VSTD::min(__n, char_traits<_CharT>::length(__str));
Howard Hinnantbc8d3f92010-05-11 19:42:16755 for (size_t __i = 0; __i < __rlen; ++__i)
Howard Hinnant34d6b192010-11-17 21:53:14756 if (__str[__i] != __zero && __str[__i] != __one)
Marshall Clow14c09a22016-08-25 15:09:01757 __throw_invalid_argument("bitset string ctor has invalid argument");
758
Howard Hinnant99968442011-11-29 18:15:50759 size_t _Mp = _VSTD::min(__rlen, _Size);
Howard Hinnantbc8d3f92010-05-11 19:42:16760 size_t __i = 0;
Howard Hinnant99968442011-11-29 18:15:50761 for (; __i < _Mp; ++__i)
Howard Hinnantbc8d3f92010-05-11 19:42:16762 {
Howard Hinnant99968442011-11-29 18:15:50763 _CharT __c = __str[_Mp - 1 - __i];
Howard Hinnant34d6b192010-11-17 21:53:14764 if (__c == __zero)
Howard Hinnantbc8d3f92010-05-11 19:42:16765 (*this)[__i] = false;
Howard Hinnant34d6b192010-11-17 21:53:14766 else
Howard Hinnantbc8d3f92010-05-11 19:42:16767 (*this)[__i] = true;
Howard Hinnantbc8d3f92010-05-11 19:42:16768 }
Howard Hinnant0949eed2011-06-30 21:18:19769 _VSTD::fill(base::__make_iter(__i), base::__make_iter(_Size), false);
Howard Hinnantbc8d3f92010-05-11 19:42:16770}
771
772template <size_t _Size>
Howard Hinnant324bb032010-08-22 00:02:43773template<class _CharT, class _Traits, class _Allocator>
774bitset<_Size>::bitset(const basic_string<_CharT,_Traits,_Allocator>& __str,
775 typename basic_string<_CharT,_Traits,_Allocator>::size_type __pos,
Howard Hinnantbc8d3f92010-05-11 19:42:16776 typename basic_string<_CharT,_Traits,_Allocator>::size_type __n,
777 _CharT __zero, _CharT __one)
778{
779 if (__pos > __str.size())
Marshall Clow14c09a22016-08-25 15:09:01780 __throw_out_of_range("bitset string pos out of range");
781
Howard Hinnant0949eed2011-06-30 21:18:19782 size_t __rlen = _VSTD::min(__n, __str.size() - __pos);
Howard Hinnantbc8d3f92010-05-11 19:42:16783 for (size_t __i = __pos; __i < __pos + __rlen; ++__i)
784 if (!_Traits::eq(__str[__i], __zero) && !_Traits::eq(__str[__i], __one))
Marshall Clow14c09a22016-08-25 15:09:01785 __throw_invalid_argument("bitset string ctor has invalid argument");
786
Howard Hinnant99968442011-11-29 18:15:50787 size_t _Mp = _VSTD::min(__rlen, _Size);
Howard Hinnantbc8d3f92010-05-11 19:42:16788 size_t __i = 0;
Howard Hinnant99968442011-11-29 18:15:50789 for (; __i < _Mp; ++__i)
Howard Hinnantbc8d3f92010-05-11 19:42:16790 {
Howard Hinnant99968442011-11-29 18:15:50791 _CharT __c = __str[__pos + _Mp - 1 - __i];
Howard Hinnantbc8d3f92010-05-11 19:42:16792 if (_Traits::eq(__c, __zero))
793 (*this)[__i] = false;
794 else
795 (*this)[__i] = true;
796 }
Howard Hinnant0949eed2011-06-30 21:18:19797 _VSTD::fill(base::__make_iter(__i), base::__make_iter(_Size), false);
Howard Hinnantbc8d3f92010-05-11 19:42:16798}
799
800template <size_t _Size>
Evgeniy Stepanova3b25f82015-11-07 01:22:13801inline
Howard Hinnantbc8d3f92010-05-11 19:42:16802bitset<_Size>&
Howard Hinnant10f25d22011-05-27 20:52:28803bitset<_Size>::operator&=(const bitset& __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16804{
805 base::operator&=(__rhs);
806 return *this;
807}
808
809template <size_t _Size>
Evgeniy Stepanova3b25f82015-11-07 01:22:13810inline
Howard Hinnantbc8d3f92010-05-11 19:42:16811bitset<_Size>&
Howard Hinnant10f25d22011-05-27 20:52:28812bitset<_Size>::operator|=(const bitset& __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16813{
814 base::operator|=(__rhs);
815 return *this;
816}
817
818template <size_t _Size>
Evgeniy Stepanova3b25f82015-11-07 01:22:13819inline
Howard Hinnantbc8d3f92010-05-11 19:42:16820bitset<_Size>&
Howard Hinnant10f25d22011-05-27 20:52:28821bitset<_Size>::operator^=(const bitset& __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16822{
823 base::operator^=(__rhs);
824 return *this;
825}
826
827template <size_t _Size>
828bitset<_Size>&
Howard Hinnant10f25d22011-05-27 20:52:28829bitset<_Size>::operator<<=(size_t __pos) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16830{
Howard Hinnant0949eed2011-06-30 21:18:19831 __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);
Howard Hinnantbc8d3f92010-05-11 19:42:16834 return *this;
835}
836
837template <size_t _Size>
838bitset<_Size>&
Howard Hinnant10f25d22011-05-27 20:52:28839bitset<_Size>::operator>>=(size_t __pos) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16840{
Howard Hinnant0949eed2011-06-30 21:18:19841 __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);
Howard Hinnantbc8d3f92010-05-11 19:42:16844 return *this;
845}
846
847template <size_t _Size>
Evgeniy Stepanova3b25f82015-11-07 01:22:13848inline
Howard Hinnantbc8d3f92010-05-11 19:42:16849bitset<_Size>&
Howard Hinnant10f25d22011-05-27 20:52:28850bitset<_Size>::set() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16851{
Howard Hinnant0949eed2011-06-30 21:18:19852 _VSTD::fill_n(base::__make_iter(0), _Size, true);
Howard Hinnantbc8d3f92010-05-11 19:42:16853 return *this;
854}
855
856template <size_t _Size>
857bitset<_Size>&
858bitset<_Size>::set(size_t __pos, bool __val)
859{
860 if (__pos >= _Size)
Marshall Clow14c09a22016-08-25 15:09:01861 __throw_out_of_range("bitset set argument out of range");
862
Howard Hinnantbc8d3f92010-05-11 19:42:16863 (*this)[__pos] = __val;
864 return *this;
865}
866
867template <size_t _Size>
Evgeniy Stepanova3b25f82015-11-07 01:22:13868inline
Howard Hinnantbc8d3f92010-05-11 19:42:16869bitset<_Size>&
Howard Hinnant10f25d22011-05-27 20:52:28870bitset<_Size>::reset() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16871{
Howard Hinnant0949eed2011-06-30 21:18:19872 _VSTD::fill_n(base::__make_iter(0), _Size, false);
Howard Hinnantbc8d3f92010-05-11 19:42:16873 return *this;
874}
875
876template <size_t _Size>
877bitset<_Size>&
878bitset<_Size>::reset(size_t __pos)
879{
880 if (__pos >= _Size)
Marshall Clow14c09a22016-08-25 15:09:01881 __throw_out_of_range("bitset reset argument out of range");
882
Howard Hinnantbc8d3f92010-05-11 19:42:16883 (*this)[__pos] = false;
884 return *this;
885}
886
887template <size_t _Size>
Evgeniy Stepanova3b25f82015-11-07 01:22:13888inline
Howard Hinnantbc8d3f92010-05-11 19:42:16889bitset<_Size>
Howard Hinnant10f25d22011-05-27 20:52:28890bitset<_Size>::operator~() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16891{
892 bitset __x(*this);
893 __x.flip();
894 return __x;
895}
896
897template <size_t _Size>
Evgeniy Stepanova3b25f82015-11-07 01:22:13898inline
Howard Hinnantbc8d3f92010-05-11 19:42:16899bitset<_Size>&
Howard Hinnant10f25d22011-05-27 20:52:28900bitset<_Size>::flip() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16901{
902 base::flip();
903 return *this;
904}
905
906template <size_t _Size>
907bitset<_Size>&
908bitset<_Size>::flip(size_t __pos)
909{
910 if (__pos >= _Size)
Marshall Clow14c09a22016-08-25 15:09:01911 __throw_out_of_range("bitset flip argument out of range");
912
Howard Hinnantbc8d3f92010-05-11 19:42:16913 reference r = base::__make_ref(__pos);
914 r = ~r;
915 return *this;
916}
917
918template <size_t _Size>
Evgeniy Stepanova3b25f82015-11-07 01:22:13919inline
Howard Hinnantbc8d3f92010-05-11 19:42:16920unsigned long
921bitset<_Size>::to_ulong() const
922{
923 return base::to_ulong();
924}
925
926template <size_t _Size>
Evgeniy Stepanova3b25f82015-11-07 01:22:13927inline
Howard Hinnantbc8d3f92010-05-11 19:42:16928unsigned long long
929bitset<_Size>::to_ullong() const
930{
931 return base::to_ullong();
932}
933
934template <size_t _Size>
Howard Hinnant324bb032010-08-22 00:02:43935template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16936basic_string<_CharT, _Traits, _Allocator>
937bitset<_Size>::to_string(_CharT __zero, _CharT __one) const
938{
939 basic_string<_CharT, _Traits, _Allocator> __r(_Size, __zero);
940 for (size_t __i = 0; __i < _Size; ++__i)
941 {
942 if ((*this)[__i])
943 __r[_Size - 1 - __i] = __one;
944 }
945 return __r;
946}
947
948template <size_t _Size>
Howard Hinnant324bb032010-08-22 00:02:43949template <class _CharT, class _Traits>
Evgeniy Stepanova3b25f82015-11-07 01:22:13950inline
Howard Hinnantbc8d3f92010-05-11 19:42:16951basic_string<_CharT, _Traits, allocator<_CharT> >
952bitset<_Size>::to_string(_CharT __zero, _CharT __one) const
953{
954 return to_string<_CharT, _Traits, allocator<_CharT> >(__zero, __one);
955}
956
957template <size_t _Size>
Howard Hinnant324bb032010-08-22 00:02:43958template <class _CharT>
Evgeniy Stepanova3b25f82015-11-07 01:22:13959inline
Howard Hinnantbc8d3f92010-05-11 19:42:16960basic_string<_CharT, char_traits<_CharT>, allocator<_CharT> >
961bitset<_Size>::to_string(_CharT __zero, _CharT __one) const
962{
963 return to_string<_CharT, char_traits<_CharT>, allocator<_CharT> >(__zero, __one);
964}
965
966template <size_t _Size>
Evgeniy Stepanova3b25f82015-11-07 01:22:13967inline
Howard Hinnantbc8d3f92010-05-11 19:42:16968basic_string<char, char_traits<char>, allocator<char> >
969bitset<_Size>::to_string(char __zero, char __one) const
970{
971 return to_string<char, char_traits<char>, allocator<char> >(__zero, __one);
972}
973
974template <size_t _Size>
Evgeniy Stepanova3b25f82015-11-07 01:22:13975inline
Howard Hinnantbc8d3f92010-05-11 19:42:16976size_t
Howard Hinnant10f25d22011-05-27 20:52:28977bitset<_Size>::count() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16978{
Howard Hinnant0949eed2011-06-30 21:18:19979 return static_cast<size_t>(_VSTD::count(base::__make_iter(0), base::__make_iter(_Size), true));
Howard Hinnantbc8d3f92010-05-11 19:42:16980}
981
982template <size_t _Size>
Evgeniy Stepanova3b25f82015-11-07 01:22:13983inline
Howard Hinnantbc8d3f92010-05-11 19:42:16984bool
Howard Hinnant10f25d22011-05-27 20:52:28985bitset<_Size>::operator==(const bitset& __rhs) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16986{
Howard Hinnant0949eed2011-06-30 21:18:19987 return _VSTD::equal(base::__make_iter(0), base::__make_iter(_Size), __rhs.__make_iter(0));
Howard Hinnantbc8d3f92010-05-11 19:42:16988}
989
990template <size_t _Size>
Evgeniy Stepanova3b25f82015-11-07 01:22:13991inline
Howard Hinnantbc8d3f92010-05-11 19:42:16992bool
Howard Hinnant10f25d22011-05-27 20:52:28993bitset<_Size>::operator!=(const bitset& __rhs) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16994{
995 return !(*this == __rhs);
996}
997
998template <size_t _Size>
999bool
1000bitset<_Size>::test(size_t __pos) const
1001{
1002 if (__pos >= _Size)
Marshall Clow14c09a22016-08-25 15:09:011003 __throw_out_of_range("bitset test argument out of range");
1004
Howard Hinnantbc8d3f92010-05-11 19:42:161005 return (*this)[__pos];
1006}
1007
1008template <size_t _Size>
Evgeniy Stepanova3b25f82015-11-07 01:22:131009inline
Howard Hinnantbc8d3f92010-05-11 19:42:161010bool
Howard Hinnant10f25d22011-05-27 20:52:281011bitset<_Size>::all() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:161012{
1013 return base::all();
1014}
1015
1016template <size_t _Size>
Evgeniy Stepanova3b25f82015-11-07 01:22:131017inline
Howard Hinnantbc8d3f92010-05-11 19:42:161018bool
Howard Hinnant10f25d22011-05-27 20:52:281019bitset<_Size>::any() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:161020{
1021 return base::any();
1022}
1023
1024template <size_t _Size>
Evgeniy Stepanova3b25f82015-11-07 01:22:131025inline
Howard Hinnantbc8d3f92010-05-11 19:42:161026bitset<_Size>
Howard Hinnant10f25d22011-05-27 20:52:281027bitset<_Size>::operator<<(size_t __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:161028{
1029 bitset __r = *this;
1030 __r <<= __pos;
1031 return __r;
1032}
1033
1034template <size_t _Size>
Evgeniy Stepanova3b25f82015-11-07 01:22:131035inline
Howard Hinnantbc8d3f92010-05-11 19:42:161036bitset<_Size>
Howard Hinnant10f25d22011-05-27 20:52:281037bitset<_Size>::operator>>(size_t __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:161038{
1039 bitset __r = *this;
1040 __r >>= __pos;
1041 return __r;
1042}
1043
1044template <size_t _Size>
1045inline _LIBCPP_INLINE_VISIBILITY
1046bitset<_Size>
Howard Hinnant10f25d22011-05-27 20:52:281047operator&(const bitset<_Size>& __x, const bitset<_Size>& __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:161048{
1049 bitset<_Size> __r = __x;
1050 __r &= __y;
1051 return __r;
1052}
1053
1054template <size_t _Size>
1055inline _LIBCPP_INLINE_VISIBILITY
1056bitset<_Size>
Howard Hinnant10f25d22011-05-27 20:52:281057operator|(const bitset<_Size>& __x, const bitset<_Size>& __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:161058{
1059 bitset<_Size> __r = __x;
1060 __r |= __y;
1061 return __r;
1062}
1063
1064template <size_t _Size>
1065inline _LIBCPP_INLINE_VISIBILITY
1066bitset<_Size>
Howard Hinnant10f25d22011-05-27 20:52:281067operator^(const bitset<_Size>& __x, const bitset<_Size>& __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:161068{
1069 bitset<_Size> __r = __x;
1070 __r ^= __y;
1071 return __r;
1072}
1073
1074template <size_t _Size>
Eric Fiselierc3589a82017-01-04 23:56:001075struct _LIBCPP_TEMPLATE_VIS hash<bitset<_Size> >
Howard Hinnantbc8d3f92010-05-11 19:42:161076 : public unary_function<bitset<_Size>, size_t>
1077{
Howard Hinnant422a53f2010-09-21 21:28:231078 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant10f25d22011-05-27 20:52:281079 size_t operator()(const bitset<_Size>& __bs) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:161080 {return __bs.__hash_code();}
1081};
1082
Howard Hinnant464aa5c2011-07-18 15:51:591083template <class _CharT, class _Traits, size_t _Size>
1084basic_istream<_CharT, _Traits>&
1085operator>>(basic_istream<_CharT, _Traits>& __is, bitset<_Size>& __x);
1086
1087template <class _CharT, class _Traits, size_t _Size>
1088basic_ostream<_CharT, _Traits>&
1089operator<<(basic_ostream<_CharT, _Traits>& __os, const bitset<_Size>& __x);
1090
Howard Hinnantbc8d3f92010-05-11 19:42:161091_LIBCPP_END_NAMESPACE_STD
1092
1093#endif // _LIBCPP_BITSET