Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork11k
ENH: Use array indexing preparation routines for flatiter objects#28590
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
base:main
Are you sure you want to change the base?
Uh oh!
There was an error while loading.Please reload this page.
Changes from1 commit
bc55a60
f19745e
a563242
9f2d51f
33109dd
dc362b4
8255e5a
cfcdabf
368b404
ae101a5
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
- Loading branch information
Uh oh!
There was an error while loading.Please reload this page.
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -426,7 +426,7 @@ iter_length(PyArrayIterObject *self) | ||
} | ||
staticPyObject * | ||
iter_subscript_Bool(PyArrayIterObject *self, PyArrayObject *ind, | ||
NPY_cast_info *cast_info) | ||
{ | ||
@@ -483,7 +483,7 @@ iter_subscript_Bool(PyArrayIterObject *self, PyArrayObject *ind, | ||
} | ||
PyArray_ITER_RESET(self); | ||
} | ||
return(PyObject *)ret; | ||
} | ||
static PyObject * | ||
@@ -561,195 +561,149 @@ iter_subscript_int(PyArrayIterObject *self, PyArrayObject *ind, | ||
NPY_NO_EXPORT PyObject * | ||
iter_subscript(PyArrayIterObject *self, PyObject *ind) | ||
{ | ||
PyObject *ret = NULL; | ||
int index_type; | ||
int index_num = -1; | ||
int ndim, fancy_ndim; | ||
npy_intp start, stop, step, n_steps; | ||
npy_index_info indices[NPY_MAXDIMS * 2 + 1]; | ||
PyArray_Descr *dtype = PyArray_DESCR(self->ao); | ||
npy_intp dtype_size = dtype->elsize; | ||
NPY_cast_info cast_info = {.func = NULL}; | ||
/* Prepare the indices */ | ||
npy_intp dims[1] = {self->size}; | ||
index_type = prepare_index_noarray(1, dims, ind, indices, &index_num, | ||
&ndim, &fancy_ndim, 1, 1); | ||
if (index_type < 0) { | ||
return NULL; | ||
} | ||
else if (index_num == 0) { | ||
Py_INCREF(self->ao); | ||
ret = (PyObject *) self->ao; | ||
goto finish; | ||
} | ||
else if (indices[0].type == HAS_NEWAXIS) { | ||
PyErr_SetString(PyExc_IndexError, | ||
"only integers, slices (`:`), ellipsis (`...`) and integer or boolean " | ||
"arrays are valid indices" | ||
); | ||
goto finish; | ||
} | ||
// Single ellipsis index | ||
else if (index_type == HAS_ELLIPSIS) { | ||
ind = PySlice_New(NULL, NULL, NULL); | ||
if (ind == NULL) { | ||
goto finish; | ||
} | ||
ret = iter_subscript(self, ind); | ||
Py_DECREF(ind); | ||
goto finish; | ||
} | ||
// Single boolean index | ||
else if (indices[0].type == HAS_0D_BOOL) { | ||
if (indices[0].value) { | ||
ret = PyArray_ToScalar(self->dataptr, self->ao); | ||
goto finish; | ||
} | ||
else { /* empty array */ | ||
npy_intp ii = 0; | ||
Py_INCREF(dtype); | ||
ret = PyArray_NewFromDescr(Py_TYPE(self->ao), | ||
dtype, | ||
1, &ii, | ||
NULL, NULL, 0, | ||
(PyObject *)self->ao); | ||
goto finish; | ||
} | ||
} | ||
PyArray_ITER_RESET(self); | ||
if (index_type == HAS_INTEGER) { | ||
if (check_and_adjust_index(&indices[0].value, self->size, -1, NULL) < 0) { | ||
goto finish; | ||
} | ||
PyArray_ITER_GOTO1D(self, indices[0].value); | ||
ret = PyArray_ToScalar(self->dataptr, self->ao); | ||
PyArray_ITER_RESET(self); | ||
goto finish; | ||
} | ||
/* set up a cast to handle item copying */ | ||
NPY_ARRAYMETHOD_FLAGS transfer_flags = 0; | ||
npy_intp one = 1; | ||
/* We can assume the newly allocated output array is aligned */ | ||
int is_aligned = IsUintAligned(self->ao); | ||
if (PyArray_GetDTypeTransferFunction( | ||
is_aligned,dtype_size, dtype_size, dtype, dtype, 0, &cast_info, | ||
&transfer_flags) < 0) { | ||
gotofinish; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Seems like | ||
if (index_type & HAS_SLICE) { | ||
if (PySlice_GetIndicesEx(indices[0].object, | ||
dims[0], | ||
&start, &stop, &step, &n_steps) < 0) { | ||
goto finish; | ||
} | ||
PyArray_ITER_GOTO1D(self, start); | ||
Py_INCREF(dtype); | ||
ret = PyArray_NewFromDescr(Py_TYPE(self->ao), | ||
dtype, | ||
1, &n_steps, | ||
NULL, NULL, | ||
0, (PyObject *)self->ao); | ||
if (ret == NULL) { | ||
gotofinish; | ||
} | ||
char *dptr = PyArray_DATA((PyArrayObject *) ret); | ||
while (n_steps--) { | ||
char *args[2] = {self->dataptr, dptr}; | ||
npy_intp transfer_strides[2] = {dtype_size, dtype_size}; | ||
if (cast_info.func(&cast_info.context, args, &one, | ||
transfer_strides, cast_info.auxdata) < 0) { | ||
gotofinish; | ||
} | ||
start +=step; | ||
PyArray_ITER_GOTO1D(self, start); | ||
dptr +=dtype_size; | ||
} | ||
PyArray_ITER_RESET(self); | ||
goto finish; | ||
} | ||
if (index_type == HAS_BOOL) { | ||
ret = iter_subscript_Bool(self, (PyArrayObject *) indices[0].object, &cast_info); | ||
gotofinish; | ||
} | ||
if (index_type == HAS_FANCY) { | ||
ret = iter_subscript_int(self, (PyArrayObject *) indices[0].object, &cast_info); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. It must be an integer array here, I think. But I don't think it is guaranteed to be an | ||
goto finish; | ||
} | ||
PyErr_SetString(PyExc_IndexError, | ||
"only integers, slices (`:`), ellipsis (`...`) and integer or boolean " | ||
"arrays are valid indices" | ||
); | ||
finish: | ||
NPY_cast_info_xfree(&cast_info); | ||
for (int i = 0; i < index_num; i++) { | ||
Py_XDECREF(indices[i].object); | ||
} | ||
return ret; | ||
} | ||
Uh oh!
There was an error while loading.Please reload this page.