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