From 8d42ed7eec07d41c53d10de7fa7e3bef2d063a70 Mon Sep 17 00:00:00 2001 From: Mateusz Pusz Date: Fri, 28 Sep 2018 14:20:23 -0700 Subject: [PATCH] Type List interface made generic --- src/include/units/bits/type_list.h | 112 ++++++++++++++--------------- test/test_type_list.cpp | 11 +-- 2 files changed, 62 insertions(+), 61 deletions(-) diff --git a/src/include/units/bits/type_list.h b/src/include/units/bits/type_list.h index da2bc7da..ba2257de 100644 --- a/src/include/units/bits/type_list.h +++ b/src/include/units/bits/type_list.h @@ -27,32 +27,30 @@ namespace mp { - // type_list +// namespace detail { +// +// template +// struct is_type_list : std::false_type {}; +// +// template typename T, typename... Types> +// struct is_type_list> : std::true_type {}; +// +// } - template - struct type_list; - - namespace detail { - - template - struct is_type_list : std::false_type {}; - - template - struct is_type_list> : std::true_type {}; - - } - - template - bool concept TypeList = detail::is_type_list::value; +// template typename T, typename... Types> +// concept bool TypeList = requires(T) { +// std::is_empty_v>; +// }; +#define TypeList typename // push_front template struct type_list_push_front; - template - struct type_list_push_front, NewTypes...> { - using type = type_list; + template typename List, typename... OldTypes, typename... NewTypes> + struct type_list_push_front, NewTypes...> { + using type = List; }; template @@ -63,9 +61,9 @@ namespace mp { template struct type_list_push_back; - template - struct type_list_push_back, NewTypes...> { - using type = type_list; + template typename List, typename... OldTypes, typename... NewTypes> + struct type_list_push_back, NewTypes...> { + using type = List; }; template @@ -75,18 +73,18 @@ namespace mp { namespace detail { - template + template typename List, std::size_t Idx, std::size_t N, typename... Types> struct split_impl; - template - struct split_impl { - using first_list = type_list<>; - using second_list = type_list<>; + template typename List, std::size_t Idx, std::size_t N> + struct split_impl { + using first_list = List<>; + using second_list = List<>; }; - template - struct split_impl : split_impl { - using base = split_impl; + template typename List, std::size_t Idx, std::size_t N, typename T, typename... Rest> + struct split_impl : split_impl { + using base = split_impl; using base_first = typename base::first_list; using base_second = typename base::second_list; using first_list = std::conditional_t, base_first>; @@ -98,10 +96,10 @@ namespace mp { template struct type_list_split; - template - struct type_list_split, N> { + template typename List, std::size_t N, typename... Types> + struct type_list_split, N> { static_assert(N <= sizeof...(Types), "Invalid index provided"); - using split = detail::split_impl<0, N, Types...>; + using split = detail::split_impl; using first_list = typename split::first_list; using second_list = typename split::second_list; }; @@ -111,8 +109,8 @@ namespace mp { template struct type_list_split_half; - template - struct type_list_split_half> : type_list_split, (sizeof...(Types) + 1) / 2> { + template typename List, typename... Types> + struct type_list_split_half> : type_list_split, (sizeof...(Types) + 1) / 2> { }; // merge_sorted @@ -123,24 +121,24 @@ namespace mp { template typename Pred> using type_list_merge_sorted_t = typename type_list_merge_sorted::type; - template typename Pred> - struct type_list_merge_sorted, type_list<>, Pred> { - using type = type_list; + template typename List, typename... Lhs, template typename Pred> + struct type_list_merge_sorted, List<>, Pred> { + using type = List; }; - template typename Pred> - struct type_list_merge_sorted, type_list, Pred> { - using type = type_list; + template typename List, typename... Rhs, template typename Pred> + struct type_list_merge_sorted, List, Pred> { + using type = List; }; - template typename List, typename Lhs1, typename... LhsRest, typename Rhs1, typename... RhsRest, template typename Pred> - struct type_list_merge_sorted, type_list, Pred> { + struct type_list_merge_sorted, List, Pred> { using type = std::conditional_t< Pred::value, - type_list_push_front_t, type_list, Pred>, + type_list_push_front_t, List, Pred>, Lhs1>, - type_list_push_front_t, type_list, Pred>, + type_list_push_front_t, List, Pred>, Rhs1>>; }; @@ -149,26 +147,26 @@ namespace mp { template typename Pred> struct type_list_sort; - template typename Pred> - struct type_list_sort, Pred> { - using type = type_list<>; + template typename List, template typename Pred> + struct type_list_sort, Pred> { + using type = List<>; }; - template typename Pred> - struct type_list_sort, Pred> { - using type = type_list; + template typename List, typename T, template typename Pred> + struct type_list_sort, Pred> { + using type = List; }; - template typename Pred> - struct type_list_sort, Pred> { - using types = type_list; - using split = type_list_split_half>; + template typename List, typename... Types, template typename Pred> + struct type_list_sort, Pred> { + using types = List; + using split = type_list_split_half>; using left = typename type_list_sort::type; using right = typename type_list_sort::type; using type = type_list_merge_sorted_t; }; - template typename Pred> - using type_list_sort_t = typename type_list_sort::type; + template typename Pred> + using type_list_sort_t = typename type_list_sort::type; } // namespace mp \ No newline at end of file diff --git a/test/test_type_list.cpp b/test/test_type_list.cpp index cb14cbc4..abdaf9ed 100644 --- a/test/test_type_list.cpp +++ b/test/test_type_list.cpp @@ -28,6 +28,9 @@ namespace { using namespace mp; using namespace units; + template + struct type_list; + // type_list_push_front static_assert(std::is_same_v, int>, type_list>); @@ -95,8 +98,8 @@ namespace { // type_list_sort - template - using dim_sort_t = type_list_sort_t; + template + using dim_sort_t = type_list_sort_t; static_assert(std::is_same_v>>, type_list>>); static_assert(std::is_same_v, dim_id<1>>>, type_list, dim_id<1>>>); @@ -110,8 +113,8 @@ namespace { template using e = exp, Value>; - template - using exp_sort_t = type_list_sort_t; + template + using exp_sort_t = type_list_sort_t; static_assert(std::is_same_v>>, dimension>>); static_assert(std::is_same_v, e<1, -1>>>, dimension, e<1, -1>>>);