Article View: comp.lang.modula2
Article #1888Re : Abstract data types and generics
From: RoDoGu
Date: Thu, 04 Apr 1991 18:11
Date: Thu, 04 Apr 1991 18:11
86 lines
2750 bytes
2750 bytes
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
Technical Details
Message-ID:
<INFO-M2%91040413181133@UCF1VM.BITNET>Path:
arc1-ca.pugleaf.net!utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!sdd.hp.com!elroy.jpl.nasa.gov!decwrl!ucbvax!VMTECSLP.BITNET!AL281785Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!sdd.hp.com!elroy.jpl.nasa.gov!decwrl!ucbvax!VMTECSLP.BITNET!AL281785 From: AL281785@VMTECSLP.BITNET (RoDoGu) Newsgroups: comp.lang.modula2 Subject: Re : Abstract data types and generics Message-ID: <INFO-M2%91040413181133@UCF1VM.BITNET> Date: 4 Apr 91 18:11:45 GMT Sender: daemon@ucbvax.BERKELEY.EDU Reply-To: Modula2 List <INFO-M2%UCF1VM.BITNET@ucf1vm.cc.ucf.edu> Distribution: world Organization: The Internet Lines: 86