通过删除重复项来生成新的integer_sequence

时间:2020-08-10 20:08:46

标签: c++ c++17 sfinae c++20

我实现了编译时检查,以检查是否使用下面给出的代码对某些内容进行了排序:

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, Next, 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,2,3,4 -> mod_seq = 1,2,3,4
// in_seq = 1,2,2,3,4 -> mod_seq = 1,2,3,4

如何在编译时使用模板从整数序列中删除重复项。 也可以在执行排序检查时删除重复项,在这种情况下,如果我们在模板检测到序列未排序后立即停止删除重复项,那很好。

// partial sort example 4,4,4,5,5,3,2,2,1 -> 4,5,3,2,2,1 (not sure if this is possible, but just curious)

我不确定如何动态生成新的std::integer_sequence

3 个答案:

答案 0 :(得分:3)

由于所有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>{});
}    

Works for GCC and Clang.

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,4,5,5,3,2,2,1>{}) ==
                                     std::index_sequence<4,5,3,2,2,1>{});
static_assert(unique_until_nonsorted(std::index_sequence<1,2,3,4>{}) == 
                                     std::index_sequence<1,2,3,4>{});
static_assert(unique_until_nonsorted(std::index_sequence<1,2,2,3,4>{}) == 
                                     std::index_sequence<1,2,3,4>{});
static_assert(unique_until_nonsorted(std::index_sequence<2,2,2,2,4,1,1>{}) == 
                                     std::index_sequence<2,4,1,1>{});
static_assert(unique_until_nonsorted(std::index_sequence<1,1,1,2,3,2,2>{}) == 
                                     std::index_sequence<1,2,3,2,2>{});
// corner case
static_assert(unique_until_nonsorted(std::index_sequence<>{}) == 
                                     std::index_sequence<>{});

答案 1 :(得分:2)

在执行排序检查时也可以删除重复项,在这种情况下,如果我们在模板检测到序列未排序后立即停止删除重复项,那很好。

是的,有可能。

也许可以简化一些,但是...使用和滥用模板专业化...

给出如下的帮助器类

// 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>,
               std::integer_sequence<T, v1, vs...>>,
        csardh<std::integer_sequence<T, ordered..., v0, v1, vs...>, void>>
 { };

template <typename T, T ... ordered, T v0, T ... vs>
struct csardh<std::integer_sequence<T, ordered...>,
              std::integer_sequence<T, v0, v0, vs...>>
   : public csardh<std::integer_sequence<T, ordered...>,
                   std::integer_sequence<T, v0, vs...>>
 { };

template <typename T, T ... ordered, T v0>
struct csardh<std::integer_sequence<T, ordered...>,
              std::integer_sequence<T, v0>>
   : public csardh<std::integer_sequence<T, ordered..., v0>, 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>,
                   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, 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>,
               std::integer_sequence<T, v1, vs...>>,
        csardh<std::integer_sequence<T, ordered..., v0, v1, vs...>, void>>
 { };

template <typename T, T ... ordered, T v0, T ... vs>
struct csardh<std::integer_sequence<T, ordered...>,
              std::integer_sequence<T, v0, v0, vs...>>
   : public csardh<std::integer_sequence<T, ordered...>,
                   std::integer_sequence<T, v0, vs...>>
 { };

template <typename T, T ... ordered, T v0>
struct csardh<std::integer_sequence<T, ordered...>,
              std::integer_sequence<T, v0>>
   : public csardh<std::integer_sequence<T, ordered..., v0>, 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>,
                   std::integer_sequence<T, vals...>>
 { };

template <typename T>
using csard_t = typename csard<T>::type;

int main()
 {
   using T0 = std::integer_sequence<int,4,4,4,5,5,3,2,2,1>;
   using T1 = csard_t<T0>;
   using T2 = std::integer_sequence<int,4,5,3,2,2,1>;

   static_assert( std::is_same<T1, T2>::value, "!" );

   using U0 = std::integer_sequence<int,1,2,3,4>;
   using U1 = csard_t<U0>;
   using U2 = U0;

   static_assert( std::is_same<U1, U2>::value, "!" );

   using V0 = std::integer_sequence<int,1,2,2,3,4>;
   using V1 = csard_t<V0>;
   using V2 = U2;

   static_assert( std::is_same<V1, V2>::value, "!" );
 }

答案 2 :(得分:1)

让我们使用Boost.Mp11进行此操作。此处,“整数序列”的概念略有不同,我们处理类型列表。 mp_list_c<int, 1, 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)的东西。

相关问题