aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAdrian Kummerlaender2015-02-19 13:45:07 +0100
committerAdrian Kummerlaender2015-02-19 13:45:07 +0100
commita8bec66b05eece7b8a39102fb1b0ba8f778eb9fe (patch)
treec1a6c4b1c6cba5ba1d87499a220f76eab59673c3
parent9bb66d9c1d8a7bf5bf02f5a7c400894e928d2a95 (diff)
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`
-rw-r--r--src/conditional/cond.h14
-rw-r--r--src/list/detail/find_variadic.h40
-rw-r--r--src/list/detail/fold_variadic.h6
-rw-r--r--src/list/operation/higher/find.h38
-rw-r--r--src/utility/predicate.h22
5 files changed, 81 insertions, 39 deletions
diff --git a/src/conditional/cond.h b/src/conditional/cond.h
index eeb31b6..8a34f79 100644
--- a/src/conditional/cond.h
+++ b/src/conditional/cond.h
@@ -1,10 +1,7 @@
#ifndef TYPEASVALUE_SRC_CONDITIONAL_COND_H_
#define TYPEASVALUE_SRC_CONDITIONAL_COND_H_
-#include <type_traits>
-
-#include "list/list.h"
-#include "list/operation/higher/find.h"
+#include "list/detail/find_variadic.h"
namespace tav {
@@ -16,12 +13,9 @@ using cond_predicate = IsTrue<Car<Pair>>;
}
template <typename... Branches>
-using Cond = Cdr<
- Find<
- detail::cond_predicate,
- List<Branches...>
- >
->;
+using Cond = Cdr<Eval<
+ detail::find_variadic<detail::cond_predicate, Branches...>
+>>;
}
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<typename> class Predicate,
+ typename Head,
+ typename... Tail
+>
+struct find_variadic {
+ typedef If<
+ Eval<Predicate<Head>>,
+ Head,
+ Eval<find_variadic<Predicate, Tail...>>
+ > type;
+};
+
+template <
+ template<typename> class Predicate,
+ typename Last
+>
+struct find_variadic<Predicate, Last> {
+ typedef If<
+ Eval<Predicate<Last>>,
+ 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 <typename, typename> class Function,
- typename Head
+ typename Last
>
-struct fold_variadic<Function, Head> {
- typedef Function<Head, void> type;
+struct fold_variadic<Function, Last> {
+ typedef Function<Last, void> 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<typename> class Predicate,
- typename Current
->
-struct Find {
- typedef If<
- Eval<Predicate<Head<Current>>>,
- Head<Current>,
- Eval<Find<Predicate, Tail<Current>>>
- > type;
-};
-
-template <
- template<typename> class Predicate
->
-struct Find<Predicate, void> {
- typedef Boolean<false> type;
-};
-
-}
-
template <
template<typename> class Predicate,
typename List
>
-using Find = Eval<detail::Find<Predicate, List>>;
+using Find = typename utility::predicate_guard<
+ IsSize,
+ Apply<Nth, _0, List>::template function,
+ Boolean<false>
+>::template check<
+ ListIndex<Predicate, List>
+>;
+
}
diff --git a/src/utility/predicate.h b/src/utility/predicate.h
index f06fcee..e84b18f 100644
--- a/src/utility/predicate.h
+++ b/src/utility/predicate.h
@@ -21,6 +21,28 @@ struct predicate_assurance {
>;
};
+template <
+ template<typename...> class Function,
+ typename... Arguments
+>
+struct defer_eval {
+ typedef Function<Arguments...> type;
+};
+
+template <
+ template<typename> class Predicate,
+ template<typename> class Charge,
+ typename Surrogate
+>
+struct predicate_guard {
+ template <typename Value>
+ using check = Eval<If<
+ Eval<Predicate<Value>>,
+ defer_eval<Charge, Value>,
+ Surrogate
+ >>;
+};
+
template <template<typename> class Predicate>
struct predicate_negator {
template <typename Element>