[email protected] | a7fa0c61 | 2013-06-10 17:50:33 | [diff] [blame] | 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 | // |
| 5 | |
| 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; |
| 13 | // |
| 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. |
| 20 | |
| 21 | #ifndef BASE_CONTAINERS_HASH_TABLES_H_ |
| 22 | #define BASE_CONTAINERS_HASH_TABLES_H_ |
| 23 | |
[email protected] | 251cd6e5 | 2013-06-11 13:36:37 | [diff] [blame^] | 24 | #include "base/strings/string16.h" |
[email protected] | a7fa0c61 | 2013-06-10 17:50:33 | [diff] [blame] | 25 | #include "build/build_config.h" |
| 26 | |
| 27 | #if defined(COMPILER_MSVC) |
| 28 | #include <hash_map> |
| 29 | #include <hash_set> |
| 30 | |
| 31 | #define BASE_HASH_NAMESPACE stdext |
| 32 | |
| 33 | #elif defined(COMPILER_GCC) |
| 34 | #if defined(OS_ANDROID) |
| 35 | #define BASE_HASH_NAMESPACE std |
| 36 | #else |
| 37 | #define BASE_HASH_NAMESPACE __gnu_cxx |
| 38 | #endif |
| 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 | #if defined(OS_ANDROID) |
| 49 | #include <hash_map> |
| 50 | #include <hash_set> |
| 51 | #else |
| 52 | #include <ext/hash_map> |
| 53 | #include <ext/hash_set> |
| 54 | #endif |
| 55 | |
| 56 | #include <string> |
| 57 | |
| 58 | #ifdef CHROME_OLD__DEPRECATED |
| 59 | #define __DEPRECATED CHROME_OLD__DEPRECATED |
| 60 | #undef CHROME_OLD__DEPRECATED |
| 61 | #endif |
| 62 | |
| 63 | namespace BASE_HASH_NAMESPACE { |
| 64 | |
| 65 | #if !defined(OS_ANDROID) |
| 66 | // The GNU C++ library provides identity hash functions for many integral types, |
| 67 | // but not for |long long|. This hash function will truncate if |size_t| is |
| 68 | // narrower than |long long|. This is probably good enough for what we will |
| 69 | // use it for. |
| 70 | |
| 71 | #define DEFINE_TRIVIAL_HASH(integral_type) \ |
| 72 | template<> \ |
| 73 | struct hash<integral_type> { \ |
| 74 | std::size_t operator()(integral_type value) const { \ |
| 75 | return static_cast<std::size_t>(value); \ |
| 76 | } \ |
| 77 | } |
| 78 | |
| 79 | DEFINE_TRIVIAL_HASH(long long); |
| 80 | DEFINE_TRIVIAL_HASH(unsigned long long); |
| 81 | |
| 82 | #undef DEFINE_TRIVIAL_HASH |
| 83 | #endif // !defined(OS_ANDROID) |
| 84 | |
| 85 | // Implement string hash functions so that strings of various flavors can |
| 86 | // be used as keys in STL maps and sets. The hash algorithm comes from the |
| 87 | // GNU C++ library, in <tr1/functional>. It is duplicated here because GCC |
| 88 | // versions prior to 4.3.2 are unable to compile <tr1/functional> when RTTI |
| 89 | // is disabled, as it is in our build. |
| 90 | |
| 91 | #define DEFINE_STRING_HASH(string_type) \ |
| 92 | template<> \ |
| 93 | struct hash<string_type> { \ |
| 94 | std::size_t operator()(const string_type& s) const { \ |
| 95 | std::size_t result = 0; \ |
| 96 | for (string_type::const_iterator i = s.begin(); i != s.end(); ++i) \ |
| 97 | result = (result * 131) + *i; \ |
| 98 | return result; \ |
| 99 | } \ |
| 100 | } |
| 101 | |
| 102 | DEFINE_STRING_HASH(std::string); |
| 103 | DEFINE_STRING_HASH(string16); |
| 104 | |
| 105 | #undef DEFINE_STRING_HASH |
| 106 | |
| 107 | } // namespace BASE_HASH_NAMESPACE |
| 108 | |
| 109 | #else // COMPILER |
| 110 | #error define BASE_HASH_NAMESPACE for your compiler |
| 111 | #endif // COMPILER |
| 112 | |
| 113 | namespace base { |
| 114 | using BASE_HASH_NAMESPACE::hash_map; |
| 115 | using BASE_HASH_NAMESPACE::hash_multimap; |
| 116 | using BASE_HASH_NAMESPACE::hash_multiset; |
| 117 | using BASE_HASH_NAMESPACE::hash_set; |
| 118 | } |
| 119 | |
| 120 | #endif // BASE_CONTAINERS_HASH_TABLES_H_ |