summaryrefslogtreecommitdiff
path: root/impl/VariableAssignment.hpp
blob: d604a74cf17e315e5ec7f20dcf910d14ed6749ea (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
#ifndef VARIABLE_ASSIGNMENT_HPP
#define VARIABLE_ASSIGNMENT_HPP

#include "Variable.hpp"
#include <ostream>

template<typename T>
struct VariableAssignment {
  VariableAssignment(unsigned int length)
    : _length(length), _assignment(new T[length]) {
    for (unsigned int i = 0; i < _length; ++i) {
      _assignment[i] = -infinity<T>();
    }
  }
  VariableAssignment(unsigned int length, const T& initial)
    : _length(length), _assignment(new T[length]) {
    for (unsigned int i = 0; i < _length; ++i) {
      _assignment[i] = initial;
    }
  }
  VariableAssignment(const VariableAssignment& other)
    : _length(other._length), _assignment(new T[other._length]) {
    for (unsigned int i = 0; i < _length; ++i) {
      _assignment[i] = other._assignment[i];
    }
  }
  ~VariableAssignment() {
    delete[] _assignment;
  }
  VariableAssignment& operator=(const VariableAssignment& other) {
    delete[] _assignment;
    _length = other._length;
    _assignment = new T[_length];
    for (unsigned int i = 0; i < _length; ++i) {
      _assignment[i] = other._assignment[i];
    }
    return *this;
  }
  const T& operator[] (const Variable<T>& x) const {
    if (x.id() >= _length) {
      throw "Array out of bounds";
    }
    return _assignment[x.id()];
  }
  T& operator[] (const Variable<T>& x) {
    if (x.id() >= _length) {
      throw "Array out of bounds";
    }
    return _assignment[x.id()];
  }

  VariableAssignment<T> expand(const VariableAssignment<T>& other) {
    return expand(other, infinity<T>());
  }
  VariableAssignment<T> expand(const VariableAssignment<T>& other, const T& value) {
    VariableAssignment<T> expansion(_length);
    for (unsigned int i = 0; i < _length; ++i) {
      if (_assignment[i] == other._assignment[i]) {
        expansion._assignment[i] = _assignment[i];
      } else {
        expansion._assignment[i] = value;
      }
    }
    return expansion;
  }

  bool operator==(const VariableAssignment& other) const {
    if (_length != other._length)
      return false;
    for (unsigned int i = 0; i < _length; ++i) {
      if (_assignment[i] != other._assignment[i]) {
        return false;
      }
    }
    return true;
  }
  bool operator!=(const VariableAssignment& other) const {
    return !(*this == other);
  }
  private:
  unsigned int _length;
  T* _assignment;
};

#endif