diff options
author | Carlo Zancanaro <carlo@carlo-laptop> | 2012-04-26 22:32:35 +1000 |
---|---|---|
committer | Carlo Zancanaro <carlo@carlo-laptop> | 2012-04-26 22:32:35 +1000 |
commit | 76a4f0fcf3a9bf54ef910cdb2c0bebea37182391 (patch) | |
tree | 86ebaa6f86b53557d92e09ea1a74d2812da01df4 /impl/MaxStrategy.hpp | |
parent | 63a10756032fa5c677787fba209706b8bf1e4bef (diff) |
A new attempt. Better strategies implementation.
Still lacking:
- Factories (to set the ids)
- Solver
- Systems of equations
Diffstat (limited to 'impl/MaxStrategy.hpp')
-rw-r--r-- | impl/MaxStrategy.hpp | 39 |
1 files changed, 39 insertions, 0 deletions
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<typename T> +struct MaxStrategy { + MaxStrategy(unsigned int length) + : _length(length), _assignment(new unsigned int[length]) { } + virtual ~MaxStrategy() { + delete[] _assignment; + } + const unsigned int& operator[] (const MaxExpression<T> x) const { + if (x.id() < 0 || x.id() >= _length) { + throw "Array out of bounds"; + } + return _assignment[x.id()]; + } + unsigned int& operator[] (const MaxExpression<T>& x) { + if (x.id() < 0 || x.id() >= _length) { + throw "Array out of bounds"; + } + return _assignment[x.id()]; + } + T operator() (const Expression<T>& expr, const VariableAssignment<T>& rho) const { + const MaxExpression<T>* max = dynamic_cast<const MaxExpression<T>*>(&expr); + if (max == NULL) { + return expr(rho); + } else { + return (*expr.arguments()[_assignment[max->id()]])(rho); + } + } + private: + unsigned int _length; + unsigned int* _assignment; +}; + +#endif |