From 2ee29b3426d2d79872fba35adbeb8d768983ffec Mon Sep 17 00:00:00 2001 From: Carlo Zancanaro Date: Thu, 19 Apr 2012 16:21:26 +1000 Subject: Add presentation; start a different implementation --- impl/Constant.h | 22 ---------- impl/Equation.h | 47 --------------------- impl/EquationSystem.h | 40 ------------------ impl/Expression.cpp | 13 ++++++ impl/Expression.h | 20 +++++++-- impl/Max.h | 22 ---------- impl/Min.h | 22 ---------- impl/Operator.h | 14 +++++++ impl/Sum.h | 22 ---------- impl/Term.h | 1 - impl/Variable.h | 24 ----------- impl/main.cpp | 111 ++++++++++++++++++++++++++++++++++++++------------ 12 files changed, 129 insertions(+), 229 deletions(-) delete mode 100644 impl/Constant.h delete mode 100644 impl/Equation.h delete mode 100644 impl/EquationSystem.h create mode 100644 impl/Expression.cpp delete mode 100644 impl/Max.h delete mode 100644 impl/Min.h create mode 100644 impl/Operator.h delete mode 100644 impl/Sum.h delete mode 100644 impl/Term.h delete mode 100644 impl/Variable.h (limited to 'impl') diff --git a/impl/Constant.h b/impl/Constant.h deleted file mode 100644 index bab8cd9..0000000 --- a/impl/Constant.h +++ /dev/null @@ -1,22 +0,0 @@ -#ifndef CONSTANT_H -#define CONSTANT_H - -#include "Expression.h" - -template -struct Constant : public Expression { - Constant(const T& value) : _value(value) { } - - T value() const { - return this->_value; - } - - T eval(const std::map&) const { - return _value; - } - - private: - const T _value; -}; - -#endif diff --git a/impl/Equation.h b/impl/Equation.h deleted file mode 100644 index b861768..0000000 --- a/impl/Equation.h +++ /dev/null @@ -1,47 +0,0 @@ -#ifndef EQUATION_H -#define EQUATION_H - -#include "Variable.h" -#include "Expression.h" -#include -#include - -template -struct Equation { - Equation(const Variable& var, Expression* value) - : _var(var), _value(value) { } - - Variable var() const { - return _var; - } - Variable var(const Variable& var) { - Variable old_var = _var; - _var = var; - return _var; - } - - Expression* value() const { - return _value; - } - Expression* value(const Expression* value) { - Expression* old_value = _value; - _value = value; - return old_value; - } - - std::map solve(const std::map& curr) { - std::map result = curr; - T last; - do { - last = result[_var.name()]; - result[_var.name()] = _value->eval(result); - } while (last != result[_var.name()]); - return result; - } - - private: - Variable _var; - Expression* _value; -}; - -#endif diff --git a/impl/EquationSystem.h b/impl/EquationSystem.h deleted file mode 100644 index 9a393c8..0000000 --- a/impl/EquationSystem.h +++ /dev/null @@ -1,40 +0,0 @@ -#ifndef EQUATIONSYSTEM_H -#define EQUATIONSYSTEM_H - -#include "Equation.h" -#include - -template -struct EquationSystem { - bool add(Equation e) { - for (iterator it = equations.begin(); - it != equations.end(); - ++it) { - if ((*it).var().name() == e.var().name()) { - return false; - } - } - this->equations.push_back(e); - return true; - } - - std::map solve(std::map start) { - std::map result = start; - std::map comparison; - do { - comparison = result; - for (iterator it = equations.begin(); - it != equations.end(); - ++it) { - result = it->solve(result); - } - } while (comparison != result); - return result; - } - - private: - typedef typename std::list< Equation >::iterator iterator; - std::list< Equation > equations; -}; - -#endif diff --git a/impl/Expression.cpp b/impl/Expression.cpp new file mode 100644 index 0000000..61fe3ac --- /dev/null +++ b/impl/Expression.cpp @@ -0,0 +1,13 @@ +#include "Expression.h" + +template +const T +Expression :: minvalue(std::map< std::string, T> > m) { + return _operator.eval(_arguments, m); +} + +template +const T +Expression :: maxvalue(std::map< std::string, T> > m) { + return _operator.eval(_arguments, m); +} diff --git a/impl/Expression.h b/impl/Expression.h index 105ad28..38a5e65 100644 --- a/impl/Expression.h +++ b/impl/Expression.h @@ -1,12 +1,26 @@ #ifndef EXPRESSION_H #define EXPRESSION_H -#include #include +#include +#include +template +struct Expression; +#include "Operator.h" -template +template struct Expression { - virtual T eval(const std::map&) const = 0; + Expression(Operator* o, std::vector< Expression > a) + : _operator(o), _arguments(a) { } + virtual const T minvalue(std::map m) const { + return _operator->eval(_arguments, m); + } + virtual const T maxvalue(std::map m) const { + return _operator->eval(_arguments, m); + } + private: + Operator* _operator; + std::vector< Expression > _arguments; }; #endif diff --git a/impl/Max.h b/impl/Max.h deleted file mode 100644 index b3b5d65..0000000 --- a/impl/Max.h +++ /dev/null @@ -1,22 +0,0 @@ -#ifndef MAX_H -#define MAX_H - -#include "Expression.h" - -template -struct Max : public Expression { - Max(Expression* left, Expression* right) - : _left(left), _right(right) { } - - T eval(const std::map& mappings) const { - T left = _left->eval(mappings); - T right = _right->eval(mappings); - return left < right ? right : left; - } - - private: - Expression* _left; - Expression* _right; -}; - -#endif diff --git a/impl/Min.h b/impl/Min.h deleted file mode 100644 index 9f17545..0000000 --- a/impl/Min.h +++ /dev/null @@ -1,22 +0,0 @@ -#ifndef MIN_H -#define MIN_H - -#include "Expression.h" - -template -struct Min : public Expression { - Min(Expression* left, Expression* right) - : _left(left), _right(right) { } - - T eval(const std::map& mappings) const { - T left = _left->eval(mappings); - T right = _right->eval(mappings); - return left < right ? left : right; - } - - private: - Expression* _left; - Expression* _right; -}; - -#endif diff --git a/impl/Operator.h b/impl/Operator.h new file mode 100644 index 0000000..e83d495 --- /dev/null +++ b/impl/Operator.h @@ -0,0 +1,14 @@ +#ifndef OPERATOR_H +#define OPERATOR_H + +#include +#include +#include +#include "Expression.h" + +template +struct Operator { + virtual const T eval(const std::vector< Expression >&, const std::map&) const = 0; +}; + +#endif diff --git a/impl/Sum.h b/impl/Sum.h deleted file mode 100644 index bf04669..0000000 --- a/impl/Sum.h +++ /dev/null @@ -1,22 +0,0 @@ -#ifndef SUM_H -#define SUM_H - -#include "Expression.h" - -template -struct Sum : public Expression { - Sum(Expression* left, Expression* right) - : _left(left), _right(right) { } - - T eval(const std::map& mappings) const { - T left = _left->eval(mappings); - T right = _right->eval(mappings); - return left + right; - } - - private: - Expression* _left; - Expression* _right; -}; - -#endif diff --git a/impl/Term.h b/impl/Term.h deleted file mode 100644 index 8b13789..0000000 --- a/impl/Term.h +++ /dev/null @@ -1 +0,0 @@ - diff --git a/impl/Variable.h b/impl/Variable.h deleted file mode 100644 index 7433426..0000000 --- a/impl/Variable.h +++ /dev/null @@ -1,24 +0,0 @@ -#ifndef VARIABLE_H -#define VARIABLE_H - -#include -#include "Expression.h" - -template -struct Variable : public Expression { - Variable(const std::string& name) - : _name(name) { } - - std::string name() const { - return this->_name; - } - - T eval(const std::map& values) const { - return values.find(_name)->second; - } - - private: - const std::string _name; -}; - -#endif diff --git a/impl/main.cpp b/impl/main.cpp index 814a3ee..dc6cf45 100644 --- a/impl/main.cpp +++ b/impl/main.cpp @@ -1,36 +1,95 @@ -#include "Constant.h" -#include "Variable.h" -#include "Min.h" -#include "Max.h" -#include "Sum.h" -#include "Equation.h" -#include "EquationSystem.h" - #include +#include +#include +#include "Expression.h" +#include "Operator.h" using namespace std; -int main() { - std::map mappings; - mappings["x"] = 10; +struct Variable : public Operator { + Variable(const std::string& x) + : _value(x) { } + const float eval(const vector >& v, const map& m) const { + //assert(v.size() == 0); + map::const_iterator it = m.find(_value); + return (it == m.end() ? -INFINITY : it->second); + } + private: + std::string _value; +}; + +struct Const : public Operator { + Const(const float& x) + : _value(x) { } + const float eval(const vector >& v, const map& m) const { + //assert(v.size() == 0); + return _value; + } + private: + float _value; +}; + +struct Mult : public Operator { + Mult(const float& x) + : _value(x) { } + const float eval(const vector >& v, const map& m) const { + //assert(v.size() == 1); + return _value * v[0].minvalue(m); + } + private: + float _value; +}; - Equation e(Variable("x"), - new Max( - new Min( - new Constant(100), - new Sum( - new Variable("x"), - new Constant(100))), - new Constant(20))); +struct Maximum : public Operator { + const float eval(const vector >& v, const map& m) const { + //assert(v.size() == 1); + float value = -INFINITY; + for (vector >::const_iterator it = v.begin(); + it != v.end(); + ++it) { + const float result = it->maxvalue(m); + value = (value < result ? result : value); + } + return value; + } +}; - Equation e2(Variable("x"), - new Constant(200)); +struct Minimum : public Operator { + const float eval(const vector >& v, const map& m) const { + //assert(v.size() == 1); + float value = INFINITY; + for (vector >::const_iterator it = v.begin(); + it != v.end(); + ++it) { + const float result = it->maxvalue(m); + value = (value < result ? value : result); + } + return value; + } +}; - EquationSystem es; - es.add(e); - es.add(e2); - std::map result = es.solve(mappings); - cout << result["x"] << endl; +int main () { + typedef Expression E; + typedef vector Args; + map m; + //m["x"] = -10; + Args empty; + { + Args a; + { + Args b; + b.push_back(E(new Const(10), empty)); + b.push_back(E(new Const(20), empty)); + b.push_back(E(new Variable("x"), empty)); + a.push_back(E(new Minimum(), b)); + cout << E(new Minimum(), b).minvalue(m) << endl; + } + } + /*map m; + vector > empty; + vector > args; + args.push_back(Expression(new Const(10), empty)); + cout << Expression(new Mult(3), args).minvalue(m) << endl;*/ return 0; } -- cgit v1.2.3