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
77
78
79
80
|
#include <boost/mpl/vector.hpp>
#include <boost/mpl/vector_c.hpp>
#include <boost/mpl/eval_if.hpp>
#include <boost/mpl/identity.hpp>
#include <boost/mpl/empty.hpp>
#include <boost/mpl/less.hpp>
#include <boost/mpl/front.hpp>
#include <boost/mpl/pop_front.hpp>
#include <boost/mpl/push_front.hpp>
#include <boost/mpl/size.hpp>
#include <boost/mpl/iterator_range.hpp>
#include <boost/mpl/int.hpp>
#include <boost/mpl/for_each.hpp>
#include <iostream>
using namespace boost::mpl;
template<typename V1, typename V2>
struct Merge {
template<typename T1, typename T2>
struct F {
typedef typename push_front<
typename Merge<typename pop_front<T1>::type, T2>::type,
typename front<T1>::type
>::type type;
};
typedef typename eval_if<
empty<V1>,
identity<V2>,
eval_if<
empty<V2>,
identity<V1>,
eval_if<
less<typename front<V1>::type, typename front<V2>::type>,
F<V1, V2>,
F<V2, V1>
>
>
>::type type;
};
template<typename V>
struct MergeSort {
template<typename T>
struct F {
typedef typename begin<T>::type t0;
typedef typename advance<t0, int_<size<T>::value/2>>::type t1;
typedef typename end<T>::type t2;
typedef typename Merge<
typename MergeSort<iterator_range<t0, t1>>::type,
typename MergeSort<iterator_range<t1, t2>>::type
>::type type;
};
typedef typename eval_if<
empty<V>,
identity<vector<>>,
eval_if<
less<size<V>, int_<2>>,
identity<vector<typename front<V>::type>>,
F<V>
>
>::type type;
};
typedef vector_c<int, 0,2,1,3,4> t1;
typedef MergeSort<t1>::type t10;
struct print {
template <typename T>
void operator()( T t ) const {
std::cout << T::value << " ";
}
};
int main() {
for_each<t10>(print());
return 0;
}
|