c++-模板元编程TypeList进阶操作Select

c+±模板元编程TypeList定义及基本操作

c+±模板元编程ValueList定义及基本操作

#include "def.h"
#include "valueList.h"

// select
template<typename List, typename Indices>
struct Select_;

template<typename List, typename Indices>
using Select_t = typename Select_<List, Indices>::type;

template<typename List, unsigned ...Indexes>
struct Select_<List, ValueList<unsigned, Indexes...>> {
    using type = TypeList<NthElem_t<List, Indexes>...>;
};

void select_test() {
    using SignedIntegralTypes = TypeList<signed char, short, int, long, long long>;
    using ReversedSignedIntegralTypes = Select_t<SignedIntegralTypes, ValueList<unsigned, 4, 3, 2, 1, 0>>;
    cout << boost::typeindex::type_id_with_cvr<ReversedSignedIntegralTypes>().pretty_name() << endl;
}

// select 实战 实现 insertionSort; 排序 index索引, 使用 Select生成目标结果

// 有序索引列表插入
template<typename List, typename SortedIndices, template<class, class, class> typename Compare, typename Elem, bool = IsEmpty_v<SortedIndices>>
struct InsertSortedIndex_ {
    using Front = Front_t<SortedIndices>;
    using Tail = typename conditional_t<
            Compare<List, Elem, Front>::value,
            Identity<SortedIndices>,
            InsertSortedIndex_<List, PopFront_t<SortedIndices>, Compare, Elem>>::type;

    using Head = conditional_t<
            Compare<List, Elem, Front>::value,
            Elem,
            Front>;

    using type = PushFront_t<Tail, Head>;
};

template<typename List, typename SortedIndices, template<class, class, class> typename Compare, typename Elem>
using InsertSortedIndex_t = typename InsertSortedIndex_<List, SortedIndices, Compare, Elem>::type;

template<typename List, typename SortedIndices, template<class, class, class> typename Compare, typename Elem>
struct InsertSortedIndex_<List, SortedIndices, Compare, Elem, true> {
    using type = PushFront_t<SortedIndices, Elem>;
};

// 排序索引
template<typename List, typename Indices, template<class, class, class> typename Compare, bool = IsEmpty_v<Indices>>
struct InsertionIndexSort_ {
    using Front = Front_t<Indices>;
    using Tail = typename InsertionIndexSort_<List, PopFront_t<Indices>, Compare>::type;
    using type = InsertSortedIndex_t<List, Tail, Compare, Front>;
};

template<typename List, typename Indices, template<class, class, class> typename Compare>
struct InsertionIndexSort_<List, Indices, Compare, true> {
    using type = Indices;
};

template<typename List, typename Indices, template<class, class, class> typename Compare, bool = IsEmpty_v<List>>
struct NewInsertionSort_ {
    using NewIndexes = typename InsertionIndexSort_<List, Indices, Compare>::type;
    using type = Select_t<List, NewIndexes>;
};
template<typename List, typename Indices, template<class, class, class> typename Compare>
using NewInsertionSort_t = typename NewInsertionSort_<List, Indices, Compare>::type;


template<typename List, typename Indices, template<class, class, class> typename Compare>
struct NewInsertionSort_<List, Indices, Compare, true> {
    using type = List;
};

template<typename List, typename Left, typename Right>
struct IndexCompare {
	// 按照类型的大小排序
    constexpr static auto value = sizeof(NthElem_t<List, Left::value>) < sizeof(NthElem_t<List, Right::value>);
};

template<typename T, auto ...Indexes>
auto makeTypeListIndices(index_sequence<Indexes...>) {
    return ValueList<T, Indexes...>{};
}

void insertionSort_test() {
    using SL = TypeList<long, char, int, short>;
    using Indices = decltype(makeTypeListIndices<unsigned>(make_index_sequence<ListSize_v<SL>>()));
    // 排序
    using Sortted = NewInsertionSort_t<SL, Indices, IndexCompare>;
    cout << boost::typeindex::type_id_with_cvr<Sortted>().pretty_name() << endl;
}

int main() {
    select_test();
    insertionSort_test();
}

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值