Intrepid
Intrepid_Rank.hpp
1 #ifndef INTREPID_RANK_HPP
2 #define INTREPID_RANK_HPP
3 
4 template<class A>
5 struct CheckType{static const bool value = false; };
6 
7 
8 template<class A>
9 struct Rank{static const int value = -1;};
10 
11 template<class A,class Scalar>
12 struct Return_Type{
13  typedef Scalar& return_type;
14  typedef Scalar const_return_type;
15  };
16 
17 //----------------------------------------------------------------------------
18 //----------------------------------------------------------------------------
19 
20 template<class DataT,int leftrank>
21 struct RankSpec{};
22 
23 template<class DataT>
24 struct RankSpec<DataT,-1>{
25 public:
26  static int returnvalue(DataT& leftvalues){
27  int dataRank = leftvalues.rank();
28  return dataRank;
29 }
30 };
31 template<class DataT>
32 struct RankSpec<DataT, 1>{
33 public:
34  static int returnvalue(DataT& leftvalues){
35  return 1;
36 }
37 };
38 template<class DataT>
39 struct RankSpec<DataT, 2>{
40 public:
41  static int returnvalue(DataT& leftvalues){
42  return 2;
43 }
44 };
45 template<class DataT>
46 struct RankSpec<DataT, 3>{
47 public:
48  static int returnvalue(DataT& leftvalues){
49  return 3;
50 }
51 };
52 template<class DataT>
53 struct RankSpec<DataT, 4>{
54 public:
55  static int returnvalue(DataT& leftvalues){
56  return 4;
57 }
58 };
59 template<class DataT>
60 struct RankSpec<DataT, 5>{
61 public:
62  static int returnvalue(DataT& leftvalues){
63  return 5;
64 }
65 };
66 template<class DataT>
67 struct RankSpec<DataT, 6>{
68 public:
69  static int returnvalue(DataT& leftvalues){
70  return 6;
71 }
72 };
73 template<class DataT>
74 struct RankSpec<DataT, 7>{
75 public:
76  static int returnvalue(DataT& leftvalues){
77  return 7;
78 }
79 };
80 template<class DataT>
81 struct RankSpec<DataT, 8>{
82 public:
83  static int returnvalue(DataT& leftvalues){
84  return 8;
85 }
86 };
87 
88 template<class DataT>
89 inline size_t getrank(DataT& leftvalue){
90  return RankSpec<DataT,Rank<DataT>::value>::returnvalue(leftvalue);
91 
92 }
93 
94 // ArrayType: Intrepid::FieldContainer or Shards
95 // container (implements operator(), and has view semantics)
96 // ArrayRank: -1 if run-time rank, else the compile-time rank
97 // isconstant: whether the array is constant
98 template<class Scalar,class ArrayType,int ArrayRank,bool isconstant>
99 struct ArrayWrapper;
100 
101 // -1 means rank is determined at run time.
102 // It's like an Intrepid FieldContainer.
103 template<class Scalar,class ArrayType>
104 struct ArrayWrapper<Scalar, ArrayType,-1, false> {
105  ArrayType& view;
106  typedef typename Return_Type<ArrayType, Scalar>::return_type rtype;
107 
113 
114  ArrayWrapper( ArrayType& view_):
115  view(view_),
116  runtimewrapper1(view_),
117  runtimewrapper2(view_),
118  runtimewrapper3(view_),
119  runtimewrapper4(view_),
120  runtimewrapper5(view_) {};
121 
122  int dimension(int i)const{
123  return view.dimension(i);
124  }
125  int rank()const{
126  return view.rank();
127  }
128 
129  rtype operator() (const int i0) const{
130  return view(i0);
131  }
132 
133  rtype operator() (const int i0, const int i1) const{
134  return view(i0,i1);
135  }
136 
137  rtype operator() (const int i0, const int i1, const int i2) const{
138  return view(i0,i1,i2);
139  }
140 
141  rtype operator() (const int i0, const int i1, const int i2,
142  const int i3) const{
143  return view(i0,i1,i2,i3);
144 }
145 
146  rtype operator() (const int i0, const int i1, const int i2,
147  const int i3, const int i4) const{
148  return view(i0,i1,i2,i3,i4);
149 }
150 
151 };
152 
153 template<class Scalar,class ArrayType>
154 struct ArrayWrapper<Scalar,ArrayType,1,false> {
155  ArrayType& view;
156 
157  typedef typename Return_Type<ArrayType, Scalar>::return_type rtype;
158 
159  ArrayWrapper( ArrayType& view_):view(view_) {};
160 
161  int dimension(int i)const{
162  return view.dimension(i);
163  }
164  int rank()const{
165  return 1;
166  }
167 
168  rtype operator() (const int i0, const int i1 = 0, const int i2 = 0,
169  const int i3 = 0, const int i4 = 0, const int i5 = 0,
170  const int i6 = 0, const int i7 = 0) const{
171  return view(i0);
172  }
173 
174 };
175 template<class Scalar,class ArrayType>
176 struct ArrayWrapper<Scalar,ArrayType,2,false> {
177  ArrayType& view;
178 
179  typedef typename Return_Type<ArrayType, Scalar>::return_type rtype;
180 
181  ArrayWrapper( ArrayType& view_):view(view_) {};
182 
183  int dimension(int i)const{
184  return view.dimension(i);
185  }
186  int rank()const{
187  return 2;
188  }
189 
190  rtype operator() (const int i0, const int i1=0, const int i2 = 0,
191  const int i3 = 0, const int i4 = 0, const int i5 = 0,
192  const int i6 = 0, const int i7 = 0) const{
193  return view(i0,i1);
194  }
195 
196 };
197 template<class Scalar,class ArrayType>
198 struct ArrayWrapper<Scalar, ArrayType,3,false> {
199  ArrayType& view;
200 
201  typedef typename Return_Type<ArrayType, Scalar>::return_type rtype;
202 
203  ArrayWrapper( ArrayType& view_):view(view_) {};
204 
205  int dimension(int i)const{
206  return view.dimension(i);
207  }
208  int rank()const{
209  return 3;
210  }
211  rtype operator() (const int i0, const int i1=0, const int i2 = 0,
212  const int i3 = 0, const int i4 = 0, const int i5 = 0,
213  const int i6 = 0, const int i7 = 0) const{
214  return view(i0,i1,i2);
215  }
216 };
217 template<class Scalar,class ArrayType>
218 struct ArrayWrapper<Scalar,ArrayType,4,false> {
219  ArrayType& view;
220 
221  typedef typename Return_Type<ArrayType, Scalar>::return_type rtype;
222 
223  ArrayWrapper( ArrayType& view_):view(view_) {};
224 
225  int dimension(int i)const{
226  return view.dimension(i);
227  }
228  int rank()const{
229  return 4;
230  }
231  rtype operator() (const int i0, const int i1=0, const int i2 = 0,
232  const int i3 = 0, const int i4 = 0, const int i5 = 0,
233  const int i6 = 0, const int i7 = 0)const {
234  return view(i0,i1,i2,i3);
235  }
236 };
237 template<class Scalar,class ArrayType>
238 struct ArrayWrapper<Scalar,ArrayType,5,false> {
239  ArrayType& view;
240 
241  typedef typename Return_Type<ArrayType, Scalar>::return_type rtype;
242 
243  ArrayWrapper( ArrayType& view_):view(view_) {};
244 
245  int dimension(int i)const{
246  return view.dimension(i);
247  }
248  int rank()const{
249  return 5;
250  }
251  rtype operator() (const int i0, const int i1=0, const int i2 = 0,
252  const int i3 = 0, const int i4 = 0, const int i5 = 0,
253  const int i6 = 0, const int i7 = 0) const {
254  return view(i0,i1,i2,i3,i4);
255  }
256 };
257 template<class Scalar,class ArrayType>
258 struct ArrayWrapper<Scalar,ArrayType,6,false> {
259  ArrayType& view;
260 
261  typedef typename Return_Type<ArrayType, Scalar>::return_type rtype;
262 
263  ArrayWrapper( ArrayType& view_):view(view_) {};
264 
265  int dimension(int i)const{
266  return view.dimension(i);
267  }
268  int rank()const{
269  return 6;
270  }
271  rtype operator() (const int i0, const int i1=0, const int i2 = 0,
272  const int i3 = 0, const int i4 = 0, const int i5 = 0,
273  const int i6 = 0, const int i7 = 0) const{
274  return view(i0,i1,i2,i3,i4,i5);
275  }
276 };
277 template<class Scalar,class ArrayType>
278 struct ArrayWrapper<Scalar,ArrayType,7,false> {
279  ArrayType& view;
280 
281  typedef typename Return_Type<ArrayType, Scalar>::return_type rtype;
282 
283  ArrayWrapper( ArrayType& view_):view(view_) {};
284 
285  int dimension(int i)const{
286  return view.dimension(i);
287  }
288  int rank()const{
289  return 7;
290  }
291  rtype operator() (const int i0, const int i1=0, const int i2 = 0,
292  const int i3 = 0, const int i4 = 0, const int i5 = 0,
293  const int i6 = 0, const int i7 = 0) const{
294  return view(i0,i1,i2,i3,i4,i5,i6);
295  }
296 };
297 template<class Scalar,class ArrayType>
298 struct ArrayWrapper<Scalar,ArrayType,8,false> {
299  ArrayType& view;
300 
301  typedef typename Return_Type<ArrayType, Scalar>::return_type rtype;
302 
303  ArrayWrapper(const ArrayType& view_):view(view_) {};
304 
305  int dimension(int i)const{
306  return view.dimension(i);
307  }
308  int rank()const{
309  return 8;
310  }
311  rtype operator() (const int i0, const int i1=0, const int i2 = 0,
312  const int i3 = 0, const int i4 = 0, const int i5 = 0,
313  const int i6 = 0, const int i7 = 0) const{
314  return view(i0,i1,i2,i3,i4,i5,i6,i7);
315  }
316 };
317 template<class Scalar,class ArrayType>
318 struct ArrayWrapper<Scalar, ArrayType,-1, true> {
319  const ArrayType& view;
320  typedef typename Return_Type<ArrayType, Scalar>::const_return_type rtype;
321 
327 
328  ArrayWrapper(const ArrayType& view_):
329  view(view_),
330  runtimewrapper1(view_),
331  runtimewrapper2(view_),
332  runtimewrapper3(view_),
333  runtimewrapper4(view_),
334  runtimewrapper5(view_) {};
335 
336  int dimension(int i)const{
337  return view.dimension(i);
338  }
339  int rank()const{
340  return view.rank();
341  }
342  rtype operator() (const int i0) const{
343  return view(i0);
344 }
345  rtype operator() (const int i0, const int i1) const{
346  return view(i0,i1);
347 }
348  rtype operator() (const int i0, const int i1, const int i2) const{
349  return view(i0,i1,i2);
350 }
351  rtype operator() (const int i0, const int i1, const int i2,
352  const int i3 ) const{
353  return view(i0,i1,i2,i3);
354 }
355  rtype operator() (const int i0, const int i1, const int i2,
356  const int i3, const int i4) const{
357  return view(i0,i1,i2,i3,i4);
358 }
359 
360 };
361 
362 
363 template<class Scalar,class ArrayType>
364 struct ArrayWrapper<Scalar,ArrayType,1,true> {
365  const ArrayType& view;
366 
367  typedef typename Return_Type<ArrayType, Scalar>::const_return_type rtype;
368 
369  ArrayWrapper(const ArrayType& view_):view(view_) {};
370 
371  int dimension(int i)const{
372  return view.dimension(i);
373  }
374  int rank()const{
375  return 1;
376  }
377  rtype operator() (const int i0, const int i1 = 0, const int i2 = 0,
378  const int i3 = 0, const int i4 = 0, const int i5 = 0,
379  const int i6 = 0, const int i7 = 0) const {
380  return view(i0);
381  }
382 
383 };
384 template<class Scalar,class ArrayType>
385 struct ArrayWrapper<Scalar,ArrayType,2,true> {
386  const ArrayType& view;
387 
388  typedef typename Return_Type<ArrayType, Scalar>::const_return_type rtype;
389 
390  ArrayWrapper(const ArrayType& view_):view(view_) {};
391 
392  int dimension(int i)const{
393  return view.dimension(i);
394  }
395  int rank()const{
396  return 2;
397  }
398 
399  rtype operator() (const int i0, const int i1=0, const int i2 = 0,
400  const int i3 = 0, const int i4 = 0, const int i5 = 0,
401  const int i6 = 0, const int i7 = 0) const {
402  return view(i0,i1);
403  }
404 
405 };
406 template<class Scalar,class ArrayType>
407 struct ArrayWrapper<Scalar, ArrayType,3,true> {
408  const ArrayType& view;
409 
410  typedef typename Return_Type<ArrayType, Scalar>::const_return_type rtype;
411 
412  ArrayWrapper(const ArrayType& view_):view(view_) {};
413 
414  int dimension(int i)const{
415  return view.dimension(i);
416  }
417  int rank()const{
418  return 3;
419  }
420 
421  rtype operator() (const int i0, const int i1=0, const int i2 = 0,
422  const int i3 = 0, const int i4 = 0, const int i5 = 0,
423  const int i6 = 0, const int i7 = 0) const {
424  return view(i0,i1,i2);
425  }
426 };
427 template<class Scalar,class ArrayType>
428 struct ArrayWrapper<Scalar,ArrayType,4,true> {
429  const ArrayType& view;
430 
431  typedef typename Return_Type<ArrayType, Scalar>::const_return_type rtype;
432 
433  ArrayWrapper(const ArrayType& view_):view(view_) {};
434  int dimension(int i)const{
435  return view.dimension(i);
436  }
437  int rank()const{
438  return 4;
439  }
440  rtype operator() (const int i0, const int i1=0, const int i2 = 0,
441  const int i3 = 0, const int i4 = 0, const int i5 = 0,
442  const int i6 = 0, const int i7 = 0) const {
443  return view(i0,i1,i2,i3);
444  }
445 };
446 template<class Scalar,class ArrayType>
447 struct ArrayWrapper<Scalar,ArrayType,5,true> {
448  const ArrayType& view;
449 
450  typedef typename Return_Type<ArrayType, Scalar>::const_return_type rtype;
451  ArrayWrapper(const ArrayType& view_):view(view_) {};
452  int dimension(int i)const{
453  return view.dimension(i);
454  }
455  int rank()const{
456  return 5;
457  }
458  rtype operator() (const int i0, const int i1=0, const int i2 = 0,
459  const int i3 = 0, const int i4 = 0, const int i5 = 0,
460  const int i6 = 0, const int i7 = 0) const{
461  return view(i0,i1,i2,i3,i4);
462  }
463 };
464 template<class Scalar,class ArrayType>
465 struct ArrayWrapper<Scalar,ArrayType,6,true> {
466  const ArrayType& view;
467 
468  typedef typename Return_Type<ArrayType, Scalar>::const_return_type rtype;
469  ArrayWrapper(const ArrayType& view_):view(view_) {};
470  int dimension(int i)const{
471  return view.dimension(i);
472  }
473  int rank()const{
474  return 6;
475  }
476  rtype operator() (const int i0, const int i1=0, const int i2 = 0,
477  const int i3 = 0, const int i4 = 0, const int i5 = 0,
478  const int i6 = 0, const int i7 = 0) const {
479  return view(i0,i1,i2,i3,i4,i5);
480  }
481 };
482 template<class Scalar,class ArrayType>
483 struct ArrayWrapper<Scalar,ArrayType,7,true> {
484  const ArrayType& view;
485 
486  typedef typename Return_Type<ArrayType, Scalar>::const_return_type rtype;
487  ArrayWrapper(const ArrayType& view_):view(view_) {};
488  int dimension(int i)const{
489  return view.dimension(i);
490  }
491  int rank()const{
492  return 7;
493  }
494 
495  rtype operator() (const int i0, const int i1=0, const int i2 = 0,
496  const int i3 = 0, const int i4 = 0, const int i5 = 0,
497  const int i6 = 0, const int i7 = 0) const {
498  return view(i0,i1,i2,i3,i4,i5,i6);
499  }
500 };
501 
502 
503 template<class Scalar,class ArrayType>
504 struct ArrayWrapper<Scalar,ArrayType,8,true> {
505  const ArrayType& view;
506 
507  typedef typename Return_Type<ArrayType, Scalar>::const_return_type rtype;
508  ArrayWrapper(const ArrayType& view_):view(view_) {};
509  int dimension(int i)const{
510  return view.dimension(i);
511  }
512  int rank()const{
513  return 8;
514  }
515  rtype operator() (const int i0, const int i1=0, const int i2 = 0,
516  const int i3 = 0, const int i4 = 0, const int i5 = 0,
517  const int i6 = 0, const int i7 = 0) const {
518  return view(i0,i1,i2,i3,i4,i5,i6,i7);
519  }
520 };
521 
522 #endif
523