[pypy-svn] r23290 - in pypy/dist/pypy/rpython: . test

pedronis at codespeak.net pedronis at codespeak.net
Mon Feb 13 19:07:11 CET 2006


Author: pedronis
Date: Mon Feb 13 19:07:08 2006
New Revision: 23290

Modified:
   pypy/dist/pypy/rpython/rlist.py
   pypy/dist/pypy/rpython/rtuple.py
   pypy/dist/pypy/rpython/test/test_rdict.py
Log:
(arre, pedronis)

* factor out list creation code into rlist.newlist(llops, r_list, items_v)

* support for tuples as dict keys in RPython (with test)




Modified: pypy/dist/pypy/rpython/rlist.py
==============================================================================
--- pypy/dist/pypy/rpython/rlist.py	(original)
+++ pypy/dist/pypy/rpython/rlist.py	Mon Feb 13 19:07:08 2006
@@ -966,6 +966,16 @@
 def ll_fixed_items(l):
     return l
 
+def newlist(llops, r_list, items_v):
+    LIST = r_list.LIST
+    cno = inputconst(Signed, len(items_v))
+    v_result = llops.gendirectcall(LIST.ll_newlist, cno)
+    v_func = inputconst(Void, dum_nocheck)
+    for i, v_item in enumerate(items_v):
+        ci = inputconst(Signed, i)
+        llops.gendirectcall(ll_setitem_nonneg, v_func, v_result, ci, v_item)
+    return v_result
+
 def rtype_newlist(hop):
     nb_args = hop.nb_args
     r_list = hop.r_result
@@ -979,15 +989,8 @@
             hop.genop('simple_call', [v_meth, v_item], resulttype = robject.pyobj_repr)
         return v_result
     r_listitem = r_list.item_repr
-    LIST = r_list.LIST
-    cno = hop.inputconst(Signed, nb_args)
-    v_result = hop.gendirectcall(LIST.ll_newlist, cno)
-    v_func = hop.inputconst(Void, dum_nocheck)
-    for i in range(nb_args):
-        ci = hop.inputconst(Signed, i)
-        v_item = hop.inputarg(r_listitem, arg=i)
-        hop.gendirectcall(ll_setitem_nonneg, v_func, v_result, ci, v_item)
-    return v_result
+    items_v = [hop.inputarg(r_listitem, arg=i) for i in range(nb_args)]
+    return newlist(hop.llops, r_list, items_v)
 
 def ll_alloc_and_set(LIST, count, item):
     if count < 0:

Modified: pypy/dist/pypy/rpython/rtuple.py
==============================================================================
--- pypy/dist/pypy/rpython/rtuple.py	(original)
+++ pypy/dist/pypy/rpython/rtuple.py	Mon Feb 13 19:07:08 2006
@@ -1,3 +1,4 @@
+import operator
 from pypy.annotation.pairtype import pairtype
 from pypy.annotation import model as annmodel
 from pypy.objspace.flow.model import Constant
@@ -28,6 +29,59 @@
         keys = [rtyper.makekey(s_item) for s_item in self.items]
         return tuple([self.__class__]+keys)
 
+_gen_eq_function_cache = {}
+_gen_hash_function_cache = {}
+
+def gen_eq_function(items_r):
+    eq_funcs = [r_item.get_ll_eq_function() or operator.eq for r_item in items_r]
+    key = tuple(eq_funcs)
+    try:
+        return _gen_eq_function_cache[key]
+    except KeyError:
+        miniglobals = {}
+        source = """
+def ll_eq(t1, t2):
+    %s
+    return True
+"""
+        body = []
+        for i, eq_func in enumerate(eq_funcs):
+            miniglobals['eq%d' % i] = eq_func
+            body.append("if not eq%d(t1.item%d, t2.item%d): return False" % (i, i, i))
+        body = ('\n'+' '*4).join(body)
+        source = source % body
+        print source
+        exec source in miniglobals
+        ll_eq = miniglobals['ll_eq']
+        _gen_eq_function_cache[key] = ll_eq
+        return ll_eq
+
+def gen_hash_function(items_r):
+    hash_funcs = [r_item.get_ll_hash_function() for r_item in items_r]
+    key = tuple(hash_funcs)
+    try:
+        return _gen_hash_function_cache[key]
+    except KeyError:
+        miniglobals = {}
+        source = """
+def ll_hash(t):
+    retval = 0
+    %s
+    return retval
+"""
+        body = []
+        for i, hash_func in enumerate(hash_funcs):
+            miniglobals['hash%d' % i] = hash_func
+            body.append("retval ^= hash%d(t.item%d)" % (i,i))
+        body = ('\n'+' '*4).join(body)
+        source = source % body
+        print source
+        exec source in miniglobals
+        ll_hash = miniglobals['ll_hash']
+        _gen_hash_function_cache[key] = ll_hash
+        return ll_hash
+
+
 
 class TupleRepr(Repr):
 
@@ -63,6 +117,12 @@
     #def get_eqfunc(self):
     #    return inputconst(Void, self.item_repr.get_ll_eq_function())
 
+    def get_ll_eq_function(self):
+        return gen_eq_function(self.items_r)
+
+    def get_ll_hash_function(self):
+        return gen_hash_function(self.items_r)    
+
     def rtype_len(self, hop):
         return hop.inputconst(Signed, len(self.items_r))
 

Modified: pypy/dist/pypy/rpython/test/test_rdict.py
==============================================================================
--- pypy/dist/pypy/rpython/test/test_rdict.py	(original)
+++ pypy/dist/pypy/rpython/test/test_rdict.py	Mon Feb 13 19:07:08 2006
@@ -550,3 +550,14 @@
 
     assert r_AB_dic.lowleveltype == r_BA_dic.lowleveltype
 
+def test_tuple_dict():
+    def f(i):
+        d = {}
+        d[(1, 2)] = 4
+        d[(1, 3)] = 6
+        return d[(1, i)]
+
+    res = interpret(f, [2])
+    assert res == f(2)
+        
+        



More information about the Pypy-commit mailing list