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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1714 - (show annotations)
Thu Aug 21 00:01:55 2008 UTC (12 years, 1 month ago) by jfenwick
File MIME type: text/plain
File size: 6221 byte(s)
Branch commit

Moved getSliceRegion() and getSliceRange() into DataTypes
Data.cpp - modified not to rely on operator() from DataArrayView
         - Used more const& to avoid copies


1
2 /* $Id$ */
3
4 /*******************************************************
5 *
6 * Copyright 2003-2008 by ACceSS MNRF
7 * Copyright 2008 by University of Queensland
8 *
9 * http://esscc.uq.edu.au
10 * Primary Business: Queensland, Australia
11 * Licensed under the Open Software License version 3.0
12 * http://www.opensource.org/licenses/osl-3.0.php
13 *
14 *******************************************************/
15
16 #if !defined escript_DataTypes_20080811_H
17 #define escript_DataTypes_20080811_H
18 #include "system_dep.h"
19 #include "DataVector.h"
20 #include <vector>
21 #include <boost/python/object.hpp>
22
23 namespace escript {
24
25 namespace DataTypes {
26 //
27 // Some basic types which define the data values and view shapes.
28 typedef DataVector ValueType;
29 typedef std::vector<int> ShapeType;
30 typedef std::vector<std::pair<int, int> > RegionType;
31 typedef std::vector<std::pair<int, int> > RegionLoopRangeType;
32 static const int maxRank=4;
33 static const ShapeType scalarShape;
34
35 // This file contains static functions moved from DataArrayView
36 /**
37 \brief
38 Calculate the number of values for the given shape.
39 */
40 ESCRIPT_DLL_API
41 int
42 noValues(const DataTypes::ShapeType& shape);
43
44 /**
45 \brief
46 Calculate the number of values for the given region.
47 */
48 ESCRIPT_DLL_API
49 int
50 noValues(const DataTypes::RegionLoopRangeType& region);
51
52 /**
53 \brief
54 Return the given shape as a string.
55
56 \param shape - Input.
57 */
58 ESCRIPT_DLL_API
59 std::string
60 shapeToString(const DataTypes::ShapeType& shape);
61
62 /**
63 \brief
64 Determine the shape of the specified slice region.
65
66 \param region - Input -
67 Slice region.
68 */
69 ESCRIPT_DLL_API
70 DataTypes::ShapeType
71 getResultSliceShape(const DataTypes::RegionType& region);
72
73
74 /**
75 \brief
76 Determine the region specified by the given python slice object.
77
78 \param key - Input -
79 python slice object specifying region to be returned.
80
81 The slice object is a tuple of n python slice specifiers, where
82 n <= the rank of this Data object. Each slice specifier specifies the
83 range of indexes to be sliced from the corresponding dimension. The
84 first specifier corresponds to the first dimension, the second to the
85 second and so on. Where n < the rank, the remaining dimensions are
86 sliced across the full range of their indicies.
87
88 Each slice specifier is of the form "a:b", which specifies a slice
89 from index a, up to but not including index b. Where index a is ommitted
90 a is assumed to be 0. Where index b is ommitted, b is assumed to be the
91 length of this dimension. Where both are ommitted (eg: ":") the slice is
92 assumed to encompass that entire dimension.
93
94 Where one of the slice specifiers is a single integer, eg: [1], we
95 want to generate a rank-1 dimension object, as opposed to eg: [1,2]
96 which implies we want to take a rank dimensional object with one
97 dimension of size 1.
98
99 The return value is a vector of pairs with length equal to the rank of
100 this object. Each pair corresponds to the range of indicies from the
101 corresponding dimension to be sliced from, as specified in the input
102 slice object.
103
104 Examples:
105
106 For a rank 1 object of shape(5):
107
108 getSliceRegion(:) => < <0,5> >
109 getSliceRegion(2:3) => < <2,3> >
110 getSliceRegion(:3) => < <0,3> >
111 getSliceRegion(2:) => < <2,5> >
112
113 For a rank 2 object of shape(4,5):
114
115 getSliceRegion(2:3) => < <2,3> <0,5> >
116 getSliceRegion(2) => < <2,3> <0,5> >
117 NB: but return object requested will have rank 1, shape(5), with
118 values taken from index 2 of this object's first dimension.
119
120 For a rank 3 object of shape (2,4,6):
121
122 getSliceRegion(0:2,0:4,0:6) => < <0,2> <0,4> <0,6> >
123 getSliceRegion(:,:,:) => < <0,2> <0,4> <0,6> >
124 getSliceRegion(0:1) => < <0,1> <0,4> <0,6> >
125 getSliceRegion(:1,0:2) => < <0,1> <0,2> <0,6> >
126
127 */
128 ESCRIPT_DLL_API
129 DataTypes::RegionType
130 getSliceRegion(const DataTypes::ShapeType& shape, const boost::python::object& key);
131
132
133
134 ESCRIPT_DLL_API
135 inline
136 int
137 getRank(const DataTypes::ShapeType& shape)
138 {
139 return shape.size();
140 }
141
142 ESCRIPT_DLL_API
143 inline
144 DataTypes::ValueType::size_type
145 getRelIndex(const DataTypes::ShapeType& shape, DataTypes::ValueType::size_type i)
146 {
147 EsysAssert((getRank(shape)==1),"Incorrect number of indices for the rank of this object.");
148 EsysAssert((i < DataTypes::noValues(shape)), "Error - Invalid index.");
149 return i;
150 }
151
152 ESCRIPT_DLL_API
153 inline
154 DataTypes::ValueType::size_type
155 getRelIndex(const DataTypes::ShapeType& shape, DataTypes::ValueType::size_type i,
156 DataTypes::ValueType::size_type j)
157 {
158 EsysAssert((getRank()==2),"Incorrect number of indices for the rank of this object.");
159 DataTypes::ValueType::size_type temp=i+j*shape[0];
160 EsysAssert((temp < DataTypes::noValues(shape)), "Error - Invalid index.");
161 return temp;
162 }
163
164 ESCRIPT_DLL_API
165 inline
166 DataTypes::ValueType::size_type
167 getRelIndex(const DataTypes::ShapeType& shape, DataTypes::ValueType::size_type i,
168 DataTypes::ValueType::size_type j, DataTypes::ValueType::size_type k)
169 {
170 EsysAssert((getRank()==3),"Incorrect number of indices for the rank of this object.");
171 DataTypes::ValueType::size_type temp=i+j*shape[0]+k*shape[1]*shape[0];
172 EsysAssert((temp < DataTypes::noValues(shape)), "Error - Invalid index.");
173 return temp;
174 }
175
176 ESCRIPT_DLL_API
177 inline
178 DataTypes::ValueType::size_type
179 getRelIndex(const DataTypes::ShapeType& shape, DataTypes::ValueType::size_type i,
180 DataTypes::ValueType::size_type j, DataTypes::ValueType::size_type k,
181 DataTypes::ValueType::size_type m)
182 {
183 EsysAssert((getRank()==4),"Incorrect number of indices for the rank of this object.");
184 DataTypes::ValueType::size_type temp=i+j*shape[0]+k*shape[1]*shape[0]+m*shape[2]*shape[1]*shape[0];
185 EsysAssert((temp < DataTypes::noValues(m_shape)), "Error - Invalid index.");
186 return temp;
187 }
188
189
190 } // End namespace DataTypes
191
192
193 } // end of namespace escipt
194
195 #endif

  ViewVC Help
Powered by ViewVC 1.1.26