blob: 0c40b9b79d956261d030ad40767cbea4eeca2ce0 [file] [log] [blame]
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001// -*- C++ -*-
2//===---------------------------- bitset ----------------------------------===//
3//
Howard Hinnantf5256e12010-05-11 21:36:01 +00004// The LLVM Compiler Infrastructure
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005//
Howard Hinnantb64f8b02010-11-16 22:09:02 +00006// 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:16 +00008//
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:28 +000030 reference() noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000031 public:
Howard Hinnant10f25d22011-05-27 20:52:28 +000032 ~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:16 +000038 };
39
40 // 23.3.5.1 constructors:
Howard Hinnant10f25d22011-05-27 20:52:28 +000041 constexpr bitset() noexcept;
42 constexpr bitset(unsigned long long val) noexcept;
Howard Hinnant34d6b192010-11-17 21:53:14 +000043 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:16 +000047 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:28 +000055 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:16 +000061 bitset& set(size_t pos, bool val = true);
Howard Hinnant10f25d22011-05-27 20:52:28 +000062 bitset& reset() noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000063 bitset& reset(size_t pos);
Howard Hinnant10f25d22011-05-27 20:52:28 +000064 bitset operator~() const noexcept;
65 bitset& flip() noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000066 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:28 +000080 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:16 +000084 bool test(size_t pos) const;
Howard Hinnant10f25d22011-05-27 20:52:28 +000085 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:16 +000090};
91
92// 23.3.5.3 bitset operators:
93template <size_t N>
Howard Hinnant10f25d22011-05-27 20:52:28 +000094bitset<N> operator&(const bitset<N>&, const bitset<N>&) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000095
96template <size_t N>
Howard Hinnant10f25d22011-05-27 20:52:28 +000097bitset<N> operator|(const bitset<N>&, const bitset<N>&) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000098
99template <size_t N>
Howard Hinnant10f25d22011-05-27 20:52:28 +0000100bitset<N> operator^(const bitset<N>&, const bitset<N>&) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000101
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:10 +0000116#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000117#pragma GCC system_header
Howard Hinnant08e17472011-10-17 20:05:10 +0000118#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000119
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:27 +0000132#include <__undef_min_max>
133
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000134_LIBCPP_BEGIN_NAMESPACE_STD
135
136template <size_t _N_words, size_t _Size>
Howard Hinnantf03c3b42011-07-02 20:33:23 +0000137class __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:16 +0000146class __bitset
147{
148public:
149 typedef ptrdiff_t difference_type;
150 typedef size_t size_type;
Howard Hinnantf867f632012-05-07 16:50:38 +0000151 typedef size_type __storage_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000152protected:
153 typedef __bitset __self;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000154 typedef __storage_type* __storage_pointer;
155 typedef const __storage_type* __const_storage_pointer;
156 static const unsigned __bits_per_word = static_cast<unsigned>(sizeof(__storage_type) * CHAR_BIT);
157
158 friend class __bit_reference<__bitset>;
159 friend class __bit_const_reference<__bitset>;
160 friend class __bit_iterator<__bitset, false>;
161 friend class __bit_iterator<__bitset, true>;
Howard Hinnant4ae952a2012-08-17 17:10:18 +0000162 friend struct __bit_array<__bitset>;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000163
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:52 +0000171 _LIBCPP_CONSTEXPR __bitset() _NOEXCEPT;
172 explicit _LIBCPP_CONSTEXPR __bitset(unsigned long long __v) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000173
Howard Hinnant10f25d22011-05-27 20:52:28 +0000174 _LIBCPP_INLINE_VISIBILITY reference __make_ref(size_t __pos) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000175 {return reference(__first_ + __pos / __bits_per_word, __storage_type(1) << __pos % __bits_per_word);}
Howard Hinnant90d87232012-07-07 17:04:52 +0000176 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR const_reference __make_ref(size_t __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000177 {return const_reference(__first_ + __pos / __bits_per_word, __storage_type(1) << __pos % __bits_per_word);}
Howard Hinnant10f25d22011-05-27 20:52:28 +0000178 _LIBCPP_INLINE_VISIBILITY iterator __make_iter(size_t __pos) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000179 {return iterator(__first_ + __pos / __bits_per_word, __pos % __bits_per_word);}
Howard Hinnant10f25d22011-05-27 20:52:28 +0000180 _LIBCPP_INLINE_VISIBILITY const_iterator __make_iter(size_t __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000181 {return const_iterator(__first_ + __pos / __bits_per_word, __pos % __bits_per_word);}
182
Howard Hinnant10f25d22011-05-27 20:52:28 +0000183 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:16 +0000186
Howard Hinnant10f25d22011-05-27 20:52:28 +0000187 void flip() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000188 _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:28 +0000193 bool all() const _NOEXCEPT;
194 bool any() const _NOEXCEPT;
195 size_t __hash_code() const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000196private:
Howard Hinnant90d87232012-07-07 17:04:52 +0000197#ifdef _LIBCPP_HAS_NO_CONSTEXPR
Howard Hinnant10f25d22011-05-27 20:52:28 +0000198 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:52 +0000200#endif // _LIBCPP_HAS_NO_CONSTEXPR
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000201 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:52 +0000211_LIBCPP_CONSTEXPR
Howard Hinnant10f25d22011-05-27 20:52:28 +0000212__bitset<_N_words, _Size>::__bitset() _NOEXCEPT
Howard Hinnant90d87232012-07-07 17:04:52 +0000213#ifndef _LIBCPP_HAS_NO_CONSTEXPR
214 : __first_{0}
215#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000216{
Howard Hinnant90d87232012-07-07 17:04:52 +0000217#ifdef _LIBCPP_HAS_NO_CONSTEXPR
Howard Hinnant0949eed2011-06-30 21:18:19 +0000218 _VSTD::fill_n(__first_, _N_words, __storage_type(0));
Howard Hinnant90d87232012-07-07 17:04:52 +0000219#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000220}
221
Howard Hinnant90d87232012-07-07 17:04:52 +0000222#ifdef _LIBCPP_HAS_NO_CONSTEXPR
223
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000224template <size_t _N_words, size_t _Size>
225void
Howard Hinnantec3773c2011-12-01 20:21:04 +0000226__bitset<_N_words, _Size>::__init(unsigned long long __v, false_type) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000227{
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:19 +0000231 _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:16 +0000233 __storage_type(0));
234}
235
236template <size_t _N_words, size_t _Size>
237inline _LIBCPP_INLINE_VISIBILITY
238void
Howard Hinnantec3773c2011-12-01 20:21:04 +0000239__bitset<_N_words, _Size>::__init(unsigned long long __v, true_type) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000240{
241 __first_[0] = __v;
Howard Hinnant0949eed2011-06-30 21:18:19 +0000242 _VSTD::fill(__first_ + 1, __first_ + sizeof(__first_)/sizeof(__first_[0]), __storage_type(0));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000243}
244
Howard Hinnant90d87232012-07-07 17:04:52 +0000245#endif // _LIBCPP_HAS_NO_CONSTEXPR
246
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000247template <size_t _N_words, size_t _Size>
248inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant90d87232012-07-07 17:04:52 +0000249_LIBCPP_CONSTEXPR
Howard Hinnant10f25d22011-05-27 20:52:28 +0000250__bitset<_N_words, _Size>::__bitset(unsigned long long __v) _NOEXCEPT
Howard Hinnant90d87232012-07-07 17:04:52 +0000251#ifndef _LIBCPP_HAS_NO_CONSTEXPR
252 : __first_{__v}
253#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000254{
Howard Hinnant90d87232012-07-07 17:04:52 +0000255#ifdef _LIBCPP_HAS_NO_CONSTEXPR
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000256 __init(__v, integral_constant<bool, sizeof(unsigned long long) == sizeof(__storage_type)>());
Howard Hinnant90d87232012-07-07 17:04:52 +0000257#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000258}
259
260template <size_t _N_words, size_t _Size>
261inline _LIBCPP_INLINE_VISIBILITY
262void
Howard Hinnant10f25d22011-05-27 20:52:28 +0000263__bitset<_N_words, _Size>::operator&=(const __bitset& __v) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000264{
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:28 +0000272__bitset<_N_words, _Size>::operator|=(const __bitset& __v) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000273{
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:28 +0000281__bitset<_N_words, _Size>::operator^=(const __bitset& __v) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000282{
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:28 +0000289__bitset<_N_words, _Size>::flip() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000290{
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:19 +0000311 const_iterator __i = _VSTD::find(__make_iter(sizeof(unsigned long) * CHAR_BIT), __e, true);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000312 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:19 +0000334 const_iterator __i = _VSTD::find(__make_iter(sizeof(unsigned long long) * CHAR_BIT), __e, true);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000335 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:28 +0000372__bitset<_N_words, _Size>::all() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000373{
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:28 +0000392__bitset<_N_words, _Size>::any() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000393{
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:28 +0000413__bitset<_N_words, _Size>::__hash_code() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000414{
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:38 +0000427 typedef size_type __storage_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000428protected:
429 typedef __bitset __self;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000430 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>;
Howard Hinnant4ae952a2012-08-17 17:10:18 +0000438 friend struct __bit_array<__bitset>;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000439
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:52 +0000447 _LIBCPP_CONSTEXPR __bitset() _NOEXCEPT;
448 explicit _LIBCPP_CONSTEXPR __bitset(unsigned long long __v) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000449
Howard Hinnant10f25d22011-05-27 20:52:28 +0000450 _LIBCPP_INLINE_VISIBILITY reference __make_ref(size_t __pos) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000451 {return reference(&__first_, __storage_type(1) << __pos);}
Howard Hinnant90d87232012-07-07 17:04:52 +0000452 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR const_reference __make_ref(size_t __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000453 {return const_reference(&__first_, __storage_type(1) << __pos);}
Howard Hinnant10f25d22011-05-27 20:52:28 +0000454 _LIBCPP_INLINE_VISIBILITY iterator __make_iter(size_t __pos) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000455 {return iterator(&__first_ + __pos / __bits_per_word, __pos % __bits_per_word);}
Howard Hinnant10f25d22011-05-27 20:52:28 +0000456 _LIBCPP_INLINE_VISIBILITY const_iterator __make_iter(size_t __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000457 {return const_iterator(&__first_ + __pos / __bits_per_word, __pos % __bits_per_word);}
458
Howard Hinnant10f25d22011-05-27 20:52:28 +0000459 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:16 +0000462
Howard Hinnant10f25d22011-05-27 20:52:28 +0000463 void flip() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000464
465 unsigned long to_ulong() const;
466 unsigned long long to_ullong() const;
467
Howard Hinnant10f25d22011-05-27 20:52:28 +0000468 bool all() const _NOEXCEPT;
469 bool any() const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000470
Howard Hinnant10f25d22011-05-27 20:52:28 +0000471 size_t __hash_code() const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000472};
473
474template <size_t _Size>
475inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant90d87232012-07-07 17:04:52 +0000476_LIBCPP_CONSTEXPR
Howard Hinnant10f25d22011-05-27 20:52:28 +0000477__bitset<1, _Size>::__bitset() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000478 : __first_(0)
479{
480}
481
482template <size_t _Size>
483inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant90d87232012-07-07 17:04:52 +0000484_LIBCPP_CONSTEXPR
Howard Hinnant10f25d22011-05-27 20:52:28 +0000485__bitset<1, _Size>::__bitset(unsigned long long __v) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000486 : __first_(static_cast<__storage_type>(__v))
487{
488}
489
490template <size_t _Size>
491inline _LIBCPP_INLINE_VISIBILITY
492void
Howard Hinnant10f25d22011-05-27 20:52:28 +0000493__bitset<1, _Size>::operator&=(const __bitset& __v) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000494{
495 __first_ &= __v.__first_;
496}
497
498template <size_t _Size>
499inline _LIBCPP_INLINE_VISIBILITY
500void
Howard Hinnant10f25d22011-05-27 20:52:28 +0000501__bitset<1, _Size>::operator|=(const __bitset& __v) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000502{
503 __first_ |= __v.__first_;
504}
505
506template <size_t _Size>
507inline _LIBCPP_INLINE_VISIBILITY
508void
Howard Hinnant10f25d22011-05-27 20:52:28 +0000509__bitset<1, _Size>::operator^=(const __bitset& __v) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000510{
511 __first_ ^= __v.__first_;
512}
513
514template <size_t _Size>
515inline _LIBCPP_INLINE_VISIBILITY
516void
Howard Hinnant10f25d22011-05-27 20:52:28 +0000517__bitset<1, _Size>::flip() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000518{
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:28 +0000543__bitset<1, _Size>::all() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000544{
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:28 +0000552__bitset<1, _Size>::any() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000553{
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:28 +0000561__bitset<1, _Size>::__hash_code() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000562{
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:38 +0000572 typedef size_type __storage_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000573protected:
574 typedef __bitset __self;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000575 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:04 +0000583 friend struct __bit_array<__bitset>;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000584
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:52 +0000590 _LIBCPP_CONSTEXPR __bitset() _NOEXCEPT;
591 explicit _LIBCPP_CONSTEXPR __bitset(unsigned long long) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000592
Howard Hinnant10f25d22011-05-27 20:52:28 +0000593 _LIBCPP_INLINE_VISIBILITY reference __make_ref(size_t) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000594 {return reference(0, 1);}
Howard Hinnant90d87232012-07-07 17:04:52 +0000595 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR const_reference __make_ref(size_t) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000596 {return const_reference(0, 1);}
Howard Hinnantec3773c2011-12-01 20:21:04 +0000597 _LIBCPP_INLINE_VISIBILITY iterator __make_iter(size_t) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000598 {return iterator(0, 0);}
Howard Hinnantec3773c2011-12-01 20:21:04 +0000599 _LIBCPP_INLINE_VISIBILITY const_iterator __make_iter(size_t) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000600 {return const_iterator(0, 0);}
601
Howard Hinnant10f25d22011-05-27 20:52:28 +0000602 _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:16 +0000605
Howard Hinnant10f25d22011-05-27 20:52:28 +0000606 _LIBCPP_INLINE_VISIBILITY void flip() _NOEXCEPT {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000607
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:28 +0000611 _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:16 +0000613
Howard Hinnant10f25d22011-05-27 20:52:28 +0000614 _LIBCPP_INLINE_VISIBILITY size_t __hash_code() const _NOEXCEPT {return 0;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000615};
616
617inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant90d87232012-07-07 17:04:52 +0000618_LIBCPP_CONSTEXPR
Howard Hinnant10f25d22011-05-27 20:52:28 +0000619__bitset<0, 0>::__bitset() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000620{
621}
622
623inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant90d87232012-07-07 17:04:52 +0000624_LIBCPP_CONSTEXPR
Howard Hinnant10f25d22011-05-27 20:52:28 +0000625__bitset<0, 0>::__bitset(unsigned long long) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000626{
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:23 +0000633class _LIBCPP_VISIBLE bitset
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000634 : 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:43 +0000639public:
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000640 typedef typename base::reference reference;
641 typedef typename base::const_reference const_reference;
642
Howard Hinnant324bb032010-08-22 00:02:43 +0000643 // 23.3.5.1 constructors:
Howard Hinnant90d87232012-07-07 17:04:52 +0000644 _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:14 +0000647 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:43 +0000651 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:16 +0000655 (basic_string<_CharT,_Traits,_Allocator>::npos),
Howard Hinnant324bb032010-08-22 00:02:43 +0000656 _CharT __zero = _CharT('0'), _CharT __one = _CharT('1'));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000657
Howard Hinnant324bb032010-08-22 00:02:43 +0000658 // 23.3.5.2 bitset operations:
Howard Hinnant10f25d22011-05-27 20:52:28 +0000659 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:43 +0000665 bitset& set(size_t __pos, bool __val = true);
Howard Hinnant10f25d22011-05-27 20:52:28 +0000666 bitset& reset() _NOEXCEPT;
Howard Hinnant324bb032010-08-22 00:02:43 +0000667 bitset& reset(size_t __pos);
Howard Hinnant10f25d22011-05-27 20:52:28 +0000668 bitset operator~() const _NOEXCEPT;
669 bitset& flip() _NOEXCEPT;
Howard Hinnant324bb032010-08-22 00:02:43 +0000670 bitset& flip(size_t __pos);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000671
Howard Hinnant324bb032010-08-22 00:02:43 +0000672 // element access:
Howard Hinnant90d87232012-07-07 17:04:52 +0000673 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
674 const_reference operator[](size_t __p) const {return base::__make_ref(__p);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000675 _LIBCPP_INLINE_VISIBILITY reference operator[](size_t __p) {return base::__make_ref(__p);}
Howard Hinnant324bb032010-08-22 00:02:43 +0000676 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:16 +0000679 basic_string<_CharT, _Traits, _Allocator> to_string(_CharT __zero = _CharT('0'),
Howard Hinnant324bb032010-08-22 00:02:43 +0000680 _CharT __one = _CharT('1')) const;
681 template <class _CharT, class _Traits>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000682 basic_string<_CharT, _Traits, allocator<_CharT> > to_string(_CharT __zero = _CharT('0'),
Howard Hinnant324bb032010-08-22 00:02:43 +0000683 _CharT __one = _CharT('1')) const;
684 template <class _CharT>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000685 basic_string<_CharT, char_traits<_CharT>, allocator<_CharT> > to_string(_CharT __zero = _CharT('0'),
Howard Hinnant324bb032010-08-22 00:02:43 +0000686 _CharT __one = _CharT('1')) const;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000687 basic_string<char, char_traits<char>, allocator<char> > to_string(char __zero = '0',
Howard Hinnant324bb032010-08-22 00:02:43 +0000688 char __one = '1') const;
Howard Hinnant10f25d22011-05-27 20:52:28 +0000689 size_t count() const _NOEXCEPT;
Howard Hinnant90d87232012-07-07 17:04:52 +0000690 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR size_t size() const _NOEXCEPT {return _Size;}
Howard Hinnant10f25d22011-05-27 20:52:28 +0000691 bool operator==(const bitset& __rhs) const _NOEXCEPT;
692 bool operator!=(const bitset& __rhs) const _NOEXCEPT;
Howard Hinnant324bb032010-08-22 00:02:43 +0000693 bool test(size_t __pos) const;
Howard Hinnant10f25d22011-05-27 20:52:28 +0000694 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:16 +0000699
700private:
701
Howard Hinnant422a53f2010-09-21 21:28:23 +0000702 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant10f25d22011-05-27 20:52:28 +0000703 size_t __hash_code() const _NOEXCEPT {return base::__hash_code();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000704
705 friend struct hash<bitset>;
706};
707
708template <size_t _Size>
Howard Hinnant34d6b192010-11-17 21:53:14 +0000709template<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:16 +0000713{
Howard Hinnant0949eed2011-06-30 21:18:19 +0000714 size_t __rlen = _VSTD::min(__n, char_traits<_CharT>::length(__str));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000715 for (size_t __i = 0; __i < __rlen; ++__i)
Howard Hinnant34d6b192010-11-17 21:53:14 +0000716 if (__str[__i] != __zero && __str[__i] != __one)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000717#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:50 +0000722 size_t _Mp = _VSTD::min(__rlen, _Size);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000723 size_t __i = 0;
Howard Hinnant99968442011-11-29 18:15:50 +0000724 for (; __i < _Mp; ++__i)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000725 {
Howard Hinnant99968442011-11-29 18:15:50 +0000726 _CharT __c = __str[_Mp - 1 - __i];
Howard Hinnant34d6b192010-11-17 21:53:14 +0000727 if (__c == __zero)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000728 (*this)[__i] = false;
Howard Hinnant34d6b192010-11-17 21:53:14 +0000729 else
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000730 (*this)[__i] = true;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000731 }
Howard Hinnant0949eed2011-06-30 21:18:19 +0000732 _VSTD::fill(base::__make_iter(__i), base::__make_iter(_Size), false);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000733}
734
735template <size_t _Size>
Howard Hinnant324bb032010-08-22 00:02:43 +0000736template<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:16 +0000739 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:19 +0000748 size_t __rlen = _VSTD::min(__n, __str.size() - __pos);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000749 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:50 +0000756 size_t _Mp = _VSTD::min(__rlen, _Size);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000757 size_t __i = 0;
Howard Hinnant99968442011-11-29 18:15:50 +0000758 for (; __i < _Mp; ++__i)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000759 {
Howard Hinnant99968442011-11-29 18:15:50 +0000760 _CharT __c = __str[__pos + _Mp - 1 - __i];
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000761 if (_Traits::eq(__c, __zero))
762 (*this)[__i] = false;
763 else
764 (*this)[__i] = true;
765 }
Howard Hinnant0949eed2011-06-30 21:18:19 +0000766 _VSTD::fill(base::__make_iter(__i), base::__make_iter(_Size), false);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000767}
768
769template <size_t _Size>
770inline _LIBCPP_INLINE_VISIBILITY
771bitset<_Size>&
Howard Hinnant10f25d22011-05-27 20:52:28 +0000772bitset<_Size>::operator&=(const bitset& __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000773{
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:28 +0000781bitset<_Size>::operator|=(const bitset& __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000782{
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:28 +0000790bitset<_Size>::operator^=(const bitset& __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000791{
792 base::operator^=(__rhs);
793 return *this;
794}
795
796template <size_t _Size>
797bitset<_Size>&
Howard Hinnant10f25d22011-05-27 20:52:28 +0000798bitset<_Size>::operator<<=(size_t __pos) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000799{
Howard Hinnant0949eed2011-06-30 21:18:19 +0000800 __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:16 +0000803 return *this;
804}
805
806template <size_t _Size>
807bitset<_Size>&
Howard Hinnant10f25d22011-05-27 20:52:28 +0000808bitset<_Size>::operator>>=(size_t __pos) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000809{
Howard Hinnant0949eed2011-06-30 21:18:19 +0000810 __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:16 +0000813 return *this;
814}
815
816template <size_t _Size>
817inline _LIBCPP_INLINE_VISIBILITY
818bitset<_Size>&
Howard Hinnant10f25d22011-05-27 20:52:28 +0000819bitset<_Size>::set() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000820{
Howard Hinnant0949eed2011-06-30 21:18:19 +0000821 _VSTD::fill_n(base::__make_iter(0), _Size, true);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000822 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:28 +0000842bitset<_Size>::reset() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000843{
Howard Hinnant0949eed2011-06-30 21:18:19 +0000844 _VSTD::fill_n(base::__make_iter(0), _Size, false);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000845 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:28 +0000865bitset<_Size>::operator~() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000866{
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:28 +0000875bitset<_Size>::flip() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000876{
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:43 +0000913template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000914basic_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:43 +0000927template <class _CharT, class _Traits>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000928inline _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:43 +0000936template <class _CharT>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000937inline _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:28 +0000955bitset<_Size>::count() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000956{
Howard Hinnant0949eed2011-06-30 21:18:19 +0000957 return static_cast<size_t>(_VSTD::count(base::__make_iter(0), base::__make_iter(_Size), true));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000958}
959
960template <size_t _Size>
961inline _LIBCPP_INLINE_VISIBILITY
962bool
Howard Hinnant10f25d22011-05-27 20:52:28 +0000963bitset<_Size>::operator==(const bitset& __rhs) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000964{
Howard Hinnant0949eed2011-06-30 21:18:19 +0000965 return _VSTD::equal(base::__make_iter(0), base::__make_iter(_Size), __rhs.__make_iter(0));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000966}
967
968template <size_t _Size>
969inline _LIBCPP_INLINE_VISIBILITY
970bool
Howard Hinnant10f25d22011-05-27 20:52:28 +0000971bitset<_Size>::operator!=(const bitset& __rhs) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000972{
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:28 +0000992bitset<_Size>::all() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000993{
994 return base::all();
995}
996
997template <size_t _Size>
998inline _LIBCPP_INLINE_VISIBILITY
999bool
Howard Hinnant10f25d22011-05-27 20:52:28 +00001000bitset<_Size>::any() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001001{
1002 return base::any();
1003}
1004
1005template <size_t _Size>
1006inline _LIBCPP_INLINE_VISIBILITY
1007bitset<_Size>
Howard Hinnant10f25d22011-05-27 20:52:28 +00001008bitset<_Size>::operator<<(size_t __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001009{
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:28 +00001018bitset<_Size>::operator>>(size_t __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001019{
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:28 +00001028operator&(const bitset<_Size>& __x, const bitset<_Size>& __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001029{
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:28 +00001038operator|(const bitset<_Size>& __x, const bitset<_Size>& __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001039{
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:28 +00001048operator^(const bitset<_Size>& __x, const bitset<_Size>& __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001049{
1050 bitset<_Size> __r = __x;
1051 __r ^= __y;
1052 return __r;
1053}
1054
1055template <size_t _Size>
Howard Hinnant422a53f2010-09-21 21:28:23 +00001056struct _LIBCPP_VISIBLE hash<bitset<_Size> >
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001057 : public unary_function<bitset<_Size>, size_t>
1058{
Howard Hinnant422a53f2010-09-21 21:28:23 +00001059 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant10f25d22011-05-27 20:52:28 +00001060 size_t operator()(const bitset<_Size>& __bs) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001061 {return __bs.__hash_code();}
1062};
1063
Howard Hinnant464aa5c2011-07-18 15:51:59 +00001064template <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:16 +00001072_LIBCPP_END_NAMESPACE_STD
1073
1074#endif // _LIBCPP_BITSET