AL281785@VMTECSLP.BITNET (RoDoGu) (04/05/91)
On Thu, 4 Apr 91 11:59:50, Ole Bjorn Tuftedal <ob@IFI.UIB.NO>, wrote : >I have read with interest the recent messages about abstract >data types and generic modules. >In three following messages I'll post a generic sort by Richard S. Wiener, >from SIGPLAN Notices, V19 #3, March 1984, pp 66-72. I have tested it >on Logitech Modula-2 v. 3.x for MSDOS. In the compare procedure you could take advantage of the pointer compatibility. For example : If the compare procedure is defined as : TYPE CompareProc = PROCEDURE(ADDRESS,ADDRESS) : BOOLEAN; In your aplication program your can do this : TYPE Person = RECORD Number : CARDINAL; Name : ARRAY 0..19| OF CHAR; Age : CARDINAL END (* record *); PtrPerson = POINTER TO Person; . . . PROCEDURE ComparePerson(p1,p2 : PtrPerson) : BOOLEAN; BEGIN RETURN p1^.Number > p2^.Number END ComaparePerson; The procedure ComparePerson is fully compatible with CompareProc. On the other hand, I think that you could maintain the array manipulation outsides the sort procedure. I believe that this is another level of abstraction. This is another example of generic sort. This procedure is provided by JPI in its module LIB. Its works! : TYPE CompareProc = PROCEDURE(CARDINAL, CARDINAL) : BOOLEAN; SwapProc = PROCEDURE(CARDINAL, CARDINAL); PROCEDURE QSort(N: CARDINAL; Less: CompareProc; Swap: SwapProc); PROCEDURE Sort(l,r: CARDINAL); VAR i,j:CARDINAL; BEGIN WHILE r > l DO i := l+1; j := r; WHILE i <= j DO WHILE (i <= j) AND NOT Less(l,i) DO INC(i) END; WHILE (i <= j) AND Less(l,j) DO DEC(j) END; IF i <= j THEN Swap(i,j); INC(i); DEC(j) END; END; IF j # l THEN Swap(j,l) END; IF j+j > r+l THEN Sort(j+1,r); r := j-1; ELSE Sort(l,j-1); l := j+1; END; END; END Sort; Instituto Tecnologico y de Estudios Superiores de Monterrey, Campus San Luis ROberto DOminguez GUtierrez