Debian Codegraph @

libcxx/include/__cxx03/bitset

raw
1 // -*- C++ -*-
2 //===----------------------------------------------------------------------===//
3 //
4 // 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
7 //
8 //===----------------------------------------------------------------------===//
9
10 #ifndef _LIBCPP___CXX03_BITSET
11 #define _LIBCPP___CXX03_BITSET
12
13 // clang-format off
14
15 /*
16 bitset synopsis
17
18 namespace std
19 {
20
21 namespace std {
22
23 template <size_t N>
24 class bitset
25 {
26 public:
27 // bit reference:
28 class reference
29 {
30 friend class bitset;
31 reference() noexcept;
32 public:
33 ~reference() noexcept;
34 reference& operator=(bool x) noexcept; // for b[i] = x;
35 reference& operator=(const reference&) noexcept; // for b[i] = b[j];
36 bool operator~() const noexcept; // flips the bit
37 operator bool() const noexcept; // for x = b[i];
38 reference& flip() noexcept; // for b[i].flip();
39 };
40
41 // 23.3.5.1 constructors:
42 constexpr bitset() noexcept;
43 constexpr bitset(unsigned long long val) noexcept;
44 template <class charT>
45 constexpr explicit bitset(const charT* str,
46 typename basic_string<charT>::size_type n = basic_string<charT>::npos,
47 charT zero = charT('0'), charT one = charT('1')); // until C++26, constexpr since C++23
48 template <class charT>
49 constexpr explicit bitset(const charT* str,
50 typename basic_string_view<charT>::size_type n = basic_string_view<charT>::npos,
51 charT zero = charT('0'), charT one = charT('1')); // since C++26
52 template<class charT, class traits>
53 explicit bitset(
54 const basic_string_view<charT,traits>& str,
55 typename basic_string_view<charT,traits>::size_type pos = 0,
56 typename basic_string_view<charT,traits>::size_type n = basic_string_view<charT,traits>::npos,
57 charT zero = charT('0'), charT one = charT('1')); // since C++26
58 template<class charT, class traits, class Allocator>
59 constexpr explicit bitset(
60 const basic_string<charT,traits,Allocator>& str,
61 typename basic_string<charT,traits,Allocator>::size_type pos = 0,
62 typename basic_string<charT,traits,Allocator>::size_type n = basic_string<charT,traits,Allocator>::npos,
63 charT zero = charT('0'), charT one = charT('1')); // constexpr since C++23
64
65 // 23.3.5.2 bitset operations:
66 bitset& operator&=(const bitset& rhs) noexcept; // constexpr since C++23
67 bitset& operator|=(const bitset& rhs) noexcept; // constexpr since C++23
68 bitset& operator^=(const bitset& rhs) noexcept; // constexpr since C++23
69 bitset& operator<<=(size_t pos) noexcept; // constexpr since C++23
70 bitset& operator>>=(size_t pos) noexcept; // constexpr since C++23
71 bitset& set() noexcept; // constexpr since C++23
72 bitset& set(size_t pos, bool val = true); // constexpr since C++23
73 bitset& reset() noexcept; // constexpr since C++23
74 bitset& reset(size_t pos); // constexpr since C++23
75 bitset operator~() const noexcept; // constexpr since C++23
76 bitset& flip() noexcept; // constexpr since C++23
77 bitset& flip(size_t pos); // constexpr since C++23
78
79 // element access:
80 constexpr bool operator[](size_t pos) const;
81 reference operator[](size_t pos); // constexpr since C++23
82 unsigned long to_ulong() const; // constexpr since C++23
83 unsigned long long to_ullong() const; // constexpr since C++23
84 template <class charT, class traits, class Allocator> // constexpr since C++23
85 basic_string<charT, traits, Allocator> to_string(charT zero = charT('0'), charT one = charT('1')) const;
86 template <class charT, class traits> // constexpr since C++23
87 basic_string<charT, traits, allocator<charT> > to_string(charT zero = charT('0'), charT one = charT('1')) const;
88 template <class charT> // constexpr since C++23
89 basic_string<charT, char_traits<charT>, allocator<charT> > to_string(charT zero = charT('0'), charT one = charT('1')) const;
90 basic_string<char, char_traits<char>, allocator<char> > to_string(char zero = '0', char one = '1') const; // constexpr since C++23
91 size_t count() const noexcept; // constexpr since C++23
92 constexpr size_t size() const noexcept; // constexpr since C++23
93 bool operator==(const bitset& rhs) const noexcept; // constexpr since C++23
94 bool operator!=(const bitset& rhs) const noexcept; // removed in C++20
95 bool test(size_t pos) const; // constexpr since C++23
96 bool all() const noexcept; // constexpr since C++23
97 bool any() const noexcept; // constexpr since C++23
98 bool none() const noexcept; // constexpr since C++23
99 bitset<N> operator<<(size_t pos) const noexcept; // constexpr since C++23
100 bitset<N> operator>>(size_t pos) const noexcept; // constexpr since C++23
101 };
102
103 // 23.3.5.3 bitset operators:
104 template <size_t N>
105 bitset<N> operator&(const bitset<N>&, const bitset<N>&) noexcept; // constexpr since C++23
106
107 template <size_t N>
108 bitset<N> operator|(const bitset<N>&, const bitset<N>&) noexcept; // constexpr since C++23
109
110 template <size_t N>
111 bitset<N> operator^(const bitset<N>&, const bitset<N>&) noexcept; // constexpr since C++23
112
113 template <class charT, class traits, size_t N>
114 basic_istream<charT, traits>&
115 operator>>(basic_istream<charT, traits>& is, bitset<N>& x);
116
117 template <class charT, class traits, size_t N>
118 basic_ostream<charT, traits>&
119 operator<<(basic_ostream<charT, traits>& os, const bitset<N>& x);
120
121 template <size_t N> struct hash<std::bitset<N>>;
122
123 } // std
124
125 */
126
127 // clang-format on
128
129 #include <__cxx03/__algorithm/count.h>
130 #include <__cxx03/__algorithm/fill.h>
131 #include <__cxx03/__algorithm/find.h>
132 #include <__cxx03/__bit_reference>
133 #include <__cxx03/__config>
134 #include <__cxx03/__functional/hash.h>
135 #include <__cxx03/__functional/unary_function.h>
136 #include <__cxx03/__type_traits/is_char_like_type.h>
137 #include <__cxx03/climits>
138 #include <__cxx03/cstddef>
139 #include <__cxx03/stdexcept>
140 #include <__cxx03/string_view>
141 #include <__cxx03/version>
142
143 // standard-mandated includes
144
145 // [bitset.syn]
146 #include <__cxx03/iosfwd>
147 #include <__cxx03/string>
148
149 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
150 # pragma GCC system_header
151 #endif
152
153 _LIBCPP_PUSH_MACROS
154 #include <__cxx03/__undef_macros>
155
156 _LIBCPP_BEGIN_NAMESPACE_STD
157
158 template <size_t _N_words, size_t _Size>
159 class __bitset;
160
161 template <size_t _N_words, size_t _Size>
162 struct __has_storage_type<__bitset<_N_words, _Size> > {
163 static const bool value = true;
164 };
165
166 template <size_t _N_words, size_t _Size>
167 class __bitset {
168 public:
169 typedef ptrdiff_t difference_type;
170 typedef size_t size_type;
171 typedef size_type __storage_type;
172
173 protected:
174 typedef __bitset __self;
175 typedef __storage_type* __storage_pointer;
176 typedef const __storage_type* __const_storage_pointer;
177 static const unsigned __bits_per_word = static_cast<unsigned>(sizeof(__storage_type) * CHAR_BIT);
178
179 friend class __bit_reference<__bitset>;
180 friend class __bit_const_reference<__bitset>;
181 friend class __bit_iterator<__bitset, false>;
182 friend class __bit_iterator<__bitset, true>;
183 friend struct __bit_array<__bitset>;
184
185 __storage_type __first_[_N_words];
186
187 typedef __bit_reference<__bitset> reference;
188 typedef __bit_const_reference<__bitset> __const_reference;
189 typedef __bit_iterator<__bitset, false> __iterator;
190 typedef __bit_iterator<__bitset, true> __const_iterator;
191
192 _LIBCPP_HIDE_FROM_ABI __bitset() _NOEXCEPT;
193 _LIBCPP_HIDE_FROM_ABI explicit __bitset(unsigned long long __v) _NOEXCEPT;
194
195 _LIBCPP_HIDE_FROM_ABI reference __make_ref(size_t __pos) _NOEXCEPT {
196 return reference(__first_ + __pos / __bits_per_word, __storage_type(1) << __pos % __bits_per_word);
197 }
198 _LIBCPP_HIDE_FROM_ABI __const_reference __make_ref(size_t __pos) const _NOEXCEPT {
199 return __const_reference(__first_ + __pos / __bits_per_word, __storage_type(1) << __pos % __bits_per_word);
200 }
201 _LIBCPP_HIDE_FROM_ABI __iterator __make_iter(size_t __pos) _NOEXCEPT {
202 return __iterator(__first_ + __pos / __bits_per_word, __pos % __bits_per_word);
203 }
204 _LIBCPP_HIDE_FROM_ABI __const_iterator __make_iter(size_t __pos) const _NOEXCEPT {
205 return __const_iterator(__first_ + __pos / __bits_per_word, __pos % __bits_per_word);
206 }
207
208 _LIBCPP_HIDE_FROM_ABI void operator&=(const __bitset& __v) _NOEXCEPT;
209 _LIBCPP_HIDE_FROM_ABI void operator|=(const __bitset& __v) _NOEXCEPT;
210 _LIBCPP_HIDE_FROM_ABI void operator^=(const __bitset& __v) _NOEXCEPT;
211
212 _LIBCPP_HIDE_FROM_ABI void flip() _NOEXCEPT;
213 _LIBCPP_HIDE_FROM_ABI unsigned long to_ulong() const {
214 return to_ulong(integral_constant < bool, _Size< sizeof(unsigned long) * CHAR_BIT>());
215 }
216 _LIBCPP_HIDE_FROM_ABI unsigned long long to_ullong() const {
217 return to_ullong(integral_constant < bool, _Size< sizeof(unsigned long long) * CHAR_BIT>());
218 }
219
220 _LIBCPP_HIDE_FROM_ABI bool all() const _NOEXCEPT;
221 _LIBCPP_HIDE_FROM_ABI bool any() const _NOEXCEPT;
222 _LIBCPP_HIDE_FROM_ABI size_t __hash_code() const _NOEXCEPT;
223
224 private:
225 void __init(unsigned long long __v, false_type) _NOEXCEPT;
226 _LIBCPP_HIDE_FROM_ABI void __init(unsigned long long __v, true_type) _NOEXCEPT;
227 _LIBCPP_HIDE_FROM_ABI unsigned long to_ulong(false_type) const;
228 _LIBCPP_HIDE_FROM_ABI unsigned long to_ulong(true_type) const;
229 _LIBCPP_HIDE_FROM_ABI unsigned long long to_ullong(false_type) const;
230 _LIBCPP_HIDE_FROM_ABI unsigned long long to_ullong(true_type) const;
231 _LIBCPP_HIDE_FROM_ABI unsigned long long to_ullong(true_type, false_type) const;
232 _LIBCPP_HIDE_FROM_ABI unsigned long long to_ullong(true_type, true_type) const;
233 };
234
235 template <size_t _N_words, size_t _Size>
236 inline __bitset<_N_words, _Size>::__bitset() _NOEXCEPT {
237 std::fill_n(__first_, _N_words, __storage_type(0));
238 }
239
240 template <size_t _N_words, size_t _Size>
241 void __bitset<_N_words, _Size>::__init(unsigned long long __v, false_type) _NOEXCEPT {
242 __storage_type __t[sizeof(unsigned long long) / sizeof(__storage_type)];
243 size_t __sz = _Size;
244 for (size_t __i = 0; __i < sizeof(__t) / sizeof(__t[0]); ++__i, __v >>= __bits_per_word, __sz -= __bits_per_word)
245 if (__sz < __bits_per_word)
246 __t[__i] = static_cast<__storage_type>(__v) & (1ULL << __sz) - 1;
247 else
248 __t[__i] = static_cast<__storage_type>(__v);
249
250 std::copy(__t, __t + sizeof(__t) / sizeof(__t[0]), __first_);
251 std::fill(
252 __first_ + sizeof(__t) / sizeof(__t[0]), __first_ + sizeof(__first_) / sizeof(__first_[0]), __storage_type(0));
253 }
254
255 template <size_t _N_words, size_t _Size>
256 inline _LIBCPP_HIDE_FROM_ABI void __bitset<_N_words, _Size>::__init(unsigned long long __v, true_type) _NOEXCEPT {
257 __first_[0] = __v;
258 if (_Size < __bits_per_word)
259 __first_[0] &= (1ULL << _Size) - 1;
260
261 std::fill(__first_ + 1, __first_ + sizeof(__first_) / sizeof(__first_[0]), __storage_type(0));
262 }
263
264 template <size_t _N_words, size_t _Size>
265 inline __bitset<_N_words, _Size>::__bitset(unsigned long long __v) _NOEXCEPT {
266 __init(__v, integral_constant<bool, sizeof(unsigned long long) == sizeof(__storage_type)>());
267 }
268
269 template <size_t _N_words, size_t _Size>
270 inline _LIBCPP_HIDE_FROM_ABI void __bitset<_N_words, _Size>::operator&=(const __bitset& __v) _NOEXCEPT {
271 for (size_type __i = 0; __i < _N_words; ++__i)
272 __first_[__i] &= __v.__first_[__i];
273 }
274
275 template <size_t _N_words, size_t _Size>
276 inline _LIBCPP_HIDE_FROM_ABI void __bitset<_N_words, _Size>::operator|=(const __bitset& __v) _NOEXCEPT {
277 for (size_type __i = 0; __i < _N_words; ++__i)
278 __first_[__i] |= __v.__first_[__i];
279 }
280
281 template <size_t _N_words, size_t _Size>
282 inline _LIBCPP_HIDE_FROM_ABI void __bitset<_N_words, _Size>::operator^=(const __bitset& __v) _NOEXCEPT {
283 for (size_type __i = 0; __i < _N_words; ++__i)
284 __first_[__i] ^= __v.__first_[__i];
285 }
286
287 template <size_t _N_words, size_t _Size>
288 _LIBCPP_HIDE_FROM_ABI void __bitset<_N_words, _Size>::flip() _NOEXCEPT {
289 // do middle whole words
290 size_type __n = _Size;
291 __storage_pointer __p = __first_;
292 for (; __n >= __bits_per_word; ++__p, __n -= __bits_per_word)
293 *__p = ~*__p;
294 // do last partial word
295 if (__n > 0) {
296 __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
297 __storage_type __b = *__p & __m;
298 *__p &= ~__m;
299 *__p |= ~__b & __m;
300 }
301 }
302
303 template <size_t _N_words, size_t _Size>
304 _LIBCPP_HIDE_FROM_ABI unsigned long __bitset<_N_words, _Size>::to_ulong(false_type) const {
305 __const_iterator __e = __make_iter(_Size);
306 __const_iterator __i = std::find(__make_iter(sizeof(unsigned long) * CHAR_BIT), __e, true);
307 if (__i != __e)
308 __throw_overflow_error("bitset to_ulong overflow error");
309
310 return __first_[0];
311 }
312
313 template <size_t _N_words, size_t _Size>
314 inline _LIBCPP_HIDE_FROM_ABI unsigned long __bitset<_N_words, _Size>::to_ulong(true_type) const {
315 return __first_[0];
316 }
317
318 template <size_t _N_words, size_t _Size>
319 _LIBCPP_HIDE_FROM_ABI unsigned long long __bitset<_N_words, _Size>::to_ullong(false_type) const {
320 __const_iterator __e = __make_iter(_Size);
321 __const_iterator __i = std::find(__make_iter(sizeof(unsigned long long) * CHAR_BIT), __e, true);
322 if (__i != __e)
323 __throw_overflow_error("bitset to_ullong overflow error");
324
325 return to_ullong(true_type());
326 }
327
328 template <size_t _N_words, size_t _Size>
329 inline _LIBCPP_HIDE_FROM_ABI unsigned long long __bitset<_N_words, _Size>::to_ullong(true_type) const {
330 return to_ullong(true_type(), integral_constant<bool, sizeof(__storage_type) < sizeof(unsigned long long)>());
331 }
332
333 template <size_t _N_words, size_t _Size>
334 inline _LIBCPP_HIDE_FROM_ABI unsigned long long __bitset<_N_words, _Size>::to_ullong(true_type, false_type) const {
335 return __first_[0];
336 }
337
338 template <size_t _N_words, size_t _Size>
339 _LIBCPP_HIDE_FROM_ABI unsigned long long __bitset<_N_words, _Size>::to_ullong(true_type, true_type) const {
340 unsigned long long __r = __first_[0];
341 _LIBCPP_DIAGNOSTIC_PUSH
342 _LIBCPP_GCC_DIAGNOSTIC_IGNORED("-Wshift-count-overflow")
343 for (size_t __i = 1; __i < sizeof(unsigned long long) / sizeof(__storage_type); ++__i)
344 __r |= static_cast<unsigned long long>(__first_[__i]) << (sizeof(__storage_type) * CHAR_BIT);
345 _LIBCPP_DIAGNOSTIC_POP
346 return __r;
347 }
348
349 template <size_t _N_words, size_t _Size>
350 _LIBCPP_HIDE_FROM_ABI bool __bitset<_N_words, _Size>::all() const _NOEXCEPT {
351 // do middle whole words
352 size_type __n = _Size;
353 __const_storage_pointer __p = __first_;
354 for (; __n >= __bits_per_word; ++__p, __n -= __bits_per_word)
355 if (~*__p)
356 return false;
357 // do last partial word
358 if (__n > 0) {
359 __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
360 if (~*__p & __m)
361 return false;
362 }
363 return true;
364 }
365
366 template <size_t _N_words, size_t _Size>
367 _LIBCPP_HIDE_FROM_ABI bool __bitset<_N_words, _Size>::any() const _NOEXCEPT {
368 // do middle whole words
369 size_type __n = _Size;
370 __const_storage_pointer __p = __first_;
371 for (; __n >= __bits_per_word; ++__p, __n -= __bits_per_word)
372 if (*__p)
373 return true;
374 // do last partial word
375 if (__n > 0) {
376 __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
377 if (*__p & __m)
378 return true;
379 }
380 return false;
381 }
382
383 template <size_t _N_words, size_t _Size>
384 inline size_t __bitset<_N_words, _Size>::__hash_code() const _NOEXCEPT {
385 size_t __h = 0;
386 for (size_type __i = 0; __i < _N_words; ++__i)
387 __h ^= __first_[__i];
388 return __h;
389 }
390
391 template <size_t _Size>
392 class __bitset<1, _Size> {
393 public:
394 typedef ptrdiff_t difference_type;
395 typedef size_t size_type;
396 typedef size_type __storage_type;
397
398 protected:
399 typedef __bitset __self;
400 typedef __storage_type* __storage_pointer;
401 typedef const __storage_type* __const_storage_pointer;
402 static const unsigned __bits_per_word = static_cast<unsigned>(sizeof(__storage_type) * CHAR_BIT);
403
404 friend class __bit_reference<__bitset>;
405 friend class __bit_const_reference<__bitset>;
406 friend class __bit_iterator<__bitset, false>;
407 friend class __bit_iterator<__bitset, true>;
408 friend struct __bit_array<__bitset>;
409
410 __storage_type __first_;
411
412 typedef __bit_reference<__bitset> reference;
413 typedef __bit_const_reference<__bitset> __const_reference;
414 typedef __bit_iterator<__bitset, false> __iterator;
415 typedef __bit_iterator<__bitset, true> __const_iterator;
416
417 _LIBCPP_HIDE_FROM_ABI __bitset() _NOEXCEPT;
418 _LIBCPP_HIDE_FROM_ABI explicit __bitset(unsigned long long __v) _NOEXCEPT;
419
420 _LIBCPP_HIDE_FROM_ABI reference __make_ref(size_t __pos) _NOEXCEPT {
421 return reference(&__first_, __storage_type(1) << __pos);
422 }
423 _LIBCPP_HIDE_FROM_ABI __const_reference __make_ref(size_t __pos) const _NOEXCEPT {
424 return __const_reference(&__first_, __storage_type(1) << __pos);
425 }
426 _LIBCPP_HIDE_FROM_ABI __iterator __make_iter(size_t __pos) _NOEXCEPT {
427 return __iterator(&__first_ + __pos / __bits_per_word, __pos % __bits_per_word);
428 }
429 _LIBCPP_HIDE_FROM_ABI __const_iterator __make_iter(size_t __pos) const _NOEXCEPT {
430 return __const_iterator(&__first_ + __pos / __bits_per_word, __pos % __bits_per_word);
431 }
432
433 _LIBCPP_HIDE_FROM_ABI void operator&=(const __bitset& __v) _NOEXCEPT;
434 _LIBCPP_HIDE_FROM_ABI void operator|=(const __bitset& __v) _NOEXCEPT;
435 _LIBCPP_HIDE_FROM_ABI void operator^=(const __bitset& __v) _NOEXCEPT;
436
437 _LIBCPP_HIDE_FROM_ABI void flip() _NOEXCEPT;
438
439 _LIBCPP_HIDE_FROM_ABI unsigned long to_ulong() const;
440 _LIBCPP_HIDE_FROM_ABI unsigned long long to_ullong() const;
441
442 _LIBCPP_HIDE_FROM_ABI bool all() const _NOEXCEPT;
443 _LIBCPP_HIDE_FROM_ABI bool any() const _NOEXCEPT;
444
445 _LIBCPP_HIDE_FROM_ABI size_t __hash_code() const _NOEXCEPT;
446 };
447
448 template <size_t _Size>
449 inline __bitset<1, _Size>::__bitset() _NOEXCEPT : __first_(0) {}
450
451 template <size_t _Size>
452 inline __bitset<1, _Size>::__bitset(unsigned long long __v) _NOEXCEPT
453 : __first_(_Size == __bits_per_word ? static_cast<__storage_type>(__v)
454 : static_cast<__storage_type>(__v) & ((__storage_type(1) << _Size) - 1)) {}
455
456 template <size_t _Size>
457 inline _LIBCPP_HIDE_FROM_ABI void __bitset<1, _Size>::operator&=(const __bitset& __v) _NOEXCEPT {
458 __first_ &= __v.__first_;
459 }
460
461 template <size_t _Size>
462 inline _LIBCPP_HIDE_FROM_ABI void __bitset<1, _Size>::operator|=(const __bitset& __v) _NOEXCEPT {
463 __first_ |= __v.__first_;
464 }
465
466 template <size_t _Size>
467 inline _LIBCPP_HIDE_FROM_ABI void __bitset<1, _Size>::operator^=(const __bitset& __v) _NOEXCEPT {
468 __first_ ^= __v.__first_;
469 }
470
471 template <size_t _Size>
472 inline _LIBCPP_HIDE_FROM_ABI void __bitset<1, _Size>::flip() _NOEXCEPT {
473 __storage_type __m = ~__storage_type(0) >> (__bits_per_word - _Size);
474 __first_ = ~__first_;
475 __first_ &= __m;
476 }
477
478 template <size_t _Size>
479 inline unsigned long __bitset<1, _Size>::to_ulong() const {
480 return __first_;
481 }
482
483 template <size_t _Size>
484 inline _LIBCPP_HIDE_FROM_ABI unsigned long long __bitset<1, _Size>::to_ullong() const {
485 return __first_;
486 }
487
488 template <size_t _Size>
489 inline _LIBCPP_HIDE_FROM_ABI bool __bitset<1, _Size>::all() const _NOEXCEPT {
490 __storage_type __m = ~__storage_type(0) >> (__bits_per_word - _Size);
491 return !(~__first_ & __m);
492 }
493
494 template <size_t _Size>
495 inline _LIBCPP_HIDE_FROM_ABI bool __bitset<1, _Size>::any() const _NOEXCEPT {
496 __storage_type __m = ~__storage_type(0) >> (__bits_per_word - _Size);
497 return __first_ & __m;
498 }
499
500 template <size_t _Size>
501 inline size_t __bitset<1, _Size>::__hash_code() const _NOEXCEPT {
502 return __first_;
503 }
504
505 template <>
506 class __bitset<0, 0> {
507 public:
508 typedef ptrdiff_t difference_type;
509 typedef size_t size_type;
510 typedef size_type __storage_type;
511
512 protected:
513 typedef __bitset __self;
514 typedef __storage_type* __storage_pointer;
515 typedef const __storage_type* __const_storage_pointer;
516 static const unsigned __bits_per_word = static_cast<unsigned>(sizeof(__storage_type) * CHAR_BIT);
517
518 friend class __bit_reference<__bitset>;
519 friend class __bit_const_reference<__bitset>;
520 friend class __bit_iterator<__bitset, false>;
521 friend class __bit_iterator<__bitset, true>;
522 friend struct __bit_array<__bitset>;
523
524 typedef __bit_reference<__bitset> reference;
525 typedef __bit_const_reference<__bitset> __const_reference;
526 typedef __bit_iterator<__bitset, false> __iterator;
527 typedef __bit_iterator<__bitset, true> __const_iterator;
528
529 _LIBCPP_HIDE_FROM_ABI __bitset() _NOEXCEPT;
530 _LIBCPP_HIDE_FROM_ABI explicit __bitset(unsigned long long) _NOEXCEPT;
531
532 _LIBCPP_HIDE_FROM_ABI reference __make_ref(size_t) _NOEXCEPT { return reference(nullptr, 1); }
533 _LIBCPP_HIDE_FROM_ABI __const_reference __make_ref(size_t) const _NOEXCEPT { return __const_reference(nullptr, 1); }
534 _LIBCPP_HIDE_FROM_ABI __iterator __make_iter(size_t) _NOEXCEPT { return __iterator(nullptr, 0); }
535 _LIBCPP_HIDE_FROM_ABI __const_iterator __make_iter(size_t) const _NOEXCEPT { return __const_iterator(nullptr, 0); }
536
537 _LIBCPP_HIDE_FROM_ABI void operator&=(const __bitset&) _NOEXCEPT {}
538 _LIBCPP_HIDE_FROM_ABI void operator|=(const __bitset&) _NOEXCEPT {}
539 _LIBCPP_HIDE_FROM_ABI void operator^=(const __bitset&) _NOEXCEPT {}
540
541 _LIBCPP_HIDE_FROM_ABI void flip() _NOEXCEPT {}
542
543 _LIBCPP_HIDE_FROM_ABI unsigned long to_ulong() const { return 0; }
544 _LIBCPP_HIDE_FROM_ABI unsigned long long to_ullong() const { return 0; }
545
546 _LIBCPP_HIDE_FROM_ABI bool all() const _NOEXCEPT { return true; }
547 _LIBCPP_HIDE_FROM_ABI bool any() const _NOEXCEPT { return false; }
548
549 _LIBCPP_HIDE_FROM_ABI size_t __hash_code() const _NOEXCEPT { return 0; }
550 };
551
552 inline __bitset<0, 0>::__bitset() _NOEXCEPT {}
553
554 inline __bitset<0, 0>::__bitset(unsigned long long) _NOEXCEPT {}
555
556 template <size_t _Size>
557 class _LIBCPP_TEMPLATE_VIS bitset;
558 template <size_t _Size>
559 struct hash<bitset<_Size> >;
560
561 template <size_t _Size>
562 class _LIBCPP_TEMPLATE_VIS bitset
563 : private __bitset<_Size == 0 ? 0 : (_Size - 1) / (sizeof(size_t) * CHAR_BIT) + 1, _Size> {
564 public:
565 static const unsigned __n_words = _Size == 0 ? 0 : (_Size - 1) / (sizeof(size_t) * CHAR_BIT) + 1;
566 typedef __bitset<__n_words, _Size> __base;
567
568 public:
569 typedef typename __base::reference reference;
570 typedef typename __base::__const_reference __const_reference;
571
572 // 23.3.5.1 constructors:
573 _LIBCPP_HIDE_FROM_ABI bitset() _NOEXCEPT {}
574 _LIBCPP_HIDE_FROM_ABI bitset(unsigned long long __v) _NOEXCEPT : __base(__v) {}
575 template <class _CharT, __enable_if_t<_IsCharLikeType<_CharT>::value, int> = 0>
576 _LIBCPP_HIDE_FROM_ABI explicit bitset(
577 const _CharT* __str,
578 typename basic_string<_CharT>::size_type __n = basic_string<_CharT>::npos,
579 _CharT __zero = _CharT('0'),
580 _CharT __one = _CharT('1')) {
581 size_t __rlen = std::min(__n, char_traits<_CharT>::length(__str));
582 __init_from_string_view(basic_string_view<_CharT>(__str, __rlen), __zero, __one);
583 }
584
585 template <class _CharT, class _Traits, class _Allocator>
586 _LIBCPP_HIDE_FROM_ABI explicit bitset(
587 const basic_string<_CharT, _Traits, _Allocator>& __str,
588 typename basic_string<_CharT, _Traits, _Allocator>::size_type __pos = 0,
589 typename basic_string<_CharT, _Traits, _Allocator>::size_type __n =
590 basic_string<_CharT, _Traits, _Allocator>::npos,
591 _CharT __zero = _CharT('0'),
592 _CharT __one = _CharT('1')) {
593 if (__pos > __str.size())
594 std::__throw_out_of_range("bitset string pos out of range");
595
596 size_t __rlen = std::min(__n, __str.size() - __pos);
597 __init_from_string_view(basic_string_view<_CharT, _Traits>(__str.data() + __pos, __rlen), __zero, __one);
598 }
599
600 // 23.3.5.2 bitset operations:
601 _LIBCPP_HIDE_FROM_ABI bitset& operator&=(const bitset& __rhs) _NOEXCEPT;
602 _LIBCPP_HIDE_FROM_ABI bitset& operator|=(const bitset& __rhs) _NOEXCEPT;
603 _LIBCPP_HIDE_FROM_ABI bitset& operator^=(const bitset& __rhs) _NOEXCEPT;
604 _LIBCPP_HIDE_FROM_ABI bitset& operator<<=(size_t __pos) _NOEXCEPT;
605 _LIBCPP_HIDE_FROM_ABI bitset& operator>>=(size_t __pos) _NOEXCEPT;
606 _LIBCPP_HIDE_FROM_ABI bitset& set() _NOEXCEPT;
607 _LIBCPP_HIDE_FROM_ABI bitset& set(size_t __pos, bool __val = true);
608 _LIBCPP_HIDE_FROM_ABI bitset& reset() _NOEXCEPT;
609 _LIBCPP_HIDE_FROM_ABI bitset& reset(size_t __pos);
610 _LIBCPP_HIDE_FROM_ABI bitset operator~() const _NOEXCEPT;
611 _LIBCPP_HIDE_FROM_ABI bitset& flip() _NOEXCEPT;
612 _LIBCPP_HIDE_FROM_ABI bitset& flip(size_t __pos);
613
614 // element access:
615 #ifndef _LIBCPP_DEPRECATED_ABI_BITSET_CONST_SUBSCRIPT_RETURN_REF
616 _LIBCPP_HIDE_FROM_ABI bool operator[](size_t __p) const { return __base::__make_ref(__p); }
617 #else
618 _LIBCPP_HIDE_FROM_ABI __const_reference operator[](size_t __p) const { return __base::__make_ref(__p); }
619 #endif
620 _LIBCPP_HIDE_FROM_ABI reference operator[](size_t __p) { return __base::__make_ref(__p); }
621 _LIBCPP_HIDE_FROM_ABI unsigned long to_ulong() const;
622 _LIBCPP_HIDE_FROM_ABI unsigned long long to_ullong() const;
623 template <class _CharT, class _Traits, class _Allocator>
624 _LIBCPP_HIDE_FROM_ABI basic_string<_CharT, _Traits, _Allocator>
625 to_string(_CharT __zero = _CharT('0'), _CharT __one = _CharT('1')) const;
626 template <class _CharT, class _Traits>
627 _LIBCPP_HIDE_FROM_ABI basic_string<_CharT, _Traits, allocator<_CharT> >
628 to_string(_CharT __zero = _CharT('0'), _CharT __one = _CharT('1')) const;
629 template <class _CharT>
630 _LIBCPP_HIDE_FROM_ABI basic_string<_CharT, char_traits<_CharT>, allocator<_CharT> >
631 to_string(_CharT __zero = _CharT('0'), _CharT __one = _CharT('1')) const;
632 _LIBCPP_HIDE_FROM_ABI basic_string<char, char_traits<char>, allocator<char> >
633 to_string(char __zero = '0', char __one = '1') const;
634 _LIBCPP_HIDE_FROM_ABI size_t count() const _NOEXCEPT;
635 _LIBCPP_HIDE_FROM_ABI size_t size() const _NOEXCEPT { return _Size; }
636 _LIBCPP_HIDE_FROM_ABI bool operator==(const bitset& __rhs) const _NOEXCEPT;
637 _LIBCPP_HIDE_FROM_ABI bool operator!=(const bitset& __rhs) const _NOEXCEPT;
638 _LIBCPP_HIDE_FROM_ABI bool test(size_t __pos) const;
639 _LIBCPP_HIDE_FROM_ABI bool all() const _NOEXCEPT;
640 _LIBCPP_HIDE_FROM_ABI bool any() const _NOEXCEPT;
641 _LIBCPP_HIDE_FROM_ABI bool none() const _NOEXCEPT { return !any(); }
642 _LIBCPP_HIDE_FROM_ABI bitset operator<<(size_t __pos) const _NOEXCEPT;
643 _LIBCPP_HIDE_FROM_ABI bitset operator>>(size_t __pos) const _NOEXCEPT;
644
645 private:
646 template <class _CharT, class _Traits>
647 _LIBCPP_HIDE_FROM_ABI void
648 __init_from_string_view(basic_string_view<_CharT, _Traits> __str, _CharT __zero, _CharT __one) {
649 for (size_t __i = 0; __i < __str.size(); ++__i)
650 if (!_Traits::eq(__str[__i], __zero) && !_Traits::eq(__str[__i], __one))
651 std::__throw_invalid_argument("bitset string ctor has invalid argument");
652
653 size_t __mp = std::min(__str.size(), _Size);
654 size_t __i = 0;
655 for (; __i < __mp; ++__i) {
656 _CharT __c = __str[__mp - 1 - __i];
657 (*this)[__i] = _Traits::eq(__c, __one);
658 }
659 std::fill(__base::__make_iter(__i), __base::__make_iter(_Size), false);
660 }
661
662 _LIBCPP_HIDE_FROM_ABI size_t __hash_code() const _NOEXCEPT { return __base::__hash_code(); }
663
664 friend struct hash<bitset>;
665 };
666
667 template <size_t _Size>
668 inline _LIBCPP_HIDE_FROM_ABI bitset<_Size>& bitset<_Size>::operator&=(const bitset& __rhs) _NOEXCEPT {
669 __base::operator&=(__rhs);
670 return *this;
671 }
672
673 template <size_t _Size>
674 inline _LIBCPP_HIDE_FROM_ABI bitset<_Size>& bitset<_Size>::operator|=(const bitset& __rhs) _NOEXCEPT {
675 __base::operator|=(__rhs);
676 return *this;
677 }
678
679 template <size_t _Size>
680 inline _LIBCPP_HIDE_FROM_ABI bitset<_Size>& bitset<_Size>::operator^=(const bitset& __rhs) _NOEXCEPT {
681 __base::operator^=(__rhs);
682 return *this;
683 }
684
685 template <size_t _Size>
686 _LIBCPP_HIDE_FROM_ABI bitset<_Size>& bitset<_Size>::operator<<=(size_t __pos) _NOEXCEPT {
687 __pos = std::min(__pos, _Size);
688 std::copy_backward(__base::__make_iter(0), __base::__make_iter(_Size - __pos), __base::__make_iter(_Size));
689 std::fill_n(__base::__make_iter(0), __pos, false);
690 return *this;
691 }
692
693 template <size_t _Size>
694 _LIBCPP_HIDE_FROM_ABI bitset<_Size>& bitset<_Size>::operator>>=(size_t __pos) _NOEXCEPT {
695 __pos = std::min(__pos, _Size);
696 std::copy(__base::__make_iter(__pos), __base::__make_iter(_Size), __base::__make_iter(0));
697 std::fill_n(__base::__make_iter(_Size - __pos), __pos, false);
698 return *this;
699 }
700
701 template <size_t _Size>
702 inline _LIBCPP_HIDE_FROM_ABI bitset<_Size>& bitset<_Size>::set() _NOEXCEPT {
703 std::fill_n(__base::__make_iter(0), _Size, true);
704 return *this;
705 }
706
707 template <size_t _Size>
708 _LIBCPP_HIDE_FROM_ABI bitset<_Size>& bitset<_Size>::set(size_t __pos, bool __val) {
709 if (__pos >= _Size)
710 __throw_out_of_range("bitset set argument out of range");
711
712 (*this)[__pos] = __val;
713 return *this;
714 }
715
716 template <size_t _Size>
717 inline _LIBCPP_HIDE_FROM_ABI bitset<_Size>& bitset<_Size>::reset() _NOEXCEPT {
718 std::fill_n(__base::__make_iter(0), _Size, false);
719 return *this;
720 }
721
722 template <size_t _Size>
723 _LIBCPP_HIDE_FROM_ABI bitset<_Size>& bitset<_Size>::reset(size_t __pos) {
724 if (__pos >= _Size)
725 __throw_out_of_range("bitset reset argument out of range");
726
727 (*this)[__pos] = false;
728 return *this;
729 }
730
731 template <size_t _Size>
732 inline _LIBCPP_HIDE_FROM_ABI bitset<_Size> bitset<_Size>::operator~() const _NOEXCEPT {
733 bitset __x(*this);
734 __x.flip();
735 return __x;
736 }
737
738 template <size_t _Size>
739 inline _LIBCPP_HIDE_FROM_ABI bitset<_Size>& bitset<_Size>::flip() _NOEXCEPT {
740 __base::flip();
741 return *this;
742 }
743
744 template <size_t _Size>
745 _LIBCPP_HIDE_FROM_ABI bitset<_Size>& bitset<_Size>::flip(size_t __pos) {
746 if (__pos >= _Size)
747 __throw_out_of_range("bitset flip argument out of range");
748
749 reference __r = __base::__make_ref(__pos);
750 __r = ~__r;
751 return *this;
752 }
753
754 template <size_t _Size>
755 inline _LIBCPP_HIDE_FROM_ABI unsigned long bitset<_Size>::to_ulong() const {
756 return __base::to_ulong();
757 }
758
759 template <size_t _Size>
760 inline _LIBCPP_HIDE_FROM_ABI unsigned long long bitset<_Size>::to_ullong() const {
761 return __base::to_ullong();
762 }
763
764 template <size_t _Size>
765 template <class _CharT, class _Traits, class _Allocator>
766 _LIBCPP_HIDE_FROM_ABI basic_string<_CharT, _Traits, _Allocator>
767 bitset<_Size>::to_string(_CharT __zero, _CharT __one) const {
768 basic_string<_CharT, _Traits, _Allocator> __r(_Size, __zero);
769 for (size_t __i = 0; __i != _Size; ++__i) {
770 if ((*this)[__i])
771 __r[_Size - 1 - __i] = __one;
772 }
773 return __r;
774 }
775
776 template <size_t _Size>
777 template <class _CharT, class _Traits>
778 inline _LIBCPP_HIDE_FROM_ABI basic_string<_CharT, _Traits, allocator<_CharT> >
779 bitset<_Size>::to_string(_CharT __zero, _CharT __one) const {
780 return to_string<_CharT, _Traits, allocator<_CharT> >(__zero, __one);
781 }
782
783 template <size_t _Size>
784 template <class _CharT>
785 inline _LIBCPP_HIDE_FROM_ABI basic_string<_CharT, char_traits<_CharT>, allocator<_CharT> >
786 bitset<_Size>::to_string(_CharT __zero, _CharT __one) const {
787 return to_string<_CharT, char_traits<_CharT>, allocator<_CharT> >(__zero, __one);
788 }
789
790 template <size_t _Size>
791 inline _LIBCPP_HIDE_FROM_ABI basic_string<char, char_traits<char>, allocator<char> >
792 bitset<_Size>::to_string(char __zero, char __one) const {
793 return to_string<char, char_traits<char>, allocator<char> >(__zero, __one);
794 }
795
796 template <size_t _Size>
797 inline _LIBCPP_HIDE_FROM_ABI size_t bitset<_Size>::count() const _NOEXCEPT {
798 return static_cast<size_t>(std::count(__base::__make_iter(0), __base::__make_iter(_Size), true));
799 }
800
801 template <size_t _Size>
802 inline _LIBCPP_HIDE_FROM_ABI bool bitset<_Size>::operator==(const bitset& __rhs) const _NOEXCEPT {
803 return std::equal(__base::__make_iter(0), __base::__make_iter(_Size), __rhs.__make_iter(0));
804 }
805
806 template <size_t _Size>
807 inline _LIBCPP_HIDE_FROM_ABI bool bitset<_Size>::operator!=(const bitset& __rhs) const _NOEXCEPT {
808 return !(*this == __rhs);
809 }
810
811 template <size_t _Size>
812 _LIBCPP_HIDE_FROM_ABI bool bitset<_Size>::test(size_t __pos) const {
813 if (__pos >= _Size)
814 __throw_out_of_range("bitset test argument out of range");
815
816 return (*this)[__pos];
817 }
818
819 template <size_t _Size>
820 inline _LIBCPP_HIDE_FROM_ABI bool bitset<_Size>::all() const _NOEXCEPT {
821 return __base::all();
822 }
823
824 template <size_t _Size>
825 inline _LIBCPP_HIDE_FROM_ABI bool bitset<_Size>::any() const _NOEXCEPT {
826 return __base::any();
827 }
828
829 template <size_t _Size>
830 inline _LIBCPP_HIDE_FROM_ABI bitset<_Size> bitset<_Size>::operator<<(size_t __pos) const _NOEXCEPT {
831 bitset __r = *this;
832 __r <<= __pos;
833 return __r;
834 }
835
836 template <size_t _Size>
837 inline _LIBCPP_HIDE_FROM_ABI bitset<_Size> bitset<_Size>::operator>>(size_t __pos) const _NOEXCEPT {
838 bitset __r = *this;
839 __r >>= __pos;
840 return __r;
841 }
842
843 template <size_t _Size>
844 inline _LIBCPP_HIDE_FROM_ABI bitset<_Size> operator&(const bitset<_Size>& __x, const bitset<_Size>& __y) _NOEXCEPT {
845 bitset<_Size> __r = __x;
846 __r &= __y;
847 return __r;
848 }
849
850 template <size_t _Size>
851 inline _LIBCPP_HIDE_FROM_ABI bitset<_Size> operator|(const bitset<_Size>& __x, const bitset<_Size>& __y) _NOEXCEPT {
852 bitset<_Size> __r = __x;
853 __r |= __y;
854 return __r;
855 }
856
857 template <size_t _Size>
858 inline _LIBCPP_HIDE_FROM_ABI bitset<_Size> operator^(const bitset<_Size>& __x, const bitset<_Size>& __y) _NOEXCEPT {
859 bitset<_Size> __r = __x;
860 __r ^= __y;
861 return __r;
862 }
863
864 template <size_t _Size>
865 struct _LIBCPP_TEMPLATE_VIS hash<bitset<_Size> > : public __unary_function<bitset<_Size>, size_t> {
866 _LIBCPP_HIDE_FROM_ABI size_t operator()(const bitset<_Size>& __bs) const _NOEXCEPT { return __bs.__hash_code(); }
867 };
868
869 template <class _CharT, class _Traits, size_t _Size>
870 _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
871 operator>>(basic_istream<_CharT, _Traits>& __is, bitset<_Size>& __x);
872
873 template <class _CharT, class _Traits, size_t _Size>
874 _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
875 operator<<(basic_ostream<_CharT, _Traits>& __os, const bitset<_Size>& __x);
876
877 _LIBCPP_END_NAMESPACE_STD
878
879 _LIBCPP_POP_MACROS
880
881 #if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES)
882 # include <__cxx03/cstdlib>
883 # include <__cxx03/type_traits>
884 #endif
885
886 #endif // _LIBCPP___CXX03_BITSET