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

ac at codespeak.net ac at codespeak.net
Wed Jul 20 14:35:05 CEST 2005


Author: ac
Date: Wed Jul 20 14:35:05 2005
New Revision: 14813

Modified:
   pypy/dist/pypy/rpython/rlist.py
   pypy/dist/pypy/rpython/test/test_rlist.py
Log:
Implement list multiplication.

Modified: pypy/dist/pypy/rpython/rlist.py
==============================================================================
--- pypy/dist/pypy/rpython/rlist.py	(original)
+++ pypy/dist/pypy/rpython/rlist.py	Wed Jul 20 14:35:05 2005
@@ -202,6 +202,10 @@
             llfn = ll_delitem
         return hop.gendirectcall(llfn, v_lst, v_index)
 
+    def rtype_mul((r_lst, r_int), hop):
+        v_lst, v_factor = hop.inputargs(r_lst, Signed)
+        return hop.gendirectcall(ll_mul, v_lst, v_factor)
+    
 class __extend__(pairtype(ListRepr, SliceRepr)):
 
     def rtype_getitem((r_lst, r_slic), hop):
@@ -541,6 +545,23 @@
 
 TEMP = GcArray(Ptr(rstr.STR))
 
+def ll_mul(l, f):
+    items = l.items
+    length = len(items)
+    if lenght == 0 or f <= 0:
+        return ll_newlist(typeOf(l), 0)
+
+    resultlen = length * f
+    new_lst = ll_newlist(typeOf(l), resultlen)
+    i = 0
+    new_items = new_lst.items
+    j = 0
+    while j < resultlen:
+        while i < length:
+            new_items[i + j] = items[i]
+            i += 1
+        j += length
+    return new_lst
         
         
 # ____________________________________________________________
@@ -575,6 +596,8 @@
     return v_result
 
 def ll_alloc_and_set(LISTPTR, count, item):
+    if count < 0:
+        count = 0
     l = malloc(LISTPTR.TO)
     l.items = malloc(LISTPTR.TO.items.TO, count)
     i = 0

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	Wed Jul 20 14:35:05 2005
@@ -340,3 +340,17 @@
         return lst[-1] * lst2[-1]
     res = interpret(fn, [5])
     assert res == 42
+
+def test_list_multiply():
+    def fn(i):
+        lst = [i] * i
+        return len(lst)
+    for arg in (1, 9, 0, -1, -27):
+        res = interpret(fn, [arg])
+        assert res == fn(arg)
+    def fn(i):
+        lst = [i, i + 1] * i
+        return len(lst)
+    for arg in (1, 9, 0, -1, -27):
+        res = interpret(fn, [arg])
+        assert res == fn(arg)



More information about the Pypy-commit mailing list