diff options
author | Adrian Kummerlaender | 2015-02-16 14:03:53 +0100 |
---|---|---|
committer | Adrian Kummerlaender | 2015-02-16 14:03:53 +0100 |
commit | 324988569183e38e9c5e42318571693a6fcd9569 (patch) | |
tree | ca47de290dc9b6ee1d1f771fb473c1f604e67189 | |
parent | a59df7e8c4fd1f88bc1078ebcfde944502b0c309 (diff) | |
download | TypeAsValue-324988569183e38e9c5e42318571693a6fcd9569.tar TypeAsValue-324988569183e38e9c5e42318571693a6fcd9569.tar.gz TypeAsValue-324988569183e38e9c5e42318571693a6fcd9569.tar.bz2 TypeAsValue-324988569183e38e9c5e42318571693a6fcd9569.tar.lz TypeAsValue-324988569183e38e9c5e42318571693a6fcd9569.tar.xz TypeAsValue-324988569183e38e9c5e42318571693a6fcd9569.tar.zst TypeAsValue-324988569183e38e9c5e42318571693a6fcd9569.zip |
Simplified `List`, `Length` and `Reverse` implementations
* continuation of 8e49cc6
* list constructor was generalized to a _variadic fold_
-rw-r--r-- | src/function/detail/apply.h | 7 | ||||
-rw-r--r-- | src/list/detail/fold_variadic.h | 34 | ||||
-rw-r--r-- | src/list/list.h | 38 | ||||
-rw-r--r-- | src/list/operation/basic.h | 14 | ||||
-rw-r--r-- | src/list/operation/higher/sort.h | 4 | ||||
-rw-r--r-- | src/list/operation/reverse.h | 23 | ||||
-rw-r--r-- | test.cc | 8 |
7 files changed, 49 insertions, 79 deletions
diff --git a/src/function/detail/apply.h b/src/function/detail/apply.h index 8d4ef6d..3df80ef 100644 --- a/src/function/detail/apply.h +++ b/src/function/detail/apply.h @@ -37,10 +37,7 @@ struct resolve_placeholder<Partials, placeholder<Index>> { }; template <typename... Arguments> -using count_placeholders = Count< - is_placeholder, - tav::List<Arguments...> ->; +using count_placeholders = Count<is_placeholder, List<Arguments...>>; template < template<typename...> class Function, @@ -58,7 +55,7 @@ struct apply_variadic { template <typename... Partials> using function = Function< Eval<resolve_placeholder< - tav::List<Partials...>, + List<Partials...>, Arguments >>... >; diff --git a/src/list/detail/fold_variadic.h b/src/list/detail/fold_variadic.h new file mode 100644 index 0000000..e02b604 --- /dev/null +++ b/src/list/detail/fold_variadic.h @@ -0,0 +1,34 @@ +#ifndef TYPEASVALUE_SRC_LIST_DETAIL_FOLD_VARIADIC_H_ +#define TYPEASVALUE_SRC_LIST_DETAIL_FOLD_VARIADIC_H_ + +#include "type.h" + +namespace tav { + +namespace detail { + +template < + template <typename, typename> class Function, + typename Head, + typename... Tail +> +struct fold_variadic { + typedef Function< + Head, + Eval<fold_variadic<Function, Tail...>> + > type; +}; + +template < + template <typename, typename> class Function, + typename Head +> +struct fold_variadic<Function, Head> { + typedef Function<Head, void> type; +}; + +} + +} + +#endif // TYPEASVALUE_SRC_LIST_DETAIL_FOLD_VARIADIC_H_ diff --git a/src/list/list.h b/src/list/list.h index c9e6a9e..1a4c260 100644 --- a/src/list/list.h +++ b/src/list/list.h @@ -2,46 +2,12 @@ #define TYPEASVALUE_SRC_LIST_LIST_H_ #include "cons.h" +#include "detail/fold_variadic.h" namespace tav { -namespace detail { - -template < - typename Head, - typename... Tail -> -struct List { - typedef Eval<Cons< - Head, - Eval<List<Tail...>> - >> type; -}; - -template <typename Head> -struct List<Head> { - typedef Eval<Cons<Head, void>> type; -}; - -template <typename Head> -struct List<Head, void> { - typedef Eval<List<Head>> type; -}; - -template <typename... Tail> -struct List<void, Tail...> { - typedef Eval<List<Tail...>> type; -}; - -template <> -struct List<void, void> { - typedef void type; -}; - -} - template <typename... Elements> -using List = Eval<detail::List<Elements...>>; +using List = Eval<detail::fold_variadic<Pair, Elements...>>; template < typename Type, diff --git a/src/list/operation/basic.h b/src/list/operation/basic.h index a299938..d0f6212 100644 --- a/src/list/operation/basic.h +++ b/src/list/operation/basic.h @@ -8,21 +8,13 @@ namespace tav { namespace detail { -template <typename List> -class Length { - private: - template <typename, typename Accumulated> - using accumulate = Add<Size<1>, Accumulated>; - - public: - typedef tav::Fold<accumulate, Size<0>, List> type; - -}; +template <typename, typename Accumulated> +using length_accumulate = Add<Size<1>, Accumulated>; } template <typename List> -using Length = Eval<detail::Length<List>>; +using Length = Fold<detail::length_accumulate, Size<0>, List>; } diff --git a/src/list/operation/higher/sort.h b/src/list/operation/higher/sort.h index 2976e06..26d99e6 100644 --- a/src/list/operation/higher/sort.h +++ b/src/list/operation/higher/sort.h @@ -29,9 +29,9 @@ class Sort { public: using type = Concatenate< - tav::List< + List< Eval<Sort<Comparator, lhs>>, - tav::List<pivot>, + List<pivot>, Eval<Sort<Comparator, rhs>> > >; diff --git a/src/list/operation/reverse.h b/src/list/operation/reverse.h index aabba92..6d9237d 100644 --- a/src/list/operation/reverse.h +++ b/src/list/operation/reverse.h @@ -8,27 +8,16 @@ namespace tav { namespace detail { -template <typename Sequence> -class Reverse { - private: - template < - typename Current, - typename Previous - > - using reversed_append = tav::Append< - Previous, - tav::List<Current> - >; - - public: - typedef tav::Fold<reversed_append, void, Sequence> type; - -}; +template < + typename Current, + typename Previous +> +using reversed_append = tav::Append<Previous, List<Current>>; } template <typename Sequence> -using Reverse = Eval<detail::Reverse<Sequence>>; +using Reverse = Fold<detail::reversed_append, void, Sequence>; } @@ -323,14 +323,6 @@ static_assert( static_assert( std::is_same< - tav::Pair<tav::Int<1>, tav::Pair<tav::Int<2>, void>>, - tav::List<void, tav::Int<1>, void, tav::Int<2>, void> - >::value, - "(list void 1 void 2 void) != '(1 . 2)" -); - -static_assert( - std::is_same< tav::Int<1>, tav::Head< tav::List<tav::Int<1>, tav::Int<2>, tav::Int<3>> |