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.
7 // Deal with the differences between Microsoft and GNU implemenations
8 // of hash_map. Allows all platforms to use |base::hash_map| and
11 // base::hash_map<int> my_map;
12 // base::hash_set<int> my_set;
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.
21 #ifndef BASE_HASH_TABLES_H_
22 #define BASE_HASH_TABLES_H_
24 #include "build/build_config.h"
26 #include "base/string16.h"
28 #if defined(COMPILER_MSVC)
32 #define BASE_HASH_NAMESPACE stdext
34 #elif defined(COMPILER_GCC)
35 #if defined(OS_ANDROID)
36 #define BASE_HASH_NAMESPACE std
38 #define BASE_HASH_NAMESPACE __gnu_cxx
41 // This is a hack to disable the gcc 4.4 warning about hash_map and hash_set
42 // being deprecated. We can get rid of this when we upgrade to VS2008 and we
43 // can use <tr1/unordered_map> and <tr1/unordered_set>.
45 #define CHROME_OLD__DEPRECATED __DEPRECATED
49 #if defined(OS_ANDROID)
53 #include <ext/hash_map>
54 #include <ext/hash_set>
59 #ifdef CHROME_OLD__DEPRECATED
60 #define __DEPRECATED CHROME_OLD__DEPRECATED
61 #undef CHROME_OLD__DEPRECATED
64 namespace BASE_HASH_NAMESPACE
{
66 #if !defined(OS_ANDROID)
67 // The GNU C++ library provides identity hash functions for many integral types,
68 // but not for |long long|. This hash function will truncate if |size_t| is
69 // narrower than |long long|. This is probably good enough for what we will
72 #define DEFINE_TRIVIAL_HASH(integral_type) \
74 struct hash<integral_type> { \
75 std::size_t operator()(integral_type value) const { \
76 return static_cast<std::size_t>(value); \
80 DEFINE_TRIVIAL_HASH(long long);
81 DEFINE_TRIVIAL_HASH(unsigned long long);
83 #undef DEFINE_TRIVIAL_HASH
84 #endif // !defined(OS_ANDROID)
86 // Implement string hash functions so that strings of various flavors can
87 // be used as keys in STL maps and sets. The hash algorithm comes from the
88 // GNU C++ library, in <tr1/functional>. It is duplicated here because GCC
89 // versions prior to 4.3.2 are unable to compile <tr1/functional> when RTTI
90 // is disabled, as it is in our build.
92 #define DEFINE_STRING_HASH(string_type) \
94 struct hash<string_type> { \
95 std::size_t operator()(const string_type& s) const { \
96 std::size_t result = 0; \
97 for (string_type::const_iterator i = s.begin(); i != s.end(); ++i) \
98 result = (result * 131) + *i; \
103 DEFINE_STRING_HASH(std::string
);
104 DEFINE_STRING_HASH(string16
);
106 #undef DEFINE_STRING_HASH
108 } // namespace BASE_HASH_NAMESPACE
111 #error define BASE_HASH_NAMESPACE for your compiler
115 using BASE_HASH_NAMESPACE::hash_map
;
116 using BASE_HASH_NAMESPACE::hash_multimap
;
117 using BASE_HASH_NAMESPACE::hash_multiset
;
118 using BASE_HASH_NAMESPACE::hash_set
;
121 #endif // BASE_HASH_TABLES_H_