Author: Maciej Fijalkowski <[email protected]>
Branch: numpy-refactor
Changeset: r56929:0dc92c90818f
Date: 2012-08-30 12:53 +0200
http://bitbucket.org/pypy/pypy/changeset/0dc92c90818f/

Log:    hack up to the point of array() working

diff --git a/pypy/module/micronumpy/arrayimpl/__init__.py 
b/pypy/module/micronumpy/arrayimpl/__init__.py
--- a/pypy/module/micronumpy/arrayimpl/__init__.py
+++ b/pypy/module/micronumpy/arrayimpl/__init__.py
@@ -1,8 +1,8 @@
 
 from pypy.module.micronumpy.arrayimpl import scalar, concrete
 
-def create_implementation(shape, dtype):
+def create_implementation(shape, dtype, order):
     if not shape:
         return scalar.Scalar(dtype)
     else:
-        return concrete.ConcreteArray(shape, dtype)
+        return concrete.ConcreteArray(shape, dtype, order)
diff --git a/pypy/module/micronumpy/arrayimpl/base.py 
b/pypy/module/micronumpy/arrayimpl/base.py
--- a/pypy/module/micronumpy/arrayimpl/base.py
+++ b/pypy/module/micronumpy/arrayimpl/base.py
@@ -1,3 +1,10 @@
 
 class BaseArrayImplementation(object):
     pass
+
+class BaseArrayIterator(object):
+    def next(self):
+        raise NotImplementedError # purely abstract base class
+
+    def setitem(self, elem):
+        raise NotImplementedError
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
@@ -1,9 +1,46 @@
 
 from pypy.module.micronumpy.arrayimpl import base
+from pypy.module.micronumpy import support
+
+class ConcreteArrayIterator(base.BaseArrayIterator):
+    def __init__(self, array, dtype):
+        self.array = array
+        self.offset = 0
+        self.dtype = dtype
+        self.element_size = dtype.get_size()
+
+    def setitem(self, elem):
+        self.dtype.setitem(self.array.storage, self.offset, elem)
+
+    def next(self):
+        self.offset += self.element_size
+
+def calc_strides(shape, dtype, order):
+    strides = []
+    backstrides = []
+    s = 1
+    shape_rev = shape[:]
+    if order == 'C':
+        shape_rev.reverse()
+    for sh in shape_rev:
+        strides.append(s * dtype.get_size())
+        backstrides.append(s * (sh - 1) * dtype.get_size())
+        s *= sh
+    if order == 'C':
+        strides.reverse()
+        backstrides.reverse()
+    return strides, backstrides
 
 class ConcreteArray(base.BaseArrayImplementation):
-    def __init__(self, shape, dtype):
+    def __init__(self, shape, dtype, order):
         self.shape = shape
+        self.size = support.product(shape) * dtype.get_size()
+        self.storage = dtype.itemtype.malloc(self.size)
+        self.strides, self.backstrides = calc_strides(shape, dtype, order)
+        self.order = order
 
     def get_shape(self):
         return self.shape
+
+    def create_iter(self, dtype):
+        return ConcreteArrayIterator(self, dtype)
diff --git a/pypy/module/micronumpy/interp_dtype.py 
b/pypy/module/micronumpy/interp_dtype.py
--- a/pypy/module/micronumpy/interp_dtype.py
+++ b/pypy/module/micronumpy/interp_dtype.py
@@ -49,8 +49,8 @@
     def getitem_bool(self, arr, i):
         return self.itemtype.read_bool(arr, i, 0)
 
-    def setitem(self, arr, i, box):
-        self.itemtype.store(arr, i, 0, box)
+    def setitem(self, storage, i, box):
+        self.itemtype.store(storage, i, 0, box)
 
     def fill(self, storage, box, start, stop):
         self.itemtype.fill(storage, self.get_size(), box, start, stop, 0)
diff --git a/pypy/module/micronumpy/strides.py 
b/pypy/module/micronumpy/strides.py
--- a/pypy/module/micronumpy/strides.py
+++ b/pypy/module/micronumpy/strides.py
@@ -48,11 +48,11 @@
     return rstrides, rbackstrides
 
 def is_single_elem(space, w_elem, is_rec_type):
-    from pypy.module.micronumpy.interp_numarray import BaseArray
+    from pypy.module.micronumpy.interp_numarray import W_NDimArray
     if (is_rec_type and space.isinstance_w(w_elem, space.w_tuple)):
         return True
     if (space.isinstance_w(w_elem, space.w_tuple) or
-        isinstance(w_elem, BaseArray) or    
+        isinstance(w_elem, W_NDimArray) or    
         space.isinstance_w(w_elem, space.w_list)):
         return False
     return True
diff --git a/pypy/module/micronumpy/types.py b/pypy/module/micronumpy/types.py
--- a/pypy/module/micronumpy/types.py
+++ b/pypy/module/micronumpy/types.py
@@ -125,8 +125,8 @@
     def _write(self, storage, i, offset, value):
         raw_storage_setitem(storage, i + offset, value)
 
-    def store(self, arr, i, offset, box):
-        self._write(arr.storage, i, offset, self.unbox(box))
+    def store(self, storage, i, offset, box):
+        self._write(storage, i, offset, self.unbox(box))
 
     def fill(self, storage, width, box, start, stop, offset):
         value = self.unbox(box)
@@ -956,10 +956,10 @@
         return interp_boxes.W_VoidBox(arr, 0, arr.dtype)
 
     @jit.unroll_safe
-    def store(self, arr, i, ofs, box):
+    def store(self, storage, i, ofs, box):
         assert isinstance(box, interp_boxes.W_VoidBox)
         for k in range(self.get_element_size()):
-            arr.storage[k + i] = box.arr.storage[k + box.ofs]
+            storage[k + i] = box.arr.storage[k + box.ofs]
 
     @jit.unroll_safe
     def str_format(self, box):
_______________________________________________
pypy-commit mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to