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