Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 the V8 project authors. All rights reserved. | 1 // Copyright 2014 the V8 project authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef V8_BASE_FUNCTIONAL_H_ | 5 #ifndef V8_BASE_FUNCTIONAL_H_ |
| 6 #define V8_BASE_FUNCTIONAL_H_ | 6 #define V8_BASE_FUNCTIONAL_H_ |
| 7 | 7 |
| 8 #include <cstddef> | 8 #include <cstddef> |
| 9 #include <cstring> | |
| 9 #include <functional> | 10 #include <functional> |
| 10 #include <utility> | 11 #include <utility> |
| 11 | 12 |
| 12 #include "include/v8stdint.h" | 13 #include "include/v8stdint.h" |
| 13 #include "src/base/macros.h" | 14 #include "src/base/macros.h" |
| 14 | 15 |
| 15 namespace v8 { | 16 namespace v8 { |
| 16 namespace base { | 17 namespace base { |
| 17 | 18 |
| 18 // base::hash is an implementation of the hash function object specified by | 19 // base::hash is an implementation of the hash function object specified by |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 60 | 61 |
| 61 template <typename> | 62 template <typename> |
| 62 struct hash; | 63 struct hash; |
| 63 | 64 |
| 64 | 65 |
| 65 V8_INLINE size_t hash_combine() { return 0u; } | 66 V8_INLINE size_t hash_combine() { return 0u; } |
| 66 V8_INLINE size_t hash_combine(size_t seed) { return seed; } | 67 V8_INLINE size_t hash_combine(size_t seed) { return seed; } |
| 67 size_t hash_combine(size_t seed, size_t value); | 68 size_t hash_combine(size_t seed, size_t value); |
| 68 template <typename T, typename... Ts> | 69 template <typename T, typename... Ts> |
| 69 V8_INLINE size_t hash_combine(T const& v, Ts const&... vs) { | 70 V8_INLINE size_t hash_combine(T const& v, Ts const&... vs) { |
| 70 return hash_combine(hash<T>()(v), hash_combine(vs...)); | 71 return hash_combine(hash_combine(vs...), hash<T>()(v)); |
|
Benedikt Meurer
2014/10/08 05:56:34
Reordered because we pass the seed as first parame
| |
| 71 } | 72 } |
| 72 | 73 |
| 73 | 74 |
| 75 template <typename Iterator> | |
| 76 V8_INLINE size_t hash_range(Iterator first, Iterator last) { | |
| 77 size_t seed = 0; | |
| 78 for (; first != last; ++first) { | |
| 79 seed = hash_combine(seed, *first); | |
| 80 } | |
| 81 return seed; | |
| 82 } | |
| 83 | |
| 84 | |
| 74 #define V8_BASE_HASH_VALUE_TRIVIAL(type) \ | 85 #define V8_BASE_HASH_VALUE_TRIVIAL(type) \ |
| 75 V8_INLINE size_t hash_value(type v) { return static_cast<size_t>(v); } | 86 V8_INLINE size_t hash_value(type v) { return static_cast<size_t>(v); } |
| 76 V8_BASE_HASH_VALUE_TRIVIAL(bool) | 87 V8_BASE_HASH_VALUE_TRIVIAL(bool) |
| 77 V8_BASE_HASH_VALUE_TRIVIAL(unsigned char) | 88 V8_BASE_HASH_VALUE_TRIVIAL(unsigned char) |
| 78 V8_BASE_HASH_VALUE_TRIVIAL(unsigned short) // NOLINT(runtime/int) | 89 V8_BASE_HASH_VALUE_TRIVIAL(unsigned short) // NOLINT(runtime/int) |
| 79 #undef V8_BASE_HASH_VALUE_TRIVIAL | 90 #undef V8_BASE_HASH_VALUE_TRIVIAL |
| 80 | 91 |
| 81 size_t hash_value(unsigned int); | 92 size_t hash_value(unsigned int); |
| 82 size_t hash_value(unsigned long); // NOLINT(runtime/int) | 93 size_t hash_value(unsigned long); // NOLINT(runtime/int) |
| 83 size_t hash_value(unsigned long long); // NOLINT(runtime/int) | 94 size_t hash_value(unsigned long long); // NOLINT(runtime/int) |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 96 V8_INLINE size_t hash_value(float v) { | 107 V8_INLINE size_t hash_value(float v) { |
| 97 // 0 and -0 both hash to zero. | 108 // 0 and -0 both hash to zero. |
| 98 return v != 0.0f ? hash_value(bit_cast<uint32_t>(v)) : 0; | 109 return v != 0.0f ? hash_value(bit_cast<uint32_t>(v)) : 0; |
| 99 } | 110 } |
| 100 | 111 |
| 101 V8_INLINE size_t hash_value(double v) { | 112 V8_INLINE size_t hash_value(double v) { |
| 102 // 0 and -0 both hash to zero. | 113 // 0 and -0 both hash to zero. |
| 103 return v != 0.0 ? hash_value(bit_cast<uint64_t>(v)) : 0; | 114 return v != 0.0 ? hash_value(bit_cast<uint64_t>(v)) : 0; |
| 104 } | 115 } |
| 105 | 116 |
| 117 template <typename T, size_t N> | |
| 118 V8_INLINE size_t hash_value(const T (&v)[N]) { | |
| 119 return hash_range(v, v + N); | |
| 120 } | |
| 121 | |
| 122 template <typename T, size_t N> | |
| 123 V8_INLINE size_t hash_value(T (&v)[N]) { | |
| 124 return hash_range(v, v + N); | |
| 125 } | |
| 126 | |
| 106 template <typename T> | 127 template <typename T> |
| 107 V8_INLINE size_t hash_value(T* const& v) { | 128 V8_INLINE size_t hash_value(T* const& v) { |
| 108 return hash_value(bit_cast<uintptr_t>(v)); | 129 return hash_value(bit_cast<uintptr_t>(v)); |
| 109 } | 130 } |
| 110 | 131 |
| 111 template <typename T1, typename T2> | 132 template <typename T1, typename T2> |
| 112 V8_INLINE size_t hash_value(std::pair<T1, T2> const& v) { | 133 V8_INLINE size_t hash_value(std::pair<T1, T2> const& v) { |
| 113 return hash_combine(v.first, v.second); | 134 return hash_combine(v.first, v.second); |
| 114 } | 135 } |
| 115 | 136 |
| (...skipping 25 matching lines...) Expand all Loading... | |
| 141 V8_BASE_HASH_SPECIALIZE(double) | 162 V8_BASE_HASH_SPECIALIZE(double) |
| 142 #undef V8_BASE_HASH_SPECIALIZE | 163 #undef V8_BASE_HASH_SPECIALIZE |
| 143 | 164 |
| 144 template <typename T> | 165 template <typename T> |
| 145 struct hash<T*> : public std::unary_function<T*, size_t> { | 166 struct hash<T*> : public std::unary_function<T*, size_t> { |
| 146 V8_INLINE size_t operator()(T* const v) const { | 167 V8_INLINE size_t operator()(T* const v) const { |
| 147 return ::v8::base::hash_value(v); | 168 return ::v8::base::hash_value(v); |
| 148 } | 169 } |
| 149 }; | 170 }; |
| 150 | 171 |
| 151 template <typename T1, typename T2> | 172 |
| 152 struct hash<std::pair<T1, T2> > | 173 // base::bit_equal_to is a function object class for bitwise equality |
| 153 : public std::unary_function<std::pair<T1, T2>, size_t> { | 174 // comparison, similar to std::equal_to, except that the comparison is performed |
| 154 V8_INLINE size_t operator()(std::pair<T1, T2> const& v) const { | 175 // on the bit representation of the operands. |
| 155 return ::v8::base::hash_value(v); | 176 // |
| 177 // base::bit_hash is a function object class for bitwise hashing, similar to | |
| 178 // base::hash. It can be used together with base::bit_equal_to to implement a | |
| 179 // hash data structure based on the bitwise representation of types. | |
| 180 | |
| 181 template <typename T> | |
| 182 struct bit_equal_to : public std::binary_function<T, T, bool> { | |
| 183 V8_INLINE bool operator()(T const& lhs, T const& rhs) const { | |
| 184 return memcmp(&lhs, &rhs, sizeof(T)) == 0; | |
| 156 } | 185 } |
| 157 }; | 186 }; |
| 158 | 187 |
| 188 template <typename T> | |
| 189 struct bit_hash : public std::unary_function<T, size_t> { | |
| 190 V8_INLINE size_t operator()(T const& v) const { | |
| 191 uint8_t const* const p = reinterpret_cast<uint8_t const*>(&v); | |
| 192 return hash_range(p, p + sizeof(v)); | |
| 193 } | |
| 194 }; | |
| 195 | |
| 196 #define V8_BASE_BIT_SPECIALIZE_TRIVIAL(type) \ | |
| 197 template <> \ | |
| 198 struct bit_equal_to<type> : public std::equal_to<type> {}; \ | |
| 199 template <> \ | |
| 200 struct bit_hash<type> : public hash<type> {}; | |
| 201 V8_BASE_BIT_SPECIALIZE_TRIVIAL(signed char) | |
| 202 V8_BASE_BIT_SPECIALIZE_TRIVIAL(unsigned char) | |
| 203 V8_BASE_BIT_SPECIALIZE_TRIVIAL(short) // NOLINT(runtime/int) | |
| 204 V8_BASE_BIT_SPECIALIZE_TRIVIAL(unsigned short) // NOLINT(runtime/int) | |
| 205 V8_BASE_BIT_SPECIALIZE_TRIVIAL(int) | |
| 206 V8_BASE_BIT_SPECIALIZE_TRIVIAL(unsigned int) | |
| 207 V8_BASE_BIT_SPECIALIZE_TRIVIAL(long) // NOLINT(runtime/int) | |
| 208 V8_BASE_BIT_SPECIALIZE_TRIVIAL(unsigned long) // NOLINT(runtime/int) | |
| 209 V8_BASE_BIT_SPECIALIZE_TRIVIAL(long long) // NOLINT(runtime/int) | |
| 210 V8_BASE_BIT_SPECIALIZE_TRIVIAL(unsigned long long) // NOLINT(runtime/int) | |
| 211 #undef V8_BASE_BIT_SPECIALIZE_TRIVIAL | |
| 212 | |
| 213 #define V8_BASE_BIT_SPECIALIZE_BIT_CAST(type, btype) \ | |
| 214 template <> \ | |
| 215 struct bit_equal_to<type> : public std::binary_function<type, type, bool> { \ | |
| 216 V8_INLINE bool operator()(type lhs, type rhs) const { \ | |
| 217 return bit_cast<btype>(lhs) == bit_cast<btype>(rhs); \ | |
| 218 } \ | |
| 219 }; \ | |
| 220 template <> \ | |
| 221 struct bit_hash<type> : public std::unary_function<type, size_t> { \ | |
| 222 V8_INLINE size_t operator()(type v) const { \ | |
| 223 hash<btype> h; \ | |
| 224 return h(bit_cast<btype>(v)); \ | |
| 225 } \ | |
| 226 }; | |
| 227 V8_BASE_BIT_SPECIALIZE_BIT_CAST(float, uint32_t) | |
| 228 V8_BASE_BIT_SPECIALIZE_BIT_CAST(double, uint64_t) | |
| 229 #undef V8_BASE_BIT_SPECIALIZE_BIT_CAST | |
| 230 | |
| 159 } // namespace base | 231 } // namespace base |
| 160 } // namespace v8 | 232 } // namespace v8 |
| 161 | 233 |
| 162 #endif // V8_BASE_FUNCTIONAL_H_ | 234 #endif // V8_BASE_FUNCTIONAL_H_ |
| OLD | NEW |