| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 // Defines methods for hashing a pair of integers. | 5 // Defines methods for hashing a pair of integers. |
| 6 | 6 |
| 7 #ifndef CC_BASE_HASH_PAIR_H_ | 7 #ifndef CC_BASE_HASH_PAIR_H_ |
| 8 #define CC_BASE_HASH_PAIR_H_ | 8 #define CC_BASE_HASH_PAIR_H_ |
| 9 | 9 |
| 10 #include <utility> |
| 11 |
| 12 #include "base/basictypes.h" |
| 10 #include "base/hash_tables.h" | 13 #include "base/hash_tables.h" |
| 11 | 14 |
| 12 #if defined(COMPILER_MSVC) | 15 #if defined(COMPILER_MSVC) |
| 13 | 16 |
| 14 #define DEFINE_PAIR_HASH_FUNCTION_START(type1, type2) \ | 17 #define DEFINE_PAIR_HASH_FUNCTION_START(type1, type2) \ |
| 15 template<> \ | 18 template<> \ |
| 16 inline std::size_t hash_value<std::pair<type1, type2> >( \ | 19 inline std::size_t hash_value<std::pair<type1, type2> >( \ |
| 17 const std::pair<type1, type2>& value) | 20 const std::pair<type1, type2>& value) |
| 18 | 21 |
| 19 #define DEFINE_PAIR_HASH_FUNCTION_END() | 22 #define DEFINE_PAIR_HASH_FUNCTION_END() |
| 20 | 23 |
| 21 #elif defined(COMPILER_GCC) | 24 #elif defined(COMPILER_GCC) |
| 22 | 25 |
| 23 #define DEFINE_PAIR_HASH_FUNCTION_START(type1, type2) \ | 26 #define DEFINE_PAIR_HASH_FUNCTION_START(type1, type2) \ |
| 24 template<> \ | 27 template<> \ |
| 25 struct hash<std::pair<type1, type2> > { \ | 28 struct hash<std::pair<type1, type2> > { \ |
| 26 std::size_t operator()(std::pair<type1, type2> value) const | 29 std::size_t operator()(std::pair<type1, type2> value) const |
| 27 | 30 |
| 28 #define DEFINE_PAIR_HASH_FUNCTION_END() \ | 31 #define DEFINE_PAIR_HASH_FUNCTION_END() \ |
| 29 }; | 32 }; |
| 30 | 33 |
| 31 #else | 34 #else |
| 32 #error define DEFINE_PAIR_HASH_FUNCTION_START for your compiler | 35 #error define DEFINE_PAIR_HASH_FUNCTION_START for your compiler |
| 33 #endif // COMPILER | 36 #endif // COMPILER |
| 34 | 37 |
| 35 namespace BASE_HASH_NAMESPACE { | 38 namespace BASE_HASH_NAMESPACE { |
| 36 | 39 |
| 37 // Implement hashing for pairs of at-most 32 bit integer values. | 40 // Implement hashing for pairs of at-most 32 bit integer values. |
| 38 // When size_t is 32 bits, we turn the 64-bit hash code into 32 bits by using | 41 // When size_t is 32 bits, we turn the 64-bit hash code into 32 bits by using |
| 39 // multiply-add hashing. This algorithm, as described in | 42 // multiply-add hashing. This algorithm, as described in |
| 40 // Theorem 4.3.3 of the thesis "Über die Komplexität der Multiplikation in | 43 // Theorem 4.3.3 of the thesis "Über die Komplexität der Multiplikation in |
| 41 // eingeschränkten Branchingprogrammmodellen" by Woelfel, is: | 44 // eingeschränkten Branchingprogrammmodellen" by Woelfel, is: |
| 42 // | 45 // |
| 43 // h32(x32, y32) = (h64(x32, y32) * rand_odd64 + rand16 * 2^16) % 2^64 / 2^32 | 46 // h32(x32, y32) = (h64(x32, y32) * rand_odd64 + rand16 * 2^16) % 2^64 / 2^32 |
| (...skipping 10 matching lines...) Expand all Loading... |
| 54 uint64 odd_random = 481046412LL << 32 | 1025306954LL; \ | 57 uint64 odd_random = 481046412LL << 32 | 1025306954LL; \ |
| 55 uint32 shift_random = 10121U << 16; \ | 58 uint32 shift_random = 10121U << 16; \ |
| 56 \ | 59 \ |
| 57 hash64 = hash64 * odd_random + shift_random; \ | 60 hash64 = hash64 * odd_random + shift_random; \ |
| 58 std::size_t high_bits = static_cast<std::size_t>( \ | 61 std::size_t high_bits = static_cast<std::size_t>( \ |
| 59 hash64 >> (sizeof(uint64) - sizeof(std::size_t))); \ | 62 hash64 >> (sizeof(uint64) - sizeof(std::size_t))); \ |
| 60 return high_bits; \ | 63 return high_bits; \ |
| 61 } \ | 64 } \ |
| 62 DEFINE_PAIR_HASH_FUNCTION_END(); | 65 DEFINE_PAIR_HASH_FUNCTION_END(); |
| 63 | 66 |
| 64 DEFINE_32BIT_PAIR_HASH(short, short); | 67 DEFINE_32BIT_PAIR_HASH(int16, int16); |
| 65 DEFINE_32BIT_PAIR_HASH(short, unsigned short); | 68 DEFINE_32BIT_PAIR_HASH(int16, uint16); |
| 66 DEFINE_32BIT_PAIR_HASH(short, int); | 69 DEFINE_32BIT_PAIR_HASH(int16, int32); |
| 67 DEFINE_32BIT_PAIR_HASH(short, unsigned); | 70 DEFINE_32BIT_PAIR_HASH(int16, uint32); |
| 68 DEFINE_32BIT_PAIR_HASH(unsigned short, short); | 71 DEFINE_32BIT_PAIR_HASH(uint16, int16); |
| 69 DEFINE_32BIT_PAIR_HASH(unsigned short, unsigned short); | 72 DEFINE_32BIT_PAIR_HASH(uint16, uint16); |
| 70 DEFINE_32BIT_PAIR_HASH(unsigned short, int); | 73 DEFINE_32BIT_PAIR_HASH(uint16, int32); |
| 71 DEFINE_32BIT_PAIR_HASH(unsigned short, unsigned); | 74 DEFINE_32BIT_PAIR_HASH(uint16, uint32); |
| 72 DEFINE_32BIT_PAIR_HASH(int, short); | 75 DEFINE_32BIT_PAIR_HASH(int32, int16); |
| 73 DEFINE_32BIT_PAIR_HASH(int, unsigned short); | 76 DEFINE_32BIT_PAIR_HASH(int32, uint16); |
| 74 DEFINE_32BIT_PAIR_HASH(int, int); | 77 DEFINE_32BIT_PAIR_HASH(int32, int32); |
| 75 DEFINE_32BIT_PAIR_HASH(int, unsigned); | 78 DEFINE_32BIT_PAIR_HASH(int32, uint32); |
| 76 DEFINE_32BIT_PAIR_HASH(unsigned, short); | 79 DEFINE_32BIT_PAIR_HASH(uint32, int16); |
| 77 DEFINE_32BIT_PAIR_HASH(unsigned, unsigned short); | 80 DEFINE_32BIT_PAIR_HASH(uint32, uint16); |
| 78 DEFINE_32BIT_PAIR_HASH(unsigned, int); | 81 DEFINE_32BIT_PAIR_HASH(uint32, int32); |
| 79 DEFINE_32BIT_PAIR_HASH(unsigned, unsigned); | 82 DEFINE_32BIT_PAIR_HASH(uint32, uint32); |
| 80 | 83 |
| 81 #undef DEFINE_32BIT_PAIR_HASH | 84 #undef DEFINE_32BIT_PAIR_HASH |
| 82 | 85 |
| 83 // Implement hashing for pairs of up-to 64-bit integer values. | 86 // Implement hashing for pairs of up-to 64-bit integer values. |
| 84 // We use the compound integer hash method to produce a 64-bit hash code, by | 87 // We use the compound integer hash method to produce a 64-bit hash code, by |
| 85 // breaking the two 64-bit inputs into 4 32-bit values: | 88 // breaking the two 64-bit inputs into 4 32-bit values: |
| 86 // http://opendatastructures.org/versions/edition-0.1d/ods-java/node33.html#SECT
ION00832000000000000000 | 89 // http://opendatastructures.org/versions/edition-0.1d/ods-java/node33.html#SECT
ION00832000000000000000 |
| 87 // Then we reduce our result to 32 bits if required, similar to above. | 90 // Then we reduce our result to 32 bits if required, similar to above. |
| 88 | 91 |
| 89 #define DEFINE_64BIT_PAIR_HASH(type1, type2) \ | 92 #define DEFINE_64BIT_PAIR_HASH(type1, type2) \ |
| (...skipping 23 matching lines...) Expand all Loading... |
| 113 uint64 odd_random = 1578233944LL << 32 | 194370989LL; \ | 116 uint64 odd_random = 1578233944LL << 32 | 194370989LL; \ |
| 114 uint32 shift_random = 20591U << 16; \ | 117 uint32 shift_random = 20591U << 16; \ |
| 115 \ | 118 \ |
| 116 hash64 = hash64 * odd_random + shift_random; \ | 119 hash64 = hash64 * odd_random + shift_random; \ |
| 117 std::size_t high_bits = static_cast<std::size_t>( \ | 120 std::size_t high_bits = static_cast<std::size_t>( \ |
| 118 hash64 >> (sizeof(uint64) - sizeof(std::size_t))); \ | 121 hash64 >> (sizeof(uint64) - sizeof(std::size_t))); \ |
| 119 return high_bits; \ | 122 return high_bits; \ |
| 120 } \ | 123 } \ |
| 121 DEFINE_PAIR_HASH_FUNCTION_END(); | 124 DEFINE_PAIR_HASH_FUNCTION_END(); |
| 122 | 125 |
| 123 DEFINE_64BIT_PAIR_HASH(short, int64); | 126 DEFINE_64BIT_PAIR_HASH(int16, int64); |
| 124 DEFINE_64BIT_PAIR_HASH(short, uint64); | 127 DEFINE_64BIT_PAIR_HASH(int16, uint64); |
| 125 DEFINE_64BIT_PAIR_HASH(unsigned short, int64); | 128 DEFINE_64BIT_PAIR_HASH(uint16, int64); |
| 126 DEFINE_64BIT_PAIR_HASH(unsigned short, uint64); | 129 DEFINE_64BIT_PAIR_HASH(uint16, uint64); |
| 127 DEFINE_64BIT_PAIR_HASH(int, int64); | 130 DEFINE_64BIT_PAIR_HASH(int32, int64); |
| 128 DEFINE_64BIT_PAIR_HASH(int, uint64); | 131 DEFINE_64BIT_PAIR_HASH(int32, uint64); |
| 129 DEFINE_64BIT_PAIR_HASH(unsigned, int64); | 132 DEFINE_64BIT_PAIR_HASH(uint32, int64); |
| 130 DEFINE_64BIT_PAIR_HASH(unsigned, uint64); | 133 DEFINE_64BIT_PAIR_HASH(uint32, uint64); |
| 131 DEFINE_64BIT_PAIR_HASH(int64, short); | 134 DEFINE_64BIT_PAIR_HASH(int64, int16); |
| 132 DEFINE_64BIT_PAIR_HASH(int64, unsigned short); | 135 DEFINE_64BIT_PAIR_HASH(int64, uint16); |
| 133 DEFINE_64BIT_PAIR_HASH(int64, int); | 136 DEFINE_64BIT_PAIR_HASH(int64, int32); |
| 134 DEFINE_64BIT_PAIR_HASH(int64, unsigned); | 137 DEFINE_64BIT_PAIR_HASH(int64, uint32); |
| 135 DEFINE_64BIT_PAIR_HASH(int64, int64); | 138 DEFINE_64BIT_PAIR_HASH(int64, int64); |
| 136 DEFINE_64BIT_PAIR_HASH(int64, uint64); | 139 DEFINE_64BIT_PAIR_HASH(int64, uint64); |
| 137 DEFINE_64BIT_PAIR_HASH(uint64, short); | 140 DEFINE_64BIT_PAIR_HASH(uint64, int16); |
| 138 DEFINE_64BIT_PAIR_HASH(uint64, unsigned short); | 141 DEFINE_64BIT_PAIR_HASH(uint64, uint16); |
| 139 DEFINE_64BIT_PAIR_HASH(uint64, int); | 142 DEFINE_64BIT_PAIR_HASH(uint64, int32); |
| 140 DEFINE_64BIT_PAIR_HASH(uint64, unsigned); | 143 DEFINE_64BIT_PAIR_HASH(uint64, uint32); |
| 141 DEFINE_64BIT_PAIR_HASH(uint64, int64); | 144 DEFINE_64BIT_PAIR_HASH(uint64, int64); |
| 142 DEFINE_64BIT_PAIR_HASH(uint64, uint64); | 145 DEFINE_64BIT_PAIR_HASH(uint64, uint64); |
| 143 | 146 |
| 144 #undef DEFINE_64BIT_PAIR_HASH | 147 #undef DEFINE_64BIT_PAIR_HASH |
| 145 } | 148 } |
| 146 | 149 |
| 147 #undef DEFINE_PAIR_HASH_FUNCTION_START | 150 #undef DEFINE_PAIR_HASH_FUNCTION_START |
| 148 #undef DEFINE_PAIR_HASH_FUNCTION_END | 151 #undef DEFINE_PAIR_HASH_FUNCTION_END |
| 149 | 152 |
| 150 #endif // CC_BASE_HASH_PAIR_H_ | 153 #endif // CC_BASE_HASH_PAIR_H_ |
| OLD | NEW |