本文整理汇总了C++中nd::array::get_type方法的典型用法代码示例。如果您正苦于以下问题:C++ array::get_type方法的具体用法?C++ array::get_type怎么用?C++ array::get_type使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类nd::array
的用法示例。
在下文中一共展示了array::get_type方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: view
nd::array nd::view(const nd::array &arr, const ndt::type &tp)
{
if (arr.get_type() == tp) {
// If the types match exactly, simply return 'arr'
return arr;
} else if (tp.get_type_id() == bytes_type_id) {
// If it's a request to view the data as raw bytes
nd::array result = view_as_bytes(arr, tp);
if (!result.is_null()) {
return result;
}
} else if (arr.get_type().get_type_id() == bytes_type_id) {
// If it's a request to view raw bytes as something else
nd::array result = view_from_bytes(arr, tp);
if (!result.is_null()) {
return result;
}
} else if (arr.get_ndim() == tp.get_ndim()) {
// If the type is symbolic, e.g. has a "Fixed" symbolic dimension,
// first substitute in the shape from the array
if (tp.is_symbolic()) {
dimvector shape(arr.get_ndim());
arr.get_shape(shape.get());
return view_concrete(arr, substitute_shape(tp, arr.get_ndim(), shape.get()));
} else {
return view_concrete(arr, tp);
}
}
stringstream ss;
ss << "Unable to view nd::array of type " << arr.get_type();
ss << " as type " << tp;
throw type_error(ss.str());
}
示例2: format_json
nd::array dynd::format_json(const nd::array &n, bool struct_as_list)
{
// Create a UTF-8 string
nd::array result = nd::empty(ndt::string_type::make());
// Initialize the output with some memory
output_data out;
out.out_string.resize(1024);
out.out_begin = out.out_string.begin();
out.out_capacity_end = out.out_string.end();
out.out_end = out.out_begin;
out.struct_as_list = struct_as_list;
if (!n.get_type().is_expression()) {
::format_json(out, n.get_type(), n.get_arrmeta(), n.get_readonly_originptr());
} else {
nd::array tmp = n.eval();
::format_json(out, tmp.get_type(), tmp.get_arrmeta(), tmp.get_readonly_originptr());
}
// Shrink the memory to fit, and set the pointers in the output
string *d = reinterpret_cast<string *>(result.get_readwrite_originptr());
d->assign(out.out_string.data(), out.out_end - out.out_begin);
// Finalize processing and mark the result as immutable
result.get_type().extended()->arrmeta_finalize_buffers(result.get_arrmeta());
result.flag_as_immutable();
return result;
}
示例3: view
nd::array nd::view(const nd::array& arr, const ndt::type& tp)
{
// If the types match exactly, simply return 'arr'
if (arr.get_type() == tp) {
return arr;
} else if (arr.get_ndim() == tp.get_ndim()) {
// Allocate a result array to attempt the view in it
array result(make_array_memory_block(tp.get_metadata_size()));
// Copy the fields
result.get_ndo()->m_data_pointer = arr.get_ndo()->m_data_pointer;
if (arr.get_ndo()->m_data_reference == NULL) {
// Embedded data, need reference to the array
result.get_ndo()->m_data_reference = arr.get_memblock().release();
} else {
// Use the same data reference, avoid producing a chain
result.get_ndo()->m_data_reference = arr.get_data_memblock().release();
}
result.get_ndo()->m_type = ndt::type(tp).release();
result.get_ndo()->m_flags = arr.get_ndo()->m_flags;
// Now try to copy the metadata as a view
if (try_view(arr.get_type(), arr.get_ndo_meta(), tp,
result.get_ndo_meta(), arr.get_memblock().get())) {
// If it succeeded, return it
return result;
}
// Otherwise fall through, let it get destructed, and raise an error
}
stringstream ss;
ss << "Unable to view nd::array of type " << arr.get_type();
ss << "as type " << tp;
throw type_error(ss.str());
}
示例4: format_json
nd::array dynd::format_json(const nd::array& n)
{
// Create a UTF-8 string
nd::array result = nd::empty(ndt::make_string());
// Initialize the output with some memory
output_data out;
out.blockref = reinterpret_cast<const string_type_metadata *>(result.get_ndo_meta())->blockref;
out.api = get_memory_block_pod_allocator_api(out.blockref);
out.api->allocate(out.blockref, 1024, 1, &out.out_begin, &out.out_capacity_end);
out.out_end = out.out_begin;
if (!n.get_type().is_expression()) {
::format_json(out, n.get_type(), n.get_ndo_meta(), n.get_readonly_originptr());
} else {
nd::array tmp = n.eval();
::format_json(out, tmp.get_type(), tmp.get_ndo_meta(), tmp.get_readonly_originptr());
}
// Shrink the memory to fit, and set the pointers in the output
string_type_data *d = reinterpret_cast<string_type_data *>(result.get_readwrite_originptr());
d->begin = out.out_begin;
d->end = out.out_capacity_end;
out.api->resize(out.blockref, out.out_end - out.out_begin, &d->begin, &d->end);
// Finalize processing and mark the result as immutable
result.get_type().extended()->metadata_finalize_buffers(result.get_ndo_meta());
result.flag_as_immutable();
return result;
}
示例5: broadcast_error_message
inline string broadcast_error_message(const nd::array& dst, const nd::array& src)
{
vector<intptr_t> dst_shape = dst.get_shape(), src_shape = src.get_shape();
stringstream ss;
ss << "cannot broadcast dynd array with type ";
ss << src.get_type() << " and shape ";
print_shape(ss, src_shape);
ss << " to type " << dst.get_type() << " and shape ";
print_shape(ss, dst_shape);
return ss.str();
}
示例6: bad_alloc
// Constructor which creates the output based on the input's broadcast shape
array_iter(const ndt::type& op0_dtype, nd::array& out_op0, const nd::array& op1, const nd::array& op2, const nd::array& op3) {
create_broadcast_result(op0_dtype, op1, op2, op3, out_op0, m_iter_ndim[0], m_itershape);
nd::array ops[4] = {out_op0, op1, op2, op3};
m_array_tp[0] = out_op0.get_type();
m_array_tp[1] = op1.get_type();
m_array_tp[2] = op2.get_type();
m_array_tp[3] = op3.get_type();
m_itersize = 1;
m_iter_ndim[1] = m_array_tp[1].get_ndim();
m_iter_ndim[2] = m_array_tp[2].get_ndim();
m_iter_ndim[3] = m_array_tp[3].get_ndim();
// Allocate and initialize the iterdata
if (m_iter_ndim[0] != 0) {
m_iterindex.init(m_iter_ndim[0]);
memset(m_iterindex.get(), 0, sizeof(intptr_t) * m_iter_ndim[0]);
// The destination iterdata
size_t iterdata_size = m_array_tp[0].get_iterdata_size(m_iter_ndim[0]);
m_iterdata[0] = reinterpret_cast<iterdata_common *>(malloc(iterdata_size));
if (!m_iterdata[0]) {
throw std::bad_alloc();
}
m_metadata[0] = out_op0.get_ndo_meta();
m_array_tp[0].iterdata_construct(m_iterdata[0],
&m_metadata[0], m_iter_ndim[0], m_itershape.get(), m_uniform_tp[0]);
m_data[0] = m_iterdata[0]->reset(m_iterdata[0], out_op0.get_readwrite_originptr(), m_iter_ndim[0]);
// The op iterdata
for (int i = 1; i < 4; ++i) {
iterdata_size = m_array_tp[i].get_broadcasted_iterdata_size(m_iter_ndim[i]);
m_iterdata[i] = reinterpret_cast<iterdata_common *>(malloc(iterdata_size));
if (!m_iterdata[i]) {
throw std::bad_alloc();
}
m_metadata[i] = ops[i].get_ndo_meta();
m_array_tp[i].broadcasted_iterdata_construct(m_iterdata[i],
&m_metadata[i], m_iter_ndim[i],
m_itershape.get() + (m_iter_ndim[0] - m_iter_ndim[i]), m_uniform_tp[i]);
m_data[i] = m_iterdata[i]->reset(m_iterdata[i], ops[i].get_ndo()->m_data_pointer, m_iter_ndim[0]);
}
for (size_t i = 0, i_end = m_iter_ndim[0]; i != i_end; ++i) {
m_itersize *= m_itershape[i];
}
} else {
for (size_t i = 0; i < 4; ++i) {
m_iterdata[i] = NULL;
m_uniform_tp[i] = m_array_tp[i];
m_data[i] = ops[i].get_ndo()->m_data_pointer;
m_metadata[i] = ops[i].get_ndo_meta();
}
}
}
示例7: result
static nd::array view_concrete(const nd::array &arr, const ndt::type &tp)
{
// Allocate a result array to attempt the view in it
nd::array result(make_array_memory_block(tp.get_arrmeta_size()));
// Copy the fields
result.get_ndo()->data.ptr = arr.get_ndo()->data.ptr;
if (arr.get_ndo()->data.ref == NULL) {
// Embedded data, need reference to the array
result.get_ndo()->data.ref = arr.get_memblock().release();
} else {
// Use the same data reference, avoid producing a chain
result.get_ndo()->data.ref = arr.get_data_memblock().release();
}
result.get_ndo()->m_type = ndt::type(tp).release();
result.get_ndo()->m_flags = arr.get_ndo()->m_flags;
// First handle a special case of viewing outermost "var" as "fixed[#]"
if (arr.get_type().get_type_id() == var_dim_type_id && tp.get_type_id() == fixed_dim_type_id) {
const var_dim_type_arrmeta *in_am = reinterpret_cast<const var_dim_type_arrmeta *>(arr.get_arrmeta());
const var_dim_type_data *in_dat = reinterpret_cast<const var_dim_type_data *>(arr.get_readonly_originptr());
fixed_dim_type_arrmeta *out_am = reinterpret_cast<fixed_dim_type_arrmeta *>(result.get_arrmeta());
out_am->dim_size = tp.extended<ndt::fixed_dim_type>()->get_fixed_dim_size();
out_am->stride = in_am->stride;
if ((intptr_t)in_dat->size == out_am->dim_size) {
// Use the more specific data reference from the var arrmeta if possible
if (in_am->blockref != NULL) {
memory_block_decref(result.get_ndo()->data.ref);
memory_block_incref(in_am->blockref);
result.get_ndo()->data.ref = in_am->blockref;
}
result.get_ndo()->data.ptr = in_dat->begin + in_am->offset;
// Try to copy the rest of the arrmeta as a view
if (try_view(arr.get_type().extended<ndt::base_dim_type>()->get_element_type(),
arr.get_arrmeta() + sizeof(var_dim_type_arrmeta),
tp.extended<ndt::base_dim_type>()->get_element_type(),
result.get_arrmeta() + sizeof(fixed_dim_type_arrmeta), arr.get_memblock().get())) {
return result;
}
}
}
// Otherwise try to copy the arrmeta as a view
else if (try_view(arr.get_type(), arr.get_arrmeta(), tp, result.get_arrmeta(), arr.get_memblock().get())) {
// If it succeeded, return it
return result;
}
stringstream ss;
ss << "Unable to view nd::array of type " << arr.get_type();
ss << " as type " << tp;
throw type_error(ss.str());
}
示例8: parse_json
void dynd::parse_json(nd::array &out, const char *json_begin,
const char *json_end, const eval::eval_context *ectx)
{
try {
const char *begin = json_begin, *end = json_end;
ndt::type tp = out.get_type();
::parse_json(tp, out.get_ndo_meta(), out.get_readwrite_originptr(), begin, end, ectx);
begin = skip_whitespace(begin, end);
if (begin != end) {
throw json_parse_error(begin, "unexpected trailing JSON text", tp);
}
} catch (const json_parse_error& e) {
stringstream ss;
string line_prev, line_cur;
int line, column;
get_error_line_column(json_begin, json_end, e.get_position(),
line_prev, line_cur, line, column);
ss << "Error parsing JSON at line " << line << ", column " << column << "\n";
if (e.get_type().get_type_id() != uninitialized_type_id) {
ss << "DType: " << e.get_type() << "\n";
}
ss << "Message: " << e.get_message() << "\n";
print_json_parse_error_marker(ss, line_prev, line_cur, line, column);
throw runtime_error(ss.str());
}
}
示例9: runtime_error
uint32_t ndt::categorical_type::get_value_from_category(const nd::array &category) const
{
nd::array c;
if (category.get_type() == m_category_tp) {
// If the type is right, get the category value directly
c = category;
}
else {
// Otherwise convert to the correct type, then get the category value
c = nd::empty(m_category_tp);
c.assign(category);
}
intptr_t i = nd::binary_search(m_categories, c).as<intptr_t>();
if (i < 0) {
stringstream ss;
ss << "Unrecognized category value ";
m_category_tp.print_data(ss, c.get()->metadata(), c.data());
ss << " assigning to dynd type " << type(this, true);
throw std::runtime_error(ss.str());
}
else {
return (uint32_t)unchecked_fixed_dim_get<intptr_t>(m_category_index_to_value, i);
}
}
示例10: result
static nd::array array_function_dereference(const nd::array &self)
{
// Follow the pointers to eliminate them
ndt::type dt = self.get_type();
const char *arrmeta = self.get_arrmeta();
char *data = self.get_ndo()->m_data_pointer;
memory_block_data *dataref = self.get_ndo()->m_data_reference;
if (dataref == NULL) {
dataref = self.get_memblock().get();
}
uint64_t flags = self.get_ndo()->m_flags;
while (dt.get_type_id() == pointer_type_id) {
const pointer_type_arrmeta *md =
reinterpret_cast<const pointer_type_arrmeta *>(arrmeta);
dt = dt.extended<ndt::pointer_type>()->get_target_type();
arrmeta += sizeof(pointer_type_arrmeta);
data = *reinterpret_cast<char **>(data) + md->offset;
dataref = md->blockref;
}
// Create an array without the pointers
nd::array result(make_array_memory_block(dt.get_arrmeta_size()));
if (!dt.is_builtin()) {
dt.extended()->arrmeta_copy_construct(result.get_arrmeta(), arrmeta,
&self.get_ndo()->m_memblockdata);
}
result.get_ndo()->m_type = dt.release();
result.get_ndo()->m_data_pointer = data;
result.get_ndo()->m_data_reference = dataref;
memory_block_incref(result.get_ndo()->m_data_reference);
result.get_ndo()->m_flags = flags;
return result;
}
示例11: typed_data_assign
void dynd::typed_data_assign(const ndt::type &dst_tp, const char *dst_arrmeta,
char *dst_data, const nd::array &src_arr,
const eval::eval_context *ectx)
{
typed_data_assign(dst_tp, dst_arrmeta, dst_data, src_arr.get_type(),
src_arr.get_arrmeta(), src_arr.get_readonly_originptr(),
ectx);
}
示例12: while
static nd::array property_ndo_get_groups(const nd::array& n) {
ndt::type d = n.get_type();
while (d.get_type_id() != groupby_type_id) {
d = d.at_single(0);
}
const groupby_type *gd = d.extended<groupby_type>();
return gd->get_groups_type().p("categories");
}
示例13: set
static void set(const ndt::type& paramtype, char *metadata, char *data, const nd::array& value) {
if (paramtype.get_type_id() == void_pointer_type_id) {
// TODO: switch to a better mechanism for passing nd::array references
*reinterpret_cast<const array_preamble **>(data) = value.get_ndo();
} else {
typed_data_assign(paramtype, metadata, data, value.get_type(), value.get_ndo_meta(), value.get_ndo()->m_data_pointer);
}
}
示例14: array
static nd::array view_as_bytes(const nd::array &arr, const ndt::type &tp)
{
if (arr.get_type().get_flags() & type_flag_destructor) {
// Can't view arrays of object type
return nd::array();
}
// Get the essential components of the array to analyze
memory_block_ptr data_ref = arr.get_data_memblock();
char *data_ptr = arr.get_ndo()->data.ptr;
ndt::type data_tp = arr.get_type();
const char *data_meta = arr.get_arrmeta();
intptr_t data_dim_size = -1, data_stride = 0;
// Repeatedly refine the data
while (data_tp.get_type_id() != uninitialized_type_id) {
refine_bytes_view(data_ref, data_ptr, data_tp, data_meta, data_dim_size, data_stride);
}
// Check that it worked, and that the resulting data pointer is aligned
if (data_dim_size < 0 ||
!offset_is_aligned(reinterpret_cast<size_t>(data_ptr), tp.extended<ndt::bytes_type>()->get_target_alignment())) {
// This signals we could not view the data as a
// contiguous chunk of bytes
return nd::array();
}
char *result_data_ptr = NULL;
nd::array result(make_array_memory_block(tp.extended()->get_arrmeta_size(), tp.get_data_size(),
tp.get_data_alignment(), &result_data_ptr));
// Set the bytes extents
((char **)result_data_ptr)[0] = data_ptr;
((char **)result_data_ptr)[1] = data_ptr + data_dim_size;
// Set the array arrmeta
array_preamble *ndo = result.get_ndo();
ndo->m_type = ndt::type(tp).release();
ndo->data.ptr = result_data_ptr;
ndo->data.ref = NULL;
ndo->m_flags = arr.get_flags();
// Set the bytes arrmeta
bytes_type_arrmeta *ndo_meta = reinterpret_cast<bytes_type_arrmeta *>(result.get_arrmeta());
ndo_meta->blockref = data_ref.release();
return result;
}
示例15: get_value_from_category
uint32_t categorical_type::get_value_from_category(const nd::array& category) const
{
if (category.get_type() == m_category_tp) {
// If the type is right, get the category value directly
return get_value_from_category(category.get_arrmeta(), category.get_readonly_originptr());
} else {
// Otherwise convert to the correct type, then get the category value
nd::array c = nd::empty(m_category_tp);
c.val_assign(category);
return get_value_from_category(c.get_arrmeta(), c.get_readonly_originptr());
}
}