HistogramImpl.mesa
Copyright © 1987 by Xerox Corporation. All rights reserved.
M. D. Schroeder, January 7, 1983 1:30 pm
Ron Weaver February 9, 1987 2:29:08 pm PST
DIRECTORY
Histogram,
IO USING [ PutF, PutRope, STREAM ],
Rope USING [ ROPE ];
HistogramImpl: CEDAR PROGRAM IMPORTS IO EXPORTS Histogram = BEGIN
Gram: TYPE = REF GramObject;
GramObject: PUBLIC TYPE = RECORD [ f: CARDINAL, d: SEQUENCE l: CARDINAL OF INT];
NewGram:
PUBLIC
PROCEDURE [ first, last:
CARDINAL ]
RETURNS [ Gram ] =
BEGIN
g: Gram = NEW [GramObject[last - first + 1]];
g.f ← first;
FOR i:CARDINAL IN [0..g.l) DO g.d[i] ← 0; ENDLOOP;
RETURN [g];
END;
DataPoint:
PUBLIC
PROCEDURE [ g: Gram, index:
INT ] =
{ g.d[index-g.f] ← g.d[index-g.f] + 1 };
Print:
PUBLIC
PROCEDURE [ g: Gram, s:
IO.
STREAM, title: Rope.
ROPE ] =
BEGIN
entries, sumOfProd: INT ← 0;
s.PutF[ "%g\n", [rope[title]] ];
FOR i:
CARDINAL
IN [0..g.l)
DO
IF g.d[i]#0 THEN s.PutF[ "%g %g\n", [cardinal[i+g.f]], [integer[g.d[i]]] ];
entries ← entries + g.d[i];
sumOfProd ← sumOfProd + g.d[i]*i;
ENDLOOP;
IF entries = 0 THEN {s.PutRope["\nave. value is infinity\n"]; RETURN};
s.PutF["\nave. value is %f\n", [real[REAL[sumOfProd]/REAL[entries]]]];
END;
END...