如何解决通过删除重复项来生成新的integer_sequence
我实现了编译时检查,以检查是否使用下面给出的代码对某些内容进行了排序:
template<typename IntegerSequence>
struct is_sorted {
static constexpr bool value = true;
};
template<typename Integer,Integer Head,Integer Next,Integer... Tail>
struct is_sorted<std::integer_sequence<Integer,Head,Next,Tail...>> {
static constexpr bool value = Head <= Next && is_sorted<std::integer_sequence<Integer,Tail...>>::value;
};
上面的代码有效。我打算用这些排序检查创建另外两个元函数,这些函数将生成没有重复的新序列
using in_seq = std::integer_sequence<int,1,2,3,4>;
using mod_seq = is_sorted<in_seq>::value ? remove_duplicates<in_seq>::uniq_seq : in_seq;
// Examples
// in_seq = 1,4 -> mod_seq = 1,4
// in_seq = 1,4
如何在编译时使用模板从整数序列中删除重复项。 也可以在执行排序检查时删除重复项,在这种情况下,如果我们在模板检测到序列未排序后立即停止删除重复项,那很好。
// partial sort example 4,4,5,1 -> 4,1 (not sure if this is possible,but just curious)
我不确定如何动态生成新的std::integer_sequence
。
解决方法
由于所有std算法现在在C ++ 20中都是constexpr
,因此我们可以使用它以自然方式进行编译时编程(就像@cigien这样说):
template <typename T,T... Ints>
constexpr auto unique_until_nonsorted(std::integer_sequence<T,Ints...>) {
// constexpr structured bindings are not allow :(
constexpr auto pair = [] {
std::array<T,sizeof...(Ints)> arr{Ints...};
// get last iterator of unique
auto sorted_end = std::is_sorted_until(arr.begin(),arr.end());
// unique until last iterator
auto unique_end = std::unique(arr.begin(),sorted_end);
// copy nonsorted elements to last iterator
auto copy_end = std::copy(sorted_end,arr.end(),unique_end);
// get final arr size
auto size = std::distance(arr.begin(),copy_end);
return std::pair{arr,size};
}();
constexpr auto arr = pair.first;
constexpr auto size = pair.second;
// using template lambda to expand pack
return [&arr]<std::size_t... Is>(std::index_sequence<Is...>) {
return std::integer_sequence<T,arr[Is]...>{};
}(std::make_index_sequence<size>{});
}
template <typename X,X... Xs,typename Y,Y... Ys>
constexpr bool operator==(std::integer_sequence<X,Xs...>,std::integer_sequence<Y,Ys...>) noexcept {
return ((Xs == Ys) && ...);
}
static_assert(unique_until_nonsorted(std::index_sequence<4,4,5,3,2,1>{}) ==
std::index_sequence<4,1>{});
static_assert(unique_until_nonsorted(std::index_sequence<1,4>{}) ==
std::index_sequence<1,4>{});
static_assert(unique_until_nonsorted(std::index_sequence<1,4>{});
static_assert(unique_until_nonsorted(std::index_sequence<2,1,1>{}) ==
std::index_sequence<2,2>{}) ==
std::index_sequence<1,2>{});
// corner case
static_assert(unique_until_nonsorted(std::index_sequence<>{}) ==
std::index_sequence<>{});
,
在执行排序检查时也可以删除重复项,在这种情况下,如果我们在模板检测到序列未排序后立即停止删除重复项,那很好。
是的,有可能。
也许可以简化一些,但是...使用和滥用模板专业化...
给出如下的帮助器类
// csardh: check sort and remove duplicates helper
template <typename T,typename>
struct csardh
{ using type = T; };
template <typename T,T ... ordered,T v0,T v1,T ... vs>
struct csardh<std::integer_sequence<T,ordered...>,std::integer_sequence<T,v0,v1,vs...>>
: public std::conditional_t<
(v0 < v1),csardh<std::integer_sequence<T,ordered...,v0>,vs...>>,vs...>,void>>
{ };
template <typename T,vs...>>
: public csardh<std::integer_sequence<T,vs...>>
{ };
template <typename T,T v0>
struct csardh<std::integer_sequence<T,v0>>
: public csardh<std::integer_sequence<T,void>
{ };
主要班级成为
// csard: check sort and remove duplicates
template <typename>
struct csard;
template <typename T,T... vals>
struct csard<std::integer_sequence<T,vals...>>
: public csardh<std::integer_sequence<T>,vals...>>
{ };
using
可以简化使用
template <typename T>
using csard_t = typename csard<T>::type;
以下是完整的编译示例(C ++ 14足够)
#include <utility>
#include <type_traits>
// csardh: check sort and remove duplicates helper
template <typename T,void>
{ };
// csard: check sort and remove duplicates
template <typename>
struct csard;
template <typename T,vals...>>
{ };
template <typename T>
using csard_t = typename csard<T>::type;
int main()
{
using T0 = std::integer_sequence<int,1>;
using T1 = csard_t<T0>;
using T2 = std::integer_sequence<int,1>;
static_assert( std::is_same<T1,T2>::value,"!" );
using U0 = std::integer_sequence<int,4>;
using U1 = csard_t<U0>;
using U2 = U0;
static_assert( std::is_same<U1,U2>::value,"!" );
using V0 = std::integer_sequence<int,4>;
using V1 = csard_t<V0>;
using V2 = U2;
static_assert( std::is_same<V1,V2>::value,"!" );
}
,
让我们使用Boost.Mp11进行此操作。此处,“整数序列”的概念略有不同,我们处理类型列表。 mp_list_c<int,2>
是类型mp_list<mp_int<1>,mp_int<2>>
。这使许多元编程变得更加容易。这里的一切都是单线的。
首先,is_sorted
。 Mp11(震惊器)当前缺少的一种算法是能够执行zip_with
的功能-也就是说,获取一个列表并压缩列表,使其自身偏移一个:
template <typename L,template <typename...> class P>
using zip_with = mp_transform<P,mp_pop_back<L>,mp_pop_front<L>>;
现在,is_sorted
基本上是:获取该列表,并检查每对中第一个元素是否少于第二个元素:
template <typename L>
using is_sorted = mp_rename<zip_with<L,mp_less>,mp_all>;
要删除重复项?那就是已经有一种算法用于mp_unique
。
完全放入
:using mod_seq = mp_if_c<is_sorted<L>::value,mp_unique<L>,L>;
没有类型的条件运算符,您需要类似mp_if
(或std::conditional_t
)的东西。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。