Article View: comp.lang.modula2
Article #1930Re: : Abstract data types and generics
From: Volker Koenig
Date: Wed, 10 Apr 1991 03:04
Date: Wed, 10 Apr 1991 03:04
87 lines
2774 bytes
2774 bytes
Hi Warwick!
In a messages sent 06 Apr 91 to All Warwick Allison wrote:
>> PROCEDURE QSort(N: CARDINAL; Less: CompareProc; Swap: SwapProc);
>>
>> PROCEDURE Sort(l,r: CARDINAL);
>> ...
>> END Sort;
WA> And????
WA> BEGIN
WA> Sort(1,N)
WA> END Qsort;
WA> Very interesting code though.
Yes, that's true.
But I don't need to sort abstract arrays since I mostly make use of dynamic structures. Taking a bath I just got an idea how to change this sorting-procedure to be useed with dynamic structures.
But there should be some conventions to make it less complicated.
You will have to define dynamic data types as RECORDs (as it will mostly be done). The pointers linking them should be the very first two elements of these records. They sould have a defined order. So a data structure would have to look like
RECORD
prev,
next : POINTER TO RECORD;
data : AnythingElse
END;
You will then have to define a CompareProc for your structure as
PROCEDURE(ADDRESS,ADDRESS):BOOLEAN;
to sort the nodes of the list in ascending order it should be defined as
ADDRESS^.data > ADDRESS^.data
to return TRUE in case the items are not in right order.
An external individual SwapProc is not needed since the swapping can be done by changing the links.
Now you will have to define the header of the Sort like this:
PROCEDURE Sort(Root:ADDRESS; MustSwap:CompareProc);
TYPE AbstractPtr : POINTER TO Abstract;
Abstract : RECORD
prev,
next : AbstractPtr
END;
VAR Ptr : AbstractPtr;
PROCEDURE Swap(Ptr1,Ptr2);
(* re-links the two elements by changing their pointers *)
BEGIN
Ptr := Root;
(* insert your favourite sorting-routine for dynamit structures here, with perhaps something like :
IF MustSwap(Ptr1,Ptr2) THEN Swap(Ptr1,Ptr2) END;
*)
END Sort;
Does anyone have a complete source for such a thing or have I been the first one to use something like it (I don's hope)?
Volker.
--
uucp: uunet!m2xenix!puddle!2!241!4031.3!Volker.Koenig
Internet: Volker.Koenig@p3.f4031.n241.z2.fidonet.org
Technical Details
Message-ID:
<1749.2805CB24@puddle.fidonet.org>Path:
arc1-ca.pugleaf.net!utzoo!utgpu!news-server.csri.toronto.edu!rpi!usc!sdd.hp.com!mips!cs.uoregon.edu!ogicse!zephyr.ens.tek.com!tektronix!percy!m2xenix!puddle!p3.f4031.n241.z2.fidonet.org!Volker.KoenigShow Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!utgpu!news-server.csri.toronto.edu!rpi!usc!sdd.hp.com!mips!cs.uoregon.edu!ogicse!zephyr.ens.tek.com!tektronix!percy!m2xenix!puddle!p3.f4031.n241.z2.fidonet.org!Volker.Koenig From: Volker.Koenig@p3.f4031.n241.z2.fidonet.org (Volker Koenig) Newsgroups: comp.lang.modula2 Subject: Re: : Abstract data types and generics Message-ID: <1749.2805CB24@puddle.fidonet.org> Date: 10 Apr 91 03:04:36 GMT Sender: ufgate@puddle.fidonet.org (newsout1.26) Organization: FidoNet node 2:241/4031.3 - Destroy, Ratingen Lines: 87