blob: cd6c289b4c480b7b090c6a5b2b23eb4d09435eb6 [file] [log] [blame]
Howard Hinnant3e519522010-05-11 19:42:161// -*- C++ -*-
2//===---------------------------- bitset ----------------------------------===//
3//
Howard Hinnant5b08a8a2010-05-11 21:36:014// The LLVM Compiler Infrastructure
Howard Hinnant3e519522010-05-11 19:42:165//
Howard Hinnant412dbeb2010-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 Hinnant3e519522010-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 Hinnantd368a842011-05-27 20:52:2830 reference() noexcept;
Howard Hinnant3e519522010-05-11 19:42:1631 public:
Howard Hinnantd368a842011-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 Hinnant3e519522010-05-11 19:42:1638 };
39
40 // 23.3.5.1 constructors:
Howard Hinnantd368a842011-05-27 20:52:2841 constexpr bitset() noexcept;
42 constexpr bitset(unsigned long long val) noexcept;
Howard Hinnantd09f7112010-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 Hinnant3e519522010-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 Hinnantd368a842011-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 Hinnant3e519522010-05-11 19:42:1661 bitset& set(size_t pos, bool val = true);
Howard Hinnantd368a842011-05-27 20:52:2862 bitset& reset() noexcept;
Howard Hinnant3e519522010-05-11 19:42:1663 bitset& reset(size_t pos);
Howard Hinnantd368a842011-05-27 20:52:2864 bitset operator~() const noexcept;
65 bitset& flip() noexcept;
Howard Hinnant3e519522010-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 Hinnantd368a842011-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 Hinnant3e519522010-05-11 19:42:1684 bool test(size_t pos) const;
Howard Hinnantd368a842011-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 Hinnant3e519522010-05-11 19:42:1690};
91
92// 23.3.5.3 bitset operators:
93template <size_t N>
Howard Hinnantd368a842011-05-27 20:52:2894bitset<N> operator&(const bitset<N>&, const bitset<N>&) noexcept;
Howard Hinnant3e519522010-05-11 19:42:1695
96template <size_t N>
Howard Hinnantd368a842011-05-27 20:52:2897bitset<N> operator|(const bitset<N>&, const bitset<N>&) noexcept;
Howard Hinnant3e519522010-05-11 19:42:1698
99template <size_t N>
Howard Hinnantd368a842011-05-27 20:52:28100bitset<N> operator^(const bitset<N>&, const bitset<N>&) noexcept;
Howard Hinnant3e519522010-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 Hinnant3e519522010-05-11 19:42:16116#include <__config>
117#include <__bit_reference>
118#include <cstddef>
119#include <climits>
120#include <string>
121#include <stdexcept>
122#include <iosfwd>
123#include <__functional_base>
Howard Hinnant3e519522010-05-11 19:42:16124
Eric Fiseliera016efb2017-05-31 22:07:49125#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
126#pragma GCC system_header
127#endif
128
129_LIBCPP_PUSH_MACROS
130#include <__undef_macros>
131
Howard Hinnantab4f4382011-11-29 16:45:27132
Howard Hinnant3e519522010-05-11 19:42:16133_LIBCPP_BEGIN_NAMESPACE_STD
134
135template <size_t _N_words, size_t _Size>
Howard Hinnanta7744562011-07-02 20:33:23136class __bitset;
137
138template <size_t _N_words, size_t _Size>
139struct __has_storage_type<__bitset<_N_words, _Size> >
140{
141 static const bool value = true;
142};
143
144template <size_t _N_words, size_t _Size>
Howard Hinnant3e519522010-05-11 19:42:16145class __bitset
146{
147public:
148 typedef ptrdiff_t difference_type;
149 typedef size_t size_type;
Howard Hinnant0ae9efe2012-05-07 16:50:38150 typedef size_type __storage_type;
Howard Hinnant3e519522010-05-11 19:42:16151protected:
152 typedef __bitset __self;
Howard Hinnant3e519522010-05-11 19:42:16153 typedef __storage_type* __storage_pointer;
154 typedef const __storage_type* __const_storage_pointer;
155 static const unsigned __bits_per_word = static_cast<unsigned>(sizeof(__storage_type) * CHAR_BIT);
156
157 friend class __bit_reference<__bitset>;
158 friend class __bit_const_reference<__bitset>;
159 friend class __bit_iterator<__bitset, false>;
160 friend class __bit_iterator<__bitset, true>;
Howard Hinnant7ee27132012-08-17 17:10:18161 friend struct __bit_array<__bitset>;
Howard Hinnant3e519522010-05-11 19:42:16162
163 __storage_type __first_[_N_words];
164
165 typedef __bit_reference<__bitset> reference;
166 typedef __bit_const_reference<__bitset> const_reference;
167 typedef __bit_iterator<__bitset, false> iterator;
168 typedef __bit_iterator<__bitset, true> const_iterator;
169
Evgeniy Stepanov906c8722015-11-07 01:22:13170 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanteeac9fc2012-07-07 17:04:52171 _LIBCPP_CONSTEXPR __bitset() _NOEXCEPT;
Evgeniy Stepanov906c8722015-11-07 01:22:13172 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanteeac9fc2012-07-07 17:04:52173 explicit _LIBCPP_CONSTEXPR __bitset(unsigned long long __v) _NOEXCEPT;
Howard Hinnant3e519522010-05-11 19:42:16174
Howard Hinnantd368a842011-05-27 20:52:28175 _LIBCPP_INLINE_VISIBILITY reference __make_ref(size_t __pos) _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16176 {return reference(__first_ + __pos / __bits_per_word, __storage_type(1) << __pos % __bits_per_word);}
Howard Hinnanteeac9fc2012-07-07 17:04:52177 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR const_reference __make_ref(size_t __pos) const _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16178 {return const_reference(__first_ + __pos / __bits_per_word, __storage_type(1) << __pos % __bits_per_word);}
Howard Hinnantd368a842011-05-27 20:52:28179 _LIBCPP_INLINE_VISIBILITY iterator __make_iter(size_t __pos) _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16180 {return iterator(__first_ + __pos / __bits_per_word, __pos % __bits_per_word);}
Howard Hinnantd368a842011-05-27 20:52:28181 _LIBCPP_INLINE_VISIBILITY const_iterator __make_iter(size_t __pos) const _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16182 {return const_iterator(__first_ + __pos / __bits_per_word, __pos % __bits_per_word);}
183
Evgeniy Stepanov906c8722015-11-07 01:22:13184 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd368a842011-05-27 20:52:28185 void operator&=(const __bitset& __v) _NOEXCEPT;
Evgeniy Stepanov906c8722015-11-07 01:22:13186 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd368a842011-05-27 20:52:28187 void operator|=(const __bitset& __v) _NOEXCEPT;
Evgeniy Stepanov906c8722015-11-07 01:22:13188 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd368a842011-05-27 20:52:28189 void operator^=(const __bitset& __v) _NOEXCEPT;
Howard Hinnant3e519522010-05-11 19:42:16190
Howard Hinnantd368a842011-05-27 20:52:28191 void flip() _NOEXCEPT;
Howard Hinnant3e519522010-05-11 19:42:16192 _LIBCPP_INLINE_VISIBILITY unsigned long to_ulong() const
193 {return to_ulong(integral_constant<bool, _Size < sizeof(unsigned long) * CHAR_BIT>());}
194 _LIBCPP_INLINE_VISIBILITY unsigned long long to_ullong() const
195 {return to_ullong(integral_constant<bool, _Size < sizeof(unsigned long long) * CHAR_BIT>());}
196
Howard Hinnantd368a842011-05-27 20:52:28197 bool all() const _NOEXCEPT;
198 bool any() const _NOEXCEPT;
Evgeniy Stepanov906c8722015-11-07 01:22:13199 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd368a842011-05-27 20:52:28200 size_t __hash_code() const _NOEXCEPT;
Howard Hinnant3e519522010-05-11 19:42:16201private:
Eric Fiselier046492b2017-04-19 01:34:08202#ifdef _LIBCPP_CXX03_LANG
Howard Hinnantd368a842011-05-27 20:52:28203 void __init(unsigned long long __v, false_type) _NOEXCEPT;
Evgeniy Stepanov02b8e942015-12-09 22:32:36204 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd368a842011-05-27 20:52:28205 void __init(unsigned long long __v, true_type) _NOEXCEPT;
Eric Fiselier046492b2017-04-19 01:34:08206#endif // _LIBCPP_CXX03_LANG
Howard Hinnant3e519522010-05-11 19:42:16207 unsigned long to_ulong(false_type) const;
Evgeniy Stepanov906c8722015-11-07 01:22:13208 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16209 unsigned long to_ulong(true_type) const;
210 unsigned long long to_ullong(false_type) const;
Evgeniy Stepanov906c8722015-11-07 01:22:13211 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16212 unsigned long long to_ullong(true_type) const;
Evgeniy Stepanov906c8722015-11-07 01:22:13213 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16214 unsigned long long to_ullong(true_type, false_type) const;
215 unsigned long long to_ullong(true_type, true_type) const;
216};
217
218template <size_t _N_words, size_t _Size>
Evgeniy Stepanov906c8722015-11-07 01:22:13219inline
Howard Hinnanteeac9fc2012-07-07 17:04:52220_LIBCPP_CONSTEXPR
Howard Hinnantd368a842011-05-27 20:52:28221__bitset<_N_words, _Size>::__bitset() _NOEXCEPT
Eric Fiselier046492b2017-04-19 01:34:08222#ifndef _LIBCPP_CXX03_LANG
Howard Hinnanteeac9fc2012-07-07 17:04:52223 : __first_{0}
224#endif
Howard Hinnant3e519522010-05-11 19:42:16225{
Eric Fiselier046492b2017-04-19 01:34:08226#ifdef _LIBCPP_CXX03_LANG
Howard Hinnantce48a112011-06-30 21:18:19227 _VSTD::fill_n(__first_, _N_words, __storage_type(0));
Howard Hinnanteeac9fc2012-07-07 17:04:52228#endif
Howard Hinnant3e519522010-05-11 19:42:16229}
230
Eric Fiselier046492b2017-04-19 01:34:08231#ifdef _LIBCPP_CXX03_LANG
Howard Hinnanteeac9fc2012-07-07 17:04:52232
Howard Hinnant3e519522010-05-11 19:42:16233template <size_t _N_words, size_t _Size>
234void
Howard Hinnantc2063662011-12-01 20:21:04235__bitset<_N_words, _Size>::__init(unsigned long long __v, false_type) _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16236{
237 __storage_type __t[sizeof(unsigned long long) / sizeof(__storage_type)];
Marshall Clowea44ee22017-11-27 22:27:22238 size_t __sz = _Size;
239 for (size_t __i = 0; __i < sizeof(__t)/sizeof(__t[0]); ++__i, __v >>= __bits_per_word, __sz -= __bits_per_word )
240 if ( __sz < __bits_per_word)
241 __t[__i] = static_cast<__storage_type>(__v) & ( 1ULL << __sz ) - 1;
242 else
243 __t[__i] = static_cast<__storage_type>(__v);
244
Howard Hinnantce48a112011-06-30 21:18:19245 _VSTD::copy(__t, __t + sizeof(__t)/sizeof(__t[0]), __first_);
246 _VSTD::fill(__first_ + sizeof(__t)/sizeof(__t[0]), __first_ + sizeof(__first_)/sizeof(__first_[0]),
Howard Hinnant3e519522010-05-11 19:42:16247 __storage_type(0));
248}
249
250template <size_t _N_words, size_t _Size>
251inline _LIBCPP_INLINE_VISIBILITY
252void
Howard Hinnantc2063662011-12-01 20:21:04253__bitset<_N_words, _Size>::__init(unsigned long long __v, true_type) _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16254{
255 __first_[0] = __v;
Marshall Clowea44ee22017-11-27 22:27:22256 if (_Size < __bits_per_word)
257 __first_[0] &= ( 1ULL << _Size ) - 1;
258
Howard Hinnantce48a112011-06-30 21:18:19259 _VSTD::fill(__first_ + 1, __first_ + sizeof(__first_)/sizeof(__first_[0]), __storage_type(0));
Howard Hinnant3e519522010-05-11 19:42:16260}
261
Eric Fiselier046492b2017-04-19 01:34:08262#endif // _LIBCPP_CXX03_LANG
Howard Hinnanteeac9fc2012-07-07 17:04:52263
Howard Hinnant3e519522010-05-11 19:42:16264template <size_t _N_words, size_t _Size>
Evgeniy Stepanov906c8722015-11-07 01:22:13265inline
Howard Hinnanteeac9fc2012-07-07 17:04:52266_LIBCPP_CONSTEXPR
Howard Hinnantd368a842011-05-27 20:52:28267__bitset<_N_words, _Size>::__bitset(unsigned long long __v) _NOEXCEPT
Eric Fiselier046492b2017-04-19 01:34:08268#ifndef _LIBCPP_CXX03_LANG
Nico Weber1d1b46c2014-06-04 15:46:56269#if __SIZEOF_SIZE_T__ == 8
Howard Hinnanteeac9fc2012-07-07 17:04:52270 : __first_{__v}
Nico Weber1d1b46c2014-06-04 15:46:56271#elif __SIZEOF_SIZE_T__ == 4
Marshall Clowea44ee22017-11-27 22:27:22272 : __first_{static_cast<__storage_type>(__v),
273 _Size >= 2 * __bits_per_word ? static_cast<__storage_type>(__v >> __bits_per_word)
274 : static_cast<__storage_type>((__v >> __bits_per_word) & (__storage_type(1) << (_Size - __bits_per_word)) - 1)}
Howard Hinnant3fb6c6e2013-03-06 18:16:12275#else
Howard Hinnant53b9ee02013-03-06 17:30:26276#error This constructor has not been ported to this platform
277#endif
Howard Hinnanteeac9fc2012-07-07 17:04:52278#endif
Howard Hinnant3e519522010-05-11 19:42:16279{
Eric Fiselier046492b2017-04-19 01:34:08280#ifdef _LIBCPP_CXX03_LANG
Howard Hinnant3e519522010-05-11 19:42:16281 __init(__v, integral_constant<bool, sizeof(unsigned long long) == sizeof(__storage_type)>());
Howard Hinnanteeac9fc2012-07-07 17:04:52282#endif
Howard Hinnant3e519522010-05-11 19:42:16283}
284
285template <size_t _N_words, size_t _Size>
Evgeniy Stepanov906c8722015-11-07 01:22:13286inline
Howard Hinnant3e519522010-05-11 19:42:16287void
Howard Hinnantd368a842011-05-27 20:52:28288__bitset<_N_words, _Size>::operator&=(const __bitset& __v) _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16289{
290 for (size_type __i = 0; __i < _N_words; ++__i)
291 __first_[__i] &= __v.__first_[__i];
292}
293
294template <size_t _N_words, size_t _Size>
Evgeniy Stepanov906c8722015-11-07 01:22:13295inline
Howard Hinnant3e519522010-05-11 19:42:16296void
Howard Hinnantd368a842011-05-27 20:52:28297__bitset<_N_words, _Size>::operator|=(const __bitset& __v) _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16298{
299 for (size_type __i = 0; __i < _N_words; ++__i)
300 __first_[__i] |= __v.__first_[__i];
301}
302
303template <size_t _N_words, size_t _Size>
Evgeniy Stepanov906c8722015-11-07 01:22:13304inline
Howard Hinnant3e519522010-05-11 19:42:16305void
Howard Hinnantd368a842011-05-27 20:52:28306__bitset<_N_words, _Size>::operator^=(const __bitset& __v) _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16307{
308 for (size_type __i = 0; __i < _N_words; ++__i)
309 __first_[__i] ^= __v.__first_[__i];
310}
311
312template <size_t _N_words, size_t _Size>
313void
Howard Hinnantd368a842011-05-27 20:52:28314__bitset<_N_words, _Size>::flip() _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16315{
316 // do middle whole words
317 size_type __n = _Size;
318 __storage_pointer __p = __first_;
319 for (; __n >= __bits_per_word; ++__p, __n -= __bits_per_word)
320 *__p = ~*__p;
321 // do last partial word
322 if (__n > 0)
323 {
324 __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
325 __storage_type __b = *__p & __m;
326 *__p &= ~__m;
327 *__p |= ~__b & __m;
328 }
329}
330
331template <size_t _N_words, size_t _Size>
332unsigned long
333__bitset<_N_words, _Size>::to_ulong(false_type) const
334{
335 const_iterator __e = __make_iter(_Size);
Howard Hinnantce48a112011-06-30 21:18:19336 const_iterator __i = _VSTD::find(__make_iter(sizeof(unsigned long) * CHAR_BIT), __e, true);
Howard Hinnant3e519522010-05-11 19:42:16337 if (__i != __e)
Marshall Clowd437fa52016-08-25 15:09:01338 __throw_overflow_error("bitset to_ulong overflow error");
339
Howard Hinnant3e519522010-05-11 19:42:16340 return __first_[0];
341}
342
343template <size_t _N_words, size_t _Size>
Evgeniy Stepanov906c8722015-11-07 01:22:13344inline
Howard Hinnant3e519522010-05-11 19:42:16345unsigned long
346__bitset<_N_words, _Size>::to_ulong(true_type) const
347{
348 return __first_[0];
349}
350
351template <size_t _N_words, size_t _Size>
352unsigned long long
353__bitset<_N_words, _Size>::to_ullong(false_type) const
354{
355 const_iterator __e = __make_iter(_Size);
Howard Hinnantce48a112011-06-30 21:18:19356 const_iterator __i = _VSTD::find(__make_iter(sizeof(unsigned long long) * CHAR_BIT), __e, true);
Howard Hinnant3e519522010-05-11 19:42:16357 if (__i != __e)
Marshall Clowd437fa52016-08-25 15:09:01358 __throw_overflow_error("bitset to_ullong overflow error");
359
Howard Hinnant3e519522010-05-11 19:42:16360 return to_ullong(true_type());
361}
362
363template <size_t _N_words, size_t _Size>
Evgeniy Stepanov906c8722015-11-07 01:22:13364inline
Howard Hinnant3e519522010-05-11 19:42:16365unsigned long long
366__bitset<_N_words, _Size>::to_ullong(true_type) const
367{
368 return to_ullong(true_type(), integral_constant<bool, sizeof(__storage_type) < sizeof(unsigned long long)>());
369}
370
371template <size_t _N_words, size_t _Size>
Evgeniy Stepanov906c8722015-11-07 01:22:13372inline
Howard Hinnant3e519522010-05-11 19:42:16373unsigned long long
374__bitset<_N_words, _Size>::to_ullong(true_type, false_type) const
375{
376 return __first_[0];
377}
378
379template <size_t _N_words, size_t _Size>
380unsigned long long
381__bitset<_N_words, _Size>::to_ullong(true_type, true_type) const
382{
383 unsigned long long __r = __first_[0];
384 for (std::size_t __i = 1; __i < sizeof(unsigned long long) / sizeof(__storage_type); ++__i)
385 __r |= static_cast<unsigned long long>(__first_[__i]) << (sizeof(__storage_type) * CHAR_BIT);
386 return __r;
387}
388
389template <size_t _N_words, size_t _Size>
390bool
Howard Hinnantd368a842011-05-27 20:52:28391__bitset<_N_words, _Size>::all() const _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16392{
393 // do middle whole words
394 size_type __n = _Size;
395 __const_storage_pointer __p = __first_;
396 for (; __n >= __bits_per_word; ++__p, __n -= __bits_per_word)
397 if (~*__p)
398 return false;
399 // do last partial word
400 if (__n > 0)
401 {
402 __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
403 if (~*__p & __m)
404 return false;
405 }
406 return true;
407}
408
409template <size_t _N_words, size_t _Size>
410bool
Howard Hinnantd368a842011-05-27 20:52:28411__bitset<_N_words, _Size>::any() const _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16412{
413 // do middle whole words
414 size_type __n = _Size;
415 __const_storage_pointer __p = __first_;
416 for (; __n >= __bits_per_word; ++__p, __n -= __bits_per_word)
417 if (*__p)
418 return true;
419 // do last partial word
420 if (__n > 0)
421 {
422 __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
423 if (*__p & __m)
424 return true;
425 }
426 return false;
427}
428
429template <size_t _N_words, size_t _Size>
Evgeniy Stepanov906c8722015-11-07 01:22:13430inline
Howard Hinnant3e519522010-05-11 19:42:16431size_t
Howard Hinnantd368a842011-05-27 20:52:28432__bitset<_N_words, _Size>::__hash_code() const _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16433{
434 size_t __h = 0;
435 for (size_type __i = 0; __i < _N_words; ++__i)
436 __h ^= __first_[__i];
437 return __h;
438}
439
440template <size_t _Size>
441class __bitset<1, _Size>
442{
443public:
444 typedef ptrdiff_t difference_type;
445 typedef size_t size_type;
Howard Hinnant0ae9efe2012-05-07 16:50:38446 typedef size_type __storage_type;
Howard Hinnant3e519522010-05-11 19:42:16447protected:
448 typedef __bitset __self;
Howard Hinnant3e519522010-05-11 19:42:16449 typedef __storage_type* __storage_pointer;
450 typedef const __storage_type* __const_storage_pointer;
451 static const unsigned __bits_per_word = static_cast<unsigned>(sizeof(__storage_type) * CHAR_BIT);
452
453 friend class __bit_reference<__bitset>;
454 friend class __bit_const_reference<__bitset>;
455 friend class __bit_iterator<__bitset, false>;
456 friend class __bit_iterator<__bitset, true>;
Howard Hinnant7ee27132012-08-17 17:10:18457 friend struct __bit_array<__bitset>;
Howard Hinnant3e519522010-05-11 19:42:16458
459 __storage_type __first_;
460
461 typedef __bit_reference<__bitset> reference;
462 typedef __bit_const_reference<__bitset> const_reference;
463 typedef __bit_iterator<__bitset, false> iterator;
464 typedef __bit_iterator<__bitset, true> const_iterator;
465
Evgeniy Stepanov906c8722015-11-07 01:22:13466 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanteeac9fc2012-07-07 17:04:52467 _LIBCPP_CONSTEXPR __bitset() _NOEXCEPT;
Evgeniy Stepanov906c8722015-11-07 01:22:13468 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanteeac9fc2012-07-07 17:04:52469 explicit _LIBCPP_CONSTEXPR __bitset(unsigned long long __v) _NOEXCEPT;
Howard Hinnant3e519522010-05-11 19:42:16470
Howard Hinnantd368a842011-05-27 20:52:28471 _LIBCPP_INLINE_VISIBILITY reference __make_ref(size_t __pos) _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16472 {return reference(&__first_, __storage_type(1) << __pos);}
Howard Hinnanteeac9fc2012-07-07 17:04:52473 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR const_reference __make_ref(size_t __pos) const _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16474 {return const_reference(&__first_, __storage_type(1) << __pos);}
Howard Hinnantd368a842011-05-27 20:52:28475 _LIBCPP_INLINE_VISIBILITY iterator __make_iter(size_t __pos) _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16476 {return iterator(&__first_ + __pos / __bits_per_word, __pos % __bits_per_word);}
Howard Hinnantd368a842011-05-27 20:52:28477 _LIBCPP_INLINE_VISIBILITY const_iterator __make_iter(size_t __pos) const _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16478 {return const_iterator(&__first_ + __pos / __bits_per_word, __pos % __bits_per_word);}
479
Evgeniy Stepanov906c8722015-11-07 01:22:13480 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd368a842011-05-27 20:52:28481 void operator&=(const __bitset& __v) _NOEXCEPT;
Evgeniy Stepanov906c8722015-11-07 01:22:13482 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd368a842011-05-27 20:52:28483 void operator|=(const __bitset& __v) _NOEXCEPT;
Evgeniy Stepanov906c8722015-11-07 01:22:13484 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd368a842011-05-27 20:52:28485 void operator^=(const __bitset& __v) _NOEXCEPT;
Howard Hinnant3e519522010-05-11 19:42:16486
Evgeniy Stepanov906c8722015-11-07 01:22:13487 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd368a842011-05-27 20:52:28488 void flip() _NOEXCEPT;
Howard Hinnant3e519522010-05-11 19:42:16489
Evgeniy Stepanov906c8722015-11-07 01:22:13490 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16491 unsigned long to_ulong() const;
Evgeniy Stepanov906c8722015-11-07 01:22:13492 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16493 unsigned long long to_ullong() const;
494
Evgeniy Stepanov906c8722015-11-07 01:22:13495 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd368a842011-05-27 20:52:28496 bool all() const _NOEXCEPT;
Evgeniy Stepanov906c8722015-11-07 01:22:13497 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd368a842011-05-27 20:52:28498 bool any() const _NOEXCEPT;
Howard Hinnant3e519522010-05-11 19:42:16499
Evgeniy Stepanov906c8722015-11-07 01:22:13500 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd368a842011-05-27 20:52:28501 size_t __hash_code() const _NOEXCEPT;
Howard Hinnant3e519522010-05-11 19:42:16502};
503
504template <size_t _Size>
Evgeniy Stepanov906c8722015-11-07 01:22:13505inline
Howard Hinnanteeac9fc2012-07-07 17:04:52506_LIBCPP_CONSTEXPR
Howard Hinnantd368a842011-05-27 20:52:28507__bitset<1, _Size>::__bitset() _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16508 : __first_(0)
509{
510}
511
512template <size_t _Size>
Evgeniy Stepanov906c8722015-11-07 01:22:13513inline
Howard Hinnanteeac9fc2012-07-07 17:04:52514_LIBCPP_CONSTEXPR
Howard Hinnantd368a842011-05-27 20:52:28515__bitset<1, _Size>::__bitset(unsigned long long __v) _NOEXCEPT
Marshall Clow14d7aac2017-11-27 19:03:30516 : __first_(
517 _Size == __bits_per_word ? static_cast<__storage_type>(__v)
518 : static_cast<__storage_type>(__v) & ((__storage_type(1) << _Size) - 1)
519 )
Howard Hinnant3e519522010-05-11 19:42:16520{
521}
522
523template <size_t _Size>
Evgeniy Stepanov906c8722015-11-07 01:22:13524inline
Howard Hinnant3e519522010-05-11 19:42:16525void
Howard Hinnantd368a842011-05-27 20:52:28526__bitset<1, _Size>::operator&=(const __bitset& __v) _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16527{
528 __first_ &= __v.__first_;
529}
530
531template <size_t _Size>
Evgeniy Stepanov906c8722015-11-07 01:22:13532inline
Howard Hinnant3e519522010-05-11 19:42:16533void
Howard Hinnantd368a842011-05-27 20:52:28534__bitset<1, _Size>::operator|=(const __bitset& __v) _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16535{
536 __first_ |= __v.__first_;
537}
538
539template <size_t _Size>
Evgeniy Stepanov906c8722015-11-07 01:22:13540inline
Howard Hinnant3e519522010-05-11 19:42:16541void
Howard Hinnantd368a842011-05-27 20:52:28542__bitset<1, _Size>::operator^=(const __bitset& __v) _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16543{
544 __first_ ^= __v.__first_;
545}
546
547template <size_t _Size>
Evgeniy Stepanov906c8722015-11-07 01:22:13548inline
Howard Hinnant3e519522010-05-11 19:42:16549void
Howard Hinnantd368a842011-05-27 20:52:28550__bitset<1, _Size>::flip() _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16551{
552 __storage_type __m = ~__storage_type(0) >> (__bits_per_word - _Size);
553 __first_ = ~__first_;
554 __first_ &= __m;
555}
556
557template <size_t _Size>
Evgeniy Stepanov906c8722015-11-07 01:22:13558inline
Howard Hinnant3e519522010-05-11 19:42:16559unsigned long
560__bitset<1, _Size>::to_ulong() const
561{
562 return __first_;
563}
564
565template <size_t _Size>
Evgeniy Stepanov906c8722015-11-07 01:22:13566inline
Howard Hinnant3e519522010-05-11 19:42:16567unsigned long long
568__bitset<1, _Size>::to_ullong() const
569{
570 return __first_;
571}
572
573template <size_t _Size>
Evgeniy Stepanov906c8722015-11-07 01:22:13574inline
Howard Hinnant3e519522010-05-11 19:42:16575bool
Howard Hinnantd368a842011-05-27 20:52:28576__bitset<1, _Size>::all() const _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16577{
578 __storage_type __m = ~__storage_type(0) >> (__bits_per_word - _Size);
579 return !(~__first_ & __m);
580}
581
582template <size_t _Size>
Evgeniy Stepanov906c8722015-11-07 01:22:13583inline
Howard Hinnant3e519522010-05-11 19:42:16584bool
Howard Hinnantd368a842011-05-27 20:52:28585__bitset<1, _Size>::any() const _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16586{
587 __storage_type __m = ~__storage_type(0) >> (__bits_per_word - _Size);
588 return __first_ & __m;
589}
590
591template <size_t _Size>
Evgeniy Stepanov906c8722015-11-07 01:22:13592inline
Howard Hinnant3e519522010-05-11 19:42:16593size_t
Howard Hinnantd368a842011-05-27 20:52:28594__bitset<1, _Size>::__hash_code() const _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16595{
596 return __first_;
597}
598
599template <>
600class __bitset<0, 0>
601{
602public:
603 typedef ptrdiff_t difference_type;
604 typedef size_t size_type;
Howard Hinnant0ae9efe2012-05-07 16:50:38605 typedef size_type __storage_type;
Howard Hinnant3e519522010-05-11 19:42:16606protected:
607 typedef __bitset __self;
Howard Hinnant3e519522010-05-11 19:42:16608 typedef __storage_type* __storage_pointer;
609 typedef const __storage_type* __const_storage_pointer;
610 static const unsigned __bits_per_word = static_cast<unsigned>(sizeof(__storage_type) * CHAR_BIT);
611
612 friend class __bit_reference<__bitset>;
613 friend class __bit_const_reference<__bitset>;
614 friend class __bit_iterator<__bitset, false>;
615 friend class __bit_iterator<__bitset, true>;
Howard Hinnantc2063662011-12-01 20:21:04616 friend struct __bit_array<__bitset>;
Howard Hinnant3e519522010-05-11 19:42:16617
618 typedef __bit_reference<__bitset> reference;
619 typedef __bit_const_reference<__bitset> const_reference;
620 typedef __bit_iterator<__bitset, false> iterator;
621 typedef __bit_iterator<__bitset, true> const_iterator;
622
Evgeniy Stepanov906c8722015-11-07 01:22:13623 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanteeac9fc2012-07-07 17:04:52624 _LIBCPP_CONSTEXPR __bitset() _NOEXCEPT;
Evgeniy Stepanov906c8722015-11-07 01:22:13625 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanteeac9fc2012-07-07 17:04:52626 explicit _LIBCPP_CONSTEXPR __bitset(unsigned long long) _NOEXCEPT;
Howard Hinnant3e519522010-05-11 19:42:16627
Howard Hinnantd368a842011-05-27 20:52:28628 _LIBCPP_INLINE_VISIBILITY reference __make_ref(size_t) _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16629 {return reference(0, 1);}
Howard Hinnanteeac9fc2012-07-07 17:04:52630 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR const_reference __make_ref(size_t) const _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16631 {return const_reference(0, 1);}
Howard Hinnantc2063662011-12-01 20:21:04632 _LIBCPP_INLINE_VISIBILITY iterator __make_iter(size_t) _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16633 {return iterator(0, 0);}
Howard Hinnantc2063662011-12-01 20:21:04634 _LIBCPP_INLINE_VISIBILITY const_iterator __make_iter(size_t) const _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16635 {return const_iterator(0, 0);}
636
Howard Hinnantd368a842011-05-27 20:52:28637 _LIBCPP_INLINE_VISIBILITY void operator&=(const __bitset&) _NOEXCEPT {}
638 _LIBCPP_INLINE_VISIBILITY void operator|=(const __bitset&) _NOEXCEPT {}
639 _LIBCPP_INLINE_VISIBILITY void operator^=(const __bitset&) _NOEXCEPT {}
Howard Hinnant3e519522010-05-11 19:42:16640
Howard Hinnantd368a842011-05-27 20:52:28641 _LIBCPP_INLINE_VISIBILITY void flip() _NOEXCEPT {}
Howard Hinnant3e519522010-05-11 19:42:16642
643 _LIBCPP_INLINE_VISIBILITY unsigned long to_ulong() const {return 0;}
644 _LIBCPP_INLINE_VISIBILITY unsigned long long to_ullong() const {return 0;}
645
Howard Hinnantd368a842011-05-27 20:52:28646 _LIBCPP_INLINE_VISIBILITY bool all() const _NOEXCEPT {return true;}
647 _LIBCPP_INLINE_VISIBILITY bool any() const _NOEXCEPT {return false;}
Howard Hinnant3e519522010-05-11 19:42:16648
Howard Hinnantd368a842011-05-27 20:52:28649 _LIBCPP_INLINE_VISIBILITY size_t __hash_code() const _NOEXCEPT {return 0;}
Howard Hinnant3e519522010-05-11 19:42:16650};
651
Evgeniy Stepanov906c8722015-11-07 01:22:13652inline
Howard Hinnanteeac9fc2012-07-07 17:04:52653_LIBCPP_CONSTEXPR
Howard Hinnantd368a842011-05-27 20:52:28654__bitset<0, 0>::__bitset() _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16655{
656}
657
Evgeniy Stepanov906c8722015-11-07 01:22:13658inline
Howard Hinnanteeac9fc2012-07-07 17:04:52659_LIBCPP_CONSTEXPR
Howard Hinnantd368a842011-05-27 20:52:28660__bitset<0, 0>::__bitset(unsigned long long) _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16661{
662}
663
Eric Fiseliere2f2d1ed2017-01-04 23:56:00664template <size_t _Size> class _LIBCPP_TEMPLATE_VIS bitset;
Eric Fiselier89dd1dd2016-04-21 22:54:21665template <size_t _Size> struct hash<bitset<_Size> >;
Howard Hinnant3e519522010-05-11 19:42:16666
667template <size_t _Size>
Eric Fiseliere2f2d1ed2017-01-04 23:56:00668class _LIBCPP_TEMPLATE_VIS bitset
Howard Hinnant3e519522010-05-11 19:42:16669 : private __bitset<_Size == 0 ? 0 : (_Size - 1) / (sizeof(size_t) * CHAR_BIT) + 1, _Size>
670{
Howard Hinnant53b9ee02013-03-06 17:30:26671public:
Howard Hinnant3e519522010-05-11 19:42:16672 static const unsigned __n_words = _Size == 0 ? 0 : (_Size - 1) / (sizeof(size_t) * CHAR_BIT) + 1;
673 typedef __bitset<__n_words, _Size> base;
674
Howard Hinnantb3371f62010-08-22 00:02:43675public:
Howard Hinnant3e519522010-05-11 19:42:16676 typedef typename base::reference reference;
677 typedef typename base::const_reference const_reference;
678
Howard Hinnantb3371f62010-08-22 00:02:43679 // 23.3.5.1 constructors:
Howard Hinnanteeac9fc2012-07-07 17:04:52680 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR bitset() _NOEXCEPT {}
681 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
682 bitset(unsigned long long __v) _NOEXCEPT : base(__v) {}
Howard Hinnantd09f7112010-11-17 21:53:14683 template<class _CharT>
684 explicit bitset(const _CharT* __str,
685 typename basic_string<_CharT>::size_type __n = basic_string<_CharT>::npos,
686 _CharT __zero = _CharT('0'), _CharT __one = _CharT('1'));
Howard Hinnantb3371f62010-08-22 00:02:43687 template<class _CharT, class _Traits, class _Allocator>
688 explicit bitset(const basic_string<_CharT,_Traits,_Allocator>& __str,
689 typename basic_string<_CharT,_Traits,_Allocator>::size_type __pos = 0,
690 typename basic_string<_CharT,_Traits,_Allocator>::size_type __n =
Howard Hinnant3e519522010-05-11 19:42:16691 (basic_string<_CharT,_Traits,_Allocator>::npos),
Howard Hinnantb3371f62010-08-22 00:02:43692 _CharT __zero = _CharT('0'), _CharT __one = _CharT('1'));
Howard Hinnant3e519522010-05-11 19:42:16693
Howard Hinnantb3371f62010-08-22 00:02:43694 // 23.3.5.2 bitset operations:
Evgeniy Stepanov906c8722015-11-07 01:22:13695 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd368a842011-05-27 20:52:28696 bitset& operator&=(const bitset& __rhs) _NOEXCEPT;
Evgeniy Stepanov906c8722015-11-07 01:22:13697 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd368a842011-05-27 20:52:28698 bitset& operator|=(const bitset& __rhs) _NOEXCEPT;
Evgeniy Stepanov906c8722015-11-07 01:22:13699 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd368a842011-05-27 20:52:28700 bitset& operator^=(const bitset& __rhs) _NOEXCEPT;
701 bitset& operator<<=(size_t __pos) _NOEXCEPT;
702 bitset& operator>>=(size_t __pos) _NOEXCEPT;
Evgeniy Stepanov906c8722015-11-07 01:22:13703 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd368a842011-05-27 20:52:28704 bitset& set() _NOEXCEPT;
Howard Hinnantb3371f62010-08-22 00:02:43705 bitset& set(size_t __pos, bool __val = true);
Evgeniy Stepanov906c8722015-11-07 01:22:13706 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd368a842011-05-27 20:52:28707 bitset& reset() _NOEXCEPT;
Howard Hinnantb3371f62010-08-22 00:02:43708 bitset& reset(size_t __pos);
Evgeniy Stepanov906c8722015-11-07 01:22:13709 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd368a842011-05-27 20:52:28710 bitset operator~() const _NOEXCEPT;
Evgeniy Stepanov906c8722015-11-07 01:22:13711 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd368a842011-05-27 20:52:28712 bitset& flip() _NOEXCEPT;
Howard Hinnantb3371f62010-08-22 00:02:43713 bitset& flip(size_t __pos);
Howard Hinnant3e519522010-05-11 19:42:16714
Howard Hinnantb3371f62010-08-22 00:02:43715 // element access:
Howard Hinnanteeac9fc2012-07-07 17:04:52716 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
717 const_reference operator[](size_t __p) const {return base::__make_ref(__p);}
Howard Hinnant3e519522010-05-11 19:42:16718 _LIBCPP_INLINE_VISIBILITY reference operator[](size_t __p) {return base::__make_ref(__p);}
Evgeniy Stepanov906c8722015-11-07 01:22:13719 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb3371f62010-08-22 00:02:43720 unsigned long to_ulong() const;
Evgeniy Stepanov906c8722015-11-07 01:22:13721 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb3371f62010-08-22 00:02:43722 unsigned long long to_ullong() const;
723 template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant3e519522010-05-11 19:42:16724 basic_string<_CharT, _Traits, _Allocator> to_string(_CharT __zero = _CharT('0'),
Howard Hinnantb3371f62010-08-22 00:02:43725 _CharT __one = _CharT('1')) const;
726 template <class _CharT, class _Traits>
Evgeniy Stepanov906c8722015-11-07 01:22:13727 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16728 basic_string<_CharT, _Traits, allocator<_CharT> > to_string(_CharT __zero = _CharT('0'),
Howard Hinnantb3371f62010-08-22 00:02:43729 _CharT __one = _CharT('1')) const;
730 template <class _CharT>
Evgeniy Stepanov906c8722015-11-07 01:22:13731 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16732 basic_string<_CharT, char_traits<_CharT>, allocator<_CharT> > to_string(_CharT __zero = _CharT('0'),
Howard Hinnantb3371f62010-08-22 00:02:43733 _CharT __one = _CharT('1')) const;
Evgeniy Stepanov906c8722015-11-07 01:22:13734 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16735 basic_string<char, char_traits<char>, allocator<char> > to_string(char __zero = '0',
Howard Hinnantb3371f62010-08-22 00:02:43736 char __one = '1') const;
Evgeniy Stepanov906c8722015-11-07 01:22:13737 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd368a842011-05-27 20:52:28738 size_t count() const _NOEXCEPT;
Howard Hinnanteeac9fc2012-07-07 17:04:52739 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR size_t size() const _NOEXCEPT {return _Size;}
Evgeniy Stepanov906c8722015-11-07 01:22:13740 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd368a842011-05-27 20:52:28741 bool operator==(const bitset& __rhs) const _NOEXCEPT;
Evgeniy Stepanov906c8722015-11-07 01:22:13742 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd368a842011-05-27 20:52:28743 bool operator!=(const bitset& __rhs) const _NOEXCEPT;
Howard Hinnantb3371f62010-08-22 00:02:43744 bool test(size_t __pos) const;
Evgeniy Stepanov906c8722015-11-07 01:22:13745 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd368a842011-05-27 20:52:28746 bool all() const _NOEXCEPT;
Evgeniy Stepanov906c8722015-11-07 01:22:13747 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd368a842011-05-27 20:52:28748 bool any() const _NOEXCEPT;
749 _LIBCPP_INLINE_VISIBILITY bool none() const _NOEXCEPT {return !any();}
Evgeniy Stepanov906c8722015-11-07 01:22:13750 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd368a842011-05-27 20:52:28751 bitset operator<<(size_t __pos) const _NOEXCEPT;
Evgeniy Stepanov906c8722015-11-07 01:22:13752 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd368a842011-05-27 20:52:28753 bitset operator>>(size_t __pos) const _NOEXCEPT;
Howard Hinnant3e519522010-05-11 19:42:16754
755private:
756
Howard Hinnantfb100022010-09-21 21:28:23757 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd368a842011-05-27 20:52:28758 size_t __hash_code() const _NOEXCEPT {return base::__hash_code();}
Howard Hinnant3e519522010-05-11 19:42:16759
760 friend struct hash<bitset>;
761};
762
763template <size_t _Size>
Howard Hinnantd09f7112010-11-17 21:53:14764template<class _CharT>
765bitset<_Size>::bitset(const _CharT* __str,
766 typename basic_string<_CharT>::size_type __n,
767 _CharT __zero, _CharT __one)
Howard Hinnant3e519522010-05-11 19:42:16768{
Howard Hinnantce48a112011-06-30 21:18:19769 size_t __rlen = _VSTD::min(__n, char_traits<_CharT>::length(__str));
Howard Hinnant3e519522010-05-11 19:42:16770 for (size_t __i = 0; __i < __rlen; ++__i)
Howard Hinnantd09f7112010-11-17 21:53:14771 if (__str[__i] != __zero && __str[__i] != __one)
Marshall Clowd437fa52016-08-25 15:09:01772 __throw_invalid_argument("bitset string ctor has invalid argument");
773
Howard Hinnantc003db12011-11-29 18:15:50774 size_t _Mp = _VSTD::min(__rlen, _Size);
Howard Hinnant3e519522010-05-11 19:42:16775 size_t __i = 0;
Howard Hinnantc003db12011-11-29 18:15:50776 for (; __i < _Mp; ++__i)
Howard Hinnant3e519522010-05-11 19:42:16777 {
Howard Hinnantc003db12011-11-29 18:15:50778 _CharT __c = __str[_Mp - 1 - __i];
Howard Hinnantd09f7112010-11-17 21:53:14779 if (__c == __zero)
Howard Hinnant3e519522010-05-11 19:42:16780 (*this)[__i] = false;
Howard Hinnantd09f7112010-11-17 21:53:14781 else
Howard Hinnant3e519522010-05-11 19:42:16782 (*this)[__i] = true;
Howard Hinnant3e519522010-05-11 19:42:16783 }
Howard Hinnantce48a112011-06-30 21:18:19784 _VSTD::fill(base::__make_iter(__i), base::__make_iter(_Size), false);
Howard Hinnant3e519522010-05-11 19:42:16785}
786
787template <size_t _Size>
Howard Hinnantb3371f62010-08-22 00:02:43788template<class _CharT, class _Traits, class _Allocator>
789bitset<_Size>::bitset(const basic_string<_CharT,_Traits,_Allocator>& __str,
790 typename basic_string<_CharT,_Traits,_Allocator>::size_type __pos,
Howard Hinnant3e519522010-05-11 19:42:16791 typename basic_string<_CharT,_Traits,_Allocator>::size_type __n,
792 _CharT __zero, _CharT __one)
793{
794 if (__pos > __str.size())
Marshall Clowd437fa52016-08-25 15:09:01795 __throw_out_of_range("bitset string pos out of range");
796
Howard Hinnantce48a112011-06-30 21:18:19797 size_t __rlen = _VSTD::min(__n, __str.size() - __pos);
Howard Hinnant3e519522010-05-11 19:42:16798 for (size_t __i = __pos; __i < __pos + __rlen; ++__i)
799 if (!_Traits::eq(__str[__i], __zero) && !_Traits::eq(__str[__i], __one))
Marshall Clowd437fa52016-08-25 15:09:01800 __throw_invalid_argument("bitset string ctor has invalid argument");
801
Howard Hinnantc003db12011-11-29 18:15:50802 size_t _Mp = _VSTD::min(__rlen, _Size);
Howard Hinnant3e519522010-05-11 19:42:16803 size_t __i = 0;
Howard Hinnantc003db12011-11-29 18:15:50804 for (; __i < _Mp; ++__i)
Howard Hinnant3e519522010-05-11 19:42:16805 {
Howard Hinnantc003db12011-11-29 18:15:50806 _CharT __c = __str[__pos + _Mp - 1 - __i];
Howard Hinnant3e519522010-05-11 19:42:16807 if (_Traits::eq(__c, __zero))
808 (*this)[__i] = false;
809 else
810 (*this)[__i] = true;
811 }
Howard Hinnantce48a112011-06-30 21:18:19812 _VSTD::fill(base::__make_iter(__i), base::__make_iter(_Size), false);
Howard Hinnant3e519522010-05-11 19:42:16813}
814
815template <size_t _Size>
Evgeniy Stepanov906c8722015-11-07 01:22:13816inline
Howard Hinnant3e519522010-05-11 19:42:16817bitset<_Size>&
Howard Hinnantd368a842011-05-27 20:52:28818bitset<_Size>::operator&=(const bitset& __rhs) _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16819{
820 base::operator&=(__rhs);
821 return *this;
822}
823
824template <size_t _Size>
Evgeniy Stepanov906c8722015-11-07 01:22:13825inline
Howard Hinnant3e519522010-05-11 19:42:16826bitset<_Size>&
Howard Hinnantd368a842011-05-27 20:52:28827bitset<_Size>::operator|=(const bitset& __rhs) _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16828{
829 base::operator|=(__rhs);
830 return *this;
831}
832
833template <size_t _Size>
Evgeniy Stepanov906c8722015-11-07 01:22:13834inline
Howard Hinnant3e519522010-05-11 19:42:16835bitset<_Size>&
Howard Hinnantd368a842011-05-27 20:52:28836bitset<_Size>::operator^=(const bitset& __rhs) _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16837{
838 base::operator^=(__rhs);
839 return *this;
840}
841
842template <size_t _Size>
843bitset<_Size>&
Howard Hinnantd368a842011-05-27 20:52:28844bitset<_Size>::operator<<=(size_t __pos) _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16845{
Howard Hinnantce48a112011-06-30 21:18:19846 __pos = _VSTD::min(__pos, _Size);
847 _VSTD::copy_backward(base::__make_iter(0), base::__make_iter(_Size - __pos), base::__make_iter(_Size));
848 _VSTD::fill_n(base::__make_iter(0), __pos, false);
Howard Hinnant3e519522010-05-11 19:42:16849 return *this;
850}
851
852template <size_t _Size>
853bitset<_Size>&
Howard Hinnantd368a842011-05-27 20:52:28854bitset<_Size>::operator>>=(size_t __pos) _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16855{
Howard Hinnantce48a112011-06-30 21:18:19856 __pos = _VSTD::min(__pos, _Size);
857 _VSTD::copy(base::__make_iter(__pos), base::__make_iter(_Size), base::__make_iter(0));
858 _VSTD::fill_n(base::__make_iter(_Size - __pos), __pos, false);
Howard Hinnant3e519522010-05-11 19:42:16859 return *this;
860}
861
862template <size_t _Size>
Evgeniy Stepanov906c8722015-11-07 01:22:13863inline
Howard Hinnant3e519522010-05-11 19:42:16864bitset<_Size>&
Howard Hinnantd368a842011-05-27 20:52:28865bitset<_Size>::set() _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16866{
Howard Hinnantce48a112011-06-30 21:18:19867 _VSTD::fill_n(base::__make_iter(0), _Size, true);
Howard Hinnant3e519522010-05-11 19:42:16868 return *this;
869}
870
871template <size_t _Size>
872bitset<_Size>&
873bitset<_Size>::set(size_t __pos, bool __val)
874{
875 if (__pos >= _Size)
Marshall Clowd437fa52016-08-25 15:09:01876 __throw_out_of_range("bitset set argument out of range");
877
Howard Hinnant3e519522010-05-11 19:42:16878 (*this)[__pos] = __val;
879 return *this;
880}
881
882template <size_t _Size>
Evgeniy Stepanov906c8722015-11-07 01:22:13883inline
Howard Hinnant3e519522010-05-11 19:42:16884bitset<_Size>&
Howard Hinnantd368a842011-05-27 20:52:28885bitset<_Size>::reset() _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16886{
Howard Hinnantce48a112011-06-30 21:18:19887 _VSTD::fill_n(base::__make_iter(0), _Size, false);
Howard Hinnant3e519522010-05-11 19:42:16888 return *this;
889}
890
891template <size_t _Size>
892bitset<_Size>&
893bitset<_Size>::reset(size_t __pos)
894{
895 if (__pos >= _Size)
Marshall Clowd437fa52016-08-25 15:09:01896 __throw_out_of_range("bitset reset argument out of range");
897
Howard Hinnant3e519522010-05-11 19:42:16898 (*this)[__pos] = false;
899 return *this;
900}
901
902template <size_t _Size>
Evgeniy Stepanov906c8722015-11-07 01:22:13903inline
Howard Hinnant3e519522010-05-11 19:42:16904bitset<_Size>
Howard Hinnantd368a842011-05-27 20:52:28905bitset<_Size>::operator~() const _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16906{
907 bitset __x(*this);
908 __x.flip();
909 return __x;
910}
911
912template <size_t _Size>
Evgeniy Stepanov906c8722015-11-07 01:22:13913inline
Howard Hinnant3e519522010-05-11 19:42:16914bitset<_Size>&
Howard Hinnantd368a842011-05-27 20:52:28915bitset<_Size>::flip() _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16916{
917 base::flip();
918 return *this;
919}
920
921template <size_t _Size>
922bitset<_Size>&
923bitset<_Size>::flip(size_t __pos)
924{
925 if (__pos >= _Size)
Marshall Clowd437fa52016-08-25 15:09:01926 __throw_out_of_range("bitset flip argument out of range");
927
Howard Hinnant3e519522010-05-11 19:42:16928 reference r = base::__make_ref(__pos);
929 r = ~r;
930 return *this;
931}
932
933template <size_t _Size>
Evgeniy Stepanov906c8722015-11-07 01:22:13934inline
Howard Hinnant3e519522010-05-11 19:42:16935unsigned long
936bitset<_Size>::to_ulong() const
937{
938 return base::to_ulong();
939}
940
941template <size_t _Size>
Evgeniy Stepanov906c8722015-11-07 01:22:13942inline
Howard Hinnant3e519522010-05-11 19:42:16943unsigned long long
944bitset<_Size>::to_ullong() const
945{
946 return base::to_ullong();
947}
948
949template <size_t _Size>
Howard Hinnantb3371f62010-08-22 00:02:43950template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant3e519522010-05-11 19:42:16951basic_string<_CharT, _Traits, _Allocator>
952bitset<_Size>::to_string(_CharT __zero, _CharT __one) const
953{
954 basic_string<_CharT, _Traits, _Allocator> __r(_Size, __zero);
955 for (size_t __i = 0; __i < _Size; ++__i)
956 {
957 if ((*this)[__i])
958 __r[_Size - 1 - __i] = __one;
959 }
960 return __r;
961}
962
963template <size_t _Size>
Howard Hinnantb3371f62010-08-22 00:02:43964template <class _CharT, class _Traits>
Evgeniy Stepanov906c8722015-11-07 01:22:13965inline
Howard Hinnant3e519522010-05-11 19:42:16966basic_string<_CharT, _Traits, allocator<_CharT> >
967bitset<_Size>::to_string(_CharT __zero, _CharT __one) const
968{
969 return to_string<_CharT, _Traits, allocator<_CharT> >(__zero, __one);
970}
971
972template <size_t _Size>
Howard Hinnantb3371f62010-08-22 00:02:43973template <class _CharT>
Evgeniy Stepanov906c8722015-11-07 01:22:13974inline
Howard Hinnant3e519522010-05-11 19:42:16975basic_string<_CharT, char_traits<_CharT>, allocator<_CharT> >
976bitset<_Size>::to_string(_CharT __zero, _CharT __one) const
977{
978 return to_string<_CharT, char_traits<_CharT>, allocator<_CharT> >(__zero, __one);
979}
980
981template <size_t _Size>
Evgeniy Stepanov906c8722015-11-07 01:22:13982inline
Howard Hinnant3e519522010-05-11 19:42:16983basic_string<char, char_traits<char>, allocator<char> >
984bitset<_Size>::to_string(char __zero, char __one) const
985{
986 return to_string<char, char_traits<char>, allocator<char> >(__zero, __one);
987}
988
989template <size_t _Size>
Evgeniy Stepanov906c8722015-11-07 01:22:13990inline
Howard Hinnant3e519522010-05-11 19:42:16991size_t
Howard Hinnantd368a842011-05-27 20:52:28992bitset<_Size>::count() const _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16993{
Howard Hinnantce48a112011-06-30 21:18:19994 return static_cast<size_t>(_VSTD::count(base::__make_iter(0), base::__make_iter(_Size), true));
Howard Hinnant3e519522010-05-11 19:42:16995}
996
997template <size_t _Size>
Evgeniy Stepanov906c8722015-11-07 01:22:13998inline
Howard Hinnant3e519522010-05-11 19:42:16999bool
Howard Hinnantd368a842011-05-27 20:52:281000bitset<_Size>::operator==(const bitset& __rhs) const _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:161001{
Howard Hinnantce48a112011-06-30 21:18:191002 return _VSTD::equal(base::__make_iter(0), base::__make_iter(_Size), __rhs.__make_iter(0));
Howard Hinnant3e519522010-05-11 19:42:161003}
1004
1005template <size_t _Size>
Evgeniy Stepanov906c8722015-11-07 01:22:131006inline
Howard Hinnant3e519522010-05-11 19:42:161007bool
Howard Hinnantd368a842011-05-27 20:52:281008bitset<_Size>::operator!=(const bitset& __rhs) const _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:161009{
1010 return !(*this == __rhs);
1011}
1012
1013template <size_t _Size>
1014bool
1015bitset<_Size>::test(size_t __pos) const
1016{
1017 if (__pos >= _Size)
Marshall Clowd437fa52016-08-25 15:09:011018 __throw_out_of_range("bitset test argument out of range");
1019
Howard Hinnant3e519522010-05-11 19:42:161020 return (*this)[__pos];
1021}
1022
1023template <size_t _Size>
Evgeniy Stepanov906c8722015-11-07 01:22:131024inline
Howard Hinnant3e519522010-05-11 19:42:161025bool
Howard Hinnantd368a842011-05-27 20:52:281026bitset<_Size>::all() const _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:161027{
1028 return base::all();
1029}
1030
1031template <size_t _Size>
Evgeniy Stepanov906c8722015-11-07 01:22:131032inline
Howard Hinnant3e519522010-05-11 19:42:161033bool
Howard Hinnantd368a842011-05-27 20:52:281034bitset<_Size>::any() const _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:161035{
1036 return base::any();
1037}
1038
1039template <size_t _Size>
Evgeniy Stepanov906c8722015-11-07 01:22:131040inline
Howard Hinnant3e519522010-05-11 19:42:161041bitset<_Size>
Howard Hinnantd368a842011-05-27 20:52:281042bitset<_Size>::operator<<(size_t __pos) const _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:161043{
1044 bitset __r = *this;
1045 __r <<= __pos;
1046 return __r;
1047}
1048
1049template <size_t _Size>
Evgeniy Stepanov906c8722015-11-07 01:22:131050inline
Howard Hinnant3e519522010-05-11 19:42:161051bitset<_Size>
Howard Hinnantd368a842011-05-27 20:52:281052bitset<_Size>::operator>>(size_t __pos) const _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:161053{
1054 bitset __r = *this;
1055 __r >>= __pos;
1056 return __r;
1057}
1058
1059template <size_t _Size>
1060inline _LIBCPP_INLINE_VISIBILITY
1061bitset<_Size>
Howard Hinnantd368a842011-05-27 20:52:281062operator&(const bitset<_Size>& __x, const bitset<_Size>& __y) _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:161063{
1064 bitset<_Size> __r = __x;
1065 __r &= __y;
1066 return __r;
1067}
1068
1069template <size_t _Size>
1070inline _LIBCPP_INLINE_VISIBILITY
1071bitset<_Size>
Howard Hinnantd368a842011-05-27 20:52:281072operator|(const bitset<_Size>& __x, const bitset<_Size>& __y) _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:161073{
1074 bitset<_Size> __r = __x;
1075 __r |= __y;
1076 return __r;
1077}
1078
1079template <size_t _Size>
1080inline _LIBCPP_INLINE_VISIBILITY
1081bitset<_Size>
Howard Hinnantd368a842011-05-27 20:52:281082operator^(const bitset<_Size>& __x, const bitset<_Size>& __y) _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:161083{
1084 bitset<_Size> __r = __x;
1085 __r ^= __y;
1086 return __r;
1087}
1088
1089template <size_t _Size>
Eric Fiseliere2f2d1ed2017-01-04 23:56:001090struct _LIBCPP_TEMPLATE_VIS hash<bitset<_Size> >
Howard Hinnant3e519522010-05-11 19:42:161091 : public unary_function<bitset<_Size>, size_t>
1092{
Howard Hinnantfb100022010-09-21 21:28:231093 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd368a842011-05-27 20:52:281094 size_t operator()(const bitset<_Size>& __bs) const _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:161095 {return __bs.__hash_code();}
1096};
1097
Howard Hinnante3163f52011-07-18 15:51:591098template <class _CharT, class _Traits, size_t _Size>
1099basic_istream<_CharT, _Traits>&
1100operator>>(basic_istream<_CharT, _Traits>& __is, bitset<_Size>& __x);
1101
1102template <class _CharT, class _Traits, size_t _Size>
1103basic_ostream<_CharT, _Traits>&
1104operator<<(basic_ostream<_CharT, _Traits>& __os, const bitset<_Size>& __x);
1105
Howard Hinnant3e519522010-05-11 19:42:161106_LIBCPP_END_NAMESPACE_STD
1107
Eric Fiseliera016efb2017-05-31 22:07:491108_LIBCPP_POP_MACROS
1109
Howard Hinnant3e519522010-05-11 19:42:161110#endif // _LIBCPP_BITSET