-- HistogramImpl.mesa

-- Implementation for Histogram.mesa

-- M. D. Schroeder, January 7, 1983 1:30 pm

DIRECTORY
Histogram,
IO USING [ PutF, 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;
s.PutF["\nave. value is %f\n", [real[REAL[sumOfProd]/REAL[entries]]]];
END;

END...