#ifndef EXPRESSION_HPP #define EXPRESSION_HPP #include #include "VariableAssignment.hpp" #include "Operator.hpp" template struct Variable; template struct MaxStrategy; 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); } 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]; } virtual T operator() (const VariableAssignment& assignment) const { throw "error"; } std::pair bestStrategy(const VariableAssignment& rho, const MaxStrategy& strat) const { std::pair best = std::pair(-infinity(), 0); for (unsigned int i = 0, size = Expression::_arguments.size(); i < size; ++i) { T value = strat(*Expression::_arguments[i], rho); if (value > best.first) best = std::pair(value, i); } std::cerr << "Best strategy: (" << best.first << ", " << best.second << ")" << std::endl << std::endl; return best; } private: unsigned int _id; }; #include "Variable.hpp" #include "MaxStrategy.hpp" #endif