From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.5-pre1 (2020-06-20) on ip-172-31-74-118.ec2.internal X-Spam-Level: X-Spam-Status: No, score=-0.9 required=3.0 tests=BAYES_00,FROM_ADDR_WS autolearn=no autolearn_force=no version=3.4.5-pre1 Date: 21 Oct 91 18:36:43 GMT From: netnews.upenn.edu!uofs!guinness.cs.uofs.edu!beidler@rutgers.edu (Jack Be idler) Subject: Re: Hopefully simple question regarding generics Message-ID: <10285@platypus.uofs.uofs.edu> List-Id: 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 | +-----------------------------------------------------------------------------+