comp.lang.ada
 help / color / mirror / Atom feed
* RE: MI ammunition : linked lists
@ 2003-11-12 11:29 amado.alves
  0 siblings, 0 replies; 24+ messages in thread
From: amado.alves @ 2003-11-12 11:29 UTC (permalink / raw)
  To: comp.lang.ada

"...so the structural relation between nodes in a graph is
provided by an iterator pointing to 0 or more of the neigbours in some
order. That could be applied to lists as well... "

Exactly. Actually I'm entertaining the idea of a list being a particular case of tree namely of a tree with a fixed or bounded fanout (outdegree) of 1.



^ permalink raw reply	[flat|nested] 24+ messages in thread
* RE: MI ammunition : linked lists
@ 2003-11-06 19:01 amado.alves
  2003-11-11 18:38 ` Georg Bauhaus
  0 siblings, 1 reply; 24+ messages in thread
From: amado.alves @ 2003-11-06 19:01 UTC (permalink / raw)
  To: comp.lang.ada

<<Inheritance should reflect an "is a" relationship. Do you fill comfortable stating that a middle node is a first node and a last
node at the same time?...>>

I do saying a Node_Pointing_Both_Ways is a Node_Pointing_Forward and a Node_Pointing_Backward.

But I agree with the general advice. Thanks.




^ permalink raw reply	[flat|nested] 24+ messages in thread
* RE: MI ammunition : linked lists
@ 2003-11-06 17:33 amado.alves
  0 siblings, 0 replies; 24+ messages in thread
From: amado.alves @ 2003-11-06 17:33 UTC (permalink / raw)
  To: comp.lang.ada

<<Frankly, I just don't see this as the optimal way to implement lists.>>

You're probably right. My idea was making the available operations directly dependent on the node type (e.g. no Previous for First). But it seems the cons (type change) beat the pros. Thanks.




^ permalink raw reply	[flat|nested] 24+ messages in thread
* RE: MI ammunition : linked lists
@ 2003-11-06 17:26 amado.alves
  0 siblings, 0 replies; 24+ messages in thread
From: amado.alves @ 2003-11-06 17:26 UTC (permalink / raw)
  To: Stephen Leake, comp.lang.ada

<<When I insert a node, the "last" node changes type!>>

Yes, I can see that is a problem for Ada. I guess no tagged types for linked lists then.

This must the shortest MI thread ever :-)

Thanks.




^ permalink raw reply	[flat|nested] 24+ messages in thread
* MI ammunition : linked lists
@ 2003-11-06 16:32 amado.alves
  2003-11-06 16:46 ` Stephen Leake
                   ` (4 more replies)
  0 siblings, 5 replies; 24+ messages in thread
From: amado.alves @ 2003-11-06 16:32 UTC (permalink / raw)
  To: comp.lang.ada

A very common programming task where multiple inheritance (MI) would be handy: implementation of doubly linked lists.

A linked list is a chain of nodes. The first node links to the second, the last node links to the penultimate, and each of the others nodes links to both the previous and the next. (Each node usually also carries a payload, which we can ignore here.)

  First <-----> Second <-----> ... <-----> Penultimate <-----> Last

Often (always?) a non OOP design is used with only one type for all kinds of nodes, with null values in the unused slots:
   ___________      ______               ______      ___________
  |           |    |      |             |      |    |           |
  | Prev=null |<-----Prev |<--- ... <-----Prev |<-----Prev      |
  | Next---------->| Next-----> ... --->| Next----->| Next=null |
  |___________|    |______|             |______|    |___________|

An OOP design does away with the ugly nulls (however it introduces one-of-a-kind objects, which also some people consider ugly):
               ______               ______      ______
   ______     |      |             |      |    |      |
  |      |<-----Prev |<--- ... <-----Prev |<-----Prev |
  | Next----->| Next-----> ... --->| Next----->|______|
  |______|    |______|             |______|    

It is clear that the middle nodes combine the attributes of the first and last nodes. This is where linguistic MI could help:

  type Node is tagged ...;

  type Node_Ptr is access Node'Class;

  type First_Node is new Node with record
    Next : Node_Ptr;
  end record;

  type Last_Node is new Node with record
    Prev : Node_Ptr;
  end record;

  type Middle_Node is new First_Node with Last_Node;

Of course the alternative is no big deal, syntactically:

  type Middle_Node is new Node with record
    Prev : Node_Ptr;
    Next : Node_Ptr;
  end record;

But, semantically, MI would allow conversions between Middle_Node and any the other two extended types. Whether this would be useful for linked lists remains to be ascertained. I confess I have not implemented them this way yet.

Well, just a thought for those with too much free time on their hands ;-)



^ permalink raw reply	[flat|nested] 24+ messages in thread

end of thread, other threads:[~2003-11-12 11:29 UTC | newest]

Thread overview: 24+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-11-12 11:29 MI ammunition : linked lists amado.alves
  -- strict thread matches above, loose matches on Subject: below --
2003-11-06 19:01 amado.alves
2003-11-11 18:38 ` Georg Bauhaus
2003-11-11 21:27   ` Marius Amado Alves
2003-11-12  0:23     ` Georg Bauhaus
2003-11-06 17:33 amado.alves
2003-11-06 17:26 amado.alves
2003-11-06 16:32 amado.alves
2003-11-06 16:46 ` Stephen Leake
2003-11-06 17:15 ` Frank J. Lhota
2003-11-08 10:27   ` Dmitry A. Kazakov
2003-11-06 17:16 ` Jean-Pierre Rosen
2003-11-06 18:15   ` Wes Groleau
2003-11-06 21:03   ` Simon Wright
2003-11-07 10:39     ` Dmitry A. Kazakov
2003-11-07 10:29 ` Dmitry A. Kazakov
2003-11-10 14:51 ` Lutz Donnerhacke
2003-11-10 17:52   ` Marius Amado Alves
2003-11-11  9:32     ` Lutz Donnerhacke
2003-11-11 12:24       ` Marius Amado Alves
2003-11-11 12:58         ` Lutz Donnerhacke
2003-11-11 16:09           ` Robert I. Eachus
2003-11-11 17:11             ` Marius Amado Alves
2003-11-12  9:21               ` Lutz Donnerhacke

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox