1 | /* |
2 | * Copyright (C) 2005, 2006, 2008 Apple Inc. All rights reserved. |
3 | * |
4 | * This library is free software; you can redistribute it and/or |
5 | * modify it under the terms of the GNU Library General Public |
6 | * License as published by the Free Software Foundation; either |
7 | * version 2 of the License, or (at your option) any later version. |
8 | * |
9 | * This library is distributed in the hope that it will be useful, |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
12 | * Library General Public License for more details. |
13 | * |
14 | * You should have received a copy of the GNU Library General Public License |
15 | * along with this library; see the file COPYING.LIB. If not, write to |
16 | * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
17 | * Boston, MA 02110-1301, USA. |
18 | * |
19 | */ |
20 | |
21 | #pragma once |
22 | |
23 | #include <stdint.h> |
24 | #include <tuple> |
25 | #include <wtf/GetPtr.h> |
26 | #include <wtf/RefPtr.h> |
27 | |
28 | namespace WTF { |
29 | |
30 | template<size_t size> struct IntTypes; |
31 | template<> struct IntTypes<1> { typedef int8_t SignedType; typedef uint8_t UnsignedType; }; |
32 | template<> struct IntTypes<2> { typedef int16_t SignedType; typedef uint16_t UnsignedType; }; |
33 | template<> struct IntTypes<4> { typedef int32_t SignedType; typedef uint32_t UnsignedType; }; |
34 | template<> struct IntTypes<8> { typedef int64_t SignedType; typedef uint64_t UnsignedType; }; |
35 | |
36 | // integer hash function |
37 | |
38 | // Thomas Wang's 32 Bit Mix Function: http://www.cris.com/~Ttwang/tech/inthash.htm |
39 | inline unsigned intHash(uint8_t key8) |
40 | { |
41 | unsigned key = key8; |
42 | key += ~(key << 15); |
43 | key ^= (key >> 10); |
44 | key += (key << 3); |
45 | key ^= (key >> 6); |
46 | key += ~(key << 11); |
47 | key ^= (key >> 16); |
48 | return key; |
49 | } |
50 | |
51 | // Thomas Wang's 32 Bit Mix Function: http://www.cris.com/~Ttwang/tech/inthash.htm |
52 | inline unsigned intHash(uint16_t key16) |
53 | { |
54 | unsigned key = key16; |
55 | key += ~(key << 15); |
56 | key ^= (key >> 10); |
57 | key += (key << 3); |
58 | key ^= (key >> 6); |
59 | key += ~(key << 11); |
60 | key ^= (key >> 16); |
61 | return key; |
62 | } |
63 | |
64 | // Thomas Wang's 32 Bit Mix Function: http://www.cris.com/~Ttwang/tech/inthash.htm |
65 | inline unsigned intHash(uint32_t key) |
66 | { |
67 | key += ~(key << 15); |
68 | key ^= (key >> 10); |
69 | key += (key << 3); |
70 | key ^= (key >> 6); |
71 | key += ~(key << 11); |
72 | key ^= (key >> 16); |
73 | return key; |
74 | } |
75 | |
76 | // Thomas Wang's 64 bit Mix Function: http://www.cris.com/~Ttwang/tech/inthash.htm |
77 | inline unsigned intHash(uint64_t key) |
78 | { |
79 | key += ~(key << 32); |
80 | key ^= (key >> 22); |
81 | key += ~(key << 13); |
82 | key ^= (key >> 8); |
83 | key += (key << 3); |
84 | key ^= (key >> 15); |
85 | key += ~(key << 27); |
86 | key ^= (key >> 31); |
87 | return static_cast<unsigned>(key); |
88 | } |
89 | |
90 | // Compound integer hash method: http://opendatastructures.org/versions/edition-0.1d/ods-java/node33.html#SECTION00832000000000000000 |
91 | inline unsigned pairIntHash(unsigned key1, unsigned key2) |
92 | { |
93 | unsigned shortRandom1 = 277951225; // A random 32-bit value. |
94 | unsigned shortRandom2 = 95187966; // A random 32-bit value. |
95 | uint64_t longRandom = 19248658165952622LL; // A random 64-bit value. |
96 | |
97 | uint64_t product = longRandom * (shortRandom1 * key1 + shortRandom2 * key2); |
98 | unsigned highBits = static_cast<unsigned>(product >> (sizeof(uint64_t) - sizeof(unsigned))); |
99 | return highBits; |
100 | } |
101 | |
102 | template<typename T> struct IntHash { |
103 | static unsigned hash(T key) { return intHash(static_cast<typename IntTypes<sizeof(T)>::UnsignedType>(key)); } |
104 | static bool equal(T a, T b) { return a == b; } |
105 | static const bool safeToCompareToEmptyOrDeleted = true; |
106 | }; |
107 | |
108 | template<typename T> struct FloatHash { |
109 | typedef typename IntTypes<sizeof(T)>::UnsignedType Bits; |
110 | static unsigned hash(T key) |
111 | { |
112 | return intHash(bitwise_cast<Bits>(key)); |
113 | } |
114 | static bool equal(T a, T b) |
115 | { |
116 | return bitwise_cast<Bits>(a) == bitwise_cast<Bits>(b); |
117 | } |
118 | static const bool safeToCompareToEmptyOrDeleted = true; |
119 | }; |
120 | |
121 | // pointer identity hash function |
122 | |
123 | template<typename T, bool isSmartPointer> |
124 | struct PtrHashBase; |
125 | |
126 | template <typename T> |
127 | struct PtrHashBase<T, false /* isSmartPtr */> { |
128 | typedef T PtrType; |
129 | |
130 | static unsigned hash(PtrType key) { return IntHash<uintptr_t>::hash(reinterpret_cast<uintptr_t>(key)); } |
131 | static bool equal(PtrType a, PtrType b) { return a == b; } |
132 | static const bool safeToCompareToEmptyOrDeleted = true; |
133 | }; |
134 | |
135 | template <typename T> |
136 | struct PtrHashBase<T, true /* isSmartPtr */> { |
137 | typedef typename GetPtrHelper<T>::PtrType PtrType; |
138 | |
139 | static unsigned hash(PtrType key) { return IntHash<uintptr_t>::hash(reinterpret_cast<uintptr_t>(key)); } |
140 | static bool equal(PtrType a, PtrType b) { return a == b; } |
141 | static const bool safeToCompareToEmptyOrDeleted = true; |
142 | |
143 | static unsigned hash(const T& key) { return hash(getPtr(key)); } |
144 | static bool equal(const T& a, const T& b) { return getPtr(a) == getPtr(b); } |
145 | static bool equal(PtrType a, const T& b) { return a == getPtr(b); } |
146 | static bool equal(const T& a, PtrType b) { return getPtr(a) == b; } |
147 | }; |
148 | |
149 | template<typename T> struct PtrHash : PtrHashBase<T, IsSmartPtr<T>::value> { |
150 | }; |
151 | |
152 | template<typename P> struct PtrHash<Ref<P>> : PtrHashBase<Ref<P>, IsSmartPtr<Ref<P>>::value> { |
153 | static const bool safeToCompareToEmptyOrDeleted = false; |
154 | }; |
155 | |
156 | // default hash function for each type |
157 | |
158 | template<typename T> struct DefaultHash; |
159 | |
160 | template<typename T, typename U> struct PairHash { |
161 | static unsigned hash(const std::pair<T, U>& p) |
162 | { |
163 | return pairIntHash(DefaultHash<T>::Hash::hash(p.first), DefaultHash<U>::Hash::hash(p.second)); |
164 | } |
165 | static bool equal(const std::pair<T, U>& a, const std::pair<T, U>& b) |
166 | { |
167 | return DefaultHash<T>::Hash::equal(a.first, b.first) && DefaultHash<U>::Hash::equal(a.second, b.second); |
168 | } |
169 | static const bool safeToCompareToEmptyOrDeleted = DefaultHash<T>::Hash::safeToCompareToEmptyOrDeleted && DefaultHash<U>::Hash::safeToCompareToEmptyOrDeleted; |
170 | }; |
171 | |
172 | template<typename T, typename U> struct IntPairHash { |
173 | static unsigned hash(const std::pair<T, U>& p) { return pairIntHash(p.first, p.second); } |
174 | static bool equal(const std::pair<T, U>& a, const std::pair<T, U>& b) { return PairHash<T, T>::equal(a, b); } |
175 | static const bool safeToCompareToEmptyOrDeleted = PairHash<T, U>::safeToCompareToEmptyOrDeleted; |
176 | }; |
177 | |
178 | template<typename... Types> |
179 | struct TupleHash { |
180 | template<size_t I = 0> |
181 | static typename std::enable_if<I < sizeof...(Types) - 1, unsigned>::type hash(const std::tuple<Types...>& t) |
182 | { |
183 | using IthTupleElementType = typename std::tuple_element<I, typename std::tuple<Types...>>::type; |
184 | return pairIntHash(DefaultHash<IthTupleElementType>::Hash::hash(std::get<I>(t)), hash<I + 1>(t)); |
185 | } |
186 | |
187 | template<size_t I = 0> |
188 | static typename std::enable_if<I == sizeof...(Types) - 1, unsigned>::type hash(const std::tuple<Types...>& t) |
189 | { |
190 | using IthTupleElementType = typename std::tuple_element<I, typename std::tuple<Types...>>::type; |
191 | return DefaultHash<IthTupleElementType>::Hash::hash(std::get<I>(t)); |
192 | } |
193 | |
194 | template<size_t I = 0> |
195 | static typename std::enable_if<I < sizeof...(Types) - 1, bool>::type equal(const std::tuple<Types...>& a, const std::tuple<Types...>& b) |
196 | { |
197 | using IthTupleElementType = typename std::tuple_element<I, typename std::tuple<Types...>>::type; |
198 | return DefaultHash<IthTupleElementType>::Hash::equal(std::get<I>(a), std::get<I>(b)) && equal<I + 1>(a, b); |
199 | } |
200 | |
201 | template<size_t I = 0> |
202 | static typename std::enable_if<I == sizeof...(Types) - 1, bool>::type equal(const std::tuple<Types...>& a, const std::tuple<Types...>& b) |
203 | { |
204 | using IthTupleElementType = typename std::tuple_element<I, typename std::tuple<Types...>>::type; |
205 | return DefaultHash<IthTupleElementType>::Hash::equal(std::get<I>(a), std::get<I>(b)); |
206 | } |
207 | |
208 | // We should use safeToCompareToEmptyOrDeleted = DefaultHash<Types>::Hash::safeToCompareToEmptyOrDeleted &&... whenever |
209 | // we switch to C++17. We can't do anything better here right now because GCC can't do C++. |
210 | template<typename BoolType> |
211 | static constexpr bool allTrue(BoolType value) { return value; } |
212 | template<typename BoolType, typename... BoolTypes> |
213 | static constexpr bool allTrue(BoolType value, BoolTypes... values) { return value && allTrue(values...); } |
214 | static const bool safeToCompareToEmptyOrDeleted = allTrue(DefaultHash<Types>::Hash::safeToCompareToEmptyOrDeleted...); |
215 | }; |
216 | |
217 | // make IntHash the default hash function for many integer types |
218 | |
219 | template<> struct DefaultHash<bool> { typedef IntHash<uint8_t> Hash; }; |
220 | template<> struct DefaultHash<uint8_t> { typedef IntHash<uint8_t> Hash; }; |
221 | template<> struct DefaultHash<short> { typedef IntHash<unsigned> Hash; }; |
222 | template<> struct DefaultHash<unsigned short> { typedef IntHash<unsigned> Hash; }; |
223 | template<> struct DefaultHash<int> { typedef IntHash<unsigned> Hash; }; |
224 | template<> struct DefaultHash<unsigned> { typedef IntHash<unsigned> Hash; }; |
225 | template<> struct DefaultHash<long> { typedef IntHash<unsigned long> Hash; }; |
226 | template<> struct DefaultHash<unsigned long> { typedef IntHash<unsigned long> Hash; }; |
227 | template<> struct DefaultHash<long long> { typedef IntHash<unsigned long long> Hash; }; |
228 | template<> struct DefaultHash<unsigned long long> { typedef IntHash<unsigned long long> Hash; }; |
229 | |
230 | #if defined(_NATIVE_WCHAR_T_DEFINED) |
231 | template<> struct DefaultHash<wchar_t> { typedef IntHash<wchar_t> Hash; }; |
232 | #endif |
233 | |
234 | template<> struct DefaultHash<float> { typedef FloatHash<float> Hash; }; |
235 | template<> struct DefaultHash<double> { typedef FloatHash<double> Hash; }; |
236 | |
237 | // make PtrHash the default hash function for pointer types that don't specialize |
238 | |
239 | template<typename P> struct DefaultHash<P*> { typedef PtrHash<P*> Hash; }; |
240 | template<typename P> struct DefaultHash<RefPtr<P>> { typedef PtrHash<RefPtr<P>> Hash; }; |
241 | template<typename P> struct DefaultHash<Ref<P>> { typedef PtrHash<Ref<P>> Hash; }; |
242 | |
243 | template<typename P, typename Deleter> struct DefaultHash<std::unique_ptr<P, Deleter>> { typedef PtrHash<std::unique_ptr<P, Deleter>> Hash; }; |
244 | |
245 | #ifdef __OBJC__ |
246 | template<> struct DefaultHash<__unsafe_unretained id> { using Hash = PtrHash<__unsafe_unretained id>; }; |
247 | #endif |
248 | |
249 | // make IntPairHash the default hash function for pairs of (at most) 32-bit integers. |
250 | |
251 | template<> struct DefaultHash<std::pair<short, short>> { typedef IntPairHash<short, short> Hash; }; |
252 | template<> struct DefaultHash<std::pair<short, unsigned short>> { typedef IntPairHash<short, unsigned short> Hash; }; |
253 | template<> struct DefaultHash<std::pair<short, int>> { typedef IntPairHash<short, int> Hash; }; |
254 | template<> struct DefaultHash<std::pair<short, unsigned>> { typedef IntPairHash<short, unsigned> Hash; }; |
255 | template<> struct DefaultHash<std::pair<unsigned short, short>> { typedef IntPairHash<unsigned short, short> Hash; }; |
256 | template<> struct DefaultHash<std::pair<unsigned short, unsigned short>> { typedef IntPairHash<unsigned short, unsigned short> Hash; }; |
257 | template<> struct DefaultHash<std::pair<unsigned short, int>> { typedef IntPairHash<unsigned short, int> Hash; }; |
258 | template<> struct DefaultHash<std::pair<unsigned short, unsigned>> { typedef IntPairHash<unsigned short, unsigned> Hash; }; |
259 | template<> struct DefaultHash<std::pair<int, short>> { typedef IntPairHash<int, short> Hash; }; |
260 | template<> struct DefaultHash<std::pair<int, unsigned short>> { typedef IntPairHash<int, unsigned short> Hash; }; |
261 | template<> struct DefaultHash<std::pair<int, int>> { typedef IntPairHash<int, int> Hash; }; |
262 | template<> struct DefaultHash<std::pair<int, unsigned>> { typedef IntPairHash<unsigned, unsigned> Hash; }; |
263 | template<> struct DefaultHash<std::pair<unsigned, short>> { typedef IntPairHash<unsigned, short> Hash; }; |
264 | template<> struct DefaultHash<std::pair<unsigned, unsigned short>> { typedef IntPairHash<unsigned, unsigned short> Hash; }; |
265 | template<> struct DefaultHash<std::pair<unsigned, int>> { typedef IntPairHash<unsigned, int> Hash; }; |
266 | template<> struct DefaultHash<std::pair<unsigned, unsigned>> { typedef IntPairHash<unsigned, unsigned> Hash; }; |
267 | |
268 | // make PairHash the default hash function for pairs of arbitrary values. |
269 | |
270 | template<typename T, typename U> struct DefaultHash<std::pair<T, U>> { typedef PairHash<T, U> Hash; }; |
271 | template<typename... Types> struct DefaultHash<std::tuple<Types...>> { typedef TupleHash<Types...> Hash; }; |
272 | |
273 | } // namespace WTF |
274 | |
275 | using WTF::DefaultHash; |
276 | using WTF::IntHash; |
277 | using WTF::PtrHash; |
278 | |