summaryrefslogtreecommitdiff
path: root/impl/Expression.hpp
blob: 01921b642995436b0cd38db6b187e12e2d3fa935 (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
#ifndef EXPRESSION_HPP
#define EXPRESSION_HPP

#include <string>
#include <vector>
#include <sstream>
#include "IdMap.hpp"
#include "Operator.hpp"

template<typename Domain>
struct VariableAssignment;

template<typename Domain>
struct MaxStrategy;

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

  virtual Domain eval(const VariableAssignment<Domain>&) const = 0;
  virtual Domain eval(const VariableAssignment<Domain>& rho,
                      const MaxStrategy<Domain>&) const {
    return eval(rho);
  }

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

template<typename Domain>
struct Constant : public Expression<Domain> {
  Constant(const Domain& value)
    : _value(value) { }

  virtual Domain eval(const VariableAssignment<Domain>&) const {
    return _value;
  }

  void print(std::ostream& cout) const {
    cout << _value << "!c";
  }

  private:
  Domain _value;
};

template<typename Domain>
struct Variable : public Expression<Domain> {
  Variable(const unsigned int& id, const std::string& name)
    : _id(id), _name(name) { }

  unsigned int id() const {
    return _id;
  }
  std::string name() const {
    return _name;
  }

  virtual Domain eval(const VariableAssignment<Domain>& rho) const {
    return rho[*this];
  }

  void print(std::ostream& cout) const {
    cout << _name << "!v";
  }

  private:
  const unsigned int _id;
  const std::string _name;
};

template<typename Domain>
struct OperatorExpression : public Expression<Domain> {
  OperatorExpression(const Operator<Domain>& op, const std::vector<Expression<Domain>*>& arguments)
    : _operator(op), _arguments(arguments) { }

  virtual Domain eval(const VariableAssignment<Domain>& rho) const {
    std::vector<Domain> argumentValues;
    for (typename std::vector<Expression<Domain>*>::const_iterator it = _arguments.begin();
         it != _arguments.end();
         ++it) {
      argumentValues.push_back((*it)->eval(rho));
    }
    return _operator.eval(argumentValues);
  }

  virtual Domain eval(const VariableAssignment<Domain>& rho, const MaxStrategy<Domain>& strat) const {
    std::vector<Domain> argumentValues;
    for (typename std::vector<Expression<Domain>*>::const_iterator it = _arguments.begin();
         it != _arguments.end();
         ++it) {
      argumentValues.push_back((*it)->eval(rho, strat));
    }
    return _operator.eval(argumentValues);
  }

  const std::vector<Expression<Domain>*>& arguments() const {
    return _arguments;
  }

  const Operator<Domain>& op() const {
    return _operator;
  }

  void print(std::ostream& cout) const {
    cout << _operator << "(";
    for (unsigned int i = 0, length = _arguments.size();
         i < length;
         ++i) {
      if (i > 0)
        cout << ", ";
      cout << *_arguments[i];
    }
    cout << ")";
  }

  private:
  const Operator<Domain>& _operator;
  protected:
  const std::vector<Expression<Domain>*> _arguments;
};

template<typename Domain>
struct MaxExpression : public OperatorExpression<Domain> {
  MaxExpression(const unsigned int& id, const Maximum<Domain>& op, const std::vector<Expression<Domain>*>& arguments)
    : OperatorExpression<Domain>(op, arguments), _id(id) { }

  virtual Domain eval(const VariableAssignment<Domain>& rho, const MaxStrategy<Domain>& strat) const {
    return this->_arguments[strat.get(*this)]->eval(rho, strat);
  }

  unsigned int bestStrategy(const VariableAssignment<Domain>& rho, const MaxStrategy<Domain>& strat) const {
    Domain bestValue = this->eval(rho, strat);
    unsigned int bestIndex = strat.get(*this);

    for (unsigned int i = 0, length = this->_arguments.size();
         i < length;
         ++i) {
      const Domain value = this->_arguments[i]->eval(rho, strat);
      if (bestValue < value) {
        bestValue = value;
        bestIndex = i;
      }
    }
    return bestIndex;
  }

  unsigned int id() const {
    return _id;
  }

  private:
  const unsigned int _id;
};

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

#include "VariableAssignment.hpp"

#endif