blob: 55b557595d869f827397be9423e2a18d98d6a692 [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>;
162 friend class __bit_array<__bitset>;
163
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
Howard Hinnant90d87232012-07-07 17:04:52171 _LIBCPP_CONSTEXPR __bitset() _NOEXCEPT;
172 explicit _LIBCPP_CONSTEXPR __bitset(unsigned long long __v) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16173
Howard Hinnant10f25d22011-05-27 20:52:28174 _LIBCPP_INLINE_VISIBILITY reference __make_ref(size_t __pos) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16175 {return reference(__first_ + __pos / __bits_per_word, __storage_type(1) << __pos % __bits_per_word);}
Howard Hinnant90d87232012-07-07 17:04:52176 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR const_reference __make_ref(size_t __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16177 {return const_reference(__first_ + __pos / __bits_per_word, __storage_type(1) << __pos % __bits_per_word);}
Howard Hinnant10f25d22011-05-27 20:52:28178 _LIBCPP_INLINE_VISIBILITY iterator __make_iter(size_t __pos) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16179 {return iterator(__first_ + __pos / __bits_per_word, __pos % __bits_per_word);}
Howard Hinnant10f25d22011-05-27 20:52:28180 _LIBCPP_INLINE_VISIBILITY const_iterator __make_iter(size_t __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16181 {return const_iterator(__first_ + __pos / __bits_per_word, __pos % __bits_per_word);}
182
Howard Hinnant10f25d22011-05-27 20:52:28183 void operator&=(const __bitset& __v) _NOEXCEPT;
184 void operator|=(const __bitset& __v) _NOEXCEPT;
185 void operator^=(const __bitset& __v) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16186
Howard Hinnant10f25d22011-05-27 20:52:28187 void flip() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16188 _LIBCPP_INLINE_VISIBILITY unsigned long to_ulong() const
189 {return to_ulong(integral_constant<bool, _Size < sizeof(unsigned long) * CHAR_BIT>());}
190 _LIBCPP_INLINE_VISIBILITY unsigned long long to_ullong() const
191 {return to_ullong(integral_constant<bool, _Size < sizeof(unsigned long long) * CHAR_BIT>());}
192
Howard Hinnant10f25d22011-05-27 20:52:28193 bool all() const _NOEXCEPT;
194 bool any() const _NOEXCEPT;
195 size_t __hash_code() const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16196private:
Howard Hinnant90d87232012-07-07 17:04:52197#ifdef _LIBCPP_HAS_NO_CONSTEXPR
Howard Hinnant10f25d22011-05-27 20:52:28198 void __init(unsigned long long __v, false_type) _NOEXCEPT;
199 void __init(unsigned long long __v, true_type) _NOEXCEPT;
Howard Hinnant90d87232012-07-07 17:04:52200#endif // _LIBCPP_HAS_NO_CONSTEXPR
Howard Hinnantbc8d3f92010-05-11 19:42:16201 unsigned long to_ulong(false_type) const;
202 unsigned long to_ulong(true_type) const;
203 unsigned long long to_ullong(false_type) const;
204 unsigned long long to_ullong(true_type) const;
205 unsigned long long to_ullong(true_type, false_type) const;
206 unsigned long long to_ullong(true_type, true_type) const;
207};
208
209template <size_t _N_words, size_t _Size>
210inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant90d87232012-07-07 17:04:52211_LIBCPP_CONSTEXPR
Howard Hinnant10f25d22011-05-27 20:52:28212__bitset<_N_words, _Size>::__bitset() _NOEXCEPT
Howard Hinnant90d87232012-07-07 17:04:52213#ifndef _LIBCPP_HAS_NO_CONSTEXPR
214 : __first_{0}
215#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16216{
Howard Hinnant90d87232012-07-07 17:04:52217#ifdef _LIBCPP_HAS_NO_CONSTEXPR
Howard Hinnant0949eed2011-06-30 21:18:19218 _VSTD::fill_n(__first_, _N_words, __storage_type(0));
Howard Hinnant90d87232012-07-07 17:04:52219#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16220}
221
Howard Hinnant90d87232012-07-07 17:04:52222#ifdef _LIBCPP_HAS_NO_CONSTEXPR
223
Howard Hinnantbc8d3f92010-05-11 19:42:16224template <size_t _N_words, size_t _Size>
225void
Howard Hinnantec3773c2011-12-01 20:21:04226__bitset<_N_words, _Size>::__init(unsigned long long __v, false_type) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16227{
228 __storage_type __t[sizeof(unsigned long long) / sizeof(__storage_type)];
229 for (size_t __i = 0; __i < sizeof(__t)/sizeof(__t[0]); ++__i, __v >>= __bits_per_word)
230 __t[__i] = static_cast<__storage_type>(__v);
Howard Hinnant0949eed2011-06-30 21:18:19231 _VSTD::copy(__t, __t + sizeof(__t)/sizeof(__t[0]), __first_);
232 _VSTD::fill(__first_ + sizeof(__t)/sizeof(__t[0]), __first_ + sizeof(__first_)/sizeof(__first_[0]),
Howard Hinnantbc8d3f92010-05-11 19:42:16233 __storage_type(0));
234}
235
236template <size_t _N_words, size_t _Size>
237inline _LIBCPP_INLINE_VISIBILITY
238void
Howard Hinnantec3773c2011-12-01 20:21:04239__bitset<_N_words, _Size>::__init(unsigned long long __v, true_type) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16240{
241 __first_[0] = __v;
Howard Hinnant0949eed2011-06-30 21:18:19242 _VSTD::fill(__first_ + 1, __first_ + sizeof(__first_)/sizeof(__first_[0]), __storage_type(0));
Howard Hinnantbc8d3f92010-05-11 19:42:16243}
244
Howard Hinnant90d87232012-07-07 17:04:52245#endif // _LIBCPP_HAS_NO_CONSTEXPR
246
Howard Hinnantbc8d3f92010-05-11 19:42:16247template <size_t _N_words, size_t _Size>
248inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant90d87232012-07-07 17:04:52249_LIBCPP_CONSTEXPR
Howard Hinnant10f25d22011-05-27 20:52:28250__bitset<_N_words, _Size>::__bitset(unsigned long long __v) _NOEXCEPT
Howard Hinnant90d87232012-07-07 17:04:52251#ifndef _LIBCPP_HAS_NO_CONSTEXPR
252 : __first_{__v}
253#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16254{
Howard Hinnant90d87232012-07-07 17:04:52255#ifdef _LIBCPP_HAS_NO_CONSTEXPR
Howard Hinnantbc8d3f92010-05-11 19:42:16256 __init(__v, integral_constant<bool, sizeof(unsigned long long) == sizeof(__storage_type)>());
Howard Hinnant90d87232012-07-07 17:04:52257#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16258}
259
260template <size_t _N_words, size_t _Size>
261inline _LIBCPP_INLINE_VISIBILITY
262void
Howard Hinnant10f25d22011-05-27 20:52:28263__bitset<_N_words, _Size>::operator&=(const __bitset& __v) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16264{
265 for (size_type __i = 0; __i < _N_words; ++__i)
266 __first_[__i] &= __v.__first_[__i];
267}
268
269template <size_t _N_words, size_t _Size>
270inline _LIBCPP_INLINE_VISIBILITY
271void
Howard Hinnant10f25d22011-05-27 20:52:28272__bitset<_N_words, _Size>::operator|=(const __bitset& __v) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16273{
274 for (size_type __i = 0; __i < _N_words; ++__i)
275 __first_[__i] |= __v.__first_[__i];
276}
277
278template <size_t _N_words, size_t _Size>
279inline _LIBCPP_INLINE_VISIBILITY
280void
Howard Hinnant10f25d22011-05-27 20:52:28281__bitset<_N_words, _Size>::operator^=(const __bitset& __v) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16282{
283 for (size_type __i = 0; __i < _N_words; ++__i)
284 __first_[__i] ^= __v.__first_[__i];
285}
286
287template <size_t _N_words, size_t _Size>
288void
Howard Hinnant10f25d22011-05-27 20:52:28289__bitset<_N_words, _Size>::flip() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16290{
291 // do middle whole words
292 size_type __n = _Size;
293 __storage_pointer __p = __first_;
294 for (; __n >= __bits_per_word; ++__p, __n -= __bits_per_word)
295 *__p = ~*__p;
296 // do last partial word
297 if (__n > 0)
298 {
299 __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
300 __storage_type __b = *__p & __m;
301 *__p &= ~__m;
302 *__p |= ~__b & __m;
303 }
304}
305
306template <size_t _N_words, size_t _Size>
307unsigned long
308__bitset<_N_words, _Size>::to_ulong(false_type) const
309{
310 const_iterator __e = __make_iter(_Size);
Howard Hinnant0949eed2011-06-30 21:18:19311 const_iterator __i = _VSTD::find(__make_iter(sizeof(unsigned long) * CHAR_BIT), __e, true);
Howard Hinnantbc8d3f92010-05-11 19:42:16312 if (__i != __e)
313#ifndef _LIBCPP_NO_EXCEPTIONS
314 throw overflow_error("bitset to_ulong overflow error");
315#else
316 assert(!"bitset to_ulong overflow error");
317#endif
318 return __first_[0];
319}
320
321template <size_t _N_words, size_t _Size>
322inline _LIBCPP_INLINE_VISIBILITY
323unsigned long
324__bitset<_N_words, _Size>::to_ulong(true_type) const
325{
326 return __first_[0];
327}
328
329template <size_t _N_words, size_t _Size>
330unsigned long long
331__bitset<_N_words, _Size>::to_ullong(false_type) const
332{
333 const_iterator __e = __make_iter(_Size);
Howard Hinnant0949eed2011-06-30 21:18:19334 const_iterator __i = _VSTD::find(__make_iter(sizeof(unsigned long long) * CHAR_BIT), __e, true);
Howard Hinnantbc8d3f92010-05-11 19:42:16335 if (__i != __e)
336#ifndef _LIBCPP_NO_EXCEPTIONS
337 throw overflow_error("bitset to_ullong overflow error");
338#else
339 assert(!"bitset to_ullong overflow error");
340#endif
341 return to_ullong(true_type());
342}
343
344template <size_t _N_words, size_t _Size>
345inline _LIBCPP_INLINE_VISIBILITY
346unsigned long long
347__bitset<_N_words, _Size>::to_ullong(true_type) const
348{
349 return to_ullong(true_type(), integral_constant<bool, sizeof(__storage_type) < sizeof(unsigned long long)>());
350}
351
352template <size_t _N_words, size_t _Size>
353inline _LIBCPP_INLINE_VISIBILITY
354unsigned long long
355__bitset<_N_words, _Size>::to_ullong(true_type, false_type) const
356{
357 return __first_[0];
358}
359
360template <size_t _N_words, size_t _Size>
361unsigned long long
362__bitset<_N_words, _Size>::to_ullong(true_type, true_type) const
363{
364 unsigned long long __r = __first_[0];
365 for (std::size_t __i = 1; __i < sizeof(unsigned long long) / sizeof(__storage_type); ++__i)
366 __r |= static_cast<unsigned long long>(__first_[__i]) << (sizeof(__storage_type) * CHAR_BIT);
367 return __r;
368}
369
370template <size_t _N_words, size_t _Size>
371bool
Howard Hinnant10f25d22011-05-27 20:52:28372__bitset<_N_words, _Size>::all() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16373{
374 // do middle whole words
375 size_type __n = _Size;
376 __const_storage_pointer __p = __first_;
377 for (; __n >= __bits_per_word; ++__p, __n -= __bits_per_word)
378 if (~*__p)
379 return false;
380 // do last partial word
381 if (__n > 0)
382 {
383 __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
384 if (~*__p & __m)
385 return false;
386 }
387 return true;
388}
389
390template <size_t _N_words, size_t _Size>
391bool
Howard Hinnant10f25d22011-05-27 20:52:28392__bitset<_N_words, _Size>::any() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16393{
394 // do middle whole words
395 size_type __n = _Size;
396 __const_storage_pointer __p = __first_;
397 for (; __n >= __bits_per_word; ++__p, __n -= __bits_per_word)
398 if (*__p)
399 return true;
400 // do last partial word
401 if (__n > 0)
402 {
403 __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
404 if (*__p & __m)
405 return true;
406 }
407 return false;
408}
409
410template <size_t _N_words, size_t _Size>
411inline _LIBCPP_INLINE_VISIBILITY
412size_t
Howard Hinnant10f25d22011-05-27 20:52:28413__bitset<_N_words, _Size>::__hash_code() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16414{
415 size_t __h = 0;
416 for (size_type __i = 0; __i < _N_words; ++__i)
417 __h ^= __first_[__i];
418 return __h;
419}
420
421template <size_t _Size>
422class __bitset<1, _Size>
423{
424public:
425 typedef ptrdiff_t difference_type;
426 typedef size_t size_type;
Howard Hinnantf867f632012-05-07 16:50:38427 typedef size_type __storage_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16428protected:
429 typedef __bitset __self;
Howard Hinnantbc8d3f92010-05-11 19:42:16430 typedef __storage_type* __storage_pointer;
431 typedef const __storage_type* __const_storage_pointer;
432 static const unsigned __bits_per_word = static_cast<unsigned>(sizeof(__storage_type) * CHAR_BIT);
433
434 friend class __bit_reference<__bitset>;
435 friend class __bit_const_reference<__bitset>;
436 friend class __bit_iterator<__bitset, false>;
437 friend class __bit_iterator<__bitset, true>;
438 friend class __bit_array<__bitset>;
439
440 __storage_type __first_;
441
442 typedef __bit_reference<__bitset> reference;
443 typedef __bit_const_reference<__bitset> const_reference;
444 typedef __bit_iterator<__bitset, false> iterator;
445 typedef __bit_iterator<__bitset, true> const_iterator;
446
Howard Hinnant90d87232012-07-07 17:04:52447 _LIBCPP_CONSTEXPR __bitset() _NOEXCEPT;
448 explicit _LIBCPP_CONSTEXPR __bitset(unsigned long long __v) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16449
Howard Hinnant10f25d22011-05-27 20:52:28450 _LIBCPP_INLINE_VISIBILITY reference __make_ref(size_t __pos) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16451 {return reference(&__first_, __storage_type(1) << __pos);}
Howard Hinnant90d87232012-07-07 17:04:52452 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR const_reference __make_ref(size_t __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16453 {return const_reference(&__first_, __storage_type(1) << __pos);}
Howard Hinnant10f25d22011-05-27 20:52:28454 _LIBCPP_INLINE_VISIBILITY iterator __make_iter(size_t __pos) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16455 {return iterator(&__first_ + __pos / __bits_per_word, __pos % __bits_per_word);}
Howard Hinnant10f25d22011-05-27 20:52:28456 _LIBCPP_INLINE_VISIBILITY const_iterator __make_iter(size_t __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16457 {return const_iterator(&__first_ + __pos / __bits_per_word, __pos % __bits_per_word);}
458
Howard Hinnant10f25d22011-05-27 20:52:28459 void operator&=(const __bitset& __v) _NOEXCEPT;
460 void operator|=(const __bitset& __v) _NOEXCEPT;
461 void operator^=(const __bitset& __v) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16462
Howard Hinnant10f25d22011-05-27 20:52:28463 void flip() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16464
465 unsigned long to_ulong() const;
466 unsigned long long to_ullong() const;
467
Howard Hinnant10f25d22011-05-27 20:52:28468 bool all() const _NOEXCEPT;
469 bool any() const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16470
Howard Hinnant10f25d22011-05-27 20:52:28471 size_t __hash_code() const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16472};
473
474template <size_t _Size>
475inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant90d87232012-07-07 17:04:52476_LIBCPP_CONSTEXPR
Howard Hinnant10f25d22011-05-27 20:52:28477__bitset<1, _Size>::__bitset() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16478 : __first_(0)
479{
480}
481
482template <size_t _Size>
483inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant90d87232012-07-07 17:04:52484_LIBCPP_CONSTEXPR
Howard Hinnant10f25d22011-05-27 20:52:28485__bitset<1, _Size>::__bitset(unsigned long long __v) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16486 : __first_(static_cast<__storage_type>(__v))
487{
488}
489
490template <size_t _Size>
491inline _LIBCPP_INLINE_VISIBILITY
492void
Howard Hinnant10f25d22011-05-27 20:52:28493__bitset<1, _Size>::operator&=(const __bitset& __v) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16494{
495 __first_ &= __v.__first_;
496}
497
498template <size_t _Size>
499inline _LIBCPP_INLINE_VISIBILITY
500void
Howard Hinnant10f25d22011-05-27 20:52:28501__bitset<1, _Size>::operator|=(const __bitset& __v) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16502{
503 __first_ |= __v.__first_;
504}
505
506template <size_t _Size>
507inline _LIBCPP_INLINE_VISIBILITY
508void
Howard Hinnant10f25d22011-05-27 20:52:28509__bitset<1, _Size>::operator^=(const __bitset& __v) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16510{
511 __first_ ^= __v.__first_;
512}
513
514template <size_t _Size>
515inline _LIBCPP_INLINE_VISIBILITY
516void
Howard Hinnant10f25d22011-05-27 20:52:28517__bitset<1, _Size>::flip() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16518{
519 __storage_type __m = ~__storage_type(0) >> (__bits_per_word - _Size);
520 __first_ = ~__first_;
521 __first_ &= __m;
522}
523
524template <size_t _Size>
525inline _LIBCPP_INLINE_VISIBILITY
526unsigned long
527__bitset<1, _Size>::to_ulong() const
528{
529 return __first_;
530}
531
532template <size_t _Size>
533inline _LIBCPP_INLINE_VISIBILITY
534unsigned long long
535__bitset<1, _Size>::to_ullong() const
536{
537 return __first_;
538}
539
540template <size_t _Size>
541inline _LIBCPP_INLINE_VISIBILITY
542bool
Howard Hinnant10f25d22011-05-27 20:52:28543__bitset<1, _Size>::all() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16544{
545 __storage_type __m = ~__storage_type(0) >> (__bits_per_word - _Size);
546 return !(~__first_ & __m);
547}
548
549template <size_t _Size>
550inline _LIBCPP_INLINE_VISIBILITY
551bool
Howard Hinnant10f25d22011-05-27 20:52:28552__bitset<1, _Size>::any() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16553{
554 __storage_type __m = ~__storage_type(0) >> (__bits_per_word - _Size);
555 return __first_ & __m;
556}
557
558template <size_t _Size>
559inline _LIBCPP_INLINE_VISIBILITY
560size_t
Howard Hinnant10f25d22011-05-27 20:52:28561__bitset<1, _Size>::__hash_code() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16562{
563 return __first_;
564}
565
566template <>
567class __bitset<0, 0>
568{
569public:
570 typedef ptrdiff_t difference_type;
571 typedef size_t size_type;
Howard Hinnantf867f632012-05-07 16:50:38572 typedef size_type __storage_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16573protected:
574 typedef __bitset __self;
Howard Hinnantbc8d3f92010-05-11 19:42:16575 typedef __storage_type* __storage_pointer;
576 typedef const __storage_type* __const_storage_pointer;
577 static const unsigned __bits_per_word = static_cast<unsigned>(sizeof(__storage_type) * CHAR_BIT);
578
579 friend class __bit_reference<__bitset>;
580 friend class __bit_const_reference<__bitset>;
581 friend class __bit_iterator<__bitset, false>;
582 friend class __bit_iterator<__bitset, true>;
Howard Hinnantec3773c2011-12-01 20:21:04583 friend struct __bit_array<__bitset>;
Howard Hinnantbc8d3f92010-05-11 19:42:16584
585 typedef __bit_reference<__bitset> reference;
586 typedef __bit_const_reference<__bitset> const_reference;
587 typedef __bit_iterator<__bitset, false> iterator;
588 typedef __bit_iterator<__bitset, true> const_iterator;
589
Howard Hinnant90d87232012-07-07 17:04:52590 _LIBCPP_CONSTEXPR __bitset() _NOEXCEPT;
591 explicit _LIBCPP_CONSTEXPR __bitset(unsigned long long) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16592
Howard Hinnant10f25d22011-05-27 20:52:28593 _LIBCPP_INLINE_VISIBILITY reference __make_ref(size_t) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16594 {return reference(0, 1);}
Howard Hinnant90d87232012-07-07 17:04:52595 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR const_reference __make_ref(size_t) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16596 {return const_reference(0, 1);}
Howard Hinnantec3773c2011-12-01 20:21:04597 _LIBCPP_INLINE_VISIBILITY iterator __make_iter(size_t) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16598 {return iterator(0, 0);}
Howard Hinnantec3773c2011-12-01 20:21:04599 _LIBCPP_INLINE_VISIBILITY const_iterator __make_iter(size_t) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16600 {return const_iterator(0, 0);}
601
Howard Hinnant10f25d22011-05-27 20:52:28602 _LIBCPP_INLINE_VISIBILITY void operator&=(const __bitset&) _NOEXCEPT {}
603 _LIBCPP_INLINE_VISIBILITY void operator|=(const __bitset&) _NOEXCEPT {}
604 _LIBCPP_INLINE_VISIBILITY void operator^=(const __bitset&) _NOEXCEPT {}
Howard Hinnantbc8d3f92010-05-11 19:42:16605
Howard Hinnant10f25d22011-05-27 20:52:28606 _LIBCPP_INLINE_VISIBILITY void flip() _NOEXCEPT {}
Howard Hinnantbc8d3f92010-05-11 19:42:16607
608 _LIBCPP_INLINE_VISIBILITY unsigned long to_ulong() const {return 0;}
609 _LIBCPP_INLINE_VISIBILITY unsigned long long to_ullong() const {return 0;}
610
Howard Hinnant10f25d22011-05-27 20:52:28611 _LIBCPP_INLINE_VISIBILITY bool all() const _NOEXCEPT {return true;}
612 _LIBCPP_INLINE_VISIBILITY bool any() const _NOEXCEPT {return false;}
Howard Hinnantbc8d3f92010-05-11 19:42:16613
Howard Hinnant10f25d22011-05-27 20:52:28614 _LIBCPP_INLINE_VISIBILITY size_t __hash_code() const _NOEXCEPT {return 0;}
Howard Hinnantbc8d3f92010-05-11 19:42:16615};
616
617inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant90d87232012-07-07 17:04:52618_LIBCPP_CONSTEXPR
Howard Hinnant10f25d22011-05-27 20:52:28619__bitset<0, 0>::__bitset() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16620{
621}
622
623inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant90d87232012-07-07 17:04:52624_LIBCPP_CONSTEXPR
Howard Hinnant10f25d22011-05-27 20:52:28625__bitset<0, 0>::__bitset(unsigned long long) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16626{
627}
628
629template <size_t _Size> class bitset;
630template <size_t _Size> struct hash<bitset<_Size> >;
631
632template <size_t _Size>
Howard Hinnant422a53f2010-09-21 21:28:23633class _LIBCPP_VISIBLE bitset
Howard Hinnantbc8d3f92010-05-11 19:42:16634 : private __bitset<_Size == 0 ? 0 : (_Size - 1) / (sizeof(size_t) * CHAR_BIT) + 1, _Size>
635{
636 static const unsigned __n_words = _Size == 0 ? 0 : (_Size - 1) / (sizeof(size_t) * CHAR_BIT) + 1;
637 typedef __bitset<__n_words, _Size> base;
638
Howard Hinnant324bb032010-08-22 00:02:43639public:
Howard Hinnantbc8d3f92010-05-11 19:42:16640 typedef typename base::reference reference;
641 typedef typename base::const_reference const_reference;
642
Howard Hinnant324bb032010-08-22 00:02:43643 // 23.3.5.1 constructors:
Howard Hinnant90d87232012-07-07 17:04:52644 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR bitset() _NOEXCEPT {}
645 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
646 bitset(unsigned long long __v) _NOEXCEPT : base(__v) {}
Howard Hinnant34d6b192010-11-17 21:53:14647 template<class _CharT>
648 explicit bitset(const _CharT* __str,
649 typename basic_string<_CharT>::size_type __n = basic_string<_CharT>::npos,
650 _CharT __zero = _CharT('0'), _CharT __one = _CharT('1'));
Howard Hinnant324bb032010-08-22 00:02:43651 template<class _CharT, class _Traits, class _Allocator>
652 explicit bitset(const basic_string<_CharT,_Traits,_Allocator>& __str,
653 typename basic_string<_CharT,_Traits,_Allocator>::size_type __pos = 0,
654 typename basic_string<_CharT,_Traits,_Allocator>::size_type __n =
Howard Hinnantbc8d3f92010-05-11 19:42:16655 (basic_string<_CharT,_Traits,_Allocator>::npos),
Howard Hinnant324bb032010-08-22 00:02:43656 _CharT __zero = _CharT('0'), _CharT __one = _CharT('1'));
Howard Hinnantbc8d3f92010-05-11 19:42:16657
Howard Hinnant324bb032010-08-22 00:02:43658 // 23.3.5.2 bitset operations:
Howard Hinnant10f25d22011-05-27 20:52:28659 bitset& operator&=(const bitset& __rhs) _NOEXCEPT;
660 bitset& operator|=(const bitset& __rhs) _NOEXCEPT;
661 bitset& operator^=(const bitset& __rhs) _NOEXCEPT;
662 bitset& operator<<=(size_t __pos) _NOEXCEPT;
663 bitset& operator>>=(size_t __pos) _NOEXCEPT;
664 bitset& set() _NOEXCEPT;
Howard Hinnant324bb032010-08-22 00:02:43665 bitset& set(size_t __pos, bool __val = true);
Howard Hinnant10f25d22011-05-27 20:52:28666 bitset& reset() _NOEXCEPT;
Howard Hinnant324bb032010-08-22 00:02:43667 bitset& reset(size_t __pos);
Howard Hinnant10f25d22011-05-27 20:52:28668 bitset operator~() const _NOEXCEPT;
669 bitset& flip() _NOEXCEPT;
Howard Hinnant324bb032010-08-22 00:02:43670 bitset& flip(size_t __pos);
Howard Hinnantbc8d3f92010-05-11 19:42:16671
Howard Hinnant324bb032010-08-22 00:02:43672 // element access:
Howard Hinnant90d87232012-07-07 17:04:52673 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
674 const_reference operator[](size_t __p) const {return base::__make_ref(__p);}
Howard Hinnantbc8d3f92010-05-11 19:42:16675 _LIBCPP_INLINE_VISIBILITY reference operator[](size_t __p) {return base::__make_ref(__p);}
Howard Hinnant324bb032010-08-22 00:02:43676 unsigned long to_ulong() const;
677 unsigned long long to_ullong() const;
678 template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16679 basic_string<_CharT, _Traits, _Allocator> to_string(_CharT __zero = _CharT('0'),
Howard Hinnant324bb032010-08-22 00:02:43680 _CharT __one = _CharT('1')) const;
681 template <class _CharT, class _Traits>
Howard Hinnantbc8d3f92010-05-11 19:42:16682 basic_string<_CharT, _Traits, allocator<_CharT> > to_string(_CharT __zero = _CharT('0'),
Howard Hinnant324bb032010-08-22 00:02:43683 _CharT __one = _CharT('1')) const;
684 template <class _CharT>
Howard Hinnantbc8d3f92010-05-11 19:42:16685 basic_string<_CharT, char_traits<_CharT>, allocator<_CharT> > to_string(_CharT __zero = _CharT('0'),
Howard Hinnant324bb032010-08-22 00:02:43686 _CharT __one = _CharT('1')) const;
Howard Hinnantbc8d3f92010-05-11 19:42:16687 basic_string<char, char_traits<char>, allocator<char> > to_string(char __zero = '0',
Howard Hinnant324bb032010-08-22 00:02:43688 char __one = '1') const;
Howard Hinnant10f25d22011-05-27 20:52:28689 size_t count() const _NOEXCEPT;
Howard Hinnant90d87232012-07-07 17:04:52690 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR size_t size() const _NOEXCEPT {return _Size;}
Howard Hinnant10f25d22011-05-27 20:52:28691 bool operator==(const bitset& __rhs) const _NOEXCEPT;
692 bool operator!=(const bitset& __rhs) const _NOEXCEPT;
Howard Hinnant324bb032010-08-22 00:02:43693 bool test(size_t __pos) const;
Howard Hinnant10f25d22011-05-27 20:52:28694 bool all() const _NOEXCEPT;
695 bool any() const _NOEXCEPT;
696 _LIBCPP_INLINE_VISIBILITY bool none() const _NOEXCEPT {return !any();}
697 bitset operator<<(size_t __pos) const _NOEXCEPT;
698 bitset operator>>(size_t __pos) const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16699
700private:
701
Howard Hinnant422a53f2010-09-21 21:28:23702 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant10f25d22011-05-27 20:52:28703 size_t __hash_code() const _NOEXCEPT {return base::__hash_code();}
Howard Hinnantbc8d3f92010-05-11 19:42:16704
705 friend struct hash<bitset>;
706};
707
708template <size_t _Size>
Howard Hinnant34d6b192010-11-17 21:53:14709template<class _CharT>
710bitset<_Size>::bitset(const _CharT* __str,
711 typename basic_string<_CharT>::size_type __n,
712 _CharT __zero, _CharT __one)
Howard Hinnantbc8d3f92010-05-11 19:42:16713{
Howard Hinnant0949eed2011-06-30 21:18:19714 size_t __rlen = _VSTD::min(__n, char_traits<_CharT>::length(__str));
Howard Hinnantbc8d3f92010-05-11 19:42:16715 for (size_t __i = 0; __i < __rlen; ++__i)
Howard Hinnant34d6b192010-11-17 21:53:14716 if (__str[__i] != __zero && __str[__i] != __one)
Howard Hinnantbc8d3f92010-05-11 19:42:16717#ifndef _LIBCPP_NO_EXCEPTIONS
718 throw invalid_argument("bitset string ctor has invalid argument");
719#else
720 assert(!"bitset string ctor has invalid argument");
721#endif
Howard Hinnant99968442011-11-29 18:15:50722 size_t _Mp = _VSTD::min(__rlen, _Size);
Howard Hinnantbc8d3f92010-05-11 19:42:16723 size_t __i = 0;
Howard Hinnant99968442011-11-29 18:15:50724 for (; __i < _Mp; ++__i)
Howard Hinnantbc8d3f92010-05-11 19:42:16725 {
Howard Hinnant99968442011-11-29 18:15:50726 _CharT __c = __str[_Mp - 1 - __i];
Howard Hinnant34d6b192010-11-17 21:53:14727 if (__c == __zero)
Howard Hinnantbc8d3f92010-05-11 19:42:16728 (*this)[__i] = false;
Howard Hinnant34d6b192010-11-17 21:53:14729 else
Howard Hinnantbc8d3f92010-05-11 19:42:16730 (*this)[__i] = true;
Howard Hinnantbc8d3f92010-05-11 19:42:16731 }
Howard Hinnant0949eed2011-06-30 21:18:19732 _VSTD::fill(base::__make_iter(__i), base::__make_iter(_Size), false);
Howard Hinnantbc8d3f92010-05-11 19:42:16733}
734
735template <size_t _Size>
Howard Hinnant324bb032010-08-22 00:02:43736template<class _CharT, class _Traits, class _Allocator>
737bitset<_Size>::bitset(const basic_string<_CharT,_Traits,_Allocator>& __str,
738 typename basic_string<_CharT,_Traits,_Allocator>::size_type __pos,
Howard Hinnantbc8d3f92010-05-11 19:42:16739 typename basic_string<_CharT,_Traits,_Allocator>::size_type __n,
740 _CharT __zero, _CharT __one)
741{
742 if (__pos > __str.size())
743#ifndef _LIBCPP_NO_EXCEPTIONS
744 throw out_of_range("bitset string pos out of range");
745#else
746 assert(!"bitset string pos out of range");
747#endif
Howard Hinnant0949eed2011-06-30 21:18:19748 size_t __rlen = _VSTD::min(__n, __str.size() - __pos);
Howard Hinnantbc8d3f92010-05-11 19:42:16749 for (size_t __i = __pos; __i < __pos + __rlen; ++__i)
750 if (!_Traits::eq(__str[__i], __zero) && !_Traits::eq(__str[__i], __one))
751#ifndef _LIBCPP_NO_EXCEPTIONS
752 throw invalid_argument("bitset string ctor has invalid argument");
753#else
754 assert(!"bitset string ctor has invalid argument");
755#endif
Howard Hinnant99968442011-11-29 18:15:50756 size_t _Mp = _VSTD::min(__rlen, _Size);
Howard Hinnantbc8d3f92010-05-11 19:42:16757 size_t __i = 0;
Howard Hinnant99968442011-11-29 18:15:50758 for (; __i < _Mp; ++__i)
Howard Hinnantbc8d3f92010-05-11 19:42:16759 {
Howard Hinnant99968442011-11-29 18:15:50760 _CharT __c = __str[__pos + _Mp - 1 - __i];
Howard Hinnantbc8d3f92010-05-11 19:42:16761 if (_Traits::eq(__c, __zero))
762 (*this)[__i] = false;
763 else
764 (*this)[__i] = true;
765 }
Howard Hinnant0949eed2011-06-30 21:18:19766 _VSTD::fill(base::__make_iter(__i), base::__make_iter(_Size), false);
Howard Hinnantbc8d3f92010-05-11 19:42:16767}
768
769template <size_t _Size>
770inline _LIBCPP_INLINE_VISIBILITY
771bitset<_Size>&
Howard Hinnant10f25d22011-05-27 20:52:28772bitset<_Size>::operator&=(const bitset& __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16773{
774 base::operator&=(__rhs);
775 return *this;
776}
777
778template <size_t _Size>
779inline _LIBCPP_INLINE_VISIBILITY
780bitset<_Size>&
Howard Hinnant10f25d22011-05-27 20:52:28781bitset<_Size>::operator|=(const bitset& __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16782{
783 base::operator|=(__rhs);
784 return *this;
785}
786
787template <size_t _Size>
788inline _LIBCPP_INLINE_VISIBILITY
789bitset<_Size>&
Howard Hinnant10f25d22011-05-27 20:52:28790bitset<_Size>::operator^=(const bitset& __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16791{
792 base::operator^=(__rhs);
793 return *this;
794}
795
796template <size_t _Size>
797bitset<_Size>&
Howard Hinnant10f25d22011-05-27 20:52:28798bitset<_Size>::operator<<=(size_t __pos) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16799{
Howard Hinnant0949eed2011-06-30 21:18:19800 __pos = _VSTD::min(__pos, _Size);
801 _VSTD::copy_backward(base::__make_iter(0), base::__make_iter(_Size - __pos), base::__make_iter(_Size));
802 _VSTD::fill_n(base::__make_iter(0), __pos, false);
Howard Hinnantbc8d3f92010-05-11 19:42:16803 return *this;
804}
805
806template <size_t _Size>
807bitset<_Size>&
Howard Hinnant10f25d22011-05-27 20:52:28808bitset<_Size>::operator>>=(size_t __pos) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16809{
Howard Hinnant0949eed2011-06-30 21:18:19810 __pos = _VSTD::min(__pos, _Size);
811 _VSTD::copy(base::__make_iter(__pos), base::__make_iter(_Size), base::__make_iter(0));
812 _VSTD::fill_n(base::__make_iter(_Size - __pos), __pos, false);
Howard Hinnantbc8d3f92010-05-11 19:42:16813 return *this;
814}
815
816template <size_t _Size>
817inline _LIBCPP_INLINE_VISIBILITY
818bitset<_Size>&
Howard Hinnant10f25d22011-05-27 20:52:28819bitset<_Size>::set() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16820{
Howard Hinnant0949eed2011-06-30 21:18:19821 _VSTD::fill_n(base::__make_iter(0), _Size, true);
Howard Hinnantbc8d3f92010-05-11 19:42:16822 return *this;
823}
824
825template <size_t _Size>
826bitset<_Size>&
827bitset<_Size>::set(size_t __pos, bool __val)
828{
829 if (__pos >= _Size)
830#ifndef _LIBCPP_NO_EXCEPTIONS
831 throw out_of_range("bitset set argument out of range");
832#else
833 assert(!"bitset set argument out of range");
834#endif
835 (*this)[__pos] = __val;
836 return *this;
837}
838
839template <size_t _Size>
840inline _LIBCPP_INLINE_VISIBILITY
841bitset<_Size>&
Howard Hinnant10f25d22011-05-27 20:52:28842bitset<_Size>::reset() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16843{
Howard Hinnant0949eed2011-06-30 21:18:19844 _VSTD::fill_n(base::__make_iter(0), _Size, false);
Howard Hinnantbc8d3f92010-05-11 19:42:16845 return *this;
846}
847
848template <size_t _Size>
849bitset<_Size>&
850bitset<_Size>::reset(size_t __pos)
851{
852 if (__pos >= _Size)
853#ifndef _LIBCPP_NO_EXCEPTIONS
854 throw out_of_range("bitset reset argument out of range");
855#else
856 assert(!"bitset reset argument out of range");
857#endif
858 (*this)[__pos] = false;
859 return *this;
860}
861
862template <size_t _Size>
863inline _LIBCPP_INLINE_VISIBILITY
864bitset<_Size>
Howard Hinnant10f25d22011-05-27 20:52:28865bitset<_Size>::operator~() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16866{
867 bitset __x(*this);
868 __x.flip();
869 return __x;
870}
871
872template <size_t _Size>
873inline _LIBCPP_INLINE_VISIBILITY
874bitset<_Size>&
Howard Hinnant10f25d22011-05-27 20:52:28875bitset<_Size>::flip() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16876{
877 base::flip();
878 return *this;
879}
880
881template <size_t _Size>
882bitset<_Size>&
883bitset<_Size>::flip(size_t __pos)
884{
885 if (__pos >= _Size)
886#ifndef _LIBCPP_NO_EXCEPTIONS
887 throw out_of_range("bitset flip argument out of range");
888#else
889 assert(!"bitset flip argument out of range");
890#endif
891 reference r = base::__make_ref(__pos);
892 r = ~r;
893 return *this;
894}
895
896template <size_t _Size>
897inline _LIBCPP_INLINE_VISIBILITY
898unsigned long
899bitset<_Size>::to_ulong() const
900{
901 return base::to_ulong();
902}
903
904template <size_t _Size>
905inline _LIBCPP_INLINE_VISIBILITY
906unsigned long long
907bitset<_Size>::to_ullong() const
908{
909 return base::to_ullong();
910}
911
912template <size_t _Size>
Howard Hinnant324bb032010-08-22 00:02:43913template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16914basic_string<_CharT, _Traits, _Allocator>
915bitset<_Size>::to_string(_CharT __zero, _CharT __one) const
916{
917 basic_string<_CharT, _Traits, _Allocator> __r(_Size, __zero);
918 for (size_t __i = 0; __i < _Size; ++__i)
919 {
920 if ((*this)[__i])
921 __r[_Size - 1 - __i] = __one;
922 }
923 return __r;
924}
925
926template <size_t _Size>
Howard Hinnant324bb032010-08-22 00:02:43927template <class _CharT, class _Traits>
Howard Hinnantbc8d3f92010-05-11 19:42:16928inline _LIBCPP_INLINE_VISIBILITY
929basic_string<_CharT, _Traits, allocator<_CharT> >
930bitset<_Size>::to_string(_CharT __zero, _CharT __one) const
931{
932 return to_string<_CharT, _Traits, allocator<_CharT> >(__zero, __one);
933}
934
935template <size_t _Size>
Howard Hinnant324bb032010-08-22 00:02:43936template <class _CharT>
Howard Hinnantbc8d3f92010-05-11 19:42:16937inline _LIBCPP_INLINE_VISIBILITY
938basic_string<_CharT, char_traits<_CharT>, allocator<_CharT> >
939bitset<_Size>::to_string(_CharT __zero, _CharT __one) const
940{
941 return to_string<_CharT, char_traits<_CharT>, allocator<_CharT> >(__zero, __one);
942}
943
944template <size_t _Size>
945inline _LIBCPP_INLINE_VISIBILITY
946basic_string<char, char_traits<char>, allocator<char> >
947bitset<_Size>::to_string(char __zero, char __one) const
948{
949 return to_string<char, char_traits<char>, allocator<char> >(__zero, __one);
950}
951
952template <size_t _Size>
953inline _LIBCPP_INLINE_VISIBILITY
954size_t
Howard Hinnant10f25d22011-05-27 20:52:28955bitset<_Size>::count() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16956{
Howard Hinnant0949eed2011-06-30 21:18:19957 return static_cast<size_t>(_VSTD::count(base::__make_iter(0), base::__make_iter(_Size), true));
Howard Hinnantbc8d3f92010-05-11 19:42:16958}
959
960template <size_t _Size>
961inline _LIBCPP_INLINE_VISIBILITY
962bool
Howard Hinnant10f25d22011-05-27 20:52:28963bitset<_Size>::operator==(const bitset& __rhs) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16964{
Howard Hinnant0949eed2011-06-30 21:18:19965 return _VSTD::equal(base::__make_iter(0), base::__make_iter(_Size), __rhs.__make_iter(0));
Howard Hinnantbc8d3f92010-05-11 19:42:16966}
967
968template <size_t _Size>
969inline _LIBCPP_INLINE_VISIBILITY
970bool
Howard Hinnant10f25d22011-05-27 20:52:28971bitset<_Size>::operator!=(const bitset& __rhs) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16972{
973 return !(*this == __rhs);
974}
975
976template <size_t _Size>
977bool
978bitset<_Size>::test(size_t __pos) const
979{
980 if (__pos >= _Size)
981#ifndef _LIBCPP_NO_EXCEPTIONS
982 throw out_of_range("bitset test argument out of range");
983#else
984 assert(!"bitset test argument out of range");
985#endif
986 return (*this)[__pos];
987}
988
989template <size_t _Size>
990inline _LIBCPP_INLINE_VISIBILITY
991bool
Howard Hinnant10f25d22011-05-27 20:52:28992bitset<_Size>::all() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16993{
994 return base::all();
995}
996
997template <size_t _Size>
998inline _LIBCPP_INLINE_VISIBILITY
999bool
Howard Hinnant10f25d22011-05-27 20:52:281000bitset<_Size>::any() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:161001{
1002 return base::any();
1003}
1004
1005template <size_t _Size>
1006inline _LIBCPP_INLINE_VISIBILITY
1007bitset<_Size>
Howard Hinnant10f25d22011-05-27 20:52:281008bitset<_Size>::operator<<(size_t __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:161009{
1010 bitset __r = *this;
1011 __r <<= __pos;
1012 return __r;
1013}
1014
1015template <size_t _Size>
1016inline _LIBCPP_INLINE_VISIBILITY
1017bitset<_Size>
Howard Hinnant10f25d22011-05-27 20:52:281018bitset<_Size>::operator>>(size_t __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:161019{
1020 bitset __r = *this;
1021 __r >>= __pos;
1022 return __r;
1023}
1024
1025template <size_t _Size>
1026inline _LIBCPP_INLINE_VISIBILITY
1027bitset<_Size>
Howard Hinnant10f25d22011-05-27 20:52:281028operator&(const bitset<_Size>& __x, const bitset<_Size>& __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:161029{
1030 bitset<_Size> __r = __x;
1031 __r &= __y;
1032 return __r;
1033}
1034
1035template <size_t _Size>
1036inline _LIBCPP_INLINE_VISIBILITY
1037bitset<_Size>
Howard Hinnant10f25d22011-05-27 20:52:281038operator|(const bitset<_Size>& __x, const bitset<_Size>& __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:161039{
1040 bitset<_Size> __r = __x;
1041 __r |= __y;
1042 return __r;
1043}
1044
1045template <size_t _Size>
1046inline _LIBCPP_INLINE_VISIBILITY
1047bitset<_Size>
Howard Hinnant10f25d22011-05-27 20:52:281048operator^(const bitset<_Size>& __x, const bitset<_Size>& __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:161049{
1050 bitset<_Size> __r = __x;
1051 __r ^= __y;
1052 return __r;
1053}
1054
1055template <size_t _Size>
Howard Hinnant422a53f2010-09-21 21:28:231056struct _LIBCPP_VISIBLE hash<bitset<_Size> >
Howard Hinnantbc8d3f92010-05-11 19:42:161057 : public unary_function<bitset<_Size>, size_t>
1058{
Howard Hinnant422a53f2010-09-21 21:28:231059 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant10f25d22011-05-27 20:52:281060 size_t operator()(const bitset<_Size>& __bs) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:161061 {return __bs.__hash_code();}
1062};
1063
Howard Hinnant464aa5c2011-07-18 15:51:591064template <class _CharT, class _Traits, size_t _Size>
1065basic_istream<_CharT, _Traits>&
1066operator>>(basic_istream<_CharT, _Traits>& __is, bitset<_Size>& __x);
1067
1068template <class _CharT, class _Traits, size_t _Size>
1069basic_ostream<_CharT, _Traits>&
1070operator<<(basic_ostream<_CharT, _Traits>& __os, const bitset<_Size>& __x);
1071
Howard Hinnantbc8d3f92010-05-11 19:42:161072_LIBCPP_END_NAMESPACE_STD
1073
1074#endif // _LIBCPP_BITSET