Skip to content

Tolist speedup #59

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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 35 additions & 24 deletions numpy/core/src/multiarray/convert.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,39 +17,50 @@

#include "convert.h"

/*NUMPY_API
* To List
/*
* Converts a subarray of 'self' into lists, with starting data pointer
* 'dataptr' and from dimension 'startdim' to the last dimension of 'self'.
*/
NPY_NO_EXPORT PyObject *
PyArray_ToList(PyArrayObject *self)
static PyObject *
recursive_tolist(PyArrayObject *self, char *dataptr, int startdim)
{
PyObject *lp;
PyArrayObject *v;
intp sz, i;
npy_intp i, n, stride;
PyObject *ret, *item;

if (!PyArray_Check(self)) {
return (PyObject *)self;
/* Base case */
if (startdim >= PyArray_NDIM(self)) {
return PyArray_DESCR(self)->f->getitem(dataptr,self);
}
if (self->nd == 0) {
return self->descr->f->getitem(self->data,self);

n = PyArray_DIM(self, startdim);
stride = PyArray_STRIDE(self, startdim);

ret = PyList_New(n);
if (ret == NULL) {
return NULL;
}

sz = self->dimensions[0];
lp = PyList_New(sz);
for (i = 0; i < sz; i++) {
v = (PyArrayObject *)array_big_item(self, i);
if (PyArray_Check(v) && (v->nd >= self->nd)) {
PyErr_SetString(PyExc_RuntimeError,
"array_item not returning smaller-" \
"dimensional array");
Py_DECREF(v);
Py_DECREF(lp);
for (i = 0; i < n; ++i) {
item = recursive_tolist(self, dataptr, startdim+1);
if (item == NULL) {
Py_DECREF(ret);
return NULL;
}
PyList_SetItem(lp, i, PyArray_ToList(v));
Py_DECREF(v);
PyList_SET_ITEM(ret, i, item);

dataptr += stride;
}
return lp;

return ret;
}

/*NUMPY_API
* To List
*/
NPY_NO_EXPORT PyObject *
PyArray_ToList(PyArrayObject *self)
{
return recursive_tolist(self, PyArray_DATA(self), 0);
}

/* XXX: FIXME --- add ordering argument to
Expand Down