summaryrefslogtreecommitdiff
path: root/impl/Operator.hpp
blob: 3072abecc9981b30a8a2955a263380981a741eaa (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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#ifndef OPERATOR_HPP
#define OPERATOR_HPP

#include <vector>

template<typename Domain>
struct Operator {
  virtual ~Operator() { }

  virtual Domain eval(const std::vector<Domain>&) const = 0;

  virtual void print(std::ostream&) const = 0;
};

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) {
      result = (result < *it ? *it : result);
    }
    return result;
  }
  void print(std::ostream& cout) const {
    cout << "max";
  }
};

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) {
      result = (*it < result ? *it : result);
    }
    return result;
  }
  void print(std::ostream& cout) const {
    cout << "min";
  }
};

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 << "-";
  }
};

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 result;
  }
  void print(std::ostream& cout) const {
    cout << "add";
  }
};

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 result;
  }
  void print(std::ostream& cout) const {
    cout << "sub";
  }
};

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 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";
  }
};

template<typename T>
std::ostream& operator<<(std::ostream& cout, const Operator<T>& op) {
  op.print(cout);
  return cout;
}

#endif