summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--impl/EquationSystem.hpp25
-rw-r--r--impl/Expression.hpp9
-rw-r--r--impl/ExpressionFactory.hpp12
-rw-r--r--impl/main.cpp42
4 files changed, 48 insertions, 40 deletions
diff --git a/impl/EquationSystem.hpp b/impl/EquationSystem.hpp
index adc608b..00916d0 100644
--- a/impl/EquationSystem.hpp
+++ b/impl/EquationSystem.hpp
@@ -11,6 +11,9 @@ struct EquationSystem {
for (int i = 0, size = _vars.size(); i < size; ++i) {
delete _vars[i];
}
+ for (int i = 0, size = _expressions.size(); i < size; ++i) {
+ delete _expressions[i];
+ }
}
Variable<T>* newVariable(const std::string&) {
_vars.push_back(new Variable<T>(_vars.size()));
@@ -20,10 +23,17 @@ struct EquationSystem {
return _vars.size();
}
+ Expression<T>* newExpression(Operator<T>* op, const std::vector<Expression<T>*>& args) {
+ Expression<T>* expr = new Expression<T>(op, args);
+ _expressions.push_back(expr);
+ return expr;
+ }
+
MaxExpression<T>* newMaxExpression(const std::vector<Expression<T>*>& args) {
MaxExpression<T>* expr = new MaxExpression<T>(args);
expr->id(_max_expressions.size());
_max_expressions.push_back(expr);
+ _expressions.push_back(expr);
return expr;
}
unsigned int maxCount() const {
@@ -42,23 +52,23 @@ struct EquationSystem {
}
Expression<T>*& operator[] (const Variable<T>& v) {
- if (_expressions.size() <= v.id()) {
- _expressions.resize(v.id()+1);
+ if (_right_expressions.size() <= v.id()) {
+ _right_expressions.resize(v.id()+1);
}
- return _expressions[v.id()];
+ return _right_expressions[v.id()];
}
const Expression<T>*& operator[] (const Variable<T>& v) const {
- if (_expressions.size() <= v.id()) {
+ if (_right_expressions.size() <= v.id()) {
throw "Out of range";
}
- return _expressions[v.id()];
+ return _right_expressions[v.id()];
}
template<typename F>
VariableAssignment<T> foreach(F op, const VariableAssignment<T>& rho) const {
VariableAssignment<T> result(_vars.size());
for (unsigned int i = 0, size = _vars.size(); i < size; ++i) {
- result[*_vars[i]] = op(*_expressions[i], rho);
+ result[*_vars[i]] = op(*_right_expressions[i], rho);
}
return result;
}
@@ -66,7 +76,7 @@ struct EquationSystem {
VariableAssignment<T> operator() (const VariableAssignment<T>& rho) const {
VariableAssignment<T> result(_vars.size());
for (unsigned int i = 0, size = _vars.size(); i < size; ++i) {
- result[*_vars[i]] = (*_expressions[i])(rho);
+ result[*_vars[i]] = (*_right_expressions[i])(rho);
}
return result;
}
@@ -112,6 +122,7 @@ struct EquationSystem {
std::vector<Variable<T>*> _vars;
std::vector<MaxExpression<T>*> _max_expressions;
std::vector<Expression<T>*> _expressions;
+ std::vector<Expression<T>*> _right_expressions;
};
#include "MaxStrategy.hpp"
diff --git a/impl/Expression.hpp b/impl/Expression.hpp
index 7b157c2..668f4fd 100644
--- a/impl/Expression.hpp
+++ b/impl/Expression.hpp
@@ -5,17 +5,14 @@
#include "VariableAssignment.hpp"
#include "Operator.hpp"
+int ExpressionCount;
+
template<typename T>
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) {
- delete *it;
- }*/
+ delete _operator;
}
virtual T operator() (const VariableAssignment<T>& assignment) const {
return (*_operator)(_arguments, assignment);
diff --git a/impl/ExpressionFactory.hpp b/impl/ExpressionFactory.hpp
deleted file mode 100644
index 0eb7268..0000000
--- a/impl/ExpressionFactory.hpp
+++ /dev/null
@@ -1,12 +0,0 @@
-#ifndef EXPRESSION_FACTORY_HPP
-#define EXPRESSION_FACTORY_HPP
-
-template<typename T>
-struct ExpressionFactory {
- ExpressionFactory() : _count(0) { }
-
- private:
- unsigned int _count;
-};
-
-#endif
diff --git a/impl/main.cpp b/impl/main.cpp
index db3d0f7..a0c6bea 100644
--- a/impl/main.cpp
+++ b/impl/main.cpp
@@ -4,8 +4,8 @@
#include "Operator.hpp"
#include "Expression.hpp"
-#include "EquationSystem.hpp"
#include "MaxStrategy.hpp"
+#include "EquationSystem.hpp"
using namespace std;
@@ -17,39 +17,50 @@ typedef Minimum<float> Min;
std::vector< Expression<float>* > empty;
int main () {
+ {
EquationSystem<float> sys;
Variable<float>* x = sys.newVariable("x");
Variable<float>* y = sys.newVariable("y");
Variable<float>* z = sys.newVariable("z");
Args xPlusArgs;
- xPlusArgs.push_back(new E(x, empty));
+ xPlusArgs.push_back(sys.newExpression(x, empty));
+
Args xMinArgs;
- xMinArgs.push_back(new E(new A(-1), xPlusArgs));
- xMinArgs.push_back(new E(y, empty));
+ xMinArgs.push_back(sys.newExpression(new A(-1), xPlusArgs));
+ xMinArgs.push_back(sys.newExpression(y, empty));
+
Args xMaxArgs;
- xMaxArgs.push_back(new E(new C(0), empty));
- xMaxArgs.push_back(new E(new Min(), xMinArgs));
+ xMaxArgs.push_back(sys.newExpression(new C(0), empty));
+ xMaxArgs.push_back(sys.newExpression(new Min(), xMinArgs));
+
sys[*x] = sys.newMaxExpression(xMaxArgs);
// x = max(0, min(x-1, y))
+
Args yPlusArgs;
- yPlusArgs.push_back(new E(x, empty));
+ yPlusArgs.push_back(sys.newExpression(x, empty));
+
Args yMaxArgs;
- yMaxArgs.push_back(new E(new C(0), empty));
- yMaxArgs.push_back(new E(new A(5), yPlusArgs));
- yMaxArgs.push_back(new E(x, empty));
+ yMaxArgs.push_back(sys.newExpression(new C(0), empty));
+ yMaxArgs.push_back(sys.newExpression(new A(5), yPlusArgs));
+ yMaxArgs.push_back(sys.newExpression(x, empty));
+
sys[*y] = sys.newMaxExpression(yMaxArgs);
// y = max(0, x+5, x)
+
Args zPlusArgs1;
- zPlusArgs1.push_back(new E(z, empty));
+ zPlusArgs1.push_back(sys.newExpression(z, empty));
+
Args zPlusArgs2;
- zPlusArgs2.push_back(new E(x, empty));
+ zPlusArgs2.push_back(sys.newExpression(x, empty));
+
Args zMaxArgs;
- zMaxArgs.push_back(new E(new C(0), empty));
- zMaxArgs.push_back(new E(new A(1), zPlusArgs1));
- zMaxArgs.push_back(new E(new A(0), zPlusArgs2));
+ zMaxArgs.push_back(sys.newExpression(new C(0), empty));
+ zMaxArgs.push_back(sys.newExpression(new A(1), zPlusArgs1));
+ zMaxArgs.push_back(sys.newExpression(new A(0), zPlusArgs2));
+
sys[*z] = sys.newMaxExpression(zMaxArgs);
// z = max(0, z+1, x)
@@ -63,6 +74,7 @@ int main () {
cout << "x = " << fixpoint[*x] << endl;
cout << "y = " << fixpoint[*y] << endl;
cout << "z = " << fixpoint[*z] << endl;
+ }
return 0;
}