From a7c69ec12aec23036bfdb6447e7c38b001a40f3d Mon Sep 17 00:00:00 2001 From: Carlo Zancanaro Date: Mon, 15 Oct 2012 11:45:31 +1100 Subject: Fix up to build with clang (and fix another bug) - Remove exceptions - Remove dynamic casts The bug was relating to MaxStrategy not cleaning up the influence sets after propagating changes. I just added a call to .clear() and it seems fine. --- impl/Complete.hpp | 5 +---- impl/EquationSystem.hpp | 13 ++++--------- impl/Expression.hpp | 8 ++++++++ impl/IdMap.hpp | 10 ++-------- impl/MaxStrategy.hpp | 7 +++++-- impl/Operator.hpp | 3 +-- impl/VariableAssignment.hpp | 6 ++++-- impl/main.cpp | 19 +++++++++++-------- 8 files changed, 36 insertions(+), 35 deletions(-) (limited to 'impl') diff --git a/impl/Complete.hpp b/impl/Complete.hpp index 5219c9e..9acb9d0 100644 --- a/impl/Complete.hpp +++ b/impl/Complete.hpp @@ -15,10 +15,7 @@ struct Complete { : _value(value), _infinity(false) { } Complete(const T& value, const bool& infinity) : _value(value), _infinity(infinity) { - if (value == 0 && infinity == true) { - std::cout << "throw exception" << *(char*)NULL; - //throw "Zero infinity? Die die die!"; - } + assert(value != 0 || infinity == false); } Complete(const Complete& other) : _value(other._value), _infinity(other._infinity) { } diff --git a/impl/EquationSystem.hpp b/impl/EquationSystem.hpp index c0734aa..2fd24bd 100644 --- a/impl/EquationSystem.hpp +++ b/impl/EquationSystem.hpp @@ -96,17 +96,12 @@ struct EquationSystem { } Variable* varFromExpr(const Expression& expr) const { - if (_expr_to_var) { // we've indexed: - const MaxExpression* maxExpr = dynamic_cast*>(&expr); - if (maxExpr) { - return (*_expr_to_var)[*maxExpr]; - } else { - return NULL; - } + assert(_expr_to_var); // make sure we've indexed + const MaxExpression* maxExpr = expr.toMaxExpression();//dynamic_cast*>(&expr); + if (maxExpr) { + return (*_expr_to_var)[*maxExpr]; } else { - std::cout << "throw exception" << *(char*)NULL; return NULL; - //throw "Must index max expressions before attempting lookup"; } } diff --git a/impl/Expression.hpp b/impl/Expression.hpp index eedaa52..00bc9cd 100644 --- a/impl/Expression.hpp +++ b/impl/Expression.hpp @@ -23,6 +23,10 @@ template struct Expression { virtual ~Expression() { } + virtual const MaxExpression* toMaxExpression() const { + return NULL; + } + virtual Domain eval(const VariableAssignment&) const = 0; virtual Domain eval(const VariableAssignment& rho, const MaxStrategy&) const { @@ -141,6 +145,10 @@ struct MaxExpression : public OperatorExpression { MaxExpression(const unsigned int& id, const Maximum& op, const std::vector*>& arguments) : OperatorExpression(op, arguments), _id(id) { } + const MaxExpression* toMaxExpression() const { + return this; + } + virtual Domain eval(const VariableAssignment& rho, const MaxStrategy& strat) const { return this->_arguments[strat.get(*this)]->eval(rho, strat); } diff --git a/impl/IdMap.hpp b/impl/IdMap.hpp index 27c0806..ed0723e 100644 --- a/impl/IdMap.hpp +++ b/impl/IdMap.hpp @@ -32,17 +32,11 @@ struct IdMap { return *this; } virtual const V& operator[] (const T& x) const { - if (x.id() >= _length) { - std::cout << "throw exception" << *(char*)NULL; - //throw "Array out of bounds"; - } + assert(x.id() < _length); return _assignment[x.id()]; } virtual V& operator[] (const T& x) { - if (x.id() >= _length) { - std::cout << "throw exception" << *(char*)NULL; - //throw "Array out of bounds"; - } + assert(x.id() < _length); return _assignment[x.id()]; } diff --git a/impl/MaxStrategy.hpp b/impl/MaxStrategy.hpp index 667c127..f330f3b 100644 --- a/impl/MaxStrategy.hpp +++ b/impl/MaxStrategy.hpp @@ -61,6 +61,7 @@ struct DynamicMaxStrategy : public MaxStrategy { ++it) { solve(_system.maxExpression(*it)); } + _var_influence[v].clear(); } private: @@ -93,10 +94,12 @@ private: solve(_system.maxExpression(*it)); } } else { - log::strategy << indent() << x << " did not change" << std::endl; + log::strategy << indent() << x << " did not change: " + << x << " => " << *x.arguments()[val] << std::endl; } } else { - log::strategy << indent() << x << " is stable" << std::endl; + log::strategy << indent() << x << " is stable: " + << x << " => " << *x.arguments()[_values[x]] << std::endl; } } diff --git a/impl/Operator.hpp b/impl/Operator.hpp index b67591f..cf3ffed 100644 --- a/impl/Operator.hpp +++ b/impl/Operator.hpp @@ -47,8 +47,7 @@ struct Minimum : public Operator { template struct Negation : public Operator { virtual Domain eval(const std::vector& arguments) const { - if (arguments.size() > 1) - throw "Too many arguments to a negation."; + assert(arguments.size() == 1); return -arguments[0]; } void print(std::ostream& cout) const { diff --git a/impl/VariableAssignment.hpp b/impl/VariableAssignment.hpp index 69eeef2..907b5ab 100644 --- a/impl/VariableAssignment.hpp +++ b/impl/VariableAssignment.hpp @@ -68,10 +68,12 @@ private: solve(_system.variable(*it)); } } else { - log::fixpoint << indent() << x << " did not change" << std::endl; + log::fixpoint << indent() << x << " did not change: " + << x << " = " << val << std::endl; } } else { - log::fixpoint << indent() << x << " is stable" << std::endl; + log::fixpoint << indent() << x << " is stable: " + << x << " = " << _values[x] << std::endl; } } diff --git a/impl/main.cpp b/impl/main.cpp index b1d6984..6fbbda9 100644 --- a/impl/main.cpp +++ b/impl/main.cpp @@ -65,7 +65,8 @@ Expression& treeToExpression(pANTLR3_BASE_TREE node, EquationSystem& syste } else if (name == "GUARD") { op = new Guard(); } else { - throw "Parse error: Unknown operator"; + std::cout << "throw exception" << *(char*)NULL; + //throw "Parse error: Unknown operator"; } return system.expression(op, args); } @@ -75,8 +76,10 @@ template void treeToSystem(pANTLR3_BASE_TREE node, EquationSystem& system) { ANTLR3_UINT32 num = node->getChildCount(node); - if (num % 2 == 1) - throw "Big problem here."; + if (num % 2 == 1) { + std::cout << "throw exception" << *(char*)NULL; + //throw "Big problem here."; + } pANTLR3_BASE_TREE varNode; pANTLR3_BASE_TREE exprNode; @@ -122,12 +125,12 @@ int main (int argc, char* argv[]) { } } - auto input = antlr3FileStreamNew((pANTLR3_UINT8)argv[1], ANTLR3_ENC_8BIT); - auto lex = EquationSystemLexerNew(input); - auto tokens = antlr3CommonTokenStreamSourceNew(ANTLR3_SIZE_HINT, TOKENSOURCE(lex)); - auto parser = EquationSystemParserNew(tokens); + pANTLR3_INPUT_STREAM input = antlr3FileStreamNew((pANTLR3_UINT8)argv[1], ANTLR3_ENC_8BIT); + pEquationSystemLexer lex = EquationSystemLexerNew(input); + pANTLR3_COMMON_TOKEN_STREAM tokens = antlr3CommonTokenStreamSourceNew(ANTLR3_SIZE_HINT, TOKENSOURCE(lex)); + pEquationSystemParser parser = EquationSystemParserNew(tokens); - auto ret = parser -> equation_system(parser); + EquationSystemParser_equation_system_return ret = parser -> equation_system(parser); EquationSystem system; treeToSystem(ret.tree, system); -- cgit v1.2.3