diff --git a/src/hash/FNV1a.cpp b/src/hash/FNV1a.cpp new file mode 100644 index 0000000..aea54ff --- /dev/null +++ b/src/hash/FNV1a.cpp @@ -0,0 +1,27 @@ +#include + +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 diff --git a/src/hash/GoogleCityHash.cpp b/src/hash/GoogleCityHash.cpp new file mode 100644 index 0000000..19db4b7 --- /dev/null +++ b/src/hash/GoogleCityHash.cpp @@ -0,0 +1,5 @@ +// source codes repository: https://github.com/google/cityhash.git + +// vcpkg: cityhash, cityhash[sse] + + diff --git a/src/hash/MurmurHash3.cpp b/src/hash/MurmurHash3.cpp new file mode 100644 index 0000000..2f717eb --- /dev/null +++ b/src/hash/MurmurHash3.cpp @@ -0,0 +1,65 @@ +#include + +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