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

arigo at codespeak.net arigo at codespeak.net
Thu Jun 9 10:53:32 CEST 2005


Author: arigo
Date: Thu Jun  9 10:53:30 2005
New Revision: 13221

Modified:
   pypy/dist/pypy/rpython/rlist.py
   pypy/dist/pypy/rpython/test/test_rlist.py
Log:
list.delitem; tests.


Modified: pypy/dist/pypy/rpython/rlist.py
==============================================================================
--- pypy/dist/pypy/rpython/rlist.py	(original)
+++ pypy/dist/pypy/rpython/rlist.py	Thu Jun  9 10:53:30 2005
@@ -88,6 +88,14 @@
             llfn = ll_setitem
         return hop.gendirectcall(llfn, v_lst, v_index, v_item)
 
+    def rtype_delitem((r_lst, r_int), hop):
+        v_lst, v_index = hop.inputargs(r_lst, Signed)
+        if hop.args_s[1].nonneg:
+            llfn = ll_delitem_nonneg
+        else:
+            llfn = ll_delitem
+        return hop.gendirectcall(llfn, v_lst, v_index)
+
 class __extend__(pairtype(ListRepr, SliceRepr)):
 
     def rtype_getitem((r_lst, r_slic), hop):
@@ -143,13 +151,39 @@
         i += len(l.items)
     return l.items[i].item
 
+def ll_setitem_nonneg(l, i, newitem):
+    l.items[i].item = newitem
+
 def ll_setitem(l, i, newitem):
     if i<0:
         i += len(l.items)
     l.items[i].item = newitem
 
-def ll_setitem_nonneg(l, i, newitem):
-    l.items[i].item = newitem
+def ll_delitem(l, i):
+    if i < 0:
+        i += len(l.items)
+    newlength = len(l.items) - 1
+    newitems = malloc(typeOf(l).TO.items.TO, newlength)
+    j = 0
+    while j < i:
+        newitems[j].item = l.items[j].item
+        j += 1
+    while j < newlength:
+        newitems[j].item = l.items[j+1].item
+        j += 1
+    l.items = newitems
+
+def ll_delitem_nonneg(l, i):
+    newlength = len(l.items) - 1
+    newitems = malloc(typeOf(l).TO.items.TO, newlength)
+    j = 0
+    while j < i:
+        newitems[j].item = l.items[j].item
+        j += 1
+    while j < newlength:
+        newitems[j].item = l.items[j+1].item
+        j += 1
+    l.items = newitems
 
 def ll_concat(l1, l2):
     len1 = len(l1.items)

Modified: pypy/dist/pypy/rpython/test/test_rlist.py
==============================================================================
--- pypy/dist/pypy/rpython/test/test_rlist.py	(original)
+++ pypy/dist/pypy/rpython/test/test_rlist.py	Thu Jun  9 10:53:30 2005
@@ -30,6 +30,21 @@
     assert ll_len(l) == 4
     check_list(l, [42, 43, 44, 45])
 
+def test_rlist_set_del():
+    l = sample_list()
+    ll_setitem(l, -1, 99)
+    check_list(l, [42, 43, 44, 99])
+    ll_setitem_nonneg(l, 1, 77)
+    check_list(l, [42, 77, 44, 99])
+    ll_delitem_nonneg(l, 0)
+    check_list(l, [77, 44, 99])
+    ll_delitem(l, -2)
+    check_list(l, [77, 99])
+    ll_delitem(l, 1)
+    check_list(l, [77])
+    ll_delitem(l, 0)
+    check_list(l, [])
+
 def test_rlist_extend_concat():
     l = sample_list()
     ll_extend(l, l)
@@ -110,4 +125,13 @@
     def dummyfn():
         l = [5, 6, 7, 8, 9]
         return l[:2], l[1:4], l[3:]
-    rtype(dummyfn).view()
+    rtype(dummyfn)
+
+def test_set_del_item():
+    def dummyfn():
+        l = [5, 6, 7]
+        l[1] = 55
+        l[-1] = 66
+        del l[0]
+        del l[-1]
+    rtype(dummyfn)



More information about the Pypy-commit mailing list