Example #1
0
/*
 * Assigns the scalar value to every element of the destination raw array.
 *
 * Returns 0 on success, -1 on failure.
 */
NPY_NO_EXPORT int
raw_array_assign_scalar(int ndim, npy_intp *shape,
        PyArray_Descr *dst_dtype, char *dst_data, npy_intp *dst_strides,
        PyArray_Descr *src_dtype, char *src_data)
{
    int idim;
    npy_intp shape_it[NPY_MAXDIMS], dst_strides_it[NPY_MAXDIMS];
    npy_intp coord[NPY_MAXDIMS];

    PyArray_StridedUnaryOp *stransfer = NULL;
    NpyAuxData *transferdata = NULL;
    int aligned, needs_api = 0;
    npy_intp src_itemsize = src_dtype->elsize;

    NPY_BEGIN_THREADS_DEF;

    /* Check alignment */
    aligned = raw_array_is_aligned(ndim, dst_data, dst_strides,
                                    dst_dtype->alignment);
    if (!npy_is_aligned(src_data, src_dtype->alignment)) {
        aligned = 0;
    }

    /* Use raw iteration with no heap allocation */
    if (PyArray_PrepareOneRawArrayIter(
                    ndim, shape,
                    dst_data, dst_strides,
                    &ndim, shape_it,
                    &dst_data, dst_strides_it) < 0) {
        return -1;
    }

    /* Get the function to do the casting */
    if (PyArray_GetDTypeTransferFunction(aligned,
                        0, dst_strides_it[0],
                        src_dtype, dst_dtype,
                        0,
                        &stransfer, &transferdata,
                        &needs_api) != NPY_SUCCEED) {
        return -1;
    }

    if (!needs_api) {
        npy_intp nitems = 1, i;
        for (i = 0; i < ndim; i++) {
            nitems *= shape_it[i];
        }
        NPY_BEGIN_THREADS_THRESHOLDED(nitems);
    }

    NPY_RAW_ITER_START(idim, ndim, coord, shape_it) {
        /* Process the innermost dimension */
        stransfer(dst_data, dst_strides_it[0], src_data, 0,
                    shape_it[0], src_itemsize, transferdata);
    } NPY_RAW_ITER_ONE_NEXT(idim, ndim, coord,
Example #2
0
/*NUMPY_API
 *
 * Returns false if the array has no NA support. Returns
 * true if the array has NA support AND there is an
 * NA anywhere in the array.
 *
 * If 'wheremask' is non-NULL, only positions with True
 * in 'wheremask' are checked for NA.
 *
 * The parameter 'whichna' is not yet supported, but is
 * provided for future multi-NA support. It should be set
 * to NULL.
 *
 * Returns -1 on failure, otherwise 0 for False and 1 for True.
 */
NPY_NO_EXPORT int
PyArray_ContainsNA(PyArrayObject *arr, PyArrayObject *wheremask,
                   npy_bool *whichna)
{
    /* Validate that the parameter for future expansion is NULL */
    if (whichna != NULL) {
        PyErr_SetString(PyExc_RuntimeError,
                        "multi-NA is not yet supported in PyArray_ContainsNA");
        return -1;
    }

    if (wheremask == NULL) {
        /* Need NA support to contain NA */
        if (PyArray_HASMASKNA(arr)) {
            int idim, ndim;
            char *data;
            npy_intp shape[NPY_MAXDIMS], strides[NPY_MAXDIMS];
            npy_intp i, coord[NPY_MAXDIMS];

            if (PyArray_HASFIELDS(arr)) {
                PyErr_SetString(PyExc_RuntimeError,
                                "field-NA is not yet supported");
                return -1;
            }

            /* Use raw iteration with no heap memory allocation */
            if (PyArray_PrepareOneRawArrayIter(
                        PyArray_NDIM(arr), PyArray_DIMS(arr),
                        PyArray_MASKNA_DATA(arr), PyArray_MASKNA_STRIDES(arr),
                        &ndim, shape,
                        &data, strides) < 0) {
                return -1;
            }

            /* Do the iteration */
            NPY_RAW_ITER_START(idim, ndim, coord, shape) {
                char *d = data;
                /* Process the innermost dimension */
                for (i = 0; i < shape[0]; ++i, d += strides[0]) {
                    if (!NpyMaskValue_IsExposed((npy_mask)(*d))) {
                        return 1;
                    }
                }
            }
            NPY_RAW_ITER_ONE_NEXT(idim, ndim, coord, shape, data, strides);
        }
    }