#ifndef VARIABLE_ASSIGNMENT_HPP #define VARIABLE_ASSIGNMENT_HPP #include "Variable.hpp" template struct VariableAssignment { VariableAssignment(unsigned int length) : _length(length), _assignment(new T[length]) { for (unsigned int i = 0; i < _length; ++i) { _assignment[i] = -infinity(); } } 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& x) const { if (x.id() >= _length) { throw "Array out of bounds"; } return _assignment[x.id()]; } T& operator[] (const Variable& x) { if (x.id() >= _length) { throw "Array out of bounds"; } return _assignment[x.id()]; } VariableAssignment expand(const VariableAssignment& other) { return expand(other, infinity()); } VariableAssignment expand(const VariableAssignment& other, const T& value) { VariableAssignment 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