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: |
11 // base::hash_map<int> my_map; | 11 // base::hash_map<int> my_map; |
12 // base::hash_set<int> my_set; | 12 // base::hash_set<int> my_set; |
13 // | 13 // |
14 // NOTE: It is an explicit non-goal of this class to provide a generic hash | 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, | 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 | 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 | 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 | 18 // because identity hashes are not desirable for all types that might show up |
19 // in containers as pointers. | 19 // in containers as pointers. |
20 | 20 |
21 #ifndef BASE_CONTAINERS_HASH_TABLES_H_ | 21 #ifndef BASE_CONTAINERS_HASH_TABLES_H_ |
22 #define BASE_CONTAINERS_HASH_TABLES_H_ | 22 #define BASE_CONTAINERS_HASH_TABLES_H_ |
23 | 23 |
| 24 #include <unordered_map> |
| 25 #include <unordered_set> |
24 #include <utility> | 26 #include <utility> |
25 | 27 |
26 #include "base/basictypes.h" | 28 #include "base/basictypes.h" |
27 #include "base/strings/string16.h" | |
28 #include "build/build_config.h" | |
29 | 29 |
30 #if defined(COMPILER_MSVC) | 30 // Deprecated. Specialize std::hash directly instead. |
31 #include <unordered_map> | 31 // TODO(davidben): Or do we want to tell people to pass in a custom hasher? |
32 #include <unordered_set> | 32 // |
33 | 33 // Things which specialize: |
| 34 // - base::trace_event::AllocationContext |
| 35 // - base::trace_event::BackTrace |
| 36 // - base::FilePath (hashes the string) |
| 37 // - base::StringPiece (hashes the string) |
| 38 // - cc::RenderPassId (custom pair-like type, defines a custom hasher, defined |
| 39 // in wrong file) |
| 40 // - cc::SharedBitmapId (array, calls base::Hash, defined in wrong file, wrong |
| 41 // module, even) |
| 42 // - cc::SurfaceId (wrapper over a uint64_t, hashes that) |
| 43 // - cc::SurfaceSequence (custom pair-like type) |
| 44 // - cc::TileMapKey (custom pair-like type with custom hash) |
| 45 // - content/renderer/pepper/v8_var_converter HashedHAndle (custom type with |
| 46 // cached hash) |
| 47 // - content::GpuMemoryBufferConfigurationKey (should be same as default) |
| 48 // - data_reduction_proxy::ConnectionType (enum) |
| 49 // - gfx::GenericSharedMemoryId (hashes int) |
| 50 // - std::pair<gfx::GenericSharedMemoryId, Second> (unnecessary) |
| 51 // - gn internal::UniquifyRef (caches the hash in a hash_val_) |
| 52 // - gn Label (recursively calls std::hash) |
| 53 // - gn LabelPtrPair (just hashes the label, also defines operator==) |
| 54 // - gn OutputFile (hashes the string) |
| 55 // - gn SourceDir (hashes the string) |
| 56 // - gn SourceFile (hashes the string) |
| 57 // - media::MultiBufferGlobalBlockId (std::pair, should be same as default one) |
| 58 // - mojo::system::ChannelEndpointId (wrapper over integer) |
| 59 // - mojo::system::UniqueIdentifier |
| 60 // - net::DnsHostsKey (std::pair, but instead sums AddressFamily and StringPiece |
| 61 // (not string?) hash) |
| 62 // - net::AlternativeService (custom thing, XORs stuff) |
| 63 // - net::QuicBlockedWriterInterface* (custom hash for a particular pointer, but |
| 64 // only in GCC. I must have missed this last time. |
| 65 // - password_manager::FacetURI |
| 66 // - policy::PolicyNamespace (custom thing, hash string XOR 1<<enum) |
| 67 // - signin AccountId (hashes just the email) |
| 68 // - sync_file_system::drive_backend::ParentIDAndTitle (custom pair) |
34 #define BASE_HASH_NAMESPACE std | 69 #define BASE_HASH_NAMESPACE std |
35 | 70 |
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 { | 71 namespace base { |
128 | 72 |
129 // On MSVC, use the C++11 containers. | 73 // Deprecated. Use std::unordered_map instead. |
130 #if defined(COMPILER_MSVC) | |
131 | |
132 template<class Key, class T, | 74 template<class Key, class T, |
133 class Hash = std::hash<Key>, | 75 class Hash = std::hash<Key>, |
134 class Pred = std::equal_to<Key>, | 76 class Pred = std::equal_to<Key>, |
135 class Alloc = std::allocator<std::pair<const Key, T>>> | 77 class Alloc = std::allocator<std::pair<const Key, T>>> |
136 using hash_map = std::unordered_map<Key, T, Hash, Pred, Alloc>; | 78 using hash_map = std::unordered_map<Key, T, Hash, Pred, Alloc>; |
137 | 79 |
| 80 // Deprecated. Use std::unordered_multimap instead. |
138 template<class Key, class T, | 81 template<class Key, class T, |
139 class Hash = std::hash<Key>, | 82 class Hash = std::hash<Key>, |
140 class Pred = std::equal_to<Key>, | 83 class Pred = std::equal_to<Key>, |
141 class Alloc = std::allocator<std::pair<const Key, T>>> | 84 class Alloc = std::allocator<std::pair<const Key, T>>> |
142 using hash_multimap = std::unordered_multimap<Key, T, Hash, Pred, Alloc>; | 85 using hash_multimap = std::unordered_multimap<Key, T, Hash, Pred, Alloc>; |
143 | 86 |
| 87 // Deprecated. Use std::unordered_multiset instead. |
144 template<class Key, | 88 template<class Key, |
145 class Hash = std::hash<Key>, | 89 class Hash = std::hash<Key>, |
146 class Pred = std::equal_to<Key>, | 90 class Pred = std::equal_to<Key>, |
147 class Alloc = std::allocator<Key>> | 91 class Alloc = std::allocator<Key>> |
148 using hash_multiset = std::unordered_multiset<Key, Hash, Pred, Alloc>; | 92 using hash_multiset = std::unordered_multiset<Key, Hash, Pred, Alloc>; |
149 | 93 |
| 94 // Deprecated. Use std::unordered_set instead. |
150 template<class Key, | 95 template<class Key, |
151 class Hash = std::hash<Key>, | 96 class Hash = std::hash<Key>, |
152 class Pred = std::equal_to<Key>, | 97 class Pred = std::equal_to<Key>, |
153 class Alloc = std::allocator<Key>> | 98 class Alloc = std::allocator<Key>> |
154 using hash_set = std::unordered_set<Key, Hash, Pred, Alloc>; | 99 using hash_set = std::unordered_set<Key, Hash, Pred, Alloc>; |
155 | 100 |
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. | 101 // 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 | 102 // 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 | 103 // multiply-add hashing. This algorithm, as described in |
193 // Theorem 4.3.3 of the thesis "Über die Komplexität der Multiplikation in | 104 // Theorem 4.3.3 of the thesis "Über die Komplexität der Multiplikation in |
194 // eingeschränkten Branchingprogrammmodellen" by Woelfel, is: | 105 // eingeschränkten Branchingprogrammmodellen" by Woelfel, is: |
195 // | 106 // |
196 // h32(x32, y32) = (h64(x32, y32) * rand_odd64 + rand16 * 2^16) % 2^64 / 2^32 | 107 // h32(x32, y32) = (h64(x32, y32) * rand_odd64 + rand16 * 2^16) % 2^64 / 2^32 |
197 // | 108 // |
198 // Contact danakj@chromium.org for any questions. | 109 // Contact danakj@chromium.org for any questions. |
199 inline std::size_t HashInts32(uint32 value1, uint32 value2) { | 110 inline std::size_t HashInts32(uint32 value1, uint32 value2) { |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
257 return HashInts32(value1, value2); | 168 return HashInts32(value1, value2); |
258 } | 169 } |
259 | 170 |
260 } // namespace base | 171 } // namespace base |
261 | 172 |
262 namespace BASE_HASH_NAMESPACE { | 173 namespace BASE_HASH_NAMESPACE { |
263 | 174 |
264 // Implement methods for hashing a pair of integers, so they can be used as | 175 // Implement methods for hashing a pair of integers, so they can be used as |
265 // keys in STL containers. | 176 // keys in STL containers. |
266 | 177 |
| 178 // TODO(davidben): This is not present in C++11. How to transition this tidily? |
| 179 // Should we keep a header around that specializes for std::pair (poor as it is |
| 180 // not the same header std::pair is defined in) or ask people to pass in the |
| 181 // third argument. I'm tempted to say the latter. |
267 template<typename Type1, typename Type2> | 182 template<typename Type1, typename Type2> |
268 struct hash<std::pair<Type1, Type2> > { | 183 struct hash<std::pair<Type1, Type2> > { |
269 std::size_t operator()(std::pair<Type1, Type2> value) const { | 184 std::size_t operator()(std::pair<Type1, Type2> value) const { |
270 return base::HashPair(value.first, value.second); | 185 return base::HashPair(value.first, value.second); |
271 } | 186 } |
272 }; | 187 }; |
273 | 188 |
274 } // namespace BASE_HASH_NAMESPACE | 189 } // namespace BASE_HASH_NAMESPACE |
275 | 190 |
276 #undef DEFINE_PAIR_HASH_FUNCTION_START | |
277 #undef DEFINE_PAIR_HASH_FUNCTION_END | |
278 | |
279 #endif // BASE_CONTAINERS_HASH_TABLES_H_ | 191 #endif // BASE_CONTAINERS_HASH_TABLES_H_ |
OLD | NEW |