-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFTableExpression.cpp
More file actions
237 lines (218 loc) · 7.92 KB
/
FTableExpression.cpp
File metadata and controls
237 lines (218 loc) · 7.92 KB
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
// Code to evaluate expressions from FTable columns
#include "FTable.h"
#include "Expressions.h"
using namespace img;
using namespace expressions;
class TableToken;
// Converter function. Will specialize those that are possible. Return false if conversion
// would not be wise.
// I will allow conversion of any type to bool following usual rule that empty/zero = false.
// Also allow up-conversion of long to double, but no down-conversions.
// nrows tells how big to make the vector if the expression has scalar value.
template <class T>
bool ValueConverter(vector<T>& vec, const expressions::Value* val, long nrows) {return false;}
class ColumnEvaluable: public expressions::Evaluable {
public:
ColumnEvaluable(const TableData* tptr_, const string& columnName):
tptr(tptr_), colName(columnName) {}
virtual expressions::Value* evaluate() const {
const ColumnBase* col = (*tptr)[colName];
vector<bool> vb;
vector<string> vs;
vector<long> vl;
vector<double> vd;
if (col->makeVector(vb)) {
return new expressions::VectorValue<bool>(vb);
} else if (col->makeVector(vs)) {
return new expressions::VectorValue<string>(vs);
} else if (col->makeVector(vl)) {
return new expressions::VectorValue<long>(vl);
} else if (col->makeVector(vd)) {
return new expressions::VectorValue<double>(vd);
} else
throw FTableError("Could not convert FTable column " + col->name()
+ " to VectorValue for expression evaluation");
}
virtual expressions::Value* returnEmptyEvaluation() const {
const ColumnBase* col = (*tptr)[colName];
vector<bool> vb;
vector<string> vs;
vector<long> vl;
vector<double> vd;
if (col->makeVector(vb)) {
return new expressions::VectorValue<bool>();
} else if (col->makeVector(vs)) {
return new expressions::VectorValue<string>();
} else if (col->makeVector(vl)) {
return new expressions::VectorValue<long>();
} else if (col->makeVector(vd)) {
return new expressions::VectorValue<double>();
} else
throw FTableError("Could not convert FTable column " + col->name()
+ " to VectorValue for expression evaluation");
}
private:
const TableData* tptr;
const string colName;
};
class TableToken: public expressions::Token {
public:
TableToken(const TableData* tptr_, const Header* hptr_):
tptr(tptr_), hptr(hptr_) {}
const set<string>& columnNames() const {return columns;}
virtual expressions::Token* createFromString(const std::string& input,
size_t& begin, size_t& end,
bool lastTokenWasOperator) const {
Assert(end>begin);
if (input[begin]=='@') {
// Read a header keyword as a constant
string keyword = input.substr(begin+1, end-(begin+1));
Evaluable* eval=0;
{
bool val;
if (hptr->getValue(keyword, val))
eval = new ConstantEvaluable<ScalarValue<bool> >(val);
}
{
string val;
if (hptr->getValue(keyword, val))
eval = new ConstantEvaluable<ScalarValue<string> >(val);
}
{
int val;
if (hptr->getValue(keyword, val))
eval = new ConstantEvaluable<ScalarValue<long> >((long) val);
}
{
double val;
if (hptr->getValue(keyword, val))
eval = new ConstantEvaluable<ScalarValue<double> >(val);
}
if (!eval)
throw FTableError("FTable expression evaluation cannot find Header keyword "
+ keyword);
return new EvaluableToken(eval);
} else {
string colName = input.substr(begin,end-begin);
columns.insert(colName);
return new EvaluableToken( new ColumnEvaluable(tptr, colName));
}
}
private:
const TableData* tptr;
const Header* hptr;
mutable std::set<std::string> columns;
};
// Expression evaluation, convert to type T. Use Header hh to evaluate scalars.
template <class T>
void
TableData::evaluate(vector<T>& result,
const string& expression,
const Header* hh) const {
TableToken tokenReader(this, hh);
// tokenize
std::list<expressions::Token*>
tokenList = expressions::tokenize(expression, tokenReader);
// parse
std::list<expressions::Token*>::iterator b = tokenList.begin();
expressions::Evaluable* eval = expressions::parse(tokenList,
b,
tokenList.end(),
expression);
expressions::Value* val = eval->evaluate();
// If the expression is a scalar, size the result vector to
// the table length before filling in with the scalar.
if (!ValueConverter(result, val, nrows()))
throw FTableError("Non-convertible datatype result from expression");
}
// Special the conversion from Expression Value to a vector
template <>
bool ValueConverter(vector<bool>& vec, const expressions::Value* val, long nrows) {
if (auto vptr = dynamic_cast<const VectorValue<bool>*> (val) ) {
vec = vptr->values;
return true;
} else if (auto vptr = dynamic_cast<const VectorValue<string>*> (val) ) {
vec.resize(vptr->values.size());
for (int i=0; i<vec.size(); i++) vec[i] = !vptr->values[i].empty();
return true;
} else if (auto vptr = dynamic_cast<const VectorValue<long>*> (val) ) {
vec.resize(vptr->values.size());
for (int i=0; i<vec.size(); i++) vec[i] = ( vptr->values[i] != 0);
return true;
} else if (auto vptr = dynamic_cast<const VectorValue<double>*> (val) ) {
vec.resize(vptr->values.size());
for (int i=0; i<vec.size(); i++) vec[i] = ( vptr->values[i] != 0.);
return true;
} else if (auto vptr = dynamic_cast<const ScalarValue<bool>*> (val) ) {
vec.resize(nrows);
for (int i=0; i<vec.size(); i++) vec[i] = vptr->value;
return true;
} else if (auto vptr = dynamic_cast<const ScalarValue<string>*> (val) ) {
vec.resize(nrows);
for (int i=0; i<vec.size(); i++) vec[i] = !vptr->value.empty();
return true;
} else if (auto vptr = dynamic_cast<const ScalarValue<long>*> (val) ) {
vec.resize(nrows);
for (int i=0; i<vec.size(); i++) vec[i] = ( vptr->value != 0);
return true;
} else if (auto vptr = dynamic_cast<const ScalarValue<double>*> (val) ) {
vec.resize(nrows);
for (int i=0; i<vec.size(); i++) vec[i] = ( vptr->value != 0.);
return true;
} else {
return false;
}
}
template <>
bool ValueConverter(vector<string>& vec, const expressions::Value* val, long nrows) {
if (auto vptr = dynamic_cast<const VectorValue<string>*> (val) ) {
vec = vptr->values;
return true;
} else if (auto vptr = dynamic_cast<const ScalarValue<string>*> (val) ) {
vec.resize(nrows);
for (int i=0; i<vec.size(); i++) vec[i] = vptr->value;
return true;
} else {
return false;
}
}
template <>
bool ValueConverter(vector<long>& vec, const expressions::Value* val, long nrows) {
if (auto vptr = dynamic_cast<const VectorValue<long>*> (val) ) {
vec = vptr->values;
return true;
} else if (auto vptr = dynamic_cast<const ScalarValue<long>*> (val) ) {
vec.resize(nrows);
for (int i=0; i<vec.size(); i++) vec[i] = vptr->value;
return true;
} else {
return false;
}
}
template <>
bool ValueConverter(vector<double>& vec, const expressions::Value* val, long nrows) {
if (auto vptr = dynamic_cast<const VectorValue<double>*> (val) ) {
vec = vptr->values;
return true;
} else if (auto vptr = dynamic_cast<const VectorValue<long>*> (val) ) {
vec.resize(vptr->values.size());
for (int i=0; i<vec.size(); i++) vec[i] = vptr->values[i];
return true;
} else if ( auto vptr = dynamic_cast<const ScalarValue<double>*> (val) ) {
vec.resize(nrows);
for (int i=0; i<vec.size(); i++) vec[i] = vptr->value;
return true;
} else if ( auto vptr = dynamic_cast<const ScalarValue<long>*> (val) ) {
vec.resize(nrows);
for (int i=0; i<vec.size(); i++) vec[i] = vptr->value;
return true;
} else {
return false;
}
}
template
void TableData::evaluate(vector<bool>&, const string&, const Header* hh) const;
template
void TableData::evaluate(vector<double>&, const string&, const Header* hh) const;
template
void TableData::evaluate(vector<int>&, const string&, const Header* hh) const;