Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma once
- #include <vector>
- using namespace std;
- template <class T>
- class MultiArrayIterator
- {
- public:
- typedef T arrayType;
- typedef typename arrayType::reference reference;
- typedef typename arrayType::valueType valueType;
- typedef MultiArrayIterator<arrayType> selfType;
- MultiArrayIterator(arrayType *mArray, size_t p)
- : multiArray(mArray), position(p)
- {
- neighbours.reserve(8);
- }
- ~MultiArrayIterator(void) {}
- reference &operator*() { return (*multiArray)[position]; }
- selfType &operator++() { ++position; return *this; };
- selfType operator++(int count) { selfType tmp(*this); ++(*this); return tmp; }
- selfType operator+(int n)
- {
- selfType tmp(*this);
- tmp.position += n;
- if(tmp.position > GetColumns() * GetRows())
- tmp.position -= GetColumns() * GetRows();
- return tmp;
- }
- selfType &operator--() { --position; return *this };
- selfType operator--(int count) { selfType tmp(*this); --(*this); return tmp; }
- bool operator==(const selfType &other) const { return position == other.position; }
- bool operator!=(const selfType &other) const { return position != other.position; }
- size_t GetPosition() { return position; }
- size_t GetXPos()
- {
- int i = position % multiArray->GetColumns();
- return i;
- }
- size_t GetYPos()
- {
- return position / multiArray->GetColumns();
- }
- size_t GetColumns() {return multiArray->GetColumns(); }
- size_t GetRows() {return multiArray->GetRows(); }
- vector<valueType> GetNeighbours()
- {
- neighbours.clear();
- int posX = GetXPos();
- int posY = GetYPos();
- neighbours.push_back(multiArray->GetValue(posX + 1, posY - 1));
- neighbours.push_back(multiArray->GetValue(posX + 1, posY + 1));
- neighbours.push_back(multiArray->GetValue(posX + 1, posY));
- neighbours.push_back(multiArray->GetValue(posX - 1, posY - 1));
- neighbours.push_back(multiArray->GetValue(posX - 1, posY + 1));
- neighbours.push_back(multiArray->GetValue(posX - 1, posY));
- neighbours.push_back(multiArray->GetValue(posX, posY - 1));
- neighbours.push_back(multiArray->GetValue(posX, posY + 1));
- return neighbours;
- }
- private:
- size_t position;
- arrayType *multiArray;
- vector<valueType> neighbours;
- };
- template <class T>
- class MultiArray
- {
- public:
- typedef T valueType;
- typedef T *pointer;
- typedef MultiArray<valueType> selfType;
- typedef const T *constPointer;
- typedef T &reference;
- typedef const T &constReference;
- typedef size_t sizeType;
- typedef MultiArrayIterator<selfType> Iterator;
- MultiArray(int columns = 1, int rows = 1)
- {
- this->rows = rows;
- this->columns = columns;
- this->data = new valueType[columns * rows];
- }
- ~MultiArray(void) {}
- Iterator Begin() { return Iterator(this, 0); }
- Iterator End() { return Iterator(this, columns * rows); }
- valueType &operator[](size_t index) { return data[index]; }
- valueType GetValue(int x, int y)
- {
- if(x < 0) x += columns;
- if(x >= columns) x %= columns;
- if(y < 0) y += rows;
- if(y >= rows) y %= rows;
- return data[x + y * columns];
- }
- int GetRows() { return rows; }
- int GetColumns() { return columns; }
- private:
- valueType *data;
- int rows;
- int columns;
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement