6
A minimal class used when passing multi-dimensional array
7
arguments from C++ to FORTRAN 77 (received as FORTRAN arrays),
8
and subsequently returned back to C++ as properly aranged
11
Problem : FORTRAN organises data in a "column-first" order,
12
while C++ organises data in a "row-first" order.
15
(1) The FMATRIX class can take a C++ array as a constructor
16
parameter. A FORTRAN compatible copy of the array is
17
then made. The destructor will then copy the result back
18
to the original c++ array.
20
(2) The FMATRIX class provides "subscript operators" allowing
21
the programmer to read and write from the array, using
22
FORTRAN-like syntax and indexing semantics.
24
Author: Carsten A. Arnholm, 04-MAR-1996
30
FMATRIX(size_t dim1, size_t dim2);
31
FMATRIX(T* cpparr, size_t dim1, size_t dim2);
33
T& operator()(size_t index1, size_t index2);
36
const size_t ndim; // number of array dimensions
37
size_t dim[7]; // size of each dimension
38
T* cpprep; // original c++ array
39
T* f77rep; // array used by FORTRAN
43
FMATRIX<T>::FMATRIX(size_t dim1, size_t dim2)
44
: cpprep(NULL),f77rep(new T[dim1*dim2]),ndim(2)
56
FMATRIX<T>::FMATRIX(T* cpparr, size_t dim1, size_t dim2)
57
: cpprep(cpparr),f77rep(new T[dim1*dim2]),ndim(2)
67
// make a FORTRAN-compatible copy of the array
70
for(size_t i=0;i<dim[0];i++) {
71
for(size_t j=0;j<dim[1];j++) {
72
index_f77 = j*dim[0] + i;
73
f77rep[index_f77] = cpprep[index_cpp++];
79
FMATRIX<T>::operator T*()
81
// Pass the FORTRAN representation when calling a function
86
T& FMATRIX<T>::operator()(size_t index1, size_t index2)
88
assert(ndim==2); // only 2d arrays supported (so far)
90
// indexing according to F77 conventions
91
size_t index_f77 = index2*dim[0] + index1;
93
// return a reference to the array element
94
return *(f77rep+index_f77);
98
FMATRIX<T>::~FMATRIX()
101
assert(ndim==2); // only 2d arrays supported (so far)
103
// copy back from FORTRAN to C++ array
106
for(size_t j=0;j<dim[1];j++) {
107
for(size_t i=0;i<dim[0];i++) {
108
index_cpp = i*dim[1] + j;
109
cpprep[index_cpp] = f77rep[index_f77++];
114
// delete the FORTRAN copy of the arry