blob: 4755fbeb2152cda878c4b3a8456cae14be726378 [file] [log] [blame]
Howard Hinnant3e519522010-05-11 19:42:161// -*- C++ -*-
2//===---------------------------- bitset ----------------------------------===//
3//
Chandler Carruth57b08b02019-01-19 10:56:404// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5// See https://llvm.org/LICENSE.txt for license information.
6// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Howard Hinnant3e519522010-05-11 19:42:167//
8//===----------------------------------------------------------------------===//
9
10#ifndef _LIBCPP_BITSET
11#define _LIBCPP_BITSET
12
13/*
14 bitset synopsis
15
16namespace std
17{
18
19namespace std {
20
21template <size_t N>
22class bitset
23{
24public:
25 // bit reference:
26 class reference
27 {
28 friend class bitset;
Howard Hinnantd368a842011-05-27 20:52:2829 reference() noexcept;
Howard Hinnant3e519522010-05-11 19:42:1630 public:
Howard Hinnantd368a842011-05-27 20:52:2831 ~reference() noexcept;
32 reference& operator=(bool x) noexcept; // for b[i] = x;
33 reference& operator=(const reference&) noexcept; // for b[i] = b[j];
34 bool operator~() const noexcept; // flips the bit
35 operator bool() const noexcept; // for x = b[i];
36 reference& flip() noexcept; // for b[i].flip();
Howard Hinnant3e519522010-05-11 19:42:1637 };
38
39 // 23.3.5.1 constructors:
Howard Hinnantd368a842011-05-27 20:52:2840 constexpr bitset() noexcept;
41 constexpr bitset(unsigned long long val) noexcept;
Howard Hinnantd09f7112010-11-17 21:53:1442 template <class charT>
43 explicit bitset(const charT* str,
44 typename basic_string<charT>::size_type n = basic_string<charT>::npos,
45 charT zero = charT('0'), charT one = charT('1'));
Howard Hinnant3e519522010-05-11 19:42:1646 template<class charT, class traits, class Allocator>
47 explicit bitset(const basic_string<charT,traits,Allocator>& str,
48 typename basic_string<charT,traits,Allocator>::size_type pos = 0,
49 typename basic_string<charT,traits,Allocator>::size_type n =
50 basic_string<charT,traits,Allocator>::npos,
51 charT zero = charT('0'), charT one = charT('1'));
52
53 // 23.3.5.2 bitset operations:
Howard Hinnantd368a842011-05-27 20:52:2854 bitset& operator&=(const bitset& rhs) noexcept;
55 bitset& operator|=(const bitset& rhs) noexcept;
56 bitset& operator^=(const bitset& rhs) noexcept;
57 bitset& operator<<=(size_t pos) noexcept;
58 bitset& operator>>=(size_t pos) noexcept;
59 bitset& set() noexcept;
Howard Hinnant3e519522010-05-11 19:42:1660 bitset& set(size_t pos, bool val = true);
Howard Hinnantd368a842011-05-27 20:52:2861 bitset& reset() noexcept;
Howard Hinnant3e519522010-05-11 19:42:1662 bitset& reset(size_t pos);
Howard Hinnantd368a842011-05-27 20:52:2863 bitset operator~() const noexcept;
64 bitset& flip() noexcept;
Howard Hinnant3e519522010-05-11 19:42:1665 bitset& flip(size_t pos);
66
67 // element access:
68 constexpr bool operator[](size_t pos) const; // for b[i];
69 reference operator[](size_t pos); // for b[i];
70 unsigned long to_ulong() const;
71 unsigned long long to_ullong() const;
72 template <class charT, class traits, class Allocator>
73 basic_string<charT, traits, Allocator> to_string(charT zero = charT('0'), charT one = charT('1')) const;
74 template <class charT, class traits>
75 basic_string<charT, traits, allocator<charT> > to_string(charT zero = charT('0'), charT one = charT('1')) const;
76 template <class charT>
77 basic_string<charT, char_traits<charT>, allocator<charT> > to_string(charT zero = charT('0'), charT one = charT('1')) const;
78 basic_string<char, char_traits<char>, allocator<char> > to_string(char zero = '0', char one = '1') const;
Howard Hinnantd368a842011-05-27 20:52:2879 size_t count() const noexcept;
80 constexpr size_t size() const noexcept;
81 bool operator==(const bitset& rhs) const noexcept;
82 bool operator!=(const bitset& rhs) const noexcept;
Howard Hinnant3e519522010-05-11 19:42:1683 bool test(size_t pos) const;
Howard Hinnantd368a842011-05-27 20:52:2884 bool all() const noexcept;
85 bool any() const noexcept;
86 bool none() const noexcept;
87 bitset operator<<(size_t pos) const noexcept;
88 bitset operator>>(size_t pos) const noexcept;
Howard Hinnant3e519522010-05-11 19:42:1689};
90
91// 23.3.5.3 bitset operators:
92template <size_t N>
Howard Hinnantd368a842011-05-27 20:52:2893bitset<N> operator&(const bitset<N>&, const bitset<N>&) noexcept;
Howard Hinnant3e519522010-05-11 19:42:1694
95template <size_t N>
Howard Hinnantd368a842011-05-27 20:52:2896bitset<N> operator|(const bitset<N>&, const bitset<N>&) noexcept;
Howard Hinnant3e519522010-05-11 19:42:1697
98template <size_t N>
Howard Hinnantd368a842011-05-27 20:52:2899bitset<N> operator^(const bitset<N>&, const bitset<N>&) noexcept;
Howard Hinnant3e519522010-05-11 19:42:16100
101template <class charT, class traits, size_t N>
102basic_istream<charT, traits>&
103operator>>(basic_istream<charT, traits>& is, bitset<N>& x);
104
105template <class charT, class traits, size_t N>
106basic_ostream<charT, traits>&
107operator<<(basic_ostream<charT, traits>& os, const bitset<N>& x);
108
109template <size_t N> struct hash<std::bitset<N>>;
110
111} // std
112
113*/
114
Howard Hinnant3e519522010-05-11 19:42:16115#include <__config>
116#include <__bit_reference>
117#include <cstddef>
118#include <climits>
119#include <string>
120#include <stdexcept>
121#include <iosfwd>
122#include <__functional_base>
Howard Hinnant3e519522010-05-11 19:42:16123
Eric Fiseliera016efb2017-05-31 22:07:49124#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
125#pragma GCC system_header
126#endif
127
128_LIBCPP_PUSH_MACROS
129#include <__undef_macros>
130
Howard Hinnantab4f4382011-11-29 16:45:27131
Howard Hinnant3e519522010-05-11 19:42:16132_LIBCPP_BEGIN_NAMESPACE_STD
133
134template <size_t _N_words, size_t _Size>
Howard Hinnanta7744562011-07-02 20:33:23135class __bitset;
136
137template <size_t _N_words, size_t _Size>
138struct __has_storage_type<__bitset<_N_words, _Size> >
139{
140 static const bool value = true;
141};
142
143template <size_t _N_words, size_t _Size>
Howard Hinnant3e519522010-05-11 19:42:16144class __bitset
145{
146public:
147 typedef ptrdiff_t difference_type;
148 typedef size_t size_type;
Howard Hinnant0ae9efe2012-05-07 16:50:38149 typedef size_type __storage_type;
Howard Hinnant3e519522010-05-11 19:42:16150protected:
151 typedef __bitset __self;
Howard Hinnant3e519522010-05-11 19:42:16152 typedef __storage_type* __storage_pointer;
153 typedef const __storage_type* __const_storage_pointer;
154 static const unsigned __bits_per_word = static_cast<unsigned>(sizeof(__storage_type) * CHAR_BIT);
155
156 friend class __bit_reference<__bitset>;
157 friend class __bit_const_reference<__bitset>;
158 friend class __bit_iterator<__bitset, false>;
159 friend class __bit_iterator<__bitset, true>;
Howard Hinnant7ee27132012-08-17 17:10:18160 friend struct __bit_array<__bitset>;
Howard Hinnant3e519522010-05-11 19:42:16161
162 __storage_type __first_[_N_words];
163
164 typedef __bit_reference<__bitset> reference;
165 typedef __bit_const_reference<__bitset> const_reference;
166 typedef __bit_iterator<__bitset, false> iterator;
167 typedef __bit_iterator<__bitset, true> const_iterator;
168
Evgeniy Stepanov906c8722015-11-07 01:22:13169 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanteeac9fc2012-07-07 17:04:52170 _LIBCPP_CONSTEXPR __bitset() _NOEXCEPT;
Evgeniy Stepanov906c8722015-11-07 01:22:13171 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanteeac9fc2012-07-07 17:04:52172 explicit _LIBCPP_CONSTEXPR __bitset(unsigned long long __v) _NOEXCEPT;
Howard Hinnant3e519522010-05-11 19:42:16173
Howard Hinnantd368a842011-05-27 20:52:28174 _LIBCPP_INLINE_VISIBILITY reference __make_ref(size_t __pos) _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16175 {return reference(__first_ + __pos / __bits_per_word, __storage_type(1) << __pos % __bits_per_word);}
Howard Hinnanteeac9fc2012-07-07 17:04:52176 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR const_reference __make_ref(size_t __pos) const _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16177 {return const_reference(__first_ + __pos / __bits_per_word, __storage_type(1) << __pos % __bits_per_word);}
Howard Hinnantd368a842011-05-27 20:52:28178 _LIBCPP_INLINE_VISIBILITY iterator __make_iter(size_t __pos) _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16179 {return iterator(__first_ + __pos / __bits_per_word, __pos % __bits_per_word);}
Howard Hinnantd368a842011-05-27 20:52:28180 _LIBCPP_INLINE_VISIBILITY const_iterator __make_iter(size_t __pos) const _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16181 {return const_iterator(__first_ + __pos / __bits_per_word, __pos % __bits_per_word);}
182
Evgeniy Stepanov906c8722015-11-07 01:22:13183 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd368a842011-05-27 20:52:28184 void operator&=(const __bitset& __v) _NOEXCEPT;
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;
Howard Hinnant3e519522010-05-11 19:42:16189
Howard Hinnantd368a842011-05-27 20:52:28190 void flip() _NOEXCEPT;
Howard Hinnant3e519522010-05-11 19:42:16191 _LIBCPP_INLINE_VISIBILITY unsigned long to_ulong() const
192 {return to_ulong(integral_constant<bool, _Size < sizeof(unsigned long) * CHAR_BIT>());}
193 _LIBCPP_INLINE_VISIBILITY unsigned long long to_ullong() const
194 {return to_ullong(integral_constant<bool, _Size < sizeof(unsigned long long) * CHAR_BIT>());}
195
Howard Hinnantd368a842011-05-27 20:52:28196 bool all() const _NOEXCEPT;
197 bool any() const _NOEXCEPT;
Evgeniy Stepanov906c8722015-11-07 01:22:13198 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd368a842011-05-27 20:52:28199 size_t __hash_code() const _NOEXCEPT;
Howard Hinnant3e519522010-05-11 19:42:16200private:
Eric Fiselier046492b2017-04-19 01:34:08201#ifdef _LIBCPP_CXX03_LANG
Howard Hinnantd368a842011-05-27 20:52:28202 void __init(unsigned long long __v, false_type) _NOEXCEPT;
Evgeniy Stepanov02b8e942015-12-09 22:32:36203 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd368a842011-05-27 20:52:28204 void __init(unsigned long long __v, true_type) _NOEXCEPT;
Eric Fiselier046492b2017-04-19 01:34:08205#endif // _LIBCPP_CXX03_LANG
Howard Hinnant3e519522010-05-11 19:42:16206 unsigned long to_ulong(false_type) const;
Evgeniy Stepanov906c8722015-11-07 01:22:13207 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16208 unsigned long to_ulong(true_type) const;
209 unsigned long long to_ullong(false_type) const;
Evgeniy Stepanov906c8722015-11-07 01:22:13210 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16211 unsigned long long to_ullong(true_type) const;
Evgeniy Stepanov906c8722015-11-07 01:22:13212 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16213 unsigned long long to_ullong(true_type, false_type) const;
214 unsigned long long to_ullong(true_type, true_type) const;
215};
216
217template <size_t _N_words, size_t _Size>
Evgeniy Stepanov906c8722015-11-07 01:22:13218inline
Howard Hinnanteeac9fc2012-07-07 17:04:52219_LIBCPP_CONSTEXPR
Howard Hinnantd368a842011-05-27 20:52:28220__bitset<_N_words, _Size>::__bitset() _NOEXCEPT
Eric Fiselier046492b2017-04-19 01:34:08221#ifndef _LIBCPP_CXX03_LANG
Howard Hinnanteeac9fc2012-07-07 17:04:52222 : __first_{0}
223#endif
Howard Hinnant3e519522010-05-11 19:42:16224{
Eric Fiselier046492b2017-04-19 01:34:08225#ifdef _LIBCPP_CXX03_LANG
Howard Hinnantce48a112011-06-30 21:18:19226 _VSTD::fill_n(__first_, _N_words, __storage_type(0));
Howard Hinnanteeac9fc2012-07-07 17:04:52227#endif
Howard Hinnant3e519522010-05-11 19:42:16228}
229
Eric Fiselier046492b2017-04-19 01:34:08230#ifdef _LIBCPP_CXX03_LANG
Howard Hinnanteeac9fc2012-07-07 17:04:52231
Howard Hinnant3e519522010-05-11 19:42:16232template <size_t _N_words, size_t _Size>
233void
Howard Hinnantc2063662011-12-01 20:21:04234__bitset<_N_words, _Size>::__init(unsigned long long __v, false_type) _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16235{
236 __storage_type __t[sizeof(unsigned long long) / sizeof(__storage_type)];
Marshall Clowea44ee22017-11-27 22:27:22237 size_t __sz = _Size;
238 for (size_t __i = 0; __i < sizeof(__t)/sizeof(__t[0]); ++__i, __v >>= __bits_per_word, __sz -= __bits_per_word )
239 if ( __sz < __bits_per_word)
Louis Dionne7c3492b002018-08-03 22:36:53240 __t[__i] = static_cast<__storage_type>(__v) & ( 1ULL << __sz ) - 1;
Marshall Clowea44ee22017-11-27 22:27:22241 else
Louis Dionne7c3492b002018-08-03 22:36:53242 __t[__i] = static_cast<__storage_type>(__v);
Marshall Clowea44ee22017-11-27 22:27:22243
Howard Hinnantce48a112011-06-30 21:18:19244 _VSTD::copy(__t, __t + sizeof(__t)/sizeof(__t[0]), __first_);
245 _VSTD::fill(__first_ + sizeof(__t)/sizeof(__t[0]), __first_ + sizeof(__first_)/sizeof(__first_[0]),
Howard Hinnant3e519522010-05-11 19:42:16246 __storage_type(0));
247}
248
249template <size_t _N_words, size_t _Size>
250inline _LIBCPP_INLINE_VISIBILITY
251void
Howard Hinnantc2063662011-12-01 20:21:04252__bitset<_N_words, _Size>::__init(unsigned long long __v, true_type) _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16253{
254 __first_[0] = __v;
Marshall Clowea44ee22017-11-27 22:27:22255 if (_Size < __bits_per_word)
Louis Dionne7c3492b002018-08-03 22:36:53256 __first_[0] &= ( 1ULL << _Size ) - 1;
Marshall Clowea44ee22017-11-27 22:27:22257
Howard Hinnantce48a112011-06-30 21:18:19258 _VSTD::fill(__first_ + 1, __first_ + sizeof(__first_)/sizeof(__first_[0]), __storage_type(0));
Howard Hinnant3e519522010-05-11 19:42:16259}
260
Eric Fiselier046492b2017-04-19 01:34:08261#endif // _LIBCPP_CXX03_LANG
Howard Hinnanteeac9fc2012-07-07 17:04:52262
Howard Hinnant3e519522010-05-11 19:42:16263template <size_t _N_words, size_t _Size>
Evgeniy Stepanov906c8722015-11-07 01:22:13264inline
Howard Hinnanteeac9fc2012-07-07 17:04:52265_LIBCPP_CONSTEXPR
Howard Hinnantd368a842011-05-27 20:52:28266__bitset<_N_words, _Size>::__bitset(unsigned long long __v) _NOEXCEPT
Eric Fiselier046492b2017-04-19 01:34:08267#ifndef _LIBCPP_CXX03_LANG
Nico Weber1d1b46c2014-06-04 15:46:56268#if __SIZEOF_SIZE_T__ == 8
Howard Hinnanteeac9fc2012-07-07 17:04:52269 : __first_{__v}
Nico Weber1d1b46c2014-06-04 15:46:56270#elif __SIZEOF_SIZE_T__ == 4
Louis Dionne7c3492b002018-08-03 22:36:53271 : __first_{static_cast<__storage_type>(__v),
272 _Size >= 2 * __bits_per_word ? static_cast<__storage_type>(__v >> __bits_per_word)
273 : static_cast<__storage_type>((__v >> __bits_per_word) & (__storage_type(1) << (_Size - __bits_per_word)) - 1)}
Howard Hinnant3fb6c6e2013-03-06 18:16:12274#else
Howard Hinnant53b9ee02013-03-06 17:30:26275#error This constructor has not been ported to this platform
276#endif
Howard Hinnanteeac9fc2012-07-07 17:04:52277#endif
Howard Hinnant3e519522010-05-11 19:42:16278{
Eric Fiselier046492b2017-04-19 01:34:08279#ifdef _LIBCPP_CXX03_LANG
Howard Hinnant3e519522010-05-11 19:42:16280 __init(__v, integral_constant<bool, sizeof(unsigned long long) == sizeof(__storage_type)>());
Howard Hinnanteeac9fc2012-07-07 17:04:52281#endif
Howard Hinnant3e519522010-05-11 19:42:16282}
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>
Evgeniy Stepanov906c8722015-11-07 01:22:13303inline
Howard Hinnant3e519522010-05-11 19:42:16304void
Howard Hinnantd368a842011-05-27 20:52:28305__bitset<_N_words, _Size>::operator^=(const __bitset& __v) _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16306{
307 for (size_type __i = 0; __i < _N_words; ++__i)
308 __first_[__i] ^= __v.__first_[__i];
309}
310
311template <size_t _N_words, size_t _Size>
312void
Howard Hinnantd368a842011-05-27 20:52:28313__bitset<_N_words, _Size>::flip() _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16314{
315 // do middle whole words
316 size_type __n = _Size;
317 __storage_pointer __p = __first_;
318 for (; __n >= __bits_per_word; ++__p, __n -= __bits_per_word)
319 *__p = ~*__p;
320 // do last partial word
321 if (__n > 0)
322 {
323 __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
324 __storage_type __b = *__p & __m;
325 *__p &= ~__m;
326 *__p |= ~__b & __m;
327 }
328}
329
330template <size_t _N_words, size_t _Size>
331unsigned long
332__bitset<_N_words, _Size>::to_ulong(false_type) const
333{
334 const_iterator __e = __make_iter(_Size);
Howard Hinnantce48a112011-06-30 21:18:19335 const_iterator __i = _VSTD::find(__make_iter(sizeof(unsigned long) * CHAR_BIT), __e, true);
Howard Hinnant3e519522010-05-11 19:42:16336 if (__i != __e)
Marshall Clowd437fa52016-08-25 15:09:01337 __throw_overflow_error("bitset to_ulong overflow error");
338
Howard Hinnant3e519522010-05-11 19:42:16339 return __first_[0];
340}
341
342template <size_t _N_words, size_t _Size>
Evgeniy Stepanov906c8722015-11-07 01:22:13343inline
Howard Hinnant3e519522010-05-11 19:42:16344unsigned long
345__bitset<_N_words, _Size>::to_ulong(true_type) const
346{
347 return __first_[0];
348}
349
350template <size_t _N_words, size_t _Size>
351unsigned long long
352__bitset<_N_words, _Size>::to_ullong(false_type) const
353{
354 const_iterator __e = __make_iter(_Size);
Howard Hinnantce48a112011-06-30 21:18:19355 const_iterator __i = _VSTD::find(__make_iter(sizeof(unsigned long long) * CHAR_BIT), __e, true);
Howard Hinnant3e519522010-05-11 19:42:16356 if (__i != __e)
Marshall Clowd437fa52016-08-25 15:09:01357 __throw_overflow_error("bitset to_ullong overflow error");
358
Howard Hinnant3e519522010-05-11 19:42:16359 return to_ullong(true_type());
360}
361
362template <size_t _N_words, size_t _Size>
Evgeniy Stepanov906c8722015-11-07 01:22:13363inline
Howard Hinnant3e519522010-05-11 19:42:16364unsigned long long
365__bitset<_N_words, _Size>::to_ullong(true_type) const
366{
367 return to_ullong(true_type(), integral_constant<bool, sizeof(__storage_type) < sizeof(unsigned long long)>());
368}
369
370template <size_t _N_words, size_t _Size>
Evgeniy Stepanov906c8722015-11-07 01:22:13371inline
Howard Hinnant3e519522010-05-11 19:42:16372unsigned long long
373__bitset<_N_words, _Size>::to_ullong(true_type, false_type) const
374{
375 return __first_[0];
376}
377
378template <size_t _N_words, size_t _Size>
379unsigned long long
380__bitset<_N_words, _Size>::to_ullong(true_type, true_type) const
381{
382 unsigned long long __r = __first_[0];
383 for (std::size_t __i = 1; __i < sizeof(unsigned long long) / sizeof(__storage_type); ++__i)
384 __r |= static_cast<unsigned long long>(__first_[__i]) << (sizeof(__storage_type) * CHAR_BIT);
385 return __r;
386}
387
388template <size_t _N_words, size_t _Size>
389bool
Howard Hinnantd368a842011-05-27 20:52:28390__bitset<_N_words, _Size>::all() const _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16391{
392 // do middle whole words
393 size_type __n = _Size;
394 __const_storage_pointer __p = __first_;
395 for (; __n >= __bits_per_word; ++__p, __n -= __bits_per_word)
396 if (~*__p)
397 return false;
398 // do last partial word
399 if (__n > 0)
400 {
401 __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
402 if (~*__p & __m)
403 return false;
404 }
405 return true;
406}
407
408template <size_t _N_words, size_t _Size>
409bool
Howard Hinnantd368a842011-05-27 20:52:28410__bitset<_N_words, _Size>::any() const _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16411{
412 // do middle whole words
413 size_type __n = _Size;
414 __const_storage_pointer __p = __first_;
415 for (; __n >= __bits_per_word; ++__p, __n -= __bits_per_word)
416 if (*__p)
417 return true;
418 // do last partial word
419 if (__n > 0)
420 {
421 __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
422 if (*__p & __m)
423 return true;
424 }
425 return false;
426}
427
428template <size_t _N_words, size_t _Size>
Evgeniy Stepanov906c8722015-11-07 01:22:13429inline
Howard Hinnant3e519522010-05-11 19:42:16430size_t
Howard Hinnantd368a842011-05-27 20:52:28431__bitset<_N_words, _Size>::__hash_code() const _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16432{
433 size_t __h = 0;
434 for (size_type __i = 0; __i < _N_words; ++__i)
435 __h ^= __first_[__i];
436 return __h;
437}
438
439template <size_t _Size>
440class __bitset<1, _Size>
441{
442public:
443 typedef ptrdiff_t difference_type;
444 typedef size_t size_type;
Howard Hinnant0ae9efe2012-05-07 16:50:38445 typedef size_type __storage_type;
Howard Hinnant3e519522010-05-11 19:42:16446protected:
447 typedef __bitset __self;
Howard Hinnant3e519522010-05-11 19:42:16448 typedef __storage_type* __storage_pointer;
449 typedef const __storage_type* __const_storage_pointer;
450 static const unsigned __bits_per_word = static_cast<unsigned>(sizeof(__storage_type) * CHAR_BIT);
451
452 friend class __bit_reference<__bitset>;
453 friend class __bit_const_reference<__bitset>;
454 friend class __bit_iterator<__bitset, false>;
455 friend class __bit_iterator<__bitset, true>;
Howard Hinnant7ee27132012-08-17 17:10:18456 friend struct __bit_array<__bitset>;
Howard Hinnant3e519522010-05-11 19:42:16457
458 __storage_type __first_;
459
460 typedef __bit_reference<__bitset> reference;
461 typedef __bit_const_reference<__bitset> const_reference;
462 typedef __bit_iterator<__bitset, false> iterator;
463 typedef __bit_iterator<__bitset, true> const_iterator;
464
Evgeniy Stepanov906c8722015-11-07 01:22:13465 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanteeac9fc2012-07-07 17:04:52466 _LIBCPP_CONSTEXPR __bitset() _NOEXCEPT;
Evgeniy Stepanov906c8722015-11-07 01:22:13467 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanteeac9fc2012-07-07 17:04:52468 explicit _LIBCPP_CONSTEXPR __bitset(unsigned long long __v) _NOEXCEPT;
Howard Hinnant3e519522010-05-11 19:42:16469
Howard Hinnantd368a842011-05-27 20:52:28470 _LIBCPP_INLINE_VISIBILITY reference __make_ref(size_t __pos) _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16471 {return reference(&__first_, __storage_type(1) << __pos);}
Howard Hinnanteeac9fc2012-07-07 17:04:52472 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR const_reference __make_ref(size_t __pos) const _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16473 {return const_reference(&__first_, __storage_type(1) << __pos);}
Howard Hinnantd368a842011-05-27 20:52:28474 _LIBCPP_INLINE_VISIBILITY iterator __make_iter(size_t __pos) _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16475 {return iterator(&__first_ + __pos / __bits_per_word, __pos % __bits_per_word);}
Howard Hinnantd368a842011-05-27 20:52:28476 _LIBCPP_INLINE_VISIBILITY const_iterator __make_iter(size_t __pos) const _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16477 {return const_iterator(&__first_ + __pos / __bits_per_word, __pos % __bits_per_word);}
478
Evgeniy Stepanov906c8722015-11-07 01:22:13479 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd368a842011-05-27 20:52:28480 void operator&=(const __bitset& __v) _NOEXCEPT;
Evgeniy Stepanov906c8722015-11-07 01:22:13481 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd368a842011-05-27 20:52:28482 void operator|=(const __bitset& __v) _NOEXCEPT;
Evgeniy Stepanov906c8722015-11-07 01:22:13483 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd368a842011-05-27 20:52:28484 void operator^=(const __bitset& __v) _NOEXCEPT;
Howard Hinnant3e519522010-05-11 19:42:16485
Evgeniy Stepanov906c8722015-11-07 01:22:13486 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd368a842011-05-27 20:52:28487 void flip() _NOEXCEPT;
Howard Hinnant3e519522010-05-11 19:42:16488
Evgeniy Stepanov906c8722015-11-07 01:22:13489 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16490 unsigned long to_ulong() const;
Evgeniy Stepanov906c8722015-11-07 01:22:13491 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16492 unsigned long long to_ullong() const;
493
Evgeniy Stepanov906c8722015-11-07 01:22:13494 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd368a842011-05-27 20:52:28495 bool all() const _NOEXCEPT;
Evgeniy Stepanov906c8722015-11-07 01:22:13496 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd368a842011-05-27 20:52:28497 bool any() const _NOEXCEPT;
Howard Hinnant3e519522010-05-11 19:42:16498
Evgeniy Stepanov906c8722015-11-07 01:22:13499 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd368a842011-05-27 20:52:28500 size_t __hash_code() const _NOEXCEPT;
Howard Hinnant3e519522010-05-11 19:42:16501};
502
503template <size_t _Size>
Evgeniy Stepanov906c8722015-11-07 01:22:13504inline
Howard Hinnanteeac9fc2012-07-07 17:04:52505_LIBCPP_CONSTEXPR
Howard Hinnantd368a842011-05-27 20:52:28506__bitset<1, _Size>::__bitset() _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16507 : __first_(0)
508{
509}
510
511template <size_t _Size>
Evgeniy Stepanov906c8722015-11-07 01:22:13512inline
Howard Hinnanteeac9fc2012-07-07 17:04:52513_LIBCPP_CONSTEXPR
Howard Hinnantd368a842011-05-27 20:52:28514__bitset<1, _Size>::__bitset(unsigned long long __v) _NOEXCEPT
Marshall Clow14d7aac2017-11-27 19:03:30515 : __first_(
516 _Size == __bits_per_word ? static_cast<__storage_type>(__v)
517 : static_cast<__storage_type>(__v) & ((__storage_type(1) << _Size) - 1)
518 )
Howard Hinnant3e519522010-05-11 19:42:16519{
520}
521
522template <size_t _Size>
Evgeniy Stepanov906c8722015-11-07 01:22:13523inline
Howard Hinnant3e519522010-05-11 19:42:16524void
Howard Hinnantd368a842011-05-27 20:52:28525__bitset<1, _Size>::operator&=(const __bitset& __v) _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16526{
527 __first_ &= __v.__first_;
528}
529
530template <size_t _Size>
Evgeniy Stepanov906c8722015-11-07 01:22:13531inline
Howard Hinnant3e519522010-05-11 19:42:16532void
Howard Hinnantd368a842011-05-27 20:52:28533__bitset<1, _Size>::operator|=(const __bitset& __v) _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16534{
535 __first_ |= __v.__first_;
536}
537
538template <size_t _Size>
Evgeniy Stepanov906c8722015-11-07 01:22:13539inline
Howard Hinnant3e519522010-05-11 19:42:16540void
Howard Hinnantd368a842011-05-27 20:52:28541__bitset<1, _Size>::operator^=(const __bitset& __v) _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16542{
543 __first_ ^= __v.__first_;
544}
545
546template <size_t _Size>
Evgeniy Stepanov906c8722015-11-07 01:22:13547inline
Howard Hinnant3e519522010-05-11 19:42:16548void
Howard Hinnantd368a842011-05-27 20:52:28549__bitset<1, _Size>::flip() _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16550{
551 __storage_type __m = ~__storage_type(0) >> (__bits_per_word - _Size);
552 __first_ = ~__first_;
553 __first_ &= __m;
554}
555
556template <size_t _Size>
Evgeniy Stepanov906c8722015-11-07 01:22:13557inline
Howard Hinnant3e519522010-05-11 19:42:16558unsigned long
559__bitset<1, _Size>::to_ulong() const
560{
561 return __first_;
562}
563
564template <size_t _Size>
Evgeniy Stepanov906c8722015-11-07 01:22:13565inline
Howard Hinnant3e519522010-05-11 19:42:16566unsigned long long
567__bitset<1, _Size>::to_ullong() const
568{
569 return __first_;
570}
571
572template <size_t _Size>
Evgeniy Stepanov906c8722015-11-07 01:22:13573inline
Howard Hinnant3e519522010-05-11 19:42:16574bool
Howard Hinnantd368a842011-05-27 20:52:28575__bitset<1, _Size>::all() const _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16576{
577 __storage_type __m = ~__storage_type(0) >> (__bits_per_word - _Size);
578 return !(~__first_ & __m);
579}
580
581template <size_t _Size>
Evgeniy Stepanov906c8722015-11-07 01:22:13582inline
Howard Hinnant3e519522010-05-11 19:42:16583bool
Howard Hinnantd368a842011-05-27 20:52:28584__bitset<1, _Size>::any() const _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16585{
586 __storage_type __m = ~__storage_type(0) >> (__bits_per_word - _Size);
587 return __first_ & __m;
588}
589
590template <size_t _Size>
Evgeniy Stepanov906c8722015-11-07 01:22:13591inline
Howard Hinnant3e519522010-05-11 19:42:16592size_t
Howard Hinnantd368a842011-05-27 20:52:28593__bitset<1, _Size>::__hash_code() const _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16594{
595 return __first_;
596}
597
598template <>
599class __bitset<0, 0>
600{
601public:
602 typedef ptrdiff_t difference_type;
603 typedef size_t size_type;
Howard Hinnant0ae9efe2012-05-07 16:50:38604 typedef size_type __storage_type;
Howard Hinnant3e519522010-05-11 19:42:16605protected:
606 typedef __bitset __self;
Howard Hinnant3e519522010-05-11 19:42:16607 typedef __storage_type* __storage_pointer;
608 typedef const __storage_type* __const_storage_pointer;
609 static const unsigned __bits_per_word = static_cast<unsigned>(sizeof(__storage_type) * CHAR_BIT);
610
611 friend class __bit_reference<__bitset>;
612 friend class __bit_const_reference<__bitset>;
613 friend class __bit_iterator<__bitset, false>;
614 friend class __bit_iterator<__bitset, true>;
Howard Hinnantc2063662011-12-01 20:21:04615 friend struct __bit_array<__bitset>;
Howard Hinnant3e519522010-05-11 19:42:16616
617 typedef __bit_reference<__bitset> reference;
618 typedef __bit_const_reference<__bitset> const_reference;
619 typedef __bit_iterator<__bitset, false> iterator;
620 typedef __bit_iterator<__bitset, true> const_iterator;
621
Evgeniy Stepanov906c8722015-11-07 01:22:13622 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanteeac9fc2012-07-07 17:04:52623 _LIBCPP_CONSTEXPR __bitset() _NOEXCEPT;
Evgeniy Stepanov906c8722015-11-07 01:22:13624 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanteeac9fc2012-07-07 17:04:52625 explicit _LIBCPP_CONSTEXPR __bitset(unsigned long long) _NOEXCEPT;
Howard Hinnant3e519522010-05-11 19:42:16626
Howard Hinnantd368a842011-05-27 20:52:28627 _LIBCPP_INLINE_VISIBILITY reference __make_ref(size_t) _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16628 {return reference(0, 1);}
Howard Hinnanteeac9fc2012-07-07 17:04:52629 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR const_reference __make_ref(size_t) const _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16630 {return const_reference(0, 1);}
Howard Hinnantc2063662011-12-01 20:21:04631 _LIBCPP_INLINE_VISIBILITY iterator __make_iter(size_t) _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16632 {return iterator(0, 0);}
Howard Hinnantc2063662011-12-01 20:21:04633 _LIBCPP_INLINE_VISIBILITY const_iterator __make_iter(size_t) const _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16634 {return const_iterator(0, 0);}
635
Howard Hinnantd368a842011-05-27 20:52:28636 _LIBCPP_INLINE_VISIBILITY void operator&=(const __bitset&) _NOEXCEPT {}
637 _LIBCPP_INLINE_VISIBILITY void operator|=(const __bitset&) _NOEXCEPT {}
638 _LIBCPP_INLINE_VISIBILITY void operator^=(const __bitset&) _NOEXCEPT {}
Howard Hinnant3e519522010-05-11 19:42:16639
Howard Hinnantd368a842011-05-27 20:52:28640 _LIBCPP_INLINE_VISIBILITY void flip() _NOEXCEPT {}
Howard Hinnant3e519522010-05-11 19:42:16641
642 _LIBCPP_INLINE_VISIBILITY unsigned long to_ulong() const {return 0;}
643 _LIBCPP_INLINE_VISIBILITY unsigned long long to_ullong() const {return 0;}
644
Howard Hinnantd368a842011-05-27 20:52:28645 _LIBCPP_INLINE_VISIBILITY bool all() const _NOEXCEPT {return true;}
646 _LIBCPP_INLINE_VISIBILITY bool any() const _NOEXCEPT {return false;}
Howard Hinnant3e519522010-05-11 19:42:16647
Howard Hinnantd368a842011-05-27 20:52:28648 _LIBCPP_INLINE_VISIBILITY size_t __hash_code() const _NOEXCEPT {return 0;}
Howard Hinnant3e519522010-05-11 19:42:16649};
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() _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16654{
655}
656
Evgeniy Stepanov906c8722015-11-07 01:22:13657inline
Howard Hinnanteeac9fc2012-07-07 17:04:52658_LIBCPP_CONSTEXPR
Howard Hinnantd368a842011-05-27 20:52:28659__bitset<0, 0>::__bitset(unsigned long long) _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16660{
661}
662
Eric Fiseliere2f2d1ed2017-01-04 23:56:00663template <size_t _Size> class _LIBCPP_TEMPLATE_VIS bitset;
Eric Fiselier89dd1dd2016-04-21 22:54:21664template <size_t _Size> struct hash<bitset<_Size> >;
Howard Hinnant3e519522010-05-11 19:42:16665
666template <size_t _Size>
Eric Fiseliere2f2d1ed2017-01-04 23:56:00667class _LIBCPP_TEMPLATE_VIS bitset
Howard Hinnant3e519522010-05-11 19:42:16668 : private __bitset<_Size == 0 ? 0 : (_Size - 1) / (sizeof(size_t) * CHAR_BIT) + 1, _Size>
669{
Howard Hinnant53b9ee02013-03-06 17:30:26670public:
Howard Hinnant3e519522010-05-11 19:42:16671 static const unsigned __n_words = _Size == 0 ? 0 : (_Size - 1) / (sizeof(size_t) * CHAR_BIT) + 1;
672 typedef __bitset<__n_words, _Size> base;
673
Howard Hinnantb3371f62010-08-22 00:02:43674public:
Howard Hinnant3e519522010-05-11 19:42:16675 typedef typename base::reference reference;
676 typedef typename base::const_reference const_reference;
677
Howard Hinnantb3371f62010-08-22 00:02:43678 // 23.3.5.1 constructors:
Howard Hinnanteeac9fc2012-07-07 17:04:52679 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR bitset() _NOEXCEPT {}
680 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
681 bitset(unsigned long long __v) _NOEXCEPT : base(__v) {}
Eric Fiselierd1523f72019-07-01 19:59:34682 template<class _CharT, class = _EnableIf<_IsCharLikeType<_CharT>::value> >
Howard Hinnantd09f7112010-11-17 21:53:14683 explicit bitset(const _CharT* __str,
684 typename basic_string<_CharT>::size_type __n = basic_string<_CharT>::npos,
685 _CharT __zero = _CharT('0'), _CharT __one = _CharT('1'));
Howard Hinnantb3371f62010-08-22 00:02:43686 template<class _CharT, class _Traits, class _Allocator>
687 explicit bitset(const basic_string<_CharT,_Traits,_Allocator>& __str,
688 typename basic_string<_CharT,_Traits,_Allocator>::size_type __pos = 0,
689 typename basic_string<_CharT,_Traits,_Allocator>::size_type __n =
Howard Hinnant3e519522010-05-11 19:42:16690 (basic_string<_CharT,_Traits,_Allocator>::npos),
Howard Hinnantb3371f62010-08-22 00:02:43691 _CharT __zero = _CharT('0'), _CharT __one = _CharT('1'));
Howard Hinnant3e519522010-05-11 19:42:16692
Howard Hinnantb3371f62010-08-22 00:02:43693 // 23.3.5.2 bitset operations:
Evgeniy Stepanov906c8722015-11-07 01:22:13694 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd368a842011-05-27 20:52:28695 bitset& operator&=(const bitset& __rhs) _NOEXCEPT;
Evgeniy Stepanov906c8722015-11-07 01:22:13696 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd368a842011-05-27 20:52:28697 bitset& operator|=(const bitset& __rhs) _NOEXCEPT;
Evgeniy Stepanov906c8722015-11-07 01:22:13698 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd368a842011-05-27 20:52:28699 bitset& operator^=(const bitset& __rhs) _NOEXCEPT;
700 bitset& operator<<=(size_t __pos) _NOEXCEPT;
701 bitset& operator>>=(size_t __pos) _NOEXCEPT;
Evgeniy Stepanov906c8722015-11-07 01:22:13702 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd368a842011-05-27 20:52:28703 bitset& set() _NOEXCEPT;
Howard Hinnantb3371f62010-08-22 00:02:43704 bitset& set(size_t __pos, bool __val = true);
Evgeniy Stepanov906c8722015-11-07 01:22:13705 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd368a842011-05-27 20:52:28706 bitset& reset() _NOEXCEPT;
Howard Hinnantb3371f62010-08-22 00:02:43707 bitset& reset(size_t __pos);
Evgeniy Stepanov906c8722015-11-07 01:22:13708 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd368a842011-05-27 20:52:28709 bitset operator~() const _NOEXCEPT;
Evgeniy Stepanov906c8722015-11-07 01:22:13710 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd368a842011-05-27 20:52:28711 bitset& flip() _NOEXCEPT;
Howard Hinnantb3371f62010-08-22 00:02:43712 bitset& flip(size_t __pos);
Howard Hinnant3e519522010-05-11 19:42:16713
Howard Hinnantb3371f62010-08-22 00:02:43714 // element access:
Howard Hinnanteeac9fc2012-07-07 17:04:52715 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
716 const_reference operator[](size_t __p) const {return base::__make_ref(__p);}
Howard Hinnant3e519522010-05-11 19:42:16717 _LIBCPP_INLINE_VISIBILITY reference operator[](size_t __p) {return base::__make_ref(__p);}
Evgeniy Stepanov906c8722015-11-07 01:22:13718 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb3371f62010-08-22 00:02:43719 unsigned long to_ulong() const;
Evgeniy Stepanov906c8722015-11-07 01:22:13720 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb3371f62010-08-22 00:02:43721 unsigned long long to_ullong() const;
722 template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant3e519522010-05-11 19:42:16723 basic_string<_CharT, _Traits, _Allocator> to_string(_CharT __zero = _CharT('0'),
Howard Hinnantb3371f62010-08-22 00:02:43724 _CharT __one = _CharT('1')) const;
725 template <class _CharT, class _Traits>
Evgeniy Stepanov906c8722015-11-07 01:22:13726 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16727 basic_string<_CharT, _Traits, allocator<_CharT> > to_string(_CharT __zero = _CharT('0'),
Howard Hinnantb3371f62010-08-22 00:02:43728 _CharT __one = _CharT('1')) const;
729 template <class _CharT>
Evgeniy Stepanov906c8722015-11-07 01:22:13730 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16731 basic_string<_CharT, char_traits<_CharT>, allocator<_CharT> > to_string(_CharT __zero = _CharT('0'),
Howard Hinnantb3371f62010-08-22 00:02:43732 _CharT __one = _CharT('1')) const;
Evgeniy Stepanov906c8722015-11-07 01:22:13733 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16734 basic_string<char, char_traits<char>, allocator<char> > to_string(char __zero = '0',
Howard Hinnantb3371f62010-08-22 00:02:43735 char __one = '1') const;
Evgeniy Stepanov906c8722015-11-07 01:22:13736 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd368a842011-05-27 20:52:28737 size_t count() const _NOEXCEPT;
Howard Hinnanteeac9fc2012-07-07 17:04:52738 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR size_t size() const _NOEXCEPT {return _Size;}
Evgeniy Stepanov906c8722015-11-07 01:22:13739 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd368a842011-05-27 20:52:28740 bool operator==(const bitset& __rhs) const _NOEXCEPT;
Evgeniy Stepanov906c8722015-11-07 01:22:13741 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd368a842011-05-27 20:52:28742 bool operator!=(const bitset& __rhs) const _NOEXCEPT;
Howard Hinnantb3371f62010-08-22 00:02:43743 bool test(size_t __pos) const;
Evgeniy Stepanov906c8722015-11-07 01:22:13744 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd368a842011-05-27 20:52:28745 bool all() const _NOEXCEPT;
Evgeniy Stepanov906c8722015-11-07 01:22:13746 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd368a842011-05-27 20:52:28747 bool any() const _NOEXCEPT;
748 _LIBCPP_INLINE_VISIBILITY bool none() const _NOEXCEPT {return !any();}
Evgeniy Stepanov906c8722015-11-07 01:22:13749 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd368a842011-05-27 20:52:28750 bitset operator<<(size_t __pos) const _NOEXCEPT;
Evgeniy Stepanov906c8722015-11-07 01:22:13751 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd368a842011-05-27 20:52:28752 bitset operator>>(size_t __pos) const _NOEXCEPT;
Howard Hinnant3e519522010-05-11 19:42:16753
754private:
755
Howard Hinnantfb100022010-09-21 21:28:23756 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd368a842011-05-27 20:52:28757 size_t __hash_code() const _NOEXCEPT {return base::__hash_code();}
Howard Hinnant3e519522010-05-11 19:42:16758
759 friend struct hash<bitset>;
760};
761
762template <size_t _Size>
Eric Fiselierd1523f72019-07-01 19:59:34763template<class _CharT, class>
Howard Hinnantd09f7112010-11-17 21:53:14764bitset<_Size>::bitset(const _CharT* __str,
765 typename basic_string<_CharT>::size_type __n,
766 _CharT __zero, _CharT __one)
Howard Hinnant3e519522010-05-11 19:42:16767{
Howard Hinnantce48a112011-06-30 21:18:19768 size_t __rlen = _VSTD::min(__n, char_traits<_CharT>::length(__str));
Howard Hinnant3e519522010-05-11 19:42:16769 for (size_t __i = 0; __i < __rlen; ++__i)
Howard Hinnantd09f7112010-11-17 21:53:14770 if (__str[__i] != __zero && __str[__i] != __one)
Marshall Clowd437fa52016-08-25 15:09:01771 __throw_invalid_argument("bitset string ctor has invalid argument");
772
Howard Hinnantc003db12011-11-29 18:15:50773 size_t _Mp = _VSTD::min(__rlen, _Size);
Howard Hinnant3e519522010-05-11 19:42:16774 size_t __i = 0;
Howard Hinnantc003db12011-11-29 18:15:50775 for (; __i < _Mp; ++__i)
Howard Hinnant3e519522010-05-11 19:42:16776 {
Howard Hinnantc003db12011-11-29 18:15:50777 _CharT __c = __str[_Mp - 1 - __i];
Howard Hinnantd09f7112010-11-17 21:53:14778 if (__c == __zero)
Howard Hinnant3e519522010-05-11 19:42:16779 (*this)[__i] = false;
Howard Hinnantd09f7112010-11-17 21:53:14780 else
Howard Hinnant3e519522010-05-11 19:42:16781 (*this)[__i] = true;
Howard Hinnant3e519522010-05-11 19:42:16782 }
Howard Hinnantce48a112011-06-30 21:18:19783 _VSTD::fill(base::__make_iter(__i), base::__make_iter(_Size), false);
Howard Hinnant3e519522010-05-11 19:42:16784}
785
786template <size_t _Size>
Howard Hinnantb3371f62010-08-22 00:02:43787template<class _CharT, class _Traits, class _Allocator>
788bitset<_Size>::bitset(const basic_string<_CharT,_Traits,_Allocator>& __str,
789 typename basic_string<_CharT,_Traits,_Allocator>::size_type __pos,
Howard Hinnant3e519522010-05-11 19:42:16790 typename basic_string<_CharT,_Traits,_Allocator>::size_type __n,
791 _CharT __zero, _CharT __one)
792{
793 if (__pos > __str.size())
Marshall Clowd437fa52016-08-25 15:09:01794 __throw_out_of_range("bitset string pos out of range");
795
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))
Marshall Clowd437fa52016-08-25 15:09:01799 __throw_invalid_argument("bitset string ctor has invalid argument");
800
Howard Hinnantc003db12011-11-29 18:15:50801 size_t _Mp = _VSTD::min(__rlen, _Size);
Howard Hinnant3e519522010-05-11 19:42:16802 size_t __i = 0;
Howard Hinnantc003db12011-11-29 18:15:50803 for (; __i < _Mp; ++__i)
Howard Hinnant3e519522010-05-11 19:42:16804 {
Howard Hinnantc003db12011-11-29 18:15:50805 _CharT __c = __str[__pos + _Mp - 1 - __i];
Howard Hinnant3e519522010-05-11 19:42:16806 if (_Traits::eq(__c, __zero))
807 (*this)[__i] = false;
808 else
809 (*this)[__i] = true;
810 }
Howard Hinnantce48a112011-06-30 21:18:19811 _VSTD::fill(base::__make_iter(__i), base::__make_iter(_Size), false);
Howard Hinnant3e519522010-05-11 19:42:16812}
813
814template <size_t _Size>
Evgeniy Stepanov906c8722015-11-07 01:22:13815inline
Howard Hinnant3e519522010-05-11 19:42:16816bitset<_Size>&
Howard Hinnantd368a842011-05-27 20:52:28817bitset<_Size>::operator&=(const bitset& __rhs) _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16818{
819 base::operator&=(__rhs);
820 return *this;
821}
822
823template <size_t _Size>
Evgeniy Stepanov906c8722015-11-07 01:22:13824inline
Howard Hinnant3e519522010-05-11 19:42:16825bitset<_Size>&
Howard Hinnantd368a842011-05-27 20:52:28826bitset<_Size>::operator|=(const bitset& __rhs) _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16827{
828 base::operator|=(__rhs);
829 return *this;
830}
831
832template <size_t _Size>
Evgeniy Stepanov906c8722015-11-07 01:22:13833inline
Howard Hinnant3e519522010-05-11 19:42:16834bitset<_Size>&
Howard Hinnantd368a842011-05-27 20:52:28835bitset<_Size>::operator^=(const bitset& __rhs) _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16836{
837 base::operator^=(__rhs);
838 return *this;
839}
840
841template <size_t _Size>
842bitset<_Size>&
Howard Hinnantd368a842011-05-27 20:52:28843bitset<_Size>::operator<<=(size_t __pos) _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16844{
Howard Hinnantce48a112011-06-30 21:18:19845 __pos = _VSTD::min(__pos, _Size);
846 _VSTD::copy_backward(base::__make_iter(0), base::__make_iter(_Size - __pos), base::__make_iter(_Size));
847 _VSTD::fill_n(base::__make_iter(0), __pos, false);
Howard Hinnant3e519522010-05-11 19:42:16848 return *this;
849}
850
851template <size_t _Size>
852bitset<_Size>&
Howard Hinnantd368a842011-05-27 20:52:28853bitset<_Size>::operator>>=(size_t __pos) _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16854{
Howard Hinnantce48a112011-06-30 21:18:19855 __pos = _VSTD::min(__pos, _Size);
856 _VSTD::copy(base::__make_iter(__pos), base::__make_iter(_Size), base::__make_iter(0));
857 _VSTD::fill_n(base::__make_iter(_Size - __pos), __pos, false);
Howard Hinnant3e519522010-05-11 19:42:16858 return *this;
859}
860
861template <size_t _Size>
Evgeniy Stepanov906c8722015-11-07 01:22:13862inline
Howard Hinnant3e519522010-05-11 19:42:16863bitset<_Size>&
Howard Hinnantd368a842011-05-27 20:52:28864bitset<_Size>::set() _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16865{
Howard Hinnantce48a112011-06-30 21:18:19866 _VSTD::fill_n(base::__make_iter(0), _Size, true);
Howard Hinnant3e519522010-05-11 19:42:16867 return *this;
868}
869
870template <size_t _Size>
871bitset<_Size>&
872bitset<_Size>::set(size_t __pos, bool __val)
873{
874 if (__pos >= _Size)
Marshall Clowd437fa52016-08-25 15:09:01875 __throw_out_of_range("bitset set argument out of range");
876
Howard Hinnant3e519522010-05-11 19:42:16877 (*this)[__pos] = __val;
878 return *this;
879}
880
881template <size_t _Size>
Evgeniy Stepanov906c8722015-11-07 01:22:13882inline
Howard Hinnant3e519522010-05-11 19:42:16883bitset<_Size>&
Howard Hinnantd368a842011-05-27 20:52:28884bitset<_Size>::reset() _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16885{
Howard Hinnantce48a112011-06-30 21:18:19886 _VSTD::fill_n(base::__make_iter(0), _Size, false);
Howard Hinnant3e519522010-05-11 19:42:16887 return *this;
888}
889
890template <size_t _Size>
891bitset<_Size>&
892bitset<_Size>::reset(size_t __pos)
893{
894 if (__pos >= _Size)
Marshall Clowd437fa52016-08-25 15:09:01895 __throw_out_of_range("bitset reset argument out of range");
896
Howard Hinnant3e519522010-05-11 19:42:16897 (*this)[__pos] = false;
898 return *this;
899}
900
901template <size_t _Size>
Evgeniy Stepanov906c8722015-11-07 01:22:13902inline
Howard Hinnant3e519522010-05-11 19:42:16903bitset<_Size>
Howard Hinnantd368a842011-05-27 20:52:28904bitset<_Size>::operator~() const _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16905{
906 bitset __x(*this);
907 __x.flip();
908 return __x;
909}
910
911template <size_t _Size>
Evgeniy Stepanov906c8722015-11-07 01:22:13912inline
Howard Hinnant3e519522010-05-11 19:42:16913bitset<_Size>&
Howard Hinnantd368a842011-05-27 20:52:28914bitset<_Size>::flip() _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16915{
916 base::flip();
917 return *this;
918}
919
920template <size_t _Size>
921bitset<_Size>&
922bitset<_Size>::flip(size_t __pos)
923{
924 if (__pos >= _Size)
Marshall Clowd437fa52016-08-25 15:09:01925 __throw_out_of_range("bitset flip argument out of range");
926
Howard Hinnant3e519522010-05-11 19:42:16927 reference r = base::__make_ref(__pos);
928 r = ~r;
929 return *this;
930}
931
932template <size_t _Size>
Evgeniy Stepanov906c8722015-11-07 01:22:13933inline
Howard Hinnant3e519522010-05-11 19:42:16934unsigned long
935bitset<_Size>::to_ulong() const
936{
937 return base::to_ulong();
938}
939
940template <size_t _Size>
Evgeniy Stepanov906c8722015-11-07 01:22:13941inline
Howard Hinnant3e519522010-05-11 19:42:16942unsigned long long
943bitset<_Size>::to_ullong() const
944{
945 return base::to_ullong();
946}
947
948template <size_t _Size>
Howard Hinnantb3371f62010-08-22 00:02:43949template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant3e519522010-05-11 19:42:16950basic_string<_CharT, _Traits, _Allocator>
951bitset<_Size>::to_string(_CharT __zero, _CharT __one) const
952{
953 basic_string<_CharT, _Traits, _Allocator> __r(_Size, __zero);
954 for (size_t __i = 0; __i < _Size; ++__i)
955 {
956 if ((*this)[__i])
957 __r[_Size - 1 - __i] = __one;
958 }
959 return __r;
960}
961
962template <size_t _Size>
Howard Hinnantb3371f62010-08-22 00:02:43963template <class _CharT, class _Traits>
Evgeniy Stepanov906c8722015-11-07 01:22:13964inline
Howard Hinnant3e519522010-05-11 19:42:16965basic_string<_CharT, _Traits, allocator<_CharT> >
966bitset<_Size>::to_string(_CharT __zero, _CharT __one) const
967{
968 return to_string<_CharT, _Traits, allocator<_CharT> >(__zero, __one);
969}
970
971template <size_t _Size>
Howard Hinnantb3371f62010-08-22 00:02:43972template <class _CharT>
Evgeniy Stepanov906c8722015-11-07 01:22:13973inline
Howard Hinnant3e519522010-05-11 19:42:16974basic_string<_CharT, char_traits<_CharT>, allocator<_CharT> >
975bitset<_Size>::to_string(_CharT __zero, _CharT __one) const
976{
977 return to_string<_CharT, char_traits<_CharT>, allocator<_CharT> >(__zero, __one);
978}
979
980template <size_t _Size>
Evgeniy Stepanov906c8722015-11-07 01:22:13981inline
Howard Hinnant3e519522010-05-11 19:42:16982basic_string<char, char_traits<char>, allocator<char> >
983bitset<_Size>::to_string(char __zero, char __one) const
984{
985 return to_string<char, char_traits<char>, allocator<char> >(__zero, __one);
986}
987
988template <size_t _Size>
Evgeniy Stepanov906c8722015-11-07 01:22:13989inline
Howard Hinnant3e519522010-05-11 19:42:16990size_t
Howard Hinnantd368a842011-05-27 20:52:28991bitset<_Size>::count() const _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16992{
Adhemerval Zanella63ea9582019-01-11 17:31:17993 return static_cast<size_t>(__count_bool_true(base::__make_iter(0), _Size));
Howard Hinnant3e519522010-05-11 19:42:16994}
995
996template <size_t _Size>
Evgeniy Stepanov906c8722015-11-07 01:22:13997inline
Howard Hinnant3e519522010-05-11 19:42:16998bool
Howard Hinnantd368a842011-05-27 20:52:28999bitset<_Size>::operator==(const bitset& __rhs) const _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:161000{
Howard Hinnantce48a112011-06-30 21:18:191001 return _VSTD::equal(base::__make_iter(0), base::__make_iter(_Size), __rhs.__make_iter(0));
Howard Hinnant3e519522010-05-11 19:42:161002}
1003
1004template <size_t _Size>
Evgeniy Stepanov906c8722015-11-07 01:22:131005inline
Howard Hinnant3e519522010-05-11 19:42:161006bool
Howard Hinnantd368a842011-05-27 20:52:281007bitset<_Size>::operator!=(const bitset& __rhs) const _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:161008{
1009 return !(*this == __rhs);
1010}
1011
1012template <size_t _Size>
1013bool
1014bitset<_Size>::test(size_t __pos) const
1015{
1016 if (__pos >= _Size)
Marshall Clowd437fa52016-08-25 15:09:011017 __throw_out_of_range("bitset test argument out of range");
1018
Howard Hinnant3e519522010-05-11 19:42:161019 return (*this)[__pos];
1020}
1021
1022template <size_t _Size>
Evgeniy Stepanov906c8722015-11-07 01:22:131023inline
Howard Hinnant3e519522010-05-11 19:42:161024bool
Howard Hinnantd368a842011-05-27 20:52:281025bitset<_Size>::all() const _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:161026{
1027 return base::all();
1028}
1029
1030template <size_t _Size>
Evgeniy Stepanov906c8722015-11-07 01:22:131031inline
Howard Hinnant3e519522010-05-11 19:42:161032bool
Howard Hinnantd368a842011-05-27 20:52:281033bitset<_Size>::any() const _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:161034{
1035 return base::any();
1036}
1037
1038template <size_t _Size>
Evgeniy Stepanov906c8722015-11-07 01:22:131039inline
Howard Hinnant3e519522010-05-11 19:42:161040bitset<_Size>
Howard Hinnantd368a842011-05-27 20:52:281041bitset<_Size>::operator<<(size_t __pos) const _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:161042{
1043 bitset __r = *this;
1044 __r <<= __pos;
1045 return __r;
1046}
1047
1048template <size_t _Size>
Evgeniy Stepanov906c8722015-11-07 01:22:131049inline
Howard Hinnant3e519522010-05-11 19:42:161050bitset<_Size>
Howard Hinnantd368a842011-05-27 20:52:281051bitset<_Size>::operator>>(size_t __pos) const _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:161052{
1053 bitset __r = *this;
1054 __r >>= __pos;
1055 return __r;
1056}
1057
1058template <size_t _Size>
1059inline _LIBCPP_INLINE_VISIBILITY
1060bitset<_Size>
Howard Hinnantd368a842011-05-27 20:52:281061operator&(const bitset<_Size>& __x, const bitset<_Size>& __y) _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:161062{
1063 bitset<_Size> __r = __x;
1064 __r &= __y;
1065 return __r;
1066}
1067
1068template <size_t _Size>
1069inline _LIBCPP_INLINE_VISIBILITY
1070bitset<_Size>
Howard Hinnantd368a842011-05-27 20:52:281071operator|(const bitset<_Size>& __x, const bitset<_Size>& __y) _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:161072{
1073 bitset<_Size> __r = __x;
1074 __r |= __y;
1075 return __r;
1076}
1077
1078template <size_t _Size>
1079inline _LIBCPP_INLINE_VISIBILITY
1080bitset<_Size>
Howard Hinnantd368a842011-05-27 20:52:281081operator^(const bitset<_Size>& __x, const bitset<_Size>& __y) _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:161082{
1083 bitset<_Size> __r = __x;
1084 __r ^= __y;
1085 return __r;
1086}
1087
1088template <size_t _Size>
Eric Fiseliere2f2d1ed2017-01-04 23:56:001089struct _LIBCPP_TEMPLATE_VIS hash<bitset<_Size> >
Howard Hinnant3e519522010-05-11 19:42:161090 : public unary_function<bitset<_Size>, size_t>
1091{
Howard Hinnantfb100022010-09-21 21:28:231092 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd368a842011-05-27 20:52:281093 size_t operator()(const bitset<_Size>& __bs) const _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:161094 {return __bs.__hash_code();}
1095};
1096
Howard Hinnante3163f52011-07-18 15:51:591097template <class _CharT, class _Traits, size_t _Size>
1098basic_istream<_CharT, _Traits>&
1099operator>>(basic_istream<_CharT, _Traits>& __is, bitset<_Size>& __x);
1100
1101template <class _CharT, class _Traits, size_t _Size>
1102basic_ostream<_CharT, _Traits>&
1103operator<<(basic_ostream<_CharT, _Traits>& __os, const bitset<_Size>& __x);
1104
Howard Hinnant3e519522010-05-11 19:42:161105_LIBCPP_END_NAMESPACE_STD
1106
Eric Fiseliera016efb2017-05-31 22:07:491107_LIBCPP_POP_MACROS
1108
Howard Hinnant3e519522010-05-11 19:42:161109#endif // _LIBCPP_BITSET