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