예제 #1
0
void pydynd::array_setitem(const dynd::nd::array& n, PyObject *subscript, PyObject *value)
{
    if (subscript == Py_Ellipsis) {
        array_broadcast_assign_from_py(n, value);
#if PY_VERSION_HEX < 0x03000000
    } else if (PyInt_Check(subscript)) {
        long i = PyInt_AS_LONG(subscript);
        const char *metadata = n.get_ndo_meta();
        char *data = n.get_readwrite_originptr();
        ndt::type d = n.get_type().at_single(i, &metadata, const_cast<const char **>(&data));
        array_broadcast_assign_from_py(d, metadata, data, value);
#endif // PY_VERSION_HEX < 0x03000000
    } else if (PyLong_Check(subscript)) {
        intptr_t i = PyLong_AsSsize_t(subscript);
        if (i == -1 && PyErr_Occurred()) {
            throw runtime_error("error converting int value");
        }
        const char *metadata = n.get_ndo_meta();
        char *data = n.get_readwrite_originptr();
        ndt::type d = n.get_type().at_single(i, &metadata, const_cast<const char **>(&data));
        array_broadcast_assign_from_py(d, metadata, data, value);
    } else {
        intptr_t size;
        shortvector<irange> indices;
        pyobject_as_irange_array(size, indices, subscript);
        array_broadcast_assign_from_py(n.at_array(size, indices.get(), false), value);
    }
}
예제 #2
0
bool pydynd::array_contains(const dynd::nd::array& n, PyObject *x)
{
    if (n.get_ndo() == NULL) {
        return false;
    }
    if (n.get_ndim() == 0) {
        // TODO: Allow for struct types, etc?
        throw runtime_error("cannot call __contains__ on a scalar dynd array");
    }

    // Turn 'n' into type/metadata/data with a uniform_dim leading dimension
    nd::array tmp;
    ndt::type dt;
    const base_uniform_dim_type *budd;
    const char *metadata, *data;
    if (n.get_type().get_kind() == uniform_dim_kind) {
        dt = n.get_type();
        budd = static_cast<const base_uniform_dim_type *>(dt.extended());
        metadata = n.get_ndo_meta();
        data = n.get_readonly_originptr();
    } else {
        tmp = n.eval();
        if (tmp.get_type().get_kind() != uniform_dim_kind) {
            throw runtime_error("internal error in array_contains: expected uniform_dim kind after eval() call");
        }
        dt = tmp.get_type();
        budd = static_cast<const base_uniform_dim_type *>(dt.extended());
        metadata = tmp.get_ndo_meta();
        data = tmp.get_readonly_originptr();
    }

    // Turn 'x' into a dynd array, and make a comparison kernel
    nd::array x_ndo = array_from_py(x, 0, false);
    const ndt::type& x_dt = x_ndo.get_type();
    const char *x_metadata = x_ndo.get_ndo_meta();
    const char *x_data = x_ndo.get_readonly_originptr();
    const ndt::type& child_dt = budd->get_element_type();
    const char *child_metadata = metadata + budd->get_element_metadata_offset();
    comparison_ckernel_builder k;
    try {
        make_comparison_kernel(&k, 0,
                    x_dt, x_metadata, child_dt, child_metadata,
                    comparison_type_equal, &eval::default_eval_context);
    } catch(const not_comparable_error&) {
        return false;
    }

    contains_data aux;
    aux.x_data = x_data;
    aux.k = &k;
    aux.found = false;
    budd->foreach_leading(const_cast<char *>(data), metadata, &contains_callback, &aux);
    return aux.found;
}