Text preview for : Sort.mesa_Sep78.pdf part of xerox Sort.mesa Sep78 xerox mesa 4.0_1978 listing Mesa_4_Lister Sort.mesa_Sep78.pdf



Back to : Sort.mesa_Sep78.pdf | Home

Sort.mesa 2-Sep-78 18:18:51 Page 1


--file Sort.mesa
--last modified by Bruce on July 30, 1978 4:30 PM
-- translated from Ed McCreight's BCPL by Jim Frandeen
DIRECTORY
AltoDefs: FROM "AltoDefs",
FSPDefs: FROM "fspdefs" USING [NoRoomInZone, NodeOverhead],
GPsortDefs: FROM "GPsortDefs",
InlineDefs: FROM "inlinedefs" USING [COPY],
StreamDefs: FROM "StreamDefs";
DEFINITIONS FROM GPsortDefs;
Sort: PROGRAM
IMPORTS FSPDefs, GPsortDefs, StreamDefs
EXPORTS GPsortDefs m
BEGIN
NFiles: CARDINAL = 3; -- number of scratch files

bufferSize: INTEGER;
compareProc: CompareProcType;
files: ARRAY [0 .. NFiles] OF FdHandle;
firstFreeEnt: CARDINAL; -- 1 + end of unsorted part of heap vector
getProc: GetProcType;
heap: DESCRIPTOR FOR ARRAY OF ItemHeaderHandle;
heapSize: CARDINAL; -- end of heap-sorted part of heap vector
inputFinished: BOOLEAN;
itemIsLeftOver: BOOLEAN;
leftoverItem: ItemHandle;
leftoverItemLen: ItemLength:
level: CARDINAL;
maxHeapSize: CARDINAL;
maxItemWords: CARDINAL;
occItemWords: CARDINAL;
putProc: PutProcType;
recordSize: CARDINAL;
RecordTooLong: PUBLIC ERROR CODE;
BuildHeap: PROCEDURE
BEGIN
L: CAkDINAL;
heapSize +- 0;
MaintainHeap[];
heapSize +- firstFreeEnt-1;
L +- (heapSize/2)+1;
WHILE L > 1 DO
L +- L-1;
SiftDown[L,heap[L]]:
ENDLOOP;
RETURN:
END:
BuildRuns: PROCEDURE
BEGIN
--Continue reading and sorting, alternating in Fibonacci sequence, until the input is exhausted.
A: CARDINAL;
item: ItemlleaderHandle:
i: CARDINAL:
j: CARDINAL +- 1;
LFile: FcHlandle:
NT: CARDINAL;
1 eve 1 .- 1;
00 OPEN files[j];
IF level> 1 THEN dh.put[dh,EOR]; -- end-of-run marker
FOR item +- GetHeap[], GetHeap[] UNTIL item