summaryrefslogtreecommitdiff
path: root/impl/Expression.hpp
diff options
context:
space:
mode:
authorCarlo Zancanaro <carlo@pc-4w14-0.cs.usyd.edu.au>2012-06-15 16:02:55 +1000
committerCarlo Zancanaro <carlo@pc-4w14-0.cs.usyd.edu.au>2012-06-15 16:02:55 +1000
commit4769d5edec8e78225f8f67e864c3269f1d2616e4 (patch)
treeedc95613cd16d319ac68ae1a375b28c32cd5f5be /impl/Expression.hpp
parentfd6bc1887fecca5338e7d5660d56a4038c805d96 (diff)
parenta61d8b829afab13593e254fc69e260b6346939dc (diff)
Merge commit 'a61d'
Conflicts: impl/Expression.hpp impl/Operator.hpp impl/Variable.hpp impl/main.cpp
Diffstat (limited to 'impl/Expression.hpp')
-rw-r--r--impl/Expression.hpp154
1 files changed, 95 insertions, 59 deletions
diff --git a/impl/Expression.hpp b/impl/Expression.hpp
index 3c84d30..7fc4542 100644
--- a/impl/Expression.hpp
+++ b/impl/Expression.hpp
@@ -1,89 +1,125 @@
#ifndef EXPRESSION_HPP
#define EXPRESSION_HPP
-#include <iostream>
-#include "VariableAssignment.hpp"
+#include <string>
+#include <vector>
+#include <sstream>
+#include "IdMap.hpp"
#include "Operator.hpp"
-template<typename T>
-struct Variable;
-template<typename T>
-struct MaxStrategy;
-
-int ExpressionCount;
+template<typename Domain>
+struct VariableAssignment;
-template<typename T>
+template<typename Domain>
struct Expression {
- Expression(Operator<T>* op, const std::vector< Expression<T>* >& args)
- : _operator(op), _arguments(args) { }
- virtual ~Expression() {
- if (!dynamic_cast<Variable<T>*>(_operator)) {
- delete _operator;
- }
- }
- virtual T operator() (const VariableAssignment<T>& assignment) const {
- return (*_operator)(_arguments, assignment);
+ virtual ~Expression() { }
+
+ virtual Domain eval(const VariableAssignment<Domain>&) const = 0;
+
+ virtual void print(std::ostream&) const = 0;
+};
+
+template<typename Domain>
+struct Constant : public Expression<Domain> {
+ Constant(const Domain& value)
+ : _value(value) { }
+
+ virtual Domain eval(const VariableAssignment<Domain>&) const {
+ return _value;
}
- template<typename Z>
- friend std::ostream& operator<<(std::ostream&, const Expression<Z>&);
+ void print(std::ostream& cout) const {
+ cout << _value << "!c";
+ }
- protected:
- Operator<T>* _operator;
- std::vector< Expression<T>* > _arguments;
+ private:
+ Domain _value;
};
-template<typename T>
-struct MaxExpression : public Expression<T> {
- MaxExpression(const std::vector< Expression<T>* >& args)
- : Expression<T>(new Maximum<T>, args) { }
+template<typename Domain>
+struct Variable : public Expression<Domain> {
+ 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);
- }
- Expression<T>* argument(unsigned int i) const {
- if (i >= Expression<T>::_arguments.size()) {
- throw "Error";
- }
- return Expression<T>::_arguments[i];
+ std::string name() const {
+ return _name;
}
- virtual T operator() (const VariableAssignment<T>& assignment) const {
- throw "error";
+
+ virtual Domain eval(const VariableAssignment<Domain>& rho) const {
+ return rho[*this];
}
- std::pair<T, unsigned int> bestStrategy(const VariableAssignment<T>& rho, const MaxStrategy<T>& strat) const {
- std::pair<T, unsigned int> best = std::pair<T, unsigned int>(-infinity<T>(), 0);
- for (unsigned int i = 0, size = Expression<T>::_arguments.size(); i < size; ++i) {
- T value = strat(*Expression<T>::_arguments[i], rho);
- if (best.first < value)
- best = std::pair<T, unsigned int>(value, i);
- }
- std::cerr << "Best strategy: (" << best.first << ", " << best.second << ")" << std::endl;
- return best;
+
+ void print(std::ostream& cout) const {
+ cout << _name << "!v";
}
+
private:
- unsigned int _id;
+ const unsigned int _id;
+ const std::string _name;
};
-template<typename T>
-std::ostream& operator<<(std::ostream& cout, const Expression<T>& expr) {
- if (expr._arguments.size() == 0) {
- cout << expr._operator->op_name;
- } else {
- cout << expr._operator->op_name << "(";
- for (typename std::vector<Expression<T>*>::const_iterator it = expr._arguments.begin();
- it != expr._arguments.end();
+template<typename Domain>
+struct OperatorExpression : public Expression<Domain> {
+ OperatorExpression(const Operator<Domain>& op, const std::vector<Expression<Domain>*>& arguments)
+ : _operator(op), _arguments(arguments) { }
+
+ virtual Domain eval(const VariableAssignment<Domain>& rho) const {
+ std::vector<Domain> argumentValues;
+ for (typename std::vector<Expression<Domain>*>::const_iterator it = _arguments.begin();
+ it != _arguments.end();
++it) {
- cout << (it == expr._arguments.begin() ? "" : ", ");
- cout << **it;
+ argumentValues.push_back((*it)->eval(rho));
+ }
+ return _operator.eval(argumentValues);
+ }
+
+ const std::vector<Expression<Domain>*> arguments() const {
+ return _arguments;
+ }
+
+ const Operator<Domain>& op() const {
+ return _operator;
+ }
+
+ 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];
}
cout << ")";
}
+
+ private:
+ const Operator<Domain>& _operator;
+ const std::vector<Expression<Domain>*> _arguments;
+};
+
+template<typename Domain>
+struct MaxExpression : public OperatorExpression<Domain> {
+ MaxExpression(const unsigned int& id, const Maximum<Domain>& op, const std::vector<Expression<Domain>*>& arguments)
+ : OperatorExpression<Domain>(op, arguments), _id(id) { }
+
+ unsigned int id() const {
+ return _id;
+ }
+
+ private:
+ const unsigned int _id;
+};
+
+template<typename T>
+std::ostream& operator<<(std::ostream& cout, const Expression<T>& exp) {
+ exp.print(cout);
return cout;
}
-#include "Variable.hpp"
-#include "MaxStrategy.hpp"
+#include "VariableAssignment.hpp"
#endif