1 |
/* |
2 |
****************************************************************************** |
3 |
* * |
4 |
* COPYRIGHT ACcESS 2004 - All Rights Reserved * |
5 |
* * |
6 |
* This software is the property of ACcESS. No part of this code * |
7 |
* may be copied in any form or by any means without the expressed written * |
8 |
* consent of ACcESS. Copying, use or modification of this software * |
9 |
* by any unauthorised person is illegal unless that person has a software * |
10 |
* license agreement with ACcESS. * |
11 |
* * |
12 |
****************************************************************************** |
13 |
*/ |
14 |
|
15 |
#if !defined escript_DataBlocks2D_20040405_H |
16 |
#define escript_DataBlocks2D_20040405_H |
17 |
|
18 |
#include "DataVector.h" |
19 |
#include "DataException.h" |
20 |
|
21 |
#include "EsysAssert.h" |
22 |
|
23 |
#include <sstream> |
24 |
#include <iostream> |
25 |
|
26 |
namespace escript { |
27 |
|
28 |
/** |
29 |
\brief |
30 |
DataBlocks2D manages a 2D array of multi-dimensional data points. |
31 |
|
32 |
Description: |
33 |
This class is used to manage the data held by instances of |
34 |
the DataExpanded class. |
35 |
*/ |
36 |
|
37 |
class DataBlocks2D { |
38 |
|
39 |
public: |
40 |
|
41 |
// |
42 |
// The type of the underlying data array under management. |
43 |
// The multi-dimensional data points are flattened and stored |
44 |
// serially as a vector of doubles. |
45 |
typedef DataVector ValueType; |
46 |
|
47 |
/** |
48 |
\brief |
49 |
Default constructor for DataBlocks2D. |
50 |
|
51 |
Description: |
52 |
Default constructor for DataBlocks2D. |
53 |
Creates an empty DataBlocks2D object. |
54 |
*/ |
55 |
DataBlocks2D(); |
56 |
|
57 |
/** |
58 |
\brief |
59 |
Copy constructor for DataBlocks2D. |
60 |
|
61 |
Description: |
62 |
Copy constructor for DataBlocks2D. |
63 |
*/ |
64 |
DataBlocks2D(const DataBlocks2D& other); |
65 |
|
66 |
/** |
67 |
\brief |
68 |
Constructor for DataBlocks2D. |
69 |
|
70 |
Description: |
71 |
Constructor for DataBlocks2D. |
72 |
|
73 |
\param numRows - Input - Number of rows(samples). |
74 |
\param numCols - Input - Number of columns(data-points per sample). |
75 |
\param blockSize - Input - Number of elements per block(per data-point). |
76 |
|
77 |
All parameters must be >0, else an exception will be thrown. |
78 |
*/ |
79 |
DataBlocks2D(int numRows, int numCols, int blockSize); |
80 |
|
81 |
/** |
82 |
\brief |
83 |
Default destructor for DataBlocks2D. |
84 |
|
85 |
Description: |
86 |
Default destructor for DataBlocks2D. |
87 |
*/ |
88 |
~DataBlocks2D(); |
89 |
|
90 |
/** |
91 |
\brief |
92 |
Return the size of the underlying data array. |
93 |
ie: Number of rows * Number of columns * Number of elements per data point. |
94 |
*/ |
95 |
inline |
96 |
ValueType::size_type |
97 |
size() const; |
98 |
|
99 |
/** |
100 |
\brief |
101 |
Return the number of rows in this DataBlocks2D array. |
102 |
*/ |
103 |
inline |
104 |
ValueType::size_type |
105 |
getNumRows() const; |
106 |
|
107 |
/** |
108 |
\brief |
109 |
Return the number of columns in this DataBlocks2D array. |
110 |
*/ |
111 |
inline |
112 |
ValueType::size_type |
113 |
getNumCols() const; |
114 |
|
115 |
/** |
116 |
\brief |
117 |
Return the data point size for this DataBlocks2D array. |
118 |
*/ |
119 |
inline |
120 |
ValueType::size_type |
121 |
getBlockSize() const; |
122 |
|
123 |
/** |
124 |
\brief |
125 |
Resize the underlying data array. All current data is lost. |
126 |
The new data elements are initialised to 0. |
127 |
|
128 |
\param numRows - Input - Number of rows. |
129 |
\param numCols - Input - Number of columns. |
130 |
\param blockSize - Input - Number of elements per block. |
131 |
|
132 |
All parameters must be >0, else an exception will be thrown. |
133 |
*/ |
134 |
void |
135 |
resize(int numRows, int numCols, int blockSize); |
136 |
|
137 |
/** |
138 |
\brief |
139 |
DataBlocks2D assignment operator = |
140 |
Assign the given DataBlocks2D object to this one. |
141 |
*/ |
142 |
DataBlocks2D& |
143 |
operator=(const DataBlocks2D& other); |
144 |
|
145 |
/** |
146 |
\brief |
147 |
Swap all the values managed by the given DataBlocks2D objects. |
148 |
*/ |
149 |
void |
150 |
Swap(DataBlocks2D& other); |
151 |
|
152 |
/** |
153 |
\brief |
154 |
Return the 1 dimensional index of the first element for data-point (i,j) |
155 |
within the underlying data array. |
156 |
Provides an index for accessing this data value via the [] operator. |
157 |
Subsequent elements of this data point can be accessed by manually |
158 |
incrementing the returned index value. |
159 |
*/ |
160 |
inline |
161 |
ValueType::size_type |
162 |
index(int row, int col) const; |
163 |
|
164 |
/** |
165 |
\brief |
166 |
Return a reference to the first element for the data-point with index i |
167 |
within the underlying data array as determined by the index(i,j) method. |
168 |
*/ |
169 |
inline |
170 |
ValueType::reference |
171 |
operator[](ValueType::size_type i); |
172 |
|
173 |
inline |
174 |
ValueType::const_reference |
175 |
operator[](ValueType::size_type i) const; |
176 |
|
177 |
/** |
178 |
\brief |
179 |
Return a reference to the first element for the data-point (i,j). |
180 |
*/ |
181 |
inline |
182 |
ValueType::reference |
183 |
operator()(int row, int col); |
184 |
|
185 |
inline |
186 |
ValueType::const_reference |
187 |
operator()(int row, int col) const; |
188 |
|
189 |
/** |
190 |
\brief |
191 |
Return a reference to the underlying data array. |
192 |
Data returned is an array type object that can be indexed via indexes generated |
193 |
by DataBlocks2D::index. |
194 |
*/ |
195 |
inline |
196 |
ValueType& |
197 |
getData(); |
198 |
|
199 |
inline |
200 |
const ValueType& |
201 |
getData() const; |
202 |
|
203 |
/** |
204 |
\brief |
205 |
Archive the data managed by this DataBlocks2D to the file referenced |
206 |
by ofstream. A count of the number of values expected to be written |
207 |
is provided as a cross-check. |
208 |
|
209 |
The return value indicates success (0) or otherwise (1). |
210 |
*/ |
211 |
int |
212 |
archiveData(std::ofstream& archiveFile, |
213 |
const ValueType::size_type noValues) const; |
214 |
|
215 |
/** |
216 |
\brief |
217 |
Extract the number of values specified by noValues from the file |
218 |
referenced by ifstream to this DataBlocks2D. |
219 |
|
220 |
The return value indicates success (0) or otherwise (1). |
221 |
*/ |
222 |
int |
223 |
extractData(std::ifstream& archiveFile, |
224 |
const ValueType::size_type noValues); |
225 |
|
226 |
protected: |
227 |
|
228 |
private: |
229 |
|
230 |
// |
231 |
// The underlying array of data values. |
232 |
// The two dimensional array of multi-dimensional data points is flattened |
233 |
// and serialised within this one dimensional array of doubles. |
234 |
ValueType m_data; |
235 |
|
236 |
// |
237 |
// The dimensions of the 2D array of data points. |
238 |
ValueType::size_type m_numRows; |
239 |
ValueType::size_type m_numCols; |
240 |
|
241 |
// |
242 |
// The number of values per data point. |
243 |
ValueType::size_type m_blockSize; |
244 |
|
245 |
}; |
246 |
|
247 |
inline |
248 |
DataBlocks2D::ValueType::size_type |
249 |
DataBlocks2D::size() const |
250 |
{ |
251 |
EsysAssert(((m_numRows >= 0) && (m_numCols >= 0) && (m_blockSize >= 0)), "(DataBlocks2D) Invalid object."); |
252 |
return m_data.size(); |
253 |
} |
254 |
|
255 |
inline |
256 |
DataBlocks2D::ValueType::size_type |
257 |
DataBlocks2D::getNumRows() const |
258 |
{ |
259 |
EsysAssert(((m_numRows >= 0) && (m_numCols >= 0) && (m_blockSize >= 0)), "(DataBlocks2D) Invalid object."); |
260 |
return m_numRows; |
261 |
} |
262 |
|
263 |
inline |
264 |
DataBlocks2D::ValueType::size_type |
265 |
DataBlocks2D::getNumCols() const |
266 |
{ |
267 |
EsysAssert(((m_numRows >= 0) && (m_numCols >= 0) && (m_blockSize >= 0)), "(DataBlocks2D) Invalid object."); |
268 |
return m_numCols; |
269 |
} |
270 |
|
271 |
inline |
272 |
DataBlocks2D::ValueType::size_type |
273 |
DataBlocks2D::getBlockSize() const |
274 |
{ |
275 |
EsysAssert(((m_numRows >= 0) && (m_numCols >= 0) && (m_blockSize >= 0)), "(DataBlocks2D) Invalid object."); |
276 |
return m_blockSize; |
277 |
} |
278 |
|
279 |
inline |
280 |
DataBlocks2D::ValueType::size_type |
281 |
DataBlocks2D::index(int row, int col) const |
282 |
{ |
283 |
EsysAssert(((m_numRows >= 0) && (m_numCols >= 0) && (m_blockSize >= 0)), "(DataBlocks2D) Invalid object."); |
284 |
EsysAssert(((row >= 0) && (col >= 0) && (m_data.size() > 0)), "(DataBlocks2D) Index value out of range."); |
285 |
ValueType::size_type temp=(row*m_numCols+col)*m_blockSize; |
286 |
EsysAssert((temp <= (m_data.size()-m_blockSize)), "(DataBlocks2D) Index value out of range."); |
287 |
return (temp); |
288 |
} |
289 |
|
290 |
inline |
291 |
DataBlocks2D::ValueType::reference |
292 |
DataBlocks2D::operator[](DataBlocks2D::ValueType::size_type i) |
293 |
{ |
294 |
EsysAssert(((m_numRows >= 0) && (m_numCols >= 0) && (m_blockSize >= 0)), "(DataBlocks2D) Invalid object."); |
295 |
return m_data[i]; |
296 |
} |
297 |
|
298 |
inline |
299 |
DataBlocks2D::ValueType::const_reference |
300 |
DataBlocks2D::operator[](DataBlocks2D::ValueType::size_type i) const |
301 |
{ |
302 |
EsysAssert(((m_numRows >= 0) && (m_numCols >= 0) && (m_blockSize >= 0)), "(DataBlocks2D) Invalid object."); |
303 |
return m_data[i]; |
304 |
} |
305 |
|
306 |
inline |
307 |
DataBlocks2D::ValueType::reference |
308 |
DataBlocks2D::operator()(int row, int col) |
309 |
{ |
310 |
EsysAssert(((m_numRows >= 0) && (m_numCols >= 0) && (m_blockSize >= 0)), "(DataBlocks2D) Invalid object."); |
311 |
return m_data[index(row,col)]; |
312 |
} |
313 |
|
314 |
inline |
315 |
DataBlocks2D::ValueType::const_reference |
316 |
DataBlocks2D::operator()(int row, int col) const |
317 |
{ |
318 |
EsysAssert(((m_numRows >= 0) && (m_numCols >= 0) && (m_blockSize >= 0)), "(DataBlocks2D) Invalid object."); |
319 |
return m_data[index(row,col)]; |
320 |
} |
321 |
|
322 |
inline |
323 |
DataBlocks2D::ValueType& |
324 |
DataBlocks2D::getData() |
325 |
{ |
326 |
EsysAssert(((m_numRows >= 0) && (m_numCols >= 0) && (m_blockSize >= 0)), "(DataBlocks2D) Invalid object."); |
327 |
return m_data; |
328 |
} |
329 |
|
330 |
inline |
331 |
const DataBlocks2D::ValueType& |
332 |
DataBlocks2D::getData() const |
333 |
{ |
334 |
EsysAssert(((m_numRows >= 0) && (m_numCols >= 0) && (m_blockSize >= 0)), "(DataBlocks2D) Invalid object."); |
335 |
return m_data; |
336 |
} |
337 |
|
338 |
} // end of namespace |
339 |
|
340 |
#endif |