AFF --- A container for numbers (array) by Friederich and Forbriger.
fortranshape.cc
Go to the documentation of this file.
1 
34 #define AFF_FORTRANSHAPE_CC_VERSION \
35  "AFF_FORTRANSHAPE_CC V1.0 "
36 
37 #include <aff/fortranshape.h>
38 #include <aff/lib/error.h>
39 
40 namespace aff {
41 namespace util {
42 
44  FortranShape::FortranShape(const Tshape& inshape, const bool& BaseOne)
45  {
46  // create a local copy of the shape
47  Tshape shape(inshape);
48  // set first index as required
49  if (BaseOne) shape.setfirst(TIndexVec(1));
50  // extract first and last index
51  Mfirst=shape.first();
52  Mlast=shape.last();
53  // extract offset to first element
54  Moffset=shape.first_offset();
55  // first test for Fortran memory layout
56  for (Tdim i=1; i<Tshape::Mmax_dimen; i++)
57  {
58  AFF_assert((shape.stride(i-1)<=shape.stride(i)),
59  "ERROR (FortranShape): no Fortran layout!");
60  }
61  // native Fortran arrays have stride 1 in base dimension
62  // In case we ever want to pass a slice, we have to think about how to
63  // pass strides to the Fortran subroutine
64  AFF_assert((shape.stride(0)==1),
65  "ERROR (FortranShape): No native Fortran array layout!");
66  // We assume that every properly sliced array has strides that are integer
67  // multiples of the stride of the lower dimension. Just for safety, we
68  // test for that assumption.
69  for (Tdim i=1; i<Tshape::Mmax_dimen; i++)
70  {
71  Tsize mult=shape.stride(i)/shape.stride(i-1);
72  AFF_assert((shape.stride(i)==(mult*shape.stride(i-1))),
73  "ERROR (FortranShape): Stride is no integer multiple!");
74  }
75  // calculate dimensioned last
76  for (Tdim i=1; i<Tshape::Mmax_dimen; i++)
77  {
78  Mdimlast[i-1]=shape.stride(i)/shape.stride(i-1)+Mfirst[i-1]-1;
79  }
81  // check bounds (just in case...)
82  for (Tdim i=0; i<Tshape::Mmax_dimen; i++)
83  {
84  AFF_assert((Mlast[i]<=Mdimlast[i]),
85  "ERROR (FortranShape): illegal index range in shape!");
86  }
87  // that's it...
88  }
89 
90 } // namespace util
91 } // namespace aff
92 
93 /* ----- END OF fortranshape.cc ----- */
Tsubscript Moffset
offset of first element in representation
Definition: fortranshape.h:96
Root namespace of library.
Definition: array.h:148
Shape for a rectangular array layout.
Definition: strided.h:117
TIndexVec Mfirst
first index
Definition: fortranshape.h:90
TIndexVec Mdimlast
last index as dimensioned in memory
Definition: fortranshape.h:94
Strided & setfirst(const Tdim &i, const Tsubscript &index)
setfirst
Definition: strided.cc:279
Tshape::TIndexVec TIndexVec
Vector of index values.
Definition: fortranshape.h:69
const Tsubscript & first(const Tsubscript &i) const
first index of dimension
Definition: strided.h:194
const Tsize & stride(const Tsubscript &i) const
stride of dimension
Definition: strided.h:203
exceptions and error handling macros (prototypes)
static const Tdim Mmax_dimen
instantiate static member (otherwise the linker won&#39;t find it)
Definition: strided.h:123
#define AFF_assert(C, M)
Check an assertion and report by throwing an exception.
Definition: error.h:162
TIndexVec Mlast
last index
Definition: fortranshape.h:92
Tsubscript first_offset() const
first mapped position
Definition: strided.h:188
unsigned short int Tdim
Type to hold an array dimensionality.
Definition: types.h:49
FortranShape(const Tshape &shape, const bool &BaseOne=true)
construct from shape
Definition: fortranshape.cc:44
const Tsubscript & last(const Tsubscript &i) const
last index of dimension
Definition: strided.h:197
size_t Tsize
Type to hold the size of an array dimension.
Definition: types.h:51
prepare information to pass to Fortran subroutines (prototypes)