From 8e4e3466694e5bef43f1308296a76086a7db453d Mon Sep 17 00:00:00 2001 From: Adrian Kummerlaender Date: Tue, 17 Feb 2015 18:55:08 +0100 Subject: Expressed `Take` and `Drop` in terms of new `Section` operation * unifies the common functionality between `Take` and `Drop` * renamed `basic.h` to `length.h` as it only contains the `Length` implementation --- src/list/generator/higher/list_tabulate.h | 2 +- src/list/list.h | 2 +- src/list/operation/basic.h | 21 --------- src/list/operation/drop.h | 41 ++++------------- src/list/operation/higher/fold.h | 10 ++-- src/list/operation/length.h | 21 +++++++++ src/list/operation/section.h | 76 +++++++++++++++++++++++++++++++ src/list/operation/take.h | 43 +++-------------- 8 files changed, 119 insertions(+), 97 deletions(-) delete mode 100644 src/list/operation/basic.h create mode 100644 src/list/operation/length.h create mode 100644 src/list/operation/section.h (limited to 'src') diff --git a/src/list/generator/higher/list_tabulate.h b/src/list/generator/higher/list_tabulate.h index 856ed11..4ffbc02 100644 --- a/src/list/generator/higher/list_tabulate.h +++ b/src/list/generator/higher/list_tabulate.h @@ -12,7 +12,7 @@ template < > using ListTabulate = Map< Initializer, - Eval, Size<1>>> + Iota, Size<1>> >; } diff --git a/src/list/list.h b/src/list/list.h index edb52ec..cf4f415 100644 --- a/src/list/list.h +++ b/src/list/list.h @@ -26,7 +26,7 @@ using Tail = Cdr; } -#include "operation/basic.h" +#include "operation/length.h" #include "operation/nth.h" #include "operation/take.h" #include "operation/drop.h" diff --git a/src/list/operation/basic.h b/src/list/operation/basic.h deleted file mode 100644 index d0f6212..0000000 --- a/src/list/operation/basic.h +++ /dev/null @@ -1,21 +0,0 @@ -#ifndef TYPEASVALUE_SRC_LIST_OPERATION_BASIC_H_ -#define TYPEASVALUE_SRC_LIST_OPERATION_BASIC_H_ - -#include "higher/fold.h" -#include "operation/math.h" - -namespace tav { - -namespace detail { - -template -using length_accumulate = Add, Accumulated>; - -} - -template -using Length = Fold, List>; - -} - -#endif // TYPEASVALUE_SRC_LIST_OPERATION_BASIC_H_ diff --git a/src/list/operation/drop.h b/src/list/operation/drop.h index c7745d7..aef88d3 100644 --- a/src/list/operation/drop.h +++ b/src/list/operation/drop.h @@ -1,45 +1,20 @@ #ifndef TYPEASVALUE_SRC_LIST_OPERATION_DROP_H_ #define TYPEASVALUE_SRC_LIST_OPERATION_DROP_H_ -#include "operation/math.h" +#include "length.h" +#include "section.h" namespace tav { -namespace detail { - -template < - typename Count, - typename Current -> -struct Drop { - typedef Eval>, - Tail - >> type; -}; - -template -struct Drop, Current> { - typedef Current type; -}; - -template -struct Drop { - typedef void type; -}; - -template <> -struct Drop, void> { - typedef void type; -}; - -} - template < typename Count, - typename Current + typename List > -using Drop = Eval>; +using Drop = Section< + Count, + Substract, Size<1>>, + List +>; } diff --git a/src/list/operation/higher/fold.h b/src/list/operation/higher/fold.h index 3f39b40..38c3554 100644 --- a/src/list/operation/higher/fold.h +++ b/src/list/operation/higher/fold.h @@ -8,12 +8,12 @@ namespace detail { template < template class Function, typename Initial, - typename Current + typename Pair > struct fold_pair { typedef Function< - Head, - Eval>> + Car, + Eval>> > type; }; @@ -30,9 +30,9 @@ struct fold_pair { template < template class Function, typename Initial, - typename Current + typename List > -using Fold = Eval>; +using Fold = Eval>; } diff --git a/src/list/operation/length.h b/src/list/operation/length.h new file mode 100644 index 0000000..1f609d4 --- /dev/null +++ b/src/list/operation/length.h @@ -0,0 +1,21 @@ +#ifndef TYPEASVALUE_SRC_LIST_OPERATION_LENGTH_H_ +#define TYPEASVALUE_SRC_LIST_OPERATION_LENGTH_H_ + +#include "higher/fold.h" +#include "operation/math.h" + +namespace tav { + +namespace detail { + +template +using length_accumulate = Add, Accumulated>; + +} + +template +using Length = Fold, List>; + +} + +#endif // TYPEASVALUE_SRC_LIST_OPERATION_LENGTH_H_ diff --git a/src/list/operation/section.h b/src/list/operation/section.h new file mode 100644 index 0000000..214c9fd --- /dev/null +++ b/src/list/operation/section.h @@ -0,0 +1,76 @@ +#ifndef TYPEASVALUE_SRC_LIST_OPERATION_SECTION_H_ +#define TYPEASVALUE_SRC_LIST_OPERATION_SECTION_H_ + +#include "operation/math.h" + +namespace tav { + +namespace detail { + +template < + typename Index, + typename Count, + typename List +> +struct take_count_at { + typedef Eval>, + Count, + Tail + >> type; +}; + +template < + typename Count, + typename List +> +struct take_count_at, Count, List> { + typedef Cons< + Head, + Eval, + Substract>, + Tail + >> + > type; +}; + +template < + typename Index, + typename Count +> +struct take_count_at { + typedef void type; +}; + +template +struct take_count_at, Count, void> { + typedef void type; +}; + +template +struct take_count_at, Size<0>, List> { + typedef void type; +}; + +template <> +struct take_count_at, Size<0>, void> { + typedef void type; +}; + +} + +template < + typename Start, + typename End, + typename List +> +using Section = Eval>, Start>, + List +>>; + +} + +#endif // TYPEASVALUE_SRC_LIST_OPERATION_SECTION_H_ diff --git a/src/list/operation/take.h b/src/list/operation/take.h index effba33..f9aa4b0 100644 --- a/src/list/operation/take.h +++ b/src/list/operation/take.h @@ -1,48 +1,19 @@ #ifndef TYPEASVALUE_SRC_LIST_OPERATION_TAKE_H_ #define TYPEASVALUE_SRC_LIST_OPERATION_TAKE_H_ -#include "operation/math.h" +#include "section.h" namespace tav { -namespace detail { - -template < - typename Count, - typename Current -> -struct Take { - typedef Cons< - Head, - Eval>, - Tail - >> - > type; -}; - -template -struct Take, Current> { - typedef void type; -}; - -template -struct Take { - typedef void type; -}; - -template <> -struct Take, void> { - typedef void type; -}; - -} - template < typename Count, - typename Current + typename List > -using Take = Eval>; +using Take = Section< + Size<0>, + Substract>, + List +>; } -- cgit v1.2.3