comp.lang.ada
 help / color / mirror / Atom feed
* Some questions on a library design
@ 1997-06-16  0:00 Brian Rogoff
  1997-06-17  0:00 ` Robert A Duff
  1997-06-20  0:00 ` Tucker Taft
  0 siblings, 2 replies; 11+ messages in thread
From: Brian Rogoff @ 1997-06-16  0:00 UTC (permalink / raw)



Hi,
	In the process of writing a collection library for Ada I have 
encountered a few stumbling blocks that I hope can be removed. The library 
is rather like the C++ STL, and so I avoid dynamic dispatching 
*completely*. I have also avoided tagged types although that is not
strictly necessary.

	One of the containers in the library is a generic set, whose 
specification looks like this 

generic
    type Element_Type is private;
    with function "<" ( Left, Right : Element_Type ) return Boolean is <>;
package AGL.Sets is
    type Set_Type is private;
    type Value_Type is private;
    function Is_Member ( Set : Set_Type; Key : Value_Type ) return Boolean;
    ... etc. ...
end AGL.Sets;

and a child package Iterators which instantiates its own textually nested 
packages corresponding to the kinds of traversal it supports

generic
package AGL.Sets.Iterators is
    type Iterator_Type is private;

    function Start ( Set : Set_Type ) return Iterator_Type;
    function Finish ( Set : Set_Type ) return Iterator_Type;

    ... etc. ...
    package Forward_Iterators is
      new AGL.Forward_Iterators(Value_Type, Iterator_Type, Value_Ptr,
                                Next, Get_Value, Set_Value, Get_Pointer);

    package Bidirectional_Iterators is
      new AGL.Bidirectional_Iterators(Value_Type,Iterator_Type,Value_Ptr,
                                      Next,Prev,Get_Value,Set_Value,
                                      Get_Pointer,"=");

end AGL.Sets.Iterators;

The packages AGL.Whatever_Iterators are null bodied generic signature 
packages, and their instantiations in the Whatever_Container.Iterators 
package are "plugged" in to the various algorithms which only know about 
the iterator signature.

I have a Red_Black_Trees package which also has an Iterators child 
package, and I would like to use them to implement the Sets/Sets.Iterators 
(and of course Maps/Maps.Iterators, Multisets/Multisets.Iterators, etc.). 
I'd like for aesthetic reasons (;-)) to keep this implementation choice 
private (I'll have a Splay_Trees package any time now). However, I am 
having a bit of trouble figuring out how to do this; so far I just declare
the Red_Black_Trees package in the public part of Sets' spec, and do the
same with Red_Black_Trees.Iterators in Sets.Iterators, like so:

generic
    type Element_Type is private;
    with function "<" ( Left, Right : Element_Type ) return Boolean is <>;
package AGL.Sets is
    -- Our Set_Type is implemented as a Red_Black_Tree
    package Trees is new AGL.Red_Black_Trees( Element_Type => Element_Type,
                                              Insert_Always => False,
                                              "<" => "<" );

    subtype Set_Type is Trees.Tree_Type;
    subtype Value_Type is Trees.Value_Type;
    subtype Value_Ptr is Trees.Value_Ptr;

    ... etc. ...
end AGL.Sets;

generic
package AGL.Sets.Iterators is
    package Tree_Iterators is new Trees.Iterators;
    subtype Iterator_Type is Tree_Iterators.Iterator_Type;
    ... etc. ...
end AGL.Sets.Iterators;

I find it inelegant in that I'd prefer that representation information be 
restricted to the private part of a package, even though that seems to 
be necessary in order to instantiate the signature packages in 
Sets.Iterators. Any suggestions on how to do this better?

Also, I thought it would be cool if I could do a partial instantiation of 
a generic package to create a new generic package with fewer parameters,
like 

generic package Sets is new Red_Black_Trees ( Insert_Always => False );
generic package Multisets is new Red_Black_Trees ( Insert_Always => True );

Am I missing something, i.e., is there a good approximation to this
functionality in Ada?

-- Brian






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

end of thread, other threads:[~1997-06-23  0:00 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1997-06-16  0:00 Some questions on a library design Brian Rogoff
1997-06-17  0:00 ` Robert A Duff
1997-06-17  0:00   ` Brian Rogoff
1997-06-18  0:00     ` Robert A Duff
1997-06-18  0:00       ` Brian Rogoff
1997-06-20  0:00         ` Stephen Leake
1997-06-20  0:00           ` Brian Rogoff
1997-06-21  0:00   ` Nick Roberts
1997-06-22  0:00     ` Robert Dewar
1997-06-23  0:00     ` Tucker Taft
1997-06-20  0:00 ` Tucker Taft

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