我需要为字符串标识符列表创建一个完美的哈希,所以在开始这个实现之前(我之前从未做过)我想知道是否有任何好的框架或好的教程可能有用?
谢谢!
答案 0 :(得分:1)
我使用奥斯汀·阿普尔比写的MurmurHash:
unsigned int Hash (const char* buffer, size_t size, unsigned seed)
{
const unsigned int m = 0x5bd1e995;
const int r = 2;
unsigned int h = seed ^ (unsigned int)size;
const unsigned char* data = (const unsigned char*)buffer;
while(size >= 4)
{
unsigned int k;
k = data[0];
k |= data[1] << 8;
k |= data[2] << 16;
k |= data[3] << 24;
k *= m;
k ^= k >> r;
k *= m;
h *= m;
h ^= k;
data += 4;
size -= 4;
}
switch(size)
{
case 3: h ^= data[2] << 16;
case 2: h ^= data[1] << 8;
case 1: h ^= data[0];
h *= m;
}
h ^= h >> 13;
h *= m;
h ^= h >> 15;
return h;
}
但最终你选择的散列函数取决于质量和速度之间的权衡。