-- CoordRectPQImpl.mesa -- A generic package to organize items -- in a priority queue, sorted by increasing order of -- Val. -- customized by E. McCreight, May 21, 1982 2:11 PM -- written by E. McCreight, February 8, 1982 2:04 PM -- last modified by E. McCreight, February 8, 1982 2:20 PM DIRECTORY CoordRectPQ; CoordRectPQImpl: PROGRAM IMPORTS CoordRectPQ EXPORTS CoordRectPQ SHARES CoordRectPQ = BEGIN OPEN CoordRectPQ; CoordRectPQUnderflow: PUBLIC SIGNAL = CODE; NewCoordRectPQ: PUBLIC PROCEDURE[zone: UNCOUNTED ZONE, max: CARDINAL _ 50] RETURNS[PQHandle] = {RETURN[zone.NEW[PQObject _ [zone: zone, size: 0, s: zone.NEW[PQSeq[max]]]]]}; DestroyCoordRectPQ: PUBLIC PROCEDURE[p: PQHandle] RETURNS[PQHandle] = BEGIN zone: UNCOUNTED ZONE _ p.zone; zone.FREE[@p.s]; zone.FREE[@p]; RETURN[NIL]; END; -- of DestroyCoordRectPQ InsertCoordRectPQ: PUBLIC PROCEDURE[p: PQHandle, item: Item] = BEGIN i, j: CARDINAL; pqs: PQSeqPtr _ GrowingPQ[p]; p.size _ p.size+1; FOR j _ p.size-1, i WHILE j>0 DO i _ (j-1)/2; IF Val[item]>=Val[pqs.heap[i]] THEN EXIT ELSE pqs.heap[j] _ pqs.heap[i]; ENDLOOP; pqs.heap[j] _ item; END; ExtractCoordRectPQ: PUBLIC PROCEDURE[p: PQHandle] RETURNS[item: Item] = BEGIN pqs: PQSeqPtr _ ShrinkingPQ[p]; result: Item _ pqs.heap[0]; IF (p.size _ p.size-1)>0 THEN BEGIN i, j: CARDINAL; r: CARDINAL _ p.size-1; -- max index item: Item _ pqs.heap[p.size]; FOR i _ 0, j DO j _ i+i+1; IF j>r THEN EXIT; IF j=p.s.max THEN BEGIN oldSeq: PQSeqPtr _ p.s; newSeq: PQSeqPtr _ p.zone.NEW[PQSeq[MAX[(15*p.size)/10, 50]]]; FOR i: CARDINAL IN [0..p.size) DO newSeq[i] _ oldSeq[i]; ENDLOOP; p.s _ newSeq; p.zone.FREE[@oldSeq]; END; RETURN[p.s]; END; ShrinkingPQ: PROCEDURE[p: PQHandle] RETURNS[PQSeqPtr] = BEGIN WHILE p.size<1 DO SIGNAL CoordRectPQUnderflow ENDLOOP; IF MAX[p.size, 50]