-
Notifications
You must be signed in to change notification settings - Fork 0
/
grid.h
242 lines (225 loc) · 6.6 KB
/
grid.h
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/***********************************************************************************
grid.h provides a simple class of two-dimensional array "grid".
By: Mohammed Maher Abdelrahim Mohammed
SUDAN UNIVERSITY OF SCIENCE & TECHNOLOGY
maherali8932@gmail.com
//****************************************************************************/
#pragma once
/* colors */
#define RST "\x1B[0m"
#define KRED "\x1B[31m"
#define KGRN "\x1B[32m"
#define KYEL "\x1B[33m"
#define KBLU "\x1B[34m"
#define KMAG "\x1B[35m"
#define KCYN "\x1B[36m"
#define KWHT "\x1B[37m"
#define FRED(x) KRED x RST
#define FGRN(x) KGRN x RST
#define FYEL(x) KYEL x RST
#define FBLU(x) KBLU x RST
#define FMAG(x) KMAG x RST
#define FCYN(x) KCYN x RST
#define FWHT(x) KWHT x RST
#define BOLD(x) "\x1B[1m" x RST
#define UNDL(x) "\x1B[4m" x RST
//###########################################################################
//---------------------------------------------------------------------------
// Main Class
//---------------------------------------------------------------------------
//###########################################################################
template <class T, int X, int Y>
class grid
{
public:
grid();
grid(int const &row, const int &col);
grid(const T *data);
grid(const grid<T,X,Y> &mat);
virtual ~grid();
int GetRow() const;
int GetCol() const;
T GetPoint(const int &row, const int &col) const;
T SetPoint(const int &row, const int &col,const T &val) const;
grid<T,X,Y> operator= (const grid<T,X,Y> &mat);
template<class z,int x,int y>
friend bool CheckDimension(const grid<z,x,y> &mat1,const grid<z,x,y> &mat2);
T **table;
private:
int Row, Col;
};
//###########################################################################
//---------------------------------------------------------------------------
// default constructor (set the grid to zero)
//---------------------------------------------------------------------------
template <class T, int X, int Y>
grid<T,X,Y>::grid(): Row(X), Col(Y)
{
table = new T*[Row];
for(int k = 0; k < Row; k++)
{
table[k] = new T[Col];
}
for(int i =0; i<Row; i++)
{
for(int j =0; j<Col; j++)
{
table[i][j] = {};
}
}
}
//---------------------------------------------------------------------------
// defined constructor
//---------------------------------------------------------------------------
template <class T, int X, int Y>
grid<T,X,Y>::grid(int const &row, const int &col): Row(row), Col(col)
{
table = new T*[Row];
for(int k = 0; k < Row; k++)
{
table[k] = new T[Col];
}
for(int i =0; i<Row; i++)
{
for(int j =0; j<Col; j++)
{
table[i][j] = {};
}
}
}
//---------------------------------------------------------------------------
// defined constructor (read data and store it in the grid)
//---------------------------------------------------------------------------
template <class T, int X, int Y>
grid<T,X,Y>::grid(const T *data): Row(X), Col(Y)
{
table = new T*[Row];
for(int k = 0; k < Row; k++)
{
table[k] = new T[Col];
}
for(int i =0; i<Row; i++)
{
for(int j =0; j<Col; j++)
{
table[i][j] = data[j+i*Col];
}
}
}
//---------------------------------------------------------------------------
// defined constructor (read a grid)
//---------------------------------------------------------------------------
template <class T, int X, int Y>
grid<T,X,Y>::grid(const grid<T,X,Y> &mat): Row(mat.Row), Col(mat.Col)
{
table = new T*[Row];
for(int k = 0; k < Row; k++)
{
table[k] = new T[Col];
}
for(int i =0; i<Row; i++)
{
for(int j =0; j<Col; j++)
{
table[i][j] = mat.table[i][j];
}
}
}
//---------------------------------------------------------------------------
// GetRow
//---------------------------------------------------------------------------
template <class T, int X, int Y>
int grid<T,X,Y>::GetRow() const
{
return Row;
}
//---------------------------------------------------------------------------
// GetCol
//---------------------------------------------------------------------------
template <class T, int X, int Y>
int grid<T,X,Y>::GetCol() const
{
return Col;
}
//---------------------------------------------------------------------------
// GetPoint
//---------------------------------------------------------------------------
template <class T, int X, int Y>
T grid<T,X,Y>:: GetPoint(const int &row, const int &col) const
{
return table[row][col];
}
//---------------------------------------------------------------------------
// SetPoint
//---------------------------------------------------------------------------
template <class T ,int X, int Y>
T grid<T,X,Y>:: SetPoint(const int &row, const int &col,const T &val) const
{
table[row][col]=val;
return table[row][col];
}
//--------------------------------------------------------------------------
// Overloading the = OPERATOR
//--------------------------------------------------------------------------
template <class T, int X, int Y>
grid<T,X,Y> grid<T,X,Y>::operator= (const grid<T,X,Y> &mat)
{
bool match = CheckDimension(*this,mat);
if(!match)
{
throw std::invalid_argument("lhand grid dimensios must match the rhand one.");
}
if(this!=&mat)
for(int i =0; i<Row; i++)
{
for(int j =0; j<Col; j++)
{
table[i][j] = mat.table[i][j];
}
}
grid<T,X,Y> res(*this);
return res;
}
//---------------------------------------------------------------------------
//check dimensionlity
//---------------------------------------------------------------------------
template<class z,int x,int y>
bool CheckDimension(const grid<z,x,y> &mat1,const grid<z,x,y> &mat2)
{
bool flag = false;
if(mat1.Row==mat2.Row && mat1.Col==mat2.Col) return true;
else
return flag;
}
//---------------------------------------------------------------------------
// Overloading the insertion << operator
//---------------------------------------------------------------------------
template <typename U,int Q,int W>
std::ostream& operator<<(std::ostream& output,
const grid<U,Q,W>& mt)
{
//output << "[ ";
for(int i =0; i<mt.GetRow(); i++)
{
for(int j =0; j<mt.GetCol(); j++)
{
//output<<mt.table[i][j]<<"\t";
output<<mt.table[i][j];
}
output<<"\n";
}
// output << "]\n";
return output;
}
//---------------------------------------------------------------------------
// grid class destructor: where we free all allocated memories
//---------------------------------------------------------------------------
template <class T, int X, int Y>
grid<T,X,Y>::~grid()
{
for(int i = 0; i < Row; ++i)
{
delete [] table[i];
}
delete [] table;
}