Skip to content

Commit

Permalink
ENH: use super optimized count_boolean_trues in indexing subfunction
Browse files Browse the repository at this point in the history
speeds up e.g. a[..., boolmask]
  • Loading branch information
juliantaylor committed Mar 26, 2014
1 parent 7395900 commit d3dfa68
Showing 1 changed file with 6 additions and 8 deletions.
14 changes: 6 additions & 8 deletions numpy/core/src/multiarray/mapping.c
Expand Up @@ -1982,6 +1982,7 @@ _nonzero_indices(PyObject *myBool, PyArrayObject **arrays)
npy_bool *ptr;
npy_intp coords[NPY_MAXDIMS], dims_m1[NPY_MAXDIMS];
npy_intp *dptr[NPY_MAXDIMS];
static npy_intp one = 1;

typecode=PyArray_DescrFromType(NPY_BOOL);
ba = (PyArrayObject *)PyArray_FromAny(myBool, typecode, 0, 0,
Expand All @@ -1996,14 +1997,12 @@ _nonzero_indices(PyObject *myBool, PyArrayObject **arrays)
}
size = PyArray_SIZE(ba);
ptr = (npy_bool *)PyArray_DATA(ba);
count = 0;

/* pre-determine how many nonzero entries there are */
for (i = 0; i < size; i++) {
if (*(ptr++)) {
count++;
}
}
/*
* pre-determine how many nonzero entries there are,
* ignore dimensionality of input as its a CARRAY
*/
count = count_boolean_trues(1, (char*)ptr, &size, &one);

/* create count-sized index arrays for each dimension */
for (j = 0; j < nd; j++) {
Expand All @@ -2019,7 +2018,6 @@ _nonzero_indices(PyObject *myBool, PyArrayObject **arrays)
coords[j] = 0;
dims_m1[j] = PyArray_DIMS(ba)[j]-1;
}
ptr = (npy_bool *)PyArray_DATA(ba);
if (count == 0) {
goto finish;
}
Expand Down

0 comments on commit d3dfa68

Please sign in to comment.