From 61f90f14af8796bbed074538882e76f1e1bf3333 Mon Sep 17 00:00:00 2001 From: Carlo Zancanaro Date: Fri, 25 May 2012 13:24:37 +1000 Subject: Try to make some more modular Fixpoint algorithms. --- impl/Operator.hpp | 56 ++++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 45 insertions(+), 11 deletions(-) (limited to 'impl/Operator.hpp') diff --git a/impl/Operator.hpp b/impl/Operator.hpp index c9b4a06..dee7f47 100644 --- a/impl/Operator.hpp +++ b/impl/Operator.hpp @@ -9,6 +9,12 @@ float infinity() { } #include +#include "IdSet.hpp" + +template +struct Variable; +template +struct VariableSet : public IdSet > {}; template struct VariableAssignment; @@ -17,16 +23,20 @@ struct Expression; template struct Operator { - virtual T operator() (const std::vector< Expression* >&, const VariableAssignment&) const = 0; + virtual ~Operator() { } + T operator() (const std::vector< Expression* >& args, const VariableAssignment& rho) const { + return (*this)(args, rho, (VariableSet*) NULL); + } + virtual T operator() (const std::vector< Expression* >&, const VariableAssignment&, VariableSet*) const = 0; }; template struct Maximum : public Operator { - virtual T operator() (const std::vector< Expression* >& args, const VariableAssignment& assignment) const { + virtual T operator() (const std::vector< Expression* >& args, const VariableAssignment& assignment, VariableSet* visited) const { T value = -infinity(); for (typename std::vector< Expression* >::const_iterator it = args.begin(); it != args.end(); ++it) { - T temporary = (**it)(assignment); + T temporary = (**it)(assignment, visited); value = (temporary < value ? value : temporary); } return value; @@ -34,13 +44,13 @@ struct Maximum : public Operator { }; template struct Minimum : public Operator { - virtual T operator() (const std::vector< Expression* >& args, const VariableAssignment& assignment) const { + virtual T operator() (const std::vector< Expression* >& args, const VariableAssignment& assignment, VariableSet* visited) const { T value = infinity(); for (typename std::vector< Expression* >::const_iterator it = args.begin(); it != args.end(); ++it) { - T temporary = (**it)(assignment); - value = (temporary > value ? value : temporary); + T temporary = (**it)(assignment, visited); + value = (temporary < value ? temporary : value); } return value; } @@ -50,7 +60,7 @@ template struct Constant : public Operator { Constant(const T& val) : _value(val) { } - T operator() (const std::vector< Expression* >& args, const VariableAssignment& ass) const { + T operator() (const std::vector< Expression* >& args, const VariableAssignment& ass, VariableSet*) const { return _value; } private: @@ -59,10 +69,10 @@ struct Constant : public Operator { template struct Addition: public Operator { - T operator() (const std::vector< Expression* >& args, const VariableAssignment& ass) const { + T operator() (const std::vector< Expression* >& args, const VariableAssignment& ass, VariableSet* visited) const { T sum = (*args[0])(ass); for (unsigned int i = 1, size = args.size(); i < size; ++i) { - sum += (*args[i])(ass); + sum += (*args[i])(ass, visited); } return sum; } @@ -70,15 +80,39 @@ struct Addition: public Operator { template struct Subtraction: public Operator { - T operator() (const std::vector< Expression* >& args, const VariableAssignment& ass) const { + T operator() (const std::vector< Expression* >& args, const VariableAssignment& ass, VariableSet* visited) const { T sum = (*args[0])(ass); for (unsigned int i = 1, size = args.size(); i < size; ++i) { - sum -= (*args[i])(ass); + sum -= (*args[i])(ass, visited); } return sum; } }; +template +struct Comma: public Operator { + T operator() (const std::vector< Expression* >& args, const VariableAssignment& ass, VariableSet* visited) const { + if ((*args[0])(ass, visited) == -infinity()) { + std::cout << "Comma - neg inf" << std::endl; + return -infinity(); + } else { + std::cout << "Comma - finite" << std::endl; + return (*args[1])(ass, visited); + } + } +}; + +template +struct Guard: public Operator { + T operator() (const std::vector< Expression* >& args, const VariableAssignment& ass, VariableSet* visited) const { + if ((*args[0])(ass, visited) < (*args[1])(ass, visited)) { + return -infinity(); + } else { + return (*args[2])(ass, visited); + } + } +}; + #include "VariableAssignment.hpp" #include "Expression.hpp" -- cgit v1.2.3