-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlist.cpp
More file actions
75 lines (59 loc) · 2.4 KB
/
list.cpp
File metadata and controls
75 lines (59 loc) · 2.4 KB
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
#include <iostream>
#include <climits>
#include "list.hpp"
typedef LIST5<10,23,4,2,9>::TYPE myList;
template <int n> struct SQUARE {
static const int VALUE = n*n;
};
template <int n> struct ID {
static const int VALUE = n;
};
template <int a, int b> struct ADDITION {
static const int VALUE = a + b;
};
template <int a> struct INCREMENT {
static const int VALUE = a + 1;
};
template <int a, int b> struct MAX {
static const int VALUE = a > b ? a : b;
};
template <int a, int b> struct MIN {
static const int VALUE = a < b ? a : b;
};
template <int a, int b> struct ORD {
static const bool VALUE = a < b;
};
int main() {
/* The best example */
std::cout << "The list is: ";
ListPrinter<myList>::print(); // this one generates tons of classes
std::cout << "The sorted list is: ";
ListPrinter<SORT<ORD, myList>::TYPE>::print();
/* basic list operations */
std::cout << "head = " << myList::HEAD << std::endl;
std::cout << "length = " << myList::LENGTH << std::endl;
std::cout << "sum = " << SUM< myList >::RESULT << std::endl;
std::cout << "minimum = " << myList::MINIMUM << std::endl;
/* map-reduce or fold */
std::cout << "sum of squares with map_reduce = " << MAP_REDUCE< myList, SQUARE, ADDITION, 0 >::RESULT << std::endl;
std::cout << "minimum with map_reduce = " << MAP_REDUCE< myList, ID, MIN, INT_MAX >::RESULT << std::endl;
std::cout << "sum done with map_reduce = " << MAP_REDUCE< myList, ID, ADDITION, 0 >::RESULT << std::endl;
std::cout << "mapping square over the list = ";
ListPrinter<MAP<myList, SQUARE>::TYPE>::print();
std::cout << "mapping increment over the list = ";
ListPrinter<MAP<myList, INCREMENT >::TYPE>::print();
/* other list operations */
std::cout << "prepending '3' = ";
ListPrinter<PREPEND<3,myList>::TYPE>::print();
std::cout << "tail function = ";
ListPrinter<TAIL<myList>::TYPE>::print();
std::cout << "tail member function = ";
ListPrinter<myList::TAIL>::print();
/* internal routines used for the merge sort */
std::cout << "merge (1,6,8) (6,9) = ";
ListPrinter<MERGE<ORD, LIST3<1,6,8>::TYPE, LIST2<6,9>::TYPE>::TYPE>::print();
std::cout << "fst . split (1,6,8,6,9) = ";
ListPrinter<SPLIT<LIST5<1,6,8,6,9>::TYPE>::TYPE::FST>::print();
std::cout << "snd . split (1,6,8,6,9) = ";
ListPrinter<SPLIT<LIST5<1,6,8,6,9>::TYPE>::TYPE::SND>::print();
}