aboutsummaryrefslogtreecommitdiff
path: root/src/list/operation/basic.h
blob: ff0a4d7f59938ef2cd53babe4ab82de31b286d29 (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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#ifndef TYPEASVALUE_SRC_LIST_OPERATION_BASIC_H_
#define TYPEASVALUE_SRC_LIST_OPERATION_BASIC_H_

#include "operation/math.h"
#include "higher/fold.h"

namespace tav {

template <typename Cons>
class Length {
	private:
		template <
			typename,
			typename Current
		>
		struct Count {
			typedef Add<Size<1>, Current> type;
		};

	public:
		typedef typename Fold<Count, Size<0>, Cons>::type type;
};

template <
	typename Index,
	typename Cons
>
struct Nth {
	typedef typename Nth<
		Substract<Index, Size<1>>,
		Tail<Cons>
	>::type type;
};

template <typename Index>
struct Nth<Index, void> {
	typedef void type;
};

template <typename Cons>
struct Nth<Size<0>, Cons> {
	typedef Head<Cons> type;
};

template <
	typename Count,
	typename Current
>
struct Take {
	typedef Cons<
		Head<Current>,
		typename Take<
			Substract<Count, Size<1>>,
			Tail<Current>
		>::type
	> type;
};

template <typename Current>
struct Take<Size<0>, Current> {
	typedef void type;
};

template <typename Count>
struct Take<Count, void> {
	typedef void type;
};

template <>
struct Take<Size<0>, void> {
	typedef void type;
};

}

#endif  // TYPEASVALUE_SRC_LIST_OPERATION_BASIC_H_