From 9fd34b8cdc98ee757fc047216bd51c698cb7b82f Mon Sep 17 00:00:00 2001 From: Carlo Zancanaro Date: Wed, 2 May 2012 00:03:59 +1000 Subject: Fix up some memory leaking issues. --- impl/EquationSystem.hpp | 25 ++++++++++++++++++------- impl/Expression.hpp | 9 +++------ impl/ExpressionFactory.hpp | 12 ------------ impl/main.cpp | 42 +++++++++++++++++++++++++++--------------- 4 files changed, 48 insertions(+), 40 deletions(-) delete mode 100644 impl/ExpressionFactory.hpp 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* newVariable(const std::string&) { _vars.push_back(new Variable(_vars.size())); @@ -20,10 +23,17 @@ struct EquationSystem { return _vars.size(); } + Expression* newExpression(Operator* op, const std::vector*>& args) { + Expression* expr = new Expression(op, args); + _expressions.push_back(expr); + return expr; + } + MaxExpression* newMaxExpression(const std::vector*>& args) { MaxExpression* expr = new MaxExpression(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*& operator[] (const Variable& 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*& operator[] (const Variable& 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 VariableAssignment foreach(F op, const VariableAssignment& rho) const { VariableAssignment 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 operator() (const VariableAssignment& rho) const { VariableAssignment 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*> _vars; std::vector*> _max_expressions; std::vector*> _expressions; + std::vector*> _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 struct Expression { Expression(Operator* op, const std::vector< Expression* >& args) : _operator(op), _arguments(args) { } virtual ~Expression() { - /*delete _operator; - for (typename std::vector< Expression* >::iterator it = _arguments.begin(); - it != _arguments.end(); - ++it) { - delete *it; - }*/ + delete _operator; } virtual T operator() (const VariableAssignment& 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 -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 Min; std::vector< Expression* > empty; int main () { + { EquationSystem sys; Variable* x = sys.newVariable("x"); Variable* y = sys.newVariable("y"); Variable* 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; } -- cgit v1.2.3