summaryrefslogtreecommitdiff
path: root/impl/Operator.hpp
blob: c9b4a060abe889f8f53199f37f17b802a12c054c (about) (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#ifndef OPERATOR_HPP
#define OPERATOR_HPP

template<typename T>
T infinity() { }
template<>
float infinity() {
  return INFINITY;
}

#include <vector>

template<typename T>
struct VariableAssignment;
template<typename T>
struct Expression;

template<typename T>
struct Operator {
  virtual T operator() (const std::vector< Expression<T>* >&, const VariableAssignment<T>&) 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();
         ++it) {
      T temporary = (**it)(assignment);
      value = (temporary < value ? value : temporary);
    }
    return value;
  }
};
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();
         ++it) {
      T temporary = (**it)(assignment);
      value = (temporary > value ? value : temporary);
    }
    return value;
  }
};

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;
  }
  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);
    }
    return sum;
  }
};

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);
    }
    return sum;
  }
};

#include "VariableAssignment.hpp"
#include "Expression.hpp"

#endif