From ea05c9c5fa30b8822f618e861d12a09df1f8f017 Mon Sep 17 00:00:00 2001 From: Carlo Zancanaro Date: Mon, 28 May 2012 13:00:50 +1000 Subject: Fix memory error and x = max(-inf, expr) stuff. --- impl/IdMap.hpp | 81 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 impl/IdMap.hpp (limited to 'impl/IdMap.hpp') diff --git a/impl/IdMap.hpp b/impl/IdMap.hpp new file mode 100644 index 0000000..b3a87fb --- /dev/null +++ b/impl/IdMap.hpp @@ -0,0 +1,81 @@ +#ifndef ID_MAP_HPP +#define ID_MAP_HPP + +#include + +template +struct IdMap { + IdMap(unsigned int length, V initial) + : _length(length), _assignment(new V[length]) { + for (unsigned int i = 0; i < _length; ++i) { + _assignment[i] = initial; + } + } + IdMap(const IdMap& other) + : _length(other._length), _assignment(new V[other._length]) { + for (unsigned int i = 0; i < _length; ++i) { + _assignment[i] = other._assignment[i]; + } + } + virtual ~IdMap() { + delete[] _assignment; + } + virtual IdMap& operator=(const IdMap& other) { + if (_length != other._length) { + delete[] _assignment; + _length = other._length; + _assignment = new V[_length]; + } + for (unsigned int i = 0; i < _length; ++i) { + _assignment[i] = other._assignment[i]; + } + return *this; + } + virtual const V& operator[] (const T& x) const { + if (x.id() >= _length) { + throw "Array out of bounds"; + } + return _assignment[x.id()]; + } + virtual V& operator[] (const T& x) { + if (x.id() >= _length) { + throw "Array out of bounds"; + } + return _assignment[x.id()]; + } + + virtual bool operator==(const IdMap& 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; + } + virtual bool operator!=(const IdMap& other) const { + return !(*this == other); + } + + + template + friend std::ostream& operator<<(std::ostream& cout, const IdMap& rho); + + protected: + unsigned int _length; + V* _assignment; +}; + +template +std::ostream& operator<<(std::ostream& cout, const IdMap& rho) { + cout << "{"; + for (unsigned int i = 0; i < rho._length; ++i) { + if (i != 0) cout << ", "; + cout << i << ": " << rho._assignment[i]; + } + cout << "}"; + return cout; +} + +#endif -- cgit v1.2.3