What integer hash function are good that accepts an integer hash key?

前端 未结 11 1611
孤街浪徒
孤街浪徒 2020-11-22 17:23

What integer hash function are good that accepts an integer hash key?

11条回答
  •  伪装坚强ぢ
    2020-11-22 17:50

    Knuth's multiplicative method:

    hash(i)=i*2654435761 mod 2^32
    

    In general, you should pick a multiplier that is in the order of your hash size (2^32 in the example) and has no common factors with it. This way the hash function covers all your hash space uniformly.

    Edit: The biggest disadvantage of this hash function is that it preserves divisibility, so if your integers are all divisible by 2 or by 4 (which is not uncommon), their hashes will be too. This is a problem in hash tables - you can end up with only 1/2 or 1/4 of the buckets being used.

提交回复
热议问题