diff options
| author | Carlo Zancanaro <carlo@carlo-laptop> | 2012-04-26 16:15:17 +1000 | 
|---|---|---|
| committer | Carlo Zancanaro <carlo@carlo-laptop> | 2012-04-26 16:15:17 +1000 | 
| commit | 63a10756032fa5c677787fba209706b8bf1e4bef (patch) | |
| tree | 705ee15011d9aaeec3b921c3f808427c8aa00589 /impl/Expression.hpp | |
| parent | a9de4273bf8377aeab7d3f76a6d8bc0c89b42f79 (diff) | |
A bunch of modifications working to a good solver.
Diffstat (limited to 'impl/Expression.hpp')
| -rw-r--r-- | impl/Expression.hpp | 37 | 
1 files changed, 34 insertions, 3 deletions
| 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 <ostream>  #include <map>  #include <string>  #include <vector> @@ -21,6 +22,9 @@ struct Maximum : public Operator<T> {      }      return value;    } +  virtual std::string output() const { +    return "max"; +  }  };  template<typename T> @@ -36,13 +40,16 @@ struct Minimum : public Operator<T> {      }      return value;    } +  virtual std::string output() const { +    return "min"; +  }  };  template<typename T>  struct Expression { -  Expression(Operator<T>* o, const std::vector< Expression<T> >& a) -    : _operator(o), _arguments(a) { } +  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);    } @@ -50,7 +57,7 @@ struct Expression {    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; +      const Expression<T>* best = NULL;        for (typename std::vector<Expression<T> >::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<Expression<T> >::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<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 | 
