diff options
author | 87 <hangenuit@gmail.com> | 2011-10-16 23:28:25 +0200 |
---|---|---|
committer | Sebastian Berg <sebastian@sipsolutions.net> | 2013-04-11 16:03:07 +0200 |
commit | 4fae90dd81c9abca631727f918b3c5d684984bf2 (patch) | |
tree | 910f8f22f64a4036b3a7d1c2f6345d76d006f670 | |
parent | bde1a064bda6269c06d34ace67899d7d1c73ffba (diff) | |
download | numpy-4fae90dd81c9abca631727f918b3c5d684984bf2.tar.gz |
ENH: Move fancy indexing code and ellipse check code into own functions.
Conflicts:
numpy/core/src/multiarray/mapping.c
-rw-r--r-- | numpy/core/src/multiarray/mapping.c | 130 |
1 files changed, 71 insertions, 59 deletions
diff --git a/numpy/core/src/multiarray/mapping.c b/numpy/core/src/multiarray/mapping.c index b4c578978..a4b1e6575 100644 --- a/numpy/core/src/multiarray/mapping.c +++ b/numpy/core/src/multiarray/mapping.c @@ -925,12 +925,79 @@ array_ass_boolean_subscript(PyArrayObject *self, return 0; } + +/* return TRUE if ellipses are found else return FALSE */ +static npy_bool +_check_ellipses(PyObject *op) +{ + if ((op == Py_Ellipsis) || PyString_Check(op) || PyUnicode_Check(op)) { + return TRUE; + } + else if (PyBool_Check(op) || PyArray_IsScalar(op, Bool) || + (PyArray_Check(op) && + (PyArray_DIMS((PyArrayObject *)op)==0) && + PyArray_ISBOOL((PyArrayObject *)op))) { + return TRUE; + } + else if (PySequence_Check(op)) { + Py_ssize_t n, i; + PyObject *temp; + + n = PySequence_Size(op); + i = 0; + while (i < n) { + temp = PySequence_GetItem(op, i); + if (temp == Py_Ellipsis) { + Py_DECREF(temp); + return TRUE; + } + Py_DECREF(temp); + i++; + } + } + return FALSE; +} + +NPY_NO_EXPORT PyObject * +array_subscript_fancy(PyArrayObject *self, PyObject *op, int fancy) + { + int oned; + PyObject *other; + PyArrayMapIterObject *mit; + + oned = ((PyArray_NDIM(self) == 1) && + !(PyTuple_Check(op) && PyTuple_GET_SIZE(op) > 1)); + + /* wrap arguments into a mapiter object */ + mit = (PyArrayMapIterObject *) PyArray_MapIterNew(op, oned, fancy); + if (mit == NULL) { + return NULL; + } + if (oned) { + PyArrayIterObject *it; + PyObject *rval; + it = (PyArrayIterObject *) PyArray_IterNew((PyObject *)self); + if (it == NULL) { + Py_DECREF(mit); + return NULL; + } + rval = iter_subscript(it, mit->indexobj); + Py_DECREF(it); + Py_DECREF(mit); + return rval; + } + if (PyArray_MapIterBind(mit, self) != 0) { + return NULL; + } + other = (PyObject *)PyArray_GetMap(mit); + Py_DECREF(mit); + return other; +} + NPY_NO_EXPORT PyObject * array_subscript(PyArrayObject *self, PyObject *op) { int nd, fancy; - PyArrayObject *other; - PyArrayMapIterObject *mit; PyObject *obj; if (PyString_Check(op) || PyUnicode_Check(op)) { @@ -1052,41 +1119,12 @@ array_subscript(PyArrayObject *self, PyObject *op) fancy = fancy_indexing_check(op); if (fancy != SOBJ_NOTFANCY) { - int oned; - - oned = ((PyArray_NDIM(self) == 1) && - !(PyTuple_Check(op) && PyTuple_GET_SIZE(op) > 1)); - - /* wrap arguments into a mapiter object */ - mit = (PyArrayMapIterObject *) PyArray_MapIterNew(op, oned, fancy); - if (mit == NULL) { - return NULL; - } - if (oned) { - PyArrayIterObject *it; - PyObject *rval; - it = (PyArrayIterObject *) PyArray_IterNew((PyObject *)self); - if (it == NULL) { - Py_DECREF(mit); - return NULL; - } - rval = iter_subscript(it, mit->indexobj); - Py_DECREF(it); - Py_DECREF(mit); - return rval; - } - if (PyArray_MapIterBind(mit, self) != 0) { - return NULL; - } - other = (PyArrayObject *)PyArray_GetMap(mit); - Py_DECREF(mit); - return (PyObject *)other; + return array_subscript_fancy(self, op, fancy); } return array_subscript_simple(self, op); } - /* * Another assignment hacked by using CopyObject. * This only works if subscript returns a standard view. @@ -1183,7 +1221,6 @@ _tuple_of_integers(PyObject *seq, npy_intp *vals, int maxvals) return 1; } - static int array_ass_sub(PyArrayObject *self, PyObject *ind, PyObject *op) { @@ -1462,32 +1499,7 @@ array_subscript_nice(PyArrayObject *self, PyObject *op) */ if (PyArray_Check(mp) && PyArray_NDIM(mp) == 0) { - npy_bool noellipses = NPY_TRUE; - if ((op == Py_Ellipsis) || PyString_Check(op) || PyUnicode_Check(op)) { - noellipses = NPY_FALSE; - } - else if (PyBool_Check(op) || PyArray_IsScalar(op, Bool) || - (PyArray_Check(op) && - (PyArray_DIMS((PyArrayObject *)op)==0) && - PyArray_ISBOOL((PyArrayObject *)op))) { - noellipses = NPY_FALSE; - } - else if (PySequence_Check(op)) { - Py_ssize_t n, i; - PyObject *temp; - - n = PySequence_Size(op); - i = 0; - while (i < n && noellipses) { - temp = PySequence_GetItem(op, i); - if (temp == Py_Ellipsis) { - noellipses = NPY_FALSE; - } - Py_DECREF(temp); - i++; - } - } - if (noellipses) { + if (!_check_ellipses(op)) { return PyArray_Return(mp); } } |