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

Diff of /trunk/escript/src/DataBlocks2D.h

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

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

Legend:
Removed from v.82  
changed lines
  Added in v.478

  ViewVC Help
Powered by ViewVC 1.1.26