-
Notifications
You must be signed in to change notification settings - Fork 0
/
lambda.h
298 lines (261 loc) · 9.31 KB
/
lambda.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
294
295
296
297
298
#ifndef _A2_H
#define _A2_H
// Author: Mathew Rodley
// SID: 305126180
// Ename: mrod5488
// Email: [email protected]
// Purpose:
// Defines _1 to be used for lambda expressions
// For ostream
#include <iostream>
// for ostream manip funcs
typedef std::ostream&(*iomanipfunc)(std::ostream&);
#define MAKE_TRAITS_1(TYPE1, TYPE2, RESULTTYPE) \
template<> \
struct LambdaTrait<TYPE1, TYPE2> { \
typedef RESULTTYPE Type; \
};
#define MAKE_TRAITS_2(TYPE1, TYPE2, RESULTTYPE) \
template<> \
struct LambdaTrait<TYPE1, TYPE2> { \
typedef RESULTTYPE Type; \
}; \
template<> \
struct LambdaTrait<TYPE2, TYPE1> { \
typedef RESULTTYPE Type; \
};
// Taits - default, use LHS (T1)
template <class T1, class T2>
struct LambdaTrait {
typedef T1 Type; // use T1 as default
};
// Make additional Traits
MAKE_TRAITS_1(int,int,int); // int + int = int
MAKE_TRAITS_2(int,float,float); // int + float = float && float + int = float
MAKE_TRAITS_2(int,double,double); // etc..
MAKE_TRAITS_2(float,double,double); // etc..
// Special cases for ostreams
template <class T2>
struct LambdaTrait<std::ostream, T2> {
typedef std::ostream& Type;
};
template <class T2>
struct LambdaTrait<T2, std::ostream> {
typedef std::ostream& Type;
};
template <>
struct LambdaTrait<std::ostream, std::ostream> {
typedef std::ostream& Type;
};
//
// The operator we call for an expression
// operator() cant be static inline so use doOp
//
template <class T1, class T2>
class LambdaAssignOP {
public:
typedef typename LambdaTrait<T1, T2>::Type Type;
static inline Type doOp(T1& a, T2& b) { a = (T1)b; return a; }
static inline Type doOp(T1& a, T2 b) { a = (T1)b; return a; }
};
template <class T1, class T2>
class LambdaSumOP {
public:
typedef typename LambdaTrait<T1, T2>::Type Type;
static inline Type doOp(T1 a, T2 b) { return a + b; }
};
template <class T1, class T2>
class LambdaMinusOP {
public:
typedef typename LambdaTrait<T1, T2>::Type Type;
static inline Type doOp(T1 a, T2 b) { return a - b; }
};
template <class T1, class T2>
class LambdaMultOP {
public:
typedef typename LambdaTrait<T1, T2>::Type Type;
static inline Type doOp(T1 a, T2 b) { return a * b; }
};
template <class T1, class T2>
class LambdaDivOP {
public:
typedef typename LambdaTrait<T1, T2>::Type Type;
static inline Type doOp(T1 a, T2 b) { return a / b; }
};
template <class T2>
class LambdaOStreamOP {
public:
typedef typename LambdaTrait<std::ostream, T2>::Type Type;
static inline Type doOp(std::ostream& os, T2 b) {
os << b;
return os;
}
};
// Holds a the _1 variable
class LambdaVariable {
public:
typedef int Type;
Type& operator()(Type& x) {
return x;
}
};
//
// Class remembers a literal in the expression, i.e the 1 in _1 + 1
//
template <class T1>
class LambdaLiteral {
private:
T1 _l;
public:
typedef T1 Type;
LambdaLiteral(const T1 x)
: _l(x) { }
T1 operator()(int) {
return _l;
}
};
// holds ostream
class LambdaStream {
private:
std::ostream& _o;
public:
typedef std::ostream& Type;
LambdaStream(std::ostream& o) : _o(o) { }
std::ostream& operator()(int) {
return _o;
}
};
//
// Binary Expression
//
template <class L, class R, class OP>
class LambdaBinaryExpression {
private:
L _l;
R _r;
public:
typedef typename LambdaTrait<typename L::Type, typename R::Type>::Type Type;
LambdaBinaryExpression (const L& l, const R& r)
: _l(l), _r(r) { }
Type operator()(int& x) {
return OP::doOp(_l(x), _r(x));
}
};
//
// Contains an expression (Wrapped)
//
template <class E>
class LambdaExpression {
private:
E _expr;
public:
typedef typename E::Type Type;
LambdaExpression(const E& e)
: _expr(e) { }
Type operator()(int& x) {
return _expr(x);
}
};
// Special Case for LambdaVariable where operator() wants to return a reference
// Also LambdaVariable can have an assignment operator on the LHS
// i.e
// _1 = 2
// _1 = <expr>
template <>
class LambdaExpression<LambdaVariable> {
private:
LambdaVariable _expr;
public:
typedef LambdaVariable::Type Type;
LambdaExpression(const LambdaVariable& e)
: _expr(e) { }
Type& operator()(int& x) {
return _expr(x);
}
// The assign operator for LambdaExpression
template <class R>
LambdaExpression<LambdaBinaryExpression<LambdaExpression<LambdaVariable>, LambdaExpression<R>, LambdaAssignOP<Type, typename R::Type> > >
operator=(const LambdaExpression<R>& r) {
typedef LambdaBinaryExpression<LambdaExpression<LambdaVariable>, LambdaExpression<R>, LambdaAssignOP<Type, typename R::Type> > LambdaExpression_t;
return LambdaExpression<LambdaExpression_t>(LambdaExpression_t(*this, r));
}
// The assign operator for literal
template <class R>
LambdaExpression<LambdaBinaryExpression<LambdaExpression<LambdaVariable>, LambdaExpression<LambdaLiteral<R> >, LambdaAssignOP<Type, R> > >
operator=(const R x) {
typedef LambdaBinaryExpression<LambdaExpression<LambdaVariable>, LambdaExpression<LambdaLiteral<R> >, LambdaAssignOP<Type, R > > LambdaExpression_t;
LambdaLiteral<R> r(x);
return LambdaExpression<LambdaExpression_t>(LambdaExpression_t(*this, r));
}
};
// Special case for lambda stream
template <>
class LambdaExpression<LambdaStream> {
private:
LambdaStream _expr;
public:
typedef LambdaStream::Type Type;
LambdaExpression(const LambdaStream& e)
: _expr(e) { }
std::ostream& operator()(int& x) {
return _expr(x);
}
};
// cout << EXPR
template <class R>
LambdaExpression<LambdaBinaryExpression<LambdaExpression<LambdaStream>, LambdaExpression<R>, LambdaOStreamOP<typename R::Type> > >
operator<<(std::ostream& o, const LambdaExpression<R>& r) {
typedef LambdaBinaryExpression<LambdaExpression<LambdaStream>, LambdaExpression<R>, LambdaOStreamOP<typename R::Type> > LambdaExpression_t;
LambdaStream l(o);
return LambdaExpression<LambdaExpression_t>(LambdaExpression_t(l, r));
};
// EXPR << literal
template <class L, class R>
LambdaExpression<LambdaBinaryExpression<LambdaExpression<L>, LambdaLiteral<R>, LambdaOStreamOP<R> > >
operator<<(const LambdaExpression<L>& l, R x) {
typedef LambdaBinaryExpression<LambdaExpression<L>, LambdaLiteral<R>, LambdaOStreamOP<R> > LambdaExpression_t;
LambdaLiteral<R> r(x);
return LambdaExpression<LambdaExpression_t>(LambdaExpression_t(l, r));
};
// EXPT << ManipFunc
template <class L>
LambdaExpression<LambdaBinaryExpression<LambdaExpression<L>, LambdaLiteral<iomanipfunc>, LambdaOStreamOP<iomanipfunc> > >
operator<<(const LambdaExpression<L>& l, iomanipfunc x) {
typedef LambdaBinaryExpression<LambdaExpression<L>, LambdaLiteral<iomanipfunc>, LambdaOStreamOP<iomanipfunc> > LambdaExpression_t;
LambdaLiteral<iomanipfunc> r(x);
return LambdaExpression<LambdaExpression_t>(LambdaExpression_t(l, r));
};
/*
* Creating an operation is standard bar which operator to overload
* and which evaluation object to use
*/
#define CREATE_OP(LAMBDAOP, OP) \
template <class L, class R> \
LambdaExpression<LambdaBinaryExpression<LambdaExpression<L>, LambdaExpression<R>, LAMBDAOP<typename L::Type, typename R::Type> > > \
OP(const LambdaExpression<L>& l, const LambdaExpression<R>& r) { \
typedef LambdaBinaryExpression<LambdaExpression<L>, LambdaExpression<R>, LAMBDAOP<typename L::Type, typename R::Type> > LambdaExpression_t; \
return LambdaExpression<LambdaExpression_t>(LambdaExpression_t(l, r)); \
}; \
\
template <class L, class R> \
LambdaExpression<LambdaBinaryExpression<LambdaExpression<L>, LambdaExpression<LambdaLiteral<R> >, LAMBDAOP<typename L::Type, R> > > \
OP(const LambdaExpression<L>& l, const R x) { \
typedef LambdaBinaryExpression<LambdaExpression<L>, LambdaExpression<LambdaLiteral<R> >, LAMBDAOP<typename L::Type, R> > LambdaExpression_t; \
LambdaLiteral<R> r(x); \
return LambdaExpression<LambdaExpression_t>(LambdaExpression_t(l, r)); \
}; \
template <class L, class R> \
LambdaExpression<LambdaBinaryExpression<LambdaExpression<LambdaLiteral<L> >, LambdaExpression<R>, LAMBDAOP<L, typename R::Type> > > \
OP(const L x, const LambdaExpression<R>& r) { \
typedef LambdaBinaryExpression<LambdaExpression<LambdaLiteral<L> >, LambdaExpression<R>, LAMBDAOP<L, typename R::Type> > LambdaExpression_t; \
LambdaLiteral<L> l(x); \
return LambdaExpression<LambdaExpression_t>(LambdaExpression_t(l, r)); \
};
CREATE_OP(LambdaSumOP, operator+)
CREATE_OP(LambdaMinusOP, operator-)
CREATE_OP(LambdaMultOP, operator*)
CREATE_OP(LambdaDivOP, operator/)
// Initialize out _1 Object, it should act like a variable
LambdaVariable __1;
LambdaExpression<LambdaVariable> _1(__1);
#endif