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 <stddef.h> | 8 #include <stddef.h> |
9 #include <stdint.h> | 9 #include <stdint.h> |
10 | 10 |
11 #include <cstddef> | 11 #include <cstddef> |
12 #include <cstring> | 12 #include <cstring> |
13 #include <functional> | 13 #include <functional> |
14 #include <utility> | 14 #include <utility> |
15 | 15 |
16 #include "src/base/base-export.h" | |
17 #include "src/base/macros.h" | 16 #include "src/base/macros.h" |
18 | 17 |
19 namespace v8 { | 18 namespace v8 { |
20 namespace base { | 19 namespace base { |
21 | 20 |
22 // base::hash is an implementation of the hash function object specified by | 21 // base::hash is an implementation of the hash function object specified by |
23 // C++11. It was designed to be compatible with std::hash (in C++11) and | 22 // C++11. It was designed to be compatible with std::hash (in C++11) and |
24 // boost:hash (which in turn is based on the hash function object specified by | 23 // boost:hash (which in turn is based on the hash function object specified by |
25 // the Draft Technical Report on C++ Library Extensions (TR1)). | 24 // the Draft Technical Report on C++ Library Extensions (TR1)). |
26 // | 25 // |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
61 // Based on the "Hashing User-Defined Types in C++1y" proposal from Jeffrey | 60 // Based on the "Hashing User-Defined Types in C++1y" proposal from Jeffrey |
62 // Yasskin and Chandler Carruth, see | 61 // Yasskin and Chandler Carruth, see |
63 // http://www.open-std.org/Jtc1/sc22/wg21/docs/papers/2012/n3333.html. | 62 // http://www.open-std.org/Jtc1/sc22/wg21/docs/papers/2012/n3333.html. |
64 | 63 |
65 template <typename> | 64 template <typename> |
66 struct hash; | 65 struct hash; |
67 | 66 |
68 | 67 |
69 V8_INLINE size_t hash_combine() { return 0u; } | 68 V8_INLINE size_t hash_combine() { return 0u; } |
70 V8_INLINE size_t hash_combine(size_t seed) { return seed; } | 69 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); | 70 size_t hash_combine(size_t seed, size_t value); |
72 template <typename T, typename... Ts> | 71 template <typename T, typename... Ts> |
73 V8_INLINE size_t hash_combine(T const& v, Ts const&... vs) { | 72 V8_INLINE size_t hash_combine(T const& v, Ts const&... vs) { |
74 return hash_combine(hash_combine(vs...), hash<T>()(v)); | 73 return hash_combine(hash_combine(vs...), hash<T>()(v)); |
75 } | 74 } |
76 | 75 |
77 | 76 |
78 template <typename Iterator> | 77 template <typename Iterator> |
79 V8_INLINE size_t hash_range(Iterator first, Iterator last) { | 78 V8_INLINE size_t hash_range(Iterator first, Iterator last) { |
80 size_t seed = 0; | 79 size_t seed = 0; |
81 for (; first != last; ++first) { | 80 for (; first != last; ++first) { |
82 seed = hash_combine(seed, *first); | 81 seed = hash_combine(seed, *first); |
83 } | 82 } |
84 return seed; | 83 return seed; |
85 } | 84 } |
86 | 85 |
87 | 86 |
88 #define V8_BASE_HASH_VALUE_TRIVIAL(type) \ | 87 #define V8_BASE_HASH_VALUE_TRIVIAL(type) \ |
89 V8_INLINE size_t hash_value(type v) { return static_cast<size_t>(v); } | 88 V8_INLINE size_t hash_value(type v) { return static_cast<size_t>(v); } |
90 V8_BASE_HASH_VALUE_TRIVIAL(bool) | 89 V8_BASE_HASH_VALUE_TRIVIAL(bool) |
91 V8_BASE_HASH_VALUE_TRIVIAL(unsigned char) | 90 V8_BASE_HASH_VALUE_TRIVIAL(unsigned char) |
92 V8_BASE_HASH_VALUE_TRIVIAL(unsigned short) // NOLINT(runtime/int) | 91 V8_BASE_HASH_VALUE_TRIVIAL(unsigned short) // NOLINT(runtime/int) |
93 #undef V8_BASE_HASH_VALUE_TRIVIAL | 92 #undef V8_BASE_HASH_VALUE_TRIVIAL |
94 | 93 |
95 V8_BASE_EXPORT size_t hash_value(unsigned int); | 94 size_t hash_value(unsigned int); |
96 V8_BASE_EXPORT size_t hash_value(unsigned long); // NOLINT(runtime/int) | 95 size_t hash_value(unsigned long); // NOLINT(runtime/int) |
97 V8_BASE_EXPORT size_t hash_value(unsigned long long); // NOLINT(runtime/int) | 96 size_t hash_value(unsigned long long); // NOLINT(runtime/int) |
98 | 97 |
99 #define V8_BASE_HASH_VALUE_SIGNED(type) \ | 98 #define V8_BASE_HASH_VALUE_SIGNED(type) \ |
100 V8_INLINE size_t hash_value(signed type v) { \ | 99 V8_INLINE size_t hash_value(signed type v) { \ |
101 return hash_value(bit_cast<unsigned type>(v)); \ | 100 return hash_value(bit_cast<unsigned type>(v)); \ |
102 } | 101 } |
103 V8_BASE_HASH_VALUE_SIGNED(char) | 102 V8_BASE_HASH_VALUE_SIGNED(char) |
104 V8_BASE_HASH_VALUE_SIGNED(short) // NOLINT(runtime/int) | 103 V8_BASE_HASH_VALUE_SIGNED(short) // NOLINT(runtime/int) |
105 V8_BASE_HASH_VALUE_SIGNED(int) // NOLINT(runtime/int) | 104 V8_BASE_HASH_VALUE_SIGNED(int) // NOLINT(runtime/int) |
106 V8_BASE_HASH_VALUE_SIGNED(long) // NOLINT(runtime/int) | 105 V8_BASE_HASH_VALUE_SIGNED(long) // NOLINT(runtime/int) |
107 V8_BASE_HASH_VALUE_SIGNED(long long) // NOLINT(runtime/int) | 106 V8_BASE_HASH_VALUE_SIGNED(long long) // NOLINT(runtime/int) |
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
219 } \ | 218 } \ |
220 }; | 219 }; |
221 V8_BASE_BIT_SPECIALIZE_BIT_CAST(float, uint32_t) | 220 V8_BASE_BIT_SPECIALIZE_BIT_CAST(float, uint32_t) |
222 V8_BASE_BIT_SPECIALIZE_BIT_CAST(double, uint64_t) | 221 V8_BASE_BIT_SPECIALIZE_BIT_CAST(double, uint64_t) |
223 #undef V8_BASE_BIT_SPECIALIZE_BIT_CAST | 222 #undef V8_BASE_BIT_SPECIALIZE_BIT_CAST |
224 | 223 |
225 } // namespace base | 224 } // namespace base |
226 } // namespace v8 | 225 } // namespace v8 |
227 | 226 |
228 #endif // V8_BASE_FUNCTIONAL_H_ | 227 #endif // V8_BASE_FUNCTIONAL_H_ |
OLD | NEW |