From 83e2c574bdefe2d040cdfbe20a73380a0f0123dd Mon Sep 17 00:00:00 2001 From: Carlo Zancanaro Date: Fri, 30 Mar 2012 18:09:10 +1100 Subject: Initial commit. Basic Kleene iteration stuff. --- .gitignore | 1 + Constant.h | 22 ++++++++++++++++++++++ Equation.h | 47 +++++++++++++++++++++++++++++++++++++++++++++++ EquationSystem.h | 40 ++++++++++++++++++++++++++++++++++++++++ Expression.h | 12 ++++++++++++ Max.h | 22 ++++++++++++++++++++++ Min.h | 22 ++++++++++++++++++++++ Sum.h | 22 ++++++++++++++++++++++ Term.h | 1 + Variable.h | 24 ++++++++++++++++++++++++ main.cpp | 36 ++++++++++++++++++++++++++++++++++++ 11 files changed, 249 insertions(+) create mode 100644 .gitignore create mode 100644 Constant.h create mode 100644 Equation.h create mode 100644 EquationSystem.h create mode 100644 Expression.h create mode 100644 Max.h create mode 100644 Min.h create mode 100644 Sum.h create mode 100644 Term.h create mode 100644 Variable.h create mode 100644 main.cpp diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..7fc71cc --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/a.out diff --git a/Constant.h b/Constant.h new file mode 100644 index 0000000..bab8cd9 --- /dev/null +++ b/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/Equation.h b/Equation.h new file mode 100644 index 0000000..b861768 --- /dev/null +++ b/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/EquationSystem.h b/EquationSystem.h new file mode 100644 index 0000000..9a393c8 --- /dev/null +++ b/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/Expression.h b/Expression.h new file mode 100644 index 0000000..105ad28 --- /dev/null +++ b/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/Max.h b/Max.h new file mode 100644 index 0000000..b3b5d65 --- /dev/null +++ b/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/Min.h b/Min.h new file mode 100644 index 0000000..9f17545 --- /dev/null +++ b/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/Sum.h b/Sum.h new file mode 100644 index 0000000..bf04669 --- /dev/null +++ b/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/Term.h b/Term.h new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/Term.h @@ -0,0 +1 @@ + diff --git a/Variable.h b/Variable.h new file mode 100644 index 0000000..7433426 --- /dev/null +++ b/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/main.cpp b/main.cpp new file mode 100644 index 0000000..814a3ee --- /dev/null +++ b/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