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 16 matching lines...) Expand all Loading... | |
27 #include "base/string16.h" | 27 #include "base/string16.h" |
28 | 28 |
29 #if defined(COMPILER_MSVC) | 29 #if defined(COMPILER_MSVC) |
30 #include <hash_map> | 30 #include <hash_map> |
31 #include <hash_set> | 31 #include <hash_set> |
32 namespace base { | 32 namespace base { |
33 using stdext::hash_map; | 33 using stdext::hash_map; |
34 using stdext::hash_set; | 34 using stdext::hash_set; |
35 } | 35 } |
36 #elif defined(COMPILER_GCC) | 36 #elif defined(COMPILER_GCC) |
37 #if defined(OS_ANDROID) | |
38 #define HASH_NAMESPACE std | |
darin (slow to review)
2011/06/24 16:46:05
since this is a global #define, you should probabl
michaelbai
2011/06/24 18:51:30
Done.
| |
39 #else | |
40 #define HASH_NAMESPACE __gnu_cxx | |
41 #endif | |
42 | |
37 // This is a hack to disable the gcc 4.4 warning about hash_map and hash_set | 43 // This is a hack to disable the gcc 4.4 warning about hash_map and hash_set |
38 // being deprecated. We can get rid of this when we upgrade to VS2008 and we | 44 // being deprecated. We can get rid of this when we upgrade to VS2008 and we |
39 // can use <tr1/unordered_map> and <tr1/unordered_set>. | 45 // can use <tr1/unordered_map> and <tr1/unordered_set>. |
40 #ifdef __DEPRECATED | 46 #ifdef __DEPRECATED |
41 #define CHROME_OLD__DEPRECATED __DEPRECATED | 47 #define CHROME_OLD__DEPRECATED __DEPRECATED |
42 #undef __DEPRECATED | 48 #undef __DEPRECATED |
43 #endif | 49 #endif |
44 | 50 |
51 #if defined(OS_ANDROID) | |
52 #include <hash_map> | |
53 #include <hash_set> | |
54 #else | |
45 #include <ext/hash_map> | 55 #include <ext/hash_map> |
46 #include <ext/hash_set> | 56 #include <ext/hash_set> |
57 #endif | |
58 | |
47 #include <string> | 59 #include <string> |
48 | 60 |
49 #ifdef CHROME_OLD__DEPRECATED | 61 #ifdef CHROME_OLD__DEPRECATED |
50 #define __DEPRECATED CHROME_OLD__DEPRECATED | 62 #define __DEPRECATED CHROME_OLD__DEPRECATED |
51 #undef CHROME_OLD__DEPRECATED | 63 #undef CHROME_OLD__DEPRECATED |
52 #endif | 64 #endif |
53 | 65 |
54 namespace base { | 66 namespace base { |
55 using __gnu_cxx::hash_map; | 67 using HASH_NAMESPACE::hash_map; |
56 using __gnu_cxx::hash_set; | 68 using HASH_NAMESPACE::hash_set; |
57 } // namespace base | 69 } // namespace base |
58 | 70 |
59 namespace __gnu_cxx { | 71 namespace HASH_NAMESPACE { |
60 | 72 |
73 #if !defined(OS_ANDROID) | |
61 // The GNU C++ library provides identity hash functions for many integral types, | 74 // The GNU C++ library provides identity hash functions for many integral types, |
62 // but not for |long long|. This hash function will truncate if |size_t| is | 75 // but not for |long long|. This hash function will truncate if |size_t| is |
63 // narrower than |long long|. This is probably good enough for what we will | 76 // narrower than |long long|. This is probably good enough for what we will |
64 // use it for. | 77 // use it for. |
65 | 78 |
66 #define DEFINE_TRIVIAL_HASH(integral_type) \ | 79 #define DEFINE_TRIVIAL_HASH(integral_type) \ |
67 template<> \ | 80 template<> \ |
68 struct hash<integral_type> { \ | 81 struct hash<integral_type> { \ |
69 std::size_t operator()(integral_type value) const { \ | 82 std::size_t operator()(integral_type value) const { \ |
70 return static_cast<std::size_t>(value); \ | 83 return static_cast<std::size_t>(value); \ |
71 } \ | 84 } \ |
72 } | 85 } |
73 | 86 |
74 DEFINE_TRIVIAL_HASH(long long); | 87 DEFINE_TRIVIAL_HASH(long long); |
75 DEFINE_TRIVIAL_HASH(unsigned long long); | 88 DEFINE_TRIVIAL_HASH(unsigned long long); |
76 | 89 |
77 #undef DEFINE_TRIVIAL_HASH | 90 #undef DEFINE_TRIVIAL_HASH |
91 #endif // !defined(OS_ANDROID) | |
78 | 92 |
79 // Implement string hash functions so that strings of various flavors can | 93 // Implement string hash functions so that strings of various flavors can |
80 // be used as keys in STL maps and sets. The hash algorithm comes from the | 94 // be used as keys in STL maps and sets. The hash algorithm comes from the |
81 // GNU C++ library, in <tr1/functional>. It is duplicated here because GCC | 95 // GNU C++ library, in <tr1/functional>. It is duplicated here because GCC |
82 // versions prior to 4.3.2 are unable to compile <tr1/functional> when RTTI | 96 // versions prior to 4.3.2 are unable to compile <tr1/functional> when RTTI |
83 // is disabled, as it is in our build. | 97 // is disabled, as it is in our build. |
84 | 98 |
85 #define DEFINE_STRING_HASH(string_type) \ | 99 #define DEFINE_STRING_HASH(string_type) \ |
86 template<> \ | 100 template<> \ |
87 struct hash<string_type> { \ | 101 struct hash<string_type> { \ |
88 std::size_t operator()(const string_type& s) const { \ | 102 std::size_t operator()(const string_type& s) const { \ |
89 std::size_t result = 0; \ | 103 std::size_t result = 0; \ |
90 for (string_type::const_iterator i = s.begin(); i != s.end(); ++i) \ | 104 for (string_type::const_iterator i = s.begin(); i != s.end(); ++i) \ |
91 result = (result * 131) + *i; \ | 105 result = (result * 131) + *i; \ |
92 return result; \ | 106 return result; \ |
93 } \ | 107 } \ |
94 } | 108 } |
95 | 109 |
96 DEFINE_STRING_HASH(std::string); | 110 DEFINE_STRING_HASH(std::string); |
97 DEFINE_STRING_HASH(string16); | 111 DEFINE_STRING_HASH(string16); |
98 | 112 |
99 #undef DEFINE_STRING_HASH | 113 #undef DEFINE_STRING_HASH |
100 | 114 |
101 } // namespace __gnu_cxx | 115 } // namespace HASH_NAMESPACE |
102 | 116 |
103 #endif // COMPILER | 117 #endif // COMPILER |
104 | 118 |
105 #endif // BASE_HASH_TABLES_H_ | 119 #endif // BASE_HASH_TABLES_H_ |
OLD | NEW |