| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 // | |
| 5 | |
| 6 // | |
| 7 // Deal with the differences between Microsoft and GNU implemenations | |
| 8 // of hash_map. Allows all platforms to use |base::hash_map| and | |
| 9 // |base::hash_set|. | |
| 10 // eg: | |
| 11 // base::hash_map<int> my_map; | |
| 12 // base::hash_set<int> my_set; | |
| 13 // | |
| 14 // NOTE: It is an explicit non-goal of this class to provide a generic hash | |
| 15 // function for pointers. If you want to hash a pointers to a particular class, | |
| 16 // please define the template specialization elsewhere (for example, in its | |
| 17 // header file) and keep it specific to just pointers to that class. This is | |
| 18 // because identity hashes are not desirable for all types that might show up | |
| 19 // in containers as pointers. | |
| 20 | |
| 21 #ifndef BASE_CONTAINERS_HASH_TABLES_H_ | |
| 22 #define BASE_CONTAINERS_HASH_TABLES_H_ | |
| 23 | |
| 24 #include <utility> | |
| 25 | |
| 26 #include "base/basictypes.h" | |
| 27 #include "base/strings/string16.h" | |
| 28 #include "build/build_config.h" | |
| 29 | |
| 30 #if defined(COMPILER_MSVC) | |
| 31 #include <unordered_map> | |
| 32 #include <unordered_set> | |
| 33 | |
| 34 #define BASE_HASH_NAMESPACE std | |
| 35 | |
| 36 #elif defined(COMPILER_GCC) | |
| 37 | |
| 38 #define BASE_HASH_NAMESPACE base_hash | |
| 39 | |
| 40 // This is a hack to disable the gcc 4.4 warning about hash_map and hash_set | |
| 41 // being deprecated. We can get rid of this when we upgrade to VS2008 and we | |
| 42 // can use <tr1/unordered_map> and <tr1/unordered_set>. | |
| 43 #ifdef __DEPRECATED | |
| 44 #define CHROME_OLD__DEPRECATED __DEPRECATED | |
| 45 #undef __DEPRECATED | |
| 46 #endif | |
| 47 | |
| 48 #include <ext/hash_map> | |
| 49 #include <ext/hash_set> | |
| 50 #define BASE_HASH_IMPL_NAMESPACE __gnu_cxx | |
| 51 | |
| 52 #include <string> | |
| 53 | |
| 54 #ifdef CHROME_OLD__DEPRECATED | |
| 55 #define __DEPRECATED CHROME_OLD__DEPRECATED | |
| 56 #undef CHROME_OLD__DEPRECATED | |
| 57 #endif | |
| 58 | |
| 59 namespace BASE_HASH_NAMESPACE { | |
| 60 | |
| 61 // The pre-standard hash behaves like C++11's std::hash, except around pointers. | |
| 62 // const char* is specialized to hash the C string and hash functions for | |
| 63 // general T* are missing. Define a BASE_HASH_NAMESPACE::hash which aligns with | |
| 64 // the C++11 behavior. | |
| 65 | |
| 66 template<typename T> | |
| 67 struct hash { | |
| 68 std::size_t operator()(const T& value) const { | |
| 69 return BASE_HASH_IMPL_NAMESPACE::hash<T>()(value); | |
| 70 } | |
| 71 }; | |
| 72 | |
| 73 template<typename T> | |
| 74 struct hash<T*> { | |
| 75 std::size_t operator()(T* value) const { | |
| 76 return BASE_HASH_IMPL_NAMESPACE::hash<uintptr_t>()( | |
| 77 reinterpret_cast<uintptr_t>(value)); | |
| 78 } | |
| 79 }; | |
| 80 | |
| 81 // The GNU C++ library provides identity hash functions for many integral types, | |
| 82 // but not for |long long|. This hash function will truncate if |size_t| is | |
| 83 // narrower than |long long|. This is probably good enough for what we will | |
| 84 // use it for. | |
| 85 | |
| 86 #define DEFINE_TRIVIAL_HASH(integral_type) \ | |
| 87 template<> \ | |
| 88 struct hash<integral_type> { \ | |
| 89 std::size_t operator()(integral_type value) const { \ | |
| 90 return static_cast<std::size_t>(value); \ | |
| 91 } \ | |
| 92 } | |
| 93 | |
| 94 DEFINE_TRIVIAL_HASH(long long); | |
| 95 DEFINE_TRIVIAL_HASH(unsigned long long); | |
| 96 | |
| 97 #undef DEFINE_TRIVIAL_HASH | |
| 98 | |
| 99 // Implement string hash functions so that strings of various flavors can | |
| 100 // be used as keys in STL maps and sets. The hash algorithm comes from the | |
| 101 // GNU C++ library, in <tr1/functional>. It is duplicated here because GCC | |
| 102 // versions prior to 4.3.2 are unable to compile <tr1/functional> when RTTI | |
| 103 // is disabled, as it is in our build. | |
| 104 | |
| 105 #define DEFINE_STRING_HASH(string_type) \ | |
| 106 template<> \ | |
| 107 struct hash<string_type> { \ | |
| 108 std::size_t operator()(const string_type& s) const { \ | |
| 109 std::size_t result = 0; \ | |
| 110 for (string_type::const_iterator i = s.begin(); i != s.end(); ++i) \ | |
| 111 result = (result * 131) + *i; \ | |
| 112 return result; \ | |
| 113 } \ | |
| 114 } | |
| 115 | |
| 116 DEFINE_STRING_HASH(std::string); | |
| 117 DEFINE_STRING_HASH(base::string16); | |
| 118 | |
| 119 #undef DEFINE_STRING_HASH | |
| 120 | |
| 121 } // namespace BASE_HASH_NAMESPACE | |
| 122 | |
| 123 #else // COMPILER | |
| 124 #error define BASE_HASH_NAMESPACE for your compiler | |
| 125 #endif // COMPILER | |
| 126 | |
| 127 namespace base { | |
| 128 | |
| 129 // On MSVC, use the C++11 containers. | |
| 130 #if defined(COMPILER_MSVC) | |
| 131 | |
| 132 template<class Key, class T, | |
| 133 class Hash = std::hash<Key>, | |
| 134 class Pred = std::equal_to<Key>, | |
| 135 class Alloc = std::allocator<std::pair<const Key, T>>> | |
| 136 using hash_map = std::unordered_map<Key, T, Hash, Pred, Alloc>; | |
| 137 | |
| 138 template<class Key, class T, | |
| 139 class Hash = std::hash<Key>, | |
| 140 class Pred = std::equal_to<Key>, | |
| 141 class Alloc = std::allocator<std::pair<const Key, T>>> | |
| 142 using hash_multimap = std::unordered_multimap<Key, T, Hash, Pred, Alloc>; | |
| 143 | |
| 144 template<class Key, | |
| 145 class Hash = std::hash<Key>, | |
| 146 class Pred = std::equal_to<Key>, | |
| 147 class Alloc = std::allocator<Key>> | |
| 148 using hash_multiset = std::unordered_multiset<Key, Hash, Pred, Alloc>; | |
| 149 | |
| 150 template<class Key, | |
| 151 class Hash = std::hash<Key>, | |
| 152 class Pred = std::equal_to<Key>, | |
| 153 class Alloc = std::allocator<Key>> | |
| 154 using hash_set = std::unordered_set<Key, Hash, Pred, Alloc>; | |
| 155 | |
| 156 #else // !COMPILER_MSVC | |
| 157 | |
| 158 // Otherwise, use the pre-standard ones, but override the default hash to match | |
| 159 // C++11. | |
| 160 template<class Key, class T, | |
| 161 class Hash = BASE_HASH_NAMESPACE::hash<Key>, | |
| 162 class Pred = std::equal_to<Key>, | |
| 163 class Alloc = std::allocator<std::pair<const Key, T>>> | |
| 164 using hash_map = BASE_HASH_IMPL_NAMESPACE::hash_map<Key, T, Hash, Pred, Alloc>; | |
| 165 | |
| 166 template<class Key, class T, | |
| 167 class Hash = BASE_HASH_NAMESPACE::hash<Key>, | |
| 168 class Pred = std::equal_to<Key>, | |
| 169 class Alloc = std::allocator<std::pair<const Key, T>>> | |
| 170 using hash_multimap = | |
| 171 BASE_HASH_IMPL_NAMESPACE::hash_multimap<Key, T, Hash, Pred, Alloc>; | |
| 172 | |
| 173 template<class Key, | |
| 174 class Hash = BASE_HASH_NAMESPACE::hash<Key>, | |
| 175 class Pred = std::equal_to<Key>, | |
| 176 class Alloc = std::allocator<Key>> | |
| 177 using hash_multiset = | |
| 178 BASE_HASH_IMPL_NAMESPACE::hash_multiset<Key, Hash, Pred, Alloc>; | |
| 179 | |
| 180 template<class Key, | |
| 181 class Hash = BASE_HASH_NAMESPACE::hash<Key>, | |
| 182 class Pred = std::equal_to<Key>, | |
| 183 class Alloc = std::allocator<Key>> | |
| 184 using hash_set = BASE_HASH_IMPL_NAMESPACE::hash_set<Key, Hash, Pred, Alloc>; | |
| 185 | |
| 186 #undef BASE_HASH_IMPL_NAMESPACE | |
| 187 | |
| 188 #endif // COMPILER_MSVC | |
| 189 | |
| 190 // Implement hashing for pairs of at-most 32 bit integer values. | |
| 191 // When size_t is 32 bits, we turn the 64-bit hash code into 32 bits by using | |
| 192 // multiply-add hashing. This algorithm, as described in | |
| 193 // Theorem 4.3.3 of the thesis "Über die Komplexität der Multiplikation in | |
| 194 // eingeschränkten Branchingprogrammmodellen" by Woelfel, is: | |
| 195 // | |
| 196 // h32(x32, y32) = (h64(x32, y32) * rand_odd64 + rand16 * 2^16) % 2^64 / 2^32 | |
| 197 // | |
| 198 // Contact danakj@chromium.org for any questions. | |
| 199 inline std::size_t HashInts32(uint32 value1, uint32 value2) { | |
| 200 uint64 value1_64 = value1; | |
| 201 uint64 hash64 = (value1_64 << 32) | value2; | |
| 202 | |
| 203 if (sizeof(std::size_t) >= sizeof(uint64)) | |
| 204 return static_cast<std::size_t>(hash64); | |
| 205 | |
| 206 uint64 odd_random = 481046412LL << 32 | 1025306955LL; | |
| 207 uint32 shift_random = 10121U << 16; | |
| 208 | |
| 209 hash64 = hash64 * odd_random + shift_random; | |
| 210 std::size_t high_bits = static_cast<std::size_t>( | |
| 211 hash64 >> (8 * (sizeof(uint64) - sizeof(std::size_t)))); | |
| 212 return high_bits; | |
| 213 } | |
| 214 | |
| 215 // Implement hashing for pairs of up-to 64-bit integer values. | |
| 216 // We use the compound integer hash method to produce a 64-bit hash code, by | |
| 217 // breaking the two 64-bit inputs into 4 32-bit values: | |
| 218 // http://opendatastructures.org/versions/edition-0.1d/ods-java/node33.html#SECT
ION00832000000000000000 | |
| 219 // Then we reduce our result to 32 bits if required, similar to above. | |
| 220 inline std::size_t HashInts64(uint64 value1, uint64 value2) { | |
| 221 uint32 short_random1 = 842304669U; | |
| 222 uint32 short_random2 = 619063811U; | |
| 223 uint32 short_random3 = 937041849U; | |
| 224 uint32 short_random4 = 3309708029U; | |
| 225 | |
| 226 uint32 value1a = static_cast<uint32>(value1 & 0xffffffff); | |
| 227 uint32 value1b = static_cast<uint32>((value1 >> 32) & 0xffffffff); | |
| 228 uint32 value2a = static_cast<uint32>(value2 & 0xffffffff); | |
| 229 uint32 value2b = static_cast<uint32>((value2 >> 32) & 0xffffffff); | |
| 230 | |
| 231 uint64 product1 = static_cast<uint64>(value1a) * short_random1; | |
| 232 uint64 product2 = static_cast<uint64>(value1b) * short_random2; | |
| 233 uint64 product3 = static_cast<uint64>(value2a) * short_random3; | |
| 234 uint64 product4 = static_cast<uint64>(value2b) * short_random4; | |
| 235 | |
| 236 uint64 hash64 = product1 + product2 + product3 + product4; | |
| 237 | |
| 238 if (sizeof(std::size_t) >= sizeof(uint64)) | |
| 239 return static_cast<std::size_t>(hash64); | |
| 240 | |
| 241 uint64 odd_random = 1578233944LL << 32 | 194370989LL; | |
| 242 uint32 shift_random = 20591U << 16; | |
| 243 | |
| 244 hash64 = hash64 * odd_random + shift_random; | |
| 245 std::size_t high_bits = static_cast<std::size_t>( | |
| 246 hash64 >> (8 * (sizeof(uint64) - sizeof(std::size_t)))); | |
| 247 return high_bits; | |
| 248 } | |
| 249 | |
| 250 #define DEFINE_32BIT_PAIR_HASH(Type1, Type2) \ | |
| 251 inline std::size_t HashPair(Type1 value1, Type2 value2) { \ | |
| 252 return HashInts32(value1, value2); \ | |
| 253 } | |
| 254 | |
| 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 | |
| 302 | |
| 303 namespace BASE_HASH_NAMESPACE { | |
| 304 | |
| 305 // Implement methods for hashing a pair of integers, so they can be used as | |
| 306 // keys in STL containers. | |
| 307 | |
| 308 template<typename Type1, typename Type2> | |
| 309 struct hash<std::pair<Type1, Type2> > { | |
| 310 std::size_t operator()(std::pair<Type1, Type2> value) const { | |
| 311 return base::HashPair(value.first, value.second); | |
| 312 } | |
| 313 }; | |
| 314 | |
| 315 } // namespace BASE_HASH_NAMESPACE | |
| 316 | |
| 317 #undef DEFINE_PAIR_HASH_FUNCTION_START | |
| 318 #undef DEFINE_PAIR_HASH_FUNCTION_END | |
| 319 | |
| 320 #endif // BASE_CONTAINERS_HASH_TABLES_H_ | |
| OLD | NEW |