Priming.mesa
Spreitzer, November 3, 1985 6:13:39 pm PST
DIRECTORY IO;
Priming: CEDAR PROGRAM
IMPORTS IO
=
BEGIN
CardTable: TYPE = REF CardTablePrivate;
CardTablePrivate: TYPE = RECORD [
length: NAT ← 0,
cards: SEQUENCE size: NAT OF CARDINAL
];
GenTo: PROC [n, size: CARDINAL] RETURNS [ct: CardTable] = {
nextCenter: CARDINAL ← 6;
nextLow: BOOLTRUE;
ct ← NEW [CardTablePrivate[size]];
ct.length ← 2;
ct[0] ← 2;
ct[1] ← 3;
DO
{
candidate: CARDINAL = IF nextLow THEN (nextCenter-1) ELSE (nextCenter+1);
IF candidate > n THEN EXIT;
FOR j: NAT IN [0 .. ct.length) DO
IF candidate MOD ct[j] = 0 THEN GOTO Fail;
ENDLOOP;
ct[ct.length] ← candidate;
ct.length ← ct.length + 1;
EXITS
Fail => NULL;
};
IF (nextLow ← NOT nextLow) THEN {
IF nextCenter > LAST[CARDINAL]-6 THEN EXIT;
nextCenter ← nextCenter + 6;
};
ENDLOOP
};
FilterUp: PROC [ict: CardTable, factor: REAL, start: REAL ← 0.0] RETURNS [fct: CardTable] = {
seek: REAL ← start;
fct ← NEW [CardTablePrivate[ict.length]];
fct.length ← 0;
FOR i: NAT IN [0 .. ict.length) DO
IF seek <= ict[i] THEN {
fct[fct.length] ← ict[i];
fct.length ← fct.length + 1;
seek ← ict[i] * factor;
};
ENDLOOP;
fct ← fct;
};
FilterDown: PROC [ict: CardTable, factor: REAL, start: REAL ← 65536.0] RETURNS [fct: CardTable] = {
seek: REAL ← start;
fct ← NEW [CardTablePrivate[ict.length]];
fct.length ← 0;
FOR i: NAT DECREASING IN [0 .. ict.length) DO
IF ict[i] <= seek THEN {
fct[fct.length] ← ict[i];
fct.length ← fct.length + 1;
seek ← ict[i] / factor;
};
ENDLOOP;
fct ← fct;
};
ReverseInPlace: PROC [ct: CardTable] = {
FOR i: NAT IN [0 .. ct.length/2) DO
j: NAT ← ct.length-1-i;
c: CARDINAL ← ct[i]; ct[i] ← ct[j]; ct[j] ← c;
ENDLOOP;
};
PrettyPrint: PROC [to: IO.STREAM, ct: CardTable, width: NAT] = {
to.PutF["\n%lindex +", [rope["f"]] ];
FOR i: NAT IN [0 .. width) DO to.PutF[" %05g", [integer[i]]] ENDLOOP;
to.PutRope["\n-------"];
FOR i: NAT IN [0 .. width) DO to.PutRope["------"] ENDLOOP;
FOR i: NAT IN [0 .. ct.length) DO
IF i MOD width = 0 THEN to.PutF["\n%05g |", [integer[i]]];
to.PutF[" %05g", [cardinal[ct[i]]]];
ENDLOOP;
to.PutF["%l\n", [rope["F"]] ];
};
ListPrint: PROC [to: IO.STREAM, ct: CardTable] = {
first: BOOLTRUE;
FOR i: NAT IN [0 .. ct.length) DO
IF first THEN first ← FALSE ELSE to.PutRope[", "];
to.PutF[" %05g", [cardinal[ct[i]]] ];
ENDLOOP;
first ← first;
};
END.