Fab*_*bio 5 c++ sorting metaprogramming c++11 c++14
我有数据结构:
template <int...I> struct index {};
template <typename...T> struct data {};
template <int I, int J> struct X
{
static constexpr int i = I;
static constexpr int j = J;
};
typedef data< X<0,4>, X<1,2>, X<2,1>, X<1,6>, X<1,3> > data_t;
Run Code Online (Sandbox Code Playgroud)
如果数据不包含重复项且索引J很小,则在0-31范围内.
我想创建一个静态索引,其中包含索引I等于某个给定值(例如I = 1)的所有X的数据中的位置,按索引J排序.这是我觉得困难的"排序"位.
例如,我想实现一个类build_index,以便:
typedef build_index<1,data>::type_t index_t;
Run Code Online (Sandbox Code Playgroud)
生成相同的:
typedef index<1, 4, 3> index_t;
Run Code Online (Sandbox Code Playgroud)
它反映了由J:X(1,2)在数据(1),X(1,3)在数据(4),X(1,6)处排序的元素X(1,J)的数据中的位置.数据(3)
我宁愿不使用STL,因为它不适用于gcc-avr,尽管我可以移植选定的片段.
当您在 C++ 模板编程中遇到复杂的问题时,尝试将其分解为几个较小的步骤(就像大多数编程问题一样)会很有帮助。这是一条可能的路径:
这是相应的代码。我正在使用std::conditional,但这当然很容易更换。我std::is_same在测试中使用,当然你并不真正需要它(否则实现起来会很简单)。
你的东西 + std::conditional 和 std::is_same 的实用程序标头
#include <utility>
template <int... I>
struct index
{
};
template <typename... T>
struct data
{
};
template <int I, int J>
struct X
{
static constexpr int i = I;
static constexpr int j = J;
};
typedef data<X<0, 4>, X<1, 2>, X<2, 1>, X<1, 6>, X<1, 3>> data_t;
Run Code Online (Sandbox Code Playgroud)
提取与我们要查找的Xs 匹配的 s并将s 替换为位置。Ii
template <int Pos, int I, typename Extracted, typename Rest>
struct ExtractImpl;
template <int Pos, int I, typename... ExtractedX>
struct ExtractImpl<Pos, I, data<ExtractedX...>, data<>>
{
using type = data<ExtractedX...>;
};
template <int Pos, int I, typename... ExtractedX, typename T, typename... Rest>
struct ExtractImpl<Pos, I, data<ExtractedX...>, data<T, Rest...>>
{
using type = typename std::conditional<
(T::i == I),
typename ExtractImpl<Pos + 1,
I,
data<ExtractedX..., X<Pos, T::j>>,
data<Rest...>>::type,
typename ExtractImpl<Pos + 1, I, data<ExtractedX...>, data<Rest...>>::
type>::type;
};
template <int I, typename Data>
struct Extract
{
using type = typename ExtractImpl<0, I, data<>, Data>::type;
};
using extracted = typename Extract<1, data_t>::type;
static_assert(std::is_same<extracted, data<X<1, 2>, X<3, 6>, X<4, 3>>>::value, "");
Run Code Online (Sandbox Code Playgroud)
按 J 排序。这是通过将元素增量插入排序列表来完成的。可能有更优雅的方法来做到这一点。
template <typename T, typename LessList, typename RestList>
struct insert_impl;
template <typename T, typename... Lesser>
struct insert_impl<T, data<Lesser...>, data<>>
{
using type = data<Lesser..., T>;
};
template <typename T, typename... Lesser, typename Next, typename... Rest>
struct insert_impl<T, data<Lesser...>, data<Next, Rest...>>
{
using type = typename std::conditional<
(T::j < Next::j),
data<Lesser..., T, Next, Rest...>,
typename insert_impl<T, data<Lesser..., Next>, data<Rest...>>::type>::
type;
};
template <typename T, typename SortedList>
struct insert
{
using type = typename insert_impl<T, data<>, SortedList>::type;
};
template <typename SortedList, typename UnsortedList>
struct SortImpl;
template <typename SortedList>
struct SortImpl<SortedList, data<>>
{
using type = SortedList;
};
template <typename SortedList, typename T, typename... UnsortedX>
struct SortImpl<SortedList, data<T, UnsortedX...>>
{
using type = typename SortImpl<typename insert<T, SortedList>::type,
data<UnsortedX...>>::type;
};
template <typename UnsortedList>
struct Sort
{
using type = typename SortImpl<data<>, UnsortedList>::type;
};
using sorted = typename Sort<extracted>::type;
static_assert(std::is_same<sorted, data<X<1, 2>, X<4, 3>, X<3, 6>>>::value, "");
Run Code Online (Sandbox Code Playgroud)
最后,提取您要查找的索引:
template <typename List>
struct Indexes;
template <typename... Data>
struct Indexes<data<Data...>>
{
using type = index<Data::i...>;
};
using result = typename Indexes<sorted>::type;
static_assert(std::is_same<result, index<1, 4, 3>>::value, "");
Run Code Online (Sandbox Code Playgroud)
警告:虽然我在代码中没有看到任何问题,但除了您的示例之外,我还没有对其进行测试......
| 归档时间: |
|
| 查看次数: |
219 次 |
| 最近记录: |