本文整理汇总了C++中nd::array::eval_immutable方法的典型用法代码示例。如果您正苦于以下问题:C++ array::eval_immutable方法的具体用法?C++ array::eval_immutable怎么用?C++ array::eval_immutable使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类nd::array
的用法示例。
在下文中一共展示了array::eval_immutable方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: type_error
categorical_type::categorical_type(const nd::array& categories, bool presorted)
: base_type(categorical_type_id, custom_kind, 4, 4, type_flag_scalar, 0, 0, 0)
{
intptr_t category_count;
if (presorted) {
// This is construction shortcut, for the case when the categories are already
// sorted. No validation of this is done, the caller should have ensured it
// was correct already, typically by construction.
m_categories = categories.eval_immutable();
m_category_tp = m_categories.get_type().at(0);
category_count = categories.get_dim_size();
m_value_to_category_index.resize(category_count);
m_category_index_to_value.resize(category_count);
for (size_t i = 0; i != (size_t)category_count; ++i) {
m_value_to_category_index[i] = i;
m_category_index_to_value[i] = i;
}
} else {
// Process the categories array to make sure it's valid
const ndt::type& cdt = categories.get_type();
if (cdt.get_type_id() != strided_dim_type_id) {
throw dynd::type_error("categorical_type only supports construction from a strided array of categories");
}
m_category_tp = categories.get_type().at(0);
if (!m_category_tp.is_scalar()) {
throw dynd::type_error("categorical_type only supports construction from a 1-dimensional strided array of categories");
}
category_count = categories.get_dim_size();
intptr_t categories_stride = reinterpret_cast<const strided_dim_type_arrmeta *>(categories.get_arrmeta())->stride;
const char *categories_element_arrmeta = categories.get_arrmeta() + sizeof(strided_dim_type_arrmeta);
comparison_ckernel_builder k;
::make_comparison_kernel(&k, 0,
m_category_tp, categories_element_arrmeta,
m_category_tp, categories_element_arrmeta,
comparison_type_sorting_less, &eval::default_eval_context);
cmp less(k.get_function(), k.get());
set<const char *, cmp> uniques(less);
m_value_to_category_index.resize(category_count);
m_category_index_to_value.resize(category_count);
// create the mapping from indices of (to be lexicographically sorted) categories to values
for (size_t i = 0; i != (size_t)category_count; ++i) {
m_category_index_to_value[i] = i;
const char *category_value = categories.get_readonly_originptr() +
i * categories_stride;
if (uniques.find(category_value) == uniques.end()) {
uniques.insert(category_value);
} else {
stringstream ss;
ss << "categories must be unique: category value ";
m_category_tp.print_data(ss, categories_element_arrmeta, category_value);
ss << " appears more than once";
throw std::runtime_error(ss.str());
}
}
// TODO: Putting everything in a set already caused a sort operation to occur,
// there's no reason we should need a second sort.
std::sort(m_category_index_to_value.begin(), m_category_index_to_value.end(),
sorter(categories.get_readonly_originptr(), categories_stride,
k.get_function(), k.get()));
// invert the m_category_index_to_value permutation
for (uint32_t i = 0; i < m_category_index_to_value.size(); ++i) {
m_value_to_category_index[m_category_index_to_value[i]] = i;
}
m_categories = make_sorted_categories(uniques, m_category_tp,
categories_element_arrmeta);
}
// Use the number of categories to set which underlying integer storage to use
if (category_count <= 256) {
m_storage_type = ndt::make_type<uint8_t>();
} else if (category_count <= 65536) {
m_storage_type = ndt::make_type<uint16_t>();
} else {
m_storage_type = ndt::make_type<uint32_t>();
}
m_members.data_size = m_storage_type.get_data_size();
m_members.data_alignment = (uint8_t)m_storage_type.get_data_alignment();
}
示例2: type_error
ndt::categorical_type::categorical_type(const nd::array &categories, bool presorted)
: base_type(categorical_id, 4, 4, type_flag_none, 0, 0, 0)
{
intptr_t category_count;
if (presorted) {
// This is construction shortcut, for the case when the categories are
// already
// sorted. No validation of this is done, the caller should have ensured it
// was correct already, typically by construction.
m_categories = categories.eval_immutable();
m_category_tp = m_categories.get_type().at(0);
category_count = categories.get_dim_size();
m_value_to_category_index = nd::range(category_count);
m_value_to_category_index.flag_as_immutable();
m_category_index_to_value = m_value_to_category_index;
}
else {
// Process the categories array to make sure it's valid
const type &cdt = categories.get_type();
if (cdt.get_id() != fixed_dim_id) {
throw dynd::type_error("categorical_type only supports construction from "
"a fixed-dim array of categories");
}
m_category_tp = categories.get_type().at(0);
if (!m_category_tp.is_scalar()) {
throw dynd::type_error("categorical_type only supports construction from "
"a 1-dimensional strided array of categories");
}
category_count = categories.get_dim_size();
intptr_t categories_stride = reinterpret_cast<const fixed_dim_type_arrmeta *>(categories.get()->metadata())->stride;
const char *categories_element_arrmeta = categories.get()->metadata() + sizeof(fixed_dim_type_arrmeta);
nd::kernel_builder k;
kernel_single_t fn = k.get()->get_function<kernel_single_t>();
cmp less(fn, k.get());
set<const char *, cmp> uniques(less);
m_value_to_category_index = nd::empty(category_count, make_type<intptr_t>());
m_category_index_to_value = nd::empty(category_count, make_type<intptr_t>());
// create the mapping from indices of (to be lexicographically sorted)
// categories to values
for (size_t i = 0; i != (size_t)category_count; ++i) {
unchecked_fixed_dim_get_rw<intptr_t>(m_category_index_to_value, i) = i;
const char *category_value = categories.cdata() + i * categories_stride;
if (uniques.find(category_value) == uniques.end()) {
uniques.insert(category_value);
}
else {
stringstream ss;
ss << "categories must be unique: category value ";
m_category_tp.print_data(ss, categories_element_arrmeta, category_value);
ss << " appears more than once";
throw std::runtime_error(ss.str());
}
}
// TODO: Putting everything in a set already caused a sort operation to
// occur,
// there's no reason we should need a second sort.
std::sort(&unchecked_fixed_dim_get_rw<intptr_t>(m_category_index_to_value, 0),
&unchecked_fixed_dim_get_rw<intptr_t>(m_category_index_to_value, category_count),
sorter(categories.cdata(), categories_stride, fn, k.get()));
// invert the m_category_index_to_value permutation
for (intptr_t i = 0; i < category_count; ++i) {
unchecked_fixed_dim_get_rw<intptr_t>(m_value_to_category_index,
unchecked_fixed_dim_get<intptr_t>(m_category_index_to_value, i)) = i;
}
m_categories = make_sorted_categories(uniques, m_category_tp, categories_element_arrmeta);
}
// Use the number of categories to set which underlying integer storage to use
if (category_count <= 256) {
m_storage_type = make_type<uint8_t>();
}
else if (category_count <= 65536) {
m_storage_type = make_type<uint16_t>();
}
else {
m_storage_type = make_type<uint32_t>();
}
this->data_size = m_storage_type.get_data_size();
this->data_alignment = (uint8_t)m_storage_type.get_data_alignment();
}