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/list_index.h | 45 ++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 src/list/operation/higher/list_index.h (limited to 'src/list/operation/higher/list_index.h') 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_ -- cgit v1.2.3