#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();
}
c++-模板元编程TypeList进阶操作Select
最新推荐文章于 2022-10-29 20:11:57 发布