[pypy-svn] r51204 - in pypy/branch/jit-refactoring/pypy/jit/timeshifter: . test

cfbolz at codespeak.net cfbolz at codespeak.net
Sat Feb 2 13:11:27 CET 2008


Author: cfbolz
Date: Sat Feb  2 13:11:27 2008
New Revision: 51204

Added:
   pypy/branch/jit-refactoring/pypy/jit/timeshifter/greenkey.py   (contents, props changed)
   pypy/branch/jit-refactoring/pypy/jit/timeshifter/test/test_greenkey.py   (contents, props changed)
Log:
add a key wrapper class to be able to use lists of genconsts in the
states_dicts as keys


Added: pypy/branch/jit-refactoring/pypy/jit/timeshifter/greenkey.py
==============================================================================
--- (empty file)
+++ pypy/branch/jit-refactoring/pypy/jit/timeshifter/greenkey.py	Sat Feb  2 13:11:27 2008
@@ -0,0 +1,72 @@
+from pypy.rpython.annlowlevel import cachedtype
+from pypy.rlib.rarithmetic import intmask
+from pypy.rlib.unroll import unrolling_iterable
+from pypy.rpython.lltypesystem import lltype
+
+class KeyDesc(object):
+    __metaclass__ = cachedtype
+
+    def __init__(self, RGenOp, *TYPES):
+        self.RGenOp = RGenOp
+        self.TYPES = TYPES
+        TARGETTYPES = []
+        for TYPE in TYPES:
+            # XXX more cases?
+            TARGET = lltype.Signed
+            if TYPE == lltype.Float:
+                TARGET = TYPE
+            TARGETTYPES.append(TARGET)
+
+        iterator = unrolling_iterable(enumerate(TARGETTYPES))
+        length = len(TYPES)
+        def greenhash(self, rgenop):
+            retval = 0x345678
+            mult = 1000003
+            for i, TARGET in iterator:
+                genconst = self.values[i]
+                item = genconst.revealconst(TARGET)
+                retval = intmask((retval ^ hash(item)) * intmask(mult))
+                mult = mult + 82520 + 2*length
+            return retval
+        self.hash = greenhash
+        def greencompare(self, other, rgenop):
+            for i, TARGET in iterator:
+                genconst = self.values[i]
+                item_self = genconst.revealconst(TARGET)
+                genconst = other.values[i]
+                item_other = genconst.revealconst(TARGET)
+                if item_self != item_other:
+                    return False
+            return True
+        self.compare = greencompare
+
+    def _freeze_(self):
+        return True
+
+
+class GreenKey(object):
+    def __init__(self, values, desc, rgenop):
+        self.desc = desc
+        self.values = values
+        self.rgenop = rgenop
+
+    def __eq__(self, other):
+        raise TypeError("don't store GreenKeys in a normal dict")
+
+    def __ne__(self, other):
+        raise TypeError("don't store GreenKeys in a normal dict")
+
+    def __hash__(self):
+        raise TypeError("not hashable")
+
+def greenkey_eq(self, other):
+    assert self.rgenop is other.rgenop
+    if self is other:
+        return True
+    if self.desc is not other.desc:
+        return False
+    return self.desc.compare(self, other, self.rgenop)
+
+def greenkey_hash(self):
+    return self.desc.hash(self, self.rgenop)
+

Added: pypy/branch/jit-refactoring/pypy/jit/timeshifter/test/test_greenkey.py
==============================================================================
--- (empty file)
+++ pypy/branch/jit-refactoring/pypy/jit/timeshifter/test/test_greenkey.py	Sat Feb  2 13:11:27 2008
@@ -0,0 +1,30 @@
+from pypy.jit.timeshifter.greenkey import GreenKey, KeyDesc
+from pypy.jit.timeshifter.greenkey import greenkey_hash, greenkey_eq
+from pypy.jit.codegen.llgraph.rgenop import RGenOp
+from pypy.rlib.objectmodel import r_dict
+from pypy.rpython.lltypesystem import lltype
+
+rgenop = RGenOp()
+
+class TestGreenKeys(object):
+    def newdict(self):
+        return r_dict(greenkey_eq, greenkey_hash)
+
+    def newkey(self, *values):
+        desc = KeyDesc(RGenOp, *[lltype.typeOf(val) for val in values])
+        return GreenKey([rgenop.genconst(val) for val in values], desc, rgenop)
+
+    def test_simple(self):
+        d = self.newdict()
+        d[self.newkey(1, 2)] = 1
+        assert d[self.newkey(1, 2)] == 1
+
+    def test_check_types(self):
+        d = self.newdict()
+        d[self.newkey(1, 2)] = 1
+        assert self.newkey(True, 2) not in d
+
+    def test_check_lengths(self):
+        d = self.newdict()
+        d[self.newkey(1, 2)] = 1
+        assert self.newkey(1, 2, 0) not in d



More information about the Pypy-commit mailing list