Advertisement
Guest User

Untitled

a guest
Jun 25th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.17 KB | None | 0 0
  1. #pragma once
  2. #include <vector>
  3.  
  4. using namespace std;
  5.  
  6. template <class T>
  7. class MultiArrayIterator
  8. {
  9. public:
  10.     typedef T arrayType;
  11.     typedef typename arrayType::reference reference;
  12.     typedef typename arrayType::valueType valueType;
  13.     typedef MultiArrayIterator<arrayType> selfType;
  14.  
  15.     MultiArrayIterator(arrayType *mArray, size_t p)
  16.         : multiArray(mArray), position(p)
  17.     {
  18.         neighbours.reserve(8);
  19.     }
  20.     ~MultiArrayIterator(void) {}
  21.  
  22.     reference &operator*() { return (*multiArray)[position]; }
  23.  
  24.     selfType &operator++() { ++position; return *this; };
  25.     selfType operator++(int count) { selfType tmp(*this); ++(*this); return tmp; }
  26.     selfType operator+(int n)
  27.     {
  28.       selfType tmp(*this);
  29.       tmp.position += n;
  30.       if(tmp.position > GetColumns() * GetRows())
  31.           tmp.position -= GetColumns() * GetRows();
  32.       return tmp;
  33.     }
  34.  
  35.  
  36.     selfType &operator--() { --position; return *this };
  37.     selfType operator--(int count) { selfType tmp(*this); --(*this); return tmp; }
  38.  
  39.     bool operator==(const selfType &other) const { return position == other.position; }
  40.     bool operator!=(const selfType &other) const { return position != other.position; }
  41.    
  42.     size_t GetPosition() { return position; }
  43.     size_t GetXPos()
  44.     {
  45.         int i = position % multiArray->GetColumns();
  46.         return i;
  47.     }
  48.     size_t GetYPos()
  49.     {
  50.         return position / multiArray->GetColumns();
  51.     }
  52.  
  53.     size_t GetColumns() {return multiArray->GetColumns(); }
  54.     size_t GetRows() {return multiArray->GetRows(); }
  55.  
  56.     vector<valueType> GetNeighbours()
  57.     {
  58.         neighbours.clear();
  59.  
  60.         int posX = GetXPos();
  61.         int posY = GetYPos();
  62.        
  63.         neighbours.push_back(multiArray->GetValue(posX + 1, posY - 1));
  64.         neighbours.push_back(multiArray->GetValue(posX + 1, posY + 1));
  65.         neighbours.push_back(multiArray->GetValue(posX + 1, posY));
  66.         neighbours.push_back(multiArray->GetValue(posX - 1, posY - 1));
  67.         neighbours.push_back(multiArray->GetValue(posX - 1, posY + 1));
  68.         neighbours.push_back(multiArray->GetValue(posX - 1, posY));
  69.         neighbours.push_back(multiArray->GetValue(posX, posY - 1));
  70.         neighbours.push_back(multiArray->GetValue(posX, posY + 1));
  71.        
  72.         return neighbours;
  73.     }
  74.  
  75. private:
  76.     size_t position;
  77.     arrayType *multiArray;
  78.     vector<valueType> neighbours;
  79. };
  80.  
  81. template <class T>
  82. class MultiArray
  83. {
  84. public:
  85.     typedef T valueType;
  86.     typedef T *pointer;
  87.     typedef MultiArray<valueType> selfType;
  88.     typedef const T *constPointer;
  89.     typedef T &reference;
  90.     typedef const T &constReference;
  91.     typedef size_t sizeType;
  92.  
  93.     typedef MultiArrayIterator<selfType> Iterator;
  94.  
  95.     MultiArray(int columns = 1, int rows = 1)
  96.     {
  97.         this->rows = rows;
  98.         this->columns = columns;
  99.         this->data = new valueType[columns * rows];
  100.     }
  101.  
  102.     ~MultiArray(void) {}
  103.    
  104.     Iterator Begin() { return Iterator(this, 0); }
  105.     Iterator End() { return Iterator(this, columns * rows); }
  106.    
  107.     valueType &operator[](size_t index) { return data[index]; }
  108.     valueType GetValue(int x, int y)
  109.     {
  110.         if(x < 0) x += columns;
  111.         if(x >= columns) x %= columns;
  112.         if(y < 0) y += rows;
  113.         if(y >= rows) y %= rows;
  114.         return data[x + y * columns];
  115.     }
  116.  
  117.     int GetRows() { return rows; }
  118.     int GetColumns() { return columns; }
  119.  
  120. private:
  121.     valueType *data;
  122.     int rows;
  123.     int columns;
  124. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement