hash_tables.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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.
  4. //
  5. //
  6. // Deal with the differences between Microsoft and GNU implemenations
  7. // of hash_map. Allows all platforms to use |base::hash_map| and
  8. // |base::hash_set|.
  9. // eg:
  10. // base::hash_map<int> my_map;
  11. // base::hash_set<int> my_set;
  12. //
  13. // NOTE: It is an explicit non-goal of this class to provide a generic hash
  14. // function for pointers. If you want to hash a pointers to a particular class,
  15. // please define the template specialization elsewhere (for example, in its
  16. // header file) and keep it specific to just pointers to that class. This is
  17. // because identity hashes are not desirable for all types that might show up
  18. // in containers as pointers.
  19. #ifndef BASE_CONTAINERS_HASH_TABLES_H_
  20. #define BASE_CONTAINERS_HASH_TABLES_H_
  21. #include <utility>
  22. #include "base/basictypes.h"
  23. #include "base/strings/string16.h"
  24. #include "base/build_config.h"
  25. #if defined(COMPILER_MSVC)
  26. #include <hash_map>
  27. #include <hash_set>
  28. #define BASE_HASH_NAMESPACE stdext
  29. #elif defined(COMPILER_GCC)
  30. #if defined(OS_ANDROID)
  31. #define BASE_HASH_NAMESPACE std
  32. #else
  33. #define BASE_HASH_NAMESPACE __gnu_cxx
  34. #endif
  35. // This is a hack to disable the gcc 4.4 warning about hash_map and hash_set
  36. // being deprecated. We can get rid of this when we upgrade to VS2008 and we
  37. // can use <tr1/unordered_map> and <tr1/unordered_set>.
  38. #ifdef __DEPRECATED
  39. #define CHROME_OLD__DEPRECATED __DEPRECATED
  40. #undef __DEPRECATED
  41. #endif
  42. #if defined(OS_ANDROID)
  43. #include <hash_map>
  44. #include <hash_set>
  45. #else
  46. #include <ext/hash_map>
  47. #include <ext/hash_set>
  48. #endif
  49. #include <string>
  50. #ifdef CHROME_OLD__DEPRECATED
  51. #define __DEPRECATED CHROME_OLD__DEPRECATED
  52. #undef CHROME_OLD__DEPRECATED
  53. #endif
  54. namespace BASE_HASH_NAMESPACE {
  55. #if !defined(OS_ANDROID)
  56. // The GNU C++ library provides identity hash functions for many integral types,
  57. // but not for |long long|. This hash function will truncate if |size_t| is
  58. // narrower than |long long|. This is probably good enough for what we will
  59. // use it for.
  60. #define DEFINE_TRIVIAL_HASH(integral_type) \
  61. template<> \
  62. struct hash<integral_type> { \
  63. std::size_t operator()(integral_type value) const { \
  64. return static_cast<std::size_t>(value); \
  65. } \
  66. }
  67. DEFINE_TRIVIAL_HASH(long long);
  68. DEFINE_TRIVIAL_HASH(unsigned long long);
  69. #undef DEFINE_TRIVIAL_HASH
  70. #endif // !defined(OS_ANDROID)
  71. // Implement string hash functions so that strings of various flavors can
  72. // be used as keys in STL maps and sets. The hash algorithm comes from the
  73. // GNU C++ library, in <tr1/functional>. It is duplicated here because GCC
  74. // versions prior to 4.3.2 are unable to compile <tr1/functional> when RTTI
  75. // is disabled, as it is in our build.
  76. #define DEFINE_STRING_HASH(string_type) \
  77. template<> \
  78. struct hash<string_type> { \
  79. std::size_t operator()(const string_type& s) const { \
  80. std::size_t result = 0; \
  81. for (string_type::const_iterator i = s.begin(); i != s.end(); ++i) \
  82. result = (result * 131) + *i; \
  83. return result; \
  84. } \
  85. }
  86. DEFINE_STRING_HASH(std::string);
  87. DEFINE_STRING_HASH(base::string16);
  88. #undef DEFINE_STRING_HASH
  89. } // namespace BASE_HASH_NAMESPACE
  90. #else // COMPILER
  91. #error define BASE_HASH_NAMESPACE for your compiler
  92. #endif // COMPILER
  93. namespace base {
  94. using BASE_HASH_NAMESPACE::hash_map;
  95. using BASE_HASH_NAMESPACE::hash_multimap;
  96. using BASE_HASH_NAMESPACE::hash_multiset;
  97. using BASE_HASH_NAMESPACE::hash_set;
  98. // Implement hashing for pairs of at-most 32 bit integer values.
  99. // When size_t is 32 bits, we turn the 64-bit hash code into 32 bits by using
  100. // multiply-add hashing. This algorithm, as described in
  101. // Theorem 4.3.3 of the thesis "Über die Komplexität der Multiplikation in
  102. // eingeschränkten Branchingprogrammmodellen" by Woelfel, is:
  103. //
  104. // h32(x32, y32) = (h64(x32, y32) * rand_odd64 + rand16 * 2^16) % 2^64 / 2^32
  105. //
  106. // Contact danakj@chromium.org for any questions.
  107. inline std::size_t HashInts32(uint32_t value1, uint32_t value2) {
  108. uint64_t value1_64 = value1;
  109. uint64_t hash64 = (value1_64 << 32) | value2;
  110. if (sizeof(std::size_t) >= sizeof(uint64_t))
  111. return static_cast<std::size_t>(hash64);
  112. uint64_t odd_random = 481046412LL << 32 | 1025306955LL;
  113. uint32_t shift_random = 10121U << 16;
  114. hash64 = hash64 * odd_random + shift_random;
  115. std::size_t high_bits = static_cast<std::size_t>(
  116. hash64 >> (8 * (sizeof(uint64_t) - sizeof(std::size_t))));
  117. return high_bits;
  118. }
  119. // Implement hashing for pairs of up-to 64-bit integer values.
  120. // We use the compound integer hash method to produce a 64-bit hash code, by
  121. // breaking the two 64-bit inputs into 4 32-bit values:
  122. // http://opendatastructures.org/versions/edition-0.1d/ods-java/node33.html#SECTION00832000000000000000
  123. // Then we reduce our result to 32 bits if required, similar to above.
  124. inline std::size_t HashInts64(uint64_t value1, uint64_t value2) {
  125. uint32_t short_random1 = 842304669U;
  126. uint32_t short_random2 = 619063811U;
  127. uint32_t short_random3 = 937041849U;
  128. uint32_t short_random4 = 3309708029U;
  129. uint32_t value1a = static_cast<uint32_t>(value1 & 0xffffffff);
  130. uint32_t value1b = static_cast<uint32_t>((value1 >> 32) & 0xffffffff);
  131. uint32_t value2a = static_cast<uint32_t>(value2 & 0xffffffff);
  132. uint32_t value2b = static_cast<uint32_t>((value2 >> 32) & 0xffffffff);
  133. uint64_t product1 = static_cast<uint64_t>(value1a) * short_random1;
  134. uint64_t product2 = static_cast<uint64_t>(value1b) * short_random2;
  135. uint64_t product3 = static_cast<uint64_t>(value2a) * short_random3;
  136. uint64_t product4 = static_cast<uint64_t>(value2b) * short_random4;
  137. uint64_t hash64 = product1 + product2 + product3 + product4;
  138. if (sizeof(std::size_t) >= sizeof(uint64_t))
  139. return static_cast<std::size_t>(hash64);
  140. uint64_t odd_random = 1578233944LL << 32 | 194370989LL;
  141. uint32_t shift_random = 20591U << 16;
  142. hash64 = hash64 * odd_random + shift_random;
  143. std::size_t high_bits = static_cast<std::size_t>(
  144. hash64 >> (8 * (sizeof(uint64_t) - sizeof(std::size_t))));
  145. return high_bits;
  146. }
  147. #define DEFINE_32BIT_PAIR_HASH(Type1, Type2) \
  148. inline std::size_t HashPair(Type1 value1, Type2 value2) { \
  149. return HashInts32(value1, value2); \
  150. }
  151. DEFINE_32BIT_PAIR_HASH(int16_t, int16_t);
  152. DEFINE_32BIT_PAIR_HASH(int16_t, uint16_t);
  153. DEFINE_32BIT_PAIR_HASH(int16_t, int32_t);
  154. DEFINE_32BIT_PAIR_HASH(int16_t, uint32_t);
  155. DEFINE_32BIT_PAIR_HASH(uint16_t, int16_t);
  156. DEFINE_32BIT_PAIR_HASH(uint16_t, uint16_t);
  157. DEFINE_32BIT_PAIR_HASH(uint16_t, int32_t);
  158. DEFINE_32BIT_PAIR_HASH(uint16_t, uint32_t);
  159. DEFINE_32BIT_PAIR_HASH(int32_t, int16_t);
  160. DEFINE_32BIT_PAIR_HASH(int32_t, uint16_t);
  161. DEFINE_32BIT_PAIR_HASH(int32_t, int32_t);
  162. DEFINE_32BIT_PAIR_HASH(int32_t, uint32_t);
  163. DEFINE_32BIT_PAIR_HASH(uint32_t, int16_t);
  164. DEFINE_32BIT_PAIR_HASH(uint32_t, uint16_t);
  165. DEFINE_32BIT_PAIR_HASH(uint32_t, int32_t);
  166. DEFINE_32BIT_PAIR_HASH(uint32_t, uint32_t);
  167. #undef DEFINE_32BIT_PAIR_HASH
  168. #define DEFINE_64BIT_PAIR_HASH(Type1, Type2) \
  169. inline std::size_t HashPair(Type1 value1, Type2 value2) { \
  170. return HashInts64(value1, value2); \
  171. }
  172. DEFINE_64BIT_PAIR_HASH(int16_t, int64_t);
  173. DEFINE_64BIT_PAIR_HASH(int16_t, uint64_t);
  174. DEFINE_64BIT_PAIR_HASH(uint16_t, int64_t);
  175. DEFINE_64BIT_PAIR_HASH(uint16_t, uint64_t);
  176. DEFINE_64BIT_PAIR_HASH(int32_t, int64_t);
  177. DEFINE_64BIT_PAIR_HASH(int32_t, uint64_t);
  178. DEFINE_64BIT_PAIR_HASH(uint32_t, int64_t);
  179. DEFINE_64BIT_PAIR_HASH(uint32_t, uint64_t);
  180. DEFINE_64BIT_PAIR_HASH(int64_t, int16_t);
  181. DEFINE_64BIT_PAIR_HASH(int64_t, uint16_t);
  182. DEFINE_64BIT_PAIR_HASH(int64_t, int32_t);
  183. DEFINE_64BIT_PAIR_HASH(int64_t, uint32_t);
  184. DEFINE_64BIT_PAIR_HASH(int64_t, int64_t);
  185. DEFINE_64BIT_PAIR_HASH(int64_t, uint64_t);
  186. DEFINE_64BIT_PAIR_HASH(uint64_t, int16_t);
  187. DEFINE_64BIT_PAIR_HASH(uint64_t, uint16_t);
  188. DEFINE_64BIT_PAIR_HASH(uint64_t, int32_t);
  189. DEFINE_64BIT_PAIR_HASH(uint64_t, uint32_t);
  190. DEFINE_64BIT_PAIR_HASH(uint64_t, int64_t);
  191. DEFINE_64BIT_PAIR_HASH(uint64_t, uint64_t);
  192. #undef DEFINE_64BIT_PAIR_HASH
  193. } // namespace base
  194. namespace BASE_HASH_NAMESPACE {
  195. // Implement methods for hashing a pair of integers, so they can be used as
  196. // keys in STL containers.
  197. // NOTE(gejun): Specialize ptr as well which is supposed to work with
  198. // containers by default
  199. #if defined(COMPILER_MSVC)
  200. template<typename Type1, typename Type2>
  201. inline std::size_t hash_value(const std::pair<Type1, Type2>& value) {
  202. return base::HashPair(value.first, value.second);
  203. }
  204. template<typename Type>
  205. inline std::size_t hash_value(Type* ptr) {
  206. return (uintptr_t)ptr;
  207. }
  208. #elif defined(COMPILER_GCC)
  209. template<typename Type1, typename Type2>
  210. struct hash<std::pair<Type1, Type2> > {
  211. std::size_t operator()(std::pair<Type1, Type2> value) const {
  212. return base::HashPair(value.first, value.second);
  213. }
  214. };
  215. template<typename Type>
  216. struct hash<Type*> {
  217. std::size_t operator()(Type* ptr) const {
  218. return (uintptr_t)ptr;
  219. }
  220. };
  221. #else
  222. #error define hash<std::pair<Type1, Type2> > for your compiler
  223. #endif // COMPILER
  224. }
  225. #undef DEFINE_PAIR_HASH_FUNCTION_START
  226. #undef DEFINE_PAIR_HASH_FUNCTION_END
  227. #endif // BASE_CONTAINERS_HASH_TABLES_H_