* Implements boolean indexing assignment. This takes the one-dimensional * array 'v' and assigns its values to all of the elements of 'self' for which * the corresponding element of 'op' is True. * * This operation is somewhat unfortunate, because to match up with * a one-dimensional output array, it has to choose a particular * iteration order, in the case of NumPy that is always C order ev
| 1075 | * Returns 0 on success, -1 on failure. |
| 1076 | */ |
| 1077 | NPY_NO_EXPORT int |
| 1078 | array_assign_boolean_subscript(PyArrayObject *self, |
| 1079 | PyArrayObject *bmask, PyArrayObject *v, NPY_ORDER order) |
| 1080 | { |
| 1081 | npy_intp size, v_stride; |
| 1082 | char *v_data; |
| 1083 | npy_intp bmask_size; |
| 1084 | |
| 1085 | if (PyArray_DESCR(bmask)->type_num != NPY_BOOL) { |
| 1086 | PyErr_SetString(PyExc_TypeError, |
| 1087 | "NumPy boolean array indexing assignment " |
| 1088 | "requires a boolean index"); |
| 1089 | return -1; |
| 1090 | } |
| 1091 | |
| 1092 | if (PyArray_NDIM(v) > 1) { |
| 1093 | PyErr_Format(PyExc_TypeError, |
| 1094 | "NumPy boolean array indexing assignment " |
| 1095 | "requires a 0 or 1-dimensional input, input " |
| 1096 | "has %d dimensions", PyArray_NDIM(v)); |
| 1097 | return -1; |
| 1098 | } |
| 1099 | |
| 1100 | if (PyArray_NDIM(bmask) != PyArray_NDIM(self)) { |
| 1101 | PyErr_SetString(PyExc_ValueError, |
| 1102 | "The boolean mask assignment indexing array " |
| 1103 | "must have the same number of dimensions as " |
| 1104 | "the array being indexed"); |
| 1105 | return -1; |
| 1106 | } |
| 1107 | |
| 1108 | size = count_boolean_trues(PyArray_NDIM(bmask), PyArray_DATA(bmask), |
| 1109 | PyArray_DIMS(bmask), PyArray_STRIDES(bmask)); |
| 1110 | /* Correction factor for broadcasting 'bmask' to 'self' */ |
| 1111 | bmask_size = PyArray_SIZE(bmask); |
| 1112 | if (bmask_size > 0) { |
| 1113 | size *= PyArray_SIZE(self) / bmask_size; |
| 1114 | } |
| 1115 | |
| 1116 | /* Tweak the strides for 0-dim and broadcasting cases */ |
| 1117 | if (PyArray_NDIM(v) > 0 && PyArray_DIMS(v)[0] != 1) { |
| 1118 | if (size != PyArray_DIMS(v)[0]) { |
| 1119 | PyErr_Format(PyExc_ValueError, |
| 1120 | "NumPy boolean array indexing assignment " |
| 1121 | "cannot assign %" NPY_INTP_FMT " input values to " |
| 1122 | "the %" NPY_INTP_FMT " output values where the mask is true", |
| 1123 | PyArray_DIMS(v)[0], size); |
| 1124 | return -1; |
| 1125 | } |
| 1126 | v_stride = PyArray_STRIDES(v)[0]; |
| 1127 | } |
| 1128 | else { |
| 1129 | v_stride = 0; |
| 1130 | } |
| 1131 | |
| 1132 | v_data = PyArray_DATA(v); |
| 1133 | |
| 1134 | /* Create an iterator for the data */ |
no test coverage detected