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

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

Issue 1839003002: WTF: De-specialize PtrHash<T>. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 8 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 /*
2 * Copyright (C) 2005, 2006, 2008 Apple Inc. All rights reserved. 2 * Copyright (C) 2005, 2006, 2008 Apple Inc. All rights reserved.
3 * 3 *
4 * This library is free software; you can redistribute it and/or 4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public 5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either 6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version. 7 * version 2 of the License, or (at your option) any later version.
8 * 8 *
9 * This library is distributed in the hope that it will be useful, 9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after
114 } 114 }
115 static bool equal(T a, T b) 115 static bool equal(T a, T b)
116 { 116 {
117 return bitwise_cast<Bits>(a) == bitwise_cast<Bits>(b); 117 return bitwise_cast<Bits>(a) == bitwise_cast<Bits>(b);
118 } 118 }
119 static const bool safeToCompareToEmptyOrDeleted = true; 119 static const bool safeToCompareToEmptyOrDeleted = true;
120 }; 120 };
121 121
122 // pointer identity hash function 122 // pointer identity hash function
123 123
124 template <typename T> struct PtrHash { 124 template <typename T>
125 static unsigned hash(T key) 125 struct PtrHash {
126 static unsigned hash(T* key)
126 { 127 {
127 #if COMPILER(MSVC) 128 #if COMPILER(MSVC)
128 #pragma warning(push) 129 #pragma warning(push)
129 #pragma warning(disable: 4244) // work around what seems to be a bug in MSVC's c onversion warnings 130 #pragma warning(disable: 4244) // work around what seems to be a bug in MSVC's c onversion warnings
130 #endif 131 #endif
131 return IntHash<uintptr_t>::hash(reinterpret_cast<uintptr_t>(key)); 132 return IntHash<uintptr_t>::hash(reinterpret_cast<uintptr_t>(key));
132 #if COMPILER(MSVC) 133 #if COMPILER(MSVC)
133 #pragma warning(pop) 134 #pragma warning(pop)
134 #endif 135 #endif
135 } 136 }
136 static bool equal(T a, T b) { return a == b; } 137 static bool equal(T* a, T* b) { return a == b; }
137 static bool equal(std::nullptr_t, T b) { return b == 0; } 138 static bool equal(std::nullptr_t, T* b) { return !b; }
138 static bool equal(T a, std::nullptr_t) { return a == 0; } 139 static bool equal(T* a, std::nullptr_t) { return !a; }
139 static const bool safeToCompareToEmptyOrDeleted = true; 140 static const bool safeToCompareToEmptyOrDeleted = true;
140 }; 141 };
141 template <typename P> struct PtrHash<RefPtr<P>> : PtrHash<P*> { 142
142 using PtrHash<P*>::hash; 143 template <typename T>
143 static unsigned hash(const RefPtr<P>& key) { return hash(key.get()); } 144 struct RefPtrHash : PtrHash<T> {
144 static unsigned hash(const PassRefPtr<P>& key) { return hash(key.get()); } 145 using PtrHash<T>::hash;
145 using PtrHash<P*>::equal; 146 static unsigned hash(const RefPtr<T>& key) { return hash(key.get()); }
146 static bool equal(const RefPtr<P>& a, const RefPtr<P>& b) { return a == b; } 147 static unsigned hash(const PassRefPtr<T>& key) { return hash(key.get()); }
147 static bool equal(P* a, const RefPtr<P>& b) { return a == b; } 148 using PtrHash<T>::equal;
148 static bool equal(const RefPtr<P>& a, P* b) { return a == b; } 149 static bool equal(const RefPtr<T>& a, const RefPtr<T>& b) { return a == b; }
149 static bool equal(const RefPtr<P>& a, const PassRefPtr<P>& b) { return a == b; } 150 static bool equal(T* a, const RefPtr<T>& b) { return a == b; }
151 static bool equal(const RefPtr<T>& a, T* b) { return a == b; }
152 static bool equal(const RefPtr<T>& a, const PassRefPtr<T>& b) { return a == b; }
150 }; 153 };
151 template <typename P> struct PtrHash<RawPtr<P>> : PtrHash<P*> { 154
152 using PtrHash<P*>::hash; 155 template <typename T>
153 static unsigned hash(const RawPtr<P>& key) { return hash(key.get()); } 156 struct RawPtrHash : PtrHash<T> {
154 using PtrHash<P*>::equal; 157 using PtrHash<T>::hash;
155 static bool equal(const RawPtr<P>& a, const RawPtr<P>& b) { return a == b; } 158 static unsigned hash(const RawPtr<T>& key) { return hash(key.get()); }
156 static bool equal(P* a, const RawPtr<P>& b) { return a == b; } 159 using PtrHash<T>::equal;
157 static bool equal(const RawPtr<P>& a, P* b) { return a == b; } 160 static bool equal(const RawPtr<T>& a, const RawPtr<T>& b) { return a == b; }
161 static bool equal(T* a, const RawPtr<T>& b) { return a == b; }
162 static bool equal(const RawPtr<T>& a, T* b) { return a == b; }
158 }; 163 };
159 template <typename P> struct PtrHash<OwnPtr<P>> : PtrHash<P*> {
160 using PtrHash<P*>::hash;
161 static unsigned hash(const OwnPtr<P>& key) { return hash(key.get()); }
162 static unsigned hash(const PassOwnPtr<P>& key) { return hash(key.get()); }
163 164
164 static bool equal(const OwnPtr<P>& a, const OwnPtr<P>& b) 165 template <typename T>
166 struct OwnPtrHash : PtrHash<T> {
167 using PtrHash<T>::hash;
168 static unsigned hash(const OwnPtr<T>& key) { return hash(key.get()); }
169 static unsigned hash(const PassOwnPtr<T>& key) { return hash(key.get()); }
170
171 static bool equal(const OwnPtr<T>& a, const OwnPtr<T>& b)
165 { 172 {
166 return a.get() == b.get(); 173 return a.get() == b.get();
167 } 174 }
168 static bool equal(const OwnPtr<P>& a, P* b) { return a == b; } 175 static bool equal(const OwnPtr<T>& a, T* b) { return a == b; }
169 static bool equal(const OwnPtr<P>& a, const PassOwnPtr<P>& b) 176 static bool equal(const OwnPtr<T>& a, const PassOwnPtr<T>& b)
170 { 177 {
171 return a.get() == b.get(); 178 return a.get() == b.get();
172 } 179 }
173 }; 180 };
174 181
175 // default hash function for each type 182 // default hash function for each type
176 183
177 template <typename T> struct DefaultHash; 184 template <typename T> struct DefaultHash;
178 185
179 template <typename T, typename U> struct PairHash { 186 template <typename T, typename U> struct PairHash {
(...skipping 26 matching lines...) Expand all
206 template <> struct DefaultHash<long long> { typedef IntHash<unsigned long long> Hash; }; 213 template <> struct DefaultHash<long long> { typedef IntHash<unsigned long long> Hash; };
207 template <> struct DefaultHash<unsigned long long> { typedef IntHash<unsigned lo ng long> Hash; }; 214 template <> struct DefaultHash<unsigned long long> { typedef IntHash<unsigned lo ng long> Hash; };
208 215
209 #if !COMPILER(MSVC) || defined(_NATIVE_WCHAR_T_DEFINED) 216 #if !COMPILER(MSVC) || defined(_NATIVE_WCHAR_T_DEFINED)
210 template <> struct DefaultHash<wchar_t> { typedef IntHash<wchar_t> Hash; }; 217 template <> struct DefaultHash<wchar_t> { typedef IntHash<wchar_t> Hash; };
211 #endif 218 #endif
212 219
213 template <> struct DefaultHash<float> { typedef FloatHash<float> Hash; }; 220 template <> struct DefaultHash<float> { typedef FloatHash<float> Hash; };
214 template <> struct DefaultHash<double> { typedef FloatHash<double> Hash; }; 221 template <> struct DefaultHash<double> { typedef FloatHash<double> Hash; };
215 222
216 // make PtrHash the default hash function for pointer types that don't specializ e 223 // Set default hash functions for pointer types.
217 224
218 template <typename P> struct DefaultHash<P*> { typedef PtrHash<P*> Hash; }; 225 template <typename T>
219 template <typename P> struct DefaultHash<RefPtr<P>> { typedef PtrHash<RefPtr<P>> Hash; }; 226 struct DefaultHash<T*> {
220 template <typename P> struct DefaultHash<RawPtr<P>> { typedef PtrHash<RawPtr<P>> Hash; }; 227 using Hash = PtrHash<T>;
221 template <typename P> struct DefaultHash<OwnPtr<P>> { typedef PtrHash<OwnPtr<P>> Hash; }; 228 };
Yuta Kitamura 2016/03/29 08:32:58 webkit-lint doesn't like one-liner class declarati
229 template <typename T>
230 struct DefaultHash<RefPtr<T>> {
231 using Hash = RefPtrHash<T>;
232 };
233 template <typename T>
234 struct DefaultHash<RawPtr<T>> {
235 using Hash = RawPtrHash<T>;
236 };
237 template <typename T>
238 struct DefaultHash<OwnPtr<T>> {
239 using Hash = OwnPtrHash<T>;
240 };
222 241
223 // make IntPairHash the default hash function for pairs of (at most) 32-bit inte gers. 242 // make IntPairHash the default hash function for pairs of (at most) 32-bit inte gers.
224 243
225 template <> struct DefaultHash<std::pair<short, short>> { typedef IntPairHash<sh ort, short> Hash; }; 244 template <> struct DefaultHash<std::pair<short, short>> { typedef IntPairHash<sh ort, short> Hash; };
226 template <> struct DefaultHash<std::pair<short, unsigned short>> { typedef IntPa irHash<short, unsigned short> Hash; }; 245 template <> struct DefaultHash<std::pair<short, unsigned short>> { typedef IntPa irHash<short, unsigned short> Hash; };
227 template <> struct DefaultHash<std::pair<short, int>> { typedef IntPairHash<shor t, int> Hash; }; 246 template <> struct DefaultHash<std::pair<short, int>> { typedef IntPairHash<shor t, int> Hash; };
228 template <> struct DefaultHash<std::pair<short, unsigned>> { typedef IntPairHash <short, unsigned> Hash; }; 247 template <> struct DefaultHash<std::pair<short, unsigned>> { typedef IntPairHash <short, unsigned> Hash; };
229 template <> struct DefaultHash<std::pair<unsigned short, short>> { typedef IntPa irHash<unsigned short, short> Hash; }; 248 template <> struct DefaultHash<std::pair<unsigned short, short>> { typedef IntPa irHash<unsigned short, short> Hash; };
230 template <> struct DefaultHash<std::pair<unsigned short, unsigned short>> { type def IntPairHash<unsigned short, unsigned short> Hash; }; 249 template <> struct DefaultHash<std::pair<unsigned short, unsigned short>> { type def IntPairHash<unsigned short, unsigned short> Hash; };
231 template <> struct DefaultHash<std::pair<unsigned short, int>> { typedef IntPair Hash<unsigned short, int> Hash; }; 250 template <> struct DefaultHash<std::pair<unsigned short, int>> { typedef IntPair Hash<unsigned short, int> Hash; };
(...skipping 11 matching lines...) Expand all
243 262
244 template <typename T, typename U> struct DefaultHash<std::pair<T, U>> { typedef PairHash<T, U> Hash; }; 263 template <typename T, typename U> struct DefaultHash<std::pair<T, U>> { typedef PairHash<T, U> Hash; };
245 264
246 } // namespace WTF 265 } // namespace WTF
247 266
248 using WTF::DefaultHash; 267 using WTF::DefaultHash;
249 using WTF::IntHash; 268 using WTF::IntHash;
250 using WTF::PtrHash; 269 using WTF::PtrHash;
251 270
252 #endif // WTF_HashFunctions_h 271 #endif // WTF_HashFunctions_h
OLDNEW
« no previous file with comments | « third_party/WebKit/Source/platform/heap/Handle.h ('k') | third_party/WebKit/Source/wtf/HashSetTest.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698