#ifndef MATRIX2D_H #define MATRIX2D_H template class Matrix2D { std::vector data; unsigned int sizeX, sizeY; public: unsigned int getWidth() const { return sizeX; } unsigned int getHeight() const { return sizeY; } Matrix2D (unsigned int x, unsigned int y) : sizeX (x), sizeY (y) { data.resize (sizeX*sizeY); } T& operator()( int x, int y) { if (x >= (int) sizeX ) x-= sizeX; if (x < 0 ) x+= sizeX; if (y<0) y=0; if (y >= (int) sizeY ) y =sizeY-1; //if (y>= sizeY || y<0) // throw std::out_of_range("OOB access"); // Throw something more appropriate return data[sizeX*y + x]; // Stride-aware access } T* row(unsigned int y) { if (y >= sizeY) y = sizeY - 1; return &data[sizeX * y]; } }; #endif // MATRIX2D_H