algorithm - 存储桶实例的哈希键

标签 algorithm hash

在映射 hash key --> bucket instance 时,哪种算法产生最佳分布?

换句话说,假设我有一个哈希函数(可能是 SHA-1)并且我有 n 个桶;我使用什么算法将 key 映射到存储桶?例如。低位、高位,还是其他?

最佳答案

通常,您只需使用桶的数量修改您的哈希值。万一桶的数量是 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.

关于algorithm - 存储桶实例的哈希键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7996580/

相关文章:

java - 从多个 Java 字符串对象创建散列

ruby - 要使用多个键散列的数组

algorithm - 按位和处理添加和删除项目的有效方法

c++ - 对二进制数数组进行排序的时间复杂度

sql-server - 如何在 TSQL 中将一个表中的行均匀分布到另一个表中的行?

c# - 我对这段代码不了解。哈希

java - 如何从 java 计算 html 表中的行数

algorithm - 三角测量 : Find a 3D point minimizing the Distance from N 3D Lines/Rays

asp-classic - 使用 CAPICOM 进行哈希处理和加盐?使用 asp-classic 的 SHA 512

Java - 哈希算法 - 最快的实现