-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArray2D.hpp
More file actions
30 lines (27 loc) · 765 Bytes
/
Array2D.hpp
File metadata and controls
30 lines (27 loc) · 765 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#ifndef ARRAY2D_HPP
#define ARRAY2D_HPP
#define DEL(p) do {if (p) {delete p; p = 0;}} while(0)
#define DELARY(p) do {if(p) {delete[] p; p = 0;}} while(0)
template <class T>
class Array2D {
public:
Array2D() : mArray(0), mWidth(0), mHeight(0) {}
Array2D(int width, int height) : mArray(0) {setSize(width, height);}
void setSize(int width, int height)
{
DELARY(mArray);
mWidth = width;
mHeight = height;
mArray = new T[width * height];
}
~Array2D() {DELARY(mArray);}
T& operator()(int x, int y) {return mArray[mWidth * y + x];}
const T& operator()(int x, int y) const {return mArray[mWidth * y + x];}
int width() const {return mWidth;}
int height() const {return mHeight;}
private:
T* mArray;
int mWidth;
int mHeight;
};
#endif // ARRAY2D_HPP