diff options
author | Adrian Kummerlaender | 2015-02-12 10:16:54 +0100 |
---|---|---|
committer | Adrian Kummerlaender | 2015-02-12 10:16:54 +0100 |
commit | ad27a79a2e6bc380e68ec77ae961917a9fb402d3 (patch) | |
tree | 7b6f43b9973d9ccae40adeb16f1184d8ea8d8e5b | |
parent | f1268ca3bf10ab136bafbd63894ce12353fa8690 (diff) | |
download | TypeAsValue-ad27a79a2e6bc380e68ec77ae961917a9fb402d3.tar TypeAsValue-ad27a79a2e6bc380e68ec77ae961917a9fb402d3.tar.gz TypeAsValue-ad27a79a2e6bc380e68ec77ae961917a9fb402d3.tar.bz2 TypeAsValue-ad27a79a2e6bc380e68ec77ae961917a9fb402d3.tar.lz TypeAsValue-ad27a79a2e6bc380e68ec77ae961917a9fb402d3.tar.xz TypeAsValue-ad27a79a2e6bc380e68ec77ae961917a9fb402d3.tar.zst TypeAsValue-ad27a79a2e6bc380e68ec77ae961917a9fb402d3.zip |
Revamped partial function application
* moved internals into separate header i.e. the `detail` namespace relating to `Apply`
* implemented automatic alias selection by implementing aliae of the basic variadic `type` template alias in different base classes
** variadic partial application is implemented in `detail::apply_variadic`
*** `detail::apply_single` and `detail::apply_pair` define aliae to `detail::apply_variadic`'s `type` template alias
*** both restricted aliae derive from `detail::apply_variadic`
** `Apply` derives from any of the aliae defining base classes depending on the count of placeholders as determined by `detail::count_placeholders`
*** `Apply` is guaranteed to always be derived from `detail::apply_variadic` one way or the other
* changed functions, test cases and examples depending on `Apply` accordingly
** `Length` had to be reimplemented without `Apply` as it doesn't allow usage of aliae expecting a different count of arguments anymore
*** this is a advantage in the sense that core functionality of _TypeAsValue_ now doesn't depend on this complex partial application implementation anymore
*** such functionality may be reimplemented separately from `Apply`
* removed unnecessary `tav` namespace prefixes
-rw-r--r-- | example/prime/prime.cc | 3 | ||||
-rw-r--r-- | src/function/apply.h | 62 | ||||
-rw-r--r-- | src/function/detail/apply.h | 86 | ||||
-rw-r--r-- | src/list/operation/basic.h | 21 | ||||
-rw-r--r-- | src/list/operation/contains.h | 2 | ||||
-rw-r--r-- | src/list/operation/delete.h | 2 | ||||
-rw-r--r-- | src/list/operation/higher/sort.h | 2 | ||||
-rw-r--r-- | test.cc | 4 |
8 files changed, 118 insertions, 64 deletions
diff --git a/example/prime/prime.cc b/example/prime/prime.cc index f1771a6..3d5cced 100644 --- a/example/prime/prime.cc +++ b/example/prime/prime.cc @@ -2,6 +2,7 @@ #include "type.h" #include "operation/math.h" +#include "function/apply.h" #include "list/list.h" #include "list/operation/higher/filter.h" @@ -30,7 +31,7 @@ template < typename Base > using removeMultiplesOf = tav::Remove< - tav::Apply<isMultipleOf, tav::_0, Base>::template single_type, + tav::Apply<isMultipleOf, tav::_0, Base>::template type, Candidates >; diff --git a/src/function/apply.h b/src/function/apply.h index 8e780e0..21364a8 100644 --- a/src/function/apply.h +++ b/src/function/apply.h @@ -1,43 +1,12 @@ #ifndef TYPEASVALUE_SRC_FUNCTION_APPLY_H_ #define TYPEASVALUE_SRC_FUNCTION_APPLY_H_ -#include <type_traits> +#include "conditional/if.h" -#include "list/list.h" -#include "list/operation/nth.h" +#include "detail/apply.h" namespace tav { -namespace detail { - -struct placeholder_tag { }; - -template <typename Type> -using is_placeholder = tav::Boolean< - std::is_base_of<placeholder_tag, Type>::value ->; - -template <int Index> -struct placeholder : placeholder_tag { }; - -template < - typename Partials, - typename Argument -> -struct resolve_placeholder { - typedef Argument type; -}; - -template < - typename Partials, - int Index -> -struct resolve_placeholder<Partials, placeholder<Index>> { - typedef typename Nth<Size<Index>, Partials>::type type; -}; - -} - typedef detail::placeholder<0> _0; typedef detail::placeholder<1> _1; typedef detail::placeholder<2> _2; @@ -47,24 +16,15 @@ template < template<typename...> class Function, typename... Arguments > -struct Apply { - template <typename... Partials> - using variadic_type = Function< - typename detail::resolve_placeholder< - typename tav::List<Partials...>::type, - Arguments - >::type... - >; - - template <typename Partial> - using single_type = variadic_type<Partial>; - - template <typename Partial0, typename Partial1> - using pair_type = variadic_type<Partial0, Partial1>; - - template <typename Partial0, typename Partial1, typename Partial2> - using triple_type = variadic_type<Partial0, Partial1, Partial2>; -}; +struct Apply : If< + (detail::count_placeholders<Arguments...>::type::value > 2), + detail::apply_variadic<Function, Arguments...>, + typename If< + detail::count_placeholders<Arguments...>::type::value == 2, + detail::apply_pair<Function, Arguments...>, + detail::apply_single<Function, Arguments...> + >::type +>::type { }; } diff --git a/src/function/detail/apply.h b/src/function/detail/apply.h new file mode 100644 index 0000000..bf60be1 --- /dev/null +++ b/src/function/detail/apply.h @@ -0,0 +1,86 @@ +#ifndef TYPEASVALUE_SRC_FUNCTION_DETAIL_APPLY_H_ +#define TYPEASVALUE_SRC_FUNCTION_DETAIL_APPLY_H_ + +#include <type_traits> + +#include "list/list.h" +#include "list/operation/nth.h" +#include "list/operation/higher/query.h" + +namespace tav { +namespace detail { + +struct placeholder_tag { }; + +template <typename Type> +using is_placeholder = tav::Boolean< + std::is_base_of<placeholder_tag, Type>::value +>; + +template <int Index> +struct placeholder : placeholder_tag { }; + +template < + typename Partials, + typename Argument +> +struct resolve_placeholder { + typedef Argument type; +}; + +template < + typename Partials, + int Index +> +struct resolve_placeholder<Partials, placeholder<Index>> { + typedef typename Nth<Size<Index>, Partials>::type type; +}; + +template <typename... Arguments> +using count_placeholders = Count< + is_placeholder, + typename List<Arguments...>::type +>; + +template < + template<typename...> class Function, + typename... Arguments +> +struct apply_variadic { + template <typename... Partials> + using type = Function< + typename resolve_placeholder< + typename tav::List<Partials...>::type, + Arguments + >::type... + >; +}; + +template < + template<typename...> class Function, + typename... Arguments +> +struct apply_single : apply_variadic<Function, Arguments...> { + template <typename Partial0> + using type = typename apply_variadic< + Function, + Arguments... + >::template type<Partial0>; +}; + +template < + template<typename...> class Function, + typename... Arguments +> +struct apply_pair : apply_variadic<Function, Arguments...> { + template <typename Partial0, typename Partial1> + using type = typename apply_variadic< + Function, + Arguments... + >::template type<Partial0, Partial1>; +}; + +} +} + +#endif // TYPEASVALUE_SRC_FUNCTION_DETAIL_APPLY_H_ diff --git a/src/list/operation/basic.h b/src/list/operation/basic.h index cbbe22c..b4aa35a 100644 --- a/src/list/operation/basic.h +++ b/src/list/operation/basic.h @@ -2,17 +2,24 @@ #define TYPEASVALUE_SRC_LIST_OPERATION_BASIC_H_ #include "higher/fold.h" -#include "function/apply.h" #include "operation/math.h" namespace tav { -template <typename Pair> -using Length = Fold< - Apply<Add, Size<1>, _1>::pair_type, - Size<0>, - Pair ->; +template <typename Cons> +class Length { + private: + template <typename, typename Accumulated> + using accumulate = Add<Size<1>, Accumulated>; + + public: + typedef typename Fold< + accumulate, + Size<0>, + Cons + >::type type; + +}; } diff --git a/src/list/operation/contains.h b/src/list/operation/contains.h index 51d7d9f..458acc0 100644 --- a/src/list/operation/contains.h +++ b/src/list/operation/contains.h @@ -12,7 +12,7 @@ template < typename List > using Contains = Any< - Apply<EqualValue, tav::_0, Element>::template single_type, + Apply<EqualValue, _0, Element>::template type, List >; diff --git a/src/list/operation/delete.h b/src/list/operation/delete.h index 2ff674c..a524667 100644 --- a/src/list/operation/delete.h +++ b/src/list/operation/delete.h @@ -12,7 +12,7 @@ template < typename List > using Delete = Remove< - Apply<EqualValue, tav::_0, Element>::template single_type, + Apply<EqualValue, _0, Element>::template type, List >; diff --git a/src/list/operation/higher/sort.h b/src/list/operation/higher/sort.h index f9146d6..5bc30c6 100644 --- a/src/list/operation/higher/sort.h +++ b/src/list/operation/higher/sort.h @@ -18,7 +18,7 @@ class Sort { using pivot = typename Nth<index, Sequence>::type; using partitions = typename Partition< - Apply<Comparator, pivot, tav::_0>::template single_type, + Apply<Comparator, pivot, _0>::template type, typename DeleteNth<index, Sequence>::type >::type; @@ -999,7 +999,7 @@ static_assert( tav::Multiply, tav::Int<21>, tav::_0 - >::single_type< + >::type< tav::Int<2> >::type >::value, @@ -1014,7 +1014,7 @@ static_assert( tav::Add, tav::_0, tav::Int<10> - >::single_type, + >::type, tav::List<tav::Int<0>, tav::Int<2>, tav::Int<4>>::type >::type >::value, |