【问题标题】:Pairwise independent hash functions in JavaJava中的成对独立哈希函数
【发布时间】:2018-04-05 16:04:55
【问题描述】:

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

理想情况下,我应该有一些对象 UniversalFamily(代表家庭),它会使用 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);
// ...

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

【问题讨论】:

  • hashF2.hash(1337); 在这个例子中1337 是什么?
  • @JigarJoshi 一个整数,我对散列整数感兴趣。
  • 我担心这可能过于宽泛和离题,因为你有点要求推荐。无论如何,我在帮助部分找不到足够的行,并且我个人喜欢你的问题的清晰性(以及我看到你正在编辑的工作量),所以我不会标记它。祝你好运。
  • 好的,这就是你的函数的输入。 commons.apache.org/proper/commons-lang/apidocs/org/apache/… 最接近

标签: java hash universal-hashing


【解决方案1】:

使用这样的东西:

/* 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;
    }

}

【讨论】:

    猜你喜欢
    • 2012-11-25
    • 2013-08-22
    • 1970-01-01
    • 2013-12-26
    • 1970-01-01
    • 2021-06-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多