summaryrefslogtreecommitdiff
path: root/impl/Operator.hpp
blob: 2c5af7568f542bf4a39a955690c8792dc08412a9 (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
#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> {
  Addition(int i) : _value(i) { }
  T operator() (const std::vector< Expression<T>* >& args, const VariableAssignment<T>& ass) const {
    return (*args[0])(ass) + _value;
  }
  private:
  int _value;
};

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

#endif