CDHacks.mesa 
Copyright © 1986 by Xerox Corporation. All rights reserved.
Barth, December 5, 1986 4:22:13 pm PST
Bertrand Serlet February 20, 1987 0:55:30 am PST
DIRECTORY CD, CDCells, GList, HashTable;
CDHacks: CEDAR PROGRAM
IMPORTS CDCells, GList, HashTable
SHARES CDCells =
BEGIN
RefIntList: TYPE = LIST OF REF INT;
CountInstances: PROC [object: CD.Object] RETURNS [instanceCounts: RefIntList] = {
RecurseCountInstances: PROC [object: CD.Object, oldInstanceCount: RefIntList] RETURNS [instanceCounts: RefIntList] = {
instanceCounts ← oldInstanceCount;
IF HashTable.Fetch[objectVisitTable, object].found THEN RETURN;
IF NOT HashTable.Insert[objectVisitTable, object, $Visited] THEN ERROR;
IF object.class=CDCells.pCellClass THEN {
EachSubObject: CDCells.InstEnumerator = {
instanceCounts ← RecurseCountInstances[inst.ob, instanceCounts];
};
ic: INT ← CDCells.CountInstances[object];
FOR icl: RefIntList ← instanceCounts, icl.rest UNTIL icl=NIL DO
IF icl.first^=ic THEN EXIT;
REPEAT FINISHED => instanceCounts ← CONS[NEW[INT ← ic], instanceCounts];
ENDLOOP;
[] ← CDCells.EnumerateInstances[object, EachSubObject];
};
};
ReverseIntCompare: GList.CompareProc = {
int1: REF INTNARROW[ref1];
int2: REF INTNARROW[ref2];
RETURN[SELECT TRUE FROM
int1^<int2^ => greater,
int1^>int2^ => less,
ENDCASE => equal];
};
objectVisitTable: HashTable.Table ← HashTable.Create[];
instanceCounts ← RecurseCountInstances[object, NIL];
instanceCounts ← NARROW[GList.Sort[instanceCounts, ReverseIntCompare]];
};
END.