关于哈希图(khash)中使用的哈希函数?

时间:2019-02-28 23:45:50

标签: hashmap hashtable

我看到以下哈希函数。但是我不知道为什么用这种方式定义它们。有人知道我在哪里可以找到这些哈希函数的解释吗?谢谢。

https://github.com/attractivechaos/klib/blob/master/khash.h#L368

#define kh_int64_hash_func(key) (khint32_t)((key)>>33^(key)^(key)<<11)

static kh_inline khint_t __ac_X31_hash_string(const char *s)
{
    khint_t h = (khint_t)*s;
    if (h) for (++s ; *s; ++s) h = (h << 5) - h + (khint_t)*s;
    return h;
}
static kh_inline khint_t __ac_Wang_hash(khint_t key)
{
    key += ~(key << 15);
    key ^=  (key >> 10);
    key +=  (key << 3);
    key ^=  (key >> 6);
    key += ~(key << 11);
    key ^=  (key >> 16);
    return key;
}

1 个答案:

答案 0 :(得分:1)

这个哈希函数似乎最初是由 Thomas Wang 发布的。原始网站不再可用,但您可以在 Wayback Machine 上找到它。 gist.github.com 上还有一个 reformatted version of the same text,我看到它链接到 another hashtable library。评论还链接了您可能会感兴趣的 avalanche analysis on Wang's function