Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(764)

Side by Side Diff: base/containers/hash_tables.h

Issue 623383002: Align base::hash_map with C++11, part 2. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@hash-1
Patch Set: clean up stale includes Created 6 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | base/containers/hash_tables_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 10 matching lines...) Expand all
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 <utility> 24 #include <utility>
25 25
26 #include "base/basictypes.h" 26 #include "base/basictypes.h"
27 #include "base/strings/string16.h" 27 #include "base/strings/string16.h"
28 #include "build/build_config.h" 28 #include "build/build_config.h"
29 29
30 #if defined(COMPILER_MSVC) 30 #if defined(COMPILER_MSVC)
31 #include <hash_map> 31 #include <unordered_map>
32 #include <hash_set> 32 #include <unordered_set>
33 33
34 #define BASE_HASH_NAMESPACE stdext 34 #define BASE_HASH_NAMESPACE std
35 35
36 #elif defined(COMPILER_GCC) 36 #elif defined(COMPILER_GCC)
37 #if defined(OS_ANDROID) 37
38 #define BASE_HASH_NAMESPACE std 38 #define BASE_HASH_NAMESPACE base_hash
39 #else
40 #define BASE_HASH_NAMESPACE __gnu_cxx
41 #endif
42 39
43 // This is a hack to disable the gcc 4.4 warning about hash_map and hash_set 40 // This is a hack to disable the gcc 4.4 warning about hash_map and hash_set
44 // being deprecated. We can get rid of this when we upgrade to VS2008 and we 41 // being deprecated. We can get rid of this when we upgrade to VS2008 and we
45 // can use <tr1/unordered_map> and <tr1/unordered_set>. 42 // can use <tr1/unordered_map> and <tr1/unordered_set>.
46 #ifdef __DEPRECATED 43 #ifdef __DEPRECATED
47 #define CHROME_OLD__DEPRECATED __DEPRECATED 44 #define CHROME_OLD__DEPRECATED __DEPRECATED
48 #undef __DEPRECATED 45 #undef __DEPRECATED
49 #endif 46 #endif
50 47
51 #if defined(OS_ANDROID) 48 #if defined(OS_ANDROID)
52 #include <hash_map> 49 #include <hash_map>
53 #include <hash_set> 50 #include <hash_set>
51 #define BASE_HASH_IMPL_NAMESPACE std
54 #else 52 #else
55 #include <ext/hash_map> 53 #include <ext/hash_map>
56 #include <ext/hash_set> 54 #include <ext/hash_set>
55 #define BASE_HASH_IMPL_NAMESPACE __gnu_cxx
57 #endif 56 #endif
58 57
59 #include <string> 58 #include <string>
60 59
61 #ifdef CHROME_OLD__DEPRECATED 60 #ifdef CHROME_OLD__DEPRECATED
62 #define __DEPRECATED CHROME_OLD__DEPRECATED 61 #define __DEPRECATED CHROME_OLD__DEPRECATED
63 #undef CHROME_OLD__DEPRECATED 62 #undef CHROME_OLD__DEPRECATED
64 #endif 63 #endif
65 64
66 namespace BASE_HASH_NAMESPACE { 65 namespace BASE_HASH_NAMESPACE {
67 66
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.
71
72 template<typename T>
73 struct hash {
74 std::size_t operator()(const T& value) const {
75 return BASE_HASH_IMPL_NAMESPACE::hash<T>()(value);
76 }
77 };
78
79 template<typename T>
80 struct hash<T*> {
81 std::size_t operator()(T* value) const {
82 return BASE_HASH_IMPL_NAMESPACE::hash<uintptr_t>()(
83 reinterpret_cast<uintptr_t>(value));
84 }
85 };
86
68 #if !defined(OS_ANDROID) 87 #if !defined(OS_ANDROID)
69 // The GNU C++ library provides identity hash functions for many integral types, 88 // The GNU C++ library provides identity hash functions for many integral types,
70 // but not for |long long|. This hash function will truncate if |size_t| is 89 // but not for |long long|. This hash function will truncate if |size_t| is
71 // narrower than |long long|. This is probably good enough for what we will 90 // narrower than |long long|. This is probably good enough for what we will
72 // use it for. 91 // use it for.
73 92
74 #define DEFINE_TRIVIAL_HASH(integral_type) \ 93 #define DEFINE_TRIVIAL_HASH(integral_type) \
75 template<> \ 94 template<> \
76 struct hash<integral_type> { \ 95 struct hash<integral_type> { \
77 std::size_t operator()(integral_type value) const { \ 96 std::size_t operator()(integral_type value) const { \
78 return static_cast<std::size_t>(value); \ 97 return static_cast<std::size_t>(value); \
79 } \ 98 } \
80 } 99 }
81 100
82 DEFINE_TRIVIAL_HASH(long long); 101 DEFINE_TRIVIAL_HASH(long long);
83 DEFINE_TRIVIAL_HASH(unsigned long long); 102 DEFINE_TRIVIAL_HASH(unsigned long long);
84 103
85 #undef DEFINE_TRIVIAL_HASH 104 #undef DEFINE_TRIVIAL_HASH
86 #endif // !defined(OS_ANDROID) 105 #endif // !defined(OS_ANDROID)
87 106
88 // To align with C++11's std::hash and MSVC's pre-standard stdext::hash_value,
89 // provide a default hash function for raw pointers. Note: const char * is still
90 // specialized to hash as a C string. This is consistent with the currently used
91 // stdext::hash_value, but not C++11.
92 template<typename T>
93 struct hash<T*> {
94 std::size_t operator()(T* value) const {
95 return hash<uintptr_t>()(reinterpret_cast<uintptr_t>(value));
96 }
97 };
98
99 // Implement string hash functions so that strings of various flavors can 107 // 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 108 // 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 109 // 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 110 // versions prior to 4.3.2 are unable to compile <tr1/functional> when RTTI
103 // is disabled, as it is in our build. 111 // is disabled, as it is in our build.
104 112
105 #define DEFINE_STRING_HASH(string_type) \ 113 #define DEFINE_STRING_HASH(string_type) \
106 template<> \ 114 template<> \
107 struct hash<string_type> { \ 115 struct hash<string_type> { \
108 std::size_t operator()(const string_type& s) const { \ 116 std::size_t operator()(const string_type& s) const { \
109 std::size_t result = 0; \ 117 std::size_t result = 0; \
110 for (string_type::const_iterator i = s.begin(); i != s.end(); ++i) \ 118 for (string_type::const_iterator i = s.begin(); i != s.end(); ++i) \
111 result = (result * 131) + *i; \ 119 result = (result * 131) + *i; \
112 return result; \ 120 return result; \
113 } \ 121 } \
114 } 122 }
115 123
116 DEFINE_STRING_HASH(std::string); 124 DEFINE_STRING_HASH(std::string);
117 DEFINE_STRING_HASH(base::string16); 125 DEFINE_STRING_HASH(base::string16);
118 126
119 #undef DEFINE_STRING_HASH 127 #undef DEFINE_STRING_HASH
120 128
121 } // namespace BASE_HASH_NAMESPACE 129 } // namespace BASE_HASH_NAMESPACE
122 130
123 #else // COMPILER 131 #else // COMPILER
124 #error define BASE_HASH_NAMESPACE for your compiler 132 #error define BASE_HASH_NAMESPACE for your compiler
125 #endif // COMPILER 133 #endif // COMPILER
126 134
127 namespace base { 135 namespace base {
128 using BASE_HASH_NAMESPACE::hash_map; 136
129 using BASE_HASH_NAMESPACE::hash_multimap; 137 // On MSVC, use the C++11 containers.
130 using BASE_HASH_NAMESPACE::hash_multiset; 138 #if defined(COMPILER_MSVC)
131 using BASE_HASH_NAMESPACE::hash_set; 139
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>;
145
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>;
151
152 template<class Key,
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>;
157
158 template<class Key,
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>;
163
164 #else // !COMPILER_MSVC
165
166 // Otherwise, use the pre-standard ones, but override the default hash to match
167 // C++11.
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>;
173
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>;
180
181 template<class Key,
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>;
187
188 template<class Key,
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>;
193
194 #undef BASE_HASH_IMPL_NAMESPACE
195
196 #endif // COMPILER_MSVC
132 197
133 // Implement hashing for pairs of at-most 32 bit integer values. 198 // Implement hashing for pairs of at-most 32 bit integer values.
134 // When size_t is 32 bits, we turn the 64-bit hash code into 32 bits by using 199 // When size_t is 32 bits, we turn the 64-bit hash code into 32 bits by using
135 // multiply-add hashing. This algorithm, as described in 200 // multiply-add hashing. This algorithm, as described in
136 // Theorem 4.3.3 of the thesis "Über die Komplexität der Multiplikation in 201 // Theorem 4.3.3 of the thesis "Über die Komplexität der Multiplikation in
137 // eingeschränkten Branchingprogrammmodellen" by Woelfel, is: 202 // eingeschränkten Branchingprogrammmodellen" by Woelfel, is:
138 // 203 //
139 // h32(x32, y32) = (h64(x32, y32) * rand_odd64 + rand16 * 2^16) % 2^64 / 2^32 204 // h32(x32, y32) = (h64(x32, y32) * rand_odd64 + rand16 * 2^16) % 2^64 / 2^32
140 // 205 //
141 // Contact danakj@chromium.org for any questions. 206 // Contact danakj@chromium.org for any questions.
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after
241 DEFINE_64BIT_PAIR_HASH(uint64, uint64); 306 DEFINE_64BIT_PAIR_HASH(uint64, uint64);
242 307
243 #undef DEFINE_64BIT_PAIR_HASH 308 #undef DEFINE_64BIT_PAIR_HASH
244 } // namespace base 309 } // namespace base
245 310
246 namespace BASE_HASH_NAMESPACE { 311 namespace BASE_HASH_NAMESPACE {
247 312
248 // Implement methods for hashing a pair of integers, so they can be used as 313 // Implement methods for hashing a pair of integers, so they can be used as
249 // keys in STL containers. 314 // keys in STL containers.
250 315
251 #if defined(COMPILER_MSVC)
252
253 template<typename Type1, typename Type2>
254 inline std::size_t hash_value(const std::pair<Type1, Type2>& value) {
255 return base::HashPair(value.first, value.second);
256 }
257
258 #elif defined(COMPILER_GCC)
259 template<typename Type1, typename Type2> 316 template<typename Type1, typename Type2>
260 struct hash<std::pair<Type1, Type2> > { 317 struct hash<std::pair<Type1, Type2> > {
261 std::size_t operator()(std::pair<Type1, Type2> value) const { 318 std::size_t operator()(std::pair<Type1, Type2> value) const {
262 return base::HashPair(value.first, value.second); 319 return base::HashPair(value.first, value.second);
263 } 320 }
264 }; 321 };
265 322
266 #else
267 #error define hash<std::pair<Type1, Type2> > for your compiler
268 #endif // COMPILER
269
270 } 323 }
271 324
272 #undef DEFINE_PAIR_HASH_FUNCTION_START 325 #undef DEFINE_PAIR_HASH_FUNCTION_START
273 #undef DEFINE_PAIR_HASH_FUNCTION_END 326 #undef DEFINE_PAIR_HASH_FUNCTION_END
274 327
275 #endif // BASE_CONTAINERS_HASH_TABLES_H_ 328 #endif // BASE_CONTAINERS_HASH_TABLES_H_
OLDNEW
« no previous file with comments | « no previous file | base/containers/hash_tables_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698