blob: b7d95a811f38e571455e5c508ad9b4e0a99e7370 [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 Hinnant073458b2011-10-17 20:05:10116#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnant3e519522010-05-11 19:42:16117#pragma GCC system_header
Howard Hinnant073458b2011-10-17 20:05:10118#endif
Howard Hinnant3e519522010-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>
128#if defined(_LIBCPP_NO_EXCEPTIONS)
129 #include <cassert>
130#endif
131
Howard Hinnantab4f4382011-11-29 16:45:27132#include <__undef_min_max>
133
Howard Hinnant3e519522010-05-11 19:42:16134_LIBCPP_BEGIN_NAMESPACE_STD
135
136template <size_t _N_words, size_t _Size>
Howard Hinnanta7744562011-07-02 20:33:23137class __bitset;
138
139template <size_t _N_words, size_t _Size>
140struct __has_storage_type<__bitset<_N_words, _Size> >
141{
142 static const bool value = true;
143};
144
145template <size_t _N_words, size_t _Size>
Howard Hinnant3e519522010-05-11 19:42:16146class __bitset
147{
148public:
149 typedef ptrdiff_t difference_type;
150 typedef size_t size_type;
Howard Hinnant0ae9efe2012-05-07 16:50:38151 typedef size_type __storage_type;
Howard Hinnant3e519522010-05-11 19:42:16152protected:
153 typedef __bitset __self;
Howard Hinnant3e519522010-05-11 19:42:16154 typedef __storage_type* __storage_pointer;
155 typedef const __storage_type* __const_storage_pointer;
156 static const unsigned __bits_per_word = static_cast<unsigned>(sizeof(__storage_type) * CHAR_BIT);
157
158 friend class __bit_reference<__bitset>;
159 friend class __bit_const_reference<__bitset>;
160 friend class __bit_iterator<__bitset, false>;
161 friend class __bit_iterator<__bitset, true>;
Howard Hinnant7ee27132012-08-17 17:10:18162 friend struct __bit_array<__bitset>;
Howard Hinnant3e519522010-05-11 19:42:16163
164 __storage_type __first_[_N_words];
165
166 typedef __bit_reference<__bitset> reference;
167 typedef __bit_const_reference<__bitset> const_reference;
168 typedef __bit_iterator<__bitset, false> iterator;
169 typedef __bit_iterator<__bitset, true> const_iterator;
170
Evgeniy Stepanov906c8722015-11-07 01:22:13171 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanteeac9fc2012-07-07 17:04:52172 _LIBCPP_CONSTEXPR __bitset() _NOEXCEPT;
Evgeniy Stepanov906c8722015-11-07 01:22:13173 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanteeac9fc2012-07-07 17:04:52174 explicit _LIBCPP_CONSTEXPR __bitset(unsigned long long __v) _NOEXCEPT;
Howard Hinnant3e519522010-05-11 19:42:16175
Howard Hinnantd368a842011-05-27 20:52:28176 _LIBCPP_INLINE_VISIBILITY reference __make_ref(size_t __pos) _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16177 {return reference(__first_ + __pos / __bits_per_word, __storage_type(1) << __pos % __bits_per_word);}
Howard Hinnanteeac9fc2012-07-07 17:04:52178 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR const_reference __make_ref(size_t __pos) const _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16179 {return const_reference(__first_ + __pos / __bits_per_word, __storage_type(1) << __pos % __bits_per_word);}
Howard Hinnantd368a842011-05-27 20:52:28180 _LIBCPP_INLINE_VISIBILITY iterator __make_iter(size_t __pos) _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16181 {return iterator(__first_ + __pos / __bits_per_word, __pos % __bits_per_word);}
Howard Hinnantd368a842011-05-27 20:52:28182 _LIBCPP_INLINE_VISIBILITY const_iterator __make_iter(size_t __pos) const _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16183 {return const_iterator(__first_ + __pos / __bits_per_word, __pos % __bits_per_word);}
184
Evgeniy Stepanov906c8722015-11-07 01:22:13185 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd368a842011-05-27 20:52:28186 void operator&=(const __bitset& __v) _NOEXCEPT;
Evgeniy Stepanov906c8722015-11-07 01:22:13187 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd368a842011-05-27 20:52:28188 void operator|=(const __bitset& __v) _NOEXCEPT;
Evgeniy Stepanov906c8722015-11-07 01:22:13189 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd368a842011-05-27 20:52:28190 void operator^=(const __bitset& __v) _NOEXCEPT;
Howard Hinnant3e519522010-05-11 19:42:16191
Howard Hinnantd368a842011-05-27 20:52:28192 void flip() _NOEXCEPT;
Howard Hinnant3e519522010-05-11 19:42:16193 _LIBCPP_INLINE_VISIBILITY unsigned long to_ulong() const
194 {return to_ulong(integral_constant<bool, _Size < sizeof(unsigned long) * CHAR_BIT>());}
195 _LIBCPP_INLINE_VISIBILITY unsigned long long to_ullong() const
196 {return to_ullong(integral_constant<bool, _Size < sizeof(unsigned long long) * CHAR_BIT>());}
197
Howard Hinnantd368a842011-05-27 20:52:28198 bool all() const _NOEXCEPT;
199 bool any() const _NOEXCEPT;
Evgeniy Stepanov906c8722015-11-07 01:22:13200 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd368a842011-05-27 20:52:28201 size_t __hash_code() const _NOEXCEPT;
Howard Hinnant3e519522010-05-11 19:42:16202private:
Howard Hinnanteeac9fc2012-07-07 17:04:52203#ifdef _LIBCPP_HAS_NO_CONSTEXPR
Howard Hinnantd368a842011-05-27 20:52:28204 void __init(unsigned long long __v, false_type) _NOEXCEPT;
205 void __init(unsigned long long __v, true_type) _NOEXCEPT;
Howard Hinnanteeac9fc2012-07-07 17:04:52206#endif // _LIBCPP_HAS_NO_CONSTEXPR
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
Howard Hinnanteeac9fc2012-07-07 17:04:52222#ifndef _LIBCPP_HAS_NO_CONSTEXPR
223 : __first_{0}
224#endif
Howard Hinnant3e519522010-05-11 19:42:16225{
Howard Hinnanteeac9fc2012-07-07 17:04:52226#ifdef _LIBCPP_HAS_NO_CONSTEXPR
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
Howard Hinnanteeac9fc2012-07-07 17:04:52231#ifdef _LIBCPP_HAS_NO_CONSTEXPR
232
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)];
238 for (size_t __i = 0; __i < sizeof(__t)/sizeof(__t[0]); ++__i, __v >>= __bits_per_word)
239 __t[__i] = static_cast<__storage_type>(__v);
Howard Hinnantce48a112011-06-30 21:18:19240 _VSTD::copy(__t, __t + sizeof(__t)/sizeof(__t[0]), __first_);
241 _VSTD::fill(__first_ + sizeof(__t)/sizeof(__t[0]), __first_ + sizeof(__first_)/sizeof(__first_[0]),
Howard Hinnant3e519522010-05-11 19:42:16242 __storage_type(0));
243}
244
245template <size_t _N_words, size_t _Size>
246inline _LIBCPP_INLINE_VISIBILITY
247void
Howard Hinnantc2063662011-12-01 20:21:04248__bitset<_N_words, _Size>::__init(unsigned long long __v, true_type) _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16249{
250 __first_[0] = __v;
Howard Hinnantce48a112011-06-30 21:18:19251 _VSTD::fill(__first_ + 1, __first_ + sizeof(__first_)/sizeof(__first_[0]), __storage_type(0));
Howard Hinnant3e519522010-05-11 19:42:16252}
253
Howard Hinnanteeac9fc2012-07-07 17:04:52254#endif // _LIBCPP_HAS_NO_CONSTEXPR
255
Howard Hinnant3e519522010-05-11 19:42:16256template <size_t _N_words, size_t _Size>
Evgeniy Stepanov906c8722015-11-07 01:22:13257inline
Howard Hinnanteeac9fc2012-07-07 17:04:52258_LIBCPP_CONSTEXPR
Howard Hinnantd368a842011-05-27 20:52:28259__bitset<_N_words, _Size>::__bitset(unsigned long long __v) _NOEXCEPT
Howard Hinnanteeac9fc2012-07-07 17:04:52260#ifndef _LIBCPP_HAS_NO_CONSTEXPR
Nico Weber1d1b46c2014-06-04 15:46:56261#if __SIZEOF_SIZE_T__ == 8
Howard Hinnanteeac9fc2012-07-07 17:04:52262 : __first_{__v}
Nico Weber1d1b46c2014-06-04 15:46:56263#elif __SIZEOF_SIZE_T__ == 4
Howard Hinnant53b9ee02013-03-06 17:30:26264 : __first_{__v, __v >> __bits_per_word}
Howard Hinnant3fb6c6e2013-03-06 18:16:12265#else
Howard Hinnant53b9ee02013-03-06 17:30:26266#error This constructor has not been ported to this platform
267#endif
Howard Hinnanteeac9fc2012-07-07 17:04:52268#endif
Howard Hinnant3e519522010-05-11 19:42:16269{
Howard Hinnanteeac9fc2012-07-07 17:04:52270#ifdef _LIBCPP_HAS_NO_CONSTEXPR
Howard Hinnant3e519522010-05-11 19:42:16271 __init(__v, integral_constant<bool, sizeof(unsigned long long) == sizeof(__storage_type)>());
Howard Hinnanteeac9fc2012-07-07 17:04:52272#endif
Howard Hinnant3e519522010-05-11 19:42:16273}
274
275template <size_t _N_words, size_t _Size>
Evgeniy Stepanov906c8722015-11-07 01:22:13276inline
Howard Hinnant3e519522010-05-11 19:42:16277void
Howard Hinnantd368a842011-05-27 20:52:28278__bitset<_N_words, _Size>::operator&=(const __bitset& __v) _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16279{
280 for (size_type __i = 0; __i < _N_words; ++__i)
281 __first_[__i] &= __v.__first_[__i];
282}
283
284template <size_t _N_words, size_t _Size>
Evgeniy Stepanov906c8722015-11-07 01:22:13285inline
Howard Hinnant3e519522010-05-11 19:42:16286void
Howard Hinnantd368a842011-05-27 20:52:28287__bitset<_N_words, _Size>::operator|=(const __bitset& __v) _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16288{
289 for (size_type __i = 0; __i < _N_words; ++__i)
290 __first_[__i] |= __v.__first_[__i];
291}
292
293template <size_t _N_words, size_t _Size>
Evgeniy Stepanov906c8722015-11-07 01:22:13294inline
Howard Hinnant3e519522010-05-11 19:42:16295void
Howard Hinnantd368a842011-05-27 20:52:28296__bitset<_N_words, _Size>::operator^=(const __bitset& __v) _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16297{
298 for (size_type __i = 0; __i < _N_words; ++__i)
299 __first_[__i] ^= __v.__first_[__i];
300}
301
302template <size_t _N_words, size_t _Size>
303void
Howard Hinnantd368a842011-05-27 20:52:28304__bitset<_N_words, _Size>::flip() _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16305{
306 // do middle whole words
307 size_type __n = _Size;
308 __storage_pointer __p = __first_;
309 for (; __n >= __bits_per_word; ++__p, __n -= __bits_per_word)
310 *__p = ~*__p;
311 // do last partial word
312 if (__n > 0)
313 {
314 __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
315 __storage_type __b = *__p & __m;
316 *__p &= ~__m;
317 *__p |= ~__b & __m;
318 }
319}
320
321template <size_t _N_words, size_t _Size>
322unsigned long
323__bitset<_N_words, _Size>::to_ulong(false_type) const
324{
325 const_iterator __e = __make_iter(_Size);
Howard Hinnantce48a112011-06-30 21:18:19326 const_iterator __i = _VSTD::find(__make_iter(sizeof(unsigned long) * CHAR_BIT), __e, true);
Howard Hinnant3e519522010-05-11 19:42:16327 if (__i != __e)
328#ifndef _LIBCPP_NO_EXCEPTIONS
329 throw overflow_error("bitset to_ulong overflow error");
330#else
331 assert(!"bitset to_ulong overflow error");
332#endif
333 return __first_[0];
334}
335
336template <size_t _N_words, size_t _Size>
Evgeniy Stepanov906c8722015-11-07 01:22:13337inline
Howard Hinnant3e519522010-05-11 19:42:16338unsigned long
339__bitset<_N_words, _Size>::to_ulong(true_type) const
340{
341 return __first_[0];
342}
343
344template <size_t _N_words, size_t _Size>
345unsigned long long
346__bitset<_N_words, _Size>::to_ullong(false_type) const
347{
348 const_iterator __e = __make_iter(_Size);
Howard Hinnantce48a112011-06-30 21:18:19349 const_iterator __i = _VSTD::find(__make_iter(sizeof(unsigned long long) * CHAR_BIT), __e, true);
Howard Hinnant3e519522010-05-11 19:42:16350 if (__i != __e)
351#ifndef _LIBCPP_NO_EXCEPTIONS
352 throw overflow_error("bitset to_ullong overflow error");
353#else
354 assert(!"bitset to_ullong overflow error");
355#endif
356 return to_ullong(true_type());
357}
358
359template <size_t _N_words, size_t _Size>
Evgeniy Stepanov906c8722015-11-07 01:22:13360inline
Howard Hinnant3e519522010-05-11 19:42:16361unsigned long long
362__bitset<_N_words, _Size>::to_ullong(true_type) const
363{
364 return to_ullong(true_type(), integral_constant<bool, sizeof(__storage_type) < sizeof(unsigned long long)>());
365}
366
367template <size_t _N_words, size_t _Size>
Evgeniy Stepanov906c8722015-11-07 01:22:13368inline
Howard Hinnant3e519522010-05-11 19:42:16369unsigned long long
370__bitset<_N_words, _Size>::to_ullong(true_type, false_type) const
371{
372 return __first_[0];
373}
374
375template <size_t _N_words, size_t _Size>
376unsigned long long
377__bitset<_N_words, _Size>::to_ullong(true_type, true_type) const
378{
379 unsigned long long __r = __first_[0];
380 for (std::size_t __i = 1; __i < sizeof(unsigned long long) / sizeof(__storage_type); ++__i)
381 __r |= static_cast<unsigned long long>(__first_[__i]) << (sizeof(__storage_type) * CHAR_BIT);
382 return __r;
383}
384
385template <size_t _N_words, size_t _Size>
386bool
Howard Hinnantd368a842011-05-27 20:52:28387__bitset<_N_words, _Size>::all() const _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16388{
389 // do middle whole words
390 size_type __n = _Size;
391 __const_storage_pointer __p = __first_;
392 for (; __n >= __bits_per_word; ++__p, __n -= __bits_per_word)
393 if (~*__p)
394 return false;
395 // do last partial word
396 if (__n > 0)
397 {
398 __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
399 if (~*__p & __m)
400 return false;
401 }
402 return true;
403}
404
405template <size_t _N_words, size_t _Size>
406bool
Howard Hinnantd368a842011-05-27 20:52:28407__bitset<_N_words, _Size>::any() const _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16408{
409 // do middle whole words
410 size_type __n = _Size;
411 __const_storage_pointer __p = __first_;
412 for (; __n >= __bits_per_word; ++__p, __n -= __bits_per_word)
413 if (*__p)
414 return true;
415 // do last partial word
416 if (__n > 0)
417 {
418 __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
419 if (*__p & __m)
420 return true;
421 }
422 return false;
423}
424
425template <size_t _N_words, size_t _Size>
Evgeniy Stepanov906c8722015-11-07 01:22:13426inline
Howard Hinnant3e519522010-05-11 19:42:16427size_t
Howard Hinnantd368a842011-05-27 20:52:28428__bitset<_N_words, _Size>::__hash_code() const _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16429{
430 size_t __h = 0;
431 for (size_type __i = 0; __i < _N_words; ++__i)
432 __h ^= __first_[__i];
433 return __h;
434}
435
436template <size_t _Size>
437class __bitset<1, _Size>
438{
439public:
440 typedef ptrdiff_t difference_type;
441 typedef size_t size_type;
Howard Hinnant0ae9efe2012-05-07 16:50:38442 typedef size_type __storage_type;
Howard Hinnant3e519522010-05-11 19:42:16443protected:
444 typedef __bitset __self;
Howard Hinnant3e519522010-05-11 19:42:16445 typedef __storage_type* __storage_pointer;
446 typedef const __storage_type* __const_storage_pointer;
447 static const unsigned __bits_per_word = static_cast<unsigned>(sizeof(__storage_type) * CHAR_BIT);
448
449 friend class __bit_reference<__bitset>;
450 friend class __bit_const_reference<__bitset>;
451 friend class __bit_iterator<__bitset, false>;
452 friend class __bit_iterator<__bitset, true>;
Howard Hinnant7ee27132012-08-17 17:10:18453 friend struct __bit_array<__bitset>;
Howard Hinnant3e519522010-05-11 19:42:16454
455 __storage_type __first_;
456
457 typedef __bit_reference<__bitset> reference;
458 typedef __bit_const_reference<__bitset> const_reference;
459 typedef __bit_iterator<__bitset, false> iterator;
460 typedef __bit_iterator<__bitset, true> const_iterator;
461
Evgeniy Stepanov906c8722015-11-07 01:22:13462 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanteeac9fc2012-07-07 17:04:52463 _LIBCPP_CONSTEXPR __bitset() _NOEXCEPT;
Evgeniy Stepanov906c8722015-11-07 01:22:13464 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanteeac9fc2012-07-07 17:04:52465 explicit _LIBCPP_CONSTEXPR __bitset(unsigned long long __v) _NOEXCEPT;
Howard Hinnant3e519522010-05-11 19:42:16466
Howard Hinnantd368a842011-05-27 20:52:28467 _LIBCPP_INLINE_VISIBILITY reference __make_ref(size_t __pos) _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16468 {return reference(&__first_, __storage_type(1) << __pos);}
Howard Hinnanteeac9fc2012-07-07 17:04:52469 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR const_reference __make_ref(size_t __pos) const _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16470 {return const_reference(&__first_, __storage_type(1) << __pos);}
Howard Hinnantd368a842011-05-27 20:52:28471 _LIBCPP_INLINE_VISIBILITY iterator __make_iter(size_t __pos) _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16472 {return iterator(&__first_ + __pos / __bits_per_word, __pos % __bits_per_word);}
Howard Hinnantd368a842011-05-27 20:52:28473 _LIBCPP_INLINE_VISIBILITY const_iterator __make_iter(size_t __pos) const _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16474 {return const_iterator(&__first_ + __pos / __bits_per_word, __pos % __bits_per_word);}
475
Evgeniy Stepanov906c8722015-11-07 01:22:13476 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd368a842011-05-27 20:52:28477 void operator&=(const __bitset& __v) _NOEXCEPT;
Evgeniy Stepanov906c8722015-11-07 01:22:13478 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd368a842011-05-27 20:52:28479 void operator|=(const __bitset& __v) _NOEXCEPT;
Evgeniy Stepanov906c8722015-11-07 01:22:13480 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd368a842011-05-27 20:52:28481 void operator^=(const __bitset& __v) _NOEXCEPT;
Howard Hinnant3e519522010-05-11 19:42:16482
Evgeniy Stepanov906c8722015-11-07 01:22:13483 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd368a842011-05-27 20:52:28484 void flip() _NOEXCEPT;
Howard Hinnant3e519522010-05-11 19:42:16485
Evgeniy Stepanov906c8722015-11-07 01:22:13486 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16487 unsigned long to_ulong() const;
Evgeniy Stepanov906c8722015-11-07 01:22:13488 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16489 unsigned long long to_ullong() const;
490
Evgeniy Stepanov906c8722015-11-07 01:22:13491 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd368a842011-05-27 20:52:28492 bool all() const _NOEXCEPT;
Evgeniy Stepanov906c8722015-11-07 01:22:13493 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd368a842011-05-27 20:52:28494 bool any() const _NOEXCEPT;
Howard Hinnant3e519522010-05-11 19:42:16495
Evgeniy Stepanov906c8722015-11-07 01:22:13496 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd368a842011-05-27 20:52:28497 size_t __hash_code() const _NOEXCEPT;
Howard Hinnant3e519522010-05-11 19:42:16498};
499
500template <size_t _Size>
Evgeniy Stepanov906c8722015-11-07 01:22:13501inline
Howard Hinnanteeac9fc2012-07-07 17:04:52502_LIBCPP_CONSTEXPR
Howard Hinnantd368a842011-05-27 20:52:28503__bitset<1, _Size>::__bitset() _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16504 : __first_(0)
505{
506}
507
508template <size_t _Size>
Evgeniy Stepanov906c8722015-11-07 01:22:13509inline
Howard Hinnanteeac9fc2012-07-07 17:04:52510_LIBCPP_CONSTEXPR
Howard Hinnantd368a842011-05-27 20:52:28511__bitset<1, _Size>::__bitset(unsigned long long __v) _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16512 : __first_(static_cast<__storage_type>(__v))
513{
514}
515
516template <size_t _Size>
Evgeniy Stepanov906c8722015-11-07 01:22:13517inline
Howard Hinnant3e519522010-05-11 19:42:16518void
Howard Hinnantd368a842011-05-27 20:52:28519__bitset<1, _Size>::operator&=(const __bitset& __v) _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16520{
521 __first_ &= __v.__first_;
522}
523
524template <size_t _Size>
Evgeniy Stepanov906c8722015-11-07 01:22:13525inline
Howard Hinnant3e519522010-05-11 19:42:16526void
Howard Hinnantd368a842011-05-27 20:52:28527__bitset<1, _Size>::operator|=(const __bitset& __v) _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16528{
529 __first_ |= __v.__first_;
530}
531
532template <size_t _Size>
Evgeniy Stepanov906c8722015-11-07 01:22:13533inline
Howard Hinnant3e519522010-05-11 19:42:16534void
Howard Hinnantd368a842011-05-27 20:52:28535__bitset<1, _Size>::operator^=(const __bitset& __v) _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16536{
537 __first_ ^= __v.__first_;
538}
539
540template <size_t _Size>
Evgeniy Stepanov906c8722015-11-07 01:22:13541inline
Howard Hinnant3e519522010-05-11 19:42:16542void
Howard Hinnantd368a842011-05-27 20:52:28543__bitset<1, _Size>::flip() _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16544{
545 __storage_type __m = ~__storage_type(0) >> (__bits_per_word - _Size);
546 __first_ = ~__first_;
547 __first_ &= __m;
548}
549
550template <size_t _Size>
Evgeniy Stepanov906c8722015-11-07 01:22:13551inline
Howard Hinnant3e519522010-05-11 19:42:16552unsigned long
553__bitset<1, _Size>::to_ulong() const
554{
555 return __first_;
556}
557
558template <size_t _Size>
Evgeniy Stepanov906c8722015-11-07 01:22:13559inline
Howard Hinnant3e519522010-05-11 19:42:16560unsigned long long
561__bitset<1, _Size>::to_ullong() const
562{
563 return __first_;
564}
565
566template <size_t _Size>
Evgeniy Stepanov906c8722015-11-07 01:22:13567inline
Howard Hinnant3e519522010-05-11 19:42:16568bool
Howard Hinnantd368a842011-05-27 20:52:28569__bitset<1, _Size>::all() const _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16570{
571 __storage_type __m = ~__storage_type(0) >> (__bits_per_word - _Size);
572 return !(~__first_ & __m);
573}
574
575template <size_t _Size>
Evgeniy Stepanov906c8722015-11-07 01:22:13576inline
Howard Hinnant3e519522010-05-11 19:42:16577bool
Howard Hinnantd368a842011-05-27 20:52:28578__bitset<1, _Size>::any() const _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16579{
580 __storage_type __m = ~__storage_type(0) >> (__bits_per_word - _Size);
581 return __first_ & __m;
582}
583
584template <size_t _Size>
Evgeniy Stepanov906c8722015-11-07 01:22:13585inline
Howard Hinnant3e519522010-05-11 19:42:16586size_t
Howard Hinnantd368a842011-05-27 20:52:28587__bitset<1, _Size>::__hash_code() const _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16588{
589 return __first_;
590}
591
592template <>
593class __bitset<0, 0>
594{
595public:
596 typedef ptrdiff_t difference_type;
597 typedef size_t size_type;
Howard Hinnant0ae9efe2012-05-07 16:50:38598 typedef size_type __storage_type;
Howard Hinnant3e519522010-05-11 19:42:16599protected:
600 typedef __bitset __self;
Howard Hinnant3e519522010-05-11 19:42:16601 typedef __storage_type* __storage_pointer;
602 typedef const __storage_type* __const_storage_pointer;
603 static const unsigned __bits_per_word = static_cast<unsigned>(sizeof(__storage_type) * CHAR_BIT);
604
605 friend class __bit_reference<__bitset>;
606 friend class __bit_const_reference<__bitset>;
607 friend class __bit_iterator<__bitset, false>;
608 friend class __bit_iterator<__bitset, true>;
Howard Hinnantc2063662011-12-01 20:21:04609 friend struct __bit_array<__bitset>;
Howard Hinnant3e519522010-05-11 19:42:16610
611 typedef __bit_reference<__bitset> reference;
612 typedef __bit_const_reference<__bitset> const_reference;
613 typedef __bit_iterator<__bitset, false> iterator;
614 typedef __bit_iterator<__bitset, true> const_iterator;
615
Evgeniy Stepanov906c8722015-11-07 01:22:13616 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanteeac9fc2012-07-07 17:04:52617 _LIBCPP_CONSTEXPR __bitset() _NOEXCEPT;
Evgeniy Stepanov906c8722015-11-07 01:22:13618 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanteeac9fc2012-07-07 17:04:52619 explicit _LIBCPP_CONSTEXPR __bitset(unsigned long long) _NOEXCEPT;
Howard Hinnant3e519522010-05-11 19:42:16620
Howard Hinnantd368a842011-05-27 20:52:28621 _LIBCPP_INLINE_VISIBILITY reference __make_ref(size_t) _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16622 {return reference(0, 1);}
Howard Hinnanteeac9fc2012-07-07 17:04:52623 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR const_reference __make_ref(size_t) const _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16624 {return const_reference(0, 1);}
Howard Hinnantc2063662011-12-01 20:21:04625 _LIBCPP_INLINE_VISIBILITY iterator __make_iter(size_t) _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16626 {return iterator(0, 0);}
Howard Hinnantc2063662011-12-01 20:21:04627 _LIBCPP_INLINE_VISIBILITY const_iterator __make_iter(size_t) const _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16628 {return const_iterator(0, 0);}
629
Howard Hinnantd368a842011-05-27 20:52:28630 _LIBCPP_INLINE_VISIBILITY void operator&=(const __bitset&) _NOEXCEPT {}
631 _LIBCPP_INLINE_VISIBILITY void operator|=(const __bitset&) _NOEXCEPT {}
632 _LIBCPP_INLINE_VISIBILITY void operator^=(const __bitset&) _NOEXCEPT {}
Howard Hinnant3e519522010-05-11 19:42:16633
Howard Hinnantd368a842011-05-27 20:52:28634 _LIBCPP_INLINE_VISIBILITY void flip() _NOEXCEPT {}
Howard Hinnant3e519522010-05-11 19:42:16635
636 _LIBCPP_INLINE_VISIBILITY unsigned long to_ulong() const {return 0;}
637 _LIBCPP_INLINE_VISIBILITY unsigned long long to_ullong() const {return 0;}
638
Howard Hinnantd368a842011-05-27 20:52:28639 _LIBCPP_INLINE_VISIBILITY bool all() const _NOEXCEPT {return true;}
640 _LIBCPP_INLINE_VISIBILITY bool any() const _NOEXCEPT {return false;}
Howard Hinnant3e519522010-05-11 19:42:16641
Howard Hinnantd368a842011-05-27 20:52:28642 _LIBCPP_INLINE_VISIBILITY size_t __hash_code() const _NOEXCEPT {return 0;}
Howard Hinnant3e519522010-05-11 19:42:16643};
644
Evgeniy Stepanov906c8722015-11-07 01:22:13645inline
Howard Hinnanteeac9fc2012-07-07 17:04:52646_LIBCPP_CONSTEXPR
Howard Hinnantd368a842011-05-27 20:52:28647__bitset<0, 0>::__bitset() _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16648{
649}
650
Evgeniy Stepanov906c8722015-11-07 01:22:13651inline
Howard Hinnanteeac9fc2012-07-07 17:04:52652_LIBCPP_CONSTEXPR
Howard Hinnantd368a842011-05-27 20:52:28653__bitset<0, 0>::__bitset(unsigned long long) _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16654{
655}
656
Howard Hinnantf0544c22013-08-12 18:38:34657template <size_t _Size> class _LIBCPP_TYPE_VIS_ONLY bitset;
658template <size_t _Size> struct _LIBCPP_TYPE_VIS_ONLY hash<bitset<_Size> >;
Howard Hinnant3e519522010-05-11 19:42:16659
660template <size_t _Size>
Howard Hinnantf0544c22013-08-12 18:38:34661class _LIBCPP_TYPE_VIS_ONLY bitset
Howard Hinnant3e519522010-05-11 19:42:16662 : private __bitset<_Size == 0 ? 0 : (_Size - 1) / (sizeof(size_t) * CHAR_BIT) + 1, _Size>
663{
Howard Hinnant53b9ee02013-03-06 17:30:26664public:
Howard Hinnant3e519522010-05-11 19:42:16665 static const unsigned __n_words = _Size == 0 ? 0 : (_Size - 1) / (sizeof(size_t) * CHAR_BIT) + 1;
666 typedef __bitset<__n_words, _Size> base;
667
Howard Hinnantb3371f62010-08-22 00:02:43668public:
Howard Hinnant3e519522010-05-11 19:42:16669 typedef typename base::reference reference;
670 typedef typename base::const_reference const_reference;
671
Howard Hinnantb3371f62010-08-22 00:02:43672 // 23.3.5.1 constructors:
Howard Hinnanteeac9fc2012-07-07 17:04:52673 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR bitset() _NOEXCEPT {}
674 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
675 bitset(unsigned long long __v) _NOEXCEPT : base(__v) {}
Howard Hinnantd09f7112010-11-17 21:53:14676 template<class _CharT>
677 explicit bitset(const _CharT* __str,
678 typename basic_string<_CharT>::size_type __n = basic_string<_CharT>::npos,
679 _CharT __zero = _CharT('0'), _CharT __one = _CharT('1'));
Howard Hinnantb3371f62010-08-22 00:02:43680 template<class _CharT, class _Traits, class _Allocator>
681 explicit bitset(const basic_string<_CharT,_Traits,_Allocator>& __str,
682 typename basic_string<_CharT,_Traits,_Allocator>::size_type __pos = 0,
683 typename basic_string<_CharT,_Traits,_Allocator>::size_type __n =
Howard Hinnant3e519522010-05-11 19:42:16684 (basic_string<_CharT,_Traits,_Allocator>::npos),
Howard Hinnantb3371f62010-08-22 00:02:43685 _CharT __zero = _CharT('0'), _CharT __one = _CharT('1'));
Howard Hinnant3e519522010-05-11 19:42:16686
Howard Hinnantb3371f62010-08-22 00:02:43687 // 23.3.5.2 bitset operations:
Evgeniy Stepanov906c8722015-11-07 01:22:13688 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd368a842011-05-27 20:52:28689 bitset& operator&=(const bitset& __rhs) _NOEXCEPT;
Evgeniy Stepanov906c8722015-11-07 01:22:13690 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd368a842011-05-27 20:52:28691 bitset& operator|=(const bitset& __rhs) _NOEXCEPT;
Evgeniy Stepanov906c8722015-11-07 01:22:13692 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd368a842011-05-27 20:52:28693 bitset& operator^=(const bitset& __rhs) _NOEXCEPT;
694 bitset& operator<<=(size_t __pos) _NOEXCEPT;
695 bitset& operator>>=(size_t __pos) _NOEXCEPT;
Evgeniy Stepanov906c8722015-11-07 01:22:13696 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd368a842011-05-27 20:52:28697 bitset& set() _NOEXCEPT;
Howard Hinnantb3371f62010-08-22 00:02:43698 bitset& set(size_t __pos, bool __val = true);
Evgeniy Stepanov906c8722015-11-07 01:22:13699 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd368a842011-05-27 20:52:28700 bitset& reset() _NOEXCEPT;
Howard Hinnantb3371f62010-08-22 00:02:43701 bitset& reset(size_t __pos);
Evgeniy Stepanov906c8722015-11-07 01:22:13702 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd368a842011-05-27 20:52:28703 bitset operator~() const _NOEXCEPT;
Evgeniy Stepanov906c8722015-11-07 01:22:13704 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd368a842011-05-27 20:52:28705 bitset& flip() _NOEXCEPT;
Howard Hinnantb3371f62010-08-22 00:02:43706 bitset& flip(size_t __pos);
Howard Hinnant3e519522010-05-11 19:42:16707
Howard Hinnantb3371f62010-08-22 00:02:43708 // element access:
Howard Hinnanteeac9fc2012-07-07 17:04:52709 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
710 const_reference operator[](size_t __p) const {return base::__make_ref(__p);}
Howard Hinnant3e519522010-05-11 19:42:16711 _LIBCPP_INLINE_VISIBILITY reference operator[](size_t __p) {return base::__make_ref(__p);}
Evgeniy Stepanov906c8722015-11-07 01:22:13712 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb3371f62010-08-22 00:02:43713 unsigned long to_ulong() const;
Evgeniy Stepanov906c8722015-11-07 01:22:13714 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb3371f62010-08-22 00:02:43715 unsigned long long to_ullong() const;
716 template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant3e519522010-05-11 19:42:16717 basic_string<_CharT, _Traits, _Allocator> to_string(_CharT __zero = _CharT('0'),
Howard Hinnantb3371f62010-08-22 00:02:43718 _CharT __one = _CharT('1')) const;
719 template <class _CharT, class _Traits>
Evgeniy Stepanov906c8722015-11-07 01:22:13720 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16721 basic_string<_CharT, _Traits, allocator<_CharT> > to_string(_CharT __zero = _CharT('0'),
Howard Hinnantb3371f62010-08-22 00:02:43722 _CharT __one = _CharT('1')) const;
723 template <class _CharT>
Evgeniy Stepanov906c8722015-11-07 01:22:13724 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16725 basic_string<_CharT, char_traits<_CharT>, allocator<_CharT> > to_string(_CharT __zero = _CharT('0'),
Howard Hinnantb3371f62010-08-22 00:02:43726 _CharT __one = _CharT('1')) const;
Evgeniy Stepanov906c8722015-11-07 01:22:13727 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16728 basic_string<char, char_traits<char>, allocator<char> > to_string(char __zero = '0',
Howard Hinnantb3371f62010-08-22 00:02:43729 char __one = '1') const;
Evgeniy Stepanov906c8722015-11-07 01:22:13730 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd368a842011-05-27 20:52:28731 size_t count() const _NOEXCEPT;
Howard Hinnanteeac9fc2012-07-07 17:04:52732 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR size_t size() const _NOEXCEPT {return _Size;}
Evgeniy Stepanov906c8722015-11-07 01:22:13733 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd368a842011-05-27 20:52:28734 bool operator==(const bitset& __rhs) const _NOEXCEPT;
Evgeniy Stepanov906c8722015-11-07 01:22:13735 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd368a842011-05-27 20:52:28736 bool operator!=(const bitset& __rhs) const _NOEXCEPT;
Howard Hinnantb3371f62010-08-22 00:02:43737 bool test(size_t __pos) const;
Evgeniy Stepanov906c8722015-11-07 01:22:13738 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd368a842011-05-27 20:52:28739 bool all() const _NOEXCEPT;
Evgeniy Stepanov906c8722015-11-07 01:22:13740 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd368a842011-05-27 20:52:28741 bool any() const _NOEXCEPT;
742 _LIBCPP_INLINE_VISIBILITY bool none() const _NOEXCEPT {return !any();}
Evgeniy Stepanov906c8722015-11-07 01:22:13743 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd368a842011-05-27 20:52:28744 bitset operator<<(size_t __pos) const _NOEXCEPT;
Evgeniy Stepanov906c8722015-11-07 01:22:13745 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd368a842011-05-27 20:52:28746 bitset operator>>(size_t __pos) const _NOEXCEPT;
Howard Hinnant3e519522010-05-11 19:42:16747
748private:
749
Howard Hinnantfb100022010-09-21 21:28:23750 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd368a842011-05-27 20:52:28751 size_t __hash_code() const _NOEXCEPT {return base::__hash_code();}
Howard Hinnant3e519522010-05-11 19:42:16752
753 friend struct hash<bitset>;
754};
755
756template <size_t _Size>
Howard Hinnantd09f7112010-11-17 21:53:14757template<class _CharT>
758bitset<_Size>::bitset(const _CharT* __str,
759 typename basic_string<_CharT>::size_type __n,
760 _CharT __zero, _CharT __one)
Howard Hinnant3e519522010-05-11 19:42:16761{
Howard Hinnantce48a112011-06-30 21:18:19762 size_t __rlen = _VSTD::min(__n, char_traits<_CharT>::length(__str));
Howard Hinnant3e519522010-05-11 19:42:16763 for (size_t __i = 0; __i < __rlen; ++__i)
Howard Hinnantd09f7112010-11-17 21:53:14764 if (__str[__i] != __zero && __str[__i] != __one)
Howard Hinnant3e519522010-05-11 19:42:16765#ifndef _LIBCPP_NO_EXCEPTIONS
766 throw invalid_argument("bitset string ctor has invalid argument");
767#else
768 assert(!"bitset string ctor has invalid argument");
769#endif
Howard Hinnantc003db12011-11-29 18:15:50770 size_t _Mp = _VSTD::min(__rlen, _Size);
Howard Hinnant3e519522010-05-11 19:42:16771 size_t __i = 0;
Howard Hinnantc003db12011-11-29 18:15:50772 for (; __i < _Mp; ++__i)
Howard Hinnant3e519522010-05-11 19:42:16773 {
Howard Hinnantc003db12011-11-29 18:15:50774 _CharT __c = __str[_Mp - 1 - __i];
Howard Hinnantd09f7112010-11-17 21:53:14775 if (__c == __zero)
Howard Hinnant3e519522010-05-11 19:42:16776 (*this)[__i] = false;
Howard Hinnantd09f7112010-11-17 21:53:14777 else
Howard Hinnant3e519522010-05-11 19:42:16778 (*this)[__i] = true;
Howard Hinnant3e519522010-05-11 19:42:16779 }
Howard Hinnantce48a112011-06-30 21:18:19780 _VSTD::fill(base::__make_iter(__i), base::__make_iter(_Size), false);
Howard Hinnant3e519522010-05-11 19:42:16781}
782
783template <size_t _Size>
Howard Hinnantb3371f62010-08-22 00:02:43784template<class _CharT, class _Traits, class _Allocator>
785bitset<_Size>::bitset(const basic_string<_CharT,_Traits,_Allocator>& __str,
786 typename basic_string<_CharT,_Traits,_Allocator>::size_type __pos,
Howard Hinnant3e519522010-05-11 19:42:16787 typename basic_string<_CharT,_Traits,_Allocator>::size_type __n,
788 _CharT __zero, _CharT __one)
789{
790 if (__pos > __str.size())
791#ifndef _LIBCPP_NO_EXCEPTIONS
792 throw out_of_range("bitset string pos out of range");
793#else
794 assert(!"bitset string pos out of range");
795#endif
Howard Hinnantce48a112011-06-30 21:18:19796 size_t __rlen = _VSTD::min(__n, __str.size() - __pos);
Howard Hinnant3e519522010-05-11 19:42:16797 for (size_t __i = __pos; __i < __pos + __rlen; ++__i)
798 if (!_Traits::eq(__str[__i], __zero) && !_Traits::eq(__str[__i], __one))
799#ifndef _LIBCPP_NO_EXCEPTIONS
800 throw invalid_argument("bitset string ctor has invalid argument");
801#else
802 assert(!"bitset string ctor has invalid argument");
803#endif
Howard Hinnantc003db12011-11-29 18:15:50804 size_t _Mp = _VSTD::min(__rlen, _Size);
Howard Hinnant3e519522010-05-11 19:42:16805 size_t __i = 0;
Howard Hinnantc003db12011-11-29 18:15:50806 for (; __i < _Mp; ++__i)
Howard Hinnant3e519522010-05-11 19:42:16807 {
Howard Hinnantc003db12011-11-29 18:15:50808 _CharT __c = __str[__pos + _Mp - 1 - __i];
Howard Hinnant3e519522010-05-11 19:42:16809 if (_Traits::eq(__c, __zero))
810 (*this)[__i] = false;
811 else
812 (*this)[__i] = true;
813 }
Howard Hinnantce48a112011-06-30 21:18:19814 _VSTD::fill(base::__make_iter(__i), base::__make_iter(_Size), false);
Howard Hinnant3e519522010-05-11 19:42:16815}
816
817template <size_t _Size>
Evgeniy Stepanov906c8722015-11-07 01:22:13818inline
Howard Hinnant3e519522010-05-11 19:42:16819bitset<_Size>&
Howard Hinnantd368a842011-05-27 20:52:28820bitset<_Size>::operator&=(const bitset& __rhs) _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16821{
822 base::operator&=(__rhs);
823 return *this;
824}
825
826template <size_t _Size>
Evgeniy Stepanov906c8722015-11-07 01:22:13827inline
Howard Hinnant3e519522010-05-11 19:42:16828bitset<_Size>&
Howard Hinnantd368a842011-05-27 20:52:28829bitset<_Size>::operator|=(const bitset& __rhs) _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16830{
831 base::operator|=(__rhs);
832 return *this;
833}
834
835template <size_t _Size>
Evgeniy Stepanov906c8722015-11-07 01:22:13836inline
Howard Hinnant3e519522010-05-11 19:42:16837bitset<_Size>&
Howard Hinnantd368a842011-05-27 20:52:28838bitset<_Size>::operator^=(const bitset& __rhs) _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16839{
840 base::operator^=(__rhs);
841 return *this;
842}
843
844template <size_t _Size>
845bitset<_Size>&
Howard Hinnantd368a842011-05-27 20:52:28846bitset<_Size>::operator<<=(size_t __pos) _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16847{
Howard Hinnantce48a112011-06-30 21:18:19848 __pos = _VSTD::min(__pos, _Size);
849 _VSTD::copy_backward(base::__make_iter(0), base::__make_iter(_Size - __pos), base::__make_iter(_Size));
850 _VSTD::fill_n(base::__make_iter(0), __pos, false);
Howard Hinnant3e519522010-05-11 19:42:16851 return *this;
852}
853
854template <size_t _Size>
855bitset<_Size>&
Howard Hinnantd368a842011-05-27 20:52:28856bitset<_Size>::operator>>=(size_t __pos) _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16857{
Howard Hinnantce48a112011-06-30 21:18:19858 __pos = _VSTD::min(__pos, _Size);
859 _VSTD::copy(base::__make_iter(__pos), base::__make_iter(_Size), base::__make_iter(0));
860 _VSTD::fill_n(base::__make_iter(_Size - __pos), __pos, false);
Howard Hinnant3e519522010-05-11 19:42:16861 return *this;
862}
863
864template <size_t _Size>
Evgeniy Stepanov906c8722015-11-07 01:22:13865inline
Howard Hinnant3e519522010-05-11 19:42:16866bitset<_Size>&
Howard Hinnantd368a842011-05-27 20:52:28867bitset<_Size>::set() _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16868{
Howard Hinnantce48a112011-06-30 21:18:19869 _VSTD::fill_n(base::__make_iter(0), _Size, true);
Howard Hinnant3e519522010-05-11 19:42:16870 return *this;
871}
872
873template <size_t _Size>
874bitset<_Size>&
875bitset<_Size>::set(size_t __pos, bool __val)
876{
877 if (__pos >= _Size)
878#ifndef _LIBCPP_NO_EXCEPTIONS
879 throw out_of_range("bitset set argument out of range");
880#else
881 assert(!"bitset set argument out of range");
882#endif
883 (*this)[__pos] = __val;
884 return *this;
885}
886
887template <size_t _Size>
Evgeniy Stepanov906c8722015-11-07 01:22:13888inline
Howard Hinnant3e519522010-05-11 19:42:16889bitset<_Size>&
Howard Hinnantd368a842011-05-27 20:52:28890bitset<_Size>::reset() _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16891{
Howard Hinnantce48a112011-06-30 21:18:19892 _VSTD::fill_n(base::__make_iter(0), _Size, false);
Howard Hinnant3e519522010-05-11 19:42:16893 return *this;
894}
895
896template <size_t _Size>
897bitset<_Size>&
898bitset<_Size>::reset(size_t __pos)
899{
900 if (__pos >= _Size)
901#ifndef _LIBCPP_NO_EXCEPTIONS
902 throw out_of_range("bitset reset argument out of range");
903#else
904 assert(!"bitset reset argument out of range");
905#endif
906 (*this)[__pos] = false;
907 return *this;
908}
909
910template <size_t _Size>
Evgeniy Stepanov906c8722015-11-07 01:22:13911inline
Howard Hinnant3e519522010-05-11 19:42:16912bitset<_Size>
Howard Hinnantd368a842011-05-27 20:52:28913bitset<_Size>::operator~() const _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16914{
915 bitset __x(*this);
916 __x.flip();
917 return __x;
918}
919
920template <size_t _Size>
Evgeniy Stepanov906c8722015-11-07 01:22:13921inline
Howard Hinnant3e519522010-05-11 19:42:16922bitset<_Size>&
Howard Hinnantd368a842011-05-27 20:52:28923bitset<_Size>::flip() _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16924{
925 base::flip();
926 return *this;
927}
928
929template <size_t _Size>
930bitset<_Size>&
931bitset<_Size>::flip(size_t __pos)
932{
933 if (__pos >= _Size)
934#ifndef _LIBCPP_NO_EXCEPTIONS
935 throw out_of_range("bitset flip argument out of range");
936#else
937 assert(!"bitset flip argument out of range");
938#endif
939 reference r = base::__make_ref(__pos);
940 r = ~r;
941 return *this;
942}
943
944template <size_t _Size>
Evgeniy Stepanov906c8722015-11-07 01:22:13945inline
Howard Hinnant3e519522010-05-11 19:42:16946unsigned long
947bitset<_Size>::to_ulong() const
948{
949 return base::to_ulong();
950}
951
952template <size_t _Size>
Evgeniy Stepanov906c8722015-11-07 01:22:13953inline
Howard Hinnant3e519522010-05-11 19:42:16954unsigned long long
955bitset<_Size>::to_ullong() const
956{
957 return base::to_ullong();
958}
959
960template <size_t _Size>
Howard Hinnantb3371f62010-08-22 00:02:43961template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant3e519522010-05-11 19:42:16962basic_string<_CharT, _Traits, _Allocator>
963bitset<_Size>::to_string(_CharT __zero, _CharT __one) const
964{
965 basic_string<_CharT, _Traits, _Allocator> __r(_Size, __zero);
966 for (size_t __i = 0; __i < _Size; ++__i)
967 {
968 if ((*this)[__i])
969 __r[_Size - 1 - __i] = __one;
970 }
971 return __r;
972}
973
974template <size_t _Size>
Howard Hinnantb3371f62010-08-22 00:02:43975template <class _CharT, class _Traits>
Evgeniy Stepanov906c8722015-11-07 01:22:13976inline
Howard Hinnant3e519522010-05-11 19:42:16977basic_string<_CharT, _Traits, allocator<_CharT> >
978bitset<_Size>::to_string(_CharT __zero, _CharT __one) const
979{
980 return to_string<_CharT, _Traits, allocator<_CharT> >(__zero, __one);
981}
982
983template <size_t _Size>
Howard Hinnantb3371f62010-08-22 00:02:43984template <class _CharT>
Evgeniy Stepanov906c8722015-11-07 01:22:13985inline
Howard Hinnant3e519522010-05-11 19:42:16986basic_string<_CharT, char_traits<_CharT>, allocator<_CharT> >
987bitset<_Size>::to_string(_CharT __zero, _CharT __one) const
988{
989 return to_string<_CharT, char_traits<_CharT>, allocator<_CharT> >(__zero, __one);
990}
991
992template <size_t _Size>
Evgeniy Stepanov906c8722015-11-07 01:22:13993inline
Howard Hinnant3e519522010-05-11 19:42:16994basic_string<char, char_traits<char>, allocator<char> >
995bitset<_Size>::to_string(char __zero, char __one) const
996{
997 return to_string<char, char_traits<char>, allocator<char> >(__zero, __one);
998}
999
1000template <size_t _Size>
Evgeniy Stepanov906c8722015-11-07 01:22:131001inline
Howard Hinnant3e519522010-05-11 19:42:161002size_t
Howard Hinnantd368a842011-05-27 20:52:281003bitset<_Size>::count() const _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:161004{
Howard Hinnantce48a112011-06-30 21:18:191005 return static_cast<size_t>(_VSTD::count(base::__make_iter(0), base::__make_iter(_Size), true));
Howard Hinnant3e519522010-05-11 19:42:161006}
1007
1008template <size_t _Size>
Evgeniy Stepanov906c8722015-11-07 01:22:131009inline
Howard Hinnant3e519522010-05-11 19:42:161010bool
Howard Hinnantd368a842011-05-27 20:52:281011bitset<_Size>::operator==(const bitset& __rhs) const _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:161012{
Howard Hinnantce48a112011-06-30 21:18:191013 return _VSTD::equal(base::__make_iter(0), base::__make_iter(_Size), __rhs.__make_iter(0));
Howard Hinnant3e519522010-05-11 19:42:161014}
1015
1016template <size_t _Size>
Evgeniy Stepanov906c8722015-11-07 01:22:131017inline
Howard Hinnant3e519522010-05-11 19:42:161018bool
Howard Hinnantd368a842011-05-27 20:52:281019bitset<_Size>::operator!=(const bitset& __rhs) const _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:161020{
1021 return !(*this == __rhs);
1022}
1023
1024template <size_t _Size>
1025bool
1026bitset<_Size>::test(size_t __pos) const
1027{
1028 if (__pos >= _Size)
1029#ifndef _LIBCPP_NO_EXCEPTIONS
1030 throw out_of_range("bitset test argument out of range");
1031#else
1032 assert(!"bitset test argument out of range");
1033#endif
1034 return (*this)[__pos];
1035}
1036
1037template <size_t _Size>
Evgeniy Stepanov906c8722015-11-07 01:22:131038inline
Howard Hinnant3e519522010-05-11 19:42:161039bool
Howard Hinnantd368a842011-05-27 20:52:281040bitset<_Size>::all() const _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:161041{
1042 return base::all();
1043}
1044
1045template <size_t _Size>
Evgeniy Stepanov906c8722015-11-07 01:22:131046inline
Howard Hinnant3e519522010-05-11 19:42:161047bool
Howard Hinnantd368a842011-05-27 20:52:281048bitset<_Size>::any() const _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:161049{
1050 return base::any();
1051}
1052
1053template <size_t _Size>
Evgeniy Stepanov906c8722015-11-07 01:22:131054inline
Howard Hinnant3e519522010-05-11 19:42:161055bitset<_Size>
Howard Hinnantd368a842011-05-27 20:52:281056bitset<_Size>::operator<<(size_t __pos) const _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:161057{
1058 bitset __r = *this;
1059 __r <<= __pos;
1060 return __r;
1061}
1062
1063template <size_t _Size>
Evgeniy Stepanov906c8722015-11-07 01:22:131064inline
Howard Hinnant3e519522010-05-11 19:42:161065bitset<_Size>
Howard Hinnantd368a842011-05-27 20:52:281066bitset<_Size>::operator>>(size_t __pos) const _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:161067{
1068 bitset __r = *this;
1069 __r >>= __pos;
1070 return __r;
1071}
1072
1073template <size_t _Size>
1074inline _LIBCPP_INLINE_VISIBILITY
1075bitset<_Size>
Howard Hinnantd368a842011-05-27 20:52:281076operator&(const bitset<_Size>& __x, const bitset<_Size>& __y) _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:161077{
1078 bitset<_Size> __r = __x;
1079 __r &= __y;
1080 return __r;
1081}
1082
1083template <size_t _Size>
1084inline _LIBCPP_INLINE_VISIBILITY
1085bitset<_Size>
Howard Hinnantd368a842011-05-27 20:52:281086operator|(const bitset<_Size>& __x, const bitset<_Size>& __y) _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:161087{
1088 bitset<_Size> __r = __x;
1089 __r |= __y;
1090 return __r;
1091}
1092
1093template <size_t _Size>
1094inline _LIBCPP_INLINE_VISIBILITY
1095bitset<_Size>
Howard Hinnantd368a842011-05-27 20:52:281096operator^(const bitset<_Size>& __x, const bitset<_Size>& __y) _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:161097{
1098 bitset<_Size> __r = __x;
1099 __r ^= __y;
1100 return __r;
1101}
1102
1103template <size_t _Size>
Howard Hinnantf0544c22013-08-12 18:38:341104struct _LIBCPP_TYPE_VIS_ONLY hash<bitset<_Size> >
Howard Hinnant3e519522010-05-11 19:42:161105 : public unary_function<bitset<_Size>, size_t>
1106{
Howard Hinnantfb100022010-09-21 21:28:231107 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd368a842011-05-27 20:52:281108 size_t operator()(const bitset<_Size>& __bs) const _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:161109 {return __bs.__hash_code();}
1110};
1111
Howard Hinnante3163f52011-07-18 15:51:591112template <class _CharT, class _Traits, size_t _Size>
1113basic_istream<_CharT, _Traits>&
1114operator>>(basic_istream<_CharT, _Traits>& __is, bitset<_Size>& __x);
1115
1116template <class _CharT, class _Traits, size_t _Size>
1117basic_ostream<_CharT, _Traits>&
1118operator<<(basic_ostream<_CharT, _Traits>& __os, const bitset<_Size>& __x);
1119
Howard Hinnant3e519522010-05-11 19:42:161120_LIBCPP_END_NAMESPACE_STD
1121
1122#endif // _LIBCPP_BITSET