return 来自函数的不同范围构造
return different range constructs from function
我尝试获得表现得像真假掩码的范围视图。为了进行逻辑运算,我想实现掩码的 ands 和 ors。我有一个工作编译时间或:
struct make_or_mask_fn
{
template<typename... Msks>
auto operator()(Msks&&... msks) const
{
CONCEPT_ASSERT((Range<Msks>() || ...));
return ranges::view::zip(std::forward<Msks>(msks)...) |
ranges::view::transform(
[](auto&& range_item) -> bool {
return tuple_or(range_item);
});
}
private:
template<typename... T>
static bool variable_length_or(const T... v)
{
return (v || ...);
}
template<typename... T, std::size_t... Idx>
static bool tuple_or(const std::tuple<T...> t,
std::index_sequence<Idx...>)
{
return variable_length_or(std::get<Idx>(t)...);
}
template<typename... T>
static bool tuple_or(const std::tuple<T...> t)
{
return tuple_or(t, std::index_sequence_for<T...>{});
}
};
RANGES_INLINE_VARIABLE(make_or_mask_fn, make_or_masker)
我可以这么称呼
std::vector<bool> mask1 = ...
std::vector<bool> mask2 = ...
std::vector<bool> mask3 = ...
auto or_of_masks = make_or_masker(mask1, mask2, mask3);
目前这不能做的是构建一些在编译时未知的掩码。我目前的尝试是接受范围的 vector
,检查其大小,然后调用可变参数模板或从之前调用:
struct make_vector_or_mask_fn
{
template<typename Msk>
auto operator()(std::vector<Msk> msks)
const // TODO const and reference types
{
CONCEPT_ASSERT(Range<Msk>());
// todo return range with all true (an or of zero elements is true)
assert(msks.size() != 0);
if(msks.size() == 1)
return or_ranges(msks[0]);
if(msks.size() == 2)
return or_ranges(msks[0], msks[1]);
if(msks.size() == 3)
return or_ranges(msks[0], msks[1], msks[2]);
/// TODO: go until ... maybe 8 and recurse afterwards
}
private:
template<typename... Msks>
static auto or_ranges(Msks&&... msks)
{
CONCEPT_ASSERT((Range<Msks>() || ...));
return ranges::view::zip(std::forward<Msks>(msks)...) |
ranges::view::transform(
[](auto&& range_item) -> bool {
return tuple_or(range_item);
});
}
template<typename... T>
static bool variable_length_or(const T... v)
{
return (v || ...);
}
template<typename... T, std::size_t... Idx>
static bool tuple_or(const std::tuple<T...> t,
std::index_sequence<Idx...>)
{
return variable_length_or(std::get<Idx>(t)...);
}
template<typename... T>
static bool tuple_or(const std::tuple<T...> t)
{
return tuple_or(t, std::index_sequence_for<T...>{});
}
};
RANGES_INLINE_VARIABLE(make_vector_or_mask_fn, make_vector_or_masker)
编译时出现以下错误:
../include/range/v3/view/mask.hpp:221:25: error: 'auto' in return type deduced as 'ranges::v3::transform_view<ranges::v3::zip_view<ranges::v3::iterator_range<__gnu_cxx::__normal_iterator<int *, std::vector<int, std::allocator<int> > >, __gnu_cxx::__normal_iterator<int *, std::vector<int, std::allocator<int> > > >, ranges::v3::iterator_range<__gnu_cxx::__normal_iterator<int *, std::vector<int, std::allocator<int> > >,
__gnu_cxx::__normal_iterator<int *, std::vector<int, std::allocator<int> > > > >, (lambda at ../include/range/v3/view/mask.hpp:188:32)>' here but deduced as 'ranges::v3::transform_view<ranges::v3::zip_view<ranges::v3::iterator_range<__gnu_cxx::__normal_iterator<int *, std::vector<int, std::allocator<int> > >, __gnu_cxx::__normal_iterator<int *, std::vector<int, std::allocator<int> > > > >,
(lambda at ../include/range/v3/view/mask.hpp:188:32)>' in earlier return statement
return or_ranges(msks[0], msks[1]);
^
根据我的理解,这告诉我我的 or_ranges
returns 是一种不同的类型,具体取决于参数的数量。 (我正在使用的 zip
保留了压缩在一起的内容的知识)。
所以我想知道,如何键入擦除范围内的内容?
Return ranges::v3::any_input_view < bool >
而不是 auto
.
即使在优化构建中,这也会慢 10 倍。类型擦除很昂贵。
知道输入范围有长度并在分发之前在缓冲块中获取数据的手动解决方案可能会使性能更接近非类型擦除。不过,我希望它仍然会明显变慢。
我尝试获得表现得像真假掩码的范围视图。为了进行逻辑运算,我想实现掩码的 ands 和 ors。我有一个工作编译时间或:
struct make_or_mask_fn
{
template<typename... Msks>
auto operator()(Msks&&... msks) const
{
CONCEPT_ASSERT((Range<Msks>() || ...));
return ranges::view::zip(std::forward<Msks>(msks)...) |
ranges::view::transform(
[](auto&& range_item) -> bool {
return tuple_or(range_item);
});
}
private:
template<typename... T>
static bool variable_length_or(const T... v)
{
return (v || ...);
}
template<typename... T, std::size_t... Idx>
static bool tuple_or(const std::tuple<T...> t,
std::index_sequence<Idx...>)
{
return variable_length_or(std::get<Idx>(t)...);
}
template<typename... T>
static bool tuple_or(const std::tuple<T...> t)
{
return tuple_or(t, std::index_sequence_for<T...>{});
}
};
RANGES_INLINE_VARIABLE(make_or_mask_fn, make_or_masker)
我可以这么称呼
std::vector<bool> mask1 = ...
std::vector<bool> mask2 = ...
std::vector<bool> mask3 = ...
auto or_of_masks = make_or_masker(mask1, mask2, mask3);
目前这不能做的是构建一些在编译时未知的掩码。我目前的尝试是接受范围的 vector
,检查其大小,然后调用可变参数模板或从之前调用:
struct make_vector_or_mask_fn
{
template<typename Msk>
auto operator()(std::vector<Msk> msks)
const // TODO const and reference types
{
CONCEPT_ASSERT(Range<Msk>());
// todo return range with all true (an or of zero elements is true)
assert(msks.size() != 0);
if(msks.size() == 1)
return or_ranges(msks[0]);
if(msks.size() == 2)
return or_ranges(msks[0], msks[1]);
if(msks.size() == 3)
return or_ranges(msks[0], msks[1], msks[2]);
/// TODO: go until ... maybe 8 and recurse afterwards
}
private:
template<typename... Msks>
static auto or_ranges(Msks&&... msks)
{
CONCEPT_ASSERT((Range<Msks>() || ...));
return ranges::view::zip(std::forward<Msks>(msks)...) |
ranges::view::transform(
[](auto&& range_item) -> bool {
return tuple_or(range_item);
});
}
template<typename... T>
static bool variable_length_or(const T... v)
{
return (v || ...);
}
template<typename... T, std::size_t... Idx>
static bool tuple_or(const std::tuple<T...> t,
std::index_sequence<Idx...>)
{
return variable_length_or(std::get<Idx>(t)...);
}
template<typename... T>
static bool tuple_or(const std::tuple<T...> t)
{
return tuple_or(t, std::index_sequence_for<T...>{});
}
};
RANGES_INLINE_VARIABLE(make_vector_or_mask_fn, make_vector_or_masker)
编译时出现以下错误:
../include/range/v3/view/mask.hpp:221:25: error: 'auto' in return type deduced as 'ranges::v3::transform_view<ranges::v3::zip_view<ranges::v3::iterator_range<__gnu_cxx::__normal_iterator<int *, std::vector<int, std::allocator<int> > >, __gnu_cxx::__normal_iterator<int *, std::vector<int, std::allocator<int> > > >, ranges::v3::iterator_range<__gnu_cxx::__normal_iterator<int *, std::vector<int, std::allocator<int> > >,
__gnu_cxx::__normal_iterator<int *, std::vector<int, std::allocator<int> > > > >, (lambda at ../include/range/v3/view/mask.hpp:188:32)>' here but deduced as 'ranges::v3::transform_view<ranges::v3::zip_view<ranges::v3::iterator_range<__gnu_cxx::__normal_iterator<int *, std::vector<int, std::allocator<int> > >, __gnu_cxx::__normal_iterator<int *, std::vector<int, std::allocator<int> > > > >,
(lambda at ../include/range/v3/view/mask.hpp:188:32)>' in earlier return statement
return or_ranges(msks[0], msks[1]);
^
根据我的理解,这告诉我我的 or_ranges
returns 是一种不同的类型,具体取决于参数的数量。 (我正在使用的 zip
保留了压缩在一起的内容的知识)。
所以我想知道,如何键入擦除范围内的内容?
Return ranges::v3::any_input_view < bool >
而不是 auto
.
即使在优化构建中,这也会慢 10 倍。类型擦除很昂贵。
知道输入范围有长度并在分发之前在缓冲块中获取数据的手动解决方案可能会使性能更接近非类型擦除。不过,我希望它仍然会明显变慢。