Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1 | // -*- C++ -*- |
| 2 | //===---------------------------- bitset ----------------------------------===// |
| 3 | // |
Howard Hinnant | f5256e1 | 2010-05-11 21:36:01 +0000 | [diff] [blame] | 4 | // The LLVM Compiler Infrastructure |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 5 | // |
Howard Hinnant | b64f8b0 | 2010-11-16 22:09:02 +0000 | [diff] [blame] | 6 | // This file is dual licensed under the MIT and the University of Illinois Open |
| 7 | // Source Licenses. See LICENSE.TXT for details. |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 8 | // |
| 9 | //===----------------------------------------------------------------------===// |
| 10 | |
| 11 | #ifndef _LIBCPP_BITSET |
| 12 | #define _LIBCPP_BITSET |
| 13 | |
| 14 | /* |
| 15 | bitset synopsis |
| 16 | |
| 17 | namespace std |
| 18 | { |
| 19 | |
| 20 | namespace std { |
| 21 | |
| 22 | template <size_t N> |
| 23 | class bitset |
| 24 | { |
| 25 | public: |
| 26 | // bit reference: |
| 27 | class reference |
| 28 | { |
| 29 | friend class bitset; |
Howard Hinnant | 10f25d2 | 2011-05-27 20:52:28 +0000 | [diff] [blame] | 30 | reference() noexcept; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 31 | public: |
Howard Hinnant | 10f25d2 | 2011-05-27 20:52:28 +0000 | [diff] [blame] | 32 | ~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 Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 38 | }; |
| 39 | |
| 40 | // 23.3.5.1 constructors: |
Howard Hinnant | 10f25d2 | 2011-05-27 20:52:28 +0000 | [diff] [blame] | 41 | constexpr bitset() noexcept; |
| 42 | constexpr bitset(unsigned long long val) noexcept; |
Howard Hinnant | 34d6b19 | 2010-11-17 21:53:14 +0000 | [diff] [blame] | 43 | 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 Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 47 | 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 Hinnant | 10f25d2 | 2011-05-27 20:52:28 +0000 | [diff] [blame] | 55 | 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 Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 61 | bitset& set(size_t pos, bool val = true); |
Howard Hinnant | 10f25d2 | 2011-05-27 20:52:28 +0000 | [diff] [blame] | 62 | bitset& reset() noexcept; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 63 | bitset& reset(size_t pos); |
Howard Hinnant | 10f25d2 | 2011-05-27 20:52:28 +0000 | [diff] [blame] | 64 | bitset operator~() const noexcept; |
| 65 | bitset& flip() noexcept; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 66 | 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 Hinnant | 10f25d2 | 2011-05-27 20:52:28 +0000 | [diff] [blame] | 80 | 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 Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 84 | bool test(size_t pos) const; |
Howard Hinnant | 10f25d2 | 2011-05-27 20:52:28 +0000 | [diff] [blame] | 85 | 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 Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 90 | }; |
| 91 | |
| 92 | // 23.3.5.3 bitset operators: |
| 93 | template <size_t N> |
Howard Hinnant | 10f25d2 | 2011-05-27 20:52:28 +0000 | [diff] [blame] | 94 | bitset<N> operator&(const bitset<N>&, const bitset<N>&) noexcept; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 95 | |
| 96 | template <size_t N> |
Howard Hinnant | 10f25d2 | 2011-05-27 20:52:28 +0000 | [diff] [blame] | 97 | bitset<N> operator|(const bitset<N>&, const bitset<N>&) noexcept; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 98 | |
| 99 | template <size_t N> |
Howard Hinnant | 10f25d2 | 2011-05-27 20:52:28 +0000 | [diff] [blame] | 100 | bitset<N> operator^(const bitset<N>&, const bitset<N>&) noexcept; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 101 | |
| 102 | template <class charT, class traits, size_t N> |
| 103 | basic_istream<charT, traits>& |
| 104 | operator>>(basic_istream<charT, traits>& is, bitset<N>& x); |
| 105 | |
| 106 | template <class charT, class traits, size_t N> |
| 107 | basic_ostream<charT, traits>& |
| 108 | operator<<(basic_ostream<charT, traits>& os, const bitset<N>& x); |
| 109 | |
| 110 | template <size_t N> struct hash<std::bitset<N>>; |
| 111 | |
| 112 | } // std |
| 113 | |
| 114 | */ |
| 115 | |
Howard Hinnant | 08e1747 | 2011-10-17 20:05:10 +0000 | [diff] [blame] | 116 | #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 117 | #pragma GCC system_header |
Howard Hinnant | 08e1747 | 2011-10-17 20:05:10 +0000 | [diff] [blame] | 118 | #endif |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 119 | |
| 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 Hinnant | 66c6f97 | 2011-11-29 16:45:27 +0000 | [diff] [blame] | 132 | #include <__undef_min_max> |
| 133 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 134 | _LIBCPP_BEGIN_NAMESPACE_STD |
| 135 | |
| 136 | template <size_t _N_words, size_t _Size> |
Howard Hinnant | f03c3b4 | 2011-07-02 20:33:23 +0000 | [diff] [blame] | 137 | class __bitset; |
| 138 | |
| 139 | template <size_t _N_words, size_t _Size> |
| 140 | struct __has_storage_type<__bitset<_N_words, _Size> > |
| 141 | { |
| 142 | static const bool value = true; |
| 143 | }; |
| 144 | |
| 145 | template <size_t _N_words, size_t _Size> |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 146 | class __bitset |
| 147 | { |
| 148 | public: |
| 149 | typedef ptrdiff_t difference_type; |
| 150 | typedef size_t size_type; |
Howard Hinnant | f867f63 | 2012-05-07 16:50:38 +0000 | [diff] [blame] | 151 | typedef size_type __storage_type; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 152 | protected: |
| 153 | typedef __bitset __self; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 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>; |
Howard Hinnant | 4ae952a | 2012-08-17 17:10:18 +0000 | [diff] [blame] | 162 | friend struct __bit_array<__bitset>; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 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 Hinnant | 90d8723 | 2012-07-07 17:04:52 +0000 | [diff] [blame] | 171 | _LIBCPP_CONSTEXPR __bitset() _NOEXCEPT; |
| 172 | explicit _LIBCPP_CONSTEXPR __bitset(unsigned long long __v) _NOEXCEPT; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 173 | |
Howard Hinnant | 10f25d2 | 2011-05-27 20:52:28 +0000 | [diff] [blame] | 174 | _LIBCPP_INLINE_VISIBILITY reference __make_ref(size_t __pos) _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 175 | {return reference(__first_ + __pos / __bits_per_word, __storage_type(1) << __pos % __bits_per_word);} |
Howard Hinnant | 90d8723 | 2012-07-07 17:04:52 +0000 | [diff] [blame] | 176 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR const_reference __make_ref(size_t __pos) const _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 177 | {return const_reference(__first_ + __pos / __bits_per_word, __storage_type(1) << __pos % __bits_per_word);} |
Howard Hinnant | 10f25d2 | 2011-05-27 20:52:28 +0000 | [diff] [blame] | 178 | _LIBCPP_INLINE_VISIBILITY iterator __make_iter(size_t __pos) _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 179 | {return iterator(__first_ + __pos / __bits_per_word, __pos % __bits_per_word);} |
Howard Hinnant | 10f25d2 | 2011-05-27 20:52:28 +0000 | [diff] [blame] | 180 | _LIBCPP_INLINE_VISIBILITY const_iterator __make_iter(size_t __pos) const _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 181 | {return const_iterator(__first_ + __pos / __bits_per_word, __pos % __bits_per_word);} |
| 182 | |
Howard Hinnant | 10f25d2 | 2011-05-27 20:52:28 +0000 | [diff] [blame] | 183 | void operator&=(const __bitset& __v) _NOEXCEPT; |
| 184 | void operator|=(const __bitset& __v) _NOEXCEPT; |
| 185 | void operator^=(const __bitset& __v) _NOEXCEPT; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 186 | |
Howard Hinnant | 10f25d2 | 2011-05-27 20:52:28 +0000 | [diff] [blame] | 187 | void flip() _NOEXCEPT; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 188 | _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 Hinnant | 10f25d2 | 2011-05-27 20:52:28 +0000 | [diff] [blame] | 193 | bool all() const _NOEXCEPT; |
| 194 | bool any() const _NOEXCEPT; |
| 195 | size_t __hash_code() const _NOEXCEPT; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 196 | private: |
Howard Hinnant | 90d8723 | 2012-07-07 17:04:52 +0000 | [diff] [blame] | 197 | #ifdef _LIBCPP_HAS_NO_CONSTEXPR |
Howard Hinnant | 10f25d2 | 2011-05-27 20:52:28 +0000 | [diff] [blame] | 198 | void __init(unsigned long long __v, false_type) _NOEXCEPT; |
| 199 | void __init(unsigned long long __v, true_type) _NOEXCEPT; |
Howard Hinnant | 90d8723 | 2012-07-07 17:04:52 +0000 | [diff] [blame] | 200 | #endif // _LIBCPP_HAS_NO_CONSTEXPR |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 201 | unsigned long to_ulong(false_type) const; |
| 202 | unsigned long to_ulong(true_type) const; |
| 203 | unsigned long long to_ullong(false_type) const; |
| 204 | unsigned long long to_ullong(true_type) const; |
| 205 | unsigned long long to_ullong(true_type, false_type) const; |
| 206 | unsigned long long to_ullong(true_type, true_type) const; |
| 207 | }; |
| 208 | |
| 209 | template <size_t _N_words, size_t _Size> |
| 210 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 90d8723 | 2012-07-07 17:04:52 +0000 | [diff] [blame] | 211 | _LIBCPP_CONSTEXPR |
Howard Hinnant | 10f25d2 | 2011-05-27 20:52:28 +0000 | [diff] [blame] | 212 | __bitset<_N_words, _Size>::__bitset() _NOEXCEPT |
Howard Hinnant | 90d8723 | 2012-07-07 17:04:52 +0000 | [diff] [blame] | 213 | #ifndef _LIBCPP_HAS_NO_CONSTEXPR |
| 214 | : __first_{0} |
| 215 | #endif |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 216 | { |
Howard Hinnant | 90d8723 | 2012-07-07 17:04:52 +0000 | [diff] [blame] | 217 | #ifdef _LIBCPP_HAS_NO_CONSTEXPR |
Howard Hinnant | 0949eed | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 218 | _VSTD::fill_n(__first_, _N_words, __storage_type(0)); |
Howard Hinnant | 90d8723 | 2012-07-07 17:04:52 +0000 | [diff] [blame] | 219 | #endif |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 220 | } |
| 221 | |
Howard Hinnant | 90d8723 | 2012-07-07 17:04:52 +0000 | [diff] [blame] | 222 | #ifdef _LIBCPP_HAS_NO_CONSTEXPR |
| 223 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 224 | template <size_t _N_words, size_t _Size> |
| 225 | void |
Howard Hinnant | ec3773c | 2011-12-01 20:21:04 +0000 | [diff] [blame] | 226 | __bitset<_N_words, _Size>::__init(unsigned long long __v, false_type) _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 227 | { |
| 228 | __storage_type __t[sizeof(unsigned long long) / sizeof(__storage_type)]; |
| 229 | for (size_t __i = 0; __i < sizeof(__t)/sizeof(__t[0]); ++__i, __v >>= __bits_per_word) |
| 230 | __t[__i] = static_cast<__storage_type>(__v); |
Howard Hinnant | 0949eed | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 231 | _VSTD::copy(__t, __t + sizeof(__t)/sizeof(__t[0]), __first_); |
| 232 | _VSTD::fill(__first_ + sizeof(__t)/sizeof(__t[0]), __first_ + sizeof(__first_)/sizeof(__first_[0]), |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 233 | __storage_type(0)); |
| 234 | } |
| 235 | |
| 236 | template <size_t _N_words, size_t _Size> |
| 237 | inline _LIBCPP_INLINE_VISIBILITY |
| 238 | void |
Howard Hinnant | ec3773c | 2011-12-01 20:21:04 +0000 | [diff] [blame] | 239 | __bitset<_N_words, _Size>::__init(unsigned long long __v, true_type) _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 240 | { |
| 241 | __first_[0] = __v; |
Howard Hinnant | 0949eed | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 242 | _VSTD::fill(__first_ + 1, __first_ + sizeof(__first_)/sizeof(__first_[0]), __storage_type(0)); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 243 | } |
| 244 | |
Howard Hinnant | 90d8723 | 2012-07-07 17:04:52 +0000 | [diff] [blame] | 245 | #endif // _LIBCPP_HAS_NO_CONSTEXPR |
| 246 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 247 | template <size_t _N_words, size_t _Size> |
| 248 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 90d8723 | 2012-07-07 17:04:52 +0000 | [diff] [blame] | 249 | _LIBCPP_CONSTEXPR |
Howard Hinnant | 10f25d2 | 2011-05-27 20:52:28 +0000 | [diff] [blame] | 250 | __bitset<_N_words, _Size>::__bitset(unsigned long long __v) _NOEXCEPT |
Howard Hinnant | 90d8723 | 2012-07-07 17:04:52 +0000 | [diff] [blame] | 251 | #ifndef _LIBCPP_HAS_NO_CONSTEXPR |
| 252 | : __first_{__v} |
| 253 | #endif |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 254 | { |
Howard Hinnant | 90d8723 | 2012-07-07 17:04:52 +0000 | [diff] [blame] | 255 | #ifdef _LIBCPP_HAS_NO_CONSTEXPR |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 256 | __init(__v, integral_constant<bool, sizeof(unsigned long long) == sizeof(__storage_type)>()); |
Howard Hinnant | 90d8723 | 2012-07-07 17:04:52 +0000 | [diff] [blame] | 257 | #endif |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | template <size_t _N_words, size_t _Size> |
| 261 | inline _LIBCPP_INLINE_VISIBILITY |
| 262 | void |
Howard Hinnant | 10f25d2 | 2011-05-27 20:52:28 +0000 | [diff] [blame] | 263 | __bitset<_N_words, _Size>::operator&=(const __bitset& __v) _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 264 | { |
| 265 | for (size_type __i = 0; __i < _N_words; ++__i) |
| 266 | __first_[__i] &= __v.__first_[__i]; |
| 267 | } |
| 268 | |
| 269 | template <size_t _N_words, size_t _Size> |
| 270 | inline _LIBCPP_INLINE_VISIBILITY |
| 271 | void |
Howard Hinnant | 10f25d2 | 2011-05-27 20:52:28 +0000 | [diff] [blame] | 272 | __bitset<_N_words, _Size>::operator|=(const __bitset& __v) _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 273 | { |
| 274 | for (size_type __i = 0; __i < _N_words; ++__i) |
| 275 | __first_[__i] |= __v.__first_[__i]; |
| 276 | } |
| 277 | |
| 278 | template <size_t _N_words, size_t _Size> |
| 279 | inline _LIBCPP_INLINE_VISIBILITY |
| 280 | void |
Howard Hinnant | 10f25d2 | 2011-05-27 20:52:28 +0000 | [diff] [blame] | 281 | __bitset<_N_words, _Size>::operator^=(const __bitset& __v) _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 282 | { |
| 283 | for (size_type __i = 0; __i < _N_words; ++__i) |
| 284 | __first_[__i] ^= __v.__first_[__i]; |
| 285 | } |
| 286 | |
| 287 | template <size_t _N_words, size_t _Size> |
| 288 | void |
Howard Hinnant | 10f25d2 | 2011-05-27 20:52:28 +0000 | [diff] [blame] | 289 | __bitset<_N_words, _Size>::flip() _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 290 | { |
| 291 | // do middle whole words |
| 292 | size_type __n = _Size; |
| 293 | __storage_pointer __p = __first_; |
| 294 | for (; __n >= __bits_per_word; ++__p, __n -= __bits_per_word) |
| 295 | *__p = ~*__p; |
| 296 | // do last partial word |
| 297 | if (__n > 0) |
| 298 | { |
| 299 | __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n); |
| 300 | __storage_type __b = *__p & __m; |
| 301 | *__p &= ~__m; |
| 302 | *__p |= ~__b & __m; |
| 303 | } |
| 304 | } |
| 305 | |
| 306 | template <size_t _N_words, size_t _Size> |
| 307 | unsigned long |
| 308 | __bitset<_N_words, _Size>::to_ulong(false_type) const |
| 309 | { |
| 310 | const_iterator __e = __make_iter(_Size); |
Howard Hinnant | 0949eed | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 311 | const_iterator __i = _VSTD::find(__make_iter(sizeof(unsigned long) * CHAR_BIT), __e, true); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 312 | if (__i != __e) |
| 313 | #ifndef _LIBCPP_NO_EXCEPTIONS |
| 314 | throw overflow_error("bitset to_ulong overflow error"); |
| 315 | #else |
| 316 | assert(!"bitset to_ulong overflow error"); |
| 317 | #endif |
| 318 | return __first_[0]; |
| 319 | } |
| 320 | |
| 321 | template <size_t _N_words, size_t _Size> |
| 322 | inline _LIBCPP_INLINE_VISIBILITY |
| 323 | unsigned long |
| 324 | __bitset<_N_words, _Size>::to_ulong(true_type) const |
| 325 | { |
| 326 | return __first_[0]; |
| 327 | } |
| 328 | |
| 329 | template <size_t _N_words, size_t _Size> |
| 330 | unsigned long long |
| 331 | __bitset<_N_words, _Size>::to_ullong(false_type) const |
| 332 | { |
| 333 | const_iterator __e = __make_iter(_Size); |
Howard Hinnant | 0949eed | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 334 | const_iterator __i = _VSTD::find(__make_iter(sizeof(unsigned long long) * CHAR_BIT), __e, true); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 335 | if (__i != __e) |
| 336 | #ifndef _LIBCPP_NO_EXCEPTIONS |
| 337 | throw overflow_error("bitset to_ullong overflow error"); |
| 338 | #else |
| 339 | assert(!"bitset to_ullong overflow error"); |
| 340 | #endif |
| 341 | return to_ullong(true_type()); |
| 342 | } |
| 343 | |
| 344 | template <size_t _N_words, size_t _Size> |
| 345 | inline _LIBCPP_INLINE_VISIBILITY |
| 346 | unsigned long long |
| 347 | __bitset<_N_words, _Size>::to_ullong(true_type) const |
| 348 | { |
| 349 | return to_ullong(true_type(), integral_constant<bool, sizeof(__storage_type) < sizeof(unsigned long long)>()); |
| 350 | } |
| 351 | |
| 352 | template <size_t _N_words, size_t _Size> |
| 353 | inline _LIBCPP_INLINE_VISIBILITY |
| 354 | unsigned long long |
| 355 | __bitset<_N_words, _Size>::to_ullong(true_type, false_type) const |
| 356 | { |
| 357 | return __first_[0]; |
| 358 | } |
| 359 | |
| 360 | template <size_t _N_words, size_t _Size> |
| 361 | unsigned long long |
| 362 | __bitset<_N_words, _Size>::to_ullong(true_type, true_type) const |
| 363 | { |
| 364 | unsigned long long __r = __first_[0]; |
| 365 | for (std::size_t __i = 1; __i < sizeof(unsigned long long) / sizeof(__storage_type); ++__i) |
| 366 | __r |= static_cast<unsigned long long>(__first_[__i]) << (sizeof(__storage_type) * CHAR_BIT); |
| 367 | return __r; |
| 368 | } |
| 369 | |
| 370 | template <size_t _N_words, size_t _Size> |
| 371 | bool |
Howard Hinnant | 10f25d2 | 2011-05-27 20:52:28 +0000 | [diff] [blame] | 372 | __bitset<_N_words, _Size>::all() const _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 373 | { |
| 374 | // do middle whole words |
| 375 | size_type __n = _Size; |
| 376 | __const_storage_pointer __p = __first_; |
| 377 | for (; __n >= __bits_per_word; ++__p, __n -= __bits_per_word) |
| 378 | if (~*__p) |
| 379 | return false; |
| 380 | // do last partial word |
| 381 | if (__n > 0) |
| 382 | { |
| 383 | __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n); |
| 384 | if (~*__p & __m) |
| 385 | return false; |
| 386 | } |
| 387 | return true; |
| 388 | } |
| 389 | |
| 390 | template <size_t _N_words, size_t _Size> |
| 391 | bool |
Howard Hinnant | 10f25d2 | 2011-05-27 20:52:28 +0000 | [diff] [blame] | 392 | __bitset<_N_words, _Size>::any() const _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 393 | { |
| 394 | // do middle whole words |
| 395 | size_type __n = _Size; |
| 396 | __const_storage_pointer __p = __first_; |
| 397 | for (; __n >= __bits_per_word; ++__p, __n -= __bits_per_word) |
| 398 | if (*__p) |
| 399 | return true; |
| 400 | // do last partial word |
| 401 | if (__n > 0) |
| 402 | { |
| 403 | __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n); |
| 404 | if (*__p & __m) |
| 405 | return true; |
| 406 | } |
| 407 | return false; |
| 408 | } |
| 409 | |
| 410 | template <size_t _N_words, size_t _Size> |
| 411 | inline _LIBCPP_INLINE_VISIBILITY |
| 412 | size_t |
Howard Hinnant | 10f25d2 | 2011-05-27 20:52:28 +0000 | [diff] [blame] | 413 | __bitset<_N_words, _Size>::__hash_code() const _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 414 | { |
| 415 | size_t __h = 0; |
| 416 | for (size_type __i = 0; __i < _N_words; ++__i) |
| 417 | __h ^= __first_[__i]; |
| 418 | return __h; |
| 419 | } |
| 420 | |
| 421 | template <size_t _Size> |
| 422 | class __bitset<1, _Size> |
| 423 | { |
| 424 | public: |
| 425 | typedef ptrdiff_t difference_type; |
| 426 | typedef size_t size_type; |
Howard Hinnant | f867f63 | 2012-05-07 16:50:38 +0000 | [diff] [blame] | 427 | typedef size_type __storage_type; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 428 | protected: |
| 429 | typedef __bitset __self; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 430 | typedef __storage_type* __storage_pointer; |
| 431 | typedef const __storage_type* __const_storage_pointer; |
| 432 | static const unsigned __bits_per_word = static_cast<unsigned>(sizeof(__storage_type) * CHAR_BIT); |
| 433 | |
| 434 | friend class __bit_reference<__bitset>; |
| 435 | friend class __bit_const_reference<__bitset>; |
| 436 | friend class __bit_iterator<__bitset, false>; |
| 437 | friend class __bit_iterator<__bitset, true>; |
Howard Hinnant | 4ae952a | 2012-08-17 17:10:18 +0000 | [diff] [blame] | 438 | friend struct __bit_array<__bitset>; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 439 | |
| 440 | __storage_type __first_; |
| 441 | |
| 442 | typedef __bit_reference<__bitset> reference; |
| 443 | typedef __bit_const_reference<__bitset> const_reference; |
| 444 | typedef __bit_iterator<__bitset, false> iterator; |
| 445 | typedef __bit_iterator<__bitset, true> const_iterator; |
| 446 | |
Howard Hinnant | 90d8723 | 2012-07-07 17:04:52 +0000 | [diff] [blame] | 447 | _LIBCPP_CONSTEXPR __bitset() _NOEXCEPT; |
| 448 | explicit _LIBCPP_CONSTEXPR __bitset(unsigned long long __v) _NOEXCEPT; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 449 | |
Howard Hinnant | 10f25d2 | 2011-05-27 20:52:28 +0000 | [diff] [blame] | 450 | _LIBCPP_INLINE_VISIBILITY reference __make_ref(size_t __pos) _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 451 | {return reference(&__first_, __storage_type(1) << __pos);} |
Howard Hinnant | 90d8723 | 2012-07-07 17:04:52 +0000 | [diff] [blame] | 452 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR const_reference __make_ref(size_t __pos) const _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 453 | {return const_reference(&__first_, __storage_type(1) << __pos);} |
Howard Hinnant | 10f25d2 | 2011-05-27 20:52:28 +0000 | [diff] [blame] | 454 | _LIBCPP_INLINE_VISIBILITY iterator __make_iter(size_t __pos) _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 455 | {return iterator(&__first_ + __pos / __bits_per_word, __pos % __bits_per_word);} |
Howard Hinnant | 10f25d2 | 2011-05-27 20:52:28 +0000 | [diff] [blame] | 456 | _LIBCPP_INLINE_VISIBILITY const_iterator __make_iter(size_t __pos) const _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 457 | {return const_iterator(&__first_ + __pos / __bits_per_word, __pos % __bits_per_word);} |
| 458 | |
Howard Hinnant | 10f25d2 | 2011-05-27 20:52:28 +0000 | [diff] [blame] | 459 | void operator&=(const __bitset& __v) _NOEXCEPT; |
| 460 | void operator|=(const __bitset& __v) _NOEXCEPT; |
| 461 | void operator^=(const __bitset& __v) _NOEXCEPT; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 462 | |
Howard Hinnant | 10f25d2 | 2011-05-27 20:52:28 +0000 | [diff] [blame] | 463 | void flip() _NOEXCEPT; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 464 | |
| 465 | unsigned long to_ulong() const; |
| 466 | unsigned long long to_ullong() const; |
| 467 | |
Howard Hinnant | 10f25d2 | 2011-05-27 20:52:28 +0000 | [diff] [blame] | 468 | bool all() const _NOEXCEPT; |
| 469 | bool any() const _NOEXCEPT; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 470 | |
Howard Hinnant | 10f25d2 | 2011-05-27 20:52:28 +0000 | [diff] [blame] | 471 | size_t __hash_code() const _NOEXCEPT; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 472 | }; |
| 473 | |
| 474 | template <size_t _Size> |
| 475 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 90d8723 | 2012-07-07 17:04:52 +0000 | [diff] [blame] | 476 | _LIBCPP_CONSTEXPR |
Howard Hinnant | 10f25d2 | 2011-05-27 20:52:28 +0000 | [diff] [blame] | 477 | __bitset<1, _Size>::__bitset() _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 478 | : __first_(0) |
| 479 | { |
| 480 | } |
| 481 | |
| 482 | template <size_t _Size> |
| 483 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 90d8723 | 2012-07-07 17:04:52 +0000 | [diff] [blame] | 484 | _LIBCPP_CONSTEXPR |
Howard Hinnant | 10f25d2 | 2011-05-27 20:52:28 +0000 | [diff] [blame] | 485 | __bitset<1, _Size>::__bitset(unsigned long long __v) _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 486 | : __first_(static_cast<__storage_type>(__v)) |
| 487 | { |
| 488 | } |
| 489 | |
| 490 | template <size_t _Size> |
| 491 | inline _LIBCPP_INLINE_VISIBILITY |
| 492 | void |
Howard Hinnant | 10f25d2 | 2011-05-27 20:52:28 +0000 | [diff] [blame] | 493 | __bitset<1, _Size>::operator&=(const __bitset& __v) _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 494 | { |
| 495 | __first_ &= __v.__first_; |
| 496 | } |
| 497 | |
| 498 | template <size_t _Size> |
| 499 | inline _LIBCPP_INLINE_VISIBILITY |
| 500 | void |
Howard Hinnant | 10f25d2 | 2011-05-27 20:52:28 +0000 | [diff] [blame] | 501 | __bitset<1, _Size>::operator|=(const __bitset& __v) _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 502 | { |
| 503 | __first_ |= __v.__first_; |
| 504 | } |
| 505 | |
| 506 | template <size_t _Size> |
| 507 | inline _LIBCPP_INLINE_VISIBILITY |
| 508 | void |
Howard Hinnant | 10f25d2 | 2011-05-27 20:52:28 +0000 | [diff] [blame] | 509 | __bitset<1, _Size>::operator^=(const __bitset& __v) _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 510 | { |
| 511 | __first_ ^= __v.__first_; |
| 512 | } |
| 513 | |
| 514 | template <size_t _Size> |
| 515 | inline _LIBCPP_INLINE_VISIBILITY |
| 516 | void |
Howard Hinnant | 10f25d2 | 2011-05-27 20:52:28 +0000 | [diff] [blame] | 517 | __bitset<1, _Size>::flip() _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 518 | { |
| 519 | __storage_type __m = ~__storage_type(0) >> (__bits_per_word - _Size); |
| 520 | __first_ = ~__first_; |
| 521 | __first_ &= __m; |
| 522 | } |
| 523 | |
| 524 | template <size_t _Size> |
| 525 | inline _LIBCPP_INLINE_VISIBILITY |
| 526 | unsigned long |
| 527 | __bitset<1, _Size>::to_ulong() const |
| 528 | { |
| 529 | return __first_; |
| 530 | } |
| 531 | |
| 532 | template <size_t _Size> |
| 533 | inline _LIBCPP_INLINE_VISIBILITY |
| 534 | unsigned long long |
| 535 | __bitset<1, _Size>::to_ullong() const |
| 536 | { |
| 537 | return __first_; |
| 538 | } |
| 539 | |
| 540 | template <size_t _Size> |
| 541 | inline _LIBCPP_INLINE_VISIBILITY |
| 542 | bool |
Howard Hinnant | 10f25d2 | 2011-05-27 20:52:28 +0000 | [diff] [blame] | 543 | __bitset<1, _Size>::all() const _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 544 | { |
| 545 | __storage_type __m = ~__storage_type(0) >> (__bits_per_word - _Size); |
| 546 | return !(~__first_ & __m); |
| 547 | } |
| 548 | |
| 549 | template <size_t _Size> |
| 550 | inline _LIBCPP_INLINE_VISIBILITY |
| 551 | bool |
Howard Hinnant | 10f25d2 | 2011-05-27 20:52:28 +0000 | [diff] [blame] | 552 | __bitset<1, _Size>::any() const _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 553 | { |
| 554 | __storage_type __m = ~__storage_type(0) >> (__bits_per_word - _Size); |
| 555 | return __first_ & __m; |
| 556 | } |
| 557 | |
| 558 | template <size_t _Size> |
| 559 | inline _LIBCPP_INLINE_VISIBILITY |
| 560 | size_t |
Howard Hinnant | 10f25d2 | 2011-05-27 20:52:28 +0000 | [diff] [blame] | 561 | __bitset<1, _Size>::__hash_code() const _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 562 | { |
| 563 | return __first_; |
| 564 | } |
| 565 | |
| 566 | template <> |
| 567 | class __bitset<0, 0> |
| 568 | { |
| 569 | public: |
| 570 | typedef ptrdiff_t difference_type; |
| 571 | typedef size_t size_type; |
Howard Hinnant | f867f63 | 2012-05-07 16:50:38 +0000 | [diff] [blame] | 572 | typedef size_type __storage_type; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 573 | protected: |
| 574 | typedef __bitset __self; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 575 | typedef __storage_type* __storage_pointer; |
| 576 | typedef const __storage_type* __const_storage_pointer; |
| 577 | static const unsigned __bits_per_word = static_cast<unsigned>(sizeof(__storage_type) * CHAR_BIT); |
| 578 | |
| 579 | friend class __bit_reference<__bitset>; |
| 580 | friend class __bit_const_reference<__bitset>; |
| 581 | friend class __bit_iterator<__bitset, false>; |
| 582 | friend class __bit_iterator<__bitset, true>; |
Howard Hinnant | ec3773c | 2011-12-01 20:21:04 +0000 | [diff] [blame] | 583 | friend struct __bit_array<__bitset>; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 584 | |
| 585 | typedef __bit_reference<__bitset> reference; |
| 586 | typedef __bit_const_reference<__bitset> const_reference; |
| 587 | typedef __bit_iterator<__bitset, false> iterator; |
| 588 | typedef __bit_iterator<__bitset, true> const_iterator; |
| 589 | |
Howard Hinnant | 90d8723 | 2012-07-07 17:04:52 +0000 | [diff] [blame] | 590 | _LIBCPP_CONSTEXPR __bitset() _NOEXCEPT; |
| 591 | explicit _LIBCPP_CONSTEXPR __bitset(unsigned long long) _NOEXCEPT; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 592 | |
Howard Hinnant | 10f25d2 | 2011-05-27 20:52:28 +0000 | [diff] [blame] | 593 | _LIBCPP_INLINE_VISIBILITY reference __make_ref(size_t) _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 594 | {return reference(0, 1);} |
Howard Hinnant | 90d8723 | 2012-07-07 17:04:52 +0000 | [diff] [blame] | 595 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR const_reference __make_ref(size_t) const _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 596 | {return const_reference(0, 1);} |
Howard Hinnant | ec3773c | 2011-12-01 20:21:04 +0000 | [diff] [blame] | 597 | _LIBCPP_INLINE_VISIBILITY iterator __make_iter(size_t) _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 598 | {return iterator(0, 0);} |
Howard Hinnant | ec3773c | 2011-12-01 20:21:04 +0000 | [diff] [blame] | 599 | _LIBCPP_INLINE_VISIBILITY const_iterator __make_iter(size_t) const _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 600 | {return const_iterator(0, 0);} |
| 601 | |
Howard Hinnant | 10f25d2 | 2011-05-27 20:52:28 +0000 | [diff] [blame] | 602 | _LIBCPP_INLINE_VISIBILITY void operator&=(const __bitset&) _NOEXCEPT {} |
| 603 | _LIBCPP_INLINE_VISIBILITY void operator|=(const __bitset&) _NOEXCEPT {} |
| 604 | _LIBCPP_INLINE_VISIBILITY void operator^=(const __bitset&) _NOEXCEPT {} |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 605 | |
Howard Hinnant | 10f25d2 | 2011-05-27 20:52:28 +0000 | [diff] [blame] | 606 | _LIBCPP_INLINE_VISIBILITY void flip() _NOEXCEPT {} |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 607 | |
| 608 | _LIBCPP_INLINE_VISIBILITY unsigned long to_ulong() const {return 0;} |
| 609 | _LIBCPP_INLINE_VISIBILITY unsigned long long to_ullong() const {return 0;} |
| 610 | |
Howard Hinnant | 10f25d2 | 2011-05-27 20:52:28 +0000 | [diff] [blame] | 611 | _LIBCPP_INLINE_VISIBILITY bool all() const _NOEXCEPT {return true;} |
| 612 | _LIBCPP_INLINE_VISIBILITY bool any() const _NOEXCEPT {return false;} |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 613 | |
Howard Hinnant | 10f25d2 | 2011-05-27 20:52:28 +0000 | [diff] [blame] | 614 | _LIBCPP_INLINE_VISIBILITY size_t __hash_code() const _NOEXCEPT {return 0;} |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 615 | }; |
| 616 | |
| 617 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 90d8723 | 2012-07-07 17:04:52 +0000 | [diff] [blame] | 618 | _LIBCPP_CONSTEXPR |
Howard Hinnant | 10f25d2 | 2011-05-27 20:52:28 +0000 | [diff] [blame] | 619 | __bitset<0, 0>::__bitset() _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 620 | { |
| 621 | } |
| 622 | |
| 623 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 90d8723 | 2012-07-07 17:04:52 +0000 | [diff] [blame] | 624 | _LIBCPP_CONSTEXPR |
Howard Hinnant | 10f25d2 | 2011-05-27 20:52:28 +0000 | [diff] [blame] | 625 | __bitset<0, 0>::__bitset(unsigned long long) _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 626 | { |
| 627 | } |
| 628 | |
Howard Hinnant | 33be35e | 2012-09-14 00:39:16 +0000 | [diff] [blame^] | 629 | template <size_t _Size> class _LIBCPP_VISIBLE bitset; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 630 | template <size_t _Size> struct hash<bitset<_Size> >; |
| 631 | |
| 632 | template <size_t _Size> |
Howard Hinnant | 422a53f | 2010-09-21 21:28:23 +0000 | [diff] [blame] | 633 | class _LIBCPP_VISIBLE bitset |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 634 | : private __bitset<_Size == 0 ? 0 : (_Size - 1) / (sizeof(size_t) * CHAR_BIT) + 1, _Size> |
| 635 | { |
| 636 | static const unsigned __n_words = _Size == 0 ? 0 : (_Size - 1) / (sizeof(size_t) * CHAR_BIT) + 1; |
| 637 | typedef __bitset<__n_words, _Size> base; |
| 638 | |
Howard Hinnant | 324bb03 | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 639 | public: |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 640 | typedef typename base::reference reference; |
| 641 | typedef typename base::const_reference const_reference; |
| 642 | |
Howard Hinnant | 324bb03 | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 643 | // 23.3.5.1 constructors: |
Howard Hinnant | 90d8723 | 2012-07-07 17:04:52 +0000 | [diff] [blame] | 644 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR bitset() _NOEXCEPT {} |
| 645 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR |
| 646 | bitset(unsigned long long __v) _NOEXCEPT : base(__v) {} |
Howard Hinnant | 34d6b19 | 2010-11-17 21:53:14 +0000 | [diff] [blame] | 647 | template<class _CharT> |
| 648 | explicit bitset(const _CharT* __str, |
| 649 | typename basic_string<_CharT>::size_type __n = basic_string<_CharT>::npos, |
| 650 | _CharT __zero = _CharT('0'), _CharT __one = _CharT('1')); |
Howard Hinnant | 324bb03 | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 651 | template<class _CharT, class _Traits, class _Allocator> |
| 652 | explicit bitset(const basic_string<_CharT,_Traits,_Allocator>& __str, |
| 653 | typename basic_string<_CharT,_Traits,_Allocator>::size_type __pos = 0, |
| 654 | typename basic_string<_CharT,_Traits,_Allocator>::size_type __n = |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 655 | (basic_string<_CharT,_Traits,_Allocator>::npos), |
Howard Hinnant | 324bb03 | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 656 | _CharT __zero = _CharT('0'), _CharT __one = _CharT('1')); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 657 | |
Howard Hinnant | 324bb03 | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 658 | // 23.3.5.2 bitset operations: |
Howard Hinnant | 10f25d2 | 2011-05-27 20:52:28 +0000 | [diff] [blame] | 659 | bitset& operator&=(const bitset& __rhs) _NOEXCEPT; |
| 660 | bitset& operator|=(const bitset& __rhs) _NOEXCEPT; |
| 661 | bitset& operator^=(const bitset& __rhs) _NOEXCEPT; |
| 662 | bitset& operator<<=(size_t __pos) _NOEXCEPT; |
| 663 | bitset& operator>>=(size_t __pos) _NOEXCEPT; |
| 664 | bitset& set() _NOEXCEPT; |
Howard Hinnant | 324bb03 | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 665 | bitset& set(size_t __pos, bool __val = true); |
Howard Hinnant | 10f25d2 | 2011-05-27 20:52:28 +0000 | [diff] [blame] | 666 | bitset& reset() _NOEXCEPT; |
Howard Hinnant | 324bb03 | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 667 | bitset& reset(size_t __pos); |
Howard Hinnant | 10f25d2 | 2011-05-27 20:52:28 +0000 | [diff] [blame] | 668 | bitset operator~() const _NOEXCEPT; |
| 669 | bitset& flip() _NOEXCEPT; |
Howard Hinnant | 324bb03 | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 670 | bitset& flip(size_t __pos); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 671 | |
Howard Hinnant | 324bb03 | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 672 | // element access: |
Howard Hinnant | 90d8723 | 2012-07-07 17:04:52 +0000 | [diff] [blame] | 673 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR |
| 674 | const_reference operator[](size_t __p) const {return base::__make_ref(__p);} |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 675 | _LIBCPP_INLINE_VISIBILITY reference operator[](size_t __p) {return base::__make_ref(__p);} |
Howard Hinnant | 324bb03 | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 676 | unsigned long to_ulong() const; |
| 677 | unsigned long long to_ullong() const; |
| 678 | template <class _CharT, class _Traits, class _Allocator> |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 679 | basic_string<_CharT, _Traits, _Allocator> to_string(_CharT __zero = _CharT('0'), |
Howard Hinnant | 324bb03 | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 680 | _CharT __one = _CharT('1')) const; |
| 681 | template <class _CharT, class _Traits> |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 682 | basic_string<_CharT, _Traits, allocator<_CharT> > to_string(_CharT __zero = _CharT('0'), |
Howard Hinnant | 324bb03 | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 683 | _CharT __one = _CharT('1')) const; |
| 684 | template <class _CharT> |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 685 | basic_string<_CharT, char_traits<_CharT>, allocator<_CharT> > to_string(_CharT __zero = _CharT('0'), |
Howard Hinnant | 324bb03 | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 686 | _CharT __one = _CharT('1')) const; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 687 | basic_string<char, char_traits<char>, allocator<char> > to_string(char __zero = '0', |
Howard Hinnant | 324bb03 | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 688 | char __one = '1') const; |
Howard Hinnant | 10f25d2 | 2011-05-27 20:52:28 +0000 | [diff] [blame] | 689 | size_t count() const _NOEXCEPT; |
Howard Hinnant | 90d8723 | 2012-07-07 17:04:52 +0000 | [diff] [blame] | 690 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR size_t size() const _NOEXCEPT {return _Size;} |
Howard Hinnant | 10f25d2 | 2011-05-27 20:52:28 +0000 | [diff] [blame] | 691 | bool operator==(const bitset& __rhs) const _NOEXCEPT; |
| 692 | bool operator!=(const bitset& __rhs) const _NOEXCEPT; |
Howard Hinnant | 324bb03 | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 693 | bool test(size_t __pos) const; |
Howard Hinnant | 10f25d2 | 2011-05-27 20:52:28 +0000 | [diff] [blame] | 694 | bool all() const _NOEXCEPT; |
| 695 | bool any() const _NOEXCEPT; |
| 696 | _LIBCPP_INLINE_VISIBILITY bool none() const _NOEXCEPT {return !any();} |
| 697 | bitset operator<<(size_t __pos) const _NOEXCEPT; |
| 698 | bitset operator>>(size_t __pos) const _NOEXCEPT; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 699 | |
| 700 | private: |
| 701 | |
Howard Hinnant | 422a53f | 2010-09-21 21:28:23 +0000 | [diff] [blame] | 702 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 10f25d2 | 2011-05-27 20:52:28 +0000 | [diff] [blame] | 703 | size_t __hash_code() const _NOEXCEPT {return base::__hash_code();} |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 704 | |
| 705 | friend struct hash<bitset>; |
| 706 | }; |
| 707 | |
| 708 | template <size_t _Size> |
Howard Hinnant | 34d6b19 | 2010-11-17 21:53:14 +0000 | [diff] [blame] | 709 | template<class _CharT> |
| 710 | bitset<_Size>::bitset(const _CharT* __str, |
| 711 | typename basic_string<_CharT>::size_type __n, |
| 712 | _CharT __zero, _CharT __one) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 713 | { |
Howard Hinnant | 0949eed | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 714 | size_t __rlen = _VSTD::min(__n, char_traits<_CharT>::length(__str)); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 715 | for (size_t __i = 0; __i < __rlen; ++__i) |
Howard Hinnant | 34d6b19 | 2010-11-17 21:53:14 +0000 | [diff] [blame] | 716 | if (__str[__i] != __zero && __str[__i] != __one) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 717 | #ifndef _LIBCPP_NO_EXCEPTIONS |
| 718 | throw invalid_argument("bitset string ctor has invalid argument"); |
| 719 | #else |
| 720 | assert(!"bitset string ctor has invalid argument"); |
| 721 | #endif |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 722 | size_t _Mp = _VSTD::min(__rlen, _Size); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 723 | size_t __i = 0; |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 724 | for (; __i < _Mp; ++__i) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 725 | { |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 726 | _CharT __c = __str[_Mp - 1 - __i]; |
Howard Hinnant | 34d6b19 | 2010-11-17 21:53:14 +0000 | [diff] [blame] | 727 | if (__c == __zero) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 728 | (*this)[__i] = false; |
Howard Hinnant | 34d6b19 | 2010-11-17 21:53:14 +0000 | [diff] [blame] | 729 | else |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 730 | (*this)[__i] = true; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 731 | } |
Howard Hinnant | 0949eed | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 732 | _VSTD::fill(base::__make_iter(__i), base::__make_iter(_Size), false); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 733 | } |
| 734 | |
| 735 | template <size_t _Size> |
Howard Hinnant | 324bb03 | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 736 | template<class _CharT, class _Traits, class _Allocator> |
| 737 | bitset<_Size>::bitset(const basic_string<_CharT,_Traits,_Allocator>& __str, |
| 738 | typename basic_string<_CharT,_Traits,_Allocator>::size_type __pos, |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 739 | typename basic_string<_CharT,_Traits,_Allocator>::size_type __n, |
| 740 | _CharT __zero, _CharT __one) |
| 741 | { |
| 742 | if (__pos > __str.size()) |
| 743 | #ifndef _LIBCPP_NO_EXCEPTIONS |
| 744 | throw out_of_range("bitset string pos out of range"); |
| 745 | #else |
| 746 | assert(!"bitset string pos out of range"); |
| 747 | #endif |
Howard Hinnant | 0949eed | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 748 | size_t __rlen = _VSTD::min(__n, __str.size() - __pos); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 749 | for (size_t __i = __pos; __i < __pos + __rlen; ++__i) |
| 750 | if (!_Traits::eq(__str[__i], __zero) && !_Traits::eq(__str[__i], __one)) |
| 751 | #ifndef _LIBCPP_NO_EXCEPTIONS |
| 752 | throw invalid_argument("bitset string ctor has invalid argument"); |
| 753 | #else |
| 754 | assert(!"bitset string ctor has invalid argument"); |
| 755 | #endif |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 756 | size_t _Mp = _VSTD::min(__rlen, _Size); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 757 | size_t __i = 0; |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 758 | for (; __i < _Mp; ++__i) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 759 | { |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 760 | _CharT __c = __str[__pos + _Mp - 1 - __i]; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 761 | if (_Traits::eq(__c, __zero)) |
| 762 | (*this)[__i] = false; |
| 763 | else |
| 764 | (*this)[__i] = true; |
| 765 | } |
Howard Hinnant | 0949eed | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 766 | _VSTD::fill(base::__make_iter(__i), base::__make_iter(_Size), false); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 767 | } |
| 768 | |
| 769 | template <size_t _Size> |
| 770 | inline _LIBCPP_INLINE_VISIBILITY |
| 771 | bitset<_Size>& |
Howard Hinnant | 10f25d2 | 2011-05-27 20:52:28 +0000 | [diff] [blame] | 772 | bitset<_Size>::operator&=(const bitset& __rhs) _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 773 | { |
| 774 | base::operator&=(__rhs); |
| 775 | return *this; |
| 776 | } |
| 777 | |
| 778 | template <size_t _Size> |
| 779 | inline _LIBCPP_INLINE_VISIBILITY |
| 780 | bitset<_Size>& |
Howard Hinnant | 10f25d2 | 2011-05-27 20:52:28 +0000 | [diff] [blame] | 781 | bitset<_Size>::operator|=(const bitset& __rhs) _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 782 | { |
| 783 | base::operator|=(__rhs); |
| 784 | return *this; |
| 785 | } |
| 786 | |
| 787 | template <size_t _Size> |
| 788 | inline _LIBCPP_INLINE_VISIBILITY |
| 789 | bitset<_Size>& |
Howard Hinnant | 10f25d2 | 2011-05-27 20:52:28 +0000 | [diff] [blame] | 790 | bitset<_Size>::operator^=(const bitset& __rhs) _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 791 | { |
| 792 | base::operator^=(__rhs); |
| 793 | return *this; |
| 794 | } |
| 795 | |
| 796 | template <size_t _Size> |
| 797 | bitset<_Size>& |
Howard Hinnant | 10f25d2 | 2011-05-27 20:52:28 +0000 | [diff] [blame] | 798 | bitset<_Size>::operator<<=(size_t __pos) _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 799 | { |
Howard Hinnant | 0949eed | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 800 | __pos = _VSTD::min(__pos, _Size); |
| 801 | _VSTD::copy_backward(base::__make_iter(0), base::__make_iter(_Size - __pos), base::__make_iter(_Size)); |
| 802 | _VSTD::fill_n(base::__make_iter(0), __pos, false); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 803 | return *this; |
| 804 | } |
| 805 | |
| 806 | template <size_t _Size> |
| 807 | bitset<_Size>& |
Howard Hinnant | 10f25d2 | 2011-05-27 20:52:28 +0000 | [diff] [blame] | 808 | bitset<_Size>::operator>>=(size_t __pos) _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 809 | { |
Howard Hinnant | 0949eed | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 810 | __pos = _VSTD::min(__pos, _Size); |
| 811 | _VSTD::copy(base::__make_iter(__pos), base::__make_iter(_Size), base::__make_iter(0)); |
| 812 | _VSTD::fill_n(base::__make_iter(_Size - __pos), __pos, false); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 813 | return *this; |
| 814 | } |
| 815 | |
| 816 | template <size_t _Size> |
| 817 | inline _LIBCPP_INLINE_VISIBILITY |
| 818 | bitset<_Size>& |
Howard Hinnant | 10f25d2 | 2011-05-27 20:52:28 +0000 | [diff] [blame] | 819 | bitset<_Size>::set() _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 820 | { |
Howard Hinnant | 0949eed | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 821 | _VSTD::fill_n(base::__make_iter(0), _Size, true); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 822 | return *this; |
| 823 | } |
| 824 | |
| 825 | template <size_t _Size> |
| 826 | bitset<_Size>& |
| 827 | bitset<_Size>::set(size_t __pos, bool __val) |
| 828 | { |
| 829 | if (__pos >= _Size) |
| 830 | #ifndef _LIBCPP_NO_EXCEPTIONS |
| 831 | throw out_of_range("bitset set argument out of range"); |
| 832 | #else |
| 833 | assert(!"bitset set argument out of range"); |
| 834 | #endif |
| 835 | (*this)[__pos] = __val; |
| 836 | return *this; |
| 837 | } |
| 838 | |
| 839 | template <size_t _Size> |
| 840 | inline _LIBCPP_INLINE_VISIBILITY |
| 841 | bitset<_Size>& |
Howard Hinnant | 10f25d2 | 2011-05-27 20:52:28 +0000 | [diff] [blame] | 842 | bitset<_Size>::reset() _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 843 | { |
Howard Hinnant | 0949eed | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 844 | _VSTD::fill_n(base::__make_iter(0), _Size, false); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 845 | return *this; |
| 846 | } |
| 847 | |
| 848 | template <size_t _Size> |
| 849 | bitset<_Size>& |
| 850 | bitset<_Size>::reset(size_t __pos) |
| 851 | { |
| 852 | if (__pos >= _Size) |
| 853 | #ifndef _LIBCPP_NO_EXCEPTIONS |
| 854 | throw out_of_range("bitset reset argument out of range"); |
| 855 | #else |
| 856 | assert(!"bitset reset argument out of range"); |
| 857 | #endif |
| 858 | (*this)[__pos] = false; |
| 859 | return *this; |
| 860 | } |
| 861 | |
| 862 | template <size_t _Size> |
| 863 | inline _LIBCPP_INLINE_VISIBILITY |
| 864 | bitset<_Size> |
Howard Hinnant | 10f25d2 | 2011-05-27 20:52:28 +0000 | [diff] [blame] | 865 | bitset<_Size>::operator~() const _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 866 | { |
| 867 | bitset __x(*this); |
| 868 | __x.flip(); |
| 869 | return __x; |
| 870 | } |
| 871 | |
| 872 | template <size_t _Size> |
| 873 | inline _LIBCPP_INLINE_VISIBILITY |
| 874 | bitset<_Size>& |
Howard Hinnant | 10f25d2 | 2011-05-27 20:52:28 +0000 | [diff] [blame] | 875 | bitset<_Size>::flip() _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 876 | { |
| 877 | base::flip(); |
| 878 | return *this; |
| 879 | } |
| 880 | |
| 881 | template <size_t _Size> |
| 882 | bitset<_Size>& |
| 883 | bitset<_Size>::flip(size_t __pos) |
| 884 | { |
| 885 | if (__pos >= _Size) |
| 886 | #ifndef _LIBCPP_NO_EXCEPTIONS |
| 887 | throw out_of_range("bitset flip argument out of range"); |
| 888 | #else |
| 889 | assert(!"bitset flip argument out of range"); |
| 890 | #endif |
| 891 | reference r = base::__make_ref(__pos); |
| 892 | r = ~r; |
| 893 | return *this; |
| 894 | } |
| 895 | |
| 896 | template <size_t _Size> |
| 897 | inline _LIBCPP_INLINE_VISIBILITY |
| 898 | unsigned long |
| 899 | bitset<_Size>::to_ulong() const |
| 900 | { |
| 901 | return base::to_ulong(); |
| 902 | } |
| 903 | |
| 904 | template <size_t _Size> |
| 905 | inline _LIBCPP_INLINE_VISIBILITY |
| 906 | unsigned long long |
| 907 | bitset<_Size>::to_ullong() const |
| 908 | { |
| 909 | return base::to_ullong(); |
| 910 | } |
| 911 | |
| 912 | template <size_t _Size> |
Howard Hinnant | 324bb03 | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 913 | template <class _CharT, class _Traits, class _Allocator> |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 914 | basic_string<_CharT, _Traits, _Allocator> |
| 915 | bitset<_Size>::to_string(_CharT __zero, _CharT __one) const |
| 916 | { |
| 917 | basic_string<_CharT, _Traits, _Allocator> __r(_Size, __zero); |
| 918 | for (size_t __i = 0; __i < _Size; ++__i) |
| 919 | { |
| 920 | if ((*this)[__i]) |
| 921 | __r[_Size - 1 - __i] = __one; |
| 922 | } |
| 923 | return __r; |
| 924 | } |
| 925 | |
| 926 | template <size_t _Size> |
Howard Hinnant | 324bb03 | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 927 | template <class _CharT, class _Traits> |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 928 | inline _LIBCPP_INLINE_VISIBILITY |
| 929 | basic_string<_CharT, _Traits, allocator<_CharT> > |
| 930 | bitset<_Size>::to_string(_CharT __zero, _CharT __one) const |
| 931 | { |
| 932 | return to_string<_CharT, _Traits, allocator<_CharT> >(__zero, __one); |
| 933 | } |
| 934 | |
| 935 | template <size_t _Size> |
Howard Hinnant | 324bb03 | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 936 | template <class _CharT> |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 937 | inline _LIBCPP_INLINE_VISIBILITY |
| 938 | basic_string<_CharT, char_traits<_CharT>, allocator<_CharT> > |
| 939 | bitset<_Size>::to_string(_CharT __zero, _CharT __one) const |
| 940 | { |
| 941 | return to_string<_CharT, char_traits<_CharT>, allocator<_CharT> >(__zero, __one); |
| 942 | } |
| 943 | |
| 944 | template <size_t _Size> |
| 945 | inline _LIBCPP_INLINE_VISIBILITY |
| 946 | basic_string<char, char_traits<char>, allocator<char> > |
| 947 | bitset<_Size>::to_string(char __zero, char __one) const |
| 948 | { |
| 949 | return to_string<char, char_traits<char>, allocator<char> >(__zero, __one); |
| 950 | } |
| 951 | |
| 952 | template <size_t _Size> |
| 953 | inline _LIBCPP_INLINE_VISIBILITY |
| 954 | size_t |
Howard Hinnant | 10f25d2 | 2011-05-27 20:52:28 +0000 | [diff] [blame] | 955 | bitset<_Size>::count() const _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 956 | { |
Howard Hinnant | 0949eed | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 957 | return static_cast<size_t>(_VSTD::count(base::__make_iter(0), base::__make_iter(_Size), true)); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 958 | } |
| 959 | |
| 960 | template <size_t _Size> |
| 961 | inline _LIBCPP_INLINE_VISIBILITY |
| 962 | bool |
Howard Hinnant | 10f25d2 | 2011-05-27 20:52:28 +0000 | [diff] [blame] | 963 | bitset<_Size>::operator==(const bitset& __rhs) const _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 964 | { |
Howard Hinnant | 0949eed | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 965 | return _VSTD::equal(base::__make_iter(0), base::__make_iter(_Size), __rhs.__make_iter(0)); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 966 | } |
| 967 | |
| 968 | template <size_t _Size> |
| 969 | inline _LIBCPP_INLINE_VISIBILITY |
| 970 | bool |
Howard Hinnant | 10f25d2 | 2011-05-27 20:52:28 +0000 | [diff] [blame] | 971 | bitset<_Size>::operator!=(const bitset& __rhs) const _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 972 | { |
| 973 | return !(*this == __rhs); |
| 974 | } |
| 975 | |
| 976 | template <size_t _Size> |
| 977 | bool |
| 978 | bitset<_Size>::test(size_t __pos) const |
| 979 | { |
| 980 | if (__pos >= _Size) |
| 981 | #ifndef _LIBCPP_NO_EXCEPTIONS |
| 982 | throw out_of_range("bitset test argument out of range"); |
| 983 | #else |
| 984 | assert(!"bitset test argument out of range"); |
| 985 | #endif |
| 986 | return (*this)[__pos]; |
| 987 | } |
| 988 | |
| 989 | template <size_t _Size> |
| 990 | inline _LIBCPP_INLINE_VISIBILITY |
| 991 | bool |
Howard Hinnant | 10f25d2 | 2011-05-27 20:52:28 +0000 | [diff] [blame] | 992 | bitset<_Size>::all() const _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 993 | { |
| 994 | return base::all(); |
| 995 | } |
| 996 | |
| 997 | template <size_t _Size> |
| 998 | inline _LIBCPP_INLINE_VISIBILITY |
| 999 | bool |
Howard Hinnant | 10f25d2 | 2011-05-27 20:52:28 +0000 | [diff] [blame] | 1000 | bitset<_Size>::any() const _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1001 | { |
| 1002 | return base::any(); |
| 1003 | } |
| 1004 | |
| 1005 | template <size_t _Size> |
| 1006 | inline _LIBCPP_INLINE_VISIBILITY |
| 1007 | bitset<_Size> |
Howard Hinnant | 10f25d2 | 2011-05-27 20:52:28 +0000 | [diff] [blame] | 1008 | bitset<_Size>::operator<<(size_t __pos) const _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1009 | { |
| 1010 | bitset __r = *this; |
| 1011 | __r <<= __pos; |
| 1012 | return __r; |
| 1013 | } |
| 1014 | |
| 1015 | template <size_t _Size> |
| 1016 | inline _LIBCPP_INLINE_VISIBILITY |
| 1017 | bitset<_Size> |
Howard Hinnant | 10f25d2 | 2011-05-27 20:52:28 +0000 | [diff] [blame] | 1018 | bitset<_Size>::operator>>(size_t __pos) const _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1019 | { |
| 1020 | bitset __r = *this; |
| 1021 | __r >>= __pos; |
| 1022 | return __r; |
| 1023 | } |
| 1024 | |
| 1025 | template <size_t _Size> |
| 1026 | inline _LIBCPP_INLINE_VISIBILITY |
| 1027 | bitset<_Size> |
Howard Hinnant | 10f25d2 | 2011-05-27 20:52:28 +0000 | [diff] [blame] | 1028 | operator&(const bitset<_Size>& __x, const bitset<_Size>& __y) _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1029 | { |
| 1030 | bitset<_Size> __r = __x; |
| 1031 | __r &= __y; |
| 1032 | return __r; |
| 1033 | } |
| 1034 | |
| 1035 | template <size_t _Size> |
| 1036 | inline _LIBCPP_INLINE_VISIBILITY |
| 1037 | bitset<_Size> |
Howard Hinnant | 10f25d2 | 2011-05-27 20:52:28 +0000 | [diff] [blame] | 1038 | operator|(const bitset<_Size>& __x, const bitset<_Size>& __y) _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1039 | { |
| 1040 | bitset<_Size> __r = __x; |
| 1041 | __r |= __y; |
| 1042 | return __r; |
| 1043 | } |
| 1044 | |
| 1045 | template <size_t _Size> |
| 1046 | inline _LIBCPP_INLINE_VISIBILITY |
| 1047 | bitset<_Size> |
Howard Hinnant | 10f25d2 | 2011-05-27 20:52:28 +0000 | [diff] [blame] | 1048 | operator^(const bitset<_Size>& __x, const bitset<_Size>& __y) _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1049 | { |
| 1050 | bitset<_Size> __r = __x; |
| 1051 | __r ^= __y; |
| 1052 | return __r; |
| 1053 | } |
| 1054 | |
| 1055 | template <size_t _Size> |
Howard Hinnant | 422a53f | 2010-09-21 21:28:23 +0000 | [diff] [blame] | 1056 | struct _LIBCPP_VISIBLE hash<bitset<_Size> > |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1057 | : public unary_function<bitset<_Size>, size_t> |
| 1058 | { |
Howard Hinnant | 422a53f | 2010-09-21 21:28:23 +0000 | [diff] [blame] | 1059 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 10f25d2 | 2011-05-27 20:52:28 +0000 | [diff] [blame] | 1060 | size_t operator()(const bitset<_Size>& __bs) const _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1061 | {return __bs.__hash_code();} |
| 1062 | }; |
| 1063 | |
Howard Hinnant | 464aa5c | 2011-07-18 15:51:59 +0000 | [diff] [blame] | 1064 | template <class _CharT, class _Traits, size_t _Size> |
| 1065 | basic_istream<_CharT, _Traits>& |
| 1066 | operator>>(basic_istream<_CharT, _Traits>& __is, bitset<_Size>& __x); |
| 1067 | |
| 1068 | template <class _CharT, class _Traits, size_t _Size> |
| 1069 | basic_ostream<_CharT, _Traits>& |
| 1070 | operator<<(basic_ostream<_CharT, _Traits>& __os, const bitset<_Size>& __x); |
| 1071 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1072 | _LIBCPP_END_NAMESPACE_STD |
| 1073 | |
| 1074 | #endif // _LIBCPP_BITSET |