Hash.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*
  2. Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
  3. Permission is hereby granted, free of charge, to any person
  4. obtaining a copy of this software and associated documentation
  5. files (the "Software"), to deal in the Software without
  6. restriction, including without limitation the rights to use,
  7. copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the
  9. Software is furnished to do so, subject to the following
  10. conditions:
  11. The above copyright notice and this permission notice shall be
  12. included in all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  14. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  15. OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  16. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  17. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  18. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  20. OTHER DEALINGS IN THE SOFTWARE.
  21. */
  22. #pragma once
  23. #include "Assert.h"
  24. #include "Types.h"
  25. namespace crown
  26. {
  27. /// String hashing.
  28. namespace hash
  29. {
  30. // Constants
  31. const uint32_t FNV1A_OFFSET_BASIS_32 = 2166136261u;
  32. const uint32_t FNV1A_PRIME_32 = 16777619u;
  33. const uint64_t FNV1A_OFFSET_BASIS_64 = 14695981039346656037u;
  34. const uint64_t FNV1A_PRIME_64 = 1099511628211u;
  35. // Functions
  36. uint32_t murmur2_32(const void* key, size_t len, uint32_t seed);
  37. uint32_t fnv1a_32(const void* key, size_t len);
  38. uint64_t fnv1a_64(const void* key, size_t len);
  39. //-----------------------------------------------------------------------------
  40. /// MurmurHash2, by Austin Appleby
  41. ///
  42. /// @note
  43. /// This code makes a few assumptions about how your machine behaves
  44. ///
  45. /// 1. We can read a 4-byte value from any address without crashing
  46. /// 2. sizeof(int) == 4
  47. ///
  48. /// And it has a few limitations -
  49. ///
  50. /// 1. It will not work incrementally.
  51. /// 2. It will not produce the same results on little-endian and big-endian
  52. /// machines.
  53. inline uint32_t murmur2_32(const void* key, size_t len, uint32_t seed)
  54. {
  55. CE_ASSERT(key != NULL, "Key must be != NULL");
  56. // 'm' and 'r' are mixing constants generated offline.
  57. // They're not really 'magic', they just happen to work well.
  58. const unsigned int m = 0x5bd1e995;
  59. const int r = 24;
  60. // Initialize the hash to a 'random' value
  61. unsigned int h = seed ^ len;
  62. // Mix 4 bytes at a time into the hash
  63. const unsigned char * data = (const unsigned char *)key;
  64. while(len >= 4)
  65. {
  66. unsigned int k = *(unsigned int *)data;
  67. k *= m;
  68. k ^= k >> r;
  69. k *= m;
  70. h *= m;
  71. h ^= k;
  72. data += 4;
  73. len -= 4;
  74. }
  75. // Handle the last few bytes of the input array
  76. switch(len)
  77. {
  78. case 3: h ^= data[2] << 16;
  79. case 2: h ^= data[1] << 8;
  80. case 1: h ^= data[0];
  81. h *= m;
  82. };
  83. // Do a few final mixes of the hash to ensure the last few
  84. // bytes are well-incorporated.
  85. h ^= h >> 13;
  86. h *= m;
  87. h ^= h >> 15;
  88. return h;
  89. }
  90. //-----------------------------------------------------------------------------
  91. /// FNV-1a hash, 32 bit
  92. inline uint32_t fnv1a_32(const void* key, size_t len)
  93. {
  94. CE_ASSERT(key != NULL, "Key must be != NULL");
  95. // FNV-1a
  96. uint32_t hash = FNV1A_OFFSET_BASIS_32;
  97. for (size_t i = 0; i < len; i++)
  98. {
  99. unsigned char* k = (unsigned char*)key;
  100. hash ^= k[i];
  101. hash *= FNV1A_PRIME_32;
  102. }
  103. return hash;
  104. }
  105. //-----------------------------------------------------------------------------
  106. /// FNV-1a hash, 64 bit
  107. inline uint64_t fnv1a_64(const void* key, size_t len)
  108. {
  109. CE_ASSERT(key != NULL, "Key must be != NULL");
  110. // FNV-1a
  111. uint64_t hash = FNV1A_OFFSET_BASIS_64;
  112. for (size_t i = 0; i < len; i++)
  113. {
  114. unsigned char* k = (unsigned char*)key;
  115. hash ^= k[i];
  116. hash *= FNV1A_PRIME_64;
  117. }
  118. return hash;
  119. }
  120. } // namespace hash
  121. } // namespace crown