diff options
Diffstat (limited to 'impl/EquationSystem.hpp')
-rw-r--r-- | impl/EquationSystem.hpp | 23 |
1 files changed, 18 insertions, 5 deletions
diff --git a/impl/EquationSystem.hpp b/impl/EquationSystem.hpp index 00916d0..ac0b3b2 100644 --- a/impl/EquationSystem.hpp +++ b/impl/EquationSystem.hpp @@ -1,6 +1,8 @@ #ifndef EQUATION_SYSTEM_HPP #define EQUATION_SYSTEM_HPP +#include <map> + #include "Expression.hpp" template<typename T> struct MaxStrategy; @@ -8,16 +10,26 @@ struct MaxStrategy; template<typename T> struct EquationSystem { virtual ~EquationSystem() { + for (int i = 0, size = _expressions.size(); i < size; ++i) { + delete _expressions[i]; + } for (int i = 0, size = _vars.size(); i < size; ++i) { delete _vars[i]; } - for (int i = 0, size = _expressions.size(); i < size; ++i) { - delete _expressions[i]; + } + Variable<T>* newVariable(const std::string& name) { + typename std::map<const std::string, Variable<T>*>::iterator it = _var_names.find(name); + if (it == _var_names.end()) { + Variable<T>* newVar = new Variable<T>(_vars.size(), name); + _vars.push_back(newVar); + _var_names[name] = newVar; + return _vars[_vars.size()-1]; + } else { + return it->second; } } - Variable<T>* newVariable(const std::string&) { - _vars.push_back(new Variable<T>(_vars.size())); - return _vars[_vars.size()-1]; + const std::vector<Variable<T>*> vars() const { + return _vars; } unsigned int varCount() const { return _vars.size(); @@ -119,6 +131,7 @@ struct EquationSystem { return rho; } private: + std::map<const std::string, Variable<T>*> _var_names; std::vector<Variable<T>*> _vars; std::vector<MaxExpression<T>*> _max_expressions; std::vector<Expression<T>*> _expressions; |