是否存在具有以下属性的哈希函数?
int32 hash(int32, int32)
如果我是正确的,这样的功能可以实现以下目标
到目前为止,我发现的最好的是4x4位矩阵的乘法,但这很难实现并将空间减少到16位。
我很感激任何帮助。
答案 0 :(得分:1)
这就是我提出的(用Java编写)。 基本思路是将32bit-int拆分为2个数字。较旧的位总和包括乘法效应。较小的位跟踪乘法效应。 有用。它具有良好的分布 - 也可以反对像(0,1),(1,0)这样的常见参数。
public class AssociativelyMergedIntegers {
/** biggest unsigned 16-bit prime */
private static final int PRIME = 65521;
/** associative, not commutative hash function */
public static int merged(int first, int second) {
int firstFactor = remainderOf(first & 0x0000FFFF);
int secondFactor = remainderOf(second & 0x0000FFFF);
int firstSum = remainderOf(first >>> 16 & 0x0000FFFF);
int secondSum = remainderOf(second >>> 16 & 0x0000FFFF);
int resultSum = remainderOf(firstSum + (long) firstFactor * secondSum);
int resultFactor = remainderOf((long) firstFactor * secondFactor);
return resultSum << 16 ^ resultFactor;
}
private static int remainderOf(long number) {
int rest = (int) (number % PRIME);
return rest == 0
? PRIME - 2
: rest;
}
}