diff options
Diffstat (limited to 'impl/Expression.hpp')
-rw-r--r-- | impl/Expression.hpp | 73 |
1 files changed, 56 insertions, 17 deletions
diff --git a/impl/Expression.hpp b/impl/Expression.hpp index cba9582..dcf7201 100644 --- a/impl/Expression.hpp +++ b/impl/Expression.hpp @@ -4,6 +4,7 @@ #include <string> #include <vector> #include <sstream> +#include <set> #include "IdMap.hpp" #include "Operator.hpp" @@ -31,15 +32,22 @@ struct Expression { return NULL; } - virtual Domain eval(const VariableAssignment<Domain>&) const = 0; - virtual Domain eval(const VariableAssignment<Domain>& rho, + virtual Domain eval(VariableAssignment<Domain>&) const = 0; + virtual Domain eval(VariableAssignment<Domain>& rho, const MaxStrategy<Domain>&) const { return eval(rho); } + virtual Domain eval(VariableAssignment<Domain>& rho, + MaxStrategy<Domain>&) const { + return eval(rho); + } virtual void mapTo(Variable<Domain>&, IdMap<MaxExpression<Domain>, Variable<Domain>*>&) const { } + virtual void subMaxExprs(std::set<const MaxExpression<Domain>*>&) const { + } + virtual void print(std::ostream&) const = 0; }; @@ -48,7 +56,7 @@ struct Constant : public Expression<Domain> { Constant(const Domain& value) : _value(value) { } - virtual Domain eval(const VariableAssignment<Domain>&) const { + virtual Domain eval(VariableAssignment<Domain>&) const { return _value; } @@ -72,7 +80,7 @@ struct Variable : public Expression<Domain> { return _name; } - virtual Domain eval(const VariableAssignment<Domain>& rho) const { + virtual Domain eval(VariableAssignment<Domain>& rho) const { return rho[*this]; } @@ -90,7 +98,7 @@ struct OperatorExpression : public Expression<Domain> { OperatorExpression(const Operator<Domain>& op, const std::vector<Expression<Domain>*>& arguments) : _operator(op), _arguments(arguments) { } - virtual Domain eval(const VariableAssignment<Domain>& rho) const { + virtual Domain eval(VariableAssignment<Domain>& rho) const { std::vector<Domain> argumentValues; for (typename std::vector<Expression<Domain>*>::const_iterator it = _arguments.begin(); it != _arguments.end(); @@ -100,7 +108,17 @@ struct OperatorExpression : public Expression<Domain> { return _operator.eval(argumentValues); } - virtual Domain eval(const VariableAssignment<Domain>& rho, const MaxStrategy<Domain>& strat) const { + virtual Domain eval(VariableAssignment<Domain>& rho, const MaxStrategy<Domain>& strat) const { + std::vector<Domain> argumentValues; + for (typename std::vector<Expression<Domain>*>::const_iterator it = _arguments.begin(); + it != _arguments.end(); + ++it) { + argumentValues.push_back((*it)->eval(rho, strat)); + } + return _operator.eval(argumentValues); + } + + virtual Domain eval(VariableAssignment<Domain>& rho, MaxStrategy<Domain>& strat) const { std::vector<Domain> argumentValues; for (typename std::vector<Expression<Domain>*>::const_iterator it = _arguments.begin(); it != _arguments.end(); @@ -130,6 +148,14 @@ struct OperatorExpression : public Expression<Domain> { } } + virtual void subMaxExprs(std::set<const MaxExpression<Domain>*>& exprs) const { + for (unsigned int i = 0, length = _arguments.size(); + i < length; + ++i) { + _arguments[i]->subMaxExprs(exprs); + } + } + void print(std::ostream& cout) const { cout << _operator << "("; for (unsigned int i = 0, length = _arguments.size(); @@ -151,7 +177,7 @@ struct OperatorExpression : public Expression<Domain> { template<typename Domain> struct MaxExpression : public OperatorExpression<Domain> { MaxExpression(const unsigned int& id, const Maximum<Domain>& op, const std::vector<Expression<Domain>*>& arguments) - : OperatorExpression<Domain>(op, arguments), _id(id) {} + : OperatorExpression<Domain>(op, arguments), _id(id) { } MaxExpression* toMaxExpression() { return this; @@ -161,29 +187,33 @@ struct MaxExpression : public OperatorExpression<Domain> { return this; } - virtual Domain eval(const VariableAssignment<Domain>& rho, const MaxStrategy<Domain>& strat) const { + virtual Domain eval(VariableAssignment<Domain>& rho, const MaxStrategy<Domain>& strat) const { + log::fixpoint << "const MaxExpression eval" << std::endl; + return this->_arguments[strat.get(*this)]->eval(rho, strat); + } + + virtual Domain eval(VariableAssignment<Domain>& rho, MaxStrategy<Domain>& strat) const { + log::strategy << "const MaxExpression eval" << std::endl; return this->_arguments[strat.get(*this)]->eval(rho, strat); } - unsigned int bestStrategy(const VariableAssignment<Domain>& rho, const MaxStrategy<Domain>& strat) const { - Domain bestValue = this->eval(rho, strat); - unsigned int bestIndex = strat.get(*this); + unsigned int bestStrategy(VariableAssignment<Domain>& rho, MaxStrategy<Domain>& strat) const { + unsigned int bestIndex = const_cast<const MaxStrategy<Domain>&>(strat).get(*this); + Domain bestValue = this->_arguments[bestIndex]->eval(rho, strat); for (unsigned int i = 0, length = this->_arguments.size(); i < length; ++i) { const Domain value = this->_arguments[i]->eval(rho, strat); - if (!value.isUnknown()) { - if (bestValue < value) { - bestValue = value; - bestIndex = i; - } + if (bestValue < value) { + bestIndex = i; + bestValue = value; } } return bestIndex; } - virtual void mapTo(Variable<Domain>& v, IdMap<MaxExpression<Domain>, Variable<Domain>*>& m) const { + virtual void mapTo(Variable<Domain>& v, IdMap<MaxExpression<Domain>,Variable<Domain>*>& m) const { m[*this] = &v; for (unsigned int i = 0, length = this->_arguments.size(); i < length; @@ -192,6 +222,15 @@ struct MaxExpression : public OperatorExpression<Domain> { } } + virtual void subMaxExprs(std::set<const MaxExpression<Domain>*>& exprs) const { + exprs.insert(this); + for (unsigned int i = 0, length = this->_arguments.size(); + i < length; + ++i) { + this->_arguments[i]->subMaxExprs(exprs); + } + } + unsigned int id() const { return _id; } |