java - Java 中的成对独立哈希函数

标签 java hash universal-hashing

我正在寻找一种快速、简单的方法来使用 (universal) family of pairwise independent hash functions在我的 Java 项目中。

理想情况下,我会有一些对象 UniversalFamily (代表 Family),它会使用对整数进行哈希处理的 hash() 方法返回对象。

使用示例:

// use this object to generate pairwise independent hash functions
UniversalFamily family = new UniversalFamily();

// these objects represent the pairwise independent hash functions
HashF hashF1 = fam.getHashFunction();
HashF hashF2 = fam.getHashFunction();
// ...

/* here the hash functions are being used to hash the integers 1, 2 and 
   1337, the return values (not stored) are the results of the 
   corresponding hash functions. */
hashF1.hash(1);
hashF1.hash(2);
hashF2.hash(1337);
// ...

在我开始修补之前,是否有类似的东西已经可用?

最佳答案

使用这样的东西:

/* Used to generate and encapsulate pairwise independent hash functions.
See see https://people.csail.mit.edu/ronitt/COURSE/S12/handouts/lec5.pdf , claim 5 for more information.
 */
private static class HashF {

    private final int a;
    private final int b;
    private final int p = 1610612741; // prime

    HashF(int a, int b) {
        Random rand = new Random();

        this.a = rand.nextInt(p);
        this.b = rand.nextInt(p);
    }

    // hashes integers
    public int hash(int x) {
        return (a*x + b) % p;
    }

}

关于java - Java 中的成对独立哈希函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46917625/

相关文章:

java - 从匿名类获取对包含实例的引用

java - 数独求解器调试

java - 检测套接字中的断开连接

python - 如何在 Python 中迭代哈希中的列表?

c++ - 使用 lambda 创建 unordered_set

c++ - 如何生成64位随机数?

algorithm - 普遍的哈希误解

java - 在 Apache-Cassandra 0.8.2 中插入 JSON 字符串

python - 根据 numpy 数组中的行生成唯一值

data-structures - 通用哈希的基础知识,如何确保可访问性