summaryrefslogtreecommitdiff
path: root/Equation.h
diff options
context:
space:
mode:
authorCarlo Zancanaro <carlo@pc-4w14-0.cs.usyd.edu.au>2012-03-30 18:09:10 +1100
committerCarlo Zancanaro <carlo@pc-4w14-0.cs.usyd.edu.au>2012-03-30 18:09:10 +1100
commit83e2c574bdefe2d040cdfbe20a73380a0f0123dd (patch)
tree26bac5586a6c2cf40e837e280c3720ce13f39dbb /Equation.h
Initial commit. Basic Kleene iteration stuff.
Diffstat (limited to 'Equation.h')
-rw-r--r--Equation.h47
1 files changed, 47 insertions, 0 deletions
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 <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