blob: 87d7afca5a738fe38a9aeee37489e9e244bfdae8 [file] [log] [blame]
Howard Hinnantbc8d3f92010-05-11 19:42:161// -*- C++ -*-
2//===---------------------------- bitset ----------------------------------===//
3//
Howard Hinnantf5256e12010-05-11 21:36:014// The LLVM Compiler Infrastructure
Howard Hinnantbc8d3f92010-05-11 19:42:165//
Howard Hinnantb64f8b02010-11-16 22:09:026// This file is dual licensed under the MIT and the University of Illinois Open
7// Source Licenses. See LICENSE.TXT for details.
Howard Hinnantbc8d3f92010-05-11 19:42:168//
9//===----------------------------------------------------------------------===//
10
11#ifndef _LIBCPP_BITSET
12#define _LIBCPP_BITSET
13
14/*
15 bitset synopsis
16
17namespace std
18{
19
20namespace std {
21
22template <size_t N>
23class bitset
24{
25public:
26 // bit reference:
27 class reference
28 {
29 friend class bitset;
Howard Hinnant10f25d22011-05-27 20:52:2830 reference() noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:1631 public:
Howard Hinnant10f25d22011-05-27 20:52:2832 ~reference() noexcept;
33 reference& operator=(bool x) noexcept; // for b[i] = x;
34 reference& operator=(const reference&) noexcept; // for b[i] = b[j];
35 bool operator~() const noexcept; // flips the bit
36 operator bool() const noexcept; // for x = b[i];
37 reference& flip() noexcept; // for b[i].flip();
Howard Hinnantbc8d3f92010-05-11 19:42:1638 };
39
40 // 23.3.5.1 constructors:
Howard Hinnant10f25d22011-05-27 20:52:2841 constexpr bitset() noexcept;
42 constexpr bitset(unsigned long long val) noexcept;
Howard Hinnant34d6b192010-11-17 21:53:1443 template <class charT>
44 explicit bitset(const charT* str,
45 typename basic_string<charT>::size_type n = basic_string<charT>::npos,
46 charT zero = charT('0'), charT one = charT('1'));
Howard Hinnantbc8d3f92010-05-11 19:42:1647 template<class charT, class traits, class Allocator>
48 explicit bitset(const basic_string<charT,traits,Allocator>& str,
49 typename basic_string<charT,traits,Allocator>::size_type pos = 0,
50 typename basic_string<charT,traits,Allocator>::size_type n =
51 basic_string<charT,traits,Allocator>::npos,
52 charT zero = charT('0'), charT one = charT('1'));
53
54 // 23.3.5.2 bitset operations:
Howard Hinnant10f25d22011-05-27 20:52:2855 bitset& operator&=(const bitset& rhs) noexcept;
56 bitset& operator|=(const bitset& rhs) noexcept;
57 bitset& operator^=(const bitset& rhs) noexcept;
58 bitset& operator<<=(size_t pos) noexcept;
59 bitset& operator>>=(size_t pos) noexcept;
60 bitset& set() noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:1661 bitset& set(size_t pos, bool val = true);
Howard Hinnant10f25d22011-05-27 20:52:2862 bitset& reset() noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:1663 bitset& reset(size_t pos);
Howard Hinnant10f25d22011-05-27 20:52:2864 bitset operator~() const noexcept;
65 bitset& flip() noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:1666 bitset& flip(size_t pos);
67
68 // element access:
69 constexpr bool operator[](size_t pos) const; // for b[i];
70 reference operator[](size_t pos); // for b[i];
71 unsigned long to_ulong() const;
72 unsigned long long to_ullong() const;
73 template <class charT, class traits, class Allocator>
74 basic_string<charT, traits, Allocator> to_string(charT zero = charT('0'), charT one = charT('1')) const;
75 template <class charT, class traits>
76 basic_string<charT, traits, allocator<charT> > to_string(charT zero = charT('0'), charT one = charT('1')) const;
77 template <class charT>
78 basic_string<charT, char_traits<charT>, allocator<charT> > to_string(charT zero = charT('0'), charT one = charT('1')) const;
79 basic_string<char, char_traits<char>, allocator<char> > to_string(char zero = '0', char one = '1') const;
Howard Hinnant10f25d22011-05-27 20:52:2880 size_t count() const noexcept;
81 constexpr size_t size() const noexcept;
82 bool operator==(const bitset& rhs) const noexcept;
83 bool operator!=(const bitset& rhs) const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:1684 bool test(size_t pos) const;
Howard Hinnant10f25d22011-05-27 20:52:2885 bool all() const noexcept;
86 bool any() const noexcept;
87 bool none() const noexcept;
88 bitset operator<<(size_t pos) const noexcept;
89 bitset operator>>(size_t pos) const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:1690};
91
92// 23.3.5.3 bitset operators:
93template <size_t N>
Howard Hinnant10f25d22011-05-27 20:52:2894bitset<N> operator&(const bitset<N>&, const bitset<N>&) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:1695
96template <size_t N>
Howard Hinnant10f25d22011-05-27 20:52:2897bitset<N> operator|(const bitset<N>&, const bitset<N>&) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:1698
99template <size_t N>
Howard Hinnant10f25d22011-05-27 20:52:28100bitset<N> operator^(const bitset<N>&, const bitset<N>&) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16101
102template <class charT, class traits, size_t N>
103basic_istream<charT, traits>&
104operator>>(basic_istream<charT, traits>& is, bitset<N>& x);
105
106template <class charT, class traits, size_t N>
107basic_ostream<charT, traits>&
108operator<<(basic_ostream<charT, traits>& os, const bitset<N>& x);
109
110template <size_t N> struct hash<std::bitset<N>>;
111
112} // std
113
114*/
115
Howard Hinnant08e17472011-10-17 20:05:10116#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantbc8d3f92010-05-11 19:42:16117#pragma GCC system_header
Howard Hinnant08e17472011-10-17 20:05:10118#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16119
120#include <__config>
121#include <__bit_reference>
122#include <cstddef>
123#include <climits>
124#include <string>
125#include <stdexcept>
126#include <iosfwd>
127#include <__functional_base>
128#if defined(_LIBCPP_NO_EXCEPTIONS)
129 #include <cassert>
130#endif
131
Howard Hinnant66c6f972011-11-29 16:45:27132#include <__undef_min_max>
133
Howard Hinnantbc8d3f92010-05-11 19:42:16134_LIBCPP_BEGIN_NAMESPACE_STD
135
136template <size_t _N_words, size_t _Size>
Howard Hinnantf03c3b42011-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 Hinnantbc8d3f92010-05-11 19:42:16146class __bitset
147{
148public:
149 typedef ptrdiff_t difference_type;
150 typedef size_t size_type;
Howard Hinnantf867f632012-05-07 16:50:38151 typedef size_type __storage_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16152protected:
153 typedef __bitset __self;
Howard Hinnantbc8d3f92010-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 Hinnant4ae952a2012-08-17 17:10:18162 friend struct __bit_array<__bitset>;
Howard Hinnantbc8d3f92010-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 Stepanova3b25f82015-11-07 01:22:13171 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant90d87232012-07-07 17:04:52172 _LIBCPP_CONSTEXPR __bitset() _NOEXCEPT;
Evgeniy Stepanova3b25f82015-11-07 01:22:13173 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant90d87232012-07-07 17:04:52174 explicit _LIBCPP_CONSTEXPR __bitset(unsigned long long __v) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16175
Howard Hinnant10f25d22011-05-27 20:52:28176 _LIBCPP_INLINE_VISIBILITY reference __make_ref(size_t __pos) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16177 {return reference(__first_ + __pos / __bits_per_word, __storage_type(1) << __pos % __bits_per_word);}
Howard Hinnant90d87232012-07-07 17:04:52178 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR const_reference __make_ref(size_t __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16179 {return const_reference(__first_ + __pos / __bits_per_word, __storage_type(1) << __pos % __bits_per_word);}
Howard Hinnant10f25d22011-05-27 20:52:28180 _LIBCPP_INLINE_VISIBILITY iterator __make_iter(size_t __pos) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16181 {return iterator(__first_ + __pos / __bits_per_word, __pos % __bits_per_word);}
Howard Hinnant10f25d22011-05-27 20:52:28182 _LIBCPP_INLINE_VISIBILITY const_iterator __make_iter(size_t __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16183 {return const_iterator(__first_ + __pos / __bits_per_word, __pos % __bits_per_word);}
184
Evgeniy Stepanova3b25f82015-11-07 01:22:13185 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant10f25d22011-05-27 20:52:28186 void operator&=(const __bitset& __v) _NOEXCEPT;
Evgeniy Stepanova3b25f82015-11-07 01:22:13187 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant10f25d22011-05-27 20:52:28188 void operator|=(const __bitset& __v) _NOEXCEPT;
Evgeniy Stepanova3b25f82015-11-07 01:22:13189 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant10f25d22011-05-27 20:52:28190 void operator^=(const __bitset& __v) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16191
Howard Hinnant10f25d22011-05-27 20:52:28192 void flip() _NOEXCEPT;
Howard Hinnantbc8d3f92010-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 Hinnant10f25d22011-05-27 20:52:28198 bool all() const _NOEXCEPT;
199 bool any() const _NOEXCEPT;
Evgeniy Stepanova3b25f82015-11-07 01:22:13200 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant10f25d22011-05-27 20:52:28201 size_t __hash_code() const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16202private:
Howard Hinnant90d87232012-07-07 17:04:52203#ifdef _LIBCPP_HAS_NO_CONSTEXPR
Howard Hinnant10f25d22011-05-27 20:52:28204 void __init(unsigned long long __v, false_type) _NOEXCEPT;
Evgeniy Stepanov28c02db2015-12-09 22:32:36205 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant10f25d22011-05-27 20:52:28206 void __init(unsigned long long __v, true_type) _NOEXCEPT;
Howard Hinnant90d87232012-07-07 17:04:52207#endif // _LIBCPP_HAS_NO_CONSTEXPR
Howard Hinnantbc8d3f92010-05-11 19:42:16208 unsigned long to_ulong(false_type) const;
Evgeniy Stepanova3b25f82015-11-07 01:22:13209 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16210 unsigned long to_ulong(true_type) const;
211 unsigned long long to_ullong(false_type) const;
Evgeniy Stepanova3b25f82015-11-07 01:22:13212 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16213 unsigned long long to_ullong(true_type) const;
Evgeniy Stepanova3b25f82015-11-07 01:22:13214 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16215 unsigned long long to_ullong(true_type, false_type) const;
216 unsigned long long to_ullong(true_type, true_type) const;
217};
218
219template <size_t _N_words, size_t _Size>
Evgeniy Stepanova3b25f82015-11-07 01:22:13220inline
Howard Hinnant90d87232012-07-07 17:04:52221_LIBCPP_CONSTEXPR
Howard Hinnant10f25d22011-05-27 20:52:28222__bitset<_N_words, _Size>::__bitset() _NOEXCEPT
Howard Hinnant90d87232012-07-07 17:04:52223#ifndef _LIBCPP_HAS_NO_CONSTEXPR
224 : __first_{0}
225#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16226{
Howard Hinnant90d87232012-07-07 17:04:52227#ifdef _LIBCPP_HAS_NO_CONSTEXPR
Howard Hinnant0949eed2011-06-30 21:18:19228 _VSTD::fill_n(__first_, _N_words, __storage_type(0));
Howard Hinnant90d87232012-07-07 17:04:52229#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16230}
231
Howard Hinnant90d87232012-07-07 17:04:52232#ifdef _LIBCPP_HAS_NO_CONSTEXPR
233
Howard Hinnantbc8d3f92010-05-11 19:42:16234template <size_t _N_words, size_t _Size>
235void
Howard Hinnantec3773c2011-12-01 20:21:04236__bitset<_N_words, _Size>::__init(unsigned long long __v, false_type) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16237{
238 __storage_type __t[sizeof(unsigned long long) / sizeof(__storage_type)];
239 for (size_t __i = 0; __i < sizeof(__t)/sizeof(__t[0]); ++__i, __v >>= __bits_per_word)
240 __t[__i] = static_cast<__storage_type>(__v);
Howard Hinnant0949eed2011-06-30 21:18:19241 _VSTD::copy(__t, __t + sizeof(__t)/sizeof(__t[0]), __first_);
242 _VSTD::fill(__first_ + sizeof(__t)/sizeof(__t[0]), __first_ + sizeof(__first_)/sizeof(__first_[0]),
Howard Hinnantbc8d3f92010-05-11 19:42:16243 __storage_type(0));
244}
245
246template <size_t _N_words, size_t _Size>
247inline _LIBCPP_INLINE_VISIBILITY
248void
Howard Hinnantec3773c2011-12-01 20:21:04249__bitset<_N_words, _Size>::__init(unsigned long long __v, true_type) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16250{
251 __first_[0] = __v;
Howard Hinnant0949eed2011-06-30 21:18:19252 _VSTD::fill(__first_ + 1, __first_ + sizeof(__first_)/sizeof(__first_[0]), __storage_type(0));
Howard Hinnantbc8d3f92010-05-11 19:42:16253}
254
Howard Hinnant90d87232012-07-07 17:04:52255#endif // _LIBCPP_HAS_NO_CONSTEXPR
256
Howard Hinnantbc8d3f92010-05-11 19:42:16257template <size_t _N_words, size_t _Size>
Evgeniy Stepanova3b25f82015-11-07 01:22:13258inline
Howard Hinnant90d87232012-07-07 17:04:52259_LIBCPP_CONSTEXPR
Howard Hinnant10f25d22011-05-27 20:52:28260__bitset<_N_words, _Size>::__bitset(unsigned long long __v) _NOEXCEPT
Howard Hinnant90d87232012-07-07 17:04:52261#ifndef _LIBCPP_HAS_NO_CONSTEXPR
Nico Weber0211e862014-06-04 15:46:56262#if __SIZEOF_SIZE_T__ == 8
Howard Hinnant90d87232012-07-07 17:04:52263 : __first_{__v}
Nico Weber0211e862014-06-04 15:46:56264#elif __SIZEOF_SIZE_T__ == 4
Howard Hinnant11a31d02013-03-06 17:30:26265 : __first_{__v, __v >> __bits_per_word}
Howard Hinnant31014742013-03-06 18:16:12266#else
Howard Hinnant11a31d02013-03-06 17:30:26267#error This constructor has not been ported to this platform
268#endif
Howard Hinnant90d87232012-07-07 17:04:52269#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16270{
Howard Hinnant90d87232012-07-07 17:04:52271#ifdef _LIBCPP_HAS_NO_CONSTEXPR
Howard Hinnantbc8d3f92010-05-11 19:42:16272 __init(__v, integral_constant<bool, sizeof(unsigned long long) == sizeof(__storage_type)>());
Howard Hinnant90d87232012-07-07 17:04:52273#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16274}
275
276template <size_t _N_words, size_t _Size>
Evgeniy Stepanova3b25f82015-11-07 01:22:13277inline
Howard Hinnantbc8d3f92010-05-11 19:42:16278void
Howard Hinnant10f25d22011-05-27 20:52:28279__bitset<_N_words, _Size>::operator&=(const __bitset& __v) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16280{
281 for (size_type __i = 0; __i < _N_words; ++__i)
282 __first_[__i] &= __v.__first_[__i];
283}
284
285template <size_t _N_words, size_t _Size>
Evgeniy Stepanova3b25f82015-11-07 01:22:13286inline
Howard Hinnantbc8d3f92010-05-11 19:42:16287void
Howard Hinnant10f25d22011-05-27 20:52:28288__bitset<_N_words, _Size>::operator|=(const __bitset& __v) _NOEXCEPT
Howard Hinnantbc8d3f92010-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 Stepanova3b25f82015-11-07 01:22:13295inline
Howard Hinnantbc8d3f92010-05-11 19:42:16296void
Howard Hinnant10f25d22011-05-27 20:52:28297__bitset<_N_words, _Size>::operator^=(const __bitset& __v) _NOEXCEPT
Howard Hinnantbc8d3f92010-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>
304void
Howard Hinnant10f25d22011-05-27 20:52:28305__bitset<_N_words, _Size>::flip() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16306{
307 // do middle whole words
308 size_type __n = _Size;
309 __storage_pointer __p = __first_;
310 for (; __n >= __bits_per_word; ++__p, __n -= __bits_per_word)
311 *__p = ~*__p;
312 // do last partial word
313 if (__n > 0)
314 {
315 __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
316 __storage_type __b = *__p & __m;
317 *__p &= ~__m;
318 *__p |= ~__b & __m;
319 }
320}
321
322template <size_t _N_words, size_t _Size>
323unsigned long
324__bitset<_N_words, _Size>::to_ulong(false_type) const
325{
326 const_iterator __e = __make_iter(_Size);
Howard Hinnant0949eed2011-06-30 21:18:19327 const_iterator __i = _VSTD::find(__make_iter(sizeof(unsigned long) * CHAR_BIT), __e, true);
Howard Hinnantbc8d3f92010-05-11 19:42:16328 if (__i != __e)
329#ifndef _LIBCPP_NO_EXCEPTIONS
330 throw overflow_error("bitset to_ulong overflow error");
331#else
332 assert(!"bitset to_ulong overflow error");
333#endif
334 return __first_[0];
335}
336
337template <size_t _N_words, size_t _Size>
Evgeniy Stepanova3b25f82015-11-07 01:22:13338inline
Howard Hinnantbc8d3f92010-05-11 19:42:16339unsigned long
340__bitset<_N_words, _Size>::to_ulong(true_type) const
341{
342 return __first_[0];
343}
344
345template <size_t _N_words, size_t _Size>
346unsigned long long
347__bitset<_N_words, _Size>::to_ullong(false_type) const
348{
349 const_iterator __e = __make_iter(_Size);
Howard Hinnant0949eed2011-06-30 21:18:19350 const_iterator __i = _VSTD::find(__make_iter(sizeof(unsigned long long) * CHAR_BIT), __e, true);
Howard Hinnantbc8d3f92010-05-11 19:42:16351 if (__i != __e)
352#ifndef _LIBCPP_NO_EXCEPTIONS
353 throw overflow_error("bitset to_ullong overflow error");
354#else
355 assert(!"bitset to_ullong overflow error");
356#endif
357 return to_ullong(true_type());
358}
359
360template <size_t _N_words, size_t _Size>
Evgeniy Stepanova3b25f82015-11-07 01:22:13361inline
Howard Hinnantbc8d3f92010-05-11 19:42:16362unsigned long long
363__bitset<_N_words, _Size>::to_ullong(true_type) const
364{
365 return to_ullong(true_type(), integral_constant<bool, sizeof(__storage_type) < sizeof(unsigned long long)>());
366}
367
368template <size_t _N_words, size_t _Size>
Evgeniy Stepanova3b25f82015-11-07 01:22:13369inline
Howard Hinnantbc8d3f92010-05-11 19:42:16370unsigned long long
371__bitset<_N_words, _Size>::to_ullong(true_type, false_type) const
372{
373 return __first_[0];
374}
375
376template <size_t _N_words, size_t _Size>
377unsigned long long
378__bitset<_N_words, _Size>::to_ullong(true_type, true_type) const
379{
380 unsigned long long __r = __first_[0];
381 for (std::size_t __i = 1; __i < sizeof(unsigned long long) / sizeof(__storage_type); ++__i)
382 __r |= static_cast<unsigned long long>(__first_[__i]) << (sizeof(__storage_type) * CHAR_BIT);
383 return __r;
384}
385
386template <size_t _N_words, size_t _Size>
387bool
Howard Hinnant10f25d22011-05-27 20:52:28388__bitset<_N_words, _Size>::all() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16389{
390 // do middle whole words
391 size_type __n = _Size;
392 __const_storage_pointer __p = __first_;
393 for (; __n >= __bits_per_word; ++__p, __n -= __bits_per_word)
394 if (~*__p)
395 return false;
396 // do last partial word
397 if (__n > 0)
398 {
399 __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
400 if (~*__p & __m)
401 return false;
402 }
403 return true;
404}
405
406template <size_t _N_words, size_t _Size>
407bool
Howard Hinnant10f25d22011-05-27 20:52:28408__bitset<_N_words, _Size>::any() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16409{
410 // do middle whole words
411 size_type __n = _Size;
412 __const_storage_pointer __p = __first_;
413 for (; __n >= __bits_per_word; ++__p, __n -= __bits_per_word)
414 if (*__p)
415 return true;
416 // do last partial word
417 if (__n > 0)
418 {
419 __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
420 if (*__p & __m)
421 return true;
422 }
423 return false;
424}
425
426template <size_t _N_words, size_t _Size>
Evgeniy Stepanova3b25f82015-11-07 01:22:13427inline
Howard Hinnantbc8d3f92010-05-11 19:42:16428size_t
Howard Hinnant10f25d22011-05-27 20:52:28429__bitset<_N_words, _Size>::__hash_code() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16430{
431 size_t __h = 0;
432 for (size_type __i = 0; __i < _N_words; ++__i)
433 __h ^= __first_[__i];
434 return __h;
435}
436
437template <size_t _Size>
438class __bitset<1, _Size>
439{
440public:
441 typedef ptrdiff_t difference_type;
442 typedef size_t size_type;
Howard Hinnantf867f632012-05-07 16:50:38443 typedef size_type __storage_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16444protected:
445 typedef __bitset __self;
Howard Hinnantbc8d3f92010-05-11 19:42:16446 typedef __storage_type* __storage_pointer;
447 typedef const __storage_type* __const_storage_pointer;
448 static const unsigned __bits_per_word = static_cast<unsigned>(sizeof(__storage_type) * CHAR_BIT);
449
450 friend class __bit_reference<__bitset>;
451 friend class __bit_const_reference<__bitset>;
452 friend class __bit_iterator<__bitset, false>;
453 friend class __bit_iterator<__bitset, true>;
Howard Hinnant4ae952a2012-08-17 17:10:18454 friend struct __bit_array<__bitset>;
Howard Hinnantbc8d3f92010-05-11 19:42:16455
456 __storage_type __first_;
457
458 typedef __bit_reference<__bitset> reference;
459 typedef __bit_const_reference<__bitset> const_reference;
460 typedef __bit_iterator<__bitset, false> iterator;
461 typedef __bit_iterator<__bitset, true> const_iterator;
462
Evgeniy Stepanova3b25f82015-11-07 01:22:13463 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant90d87232012-07-07 17:04:52464 _LIBCPP_CONSTEXPR __bitset() _NOEXCEPT;
Evgeniy Stepanova3b25f82015-11-07 01:22:13465 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant90d87232012-07-07 17:04:52466 explicit _LIBCPP_CONSTEXPR __bitset(unsigned long long __v) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16467
Howard Hinnant10f25d22011-05-27 20:52:28468 _LIBCPP_INLINE_VISIBILITY reference __make_ref(size_t __pos) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16469 {return reference(&__first_, __storage_type(1) << __pos);}
Howard Hinnant90d87232012-07-07 17:04:52470 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR const_reference __make_ref(size_t __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16471 {return const_reference(&__first_, __storage_type(1) << __pos);}
Howard Hinnant10f25d22011-05-27 20:52:28472 _LIBCPP_INLINE_VISIBILITY iterator __make_iter(size_t __pos) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16473 {return iterator(&__first_ + __pos / __bits_per_word, __pos % __bits_per_word);}
Howard Hinnant10f25d22011-05-27 20:52:28474 _LIBCPP_INLINE_VISIBILITY const_iterator __make_iter(size_t __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16475 {return const_iterator(&__first_ + __pos / __bits_per_word, __pos % __bits_per_word);}
476
Evgeniy Stepanova3b25f82015-11-07 01:22:13477 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant10f25d22011-05-27 20:52:28478 void operator&=(const __bitset& __v) _NOEXCEPT;
Evgeniy Stepanova3b25f82015-11-07 01:22:13479 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant10f25d22011-05-27 20:52:28480 void operator|=(const __bitset& __v) _NOEXCEPT;
Evgeniy Stepanova3b25f82015-11-07 01:22:13481 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant10f25d22011-05-27 20:52:28482 void operator^=(const __bitset& __v) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16483
Evgeniy Stepanova3b25f82015-11-07 01:22:13484 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant10f25d22011-05-27 20:52:28485 void flip() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16486
Evgeniy Stepanova3b25f82015-11-07 01:22:13487 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16488 unsigned long to_ulong() const;
Evgeniy Stepanova3b25f82015-11-07 01:22:13489 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16490 unsigned long long to_ullong() const;
491
Evgeniy Stepanova3b25f82015-11-07 01:22:13492 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant10f25d22011-05-27 20:52:28493 bool all() const _NOEXCEPT;
Evgeniy Stepanova3b25f82015-11-07 01:22:13494 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant10f25d22011-05-27 20:52:28495 bool any() const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16496
Evgeniy Stepanova3b25f82015-11-07 01:22:13497 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant10f25d22011-05-27 20:52:28498 size_t __hash_code() const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16499};
500
501template <size_t _Size>
Evgeniy Stepanova3b25f82015-11-07 01:22:13502inline
Howard Hinnant90d87232012-07-07 17:04:52503_LIBCPP_CONSTEXPR
Howard Hinnant10f25d22011-05-27 20:52:28504__bitset<1, _Size>::__bitset() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16505 : __first_(0)
506{
507}
508
509template <size_t _Size>
Evgeniy Stepanova3b25f82015-11-07 01:22:13510inline
Howard Hinnant90d87232012-07-07 17:04:52511_LIBCPP_CONSTEXPR
Howard Hinnant10f25d22011-05-27 20:52:28512__bitset<1, _Size>::__bitset(unsigned long long __v) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16513 : __first_(static_cast<__storage_type>(__v))
514{
515}
516
517template <size_t _Size>
Evgeniy Stepanova3b25f82015-11-07 01:22:13518inline
Howard Hinnantbc8d3f92010-05-11 19:42:16519void
Howard Hinnant10f25d22011-05-27 20:52:28520__bitset<1, _Size>::operator&=(const __bitset& __v) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16521{
522 __first_ &= __v.__first_;
523}
524
525template <size_t _Size>
Evgeniy Stepanova3b25f82015-11-07 01:22:13526inline
Howard Hinnantbc8d3f92010-05-11 19:42:16527void
Howard Hinnant10f25d22011-05-27 20:52:28528__bitset<1, _Size>::operator|=(const __bitset& __v) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16529{
530 __first_ |= __v.__first_;
531}
532
533template <size_t _Size>
Evgeniy Stepanova3b25f82015-11-07 01:22:13534inline
Howard Hinnantbc8d3f92010-05-11 19:42:16535void
Howard Hinnant10f25d22011-05-27 20:52:28536__bitset<1, _Size>::operator^=(const __bitset& __v) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16537{
538 __first_ ^= __v.__first_;
539}
540
541template <size_t _Size>
Evgeniy Stepanova3b25f82015-11-07 01:22:13542inline
Howard Hinnantbc8d3f92010-05-11 19:42:16543void
Howard Hinnant10f25d22011-05-27 20:52:28544__bitset<1, _Size>::flip() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16545{
546 __storage_type __m = ~__storage_type(0) >> (__bits_per_word - _Size);
547 __first_ = ~__first_;
548 __first_ &= __m;
549}
550
551template <size_t _Size>
Evgeniy Stepanova3b25f82015-11-07 01:22:13552inline
Howard Hinnantbc8d3f92010-05-11 19:42:16553unsigned long
554__bitset<1, _Size>::to_ulong() const
555{
556 return __first_;
557}
558
559template <size_t _Size>
Evgeniy Stepanova3b25f82015-11-07 01:22:13560inline
Howard Hinnantbc8d3f92010-05-11 19:42:16561unsigned long long
562__bitset<1, _Size>::to_ullong() const
563{
564 return __first_;
565}
566
567template <size_t _Size>
Evgeniy Stepanova3b25f82015-11-07 01:22:13568inline
Howard Hinnantbc8d3f92010-05-11 19:42:16569bool
Howard Hinnant10f25d22011-05-27 20:52:28570__bitset<1, _Size>::all() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16571{
572 __storage_type __m = ~__storage_type(0) >> (__bits_per_word - _Size);
573 return !(~__first_ & __m);
574}
575
576template <size_t _Size>
Evgeniy Stepanova3b25f82015-11-07 01:22:13577inline
Howard Hinnantbc8d3f92010-05-11 19:42:16578bool
Howard Hinnant10f25d22011-05-27 20:52:28579__bitset<1, _Size>::any() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16580{
581 __storage_type __m = ~__storage_type(0) >> (__bits_per_word - _Size);
582 return __first_ & __m;
583}
584
585template <size_t _Size>
Evgeniy Stepanova3b25f82015-11-07 01:22:13586inline
Howard Hinnantbc8d3f92010-05-11 19:42:16587size_t
Howard Hinnant10f25d22011-05-27 20:52:28588__bitset<1, _Size>::__hash_code() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16589{
590 return __first_;
591}
592
593template <>
594class __bitset<0, 0>
595{
596public:
597 typedef ptrdiff_t difference_type;
598 typedef size_t size_type;
Howard Hinnantf867f632012-05-07 16:50:38599 typedef size_type __storage_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16600protected:
601 typedef __bitset __self;
Howard Hinnantbc8d3f92010-05-11 19:42:16602 typedef __storage_type* __storage_pointer;
603 typedef const __storage_type* __const_storage_pointer;
604 static const unsigned __bits_per_word = static_cast<unsigned>(sizeof(__storage_type) * CHAR_BIT);
605
606 friend class __bit_reference<__bitset>;
607 friend class __bit_const_reference<__bitset>;
608 friend class __bit_iterator<__bitset, false>;
609 friend class __bit_iterator<__bitset, true>;
Howard Hinnantec3773c2011-12-01 20:21:04610 friend struct __bit_array<__bitset>;
Howard Hinnantbc8d3f92010-05-11 19:42:16611
612 typedef __bit_reference<__bitset> reference;
613 typedef __bit_const_reference<__bitset> const_reference;
614 typedef __bit_iterator<__bitset, false> iterator;
615 typedef __bit_iterator<__bitset, true> const_iterator;
616
Evgeniy Stepanova3b25f82015-11-07 01:22:13617 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant90d87232012-07-07 17:04:52618 _LIBCPP_CONSTEXPR __bitset() _NOEXCEPT;
Evgeniy Stepanova3b25f82015-11-07 01:22:13619 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant90d87232012-07-07 17:04:52620 explicit _LIBCPP_CONSTEXPR __bitset(unsigned long long) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16621
Howard Hinnant10f25d22011-05-27 20:52:28622 _LIBCPP_INLINE_VISIBILITY reference __make_ref(size_t) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16623 {return reference(0, 1);}
Howard Hinnant90d87232012-07-07 17:04:52624 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR const_reference __make_ref(size_t) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16625 {return const_reference(0, 1);}
Howard Hinnantec3773c2011-12-01 20:21:04626 _LIBCPP_INLINE_VISIBILITY iterator __make_iter(size_t) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16627 {return iterator(0, 0);}
Howard Hinnantec3773c2011-12-01 20:21:04628 _LIBCPP_INLINE_VISIBILITY const_iterator __make_iter(size_t) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16629 {return const_iterator(0, 0);}
630
Howard Hinnant10f25d22011-05-27 20:52:28631 _LIBCPP_INLINE_VISIBILITY void operator&=(const __bitset&) _NOEXCEPT {}
632 _LIBCPP_INLINE_VISIBILITY void operator|=(const __bitset&) _NOEXCEPT {}
633 _LIBCPP_INLINE_VISIBILITY void operator^=(const __bitset&) _NOEXCEPT {}
Howard Hinnantbc8d3f92010-05-11 19:42:16634
Howard Hinnant10f25d22011-05-27 20:52:28635 _LIBCPP_INLINE_VISIBILITY void flip() _NOEXCEPT {}
Howard Hinnantbc8d3f92010-05-11 19:42:16636
637 _LIBCPP_INLINE_VISIBILITY unsigned long to_ulong() const {return 0;}
638 _LIBCPP_INLINE_VISIBILITY unsigned long long to_ullong() const {return 0;}
639
Howard Hinnant10f25d22011-05-27 20:52:28640 _LIBCPP_INLINE_VISIBILITY bool all() const _NOEXCEPT {return true;}
641 _LIBCPP_INLINE_VISIBILITY bool any() const _NOEXCEPT {return false;}
Howard Hinnantbc8d3f92010-05-11 19:42:16642
Howard Hinnant10f25d22011-05-27 20:52:28643 _LIBCPP_INLINE_VISIBILITY size_t __hash_code() const _NOEXCEPT {return 0;}
Howard Hinnantbc8d3f92010-05-11 19:42:16644};
645
Evgeniy Stepanova3b25f82015-11-07 01:22:13646inline
Howard Hinnant90d87232012-07-07 17:04:52647_LIBCPP_CONSTEXPR
Howard Hinnant10f25d22011-05-27 20:52:28648__bitset<0, 0>::__bitset() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16649{
650}
651
Evgeniy Stepanova3b25f82015-11-07 01:22:13652inline
Howard Hinnant90d87232012-07-07 17:04:52653_LIBCPP_CONSTEXPR
Howard Hinnant10f25d22011-05-27 20:52:28654__bitset<0, 0>::__bitset(unsigned long long) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16655{
656}
657
Howard Hinnant0f678bd2013-08-12 18:38:34658template <size_t _Size> class _LIBCPP_TYPE_VIS_ONLY bitset;
659template <size_t _Size> struct _LIBCPP_TYPE_VIS_ONLY hash<bitset<_Size> >;
Howard Hinnantbc8d3f92010-05-11 19:42:16660
661template <size_t _Size>
Howard Hinnant0f678bd2013-08-12 18:38:34662class _LIBCPP_TYPE_VIS_ONLY bitset
Howard Hinnantbc8d3f92010-05-11 19:42:16663 : private __bitset<_Size == 0 ? 0 : (_Size - 1) / (sizeof(size_t) * CHAR_BIT) + 1, _Size>
664{
Howard Hinnant11a31d02013-03-06 17:30:26665public:
Howard Hinnantbc8d3f92010-05-11 19:42:16666 static const unsigned __n_words = _Size == 0 ? 0 : (_Size - 1) / (sizeof(size_t) * CHAR_BIT) + 1;
667 typedef __bitset<__n_words, _Size> base;
668
Howard Hinnant324bb032010-08-22 00:02:43669public:
Howard Hinnantbc8d3f92010-05-11 19:42:16670 typedef typename base::reference reference;
671 typedef typename base::const_reference const_reference;
672
Howard Hinnant324bb032010-08-22 00:02:43673 // 23.3.5.1 constructors:
Howard Hinnant90d87232012-07-07 17:04:52674 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR bitset() _NOEXCEPT {}
675 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
676 bitset(unsigned long long __v) _NOEXCEPT : base(__v) {}
Howard Hinnant34d6b192010-11-17 21:53:14677 template<class _CharT>
678 explicit bitset(const _CharT* __str,
679 typename basic_string<_CharT>::size_type __n = basic_string<_CharT>::npos,
680 _CharT __zero = _CharT('0'), _CharT __one = _CharT('1'));
Howard Hinnant324bb032010-08-22 00:02:43681 template<class _CharT, class _Traits, class _Allocator>
682 explicit bitset(const basic_string<_CharT,_Traits,_Allocator>& __str,
683 typename basic_string<_CharT,_Traits,_Allocator>::size_type __pos = 0,
684 typename basic_string<_CharT,_Traits,_Allocator>::size_type __n =
Howard Hinnantbc8d3f92010-05-11 19:42:16685 (basic_string<_CharT,_Traits,_Allocator>::npos),
Howard Hinnant324bb032010-08-22 00:02:43686 _CharT __zero = _CharT('0'), _CharT __one = _CharT('1'));
Howard Hinnantbc8d3f92010-05-11 19:42:16687
Howard Hinnant324bb032010-08-22 00:02:43688 // 23.3.5.2 bitset operations:
Evgeniy Stepanova3b25f82015-11-07 01:22:13689 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant10f25d22011-05-27 20:52:28690 bitset& operator&=(const bitset& __rhs) _NOEXCEPT;
Evgeniy Stepanova3b25f82015-11-07 01:22:13691 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant10f25d22011-05-27 20:52:28692 bitset& operator|=(const bitset& __rhs) _NOEXCEPT;
Evgeniy Stepanova3b25f82015-11-07 01:22:13693 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant10f25d22011-05-27 20:52:28694 bitset& operator^=(const bitset& __rhs) _NOEXCEPT;
695 bitset& operator<<=(size_t __pos) _NOEXCEPT;
696 bitset& operator>>=(size_t __pos) _NOEXCEPT;
Evgeniy Stepanova3b25f82015-11-07 01:22:13697 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant10f25d22011-05-27 20:52:28698 bitset& set() _NOEXCEPT;
Howard Hinnant324bb032010-08-22 00:02:43699 bitset& set(size_t __pos, bool __val = true);
Evgeniy Stepanova3b25f82015-11-07 01:22:13700 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant10f25d22011-05-27 20:52:28701 bitset& reset() _NOEXCEPT;
Howard Hinnant324bb032010-08-22 00:02:43702 bitset& reset(size_t __pos);
Evgeniy Stepanova3b25f82015-11-07 01:22:13703 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant10f25d22011-05-27 20:52:28704 bitset operator~() const _NOEXCEPT;
Evgeniy Stepanova3b25f82015-11-07 01:22:13705 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant10f25d22011-05-27 20:52:28706 bitset& flip() _NOEXCEPT;
Howard Hinnant324bb032010-08-22 00:02:43707 bitset& flip(size_t __pos);
Howard Hinnantbc8d3f92010-05-11 19:42:16708
Howard Hinnant324bb032010-08-22 00:02:43709 // element access:
Howard Hinnant90d87232012-07-07 17:04:52710 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
711 const_reference operator[](size_t __p) const {return base::__make_ref(__p);}
Howard Hinnantbc8d3f92010-05-11 19:42:16712 _LIBCPP_INLINE_VISIBILITY reference operator[](size_t __p) {return base::__make_ref(__p);}
Evgeniy Stepanova3b25f82015-11-07 01:22:13713 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant324bb032010-08-22 00:02:43714 unsigned long to_ulong() const;
Evgeniy Stepanova3b25f82015-11-07 01:22:13715 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant324bb032010-08-22 00:02:43716 unsigned long long to_ullong() const;
717 template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16718 basic_string<_CharT, _Traits, _Allocator> to_string(_CharT __zero = _CharT('0'),
Howard Hinnant324bb032010-08-22 00:02:43719 _CharT __one = _CharT('1')) const;
720 template <class _CharT, class _Traits>
Evgeniy Stepanova3b25f82015-11-07 01:22:13721 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16722 basic_string<_CharT, _Traits, allocator<_CharT> > to_string(_CharT __zero = _CharT('0'),
Howard Hinnant324bb032010-08-22 00:02:43723 _CharT __one = _CharT('1')) const;
724 template <class _CharT>
Evgeniy Stepanova3b25f82015-11-07 01:22:13725 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16726 basic_string<_CharT, char_traits<_CharT>, allocator<_CharT> > to_string(_CharT __zero = _CharT('0'),
Howard Hinnant324bb032010-08-22 00:02:43727 _CharT __one = _CharT('1')) const;
Evgeniy Stepanova3b25f82015-11-07 01:22:13728 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16729 basic_string<char, char_traits<char>, allocator<char> > to_string(char __zero = '0',
Howard Hinnant324bb032010-08-22 00:02:43730 char __one = '1') const;
Evgeniy Stepanova3b25f82015-11-07 01:22:13731 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant10f25d22011-05-27 20:52:28732 size_t count() const _NOEXCEPT;
Howard Hinnant90d87232012-07-07 17:04:52733 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR size_t size() const _NOEXCEPT {return _Size;}
Evgeniy Stepanova3b25f82015-11-07 01:22:13734 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant10f25d22011-05-27 20:52:28735 bool operator==(const bitset& __rhs) const _NOEXCEPT;
Evgeniy Stepanova3b25f82015-11-07 01:22:13736 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant10f25d22011-05-27 20:52:28737 bool operator!=(const bitset& __rhs) const _NOEXCEPT;
Howard Hinnant324bb032010-08-22 00:02:43738 bool test(size_t __pos) const;
Evgeniy Stepanova3b25f82015-11-07 01:22:13739 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant10f25d22011-05-27 20:52:28740 bool all() const _NOEXCEPT;
Evgeniy Stepanova3b25f82015-11-07 01:22:13741 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant10f25d22011-05-27 20:52:28742 bool any() const _NOEXCEPT;
743 _LIBCPP_INLINE_VISIBILITY bool none() const _NOEXCEPT {return !any();}
Evgeniy Stepanova3b25f82015-11-07 01:22:13744 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant10f25d22011-05-27 20:52:28745 bitset operator<<(size_t __pos) const _NOEXCEPT;
Evgeniy Stepanova3b25f82015-11-07 01:22:13746 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant10f25d22011-05-27 20:52:28747 bitset operator>>(size_t __pos) const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16748
749private:
750
Howard Hinnant422a53f2010-09-21 21:28:23751 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant10f25d22011-05-27 20:52:28752 size_t __hash_code() const _NOEXCEPT {return base::__hash_code();}
Howard Hinnantbc8d3f92010-05-11 19:42:16753
754 friend struct hash<bitset>;
755};
756
757template <size_t _Size>
Howard Hinnant34d6b192010-11-17 21:53:14758template<class _CharT>
759bitset<_Size>::bitset(const _CharT* __str,
760 typename basic_string<_CharT>::size_type __n,
761 _CharT __zero, _CharT __one)
Howard Hinnantbc8d3f92010-05-11 19:42:16762{
Howard Hinnant0949eed2011-06-30 21:18:19763 size_t __rlen = _VSTD::min(__n, char_traits<_CharT>::length(__str));
Howard Hinnantbc8d3f92010-05-11 19:42:16764 for (size_t __i = 0; __i < __rlen; ++__i)
Howard Hinnant34d6b192010-11-17 21:53:14765 if (__str[__i] != __zero && __str[__i] != __one)
Howard Hinnantbc8d3f92010-05-11 19:42:16766#ifndef _LIBCPP_NO_EXCEPTIONS
767 throw invalid_argument("bitset string ctor has invalid argument");
768#else
769 assert(!"bitset string ctor has invalid argument");
770#endif
Howard Hinnant99968442011-11-29 18:15:50771 size_t _Mp = _VSTD::min(__rlen, _Size);
Howard Hinnantbc8d3f92010-05-11 19:42:16772 size_t __i = 0;
Howard Hinnant99968442011-11-29 18:15:50773 for (; __i < _Mp; ++__i)
Howard Hinnantbc8d3f92010-05-11 19:42:16774 {
Howard Hinnant99968442011-11-29 18:15:50775 _CharT __c = __str[_Mp - 1 - __i];
Howard Hinnant34d6b192010-11-17 21:53:14776 if (__c == __zero)
Howard Hinnantbc8d3f92010-05-11 19:42:16777 (*this)[__i] = false;
Howard Hinnant34d6b192010-11-17 21:53:14778 else
Howard Hinnantbc8d3f92010-05-11 19:42:16779 (*this)[__i] = true;
Howard Hinnantbc8d3f92010-05-11 19:42:16780 }
Howard Hinnant0949eed2011-06-30 21:18:19781 _VSTD::fill(base::__make_iter(__i), base::__make_iter(_Size), false);
Howard Hinnantbc8d3f92010-05-11 19:42:16782}
783
784template <size_t _Size>
Howard Hinnant324bb032010-08-22 00:02:43785template<class _CharT, class _Traits, class _Allocator>
786bitset<_Size>::bitset(const basic_string<_CharT,_Traits,_Allocator>& __str,
787 typename basic_string<_CharT,_Traits,_Allocator>::size_type __pos,
Howard Hinnantbc8d3f92010-05-11 19:42:16788 typename basic_string<_CharT,_Traits,_Allocator>::size_type __n,
789 _CharT __zero, _CharT __one)
790{
791 if (__pos > __str.size())
792#ifndef _LIBCPP_NO_EXCEPTIONS
793 throw out_of_range("bitset string pos out of range");
794#else
795 assert(!"bitset string pos out of range");
796#endif
Howard Hinnant0949eed2011-06-30 21:18:19797 size_t __rlen = _VSTD::min(__n, __str.size() - __pos);
Howard Hinnantbc8d3f92010-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))
800#ifndef _LIBCPP_NO_EXCEPTIONS
801 throw invalid_argument("bitset string ctor has invalid argument");
802#else
803 assert(!"bitset string ctor has invalid argument");
804#endif
Howard Hinnant99968442011-11-29 18:15:50805 size_t _Mp = _VSTD::min(__rlen, _Size);
Howard Hinnantbc8d3f92010-05-11 19:42:16806 size_t __i = 0;
Howard Hinnant99968442011-11-29 18:15:50807 for (; __i < _Mp; ++__i)
Howard Hinnantbc8d3f92010-05-11 19:42:16808 {
Howard Hinnant99968442011-11-29 18:15:50809 _CharT __c = __str[__pos + _Mp - 1 - __i];
Howard Hinnantbc8d3f92010-05-11 19:42:16810 if (_Traits::eq(__c, __zero))
811 (*this)[__i] = false;
812 else
813 (*this)[__i] = true;
814 }
Howard Hinnant0949eed2011-06-30 21:18:19815 _VSTD::fill(base::__make_iter(__i), base::__make_iter(_Size), false);
Howard Hinnantbc8d3f92010-05-11 19:42:16816}
817
818template <size_t _Size>
Evgeniy Stepanova3b25f82015-11-07 01:22:13819inline
Howard Hinnantbc8d3f92010-05-11 19:42:16820bitset<_Size>&
Howard Hinnant10f25d22011-05-27 20:52:28821bitset<_Size>::operator&=(const bitset& __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16822{
823 base::operator&=(__rhs);
824 return *this;
825}
826
827template <size_t _Size>
Evgeniy Stepanova3b25f82015-11-07 01:22:13828inline
Howard Hinnantbc8d3f92010-05-11 19:42:16829bitset<_Size>&
Howard Hinnant10f25d22011-05-27 20:52:28830bitset<_Size>::operator|=(const bitset& __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16831{
832 base::operator|=(__rhs);
833 return *this;
834}
835
836template <size_t _Size>
Evgeniy Stepanova3b25f82015-11-07 01:22:13837inline
Howard Hinnantbc8d3f92010-05-11 19:42:16838bitset<_Size>&
Howard Hinnant10f25d22011-05-27 20:52:28839bitset<_Size>::operator^=(const bitset& __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16840{
841 base::operator^=(__rhs);
842 return *this;
843}
844
845template <size_t _Size>
846bitset<_Size>&
Howard Hinnant10f25d22011-05-27 20:52:28847bitset<_Size>::operator<<=(size_t __pos) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16848{
Howard Hinnant0949eed2011-06-30 21:18:19849 __pos = _VSTD::min(__pos, _Size);
850 _VSTD::copy_backward(base::__make_iter(0), base::__make_iter(_Size - __pos), base::__make_iter(_Size));
851 _VSTD::fill_n(base::__make_iter(0), __pos, false);
Howard Hinnantbc8d3f92010-05-11 19:42:16852 return *this;
853}
854
855template <size_t _Size>
856bitset<_Size>&
Howard Hinnant10f25d22011-05-27 20:52:28857bitset<_Size>::operator>>=(size_t __pos) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16858{
Howard Hinnant0949eed2011-06-30 21:18:19859 __pos = _VSTD::min(__pos, _Size);
860 _VSTD::copy(base::__make_iter(__pos), base::__make_iter(_Size), base::__make_iter(0));
861 _VSTD::fill_n(base::__make_iter(_Size - __pos), __pos, false);
Howard Hinnantbc8d3f92010-05-11 19:42:16862 return *this;
863}
864
865template <size_t _Size>
Evgeniy Stepanova3b25f82015-11-07 01:22:13866inline
Howard Hinnantbc8d3f92010-05-11 19:42:16867bitset<_Size>&
Howard Hinnant10f25d22011-05-27 20:52:28868bitset<_Size>::set() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16869{
Howard Hinnant0949eed2011-06-30 21:18:19870 _VSTD::fill_n(base::__make_iter(0), _Size, true);
Howard Hinnantbc8d3f92010-05-11 19:42:16871 return *this;
872}
873
874template <size_t _Size>
875bitset<_Size>&
876bitset<_Size>::set(size_t __pos, bool __val)
877{
878 if (__pos >= _Size)
879#ifndef _LIBCPP_NO_EXCEPTIONS
880 throw out_of_range("bitset set argument out of range");
881#else
882 assert(!"bitset set argument out of range");
883#endif
884 (*this)[__pos] = __val;
885 return *this;
886}
887
888template <size_t _Size>
Evgeniy Stepanova3b25f82015-11-07 01:22:13889inline
Howard Hinnantbc8d3f92010-05-11 19:42:16890bitset<_Size>&
Howard Hinnant10f25d22011-05-27 20:52:28891bitset<_Size>::reset() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16892{
Howard Hinnant0949eed2011-06-30 21:18:19893 _VSTD::fill_n(base::__make_iter(0), _Size, false);
Howard Hinnantbc8d3f92010-05-11 19:42:16894 return *this;
895}
896
897template <size_t _Size>
898bitset<_Size>&
899bitset<_Size>::reset(size_t __pos)
900{
901 if (__pos >= _Size)
902#ifndef _LIBCPP_NO_EXCEPTIONS
903 throw out_of_range("bitset reset argument out of range");
904#else
905 assert(!"bitset reset argument out of range");
906#endif
907 (*this)[__pos] = false;
908 return *this;
909}
910
911template <size_t _Size>
Evgeniy Stepanova3b25f82015-11-07 01:22:13912inline
Howard Hinnantbc8d3f92010-05-11 19:42:16913bitset<_Size>
Howard Hinnant10f25d22011-05-27 20:52:28914bitset<_Size>::operator~() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16915{
916 bitset __x(*this);
917 __x.flip();
918 return __x;
919}
920
921template <size_t _Size>
Evgeniy Stepanova3b25f82015-11-07 01:22:13922inline
Howard Hinnantbc8d3f92010-05-11 19:42:16923bitset<_Size>&
Howard Hinnant10f25d22011-05-27 20:52:28924bitset<_Size>::flip() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16925{
926 base::flip();
927 return *this;
928}
929
930template <size_t _Size>
931bitset<_Size>&
932bitset<_Size>::flip(size_t __pos)
933{
934 if (__pos >= _Size)
935#ifndef _LIBCPP_NO_EXCEPTIONS
936 throw out_of_range("bitset flip argument out of range");
937#else
938 assert(!"bitset flip argument out of range");
939#endif
940 reference r = base::__make_ref(__pos);
941 r = ~r;
942 return *this;
943}
944
945template <size_t _Size>
Evgeniy Stepanova3b25f82015-11-07 01:22:13946inline
Howard Hinnantbc8d3f92010-05-11 19:42:16947unsigned long
948bitset<_Size>::to_ulong() const
949{
950 return base::to_ulong();
951}
952
953template <size_t _Size>
Evgeniy Stepanova3b25f82015-11-07 01:22:13954inline
Howard Hinnantbc8d3f92010-05-11 19:42:16955unsigned long long
956bitset<_Size>::to_ullong() const
957{
958 return base::to_ullong();
959}
960
961template <size_t _Size>
Howard Hinnant324bb032010-08-22 00:02:43962template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16963basic_string<_CharT, _Traits, _Allocator>
964bitset<_Size>::to_string(_CharT __zero, _CharT __one) const
965{
966 basic_string<_CharT, _Traits, _Allocator> __r(_Size, __zero);
967 for (size_t __i = 0; __i < _Size; ++__i)
968 {
969 if ((*this)[__i])
970 __r[_Size - 1 - __i] = __one;
971 }
972 return __r;
973}
974
975template <size_t _Size>
Howard Hinnant324bb032010-08-22 00:02:43976template <class _CharT, class _Traits>
Evgeniy Stepanova3b25f82015-11-07 01:22:13977inline
Howard Hinnantbc8d3f92010-05-11 19:42:16978basic_string<_CharT, _Traits, allocator<_CharT> >
979bitset<_Size>::to_string(_CharT __zero, _CharT __one) const
980{
981 return to_string<_CharT, _Traits, allocator<_CharT> >(__zero, __one);
982}
983
984template <size_t _Size>
Howard Hinnant324bb032010-08-22 00:02:43985template <class _CharT>
Evgeniy Stepanova3b25f82015-11-07 01:22:13986inline
Howard Hinnantbc8d3f92010-05-11 19:42:16987basic_string<_CharT, char_traits<_CharT>, allocator<_CharT> >
988bitset<_Size>::to_string(_CharT __zero, _CharT __one) const
989{
990 return to_string<_CharT, char_traits<_CharT>, allocator<_CharT> >(__zero, __one);
991}
992
993template <size_t _Size>
Evgeniy Stepanova3b25f82015-11-07 01:22:13994inline
Howard Hinnantbc8d3f92010-05-11 19:42:16995basic_string<char, char_traits<char>, allocator<char> >
996bitset<_Size>::to_string(char __zero, char __one) const
997{
998 return to_string<char, char_traits<char>, allocator<char> >(__zero, __one);
999}
1000
1001template <size_t _Size>
Evgeniy Stepanova3b25f82015-11-07 01:22:131002inline
Howard Hinnantbc8d3f92010-05-11 19:42:161003size_t
Howard Hinnant10f25d22011-05-27 20:52:281004bitset<_Size>::count() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:161005{
Howard Hinnant0949eed2011-06-30 21:18:191006 return static_cast<size_t>(_VSTD::count(base::__make_iter(0), base::__make_iter(_Size), true));
Howard Hinnantbc8d3f92010-05-11 19:42:161007}
1008
1009template <size_t _Size>
Evgeniy Stepanova3b25f82015-11-07 01:22:131010inline
Howard Hinnantbc8d3f92010-05-11 19:42:161011bool
Howard Hinnant10f25d22011-05-27 20:52:281012bitset<_Size>::operator==(const bitset& __rhs) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:161013{
Howard Hinnant0949eed2011-06-30 21:18:191014 return _VSTD::equal(base::__make_iter(0), base::__make_iter(_Size), __rhs.__make_iter(0));
Howard Hinnantbc8d3f92010-05-11 19:42:161015}
1016
1017template <size_t _Size>
Evgeniy Stepanova3b25f82015-11-07 01:22:131018inline
Howard Hinnantbc8d3f92010-05-11 19:42:161019bool
Howard Hinnant10f25d22011-05-27 20:52:281020bitset<_Size>::operator!=(const bitset& __rhs) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:161021{
1022 return !(*this == __rhs);
1023}
1024
1025template <size_t _Size>
1026bool
1027bitset<_Size>::test(size_t __pos) const
1028{
1029 if (__pos >= _Size)
1030#ifndef _LIBCPP_NO_EXCEPTIONS
1031 throw out_of_range("bitset test argument out of range");
1032#else
1033 assert(!"bitset test argument out of range");
1034#endif
1035 return (*this)[__pos];
1036}
1037
1038template <size_t _Size>
Evgeniy Stepanova3b25f82015-11-07 01:22:131039inline
Howard Hinnantbc8d3f92010-05-11 19:42:161040bool
Howard Hinnant10f25d22011-05-27 20:52:281041bitset<_Size>::all() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:161042{
1043 return base::all();
1044}
1045
1046template <size_t _Size>
Evgeniy Stepanova3b25f82015-11-07 01:22:131047inline
Howard Hinnantbc8d3f92010-05-11 19:42:161048bool
Howard Hinnant10f25d22011-05-27 20:52:281049bitset<_Size>::any() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:161050{
1051 return base::any();
1052}
1053
1054template <size_t _Size>
Evgeniy Stepanova3b25f82015-11-07 01:22:131055inline
Howard Hinnantbc8d3f92010-05-11 19:42:161056bitset<_Size>
Howard Hinnant10f25d22011-05-27 20:52:281057bitset<_Size>::operator<<(size_t __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:161058{
1059 bitset __r = *this;
1060 __r <<= __pos;
1061 return __r;
1062}
1063
1064template <size_t _Size>
Evgeniy Stepanova3b25f82015-11-07 01:22:131065inline
Howard Hinnantbc8d3f92010-05-11 19:42:161066bitset<_Size>
Howard Hinnant10f25d22011-05-27 20:52:281067bitset<_Size>::operator>>(size_t __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:161068{
1069 bitset __r = *this;
1070 __r >>= __pos;
1071 return __r;
1072}
1073
1074template <size_t _Size>
1075inline _LIBCPP_INLINE_VISIBILITY
1076bitset<_Size>
Howard Hinnant10f25d22011-05-27 20:52:281077operator&(const bitset<_Size>& __x, const bitset<_Size>& __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:161078{
1079 bitset<_Size> __r = __x;
1080 __r &= __y;
1081 return __r;
1082}
1083
1084template <size_t _Size>
1085inline _LIBCPP_INLINE_VISIBILITY
1086bitset<_Size>
Howard Hinnant10f25d22011-05-27 20:52:281087operator|(const bitset<_Size>& __x, const bitset<_Size>& __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:161088{
1089 bitset<_Size> __r = __x;
1090 __r |= __y;
1091 return __r;
1092}
1093
1094template <size_t _Size>
1095inline _LIBCPP_INLINE_VISIBILITY
1096bitset<_Size>
Howard Hinnant10f25d22011-05-27 20:52:281097operator^(const bitset<_Size>& __x, const bitset<_Size>& __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:161098{
1099 bitset<_Size> __r = __x;
1100 __r ^= __y;
1101 return __r;
1102}
1103
1104template <size_t _Size>
Howard Hinnant0f678bd2013-08-12 18:38:341105struct _LIBCPP_TYPE_VIS_ONLY hash<bitset<_Size> >
Howard Hinnantbc8d3f92010-05-11 19:42:161106 : public unary_function<bitset<_Size>, size_t>
1107{
Howard Hinnant422a53f2010-09-21 21:28:231108 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant10f25d22011-05-27 20:52:281109 size_t operator()(const bitset<_Size>& __bs) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:161110 {return __bs.__hash_code();}
1111};
1112
Howard Hinnant464aa5c2011-07-18 15:51:591113template <class _CharT, class _Traits, size_t _Size>
1114basic_istream<_CharT, _Traits>&
1115operator>>(basic_istream<_CharT, _Traits>& __is, bitset<_Size>& __x);
1116
1117template <class _CharT, class _Traits, size_t _Size>
1118basic_ostream<_CharT, _Traits>&
1119operator<<(basic_ostream<_CharT, _Traits>& __os, const bitset<_Size>& __x);
1120
Howard Hinnantbc8d3f92010-05-11 19:42:161121_LIBCPP_END_NAMESPACE_STD
1122
1123#endif // _LIBCPP_BITSET