BDE 4.14.0 Production release
|
Provide a utility of hash functions.
This component provides a namespace class, HashUtil
, for hash functions. At the current time it has one hash function, HashUtil::computeHash
, which will hash most fundamental types, and pointers, rapidly. Note that when a pointer is passed, only the bits in the pointer itself are hashed, the memory the pointer refers to is not examined.
This section illustrates intended usage of this component.
Suppose we want to analyze our hash function by seeing how it distributes integers across buckets. We will declare 64 buckets, and distribute hits among the bucket by indexing them with the low order 6 bits of the hash. Then we will display the distribution of hits in each bucket, to see if the hash function is distributing them evenly.
First, we hash on the values of i in the range [ 0, 1 << 15 )
:
Then, we will hash on the values of 4 * i
for i in the range [ 0, 1 << 15 )
. This is interesting because pointers will often be 4-byte aligned and have the 2 low-order bits always zero, so this will be a simulation of that:
Next, we will xor the bottom 30 bits of the hash into the bottom 6 bits, so we'll be observing more of the whole word:
Now, bear in mind that an identity hash will perform very optimally on the first and third tests we did. This time we will take the difference between the current hash and the previous one, a test for which the identity function would perform abominably:
Finally, take the difference between the previous hash and the current one, only this time, instead of subtracting, take a bitwise xor:
The output produced by this usage example follows: