-
Notifications
You must be signed in to change notification settings - Fork 0
/
VerbHelpers.hpp
259 lines (199 loc) · 6.8 KB
/
VerbHelpers.hpp
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
#ifndef VERBHELPERS_HPP
#define VERBHELPERS_HPP
#include "JNoun.hpp"
#include "JVerbs.hpp"
#include "JTypes.hpp"
#include "Aggregates.hpp"
#include "utils.hpp"
#include <boost/shared_ptr.hpp>
#include <boost/make_shared.hpp>
#include <cmath>
#include <functional>
#include <algorithm>
#include <iterator>
#include <boost/bind.hpp>
#include <boost/iterator/filter_iterator.hpp>
#include <boost/iterator/transform_iterator.hpp>
#include <boost/foreach.hpp>
namespace J {
using boost::shared_ptr;
using boost::make_shared;
using boost::optional;
using J::Aggregates::JResult;
OperationIteratorBase::Ptr get_operation_iterator(const JNoun& arg, const Dimensions& frame,
int output_rank);
Dimensions find_frame(int lrank, int rrank, const Dimensions& larg, const Dimensions& rarg);
template <typename Op>
JNoun::Ptr dyadic_apply(int lrank, int rrank,
JMachine::Ptr m, const JNoun& larg, const JNoun& rarg,
Op op) {
if (lrank >= larg.get_rank() && rrank >= rarg.get_rank()) {
return op(m, larg, rarg)->clone();
}
Dimensions frame = find_frame(lrank, rrank, larg.get_dims(), rarg.get_dims());
OperationIteratorBase::Ptr liter(get_operation_iterator(larg, frame, lrank));
OperationIteratorBase::Ptr riter(get_operation_iterator(rarg, frame, rrank));
JResult res(frame);
for (;!liter->at_end() && !riter->at_end(); ++(*liter), ++(*riter)) {
res.add_noun(op(m, ***liter, ***riter));
}
assert(liter->at_end() && riter->at_end());
return res.assemble_result();
}
template <template <typename> class Op>
struct scalar_monadic_apply {
template <typename T>
struct Impl {
typedef Op<T> our_op;
typedef typename our_op::result_type result_type;
typedef typename our_op::argument_type argument_type;
JNoun::Ptr
operator()(const JArray<argument_type>& arg) {
Dimensions d(arg.get_dims());
shared_ptr<vector<result_type> > v
(new vector<result_type>(d.number_of_elems(), JTypeTrait<result_type>::base_elem()));
transform(arg.begin(), arg.end(), v->begin(), our_op());
return JNoun::Ptr(new JArray<result_type>(d, v));
}
};
};
template <typename OpType>
JNoun::Ptr monadic_apply(int rank, JMachine::Ptr m, const JNoun& arg, OpType op) {
if (rank < 0) {
rank = std::max(0, arg.get_rank() + rank);
}
if ( rank >= arg.get_rank()) {
return op(m, arg);
}
Dimensions frame(rank == 0 ? arg.get_dims() : arg.get_dims().prefix(-rank));
if (frame.number_of_elems() == 0) {
return JNoun::Ptr(new JArray<JInt>(frame));
}
JResult res(frame);
for (OperationIteratorBase::Ptr input(get_operation_iterator(arg, frame, rank));
!input->at_end(); ++(*input)) {
res.add_noun(op(m, ***input));
}
return res.assemble_result();
}
template <template <typename> class OpType>
struct scalar_dyadic_apply {
template <typename T>
struct Impl {
JNoun::Ptr operator()(const JArray<T>& larg, const JArray<T>& rarg, JMachine::Ptr) const {
typedef typename OpType<T>::result_type result_type;
typedef vector<result_type> res_vec;
if (larg.get_dims() == rarg.get_dims()) {
Dimensions d(larg.get_dims());
shared_ptr<res_vec > v(new res_vec(d.number_of_elems(), JTypeTrait<result_type>::base_elem()));
transform(larg.begin(), larg.end(), rarg.begin(), v->begin(), OpType<T>());
return JNoun::Ptr(new JArray<result_type>(d, v));
}
Dimensions frame(find_frame(0, 0, larg.get_dims(), rarg.get_dims()));
if (frame.number_of_elems() == 0) {
return JNoun::Ptr(new JArray<JInt>(frame));
}
OperationScalarIterator<T> liter(larg, frame), riter(rarg, frame);
shared_ptr<res_vec > v(new res_vec(frame.number_of_elems(), JTypeTrait<result_type>::base_elem()));
OpType<T> op;
for(typename res_vec::iterator output(v->begin()), output_end(v->end()); output != output_end;
++output, ++liter, ++riter) {
*output = op(*liter, *riter);
}
return JNoun::Ptr(new JArray<result_type>(frame, v));
}
};
};
template <template <typename> class Op>
struct ScalarDyad: public Dyad {
ScalarDyad(): Dyad(0, 0) {}
static Ptr Instantiate() {
return Ptr(new ScalarDyad<Op>());
}
JNoun::Ptr operator()(JMachine::Ptr m, const JNoun& larg, const JNoun& rarg) const {
return CallWithCommonType<scalar_dyadic_apply<Op>::template Impl, JNoun::Ptr>()(larg, rarg, m);
}
};
template <typename Op>
class DefaultDyad: public Dyad {
Op op;
public:
DefaultDyad(int lrank, int rrank, Op op): Dyad(lrank, rrank), op(op) {}
static Ptr Instantiate(int lrank, int rrank, Op op) {
return Ptr(new DefaultDyad<Op>(lrank, rrank, op));
}
JNoun::Ptr operator()(JMachine::Ptr m, const JNoun& larg, const JNoun& rarg) const {
return dyadic_apply(get_lrank(), get_rrank(), m, larg, rarg, op);
}
};
template <template <typename> class Op>
struct ScalarMonad: public Monad {
ScalarMonad(): Monad(0) {}
static Ptr Instantiate() {
return Ptr(new ScalarMonad<Op>());
}
JNoun::Ptr operator()(JMachine::Ptr, const JNoun& arg) const {
JArrayCaller<scalar_monadic_apply<Op>::template Impl, JNoun::Ptr> caller;
return caller(arg);
}
};
template <typename Op>
class DefaultMonad: public Monad {
Op op;
public:
DefaultMonad(int rank, Op op): Monad(rank), op(op) {}
static Ptr Instantiate(int rank, Op op) {
return Ptr(new DefaultMonad(rank, op));
}
JNoun::Ptr operator()(JMachine::Ptr m, const JNoun& arg) const {
return monadic_apply(get_rank(), m, arg, op);
}
};
class DimensionCounter {
vector<int> reference;
vector<int> current_count;
vector<int> suffix_array;
bool turned_around;
void increment(int pos);
public:
DimensionCounter(const vector<int>& ref);
DimensionCounter& operator++();
int operator*() const;
bool at_end() const {
return turned_around;
}
};
class VerbContainer {
JVerb::Ptr verb;
JMachine::Ptr jmachine;
public:
JNoun::Ptr operator()(const JNoun& larg, const JNoun& rarg) const {
return (*verb)(jmachine, larg, rarg);
}
JNoun::Ptr operator()(const JNoun& arg) const {
return (*verb)(jmachine, arg);
}
VerbContainer(JMachine::Ptr jmachine, JVerb::Ptr verb): verb(verb), jmachine(jmachine) {}
};
template <typename Arg>
struct BadScalarMonadOp: std::unary_function<Arg, Arg> {
Arg operator()(Arg) {
throw JIllegalValueTypeException();
}
};
template <typename Arg>
struct BadScalarDyadOp: std::binary_function<Arg, Arg, Arg> {
Arg operator()(Arg, Arg) {
throw JIllegalValueTypeException();
}
};
template <typename T>
struct new_operation_iterator {
OperationIteratorBase::Ptr operator()(const JArray<T>& arg,
const Dimensions& frame, int output_rank) const {
return boost::static_pointer_cast<OperationIteratorBase>
(make_shared<OperationIterator<T> >(arg, frame, output_rank));
}
};
}
#endif