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: |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
78 return static_cast<std::size_t>(value); \ | 78 return static_cast<std::size_t>(value); \ |
79 } \ | 79 } \ |
80 } | 80 } |
81 | 81 |
82 DEFINE_TRIVIAL_HASH(long long); | 82 DEFINE_TRIVIAL_HASH(long long); |
83 DEFINE_TRIVIAL_HASH(unsigned long long); | 83 DEFINE_TRIVIAL_HASH(unsigned long long); |
84 | 84 |
85 #undef DEFINE_TRIVIAL_HASH | 85 #undef DEFINE_TRIVIAL_HASH |
86 #endif // !defined(OS_ANDROID) | 86 #endif // !defined(OS_ANDROID) |
87 | 87 |
| 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 |
88 // Implement string hash functions so that strings of various flavors can | 99 // Implement string hash functions so that strings of various flavors can |
89 // be used as keys in STL maps and sets. The hash algorithm comes from the | 100 // be used as keys in STL maps and sets. The hash algorithm comes from the |
90 // GNU C++ library, in <tr1/functional>. It is duplicated here because GCC | 101 // GNU C++ library, in <tr1/functional>. It is duplicated here because GCC |
91 // versions prior to 4.3.2 are unable to compile <tr1/functional> when RTTI | 102 // versions prior to 4.3.2 are unable to compile <tr1/functional> when RTTI |
92 // is disabled, as it is in our build. | 103 // is disabled, as it is in our build. |
93 | 104 |
94 #define DEFINE_STRING_HASH(string_type) \ | 105 #define DEFINE_STRING_HASH(string_type) \ |
95 template<> \ | 106 template<> \ |
96 struct hash<string_type> { \ | 107 struct hash<string_type> { \ |
97 std::size_t operator()(const string_type& s) const { \ | 108 std::size_t operator()(const string_type& s) const { \ |
(...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
255 #else | 266 #else |
256 #error define hash<std::pair<Type1, Type2> > for your compiler | 267 #error define hash<std::pair<Type1, Type2> > for your compiler |
257 #endif // COMPILER | 268 #endif // COMPILER |
258 | 269 |
259 } | 270 } |
260 | 271 |
261 #undef DEFINE_PAIR_HASH_FUNCTION_START | 272 #undef DEFINE_PAIR_HASH_FUNCTION_START |
262 #undef DEFINE_PAIR_HASH_FUNCTION_END | 273 #undef DEFINE_PAIR_HASH_FUNCTION_END |
263 | 274 |
264 #endif // BASE_CONTAINERS_HASH_TABLES_H_ | 275 #endif // BASE_CONTAINERS_HASH_TABLES_H_ |
OLD | NEW |