From 76a4f0fcf3a9bf54ef910cdb2c0bebea37182391 Mon Sep 17 00:00:00 2001 From: Carlo Zancanaro Date: Thu, 26 Apr 2012 22:32:35 +1000 Subject: A new attempt. Better strategies implementation. Still lacking: - Factories (to set the ids) - Solver - Systems of equations --- impl/MaxStrategy.hpp | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 impl/MaxStrategy.hpp (limited to 'impl/MaxStrategy.hpp') diff --git a/impl/MaxStrategy.hpp b/impl/MaxStrategy.hpp new file mode 100644 index 0000000..a324359 --- /dev/null +++ b/impl/MaxStrategy.hpp @@ -0,0 +1,39 @@ +#ifndef MAX_STRATEGY_HPP +#define MAX_STRATEGY_HPP + +#include "Expression.hpp" +#include "VariableAssignment.hpp" + +template +struct MaxStrategy { + MaxStrategy(unsigned int length) + : _length(length), _assignment(new unsigned int[length]) { } + virtual ~MaxStrategy() { + delete[] _assignment; + } + const unsigned int& operator[] (const MaxExpression x) const { + if (x.id() < 0 || x.id() >= _length) { + throw "Array out of bounds"; + } + return _assignment[x.id()]; + } + unsigned int& operator[] (const MaxExpression& x) { + if (x.id() < 0 || x.id() >= _length) { + throw "Array out of bounds"; + } + return _assignment[x.id()]; + } + T operator() (const Expression& expr, const VariableAssignment& rho) const { + const MaxExpression* max = dynamic_cast*>(&expr); + if (max == NULL) { + return expr(rho); + } else { + return (*expr.arguments()[_assignment[max->id()]])(rho); + } + } + private: + unsigned int _length; + unsigned int* _assignment; +}; + +#endif -- cgit v1.2.3