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 ++-------- test.cc | 132 +++++++++++++++--------------- 9 files changed, 185 insertions(+), 163 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 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 +>; } diff --git a/test.cc b/test.cc index 074d711..6560795 100644 --- a/test.cc +++ b/test.cc @@ -359,155 +359,155 @@ static_assert( "(list 1 2) != (cons 1 (cons 2 void))" ); -// list length +// list take static_assert( std::is_same< - tav::Size<1>, - tav::Length< - tav::List> + tav::List>, + tav::Take< + tav::Size<1>, + tav::List, tav::Int<2>> > >::value, - "(length (list 1)) != 1" + "(take 1 (list 1 2)) != (list 1)" ); static_assert( std::is_same< - tav::Size<2>, - tav::Length< + tav::List, tav::Int<2>>, + tav::Take< + tav::Size<2>, tav::List, tav::Int<2>> > >::value, - "(length (list 1 2)) != 2" + "(take 2 (list 1 2)) != (list 1 2)" ); -// list nth - static_assert( std::is_same< - tav::Int<1>, - tav::Nth< - tav::Size<0>, - tav::List> + tav::List, tav::Int<2>>, + tav::Take< + tav::Size<3>, + tav::List, tav::Int<2>> > >::value, - "(nth 0 (list 1)) != 1" + "(take 3 (list 1 2)) != (list 1 2)" ); +// list drop + static_assert( std::is_same< - tav::Int<1>, - tav::Nth< - tav::Size<0>, + tav::List>, + tav::Drop< + tav::Size<1>, tav::List, tav::Int<2>> > >::value, - "(nth 0 (list 1 2)) != 1" + "(drop 1 (list 1 2)) != (list 2)" ); static_assert( std::is_same< - tav::Int<2>, - tav::Nth< - tav::Size<1>, + void, + tav::Drop< + tav::Size<2>, tav::List, tav::Int<2>> > >::value, - "(nth 1 (list 1 2)) != 2" + "(drop 2 (list 1 2)) != void" ); static_assert( std::is_same< - tav::Int<1>, - tav::First, tav::Int<2>, tav::Int<3>>> + void, + tav::Drop< + tav::Size<3>, + tav::List, tav::Int<2>> + > >::value, - "(first (list 1 2 3)) != 1" + "(drop 3 (list 1 2)) != void" ); +// list length + static_assert( std::is_same< - tav::Int<2>, - tav::Second, tav::Int<2>, tav::Int<3>>> + tav::Size<1>, + tav::Length< + tav::List> + > >::value, - "(second (list 1 2 3)) != 2" + "(length (list 1)) != 1" ); static_assert( std::is_same< - tav::Int<3>, - tav::Third, tav::Int<2>, tav::Int<3>>> + tav::Size<2>, + tav::Length< + tav::List, tav::Int<2>> + > >::value, - "(third (list 1 2 3)) != 3" + "(length (list 1 2)) != 2" ); -// list take +// list nth static_assert( std::is_same< - tav::List>, - tav::Take< - tav::Size<1>, - tav::List, tav::Int<2>> + tav::Int<1>, + tav::Nth< + tav::Size<0>, + tav::List> > >::value, - "(take 1 (list 1 2)) != (list 1)" + "(nth 0 (list 1)) != 1" ); static_assert( std::is_same< - tav::List, tav::Int<2>>, - tav::Take< - tav::Size<2>, + tav::Int<1>, + tav::Nth< + tav::Size<0>, tav::List, tav::Int<2>> > >::value, - "(take 2 (list 1 2)) != (list 1 2)" + "(nth 0 (list 1 2)) != 1" ); static_assert( std::is_same< - tav::List, tav::Int<2>>, - tav::Take< - tav::Size<3>, + tav::Int<2>, + tav::Nth< + tav::Size<1>, tav::List, tav::Int<2>> > >::value, - "(take 3 (list 1 2)) != (list 1 2)" + "(nth 1 (list 1 2)) != 2" ); -// list drop - static_assert( std::is_same< - tav::List>, - tav::Drop< - tav::Size<1>, - tav::List, tav::Int<2>> - > + tav::Int<1>, + tav::First, tav::Int<2>, tav::Int<3>>> >::value, - "(drop 1 (list 1 2)) != (list 2)" + "(first (list 1 2 3)) != 1" ); static_assert( std::is_same< - void, - tav::Drop< - tav::Size<2>, - tav::List, tav::Int<2>> - > + tav::Int<2>, + tav::Second, tav::Int<2>, tav::Int<3>>> >::value, - "(drop 2 (list 1 2)) != void" + "(second (list 1 2 3)) != 2" ); static_assert( std::is_same< - void, - tav::Drop< - tav::Size<3>, - tav::List, tav::Int<2>> - > + tav::Int<3>, + tav::Third, tav::Int<2>, tav::Int<3>>> >::value, - "(drop 3 (list 1 2)) != void" + "(third (list 1 2 3)) != 3" ); // list append -- cgit v1.2.3