Python frozenset hashing algorithm / implementation

后端 未结 3 977
[愿得一人]
[愿得一人] 2020-12-08 20:33

I\'m currently trying to understand the mechanism behind the hash function defined for Python\'s built-in frozenset data type. The implementation is shown at t

3条回答
  •  一整个雨季
    2020-12-08 21:29

    In

    (h ^ (h << 16) ^ 89869747) * 3644798167
    

    the multiplicative integer is a large prime to reduce collisions. This is especially relevant since the operation is under modulo.

    The rest is probably arbitrary; I see no reason for the 89869747 to be specific. The most important usage you would get out of that is enlarging hashes of small numbers (most integers hash to themselves). This prevents high collisions for sets of small integers.

    That's all I can think of. What do you need this for?

提交回复
热议问题