diff options
author | Carlo Zancanaro <carlo@carlo-laptop> | 2012-04-19 16:21:26 +1000 |
---|---|---|
committer | Carlo Zancanaro <carlo@carlo-laptop> | 2012-04-19 16:21:26 +1000 |
commit | 2ee29b3426d2d79872fba35adbeb8d768983ffec (patch) | |
tree | c1c516816b7fa8d59bea445abd4505860be376bc /impl | |
parent | 5d7252681da3b26845fc4e5dcf0b0e94ed9fabb1 (diff) |
Add presentation; start a different implementation
Diffstat (limited to 'impl')
-rw-r--r-- | impl/Constant.h | 22 | ||||
-rw-r--r-- | impl/Equation.h | 47 | ||||
-rw-r--r-- | impl/EquationSystem.h | 40 | ||||
-rw-r--r-- | impl/Expression.cpp | 13 | ||||
-rw-r--r-- | impl/Expression.h | 20 | ||||
-rw-r--r-- | impl/Max.h | 22 | ||||
-rw-r--r-- | impl/Min.h | 22 | ||||
-rw-r--r-- | impl/Operator.h | 14 | ||||
-rw-r--r-- | impl/Sum.h | 22 | ||||
-rw-r--r-- | impl/Term.h | 1 | ||||
-rw-r--r-- | impl/Variable.h | 24 | ||||
-rw-r--r-- | impl/main.cpp | 111 |
12 files changed, 129 insertions, 229 deletions
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<class T> -struct Constant : public Expression<T> { - Constant(const T& value) : _value(value) { } - - T value() const { - return this->_value; - } - - T eval(const std::map<std::string, T>&) 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 <map> -#include <string> - -template<class T> -struct Equation { - Equation(const Variable<T>& var, Expression<T>* value) - : _var(var), _value(value) { } - - Variable<T> var() const { - return _var; - } - Variable<T> var(const Variable<T>& var) { - Variable<T> old_var = _var; - _var = var; - return _var; - } - - Expression<T>* value() const { - return _value; - } - Expression<T>* value(const Expression<T>* value) { - Expression<T>* old_value = _value; - _value = value; - return old_value; - } - - std::map<std::string, T> solve(const std::map<std::string, T>& curr) { - std::map<std::string, T> result = curr; - T last; - do { - last = result[_var.name()]; - result[_var.name()] = _value->eval(result); - } while (last != result[_var.name()]); - return result; - } - - private: - Variable<T> _var; - Expression<T>* _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 <list> - -template<class T> -struct EquationSystem { - bool add(Equation<T> 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<std::string, T> solve(std::map<std::string, T> start) { - std::map<std::string, T> result = start; - std::map<std::string, T> 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<T> >::iterator iterator; - std::list< Equation<T> > 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<typename T> +const T +Expression<T> :: minvalue(std::map< std::string, T> > m) { + return _operator.eval(_arguments, m); +} + +template<typename T> +const T +Expression<T> :: 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 <string> #include <map> +#include <string> +#include <vector> +template<typename T> +struct Expression; +#include "Operator.h" -template<class T> +template<typename T> struct Expression { - virtual T eval(const std::map<std::string, T>&) const = 0; + Expression(Operator<T>* o, std::vector< Expression<T> > a) + : _operator(o), _arguments(a) { } + virtual const T minvalue(std::map<std::string, T> m) const { + return _operator->eval(_arguments, m); + } + virtual const T maxvalue(std::map<std::string, T> m) const { + return _operator->eval(_arguments, m); + } + private: + Operator<T>* _operator; + std::vector< Expression<T> > _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<class T> -struct Max : public Expression<T> { - Max(Expression<T>* left, Expression<T>* right) - : _left(left), _right(right) { } - - T eval(const std::map<std::string, T>& mappings) const { - T left = _left->eval(mappings); - T right = _right->eval(mappings); - return left < right ? right : left; - } - - private: - Expression<T>* _left; - Expression<T>* _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<class T> -struct Min : public Expression<T> { - Min(Expression<T>* left, Expression<T>* right) - : _left(left), _right(right) { } - - T eval(const std::map<std::string, T>& mappings) const { - T left = _left->eval(mappings); - T right = _right->eval(mappings); - return left < right ? left : right; - } - - private: - Expression<T>* _left; - Expression<T>* _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 <map> +#include <string> +#include <vector> +#include "Expression.h" + +template<typename T> +struct Operator { + virtual const T eval(const std::vector< Expression<T> >&, const std::map<std::string, T>&) 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<class T> -struct Sum : public Expression<T> { - Sum(Expression<T>* left, Expression<T>* right) - : _left(left), _right(right) { } - - T eval(const std::map<std::string, T>& mappings) const { - T left = _left->eval(mappings); - T right = _right->eval(mappings); - return left + right; - } - - private: - Expression<T>* _left; - Expression<T>* _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 <string> -#include "Expression.h" - -template<class T> -struct Variable : public Expression<T> { - Variable(const std::string& name) - : _name(name) { } - - std::string name() const { - return this->_name; - } - - T eval(const std::map<std::string, T>& 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 <iostream> +#include <vector> +#include <math.h> +#include "Expression.h" +#include "Operator.h" using namespace std; -int main() { - std::map<std::string, int> mappings; - mappings["x"] = 10; +struct Variable : public Operator<float> { + Variable(const std::string& x) + : _value(x) { } + const float eval(const vector<Expression<float> >& v, const map<string, float>& m) const { + //assert(v.size() == 0); + map<string, float>::const_iterator it = m.find(_value); + return (it == m.end() ? -INFINITY : it->second); + } + private: + std::string _value; +}; + +struct Const : public Operator<float> { + Const(const float& x) + : _value(x) { } + const float eval(const vector<Expression<float> >& v, const map<string, float>& m) const { + //assert(v.size() == 0); + return _value; + } + private: + float _value; +}; + +struct Mult : public Operator<float> { + Mult(const float& x) + : _value(x) { } + const float eval(const vector<Expression<float> >& v, const map<string, float>& m) const { + //assert(v.size() == 1); + return _value * v[0].minvalue(m); + } + private: + float _value; +}; - Equation<int> e(Variable<int>("x"), - new Max<int>( - new Min<int>( - new Constant<int>(100), - new Sum<int>( - new Variable<int>("x"), - new Constant<int>(100))), - new Constant<int>(20))); +struct Maximum : public Operator<float> { + const float eval(const vector<Expression<float> >& v, const map<string, float>& m) const { + //assert(v.size() == 1); + float value = -INFINITY; + for (vector<Expression<float> >::const_iterator it = v.begin(); + it != v.end(); + ++it) { + const float result = it->maxvalue(m); + value = (value < result ? result : value); + } + return value; + } +}; - Equation<int> e2(Variable<int>("x"), - new Constant<int>(200)); +struct Minimum : public Operator<float> { + const float eval(const vector<Expression<float> >& v, const map<string, float>& m) const { + //assert(v.size() == 1); + float value = INFINITY; + for (vector<Expression<float> >::const_iterator it = v.begin(); + it != v.end(); + ++it) { + const float result = it->maxvalue(m); + value = (value < result ? value : result); + } + return value; + } +}; - EquationSystem<int> es; - es.add(e); - es.add(e2); - std::map<std::string, int> result = es.solve(mappings); - cout << result["x"] << endl; +int main () { + typedef Expression<float> E; + typedef vector<E> Args; + map<string, float> 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<string, float> m; + vector<Expression<float> > empty; + vector<Expression<float> > args; + args.push_back(Expression<float>(new Const(10), empty)); + cout << Expression<float>(new Mult(3), args).minvalue(m) << endl;*/ return 0; } |