diff options
author | Carlo Zancanaro <carlo@pc-4w14-0.cs.usyd.edu.au> | 2012-05-25 13:24:37 +1000 |
---|---|---|
committer | Carlo Zancanaro <carlo@pc-4w14-0.cs.usyd.edu.au> | 2012-05-25 13:24:37 +1000 |
commit | 61f90f14af8796bbed074538882e76f1e1bf3333 (patch) | |
tree | 5d1dc744b47817a39b905418b1fc76bb828cea4a /impl/IdMap.hpp | |
parent | ee8547cf3c89c51ff10603814e6f745466bc4c79 (diff) |
Try to make some more modular Fixpoint algorithms.
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 |