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;
}
Beispiel #2
0
dynd::nd::array pydynd::array_eval(const dynd::nd::array &n)
{
  return n.eval();
}
dynd::nd::array pydynd::array_eval(const dynd::nd::array &n, PyObject *ectx_obj)
{
    return n.eval(eval_context_from_pyobj(ectx_obj));
}