#ifndef OPERATOR_HPP #define OPERATOR_HPP template T infinity() { } template<> float infinity() { return INFINITY; } #include #include "IdSet.hpp" template struct Variable; template struct VariableAssignment; template struct Expression; template struct Operator { Operator(const std::string& name) : op_name(name) { } virtual ~Operator() { } virtual T operator() (const std::vector< Expression* >& args, const VariableAssignment& rho) const = 0; const std::string op_name; }; template struct Maximum : public Operator { Maximum() : Operator("max") { } virtual T operator() (const std::vector< Expression* >& args, const VariableAssignment& assignment) const { T value = -infinity(); for (typename std::vector< Expression* >::const_iterator it = args.begin(); it != args.end(); ++it) { T temporary = (**it)(assignment); value = (value < temporary ? value : temporary); //if (value == infinity()) break; } return value; } }; template struct Minimum : public Operator { Minimum() : Operator("min") { } virtual T operator() (const std::vector< Expression* >& args, const VariableAssignment& assignment) 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 ? temporary : value); //if (value == -infinity()) break; } return value; } }; template struct Constant : public Operator { Constant(const std::string& value, const T& val) : Operator(value), _value(val) { } T operator() (const std::vector< Expression* >& args, const VariableAssignment& ass) const { return _value; } private: const T _value; }; template struct Addition: public Operator { Addition() : Operator("add") { } T operator() (const std::vector< Expression* >& args, const VariableAssignment& ass) const { T sum = (*args[0])(ass); for (unsigned int i = 1, size = args.size(); i < size; ++i) { sum += (*args[i])(ass); } return sum; } }; template struct Negate: public Operator { Negate() : Operator("neg") { } T operator() (const std::vector< Expression* >& args, const VariableAssignment& ass) const { // assert(args.size() == 1); return -(*args[0])(ass); } }; template struct Comma: public Operator { Comma() : Operator("comma") { } T operator() (const std::vector< Expression* >& args, const VariableAssignment& ass) const { if ((*args[0])(ass) == -infinity()) { std::cout << "Comma - neg inf" << std::endl; return -infinity(); } else { std::cout << "Comma - finite" << std::endl; return (*args[1])(ass); } } }; template struct Guard: public Operator { Guard() : Operator("guard") { } T operator() (const std::vector< Expression* >& args, const VariableAssignment& ass) const { if ((*args[0])(ass) < (*args[1])(ass)) { return -infinity(); } else { return (*args[2])(ass); } } }; #include "VariableAssignment.hpp" #include "Expression.hpp" #endif