OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium 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 | 5 |
6 // | 6 // |
7 // Deal with the differences between Microsoft and GNU implemenations | 7 // Deal with the differences between Microsoft and GNU implemenations |
8 // of hash_map. Allows all platforms to use |base::hash_map| and | 8 // of hash_map. Allows all platforms to use |base::hash_map| and |
9 // |base::hash_set|. | 9 // |base::hash_set|. |
10 // eg: | 10 // eg: |
(...skipping 229 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
240 | 240 |
241 uint64 odd_random = 1578233944LL << 32 | 194370989LL; | 241 uint64 odd_random = 1578233944LL << 32 | 194370989LL; |
242 uint32 shift_random = 20591U << 16; | 242 uint32 shift_random = 20591U << 16; |
243 | 243 |
244 hash64 = hash64 * odd_random + shift_random; | 244 hash64 = hash64 * odd_random + shift_random; |
245 std::size_t high_bits = static_cast<std::size_t>( | 245 std::size_t high_bits = static_cast<std::size_t>( |
246 hash64 >> (8 * (sizeof(uint64) - sizeof(std::size_t)))); | 246 hash64 >> (8 * (sizeof(uint64) - sizeof(std::size_t)))); |
247 return high_bits; | 247 return high_bits; |
248 } | 248 } |
249 | 249 |
250 #define DEFINE_32BIT_PAIR_HASH(Type1, Type2) \ | 250 template<typename T1, typename T2> |
251 inline std::size_t HashPair(Type1 value1, Type2 value2) { \ | 251 inline std::size_t HashPair(T1 value1, T2 value2) { |
252 return HashInts32(value1, value2); \ | 252 // This condition is expected to be compile-time evaluated and optimised away |
| 253 // in release builds. |
| 254 if (sizeof(T1) > sizeof(uint32_t) || (sizeof(T2) > sizeof(uint32_t))) |
| 255 return HashInts64(value1, value2); |
| 256 |
| 257 return HashInts32(value1, value2); |
253 } | 258 } |
254 | 259 |
255 DEFINE_32BIT_PAIR_HASH(int16, int16); | |
256 DEFINE_32BIT_PAIR_HASH(int16, uint16); | |
257 DEFINE_32BIT_PAIR_HASH(int16, int32); | |
258 DEFINE_32BIT_PAIR_HASH(int16, uint32); | |
259 DEFINE_32BIT_PAIR_HASH(uint16, int16); | |
260 DEFINE_32BIT_PAIR_HASH(uint16, uint16); | |
261 DEFINE_32BIT_PAIR_HASH(uint16, int32); | |
262 DEFINE_32BIT_PAIR_HASH(uint16, uint32); | |
263 DEFINE_32BIT_PAIR_HASH(int32, int16); | |
264 DEFINE_32BIT_PAIR_HASH(int32, uint16); | |
265 DEFINE_32BIT_PAIR_HASH(int32, int32); | |
266 DEFINE_32BIT_PAIR_HASH(int32, uint32); | |
267 DEFINE_32BIT_PAIR_HASH(uint32, int16); | |
268 DEFINE_32BIT_PAIR_HASH(uint32, uint16); | |
269 DEFINE_32BIT_PAIR_HASH(uint32, int32); | |
270 DEFINE_32BIT_PAIR_HASH(uint32, uint32); | |
271 | |
272 #undef DEFINE_32BIT_PAIR_HASH | |
273 | |
274 #define DEFINE_64BIT_PAIR_HASH(Type1, Type2) \ | |
275 inline std::size_t HashPair(Type1 value1, Type2 value2) { \ | |
276 return HashInts64(value1, value2); \ | |
277 } | |
278 | |
279 DEFINE_64BIT_PAIR_HASH(int16, int64); | |
280 DEFINE_64BIT_PAIR_HASH(int16, uint64); | |
281 DEFINE_64BIT_PAIR_HASH(uint16, int64); | |
282 DEFINE_64BIT_PAIR_HASH(uint16, uint64); | |
283 DEFINE_64BIT_PAIR_HASH(int32, int64); | |
284 DEFINE_64BIT_PAIR_HASH(int32, uint64); | |
285 DEFINE_64BIT_PAIR_HASH(uint32, int64); | |
286 DEFINE_64BIT_PAIR_HASH(uint32, uint64); | |
287 DEFINE_64BIT_PAIR_HASH(int64, int16); | |
288 DEFINE_64BIT_PAIR_HASH(int64, uint16); | |
289 DEFINE_64BIT_PAIR_HASH(int64, int32); | |
290 DEFINE_64BIT_PAIR_HASH(int64, uint32); | |
291 DEFINE_64BIT_PAIR_HASH(int64, int64); | |
292 DEFINE_64BIT_PAIR_HASH(int64, uint64); | |
293 DEFINE_64BIT_PAIR_HASH(uint64, int16); | |
294 DEFINE_64BIT_PAIR_HASH(uint64, uint16); | |
295 DEFINE_64BIT_PAIR_HASH(uint64, int32); | |
296 DEFINE_64BIT_PAIR_HASH(uint64, uint32); | |
297 DEFINE_64BIT_PAIR_HASH(uint64, int64); | |
298 DEFINE_64BIT_PAIR_HASH(uint64, uint64); | |
299 | |
300 #undef DEFINE_64BIT_PAIR_HASH | |
301 } // namespace base | 260 } // namespace base |
302 | 261 |
303 namespace BASE_HASH_NAMESPACE { | 262 namespace BASE_HASH_NAMESPACE { |
304 | 263 |
305 // Implement methods for hashing a pair of integers, so they can be used as | 264 // Implement methods for hashing a pair of integers, so they can be used as |
306 // keys in STL containers. | 265 // keys in STL containers. |
307 | 266 |
308 template<typename Type1, typename Type2> | 267 template<typename Type1, typename Type2> |
309 struct hash<std::pair<Type1, Type2> > { | 268 struct hash<std::pair<Type1, Type2> > { |
310 std::size_t operator()(std::pair<Type1, Type2> value) const { | 269 std::size_t operator()(std::pair<Type1, Type2> value) const { |
311 return base::HashPair(value.first, value.second); | 270 return base::HashPair(value.first, value.second); |
312 } | 271 } |
313 }; | 272 }; |
314 | 273 |
315 } // namespace BASE_HASH_NAMESPACE | 274 } // namespace BASE_HASH_NAMESPACE |
316 | 275 |
317 #undef DEFINE_PAIR_HASH_FUNCTION_START | 276 #undef DEFINE_PAIR_HASH_FUNCTION_START |
318 #undef DEFINE_PAIR_HASH_FUNCTION_END | 277 #undef DEFINE_PAIR_HASH_FUNCTION_END |
319 | 278 |
320 #endif // BASE_CONTAINERS_HASH_TABLES_H_ | 279 #endif // BASE_CONTAINERS_HASH_TABLES_H_ |
OLD | NEW |