summaryrefslogtreecommitdiff
path: root/impl/Expression.hpp
diff options
context:
space:
mode:
authorCarlo Zancanaro <carlo@carlo-laptop>2012-04-20 01:19:54 +1000
committerCarlo Zancanaro <carlo@carlo-laptop>2012-04-20 01:19:54 +1000
commita9de4273bf8377aeab7d3f76a6d8bc0c89b42f79 (patch)
treeb49e84610e194047b77620afb28f864639b3176b /impl/Expression.hpp
parent2ee29b3426d2d79872fba35adbeb8d768983ffec (diff)
Start on the max-strategy stuff. Also more BF.
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