--File: OrderedSymbolTableRefImpl.mesa
Last Edited by: CSChow, January 28, 1985 2:34:40 am PST
Preas, August 2, 1986 8:15:10 pm PDT
DIRECTORY
List,
OrderedSymbolTableRef;
OrderedSymbolTableRefImpl: CEDAR PROGRAM
IMPORTS List
EXPORTS OrderedSymbolTableRef = BEGIN OPEN OrderedSymbolTableRef;
Table: TYPE = REF Rep;
Rep: PUBLIC TYPE = RECORD [compare: CompareProc, items: LIST OF REF ANY];
CreateTable: PUBLIC PROC [compare: CompareProc] RETURNS [Table] ={
RETURN [NEW[Rep ← [compare: compare, items: NIL]]]
}; --CreateTable
Insert: PUBLIC PROC[tab: Table, item: REF ANY] ={
compare: CompareProc ← tab.compare;
IF List.Memb[item, tab.items] THEN RETURN;
IF tab.items = NIL OR compare[item, tab.items.first] = less
THEN tab.items ← CONS[item, tab.items]
ELSE {
FOR l: LIST OF REF ANY ← tab.items, l.rest UNTIL l = NIL DO
IF l.rest = NIL OR compare[item, l.rest.first] = less THEN {l.rest ← CONS[item, l.rest]; EXIT}
ENDLOOP;}
};-- Insert
DestroyTable: PUBLIC PROC[tab: Table] = {
tab.compare ← NIL;
tab.items ← NIL;
}; --DestroyTable
DeleteAllItems: PUBLIC PROC[tab: Table] = {
tab.items ← NIL;};--DeleteAllItems
LookupSmallest: PUBLIC PROC [tab: Table] RETURNS [REF ANY] = {
RETURN[tab.items.first]
}; --LookupSmallest
LookupNextLarger: PUBLIC PROC [tab: Table, item: REF ANY] RETURNS [REF ANY] = {
nextLarger: REF ANYNIL;
FOR l: LIST OF REF ANY ← tab.items, l.rest UNTIL l = NIL DO
IF item = l.first AND l.rest # NIL THEN {nextLarger ← l.rest.first; EXIT};
ENDLOOP;
RETURN [nextLarger]
}; --LookupNextLarger
EnumerateIncreasing: PUBLIC PROC[tab: Table, action: EachItemAction] = {
FOR l: LIST OF REF ANY ← tab.items, l.rest UNTIL l = NIL DO
IF action[l.first] THEN EXIT;
ENDLOOP;
}; --EnumerateIncreasing
Size: PUBLIC PROC [tab: Table] RETURNS [INT] ={
RETURN [List.Length[tab.items]]
}; --Size
END.