#ifndef EXPRESSION_HPP #define EXPRESSION_HPP #include #include "VariableAssignment.hpp" #include "Operator.hpp" template struct Variable; int ExpressionCount; template struct Expression { Expression(Operator* op, const std::vector< Expression* >& args) : _operator(op), _arguments(args) { } virtual ~Expression() { if (!dynamic_cast*>(_operator)) { delete _operator; } } virtual T operator() (const VariableAssignment& assignment) const { return (*_operator)(_arguments, assignment); } virtual T operator() (const VariableAssignment& assignment, VariableSet* visited) const { return (*_operator)(_arguments, assignment, visited); } protected: Operator* _operator; std::vector< Expression* > _arguments; }; template struct MaxExpression : public Expression { MaxExpression(const std::vector< Expression* >& args) : Expression(new Maximum, args) { } unsigned int id() const { return _id; } unsigned int id(unsigned int id) { return (_id = id); } Expression* argument(unsigned int i) const { if (i >= Expression::_arguments.size()) { throw "Error"; } return Expression::_arguments[i]; } std::pair bestStrategy(const VariableAssignment& rho) const { std::pair best = std::pair(-infinity(), 0); for (unsigned int i = 0, size = Expression::_arguments.size(); i < size; ++i) { T value = (*Expression::_arguments[i])(rho); if (value > best.first) best = std::pair(value, i); } std::cerr << "Best strategy: (" << best.first << ", " << best.second << ")" << std::endl; return best; } private: unsigned int _id; }; #include "Variable.hpp" #endif