c - 关于hashmap(khash)中使用的哈希函数?

标签 c 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;
}

最佳答案

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

关于c - 关于hashmap(khash)中使用的哈希函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54936008/

相关文章:

c - C 标准库中的 assert.h 中的 assert() 是否支持某种失败的断言处理程序?

c++ - 写入文件原始磁盘扇区

java - 使用嵌套的 HashMap 是一种不好的做法吗?

c++ - 一个哈希集可以找到 O(1) 的最小或最大元素?

c - Makefile 给出重复错误 C

c - 在 Win32 的 ListView 中保持行永久选中

java - 同步不经常更新的 hashmap 的最佳方式

java - 应为 BEGIN_OBJECT,但在第 1 行第 13 列为 BEGIN_ARRAY

c - 如何改进 C 程序中的拼写检查时间?

java使用两个哈希表比一个更好?