/[escript]/branches/arrayview_from_1695_trunk/escript/src/DataTypes.h
ViewVC logotype

Diff of /branches/arrayview_from_1695_trunk/escript/src/DataTypes.h

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

revision 1697 by jfenwick, Mon Aug 11 06:29:54 2008 UTC revision 1734 by jfenwick, Thu Aug 28 06:11:56 2008 UTC
# Line 18  Line 18 
18  #include "system_dep.h"  #include "system_dep.h"
19  #include "DataVector.h"  #include "DataVector.h"
20  #include <vector>  #include <vector>
21    #include <string>
22    #include <boost/python/object.hpp>
23    
24  namespace escript {  namespace escript {
25    
# Line 29  namespace escript { Line 31  namespace escript {
31    typedef std::vector<std::pair<int, int> > RegionType;    typedef std::vector<std::pair<int, int> > RegionType;
32    typedef std::vector<std::pair<int, int> > RegionLoopRangeType;    typedef std::vector<std::pair<int, int> > RegionLoopRangeType;
33    static const int maxRank=4;    static const int maxRank=4;
34      static const ShapeType scalarShape;
35    
36   }   // End DataTypes  // This file contains static functions moved from DataArrayView
37      /**
38         \brief
39         Calculate the number of values for the given shape.
40      */
41      ESCRIPT_DLL_API
42      int
43      noValues(const DataTypes::ShapeType& shape);
44    
45      /**
46         \brief
47         Calculate the number of values for the given region.
48      */
49      ESCRIPT_DLL_API
50      int
51      noValues(const DataTypes::RegionLoopRangeType& region);
52    
53      /**
54         \brief
55         Return the given shape as a string.
56    
57         \param shape - Input.
58      */
59      ESCRIPT_DLL_API
60      std::string
61      shapeToString(const DataTypes::ShapeType& shape);
62    
63      /**
64         \brief
65         Determine the shape of the specified slice region.
66    
67         \param region - Input -
68                           Slice region.
69      */
70      ESCRIPT_DLL_API
71      DataTypes::ShapeType
72      getResultSliceShape(const DataTypes::RegionType& region);
73    
74    
75     /**
76         \brief
77         Determine the region specified by the given python slice object.
78    
79         \param key - Input -
80                        python slice object specifying region to be returned.
81    
82         The slice object is a tuple of n python slice specifiers, where
83         n <= the rank of this Data object. Each slice specifier specifies the
84         range of indexes to be sliced from the corresponding dimension. The
85         first specifier corresponds to the first dimension, the second to the
86         second and so on. Where n < the rank, the remaining dimensions are
87         sliced across the full range of their indicies.
88    
89         Each slice specifier is of the form "a:b", which specifies a slice
90         from index a, up to but not including index b. Where index a is ommitted
91         a is assumed to be 0. Where index b is ommitted, b is assumed to be the
92         length of this dimension. Where both are ommitted (eg: ":") the slice is
93         assumed to encompass that entire dimension.
94    
95         Where one of the slice specifiers is a single integer, eg: [1], we
96         want to generate a rank-1 dimension object, as opposed to eg: [1,2]
97         which implies we want to take a rank dimensional object with one
98         dimension of size 1.
99    
100         The return value is a vector of pairs with length equal to the rank of
101         this object. Each pair corresponds to the range of indicies from the
102         corresponding dimension to be sliced from, as specified in the input
103         slice object.
104    
105         Examples:
106    
107           For a rank 1 object of shape(5):
108    
109             getSliceRegion(:)   => < <0,5> >
110             getSliceRegion(2:3) => < <2,3> >
111             getSliceRegion(:3)  => < <0,3> >
112             getSliceRegion(2:)  => < <2,5> >
113    
114           For a rank 2 object of shape(4,5):
115    
116             getSliceRegion(2:3) => < <2,3> <0,5> >
117             getSliceRegion(2)   => < <2,3> <0,5> >
118               NB: but return object requested will have rank 1, shape(5), with
119                   values taken from index 2 of this object's first dimension.
120    
121           For a rank 3 object of shape (2,4,6):
122    
123             getSliceRegion(0:2,0:4,0:6) => < <0,2> <0,4> <0,6> >
124             getSliceRegion(:,:,:)       => < <0,2> <0,4> <0,6> >
125             getSliceRegion(0:1)         => < <0,1> <0,4> <0,6> >
126             getSliceRegion(:1,0:2)      => < <0,1> <0,2> <0,6> >
127    
128    
129         Note: Not unit tested in c++.
130      */
131       ESCRIPT_DLL_API
132       DataTypes::RegionType
133       getSliceRegion(const DataTypes::ShapeType& shape, const boost::python::object& key);
134    
135      /**
136      \brief
137       Modify region to copy from in order to
138       deal with the case where one range in the region contains identical indexes,
139       eg: <<1,1><0,3><0,3>>
140       This situation implies we want to copy from an object with rank greater than that of this
141       object. eg: we want to copy the values from a two dimensional slice out of a three
142       dimensional object into a two dimensional object.
143       We do this by taking a slice from the other object where one dimension of
144       the slice region is of size 1. So in the above example, we modify the above
145       region like so: <<1,2><0,3><0,3>> and take this slice.
146      */
147      DataTypes::RegionLoopRangeType
148      getSliceRegionLoopRange(const DataTypes::RegionType& region);
149    
150      ESCRIPT_DLL_API
151      inline
152      int
153      getRank(const DataTypes::ShapeType& shape)
154      {
155        return shape.size();
156      }
157    
158      ESCRIPT_DLL_API
159      inline
160      DataTypes::ValueType::size_type
161      getRelIndex(const DataTypes::ShapeType& shape, DataTypes::ValueType::size_type i)
162      {
163        EsysAssert((getRank(shape)==1),"Incorrect number of indices for the rank of this object.");
164        EsysAssert((i < DataTypes::noValues(shape)), "Error - Invalid index.");
165        return i;
166      }
167    
168      ESCRIPT_DLL_API
169      inline
170      DataTypes::ValueType::size_type
171      getRelIndex(const DataTypes::ShapeType& shape, DataTypes::ValueType::size_type i,
172           DataTypes::ValueType::size_type j)
173      {
174        EsysAssert((getRank(shape)==2),"Incorrect number of indices for the rank of this object.");
175        DataTypes::ValueType::size_type temp=i+j*shape[0];
176        EsysAssert((temp < DataTypes::noValues(shape)), "Error - Invalid index.");
177        return temp;
178      }
179    
180      ESCRIPT_DLL_API
181      inline
182      DataTypes::ValueType::size_type
183      getRelIndex(const DataTypes::ShapeType& shape, DataTypes::ValueType::size_type i,
184           DataTypes::ValueType::size_type j, DataTypes::ValueType::size_type k)
185      {
186        EsysAssert((getRank(shape)==3),"Incorrect number of indices for the rank of this object.");
187        DataTypes::ValueType::size_type temp=i+j*shape[0]+k*shape[1]*shape[0];
188        EsysAssert((temp < DataTypes::noValues(shape)), "Error - Invalid index.");
189        return temp;
190      }
191    
192      ESCRIPT_DLL_API
193      inline
194      DataTypes::ValueType::size_type
195      getRelIndex(const DataTypes::ShapeType& shape, DataTypes::ValueType::size_type i,
196           DataTypes::ValueType::size_type j, DataTypes::ValueType::size_type k,
197           DataTypes::ValueType::size_type m)
198      {
199        EsysAssert((getRank(shape)==4),"Incorrect number of indices for the rank of this object.");
200        DataTypes::ValueType::size_type temp=i+j*shape[0]+k*shape[1]*shape[0]+m*shape[2]*shape[1]*shape[0];
201        EsysAssert((temp < DataTypes::noValues(shape)), "Error - Invalid index.");
202        return temp;
203      }
204    
205      ESCRIPT_DLL_API
206      inline
207      bool
208      checkShape(const ShapeType& s1, const ShapeType& s2)
209      {
210        return s1==s2;
211      }
212    
213       std::string
214       createShapeErrorMessage(const std::string& messagePrefix,
215                                              const DataTypes::ShapeType& other,
216                          const DataTypes::ShapeType& thisShape);
217    
218    
219      /**
220         \brief
221         Copy a data slice specified by the given region and offset from the
222         given view into this view at the given offset.
223    
224         \param thisOffset - Input -
225                          Copy the slice to this offset in this view.
226         \param other - Input -
227                          View to copy data from.
228         \param otherOffset - Input -
229                          Copy the slice from this offset in the given view.
230         \param region - Input -
231                          Region in other view to copy data from.
232      */
233       void
234       copySlice(ValueType& left,
235                    const ShapeType& leftShape,
236                    ValueType::size_type thisOffset,
237                                const ValueType& other,
238                    const ShapeType& otherShape,
239                                ValueType::size_type otherOffset,
240                                const RegionLoopRangeType& region);
241    
242      /**
243         \brief
244         Copy data into a slice specified by the given region and offset in
245         this view from the given view at the given offset.
246    
247         \param thisOffset - Input -
248                        Copy the slice to this offset in this view.
249         \param other - Input -
250                        View to copy data from.
251         \param otherOffset - Input -
252                        Copy the slice from this offset in the given view.
253         \param region - Input -
254                        Region in this view to copy data to.
255      */
256       void
257       copySliceFrom(ValueType& left,
258                    const ShapeType& leftShape,
259                    ValueType::size_type thisOffset,
260                                    const ValueType& other,
261                    const ShapeType& otherShape,
262                                    ValueType::size_type otherOffset,
263                                    const RegionLoopRangeType& region);
264    
265    
266  } // end of namespace   }   // End namespace DataTypes
267    
268    
269    } // end of namespace escipt
270    
271  #endif  #endif

Legend:
Removed from v.1697  
changed lines
  Added in v.1734

  ViewVC Help
Powered by ViewVC 1.1.26