c++ - Knuth 的乘法散列函数通过位移位

标签 c++ algorithm hash

我尝试通过 A = 2654435769 的位移位来实现 Knuth 的乘法算法 和 2^p 个元素的散列大小 但是非移位和移位算法给出不同的结果

我是如何尝试实现这两个算法的:



    template
    int mult_hash_simple(int key)
    {
        double A = 0.61803398863412439823150634765625;
        double exp = A*key;
        return max_key*(exp - (int) exp);
    }

    template
    int mult_hash_advanced(int key)
    {
    //  const int w = 32;
        const unsigned long long A = 2654435769;
        unsigned long long r0 = key*A;

        return r0 & (  (1 

UPD:更新了一些 var 类型。问题依然存在

最佳答案

以下函数具有等同于 mult_hash_simple 函数的高级哈希逻辑。

template<int max_key_power, int p>
int mult_hash_advanced(int key)
{
//  const int w = 32;
    const long long w_bit = 4294967295; // Integer representation of binary value that has last 32 ( which is w value )  bits as 1
    const int A = 2654435769; 
    long long r0 = key*A;


    return (( r0 & w_bit ) >> ( 32 - p ) ) ;
}

关于c++ - Knuth 的乘法散列函数通过位移位,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28463794/

相关文章:

ruby - 如何在不出现 'undefined method' 错误的情况下访问数组深处的元素

r - 具有任意长度查询的查找表,无需在 R 中使用 for 循环

c++ - VirtualFree 是否解锁 VirtualLock?

algorithm - 表明,给定一个查询点 q,可以在时间 O(log n) 内测试 q 是否在 P 内

algorithm - 在matlab中数值求解二重积分

c++ - 我可以像这样使用 mem_fun 吗?

c++ - 在openCV中访问特定像素的强度值(灰度图像)

c++ - 插入到只移动类型的 vector 中

c++ - c++ 中 java 的完整代码

python - 为小 k(<=5) 从 k 明智的独立散列族生成散列函数的最快方法