diff options
Diffstat (limited to 'impl/Operator.hpp')
-rw-r--r-- | impl/Operator.hpp | 166 |
1 files changed, 88 insertions, 78 deletions
diff --git a/impl/Operator.hpp b/impl/Operator.hpp index 5f7cd1e..3072abe 100644 --- a/impl/Operator.hpp +++ b/impl/Operator.hpp @@ -1,116 +1,126 @@ #ifndef OPERATOR_HPP #define OPERATOR_HPP -template<typename T> -T infinity() { } -template<> -float infinity() { - return INFINITY; -} - #include <vector> -#include "IdSet.hpp" - -template<typename T> -struct Variable; - -template<typename T> -struct VariableAssignment; -template<typename T> -struct Expression; -template<typename T> +template<typename Domain> struct Operator { virtual ~Operator() { } - virtual T operator() (const std::vector< Expression<T>* >& args, const VariableAssignment<T>& rho) const = 0; + + virtual Domain eval(const std::vector<Domain>&) const = 0; + + virtual void print(std::ostream&) const = 0; }; -template<typename T> -struct Maximum : public Operator<T> { - virtual T operator() (const std::vector< Expression<T>* >& args, const VariableAssignment<T>& assignment) const { - T value = -infinity<T>(); - for (typename std::vector< Expression<T>* >::const_iterator it = args.begin(); - it != args.end(); + +template<typename Domain> +struct Maximum : public Operator<Domain> { + virtual Domain eval(const std::vector<Domain>& arguments) const { + Domain result = -infinity<Domain>(); + for (typename std::vector<Domain>::const_iterator it = arguments.begin(); + it != arguments.end(); ++it) { - T temporary = (**it)(assignment); - value = (temporary > value ? temporary : value); - //if (value == infinity<T>()) break; + result = (result < *it ? *it : result); } - return value; + return result; + } + void print(std::ostream& cout) const { + cout << "max"; } }; -template<typename T> -struct Minimum : public Operator<T> { - virtual T operator() (const std::vector< Expression<T>* >& args, const VariableAssignment<T>& assignment) const { - T value = infinity<T>(); - for (typename std::vector< Expression<T>* >::const_iterator it = args.begin(); - it != args.end(); + +template<typename Domain> +struct Minimum : public Operator<Domain> { + virtual Domain eval(const std::vector<Domain>& arguments) const { + Domain result = infinity<Domain>(); + for (typename std::vector<Domain>::const_iterator it = arguments.begin(); + it != arguments.end(); ++it) { - T temporary = (**it)(assignment); - value = (temporary < value ? temporary : value); - //if (value == -infinity<T>()) break; + result = (*it < result ? *it : result); } - return value; + return result; + } + void print(std::ostream& cout) const { + cout << "min"; } }; -template<typename T> -struct Constant : public Operator<T> { - Constant(const T& val) - : _value(val) { } - T operator() (const std::vector< Expression<T>* >& args, const VariableAssignment<T>& ass) const { - return _value; +template<typename Domain> +struct Negation : public Operator<Domain> { + virtual Domain eval(const std::vector<Domain>& arguments) const { + if (arguments.size() > 1) + throw "Too many arguments to a negation."; + return -arguments[0]; + } + void print(std::ostream& cout) const { + cout << "-"; } - private: - const T _value; }; -template<typename T> -struct Addition: public Operator<T> { - T operator() (const std::vector< Expression<T>* >& args, const VariableAssignment<T>& ass) const { - T sum = (*args[0])(ass); - for (unsigned int i = 1, size = args.size(); i < size; ++i) { - sum += (*args[i])(ass); +template<typename Domain> +struct Addition : public Operator<Domain> { + virtual Domain eval(const std::vector<Domain>& arguments) const { + Domain result = 0; + for (typename std::vector<Domain>::const_iterator it = arguments.begin(); + it != arguments.end(); + ++it) { + result += *it; } - return sum; + return result; + } + void print(std::ostream& cout) const { + cout << "add"; } }; -template<typename T> -struct Subtraction: public Operator<T> { - T operator() (const std::vector< Expression<T>* >& args, const VariableAssignment<T>& ass) const { - T sum = (*args[0])(ass); - for (unsigned int i = 1, size = args.size(); i < size; ++i) { - sum -= (*args[i])(ass); +template<typename Domain> +struct Subtraction : public Operator<Domain> { + virtual Domain eval(const std::vector<Domain>& arguments) const { + Domain result = 0; + for (typename std::vector<Domain>::const_iterator it = arguments.begin(); + it != arguments.end(); + ++it) { + if (it == arguments.begin()) + result = *it; + else + result -= *it; } - return sum; + return result; + } + void print(std::ostream& cout) const { + cout << "sub"; } }; -template<typename T> -struct Comma: public Operator<T> { - T operator() (const std::vector< Expression<T>* >& args, const VariableAssignment<T>& ass) const { - if ((*args[0])(ass) == -infinity<T>()) { - std::cout << "Comma - neg inf" << std::endl; - return -infinity<T>(); - } else { - std::cout << "Comma - finite" << std::endl; - return (*args[1])(ass); +template<typename Domain> +struct Comma : public Operator<Domain> { + virtual Domain eval(const std::vector<Domain>& arguments) const { + if (arguments[0] == -infinity<Domain>()) { + return -infinity<Domain>(); } + return arguments[1]; + } + void print(std::ostream& cout) const { + cout << "comma"; } }; -template<typename T> -struct Guard: public Operator<T> { - T operator() (const std::vector< Expression<T>* >& args, const VariableAssignment<T>& ass) const { - if ((*args[0])(ass) < (*args[1])(ass)) { - return -infinity<T>(); - } else { - return (*args[2])(ass); +template<typename Domain> +struct Guard : public Operator<Domain> { + virtual Domain eval(const std::vector<Domain>& arguments) const { + if (arguments[0] < arguments[1]) { + return -infinity<Domain>(); } + return arguments[2]; + } + void print(std::ostream& cout) const { + cout << "guard"; } }; -#include "VariableAssignment.hpp" -#include "Expression.hpp" +template<typename T> +std::ostream& operator<<(std::ostream& cout, const Operator<T>& op) { + op.print(cout); + return cout; +} #endif |