Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

100 lines
2.1 KiB

  1. //======= Copyright � Valve Corporation, All rights reserved. =================
  2. //
  3. // Public domain MurmurHash3 by Austin Appleby is a very solid general-purpose
  4. // hash with a 32-bit output. References:
  5. // http://code.google.com/p/smhasher/ (home of MurmurHash3)
  6. // https://sites.google.com/site/murmurhash/avalanche
  7. // http://www.strchr.com/hash_functions
  8. //
  9. //=============================================================================
  10. #ifndef MURMURHASH3_H
  11. #define MURMURHASH3_H
  12. #if defined(_WIN32)
  13. #pragma once
  14. #endif
  15. uint32 MurmurHash3_32( const void *key, size_t len, uint32 seed, bool bCaselessStringVariant = false );
  16. inline uint32 MurmurHash3String( const char *pszKey, size_t len )
  17. {
  18. return MurmurHash3_32( pszKey, len, 1047 /*anything will do for a seed*/, false );
  19. }
  20. inline uint32 MurmurHash3StringCaseless( const char *pszKey, size_t len )
  21. {
  22. return MurmurHash3_32( pszKey, len, 1047 /*anything will do for a seed*/, true );
  23. }
  24. inline uint32 MurmurHash3String( const char *pszKey )
  25. {
  26. return MurmurHash3String( pszKey, strlen( pszKey ) );
  27. }
  28. inline uint32 MurmurHash3StringCaseless( const char *pszKey )
  29. {
  30. return MurmurHash3StringCaseless( pszKey, strlen( pszKey ) );
  31. }
  32. template <typename T>
  33. inline uint32 MurmurHash3Item( const T &item )
  34. {
  35. return MurmurHash3_32( &item, sizeof(item), 1047 );
  36. }
  37. inline uint32 MurmurHash3Int( uint32 h )
  38. {
  39. h ^= h >> 16;
  40. h *= 0x85ebca6b;
  41. h ^= h >> 13;
  42. h *= 0xc2b2ae35;
  43. h ^= h >> 16;
  44. return h;
  45. }
  46. template <>
  47. inline uint32 MurmurHash3Item( const uint32 &item )
  48. {
  49. return MurmurHash3Int( item );
  50. }
  51. template <>
  52. inline uint32 MurmurHash3Item( const int32 &item )
  53. {
  54. return MurmurHash3Int( item );
  55. }
  56. template<typename T>
  57. struct MurmurHash3Functor
  58. {
  59. typedef uint32 TargetType;
  60. TargetType operator()(const T &key) const
  61. {
  62. return MurmurHash3Item( key );
  63. }
  64. };
  65. template<>
  66. struct MurmurHash3Functor<char *>
  67. {
  68. typedef uint32 TargetType;
  69. TargetType operator()(const char *key) const
  70. {
  71. return MurmurHash3String( key );
  72. }
  73. };
  74. template<>
  75. struct MurmurHash3Functor<const char *>
  76. {
  77. typedef uint32 TargetType;
  78. TargetType operator()(const char *key) const
  79. {
  80. return MurmurHash3String( key );
  81. }
  82. };
  83. #endif // MURMURHASH3_H