blob: 6e12e5ccc5156df4b026af6c697ebb6a056f1cd8 [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;
151protected:
152 typedef __bitset __self;
153 typedef size_type __storage_type;
154 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 Hinnant10f25d22011-05-27 20:52:28 +0000171 __bitset() _NOEXCEPT;
172 explicit __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 Hinnant10f25d22011-05-27 20:52:28 +0000176 _LIBCPP_INLINE_VISIBILITY 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 Hinnant10f25d22011-05-27 20:52:28 +0000197 void __init(unsigned long long __v, false_type) _NOEXCEPT;
198 void __init(unsigned long long __v, true_type) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000199 unsigned long to_ulong(false_type) const;
200 unsigned long to_ulong(true_type) const;
201 unsigned long long to_ullong(false_type) const;
202 unsigned long long to_ullong(true_type) const;
203 unsigned long long to_ullong(true_type, false_type) const;
204 unsigned long long to_ullong(true_type, true_type) const;
205};
206
207template <size_t _N_words, size_t _Size>
208inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant10f25d22011-05-27 20:52:28 +0000209__bitset<_N_words, _Size>::__bitset() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000210{
Howard Hinnant0949eed2011-06-30 21:18:19 +0000211 _VSTD::fill_n(__first_, _N_words, __storage_type(0));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000212}
213
214template <size_t _N_words, size_t _Size>
215void
Howard Hinnantec3773c2011-12-01 20:21:04 +0000216__bitset<_N_words, _Size>::__init(unsigned long long __v, false_type) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000217{
218 __storage_type __t[sizeof(unsigned long long) / sizeof(__storage_type)];
219 for (size_t __i = 0; __i < sizeof(__t)/sizeof(__t[0]); ++__i, __v >>= __bits_per_word)
220 __t[__i] = static_cast<__storage_type>(__v);
Howard Hinnant0949eed2011-06-30 21:18:19 +0000221 _VSTD::copy(__t, __t + sizeof(__t)/sizeof(__t[0]), __first_);
222 _VSTD::fill(__first_ + sizeof(__t)/sizeof(__t[0]), __first_ + sizeof(__first_)/sizeof(__first_[0]),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000223 __storage_type(0));
224}
225
226template <size_t _N_words, size_t _Size>
227inline _LIBCPP_INLINE_VISIBILITY
228void
Howard Hinnantec3773c2011-12-01 20:21:04 +0000229__bitset<_N_words, _Size>::__init(unsigned long long __v, true_type) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000230{
231 __first_[0] = __v;
Howard Hinnant0949eed2011-06-30 21:18:19 +0000232 _VSTD::fill(__first_ + 1, __first_ + sizeof(__first_)/sizeof(__first_[0]), __storage_type(0));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000233}
234
235template <size_t _N_words, size_t _Size>
236inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant10f25d22011-05-27 20:52:28 +0000237__bitset<_N_words, _Size>::__bitset(unsigned long long __v) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000238{
239 __init(__v, integral_constant<bool, sizeof(unsigned long long) == sizeof(__storage_type)>());
240}
241
242template <size_t _N_words, size_t _Size>
243inline _LIBCPP_INLINE_VISIBILITY
244void
Howard Hinnant10f25d22011-05-27 20:52:28 +0000245__bitset<_N_words, _Size>::operator&=(const __bitset& __v) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000246{
247 for (size_type __i = 0; __i < _N_words; ++__i)
248 __first_[__i] &= __v.__first_[__i];
249}
250
251template <size_t _N_words, size_t _Size>
252inline _LIBCPP_INLINE_VISIBILITY
253void
Howard Hinnant10f25d22011-05-27 20:52:28 +0000254__bitset<_N_words, _Size>::operator|=(const __bitset& __v) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000255{
256 for (size_type __i = 0; __i < _N_words; ++__i)
257 __first_[__i] |= __v.__first_[__i];
258}
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>
270void
Howard Hinnant10f25d22011-05-27 20:52:28 +0000271__bitset<_N_words, _Size>::flip() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000272{
273 // do middle whole words
274 size_type __n = _Size;
275 __storage_pointer __p = __first_;
276 for (; __n >= __bits_per_word; ++__p, __n -= __bits_per_word)
277 *__p = ~*__p;
278 // do last partial word
279 if (__n > 0)
280 {
281 __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
282 __storage_type __b = *__p & __m;
283 *__p &= ~__m;
284 *__p |= ~__b & __m;
285 }
286}
287
288template <size_t _N_words, size_t _Size>
289unsigned long
290__bitset<_N_words, _Size>::to_ulong(false_type) const
291{
292 const_iterator __e = __make_iter(_Size);
Howard Hinnant0949eed2011-06-30 21:18:19 +0000293 const_iterator __i = _VSTD::find(__make_iter(sizeof(unsigned long) * CHAR_BIT), __e, true);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000294 if (__i != __e)
295#ifndef _LIBCPP_NO_EXCEPTIONS
296 throw overflow_error("bitset to_ulong overflow error");
297#else
298 assert(!"bitset to_ulong overflow error");
299#endif
300 return __first_[0];
301}
302
303template <size_t _N_words, size_t _Size>
304inline _LIBCPP_INLINE_VISIBILITY
305unsigned long
306__bitset<_N_words, _Size>::to_ulong(true_type) const
307{
308 return __first_[0];
309}
310
311template <size_t _N_words, size_t _Size>
312unsigned long long
313__bitset<_N_words, _Size>::to_ullong(false_type) const
314{
315 const_iterator __e = __make_iter(_Size);
Howard Hinnant0949eed2011-06-30 21:18:19 +0000316 const_iterator __i = _VSTD::find(__make_iter(sizeof(unsigned long long) * CHAR_BIT), __e, true);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000317 if (__i != __e)
318#ifndef _LIBCPP_NO_EXCEPTIONS
319 throw overflow_error("bitset to_ullong overflow error");
320#else
321 assert(!"bitset to_ullong overflow error");
322#endif
323 return to_ullong(true_type());
324}
325
326template <size_t _N_words, size_t _Size>
327inline _LIBCPP_INLINE_VISIBILITY
328unsigned long long
329__bitset<_N_words, _Size>::to_ullong(true_type) const
330{
331 return to_ullong(true_type(), integral_constant<bool, sizeof(__storage_type) < sizeof(unsigned long long)>());
332}
333
334template <size_t _N_words, size_t _Size>
335inline _LIBCPP_INLINE_VISIBILITY
336unsigned long long
337__bitset<_N_words, _Size>::to_ullong(true_type, false_type) const
338{
339 return __first_[0];
340}
341
342template <size_t _N_words, size_t _Size>
343unsigned long long
344__bitset<_N_words, _Size>::to_ullong(true_type, true_type) const
345{
346 unsigned long long __r = __first_[0];
347 for (std::size_t __i = 1; __i < sizeof(unsigned long long) / sizeof(__storage_type); ++__i)
348 __r |= static_cast<unsigned long long>(__first_[__i]) << (sizeof(__storage_type) * CHAR_BIT);
349 return __r;
350}
351
352template <size_t _N_words, size_t _Size>
353bool
Howard Hinnant10f25d22011-05-27 20:52:28 +0000354__bitset<_N_words, _Size>::all() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000355{
356 // do middle whole words
357 size_type __n = _Size;
358 __const_storage_pointer __p = __first_;
359 for (; __n >= __bits_per_word; ++__p, __n -= __bits_per_word)
360 if (~*__p)
361 return false;
362 // do last partial word
363 if (__n > 0)
364 {
365 __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
366 if (~*__p & __m)
367 return false;
368 }
369 return true;
370}
371
372template <size_t _N_words, size_t _Size>
373bool
Howard Hinnant10f25d22011-05-27 20:52:28 +0000374__bitset<_N_words, _Size>::any() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000375{
376 // do middle whole words
377 size_type __n = _Size;
378 __const_storage_pointer __p = __first_;
379 for (; __n >= __bits_per_word; ++__p, __n -= __bits_per_word)
380 if (*__p)
381 return true;
382 // do last partial word
383 if (__n > 0)
384 {
385 __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
386 if (*__p & __m)
387 return true;
388 }
389 return false;
390}
391
392template <size_t _N_words, size_t _Size>
393inline _LIBCPP_INLINE_VISIBILITY
394size_t
Howard Hinnant10f25d22011-05-27 20:52:28 +0000395__bitset<_N_words, _Size>::__hash_code() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000396{
397 size_t __h = 0;
398 for (size_type __i = 0; __i < _N_words; ++__i)
399 __h ^= __first_[__i];
400 return __h;
401}
402
403template <size_t _Size>
404class __bitset<1, _Size>
405{
406public:
407 typedef ptrdiff_t difference_type;
408 typedef size_t size_type;
409protected:
410 typedef __bitset __self;
411 typedef size_type __storage_type;
412 typedef __storage_type* __storage_pointer;
413 typedef const __storage_type* __const_storage_pointer;
414 static const unsigned __bits_per_word = static_cast<unsigned>(sizeof(__storage_type) * CHAR_BIT);
415
416 friend class __bit_reference<__bitset>;
417 friend class __bit_const_reference<__bitset>;
418 friend class __bit_iterator<__bitset, false>;
419 friend class __bit_iterator<__bitset, true>;
420 friend class __bit_array<__bitset>;
421
422 __storage_type __first_;
423
424 typedef __bit_reference<__bitset> reference;
425 typedef __bit_const_reference<__bitset> const_reference;
426 typedef __bit_iterator<__bitset, false> iterator;
427 typedef __bit_iterator<__bitset, true> const_iterator;
428
Howard Hinnant10f25d22011-05-27 20:52:28 +0000429 __bitset() _NOEXCEPT;
430 explicit __bitset(unsigned long long __v) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000431
Howard Hinnant10f25d22011-05-27 20:52:28 +0000432 _LIBCPP_INLINE_VISIBILITY reference __make_ref(size_t __pos) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000433 {return reference(&__first_, __storage_type(1) << __pos);}
Howard Hinnant10f25d22011-05-27 20:52:28 +0000434 _LIBCPP_INLINE_VISIBILITY const_reference __make_ref(size_t __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000435 {return const_reference(&__first_, __storage_type(1) << __pos);}
Howard Hinnant10f25d22011-05-27 20:52:28 +0000436 _LIBCPP_INLINE_VISIBILITY iterator __make_iter(size_t __pos) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000437 {return iterator(&__first_ + __pos / __bits_per_word, __pos % __bits_per_word);}
Howard Hinnant10f25d22011-05-27 20:52:28 +0000438 _LIBCPP_INLINE_VISIBILITY const_iterator __make_iter(size_t __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000439 {return const_iterator(&__first_ + __pos / __bits_per_word, __pos % __bits_per_word);}
440
Howard Hinnant10f25d22011-05-27 20:52:28 +0000441 void operator&=(const __bitset& __v) _NOEXCEPT;
442 void operator|=(const __bitset& __v) _NOEXCEPT;
443 void operator^=(const __bitset& __v) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000444
Howard Hinnant10f25d22011-05-27 20:52:28 +0000445 void flip() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000446
447 unsigned long to_ulong() const;
448 unsigned long long to_ullong() const;
449
Howard Hinnant10f25d22011-05-27 20:52:28 +0000450 bool all() const _NOEXCEPT;
451 bool any() const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000452
Howard Hinnant10f25d22011-05-27 20:52:28 +0000453 size_t __hash_code() const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000454};
455
456template <size_t _Size>
457inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant10f25d22011-05-27 20:52:28 +0000458__bitset<1, _Size>::__bitset() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000459 : __first_(0)
460{
461}
462
463template <size_t _Size>
464inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant10f25d22011-05-27 20:52:28 +0000465__bitset<1, _Size>::__bitset(unsigned long long __v) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000466 : __first_(static_cast<__storage_type>(__v))
467{
468}
469
470template <size_t _Size>
471inline _LIBCPP_INLINE_VISIBILITY
472void
Howard Hinnant10f25d22011-05-27 20:52:28 +0000473__bitset<1, _Size>::operator&=(const __bitset& __v) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000474{
475 __first_ &= __v.__first_;
476}
477
478template <size_t _Size>
479inline _LIBCPP_INLINE_VISIBILITY
480void
Howard Hinnant10f25d22011-05-27 20:52:28 +0000481__bitset<1, _Size>::operator|=(const __bitset& __v) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000482{
483 __first_ |= __v.__first_;
484}
485
486template <size_t _Size>
487inline _LIBCPP_INLINE_VISIBILITY
488void
Howard Hinnant10f25d22011-05-27 20:52:28 +0000489__bitset<1, _Size>::operator^=(const __bitset& __v) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000490{
491 __first_ ^= __v.__first_;
492}
493
494template <size_t _Size>
495inline _LIBCPP_INLINE_VISIBILITY
496void
Howard Hinnant10f25d22011-05-27 20:52:28 +0000497__bitset<1, _Size>::flip() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000498{
499 __storage_type __m = ~__storage_type(0) >> (__bits_per_word - _Size);
500 __first_ = ~__first_;
501 __first_ &= __m;
502}
503
504template <size_t _Size>
505inline _LIBCPP_INLINE_VISIBILITY
506unsigned long
507__bitset<1, _Size>::to_ulong() const
508{
509 return __first_;
510}
511
512template <size_t _Size>
513inline _LIBCPP_INLINE_VISIBILITY
514unsigned long long
515__bitset<1, _Size>::to_ullong() const
516{
517 return __first_;
518}
519
520template <size_t _Size>
521inline _LIBCPP_INLINE_VISIBILITY
522bool
Howard Hinnant10f25d22011-05-27 20:52:28 +0000523__bitset<1, _Size>::all() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000524{
525 __storage_type __m = ~__storage_type(0) >> (__bits_per_word - _Size);
526 return !(~__first_ & __m);
527}
528
529template <size_t _Size>
530inline _LIBCPP_INLINE_VISIBILITY
531bool
Howard Hinnant10f25d22011-05-27 20:52:28 +0000532__bitset<1, _Size>::any() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000533{
534 __storage_type __m = ~__storage_type(0) >> (__bits_per_word - _Size);
535 return __first_ & __m;
536}
537
538template <size_t _Size>
539inline _LIBCPP_INLINE_VISIBILITY
540size_t
Howard Hinnant10f25d22011-05-27 20:52:28 +0000541__bitset<1, _Size>::__hash_code() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000542{
543 return __first_;
544}
545
546template <>
547class __bitset<0, 0>
548{
549public:
550 typedef ptrdiff_t difference_type;
551 typedef size_t size_type;
552protected:
553 typedef __bitset __self;
554 typedef size_type __storage_type;
555 typedef __storage_type* __storage_pointer;
556 typedef const __storage_type* __const_storage_pointer;
557 static const unsigned __bits_per_word = static_cast<unsigned>(sizeof(__storage_type) * CHAR_BIT);
558
559 friend class __bit_reference<__bitset>;
560 friend class __bit_const_reference<__bitset>;
561 friend class __bit_iterator<__bitset, false>;
562 friend class __bit_iterator<__bitset, true>;
Howard Hinnantec3773c2011-12-01 20:21:04 +0000563 friend struct __bit_array<__bitset>;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000564
565 typedef __bit_reference<__bitset> reference;
566 typedef __bit_const_reference<__bitset> const_reference;
567 typedef __bit_iterator<__bitset, false> iterator;
568 typedef __bit_iterator<__bitset, true> const_iterator;
569
Howard Hinnant10f25d22011-05-27 20:52:28 +0000570 __bitset() _NOEXCEPT;
571 explicit __bitset(unsigned long long) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000572
Howard Hinnant10f25d22011-05-27 20:52:28 +0000573 _LIBCPP_INLINE_VISIBILITY reference __make_ref(size_t) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000574 {return reference(0, 1);}
Howard Hinnant10f25d22011-05-27 20:52:28 +0000575 _LIBCPP_INLINE_VISIBILITY const_reference __make_ref(size_t) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000576 {return const_reference(0, 1);}
Howard Hinnantec3773c2011-12-01 20:21:04 +0000577 _LIBCPP_INLINE_VISIBILITY iterator __make_iter(size_t) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000578 {return iterator(0, 0);}
Howard Hinnantec3773c2011-12-01 20:21:04 +0000579 _LIBCPP_INLINE_VISIBILITY const_iterator __make_iter(size_t) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000580 {return const_iterator(0, 0);}
581
Howard Hinnant10f25d22011-05-27 20:52:28 +0000582 _LIBCPP_INLINE_VISIBILITY void operator&=(const __bitset&) _NOEXCEPT {}
583 _LIBCPP_INLINE_VISIBILITY void operator|=(const __bitset&) _NOEXCEPT {}
584 _LIBCPP_INLINE_VISIBILITY void operator^=(const __bitset&) _NOEXCEPT {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000585
Howard Hinnant10f25d22011-05-27 20:52:28 +0000586 _LIBCPP_INLINE_VISIBILITY void flip() _NOEXCEPT {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000587
588 _LIBCPP_INLINE_VISIBILITY unsigned long to_ulong() const {return 0;}
589 _LIBCPP_INLINE_VISIBILITY unsigned long long to_ullong() const {return 0;}
590
Howard Hinnant10f25d22011-05-27 20:52:28 +0000591 _LIBCPP_INLINE_VISIBILITY bool all() const _NOEXCEPT {return true;}
592 _LIBCPP_INLINE_VISIBILITY bool any() const _NOEXCEPT {return false;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000593
Howard Hinnant10f25d22011-05-27 20:52:28 +0000594 _LIBCPP_INLINE_VISIBILITY size_t __hash_code() const _NOEXCEPT {return 0;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000595};
596
597inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant10f25d22011-05-27 20:52:28 +0000598__bitset<0, 0>::__bitset() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000599{
600}
601
602inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant10f25d22011-05-27 20:52:28 +0000603__bitset<0, 0>::__bitset(unsigned long long) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000604{
605}
606
607template <size_t _Size> class bitset;
608template <size_t _Size> struct hash<bitset<_Size> >;
609
610template <size_t _Size>
Howard Hinnant422a53f2010-09-21 21:28:23 +0000611class _LIBCPP_VISIBLE bitset
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000612 : private __bitset<_Size == 0 ? 0 : (_Size - 1) / (sizeof(size_t) * CHAR_BIT) + 1, _Size>
613{
614 static const unsigned __n_words = _Size == 0 ? 0 : (_Size - 1) / (sizeof(size_t) * CHAR_BIT) + 1;
615 typedef __bitset<__n_words, _Size> base;
616
Howard Hinnant324bb032010-08-22 00:02:43 +0000617public:
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000618 typedef typename base::reference reference;
619 typedef typename base::const_reference const_reference;
620
Howard Hinnant324bb032010-08-22 00:02:43 +0000621 // 23.3.5.1 constructors:
Howard Hinnant10f25d22011-05-27 20:52:28 +0000622 /*constexpr*/ _LIBCPP_INLINE_VISIBILITY bitset() _NOEXCEPT {}
623 /*constexpr*/ _LIBCPP_INLINE_VISIBILITY bitset(unsigned long long __v) _NOEXCEPT : base(__v) {}
Howard Hinnant34d6b192010-11-17 21:53:14 +0000624 template<class _CharT>
625 explicit bitset(const _CharT* __str,
626 typename basic_string<_CharT>::size_type __n = basic_string<_CharT>::npos,
627 _CharT __zero = _CharT('0'), _CharT __one = _CharT('1'));
Howard Hinnant324bb032010-08-22 00:02:43 +0000628 template<class _CharT, class _Traits, class _Allocator>
629 explicit bitset(const basic_string<_CharT,_Traits,_Allocator>& __str,
630 typename basic_string<_CharT,_Traits,_Allocator>::size_type __pos = 0,
631 typename basic_string<_CharT,_Traits,_Allocator>::size_type __n =
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000632 (basic_string<_CharT,_Traits,_Allocator>::npos),
Howard Hinnant324bb032010-08-22 00:02:43 +0000633 _CharT __zero = _CharT('0'), _CharT __one = _CharT('1'));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000634
Howard Hinnant324bb032010-08-22 00:02:43 +0000635 // 23.3.5.2 bitset operations:
Howard Hinnant10f25d22011-05-27 20:52:28 +0000636 bitset& operator&=(const bitset& __rhs) _NOEXCEPT;
637 bitset& operator|=(const bitset& __rhs) _NOEXCEPT;
638 bitset& operator^=(const bitset& __rhs) _NOEXCEPT;
639 bitset& operator<<=(size_t __pos) _NOEXCEPT;
640 bitset& operator>>=(size_t __pos) _NOEXCEPT;
641 bitset& set() _NOEXCEPT;
Howard Hinnant324bb032010-08-22 00:02:43 +0000642 bitset& set(size_t __pos, bool __val = true);
Howard Hinnant10f25d22011-05-27 20:52:28 +0000643 bitset& reset() _NOEXCEPT;
Howard Hinnant324bb032010-08-22 00:02:43 +0000644 bitset& reset(size_t __pos);
Howard Hinnant10f25d22011-05-27 20:52:28 +0000645 bitset operator~() const _NOEXCEPT;
646 bitset& flip() _NOEXCEPT;
Howard Hinnant324bb032010-08-22 00:02:43 +0000647 bitset& flip(size_t __pos);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000648
Howard Hinnant324bb032010-08-22 00:02:43 +0000649 // element access:
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000650 _LIBCPP_INLINE_VISIBILITY const_reference operator[](size_t __p) const {return base::__make_ref(__p);}
651 _LIBCPP_INLINE_VISIBILITY reference operator[](size_t __p) {return base::__make_ref(__p);}
Howard Hinnant324bb032010-08-22 00:02:43 +0000652 unsigned long to_ulong() const;
653 unsigned long long to_ullong() const;
654 template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000655 basic_string<_CharT, _Traits, _Allocator> to_string(_CharT __zero = _CharT('0'),
Howard Hinnant324bb032010-08-22 00:02:43 +0000656 _CharT __one = _CharT('1')) const;
657 template <class _CharT, class _Traits>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000658 basic_string<_CharT, _Traits, allocator<_CharT> > to_string(_CharT __zero = _CharT('0'),
Howard Hinnant324bb032010-08-22 00:02:43 +0000659 _CharT __one = _CharT('1')) const;
660 template <class _CharT>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000661 basic_string<_CharT, char_traits<_CharT>, allocator<_CharT> > to_string(_CharT __zero = _CharT('0'),
Howard Hinnant324bb032010-08-22 00:02:43 +0000662 _CharT __one = _CharT('1')) const;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000663 basic_string<char, char_traits<char>, allocator<char> > to_string(char __zero = '0',
Howard Hinnant324bb032010-08-22 00:02:43 +0000664 char __one = '1') const;
Howard Hinnant10f25d22011-05-27 20:52:28 +0000665 size_t count() const _NOEXCEPT;
666 /*constexpr*/ _LIBCPP_INLINE_VISIBILITY size_t size() const _NOEXCEPT {return _Size;}
667 bool operator==(const bitset& __rhs) const _NOEXCEPT;
668 bool operator!=(const bitset& __rhs) const _NOEXCEPT;
Howard Hinnant324bb032010-08-22 00:02:43 +0000669 bool test(size_t __pos) const;
Howard Hinnant10f25d22011-05-27 20:52:28 +0000670 bool all() const _NOEXCEPT;
671 bool any() const _NOEXCEPT;
672 _LIBCPP_INLINE_VISIBILITY bool none() const _NOEXCEPT {return !any();}
673 bitset operator<<(size_t __pos) const _NOEXCEPT;
674 bitset operator>>(size_t __pos) const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000675
676private:
677
Howard Hinnant422a53f2010-09-21 21:28:23 +0000678 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant10f25d22011-05-27 20:52:28 +0000679 size_t __hash_code() const _NOEXCEPT {return base::__hash_code();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000680
681 friend struct hash<bitset>;
682};
683
684template <size_t _Size>
Howard Hinnant34d6b192010-11-17 21:53:14 +0000685template<class _CharT>
686bitset<_Size>::bitset(const _CharT* __str,
687 typename basic_string<_CharT>::size_type __n,
688 _CharT __zero, _CharT __one)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000689{
Howard Hinnant0949eed2011-06-30 21:18:19 +0000690 size_t __rlen = _VSTD::min(__n, char_traits<_CharT>::length(__str));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000691 for (size_t __i = 0; __i < __rlen; ++__i)
Howard Hinnant34d6b192010-11-17 21:53:14 +0000692 if (__str[__i] != __zero && __str[__i] != __one)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000693#ifndef _LIBCPP_NO_EXCEPTIONS
694 throw invalid_argument("bitset string ctor has invalid argument");
695#else
696 assert(!"bitset string ctor has invalid argument");
697#endif
Howard Hinnant99968442011-11-29 18:15:50 +0000698 size_t _Mp = _VSTD::min(__rlen, _Size);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000699 size_t __i = 0;
Howard Hinnant99968442011-11-29 18:15:50 +0000700 for (; __i < _Mp; ++__i)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000701 {
Howard Hinnant99968442011-11-29 18:15:50 +0000702 _CharT __c = __str[_Mp - 1 - __i];
Howard Hinnant34d6b192010-11-17 21:53:14 +0000703 if (__c == __zero)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000704 (*this)[__i] = false;
Howard Hinnant34d6b192010-11-17 21:53:14 +0000705 else
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000706 (*this)[__i] = true;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000707 }
Howard Hinnant0949eed2011-06-30 21:18:19 +0000708 _VSTD::fill(base::__make_iter(__i), base::__make_iter(_Size), false);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000709}
710
711template <size_t _Size>
Howard Hinnant324bb032010-08-22 00:02:43 +0000712template<class _CharT, class _Traits, class _Allocator>
713bitset<_Size>::bitset(const basic_string<_CharT,_Traits,_Allocator>& __str,
714 typename basic_string<_CharT,_Traits,_Allocator>::size_type __pos,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000715 typename basic_string<_CharT,_Traits,_Allocator>::size_type __n,
716 _CharT __zero, _CharT __one)
717{
718 if (__pos > __str.size())
719#ifndef _LIBCPP_NO_EXCEPTIONS
720 throw out_of_range("bitset string pos out of range");
721#else
722 assert(!"bitset string pos out of range");
723#endif
Howard Hinnant0949eed2011-06-30 21:18:19 +0000724 size_t __rlen = _VSTD::min(__n, __str.size() - __pos);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000725 for (size_t __i = __pos; __i < __pos + __rlen; ++__i)
726 if (!_Traits::eq(__str[__i], __zero) && !_Traits::eq(__str[__i], __one))
727#ifndef _LIBCPP_NO_EXCEPTIONS
728 throw invalid_argument("bitset string ctor has invalid argument");
729#else
730 assert(!"bitset string ctor has invalid argument");
731#endif
Howard Hinnant99968442011-11-29 18:15:50 +0000732 size_t _Mp = _VSTD::min(__rlen, _Size);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000733 size_t __i = 0;
Howard Hinnant99968442011-11-29 18:15:50 +0000734 for (; __i < _Mp; ++__i)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000735 {
Howard Hinnant99968442011-11-29 18:15:50 +0000736 _CharT __c = __str[__pos + _Mp - 1 - __i];
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000737 if (_Traits::eq(__c, __zero))
738 (*this)[__i] = false;
739 else
740 (*this)[__i] = true;
741 }
Howard Hinnant0949eed2011-06-30 21:18:19 +0000742 _VSTD::fill(base::__make_iter(__i), base::__make_iter(_Size), false);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000743}
744
745template <size_t _Size>
746inline _LIBCPP_INLINE_VISIBILITY
747bitset<_Size>&
Howard Hinnant10f25d22011-05-27 20:52:28 +0000748bitset<_Size>::operator&=(const bitset& __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000749{
750 base::operator&=(__rhs);
751 return *this;
752}
753
754template <size_t _Size>
755inline _LIBCPP_INLINE_VISIBILITY
756bitset<_Size>&
Howard Hinnant10f25d22011-05-27 20:52:28 +0000757bitset<_Size>::operator|=(const bitset& __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000758{
759 base::operator|=(__rhs);
760 return *this;
761}
762
763template <size_t _Size>
764inline _LIBCPP_INLINE_VISIBILITY
765bitset<_Size>&
Howard Hinnant10f25d22011-05-27 20:52:28 +0000766bitset<_Size>::operator^=(const bitset& __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000767{
768 base::operator^=(__rhs);
769 return *this;
770}
771
772template <size_t _Size>
773bitset<_Size>&
Howard Hinnant10f25d22011-05-27 20:52:28 +0000774bitset<_Size>::operator<<=(size_t __pos) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000775{
Howard Hinnant0949eed2011-06-30 21:18:19 +0000776 __pos = _VSTD::min(__pos, _Size);
777 _VSTD::copy_backward(base::__make_iter(0), base::__make_iter(_Size - __pos), base::__make_iter(_Size));
778 _VSTD::fill_n(base::__make_iter(0), __pos, false);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000779 return *this;
780}
781
782template <size_t _Size>
783bitset<_Size>&
Howard Hinnant10f25d22011-05-27 20:52:28 +0000784bitset<_Size>::operator>>=(size_t __pos) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000785{
Howard Hinnant0949eed2011-06-30 21:18:19 +0000786 __pos = _VSTD::min(__pos, _Size);
787 _VSTD::copy(base::__make_iter(__pos), base::__make_iter(_Size), base::__make_iter(0));
788 _VSTD::fill_n(base::__make_iter(_Size - __pos), __pos, false);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000789 return *this;
790}
791
792template <size_t _Size>
793inline _LIBCPP_INLINE_VISIBILITY
794bitset<_Size>&
Howard Hinnant10f25d22011-05-27 20:52:28 +0000795bitset<_Size>::set() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000796{
Howard Hinnant0949eed2011-06-30 21:18:19 +0000797 _VSTD::fill_n(base::__make_iter(0), _Size, true);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000798 return *this;
799}
800
801template <size_t _Size>
802bitset<_Size>&
803bitset<_Size>::set(size_t __pos, bool __val)
804{
805 if (__pos >= _Size)
806#ifndef _LIBCPP_NO_EXCEPTIONS
807 throw out_of_range("bitset set argument out of range");
808#else
809 assert(!"bitset set argument out of range");
810#endif
811 (*this)[__pos] = __val;
812 return *this;
813}
814
815template <size_t _Size>
816inline _LIBCPP_INLINE_VISIBILITY
817bitset<_Size>&
Howard Hinnant10f25d22011-05-27 20:52:28 +0000818bitset<_Size>::reset() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000819{
Howard Hinnant0949eed2011-06-30 21:18:19 +0000820 _VSTD::fill_n(base::__make_iter(0), _Size, false);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000821 return *this;
822}
823
824template <size_t _Size>
825bitset<_Size>&
826bitset<_Size>::reset(size_t __pos)
827{
828 if (__pos >= _Size)
829#ifndef _LIBCPP_NO_EXCEPTIONS
830 throw out_of_range("bitset reset argument out of range");
831#else
832 assert(!"bitset reset argument out of range");
833#endif
834 (*this)[__pos] = false;
835 return *this;
836}
837
838template <size_t _Size>
839inline _LIBCPP_INLINE_VISIBILITY
840bitset<_Size>
Howard Hinnant10f25d22011-05-27 20:52:28 +0000841bitset<_Size>::operator~() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000842{
843 bitset __x(*this);
844 __x.flip();
845 return __x;
846}
847
848template <size_t _Size>
849inline _LIBCPP_INLINE_VISIBILITY
850bitset<_Size>&
Howard Hinnant10f25d22011-05-27 20:52:28 +0000851bitset<_Size>::flip() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000852{
853 base::flip();
854 return *this;
855}
856
857template <size_t _Size>
858bitset<_Size>&
859bitset<_Size>::flip(size_t __pos)
860{
861 if (__pos >= _Size)
862#ifndef _LIBCPP_NO_EXCEPTIONS
863 throw out_of_range("bitset flip argument out of range");
864#else
865 assert(!"bitset flip argument out of range");
866#endif
867 reference r = base::__make_ref(__pos);
868 r = ~r;
869 return *this;
870}
871
872template <size_t _Size>
873inline _LIBCPP_INLINE_VISIBILITY
874unsigned long
875bitset<_Size>::to_ulong() const
876{
877 return base::to_ulong();
878}
879
880template <size_t _Size>
881inline _LIBCPP_INLINE_VISIBILITY
882unsigned long long
883bitset<_Size>::to_ullong() const
884{
885 return base::to_ullong();
886}
887
888template <size_t _Size>
Howard Hinnant324bb032010-08-22 00:02:43 +0000889template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000890basic_string<_CharT, _Traits, _Allocator>
891bitset<_Size>::to_string(_CharT __zero, _CharT __one) const
892{
893 basic_string<_CharT, _Traits, _Allocator> __r(_Size, __zero);
894 for (size_t __i = 0; __i < _Size; ++__i)
895 {
896 if ((*this)[__i])
897 __r[_Size - 1 - __i] = __one;
898 }
899 return __r;
900}
901
902template <size_t _Size>
Howard Hinnant324bb032010-08-22 00:02:43 +0000903template <class _CharT, class _Traits>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000904inline _LIBCPP_INLINE_VISIBILITY
905basic_string<_CharT, _Traits, allocator<_CharT> >
906bitset<_Size>::to_string(_CharT __zero, _CharT __one) const
907{
908 return to_string<_CharT, _Traits, allocator<_CharT> >(__zero, __one);
909}
910
911template <size_t _Size>
Howard Hinnant324bb032010-08-22 00:02:43 +0000912template <class _CharT>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000913inline _LIBCPP_INLINE_VISIBILITY
914basic_string<_CharT, char_traits<_CharT>, allocator<_CharT> >
915bitset<_Size>::to_string(_CharT __zero, _CharT __one) const
916{
917 return to_string<_CharT, char_traits<_CharT>, allocator<_CharT> >(__zero, __one);
918}
919
920template <size_t _Size>
921inline _LIBCPP_INLINE_VISIBILITY
922basic_string<char, char_traits<char>, allocator<char> >
923bitset<_Size>::to_string(char __zero, char __one) const
924{
925 return to_string<char, char_traits<char>, allocator<char> >(__zero, __one);
926}
927
928template <size_t _Size>
929inline _LIBCPP_INLINE_VISIBILITY
930size_t
Howard Hinnant10f25d22011-05-27 20:52:28 +0000931bitset<_Size>::count() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000932{
Howard Hinnant0949eed2011-06-30 21:18:19 +0000933 return static_cast<size_t>(_VSTD::count(base::__make_iter(0), base::__make_iter(_Size), true));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000934}
935
936template <size_t _Size>
937inline _LIBCPP_INLINE_VISIBILITY
938bool
Howard Hinnant10f25d22011-05-27 20:52:28 +0000939bitset<_Size>::operator==(const bitset& __rhs) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000940{
Howard Hinnant0949eed2011-06-30 21:18:19 +0000941 return _VSTD::equal(base::__make_iter(0), base::__make_iter(_Size), __rhs.__make_iter(0));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000942}
943
944template <size_t _Size>
945inline _LIBCPP_INLINE_VISIBILITY
946bool
Howard Hinnant10f25d22011-05-27 20:52:28 +0000947bitset<_Size>::operator!=(const bitset& __rhs) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000948{
949 return !(*this == __rhs);
950}
951
952template <size_t _Size>
953bool
954bitset<_Size>::test(size_t __pos) const
955{
956 if (__pos >= _Size)
957#ifndef _LIBCPP_NO_EXCEPTIONS
958 throw out_of_range("bitset test argument out of range");
959#else
960 assert(!"bitset test argument out of range");
961#endif
962 return (*this)[__pos];
963}
964
965template <size_t _Size>
966inline _LIBCPP_INLINE_VISIBILITY
967bool
Howard Hinnant10f25d22011-05-27 20:52:28 +0000968bitset<_Size>::all() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000969{
970 return base::all();
971}
972
973template <size_t _Size>
974inline _LIBCPP_INLINE_VISIBILITY
975bool
Howard Hinnant10f25d22011-05-27 20:52:28 +0000976bitset<_Size>::any() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000977{
978 return base::any();
979}
980
981template <size_t _Size>
982inline _LIBCPP_INLINE_VISIBILITY
983bitset<_Size>
Howard Hinnant10f25d22011-05-27 20:52:28 +0000984bitset<_Size>::operator<<(size_t __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000985{
986 bitset __r = *this;
987 __r <<= __pos;
988 return __r;
989}
990
991template <size_t _Size>
992inline _LIBCPP_INLINE_VISIBILITY
993bitset<_Size>
Howard Hinnant10f25d22011-05-27 20:52:28 +0000994bitset<_Size>::operator>>(size_t __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000995{
996 bitset __r = *this;
997 __r >>= __pos;
998 return __r;
999}
1000
1001template <size_t _Size>
1002inline _LIBCPP_INLINE_VISIBILITY
1003bitset<_Size>
Howard Hinnant10f25d22011-05-27 20:52:28 +00001004operator&(const bitset<_Size>& __x, const bitset<_Size>& __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001005{
1006 bitset<_Size> __r = __x;
1007 __r &= __y;
1008 return __r;
1009}
1010
1011template <size_t _Size>
1012inline _LIBCPP_INLINE_VISIBILITY
1013bitset<_Size>
Howard Hinnant10f25d22011-05-27 20:52:28 +00001014operator|(const bitset<_Size>& __x, const bitset<_Size>& __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001015{
1016 bitset<_Size> __r = __x;
1017 __r |= __y;
1018 return __r;
1019}
1020
1021template <size_t _Size>
1022inline _LIBCPP_INLINE_VISIBILITY
1023bitset<_Size>
Howard Hinnant10f25d22011-05-27 20:52:28 +00001024operator^(const bitset<_Size>& __x, const bitset<_Size>& __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001025{
1026 bitset<_Size> __r = __x;
1027 __r ^= __y;
1028 return __r;
1029}
1030
1031template <size_t _Size>
Howard Hinnant422a53f2010-09-21 21:28:23 +00001032struct _LIBCPP_VISIBLE hash<bitset<_Size> >
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001033 : public unary_function<bitset<_Size>, size_t>
1034{
Howard Hinnant422a53f2010-09-21 21:28:23 +00001035 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant10f25d22011-05-27 20:52:28 +00001036 size_t operator()(const bitset<_Size>& __bs) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001037 {return __bs.__hash_code();}
1038};
1039
Howard Hinnant464aa5c2011-07-18 15:51:59 +00001040template <class _CharT, class _Traits, size_t _Size>
1041basic_istream<_CharT, _Traits>&
1042operator>>(basic_istream<_CharT, _Traits>& __is, bitset<_Size>& __x);
1043
1044template <class _CharT, class _Traits, size_t _Size>
1045basic_ostream<_CharT, _Traits>&
1046operator<<(basic_ostream<_CharT, _Traits>& __os, const bitset<_Size>& __x);
1047
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001048_LIBCPP_END_NAMESPACE_STD
1049
1050#endif // _LIBCPP_BITSET