Commit 7ba1c14c authored by Raymond Hettinger's avatar Raymond Hettinger

Fixups to the hash function for frozensets.

* Non-zero initial value so that hash(frozenset()) != hash(0).
* Final permutation to differentiate nested sets.
* Add logic to make sure that -1 is not a possible hash value.
parent 65f28513
...@@ -663,7 +663,7 @@ frozenset_hash(PyObject *self) ...@@ -663,7 +663,7 @@ frozenset_hash(PyObject *self)
PySetObject *so = (PySetObject *)self; PySetObject *so = (PySetObject *)self;
PyObject *key, *value; PyObject *key, *value;
int pos = 0; int pos = 0;
long hash = 0; long hash = 1905176217L;
if (so->hash != -1) if (so->hash != -1)
return so->hash; return so->hash;
...@@ -676,6 +676,9 @@ frozenset_hash(PyObject *self) ...@@ -676,6 +676,9 @@ frozenset_hash(PyObject *self)
collapse to only a handful of distinct hash values. */ collapse to only a handful of distinct hash values. */
hash ^= PyObject_Hash(key) * 3644798167u; hash ^= PyObject_Hash(key) * 3644798167u;
} }
hash *= 69069L;
if (hash == -1)
hash = 590923713L;
so->hash = hash; so->hash = hash;
return hash; return hash;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment