NUMPY_API * * Same as PyArray_MapIterArray, but: * * If copy_if_overlap != 0, check if `a` has memory overlap with any of the * arrays in `index` and with `extra_op`. If yes, make copies as appropriate * to avoid problems if `a` is modified during the iteration. * `iter->array` may contain a copied array (WRITEBACKIFCOPY set). */
| 3319 | * `iter->array` may contain a copied array (WRITEBACKIFCOPY set). |
| 3320 | */ |
| 3321 | NPY_NO_EXPORT PyObject * |
| 3322 | PyArray_MapIterArrayCopyIfOverlap(PyArrayObject * a, PyObject * index, |
| 3323 | int copy_if_overlap, PyArrayObject *extra_op) |
| 3324 | { |
| 3325 | PyArrayMapIterObject * mit = NULL; |
| 3326 | PyArrayObject *subspace = NULL; |
| 3327 | npy_index_info indices[NPY_MAXDIMS * 2 + 1]; |
| 3328 | int i, index_num, ndim, fancy_ndim, index_type; |
| 3329 | PyArrayObject *a_copy = NULL; |
| 3330 | |
| 3331 | index_type = prepare_index(a, index, indices, &index_num, |
| 3332 | &ndim, &fancy_ndim, 0); |
| 3333 | |
| 3334 | if (index_type < 0) { |
| 3335 | return NULL; |
| 3336 | } |
| 3337 | |
| 3338 | if (copy_if_overlap && index_has_memory_overlap(a, index_type, indices, |
| 3339 | index_num, |
| 3340 | (PyObject *)extra_op)) { |
| 3341 | /* Make a copy of the input array */ |
| 3342 | a_copy = (PyArrayObject *)PyArray_NewLikeArray(a, NPY_ANYORDER, |
| 3343 | NULL, 0); |
| 3344 | if (a_copy == NULL) { |
| 3345 | goto fail; |
| 3346 | } |
| 3347 | |
| 3348 | if (PyArray_CopyInto(a_copy, a) != 0) { |
| 3349 | goto fail; |
| 3350 | } |
| 3351 | |
| 3352 | Py_INCREF(a); |
| 3353 | if (PyArray_SetWritebackIfCopyBase(a_copy, a) < 0) { |
| 3354 | goto fail; |
| 3355 | } |
| 3356 | |
| 3357 | a = a_copy; |
| 3358 | } |
| 3359 | |
| 3360 | /* If it is not a pure fancy index, need to get the subspace */ |
| 3361 | if (index_type != HAS_FANCY) { |
| 3362 | if (get_view_from_index(a, &subspace, indices, index_num, 1) < 0) { |
| 3363 | goto fail; |
| 3364 | } |
| 3365 | } |
| 3366 | |
| 3367 | mit = (PyArrayMapIterObject *)PyArray_MapIterNew(indices, index_num, |
| 3368 | index_type, ndim, |
| 3369 | fancy_ndim, |
| 3370 | a, subspace, 0, |
| 3371 | NPY_ITER_READWRITE, |
| 3372 | 0, NULL, NULL); |
| 3373 | if (mit == NULL) { |
| 3374 | goto fail; |
| 3375 | } |
| 3376 | |
| 3377 | /* Required for backward compatibility */ |
| 3378 | mit->ait = (PyArrayIterObject *)PyArray_IterNew((PyObject *)a); |
no test coverage detected