From: netnews.upenn.edu!uofs!guinness.cs.uofs.edu!beidler@rutgers.edu (Jack Be
Subject: Re: Hopefully simple question regarding generics
Date: 21 Oct 91 18:36:43 GMT [thread overview]
Message-ID: <10285@platypus.uofs.uofs.edu> (raw)
In article <1991Oct16.085901@lglsun.epfl.ch>, gauthier@lglsun.epfl.ch (Michel G
authier) writes:
|>
|> Stephen J Bevan writes:
|>
|> >I read (and think I understand) the following in "Software Engineering
|> >with Ada" by Booch :-
|> >[ generic array sorter's interface deleted ]
|> >
|> >What I'd like to know is how can I change it so that it doesn't assume
|> >the items to sort are in an array i.e. how to I parameterize the above
|> >over the implementation of the items to be sorted. For example, say I
|> >want to sort a list as opposed to an array. With a parameterized
|> >version I could then do something like :-
|> >
|> > procedure STRING_LIST_SORT is new
|> > SORT(ITEM => CHARACTER,
|> > INDEX => POSITIVE,
|> > ITEMS => STRING,
|> > COLLECTION => LIST -- or maybe LIST(INDEX, ITEM) ???
|> > "<" => "<");
|>
Laverman and Gauthier's separate replies gave excellent advise.
Let me take a slightly different approach -- does Bevan need to change the gene
ric sort at all?
Specifically, if the generic sort is something like
generic
type Object_Type is private
type Array_range is (<>) ;
type array_type is array (Array_range) of Object_Type ;
with function "<" (left, right: Object_Type return boolean ;
procedure Sort (An_Array : in out AArray_Type) ;
if the objects are pointers to the values and the sort orders the pointers acco
rding to the
values of the objects being represented by the strings, lists, or whateverthe p
ointers are
pointing to, he can use this generic Sort directly simply by defining a special
purpose
function less_than (left, right : Object_Type) return boolean ;
that does the comparisons he want between the objects he is trying to manipulat
e. Then
use this function to instantiate the "<" generic formal parameter to Sort and h
e's is business.
Further, since he is only moving the locations of the pointers and not trying t
o physically
move the objects being referenced by indirection, he is as safde an he can be,
given that
he is using private types. It might be wise to create a "limited private" vers
ion of this
Sort,
generic
type Object_Type is limited private
type Array_range is (<>) ;
type array_type is array (Array_range) of Object_Type ;
with function "<" (left, right: Object_Type return boolean ;
with procedure SWAP (left, right : in out OBJECT_TYPE) ;
procedure Sort (An_Array : in out AArray_Type) ;
which would need a procedure, like SWAP, to flip the object references around.
--
+-----------------------------------------------------------------------------+
| John (Jack) Beidler |
| Prof. of Computer Science Internet (VAX/VMS) BEIDLER@JAGUAR.UOFS.ED |
| University of Scranton Internet (SUN/UNIX) BEIDLER@GUINNESS.CS.UOFS.EDU|
| Scranton, PA 18510 Bitnet (VAX/VMS) BEIDLER@SCRANTON |
| |
| Phone: (717) 941-7446 FAX: (717) 941-4250 |
+-----------------------------------------------------------------------------+
next reply other threads:[~1991-10-21 18:36 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
1991-10-21 18:36 netnews.upenn.edu!uofs!guinness.cs.uofs.edu!beidler [this message]
-- strict thread matches above, loose matches on Subject: below --
1991-10-23 8:39 Hopefully simple question regarding generics mcsun!hp4nl!gufalet.let.rug.nl!rug4!laverman
1991-10-17 8:59 Stephen J Bevan
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox