evan@chromium.org | 4fbb9b2 | 2011-05-17 08:40:05 +0900 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
license.bot | f003cfe | 2008-08-24 09:55:55 +0900 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 4 | // |
pinkerton@google.com | 3f807de | 2008-08-05 03:36:53 +0900 | [diff] [blame] | 5 | |
| 6 | // |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 7 | // Deal with the differences between Microsoft and GNU implemenations |
pinkerton@google.com | 3f807de | 2008-08-05 03:36:53 +0900 | [diff] [blame] | 8 | // of hash_map. Allows all platforms to use |base::hash_map| and |
| 9 | // |base::hash_set|. |
maruel@chromium.org | 8fe7adc | 2009-03-04 00:01:12 +0900 | [diff] [blame] | 10 | // eg: |
pinkerton@google.com | 3f807de | 2008-08-05 03:36:53 +0900 | [diff] [blame] | 11 | // base::hash_map<int> my_map; |
| 12 | // base::hash_set<int> my_set; |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 13 | // |
jorlow@chromium.org | 79931a9 | 2009-09-03 14:48:45 +0900 | [diff] [blame] | 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. |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 20 | |
mark@chromium.org | 6211bbd | 2009-08-13 21:53:16 +0900 | [diff] [blame] | 21 | #ifndef BASE_HASH_TABLES_H_ |
| 22 | #define BASE_HASH_TABLES_H_ |
thakis@chromium.org | 01d1452 | 2010-07-27 08:08:24 +0900 | [diff] [blame] | 23 | #pragma once |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 24 | |
pinkerton@google.com | e3eb63f | 2008-08-06 23:36:32 +0900 | [diff] [blame] | 25 | #include "build/build_config.h" |
| 26 | |
jorlow@chromium.org | 6c83688 | 2009-07-24 10:20:08 +0900 | [diff] [blame] | 27 | #include "base/string16.h" |
| 28 | |
pinkerton@google.com | e3eb63f | 2008-08-06 23:36:32 +0900 | [diff] [blame] | 29 | #if defined(COMPILER_MSVC) |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 30 | #include <hash_map> |
| 31 | #include <hash_set> |
| 32 | namespace base { |
pinkerton@google.com | 3f807de | 2008-08-05 03:36:53 +0900 | [diff] [blame] | 33 | using stdext::hash_map; |
| 34 | using stdext::hash_set; |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 35 | } |
pinkerton@google.com | e3eb63f | 2008-08-06 23:36:32 +0900 | [diff] [blame] | 36 | #elif defined(COMPILER_GCC) |
tc@google.com | f4a5e44 | 2009-04-14 01:59:03 +0900 | [diff] [blame] | 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 | |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 45 | #include <ext/hash_map> |
| 46 | #include <ext/hash_set> |
mark@chromium.org | 6211bbd | 2009-08-13 21:53:16 +0900 | [diff] [blame] | 47 | #include <string> |
tc@google.com | f4a5e44 | 2009-04-14 01:59:03 +0900 | [diff] [blame] | 48 | |
| 49 | #ifdef CHROME_OLD__DEPRECATED |
| 50 | #define __DEPRECATED CHROME_OLD__DEPRECATED |
| 51 | #undef CHROME_OLD__DEPRECATED |
| 52 | #endif |
| 53 | |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 54 | namespace base { |
pinkerton@google.com | 3f807de | 2008-08-05 03:36:53 +0900 | [diff] [blame] | 55 | using __gnu_cxx::hash_map; |
| 56 | using __gnu_cxx::hash_set; |
mark@chromium.org | 6211bbd | 2009-08-13 21:53:16 +0900 | [diff] [blame] | 57 | } // namespace base |
pinkerton@google.com | 3f807de | 2008-08-05 03:36:53 +0900 | [diff] [blame] | 58 | |
pinkerton@google.com | 3f807de | 2008-08-05 03:36:53 +0900 | [diff] [blame] | 59 | namespace __gnu_cxx { |
| 60 | |
dilmah@chromium.org | 4dba82d | 2010-12-01 22:08:56 +0900 | [diff] [blame] | 61 | // The GNU C++ library provides identity hash functions for many integral types, |
mark@chromium.org | 6211bbd | 2009-08-13 21:53:16 +0900 | [diff] [blame] | 62 | // but not for |long long|. This hash function will truncate if |size_t| is |
| 63 | // narrower than |long long|. This is probably good enough for what we will |
| 64 | // use it for. |
pinkerton@google.com | 3f807de | 2008-08-05 03:36:53 +0900 | [diff] [blame] | 65 | |
mark@chromium.org | 6211bbd | 2009-08-13 21:53:16 +0900 | [diff] [blame] | 66 | #define DEFINE_TRIVIAL_HASH(integral_type) \ |
| 67 | template<> \ |
| 68 | struct hash<integral_type> { \ |
| 69 | std::size_t operator()(integral_type value) const { \ |
| 70 | return static_cast<std::size_t>(value); \ |
| 71 | } \ |
| 72 | } |
pinkerton@google.com | 3f807de | 2008-08-05 03:36:53 +0900 | [diff] [blame] | 73 | |
mark@chromium.org | 6211bbd | 2009-08-13 21:53:16 +0900 | [diff] [blame] | 74 | DEFINE_TRIVIAL_HASH(long long); |
| 75 | DEFINE_TRIVIAL_HASH(unsigned long long); |
pinkerton@google.com | 3f807de | 2008-08-05 03:36:53 +0900 | [diff] [blame] | 76 | |
mark@chromium.org | 6211bbd | 2009-08-13 21:53:16 +0900 | [diff] [blame] | 77 | #undef DEFINE_TRIVIAL_HASH |
pinkerton@google.com | 3f807de | 2008-08-05 03:36:53 +0900 | [diff] [blame] | 78 | |
mark@chromium.org | 6211bbd | 2009-08-13 21:53:16 +0900 | [diff] [blame] | 79 | // Implement string hash functions so that strings of various flavors can |
| 80 | // be used as keys in STL maps and sets. The hash algorithm comes from the |
| 81 | // GNU C++ library, in <tr1/functional>. It is duplicated here because GCC |
| 82 | // versions prior to 4.3.2 are unable to compile <tr1/functional> when RTTI |
| 83 | // is disabled, as it is in our build. |
pinkerton@google.com | 3f807de | 2008-08-05 03:36:53 +0900 | [diff] [blame] | 84 | |
mark@chromium.org | 6211bbd | 2009-08-13 21:53:16 +0900 | [diff] [blame] | 85 | #define DEFINE_STRING_HASH(string_type) \ |
| 86 | template<> \ |
| 87 | struct hash<string_type> { \ |
| 88 | std::size_t operator()(const string_type& s) const { \ |
| 89 | std::size_t result = 0; \ |
| 90 | for (string_type::const_iterator i = s.begin(); i != s.end(); ++i) \ |
| 91 | result = (result * 131) + *i; \ |
| 92 | return result; \ |
| 93 | } \ |
| 94 | } |
estade@chromium.org | aa1f5dc | 2009-02-22 10:21:56 +0900 | [diff] [blame] | 95 | |
mark@chromium.org | 6211bbd | 2009-08-13 21:53:16 +0900 | [diff] [blame] | 96 | DEFINE_STRING_HASH(std::string); |
mark@chromium.org | 6211bbd | 2009-08-13 21:53:16 +0900 | [diff] [blame] | 97 | DEFINE_STRING_HASH(string16); |
jorlow@chromium.org | 6c83688 | 2009-07-24 10:20:08 +0900 | [diff] [blame] | 98 | |
mark@chromium.org | 6211bbd | 2009-08-13 21:53:16 +0900 | [diff] [blame] | 99 | #undef DEFINE_STRING_HASH |
jorlow@chromium.org | 6c83688 | 2009-07-24 10:20:08 +0900 | [diff] [blame] | 100 | |
mark@chromium.org | 6211bbd | 2009-08-13 21:53:16 +0900 | [diff] [blame] | 101 | } // namespace __gnu_cxx |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 102 | |
mark@chromium.org | 6211bbd | 2009-08-13 21:53:16 +0900 | [diff] [blame] | 103 | #endif // COMPILER |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 104 | |
mark@chromium.org | 6211bbd | 2009-08-13 21:53:16 +0900 | [diff] [blame] | 105 | #endif // BASE_HASH_TABLES_H_ |