diff options
Diffstat (limited to 'impl/IdMap.hpp')
-rw-r--r-- | impl/IdMap.hpp | 81 |
1 files changed, 81 insertions, 0 deletions
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 <ostream> + +template<typename T, typename V> +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<typename Q,typename Z> + friend std::ostream& operator<<(std::ostream& cout, const IdMap<Q, Z>& rho); + + protected: + unsigned int _length; + V* _assignment; +}; + +template<typename T, typename V> +std::ostream& operator<<(std::ostream& cout, const IdMap<T,V>& rho) { + cout << "{"; + for (unsigned int i = 0; i < rho._length; ++i) { + if (i != 0) cout << ", "; + cout << i << ": " << rho._assignment[i]; + } + cout << "}"; + return cout; +} + +#endif |