MCPcopy Create free account
hub / github.com/numpy/numpy / mapiter_fill_info

Function mapiter_fill_info

numpy/core/src/multiarray/mapping.c:2406–2550  ·  view source on GitHub ↗

* Fill information about the iterator. The MapIterObject does not * need to have any information set for this function to work. * (PyArray_MapIterSwapAxes requires also nd and nd_fancy info) * * Sets the following information: * * mit->consec: The axis where the fancy indices need transposing to. * * mit->iteraxes: The axis which the fancy index corresponds to. * * mit-> fancy_dims

Source from the content-addressed store, hash-verified

2404 * @return 0 on success -1 on failure (broadcasting or too many fancy indices)
2405 */
2406static int
2407mapiter_fill_info(PyArrayMapIterObject *mit, npy_index_info *indices,
2408 int index_num, PyArrayObject *arr)
2409{
2410 int j = 0, i;
2411 int curr_dim = 0;
2412 /* dimension of index result (up to first fancy index) */
2413 int result_dim = 0;
2414 /* -1 init; 0 found fancy; 1 fancy stopped; 2 found not consecutive fancy */
2415 int consec_status = -1;
2416 int axis, broadcast_axis;
2417 npy_intp dimension;
2418
2419 for (i = 0; i < mit->nd_fancy; i++) {
2420 mit->dimensions[i] = 1;
2421 }
2422
2423 mit->consec = 0;
2424 for (i = 0; i < index_num; i++) {
2425 /* integer and fancy indexes are transposed together */
2426 if (indices[i].type & (HAS_FANCY | HAS_INTEGER)) {
2427 /* there was no previous fancy index, so set consec */
2428 if (consec_status == -1) {
2429 mit->consec = result_dim;
2430 consec_status = 0;
2431 }
2432 /* there was already a non-fancy index after a fancy one */
2433 else if (consec_status == 1) {
2434 consec_status = 2;
2435 mit->consec = 0;
2436 }
2437 }
2438 else {
2439 /* consec_status == 0 means there was a fancy index before */
2440 if (consec_status == 0) {
2441 consec_status = 1;
2442 }
2443 }
2444
2445 /* Before contunuing, ensure that there are not too fancy indices */
2446 if (indices[i].type & HAS_FANCY) {
2447 if (NPY_UNLIKELY(j >= NPY_MAXDIMS)) {
2448 PyErr_Format(PyExc_IndexError,
2449 "too many advanced (array) indices. This probably "
2450 "means you are indexing with too many booleans. "
2451 "(more than %d found)", NPY_MAXDIMS);
2452 return -1;
2453 }
2454 }
2455
2456 /* (iterating) fancy index, store the iterator */
2457 if (indices[i].type == HAS_FANCY) {
2458 mit->fancy_strides[j] = PyArray_STRIDE(arr, curr_dim);
2459 mit->fancy_dims[j] = PyArray_DIM(arr, curr_dim);
2460 mit->iteraxes[j++] = curr_dim++;
2461
2462 /* Check broadcasting */
2463 broadcast_axis = mit->nd_fancy;

Callers 1

PyArray_MapIterNewFunction · 0.85

Calls 5

PyArray_STRIDEFunction · 0.85
PyArray_DIMFunction · 0.85
PyArray_NDIMFunction · 0.85
PyArray_SHAPEFunction · 0.85
convert_shape_to_stringFunction · 0.85

Tested by

no test coverage detected