From fcecd0e7dc0bf103986c02e2f29fb518cd5571c5 Mon Sep 17 00:00:00 2001 From: Carlo Zancanaro Date: Thu, 3 May 2012 15:35:39 +1000 Subject: Add a parser for linear equations (Also add the antlr jar and C runtime) --- impl/EquationSystem.hpp | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) (limited to 'impl/EquationSystem.hpp') 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 + #include "Expression.hpp" template struct MaxStrategy; @@ -8,16 +10,26 @@ struct MaxStrategy; template 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* newVariable(const std::string& name) { + typename std::map*>::iterator it = _var_names.find(name); + if (it == _var_names.end()) { + Variable* newVar = new Variable(_vars.size(), name); + _vars.push_back(newVar); + _var_names[name] = newVar; + return _vars[_vars.size()-1]; + } else { + return it->second; } } - Variable* newVariable(const std::string&) { - _vars.push_back(new Variable(_vars.size())); - return _vars[_vars.size()-1]; + const std::vector*> vars() const { + return _vars; } unsigned int varCount() const { return _vars.size(); @@ -119,6 +131,7 @@ struct EquationSystem { return rho; } private: + std::map*> _var_names; std::vector*> _vars; std::vector*> _max_expressions; std::vector*> _expressions; -- cgit v1.2.3