blob: ff0d8075b943079de99555dc9ec733ca5ba0337b [file] [log] [blame]
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001// 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 <stddef.h>
9#include <stdint.h>
10
11#include <cstddef>
12#include <cstring>
13#include <functional>
14#include <utility>
15
16#include "src/base/macros.h"
17
18namespace v8 {
19namespace base {
20
21// base::hash is an implementation of the hash function object specified by
22// C++11. It was designed to be compatible with std::hash (in C++11) and
23// boost:hash (which in turn is based on the hash function object specified by
24// the Draft Technical Report on C++ Library Extensions (TR1)).
25//
26// base::hash is implemented by calling the hash_value function. The namespace
27// isn't specified so that it can detect overloads via argument dependant
28// lookup. So if there is a free function hash_value in the same namespace as a
29// custom type, it will get called.
30//
31// If users are asked to implement a hash function for their own types with no
32// guidance, they generally write bad hash functions. Instead, we provide a
33// simple function base::hash_combine to pass hash-relevant member variables
34// into, in order to define a decent hash function. base::hash_combine is
35// declared as:
36//
37// template<typename T, typename... Ts>
38// size_t hash_combine(const T& v, const Ts& ...vs);
39//
40// Consider the following example:
41//
42// namespace v8 {
43// namespace bar {
44// struct Point { int x; int y; };
45// size_t hash_value(Point const& p) {
46// return base::hash_combine(p.x, p.y);
47// }
48// }
49//
50// namespace foo {
51// void DoSomeWork(bar::Point const& p) {
52// base::hash<bar::Point> h;
53// ...
54// size_t hash_code = h(p); // calls bar::hash_value(Point const&)
55// ...
56// }
57// }
58// }
59//
60// Based on the "Hashing User-Defined Types in C++1y" proposal from Jeffrey
61// Yasskin and Chandler Carruth, see
62// http://www.open-std.org/Jtc1/sc22/wg21/docs/papers/2012/n3333.html.
63
64template <typename>
65struct hash;
66
67
68V8_INLINE size_t hash_combine() { return 0u; }
69V8_INLINE size_t hash_combine(size_t seed) { return seed; }
70size_t hash_combine(size_t seed, size_t value);
71template <typename T, typename... Ts>
72V8_INLINE size_t hash_combine(T const& v, Ts const&... vs) {
73 return hash_combine(hash_combine(vs...), hash<T>()(v));
74}
75
76
77template <typename Iterator>
78V8_INLINE size_t hash_range(Iterator first, Iterator last) {
79 size_t seed = 0;
80 for (; first != last; ++first) {
81 seed = hash_combine(seed, *first);
82 }
83 return seed;
84}
85
86
87#define V8_BASE_HASH_VALUE_TRIVIAL(type) \
88 V8_INLINE size_t hash_value(type v) { return static_cast<size_t>(v); }
89V8_BASE_HASH_VALUE_TRIVIAL(bool)
90V8_BASE_HASH_VALUE_TRIVIAL(unsigned char)
91V8_BASE_HASH_VALUE_TRIVIAL(unsigned short) // NOLINT(runtime/int)
92#undef V8_BASE_HASH_VALUE_TRIVIAL
93
94size_t hash_value(unsigned int);
95size_t hash_value(unsigned long); // NOLINT(runtime/int)
96size_t hash_value(unsigned long long); // NOLINT(runtime/int)
97
98#define V8_BASE_HASH_VALUE_SIGNED(type) \
99 V8_INLINE size_t hash_value(signed type v) { \
100 return hash_value(bit_cast<unsigned type>(v)); \
101 }
102V8_BASE_HASH_VALUE_SIGNED(char)
103V8_BASE_HASH_VALUE_SIGNED(short) // NOLINT(runtime/int)
104V8_BASE_HASH_VALUE_SIGNED(int) // NOLINT(runtime/int)
105V8_BASE_HASH_VALUE_SIGNED(long) // NOLINT(runtime/int)
106V8_BASE_HASH_VALUE_SIGNED(long long) // NOLINT(runtime/int)
107#undef V8_BASE_HASH_VALUE_SIGNED
108
109V8_INLINE size_t hash_value(float v) {
110 // 0 and -0 both hash to zero.
111 return v != 0.0f ? hash_value(bit_cast<uint32_t>(v)) : 0;
112}
113
114V8_INLINE size_t hash_value(double v) {
115 // 0 and -0 both hash to zero.
116 return v != 0.0 ? hash_value(bit_cast<uint64_t>(v)) : 0;
117}
118
119template <typename T, size_t N>
120V8_INLINE size_t hash_value(const T (&v)[N]) {
121 return hash_range(v, v + N);
122}
123
124template <typename T, size_t N>
125V8_INLINE size_t hash_value(T (&v)[N]) {
126 return hash_range(v, v + N);
127}
128
129template <typename T>
130V8_INLINE size_t hash_value(T* const& v) {
131 return hash_value(bit_cast<uintptr_t>(v));
132}
133
134template <typename T1, typename T2>
135V8_INLINE size_t hash_value(std::pair<T1, T2> const& v) {
136 return hash_combine(v.first, v.second);
137}
138
139
140template <typename T>
141struct hash : public std::unary_function<T, size_t> {
142 V8_INLINE size_t operator()(T const& v) const { return hash_value(v); }
143};
144
145#define V8_BASE_HASH_SPECIALIZE(type) \
146 template <> \
147 struct hash<type> : public std::unary_function<type, size_t> { \
148 V8_INLINE size_t operator()(type const v) const { \
149 return ::v8::base::hash_value(v); \
150 } \
151 };
152V8_BASE_HASH_SPECIALIZE(bool)
153V8_BASE_HASH_SPECIALIZE(signed char)
154V8_BASE_HASH_SPECIALIZE(unsigned char)
155V8_BASE_HASH_SPECIALIZE(short) // NOLINT(runtime/int)
156V8_BASE_HASH_SPECIALIZE(unsigned short) // NOLINT(runtime/int)
157V8_BASE_HASH_SPECIALIZE(int)
158V8_BASE_HASH_SPECIALIZE(unsigned int)
159V8_BASE_HASH_SPECIALIZE(long) // NOLINT(runtime/int)
160V8_BASE_HASH_SPECIALIZE(unsigned long) // NOLINT(runtime/int)
161V8_BASE_HASH_SPECIALIZE(long long) // NOLINT(runtime/int)
162V8_BASE_HASH_SPECIALIZE(unsigned long long) // NOLINT(runtime/int)
163V8_BASE_HASH_SPECIALIZE(float)
164V8_BASE_HASH_SPECIALIZE(double)
165#undef V8_BASE_HASH_SPECIALIZE
166
167template <typename T>
168struct hash<T*> : public std::unary_function<T*, size_t> {
169 V8_INLINE size_t operator()(T* const v) const {
170 return ::v8::base::hash_value(v);
171 }
172};
173
174
175// base::bit_equal_to is a function object class for bitwise equality
176// comparison, similar to std::equal_to, except that the comparison is performed
177// on the bit representation of the operands.
178//
179// base::bit_hash is a function object class for bitwise hashing, similar to
180// base::hash. It can be used together with base::bit_equal_to to implement a
181// hash data structure based on the bitwise representation of types.
182
183template <typename T>
184struct bit_equal_to : public std::binary_function<T, T, bool> {};
185
186template <typename T>
187struct bit_hash : public std::unary_function<T, size_t> {};
188
189#define V8_BASE_BIT_SPECIALIZE_TRIVIAL(type) \
190 template <> \
191 struct bit_equal_to<type> : public std::equal_to<type> {}; \
192 template <> \
193 struct bit_hash<type> : public hash<type> {};
194V8_BASE_BIT_SPECIALIZE_TRIVIAL(signed char)
195V8_BASE_BIT_SPECIALIZE_TRIVIAL(unsigned char)
196V8_BASE_BIT_SPECIALIZE_TRIVIAL(short) // NOLINT(runtime/int)
197V8_BASE_BIT_SPECIALIZE_TRIVIAL(unsigned short) // NOLINT(runtime/int)
198V8_BASE_BIT_SPECIALIZE_TRIVIAL(int)
199V8_BASE_BIT_SPECIALIZE_TRIVIAL(unsigned int)
200V8_BASE_BIT_SPECIALIZE_TRIVIAL(long) // NOLINT(runtime/int)
201V8_BASE_BIT_SPECIALIZE_TRIVIAL(unsigned long) // NOLINT(runtime/int)
202V8_BASE_BIT_SPECIALIZE_TRIVIAL(long long) // NOLINT(runtime/int)
203V8_BASE_BIT_SPECIALIZE_TRIVIAL(unsigned long long) // NOLINT(runtime/int)
204#undef V8_BASE_BIT_SPECIALIZE_TRIVIAL
205
206#define V8_BASE_BIT_SPECIALIZE_BIT_CAST(type, btype) \
207 template <> \
208 struct bit_equal_to<type> : public std::binary_function<type, type, bool> { \
209 V8_INLINE bool operator()(type lhs, type rhs) const { \
210 return bit_cast<btype>(lhs) == bit_cast<btype>(rhs); \
211 } \
212 }; \
213 template <> \
214 struct bit_hash<type> : public std::unary_function<type, size_t> { \
215 V8_INLINE size_t operator()(type v) const { \
216 hash<btype> h; \
217 return h(bit_cast<btype>(v)); \
218 } \
219 };
220V8_BASE_BIT_SPECIALIZE_BIT_CAST(float, uint32_t)
221V8_BASE_BIT_SPECIALIZE_BIT_CAST(double, uint64_t)
222#undef V8_BASE_BIT_SPECIALIZE_BIT_CAST
223
224} // namespace base
225} // namespace v8
226
227#endif // V8_BASE_FUNCTIONAL_H_