forked from ClimateGlobalChange/tempestremap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DataMatrix.h
319 lines (268 loc) · 6.39 KB
/
DataMatrix.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
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
///////////////////////////////////////////////////////////////////////////////
///
/// \file DataMatrix.h
/// \author Paul Ullrich
/// \version July 26, 2010
///
/// <remarks>
/// Copyright 2000-2010 Paul Ullrich
///
/// This file is distributed as part of the Tempest source code package.
/// Permission is granted to use, copy, modify and distribute this
/// source code and its documentation under the terms of the GNU General
/// Public License. This software is provided "as is" without express
/// or implied warranty.
/// </remarks>
#ifndef _DATAMATRIX_H_
#define _DATAMATRIX_H_
///////////////////////////////////////////////////////////////////////////////
#include "Exception.h"
#include <sstream>
#include <iostream>
#include <cstdlib>
#include <cstring>
///////////////////////////////////////////////////////////////////////////////
/// <summary>
/// A DataMatrix is a datatype that stores data in a 2D structure.
/// Arithmatic operations are not supported for this datatype.
/// </summary>
template <typename DataType>
class DataMatrix {
public:
/// <summary>
/// Default constructor.
/// </summary>
DataMatrix() :
m_sRows(0),
m_sColumns(0),
m_data(NULL)
{ }
/// <summary>
/// Constructor.
/// </summary>
/// <param name="sRows">
/// Number of rows in this matrix.
/// </param>
/// <param name="sColumns">
/// Number of columns in this matrix.
/// </param>
DataMatrix(
unsigned int sRows,
unsigned int sColumns
) :
m_sRows(0),
m_sColumns(0),
m_data(NULL)
{
Initialize(sRows, sColumns);
}
/// <summary>
/// Copy constructor.
/// </summary>
DataMatrix(
const DataMatrix<DataType> & dm
) :
m_sRows(0),
m_sColumns(0),
m_data(NULL)
{
Assign(dm);
}
/// <summary>
/// Destructor.
/// </summary>
virtual ~DataMatrix() {
if (m_data != NULL) {
free(reinterpret_cast<void*>(m_data));
}
}
public:
/// <summary>
/// Determine if this DataMatrix is initialized.
/// </summary>
bool IsInitialized() const {
if (m_data == NULL) {
return false;
} else {
return true;
}
}
public:
/// <summary>
/// Deallocate data for this object.
/// </summary>
void Deinitialize() {
if (m_data != NULL) {
free(reinterpret_cast<void*>(m_data));
}
m_data = NULL;
m_sRows = 0;
m_sColumns = 0;
}
/// <summary>
/// Allocate memory for this object.
/// </summary>
void Initialize(
unsigned int sRows,
unsigned int sColumns,
bool fAutoZero = true
) {
unsigned int sI;
// Check for zero size
if ((sRows == 0) || (sColumns == 0)) {
Deinitialize();
return;
}
// No need to reallocate memory if this matrix already has
// the correct dimensions.
if ((m_sRows == sRows) && (m_sColumns == sColumns)) {
// Auto zero
if (fAutoZero) {
Zero();
}
return;
}
// Deinitialize existing content
Deinitialize();
// Calculate the per-row footprint
unsigned int sRowPtrFootprint = sRows * sizeof(DataType *);
unsigned int sRowFootprint = sColumns * sizeof(DataType);
// Calculate padding to align on DataType boundaries
unsigned int sPadding;
if ((sRowPtrFootprint % sizeof(DataType)) == 0) {
sPadding = 0;
} else {
sPadding =
sizeof(DataType) - sRowPtrFootprint % sizeof(DataType);
}
// Allocate memory
char *rawdata = reinterpret_cast<char*>(
malloc((size_t) sRowPtrFootprint + (size_t) sPadding + (size_t) sRows * (size_t) sRowFootprint));
if (rawdata == NULL) {
_EXCEPTIONT("Out of memory.");
}
// Assign memory pointers
char *rawdataRowStart = rawdata + sPadding + sRowPtrFootprint;
m_data = reinterpret_cast<DataType**>(rawdata);
for (sI = 0; sI < sRows; sI++) {
m_data[sI] = reinterpret_cast<DataType*>(
rawdataRowStart + sI * sRowFootprint
);
}
// Assign dimensions
m_sRows = sRows;
m_sColumns = sColumns;
// Auto zero
if (fAutoZero) {
Zero();
}
}
public:
/// <summary>
/// Assignment operator.
/// </summary>
void Assign(const DataMatrix<DataType> & dm) {
// Check initialization status
if (!dm.IsInitialized()) {
Deinitialize();
return;
}
// Allocate memory
Initialize(dm.m_sRows, dm.m_sColumns, false);
// Copy data
memcpy(
&(m_data[0][0]),
&(dm.m_data[0][0]),
m_sRows * m_sColumns * sizeof(DataType)
);
}
/// <summary>
/// Assignment operator.
/// </summary>
DataMatrix & operator= (const DataMatrix<DataType> & dm) {
Assign(dm);
return (*this);
}
/// <summary>
/// Zero the data content of this object.
/// </summary>
void Zero() {
// Check initialization status
if (!IsInitialized()) {
_EXCEPTIONT(
"Attempted operation on uninitialized DataMatrix3D.");
}
// Set content to zero
memset(
&(m_data[0][0]),
0,
(long) m_sRows * (long) m_sColumns * (long) sizeof(DataType)
);
}
public:
/// <summary>
/// Get the number of rows in this matrix.
/// </summary>
inline unsigned int GetRows() const {
return m_sRows;
}
/// <summary>
/// Get the number of columns in this matrix.
/// </summary>
inline unsigned int GetColumns() const {
return m_sColumns;
}
/// <summary>
/// Get the total number of elements in this matrix.
/// </summary>
inline unsigned int GetTotalElements() const {
return m_sRows * m_sColumns;
}
public:
/// <summary>
/// Cast to an array.
/// </summary>
inline operator DataType**() const {
return m_data;
}
public:
/// <summary>
/// Give a string representation of this object.
/// </summary>
std::string ToString() const {
unsigned int i;
unsigned int j;
std::stringstream strstr;
strstr << "[";
for(i = 0; i < GetRows(); i++) {
strstr << "[";
for(j = 0; j < GetColumns(); j++) {
strstr << m_data[i][j];
if (j != GetColumns()-1) {
strstr << ", ";
}
}
strstr << "]";
if (i != GetRows()-1) {
strstr << ", ";
}
}
strstr << "]";
return (strstr.str());
}
private:
/// <summary>
/// The number of rows in this matrix.
/// </summary>
unsigned int m_sRows;
/// <summary>
/// The number of columns in this matrix.
/// </summary>
unsigned int m_sColumns;
/// <summary>
/// A pointer to the data associated with this matrix.
/// </summary>
DataType** m_data;
};
///////////////////////////////////////////////////////////////////////////////
#endif