blob: 7d2d956b7f495a66c2fbbf3908a0ab144aceab08 (
about) (
plain)
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
|
#ifndef EXPRESSION_H
#define EXPRESSION_H
#include <map>
#include <string>
#include <vector>
template<typename T>
struct Expression;
#include "Operator.hpp"
template<typename T>
struct Maximum : public Operator<T> {
const T eval(const std::vector<Expression<T> >& v, const std::map<std::string, T>& m) const {
//assert(v.size() == 1);
T value = -INFINITY;
for (typename std::vector<Expression<T> >::const_iterator it = v.begin();
it != v.end();
++it) {
const T result = it->eval(m);
value = (value < result ? result : value);
}
return value;
}
};
template<typename T>
struct Minimum : public Operator<T> {
const T eval(const std::vector<Expression<T> >& v, const std::map<std::string, T>& m) const {
//assert(v.size() == 1);
T value = INFINITY;
for (typename std::vector<Expression<T> >::const_iterator it = v.begin();
it != v.end();
++it) {
const T result = it->eval(m);
value = (value < result ? value : result);
}
return value;
}
};
template<typename T>
struct Expression {
Expression(Operator<T>* o, const std::vector< Expression<T> >& a)
: _operator(o), _arguments(a) { }
virtual const T eval(std::map<std::string, T> m) const {
return _operator->eval(_arguments, m);
}
virtual Expression<T> maxStrategyEager(const std::map<std::string, T> assignment) const {
if (dynamic_cast<Maximum<T>*>(_operator) != NULL) {
T bestVal;
const Expression<T>* best;
for (typename std::vector<Expression<T> >::const_iterator it = _arguments.begin();
it != _arguments.end();
++it) {
T nextVal = it->eval(assignment);
if (best == NULL || nextVal > bestVal) {
best = &(*it);
bestVal = nextVal;
}
}
return best->maxStrategyEager(assignment);
}
std::vector<Expression<T> > newArgs;
for (typename std::vector<Expression<T> >::const_iterator it = _arguments.begin();
it != _arguments.end();
++it) {
newArgs.push_back(it->maxStrategyEager(assignment));
}
return Expression(_operator, newArgs);
}
virtual ~Expression() {}
private:
Operator<T>* _operator;
std::vector< Expression<T> > _arguments;
};
#endif
|