-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTArray.h
More file actions
352 lines (283 loc) · 8.11 KB
/
TArray.h
File metadata and controls
352 lines (283 loc) · 8.11 KB
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
// * ************************************************************************ //
// * * TArray2D header * * //
// * * : defines TArray2D template class * * //
// * * * * //
// * * Disclaimer: This document represents no significant intellectual * * //
// * * property and my be used freely. The author(s) takes no * * //
// * * responsibility for any loss or damage arising from the use of * * //
// * * the computer code contained herein. Where this document is * * //
// * * submitted as part of an assessment task this header must remain * * //
// * * intact and the student must make clear indication which parts * * //
// * * have been added by themselves. * * //
// * * * * //
// * ********************************************************************** * //
// * ********************************************************************** * //
// * * TArray2D.h * * //
// * * Author: Tim Wilkin * * //
// * * Created: 05/03/11 Last Modified: 12/07/11 * * //
// * ********************************************************************** * //
#ifndef TARRAY2D_H
#define TARRAY2D_H
#include <memory>
#include <new>
#include <exception>
#include <string.h>
const size_t BUFSIZE = 256;
// Visual Studio accepts but does not implement exception specifications... so stop it from complaining
#pragma warning( disable : 4290)
// define a new exception class for invalid matrix (2D) indices
class invalid_index : public std::exception
{
private:
size_t r, c;
char szMsg[BUFSIZE];
protected:
invalid_index& operator=(const invalid_index &bi) {}
public:
invalid_index(size_t const &_r, size_t const &_c)
: r(_r), c(_c) {}
invalid_index(const invalid_index &bi)
{
r = bi.r;
c = bi.c;
#ifdef _WIN32
strcpy_s(szMsg, BUFSIZE * sizeof(char), bi.szMsg);
#else
strncpy(szMsg, bi.szMsg, BUFSIZE * sizeof(char));
#endif
}
const char * what()
{
// construct error msg only when needed
#ifdef _WIN32
sprintf_s(szMsg, BUFSIZE * sizeof(char), "Bad index value (%u,%u)", r, c);
#else
snprintf(szMsg, BUFSIZE * sizeof(char), "Bad index value (%u,%u)", r, c);
#endif
return szMsg;
}
};
#ifdef __cplusplus
extern "C" {
#endif
typedef struct _coord2
{
short row, col;
_coord2(short r = 0, short c = 0) : row(r), col(c) {}
_coord2(_coord2 const& c) : row(c.row), col(c.col) {}
#ifdef _WINDOWS
// if _WINDOWS is defined the COORD is defined as a struct{ SHORT X,Y; }
// and is used as an argument for Windows Console API functions, so
// we support casting to a COORD
operator COORD()
{
COORD c = { col,row };
return c;
}
#endif
} COORD2, *PCOORD2;
#ifdef __cplusplus
}
#endif
template <typename ValueType>
class TArray2D
{
public:
typedef ValueType value_type;
typedef ValueType *pointer;
typedef ValueType &reference;
typedef ValueType const *const_pointer;
typedef ValueType const &const_reference;
typedef pointer iterator;
typedef const_pointer const_iterator;
private:
size_t m_uRows,
m_uCols;
value_type *m_pBuffer;
void DeleteBuffer();
void AllocateBuffer(size_t r, size_t c);
size_t index_of(size_t r, size_t c);
protected:
TArray2D(const TArray2D&) {}
TArray2D& operator=(const TArray2D&) {}
public:
TArray2D();
TArray2D(size_t _rows, size_t _cols);
~TArray2D();
void resize(size_t _rows, size_t _cols);
reference operator()(size_t row, size_t col) throw (std::bad_alloc);
value_type operator()(size_t row, size_t col) const throw (std::bad_alloc);
reference operator()(COORD2 const & p) throw (std::bad_alloc);
value_type operator()(COORD2 const & p) const throw (std::bad_alloc);
iterator begin();
const_iterator begin() const;
iterator end();
const_iterator end() const;
size_t const& rows() const;
size_t const& cols() const;
COORD2 size() const;
size_t const length() const;
pointer buffer();
void clear();
};
template <typename T>
TArray2D<T>::TArray2D()
: m_pBuffer(NULL), m_uRows(0), m_uCols(0)
{
}
template <typename T>
TArray2D<T>::TArray2D(size_t _rows, size_t _cols)
: m_pBuffer(NULL), m_uRows(_rows), m_uCols(_cols)
{
AllocateBuffer(m_uRows, m_uCols);
}
template <typename T>
TArray2D<T>::~TArray2D()
{
DeleteBuffer();
}
template <typename T>
void TArray2D<T>::DeleteBuffer()
{
if (m_pBuffer)
{
delete[] m_pBuffer;
m_pBuffer = NULL;
}
}
template <typename T>
void TArray2D<T>::AllocateBuffer(size_t r, size_t c)
{
if (m_pBuffer)
DeleteBuffer();
if (r * c == 0)
throw invalid_index(r, c);
m_pBuffer = new value_type[r*c];
if (!m_pBuffer)
#ifdef _WIN32
// throw std::exception("Invalid array access: memory not allocated");
throw std::bad_alloc();
#else
throw std::bad_alloc();
#endif
memset(m_pBuffer, 0, r*c * sizeof(value_type));
}
template <typename T>
void TArray2D<T>::resize(size_t _rows, size_t _cols)
{
DeleteBuffer();
m_uRows = _rows;
m_uCols = _cols;
AllocateBuffer(m_uRows, m_uCols);
}
// row,col hash function (simple array index, column major form
template <typename T>
size_t TArray2D<T>::index_of(size_t r, size_t c)
{
if (r >= m_uRows || c >= m_uCols)
throw invalid_index(r, c);
return c + r * m_uCols;
}
// Array dimension accessors
template <typename T>
const size_t& TArray2D<T>::rows() const
{
return m_uRows;
}
template <typename T>
const size_t& TArray2D<T>::cols() const
{
return m_uCols;
}
template <typename T>
COORD2 TArray2D<T>::size() const
{
COORD2 c(m_uRows, m_uCols);
return c;
}
// Element access methods
template <typename T>
T& TArray2D<T>::operator()(size_t row, size_t col) throw (std::bad_alloc)
{
if (!m_pBuffer)
#ifdef _WIN32
// throw std::exception("Invalid array access: memory not allocated");
throw std::bad_array_new_length();
#else
throw std::bad_alloc();
#endif
return m_pBuffer[index_of(row, col)];
}
template <typename T>
T TArray2D<T>::operator()(size_t row, size_t col) const throw (std::bad_alloc)
{
if (!m_pBuffer)
#ifdef _WIN32
// throw std::exception("Invalid array access: memory not allocated");
throw std::bad_array_new_length();
#else
throw std::bad_alloc();
#endif
return m_pBuffer[index_of(row, col)];
}
template <typename T>
T& TArray2D<T>::operator()(COORD2 const & p) throw (std::bad_alloc)
{
if (!m_pBuffer)
#ifdef _WIN32
// throw std::exception("Invalid array access: memory not allocated");
throw std::bad_array_new_length();
#else
throw std::bad_alloc();
#endif
return m_pBuffer[index_of(p.row, p.col)];
}
template <typename T>
T TArray2D<T>::operator()(COORD2 const & p) const throw (std::bad_alloc)
{
if (!m_pBuffer)
#ifdef _WIN32
// throw std::exception("Invalid array access: memory not allocated");
throw std::bad_array_new_length();
#else
throw std::bad_alloc();
#endif
return m_pBuffer[index_of(p.row, p.col)];
}
template <typename T>
T* TArray2D<T>::buffer()
{
return m_pBuffer;
}
template <typename T>
void TArray2D<T>::clear()
{
memset(m_pBuffer, 0, m_uRows*m_uCols * sizeof(T));
}
template <typename T>
size_t const TArray2D<T>::length() const
{
return m_uRows*m_uCols;
}
template <typename T>
typename TArray2D<T>::iterator TArray2D<T>::begin()
{
return m_pBuffer;
}
template <typename T>
typename TArray2D<T>::const_iterator TArray2D<T>::begin() const
{
return m_pBuffer;
}
template <typename T>
typename TArray2D<T>::iterator TArray2D<T>::end()
{
return m_pBuffer + length();
}
template <typename T>
typename TArray2D<T>::const_iterator TArray2D<T>::end() const
{
return m_pBuffer + length();
}
#endif
// ************************************************************************** //