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 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
111 | 111 |
112 #else // COMPILER | 112 #else // COMPILER |
113 #error define BASE_HASH_NAMESPACE for your compiler | 113 #error define BASE_HASH_NAMESPACE for your compiler |
114 #endif // COMPILER | 114 #endif // COMPILER |
115 | 115 |
116 namespace base { | 116 namespace base { |
117 using BASE_HASH_NAMESPACE::hash_map; | 117 using BASE_HASH_NAMESPACE::hash_map; |
118 using BASE_HASH_NAMESPACE::hash_multimap; | 118 using BASE_HASH_NAMESPACE::hash_multimap; |
119 using BASE_HASH_NAMESPACE::hash_multiset; | 119 using BASE_HASH_NAMESPACE::hash_multiset; |
120 using BASE_HASH_NAMESPACE::hash_set; | 120 using BASE_HASH_NAMESPACE::hash_set; |
121 } | |
122 | |
123 | |
124 // Implement methods for hashing a pair of integers, so they can be used as | |
125 // keys in STL containers. | |
126 | |
127 #if defined(COMPILER_MSVC) | |
128 | |
129 #define DEFINE_PAIR_HASH_FUNCTION_START(type1, type2) \ | |
130 template<> \ | |
131 inline std::size_t hash_value<std::pair<type1, type2> >( \ | |
132 const std::pair<type1, type2>& value) | |
133 | |
134 #define DEFINE_PAIR_HASH_FUNCTION_END() | |
135 | |
136 #elif defined(COMPILER_GCC) | |
137 | |
138 #define DEFINE_PAIR_HASH_FUNCTION_START(type1, type2) \ | |
139 template<> \ | |
140 struct hash<std::pair<type1, type2> > { \ | |
141 std::size_t operator()(std::pair<type1, type2> value) const | |
142 | |
143 #define DEFINE_PAIR_HASH_FUNCTION_END() \ | |
144 }; | |
145 | |
146 #else | |
147 #error define DEFINE_PAIR_HASH_FUNCTION_START for your compiler | |
148 #endif // COMPILER | |
149 | |
150 namespace BASE_HASH_NAMESPACE { | |
151 | 121 |
152 // Implement hashing for pairs of at-most 32 bit integer values. | 122 // Implement hashing for pairs of at-most 32 bit integer values. |
153 // When size_t is 32 bits, we turn the 64-bit hash code into 32 bits by using | 123 // When size_t is 32 bits, we turn the 64-bit hash code into 32 bits by using |
154 // multiply-add hashing. This algorithm, as described in | 124 // multiply-add hashing. This algorithm, as described in |
155 // Theorem 4.3.3 of the thesis "Über die Komplexität der Multiplikation in | 125 // Theorem 4.3.3 of the thesis "Über die Komplexität der Multiplikation in |
156 // eingeschränkten Branchingprogrammmodellen" by Woelfel, is: | 126 // eingeschränkten Branchingprogrammmodellen" by Woelfel, is: |
157 // | 127 // |
158 // h32(x32, y32) = (h64(x32, y32) * rand_odd64 + rand16 * 2^16) % 2^64 / 2^32 | 128 // h32(x32, y32) = (h64(x32, y32) * rand_odd64 + rand16 * 2^16) % 2^64 / 2^32 |
159 // | 129 // |
160 // Contact danakj@chromium.org for any questions. | 130 // Contact danakj@chromium.org for any questions. |
161 #define DEFINE_32BIT_PAIR_HASH(type1, type2) \ | 131 inline std::size_t HashInts32(uint32 value1, uint32 value2) { |
162 DEFINE_PAIR_HASH_FUNCTION_START(type1, type2) { \ | 132 uint64 value1_64 = value1; |
163 uint64 first = value.first; \ | 133 uint64 hash64 = (value1_64 << 32) | value2; |
164 uint32 second = value.second; \ | 134 |
165 uint64 hash64 = (first << 32) | second; \ | 135 if (sizeof(std::size_t) >= sizeof(uint64)) |
166 \ | 136 return static_cast<std::size_t>(hash64); |
167 if (sizeof(std::size_t) >= sizeof(uint64)) \ | 137 |
168 return static_cast<std::size_t>(hash64); \ | 138 uint64 odd_random = 481046412LL << 32 | 1025306955LL; |
169 \ | 139 uint32 shift_random = 10121U << 16; |
170 uint64 odd_random = 481046412LL << 32 | 1025306954LL; \ | 140 |
171 uint32 shift_random = 10121U << 16; \ | 141 hash64 = hash64 * odd_random + shift_random; |
172 \ | 142 std::size_t high_bits = static_cast<std::size_t>( |
173 hash64 = hash64 * odd_random + shift_random; \ | 143 hash64 >> (sizeof(uint64) - sizeof(std::size_t))); |
174 std::size_t high_bits = static_cast<std::size_t>( \ | 144 return high_bits; |
175 hash64 >> (sizeof(uint64) - sizeof(std::size_t))); \ | 145 } |
176 return high_bits; \ | 146 |
177 } \ | 147 // Implement hashing for pairs of up-to 64-bit integer values. |
178 DEFINE_PAIR_HASH_FUNCTION_END(); | 148 // We use the compound integer hash method to produce a 64-bit hash code, by |
| 149 // breaking the two 64-bit inputs into 4 32-bit values: |
| 150 // http://opendatastructures.org/versions/edition-0.1d/ods-java/node33.html#SECT
ION00832000000000000000 |
| 151 // Then we reduce our result to 32 bits if required, similar to above. |
| 152 inline std::size_t HashInts64(uint64 value1, uint64 value2) { |
| 153 uint32 short_random1 = 842304669U; |
| 154 uint32 short_random2 = 619063811U; |
| 155 uint32 short_random3 = 937041849U; |
| 156 uint32 short_random4 = 3309708029U; |
| 157 |
| 158 uint32 value1a = static_cast<uint32>(value1 & 0xffffffff); |
| 159 uint32 value1b = static_cast<uint32>((value1 >> 32) & 0xffffffff); |
| 160 uint32 value2a = static_cast<uint32>(value2 & 0xffffffff); |
| 161 uint32 value2b = static_cast<uint32>((value2 >> 32) & 0xffffffff); |
| 162 |
| 163 uint64 product1 = static_cast<uint64>(value1a) * short_random1; |
| 164 uint64 product2 = static_cast<uint64>(value1b) * short_random2; |
| 165 uint64 product3 = static_cast<uint64>(value2a) * short_random3; |
| 166 uint64 product4 = static_cast<uint64>(value2b) * short_random4; |
| 167 |
| 168 uint64 hash64 = product1 + product2 + product3 + product4; |
| 169 |
| 170 if (sizeof(std::size_t) >= sizeof(uint64)) |
| 171 return static_cast<std::size_t>(hash64); |
| 172 |
| 173 uint64 odd_random = 1578233944LL << 32 | 194370989LL; |
| 174 uint32 shift_random = 20591U << 16; |
| 175 |
| 176 hash64 = hash64 * odd_random + shift_random; |
| 177 std::size_t high_bits = static_cast<std::size_t>( |
| 178 hash64 >> (sizeof(uint64) - sizeof(std::size_t))); |
| 179 return high_bits; |
| 180 } |
| 181 |
| 182 #define DEFINE_32BIT_PAIR_HASH(Type1, Type2) \ |
| 183 inline std::size_t HashPair(Type1 value1, Type2 value2) { \ |
| 184 return HashInts32(value1, value2); \ |
| 185 } |
179 | 186 |
180 DEFINE_32BIT_PAIR_HASH(int16, int16); | 187 DEFINE_32BIT_PAIR_HASH(int16, int16); |
181 DEFINE_32BIT_PAIR_HASH(int16, uint16); | 188 DEFINE_32BIT_PAIR_HASH(int16, uint16); |
182 DEFINE_32BIT_PAIR_HASH(int16, int32); | 189 DEFINE_32BIT_PAIR_HASH(int16, int32); |
183 DEFINE_32BIT_PAIR_HASH(int16, uint32); | 190 DEFINE_32BIT_PAIR_HASH(int16, uint32); |
184 DEFINE_32BIT_PAIR_HASH(uint16, int16); | 191 DEFINE_32BIT_PAIR_HASH(uint16, int16); |
185 DEFINE_32BIT_PAIR_HASH(uint16, uint16); | 192 DEFINE_32BIT_PAIR_HASH(uint16, uint16); |
186 DEFINE_32BIT_PAIR_HASH(uint16, int32); | 193 DEFINE_32BIT_PAIR_HASH(uint16, int32); |
187 DEFINE_32BIT_PAIR_HASH(uint16, uint32); | 194 DEFINE_32BIT_PAIR_HASH(uint16, uint32); |
188 DEFINE_32BIT_PAIR_HASH(int32, int16); | 195 DEFINE_32BIT_PAIR_HASH(int32, int16); |
189 DEFINE_32BIT_PAIR_HASH(int32, uint16); | 196 DEFINE_32BIT_PAIR_HASH(int32, uint16); |
190 DEFINE_32BIT_PAIR_HASH(int32, int32); | 197 DEFINE_32BIT_PAIR_HASH(int32, int32); |
191 DEFINE_32BIT_PAIR_HASH(int32, uint32); | 198 DEFINE_32BIT_PAIR_HASH(int32, uint32); |
192 DEFINE_32BIT_PAIR_HASH(uint32, int16); | 199 DEFINE_32BIT_PAIR_HASH(uint32, int16); |
193 DEFINE_32BIT_PAIR_HASH(uint32, uint16); | 200 DEFINE_32BIT_PAIR_HASH(uint32, uint16); |
194 DEFINE_32BIT_PAIR_HASH(uint32, int32); | 201 DEFINE_32BIT_PAIR_HASH(uint32, int32); |
195 DEFINE_32BIT_PAIR_HASH(uint32, uint32); | 202 DEFINE_32BIT_PAIR_HASH(uint32, uint32); |
196 | 203 |
197 #undef DEFINE_32BIT_PAIR_HASH | 204 #undef DEFINE_32BIT_PAIR_HASH |
198 | 205 |
199 // Implement hashing for pairs of up-to 64-bit integer values. | 206 #define DEFINE_64BIT_PAIR_HASH(Type1, Type2) \ |
200 // We use the compound integer hash method to produce a 64-bit hash code, by | 207 inline std::size_t HashPair(Type1 value1, Type2 value2) { \ |
201 // breaking the two 64-bit inputs into 4 32-bit values: | 208 return HashInts64(value1, value2); \ |
202 // http://opendatastructures.org/versions/edition-0.1d/ods-java/node33.html#SECT
ION00832000000000000000 | 209 } |
203 // Then we reduce our result to 32 bits if required, similar to above. | |
204 #define DEFINE_64BIT_PAIR_HASH(type1, type2) \ | |
205 DEFINE_PAIR_HASH_FUNCTION_START(type1, type2) { \ | |
206 uint32 short_random1 = 842304669U; \ | |
207 uint32 short_random2 = 619063811U; \ | |
208 uint32 short_random3 = 937041849U; \ | |
209 uint32 short_random4 = 3309708029U; \ | |
210 \ | |
211 uint64 value1 = value.first; \ | |
212 uint64 value2 = value.second; \ | |
213 uint32 value1a = static_cast<uint32>(value1 & 0xffffffff); \ | |
214 uint32 value1b = static_cast<uint32>((value1 >> 32) & 0xffffffff); \ | |
215 uint32 value2a = static_cast<uint32>(value2 & 0xffffffff); \ | |
216 uint32 value2b = static_cast<uint32>((value2 >> 32) & 0xffffffff); \ | |
217 \ | |
218 uint64 product1 = static_cast<uint64>(value1a) * short_random1; \ | |
219 uint64 product2 = static_cast<uint64>(value1b) * short_random2; \ | |
220 uint64 product3 = static_cast<uint64>(value2a) * short_random3; \ | |
221 uint64 product4 = static_cast<uint64>(value2b) * short_random4; \ | |
222 \ | |
223 uint64 hash64 = product1 + product2 + product3 + product4; \ | |
224 \ | |
225 if (sizeof(std::size_t) >= sizeof(uint64)) \ | |
226 return static_cast<std::size_t>(hash64); \ | |
227 \ | |
228 uint64 odd_random = 1578233944LL << 32 | 194370989LL; \ | |
229 uint32 shift_random = 20591U << 16; \ | |
230 \ | |
231 hash64 = hash64 * odd_random + shift_random; \ | |
232 std::size_t high_bits = static_cast<std::size_t>( \ | |
233 hash64 >> (sizeof(uint64) - sizeof(std::size_t))); \ | |
234 return high_bits; \ | |
235 } \ | |
236 DEFINE_PAIR_HASH_FUNCTION_END(); | |
237 | 210 |
238 DEFINE_64BIT_PAIR_HASH(int16, int64); | 211 DEFINE_64BIT_PAIR_HASH(int16, int64); |
239 DEFINE_64BIT_PAIR_HASH(int16, uint64); | 212 DEFINE_64BIT_PAIR_HASH(int16, uint64); |
240 DEFINE_64BIT_PAIR_HASH(uint16, int64); | 213 DEFINE_64BIT_PAIR_HASH(uint16, int64); |
241 DEFINE_64BIT_PAIR_HASH(uint16, uint64); | 214 DEFINE_64BIT_PAIR_HASH(uint16, uint64); |
242 DEFINE_64BIT_PAIR_HASH(int32, int64); | 215 DEFINE_64BIT_PAIR_HASH(int32, int64); |
243 DEFINE_64BIT_PAIR_HASH(int32, uint64); | 216 DEFINE_64BIT_PAIR_HASH(int32, uint64); |
244 DEFINE_64BIT_PAIR_HASH(uint32, int64); | 217 DEFINE_64BIT_PAIR_HASH(uint32, int64); |
245 DEFINE_64BIT_PAIR_HASH(uint32, uint64); | 218 DEFINE_64BIT_PAIR_HASH(uint32, uint64); |
246 DEFINE_64BIT_PAIR_HASH(int64, int16); | 219 DEFINE_64BIT_PAIR_HASH(int64, int16); |
247 DEFINE_64BIT_PAIR_HASH(int64, uint16); | 220 DEFINE_64BIT_PAIR_HASH(int64, uint16); |
248 DEFINE_64BIT_PAIR_HASH(int64, int32); | 221 DEFINE_64BIT_PAIR_HASH(int64, int32); |
249 DEFINE_64BIT_PAIR_HASH(int64, uint32); | 222 DEFINE_64BIT_PAIR_HASH(int64, uint32); |
250 DEFINE_64BIT_PAIR_HASH(int64, int64); | 223 DEFINE_64BIT_PAIR_HASH(int64, int64); |
251 DEFINE_64BIT_PAIR_HASH(int64, uint64); | 224 DEFINE_64BIT_PAIR_HASH(int64, uint64); |
252 DEFINE_64BIT_PAIR_HASH(uint64, int16); | 225 DEFINE_64BIT_PAIR_HASH(uint64, int16); |
253 DEFINE_64BIT_PAIR_HASH(uint64, uint16); | 226 DEFINE_64BIT_PAIR_HASH(uint64, uint16); |
254 DEFINE_64BIT_PAIR_HASH(uint64, int32); | 227 DEFINE_64BIT_PAIR_HASH(uint64, int32); |
255 DEFINE_64BIT_PAIR_HASH(uint64, uint32); | 228 DEFINE_64BIT_PAIR_HASH(uint64, uint32); |
256 DEFINE_64BIT_PAIR_HASH(uint64, int64); | 229 DEFINE_64BIT_PAIR_HASH(uint64, int64); |
257 DEFINE_64BIT_PAIR_HASH(uint64, uint64); | 230 DEFINE_64BIT_PAIR_HASH(uint64, uint64); |
258 | 231 |
259 #undef DEFINE_64BIT_PAIR_HASH | 232 #undef DEFINE_64BIT_PAIR_HASH |
| 233 } // namespace base |
| 234 |
| 235 namespace BASE_HASH_NAMESPACE { |
| 236 |
| 237 // Implement methods for hashing a pair of integers, so they can be used as |
| 238 // keys in STL containers. |
| 239 |
| 240 #if defined(COMPILER_MSVC) |
| 241 |
| 242 template<typename Type1, typename Type2> |
| 243 inline std::size_t hash_value(const std::pair<Type1, Type2>& value) { |
| 244 return base::HashPair(value.first, value.second); |
| 245 } |
| 246 |
| 247 #elif defined(COMPILER_GCC) |
| 248 template<typename Type1, typename Type2> |
| 249 struct hash<std::pair<Type1, Type2> > { |
| 250 std::size_t operator()(std::pair<Type1, Type2> value) const { |
| 251 return base::HashPair(value.first, value.second); |
| 252 } |
| 253 }; |
| 254 |
| 255 #else |
| 256 #error define hash<std::pair<Type1, Type2> > for your compiler |
| 257 #endif // COMPILER |
| 258 |
260 } | 259 } |
261 | 260 |
262 #undef DEFINE_PAIR_HASH_FUNCTION_START | 261 #undef DEFINE_PAIR_HASH_FUNCTION_START |
263 #undef DEFINE_PAIR_HASH_FUNCTION_END | 262 #undef DEFINE_PAIR_HASH_FUNCTION_END |
264 | 263 |
265 #endif // BASE_CONTAINERS_HASH_TABLES_H_ | 264 #endif // BASE_CONTAINERS_HASH_TABLES_H_ |
OLD | NEW |