home *** CD-ROM | disk | FTP | other *** search
- (**********************************************************)
- (* Modula-Kurs Teil 6 *)
- (* (C) Peter Viczena & toolbox 1990 *)
- (**********************************************************)
-
- MODULE Qsort1;
- (* ein kurzes Beispiel, wie man PointerQuickSort zur Sortie-
- rung von Integerzahlen verwenden kann. *)
-
- FROM InOut IMPORT WriteString,WriteInt,WriteLn;
- FROM IO IMPORT WrLngCard;
- FROM Storage IMPORT ALLOCATE,DEALLOCATE;
- (* FROM SYSTEM IMPORT LONG; *)
- FROM SortMerg IMPORT Ptr,CompareResult,CProc,
- PointerQuickSort;
-
- CONST nmax = 20; (* Zahl der zu sortierenden Elemente *)
- Greater = 1; (* evtl. ändern *)
- Equal = 0;
- Less = -1;
- TYPE IntPtr = POINTER TO LONGCARD; (* INTEGER *)
- (* evtl. ändern *)
- VAR a : ARRAY [1..nmax] OF Ptr;
- i,j : INTEGER;
- dummypt : IntPtr; (* Hilfspointer *)
-
- (* This is an example of a user-defined procedure that com-
- pares the values to which its two arguments point. Note
- that you have to use two pointers of the appropriate type
- to do the comparison. This may seem inefficient, but it
- is necessary (how else are you going to compare any-
- thing?). Anyway, a good compiler will not produce any
- extra code for the variables ah and bh. *)
-
- PROCEDURE CompareInt(a,b:Ptr):CompareResult;
-
- (* Diese Prozedur zeigt, wie eine benutzerdefinierte Ver-
- gleichsoperation aufgebaut sein kann. Beachten Sie, daß
- die unten benutzten Pointer vom richtigen Typ sein müs-
- sen. Da diese Pointer aber kein Allocate machen, wird
- kein überflüssiger speicherplatz verbraucht. *)
-
- VAR ah,bh: IntPtr;
-
- BEGIN (* ah zeigt jetzt auf denselben Inhalt wie a *)
- ah := a; bh := b;
- IF ah^ > bh^ THEN
- RETURN Greater
- ELSIF ah^ = bh^ THEN
- RETURN Equal
- ELSE
- RETURN Less
- END;
- END CompareInt; (* Vergleich der Inhalte von ah und bh *)
-
- BEGIN (* Hauptprogramm *)
-
- WriteString('BEGIN Qsort1'); WriteLn;
- WriteString('raw data:'); WriteLn;
-
- FOR i := 1 TO nmax DO (* Fülle einen Array mit Werten *)
- ALLOCATE(dummypt,SIZE(dummypt^));
- dummypt^ := (LONGCARD(i) * 35256) MOD 2156;
- a[i] := dummypt; (* evtl. ändern *)
- WrLngCard(dummypt^,10);WriteLn; (* evtl. ändern *)
- END;
-
- PointerQuickSort(a,nmax,CompareInt);
- (* sortiere den Array *)
- WriteString('ordered data:'); WriteLn;
- FOR i := 1 TO nmax DO (* Gib sortierten Array aus *)
- dummypt := a[i];
- WrLngCard(dummypt^,10);WriteLn; (* evtl. ändern *)
- DEALLOCATE(a[i],SIZE(a[i]^)); (* evtl. ändern *)
- END; (* Gibt Speicher wieder frei *)
- WriteString('END Qsort1.'); WriteLn;
-
- END Qsort1. (* Hauptprogramm *)