[pypy-commit] pypy numpypy-is_contiguous: refactor calc_steps

mattip noreply at buildbot.pypy.org
Tue Dec 6 21:16:28 CET 2011


Author: mattip
Branch: numpypy-is_contiguous
Changeset: r50224:9166c3425257
Date: 2011-12-06 18:39 +0200
http://bitbucket.org/pypy/pypy/changeset/9166c3425257/

Log:	refactor calc_steps

diff --git a/pypy/module/micronumpy/interp_numarray.py b/pypy/module/micronumpy/interp_numarray.py
--- a/pypy/module/micronumpy/interp_numarray.py
+++ b/pypy/module/micronumpy/interp_numarray.py
@@ -140,6 +140,21 @@
                 space.wrap("total size of new array must be unchanged"))
     return new_shape
 
+def calc_steps(shape, strides):
+    steps = []
+    last_step = 1
+    #Irregardless of order, the strides can itterate faster left to right
+    # or right to left. Take each case seperately.
+    if strides[0] < strides[-1]:
+        for i in range(len(shape)):
+            steps.append(strides[i] / last_step)
+            last_step *= shape[i]
+    else:
+        for i in range(len(shape) - 1, -1, -1):
+            steps.insert(0, strides[i] / last_step)
+            last_step *= shape[i]
+    return steps
+
 #Recalculating strides. Find the steps that the iteration does for each
 #dimension, given the stride and shape. Then try to create a new stride that
 #fits the new shape, using those steps. If there is a shape/step mismatch
@@ -151,14 +166,10 @@
 
     #Assumes that prod(old_shape) ==prod(new_shape), len(old_shape) > 1 and
     # len(new_shape) > 0
-    steps = []
-    last_step = 1
+    steps = calc_steps(old_shape, old_strides)
     oldI = 0
     new_strides = []
     if old_strides[0] < old_strides[-1]:
-        for i in range(len(old_shape)):
-            steps.append(old_strides[i] / last_step)
-            last_step *= old_shape[i]
         cur_step = steps[0]
         n_new_elems_used = 1
         n_old_elems_to_use = old_shape[0]
@@ -177,9 +188,6 @@
                 cur_step = steps[oldI]
                 n_old_elems_to_use *= old_shape[oldI]
     else:
-        for i in range(len(old_shape) - 1, -1, -1):
-            steps.insert(0, old_strides[i] / last_step)
-            last_step *= old_shape[i]
         cur_step = steps[-1]
         n_new_elems_used = 1
         oldI = -1


More information about the pypy-commit mailing list