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

hpk at codespeak.net hpk at codespeak.net
Thu Jun 23 17:07:36 CEST 2005


Author: hpk
Date: Thu Jun 23 17:07:35 2005
New Revision: 13722

Modified:
   pypy/dist/pypy/rpython/rdict.py
   pypy/dist/pypy/rpython/test/test_rdict.py
Log:
(hpk, arre) 

a slightly more clever implementation for StrDict's getitem/setitem 



Modified: pypy/dist/pypy/rpython/rdict.py
==============================================================================
--- pypy/dist/pypy/rpython/rdict.py	(original)
+++ pypy/dist/pypy/rpython/rdict.py	Thu Jun 23 17:07:35 2005
@@ -1,8 +1,8 @@
 from pypy.annotation.pairtype import pairtype
 from pypy.annotation import model as annmodel
 from pypy.objspace.flow.model import Constant
-from pypy.rpython import rmodel, lltype 
-from pypy.rpython.rstr import STR, string_repr, ll_strhash 
+from pypy.rpython import rmodel, lltype, rstr
+from pypy.rpython.rstr import STR, string_repr
 
 # ____________________________________________________________
 #
@@ -118,15 +118,38 @@
 #  be direct_call'ed from rtyped flow graphs, which means that they will
 #  get flowed and annotated, mostly with SomePtr.
 
+deleted_entry_marker = lltype.malloc(STR, 0, immortal=True)
+
 def ll_strdict_len(d):
     return d.num_used_entries 
 
 def ll_strdict_getitem(d, key): 
-    return d.entries[0].value 
+    entry = ll_strdict_lookup(d, key) 
+    if entry.key: 
+        return entry.value 
+    else: 
+        raise KeyError 
 
 def ll_strdict_setitem(d, key, value): 
-    d.entries[0].key = key 
-    d.entries[0].value = value 
+    entry = ll_strdict_lookup(d, key)
+    if not entry.key: 
+        entry.key = key 
+    entry.value = value 
+
+def ll_strdict_lookup(d, key): 
+    keyhash = rstr.ll_strhash(key) 
+    n = len(d.entries) 
+    index = keyhash & (n - 1)  
+    while 1: 
+        entry = d.entries[index]
+        if entry.key: 
+            if rstr.ll_streq(entry.key, key): 
+                break 
+            # XXX check for delitem 
+            index = (index + 1) & (n-1)
+        else: 
+            break 
+    return entry 
 
 # ____________________________________________________________
 #

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	Thu Jun 23 17:07:35 2005
@@ -11,3 +11,11 @@
 
     res = interpret(createdict, [42])
     assert res == 42
+
+def test_dict_getitem_setitem(): 
+    def func(i): 
+        d = {'hello' : i}
+        d['world'] = i + 1
+        return d['hello'] * d['world'] 
+    res = interpret(func, [6])
+    assert res == 42



More information about the Pypy-commit mailing list