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 <functional> | 9 #include <functional> |
10 #include <utility> | 10 #include <utility> |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
55 // } | 55 // } |
56 // | 56 // |
57 // Based on the "Hashing User-Defined Types in C++1y" proposal from Jeffrey | 57 // Based on the "Hashing User-Defined Types in C++1y" proposal from Jeffrey |
58 // Yasskin and Chandler Carruth, see | 58 // Yasskin and Chandler Carruth, see |
59 // http://www.open-std.org/Jtc1/sc22/wg21/docs/papers/2012/n3333.html. | 59 // http://www.open-std.org/Jtc1/sc22/wg21/docs/papers/2012/n3333.html. |
60 | 60 |
61 template <typename> | 61 template <typename> |
62 struct hash; | 62 struct hash; |
63 | 63 |
64 | 64 |
65 inline size_t hash_combine() { return 0u; } | 65 V8_INLINE size_t hash_combine() { return 0u; } |
66 inline size_t hash_combine(size_t seed) { return seed; } | 66 V8_INLINE size_t hash_combine(size_t seed) { return seed; } |
67 size_t hash_combine(size_t seed, size_t value); | 67 size_t hash_combine(size_t seed, size_t value); |
68 template <typename T, typename... Ts> | 68 template <typename T, typename... Ts> |
69 inline size_t hash_combine(T const& v, Ts const&... vs) { | 69 V8_INLINE size_t hash_combine(T const& v, Ts const&... vs) { |
70 return hash_combine(hash<T>()(v), hash_combine(vs...)); | 70 return hash_combine(hash<T>()(v), hash_combine(vs...)); |
71 } | 71 } |
72 | 72 |
73 | 73 |
74 #define V8_BASE_HASH_VALUE_TRIVIAL(type) \ | 74 #define V8_BASE_HASH_VALUE_TRIVIAL(type) \ |
75 inline size_t hash_value(type v) { return static_cast<size_t>(v); } | 75 V8_INLINE size_t hash_value(type v) { return static_cast<size_t>(v); } |
76 V8_BASE_HASH_VALUE_TRIVIAL(bool) | 76 V8_BASE_HASH_VALUE_TRIVIAL(bool) |
77 V8_BASE_HASH_VALUE_TRIVIAL(unsigned char) | 77 V8_BASE_HASH_VALUE_TRIVIAL(unsigned char) |
78 V8_BASE_HASH_VALUE_TRIVIAL(unsigned short) // NOLINT(runtime/int) | 78 V8_BASE_HASH_VALUE_TRIVIAL(unsigned short) // NOLINT(runtime/int) |
79 #undef V8_BASE_HASH_VALUE_TRIVIAL | 79 #undef V8_BASE_HASH_VALUE_TRIVIAL |
80 | 80 |
81 size_t hash_value(unsigned int); | 81 size_t hash_value(unsigned int); |
82 size_t hash_value(unsigned long); // NOLINT(runtime/int) | 82 size_t hash_value(unsigned long); // NOLINT(runtime/int) |
83 size_t hash_value(unsigned long long); // NOLINT(runtime/int) | 83 size_t hash_value(unsigned long long); // NOLINT(runtime/int) |
84 | 84 |
85 #define V8_BASE_HASH_VALUE_SIGNED(type) \ | 85 #define V8_BASE_HASH_VALUE_SIGNED(type) \ |
86 inline size_t hash_value(signed type v) { \ | 86 V8_INLINE size_t hash_value(signed type v) { \ |
87 return hash_value(bit_cast<unsigned type>(v)); \ | 87 return hash_value(bit_cast<unsigned type>(v)); \ |
88 } | 88 } |
89 V8_BASE_HASH_VALUE_SIGNED(char) | 89 V8_BASE_HASH_VALUE_SIGNED(char) |
90 V8_BASE_HASH_VALUE_SIGNED(short) // NOLINT(runtime/int) | 90 V8_BASE_HASH_VALUE_SIGNED(short) // NOLINT(runtime/int) |
91 V8_BASE_HASH_VALUE_SIGNED(int) // NOLINT(runtime/int) | 91 V8_BASE_HASH_VALUE_SIGNED(int) // NOLINT(runtime/int) |
92 V8_BASE_HASH_VALUE_SIGNED(long) // NOLINT(runtime/int) | 92 V8_BASE_HASH_VALUE_SIGNED(long) // NOLINT(runtime/int) |
93 V8_BASE_HASH_VALUE_SIGNED(long long) // NOLINT(runtime/int) | 93 V8_BASE_HASH_VALUE_SIGNED(long long) // NOLINT(runtime/int) |
94 #undef V8_BASE_HASH_VALUE_SIGNED | 94 #undef V8_BASE_HASH_VALUE_SIGNED |
95 | 95 |
96 size_t hash_value(float v); | 96 V8_INLINE size_t hash_value(float v) { |
97 size_t hash_value(double v); | 97 // 0 and -0 both hash to zero. |
| 98 return v != 0.0f ? hash_value(bit_cast<uint32_t>(v)) : 0; |
| 99 } |
| 100 |
| 101 V8_INLINE size_t hash_value(double v) { |
| 102 // 0 and -0 both hash to zero. |
| 103 return v != 0.0 ? hash_value(bit_cast<uint64_t>(v)) : 0; |
| 104 } |
98 | 105 |
99 template <typename T> | 106 template <typename T> |
100 inline size_t hash_value(T* const& v) { | 107 V8_INLINE size_t hash_value(T* const& v) { |
101 size_t const x = bit_cast<size_t>(v); | 108 return hash_value(bit_cast<uintptr_t>(v)); |
102 return (x >> 3) + x; | |
103 } | 109 } |
104 | 110 |
105 template <typename T1, typename T2> | 111 template <typename T1, typename T2> |
106 inline size_t hash_value(std::pair<T1, T2> const& v) { | 112 V8_INLINE size_t hash_value(std::pair<T1, T2> const& v) { |
107 return hash_combine(v.first, v.second); | 113 return hash_combine(v.first, v.second); |
108 } | 114 } |
109 | 115 |
110 | 116 |
111 template <typename T> | 117 template <typename T> |
112 struct hash : public std::unary_function<T, size_t> { | 118 struct hash : public std::unary_function<T, size_t> { |
113 size_t operator()(T const& v) const { return hash_value(v); } | 119 V8_INLINE size_t operator()(T const& v) const { return hash_value(v); } |
114 }; | 120 }; |
115 | 121 |
116 #define V8_BASE_HASH_SPECIALIZE(type) \ | 122 #define V8_BASE_HASH_SPECIALIZE(type) \ |
117 template <> \ | 123 template <> \ |
118 struct hash<type> : public std::unary_function<type, size_t> { \ | 124 struct hash<type> : public std::unary_function<type, size_t> { \ |
119 size_t operator()(type const v) const { \ | 125 V8_INLINE size_t operator()(type const v) const { \ |
120 return ::v8::base::hash_value(v); \ | 126 return ::v8::base::hash_value(v); \ |
121 } \ | 127 } \ |
122 }; | 128 }; |
123 V8_BASE_HASH_SPECIALIZE(bool) | 129 V8_BASE_HASH_SPECIALIZE(bool) |
124 V8_BASE_HASH_SPECIALIZE(signed char) | 130 V8_BASE_HASH_SPECIALIZE(signed char) |
125 V8_BASE_HASH_SPECIALIZE(unsigned char) | 131 V8_BASE_HASH_SPECIALIZE(unsigned char) |
126 V8_BASE_HASH_SPECIALIZE(short) // NOLINT(runtime/int) | 132 V8_BASE_HASH_SPECIALIZE(short) // NOLINT(runtime/int) |
127 V8_BASE_HASH_SPECIALIZE(unsigned short) // NOLINT(runtime/int) | 133 V8_BASE_HASH_SPECIALIZE(unsigned short) // NOLINT(runtime/int) |
128 V8_BASE_HASH_SPECIALIZE(int) | 134 V8_BASE_HASH_SPECIALIZE(int) |
129 V8_BASE_HASH_SPECIALIZE(unsigned int) | 135 V8_BASE_HASH_SPECIALIZE(unsigned int) |
130 V8_BASE_HASH_SPECIALIZE(long) // NOLINT(runtime/int) | 136 V8_BASE_HASH_SPECIALIZE(long) // NOLINT(runtime/int) |
131 V8_BASE_HASH_SPECIALIZE(unsigned long) // NOLINT(runtime/int) | 137 V8_BASE_HASH_SPECIALIZE(unsigned long) // NOLINT(runtime/int) |
132 V8_BASE_HASH_SPECIALIZE(long long) // NOLINT(runtime/int) | 138 V8_BASE_HASH_SPECIALIZE(long long) // NOLINT(runtime/int) |
133 V8_BASE_HASH_SPECIALIZE(unsigned long long) // NOLINT(runtime/int) | 139 V8_BASE_HASH_SPECIALIZE(unsigned long long) // NOLINT(runtime/int) |
134 V8_BASE_HASH_SPECIALIZE(float) | 140 V8_BASE_HASH_SPECIALIZE(float) |
135 V8_BASE_HASH_SPECIALIZE(double) | 141 V8_BASE_HASH_SPECIALIZE(double) |
136 #undef V8_BASE_HASH_SPECIALIZE | 142 #undef V8_BASE_HASH_SPECIALIZE |
137 | 143 |
138 template <typename T> | 144 template <typename T> |
139 struct hash<T*> : public std::unary_function<T*, size_t> { | 145 struct hash<T*> : public std::unary_function<T*, size_t> { |
140 size_t operator()(T* const v) const { return ::v8::base::hash_value(v); } | 146 V8_INLINE size_t operator()(T* const v) const { |
| 147 return ::v8::base::hash_value(v); |
| 148 } |
141 }; | 149 }; |
142 | 150 |
143 template <typename T1, typename T2> | 151 template <typename T1, typename T2> |
144 struct hash<std::pair<T1, T2> > | 152 struct hash<std::pair<T1, T2> > |
145 : public std::unary_function<std::pair<T1, T2>, size_t> { | 153 : public std::unary_function<std::pair<T1, T2>, size_t> { |
146 size_t operator()(std::pair<T1, T2> const& v) const { | 154 V8_INLINE size_t operator()(std::pair<T1, T2> const& v) const { |
147 return ::v8::base::hash_value(v); | 155 return ::v8::base::hash_value(v); |
148 } | 156 } |
149 }; | 157 }; |
150 | 158 |
151 } // namespace base | 159 } // namespace base |
152 } // namespace v8 | 160 } // namespace v8 |
153 | 161 |
154 #endif // V8_BASE_FUNCTIONAL_H_ | 162 #endif // V8_BASE_FUNCTIONAL_H_ |
OLD | NEW |