[pypy-commit] pypy numpy-refactor: multidim iterator, start passing more tests

fijal noreply at buildbot.pypy.org
Thu Aug 30 20:37:57 CEST 2012


Author: Maciej Fijalkowski <fijall at gmail.com>
Branch: numpy-refactor
Changeset: r57033:2a449b5fea59
Date: 2012-08-30 20:37 +0200
http://bitbucket.org/pypy/pypy/changeset/2a449b5fea59/

Log:	multidim iterator, start passing more tests

diff --git a/pypy/module/micronumpy/arrayimpl/concrete.py b/pypy/module/micronumpy/arrayimpl/concrete.py
--- a/pypy/module/micronumpy/arrayimpl/concrete.py
+++ b/pypy/module/micronumpy/arrayimpl/concrete.py
@@ -26,7 +26,7 @@
     def done(self):
         return self.offset >= self.size
 
-class OneDimViewIterator(base.BaseArrayIterator):
+class OneDimViewIterator(ConcreteArrayIterator):
     def __init__(self, array):
         self.array = array
         self.offset = array.start
@@ -35,12 +35,6 @@
         self.index = 0
         self.size = array.shape[0]
 
-    def setitem(self, elem):
-        self.array.setitem(self.offset, elem)
-
-    def getitem(self):
-        return self.array.getitem(self.offset)
-
     def next(self):
         self.offset += self.skip
         self.index += 1
@@ -48,6 +42,35 @@
     def done(self):
         return self.index >= self.size
 
+class MultiDimViewIterator(ConcreteArrayIterator):
+    def __init__(self, array):
+        self.indexes = [0] * len(array.shape)
+        self.array = array
+        self.shape = array.shape
+        self.offset = array.start
+        self.shapelen = len(self.shape)
+        self._done = False
+        self.strides = array.strides
+        self.backstrides = array.backstrides
+
+    @jit.unroll_safe
+    def next(self):
+        offset = self.offset
+        for i in range(self.shapelen - 1, -1, -1):
+            if self.indexes[i] < self.shape[i] - 1:
+                self.indexes[i] += 1
+                offset += self.strides[i]
+                break
+            else:
+                self.indexes[i] = 0
+                offset -= self.backstrides[i]
+        else:
+            self._done = True
+        self.offset = offset
+
+    def done(self):
+        return self._done
+
 
 def calc_strides(shape, dtype, order):
     strides = []
@@ -105,6 +128,10 @@
         return loop.setslice(impl, self)
 
     def setslice(self, arr):
+        if arr.is_scalar():
+            self.fill(arr.get_scalar_value())
+            return
+        assert isinstance(arr, ConcreteArray)
         if arr.storage == self.storage:
             arr = arr.copy()
         loop.setslice(self, arr)
@@ -241,9 +268,9 @@
         self.start = start
 
     def fill(self, box):
-        xxx
+        loop.fill(self, box)
 
     def create_iter(self):
         if len(self.shape) == 1:
             return OneDimViewIterator(self)
-        xxx
+        return MultiDimViewIterator(self)
diff --git a/pypy/module/micronumpy/loop.py b/pypy/module/micronumpy/loop.py
--- a/pypy/module/micronumpy/loop.py
+++ b/pypy/module/micronumpy/loop.py
@@ -51,3 +51,9 @@
         cur_value = func(calc_dtype, cur_value, rval)
         obj_iter.next()
     return cur_value
+
+def fill(arr, box):
+    arr_iter = arr.create_iter()
+    while not arr_iter.done():
+        arr_iter.setitem(box)
+        arr_iter.next()


More information about the pypy-commit mailing list