HashFunctions.h 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /*
  2. * Copyright (C) 2005, 2006, 2008 Apple Inc. All rights reserved.
  3. *
  4. * This library is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Library General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2 of the License, or (at your option) any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Library General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Library General Public License
  15. * along with this library; see the file COPYING.LIB. If not, write to
  16. * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  17. * Boston, MA 02110-1301, USA.
  18. *
  19. */
  20. #ifndef WTF_HashFunctions_h
  21. #define WTF_HashFunctions_h
  22. #include <wtf/RefPtr.h>
  23. #include <stdint.h>
  24. namespace WTF {
  25. template<size_t size> struct IntTypes;
  26. template<> struct IntTypes<1> { typedef int8_t SignedType; typedef uint8_t UnsignedType; };
  27. template<> struct IntTypes<2> { typedef int16_t SignedType; typedef uint16_t UnsignedType; };
  28. template<> struct IntTypes<4> { typedef int32_t SignedType; typedef uint32_t UnsignedType; };
  29. template<> struct IntTypes<8> { typedef int64_t SignedType; typedef uint64_t UnsignedType; };
  30. // integer hash function
  31. // Thomas Wang's 32 Bit Mix Function: http://www.cris.com/~Ttwang/tech/inthash.htm
  32. inline unsigned intHash(uint8_t key8)
  33. {
  34. unsigned key = key8;
  35. key += ~(key << 15);
  36. key ^= (key >> 10);
  37. key += (key << 3);
  38. key ^= (key >> 6);
  39. key += ~(key << 11);
  40. key ^= (key >> 16);
  41. return key;
  42. }
  43. // Thomas Wang's 32 Bit Mix Function: http://www.cris.com/~Ttwang/tech/inthash.htm
  44. inline unsigned intHash(uint16_t key16)
  45. {
  46. unsigned key = key16;
  47. key += ~(key << 15);
  48. key ^= (key >> 10);
  49. key += (key << 3);
  50. key ^= (key >> 6);
  51. key += ~(key << 11);
  52. key ^= (key >> 16);
  53. return key;
  54. }
  55. // Thomas Wang's 32 Bit Mix Function: http://www.cris.com/~Ttwang/tech/inthash.htm
  56. inline unsigned intHash(uint32_t key)
  57. {
  58. key += ~(key << 15);
  59. key ^= (key >> 10);
  60. key += (key << 3);
  61. key ^= (key >> 6);
  62. key += ~(key << 11);
  63. key ^= (key >> 16);
  64. return key;
  65. }
  66. // Thomas Wang's 64 bit Mix Function: http://www.cris.com/~Ttwang/tech/inthash.htm
  67. inline unsigned intHash(uint64_t key)
  68. {
  69. key += ~(key << 32);
  70. key ^= (key >> 22);
  71. key += ~(key << 13);
  72. key ^= (key >> 8);
  73. key += (key << 3);
  74. key ^= (key >> 15);
  75. key += ~(key << 27);
  76. key ^= (key >> 31);
  77. return static_cast<unsigned>(key);
  78. }
  79. // Compound integer hash method: http://opendatastructures.org/versions/edition-0.1d/ods-java/node33.html#SECTION00832000000000000000
  80. inline unsigned pairIntHash(unsigned key1, unsigned key2)
  81. {
  82. unsigned shortRandom1 = 277951225; // A random 32-bit value.
  83. unsigned shortRandom2 = 95187966; // A random 32-bit value.
  84. uint64_t longRandom = 19248658165952622LL; // A random 64-bit value.
  85. uint64_t product = longRandom * (shortRandom1 * key1 + shortRandom2 * key2);
  86. unsigned highBits = static_cast<unsigned>(product >> (sizeof(uint64_t) - sizeof(unsigned)));
  87. return highBits;
  88. }
  89. template<typename T> struct IntHash {
  90. static unsigned hash(T key) { return intHash(static_cast<typename IntTypes<sizeof(T)>::UnsignedType>(key)); }
  91. static bool equal(T a, T b) { return a == b; }
  92. static const bool safeToCompareToEmptyOrDeleted = true;
  93. };
  94. template<typename T> struct FloatHash {
  95. typedef typename IntTypes<sizeof(T)>::UnsignedType Bits;
  96. static unsigned hash(T key)
  97. {
  98. return intHash(bitwise_cast<Bits>(key));
  99. }
  100. static bool equal(T a, T b)
  101. {
  102. return bitwise_cast<Bits>(a) == bitwise_cast<Bits>(b);
  103. }
  104. static const bool safeToCompareToEmptyOrDeleted = true;
  105. };
  106. // pointer identity hash function
  107. template<typename T> struct PtrHash {
  108. static unsigned hash(T key)
  109. {
  110. #if COMPILER(MSVC)
  111. #pragma warning(push)
  112. #pragma warning(disable: 4244) // work around what seems to be a bug in MSVC's conversion warnings
  113. #endif
  114. return IntHash<uintptr_t>::hash(reinterpret_cast<uintptr_t>(key));
  115. #if COMPILER(MSVC)
  116. #pragma warning(pop)
  117. #endif
  118. }
  119. static bool equal(T a, T b) { return a == b; }
  120. static const bool safeToCompareToEmptyOrDeleted = true;
  121. };
  122. template<typename P> struct PtrHash<RefPtr<P> > : PtrHash<P*> {
  123. using PtrHash<P*>::hash;
  124. static unsigned hash(const RefPtr<P>& key) { return hash(key.get()); }
  125. using PtrHash<P*>::equal;
  126. static bool equal(const RefPtr<P>& a, const RefPtr<P>& b) { return a == b; }
  127. static bool equal(P* a, const RefPtr<P>& b) { return a == b; }
  128. static bool equal(const RefPtr<P>& a, P* b) { return a == b; }
  129. };
  130. // default hash function for each type
  131. template<typename T> struct DefaultHash;
  132. template<typename T, typename U> struct PairHash {
  133. static unsigned hash(const std::pair<T, U>& p)
  134. {
  135. return pairIntHash(DefaultHash<T>::Hash::hash(p.first), DefaultHash<U>::Hash::hash(p.second));
  136. }
  137. static bool equal(const std::pair<T, U>& a, const std::pair<T, U>& b)
  138. {
  139. return DefaultHash<T>::Hash::equal(a.first, b.first) && DefaultHash<U>::Hash::equal(a.second, b.second);
  140. }
  141. static const bool safeToCompareToEmptyOrDeleted = DefaultHash<T>::Hash::safeToCompareToEmptyOrDeleted
  142. && DefaultHash<U>::Hash::safeToCompareToEmptyOrDeleted;
  143. };
  144. template<typename T, typename U> struct IntPairHash {
  145. static unsigned hash(const std::pair<T, U>& p) { return pairIntHash(p.first, p.second); }
  146. static bool equal(const std::pair<T, U>& a, const std::pair<T, U>& b) { return PairHash<T, T>::equal(a, b); }
  147. static const bool safeToCompareToEmptyOrDeleted = PairHash<T, U>::safeToCompareToEmptyOrDeleted;
  148. };
  149. // make IntHash the default hash function for many integer types
  150. template<> struct DefaultHash<short> { typedef IntHash<unsigned> Hash; };
  151. template<> struct DefaultHash<unsigned short> { typedef IntHash<unsigned> Hash; };
  152. template<> struct DefaultHash<int> { typedef IntHash<unsigned> Hash; };
  153. template<> struct DefaultHash<unsigned> { typedef IntHash<unsigned> Hash; };
  154. template<> struct DefaultHash<long> { typedef IntHash<unsigned long> Hash; };
  155. template<> struct DefaultHash<unsigned long> { typedef IntHash<unsigned long> Hash; };
  156. template<> struct DefaultHash<long long> { typedef IntHash<unsigned long long> Hash; };
  157. template<> struct DefaultHash<unsigned long long> { typedef IntHash<unsigned long long> Hash; };
  158. #if !COMPILER(MSVC) || defined(_NATIVE_WCHAR_T_DEFINED)
  159. template<> struct DefaultHash<wchar_t> { typedef IntHash<wchar_t> Hash; };
  160. #endif
  161. template<> struct DefaultHash<float> { typedef FloatHash<float> Hash; };
  162. template<> struct DefaultHash<double> { typedef FloatHash<double> Hash; };
  163. // make PtrHash the default hash function for pointer types that don't specialize
  164. template<typename P> struct DefaultHash<P*> { typedef PtrHash<P*> Hash; };
  165. template<typename P> struct DefaultHash<RefPtr<P> > { typedef PtrHash<RefPtr<P> > Hash; };
  166. // make IntPairHash the default hash function for pairs of (at most) 32-bit integers.
  167. template<> struct DefaultHash<std::pair<short, short> > { typedef IntPairHash<short, short> Hash; };
  168. template<> struct DefaultHash<std::pair<short, unsigned short> > { typedef IntPairHash<short, unsigned short> Hash; };
  169. template<> struct DefaultHash<std::pair<short, int> > { typedef IntPairHash<short, int> Hash; };
  170. template<> struct DefaultHash<std::pair<short, unsigned> > { typedef IntPairHash<short, unsigned> Hash; };
  171. template<> struct DefaultHash<std::pair<unsigned short, short> > { typedef IntPairHash<unsigned short, short> Hash; };
  172. template<> struct DefaultHash<std::pair<unsigned short, unsigned short> > { typedef IntPairHash<unsigned short, unsigned short> Hash; };
  173. template<> struct DefaultHash<std::pair<unsigned short, int> > { typedef IntPairHash<unsigned short, int> Hash; };
  174. template<> struct DefaultHash<std::pair<unsigned short, unsigned> > { typedef IntPairHash<unsigned short, unsigned> Hash; };
  175. template<> struct DefaultHash<std::pair<int, short> > { typedef IntPairHash<int, short> Hash; };
  176. template<> struct DefaultHash<std::pair<int, unsigned short> > { typedef IntPairHash<int, unsigned short> Hash; };
  177. template<> struct DefaultHash<std::pair<int, int> > { typedef IntPairHash<int, int> Hash; };
  178. template<> struct DefaultHash<std::pair<int, unsigned> > { typedef IntPairHash<unsigned, unsigned> Hash; };
  179. template<> struct DefaultHash<std::pair<unsigned, short> > { typedef IntPairHash<unsigned, short> Hash; };
  180. template<> struct DefaultHash<std::pair<unsigned, unsigned short> > { typedef IntPairHash<unsigned, unsigned short> Hash; };
  181. template<> struct DefaultHash<std::pair<unsigned, int> > { typedef IntPairHash<unsigned, int> Hash; };
  182. template<> struct DefaultHash<std::pair<unsigned, unsigned> > { typedef IntPairHash<unsigned, unsigned> Hash; };
  183. // make PairHash the default hash function for pairs of arbitrary values.
  184. template<typename T, typename U> struct DefaultHash<std::pair<T, U> > { typedef PairHash<T, U> Hash; };
  185. } // namespace WTF
  186. using WTF::DefaultHash;
  187. using WTF::IntHash;
  188. using WTF::PtrHash;
  189. #endif // WTF_HashFunctions_h