GianLuigi Piacentini writes:
> TYPE List2link_node
> TYPE (generic) :: node_payload
> TYPE (List2link_node), POINTER :: back, front
> END TYPE List2link_node
> without having to rewrite/adapt all list (tree, queue, stack...)
> processing routines to suit the payload type, as I have currently
> envisioned.
Alas, its not trivial to do nicely in f90/f95. This is exactly one of
the kinds of things that the object-oriented stuff in the draft f2k
makes much simpler...but its going to be a bit of a wait for compilers
for that. In the meantime...
There are 3 general approaches in f90/f95, no doubt with many variants in detail
1. Write different routines for each "payload" type, possibly assisted
by some script tools to do it automatically from a "template" file.
Easy in principle, but a bit of a bother. Needs tools outside of
Fortran to automate it nicely. Code bloat from the seperate code
for each payload type. And works only for homogeneous lists.
2. Write the routines for one payload type. Use the TRANSFER
intrinsic to get it to work for others. I've done this. It may
sound easy, but its actually horribly ugly. Particularly if you
pay attention to the part of the standard that says you can't
assume that pointers to different types have the same size - that
makes it about 5 times as ugly. It is still doable (I've done it).
But its really, really ugly. Needs heavy commenting or its
incomprehensible.
Also, I got tired of working around compiler bugs. This approach
seems really good at uncovering obscure bugs in lots of different
compilers. That eventually drove me to stop doing this - I had
other work to do instead of debugging compilers. I still have some
of my old code around for when I'm in a sadistic mood and want to
torture a poor compiler. But I no longer use it for production.
3. Something along the lines of my current approach. Write the
routines for a single payload type and establish a way to map
from that payload to the others.
I suppose you could view approach 2 with TRANSFER as a particular
such mapping. Its just not one that works very well.
For example, I use integers. Then keep a mapping of integers to
objects for each payload type. Yes, there are some serious
limitations to this. Means you basically need an array of pointers
to all objects of the type. Simple enough if you can determine
a'priori a reasonable limit on how many objects of the type you
could have. A substantial pain if the number of objects can
vary widely and unpredictably.
Works for what I was doing. Might not work at all well in other cases.
4. (being a rocket scientist - I don't have to know how to count up,
just down). Whatever other approaches I didn't recall when writing
this.
--
Richard Maine
[log in to unmask]
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|