blob: b91d5d48bb6f4db333760087986de34e5bde84eb [file] [log] [blame]
evan@chromium.org4fbb9b22011-05-17 08:40:05 +09001// Copyright (c) 2011 The Chromium Authors. All rights reserved.
license.botf003cfe2008-08-24 09:55:55 +09002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit3f4a7322008-07-27 06:49:38 +09004//
pinkerton@google.com3f807de2008-08-05 03:36:53 +09005
6//
initial.commit3f4a7322008-07-27 06:49:38 +09007// Deal with the differences between Microsoft and GNU implemenations
pinkerton@google.com3f807de2008-08-05 03:36:53 +09008// of hash_map. Allows all platforms to use |base::hash_map| and
9// |base::hash_set|.
maruel@chromium.org8fe7adc2009-03-04 00:01:12 +090010// eg:
pinkerton@google.com3f807de2008-08-05 03:36:53 +090011// base::hash_map<int> my_map;
12// base::hash_set<int> my_set;
initial.commit3f4a7322008-07-27 06:49:38 +090013//
jorlow@chromium.org79931a92009-09-03 14:48:45 +090014// 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.
initial.commit3f4a7322008-07-27 06:49:38 +090020
mark@chromium.org6211bbd2009-08-13 21:53:16 +090021#ifndef BASE_HASH_TABLES_H_
22#define BASE_HASH_TABLES_H_
thakis@chromium.org01d14522010-07-27 08:08:24 +090023#pragma once
initial.commit3f4a7322008-07-27 06:49:38 +090024
pinkerton@google.come3eb63f2008-08-06 23:36:32 +090025#include "build/build_config.h"
26
jorlow@chromium.org6c836882009-07-24 10:20:08 +090027#include "base/string16.h"
28
pinkerton@google.come3eb63f2008-08-06 23:36:32 +090029#if defined(COMPILER_MSVC)
initial.commit3f4a7322008-07-27 06:49:38 +090030#include <hash_map>
31#include <hash_set>
michaelbai@google.combd0b2452011-06-28 04:13:10 +090032
33#define BASE_HASH_NAMESPACE stdext
34
pinkerton@google.come3eb63f2008-08-06 23:36:32 +090035#elif defined(COMPILER_GCC)
michaelbai@google.combd0b2452011-06-28 04:13:10 +090036#if defined(OS_ANDROID)
37#define BASE_HASH_NAMESPACE std
38#else
39#define BASE_HASH_NAMESPACE __gnu_cxx
40#endif
41
tc@google.comf4a5e442009-04-14 01:59:03 +090042// This is a hack to disable the gcc 4.4 warning about hash_map and hash_set
43// being deprecated. We can get rid of this when we upgrade to VS2008 and we
44// can use <tr1/unordered_map> and <tr1/unordered_set>.
45#ifdef __DEPRECATED
46#define CHROME_OLD__DEPRECATED __DEPRECATED
47#undef __DEPRECATED
48#endif
49
michaelbai@google.combd0b2452011-06-28 04:13:10 +090050#if defined(OS_ANDROID)
51#include <hash_map>
52#include <hash_set>
53#else
initial.commit3f4a7322008-07-27 06:49:38 +090054#include <ext/hash_map>
55#include <ext/hash_set>
michaelbai@google.combd0b2452011-06-28 04:13:10 +090056#endif
57
mark@chromium.org6211bbd2009-08-13 21:53:16 +090058#include <string>
tc@google.comf4a5e442009-04-14 01:59:03 +090059
60#ifdef CHROME_OLD__DEPRECATED
61#define __DEPRECATED CHROME_OLD__DEPRECATED
62#undef CHROME_OLD__DEPRECATED
63#endif
64
michaelbai@google.combd0b2452011-06-28 04:13:10 +090065namespace BASE_HASH_NAMESPACE {
pinkerton@google.com3f807de2008-08-05 03:36:53 +090066
michaelbai@google.combd0b2452011-06-28 04:13:10 +090067#if !defined(OS_ANDROID)
dilmah@chromium.org4dba82d2010-12-01 22:08:56 +090068// The GNU C++ library provides identity hash functions for many integral types,
mark@chromium.org6211bbd2009-08-13 21:53:16 +090069// but not for |long long|. This hash function will truncate if |size_t| is
70// narrower than |long long|. This is probably good enough for what we will
71// use it for.
pinkerton@google.com3f807de2008-08-05 03:36:53 +090072
mark@chromium.org6211bbd2009-08-13 21:53:16 +090073#define DEFINE_TRIVIAL_HASH(integral_type) \
74 template<> \
75 struct hash<integral_type> { \
76 std::size_t operator()(integral_type value) const { \
77 return static_cast<std::size_t>(value); \
78 } \
79 }
pinkerton@google.com3f807de2008-08-05 03:36:53 +090080
mark@chromium.org6211bbd2009-08-13 21:53:16 +090081DEFINE_TRIVIAL_HASH(long long);
82DEFINE_TRIVIAL_HASH(unsigned long long);
pinkerton@google.com3f807de2008-08-05 03:36:53 +090083
mark@chromium.org6211bbd2009-08-13 21:53:16 +090084#undef DEFINE_TRIVIAL_HASH
michaelbai@google.combd0b2452011-06-28 04:13:10 +090085#endif // !defined(OS_ANDROID)
pinkerton@google.com3f807de2008-08-05 03:36:53 +090086
mark@chromium.org6211bbd2009-08-13 21:53:16 +090087// Implement string hash functions so that strings of various flavors can
88// be used as keys in STL maps and sets. The hash algorithm comes from the
89// GNU C++ library, in <tr1/functional>. It is duplicated here because GCC
90// versions prior to 4.3.2 are unable to compile <tr1/functional> when RTTI
91// is disabled, as it is in our build.
pinkerton@google.com3f807de2008-08-05 03:36:53 +090092
mark@chromium.org6211bbd2009-08-13 21:53:16 +090093#define DEFINE_STRING_HASH(string_type) \
94 template<> \
95 struct hash<string_type> { \
96 std::size_t operator()(const string_type& s) const { \
97 std::size_t result = 0; \
98 for (string_type::const_iterator i = s.begin(); i != s.end(); ++i) \
99 result = (result * 131) + *i; \
100 return result; \
101 } \
102 }
estade@chromium.orgaa1f5dc2009-02-22 10:21:56 +0900103
mark@chromium.org6211bbd2009-08-13 21:53:16 +0900104DEFINE_STRING_HASH(std::string);
mark@chromium.org6211bbd2009-08-13 21:53:16 +0900105DEFINE_STRING_HASH(string16);
jorlow@chromium.org6c836882009-07-24 10:20:08 +0900106
mark@chromium.org6211bbd2009-08-13 21:53:16 +0900107#undef DEFINE_STRING_HASH
jorlow@chromium.org6c836882009-07-24 10:20:08 +0900108
michaelbai@google.combd0b2452011-06-28 04:13:10 +0900109} // namespace BASE_HASH_NAMESPACE
initial.commit3f4a7322008-07-27 06:49:38 +0900110
michaelbai@google.combd0b2452011-06-28 04:13:10 +0900111#else // COMPILER
112#error define BASE_HASH_NAMESPACE for your compiler
mark@chromium.org6211bbd2009-08-13 21:53:16 +0900113#endif // COMPILER
initial.commit3f4a7322008-07-27 06:49:38 +0900114
michaelbai@google.combd0b2452011-06-28 04:13:10 +0900115namespace base {
116using BASE_HASH_NAMESPACE::hash_map;
117using BASE_HASH_NAMESPACE::hash_set;
118}
119
mark@chromium.org6211bbd2009-08-13 21:53:16 +0900120#endif // BASE_HASH_TABLES_H_