From af5662781840ac45c21cbf14cbc7973bcf39d1da Mon Sep 17 00:00:00 2001 From: Adrian Kummerlaender Date: Tue, 17 Feb 2015 22:47:56 +0100 Subject: Reimplemented `TakeWhile` and `DropWhile` in terms of `ListIndex` * new higher order list operation `ListIndex` returns the index of the first list element satisfying a given predicate function ** this enables finding the split index required for both `TakeWhile` and `DropWhile` ** actual list manipulation is then performed using lower order `Take` or `Drop` ** analogously to `list-index` in SRFI-1 ** added appropriate test case * added higher order predicate utility function `utility::predicate_assurance` ** as its name implies this function enables assuring that a value satisfies a given predicate *** if this is not the case a surrogate value is returned instead ** this is used to return a appropriate size value if the `ListIndex` call fails because no element satisfies its predicate ** `detail::predicate_negator` was moved from `Remove`'s implementation details into the newly introduced `utility` namespace *** as it is required by both `TakeWhile` and `DropWhile` in addition to `Remove` * continuation of 8e49cc6 --- src/list/operation/higher/drop_while.h | 42 ++++++++++++------------------- src/list/operation/higher/list_index.h | 45 ++++++++++++++++++++++++++++++++++ src/list/operation/higher/remove.h | 13 ++-------- src/list/operation/higher/take_while.h | 45 ++++++++++++---------------------- src/utility/predicate.h | 34 +++++++++++++++++++++++++ test.cc | 25 +++++++++++++++++++ 6 files changed, 138 insertions(+), 66 deletions(-) create mode 100644 src/list/operation/higher/list_index.h create mode 100644 src/utility/predicate.h diff --git a/src/list/operation/higher/drop_while.h b/src/list/operation/higher/drop_while.h index 021698f..1207020 100644 --- a/src/list/operation/higher/drop_while.h +++ b/src/list/operation/higher/drop_while.h @@ -1,38 +1,28 @@ #ifndef TYPEASVALUE_SRC_LIST_OPERATION_HIGHER_DROP_WHILE_H_ #define TYPEASVALUE_SRC_LIST_OPERATION_HIGHER_DROP_WHILE_H_ -#include "conditional/if.h" +#include "list_index.h" +#include "list/operation/drop.h" +#include "utility/predicate.h" namespace tav { -namespace detail { - -template < - template class Predicate, - typename Current -> -struct DropWhile { - typedef If< - Eval>>, - Eval>>, - Current - > type; -}; - -template < - template class Predicate -> -struct DropWhile { - typedef void type; -}; - -} - template < template class Predicate, - typename Current + typename List > -using DropWhile = Eval>; +using DropWhile = Drop< + typename utility::predicate_assurance< + utility::predicate_negator::template function, + Length + >::template assure< + ListIndex< + utility::predicate_negator::template function, + List + > + >, + List +>; } diff --git a/src/list/operation/higher/list_index.h b/src/list/operation/higher/list_index.h new file mode 100644 index 0000000..bbf43ee --- /dev/null +++ b/src/list/operation/higher/list_index.h @@ -0,0 +1,45 @@ +#ifndef TYPEASVALUE_SRC_LIST_OPERATION_HIGHER_LIST_INDEX_H_ +#define TYPEASVALUE_SRC_LIST_OPERATION_HIGHER_LIST_INDEX_H_ + +#include "operation/math.h" + +namespace tav { + +namespace detail { + +template < + template class Predicate, + typename List, + typename Index = Size<0> +> +struct index_of_first { + typedef If< + Eval>>, + Index, + Eval, + Add> + >> + > type; +}; + +template < + template class Predicate, + typename Index +> +struct index_of_first { + typedef void type; +}; + +} + +template < + template class Predicate, + typename List +> +using ListIndex = Eval>; + +} + +#endif // TYPEASVALUE_SRC_LIST_OPERATION_HIGHER_LIST_INDEX_H_ diff --git a/src/list/operation/higher/remove.h b/src/list/operation/higher/remove.h index 598f5a6..6770359 100644 --- a/src/list/operation/higher/remove.h +++ b/src/list/operation/higher/remove.h @@ -2,25 +2,16 @@ #define TYPEASVALUE_SRC_LIST_OPERATION_HIGHER_REMOVE_H_ #include "filter.h" +#include "utility/predicate.h" namespace tav { -namespace detail { - -template class Predicate> -struct predicate_negator { - template - using function = Not>; -}; - -} - template < template class Predicate, typename List > using Remove = Filter< - detail::predicate_negator::template function, + utility::predicate_negator::template function, List >; diff --git a/src/list/operation/higher/take_while.h b/src/list/operation/higher/take_while.h index dec5551..f9fc3aa 100644 --- a/src/list/operation/higher/take_while.h +++ b/src/list/operation/higher/take_while.h @@ -1,41 +1,28 @@ #ifndef TYPEASVALUE_SRC_LIST_OPERATION_HIGHER_TAKE_WHILE_H_ #define TYPEASVALUE_SRC_LIST_OPERATION_HIGHER_TAKE_WHILE_H_ -#include "conditional/if.h" +#include "list_index.h" +#include "list/operation/take.h" +#include "utility/predicate.h" namespace tav { -namespace detail { - -template < - template class Predicate, - typename Current -> -struct TakeWhile { - typedef If< - Eval>>, - Cons< - Head, - Eval>> - >, - void - > type; -}; - -template < - template class Predicate -> -struct TakeWhile { - typedef void type; -}; - -} - template < template class Predicate, - typename Current + typename List > -using TakeWhile = Eval>; +using TakeWhile = Take< + typename utility::predicate_assurance< + utility::predicate_negator::template function, + Length + >::template assure< + ListIndex< + utility::predicate_negator::template function, + List + > + >, + List +>; } diff --git a/src/utility/predicate.h b/src/utility/predicate.h new file mode 100644 index 0000000..f06fcee --- /dev/null +++ b/src/utility/predicate.h @@ -0,0 +1,34 @@ +#ifndef TYPEASVALUE_SRC_UTILITY_PREDICATE_H_ +#define TYPEASVALUE_SRC_UTILITY_PREDICATE_H_ + +#include "conditional/if.h" +#include "operation/logic.h" + +namespace tav { + +namespace utility { + +template < + template class Predicate, + typename Surrogate +> +struct predicate_assurance { + template + using assure = If< + Eval>, + Value, + Surrogate + >; +}; + +template class Predicate> +struct predicate_negator { + template + using function = Not>; +}; + +} + +} + +#endif // TYPEASVALUE_SRC_UTILITY_PREDICATE_H_ diff --git a/test.cc b/test.cc index 2f95561..6a957b1 100644 --- a/test.cc +++ b/test.cc @@ -12,6 +12,7 @@ #include "list/operation/higher/remove.h" #include "list/operation/higher/partition.h" #include "list/operation/higher/query.h" +#include "list/operation/higher/list_index.h" #include "list/operation/higher/find.h" #include "list/operation/higher/take_while.h" #include "list/operation/higher/drop_while.h" @@ -906,6 +907,30 @@ static_assert( "(count even? (list 1 3 5)) != 0" ); +// list index + +static_assert( + std::is_same< + tav::Size<2>, + tav::ListIndex< + tav::Even, + tav::List, tav::Int<3>, tav::Int<4>, tav::Int<6>> + > + >::value, + "(list-index even? (list 1 3 4 6)) != 2" +); + +static_assert( + std::is_same< + void, + tav::ListIndex< + tav::Even, + tav::List, tav::Int<3>, tav::Int<5>> + > + >::value, + "(list-index even? (list 1 3 5)) != void" +); + // list find static_assert( -- cgit v1.2.3