From 63a10756032fa5c677787fba209706b8bf1e4bef Mon Sep 17 00:00:00 2001 From: Carlo Zancanaro Date: Thu, 26 Apr 2012 16:15:17 +1000 Subject: A bunch of modifications working to a good solver. --- impl/Expression.hpp | 37 ++++++++++++++++++++++++++++++++++--- 1 file changed, 34 insertions(+), 3 deletions(-) (limited to 'impl/Expression.hpp') diff --git a/impl/Expression.hpp b/impl/Expression.hpp index 7d2d956..97dda10 100644 --- a/impl/Expression.hpp +++ b/impl/Expression.hpp @@ -1,6 +1,7 @@ #ifndef EXPRESSION_H #define EXPRESSION_H +#include #include #include #include @@ -21,6 +22,9 @@ struct Maximum : public Operator { } return value; } + virtual std::string output() const { + return "max"; + } }; template @@ -36,13 +40,16 @@ struct Minimum : public Operator { } return value; } + virtual std::string output() const { + return "min"; + } }; template struct Expression { - Expression(Operator* o, const std::vector< Expression >& a) - : _operator(o), _arguments(a) { } + Expression(unsigned int id, Operator* o, const std::vector< Expression >& a) + : _id(id), _operator(o), _arguments(a) { } virtual const T eval(std::map m) const { return _operator->eval(_arguments, m); } @@ -50,7 +57,7 @@ struct Expression { virtual Expression maxStrategyEager(const std::map assignment) const { if (dynamic_cast*>(_operator) != NULL) { T bestVal; - const Expression* best; + const Expression* best = NULL; for (typename std::vector >::const_iterator it = _arguments.begin(); it != _arguments.end(); ++it) { @@ -71,10 +78,34 @@ struct Expression { return Expression(_operator, newArgs); } + std::string output() const { + std::string result = _operator->output() + "("; + for (typename std::vector >::const_iterator it = _arguments.begin(); + it != _arguments.end(); + ++it) { + if (it != _arguments.begin()) { + result += ", "; + } + result += it->output(); + } + return result + ")"; + } + virtual ~Expression() {} private: + unsigned int _id; Operator* _operator; std::vector< Expression > _arguments; + + template + friend std::ostream& operator<<(std::ostream&, const Expression&); }; +template +std::ostream& operator<<(std::ostream& cout, const Expression& e) { + cout << e._operator->output() << "(" + << ")"; + return cout; +} + #endif -- cgit v1.2.3