From 5f3cb0bf02ed32976370cf6c7f656d000b4d7694 Mon Sep 17 00:00:00 2001 From: Carlo Zancanaro Date: Fri, 15 Jun 2012 11:26:32 +1000 Subject: Re-write heaps of code to work better. --- impl/Expression.hpp | 143 ++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 100 insertions(+), 43 deletions(-) (limited to 'impl/Expression.hpp') diff --git a/impl/Expression.hpp b/impl/Expression.hpp index d3dfcf3..72318e4 100644 --- a/impl/Expression.hpp +++ b/impl/Expression.hpp @@ -1,68 +1,125 @@ #ifndef EXPRESSION_HPP #define EXPRESSION_HPP -#include -#include "VariableAssignment.hpp" +#include +#include +#include +#include "IdMap.hpp" #include "Operator.hpp" -template -struct Variable; -template -struct MaxStrategy; - -int ExpressionCount; +template +struct VariableAssignment; -template +template struct Expression { - Expression(Operator* op, const std::vector< Expression* >& args) - : _operator(op), _arguments(args) { } - virtual ~Expression() { - if (!dynamic_cast*>(_operator)) { - delete _operator; - } + virtual ~Expression() { } + + virtual Domain eval(const VariableAssignment&) const = 0; + + virtual void print(std::ostream&) const = 0; +}; + +template +struct Constant : public Expression { + Constant(const Domain& value) + : _value(value) { } + + virtual Domain eval(const VariableAssignment&) const { + return _value; } - virtual T operator() (const VariableAssignment& assignment) const { - return (*_operator)(_arguments, assignment); + + void print(std::ostream& cout) const { + cout << _value; } - protected: - Operator* _operator; - std::vector< Expression* > _arguments; + + private: + Domain _value; }; -template -struct MaxExpression : public Expression { - MaxExpression(const std::vector< Expression* >& args) - : Expression(new Maximum, args) { } +template +struct Variable : public Expression { + Variable(const unsigned int& id, const std::string& name) + : _id(id), _name(name) { } + unsigned int id() const { return _id; } - unsigned int id(unsigned int id) { - return (_id = id); + std::string name() const { + return _name; } - Expression* argument(unsigned int i) const { - if (i >= Expression::_arguments.size()) { - throw "Error"; + + virtual Domain eval(const VariableAssignment& rho) const { + return rho[*this]; + } + + void print(std::ostream& cout) const { + cout << name(); + } + + private: + const unsigned int _id; + const std::string _name; +}; + +template +struct OperatorExpression : public Expression { + OperatorExpression(const Operator& op, const std::vector*>& arguments) + : _operator(op), _arguments(arguments) { } + + virtual Domain eval(const VariableAssignment& rho) const { + std::vector argumentValues; + for (typename std::vector*>::const_iterator it = _arguments.begin(); + it != _arguments.end(); + ++it) { + argumentValues.push_back((*it)->eval(rho)); } - return Expression::_arguments[i]; + return _operator.eval(argumentValues); + } + + const std::vector*> arguments() const { + return _arguments; } - virtual T operator() (const VariableAssignment& assignment) const { - throw "error"; + + const Operator& op() const { + return _operator; } - 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); + + void print(std::ostream& cout) const { + cout << _operator << "("; + for (unsigned int i = 0, length = _arguments.size(); + i < length; + ++i) { + if (i > 0) + cout << ", "; + cout << *_arguments[i]; } - std::cerr << "Best strategy: (" << best.first << ", " << best.second << ")" << std::endl << std::endl; - return best; + cout << ")"; + } + + private: + const Operator& _operator; + const std::vector*> _arguments; +}; + +template +struct MaxExpression : public OperatorExpression { + MaxExpression(const unsigned int& id, const Maximum& op, const std::vector*>& arguments) + : OperatorExpression(op, arguments), _id(id) { } + + unsigned int id() const { + return _id; } + private: - unsigned int _id; + const unsigned int _id; }; -#include "Variable.hpp" -#include "MaxStrategy.hpp" +template +std::ostream& operator<<(std::ostream& cout, const Expression& exp) { + exp.print(cout); + return cout; +} + +#include "VariableAssignment.hpp" #endif -- cgit v1.2.3