diff options
author | Julian Taylor <jtaylor.debian@googlemail.com> | 2013-05-28 20:26:55 +0200 |
---|---|---|
committer | Julian Taylor <jtaylor.debian@googlemail.com> | 2013-05-28 20:30:07 +0200 |
commit | 5448d7fbdc4111c80763ae519c7007982b1a6830 (patch) | |
tree | b562f15cf7e665a2f3331a8b90c461edcdb4f27b | |
parent | 19e05629b42ec73b48237c186c2475b7c80fa5cc (diff) | |
download | numpy-5448d7fbdc4111c80763ae519c7007982b1a6830.tar.gz |
MAINT: remove duplicated array extent calculation
-rw-r--r-- | numpy/core/src/multiarray/array_assign.c | 37 | ||||
-rw-r--r-- | numpy/core/src/multiarray/common.c | 5 |
2 files changed, 12 insertions, 30 deletions
diff --git a/numpy/core/src/multiarray/array_assign.c b/numpy/core/src/multiarray/array_assign.c index 4f350388b..6467b6cfd 100644 --- a/numpy/core/src/multiarray/array_assign.c +++ b/numpy/core/src/multiarray/array_assign.c @@ -21,6 +21,7 @@ #include "shape.h" #include "array_assign.h" +#include "common.h" /* See array_assign.h for parameter documentation */ NPY_NO_EXPORT int @@ -102,36 +103,14 @@ raw_array_is_aligned(int ndim, char *data, npy_intp *strides, int alignment) /* Gets a half-open range [start, end) which contains the array data */ NPY_NO_EXPORT void get_array_memory_extents(PyArrayObject *arr, - npy_uintp *out_start, npy_uintp *out_end) + npy_uintp *out_start, npy_uintp *out_end) { - npy_uintp start, end; - npy_intp idim, ndim = PyArray_NDIM(arr); - npy_intp *dimensions = PyArray_DIMS(arr), - *strides = PyArray_STRIDES(arr); - - /* Calculate with a closed range [start, end] */ - start = end = (npy_uintp)PyArray_DATA(arr); - for (idim = 0; idim < ndim; ++idim) { - npy_intp stride = strides[idim], dim = dimensions[idim]; - /* If the array size is zero, return an empty range */ - if (dim == 0) { - *out_start = *out_end = (npy_uintp)PyArray_DATA(arr); - return; - } - /* Expand either upwards or downwards depending on stride */ - else { - if (stride > 0) { - end += stride*(dim-1); - } - else if (stride < 0) { - start += stride*(dim-1); - } - } - } - - /* Return a half-open range */ - *out_start = start; - *out_end = end + PyArray_DESCR(arr)->elsize; + npy_intp low, upper; + offset_bounds_from_strides(PyArray_ITEMSIZE(arr), PyArray_NDIM(arr), + PyArray_DIMS(arr), PyArray_STRIDES(arr), + &low, &upper); + *out_start = (npy_uintp)PyArray_DATA(arr) + (npy_uintp)low; + *out_end = (npy_uintp)PyArray_DATA(arr) + (npy_uintp)upper; } /* Returns 1 if the arrays have overlapping data, 0 otherwise */ diff --git a/numpy/core/src/multiarray/common.c b/numpy/core/src/multiarray/common.c index 4f25dc913..01bf9cd74 100644 --- a/numpy/core/src/multiarray/common.c +++ b/numpy/core/src/multiarray/common.c @@ -688,6 +688,7 @@ _IsWriteable(PyArrayObject *ap) } +/* Gets a half-open range [start, end) of offsets from the data pointer */ NPY_NO_EXPORT void offset_bounds_from_strides(const int itemsize, const int nd, const npy_intp *dims, const npy_intp *strides, @@ -699,11 +700,12 @@ offset_bounds_from_strides(const int itemsize, const int nd, for (i = 0; i < nd; i++) { if (dims[i] == 0) { - /* Empty array special case */ + /* If the array size is zero, return an empty range */ *lower_offset = 0; *upper_offset = 0; return; } + /* Expand either upwards or downwards depending on stride */ max_axis_offset = strides[i] * (dims[i] - 1); if (max_axis_offset > 0) { upper += max_axis_offset; @@ -712,6 +714,7 @@ offset_bounds_from_strides(const int itemsize, const int nd, lower += max_axis_offset; } } + /* Return a half-open range */ upper += itemsize; *lower_offset = lower; *upper_offset = upper; |