Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(144)

Side by Side Diff: third_party/WebKit/Source/wtf/HashFunctions.h

Issue 2769603002: Move files in wtf/ to platform/wtf/ (Part 8). (Closed)
Patch Set: Rebase. Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 // Copyright 2017 The Chromium Authors. All rights reserved.
2 * Copyright (C) 2005, 2006, 2008 Apple Inc. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be
3 * 3 // found in the LICENSE file.
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 4
21 #ifndef WTF_HashFunctions_h 5 #include "platform/wtf/HashFunctions.h"
22 #define WTF_HashFunctions_h
23 6
24 #include "wtf/RefPtr.h" 7 // The contents of this header was moved to platform/wtf as part of
25 #include "wtf/StdLibExtras.h" 8 // WTF migration project. See the following post for details:
26 #include <memory> 9 // https://groups.google.com/a/chromium.org/d/msg/blink-dev/tLdAZCTlcAA/bYXVT8gY CAAJ
27 #include <stdint.h>
28 #include <type_traits>
29
30 namespace WTF {
31
32 template <size_t size>
33 struct IntTypes;
34 template <>
35 struct IntTypes<1> {
36 typedef int8_t SignedType;
37 typedef uint8_t UnsignedType;
38 };
39 template <>
40 struct IntTypes<2> {
41 typedef int16_t SignedType;
42 typedef uint16_t UnsignedType;
43 };
44 template <>
45 struct IntTypes<4> {
46 typedef int32_t SignedType;
47 typedef uint32_t UnsignedType;
48 };
49 template <>
50 struct IntTypes<8> {
51 typedef int64_t SignedType;
52 typedef uint64_t UnsignedType;
53 };
54
55 // integer hash function
56
57 // Thomas Wang's 32 Bit Mix Function:
58 // http://www.cris.com/~Ttwang/tech/inthash.htm
59 inline unsigned hashInt(uint8_t key8) {
60 unsigned key = key8;
61 key += ~(key << 15);
62 key ^= (key >> 10);
63 key += (key << 3);
64 key ^= (key >> 6);
65 key += ~(key << 11);
66 key ^= (key >> 16);
67 return key;
68 }
69
70 // Thomas Wang's 32 Bit Mix Function:
71 // http://www.cris.com/~Ttwang/tech/inthash.htm
72 inline unsigned hashInt(uint16_t key16) {
73 unsigned key = key16;
74 key += ~(key << 15);
75 key ^= (key >> 10);
76 key += (key << 3);
77 key ^= (key >> 6);
78 key += ~(key << 11);
79 key ^= (key >> 16);
80 return key;
81 }
82
83 // Thomas Wang's 32 Bit Mix Function:
84 // http://www.cris.com/~Ttwang/tech/inthash.htm
85 inline unsigned hashInt(uint32_t key) {
86 key += ~(key << 15);
87 key ^= (key >> 10);
88 key += (key << 3);
89 key ^= (key >> 6);
90 key += ~(key << 11);
91 key ^= (key >> 16);
92 return key;
93 }
94
95 // Thomas Wang's 64 bit Mix Function:
96 // http://www.cris.com/~Ttwang/tech/inthash.htm
97 inline unsigned hashInt(uint64_t key) {
98 key += ~(key << 32);
99 key ^= (key >> 22);
100 key += ~(key << 13);
101 key ^= (key >> 8);
102 key += (key << 3);
103 key ^= (key >> 15);
104 key += ~(key << 27);
105 key ^= (key >> 31);
106 return static_cast<unsigned>(key);
107 }
108
109 // Compound integer hash method:
110 // http://opendatastructures.org/versions/edition-0.1d/ods-java/node33.html#SECT ION00832000000000000000
111 inline unsigned hashInts(unsigned key1, unsigned key2) {
112 unsigned shortRandom1 = 277951225; // A random 32-bit value.
113 unsigned shortRandom2 = 95187966; // A random 32-bit value.
114 uint64_t longRandom = 19248658165952623LL; // A random, odd 64-bit value.
115
116 uint64_t product =
117 longRandom * shortRandom1 * key1 + longRandom * shortRandom2 * key2;
118 unsigned highBits = static_cast<unsigned>(
119 product >> (8 * (sizeof(uint64_t) - sizeof(unsigned))));
120 return highBits;
121 }
122
123 template <typename T>
124 struct IntHash {
125 static unsigned hash(T key) {
126 return hashInt(
127 static_cast<typename IntTypes<sizeof(T)>::UnsignedType>(key));
128 }
129 static bool equal(T a, T b) { return a == b; }
130 static const bool safeToCompareToEmptyOrDeleted = true;
131 };
132
133 template <typename T>
134 struct FloatHash {
135 typedef typename IntTypes<sizeof(T)>::UnsignedType Bits;
136 static unsigned hash(T key) { return hashInt(bitwiseCast<Bits>(key)); }
137 static bool equal(T a, T b) {
138 return bitwiseCast<Bits>(a) == bitwiseCast<Bits>(b);
139 }
140 static const bool safeToCompareToEmptyOrDeleted = true;
141 };
142
143 // pointer identity hash function
144
145 template <typename T>
146 struct PtrHash {
147 static unsigned hash(T* key) {
148 #if COMPILER(MSVC)
149 #pragma warning(push)
150 // work around what seems to be a bug in MSVC's conversion warnings
151 #pragma warning(disable : 4244)
152 #endif
153 return IntHash<uintptr_t>::hash(reinterpret_cast<uintptr_t>(key));
154 #if COMPILER(MSVC)
155 #pragma warning(pop)
156 #endif
157 }
158 static bool equal(T* a, T* b) { return a == b; }
159 static bool equal(std::nullptr_t, T* b) { return !b; }
160 static bool equal(T* a, std::nullptr_t) { return !a; }
161 static const bool safeToCompareToEmptyOrDeleted = true;
162 };
163
164 template <typename T>
165 struct RefPtrHash : PtrHash<T> {
166 using PtrHash<T>::hash;
167 static unsigned hash(const RefPtr<T>& key) { return hash(key.get()); }
168 static unsigned hash(const PassRefPtr<T>& key) { return hash(key.get()); }
169 using PtrHash<T>::equal;
170 static bool equal(const RefPtr<T>& a, const RefPtr<T>& b) { return a == b; }
171 static bool equal(T* a, const RefPtr<T>& b) { return a == b; }
172 static bool equal(const RefPtr<T>& a, T* b) { return a == b; }
173 static bool equal(const RefPtr<T>& a, const PassRefPtr<T>& b) {
174 return a == b;
175 }
176 };
177
178 template <typename T>
179 struct UniquePtrHash : PtrHash<T> {
180 using PtrHash<T>::hash;
181 static unsigned hash(const std::unique_ptr<T>& key) {
182 return hash(key.get());
183 }
184 static bool equal(const std::unique_ptr<T>& a, const std::unique_ptr<T>& b) {
185 return a == b;
186 }
187 static bool equal(const std::unique_ptr<T>& a, const T* b) {
188 return a.get() == b;
189 }
190 static bool equal(const T* a, const std::unique_ptr<T>& b) {
191 return a == b.get();
192 }
193 };
194
195 // Default hash function for each type.
196 template <typename T>
197 struct DefaultHash;
198
199 // Actual implementation of DefaultHash.
200 //
201 // The case of |isIntegral| == false is not implemented. If you see a compile
202 // error saying DefaultHashImpl<T, false> is not defined, that's because the
203 // default hash functions for T are not defined. You need to implement them
204 // yourself.
205 template <typename T, bool isIntegral>
206 struct DefaultHashImpl;
207
208 template <typename T>
209 struct DefaultHashImpl<T, true> {
210 using Hash = IntHash<typename std::make_unsigned<T>::type>;
211 };
212
213 // Canonical implementation of DefaultHash.
214 template <typename T>
215 struct DefaultHash : DefaultHashImpl<T, std::is_integral<T>::value> {};
216
217 // Specializations of DefaultHash follow.
218 template <>
219 struct DefaultHash<float> {
220 using Hash = FloatHash<float>;
221 };
222 template <>
223 struct DefaultHash<double> {
224 using Hash = FloatHash<double>;
225 };
226
227 // Specializations for pointer types.
228 template <typename T>
229 struct DefaultHash<T*> {
230 using Hash = PtrHash<T>;
231 };
232 template <typename T>
233 struct DefaultHash<RefPtr<T>> {
234 using Hash = RefPtrHash<T>;
235 };
236 template <typename T>
237 struct DefaultHash<std::unique_ptr<T>> {
238 using Hash = UniquePtrHash<T>;
239 };
240
241 // Specializations for pairs.
242
243 // Generic case (T or U is non-integral):
244 template <typename T, typename U, bool areBothIntegral>
245 struct PairHashImpl {
246 static unsigned hash(const std::pair<T, U>& p) {
247 return hashInts(DefaultHash<T>::Hash::hash(p.first),
248 DefaultHash<U>::Hash::hash(p.second));
249 }
250 static bool equal(const std::pair<T, U>& a, const std::pair<T, U>& b) {
251 return DefaultHash<T>::Hash::equal(a.first, b.first) &&
252 DefaultHash<U>::Hash::equal(a.second, b.second);
253 }
254 static const bool safeToCompareToEmptyOrDeleted =
255 DefaultHash<T>::Hash::safeToCompareToEmptyOrDeleted &&
256 DefaultHash<U>::Hash::safeToCompareToEmptyOrDeleted;
257 };
258
259 // Special version for pairs of integrals:
260 template <typename T, typename U>
261 struct PairHashImpl<T, U, true> {
262 static unsigned hash(const std::pair<T, U>& p) {
263 return hashInts(p.first, p.second);
264 }
265 static bool equal(const std::pair<T, U>& a, const std::pair<T, U>& b) {
266 return PairHashImpl<T, U, false>::equal(
267 a, b); // Refer to the generic version.
268 }
269 static const bool safeToCompareToEmptyOrDeleted =
270 PairHashImpl<T, U, false>::safeToCompareToEmptyOrDeleted;
271 };
272
273 // Combined version:
274 template <typename T, typename U>
275 struct PairHash
276 : PairHashImpl<T,
277 U,
278 std::is_integral<T>::value && std::is_integral<U>::value> {};
279
280 template <typename T, typename U>
281 struct DefaultHash<std::pair<T, U>> {
282 using Hash = PairHash<T, U>;
283 };
284
285 } // namespace WTF
286
287 using WTF::DefaultHash;
288 using WTF::IntHash;
289 using WTF::PtrHash;
290
291 #endif // WTF_HashFunctions_h
OLDNEW
« no previous file with comments | « third_party/WebKit/Source/wtf/HashCountedSet.h ('k') | third_party/WebKit/Source/wtf/HashIterators.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698