Hi, Toon,
To answer your question, yes, I think you are missing a few things. :-)
Suppose the original order of the links was 1->2->3 and that you wanted
to insert a new link after 2, giving 1->2->4->3.
With pointers, you'd do the following.
Set 4's NEXT to 2's NEXT
Set 2's NEXT to 4.
With allocatable NEXT components, note first that in F9x, only an array
can be allocatable; a scalar cannot. I don't know if this changes for
f2k, but let's suppose it does.
With allocatables, it's not even obvious what the semantics of
recursive derived-type component membership is, if indeed it is
allowed. For example, if we were to set up 1->2->3, allocating
1's next, then 2's, what would copying 1 to a new link (as in
"link_new = link1") do?
It would seem that 1 actually contains all of 2, rather than just
a pointer to 2; and that similarly 2 contains all of 3. So a
copy of link1 might have to copy all of link2 and link3's
contents, too. I'm not sure how f2k handles this, but let's
suppose there's a rule that says that on assignment, allocatable
components aren't copied, and these components in the new copy
have unallocated status.
Then, to do the insertion of link 4, I think you'd have to do the
following:
copy the entire chain starting with the NEXT of link2
to temporary storage.
allocate the NEXT of link 2 to the new storage (link 4)
allocate link 4's NEXT, and it's NEXT, etc. -- enough
time to hold the chain stored in the first step
copy the chain stored in the first step to this newly
allocated storage
deallocate the chain stored in the first step.
Some of this could be simplified by giving the NEXT
components the TARGET attribute and using POINTERs to hold
the temporary storage, but after all, the whole purpose
of ALLOCATABLE derived-type components is to avoid both these
things.
Bottom line is that POINTERs are better than ALLOCATABLEs
for linked lists and the like.
Even so, if you want to avoid POINTERs, they aren't completely
necessary; INTEGERs could be used. One would maintain a flat
array of links and use the NEXT component of a link to store
the index to the next link in the chain.
With pointers or integers, the advantage is copying some small
reference (pointer or integer) rather than entire link contents,
or (as appears necessary with ALLOCATABLEs) whole chains of link
contents.
-P.
On Wed, 10 May 2000, Toon Moene wrote:
> Richard Maine wrote:
> >
> > Toon Moene writes:
> > > > 1. When you _really_ want a pointer, e.g. for a linked-list.
> > >
> > > > 2. When you want an allocatable component of a derived type.
> >
> > > I'm coming a bit late in this conversation (been attending a workshop at
> > > the UK Met. Office the last few days) - but isn't (1) just a specific
> > > example of (2) ?
> >
> > Not at all. Indeed almost the opposite. In (1) you really want a
> > pointer because it needs to point to something else, as in a linked
> > list.
>
> Huh? - Perhaps I do not understand "linked lists" ? :-)
>
> TYPE LINKEDLIST
> TYPE (LINKEDLIST), ALLOCATABLE :: NEXT
> TYPE CONTENT
> blah ... blah ... blah
> END TYPE CONTENT
> END TYPE LINKEDLIST
>
> ....
>
> TYPE(LINKEDLIST) THEHEAD
>
> ....
>
> ! Start linked list
>
> ALLOCATE(THEHEAD % NEXT, bla-dee-blah, STAT=ILOVEYOU)
>
> I must be missing something ...
>
> --
> Toon Moene - mailto:[log in to unmask] - phoneto: +31 346 214290
> Saturnushof 14, 3738 XG Maartensdijk, The Netherlands
> GNU Fortran 77: http://gcc.gnu.org/onlinedocs/g77_news.html
> GNU Fortran 95: http://g95.sourceforge.net/ (under construction)
>
--
** Whether the playing field is level depends on the coordinate system. ***
********* Peter S. Shenkin; Schrodinger, Inc.; (201)433-2014 x111 *********
*********** [log in to unmask]; http://www.schrodinger.com ***********
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|