1 |
// $Id$ |
2 |
|
3 |
/* |
4 |
****************************************************************************** |
5 |
* * |
6 |
* COPYRIGHT ACcESS 2004 - All Rights Reserved * |
7 |
* * |
8 |
* This software is the property of ACcESS. No part of this code * |
9 |
* may be copied in any form or by any means without the expressed written * |
10 |
* consent of ACcESS. Copying, use or modification of this software * |
11 |
* by any unauthorised person is illegal unless that person has a software * |
12 |
* license agreement with ACcESS. * |
13 |
* * |
14 |
****************************************************************************** |
15 |
*/ |
16 |
|
17 |
#if !defined escript_DataTagged_20040615_H |
18 |
#define escript_DataTagged_20040615_H |
19 |
|
20 |
#include "DataAbstract.h" |
21 |
#include "DataArrayView.h" |
22 |
|
23 |
#include <vector> |
24 |
#include <map> |
25 |
|
26 |
namespace escript { |
27 |
|
28 |
class DataConstant; |
29 |
|
30 |
/** |
31 |
\brief |
32 |
Simulates a full dataset accessible via sampleNo and dataPointNo. |
33 |
|
34 |
Description: |
35 |
Each data-point has an associated tag number, and a given tag can represent a |
36 |
range of dataPointNo and sampleNo. Each tag indexes only a single data-point. |
37 |
Thus only a single data-point needs to be stored for a range of sampleNo and |
38 |
dataPointNo values. |
39 |
*/ |
40 |
|
41 |
class DataTagged : public DataAbstract { |
42 |
|
43 |
public: |
44 |
|
45 |
// |
46 |
// Types for the lists of tags and values. |
47 |
typedef std::vector<int> TagListType; |
48 |
typedef std::vector<DataArrayView> ValueListType; |
49 |
typedef DataArrayView::ValueType ValueType; |
50 |
|
51 |
// |
52 |
// Map from a tag to an offset into the data array. |
53 |
typedef std::map<int, int> DataMapType; |
54 |
|
55 |
/** |
56 |
\brief |
57 |
Default constructor for DataTagged. |
58 |
|
59 |
Description: |
60 |
Default constructor for DataTagged. Creates a DataTagged object for which |
61 |
the only data-point is a scalar data-point with value 0.0. All tags |
62 |
will map to this single data-point. |
63 |
T |
64 |
*/ |
65 |
DataTagged(); |
66 |
|
67 |
/** |
68 |
\brief |
69 |
Constructor for DataTagged. |
70 |
|
71 |
Description: |
72 |
Constructor for DataTagged. |
73 |
\param tagKeys - Input - A vector of integer keys. |
74 |
\param values - Input - A vector of DataArrayViews. If this is empty |
75 |
all tag values will be assigned a value of zero. If |
76 |
it contains one value all tag values will be assigned the |
77 |
same value. Otherwise if there is a mismatch between |
78 |
the number of keys and the number of values an exception |
79 |
will be generated. |
80 |
\param defaultValue - Input - Value returned if a requested tag doesn't exist. |
81 |
\param what - Input - A description of what this data represents. |
82 |
T |
83 |
*/ |
84 |
DataTagged(const TagListType& tagKeys, |
85 |
const ValueListType& values, |
86 |
const DataArrayView& defaultValue, |
87 |
const FunctionSpace& what); |
88 |
|
89 |
/** |
90 |
\brief |
91 |
Alternative Constructor for DataTagged. |
92 |
|
93 |
Description: |
94 |
Alternative Constructor for DataTagged. |
95 |
\param what - Input - A description of what this data object represents. |
96 |
\param shape - Input - The shape of each data-point. |
97 |
\param tags - Input - An array of tags, one for each sample number. |
98 |
\param data - The data values for each tag. |
99 |
*/ |
100 |
DataTagged(const FunctionSpace& what, |
101 |
const DataArrayView::ShapeType &shape, |
102 |
const int tags[], |
103 |
const ValueType& data); |
104 |
|
105 |
/** |
106 |
\brief |
107 |
Slice Constructor for DataTagged. |
108 |
|
109 |
Description: |
110 |
Slice Constructor for DataTagged. |
111 |
Copies a slice from another DataTagged object. |
112 |
\param other - Input - DataTagged object to copy from. |
113 |
\param region - Input - Region to copy. |
114 |
*/ |
115 |
DataTagged(const DataTagged& other, |
116 |
const DataArrayView::RegionType& region); |
117 |
|
118 |
/** |
119 |
\brief |
120 |
Copy Constructor for DataTagged. |
121 |
Performs a deep copy from the given DataTagged object. |
122 |
*/ |
123 |
DataTagged(const DataTagged& other); |
124 |
|
125 |
/** |
126 |
\brief |
127 |
Copy Constructor for DataTagged. |
128 |
Construct a tagged data from a DataConstant object. |
129 |
The default value will be that held by the DataConstant object. |
130 |
*/ |
131 |
DataTagged(const DataConstant& other); |
132 |
|
133 |
/** |
134 |
\brief |
135 |
getSampleDataByTag |
136 |
|
137 |
Description: |
138 |
Return the data-point for the given tag. All of the data for the |
139 |
sample will be visible via the returned pointer. |
140 |
|
141 |
** This provides an interface into the data suitable for legacy C code. |
142 |
*/ |
143 |
virtual |
144 |
double* |
145 |
getSampleDataByTag(int tag); |
146 |
|
147 |
/** |
148 |
\brief |
149 |
Write the data as a string. |
150 |
Writes out each tag, including the default, and the data-point which is |
151 |
associated with each tag. |
152 |
T |
153 |
*/ |
154 |
virtual |
155 |
std::string |
156 |
toString() const; |
157 |
|
158 |
/** |
159 |
\brief |
160 |
Return the tag number associated with the given data-point number |
161 |
according to the associated function space. |
162 |
T |
163 |
*/ |
164 |
virtual |
165 |
int |
166 |
getTagNumber(int dpno); |
167 |
|
168 |
/** |
169 |
\brief |
170 |
getPointOffset |
171 |
|
172 |
Description: |
173 |
Return the offset to the given data-point value in the underlying |
174 |
data vector. |
175 |
|
176 |
\param sampleNo - Input - sample number. |
177 |
\param dataPointNo - Input - data-point number. |
178 |
T |
179 |
*/ |
180 |
virtual |
181 |
ValueType::size_type |
182 |
getPointOffset(int sampleNo, |
183 |
int dataPointNo) const; |
184 |
|
185 |
/** |
186 |
\brief |
187 |
addTaggedValues |
188 |
|
189 |
Description: |
190 |
Add the given tags and values to this DataTagged object, by repeatedly |
191 |
using addTaggedValue for each given tag/value pair. |
192 |
\param tagKeys - Input - A vector of integer keys. |
193 |
\param values - Input - A vector of DataArrayViews. If this is empty |
194 |
then all given tags will be assigned a value of zero. If |
195 |
it contains one value all tags will be assigned the same value. |
196 |
Otherwise if there is a mismatch between the number of tags and |
197 |
the number of values an exception will be generated. |
198 |
*/ |
199 |
void |
200 |
addTaggedValues(const TagListType& tagKeys, |
201 |
const ValueListType& values); |
202 |
|
203 |
/** |
204 |
\brief |
205 |
addTaggedValue |
206 |
|
207 |
Description: |
208 |
Add a single tag and value to this DataTagged object. If this tag already has |
209 |
a value associated with it, setTaggedValue will be used to update this value. |
210 |
\param tagKey - Input - Integer key. |
211 |
\param value - Input - Single DataArrayView value to be assigned to the tag. |
212 |
*/ |
213 |
void |
214 |
addTaggedValue(int tagKey, |
215 |
const DataArrayView& value); |
216 |
|
217 |
/** |
218 |
\brief |
219 |
setTaggedValues |
220 |
|
221 |
Description: |
222 |
Set the given tags to the given values in this DataTagged object, by repeatedly |
223 |
using setTaggedValue for each given tag/value pair. |
224 |
\param tagKeys - Input - A vector of integer keys. |
225 |
\param values - Input - A vector of DataArrayViews. If this is empty |
226 |
then all given tags will be assigned a value of zero. If |
227 |
it contains one value all tag values will be assigned the same value. |
228 |
Otherwise if there is a mismatch between the number of keys and |
229 |
the number of values an exception will be generated. |
230 |
*/ |
231 |
void |
232 |
setTaggedValues(const TagListType& tagKeys, |
233 |
const ValueListType& values); |
234 |
|
235 |
/** |
236 |
\brief |
237 |
setTaggedValue |
238 |
|
239 |
Description: |
240 |
Assign the given value to the given tag. If this tag does not already have a value |
241 |
associated with it, addTaggedValue will be used to add this tag/value pair. |
242 |
\param tagKey - Input - Integer key. |
243 |
\param value - Input - Single DataArrayView value to be assigned to the tag. |
244 |
*/ |
245 |
virtual |
246 |
void |
247 |
setTaggedValue(int tagKey, |
248 |
const DataArrayView& value); |
249 |
|
250 |
/** |
251 |
\brief |
252 |
getDataPointByTag |
253 |
|
254 |
Description: |
255 |
Return a view into the data-point associated with the given tag. |
256 |
\param tag - Input - Integer key. |
257 |
T |
258 |
*/ |
259 |
DataArrayView |
260 |
getDataPointByTag(int tag) const; |
261 |
|
262 |
/** |
263 |
\brief |
264 |
getDataPoint |
265 |
|
266 |
Description: |
267 |
Return a view into the data-point specified by the given sample |
268 |
and data-point numbers. |
269 |
\param sampleNo - Input. |
270 |
\param dataPointNo - Input. |
271 |
T |
272 |
*/ |
273 |
virtual |
274 |
DataArrayView |
275 |
getDataPoint(int sampleNo, |
276 |
int dataPointNo); |
277 |
|
278 |
/** |
279 |
\brief |
280 |
getTagLookup |
281 |
|
282 |
Description: |
283 |
Return a reference to the tag offset lookup table. |
284 |
T |
285 |
*/ |
286 |
const DataMapType& |
287 |
getTagLookup() const; |
288 |
|
289 |
/** |
290 |
\brief |
291 |
isCurrentTag |
292 |
|
293 |
Description: |
294 |
Return true if the given tag exists within the DataTagged tag map. |
295 |
|
296 |
NOTE: The DataTagged tag map does not necessarily coincide with the tag |
297 |
keys in the associated function space. |
298 |
T |
299 |
*/ |
300 |
bool |
301 |
isCurrentTag(int tag) const; |
302 |
|
303 |
/** |
304 |
\brief |
305 |
getDefaultValue |
306 |
|
307 |
Description: |
308 |
Return the default value. This value is associated with any tag which |
309 |
is not explicitly recorded in this DataTagged object's tag map. |
310 |
T |
311 |
*/ |
312 |
DataArrayView& |
313 |
getDefaultValue(); |
314 |
|
315 |
const DataArrayView& |
316 |
getDefaultValue() const; |
317 |
|
318 |
/** |
319 |
\brief |
320 |
getLength |
321 |
|
322 |
Description: |
323 |
Return the number of doubles stored for the Data. |
324 |
T |
325 |
*/ |
326 |
virtual |
327 |
ValueType::size_type |
328 |
getLength() const; |
329 |
|
330 |
/** |
331 |
\brief |
332 |
getSlice |
333 |
|
334 |
Description: |
335 |
Factory method that returns a newly created DataTagged object. |
336 |
The caller is reponsible for managing the object created. |
337 |
*/ |
338 |
virtual |
339 |
DataAbstract* |
340 |
getSlice(const DataArrayView::RegionType& region) const; |
341 |
|
342 |
/** |
343 |
\brief |
344 |
setSlice |
345 |
|
346 |
Description: |
347 |
Copy the specified region from the given value into this object. |
348 |
\param value - Input - Data to copy from. |
349 |
\param region - Input - Region to copy. |
350 |
*/ |
351 |
virtual |
352 |
void |
353 |
setSlice(const DataAbstract* value, |
354 |
const DataArrayView::RegionType& region); |
355 |
|
356 |
/** |
357 |
\brief |
358 |
reshapeDataPoint |
359 |
|
360 |
Description: |
361 |
Reshape the data point only if the data-point is currently rank 0. |
362 |
An exception is thrown if the data-point has rank other than 0. |
363 |
The original data point value is used for all values of the new |
364 |
data point. |
365 |
*/ |
366 |
void |
367 |
reshapeDataPoint(const DataArrayView::ShapeType& shape); |
368 |
|
369 |
/** |
370 |
\brief |
371 |
Archive the underlying data values to the file referenced |
372 |
by ofstream. A count of the number of values expected to be written |
373 |
is provided as a cross-check. |
374 |
|
375 |
The return value indicates success (0) or otherwise (1). |
376 |
*/ |
377 |
int |
378 |
archiveData(std::ofstream& archiveFile, |
379 |
const DataArrayView::ValueType::size_type noValues) const; |
380 |
|
381 |
/** |
382 |
\brief |
383 |
Extract the number of values specified by noValues from the file |
384 |
referenced by ifstream to the underlying data structure. |
385 |
|
386 |
The return value indicates success (0) or otherwise (1). |
387 |
*/ |
388 |
int |
389 |
extractData(std::ifstream& archiveFile, |
390 |
const DataArrayView::ValueType::size_type noValues); |
391 |
|
392 |
protected: |
393 |
|
394 |
private: |
395 |
|
396 |
// |
397 |
// The offset lookup table |
398 |
DataMapType m_offsetLookup; |
399 |
|
400 |
// |
401 |
// the offset to the default value |
402 |
static const int m_defaultValueOffset = 0; |
403 |
|
404 |
// |
405 |
// The actual data |
406 |
ValueType m_data; |
407 |
|
408 |
}; |
409 |
|
410 |
inline |
411 |
bool |
412 |
DataTagged::isCurrentTag(int tag) const |
413 |
{ |
414 |
DataMapType::const_iterator pos(m_offsetLookup.find(tag)); |
415 |
return (pos!=m_offsetLookup.end()); |
416 |
} |
417 |
|
418 |
inline |
419 |
DataArrayView& |
420 |
DataTagged::getDefaultValue() |
421 |
{ |
422 |
// The default value is always the first value. |
423 |
return getPointDataView(); |
424 |
} |
425 |
|
426 |
inline |
427 |
const DataArrayView& |
428 |
DataTagged::getDefaultValue() const |
429 |
{ |
430 |
// The default value is always the first value. |
431 |
return getPointDataView(); |
432 |
} |
433 |
|
434 |
inline |
435 |
const DataTagged::DataMapType& |
436 |
DataTagged::getTagLookup() const |
437 |
{ |
438 |
return m_offsetLookup; |
439 |
} |
440 |
|
441 |
inline |
442 |
DataArrayView::ValueType::size_type |
443 |
DataTagged::getLength() const |
444 |
{ |
445 |
return m_data.size(); |
446 |
} |
447 |
|
448 |
} // end of namespace |
449 |
|
450 |
#endif |