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.
7 // Deal with the differences between Microsoft and GNU implemenations
8 // of hash_map. Allows all platforms to use |base::hash_map| and
11 // base::hash_map<int> my_map;
12 // base::hash_set<int> my_set;
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.
21 #ifndef BASE_CONTAINERS_HASH_TABLES_H_
22 #define BASE_CONTAINERS_HASH_TABLES_H_
26 #include "base/basictypes.h"
27 #include "base/strings/string16.h"
28 #include "build/build_config.h"
30 #if defined(COMPILER_MSVC)
31 #include <unordered_map>
32 #include <unordered_set>
34 #define BASE_HASH_NAMESPACE std
36 #elif defined(COMPILER_GCC)
38 #define BASE_HASH_NAMESPACE base_hash
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>.
44 #define CHROME_OLD__DEPRECATED __DEPRECATED
48 #if defined(OS_ANDROID)
51 #define BASE_HASH_IMPL_NAMESPACE std
53 #include <ext/hash_map>
54 #include <ext/hash_set>
55 #define BASE_HASH_IMPL_NAMESPACE __gnu_cxx
60 #ifdef CHROME_OLD__DEPRECATED
61 #define __DEPRECATED CHROME_OLD__DEPRECATED
62 #undef CHROME_OLD__DEPRECATED
65 namespace BASE_HASH_NAMESPACE
{
67 // The pre-standard hash behaves like C++11's std::hash, except around pointers.
68 // const char* is specialized to hash the C string and hash functions for
69 // general T* are missing. Define a BASE_HASH_NAMESPACE::hash which aligns with
70 // the C++11 behavior.
74 std::size_t operator()(const T
& value
) const {
75 return BASE_HASH_IMPL_NAMESPACE::hash
<T
>()(value
);
81 std::size_t operator()(T
* value
) const {
82 return BASE_HASH_IMPL_NAMESPACE::hash
<uintptr_t>()(
83 reinterpret_cast<uintptr_t>(value
));
87 #if !defined(OS_ANDROID)
88 // The GNU C++ library provides identity hash functions for many integral types,
89 // but not for |long long|. This hash function will truncate if |size_t| is
90 // narrower than |long long|. This is probably good enough for what we will
93 #define DEFINE_TRIVIAL_HASH(integral_type) \
95 struct hash<integral_type> { \
96 std::size_t operator()(integral_type value) const { \
97 return static_cast<std::size_t>(value); \
101 DEFINE_TRIVIAL_HASH(long long);
102 DEFINE_TRIVIAL_HASH(unsigned long long);
104 #undef DEFINE_TRIVIAL_HASH
105 #endif // !defined(OS_ANDROID)
107 // Implement string hash functions so that strings of various flavors can
108 // be used as keys in STL maps and sets. The hash algorithm comes from the
109 // GNU C++ library, in <tr1/functional>. It is duplicated here because GCC
110 // versions prior to 4.3.2 are unable to compile <tr1/functional> when RTTI
111 // is disabled, as it is in our build.
113 #define DEFINE_STRING_HASH(string_type) \
115 struct hash<string_type> { \
116 std::size_t operator()(const string_type& s) const { \
117 std::size_t result = 0; \
118 for (string_type::const_iterator i = s.begin(); i != s.end(); ++i) \
119 result = (result * 131) + *i; \
124 DEFINE_STRING_HASH(std::string
);
125 DEFINE_STRING_HASH(base::string16
);
127 #undef DEFINE_STRING_HASH
129 } // namespace BASE_HASH_NAMESPACE
132 #error define BASE_HASH_NAMESPACE for your compiler
137 // On MSVC, use the C++11 containers.
138 #if defined(COMPILER_MSVC)
140 template<class Key
, class T
,
141 class Hash
= std::hash
<Key
>,
142 class Pred
= std::equal_to
<Key
>,
143 class Alloc
= std::allocator
<std::pair
<const Key
, T
>>>
144 using hash_map
= std::unordered_map
<Key
, T
, Hash
, Pred
, Alloc
>;
146 template<class Key
, class T
,
147 class Hash
= std::hash
<Key
>,
148 class Pred
= std::equal_to
<Key
>,
149 class Alloc
= std::allocator
<std::pair
<const Key
, T
>>>
150 using hash_multimap
= std::unordered_multimap
<Key
, T
, Hash
, Pred
, Alloc
>;
153 class Hash
= std::hash
<Key
>,
154 class Pred
= std::equal_to
<Key
>,
155 class Alloc
= std::allocator
<Key
>>
156 using hash_multiset
= std::unordered_multiset
<Key
, Hash
, Pred
, Alloc
>;
159 class Hash
= std::hash
<Key
>,
160 class Pred
= std::equal_to
<Key
>,
161 class Alloc
= std::allocator
<Key
>>
162 using hash_set
= std::unordered_set
<Key
, Hash
, Pred
, Alloc
>;
164 #else // !COMPILER_MSVC
166 // Otherwise, use the pre-standard ones, but override the default hash to match
168 template<class Key
, class T
,
169 class Hash
= BASE_HASH_NAMESPACE::hash
<Key
>,
170 class Pred
= std::equal_to
<Key
>,
171 class Alloc
= std::allocator
<std::pair
<const Key
, T
>>>
172 using hash_map
= BASE_HASH_IMPL_NAMESPACE::hash_map
<Key
, T
, Hash
, Pred
, Alloc
>;
174 template<class Key
, class T
,
175 class Hash
= BASE_HASH_NAMESPACE::hash
<Key
>,
176 class Pred
= std::equal_to
<Key
>,
177 class Alloc
= std::allocator
<std::pair
<const Key
, T
>>>
178 using hash_multimap
=
179 BASE_HASH_IMPL_NAMESPACE::hash_multimap
<Key
, T
, Hash
, Pred
, Alloc
>;
182 class Hash
= BASE_HASH_NAMESPACE::hash
<Key
>,
183 class Pred
= std::equal_to
<Key
>,
184 class Alloc
= std::allocator
<Key
>>
185 using hash_multiset
=
186 BASE_HASH_IMPL_NAMESPACE::hash_multiset
<Key
, Hash
, Pred
, Alloc
>;
189 class Hash
= BASE_HASH_NAMESPACE::hash
<Key
>,
190 class Pred
= std::equal_to
<Key
>,
191 class Alloc
= std::allocator
<Key
>>
192 using hash_set
= BASE_HASH_IMPL_NAMESPACE::hash_set
<Key
, Hash
, Pred
, Alloc
>;
194 #undef BASE_HASH_IMPL_NAMESPACE
196 #endif // COMPILER_MSVC
198 // Implement hashing for pairs of at-most 32 bit integer values.
199 // When size_t is 32 bits, we turn the 64-bit hash code into 32 bits by using
200 // multiply-add hashing. This algorithm, as described in
201 // Theorem 4.3.3 of the thesis "Über die Komplexität der Multiplikation in
202 // eingeschränkten Branchingprogrammmodellen" by Woelfel, is:
204 // h32(x32, y32) = (h64(x32, y32) * rand_odd64 + rand16 * 2^16) % 2^64 / 2^32
206 // Contact danakj@chromium.org for any questions.
207 inline std::size_t HashInts32(uint32 value1
, uint32 value2
) {
208 uint64 value1_64
= value1
;
209 uint64 hash64
= (value1_64
<< 32) | value2
;
211 if (sizeof(std::size_t) >= sizeof(uint64
))
212 return static_cast<std::size_t>(hash64
);
214 uint64 odd_random
= 481046412LL << 32 | 1025306955LL;
215 uint32 shift_random
= 10121U << 16;
217 hash64
= hash64
* odd_random
+ shift_random
;
218 std::size_t high_bits
= static_cast<std::size_t>(
219 hash64
>> (8 * (sizeof(uint64
) - sizeof(std::size_t))));
223 // Implement hashing for pairs of up-to 64-bit integer values.
224 // We use the compound integer hash method to produce a 64-bit hash code, by
225 // breaking the two 64-bit inputs into 4 32-bit values:
226 // http://opendatastructures.org/versions/edition-0.1d/ods-java/node33.html#SECTION00832000000000000000
227 // Then we reduce our result to 32 bits if required, similar to above.
228 inline std::size_t HashInts64(uint64 value1
, uint64 value2
) {
229 uint32 short_random1
= 842304669U;
230 uint32 short_random2
= 619063811U;
231 uint32 short_random3
= 937041849U;
232 uint32 short_random4
= 3309708029U;
234 uint32 value1a
= static_cast<uint32
>(value1
& 0xffffffff);
235 uint32 value1b
= static_cast<uint32
>((value1
>> 32) & 0xffffffff);
236 uint32 value2a
= static_cast<uint32
>(value2
& 0xffffffff);
237 uint32 value2b
= static_cast<uint32
>((value2
>> 32) & 0xffffffff);
239 uint64 product1
= static_cast<uint64
>(value1a
) * short_random1
;
240 uint64 product2
= static_cast<uint64
>(value1b
) * short_random2
;
241 uint64 product3
= static_cast<uint64
>(value2a
) * short_random3
;
242 uint64 product4
= static_cast<uint64
>(value2b
) * short_random4
;
244 uint64 hash64
= product1
+ product2
+ product3
+ product4
;
246 if (sizeof(std::size_t) >= sizeof(uint64
))
247 return static_cast<std::size_t>(hash64
);
249 uint64 odd_random
= 1578233944LL << 32 | 194370989LL;
250 uint32 shift_random
= 20591U << 16;
252 hash64
= hash64
* odd_random
+ shift_random
;
253 std::size_t high_bits
= static_cast<std::size_t>(
254 hash64
>> (8 * (sizeof(uint64
) - sizeof(std::size_t))));
258 #define DEFINE_32BIT_PAIR_HASH(Type1, Type2) \
259 inline std::size_t HashPair(Type1 value1, Type2 value2) { \
260 return HashInts32(value1, value2); \
263 DEFINE_32BIT_PAIR_HASH(int16
, int16
);
264 DEFINE_32BIT_PAIR_HASH(int16
, uint16
);
265 DEFINE_32BIT_PAIR_HASH(int16
, int32
);
266 DEFINE_32BIT_PAIR_HASH(int16
, uint32
);
267 DEFINE_32BIT_PAIR_HASH(uint16
, int16
);
268 DEFINE_32BIT_PAIR_HASH(uint16
, uint16
);
269 DEFINE_32BIT_PAIR_HASH(uint16
, int32
);
270 DEFINE_32BIT_PAIR_HASH(uint16
, uint32
);
271 DEFINE_32BIT_PAIR_HASH(int32
, int16
);
272 DEFINE_32BIT_PAIR_HASH(int32
, uint16
);
273 DEFINE_32BIT_PAIR_HASH(int32
, int32
);
274 DEFINE_32BIT_PAIR_HASH(int32
, uint32
);
275 DEFINE_32BIT_PAIR_HASH(uint32
, int16
);
276 DEFINE_32BIT_PAIR_HASH(uint32
, uint16
);
277 DEFINE_32BIT_PAIR_HASH(uint32
, int32
);
278 DEFINE_32BIT_PAIR_HASH(uint32
, uint32
);
280 #undef DEFINE_32BIT_PAIR_HASH
282 #define DEFINE_64BIT_PAIR_HASH(Type1, Type2) \
283 inline std::size_t HashPair(Type1 value1, Type2 value2) { \
284 return HashInts64(value1, value2); \
287 DEFINE_64BIT_PAIR_HASH(int16
, int64
);
288 DEFINE_64BIT_PAIR_HASH(int16
, uint64
);
289 DEFINE_64BIT_PAIR_HASH(uint16
, int64
);
290 DEFINE_64BIT_PAIR_HASH(uint16
, uint64
);
291 DEFINE_64BIT_PAIR_HASH(int32
, int64
);
292 DEFINE_64BIT_PAIR_HASH(int32
, uint64
);
293 DEFINE_64BIT_PAIR_HASH(uint32
, int64
);
294 DEFINE_64BIT_PAIR_HASH(uint32
, uint64
);
295 DEFINE_64BIT_PAIR_HASH(int64
, int16
);
296 DEFINE_64BIT_PAIR_HASH(int64
, uint16
);
297 DEFINE_64BIT_PAIR_HASH(int64
, int32
);
298 DEFINE_64BIT_PAIR_HASH(int64
, uint32
);
299 DEFINE_64BIT_PAIR_HASH(int64
, int64
);
300 DEFINE_64BIT_PAIR_HASH(int64
, uint64
);
301 DEFINE_64BIT_PAIR_HASH(uint64
, int16
);
302 DEFINE_64BIT_PAIR_HASH(uint64
, uint16
);
303 DEFINE_64BIT_PAIR_HASH(uint64
, int32
);
304 DEFINE_64BIT_PAIR_HASH(uint64
, uint32
);
305 DEFINE_64BIT_PAIR_HASH(uint64
, int64
);
306 DEFINE_64BIT_PAIR_HASH(uint64
, uint64
);
308 #undef DEFINE_64BIT_PAIR_HASH
311 namespace BASE_HASH_NAMESPACE
{
313 // Implement methods for hashing a pair of integers, so they can be used as
314 // keys in STL containers.
316 template<typename Type1
, typename Type2
>
317 struct hash
<std::pair
<Type1
, Type2
> > {
318 std::size_t operator()(std::pair
<Type1
, Type2
> value
) const {
319 return base::HashPair(value
.first
, value
.second
);
325 #undef DEFINE_PAIR_HASH_FUNCTION_START
326 #undef DEFINE_PAIR_HASH_FUNCTION_END
328 #endif // BASE_CONTAINERS_HASH_TABLES_H_