#ifndef OPERATOR_HPP #define OPERATOR_HPP template T infinity() { } template<> float infinity() { return INFINITY; } #include #include "IdSet.hpp" template struct Variable; template struct VariableSet : public IdSet > {}; template struct VariableAssignment; template struct Expression; template struct Operator { 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, VariableSet* visited) const { T value = -infinity(); for (typename std::vector< Expression* >::const_iterator it = args.begin(); it != args.end(); ++it) { T temporary = (**it)(assignment, visited); value = (temporary < value ? value : temporary); } return value; } }; template struct Minimum : public Operator { 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, visited); value = (temporary < value ? temporary : value); } return value; } }; template struct Constant : public Operator { Constant(const T& val) : _value(val) { } T operator() (const std::vector< Expression* >& args, const VariableAssignment& ass, VariableSet*) const { return _value; } private: const T _value; }; template struct Addition: public Operator { 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, visited); } return sum; } }; template struct Subtraction: public Operator { 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, 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" #endif