OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef V8_BASE_FUNCTIONAL_H_ |
| 6 #define V8_BASE_FUNCTIONAL_H_ |
| 7 |
| 8 #include <cstddef> |
| 9 #include <functional> |
| 10 #include <utility> |
| 11 |
| 12 #include "include/v8stdint.h" |
| 13 #include "src/base/macros.h" |
| 14 |
| 15 namespace v8 { |
| 16 namespace base { |
| 17 |
| 18 // base::hash is an implementation of the hash function object specified by |
| 19 // C++11. It was designed to be compatible with std::hash (in C++11) and |
| 20 // boost:hash (which in turn is based on the hash function object specified by |
| 21 // the Draft Technical Report on C++ Library Extensions (TR1)). |
| 22 // |
| 23 // base::hash is implemented by calling the hash_value function. The namespace |
| 24 // isn't specified so that it can detect overloads via argument dependant |
| 25 // lookup. So if there is a free function hash_value in the same namespace as a |
| 26 // custom type, it will get called. |
| 27 // |
| 28 // If users are asked to implement a hash function for their own types with no |
| 29 // guidance, they generally write bad hash functions. Instead, we provide a |
| 30 // simple function base::hash_combine to pass hash-relevant member variables |
| 31 // into, in order to define a decent hash function. base::hash_combine is |
| 32 // declared as: |
| 33 // |
| 34 // template<typename T, typename... Ts> |
| 35 // size_t hash_combine(const T& v, const Ts& ...vs); |
| 36 // |
| 37 // Consider the following example: |
| 38 // |
| 39 // namespace v8 { |
| 40 // namespace bar { |
| 41 // struct Point { int x; int y; }; |
| 42 // size_t hash_value(Point const& p) { |
| 43 // return base::hash_combine(p.x, p.y); |
| 44 // } |
| 45 // } |
| 46 // |
| 47 // namespace foo { |
| 48 // void DoSomeWork(bar::Point const& p) { |
| 49 // base::hash<bar::Point> h; |
| 50 // ... |
| 51 // size_t hash_code = h(p); // calls bar::hash_value(Point const&) |
| 52 // ... |
| 53 // } |
| 54 // } |
| 55 // } |
| 56 // |
| 57 // Based on the "Hashing User-Defined Types in C++1y" proposal from Jeffrey |
| 58 // Yasskin and Chandler Carruth, see |
| 59 // http://www.open-std.org/Jtc1/sc22/wg21/docs/papers/2012/n3333.html. |
| 60 |
| 61 template <typename> |
| 62 struct hash; |
| 63 |
| 64 |
| 65 inline size_t hash_combine() { return 0u; } |
| 66 inline size_t hash_combine(size_t seed) { return seed; } |
| 67 size_t hash_combine(size_t seed, size_t value); |
| 68 template <typename T, typename... Ts> |
| 69 inline size_t hash_combine(T const& v, Ts const&... vs) { |
| 70 return hash_combine(hash<T>()(v), hash_combine(vs...)); |
| 71 } |
| 72 |
| 73 |
| 74 #define V8_BASE_HASH_VALUE_TRIVIAL(type) \ |
| 75 inline size_t hash_value(type v) { return static_cast<size_t>(v); } |
| 76 V8_BASE_HASH_VALUE_TRIVIAL(bool) |
| 77 V8_BASE_HASH_VALUE_TRIVIAL(unsigned char) |
| 78 V8_BASE_HASH_VALUE_TRIVIAL(unsigned short) // NOLINT(runtime/int) |
| 79 V8_BASE_HASH_VALUE_TRIVIAL(unsigned int) |
| 80 #undef V8_BASE_HASH_VALUE_TRIVIAL |
| 81 |
| 82 size_t hash_value(unsigned long v); // NOLINT(runtime/int) |
| 83 size_t hash_value(unsigned long long v); // NOLINT(runtime/int) |
| 84 |
| 85 #define V8_BASE_HASH_VALUE_SIGNED(type) \ |
| 86 inline size_t hash_value(signed type v) { \ |
| 87 return hash_value(bit_cast<unsigned type>(v)); \ |
| 88 } |
| 89 V8_BASE_HASH_VALUE_SIGNED(char) |
| 90 V8_BASE_HASH_VALUE_SIGNED(short) // NOLINT(runtime/int) |
| 91 V8_BASE_HASH_VALUE_SIGNED(int) // NOLINT(runtime/int) |
| 92 V8_BASE_HASH_VALUE_SIGNED(long) // NOLINT(runtime/int) |
| 93 V8_BASE_HASH_VALUE_SIGNED(long long) // NOLINT(runtime/int) |
| 94 #undef V8_BASE_HASH_VALUE_SIGNED |
| 95 |
| 96 size_t hash_value(float v); |
| 97 size_t hash_value(double v); |
| 98 |
| 99 template <typename T> |
| 100 inline size_t hash_value(T* const& v) { |
| 101 size_t const x = bit_cast<size_t>(v); |
| 102 return (x >> 3) + x; |
| 103 } |
| 104 |
| 105 template <typename T1, typename T2> |
| 106 inline size_t hash_value(std::pair<T1, T2> const& v) { |
| 107 return hash_combine(v.first, v.second); |
| 108 } |
| 109 |
| 110 |
| 111 template <typename T> |
| 112 struct hash : public std::unary_function<T, size_t> { |
| 113 size_t operator()(T const& v) const { return hash_value(v); } |
| 114 }; |
| 115 |
| 116 #define V8_BASE_HASH_SPECIALIZE(type) \ |
| 117 template <> \ |
| 118 struct hash<type> : public std::unary_function<type, size_t> { \ |
| 119 size_t operator()(type const v) const { \ |
| 120 return ::v8::base::hash_value(v); \ |
| 121 } \ |
| 122 }; |
| 123 V8_BASE_HASH_SPECIALIZE(bool) |
| 124 V8_BASE_HASH_SPECIALIZE(signed char) |
| 125 V8_BASE_HASH_SPECIALIZE(unsigned char) |
| 126 V8_BASE_HASH_SPECIALIZE(short) // NOLINT(runtime/int) |
| 127 V8_BASE_HASH_SPECIALIZE(unsigned short) // NOLINT(runtime/int) |
| 128 V8_BASE_HASH_SPECIALIZE(int) |
| 129 V8_BASE_HASH_SPECIALIZE(unsigned int) |
| 130 V8_BASE_HASH_SPECIALIZE(long) // NOLINT(runtime/int) |
| 131 V8_BASE_HASH_SPECIALIZE(unsigned long) // NOLINT(runtime/int) |
| 132 V8_BASE_HASH_SPECIALIZE(long long) // NOLINT(runtime/int) |
| 133 V8_BASE_HASH_SPECIALIZE(unsigned long long) // NOLINT(runtime/int) |
| 134 V8_BASE_HASH_SPECIALIZE(float) |
| 135 V8_BASE_HASH_SPECIALIZE(double) |
| 136 #undef V8_BASE_HASH_SPECIALIZE |
| 137 |
| 138 template <typename T> |
| 139 struct hash<T*> : public std::unary_function<T*, size_t> { |
| 140 size_t operator()(T* const v) const { return ::v8::base::hash_value(v); } |
| 141 }; |
| 142 |
| 143 template <typename T1, typename T2> |
| 144 struct hash<std::pair<T1, T2> > |
| 145 : public std::unary_function<std::pair<T1, T2>, size_t> { |
| 146 size_t operator()(std::pair<T1, T2> const& v) const { |
| 147 return ::v8::base::hash_value(v); |
| 148 } |
| 149 }; |
| 150 |
| 151 } // namespace base |
| 152 } // namespace v8 |
| 153 |
| 154 #endif // V8_BASE_FUNCTIONAL_H_ |
OLD | NEW |