From a8bec66b05eece7b8a39102fb1b0ba8f778eb9fe Mon Sep 17 00:00:00 2001 From: Adrian Kummerlaender Date: Thu, 19 Feb 2015 13:45:07 +0100 Subject: Reimplemented `Find` in terms of `ListIndex` * `ListIndex` already implements the necessary partial template specializations for finding elements matching a predicate * reimplemented `Cond` using `detail::find_variadic` as `Find` depends on `Apply` which in turn depends on `Cond` ** it is useful if core functionality such as the branched conditional `Cond` do not require higher order functionality such as `Find` *** otherwise one can not use core functionality in the implementation of higher order functionality * introduced `utility::predicate_guard` helper template ** checks a given value using a predicate and only forwards the value to the guarded function if this check is successful ** if check is unsuccessful it returns a surrogate value simmilar to `utility::predicate_assurance` --- src/list/detail/find_variadic.h | 40 ++++++++++++++++++++++++++++++++++++++++ src/list/detail/fold_variadic.h | 6 +++--- src/list/operation/higher/find.h | 38 ++++++++++++-------------------------- 3 files changed, 55 insertions(+), 29 deletions(-) create mode 100644 src/list/detail/find_variadic.h (limited to 'src/list') diff --git a/src/list/detail/find_variadic.h b/src/list/detail/find_variadic.h new file mode 100644 index 0000000..824f385 --- /dev/null +++ b/src/list/detail/find_variadic.h @@ -0,0 +1,40 @@ +#ifndef TYPEASVALUE_SRC_LIST_DETAIL_FIND_VARIADIC_H_ +#define TYPEASVALUE_SRC_LIST_DETAIL_FIND_VARIADIC_H_ + +#include "type.h" +#include "conditional/if.h" + +namespace tav { + +namespace detail { + +template < + template class Predicate, + typename Head, + typename... Tail +> +struct find_variadic { + typedef If< + Eval>, + Head, + Eval> + > type; +}; + +template < + template class Predicate, + typename Last +> +struct find_variadic { + typedef If< + Eval>, + Last, + void + > type; +}; + +} + +} + +#endif // TYPEASVALUE_SRC_LIST_DETAIL_FIND_VARIADIC_H_ diff --git a/src/list/detail/fold_variadic.h b/src/list/detail/fold_variadic.h index e02b604..2b4fdd8 100644 --- a/src/list/detail/fold_variadic.h +++ b/src/list/detail/fold_variadic.h @@ -21,10 +21,10 @@ struct fold_variadic { template < template class Function, - typename Head + typename Last > -struct fold_variadic { - typedef Function type; +struct fold_variadic { + typedef Function type; }; } diff --git a/src/list/operation/higher/find.h b/src/list/operation/higher/find.h index 48533b7..aeb0c2a 100644 --- a/src/list/operation/higher/find.h +++ b/src/list/operation/higher/find.h @@ -1,39 +1,25 @@ #ifndef TYPEASVALUE_SRC_LIST_OPERATION_HIGHER_FIND_H_ #define TYPEASVALUE_SRC_LIST_OPERATION_HIGHER_FIND_H_ -#include "type.h" -#include "conditional/if.h" +#include "utility/predicate.h" +#include "function/apply.h" +#include "list/operation/nth.h" +#include "list_index.h" namespace tav { -namespace detail { - -template < - template class Predicate, - typename Current -> -struct Find { - typedef If< - Eval>>, - Head, - Eval>> - > type; -}; - -template < - template class Predicate -> -struct Find { - typedef Boolean type; -}; - -} - template < template class Predicate, typename List > -using Find = Eval>; +using Find = typename utility::predicate_guard< + IsSize, + Apply::template function, + Boolean +>::template check< + ListIndex +>; + } -- cgit v1.2.3