c# - HashHelpers.GetPrime 中这一行的目的是什么?

标签 c# .net dictionary

我正在研究 .NET 的字典实现,发现了一个我很好奇的函数:HashHelpers.GetPrime

它所做的大部分工作都非常简单,它会寻找一个大于某个最小值的质数,并将其作为参数传递给它,显然是为了在类似哈希表的结构中用作多个桶的特定目的。但有一个神秘的部分:

if (HashHelpers.IsPrime(j) && (j - 1) % 101 != 0)
{
    return j;
}

(j - 1) % 101 != 0 检查的目的是什么?即,为什么我们显然希望避免使用比 101 的倍数多 1 的桶?

最佳答案

comments解释得很好:

‘InitHash’ is basically an implementation of classic DoubleHashing (see http://en.wikipedia.org/wiki/Double_hashing)

1) The only ‘correctness’ requirement is that the ‘increment’ used to probe a. Be non-zero b. Be relatively prime to the table size ‘hashSize’. (This is needed to insure you probe all entries in the table before you ‘wrap’ and visit entries already probed)

2) Because we choose table sizes to be primes, we just need to insure that the increment is 0 < incr < hashSize

Thus this function would work: Incr = 1 + (seed % (hashSize-1))

While this works well for ‘uniformly distributed’ keys, in practice, non-uniformity is common. In particular in practice we can see ‘mostly sequential’ where you get long clusters of keys that ‘pack’. To avoid bad behavior you want it to be the case that the increment is ‘large’ even for ‘small’ values (because small values tend to happen more in practice). Thus we multiply ‘seed’ by a number that will make these small values bigger (and not hurt large values). We picked HashPrime (101) because it was prime, and if ‘hashSize-1’ is not a multiple of HashPrime (enforced in GetPrime), then incr has the potential of being every value from 1 to hashSize-1. The choice was largely arbitrary.

关于c# - HashHelpers.GetPrime 中这一行的目的是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25312048/

相关文章:

c# - FFMediaElement : How to stream from CamLink 4K?

c# - 使用 Windows 安装程序部署 Web 应用程序期间出错

c# - 有什么方法可以限制类型变量可能持有的类型?

c# - Decimal.Parse 和不正确的字符串格式错误

.net - 针对锁定的文件测试您的代码?

c# - 何时可以覆盖与 ViewState 关联的功能并使其使用 ControlState?

.net - Sitecore Analytics 机器人 SessionTimeout 导致 session 过早超时

delphi - 使用 TDictionary "for...in"

python - 如何使用 Python 集并将字符串作为字典值添加到其中

c++ - 如何在 STL map 上使用 binary_search