blob: c970f32907170394f64fb9f343d1bcd5a69a2b17 [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
132_LIBCPP_BEGIN_NAMESPACE_STD
133
134template <size_t _N_words, size_t _Size>
Howard Hinnantf03c3b42011-07-02 20:33:23 +0000135class __bitset;
136
137template <size_t _N_words, size_t _Size>
138struct __has_storage_type<__bitset<_N_words, _Size> >
139{
140 static const bool value = true;
141};
142
143template <size_t _N_words, size_t _Size>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000144class __bitset
145{
146public:
147 typedef ptrdiff_t difference_type;
148 typedef size_t size_type;
149protected:
150 typedef __bitset __self;
151 typedef size_type __storage_type;
152 typedef __storage_type* __storage_pointer;
153 typedef const __storage_type* __const_storage_pointer;
154 static const unsigned __bits_per_word = static_cast<unsigned>(sizeof(__storage_type) * CHAR_BIT);
155
156 friend class __bit_reference<__bitset>;
157 friend class __bit_const_reference<__bitset>;
158 friend class __bit_iterator<__bitset, false>;
159 friend class __bit_iterator<__bitset, true>;
160 friend class __bit_array<__bitset>;
161
162 __storage_type __first_[_N_words];
163
164 typedef __bit_reference<__bitset> reference;
165 typedef __bit_const_reference<__bitset> const_reference;
166 typedef __bit_iterator<__bitset, false> iterator;
167 typedef __bit_iterator<__bitset, true> const_iterator;
168
Howard Hinnant10f25d22011-05-27 20:52:28 +0000169 __bitset() _NOEXCEPT;
170 explicit __bitset(unsigned long long __v) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000171
Howard Hinnant10f25d22011-05-27 20:52:28 +0000172 _LIBCPP_INLINE_VISIBILITY reference __make_ref(size_t __pos) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000173 {return reference(__first_ + __pos / __bits_per_word, __storage_type(1) << __pos % __bits_per_word);}
Howard Hinnant10f25d22011-05-27 20:52:28 +0000174 _LIBCPP_INLINE_VISIBILITY const_reference __make_ref(size_t __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000175 {return const_reference(__first_ + __pos / __bits_per_word, __storage_type(1) << __pos % __bits_per_word);}
Howard Hinnant10f25d22011-05-27 20:52:28 +0000176 _LIBCPP_INLINE_VISIBILITY iterator __make_iter(size_t __pos) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000177 {return iterator(__first_ + __pos / __bits_per_word, __pos % __bits_per_word);}
Howard Hinnant10f25d22011-05-27 20:52:28 +0000178 _LIBCPP_INLINE_VISIBILITY const_iterator __make_iter(size_t __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000179 {return const_iterator(__first_ + __pos / __bits_per_word, __pos % __bits_per_word);}
180
Howard Hinnant10f25d22011-05-27 20:52:28 +0000181 void operator&=(const __bitset& __v) _NOEXCEPT;
182 void operator|=(const __bitset& __v) _NOEXCEPT;
183 void operator^=(const __bitset& __v) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000184
Howard Hinnant10f25d22011-05-27 20:52:28 +0000185 void flip() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000186 _LIBCPP_INLINE_VISIBILITY unsigned long to_ulong() const
187 {return to_ulong(integral_constant<bool, _Size < sizeof(unsigned long) * CHAR_BIT>());}
188 _LIBCPP_INLINE_VISIBILITY unsigned long long to_ullong() const
189 {return to_ullong(integral_constant<bool, _Size < sizeof(unsigned long long) * CHAR_BIT>());}
190
Howard Hinnant10f25d22011-05-27 20:52:28 +0000191 bool all() const _NOEXCEPT;
192 bool any() const _NOEXCEPT;
193 size_t __hash_code() const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000194private:
Howard Hinnant10f25d22011-05-27 20:52:28 +0000195 void __init(unsigned long long __v, false_type) _NOEXCEPT;
196 void __init(unsigned long long __v, true_type) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000197 unsigned long to_ulong(false_type) const;
198 unsigned long to_ulong(true_type) const;
199 unsigned long long to_ullong(false_type) const;
200 unsigned long long to_ullong(true_type) const;
201 unsigned long long to_ullong(true_type, false_type) const;
202 unsigned long long to_ullong(true_type, true_type) const;
203};
204
205template <size_t _N_words, size_t _Size>
206inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant10f25d22011-05-27 20:52:28 +0000207__bitset<_N_words, _Size>::__bitset() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000208{
Howard Hinnant0949eed2011-06-30 21:18:19 +0000209 _VSTD::fill_n(__first_, _N_words, __storage_type(0));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000210}
211
212template <size_t _N_words, size_t _Size>
213void
214__bitset<_N_words, _Size>::__init(unsigned long long __v, false_type)
215{
216 __storage_type __t[sizeof(unsigned long long) / sizeof(__storage_type)];
217 for (size_t __i = 0; __i < sizeof(__t)/sizeof(__t[0]); ++__i, __v >>= __bits_per_word)
218 __t[__i] = static_cast<__storage_type>(__v);
Howard Hinnant0949eed2011-06-30 21:18:19 +0000219 _VSTD::copy(__t, __t + sizeof(__t)/sizeof(__t[0]), __first_);
220 _VSTD::fill(__first_ + sizeof(__t)/sizeof(__t[0]), __first_ + sizeof(__first_)/sizeof(__first_[0]),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000221 __storage_type(0));
222}
223
224template <size_t _N_words, size_t _Size>
225inline _LIBCPP_INLINE_VISIBILITY
226void
227__bitset<_N_words, _Size>::__init(unsigned long long __v, true_type)
228{
229 __first_[0] = __v;
Howard Hinnant0949eed2011-06-30 21:18:19 +0000230 _VSTD::fill(__first_ + 1, __first_ + sizeof(__first_)/sizeof(__first_[0]), __storage_type(0));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000231}
232
233template <size_t _N_words, size_t _Size>
234inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant10f25d22011-05-27 20:52:28 +0000235__bitset<_N_words, _Size>::__bitset(unsigned long long __v) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000236{
237 __init(__v, integral_constant<bool, sizeof(unsigned long long) == sizeof(__storage_type)>());
238}
239
240template <size_t _N_words, size_t _Size>
241inline _LIBCPP_INLINE_VISIBILITY
242void
Howard Hinnant10f25d22011-05-27 20:52:28 +0000243__bitset<_N_words, _Size>::operator&=(const __bitset& __v) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000244{
245 for (size_type __i = 0; __i < _N_words; ++__i)
246 __first_[__i] &= __v.__first_[__i];
247}
248
249template <size_t _N_words, size_t _Size>
250inline _LIBCPP_INLINE_VISIBILITY
251void
Howard Hinnant10f25d22011-05-27 20:52:28 +0000252__bitset<_N_words, _Size>::operator|=(const __bitset& __v) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000253{
254 for (size_type __i = 0; __i < _N_words; ++__i)
255 __first_[__i] |= __v.__first_[__i];
256}
257
258template <size_t _N_words, size_t _Size>
259inline _LIBCPP_INLINE_VISIBILITY
260void
Howard Hinnant10f25d22011-05-27 20:52:28 +0000261__bitset<_N_words, _Size>::operator^=(const __bitset& __v) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000262{
263 for (size_type __i = 0; __i < _N_words; ++__i)
264 __first_[__i] ^= __v.__first_[__i];
265}
266
267template <size_t _N_words, size_t _Size>
268void
Howard Hinnant10f25d22011-05-27 20:52:28 +0000269__bitset<_N_words, _Size>::flip() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000270{
271 // do middle whole words
272 size_type __n = _Size;
273 __storage_pointer __p = __first_;
274 for (; __n >= __bits_per_word; ++__p, __n -= __bits_per_word)
275 *__p = ~*__p;
276 // do last partial word
277 if (__n > 0)
278 {
279 __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
280 __storage_type __b = *__p & __m;
281 *__p &= ~__m;
282 *__p |= ~__b & __m;
283 }
284}
285
286template <size_t _N_words, size_t _Size>
287unsigned long
288__bitset<_N_words, _Size>::to_ulong(false_type) const
289{
290 const_iterator __e = __make_iter(_Size);
Howard Hinnant0949eed2011-06-30 21:18:19 +0000291 const_iterator __i = _VSTD::find(__make_iter(sizeof(unsigned long) * CHAR_BIT), __e, true);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000292 if (__i != __e)
293#ifndef _LIBCPP_NO_EXCEPTIONS
294 throw overflow_error("bitset to_ulong overflow error");
295#else
296 assert(!"bitset to_ulong overflow error");
297#endif
298 return __first_[0];
299}
300
301template <size_t _N_words, size_t _Size>
302inline _LIBCPP_INLINE_VISIBILITY
303unsigned long
304__bitset<_N_words, _Size>::to_ulong(true_type) const
305{
306 return __first_[0];
307}
308
309template <size_t _N_words, size_t _Size>
310unsigned long long
311__bitset<_N_words, _Size>::to_ullong(false_type) const
312{
313 const_iterator __e = __make_iter(_Size);
Howard Hinnant0949eed2011-06-30 21:18:19 +0000314 const_iterator __i = _VSTD::find(__make_iter(sizeof(unsigned long long) * CHAR_BIT), __e, true);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000315 if (__i != __e)
316#ifndef _LIBCPP_NO_EXCEPTIONS
317 throw overflow_error("bitset to_ullong overflow error");
318#else
319 assert(!"bitset to_ullong overflow error");
320#endif
321 return to_ullong(true_type());
322}
323
324template <size_t _N_words, size_t _Size>
325inline _LIBCPP_INLINE_VISIBILITY
326unsigned long long
327__bitset<_N_words, _Size>::to_ullong(true_type) const
328{
329 return to_ullong(true_type(), integral_constant<bool, sizeof(__storage_type) < sizeof(unsigned long long)>());
330}
331
332template <size_t _N_words, size_t _Size>
333inline _LIBCPP_INLINE_VISIBILITY
334unsigned long long
335__bitset<_N_words, _Size>::to_ullong(true_type, false_type) const
336{
337 return __first_[0];
338}
339
340template <size_t _N_words, size_t _Size>
341unsigned long long
342__bitset<_N_words, _Size>::to_ullong(true_type, true_type) const
343{
344 unsigned long long __r = __first_[0];
345 for (std::size_t __i = 1; __i < sizeof(unsigned long long) / sizeof(__storage_type); ++__i)
346 __r |= static_cast<unsigned long long>(__first_[__i]) << (sizeof(__storage_type) * CHAR_BIT);
347 return __r;
348}
349
350template <size_t _N_words, size_t _Size>
351bool
Howard Hinnant10f25d22011-05-27 20:52:28 +0000352__bitset<_N_words, _Size>::all() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000353{
354 // do middle whole words
355 size_type __n = _Size;
356 __const_storage_pointer __p = __first_;
357 for (; __n >= __bits_per_word; ++__p, __n -= __bits_per_word)
358 if (~*__p)
359 return false;
360 // do last partial word
361 if (__n > 0)
362 {
363 __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
364 if (~*__p & __m)
365 return false;
366 }
367 return true;
368}
369
370template <size_t _N_words, size_t _Size>
371bool
Howard Hinnant10f25d22011-05-27 20:52:28 +0000372__bitset<_N_words, _Size>::any() 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 true;
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 true;
386 }
387 return false;
388}
389
390template <size_t _N_words, size_t _Size>
391inline _LIBCPP_INLINE_VISIBILITY
392size_t
Howard Hinnant10f25d22011-05-27 20:52:28 +0000393__bitset<_N_words, _Size>::__hash_code() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000394{
395 size_t __h = 0;
396 for (size_type __i = 0; __i < _N_words; ++__i)
397 __h ^= __first_[__i];
398 return __h;
399}
400
401template <size_t _Size>
402class __bitset<1, _Size>
403{
404public:
405 typedef ptrdiff_t difference_type;
406 typedef size_t size_type;
407protected:
408 typedef __bitset __self;
409 typedef size_type __storage_type;
410 typedef __storage_type* __storage_pointer;
411 typedef const __storage_type* __const_storage_pointer;
412 static const unsigned __bits_per_word = static_cast<unsigned>(sizeof(__storage_type) * CHAR_BIT);
413
414 friend class __bit_reference<__bitset>;
415 friend class __bit_const_reference<__bitset>;
416 friend class __bit_iterator<__bitset, false>;
417 friend class __bit_iterator<__bitset, true>;
418 friend class __bit_array<__bitset>;
419
420 __storage_type __first_;
421
422 typedef __bit_reference<__bitset> reference;
423 typedef __bit_const_reference<__bitset> const_reference;
424 typedef __bit_iterator<__bitset, false> iterator;
425 typedef __bit_iterator<__bitset, true> const_iterator;
426
Howard Hinnant10f25d22011-05-27 20:52:28 +0000427 __bitset() _NOEXCEPT;
428 explicit __bitset(unsigned long long __v) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000429
Howard Hinnant10f25d22011-05-27 20:52:28 +0000430 _LIBCPP_INLINE_VISIBILITY reference __make_ref(size_t __pos) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000431 {return reference(&__first_, __storage_type(1) << __pos);}
Howard Hinnant10f25d22011-05-27 20:52:28 +0000432 _LIBCPP_INLINE_VISIBILITY const_reference __make_ref(size_t __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000433 {return const_reference(&__first_, __storage_type(1) << __pos);}
Howard Hinnant10f25d22011-05-27 20:52:28 +0000434 _LIBCPP_INLINE_VISIBILITY iterator __make_iter(size_t __pos) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000435 {return iterator(&__first_ + __pos / __bits_per_word, __pos % __bits_per_word);}
Howard Hinnant10f25d22011-05-27 20:52:28 +0000436 _LIBCPP_INLINE_VISIBILITY const_iterator __make_iter(size_t __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000437 {return const_iterator(&__first_ + __pos / __bits_per_word, __pos % __bits_per_word);}
438
Howard Hinnant10f25d22011-05-27 20:52:28 +0000439 void operator&=(const __bitset& __v) _NOEXCEPT;
440 void operator|=(const __bitset& __v) _NOEXCEPT;
441 void operator^=(const __bitset& __v) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000442
Howard Hinnant10f25d22011-05-27 20:52:28 +0000443 void flip() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000444
445 unsigned long to_ulong() const;
446 unsigned long long to_ullong() const;
447
Howard Hinnant10f25d22011-05-27 20:52:28 +0000448 bool all() const _NOEXCEPT;
449 bool any() const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000450
Howard Hinnant10f25d22011-05-27 20:52:28 +0000451 size_t __hash_code() const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000452};
453
454template <size_t _Size>
455inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant10f25d22011-05-27 20:52:28 +0000456__bitset<1, _Size>::__bitset() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000457 : __first_(0)
458{
459}
460
461template <size_t _Size>
462inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant10f25d22011-05-27 20:52:28 +0000463__bitset<1, _Size>::__bitset(unsigned long long __v) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000464 : __first_(static_cast<__storage_type>(__v))
465{
466}
467
468template <size_t _Size>
469inline _LIBCPP_INLINE_VISIBILITY
470void
Howard Hinnant10f25d22011-05-27 20:52:28 +0000471__bitset<1, _Size>::operator&=(const __bitset& __v) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000472{
473 __first_ &= __v.__first_;
474}
475
476template <size_t _Size>
477inline _LIBCPP_INLINE_VISIBILITY
478void
Howard Hinnant10f25d22011-05-27 20:52:28 +0000479__bitset<1, _Size>::operator|=(const __bitset& __v) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000480{
481 __first_ |= __v.__first_;
482}
483
484template <size_t _Size>
485inline _LIBCPP_INLINE_VISIBILITY
486void
Howard Hinnant10f25d22011-05-27 20:52:28 +0000487__bitset<1, _Size>::operator^=(const __bitset& __v) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000488{
489 __first_ ^= __v.__first_;
490}
491
492template <size_t _Size>
493inline _LIBCPP_INLINE_VISIBILITY
494void
Howard Hinnant10f25d22011-05-27 20:52:28 +0000495__bitset<1, _Size>::flip() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000496{
497 __storage_type __m = ~__storage_type(0) >> (__bits_per_word - _Size);
498 __first_ = ~__first_;
499 __first_ &= __m;
500}
501
502template <size_t _Size>
503inline _LIBCPP_INLINE_VISIBILITY
504unsigned long
505__bitset<1, _Size>::to_ulong() const
506{
507 return __first_;
508}
509
510template <size_t _Size>
511inline _LIBCPP_INLINE_VISIBILITY
512unsigned long long
513__bitset<1, _Size>::to_ullong() const
514{
515 return __first_;
516}
517
518template <size_t _Size>
519inline _LIBCPP_INLINE_VISIBILITY
520bool
Howard Hinnant10f25d22011-05-27 20:52:28 +0000521__bitset<1, _Size>::all() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000522{
523 __storage_type __m = ~__storage_type(0) >> (__bits_per_word - _Size);
524 return !(~__first_ & __m);
525}
526
527template <size_t _Size>
528inline _LIBCPP_INLINE_VISIBILITY
529bool
Howard Hinnant10f25d22011-05-27 20:52:28 +0000530__bitset<1, _Size>::any() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000531{
532 __storage_type __m = ~__storage_type(0) >> (__bits_per_word - _Size);
533 return __first_ & __m;
534}
535
536template <size_t _Size>
537inline _LIBCPP_INLINE_VISIBILITY
538size_t
Howard Hinnant10f25d22011-05-27 20:52:28 +0000539__bitset<1, _Size>::__hash_code() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000540{
541 return __first_;
542}
543
544template <>
545class __bitset<0, 0>
546{
547public:
548 typedef ptrdiff_t difference_type;
549 typedef size_t size_type;
550protected:
551 typedef __bitset __self;
552 typedef size_type __storage_type;
553 typedef __storage_type* __storage_pointer;
554 typedef const __storage_type* __const_storage_pointer;
555 static const unsigned __bits_per_word = static_cast<unsigned>(sizeof(__storage_type) * CHAR_BIT);
556
557 friend class __bit_reference<__bitset>;
558 friend class __bit_const_reference<__bitset>;
559 friend class __bit_iterator<__bitset, false>;
560 friend class __bit_iterator<__bitset, true>;
561 friend class __bit_array<__bitset>;
562
563 typedef __bit_reference<__bitset> reference;
564 typedef __bit_const_reference<__bitset> const_reference;
565 typedef __bit_iterator<__bitset, false> iterator;
566 typedef __bit_iterator<__bitset, true> const_iterator;
567
Howard Hinnant10f25d22011-05-27 20:52:28 +0000568 __bitset() _NOEXCEPT;
569 explicit __bitset(unsigned long long) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000570
Howard Hinnant10f25d22011-05-27 20:52:28 +0000571 _LIBCPP_INLINE_VISIBILITY reference __make_ref(size_t) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000572 {return reference(0, 1);}
Howard Hinnant10f25d22011-05-27 20:52:28 +0000573 _LIBCPP_INLINE_VISIBILITY const_reference __make_ref(size_t) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000574 {return const_reference(0, 1);}
Howard Hinnant10f25d22011-05-27 20:52:28 +0000575 _LIBCPP_INLINE_VISIBILITY iterator __make_iter(size_t __pos) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000576 {return iterator(0, 0);}
Howard Hinnant10f25d22011-05-27 20:52:28 +0000577 _LIBCPP_INLINE_VISIBILITY const_iterator __make_iter(size_t __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000578 {return const_iterator(0, 0);}
579
Howard Hinnant10f25d22011-05-27 20:52:28 +0000580 _LIBCPP_INLINE_VISIBILITY void operator&=(const __bitset&) _NOEXCEPT {}
581 _LIBCPP_INLINE_VISIBILITY void operator|=(const __bitset&) _NOEXCEPT {}
582 _LIBCPP_INLINE_VISIBILITY void operator^=(const __bitset&) _NOEXCEPT {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000583
Howard Hinnant10f25d22011-05-27 20:52:28 +0000584 _LIBCPP_INLINE_VISIBILITY void flip() _NOEXCEPT {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000585
586 _LIBCPP_INLINE_VISIBILITY unsigned long to_ulong() const {return 0;}
587 _LIBCPP_INLINE_VISIBILITY unsigned long long to_ullong() const {return 0;}
588
Howard Hinnant10f25d22011-05-27 20:52:28 +0000589 _LIBCPP_INLINE_VISIBILITY bool all() const _NOEXCEPT {return true;}
590 _LIBCPP_INLINE_VISIBILITY bool any() const _NOEXCEPT {return false;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000591
Howard Hinnant10f25d22011-05-27 20:52:28 +0000592 _LIBCPP_INLINE_VISIBILITY size_t __hash_code() const _NOEXCEPT {return 0;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000593};
594
595inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant10f25d22011-05-27 20:52:28 +0000596__bitset<0, 0>::__bitset() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000597{
598}
599
600inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant10f25d22011-05-27 20:52:28 +0000601__bitset<0, 0>::__bitset(unsigned long long) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000602{
603}
604
605template <size_t _Size> class bitset;
606template <size_t _Size> struct hash<bitset<_Size> >;
607
608template <size_t _Size>
Howard Hinnant422a53f2010-09-21 21:28:23 +0000609class _LIBCPP_VISIBLE bitset
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000610 : private __bitset<_Size == 0 ? 0 : (_Size - 1) / (sizeof(size_t) * CHAR_BIT) + 1, _Size>
611{
612 static const unsigned __n_words = _Size == 0 ? 0 : (_Size - 1) / (sizeof(size_t) * CHAR_BIT) + 1;
613 typedef __bitset<__n_words, _Size> base;
614
Howard Hinnant324bb032010-08-22 00:02:43 +0000615public:
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000616 typedef typename base::reference reference;
617 typedef typename base::const_reference const_reference;
618
Howard Hinnant324bb032010-08-22 00:02:43 +0000619 // 23.3.5.1 constructors:
Howard Hinnant10f25d22011-05-27 20:52:28 +0000620 /*constexpr*/ _LIBCPP_INLINE_VISIBILITY bitset() _NOEXCEPT {}
621 /*constexpr*/ _LIBCPP_INLINE_VISIBILITY bitset(unsigned long long __v) _NOEXCEPT : base(__v) {}
Howard Hinnant34d6b192010-11-17 21:53:14 +0000622 template<class _CharT>
623 explicit bitset(const _CharT* __str,
624 typename basic_string<_CharT>::size_type __n = basic_string<_CharT>::npos,
625 _CharT __zero = _CharT('0'), _CharT __one = _CharT('1'));
Howard Hinnant324bb032010-08-22 00:02:43 +0000626 template<class _CharT, class _Traits, class _Allocator>
627 explicit bitset(const basic_string<_CharT,_Traits,_Allocator>& __str,
628 typename basic_string<_CharT,_Traits,_Allocator>::size_type __pos = 0,
629 typename basic_string<_CharT,_Traits,_Allocator>::size_type __n =
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000630 (basic_string<_CharT,_Traits,_Allocator>::npos),
Howard Hinnant324bb032010-08-22 00:02:43 +0000631 _CharT __zero = _CharT('0'), _CharT __one = _CharT('1'));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000632
Howard Hinnant324bb032010-08-22 00:02:43 +0000633 // 23.3.5.2 bitset operations:
Howard Hinnant10f25d22011-05-27 20:52:28 +0000634 bitset& operator&=(const bitset& __rhs) _NOEXCEPT;
635 bitset& operator|=(const bitset& __rhs) _NOEXCEPT;
636 bitset& operator^=(const bitset& __rhs) _NOEXCEPT;
637 bitset& operator<<=(size_t __pos) _NOEXCEPT;
638 bitset& operator>>=(size_t __pos) _NOEXCEPT;
639 bitset& set() _NOEXCEPT;
Howard Hinnant324bb032010-08-22 00:02:43 +0000640 bitset& set(size_t __pos, bool __val = true);
Howard Hinnant10f25d22011-05-27 20:52:28 +0000641 bitset& reset() _NOEXCEPT;
Howard Hinnant324bb032010-08-22 00:02:43 +0000642 bitset& reset(size_t __pos);
Howard Hinnant10f25d22011-05-27 20:52:28 +0000643 bitset operator~() const _NOEXCEPT;
644 bitset& flip() _NOEXCEPT;
Howard Hinnant324bb032010-08-22 00:02:43 +0000645 bitset& flip(size_t __pos);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000646
Howard Hinnant324bb032010-08-22 00:02:43 +0000647 // element access:
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000648 _LIBCPP_INLINE_VISIBILITY const_reference operator[](size_t __p) const {return base::__make_ref(__p);}
649 _LIBCPP_INLINE_VISIBILITY reference operator[](size_t __p) {return base::__make_ref(__p);}
Howard Hinnant324bb032010-08-22 00:02:43 +0000650 unsigned long to_ulong() const;
651 unsigned long long to_ullong() const;
652 template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000653 basic_string<_CharT, _Traits, _Allocator> to_string(_CharT __zero = _CharT('0'),
Howard Hinnant324bb032010-08-22 00:02:43 +0000654 _CharT __one = _CharT('1')) const;
655 template <class _CharT, class _Traits>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000656 basic_string<_CharT, _Traits, allocator<_CharT> > to_string(_CharT __zero = _CharT('0'),
Howard Hinnant324bb032010-08-22 00:02:43 +0000657 _CharT __one = _CharT('1')) const;
658 template <class _CharT>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000659 basic_string<_CharT, char_traits<_CharT>, allocator<_CharT> > to_string(_CharT __zero = _CharT('0'),
Howard Hinnant324bb032010-08-22 00:02:43 +0000660 _CharT __one = _CharT('1')) const;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000661 basic_string<char, char_traits<char>, allocator<char> > to_string(char __zero = '0',
Howard Hinnant324bb032010-08-22 00:02:43 +0000662 char __one = '1') const;
Howard Hinnant10f25d22011-05-27 20:52:28 +0000663 size_t count() const _NOEXCEPT;
664 /*constexpr*/ _LIBCPP_INLINE_VISIBILITY size_t size() const _NOEXCEPT {return _Size;}
665 bool operator==(const bitset& __rhs) const _NOEXCEPT;
666 bool operator!=(const bitset& __rhs) const _NOEXCEPT;
Howard Hinnant324bb032010-08-22 00:02:43 +0000667 bool test(size_t __pos) const;
Howard Hinnant10f25d22011-05-27 20:52:28 +0000668 bool all() const _NOEXCEPT;
669 bool any() const _NOEXCEPT;
670 _LIBCPP_INLINE_VISIBILITY bool none() const _NOEXCEPT {return !any();}
671 bitset operator<<(size_t __pos) const _NOEXCEPT;
672 bitset operator>>(size_t __pos) const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000673
674private:
675
Howard Hinnant422a53f2010-09-21 21:28:23 +0000676 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant10f25d22011-05-27 20:52:28 +0000677 size_t __hash_code() const _NOEXCEPT {return base::__hash_code();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000678
679 friend struct hash<bitset>;
680};
681
682template <size_t _Size>
Howard Hinnant34d6b192010-11-17 21:53:14 +0000683template<class _CharT>
684bitset<_Size>::bitset(const _CharT* __str,
685 typename basic_string<_CharT>::size_type __n,
686 _CharT __zero, _CharT __one)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000687{
Howard Hinnant0949eed2011-06-30 21:18:19 +0000688 size_t __rlen = _VSTD::min(__n, char_traits<_CharT>::length(__str));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000689 for (size_t __i = 0; __i < __rlen; ++__i)
Howard Hinnant34d6b192010-11-17 21:53:14 +0000690 if (__str[__i] != __zero && __str[__i] != __one)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000691#ifndef _LIBCPP_NO_EXCEPTIONS
692 throw invalid_argument("bitset string ctor has invalid argument");
693#else
694 assert(!"bitset string ctor has invalid argument");
695#endif
Howard Hinnant0949eed2011-06-30 21:18:19 +0000696 size_t _M = _VSTD::min(__rlen, _Size);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000697 size_t __i = 0;
698 for (; __i < _M; ++__i)
699 {
Howard Hinnant34d6b192010-11-17 21:53:14 +0000700 _CharT __c = __str[_M - 1 - __i];
701 if (__c == __zero)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000702 (*this)[__i] = false;
Howard Hinnant34d6b192010-11-17 21:53:14 +0000703 else
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000704 (*this)[__i] = true;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000705 }
Howard Hinnant0949eed2011-06-30 21:18:19 +0000706 _VSTD::fill(base::__make_iter(__i), base::__make_iter(_Size), false);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000707}
708
709template <size_t _Size>
Howard Hinnant324bb032010-08-22 00:02:43 +0000710template<class _CharT, class _Traits, class _Allocator>
711bitset<_Size>::bitset(const basic_string<_CharT,_Traits,_Allocator>& __str,
712 typename basic_string<_CharT,_Traits,_Allocator>::size_type __pos,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000713 typename basic_string<_CharT,_Traits,_Allocator>::size_type __n,
714 _CharT __zero, _CharT __one)
715{
716 if (__pos > __str.size())
717#ifndef _LIBCPP_NO_EXCEPTIONS
718 throw out_of_range("bitset string pos out of range");
719#else
720 assert(!"bitset string pos out of range");
721#endif
Howard Hinnant0949eed2011-06-30 21:18:19 +0000722 size_t __rlen = _VSTD::min(__n, __str.size() - __pos);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000723 for (size_t __i = __pos; __i < __pos + __rlen; ++__i)
724 if (!_Traits::eq(__str[__i], __zero) && !_Traits::eq(__str[__i], __one))
725#ifndef _LIBCPP_NO_EXCEPTIONS
726 throw invalid_argument("bitset string ctor has invalid argument");
727#else
728 assert(!"bitset string ctor has invalid argument");
729#endif
Howard Hinnant0949eed2011-06-30 21:18:19 +0000730 size_t _M = _VSTD::min(__rlen, _Size);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000731 size_t __i = 0;
732 for (; __i < _M; ++__i)
733 {
734 _CharT __c = __str[__pos + _M - 1 - __i];
735 if (_Traits::eq(__c, __zero))
736 (*this)[__i] = false;
737 else
738 (*this)[__i] = true;
739 }
Howard Hinnant0949eed2011-06-30 21:18:19 +0000740 _VSTD::fill(base::__make_iter(__i), base::__make_iter(_Size), false);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000741}
742
743template <size_t _Size>
744inline _LIBCPP_INLINE_VISIBILITY
745bitset<_Size>&
Howard Hinnant10f25d22011-05-27 20:52:28 +0000746bitset<_Size>::operator&=(const bitset& __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000747{
748 base::operator&=(__rhs);
749 return *this;
750}
751
752template <size_t _Size>
753inline _LIBCPP_INLINE_VISIBILITY
754bitset<_Size>&
Howard Hinnant10f25d22011-05-27 20:52:28 +0000755bitset<_Size>::operator|=(const bitset& __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000756{
757 base::operator|=(__rhs);
758 return *this;
759}
760
761template <size_t _Size>
762inline _LIBCPP_INLINE_VISIBILITY
763bitset<_Size>&
Howard Hinnant10f25d22011-05-27 20:52:28 +0000764bitset<_Size>::operator^=(const bitset& __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000765{
766 base::operator^=(__rhs);
767 return *this;
768}
769
770template <size_t _Size>
771bitset<_Size>&
Howard Hinnant10f25d22011-05-27 20:52:28 +0000772bitset<_Size>::operator<<=(size_t __pos) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000773{
Howard Hinnant0949eed2011-06-30 21:18:19 +0000774 __pos = _VSTD::min(__pos, _Size);
775 _VSTD::copy_backward(base::__make_iter(0), base::__make_iter(_Size - __pos), base::__make_iter(_Size));
776 _VSTD::fill_n(base::__make_iter(0), __pos, false);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000777 return *this;
778}
779
780template <size_t _Size>
781bitset<_Size>&
Howard Hinnant10f25d22011-05-27 20:52:28 +0000782bitset<_Size>::operator>>=(size_t __pos) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000783{
Howard Hinnant0949eed2011-06-30 21:18:19 +0000784 __pos = _VSTD::min(__pos, _Size);
785 _VSTD::copy(base::__make_iter(__pos), base::__make_iter(_Size), base::__make_iter(0));
786 _VSTD::fill_n(base::__make_iter(_Size - __pos), __pos, false);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000787 return *this;
788}
789
790template <size_t _Size>
791inline _LIBCPP_INLINE_VISIBILITY
792bitset<_Size>&
Howard Hinnant10f25d22011-05-27 20:52:28 +0000793bitset<_Size>::set() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000794{
Howard Hinnant0949eed2011-06-30 21:18:19 +0000795 _VSTD::fill_n(base::__make_iter(0), _Size, true);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000796 return *this;
797}
798
799template <size_t _Size>
800bitset<_Size>&
801bitset<_Size>::set(size_t __pos, bool __val)
802{
803 if (__pos >= _Size)
804#ifndef _LIBCPP_NO_EXCEPTIONS
805 throw out_of_range("bitset set argument out of range");
806#else
807 assert(!"bitset set argument out of range");
808#endif
809 (*this)[__pos] = __val;
810 return *this;
811}
812
813template <size_t _Size>
814inline _LIBCPP_INLINE_VISIBILITY
815bitset<_Size>&
Howard Hinnant10f25d22011-05-27 20:52:28 +0000816bitset<_Size>::reset() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000817{
Howard Hinnant0949eed2011-06-30 21:18:19 +0000818 _VSTD::fill_n(base::__make_iter(0), _Size, false);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000819 return *this;
820}
821
822template <size_t _Size>
823bitset<_Size>&
824bitset<_Size>::reset(size_t __pos)
825{
826 if (__pos >= _Size)
827#ifndef _LIBCPP_NO_EXCEPTIONS
828 throw out_of_range("bitset reset argument out of range");
829#else
830 assert(!"bitset reset argument out of range");
831#endif
832 (*this)[__pos] = false;
833 return *this;
834}
835
836template <size_t _Size>
837inline _LIBCPP_INLINE_VISIBILITY
838bitset<_Size>
Howard Hinnant10f25d22011-05-27 20:52:28 +0000839bitset<_Size>::operator~() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000840{
841 bitset __x(*this);
842 __x.flip();
843 return __x;
844}
845
846template <size_t _Size>
847inline _LIBCPP_INLINE_VISIBILITY
848bitset<_Size>&
Howard Hinnant10f25d22011-05-27 20:52:28 +0000849bitset<_Size>::flip() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000850{
851 base::flip();
852 return *this;
853}
854
855template <size_t _Size>
856bitset<_Size>&
857bitset<_Size>::flip(size_t __pos)
858{
859 if (__pos >= _Size)
860#ifndef _LIBCPP_NO_EXCEPTIONS
861 throw out_of_range("bitset flip argument out of range");
862#else
863 assert(!"bitset flip argument out of range");
864#endif
865 reference r = base::__make_ref(__pos);
866 r = ~r;
867 return *this;
868}
869
870template <size_t _Size>
871inline _LIBCPP_INLINE_VISIBILITY
872unsigned long
873bitset<_Size>::to_ulong() const
874{
875 return base::to_ulong();
876}
877
878template <size_t _Size>
879inline _LIBCPP_INLINE_VISIBILITY
880unsigned long long
881bitset<_Size>::to_ullong() const
882{
883 return base::to_ullong();
884}
885
886template <size_t _Size>
Howard Hinnant324bb032010-08-22 00:02:43 +0000887template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000888basic_string<_CharT, _Traits, _Allocator>
889bitset<_Size>::to_string(_CharT __zero, _CharT __one) const
890{
891 basic_string<_CharT, _Traits, _Allocator> __r(_Size, __zero);
892 for (size_t __i = 0; __i < _Size; ++__i)
893 {
894 if ((*this)[__i])
895 __r[_Size - 1 - __i] = __one;
896 }
897 return __r;
898}
899
900template <size_t _Size>
Howard Hinnant324bb032010-08-22 00:02:43 +0000901template <class _CharT, class _Traits>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000902inline _LIBCPP_INLINE_VISIBILITY
903basic_string<_CharT, _Traits, allocator<_CharT> >
904bitset<_Size>::to_string(_CharT __zero, _CharT __one) const
905{
906 return to_string<_CharT, _Traits, allocator<_CharT> >(__zero, __one);
907}
908
909template <size_t _Size>
Howard Hinnant324bb032010-08-22 00:02:43 +0000910template <class _CharT>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000911inline _LIBCPP_INLINE_VISIBILITY
912basic_string<_CharT, char_traits<_CharT>, allocator<_CharT> >
913bitset<_Size>::to_string(_CharT __zero, _CharT __one) const
914{
915 return to_string<_CharT, char_traits<_CharT>, allocator<_CharT> >(__zero, __one);
916}
917
918template <size_t _Size>
919inline _LIBCPP_INLINE_VISIBILITY
920basic_string<char, char_traits<char>, allocator<char> >
921bitset<_Size>::to_string(char __zero, char __one) const
922{
923 return to_string<char, char_traits<char>, allocator<char> >(__zero, __one);
924}
925
926template <size_t _Size>
927inline _LIBCPP_INLINE_VISIBILITY
928size_t
Howard Hinnant10f25d22011-05-27 20:52:28 +0000929bitset<_Size>::count() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000930{
Howard Hinnant0949eed2011-06-30 21:18:19 +0000931 return static_cast<size_t>(_VSTD::count(base::__make_iter(0), base::__make_iter(_Size), true));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000932}
933
934template <size_t _Size>
935inline _LIBCPP_INLINE_VISIBILITY
936bool
Howard Hinnant10f25d22011-05-27 20:52:28 +0000937bitset<_Size>::operator==(const bitset& __rhs) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000938{
Howard Hinnant0949eed2011-06-30 21:18:19 +0000939 return _VSTD::equal(base::__make_iter(0), base::__make_iter(_Size), __rhs.__make_iter(0));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000940}
941
942template <size_t _Size>
943inline _LIBCPP_INLINE_VISIBILITY
944bool
Howard Hinnant10f25d22011-05-27 20:52:28 +0000945bitset<_Size>::operator!=(const bitset& __rhs) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000946{
947 return !(*this == __rhs);
948}
949
950template <size_t _Size>
951bool
952bitset<_Size>::test(size_t __pos) const
953{
954 if (__pos >= _Size)
955#ifndef _LIBCPP_NO_EXCEPTIONS
956 throw out_of_range("bitset test argument out of range");
957#else
958 assert(!"bitset test argument out of range");
959#endif
960 return (*this)[__pos];
961}
962
963template <size_t _Size>
964inline _LIBCPP_INLINE_VISIBILITY
965bool
Howard Hinnant10f25d22011-05-27 20:52:28 +0000966bitset<_Size>::all() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000967{
968 return base::all();
969}
970
971template <size_t _Size>
972inline _LIBCPP_INLINE_VISIBILITY
973bool
Howard Hinnant10f25d22011-05-27 20:52:28 +0000974bitset<_Size>::any() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000975{
976 return base::any();
977}
978
979template <size_t _Size>
980inline _LIBCPP_INLINE_VISIBILITY
981bitset<_Size>
Howard Hinnant10f25d22011-05-27 20:52:28 +0000982bitset<_Size>::operator<<(size_t __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000983{
984 bitset __r = *this;
985 __r <<= __pos;
986 return __r;
987}
988
989template <size_t _Size>
990inline _LIBCPP_INLINE_VISIBILITY
991bitset<_Size>
Howard Hinnant10f25d22011-05-27 20:52:28 +0000992bitset<_Size>::operator>>(size_t __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000993{
994 bitset __r = *this;
995 __r >>= __pos;
996 return __r;
997}
998
999template <size_t _Size>
1000inline _LIBCPP_INLINE_VISIBILITY
1001bitset<_Size>
Howard Hinnant10f25d22011-05-27 20:52:28 +00001002operator&(const bitset<_Size>& __x, const bitset<_Size>& __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001003{
1004 bitset<_Size> __r = __x;
1005 __r &= __y;
1006 return __r;
1007}
1008
1009template <size_t _Size>
1010inline _LIBCPP_INLINE_VISIBILITY
1011bitset<_Size>
Howard Hinnant10f25d22011-05-27 20:52:28 +00001012operator|(const bitset<_Size>& __x, const bitset<_Size>& __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001013{
1014 bitset<_Size> __r = __x;
1015 __r |= __y;
1016 return __r;
1017}
1018
1019template <size_t _Size>
1020inline _LIBCPP_INLINE_VISIBILITY
1021bitset<_Size>
Howard Hinnant10f25d22011-05-27 20:52:28 +00001022operator^(const bitset<_Size>& __x, const bitset<_Size>& __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001023{
1024 bitset<_Size> __r = __x;
1025 __r ^= __y;
1026 return __r;
1027}
1028
1029template <size_t _Size>
Howard Hinnant422a53f2010-09-21 21:28:23 +00001030struct _LIBCPP_VISIBLE hash<bitset<_Size> >
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001031 : public unary_function<bitset<_Size>, size_t>
1032{
Howard Hinnant422a53f2010-09-21 21:28:23 +00001033 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant10f25d22011-05-27 20:52:28 +00001034 size_t operator()(const bitset<_Size>& __bs) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001035 {return __bs.__hash_code();}
1036};
1037
Howard Hinnant464aa5c2011-07-18 15:51:59 +00001038template <class _CharT, class _Traits, size_t _Size>
1039basic_istream<_CharT, _Traits>&
1040operator>>(basic_istream<_CharT, _Traits>& __is, bitset<_Size>& __x);
1041
1042template <class _CharT, class _Traits, size_t _Size>
1043basic_ostream<_CharT, _Traits>&
1044operator<<(basic_ostream<_CharT, _Traits>& __os, const bitset<_Size>& __x);
1045
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001046_LIBCPP_END_NAMESPACE_STD
1047
1048#endif // _LIBCPP_BITSET