diff options
author | Carlo Zancanaro <carlo@pc-4w14-0.cs.usyd.edu.au> | 2012-04-30 21:17:07 +1000 |
---|---|---|
committer | Carlo Zancanaro <carlo@pc-4w14-0.cs.usyd.edu.au> | 2012-04-30 21:17:07 +1000 |
commit | e00c1e3f71bb1840e10a85af53469a81c73c7ef1 (patch) | |
tree | 8b24e7be29b364a5a17bdbaabeb3115261bba783 /impl/EquationSystem.hpp | |
parent | 2c22cee1f8fa87c527449a8bdc668ea311fdaf64 (diff) |
Functional algorithm. Unoptimised.
Diffstat (limited to 'impl/EquationSystem.hpp')
-rw-r--r-- | impl/EquationSystem.hpp | 58 |
1 files changed, 49 insertions, 9 deletions
diff --git a/impl/EquationSystem.hpp b/impl/EquationSystem.hpp index 065a1a2..adc608b 100644 --- a/impl/EquationSystem.hpp +++ b/impl/EquationSystem.hpp @@ -2,11 +2,11 @@ #define EQUATION_SYSTEM_HPP #include "Expression.hpp" +template<typename T> +struct MaxStrategy; template<typename T> struct EquationSystem { - EquationSystem () - : _max_id(0) { } virtual ~EquationSystem() { for (int i = 0, size = _vars.size(); i < size; ++i) { delete _vars[i]; @@ -19,13 +19,26 @@ struct EquationSystem { unsigned int varCount() const { return _vars.size(); } + MaxExpression<T>* newMaxExpression(const std::vector<Expression<T>*>& args) { MaxExpression<T>* expr = new MaxExpression<T>(args); - expr->id(_max_id++); + expr->id(_max_expressions.size()); + _max_expressions.push_back(expr); return expr; } unsigned int maxCount() const { - return _max_id; + return _max_expressions.count(); + } + const MaxExpression<T>* getMax(unsigned int i) const { + return _max_expressions[i]; + } + + MaxStrategy<T> strategy() const { + return MaxStrategy<T>(_max_expressions.size()); + } + + VariableAssignment<T> assignment() const { + return VariableAssignment<T>(_vars.size()); } Expression<T>*& operator[] (const Variable<T>& v) { @@ -42,14 +55,15 @@ struct EquationSystem { } template<typename F> - VariableAssignment<T> foreach(F op, const VariableAssignment<T>& v) { + 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], v); + result[*_vars[i]] = op(*_expressions[i], rho); } + return result; } - VariableAssignment<T> operator() (const VariableAssignment<T>& rho) { + 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); @@ -57,7 +71,7 @@ struct EquationSystem { return result; } - VariableAssignment<T> maxFixpoint() { + VariableAssignment<T> maxFixpoint() const { unsigned int size = _vars.size(); VariableAssignment<T> result(size, infinity<T>()); for (unsigned int i = 0; i < size; ++i) { @@ -69,11 +83,37 @@ struct EquationSystem { } return result; } + + VariableAssignment<T> maxFixpoint(const MaxStrategy<T>& strat) const { + unsigned int size = _vars.size(); + VariableAssignment<T> result(size, infinity<T>()); + for (unsigned int i = 0; i < size; ++i) { + result = strat(*this, result); + } + result = result.expand(strat(*this, result), -infinity<T>()); + for (unsigned int i = 0; i < size-1; ++i) { + result = strat(*this, result); + } + return result; + } + + VariableAssignment<T> minFixpoint() const { + VariableAssignment<T> rho = assignment(); + VariableAssignment<T> lastRho = assignment(); + MaxStrategy<T> strat = strategy(); + do { + lastRho = rho; + strat = strat.improve(*this, rho); + rho = maxFixpoint(strat); + } while(lastRho != rho); + return rho; + } private: - unsigned int _max_id; std::vector<Variable<T>*> _vars; + std::vector<MaxExpression<T>*> _max_expressions; std::vector<Expression<T>*> _expressions; }; +#include "MaxStrategy.hpp" #endif |