summaryrefslogtreecommitdiff
path: root/impl/Operator.hpp
blob: 0c6e9df6b9773c33048302329033c41268c6599d (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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#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 Multiplication : public Operator<Domain> {
  virtual Domain eval(const std::vector<Domain>& arguments) const {
    Domain result = 1;
    for (auto it = arguments.begin(),
           end = arguments.end();
         it != end;
         ++it) {
      result *= *it;
    }
    return result;
  }
  void print(std::ostream& cout) const {
    cout << "mult";
  }
};

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 {
    Domain result = arguments[2];
    if (arguments[0] < arguments[1]) {
      result = -infinity<Domain>();
    }
    log::operators << "Guard: "
                   << arguments[0] << " >= " << arguments[1]
                   << " ? " << arguments[2]
                   << " -> " << result << std::endl;
    return result;
  }
  void print(std::ostream& cout) const {
    cout << "guard";
  }
};

/*#include "TemplateConstraintMatrix.hpp"

template<typename Domain>
struct MinimumCostFlow : public Operator<Domain> {
  MinimumCostFlow() {
  }
  Domain solve(const Domain& d) const {
  
  }
  virtual Domain eval(const std::vector<Domain>& arguments) const {
    if (arguments.size() != 1)
      throw "Incorrect number of arguments.";
    return solve(arguments[0]);
  }
  void print(std::ostream& cout) const {
    cout << "minCostFlow";
  }
private:
  TemplateConstraintMatrix& constraint; // T
  std::vector<Domain> guard; // c
  std::vector<std::vector<Domain>> multiplication; //A
  unsigned int row;
};*/

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

#endif