Gallery: replace paper-input of rename field with HTMLInputElement.
[chromium-blink-merge.git] / base / containers / hash_tables.h
blob5ce9161917269bde566188c88670f00fe1f83483
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 //
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;
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_
24 #include <utility>
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>.
43 #ifdef __DEPRECATED
44 #define CHROME_OLD__DEPRECATED __DEPRECATED
45 #undef __DEPRECATED
46 #endif
48 #include <ext/hash_map>
49 #include <ext/hash_set>
50 #define BASE_HASH_IMPL_NAMESPACE __gnu_cxx
52 #include <string>
54 #ifdef CHROME_OLD__DEPRECATED
55 #define __DEPRECATED CHROME_OLD__DEPRECATED
56 #undef CHROME_OLD__DEPRECATED
57 #endif
59 namespace BASE_HASH_NAMESPACE {
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.
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);
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));
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.
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 } \
94 DEFINE_TRIVIAL_HASH(long long);
95 DEFINE_TRIVIAL_HASH(unsigned long long);
97 #undef DEFINE_TRIVIAL_HASH
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.
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; \
116 DEFINE_STRING_HASH(std::string);
117 DEFINE_STRING_HASH(base::string16);
119 #undef DEFINE_STRING_HASH
121 } // namespace BASE_HASH_NAMESPACE
123 #else // COMPILER
124 #error define BASE_HASH_NAMESPACE for your compiler
125 #endif // COMPILER
127 namespace base {
129 // On MSVC, use the C++11 containers.
130 #if defined(COMPILER_MSVC)
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>;
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>;
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>;
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>;
156 #else // !COMPILER_MSVC
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>;
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>;
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>;
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>;
186 #undef BASE_HASH_IMPL_NAMESPACE
188 #endif // COMPILER_MSVC
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:
196 // h32(x32, y32) = (h64(x32, y32) * rand_odd64 + rand16 * 2^16) % 2^64 / 2^32
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;
203 if (sizeof(std::size_t) >= sizeof(uint64))
204 return static_cast<std::size_t>(hash64);
206 uint64 odd_random = 481046412LL << 32 | 1025306955LL;
207 uint32 shift_random = 10121U << 16;
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;
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#SECTION00832000000000000000
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;
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);
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;
236 uint64 hash64 = product1 + product2 + product3 + product4;
238 if (sizeof(std::size_t) >= sizeof(uint64))
239 return static_cast<std::size_t>(hash64);
241 uint64 odd_random = 1578233944LL << 32 | 194370989LL;
242 uint32 shift_random = 20591U << 16;
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;
250 #define DEFINE_32BIT_PAIR_HASH(Type1, Type2) \
251 inline std::size_t HashPair(Type1 value1, Type2 value2) { \
252 return HashInts32(value1, value2); \
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);
272 #undef DEFINE_32BIT_PAIR_HASH
274 #define DEFINE_64BIT_PAIR_HASH(Type1, Type2) \
275 inline std::size_t HashPair(Type1 value1, Type2 value2) { \
276 return HashInts64(value1, value2); \
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);
300 #undef DEFINE_64BIT_PAIR_HASH
301 } // namespace base
303 namespace BASE_HASH_NAMESPACE {
305 // Implement methods for hashing a pair of integers, so they can be used as
306 // keys in STL containers.
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);
315 } // namespace BASE_HASH_NAMESPACE
317 #undef DEFINE_PAIR_HASH_FUNCTION_START
318 #undef DEFINE_PAIR_HASH_FUNCTION_END
320 #endif // BASE_CONTAINERS_HASH_TABLES_H_