-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmy_type_functions.h
293 lines (225 loc) · 5.46 KB
/
my_type_functions.h
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
#ifndef MY_TYPE_FUNCTIONS
#define MY_TYPE_FUNCTIONS
#include <iostream>
#include <sstream>
#include "my_intrinsics.h"
using namespace std;
// InputType: FunctionalProcedure x size_t -> Regular
template<typename T, size_t i>
requires(FunctionalProcedure(T))
struct input_type;
#define InputType(T, i) typename input_type< T, i >::type
// action
template<typename T>
struct input_type<void (*)(T&), 0> {
typedef T type;
};
// unary operation
template<typename T>
struct input_type<T (*)(T), 0> {
typedef T type;
};
template<typename T>
struct input_type<T (*)(const T&), 0> {
typedef T type;
};
// left binary accumulation
template<typename T>
struct input_type<void (*)(T&, const T&), 0> {
typedef T type;
};
// right binary accumulation
template<typename T>
struct input_type<void (*)(const T&, T&), 0> {
typedef T type;
};
// binary operation
template<typename T>
struct input_type<T (*)(T, T), 0> {
typedef T type;
};
template<typename T>
struct input_type<T (*)(const T&, const T&), 0> {
typedef T type;
};
// unary predicate
template<typename T>
struct input_type<bool (*)(T), 0> {
typedef T type;
};
template<typename T>
struct input_type<bool (*)(const T&), 0> {
typedef T type;
};
// binary predicate
template<typename T>
struct input_type<bool (*)(T, T), 0> {
typedef T type;
};
template<typename T>
struct input_type<bool (*)(const T&, const T&), 0> {
typedef T type;
};
// iterator traversal
template<typename T>
struct input_type<void (*)(T), 0> {
typedef T type;
};
template<typename T>
struct input_type<void (*)(const T&), 0> {
typedef T type;
};
// Domain: HomogeneousFunction -> Regular
#define Domain(T) InputType(T, 0)
// Codomain: FunctionalProcedure -> Regular
template<typename T>
struct codomain_type;
template<typename T>
struct codomain_type<T (*)(T)> {
typedef T type;
};
template<typename T>
struct codomain_type<T (*)(T, T)> {
typedef T type;
};
template<typename T>
struct codomain_type<bool (*)(T)> {
typedef bool type;
};
template<typename T>
struct codomain_type<bool (*)(T, T)> {
typedef bool type;
};
#define Codomain(T) typename codomain_type< T >::type
// DistanceType: Transformation -> Integer
template<typename F>
requires(Transformation(F))
struct distance_type {
typedef Domain(F) type;
};
template<>
struct distance_type<int> {
typedef unsigned int type;
};
template<>
struct distance_type<unsigned long> {
typedef unsigned long type;
};
template<typename T>
struct distance_type<T*> {
typedef unsigned long type;
};
#define DistanceType(T) typename distance_type< T >::type
template<typename T>
requires(ArchimedeanMonoid(T))
struct quotient_type;
template<>
struct quotient_type<int> {
typedef int type;
};
#define QuotientType(T) typename quotient_type< T >::type
// quotient_remainder
template<typename T>
struct input_type<pair<QuotientType(T), T> (*)(T, T), 0> {
typedef T type;
};
template<typename T>
struct input_type<pair<QuotientType(T), T> (*)(const T&, const T&), 0> {
typedef T type;
};
template<typename T>
requires(Regular(T))
struct value_type {
typedef T type;
};
template<typename T>
requires(Regular(T))
struct value_type< T* > {
typedef T type;
};
#define ValueType(T) typename value_type< T >::type
template<typename T>
requires(Regular(T))
const T& source(const T& x)
{
return x;
}
template<typename T>
requires(Regular(ValueType(T)))
const T& source(T* x)
{
return *x;
}
template<typename T>
requires(Regular(T))
T& sink(T& x)
{
return x;
}
template<typename T>
requires(Regular(T))
T& sink(T* x)
{
return *x;
}
template<typename C>
requires(BifurcateCoordinate(C))
struct weight_type;
#define WeightType(C) typename weight_type< C >::type
template<typename S>
requires(ForwardLinker(S))
struct iterator_type;
#define IteratorType(S) typename iterator_type< S >::type
struct my_iterator_tag {};
struct my_forward_iterator_tag {};
struct my_bidirectional_iterator_tag {};
struct my_indexed_iterator_tag {};
struct my_random_access_iterator_tag {};
template<typename I>
requires(Iterator(I))
struct my_iterator_concept
{
typedef my_iterator_tag type;
};
template<typename T>
struct my_iterator_concept<T*>
{
typedef my_random_access_iterator_tag type;
};
#define IteratorConcept(I) typename my_iterator_concept< I >::type
template<typename T0, typename T1, typename T2>
requires(Regular(T0), Regular(T1), Regular(T2))
struct triple
{
T0 m0;
T1 m1;
T2 m2;
triple() { }
triple(T0 m0, T1 m1, T2 m2) : m0(m0), m1(m1), m2(m2) { }
};
template<typename T0, typename T1, typename T2>
requires(Regular(T0) && Regular(T1) && Regular(T2))
bool operator==(const triple<T0, T1, T2>& x, const triple<T0, T1, T2>& y)
{
return x.m0 == y.m0 && x.m1 == y.m1 && x.m2 == y.m2;
}
template<typename T0, typename T1, typename T2>
requires(TotalOrdering(T0) &&
TotalOrdering(T1) &&
TotalOrdering(T2))
bool operator<(const triple<T0, T1, T2>& x, const triple<T0, T1, T2>& y)
{
return x.m0 < y.m0 ||
(!(y.m0 < x.m0) && x.m1 < y.m1) ||
(!(y.m0 < x.m0) && !(y.m1 < x.m1) && x.m2 < y.m2);
}
template<typename T0, typename T1, typename T2>
requires(Regular(T0), Regular(T1), Regular(T2))
ostream& operator<<(ostream& output, const triple<T0, T1, T2>& x)
{
stringstream combiner;
combiner << "(" << x.m0 << ", " << x.m1 << ", " << x.m2 << ")";
output << combiner.str();
return output;
}
#endif