summaryrefslogtreecommitdiff
path: root/impl/Expression.hpp
diff options
context:
space:
mode:
Diffstat (limited to 'impl/Expression.hpp')
-rw-r--r--impl/Expression.hpp80
1 files changed, 80 insertions, 0 deletions
diff --git a/impl/Expression.hpp b/impl/Expression.hpp
new file mode 100644
index 0000000..7d2d956
--- /dev/null
+++ b/impl/Expression.hpp
@@ -0,0 +1,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