diff options
author | Robert Vollmert <rvollmert@gmx.net> | 2009-11-11 09:54:07 +0100 |
---|---|---|
committer | Robert Vollmert <rvollmert@gmx.net> | 2009-11-11 20:13:26 +0100 |
commit | 50c63fe994e6fa7b43da5c3641ebab9ffa79f941 (patch) | |
tree | fd4fd4c12f7d1a485b59bb011d375d9eb1306dc0 /crawl-ref/source/matrix.h | |
parent | f082540fd63210f0ae9dba063c79c3c0a3c20f82 (diff) | |
download | crawl-ref-50c63fe994e6fa7b43da5c3641ebab9ffa79f941.tar.gz crawl-ref-50c63fe994e6fa7b43da5c3641ebab9ffa79f941.zip |
Extract Matrix class from fixary.h.
It didn't really have anything to do with FixedArray.
Diffstat (limited to 'crawl-ref/source/matrix.h')
-rw-r--r-- | crawl-ref/source/matrix.h | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/crawl-ref/source/matrix.h b/crawl-ref/source/matrix.h new file mode 100644 index 0000000000..520711ca88 --- /dev/null +++ b/crawl-ref/source/matrix.h @@ -0,0 +1,59 @@ +/* + * File: matrix.h + * Summary: Two-dimensional array class. + */ + +#ifndef MATRIX_H +#define MATRIX_H + +template <typename Z> +class Matrix { +public: + Matrix(int width, int height, const Z &initial); + Matrix(int width, int height); + ~Matrix(); + + void init(const Z &initial); + Z &operator () (int x, int y) + { + return data[x + y * width]; + } + const Z &operator () (int x, int y) const + { + return data[x + y * width]; + } + +private: + Z *data; + int width, height, size; +}; + +template <typename Z> +Matrix<Z>::Matrix(int _width, int _height, const Z &initial) + : data(NULL), width(_width), height(_height), size(_width * _height) +{ + data = new Z [ size ]; + init(initial); +} + +template <typename Z> +Matrix<Z>::Matrix(int _width, int _height) + : data(NULL), width(_width), height(_height), size(_width * _height) +{ + data = new Z [ size ]; +} + +template <typename Z> +Matrix<Z>::~Matrix() +{ + delete [] data; +} + +template <typename Z> +void Matrix<Z>::init(const Z &initial) +{ + for (int i = 0; i < size; ++i) + data[i] = initial; +} + +#endif // MATRIX_H |