Commit 98a5fb9a authored by Uwe Schulzweida's avatar Uwe Schulzweida

matrix_view.h cleanup.

parent 6de9492e
Pipeline #6885 passed with stages
in 16 minutes and 14 seconds
......@@ -3,49 +3,35 @@
// Modified code from https://github.com/pwwiur/Matrix
template <class T, int R = 0, int C = 0>
class // MatrixView
#ifdef __GNUG__
[[gnu::warn_unused]]
#endif
MatrixView
template <typename T>
class MatrixView
{
private:
T *arr;
size_t numRows, numColumns;
class Proxy
{
public:
Proxy(T *arr, int width, int x) : px_arr(arr), px_width(width), px_x(x) {}
T &operator[](int y) { return px_arr[y + px_width * px_x]; }
Proxy(T *arr, size_t columns, size_t x) : px_arr(arr), px_columns(columns), px_x(x) {}
T &operator[](size_t y) { return px_arr[y + px_columns * px_x]; }
private:
T *px_arr;
int px_width, px_x;
size_t px_columns, px_x;
};
public:
int width, height;
MatrixView(T *array, int rows = 0, int columns = 0)
MatrixView(T *array, size_t rows, size_t columns) : arr(array), numRows(rows), numColumns(columns)
{
if (rows > 0 && columns > 0)
{
height = rows;
width = columns;
}
else if (R > 0 && C > 0)
{
height = R;
width = C;
}
else
{
throw "Matrix initialization should have height and width arguments and must be greater than zero.";
}
arr = array;
if (rows == 0 || columns == 0) throw "Matrix initialization arguments rows and columns must be greater than zero.";
}
Proxy operator[](int x) { return Proxy(arr, width, x); }
Proxy operator[](size_t x) { return Proxy(arr, numColumns, x); }
};
#endif // MATRIX_VIEW_H
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment