summaryrefslogtreecommitdiff
path: root/impl/MaxStrategy.hpp
blob: 21a6420f758e1ff693f4320b13966d0ef791ae00 (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
#ifndef MAX_EXPRESSION_HPP
#define MAX_EXPRESSION_HPP

#include <ostream>
#include "Expression.hpp"
#include "EquationSystem.hpp"
#include "IdSet.hpp"

template<typename Domain>
struct MaxStrategy {
  virtual ~MaxStrategy() { }
  virtual unsigned int get(const MaxExpression<Domain>& e) const = 0;
};

template<typename Domain>
struct ConcreteMaxStrategy : public MaxStrategy<Domain> {
  ConcreteMaxStrategy(const EquationSystem<Domain>& system)
    : _system(system),
      _strategy(system.maxExpressionCount(), 0) {
  }

  unsigned int get(const MaxExpression<Domain>& e) const {
    return _strategy[e];
  }
  unsigned int set(const MaxExpression<Domain>& e, unsigned int i) {
    _strategy[e] = i;
    return i;
  }

  void print(std::ostream& cout) const {
    for (unsigned int i = 0, length = _system.maxExpressionCount();
         i < length;
         ++i) {
      auto expr = _system.maxExpression(i);
      unsigned int subExpression = get(expr);
      cout << expr
           << " --[" << subExpression << "]-> "
           << *expr.arguments()[subExpression] << std::endl;
    }
  }

  private:
  const EquationSystem<Domain>& _system;
  IdMap<MaxExpression<Domain>,unsigned int> _strategy;
};

template<typename Domain>
std::ostream& operator<<(std::ostream& cout, const ConcreteMaxStrategy<Domain>& strat) {
  strat.print(cout);
  return cout;
}

#endif