5 #ifndef V8_BASE_FUNCTIONAL_H_ 6 #define V8_BASE_FUNCTIONAL_H_ 16 #include "src/base/base-export.h" 17 #include "src/base/macros.h" 69 V8_INLINE
size_t hash_combine() {
return 0u; }
70 V8_INLINE
size_t hash_combine(
size_t seed) {
return seed; }
71 V8_BASE_EXPORT
size_t hash_combine(
size_t seed,
size_t value);
72 template <
typename T,
typename... Ts>
73 V8_INLINE
size_t hash_combine(
T const& v, Ts
const&... vs) {
74 return hash_combine(hash_combine(vs...), hash<T>()(v));
78 template <
typename Iterator>
79 V8_INLINE
size_t hash_range(Iterator first, Iterator last) {
81 for (; first != last; ++first) {
82 seed = hash_combine(seed, *first);
88 #define V8_BASE_HASH_VALUE_TRIVIAL(type) \ 89 V8_INLINE size_t hash_value(type v) { return static_cast<size_t>(v); } 90 V8_BASE_HASH_VALUE_TRIVIAL(
bool)
91 V8_BASE_HASH_VALUE_TRIVIAL(
unsigned char)
92 V8_BASE_HASH_VALUE_TRIVIAL(
unsigned short)
93 #undef V8_BASE_HASH_VALUE_TRIVIAL 95 V8_BASE_EXPORT
size_t hash_value(
unsigned int);
96 V8_BASE_EXPORT
size_t hash_value(
unsigned long);
97 V8_BASE_EXPORT
size_t hash_value(
unsigned long long);
99 #define V8_BASE_HASH_VALUE_SIGNED(type) \ 100 V8_INLINE size_t hash_value(signed type v) { \ 101 return hash_value(bit_cast<unsigned type>(v)); \ 103 V8_BASE_HASH_VALUE_SIGNED(
char)
104 V8_BASE_HASH_VALUE_SIGNED(
short)
105 V8_BASE_HASH_VALUE_SIGNED(
int)
106 V8_BASE_HASH_VALUE_SIGNED(
long)
107 V8_BASE_HASH_VALUE_SIGNED(
long long)
108 #undef V8_BASE_HASH_VALUE_SIGNED 110 V8_INLINE
size_t hash_value(
float v) {
112 return v != 0.0f ? hash_value(bit_cast<uint32_t>(v)) : 0;
115 V8_INLINE
size_t hash_value(
double v) {
117 return v != 0.0 ? hash_value(bit_cast<uint64_t>(v)) : 0;
120 template <
typename T,
size_t N>
121 V8_INLINE
size_t hash_value(
const T (&v)[N]) {
122 return hash_range(v, v + N);
125 template <
typename T,
size_t N>
126 V8_INLINE
size_t hash_value(T (&v)[N]) {
127 return hash_range(v, v + N);
130 template <
typename T>
131 V8_INLINE
size_t hash_value(T*
const& v) {
132 return hash_value(bit_cast<uintptr_t>(v));
135 template <
typename T1,
typename T2>
136 V8_INLINE
size_t hash_value(std::pair<T1, T2>
const& v) {
137 return hash_combine(v.first, v.second);
140 template <
typename T>
142 V8_INLINE
size_t operator()(T
const& v)
const {
return hash_value(v); }
145 #define V8_BASE_HASH_SPECIALIZE(type) \ 147 struct hash<type> { \ 148 V8_INLINE size_t operator()(type const v) const { \ 149 return ::v8::base::hash_value(v); \ 152 V8_BASE_HASH_SPECIALIZE(
bool)
153 V8_BASE_HASH_SPECIALIZE(
signed char)
154 V8_BASE_HASH_SPECIALIZE(
unsigned char)
155 V8_BASE_HASH_SPECIALIZE(
short)
156 V8_BASE_HASH_SPECIALIZE(
unsigned short)
157 V8_BASE_HASH_SPECIALIZE(
int)
158 V8_BASE_HASH_SPECIALIZE(
unsigned int)
159 V8_BASE_HASH_SPECIALIZE(
long)
160 V8_BASE_HASH_SPECIALIZE(
unsigned long)
161 V8_BASE_HASH_SPECIALIZE(
long long)
162 V8_BASE_HASH_SPECIALIZE(
unsigned long long)
163 V8_BASE_HASH_SPECIALIZE(
float)
164 V8_BASE_HASH_SPECIALIZE(
double)
165 #undef V8_BASE_HASH_SPECIALIZE 167 template <
typename T>
169 V8_INLINE
size_t operator()(T*
const v)
const {
170 return ::v8::base::hash_value(v);
182 template <
typename T>
185 template <
typename T>
188 #define V8_BASE_BIT_SPECIALIZE_TRIVIAL(type) \ 190 struct bit_equal_to<type> : public std::equal_to<type> {}; \ 192 struct bit_hash<type> : public hash<type> {}; 193 V8_BASE_BIT_SPECIALIZE_TRIVIAL(
signed char)
194 V8_BASE_BIT_SPECIALIZE_TRIVIAL(
unsigned char)
195 V8_BASE_BIT_SPECIALIZE_TRIVIAL(
short)
196 V8_BASE_BIT_SPECIALIZE_TRIVIAL(
unsigned short)
197 V8_BASE_BIT_SPECIALIZE_TRIVIAL(
int)
198 V8_BASE_BIT_SPECIALIZE_TRIVIAL(
unsigned int)
199 V8_BASE_BIT_SPECIALIZE_TRIVIAL(
long)
200 V8_BASE_BIT_SPECIALIZE_TRIVIAL(
unsigned long)
201 V8_BASE_BIT_SPECIALIZE_TRIVIAL(
long long)
202 V8_BASE_BIT_SPECIALIZE_TRIVIAL(
unsigned long long)
203 #undef V8_BASE_BIT_SPECIALIZE_TRIVIAL 205 #define V8_BASE_BIT_SPECIALIZE_BIT_CAST(type, btype) \ 207 struct bit_equal_to<type> { \ 208 V8_INLINE bool operator()(type lhs, type rhs) const { \ 209 return bit_cast<btype>(lhs) == bit_cast<btype>(rhs); \ 213 struct bit_hash<type> { \ 214 V8_INLINE size_t operator()(type v) const { \ 216 return h(bit_cast<btype>(v)); \ 219 V8_BASE_BIT_SPECIALIZE_BIT_CAST(
float,
uint32_t)
220 V8_BASE_BIT_SPECIALIZE_BIT_CAST(
double, uint64_t)
221 #undef V8_BASE_BIT_SPECIALIZE_BIT_CAST 226 #endif // V8_BASE_FUNCTIONAL_H_