当涉及映射散列键时,什么算法产生最佳分布 – >斗式实例?
换句话说,假设我有一个散列函数(可能是SHA-1),我有n个桶;我使用什么算法将密钥映射到存储桶?例如.低位,高位,还有别的什么?
最佳答案 通常,您只需使用桶数修改哈希值.在极少数情况下,桶的数量是2的幂,您可以使用按位 – 而不是.
维基百科hash function的摘录:
A common solution is to compute a fixed hash function with a very
large range (say, 0 to 232 − 1), divide the result by n, and use the
division’s remainder. If n is itself a power of 2, this can be done by
bit masking and bit shifting. When this approach is used, the hash
function must be chosen so that the result has fairly uniform
distribution between 0 and n−1, for any n that may occur in the
application. Depending on the function, the remainder may be uniform
only for certain n, e.g. odd or prime numbers.