summaryrefslogtreecommitdiff
path: root/impl/Equation.h
diff options
context:
space:
mode:
authorCarlo Zancanaro <carlo@carlo-laptop>2012-04-06 14:02:26 +1000
committerCarlo Zancanaro <carlo@carlo-laptop>2012-04-06 14:02:26 +1000
commit5d7252681da3b26845fc4e5dcf0b0e94ed9fabb1 (patch)
tree20bc0a6b62390aabda0b8c446d3a2177990863f6 /impl/Equation.h
parent83e2c574bdefe2d040cdfbe20a73380a0f0123dd (diff)
Move everything into impl/ and add a Makefile.
Diffstat (limited to 'impl/Equation.h')
-rw-r--r--impl/Equation.h47
1 files changed, 47 insertions, 0 deletions
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 <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