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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 507 - (hide annotations)
Thu Feb 9 06:13:07 2006 UTC (13 years, 7 months ago) by jgs
File MIME type: text/plain
File size: 11980 byte(s)
minor comment changes

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

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26