生成模板包

时间:2015-09-05 05:01:26

标签: c++ templates c++11 variadic

Generate<P<3>, P<5,0>, P<4,0,0>, P<3,0,1>>::type将是

Pack< A<0>, A<0,0>, A<0,0,0>, A<0,0,1>, A<0,0,2>, A<0,0,3>, A<0,1>, A<0,1,0>, A<0,1,1>, A<0,1,2>, A<0,2>, A<0,3>, A<0,4>, A<1>, A<2> >

因为P<3>表示对于n = 0,1,2存在P<n>; P<5,0>表示{= 1}}存在n = 0,1,2,3,4; A<0,n>表示{= 1}}存在n = 0,1,2,3; P<4,0,0>表示{= 1}}存在n = 0,1,2。 在排序方面,A<0,0,n>将始终位于P<3,0,1>之前,如果x&lt; y和A<0,1,n>将始终位于A<n1,n2,n3,...,nk,x,...>之前,其中第二个elipses ...非空。 所以我需要写出A<n1,n2,n3,...,nk,y,...>的实现。

对此的动机:如果A<n1,n2,n3,...,nk>具有某些可能性的A<n1,n2,n3,...,nk, ...>包,由上面的3,5,4和3等常量定义,那么所有可能类型的包将允许 通过迭代包生成特定的Generate<Packs...>实例。

这是我到目前为止所做的:

template <int... Is> class Object

但现在我坚持使用Generate中只有2包的情况。任何人都可以帮我继续吗?

想法:对于2个包,只需单独生成,合并两个Is...,然后排序?但那时排序将是我认为最困难的部分。

2 个答案:

答案 0 :(得分:3)

诀窍在于意识到你从每一代产生的序列&#34;过程已经排序,问题减少到合并几个排序列表。

为简单起见,我制作了APackP空结构。

template <int...> class A {};
template <typename...> struct Pack {};
template <int...> struct P {};

从一个A生成一包P

template<int I, int... Tail>
auto do_sequence_for(P<I, Tail...>) -> std::make_integer_sequence<int, I>;

template<class PP>
using sequence_for = decltype(do_sequence_for(PP()));

template<int I, int... Front, int... Tail>
auto do_generate_single(P<I, Front...>, std::integer_sequence<int, Tail...>)
     -> Pack<A<Front..., Tail>...>;

template<class PP>
using generate_single = decltype(do_generate_single(PP(), sequence_for<PP>()));

两个A s的词典比较:

template<class A1, class A2>
struct compare; // returns A1 < A2

template<int I, int J, int...Is, int...Js>
struct compare<A<I, Is...>, A<J, Js...>> : std::integral_constant<bool, I < J> {};

template<int I, int...Is, int...Js>
struct compare<A<I, Is...>, A<I, Js...>> : compare<A<Is...>, A<Js...>> {};

template<int...Is>
struct compare<A<Is...>, A<>> : std::false_type {};

template<int J, int...Js>
struct compare<A<>, A<J, Js...>> : std::true_type {};

合并两个排序的A s包:

template<class Pack1, class Pack2, class Result=Pack<>>
struct merge2;

template<class A1, class...A1s, class A2, class...A2s, class...R>
struct merge2<Pack<A1, A1s...>, Pack<A2, A2s...>, Pack<R...>>
    : std::conditional_t<compare<A1, A2>::value,
                         merge2<Pack<A1s...>, Pack<A2, A2s...>, Pack<R..., A1>>,
                         merge2<Pack<A1, A1s...>, Pack<A2s...>, Pack<R..., A2>>>
{};

template<class...A1s, class...R>
struct merge2<Pack<A1s...>, Pack<>, Pack<R...>>
{
    using type = Pack<R..., A1s...>;
};

template<class A2, class...A2s, class...R>
struct merge2<Pack<>, Pack<A2, A2s...>, Pack<R...>>
{
    using type = Pack<R..., A2, A2s...>;
};

合并许多已排序的A s包:

template<class... Packs>
struct merge;

template<class P1>
struct merge<P1> {
    using type = P1;
};

template<class P1, class P2, class... Ps>
struct merge<P1, P2, Ps...> : merge<typename merge2<P1, P2>::type, Ps...> {};

将所有这些结合在一起:

template<class...Ps>
struct Generate{
    using type = typename merge<generate_single<Ps>...>::type;
};

Demo

答案 1 :(得分:0)

不幸的是,Visual Studio 2015将不会编译TC的优秀解决方案(由于它有一些错误),因此以下内容适用于我编译Visual Studio 2015和GCC 5.1.0(并且也适用于任何积分型,任何类等......)。只需更换他的generate_single功能。

#include <iostream>
#include <utility>
#include <type_traits>

// Another way to generate a pack of A's from one P than the above.
template <typename T, template<T...> class Class, T N, T Count, typename Front, typename Output> struct GenerateSingleHelper;

template <typename T, template<T...> class Class, T N, T Count, template <T...> class Z, T... Is, template <typename...> class PackOfPacks, typename... As>
struct GenerateSingleHelper<T, Class, N, Count, Z<Is...>, PackOfPacks<As...>> : GenerateSingleHelper<T, Class, N, Count + 1, Z<Is...>, PackOfPacks<As..., Class<Is..., Count>>> {};

template <typename T, template<T...> class Class, T N, template <T...> class Z, T... Is, template <typename...> class PackOfPacks, typename... As>
struct GenerateSingleHelper<T, Class, N, N, Z<Is...>, PackOfPacks<As...>> {
    using type = PackOfPacks<As...>;
};

template <typename T, template<T...> class Class, template <typename...> class PackOfPacks, typename> struct GenerateSingle;

template <typename T, template<T...> class Class, template <typename...> class PackOfPacks, template <T...> class Z, T N, T... Is>
struct GenerateSingle<T, Class, PackOfPacks, Z<N, Is...>> : GenerateSingleHelper<T, Class, N, 0, Z<Is...>, PackOfPacks<>> {};

// Lexicographical comparison of two A's.
template <typename T, typename A1, typename A2> struct Compare;  // Determines if A1 < A2.

template <typename T, template <T...> class Pack, T I, T J, T... Is, T... Js>
struct Compare<T, Pack<I, Is...>, Pack<J, Js...>> : std::integral_constant<bool, I < J> {};

template <typename T, template <T...> class Pack, T I, T... Is, T... Js>
struct Compare<T, Pack<I, Is...>, Pack<I, Js...>> : Compare<T, Pack<Is...>, Pack<Js...>> {};

template <typename T, template <T...> class Pack, T... Is>
struct Compare<T, Pack<Is...>, Pack<>> : std::false_type {};

template <typename T, template <T...> class Pack, T J, T... Js>
struct Compare<T, Pack<>, Pack<J, Js...>> : std::true_type {};  // J is needed to indicate that Pack<J, Js...> is not empty.

// Merging two sorted packs of A's.
template <typename T, template <typename...> class PackOfPacks, typename PackOfPacks1, typename PackOfPacks2, typename Result = PackOfPacks<>> struct MergeTwoPacks;

template <typename T, template <typename...> class PackOfPacks, typename A1, typename... A1s, typename A2, typename... A2s, typename... Accumulated>
struct MergeTwoPacks<T, PackOfPacks, PackOfPacks<A1, A1s...>, PackOfPacks<A2, A2s...>, PackOfPacks<Accumulated...>> : std::conditional_t<Compare<T, A1, A2>::value,
    MergeTwoPacks<T, PackOfPacks, PackOfPacks<A1s...>, PackOfPacks<A2, A2s...>, PackOfPacks<Accumulated..., A1>>,
    MergeTwoPacks<T, PackOfPacks, PackOfPacks<A1, A1s...>, PackOfPacks<A2s...>, PackOfPacks<Accumulated..., A2>>> {};

template <typename T, template <typename...> class PackOfPacks, typename... A1s, typename... Accumulated>
struct MergeTwoPacks<T, PackOfPacks, PackOfPacks<A1s...>, PackOfPacks<>, PackOfPacks<Accumulated...>> {
    using type = PackOfPacks<Accumulated..., A1s...>;  // Since PackOfPacks<A1s...> is already sorted.
};

template <typename T, template <typename...> class PackOfPacks, typename A2, typename... A2s, typename... Accumulated>
struct MergeTwoPacks<T, PackOfPacks, PackOfPacks<>, PackOfPacks<A2, A2s...>, PackOfPacks<Accumulated...>> {  // A2 is needed to indicate that PackOfPacks<A2, A2s...> is not empty.
    using type = PackOfPacks<Accumulated..., A2, A2s...>;  // Since PackOfPacks<A2s...> is already sorted.
};

// Merging any number of sorted packs of A's.
template <typename T, template <typename...> class PackOfPacks, typename... Packs> struct Merge;

template<typename T, template <typename...> class PackOfPacks, typename First, typename Second, typename... Rest>
struct Merge<T, PackOfPacks, First, Second, Rest...> : Merge<T, PackOfPacks, typename MergeTwoPacks<T, PackOfPacks, First, Second>::type, Rest...> {};

template <typename T, template <typename...> class PackOfPacks, typename Last>
struct Merge<T, PackOfPacks, Last> {
    using type = Last;
};

// Putting it all together.
template <typename T, template <T...> class Class, template <typename...> class PackOfPacks, typename... Packs>
struct Generate : Merge<T, PackOfPacks, typename GenerateSingle<T, Class, PackOfPacks, Packs>::type...> {};

// Testing.
template <int...> class A {};
template <typename...> struct PackOfPacks;
template <int...> struct P;

int main() {
    std::cout << std::boolalpha << std::is_same<
        Generate<int, A, PackOfPacks, P<3>, P<5,0>, P<4,0,0>, P<3,0,1>>::type,
        PackOfPacks< A<0>, A<0,0>, A<0,0,0>, A<0,0,1>, A<0,0,2>, A<0,0,3>, A<0,1>, A<0,1,0>, A<0,1,1>, A<0,1,2>, A<0,2>, A<0,3>, A<0,4>, A<1>, A<2> >
    >::value << '\n';  // true
}