]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Fixups to the hash function for frozensets.
authorRaymond Hettinger <python@rcn.com>
Thu, 10 Jun 2004 21:38:41 +0000 (21:38 +0000)
committerRaymond Hettinger <python@rcn.com>
Thu, 10 Jun 2004 21:38:41 +0000 (21:38 +0000)
* 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.

Objects/setobject.c

index fbff0779c212ac47de7714ab34140db02805a7a5..8de04190310e0a8ba9fec7ca7c40787a00b7f5a0 100644 (file)
@@ -663,7 +663,7 @@ frozenset_hash(PyObject *self)
        PySetObject *so = (PySetObject *)self;
        PyObject *key, *value;
        int pos = 0;
-       long hash = 0;
+       long hash = 1905176217L;
 
        if (so->hash != -1)
                return so->hash;
@@ -676,6 +676,9 @@ frozenset_hash(PyObject *self)
                   collapse to only a handful of distinct hash values. */
                hash ^= PyObject_Hash(key) * 3644798167u;
        }
+       hash *= 69069L;
+       if (hash == -1)
+               hash = 590923713L;
        so->hash = hash;
        return hash;
 }