* Helper function to clear whole array. It seems plausible that we should * be able to get away with assuming the the array is contiguous. * * Must only be called on arrays which own their data (and asserts this). */
| 63 | * Must only be called on arrays which own their data (and asserts this). |
| 64 | */ |
| 65 | NPY_NO_EXPORT int |
| 66 | PyArray_ClearArray(PyArrayObject *arr) |
| 67 | { |
| 68 | assert(PyArray_FLAGS(arr) & NPY_ARRAY_OWNDATA); |
| 69 | |
| 70 | PyArray_Descr *descr = PyArray_DESCR(arr); |
| 71 | if (!PyDataType_REFCHK(descr)) { |
| 72 | return 0; |
| 73 | } |
| 74 | /* |
| 75 | * The contiguous path should cover practically all important cases since |
| 76 | * it is difficult to create a non-contiguous array which owns its memory |
| 77 | * and only arrays which own their memory should clear it. |
| 78 | */ |
| 79 | int aligned = PyArray_ISALIGNED(arr); |
| 80 | if (PyArray_ISCONTIGUOUS(arr)) { |
| 81 | return PyArray_ClearBuffer( |
| 82 | descr, PyArray_BYTES(arr), descr->elsize, |
| 83 | PyArray_SIZE(arr), aligned); |
| 84 | } |
| 85 | int idim, ndim; |
| 86 | npy_intp shape_it[NPY_MAXDIMS], strides_it[NPY_MAXDIMS]; |
| 87 | npy_intp coord[NPY_MAXDIMS]; |
| 88 | char *data_it; |
| 89 | if (PyArray_PrepareOneRawArrayIter( |
| 90 | PyArray_NDIM(arr), PyArray_DIMS(arr), |
| 91 | PyArray_BYTES(arr), PyArray_STRIDES(arr), |
| 92 | &ndim, shape_it, &data_it, strides_it) < 0) { |
| 93 | return -1; |
| 94 | } |
| 95 | npy_intp inner_stride = strides_it[0]; |
| 96 | npy_intp inner_shape = shape_it[0]; |
| 97 | NPY_traverse_info clear_info; |
| 98 | /* Flags unused: float errors do not matter and we do not release GIL */ |
| 99 | NPY_ARRAYMETHOD_FLAGS flags_unused; |
| 100 | if (PyArray_GetClearFunction( |
| 101 | aligned, inner_stride, descr, &clear_info, &flags_unused) < 0) { |
| 102 | return -1; |
| 103 | } |
| 104 | NPY_RAW_ITER_START(idim, ndim, coord, shape_it) { |
| 105 | /* Process the innermost dimension */ |
| 106 | if (clear_info.func(NULL, clear_info.descr, |
| 107 | data_it, inner_shape, inner_stride, clear_info.auxdata) < 0) { |
| 108 | return -1; |
| 109 | } |
| 110 | } NPY_RAW_ITER_ONE_NEXT(idim, ndim, coord, |
| 111 | shape_it, data_it, strides_it); |
| 112 | return 0; |
| 113 | } |
| 114 | |
| 115 | |
| 116 | /*NUMPY_API |
no test coverage detected