michael@0: // Copyright (c) 2011 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: // NOTE: It is an explicit non-goal of this class to provide a generic hash michael@0: // function for pointers. If you want to hash a pointers to a particular class, michael@0: // please define the template specialization elsewhere (for example, in its michael@0: // header file) and keep it specific to just pointers to that class. This is michael@0: // because identity hashes are not desirable for all types that might show up michael@0: // in containers as pointers. michael@0: michael@0: #ifndef BASE_CONTAINERS_HASH_TABLES_H_ michael@0: #define BASE_CONTAINERS_HASH_TABLES_H_ michael@0: michael@0: #include michael@0: michael@0: #include "base/basictypes.h" michael@0: #include "base/strings/string16.h" michael@0: #include "build/build_config.h" michael@0: michael@0: #if defined(COMPILER_MSVC) michael@0: #include michael@0: #include michael@0: michael@0: #define BASE_HASH_NAMESPACE stdext michael@0: michael@0: #elif defined(COMPILER_GCC) michael@0: #if defined(OS_ANDROID) michael@0: #define BASE_HASH_NAMESPACE std michael@0: #else michael@0: #define BASE_HASH_NAMESPACE __gnu_cxx michael@0: #endif michael@0: 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: #if defined(OS_ANDROID) michael@0: #include michael@0: #include michael@0: #else michael@0: #include michael@0: #include michael@0: #endif michael@0: 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_HASH_NAMESPACE { michael@0: michael@0: #if !defined(OS_ANDROID) michael@0: // The GNU C++ library provides identity 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: #endif // !defined(OS_ANDROID) 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(string16); michael@0: michael@0: #undef DEFINE_STRING_HASH michael@0: michael@0: } // namespace BASE_HASH_NAMESPACE michael@0: michael@0: #else // COMPILER michael@0: #error define BASE_HASH_NAMESPACE for your compiler michael@0: #endif // COMPILER michael@0: michael@0: namespace base { michael@0: using BASE_HASH_NAMESPACE::hash_map; michael@0: using BASE_HASH_NAMESPACE::hash_multimap; michael@0: using BASE_HASH_NAMESPACE::hash_multiset; michael@0: using BASE_HASH_NAMESPACE::hash_set; michael@0: michael@0: // Implement hashing for pairs of at-most 32 bit integer values. michael@0: // When size_t is 32 bits, we turn the 64-bit hash code into 32 bits by using michael@0: // multiply-add hashing. This algorithm, as described in michael@0: // Theorem 4.3.3 of the thesis "Über die Komplexität der Multiplikation in michael@0: // eingeschränkten Branchingprogrammmodellen" by Woelfel, is: michael@0: // michael@0: // h32(x32, y32) = (h64(x32, y32) * rand_odd64 + rand16 * 2^16) % 2^64 / 2^32 michael@0: // michael@0: // Contact danakj@chromium.org for any questions. michael@0: inline std::size_t HashInts32(uint32 value1, uint32 value2) { michael@0: uint64 value1_64 = value1; michael@0: uint64 hash64 = (value1_64 << 32) | value2; michael@0: michael@0: if (sizeof(std::size_t) >= sizeof(uint64)) michael@0: return static_cast(hash64); michael@0: michael@0: uint64 odd_random = 481046412LL << 32 | 1025306955LL; michael@0: uint32 shift_random = 10121U << 16; michael@0: michael@0: hash64 = hash64 * odd_random + shift_random; michael@0: std::size_t high_bits = static_cast( michael@0: hash64 >> (sizeof(uint64) - sizeof(std::size_t))); michael@0: return high_bits; michael@0: } michael@0: michael@0: // Implement hashing for pairs of up-to 64-bit integer values. michael@0: // We use the compound integer hash method to produce a 64-bit hash code, by michael@0: // breaking the two 64-bit inputs into 4 32-bit values: michael@0: // http://opendatastructures.org/versions/edition-0.1d/ods-java/node33.html#SECTION00832000000000000000 michael@0: // Then we reduce our result to 32 bits if required, similar to above. michael@0: inline std::size_t HashInts64(uint64 value1, uint64 value2) { michael@0: uint32 short_random1 = 842304669U; michael@0: uint32 short_random2 = 619063811U; michael@0: uint32 short_random3 = 937041849U; michael@0: uint32 short_random4 = 3309708029U; michael@0: michael@0: uint32 value1a = static_cast(value1 & 0xffffffff); michael@0: uint32 value1b = static_cast((value1 >> 32) & 0xffffffff); michael@0: uint32 value2a = static_cast(value2 & 0xffffffff); michael@0: uint32 value2b = static_cast((value2 >> 32) & 0xffffffff); michael@0: michael@0: uint64 product1 = static_cast(value1a) * short_random1; michael@0: uint64 product2 = static_cast(value1b) * short_random2; michael@0: uint64 product3 = static_cast(value2a) * short_random3; michael@0: uint64 product4 = static_cast(value2b) * short_random4; michael@0: michael@0: uint64 hash64 = product1 + product2 + product3 + product4; michael@0: michael@0: if (sizeof(std::size_t) >= sizeof(uint64)) michael@0: return static_cast(hash64); michael@0: michael@0: uint64 odd_random = 1578233944LL << 32 | 194370989LL; michael@0: uint32 shift_random = 20591U << 16; michael@0: michael@0: hash64 = hash64 * odd_random + shift_random; michael@0: std::size_t high_bits = static_cast( michael@0: hash64 >> (sizeof(uint64) - sizeof(std::size_t))); michael@0: return high_bits; michael@0: } michael@0: michael@0: #define DEFINE_32BIT_PAIR_HASH(Type1, Type2) \ michael@0: inline std::size_t HashPair(Type1 value1, Type2 value2) { \ michael@0: return HashInts32(value1, value2); \ michael@0: } michael@0: michael@0: DEFINE_32BIT_PAIR_HASH(int16, int16); michael@0: DEFINE_32BIT_PAIR_HASH(int16, uint16); michael@0: DEFINE_32BIT_PAIR_HASH(int16, int32); michael@0: DEFINE_32BIT_PAIR_HASH(int16, uint32); michael@0: DEFINE_32BIT_PAIR_HASH(uint16, int16); michael@0: DEFINE_32BIT_PAIR_HASH(uint16, uint16); michael@0: DEFINE_32BIT_PAIR_HASH(uint16, int32); michael@0: DEFINE_32BIT_PAIR_HASH(uint16, uint32); michael@0: DEFINE_32BIT_PAIR_HASH(int32, int16); michael@0: DEFINE_32BIT_PAIR_HASH(int32, uint16); michael@0: DEFINE_32BIT_PAIR_HASH(int32, int32); michael@0: DEFINE_32BIT_PAIR_HASH(int32, uint32); michael@0: DEFINE_32BIT_PAIR_HASH(uint32, int16); michael@0: DEFINE_32BIT_PAIR_HASH(uint32, uint16); michael@0: DEFINE_32BIT_PAIR_HASH(uint32, int32); michael@0: DEFINE_32BIT_PAIR_HASH(uint32, uint32); michael@0: michael@0: #undef DEFINE_32BIT_PAIR_HASH michael@0: michael@0: #define DEFINE_64BIT_PAIR_HASH(Type1, Type2) \ michael@0: inline std::size_t HashPair(Type1 value1, Type2 value2) { \ michael@0: return HashInts64(value1, value2); \ michael@0: } michael@0: michael@0: DEFINE_64BIT_PAIR_HASH(int16, int64); michael@0: DEFINE_64BIT_PAIR_HASH(int16, uint64); michael@0: DEFINE_64BIT_PAIR_HASH(uint16, int64); michael@0: DEFINE_64BIT_PAIR_HASH(uint16, uint64); michael@0: DEFINE_64BIT_PAIR_HASH(int32, int64); michael@0: DEFINE_64BIT_PAIR_HASH(int32, uint64); michael@0: DEFINE_64BIT_PAIR_HASH(uint32, int64); michael@0: DEFINE_64BIT_PAIR_HASH(uint32, uint64); michael@0: DEFINE_64BIT_PAIR_HASH(int64, int16); michael@0: DEFINE_64BIT_PAIR_HASH(int64, uint16); michael@0: DEFINE_64BIT_PAIR_HASH(int64, int32); michael@0: DEFINE_64BIT_PAIR_HASH(int64, uint32); michael@0: DEFINE_64BIT_PAIR_HASH(int64, int64); michael@0: DEFINE_64BIT_PAIR_HASH(int64, uint64); michael@0: DEFINE_64BIT_PAIR_HASH(uint64, int16); michael@0: DEFINE_64BIT_PAIR_HASH(uint64, uint16); michael@0: DEFINE_64BIT_PAIR_HASH(uint64, int32); michael@0: DEFINE_64BIT_PAIR_HASH(uint64, uint32); michael@0: DEFINE_64BIT_PAIR_HASH(uint64, int64); michael@0: DEFINE_64BIT_PAIR_HASH(uint64, uint64); michael@0: michael@0: #undef DEFINE_64BIT_PAIR_HASH michael@0: } // namespace base michael@0: michael@0: namespace BASE_HASH_NAMESPACE { michael@0: michael@0: // Implement methods for hashing a pair of integers, so they can be used as michael@0: // keys in STL containers. michael@0: michael@0: #if defined(COMPILER_MSVC) michael@0: michael@0: template michael@0: inline std::size_t hash_value(const std::pair& value) { michael@0: return base::HashPair(value.first, value.second); michael@0: } michael@0: michael@0: #elif defined(COMPILER_GCC) michael@0: template michael@0: struct hash > { michael@0: std::size_t operator()(std::pair value) const { michael@0: return base::HashPair(value.first, value.second); michael@0: } michael@0: }; michael@0: michael@0: #else michael@0: #error define hash > for your compiler michael@0: #endif // COMPILER michael@0: michael@0: } michael@0: michael@0: #undef DEFINE_PAIR_HASH_FUNCTION_START michael@0: #undef DEFINE_PAIR_HASH_FUNCTION_END michael@0: michael@0: #endif // BASE_CONTAINERS_HASH_TABLES_H_