哈希指针值指针

2023-09-11 23:02:47 作者:碧海蓝天钻戒白婚纱我的他

有时候,你需要一个指针的哈希函数;不是对象的指针指向的,但指针本身。很多时候,人们只是踢和使用指针值为整数,砍掉一些高位,使之适合,也许移出称为零位在底部。的是,指针值不一定均匀分布在code空间;事实上,如果你的分配程序做自己的工作,有一个极好的机会,他们都聚集并拢。

Sometimes you need to take a hash function of a pointer; not the object the pointer points to, but the pointer itself. Lots of the time, folks just punt and use the pointer value as an integer, chop off some high bits to make it fit, maybe shift out known-zero bits at the bottom. Thing is, pointer values aren't necessarily well-distributed in the code space; in fact, if your allocator is doing its job, there's an excellent chance they're all clustered close together.

所以,我的问题是,有没有人开发的哈希函数,它是良好的吗?以一个32位或64位的值,它可能真实在了这12位熵的地方的和s $ P $填充它均匀地分布在一个32位数字的空间。

So, my question is, has anyone developed hash functions that are good for this? Take a 32- or 64-bit value that's maybe got 12 bits of entropy in it somewhere and spread it evenly across a 32-bit number space.

推荐答案

此页面列出了可能的几种方法使用。其中之一,由于是Knuth,是一个简单的如通过2654435761相乘(在32位),但为散列结果产生如果键而变化的高位。在三分球的情况下,这是一个难得的足够的情况。

This page lists several methods that might be of use. One of them, due to Knuth, is a simple as multiplying (in 32 bits) by 2654435761, but "Bad hash results are produced if the keys vary in the upper bits." In the case of pointers, that's a rare enough situation.

这里是一些算法,包括性能测试。

Here are some more algorithms, including performance tests.

看来这个神奇的词是整数散列。

It seems that the magic words are "integer hashing".