I have to work on some legacy Fortran, although I can use the latest compilers.
The code stores enormous amounts of data in one-dimensional arrays.
For example,
PROGRAM horrible_coding
IMPLICIT EVERYTHING ! Sarcasm
REAL, DIMENSION(1000) :: data
INTEGER, DIMENSION(50) :: info_location
! Somewhere, info is read in and stored temporarily as info_1, info_2, etc.
data(1:3) = info_1
data(4:9) = info_2
...
data(134:192) = info_n
The association between which elements in the DATA
array go with which pieces of info
is stored in a second array. Something like:
info_location(1) = 1
info_location(2) = 4
info_location(n) = 134
That's right. The value of each element of the info_location
array refers to the first element of the data array where you can find the relevant info.
So, for example, if you want to get the data for info_7, you have to do the following:
size_of_info_7 = info_location(8) - info_location(7)
ALLOCATE(data_for_info_7(size_of_info_7))
data_for_info_7 = data(info_location(7) : info_location(7) + size_of_info_7 - 1)
By now, the sight of this insanity might have caused blood to start shooting out of your eyes so I apologize.
I would like to create object instances and store all the relevant data for each piece of info as instance data. And while I'm bringing things to 2003, I would create instance methods to get and set the instance data.
I do not want to rewrite the code but want to put this modernization on top of what's already there.
I believe I can accomplish this by making the object instance data simply pointers that point to the actual elements of the data
array that contains the relevant info.
Is this a reasonable thing to do?
Can I create objects in Fortran whose instance data is predominantly pointers?
I am hesitant to begin this task without first tossing the idea around to stackoverflow.
I'm not sure, what you mean, but you can definitely do something as:
And you can also make arrays of these types:
(can be also pointer or static, as you wish.)
Then, if original data has
TARGET
attribute, you can use:You can also remap the pointer to different lower bounds, the process above gives it the lower bound 1.