diff options
author | Travis Oliphant <oliphant@enthought.com> | 2006-11-16 23:59:19 +0000 |
---|---|---|
committer | Travis Oliphant <oliphant@enthought.com> | 2006-11-16 23:59:19 +0000 |
commit | 3e2d1f8c497c9f43ecca15b4a309beb73e128fbd (patch) | |
tree | d8bc516e99fc594a417c07fb08420be74f8351d8 /numpy/core/src/arrayobject.c | |
parent | 7dc0cb19610c4ce559b32617eace1091cddd8ea4 (diff) | |
download | numpy-3e2d1f8c497c9f43ecca15b4a309beb73e128fbd.tar.gz |
Used an adapted setArrayFromSequence from numarray to increase speed of Assign_Array dramatically
Diffstat (limited to 'numpy/core/src/arrayobject.c')
-rw-r--r-- | numpy/core/src/arrayobject.c | 56 |
1 files changed, 41 insertions, 15 deletions
diff --git a/numpy/core/src/arrayobject.c b/numpy/core/src/arrayobject.c index f07463eb9..a337aa176 100644 --- a/numpy/core/src/arrayobject.c +++ b/numpy/core/src/arrayobject.c @@ -7088,30 +7088,56 @@ _array_find_type(PyObject *op, PyArray_Descr *minitype, int max) return outtype; } +/* adapted from Numarray */ +static int +setArrayFromSequence(PyArrayObject *a, PyObject *s, int dim, intp offset) +{ + Py_ssize_t i, slen = PySequence_Length(s); + int res = 0; + + if (dim > a->nd) { + PyErr_Format(PyExc_ValueError, + "setArrayFromSequence: sequence/array dimensions mismatch."); + return -1; + } + + if (slen != a->dimensions[dim]) { + PyErr_Format(PyExc_ValueError, + "setArrayFromSequence: sequence/array shape mismatch."); + return -1; + } + + for(i=0; i<slen; i++) { + PyObject *o = PySequence_GetItem(s, i); + if ((a->nd - dim) > 1) { + res = setArrayFromSequence(a, o, dim+1, offset); + } + else { + res = a->descr->f->setitem(o, (a->data + offset), a); + } + Py_DECREF(o); + if (res < 0) return res; + offset += a->strides[dim]; + } + return 0; +} + + static int Assign_Array(PyArrayObject *self, PyObject *v) { - PyObject *e; - int l, r; - if (!PySequence_Check(v)) { PyErr_SetString(PyExc_ValueError, "assignment from non-sequence"); return -1; } + if (self->nd == 0) { + PyErr_SetString(PyExc_ValueError, + "assignment to 0-d array"); + return -1; + } - l=PyObject_Length(v); - if(l < 0) return -1; - - while(--l >= 0) - { - e=PySequence_GetItem(v,l); - if (e == NULL) return -1; - r = PySequence_SetItem((PyObject*)self,l,e); - Py_DECREF(e); - if(r == -1) return -1; - } - return 0; + return setArrayFromSequence(self, v, 0, 0); } /* "Array Scalars don't call this code" */ |