-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstatic-sort.cpp
250 lines (231 loc) · 6.3 KB
/
static-sort.cpp
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
/*
* Created by avlechen (c), 2017
* MIT license, so do whatever you want.
*
* This code implements sorting of types if some precedence order is defined
*/
// TODO use static-list.hpp for all service functions
#include <iostream>
#include <typeinfo>
#include "static-list.hpp"
template<bool c, typename NewT, typename TrueClsT, typename FalseClsT>
struct Classify{
};
template<typename NewT, typename TrueClsT, typename FalseClsT>
struct Classify<true, NewT, TrueClsT, FalseClsT>{
typedef typename PushFront<NewT, TrueClsT>::Result NewTrueClsT;
typedef FalseClsT NewFalseClsT;
};
template<typename NewT, typename TrueClsT, typename FalseClsT>
struct Classify<false, NewT, TrueClsT, FalseClsT>{
typedef TrueClsT NewTrueClsT;
typedef typename PushFront<NewT, FalseClsT>::Result NewFalseClsT;
};
template<typename T1, typename T2>
struct Less{
static const bool result = false;
};
template<typename Anchor, typename SeqT, typename RightSet, typename LeftSet>
struct PartitionImpl{};
template<typename Anchor, typename v1, typename RightSet, typename LeftSet>
struct PartitionImpl<Anchor, List<v1>, RightSet, LeftSet>{
static const bool less=Less<v1, Anchor>::result;
typedef typename Classify<less, v1, RightSet, LeftSet>::NewTrueClsT RstRightSet;
typedef typename Classify<less, v1, RightSet, LeftSet>::NewFalseClsT RstLeftSet;
};
template<typename RightSet, typename LeftSet, typename Anchor, typename v1, typename...vs>
struct PartitionImpl<Anchor, List<v1, vs...>, RightSet, LeftSet>{
static const bool less=Less<typename PopFront<List<v1, vs...> >::Result, Anchor>::result;
typedef typename Classify<less, v1, RightSet, LeftSet>::NewTrueClsT TmpRstRightSet;
typedef typename Classify<less, v1, RightSet, LeftSet>::NewFalseClsT TmpRstLeftSet;
typedef typename PartitionImpl<Anchor, List<vs...>, TmpRstRightSet, TmpRstLeftSet>::RstRightSet RstRightSet;
typedef typename PartitionImpl<Anchor, List<vs...>, TmpRstRightSet, TmpRstLeftSet>::RstLeftSet RstLeftSet;
};
template<typename T>
struct Partition{
};
template<typename v1, typename v2, typename...vs>
struct Partition<List<v1, v2, vs...> >{
typedef v1 Anchor;
typedef List<> RightSet;
typedef List<> LeftSet;
typedef typename PartitionImpl<Anchor, List<v1, v2, vs...>, RightSet, LeftSet>::RstRightSet RstRightSet;
typedef typename PartitionImpl<Anchor, List<v1, v2, vs...>, RightSet, LeftSet>::RstLeftSet RstLeftSet;
};
//why introduce this? refer to Sort
template<typename SrcT, typename RightSet, typename LeftSet, template<typename > class SortOp>
struct SortSub{
typedef typename SortOp<RightSet>::Result TmpRightSet2;
typedef typename SortOp<LeftSet>::Result TmpLeftSet2;
};
template<typename SrcT, typename LeftSet, template<typename> class SortOp>
struct SortSub<SrcT, SrcT, LeftSet, SortOp>{
typedef SrcT TmpRightSet2;
typedef typename SortOp<LeftSet>::Result TmpLeftSet2;
};
template<typename SrcT, typename RightSet, template<typename> class SortOp>
struct SortSub<SrcT, RightSet, SrcT, SortOp>{
typedef typename SortOp<RightSet>::Result TmpRightSet2;
typedef SrcT TmpLeftSet2;
};
template<typename T>
struct Sort;
template<>
struct Sort<List<> >{
typedef List<> Result;
};
template<typename v1>
struct Sort<List<v1>> {
typedef List<v1> Result;
};
template<typename v1, typename...vs>
struct Sort< List<v1, vs...> >{
typedef List<v1, vs...> SrcType;
typedef typename Partition< List<v1, vs...> >::RstRightSet TmpRightSet;
typedef typename Partition< List<v1, vs...> >::RstLeftSet TmpLeftSet;
//to by pass the case SrcType <==> TmpRightSet or SrcType <==> TmpLeftSet
typedef typename SortSub<SrcType, TmpRightSet, TmpLeftSet, Sort>::TmpRightSet2 TmpRightSet2;
typedef typename SortSub<SrcType, TmpRightSet, TmpLeftSet, Sort>::TmpLeftSet2 TmpLeftSet2;
typedef typename CatList<TmpRightSet2, TmpLeftSet2>::Result Result;
};
// This is test case
// It is f-ng huge and seems to be overkill
// Needs to be extracted in a separate test
class A {};
class B {};
class C {};
class D {};
class E {};
template<>
struct Less<A, B> {
static const bool result = true;
};
template<>
struct Less<A, C> {
static const bool result = true;
};
template<>
struct Less<A, D> {
static const bool result = true;
};
template<>
struct Less<A, E> {
static const bool result = true;
};
template<>
struct Less<B, A> {
static const bool result = false;
};
template<>
struct Less<B, C> {
static const bool result = true;
};
template<>
struct Less<B, D> {
static const bool result = true;
};
template<>
struct Less<B, E> {
static const bool result = true;
};
template<>
struct Less<C, A> {
static const bool result = false;
};
template<>
struct Less<C, B> {
static const bool result = false;
};
template<>
struct Less<C, D> {
static const bool result = true;
};
template<>
struct Less<C, E> {
static const bool result = true;
};
template<>
struct Less<D, A> {
static const bool result = false;
};
template<>
struct Less<D, B> {
static const bool result = false;
};
template<>
struct Less<D, C> {
static const bool result = false;
};
template<>
struct Less<D, E> {
static const bool result = true;
};
template<>
struct Less<E, A> {
static const bool result = false;
};
template<>
struct Less<E, B> {
static const bool result = false;
};
template<>
struct Less<E, C> {
static const bool result = false;
};
template<>
struct Less<E, D> {
static const bool result = false;
};
// FIXME need to extract to separate
int main() {
using namespace std;
{
typedef List<A> t;
dumpListType(t());
dumpListType(Sort<t>::Result());
cout << endl;
}
{
typedef List<B, A> t;
dumpListType(t());
dumpListType(Sort<t>::Result());
cout << endl;
}
{
//
typedef List<B, B, A> t;
dumpListType(t());
dumpListType(Sort<t>::Result());
cout << endl;
}
{
typedef List<B, A, B > t;
dumpListType(t());
dumpListType(Sort<t>::Result());
cout << endl;
}
{
typedef List<B, B, B, B, B, A, B, B, B, B> t;
dumpListType(t());
dumpListType(Sort<t>::Result());
cout << endl;
}
{
typedef List<B, B, B, B, B, A> t;
dumpListType(t());
dumpListType(Sort<t>::Result());
cout << endl;
}
{
typedef List<E, E, E, D, D, C, B, B, B, A, A, A, A> t;
dumpListType(t());
dumpListType(Sort<t>::Result());
cout << endl;
}
{
typedef List<D, B, C, E, A, B, D, C> t;
dumpListType(t());
dumpListType(Sort<t>::Result());
cout << endl; // this result is actuall incorrect
}
}