[pypy-commit] pypy default: Added hash-Method for small tuples

l.diekmann noreply at buildbot.pypy.org
Wed May 25 16:47:40 CEST 2011


Author: Lukas Diekmann <lukas.diekmann at uni-duesseldorf.de>
Branch: 
Changeset: r44434:d325e8a0c84d
Date: 2011-02-02 11:40 +0100
http://bitbucket.org/pypy/pypy/changeset/d325e8a0c84d/

Log:	Added hash-Method for small tuples

diff --git a/pypy/objspace/std/smalltupleobject.py b/pypy/objspace/std/smalltupleobject.py
--- a/pypy/objspace/std/smalltupleobject.py
+++ b/pypy/objspace/std/smalltupleobject.py
@@ -111,5 +111,18 @@
             return space.w_False
     return space.w_True
 
+def hash__SmallTuple(space, w_tuple):
+    # this is the CPython 2.4 algorithm (changed from 2.3)
+    mult = 1000003
+    x = 0x345678
+    z = w_tuple.length()
+    for w_item in w_tuple.tolist():     #XXX: remove list and run through items directly, later
+        y = space.int_w(space.hash(w_item))
+        x = (x ^ y) * mult
+        z -= 1
+        mult += 82520 + z + z
+    x += 97531
+    return space.wrap(intmask(x))
+
 from pypy.objspace.std import tupletype
 register_all(vars(), tupletype)
diff --git a/pypy/objspace/std/test/test_smalltupleobject.py b/pypy/objspace/std/test/test_smalltupleobject.py
--- a/pypy/objspace/std/test/test_smalltupleobject.py
+++ b/pypy/objspace/std/test/test_smalltupleobject.py
@@ -43,6 +43,11 @@
         b = (1,2,3)
         assert a == b
 
+    def test_hash(self):
+        a = (1,2,3)
+        b = (1,2,3)
+        assert hash(a) == hash(b)
+
 class TestW_SmallTupleObject():
 
     def setup_class(cls):


More information about the pypy-commit mailing list