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

Function prepare_index

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

* Prepare an npy_index_object from the python slicing object. * * This function handles all index preparations with the exception * of field access. It fills the array of index_info structs correctly. * It already handles the boolean array special case for fancy indexing, * i.e. if the index type is boolean, it is exactly one matching boolean * array. If the index type is fancy, the boolean

Source from the content-addressed store, hash-verified

275 * @returns the index_type or -1 on failure and fills the number of indices.
276 */
277NPY_NO_EXPORT int
278prepare_index(PyArrayObject *self, PyObject *index,
279 npy_index_info *indices,
280 int *num, int *ndim, int *out_fancy_ndim, int allow_boolean)
281{
282 int new_ndim, fancy_ndim, used_ndim, index_ndim;
283 int curr_idx, get_idx;
284
285 int i;
286 npy_intp n;
287
288 PyObject *obj = NULL;
289 PyArrayObject *arr;
290
291 int index_type = 0;
292 int ellipsis_pos = -1;
293
294 /*
295 * The choice of only unpacking `2*NPY_MAXDIMS` items is historic.
296 * The longest "reasonable" index that produces a result of <= 32 dimensions
297 * is `(0,)*np.MAXDIMS + (None,)*np.MAXDIMS`. Longer indices can exist, but
298 * are uncommon.
299 */
300 PyObject *raw_indices[NPY_MAXDIMS*2];
301
302 index_ndim = unpack_indices(index, raw_indices, NPY_MAXDIMS*2);
303 if (index_ndim == -1) {
304 return -1;
305 }
306
307 /*
308 * Parse all indices into the `indices` array of index_info structs
309 */
310 used_ndim = 0;
311 new_ndim = 0;
312 fancy_ndim = 0;
313 get_idx = 0;
314 curr_idx = 0;
315
316 while (get_idx < index_ndim) {
317 if (curr_idx > NPY_MAXDIMS * 2) {
318 PyErr_SetString(PyExc_IndexError,
319 "too many indices for array");
320 goto failed_building_indices;
321 }
322
323 obj = raw_indices[get_idx++];
324
325 /**** Try the cascade of possible indices ****/
326
327 /* Index is an ellipsis (`...`) */
328 if (obj == Py_Ellipsis) {
329 /* At most one ellipsis in an index */
330 if (index_type & HAS_ELLIPSIS) {
331 PyErr_Format(PyExc_IndexError,
332 "an index can only have a single ellipsis ('...')");
333 goto failed_building_indices;
334 }

Callers 3

array_subscriptFunction · 0.85
array_assign_subscriptFunction · 0.85

Calls 10

unpack_indicesFunction · 0.85
PyArray_NDIMFunction · 0.85
PyArray_PyIntAsIntpFunction · 0.85
PyArray_FromArrayFunction · 0.85
PyArray_CompareListsFunction · 0.85
PyArray_DIMSFunction · 0.85
PyArray_ZerosFunction · 0.85
_nonzero_indicesFunction · 0.85
PyArray_DIMFunction · 0.85
multi_DECREFFunction · 0.85

Tested by

no test coverage detected