1

我需要为字符串标识符列表创建一个完美的哈希,所以在开始这个实现之前(我以前从未做过)我想知道是否有任何好的框架或好的教程可能有用?

谢谢!

4

1 回答 1

1

我使用 Austin Appleby 编写的 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;
}

但最终你对散列函数的选择取决于质量和速度之间的权衡。

于 2012-03-26T17:52:14.900 回答