diff options
author | Carlo Zancanaro <carlo@carlo-laptop> | 2012-04-26 22:32:35 +1000 |
---|---|---|
committer | Carlo Zancanaro <carlo@carlo-laptop> | 2012-04-26 22:32:35 +1000 |
commit | 76a4f0fcf3a9bf54ef910cdb2c0bebea37182391 (patch) | |
tree | 86ebaa6f86b53557d92e09ea1a74d2812da01df4 /impl/Expression.hpp | |
parent | 63a10756032fa5c677787fba209706b8bf1e4bef (diff) |
A new attempt. Better strategies implementation.
Still lacking:
- Factories (to set the ids)
- Solver
- Systems of equations
Diffstat (limited to 'impl/Expression.hpp')
-rw-r--r-- | impl/Expression.hpp | 119 |
1 files changed, 26 insertions, 93 deletions
diff --git a/impl/Expression.hpp b/impl/Expression.hpp index 97dda10..46ccf23 100644 --- a/impl/Expression.hpp +++ b/impl/Expression.hpp @@ -1,111 +1,44 @@ -#ifndef EXPRESSION_H -#define EXPRESSION_H +#ifndef EXPRESSION_HPP +#define EXPRESSION_HPP -#include <ostream> -#include <map> -#include <string> -#include <vector> -template<typename T> -struct Expression; +#include "VariableAssignment.hpp" #include "Operator.hpp" template<typename T> -struct Maximum : public Operator<T> { - const T eval(const std::vector<Expression<T> >& v, const std::map<std::string, T>& m) const { - //assert(v.size() == 1); - T value = -INFINITY; - for (typename std::vector<Expression<T> >::const_iterator it = v.begin(); - it != v.end(); +struct Expression { + Expression(Operator<T>* op, const std::vector< Expression<T>* >& args) + : _operator(op), _arguments(args) { } + virtual ~Expression() { + /*delete _operator; + for (typename std::vector< Expression<T>* >::iterator it = _arguments.begin(); + it != _arguments.end(); ++it) { - const T result = it->eval(m); - value = (value < result ? result : value); - } - return value; + delete *it; + }*/ } - virtual std::string output() const { - return "max"; - } -}; - -template<typename T> -struct Minimum : public Operator<T> { - const T eval(const std::vector<Expression<T> >& v, const std::map<std::string, T>& m) const { - //assert(v.size() == 1); - T value = INFINITY; - for (typename std::vector<Expression<T> >::const_iterator it = v.begin(); - it != v.end(); - ++it) { - const T result = it->eval(m); - value = (value < result ? value : result); - } - return value; + const std::vector<Expression<T>*>& arguments() const { + return _arguments; } - virtual std::string output() const { - return "min"; + virtual T operator() (const VariableAssignment<T>& assignment) const { + return (*_operator)(_arguments, assignment); } + private: + Operator<T>* _operator; + std::vector< Expression<T>* > _arguments; }; - template<typename T> -struct Expression { - Expression(unsigned int id, Operator<T>* o, const std::vector< Expression<T> >& a) - : _id(id), _operator(o), _arguments(a) { } - virtual const T eval(std::map<std::string, T> m) const { - return _operator->eval(_arguments, m); +struct MaxExpression : public Expression<T> { + MaxExpression(const std::vector< Expression<T>* >& args) + : Expression<T>(new Maximum<T>, args) { } + unsigned int id() const { + return _id; } - - virtual Expression<T> maxStrategyEager(const std::map<std::string, T> assignment) const { - if (dynamic_cast<Maximum<T>*>(_operator) != NULL) { - T bestVal; - const Expression<T>* best = NULL; - for (typename std::vector<Expression<T> >::const_iterator it = _arguments.begin(); - it != _arguments.end(); - ++it) { - T nextVal = it->eval(assignment); - if (best == NULL || nextVal > bestVal) { - best = &(*it); - bestVal = nextVal; - } - } - return best->maxStrategyEager(assignment); - } - std::vector<Expression<T> > newArgs; - for (typename std::vector<Expression<T> >::const_iterator it = _arguments.begin(); - it != _arguments.end(); - ++it) { - newArgs.push_back(it->maxStrategyEager(assignment)); - } - return Expression(_operator, newArgs); - } - - std::string output() const { - std::string result = _operator->output() + "("; - for (typename std::vector<Expression<T> >::const_iterator it = _arguments.begin(); - it != _arguments.end(); - ++it) { - if (it != _arguments.begin()) { - result += ", "; - } - result += it->output(); - } - return result + ")"; + unsigned int id(unsigned int id) { + return (_id = id); } - - virtual ~Expression() {} private: unsigned int _id; - Operator<T>* _operator; - std::vector< Expression<T> > _arguments; - - template<typename E> - friend std::ostream& operator<<(std::ostream&, const Expression<E>&); }; -template<typename T> -std::ostream& operator<<(std::ostream& cout, const Expression<T>& e) { - cout << e._operator->output() << "(" - << ")"; - return cout; -} - #endif |