本文整理汇总了C++中ndt::type类的典型用法代码示例。如果您正苦于以下问题:C++ type类的具体用法?C++ type怎么用?C++ type使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了type类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: init
inline void init(const ndt::type& tp0, const char *metadata0, char *data0)
{
m_array_tp = tp0;
m_iter_ndim = m_array_tp.get_ndim();
m_itersize = 1;
if (m_iter_ndim != 0) {
m_iterindex.init(m_iter_ndim);
memset(m_iterindex.get(), 0, sizeof(intptr_t) * m_iter_ndim);
m_itershape.init(m_iter_ndim);
m_array_tp.extended()->get_shape(m_iter_ndim, 0, m_itershape.get(), metadata0);
size_t iterdata_size = m_array_tp.extended()->get_iterdata_size(m_iter_ndim);
m_iterdata = reinterpret_cast<iterdata_common *>(malloc(iterdata_size));
if (!m_iterdata) {
throw std::bad_alloc();
}
m_metadata = metadata0;
m_array_tp.iterdata_construct(m_iterdata,
&m_metadata, m_iter_ndim, m_itershape.get(), m_uniform_tp);
m_data = m_iterdata->reset(m_iterdata, data0, m_iter_ndim);
for (size_t i = 0, i_end = m_iter_ndim; i != i_end; ++i) {
m_itersize *= m_itershape[i];
}
} else {
m_iterdata = NULL;
m_uniform_tp = m_array_tp;
m_data = data0;
m_metadata = metadata0;
}
}
示例2: array_getbuffer_pep3118_bytes
static void array_getbuffer_pep3118_bytes(const ndt::type &tp, const char *arrmeta, char *data, Py_buffer *buffer,
int flags)
{
buffer->itemsize = 1;
if (flags & PyBUF_FORMAT) {
buffer->format = (char *)"c";
}
else {
buffer->format = NULL;
}
buffer->ndim = 1;
#if PY_VERSION_HEX == 0x02070000
buffer->internal = NULL;
buffer->shape = &buffer->smalltable[0];
buffer->strides = &buffer->smalltable[1];
#else
buffer->internal = malloc(2 * sizeof(intptr_t));
buffer->shape = reinterpret_cast<Py_ssize_t *>(buffer->internal);
buffer->strides = buffer->shape + 1;
#endif
buffer->strides[0] = 1;
if (tp.get_id() == bytes_id) {
// Variable-length bytes type
buffer->buf = reinterpret_cast<bytes *>(data)->begin();
buffer->len = reinterpret_cast<bytes *>(data)->size();
}
else {
// Fixed-length bytes type
buffer->len = tp.get_data_size();
}
buffer->shape[0] = buffer->len;
}
示例3:
ndt::adapt_type::adapt_type(const ndt::type &value_tp, const ndt::type &storage_tp, const nd::callable &forward,
const nd::callable &inverse)
: base_expr_type(adapt_id, storage_tp.get_data_size(), storage_tp.get_data_alignment(), type_flag_none,
storage_tp.get_arrmeta_size(), storage_tp.get_ndim()),
m_value_tp(value_tp), m_storage_tp(storage_tp), m_forward(forward), m_inverse(inverse)
{
}
示例4: runtime_error
size_t dynd::make_var_to_fixed_dim_assignment_kernel(void *ckb, intptr_t ckb_offset,
const ndt::type &dst_strided_dim_tp, const char *dst_arrmeta,
const ndt::type &src_var_dim_tp, const char *src_arrmeta,
kernel_request_t kernreq, const eval::eval_context *ectx)
{
typedef var_to_strided_assign_ck self_type;
if (src_var_dim_tp.get_type_id() != var_dim_type_id) {
stringstream ss;
ss << "make_var_to_fixed_dim_assignment_kernel: provided source type " << src_var_dim_tp << " is not a var_dim";
throw runtime_error(ss.str());
}
const ndt::var_dim_type *src_vad = src_var_dim_tp.extended<ndt::var_dim_type>();
const var_dim_type_arrmeta *src_md = reinterpret_cast<const var_dim_type_arrmeta *>(src_arrmeta);
self_type *self = self_type::make(ckb, kernreq, ckb_offset);
ndt::type dst_element_tp;
const char *dst_element_arrmeta;
if (!dst_strided_dim_tp.get_as_strided(dst_arrmeta, &self->m_dst_dim_size, &self->m_dst_stride, &dst_element_tp,
&dst_element_arrmeta)) {
stringstream ss;
ss << "make_var_to_fixed_dim_assignment_kernel: provided destination "
"type " << dst_strided_dim_tp << " is not a strided_dim or fixed_array";
throw runtime_error(ss.str());
}
self->m_src_md = src_md;
return ::make_assignment_kernel(ckb, ckb_offset, dst_element_tp, dst_element_arrmeta, src_vad->get_element_type(),
src_arrmeta + sizeof(var_dim_type_arrmeta), kernel_request_strided, ectx);
}
示例5: broadcast_tagged_dims_from_type
static inline bool broadcast_tagged_dims_from_type(intptr_t ndim, ndt::type tp, const intptr_t *tagged_dims,
intptr_t *out_tagged_dims) {
tp = tp.without_memory_type();
for (intptr_t i = 0; i < ndim; ++i) {
intptr_t tagged_dim = tagged_dims[i], dim_size;
switch (tp.get_id()) {
case fixed_dim_kind_id: {
if (tagged_dim < 0) {
out_tagged_dims[i] = -2;
}
} break;
case fixed_dim_id: {
dim_size = tp.extended<ndt::fixed_dim_type>()->get_fixed_dim_size();
if (tagged_dim < 0 || tagged_dim == 1) {
out_tagged_dims[i] = dim_size;
} else if (tagged_dim != dim_size && dim_size != 1) {
return false;
}
} break;
case var_dim_id:
// All broadcasting is done dynamically for var
break;
default: {
stringstream ss;
ss << "dim_fragment_type failed to get shape from type " << tp;
throw type_error(ss.str());
}
}
tp = tp.extended<ndt::base_dim_type>()->get_element_type();
}
return true;
}
示例6: DYND_UNUSED
intptr_t dynd::make_cuda_from_device_builtin_type_assignment_kernel(
const callable_type_data *DYND_UNUSED(self),
const ndt::callable_type *DYND_UNUSED(af_tp), char *DYND_UNUSED(data),
void *ckb, intptr_t ckb_offset, const ndt::type &dst_tp,
const char *DYND_UNUSED(dst_arrmeta), intptr_t DYND_UNUSED(nsrc),
const ndt::type *src_tp, const char *const *DYND_UNUSED(src_arrmeta),
kernel_request_t kernreq, const eval::eval_context *ectx,
const nd::array &DYND_UNUSED(kwds),
const std::map<std::string, ndt::type> &DYND_UNUSED(tp_vars))
{
assign_error_mode errmode = ectx->errmode;
if (errmode != assign_error_nocheck &&
is_lossless_assignment(dst_tp, *src_tp)) {
errmode = assign_error_nocheck;
}
if (!dst_tp.is_builtin() || !src_tp->is_builtin() ||
errmode == assign_error_default) {
stringstream ss;
ss << "cannot assign from CUDA device with types " << *src_tp << " to "
<< dst_tp;
throw type_error(ss.str());
}
nd::cuda_device_to_host_assign_ck::make(ckb, kernreq, ckb_offset,
src_tp->get_data_size());
return make_builtin_type_assignment_kernel(
ckb, ckb_offset, dst_tp.without_memory_type().get_type_id(),
src_tp->without_memory_type().get_type_id(), kernel_request_single,
errmode);
}
示例7: make_var_dim_assignment_kernel
size_t dynd::make_var_dim_assignment_kernel(void *ckb, intptr_t ckb_offset, const ndt::type &dst_var_dim_tp,
const char *dst_arrmeta, const ndt::type &src_var_dim_tp,
const char *src_arrmeta, kernel_request_t kernreq,
const eval::eval_context *ectx)
{
typedef var_assign_ck self_type;
if (dst_var_dim_tp.get_type_id() != var_dim_type_id) {
stringstream ss;
ss << "make_broadcast_to_blockref_array_assignment_kernel: provided "
"destination type " << dst_var_dim_tp << " is not a var_dim";
throw runtime_error(ss.str());
}
if (src_var_dim_tp.get_type_id() != var_dim_type_id) {
stringstream ss;
ss << "make_broadcast_to_blockref_array_assignment_kernel: provided "
"source type " << src_var_dim_tp << " is not a var_dim";
throw runtime_error(ss.str());
}
const ndt::var_dim_type *dst_vad = dst_var_dim_tp.extended<ndt::var_dim_type>();
const ndt::var_dim_type *src_vad = src_var_dim_tp.extended<ndt::var_dim_type>();
const var_dim_type_arrmeta *dst_md = reinterpret_cast<const var_dim_type_arrmeta *>(dst_arrmeta);
const var_dim_type_arrmeta *src_md = reinterpret_cast<const var_dim_type_arrmeta *>(src_arrmeta);
self_type *self = self_type::make(ckb, kernreq, ckb_offset);
self->m_dst_target_alignment = dst_vad->get_target_alignment();
self->m_dst_md = dst_md;
self->m_src_md = src_md;
return ::make_assignment_kernel(ckb, ckb_offset, dst_vad->get_element_type(),
dst_arrmeta + sizeof(var_dim_type_arrmeta), src_vad->get_element_type(),
src_arrmeta + sizeof(var_dim_type_arrmeta), kernel_request_strided, ectx);
}
示例8: make_comparison_kernel
size_t string_type::make_comparison_kernel(
ckernel_builder *out, size_t offset_out,
const ndt::type& src0_dt, const char *src0_metadata,
const ndt::type& src1_dt, const char *src1_metadata,
comparison_type_t comptype,
const eval::eval_context *ectx) const
{
if (this == src0_dt.extended()) {
if (*this == *src1_dt.extended()) {
return make_string_comparison_kernel(out, offset_out,
m_encoding,
comptype, ectx);
} else if (src1_dt.get_kind() == string_kind) {
return make_general_string_comparison_kernel(out, offset_out,
src0_dt, src0_metadata,
src1_dt, src1_metadata,
comptype, ectx);
} else if (!src1_dt.is_builtin()) {
return src1_dt.extended()->make_comparison_kernel(out, offset_out,
src0_dt, src0_metadata,
src1_dt, src1_metadata,
comptype, ectx);
}
}
throw not_comparable_error(src0_dt, src1_dt, comptype);
}
示例9: parse_number_json
static void parse_number_json(const ndt::type &tp, const char *arrmeta, char *out_data, const char *&rbegin,
const char *end, bool option, const eval::eval_context *ectx)
{
const char *begin = rbegin;
const char *nbegin, *nend;
bool escaped = false;
if (option && parse::parse_token_no_ws(begin, end, "null")) {
ndt::option_type::make(tp).extended<ndt::option_type>()->assign_na(arrmeta, out_data, ectx);
} else if (parse::parse_json_number_no_ws(begin, end, nbegin, nend)) {
parse::string_to_number(out_data, tp.get_type_id(), nbegin, nend, false, ectx->errmode);
} else if (parse::parse_doublequote_string_no_ws(begin, end, nbegin, nend, escaped)) {
// Interpret the data inside the string as an int
try
{
if (!escaped) {
parse::string_to_number(out_data, tp.get_type_id(), nbegin, nend, option, ectx->errmode);
} else {
std::string s;
parse::unescape_string(nbegin, nend, s);
parse::string_to_number(out_data, tp.get_type_id(), nbegin, nend, option, ectx->errmode);
}
}
catch (const std::exception &e)
{
throw json_parse_error(rbegin, e.what(), tp);
}
catch (const dynd::dynd_exception &e)
{
throw json_parse_error(rbegin, e.what(), tp);
}
} else {
throw json_parse_error(rbegin, "expected a number", tp);
}
rbegin = begin;
}
示例10: make_assignment_kernel
size_t fixedbytes_type::make_assignment_kernel(
ckernel_builder *ckb, intptr_t ckb_offset, const ndt::type &dst_tp,
const char *dst_arrmeta, const ndt::type &src_tp, const char *src_arrmeta,
kernel_request_t kernreq, const eval::eval_context *ectx) const
{
if (this == dst_tp.extended()) {
switch (src_tp.get_type_id()) {
case fixedbytes_type_id: {
const fixedbytes_type *src_fs = src_tp.tcast<fixedbytes_type>();
if (get_data_size() != src_fs->get_data_size()) {
throw runtime_error("cannot assign to a fixedbytes type of a different size");
}
return ::make_pod_typed_data_assignment_kernel(ckb, ckb_offset,
get_data_size(), std::min(get_data_alignment(), src_fs->get_data_alignment()),
kernreq);
}
default: {
return src_tp.extended()->make_assignment_kernel(
ckb, ckb_offset, dst_tp, dst_arrmeta, src_tp, src_arrmeta,
kernreq, ectx);
}
}
} else {
stringstream ss;
ss << "Cannot assign from " << src_tp << " to " << dst_tp;
throw dynd::type_error(ss.str());
}
}
示例11: typed_data_assign
void dynd::typed_data_assign(const ndt::type& dst_tp, const char *dst_metadata, char *dst_data,
const ndt::type& src_tp, const char *src_metadata, const char *src_data,
assign_error_mode errmode, const eval::eval_context *ectx)
{
DYND_ASSERT_ALIGNED(dst, 0, dst_tp.get_data_alignment(), "dst type: " << dst_tp << ", src type: " << src_tp);
DYND_ASSERT_ALIGNED(src, 0, src_dt.get_data_alignment(), "src type: " << src_tp << ", dst type: " << dst_tp);
if (errmode == assign_error_default) {
if (ectx != NULL) {
if (dst_tp.get_dtype().get_type_id() == cuda_device_type_id && src_tp.get_dtype().get_type_id() == cuda_device_type_id) {
errmode = ectx->default_cuda_device_errmode;
} else {
errmode = ectx->default_errmode;
}
} else if (dst_tp == src_tp) {
errmode = assign_error_none;
} else {
stringstream ss;
ss << "assignment from " << src_tp << " to " << dst_tp << " with default error mode requires an eval_context";
throw dynd::type_error(ss.str());
}
}
assignment_ckernel_builder k;
make_assignment_kernel(&k, 0, dst_tp, dst_metadata,
src_tp, src_metadata,
kernel_request_single,
errmode, ectx);
k(dst_data, src_data);
}
示例12: if
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());
}
示例13: parse_bool_json
static void parse_bool_json(const ndt::type &tp, const char *arrmeta,
char *out_data, const char *&rbegin,
const char *end, bool option,
const eval::eval_context *ectx)
{
const char *begin = rbegin;
char value = 3;
const char *nbegin, *nend;
bool escaped;
if (parse_token(begin, end, "true")) {
value = 1;
} else if (parse_token(begin, end, "false")) {
value = 0;
} else if (parse_token(begin, end, "null")) {
if (option || ectx->errmode != assign_error_nocheck) {
value = 2;
} else {
value = 0;
}
} else if (parse::parse_json_number_no_ws(begin, end, nbegin, nend)) {
if (nend - nbegin == 1) {
if (*nbegin == '0') {
value = 0;
} else if (*nbegin == '1' || ectx->errmode == assign_error_nocheck) {
value = 1;
}
}
} else if (parse::parse_doublequote_string_no_ws(begin, end, nbegin, nend,
escaped)) {
if (!escaped) {
parse::string_to_bool(&value, nbegin, nend, option, ectx->errmode);
} else {
string s;
parse::unescape_string(nbegin, nend, s);
parse::string_to_bool(&value, s.data(), s.data() + s.size(), option,
ectx->errmode);
}
}
if (value < 2) {
if (tp.get_type_id() == bool_type_id) {
*out_data = value;
} else {
typed_data_assign(tp, arrmeta, out_data, ndt::type::make<bool1>(), NULL,
&value);
}
rbegin = begin;
} else if (value == 2 && option) {
if (!tp.is_expression()) {
*out_data = value;
} else {
typed_data_assign(tp, arrmeta, out_data,
ndt::option_type::make(ndt::type::make<bool1>()), NULL,
&value);
}
rbegin = begin;
} else {
throw json_parse_error(rbegin, "expected a boolean true or false", tp);
}
}
示例14: make_assignment_kernel
size_t cstruct_type::make_assignment_kernel(
ckernel_builder *out, size_t offset_out,
const ndt::type& dst_tp, const char *dst_metadata,
const ndt::type& src_tp, const char *src_metadata,
kernel_request_t kernreq, assign_error_mode errmode,
const eval::eval_context *ectx) const
{
if (this == dst_tp.extended()) {
if (this == src_tp.extended()) {
return make_struct_identical_assignment_kernel(out, offset_out,
dst_tp,
dst_metadata, src_metadata,
kernreq, errmode, ectx);
} else if (src_tp.get_kind() == struct_kind) {
return make_struct_assignment_kernel(out, offset_out,
dst_tp, dst_metadata,
src_tp, src_metadata,
kernreq, errmode, ectx);
} else if (!src_tp.is_builtin()) {
return src_tp.extended()->make_assignment_kernel(out, offset_out,
dst_tp, dst_metadata,
src_tp, src_metadata,
kernreq, errmode, ectx);
}
}
stringstream ss;
ss << "Cannot assign from " << src_tp << " to " << dst_tp;
throw runtime_error(ss.str());
}
示例15: 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());
}