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