00001
00002
00003
00004
00005
00006 #ifndef MATRIX_H
00007 #define MATRIX_H
00008
00009 template <typename Z>
00010 class Matrix {
00011 public:
00012 Matrix(int width, int height, const Z &initial);
00013 Matrix(int width, int height);
00014 ~Matrix();
00015
00016 void init(const Z &initial);
00017 Z &operator () (int x, int y)
00018 {
00019 return data[x + y * mwidth];
00020 }
00021 Z &operator () (coord_def c)
00022 {
00023 return (*this)(c.x, c.y);
00024 }
00025 const Z &operator () (int x, int y) const
00026 {
00027 return data[x + y * mwidth];
00028 }
00029 const Z &operator () (coord_def c) const
00030 {
00031 return (*this)(c.x, c.y);
00032 }
00033
00034 int width() const { return mwidth; }
00035 int height() const { return mheight; }
00036
00037 private:
00038 Z *data;
00039 int mwidth, mheight, size;
00040 };
00041
00042 template <typename Z>
00043 Matrix<Z>::Matrix(int _width, int _height, const Z &initial)
00044 : data(NULL), mwidth(_width), mheight(_height), size(_width * _height)
00045 {
00046 data = new Z [ size ];
00047 init(initial);
00048 }
00049
00050 template <typename Z>
00051 Matrix<Z>::Matrix(int _width, int _height)
00052 : data(NULL), mwidth(_width), mheight(_height), size(_width * _height)
00053 {
00054 data = new Z [ size ];
00055 }
00056
00057 template <typename Z>
00058 Matrix<Z>::~Matrix()
00059 {
00060 delete [] data;
00061 }
00062
00063 template <typename Z>
00064 void Matrix<Z>::init(const Z &initial)
00065 {
00066 for (int i = 0; i < size; ++i)
00067 data[i] = initial;
00068 }
00069
00070 #endif // MATRIX_H