From 5d7252681da3b26845fc4e5dcf0b0e94ed9fabb1 Mon Sep 17 00:00:00 2001 From: Carlo Zancanaro Date: Fri, 6 Apr 2012 14:02:26 +1000 Subject: Move everything into impl/ and add a Makefile. --- impl/Constant.h | 22 ++++++++++++++++++++++ impl/Equation.h | 47 +++++++++++++++++++++++++++++++++++++++++++++++ impl/EquationSystem.h | 40 ++++++++++++++++++++++++++++++++++++++++ impl/Expression.h | 12 ++++++++++++ impl/Makefile | 8 ++++++++ impl/Max.h | 22 ++++++++++++++++++++++ impl/Min.h | 22 ++++++++++++++++++++++ impl/Sum.h | 22 ++++++++++++++++++++++ impl/Term.h | 1 + impl/Variable.h | 24 ++++++++++++++++++++++++ impl/main.cpp | 36 ++++++++++++++++++++++++++++++++++++ 11 files changed, 256 insertions(+) create mode 100644 impl/Constant.h create mode 100644 impl/Equation.h create mode 100644 impl/EquationSystem.h create mode 100644 impl/Expression.h create mode 100644 impl/Makefile create mode 100644 impl/Max.h create mode 100644 impl/Min.h create mode 100644 impl/Sum.h create mode 100644 impl/Term.h create mode 100644 impl/Variable.h create mode 100644 impl/main.cpp (limited to 'impl') diff --git a/impl/Constant.h b/impl/Constant.h new file mode 100644 index 0000000..bab8cd9 --- /dev/null +++ b/impl/Constant.h @@ -0,0 +1,22 @@ +#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 new file mode 100644 index 0000000..b861768 --- /dev/null +++ b/impl/Equation.h @@ -0,0 +1,47 @@ +#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 new file mode 100644 index 0000000..9a393c8 --- /dev/null +++ b/impl/EquationSystem.h @@ -0,0 +1,40 @@ +#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.h b/impl/Expression.h new file mode 100644 index 0000000..105ad28 --- /dev/null +++ b/impl/Expression.h @@ -0,0 +1,12 @@ +#ifndef EXPRESSION_H +#define EXPRESSION_H + +#include +#include + +template +struct Expression { + virtual T eval(const std::map&) const = 0; +}; + +#endif diff --git a/impl/Makefile b/impl/Makefile new file mode 100644 index 0000000..1d2eb4b --- /dev/null +++ b/impl/Makefile @@ -0,0 +1,8 @@ +CC=g++ +BUILD=build/ + +all: build-dir + $(CC) main.cpp -Wall -Werror -o $(BUILD)/main + +build-dir: + mkdir -p $(BUILD) diff --git a/impl/Max.h b/impl/Max.h new file mode 100644 index 0000000..b3b5d65 --- /dev/null +++ b/impl/Max.h @@ -0,0 +1,22 @@ +#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 new file mode 100644 index 0000000..9f17545 --- /dev/null +++ b/impl/Min.h @@ -0,0 +1,22 @@ +#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/Sum.h b/impl/Sum.h new file mode 100644 index 0000000..bf04669 --- /dev/null +++ b/impl/Sum.h @@ -0,0 +1,22 @@ +#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 new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/impl/Term.h @@ -0,0 +1 @@ + diff --git a/impl/Variable.h b/impl/Variable.h new file mode 100644 index 0000000..7433426 --- /dev/null +++ b/impl/Variable.h @@ -0,0 +1,24 @@ +#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 new file mode 100644 index 0000000..814a3ee --- /dev/null +++ b/impl/main.cpp @@ -0,0 +1,36 @@ +#include "Constant.h" +#include "Variable.h" +#include "Min.h" +#include "Max.h" +#include "Sum.h" +#include "Equation.h" +#include "EquationSystem.h" + +#include + +using namespace std; + +int main() { + std::map mappings; + mappings["x"] = 10; + + Equation e(Variable("x"), + new Max( + new Min( + new Constant(100), + new Sum( + new Variable("x"), + new Constant(100))), + new Constant(20))); + + Equation e2(Variable("x"), + new Constant(200)); + + EquationSystem es; + es.add(e); + es.add(e2); + std::map result = es.solve(mappings); + cout << result["x"] << endl; + + return 0; +} -- cgit v1.2.3