Author: Ronan Lamy <ronan.l...@gmail.com> Branch: indexing Changeset: r78560:360ba1502914 Date: 2015-07-16 18:47 +0100 http://bitbucket.org/pypy/pypy/changeset/360ba1502914/
Log: Kill Chunks and simply use a new_view() function instead diff --git a/pypy/module/micronumpy/arrayops.py b/pypy/module/micronumpy/arrayops.py --- a/pypy/module/micronumpy/arrayops.py +++ b/pypy/module/micronumpy/arrayops.py @@ -5,7 +5,7 @@ from pypy.module.micronumpy.base import convert_to_array, W_NDimArray from pypy.module.micronumpy.converters import clipmode_converter from pypy.module.micronumpy.strides import ( - Chunk, Chunks, shape_agreement, shape_agreement_multiple) + Chunk, new_view, shape_agreement, shape_agreement_multiple) from .casting import find_binop_result_dtype, find_result_type @@ -148,7 +148,8 @@ continue chunks[axis] = Chunk(axis_start, axis_start + arr.get_shape()[axis], 1, arr.get_shape()[axis]) - Chunks(chunks).apply(space, res).implementation.setslice(space, arr) + view = new_view(space, res, chunks) + view.implementation.setslice(space, arr) axis_start += arr.get_shape()[axis] return res @@ -162,9 +163,8 @@ shape = [arr.get_shape()[0] * repeats] w_res = W_NDimArray.from_shape(space, shape, arr.get_dtype(), w_instance=arr) for i in range(repeats): - chunks = Chunks([Chunk(i, shape[0] - repeats + i, repeats, - orig_size)]) - view = chunks.apply(space, w_res) + chunks = [Chunk(i, shape[0] - repeats + i, repeats, orig_size)] + view = new_view(space, w_res, chunks) view.implementation.setslice(space, arr) else: axis = space.int_w(w_axis) @@ -176,7 +176,7 @@ for i in range(repeats): chunks[axis] = Chunk(i, shape[axis] - repeats + i, repeats, orig_size) - view = Chunks(chunks).apply(space, w_res) + view = new_view(space, w_res, chunks) view.implementation.setslice(space, arr) return w_res diff --git a/pypy/module/micronumpy/concrete.py b/pypy/module/micronumpy/concrete.py --- a/pypy/module/micronumpy/concrete.py +++ b/pypy/module/micronumpy/concrete.py @@ -1,7 +1,7 @@ from pypy.interpreter.error import OperationError, oefmt from rpython.rlib import jit, rgc from rpython.rlib.buffer import Buffer -from rpython.rlib.debug import make_sure_not_resized, debug_print +from rpython.rlib.debug import make_sure_not_resized from rpython.rlib.rawstorage import alloc_raw_storage, free_raw_storage, \ raw_storage_getitem, raw_storage_setitem, RAW_STORAGE from rpython.rtyper.lltypesystem import rffi, lltype, llmemory @@ -10,13 +10,11 @@ ArrayArgumentException, W_NumpyObject from pypy.module.micronumpy.iterators import ArrayIter from pypy.module.micronumpy.strides import ( - Chunk, Chunks, NewAxisChunk, EllipsisChunk, + Chunk, new_view, NewAxisChunk, EllipsisChunk, calc_strides, calc_new_strides, shape_agreement, calculate_broadcast_strides, calc_backstrides, calc_start, is_c_contiguous, is_f_contiguous) from rpython.rlib.objectmodel import keepalive_until_here -from rpython.rtyper.annlowlevel import cast_gcref_to_instance -from pypy.interpreter.baseobjspace import W_Root class BaseConcreteArray(object): _immutable_fields_ = ['dtype?', 'storage', 'start', 'size', 'shape[*]', @@ -225,16 +223,16 @@ space.isinstance_w(w_idx, space.w_slice)): if len(self.get_shape()) == 0: raise oefmt(space.w_ValueError, "cannot slice a 0-d array") - return Chunks([Chunk(*space.decode_index4(w_idx, self.get_shape()[0]))]) + return [Chunk(*space.decode_index4(w_idx, self.get_shape()[0]))] elif isinstance(w_idx, W_NDimArray) and w_idx.is_scalar(): w_idx = w_idx.get_scalar_value().item(space) if not space.isinstance_w(w_idx, space.w_int) and \ not space.isinstance_w(w_idx, space.w_bool): raise OperationError(space.w_IndexError, space.wrap( "arrays used as indices must be of integer (or boolean) type")) - return Chunks([Chunk(*space.decode_index4(w_idx, self.get_shape()[0]))]) + return [Chunk(*space.decode_index4(w_idx, self.get_shape()[0]))] elif space.is_w(w_idx, space.w_None): - return Chunks([NewAxisChunk()]) + return [NewAxisChunk()] result = [] i = 0 has_ellipsis = False @@ -253,7 +251,7 @@ result.append(Chunk(*space.decode_index4(w_item, self.get_shape()[i]))) i += 1 - return Chunks(result) + return result def descr_getitem(self, space, orig_arr, w_index): try: @@ -262,7 +260,7 @@ except IndexError: # not a single result chunks = self._prepare_slice_args(space, w_index) - return chunks.apply(space, orig_arr) + return new_view(space, orig_arr, chunks) def descr_setitem(self, space, orig_arr, w_index, w_value): try: @@ -271,7 +269,7 @@ except IndexError: w_value = convert_to_array(space, w_value) chunks = self._prepare_slice_args(space, w_index) - view = chunks.apply(space, orig_arr) + view = new_view(space, orig_arr, chunks) view.implementation.setslice(space, w_value) def transpose(self, orig_array, axes=None): diff --git a/pypy/module/micronumpy/ndarray.py b/pypy/module/micronumpy/ndarray.py --- a/pypy/module/micronumpy/ndarray.py +++ b/pypy/module/micronumpy/ndarray.py @@ -20,7 +20,7 @@ from pypy.module.micronumpy.flagsobj import W_FlagsObject from pypy.module.micronumpy.strides import ( get_shape_from_iterable, shape_agreement, shape_agreement_multiple, - is_c_contiguous, is_f_contiguous, calc_strides) + is_c_contiguous, is_f_contiguous, calc_strides, new_view) from pypy.module.micronumpy.casting import can_cast_array @@ -179,7 +179,7 @@ if iter_shape is None: # w_index is a list of slices, return a view chunks = self.implementation._prepare_slice_args(space, w_index) - return chunks.apply(space, self) + return new_view(space, self, chunks) shape = res_shape + self.get_shape()[len(indexes):] w_res = W_NDimArray.from_shape(space, shape, self.get_dtype(), self.get_order(), w_instance=self) @@ -195,7 +195,7 @@ if iter_shape is None: # w_index is a list of slices chunks = self.implementation._prepare_slice_args(space, w_index) - view = chunks.apply(space, self) + view = new_view(space, self, chunks) view.implementation.setslice(space, val_arr) return if support.product(iter_shape) == 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 @@ -10,31 +10,6 @@ pass -class Chunks(BaseChunk): - def __init__(self, l): - self.l = l - - @jit.unroll_safe - def extend_shape(self, old_shape): - shape = [] - i = -1 - for i, c in enumerate_chunks(self.l): - if c.step != 0: - shape.append(c.lgt) - s = i + 1 - assert s >= 0 - return shape[:] + old_shape[s:] - - def apply(self, space, orig_arr): - arr = orig_arr.implementation - shape = self.extend_shape(arr.shape) - r = calculate_slice_strides(arr.shape, arr.start, arr.get_strides(), - arr.get_backstrides(), self.l) - _, start, strides, backstrides = r - return W_NDimArray.new_slice(space, start, strides[:], backstrides[:], - shape[:], arr, orig_arr) - - class Chunk(BaseChunk): axis_step = 1 @@ -64,6 +39,27 @@ pass +def new_view(space, w_arr, chunks): + arr = w_arr.implementation + shape = _extend_shape(arr.shape, chunks) + r = calculate_slice_strides(arr.shape, arr.start, arr.get_strides(), + arr.get_backstrides(), chunks) + _, start, strides, backstrides = r + return W_NDimArray.new_slice(space, start, strides[:], backstrides[:], + shape[:], arr, w_arr) + +@jit.unroll_safe +def _extend_shape(old_shape, chunks): + shape = [] + i = -1 + for i, c in enumerate_chunks(chunks): + if c.step != 0: + shape.append(c.lgt) + s = i + 1 + assert s >= 0 + return shape[:] + old_shape[s:] + + class BaseTransform(object): pass diff --git a/pypy/module/micronumpy/test/test_ndarray.py b/pypy/module/micronumpy/test/test_ndarray.py --- a/pypy/module/micronumpy/test/test_ndarray.py +++ b/pypy/module/micronumpy/test/test_ndarray.py @@ -4,7 +4,7 @@ from pypy.conftest import option from pypy.module.micronumpy.appbridge import get_appbridge_cache -from pypy.module.micronumpy.strides import Chunk, Chunks +from pypy.module.micronumpy.strides import Chunk, new_view from pypy.module.micronumpy.ndarray import W_NDimArray from pypy.module.micronumpy.test.test_base import BaseNumpyAppTest @@ -22,7 +22,7 @@ def create_slice(space, a, chunks): - return Chunks(chunks).apply(space, W_NDimArray(a)).implementation + return new_view(space, W_NDimArray(a), chunks).implementation def create_array(*args, **kwargs): _______________________________________________ pypy-commit mailing list pypy-commit@python.org https://mail.python.org/mailman/listinfo/pypy-commit