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

Generate a new integer_sequence by removing duplicates

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

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

让我们用 Boost.Mp11 来做这件事。在那里,“整数序列”的概念有点不同,我们处理类型列表。 mp_list_c<int, 1, 2> 是类型 mp_list<mp_int<1>, mp_int<2>>。这使得很多元编程变得容易得多。这里的一切都是 one-liner.

首先,is_sorted。 Mp11(令人震惊)目前缺少的一种算法是能够执行 zip_with - 也就是说,获取一个列表并将该列表自身偏移一个 zip:

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)的东西。

Also is it possible to remove duplicates while we are performing the sort check, in this case I am fine if we stop removing duplicates as soon as templates detect that sequence is not sorted.

是的,有可能。

也许可以做得更简单一些,但是...使用和滥用模板特化...

给定一个助手class如下

// 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>
 { };

主要class成为

// 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, "!" );
 }

由于所有标准算法现在在 C++20 中都是 constexpr,我们可以使用它以自然方式进行 compile-time 编程(只是喜欢 @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<>{});