/[escript]/trunk/escript/src/DataTagged.h
ViewVC logotype

Contents of /trunk/escript/src/DataTagged.h

Parent Directory Parent Directory | Revision Log Revision Log


Revision 123 - (show annotations)
Fri Jul 8 04:08:13 2005 UTC (14 years, 2 months ago) by jgs
Original Path: trunk/esys2/escript/src/Data/DataTagged.h
File MIME type: text/plain
File size: 11789 byte(s)
Merge of development branch back to main trunk on 2005-07-08

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

Properties

Name Value
svn:eol-style native
svn:keywords Author Date Id Revision

  ViewVC Help
Powered by ViewVC 1.1.26