From beb46377c075ccc07a22b45771f8ad993a4e088e Mon Sep 17 00:00:00 2001 From: Adrian Kummerlaender Date: Sun, 8 Feb 2015 20:38:00 +0100 Subject: Implemented higher order `Sort` list operation * _Quicksort_ is the algorithm of choice ** it lends itself quite well to the _TypeAsValue_ approach because of its recursive nature ** this required implementation of a `Drop` counterpart to `Take` * the middle item of a given list is selected as the _pivot_ element * the `List` list contructor had to be expanded to allow `void` arguments inside its variadic parameter pack * added appropriate test cases --- src/list/list.h | 11 ++++++++++ src/list/operation/higher/sort.h | 46 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+) create mode 100644 src/list/operation/higher/sort.h (limited to 'src/list') diff --git a/src/list/list.h b/src/list/list.h index 2e335e2..d2a5cb0 100644 --- a/src/list/list.h +++ b/src/list/list.h @@ -21,6 +21,16 @@ struct List { typedef typename Cons::type type; }; +template +struct List { + typedef typename List::type type; +}; + +template +struct List { + typedef typename List::type type; +}; + template < typename Type, Type... Values @@ -40,6 +50,7 @@ using Tail = typename Cdr::type; #include "operation/basic.h" #include "operation/nth.h" #include "operation/take.h" +#include "operation/drop.h" #include "operation/append.h" #include "operation/concatenate.h" diff --git a/src/list/operation/higher/sort.h b/src/list/operation/higher/sort.h new file mode 100644 index 0000000..0ff5c40 --- /dev/null +++ b/src/list/operation/higher/sort.h @@ -0,0 +1,46 @@ +#ifndef TYPEASVALUE_SRC_LIST_OPERATION_HIGHER_SORT_H_ +#define TYPEASVALUE_SRC_LIST_OPERATION_HIGHER_SORT_H_ + +#include "filter.h" +#include "list/operation/concatenate.h" + +namespace tav { + +template < + template class Comparator, + typename Sequence +> +class Sort { + private: + using index = Divide::type, Size<2>>; + using pivot = typename Nth::type; + + using sequence_sans_pivot = typename Append< + typename Take::type, + typename Drop>, Sequence>::type + >::type; + + template + using comparator_wrapper = Comparator; + + using lhs = typename Filter::type; + using rhs = typename Remove::type; + + public: + typedef typename Concatenate< + typename List< + typename Sort::type, + typename List::type, + typename Sort::type + >::type + >::type type; +}; + +template class Comparator> +struct Sort { + typedef void type; +}; + +} + +#endif // TYPEASVALUE_SRC_LIST_OPERATION_HIGHER_SORT_H_ -- cgit v1.2.3