blob: 37f9fee76019e33800a91494d48c9331f3a7759c [file] [log] [blame]
Howard Hinnant3e519522010-05-11 19:42:161// -*- C++ -*-
2//===---------------------------- bitset ----------------------------------===//
3//
Howard Hinnant5b08a8a2010-05-11 21:36:014// The LLVM Compiler Infrastructure
Howard Hinnant3e519522010-05-11 19:42:165//
Howard Hinnant412dbeb2010-11-16 22:09:026// This file is dual licensed under the MIT and the University of Illinois Open
7// Source Licenses. See LICENSE.TXT for details.
Howard Hinnant3e519522010-05-11 19:42:168//
9//===----------------------------------------------------------------------===//
10
11#ifndef _LIBCPP_BITSET
12#define _LIBCPP_BITSET
13
14/*
15 bitset synopsis
16
17namespace std
18{
19
20namespace std {
21
22template <size_t N>
23class bitset
24{
25public:
26 // bit reference:
27 class reference
28 {
29 friend class bitset;
Howard Hinnantd368a842011-05-27 20:52:2830 reference() noexcept;
Howard Hinnant3e519522010-05-11 19:42:1631 public:
Howard Hinnantd368a842011-05-27 20:52:2832 ~reference() noexcept;
33 reference& operator=(bool x) noexcept; // for b[i] = x;
34 reference& operator=(const reference&) noexcept; // for b[i] = b[j];
35 bool operator~() const noexcept; // flips the bit
36 operator bool() const noexcept; // for x = b[i];
37 reference& flip() noexcept; // for b[i].flip();
Howard Hinnant3e519522010-05-11 19:42:1638 };
39
40 // 23.3.5.1 constructors:
Howard Hinnantd368a842011-05-27 20:52:2841 constexpr bitset() noexcept;
42 constexpr bitset(unsigned long long val) noexcept;
Howard Hinnantd09f7112010-11-17 21:53:1443 template <class charT>
44 explicit bitset(const charT* str,
45 typename basic_string<charT>::size_type n = basic_string<charT>::npos,
46 charT zero = charT('0'), charT one = charT('1'));
Howard Hinnant3e519522010-05-11 19:42:1647 template<class charT, class traits, class Allocator>
48 explicit bitset(const basic_string<charT,traits,Allocator>& str,
49 typename basic_string<charT,traits,Allocator>::size_type pos = 0,
50 typename basic_string<charT,traits,Allocator>::size_type n =
51 basic_string<charT,traits,Allocator>::npos,
52 charT zero = charT('0'), charT one = charT('1'));
53
54 // 23.3.5.2 bitset operations:
Howard Hinnantd368a842011-05-27 20:52:2855 bitset& operator&=(const bitset& rhs) noexcept;
56 bitset& operator|=(const bitset& rhs) noexcept;
57 bitset& operator^=(const bitset& rhs) noexcept;
58 bitset& operator<<=(size_t pos) noexcept;
59 bitset& operator>>=(size_t pos) noexcept;
60 bitset& set() noexcept;
Howard Hinnant3e519522010-05-11 19:42:1661 bitset& set(size_t pos, bool val = true);
Howard Hinnantd368a842011-05-27 20:52:2862 bitset& reset() noexcept;
Howard Hinnant3e519522010-05-11 19:42:1663 bitset& reset(size_t pos);
Howard Hinnantd368a842011-05-27 20:52:2864 bitset operator~() const noexcept;
65 bitset& flip() noexcept;
Howard Hinnant3e519522010-05-11 19:42:1666 bitset& flip(size_t pos);
67
68 // element access:
69 constexpr bool operator[](size_t pos) const; // for b[i];
70 reference operator[](size_t pos); // for b[i];
71 unsigned long to_ulong() const;
72 unsigned long long to_ullong() const;
73 template <class charT, class traits, class Allocator>
74 basic_string<charT, traits, Allocator> to_string(charT zero = charT('0'), charT one = charT('1')) const;
75 template <class charT, class traits>
76 basic_string<charT, traits, allocator<charT> > to_string(charT zero = charT('0'), charT one = charT('1')) const;
77 template <class charT>
78 basic_string<charT, char_traits<charT>, allocator<charT> > to_string(charT zero = charT('0'), charT one = charT('1')) const;
79 basic_string<char, char_traits<char>, allocator<char> > to_string(char zero = '0', char one = '1') const;
Howard Hinnantd368a842011-05-27 20:52:2880 size_t count() const noexcept;
81 constexpr size_t size() const noexcept;
82 bool operator==(const bitset& rhs) const noexcept;
83 bool operator!=(const bitset& rhs) const noexcept;
Howard Hinnant3e519522010-05-11 19:42:1684 bool test(size_t pos) const;
Howard Hinnantd368a842011-05-27 20:52:2885 bool all() const noexcept;
86 bool any() const noexcept;
87 bool none() const noexcept;
88 bitset operator<<(size_t pos) const noexcept;
89 bitset operator>>(size_t pos) const noexcept;
Howard Hinnant3e519522010-05-11 19:42:1690};
91
92// 23.3.5.3 bitset operators:
93template <size_t N>
Howard Hinnantd368a842011-05-27 20:52:2894bitset<N> operator&(const bitset<N>&, const bitset<N>&) noexcept;
Howard Hinnant3e519522010-05-11 19:42:1695
96template <size_t N>
Howard Hinnantd368a842011-05-27 20:52:2897bitset<N> operator|(const bitset<N>&, const bitset<N>&) noexcept;
Howard Hinnant3e519522010-05-11 19:42:1698
99template <size_t N>
Howard Hinnantd368a842011-05-27 20:52:28100bitset<N> operator^(const bitset<N>&, const bitset<N>&) noexcept;
Howard Hinnant3e519522010-05-11 19:42:16101
102template <class charT, class traits, size_t N>
103basic_istream<charT, traits>&
104operator>>(basic_istream<charT, traits>& is, bitset<N>& x);
105
106template <class charT, class traits, size_t N>
107basic_ostream<charT, traits>&
108operator<<(basic_ostream<charT, traits>& os, const bitset<N>& x);
109
110template <size_t N> struct hash<std::bitset<N>>;
111
112} // std
113
114*/
115
Howard Hinnant073458b2011-10-17 20:05:10116#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnant3e519522010-05-11 19:42:16117#pragma GCC system_header
Howard Hinnant073458b2011-10-17 20:05:10118#endif
Howard Hinnant3e519522010-05-11 19:42:16119
120#include <__config>
121#include <__bit_reference>
122#include <cstddef>
123#include <climits>
124#include <string>
125#include <stdexcept>
126#include <iosfwd>
127#include <__functional_base>
128#if defined(_LIBCPP_NO_EXCEPTIONS)
129 #include <cassert>
130#endif
131
Howard Hinnantab4f4382011-11-29 16:45:27132#include <__undef_min_max>
133
Howard Hinnant3e519522010-05-11 19:42:16134_LIBCPP_BEGIN_NAMESPACE_STD
135
136template <size_t _N_words, size_t _Size>
Howard Hinnanta7744562011-07-02 20:33:23137class __bitset;
138
139template <size_t _N_words, size_t _Size>
140struct __has_storage_type<__bitset<_N_words, _Size> >
141{
142 static const bool value = true;
143};
144
145template <size_t _N_words, size_t _Size>
Howard Hinnant3e519522010-05-11 19:42:16146class __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 Hinnantd368a842011-05-27 20:52:28171 __bitset() _NOEXCEPT;
172 explicit __bitset(unsigned long long __v) _NOEXCEPT;
Howard Hinnant3e519522010-05-11 19:42:16173
Howard Hinnantd368a842011-05-27 20:52:28174 _LIBCPP_INLINE_VISIBILITY reference __make_ref(size_t __pos) _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16175 {return reference(__first_ + __pos / __bits_per_word, __storage_type(1) << __pos % __bits_per_word);}
Howard Hinnantd368a842011-05-27 20:52:28176 _LIBCPP_INLINE_VISIBILITY const_reference __make_ref(size_t __pos) const _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16177 {return const_reference(__first_ + __pos / __bits_per_word, __storage_type(1) << __pos % __bits_per_word);}
Howard Hinnantd368a842011-05-27 20:52:28178 _LIBCPP_INLINE_VISIBILITY iterator __make_iter(size_t __pos) _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16179 {return iterator(__first_ + __pos / __bits_per_word, __pos % __bits_per_word);}
Howard Hinnantd368a842011-05-27 20:52:28180 _LIBCPP_INLINE_VISIBILITY const_iterator __make_iter(size_t __pos) const _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16181 {return const_iterator(__first_ + __pos / __bits_per_word, __pos % __bits_per_word);}
182
Howard Hinnantd368a842011-05-27 20:52:28183 void operator&=(const __bitset& __v) _NOEXCEPT;
184 void operator|=(const __bitset& __v) _NOEXCEPT;
185 void operator^=(const __bitset& __v) _NOEXCEPT;
Howard Hinnant3e519522010-05-11 19:42:16186
Howard Hinnantd368a842011-05-27 20:52:28187 void flip() _NOEXCEPT;
Howard Hinnant3e519522010-05-11 19:42:16188 _LIBCPP_INLINE_VISIBILITY unsigned long to_ulong() const
189 {return to_ulong(integral_constant<bool, _Size < sizeof(unsigned long) * CHAR_BIT>());}
190 _LIBCPP_INLINE_VISIBILITY unsigned long long to_ullong() const
191 {return to_ullong(integral_constant<bool, _Size < sizeof(unsigned long long) * CHAR_BIT>());}
192
Howard Hinnantd368a842011-05-27 20:52:28193 bool all() const _NOEXCEPT;
194 bool any() const _NOEXCEPT;
195 size_t __hash_code() const _NOEXCEPT;
Howard Hinnant3e519522010-05-11 19:42:16196private:
Howard Hinnantd368a842011-05-27 20:52:28197 void __init(unsigned long long __v, false_type) _NOEXCEPT;
198 void __init(unsigned long long __v, true_type) _NOEXCEPT;
Howard Hinnant3e519522010-05-11 19:42:16199 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 Hinnantd368a842011-05-27 20:52:28209__bitset<_N_words, _Size>::__bitset() _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16210{
Howard Hinnantce48a112011-06-30 21:18:19211 _VSTD::fill_n(__first_, _N_words, __storage_type(0));
Howard Hinnant3e519522010-05-11 19:42:16212}
213
214template <size_t _N_words, size_t _Size>
215void
216__bitset<_N_words, _Size>::__init(unsigned long long __v, false_type)
217{
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 Hinnantce48a112011-06-30 21:18:19221 _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 Hinnant3e519522010-05-11 19:42:16223 __storage_type(0));
224}
225
226template <size_t _N_words, size_t _Size>
227inline _LIBCPP_INLINE_VISIBILITY
228void
229__bitset<_N_words, _Size>::__init(unsigned long long __v, true_type)
230{
231 __first_[0] = __v;
Howard Hinnantce48a112011-06-30 21:18:19232 _VSTD::fill(__first_ + 1, __first_ + sizeof(__first_)/sizeof(__first_[0]), __storage_type(0));
Howard Hinnant3e519522010-05-11 19:42:16233}
234
235template <size_t _N_words, size_t _Size>
236inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd368a842011-05-27 20:52:28237__bitset<_N_words, _Size>::__bitset(unsigned long long __v) _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16238{
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 Hinnantd368a842011-05-27 20:52:28245__bitset<_N_words, _Size>::operator&=(const __bitset& __v) _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16246{
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 Hinnantd368a842011-05-27 20:52:28254__bitset<_N_words, _Size>::operator|=(const __bitset& __v) _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16255{
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 Hinnantd368a842011-05-27 20:52:28263__bitset<_N_words, _Size>::operator^=(const __bitset& __v) _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16264{
265 for (size_type __i = 0; __i < _N_words; ++__i)
266 __first_[__i] ^= __v.__first_[__i];
267}
268
269template <size_t _N_words, size_t _Size>
270void
Howard Hinnantd368a842011-05-27 20:52:28271__bitset<_N_words, _Size>::flip() _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16272{
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 Hinnantce48a112011-06-30 21:18:19293 const_iterator __i = _VSTD::find(__make_iter(sizeof(unsigned long) * CHAR_BIT), __e, true);
Howard Hinnant3e519522010-05-11 19:42:16294 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 Hinnantce48a112011-06-30 21:18:19316 const_iterator __i = _VSTD::find(__make_iter(sizeof(unsigned long long) * CHAR_BIT), __e, true);
Howard Hinnant3e519522010-05-11 19:42:16317 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 Hinnantd368a842011-05-27 20:52:28354__bitset<_N_words, _Size>::all() const _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16355{
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 Hinnantd368a842011-05-27 20:52:28374__bitset<_N_words, _Size>::any() const _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16375{
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 Hinnantd368a842011-05-27 20:52:28395__bitset<_N_words, _Size>::__hash_code() const _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16396{
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 Hinnantd368a842011-05-27 20:52:28429 __bitset() _NOEXCEPT;
430 explicit __bitset(unsigned long long __v) _NOEXCEPT;
Howard Hinnant3e519522010-05-11 19:42:16431
Howard Hinnantd368a842011-05-27 20:52:28432 _LIBCPP_INLINE_VISIBILITY reference __make_ref(size_t __pos) _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16433 {return reference(&__first_, __storage_type(1) << __pos);}
Howard Hinnantd368a842011-05-27 20:52:28434 _LIBCPP_INLINE_VISIBILITY const_reference __make_ref(size_t __pos) const _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16435 {return const_reference(&__first_, __storage_type(1) << __pos);}
Howard Hinnantd368a842011-05-27 20:52:28436 _LIBCPP_INLINE_VISIBILITY iterator __make_iter(size_t __pos) _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16437 {return iterator(&__first_ + __pos / __bits_per_word, __pos % __bits_per_word);}
Howard Hinnantd368a842011-05-27 20:52:28438 _LIBCPP_INLINE_VISIBILITY const_iterator __make_iter(size_t __pos) const _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16439 {return const_iterator(&__first_ + __pos / __bits_per_word, __pos % __bits_per_word);}
440
Howard Hinnantd368a842011-05-27 20:52:28441 void operator&=(const __bitset& __v) _NOEXCEPT;
442 void operator|=(const __bitset& __v) _NOEXCEPT;
443 void operator^=(const __bitset& __v) _NOEXCEPT;
Howard Hinnant3e519522010-05-11 19:42:16444
Howard Hinnantd368a842011-05-27 20:52:28445 void flip() _NOEXCEPT;
Howard Hinnant3e519522010-05-11 19:42:16446
447 unsigned long to_ulong() const;
448 unsigned long long to_ullong() const;
449
Howard Hinnantd368a842011-05-27 20:52:28450 bool all() const _NOEXCEPT;
451 bool any() const _NOEXCEPT;
Howard Hinnant3e519522010-05-11 19:42:16452
Howard Hinnantd368a842011-05-27 20:52:28453 size_t __hash_code() const _NOEXCEPT;
Howard Hinnant3e519522010-05-11 19:42:16454};
455
456template <size_t _Size>
457inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd368a842011-05-27 20:52:28458__bitset<1, _Size>::__bitset() _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16459 : __first_(0)
460{
461}
462
463template <size_t _Size>
464inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd368a842011-05-27 20:52:28465__bitset<1, _Size>::__bitset(unsigned long long __v) _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16466 : __first_(static_cast<__storage_type>(__v))
467{
468}
469
470template <size_t _Size>
471inline _LIBCPP_INLINE_VISIBILITY
472void
Howard Hinnantd368a842011-05-27 20:52:28473__bitset<1, _Size>::operator&=(const __bitset& __v) _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16474{
475 __first_ &= __v.__first_;
476}
477
478template <size_t _Size>
479inline _LIBCPP_INLINE_VISIBILITY
480void
Howard Hinnantd368a842011-05-27 20:52:28481__bitset<1, _Size>::operator|=(const __bitset& __v) _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16482{
483 __first_ |= __v.__first_;
484}
485
486template <size_t _Size>
487inline _LIBCPP_INLINE_VISIBILITY
488void
Howard Hinnantd368a842011-05-27 20:52:28489__bitset<1, _Size>::operator^=(const __bitset& __v) _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16490{
491 __first_ ^= __v.__first_;
492}
493
494template <size_t _Size>
495inline _LIBCPP_INLINE_VISIBILITY
496void
Howard Hinnantd368a842011-05-27 20:52:28497__bitset<1, _Size>::flip() _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16498{
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 Hinnantd368a842011-05-27 20:52:28523__bitset<1, _Size>::all() const _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16524{
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 Hinnantd368a842011-05-27 20:52:28532__bitset<1, _Size>::any() const _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16533{
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 Hinnantd368a842011-05-27 20:52:28541__bitset<1, _Size>::__hash_code() const _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16542{
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>;
563 friend class __bit_array<__bitset>;
564
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 Hinnantd368a842011-05-27 20:52:28570 __bitset() _NOEXCEPT;
571 explicit __bitset(unsigned long long) _NOEXCEPT;
Howard Hinnant3e519522010-05-11 19:42:16572
Howard Hinnantd368a842011-05-27 20:52:28573 _LIBCPP_INLINE_VISIBILITY reference __make_ref(size_t) _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16574 {return reference(0, 1);}
Howard Hinnantd368a842011-05-27 20:52:28575 _LIBCPP_INLINE_VISIBILITY const_reference __make_ref(size_t) const _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16576 {return const_reference(0, 1);}
Howard Hinnantd368a842011-05-27 20:52:28577 _LIBCPP_INLINE_VISIBILITY iterator __make_iter(size_t __pos) _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16578 {return iterator(0, 0);}
Howard Hinnantd368a842011-05-27 20:52:28579 _LIBCPP_INLINE_VISIBILITY const_iterator __make_iter(size_t __pos) const _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16580 {return const_iterator(0, 0);}
581
Howard Hinnantd368a842011-05-27 20:52:28582 _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 Hinnant3e519522010-05-11 19:42:16585
Howard Hinnantd368a842011-05-27 20:52:28586 _LIBCPP_INLINE_VISIBILITY void flip() _NOEXCEPT {}
Howard Hinnant3e519522010-05-11 19:42:16587
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 Hinnantd368a842011-05-27 20:52:28591 _LIBCPP_INLINE_VISIBILITY bool all() const _NOEXCEPT {return true;}
592 _LIBCPP_INLINE_VISIBILITY bool any() const _NOEXCEPT {return false;}
Howard Hinnant3e519522010-05-11 19:42:16593
Howard Hinnantd368a842011-05-27 20:52:28594 _LIBCPP_INLINE_VISIBILITY size_t __hash_code() const _NOEXCEPT {return 0;}
Howard Hinnant3e519522010-05-11 19:42:16595};
596
597inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd368a842011-05-27 20:52:28598__bitset<0, 0>::__bitset() _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16599{
600}
601
602inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd368a842011-05-27 20:52:28603__bitset<0, 0>::__bitset(unsigned long long) _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16604{
605}
606
607template <size_t _Size> class bitset;
608template <size_t _Size> struct hash<bitset<_Size> >;
609
610template <size_t _Size>
Howard Hinnantfb100022010-09-21 21:28:23611class _LIBCPP_VISIBLE bitset
Howard Hinnant3e519522010-05-11 19:42:16612 : 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 Hinnantb3371f62010-08-22 00:02:43617public:
Howard Hinnant3e519522010-05-11 19:42:16618 typedef typename base::reference reference;
619 typedef typename base::const_reference const_reference;
620
Howard Hinnantb3371f62010-08-22 00:02:43621 // 23.3.5.1 constructors:
Howard Hinnantd368a842011-05-27 20:52:28622 /*constexpr*/ _LIBCPP_INLINE_VISIBILITY bitset() _NOEXCEPT {}
623 /*constexpr*/ _LIBCPP_INLINE_VISIBILITY bitset(unsigned long long __v) _NOEXCEPT : base(__v) {}
Howard Hinnantd09f7112010-11-17 21:53:14624 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 Hinnantb3371f62010-08-22 00:02:43628 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 Hinnant3e519522010-05-11 19:42:16632 (basic_string<_CharT,_Traits,_Allocator>::npos),
Howard Hinnantb3371f62010-08-22 00:02:43633 _CharT __zero = _CharT('0'), _CharT __one = _CharT('1'));
Howard Hinnant3e519522010-05-11 19:42:16634
Howard Hinnantb3371f62010-08-22 00:02:43635 // 23.3.5.2 bitset operations:
Howard Hinnantd368a842011-05-27 20:52:28636 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 Hinnantb3371f62010-08-22 00:02:43642 bitset& set(size_t __pos, bool __val = true);
Howard Hinnantd368a842011-05-27 20:52:28643 bitset& reset() _NOEXCEPT;
Howard Hinnantb3371f62010-08-22 00:02:43644 bitset& reset(size_t __pos);
Howard Hinnantd368a842011-05-27 20:52:28645 bitset operator~() const _NOEXCEPT;
646 bitset& flip() _NOEXCEPT;
Howard Hinnantb3371f62010-08-22 00:02:43647 bitset& flip(size_t __pos);
Howard Hinnant3e519522010-05-11 19:42:16648
Howard Hinnantb3371f62010-08-22 00:02:43649 // element access:
Howard Hinnant3e519522010-05-11 19:42:16650 _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 Hinnantb3371f62010-08-22 00:02:43652 unsigned long to_ulong() const;
653 unsigned long long to_ullong() const;
654 template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant3e519522010-05-11 19:42:16655 basic_string<_CharT, _Traits, _Allocator> to_string(_CharT __zero = _CharT('0'),
Howard Hinnantb3371f62010-08-22 00:02:43656 _CharT __one = _CharT('1')) const;
657 template <class _CharT, class _Traits>
Howard Hinnant3e519522010-05-11 19:42:16658 basic_string<_CharT, _Traits, allocator<_CharT> > to_string(_CharT __zero = _CharT('0'),
Howard Hinnantb3371f62010-08-22 00:02:43659 _CharT __one = _CharT('1')) const;
660 template <class _CharT>
Howard Hinnant3e519522010-05-11 19:42:16661 basic_string<_CharT, char_traits<_CharT>, allocator<_CharT> > to_string(_CharT __zero = _CharT('0'),
Howard Hinnantb3371f62010-08-22 00:02:43662 _CharT __one = _CharT('1')) const;
Howard Hinnant3e519522010-05-11 19:42:16663 basic_string<char, char_traits<char>, allocator<char> > to_string(char __zero = '0',
Howard Hinnantb3371f62010-08-22 00:02:43664 char __one = '1') const;
Howard Hinnantd368a842011-05-27 20:52:28665 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 Hinnantb3371f62010-08-22 00:02:43669 bool test(size_t __pos) const;
Howard Hinnantd368a842011-05-27 20:52:28670 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 Hinnant3e519522010-05-11 19:42:16675
676private:
677
Howard Hinnantfb100022010-09-21 21:28:23678 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd368a842011-05-27 20:52:28679 size_t __hash_code() const _NOEXCEPT {return base::__hash_code();}
Howard Hinnant3e519522010-05-11 19:42:16680
681 friend struct hash<bitset>;
682};
683
684template <size_t _Size>
Howard Hinnantd09f7112010-11-17 21:53:14685template<class _CharT>
686bitset<_Size>::bitset(const _CharT* __str,
687 typename basic_string<_CharT>::size_type __n,
688 _CharT __zero, _CharT __one)
Howard Hinnant3e519522010-05-11 19:42:16689{
Howard Hinnantce48a112011-06-30 21:18:19690 size_t __rlen = _VSTD::min(__n, char_traits<_CharT>::length(__str));
Howard Hinnant3e519522010-05-11 19:42:16691 for (size_t __i = 0; __i < __rlen; ++__i)
Howard Hinnantd09f7112010-11-17 21:53:14692 if (__str[__i] != __zero && __str[__i] != __one)
Howard Hinnant3e519522010-05-11 19:42:16693#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 Hinnantc003db12011-11-29 18:15:50698 size_t _Mp = _VSTD::min(__rlen, _Size);
Howard Hinnant3e519522010-05-11 19:42:16699 size_t __i = 0;
Howard Hinnantc003db12011-11-29 18:15:50700 for (; __i < _Mp; ++__i)
Howard Hinnant3e519522010-05-11 19:42:16701 {
Howard Hinnantc003db12011-11-29 18:15:50702 _CharT __c = __str[_Mp - 1 - __i];
Howard Hinnantd09f7112010-11-17 21:53:14703 if (__c == __zero)
Howard Hinnant3e519522010-05-11 19:42:16704 (*this)[__i] = false;
Howard Hinnantd09f7112010-11-17 21:53:14705 else
Howard Hinnant3e519522010-05-11 19:42:16706 (*this)[__i] = true;
Howard Hinnant3e519522010-05-11 19:42:16707 }
Howard Hinnantce48a112011-06-30 21:18:19708 _VSTD::fill(base::__make_iter(__i), base::__make_iter(_Size), false);
Howard Hinnant3e519522010-05-11 19:42:16709}
710
711template <size_t _Size>
Howard Hinnantb3371f62010-08-22 00:02:43712template<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 Hinnant3e519522010-05-11 19:42:16715 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 Hinnantce48a112011-06-30 21:18:19724 size_t __rlen = _VSTD::min(__n, __str.size() - __pos);
Howard Hinnant3e519522010-05-11 19:42:16725 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 Hinnantc003db12011-11-29 18:15:50732 size_t _Mp = _VSTD::min(__rlen, _Size);
Howard Hinnant3e519522010-05-11 19:42:16733 size_t __i = 0;
Howard Hinnantc003db12011-11-29 18:15:50734 for (; __i < _Mp; ++__i)
Howard Hinnant3e519522010-05-11 19:42:16735 {
Howard Hinnantc003db12011-11-29 18:15:50736 _CharT __c = __str[__pos + _Mp - 1 - __i];
Howard Hinnant3e519522010-05-11 19:42:16737 if (_Traits::eq(__c, __zero))
738 (*this)[__i] = false;
739 else
740 (*this)[__i] = true;
741 }
Howard Hinnantce48a112011-06-30 21:18:19742 _VSTD::fill(base::__make_iter(__i), base::__make_iter(_Size), false);
Howard Hinnant3e519522010-05-11 19:42:16743}
744
745template <size_t _Size>
746inline _LIBCPP_INLINE_VISIBILITY
747bitset<_Size>&
Howard Hinnantd368a842011-05-27 20:52:28748bitset<_Size>::operator&=(const bitset& __rhs) _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16749{
750 base::operator&=(__rhs);
751 return *this;
752}
753
754template <size_t _Size>
755inline _LIBCPP_INLINE_VISIBILITY
756bitset<_Size>&
Howard Hinnantd368a842011-05-27 20:52:28757bitset<_Size>::operator|=(const bitset& __rhs) _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16758{
759 base::operator|=(__rhs);
760 return *this;
761}
762
763template <size_t _Size>
764inline _LIBCPP_INLINE_VISIBILITY
765bitset<_Size>&
Howard Hinnantd368a842011-05-27 20:52:28766bitset<_Size>::operator^=(const bitset& __rhs) _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16767{
768 base::operator^=(__rhs);
769 return *this;
770}
771
772template <size_t _Size>
773bitset<_Size>&
Howard Hinnantd368a842011-05-27 20:52:28774bitset<_Size>::operator<<=(size_t __pos) _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16775{
Howard Hinnantce48a112011-06-30 21:18:19776 __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 Hinnant3e519522010-05-11 19:42:16779 return *this;
780}
781
782template <size_t _Size>
783bitset<_Size>&
Howard Hinnantd368a842011-05-27 20:52:28784bitset<_Size>::operator>>=(size_t __pos) _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16785{
Howard Hinnantce48a112011-06-30 21:18:19786 __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 Hinnant3e519522010-05-11 19:42:16789 return *this;
790}
791
792template <size_t _Size>
793inline _LIBCPP_INLINE_VISIBILITY
794bitset<_Size>&
Howard Hinnantd368a842011-05-27 20:52:28795bitset<_Size>::set() _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16796{
Howard Hinnantce48a112011-06-30 21:18:19797 _VSTD::fill_n(base::__make_iter(0), _Size, true);
Howard Hinnant3e519522010-05-11 19:42:16798 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 Hinnantd368a842011-05-27 20:52:28818bitset<_Size>::reset() _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16819{
Howard Hinnantce48a112011-06-30 21:18:19820 _VSTD::fill_n(base::__make_iter(0), _Size, false);
Howard Hinnant3e519522010-05-11 19:42:16821 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 Hinnantd368a842011-05-27 20:52:28841bitset<_Size>::operator~() const _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16842{
843 bitset __x(*this);
844 __x.flip();
845 return __x;
846}
847
848template <size_t _Size>
849inline _LIBCPP_INLINE_VISIBILITY
850bitset<_Size>&
Howard Hinnantd368a842011-05-27 20:52:28851bitset<_Size>::flip() _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16852{
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 Hinnantb3371f62010-08-22 00:02:43889template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant3e519522010-05-11 19:42:16890basic_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 Hinnantb3371f62010-08-22 00:02:43903template <class _CharT, class _Traits>
Howard Hinnant3e519522010-05-11 19:42:16904inline _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 Hinnantb3371f62010-08-22 00:02:43912template <class _CharT>
Howard Hinnant3e519522010-05-11 19:42:16913inline _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 Hinnantd368a842011-05-27 20:52:28931bitset<_Size>::count() const _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16932{
Howard Hinnantce48a112011-06-30 21:18:19933 return static_cast<size_t>(_VSTD::count(base::__make_iter(0), base::__make_iter(_Size), true));
Howard Hinnant3e519522010-05-11 19:42:16934}
935
936template <size_t _Size>
937inline _LIBCPP_INLINE_VISIBILITY
938bool
Howard Hinnantd368a842011-05-27 20:52:28939bitset<_Size>::operator==(const bitset& __rhs) const _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16940{
Howard Hinnantce48a112011-06-30 21:18:19941 return _VSTD::equal(base::__make_iter(0), base::__make_iter(_Size), __rhs.__make_iter(0));
Howard Hinnant3e519522010-05-11 19:42:16942}
943
944template <size_t _Size>
945inline _LIBCPP_INLINE_VISIBILITY
946bool
Howard Hinnantd368a842011-05-27 20:52:28947bitset<_Size>::operator!=(const bitset& __rhs) const _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16948{
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 Hinnantd368a842011-05-27 20:52:28968bitset<_Size>::all() const _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16969{
970 return base::all();
971}
972
973template <size_t _Size>
974inline _LIBCPP_INLINE_VISIBILITY
975bool
Howard Hinnantd368a842011-05-27 20:52:28976bitset<_Size>::any() const _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16977{
978 return base::any();
979}
980
981template <size_t _Size>
982inline _LIBCPP_INLINE_VISIBILITY
983bitset<_Size>
Howard Hinnantd368a842011-05-27 20:52:28984bitset<_Size>::operator<<(size_t __pos) const _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16985{
986 bitset __r = *this;
987 __r <<= __pos;
988 return __r;
989}
990
991template <size_t _Size>
992inline _LIBCPP_INLINE_VISIBILITY
993bitset<_Size>
Howard Hinnantd368a842011-05-27 20:52:28994bitset<_Size>::operator>>(size_t __pos) const _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16995{
996 bitset __r = *this;
997 __r >>= __pos;
998 return __r;
999}
1000
1001template <size_t _Size>
1002inline _LIBCPP_INLINE_VISIBILITY
1003bitset<_Size>
Howard Hinnantd368a842011-05-27 20:52:281004operator&(const bitset<_Size>& __x, const bitset<_Size>& __y) _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:161005{
1006 bitset<_Size> __r = __x;
1007 __r &= __y;
1008 return __r;
1009}
1010
1011template <size_t _Size>
1012inline _LIBCPP_INLINE_VISIBILITY
1013bitset<_Size>
Howard Hinnantd368a842011-05-27 20:52:281014operator|(const bitset<_Size>& __x, const bitset<_Size>& __y) _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:161015{
1016 bitset<_Size> __r = __x;
1017 __r |= __y;
1018 return __r;
1019}
1020
1021template <size_t _Size>
1022inline _LIBCPP_INLINE_VISIBILITY
1023bitset<_Size>
Howard Hinnantd368a842011-05-27 20:52:281024operator^(const bitset<_Size>& __x, const bitset<_Size>& __y) _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:161025{
1026 bitset<_Size> __r = __x;
1027 __r ^= __y;
1028 return __r;
1029}
1030
1031template <size_t _Size>
Howard Hinnantfb100022010-09-21 21:28:231032struct _LIBCPP_VISIBLE hash<bitset<_Size> >
Howard Hinnant3e519522010-05-11 19:42:161033 : public unary_function<bitset<_Size>, size_t>
1034{
Howard Hinnantfb100022010-09-21 21:28:231035 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd368a842011-05-27 20:52:281036 size_t operator()(const bitset<_Size>& __bs) const _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:161037 {return __bs.__hash_code();}
1038};
1039
Howard Hinnante3163f52011-07-18 15:51:591040template <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 Hinnant3e519522010-05-11 19:42:161048_LIBCPP_END_NAMESPACE_STD
1049
1050#endif // _LIBCPP_BITSET