Added FNV1a/MurmurHash3/CityHash
This commit is contained in:
parent
f7dce03048
commit
a1cd09b5d4
27
src/hash/FNV1a.cpp
Normal file
27
src/hash/FNV1a.cpp
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
#include<hgl/util/hash/Hash.h>
|
||||||
|
|
||||||
|
namespace hgl
|
||||||
|
{
|
||||||
|
namespace util
|
||||||
|
{
|
||||||
|
namespace
|
||||||
|
{
|
||||||
|
uint32_t FNV1aHash(const void *key, int len)
|
||||||
|
{
|
||||||
|
//本代码来自Github Copilot
|
||||||
|
|
||||||
|
//FNV-1a 是一种简单且高效的哈希算法,适用于大多数场景。它具有良好的分布性和较快的计算速度。
|
||||||
|
|
||||||
|
const uint8_t *data = (const uint8_t *)key;
|
||||||
|
uint32_t hash = 2166136261u;
|
||||||
|
|
||||||
|
for (int i = 0; i < len; ++i) {
|
||||||
|
hash ^= data[i];
|
||||||
|
hash *= 16777619u;
|
||||||
|
}
|
||||||
|
|
||||||
|
return hash;
|
||||||
|
}
|
||||||
|
}//namespace
|
||||||
|
}//namespace util
|
||||||
|
}//namespace hgl
|
5
src/hash/GoogleCityHash.cpp
Normal file
5
src/hash/GoogleCityHash.cpp
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
// source codes repository: https://github.com/google/cityhash.git
|
||||||
|
|
||||||
|
// vcpkg: cityhash, cityhash[sse]
|
||||||
|
|
||||||
|
|
65
src/hash/MurmurHash3.cpp
Normal file
65
src/hash/MurmurHash3.cpp
Normal file
@ -0,0 +1,65 @@
|
|||||||
|
#include<hgl/util/hash/Hash.h>
|
||||||
|
|
||||||
|
namespace hgl
|
||||||
|
{
|
||||||
|
namespace util
|
||||||
|
{
|
||||||
|
namespace
|
||||||
|
{
|
||||||
|
//本代码来自Github Copilot
|
||||||
|
//MurmurHash 是一种高性能的哈希算法,特别适用于哈希表。它具有良好的分布性和较低的碰撞率。
|
||||||
|
|
||||||
|
uint32_t MurmurHash3(const void *key, int len, uint32_t seed)
|
||||||
|
{
|
||||||
|
const uint8_t *data = (const uint8_t *)key;
|
||||||
|
const int nblocks = len / 4;
|
||||||
|
uint32_t h1 = seed;
|
||||||
|
|
||||||
|
const uint32_t c1 = 0xcc9e2d51;
|
||||||
|
const uint32_t c2 = 0x1b873593;
|
||||||
|
|
||||||
|
// Body
|
||||||
|
const uint32_t *blocks = (const uint32_t *)(data + nblocks * 4);
|
||||||
|
for (int i = -nblocks; i; i++) {
|
||||||
|
uint32_t k1 = blocks[i];
|
||||||
|
|
||||||
|
k1 *= c1;
|
||||||
|
k1 = (k1 << 15) | (k1 >> (32 - 15));
|
||||||
|
k1 *= c2;
|
||||||
|
|
||||||
|
h1 ^= k1;
|
||||||
|
h1 = (h1 << 13) | (h1 >> (32 - 13));
|
||||||
|
h1 = h1 * 5 + 0xe6546b64;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Tail
|
||||||
|
const uint8_t *tail = (const uint8_t *)(data + nblocks * 4);
|
||||||
|
uint32_t k1 = 0;
|
||||||
|
|
||||||
|
switch (len & 3)
|
||||||
|
{
|
||||||
|
case 3:
|
||||||
|
k1 ^= tail[2] << 16;
|
||||||
|
case 2:
|
||||||
|
k1 ^= tail[1] << 8;
|
||||||
|
case 1:
|
||||||
|
k1 ^= tail[0];
|
||||||
|
k1 *= c1;
|
||||||
|
k1 = (k1 << 15) | (k1 >> (32 - 15));
|
||||||
|
k1 *= c2;
|
||||||
|
h1 ^= k1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Finalization
|
||||||
|
h1 ^= len;
|
||||||
|
h1 ^= h1 >> 16;
|
||||||
|
h1 *= 0x85ebca6b;
|
||||||
|
h1 ^= h1 >> 13;
|
||||||
|
h1 *= 0xc2b2ae35;
|
||||||
|
h1 ^= h1 >> 16;
|
||||||
|
|
||||||
|
return h1;
|
||||||
|
}
|
||||||
|
}//namespace
|
||||||
|
}//namespace util
|
||||||
|
}//namespace hgl
|
Loading…
x
Reference in New Issue
Block a user