aboutsummaryrefslogtreecommitdiff
path: root/src/list/operation/higher/sort.h
blob: 51317fa0766fc10bc99565ef2251c259aaf52fff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#ifndef TYPEASVALUE_SRC_LIST_OPERATION_HIGHER_SORT_H_
#define TYPEASVALUE_SRC_LIST_OPERATION_HIGHER_SORT_H_

#include "list/operation/concatenate.h"
#include "list/operation/delete_nth.h"
#include "list/operation/higher/partition.h"
#include "function/apply.h"

namespace tav {

namespace detail {

template <
	template<typename, typename> class Comparator,
	typename                           Sequence
>
class quick_sort {
	private:
		using index = Divide<Length<Sequence>, Size<2>>;
		using pivot = Nth<index, Sequence>;

		using partitions = Partition<
			Apply<Comparator, pivot, _0>::template function,
			DeleteNth<index, Sequence>
		>;

		using lhs = Car<partitions>;
		using rhs = Cdr<partitions>;

	public:
		using type = Concatenate<
			Eval<quick_sort<Comparator, lhs>>,
			List<pivot>,
			Eval<quick_sort<Comparator, rhs>>
		>;
};

template <template<typename, typename> class Comparator>
struct quick_sort<Comparator, void> {
	typedef void type;
};

}

template <
	template<typename, typename> class Comparator,
	typename                           Sequence
>
using Sort = Eval<detail::quick_sort<Comparator, Sequence>>;

}

#endif  // TYPEASVALUE_SRC_LIST_OPERATION_HIGHER_SORT_H_