From ea05c9c5fa30b8822f618e861d12a09df1f8f017 Mon Sep 17 00:00:00 2001 From: Carlo Zancanaro Date: Mon, 28 May 2012 13:00:50 +1000 Subject: Fix memory error and x = max(-inf, expr) stuff. --- impl/EquationSystem.hpp | 46 ++++++++++++++++++++++------------------------ 1 file changed, 22 insertions(+), 24 deletions(-) (limited to 'impl/EquationSystem.hpp') diff --git a/impl/EquationSystem.hpp b/impl/EquationSystem.hpp index ac0b3b2..772ac37 100644 --- a/impl/EquationSystem.hpp +++ b/impl/EquationSystem.hpp @@ -3,7 +3,13 @@ #include +template +struct EquationSystem; + +#include "IdSet.hpp" #include "Expression.hpp" +#include "FixpointAlgorithm.hpp" + template struct MaxStrategy; @@ -31,11 +37,14 @@ struct EquationSystem { const std::vector*> vars() const { return _vars; } + const Variable* getVar(unsigned int i) const { + return _vars[i]; + } unsigned int varCount() const { return _vars.size(); } - Expression* newExpression(Operator* op, const std::vector*>& args) { + Expression* newExpression(Operator* op, const std::vector*>& args=std::vector*>()) { Expression* expr = new Expression(op, args); _expressions.push_back(expr); return expr; @@ -58,7 +67,7 @@ struct EquationSystem { MaxStrategy strategy() const { return MaxStrategy(_max_expressions.size()); } - + VariableAssignment assignment() const { return VariableAssignment(_vars.size()); } @@ -69,7 +78,7 @@ struct EquationSystem { } return _right_expressions[v.id()]; } - const Expression*& operator[] (const Variable& v) const { + const Expression* operator[] (const Variable& v) const { if (_right_expressions.size() <= v.id()) { throw "Out of range"; } @@ -95,38 +104,27 @@ struct EquationSystem { VariableAssignment maxFixpoint() const { unsigned int size = _vars.size(); - VariableAssignment result(size, infinity()); - for (unsigned int i = 0; i < size; ++i) { - result = (*this)(result); - } - result = result.expand((*this)(result), -infinity()); - for (unsigned int i = 0; i < size-1; ++i) { - result = (*this)(result); - } + VariableAssignment newResult(size, infinity()); + VariableAssignment result(0); + do { + result = newResult; + newResult = (*this)(result); + } while (newResult != result); return result; } - VariableAssignment maxFixpoint(const MaxStrategy& strat) const { - unsigned int size = _vars.size(); - VariableAssignment result(size, infinity()); - for (unsigned int i = 0; i < size; ++i) { - result = strat(*this, result); - } - result = result.expand(strat(*this, result), -infinity()); - for (unsigned int i = 0; i < size-1; ++i) { - result = strat(*this, result); - } - return result; + VariableAssignment maxFixpoint(const MaxStrategy& strat, const FixpointAlgorithm& algo) const { + return algo.maxFixpoint(strat, *this); } - VariableAssignment minFixpoint() const { + VariableAssignment minFixpoint(const FixpointAlgorithm& algo) const { VariableAssignment rho = assignment(); VariableAssignment lastRho = assignment(); MaxStrategy strat = strategy(); do { lastRho = rho; strat = strat.improve(*this, rho); - rho = maxFixpoint(strat); + rho = maxFixpoint(strat, algo); } while(lastRho != rho); return rho; } -- cgit v1.2.3