michael@0: // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. michael@0: // Use of this source code is governed by a BSD-style license that can be michael@0: // found in the LICENSE file. michael@0: // michael@0: michael@0: // michael@0: // Deal with the differences between Microsoft and GNU implemenations michael@0: // of hash_map. Allows all platforms to use |base::hash_map| and michael@0: // |base::hash_set|. michael@0: // eg: michael@0: // base::hash_map my_map; michael@0: // base::hash_set my_set; michael@0: // michael@0: michael@0: #ifndef BASE_HASH_TABLES_H_ michael@0: #define BASE_HASH_TABLES_H_ michael@0: michael@0: #include "build/build_config.h" michael@0: michael@0: #include "base/string16.h" michael@0: michael@0: #if defined(COMPILER_MSVC) || (defined(ANDROID) && defined(_STLP_STD_NAME)) michael@0: #include michael@0: #include michael@0: namespace base { michael@0: #ifdef ANDROID michael@0: using _STLP_STD_NAME::hash_map; michael@0: using _STLP_STD_NAME::hash_set; michael@0: #else michael@0: using stdext::hash_map; michael@0: using stdext::hash_set; michael@0: #endif michael@0: } michael@0: #elif defined(COMPILER_GCC) michael@0: // This is a hack to disable the gcc 4.4 warning about hash_map and hash_set michael@0: // being deprecated. We can get rid of this when we upgrade to VS2008 and we michael@0: // can use and . michael@0: #ifdef __DEPRECATED michael@0: #define CHROME_OLD__DEPRECATED __DEPRECATED michael@0: #undef __DEPRECATED michael@0: #endif michael@0: michael@0: #include michael@0: #include michael@0: #include michael@0: michael@0: #ifdef CHROME_OLD__DEPRECATED michael@0: #define __DEPRECATED CHROME_OLD__DEPRECATED michael@0: #undef CHROME_OLD__DEPRECATED michael@0: #endif michael@0: michael@0: namespace base { michael@0: using __gnu_cxx::hash_map; michael@0: using __gnu_cxx::hash_set; michael@0: } // namespace base michael@0: michael@0: namespace __gnu_cxx { michael@0: michael@0: // The GNU C++ library provides identiy hash functions for many integral types, michael@0: // but not for |long long|. This hash function will truncate if |size_t| is michael@0: // narrower than |long long|. This is probably good enough for what we will michael@0: // use it for. michael@0: michael@0: #define DEFINE_TRIVIAL_HASH(integral_type) \ michael@0: template<> \ michael@0: struct hash { \ michael@0: std::size_t operator()(integral_type value) const { \ michael@0: return static_cast(value); \ michael@0: } \ michael@0: } michael@0: michael@0: DEFINE_TRIVIAL_HASH(long long); michael@0: DEFINE_TRIVIAL_HASH(unsigned long long); michael@0: michael@0: #undef DEFINE_TRIVIAL_HASH michael@0: michael@0: // Implement string hash functions so that strings of various flavors can michael@0: // be used as keys in STL maps and sets. The hash algorithm comes from the michael@0: // GNU C++ library, in . It is duplicated here because GCC michael@0: // versions prior to 4.3.2 are unable to compile when RTTI michael@0: // is disabled, as it is in our build. michael@0: michael@0: #define DEFINE_STRING_HASH(string_type) \ michael@0: template<> \ michael@0: struct hash { \ michael@0: std::size_t operator()(const string_type& s) const { \ michael@0: std::size_t result = 0; \ michael@0: for (string_type::const_iterator i = s.begin(); i != s.end(); ++i) \ michael@0: result = (result * 131) + *i; \ michael@0: return result; \ michael@0: } \ michael@0: } michael@0: michael@0: DEFINE_STRING_HASH(std::string); michael@0: DEFINE_STRING_HASH(std::wstring); michael@0: michael@0: #if defined(WCHAR_T_IS_UTF32) michael@0: // If string16 and std::wstring are not the same type, provide a michael@0: // specialization for string16. michael@0: DEFINE_STRING_HASH(string16); michael@0: #endif // WCHAR_T_IS_UTF32 michael@0: michael@0: #undef DEFINE_STRING_HASH michael@0: michael@0: } // namespace __gnu_cxx michael@0: michael@0: #endif // COMPILER michael@0: michael@0: #endif // BASE_HASH_TABLES_H_