comp.lang.ada
 help / color / mirror / Atom feed
* Translate this into Ada ...
@ 1996-08-20  0:00 Richard Irvine
  1996-08-21  0:00 ` Norman H. Cohen
  1996-08-21  0:00 ` David Weller
  0 siblings, 2 replies; 10+ messages in thread
From: Richard Irvine @ 1996-08-20  0:00 UTC (permalink / raw)



Can anyone help with what I imagine is a frequently ocurring problem?

We would like all lists in an application to be read using an iterator
with operations like:

  function  theCurrentItem( theIterator :        Iterator ) return
ItemType;
  function  atTheEnd      ( theIterator :        Iterator ) return
Boolean;
  procedure goToTheNext   ( theIterator : in out Iterator );

Furthermore, since an iterator has to be created for a particular list
and since the implementation of an iterator is dependent on that of the
list 
we would like all list types to have a (constructor) operation like

  function  aNewIteratorFor( theList : List ) return Iterator;

This design decision should apply to all types of list, regardless of
the ItemType and regardless of the implementation of the lists.

How using generics, abstract types (e.g. AbstractList and
AbstractIterator), 
abstract subprograms or any other Ada features can this decision be
expressed? 

I have struggled with this for quite some time without finding a
satisfactory
solution. Any suggestions gratefully received.




^ permalink raw reply	[flat|nested] 10+ messages in thread
* Re: Translate this into Ada ...
@ 1996-08-21  0:00 Spasmo
  1996-08-21  0:00 ` Richard Irvine
  0 siblings, 1 reply; 10+ messages in thread
From: Spasmo @ 1996-08-21  0:00 UTC (permalink / raw)



Richard Irvine (Richard.Irvine@eurocontrol.fr) wrote:
: Can anyone help with what I imagine is a frequently ocurring problem?

: We would like all lists in an application to be read using an iterator
: with operations like:

[Snip]

Well I'm no expert in Ada, but I have successfully written 
generic linked list routines that pretty much do what you want.
Here's some info, but if you would like the actual source code
I'd be more than happy to provide it (I haven't done all that
much debugging but it seems to work and at the very least should
give you an idea of how to proceed).

I did a generic package that was instantiated with the data
type to be stored in the list.  This data type was imbedded
in a record similar to the following:

type List_Node;
type List_Node_Ptr is access List_Node;
type List_Node is 
	record
		Data : Generic_Data;
		Next : List_Node_Ptr;
	end record;

Where Data was what was generically instantiated.  Of course
access restrictions were applied so the user only saw what
I wanted him/her to see.  Anyway as for the list itself I
used a simple singly linked list with head and tail pointers
for simplicity of operations, so the data type that was
immediately visible to the user was something like the following:

type List is private;

private
	type List is
		record
			List_Body, Head, Tail : List_Node_Ptr;
		end record;

So for use the user would say instantiate the package like
the following:
	package My_List_Pkg is new Lists ( Rec );

Then declare lists by:
	My_List : My_List_Pkg.List;

So the package would provide the data type for the list and
also all the operations (iterators, etc...).  I was able to
do linked lists of linked lists like this and so forth.

For multiple linked lists of the same type one could just
use the same instantiation but of course different types
would mean different instantiations but this was very
flexible.  BTW, I really didn't consider my implementation
to be an "iterator", since it provided iterator operations
and the data type itself, so it was more of a linked list
package but the functionality again seems to be what you
are looking for.

One thing that's a problem is if you want a heterogenous list
of objects.  Something like the above would not accomodate 
your needs unfortunately so you'd have to have a linked list
of pointers to class wide types, which isn't hard to achieve
but it's something to keep in mind.  My list package didn't
do this so while it could handle any data type, heterogenous
lists of related classes are pretty much out of the question.
		
Damn I love Ada!

--
Spasmo
"Everyone has secrets, but sometimes you get caught,
 So if it's just between us, my silence can be bought"
	"Blackmail" by Sloppy Seconds





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

end of thread, other threads:[~1996-08-29  0:00 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1996-08-20  0:00 Translate this into Ada Richard Irvine
1996-08-21  0:00 ` Norman H. Cohen
1996-08-22  0:00   ` Richard Irvine
1996-08-22  0:00     ` Richard Irvine
1996-08-22  0:00     ` Norman H. Cohen
1996-08-29  0:00   ` Robert A Duff
1996-08-21  0:00 ` David Weller
  -- strict thread matches above, loose matches on Subject: below --
1996-08-21  0:00 Spasmo
1996-08-21  0:00 ` Richard Irvine
1996-08-28  0:00   ` Robert A Duff

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