-- Set.Mesa
-- Last Modified by Paul Rovner on February 15, 1983 4:13 pm

Set: CEDAR DEFINITIONS =
BEGIN

--This package is intended for use with Cedar for dealing with "sets" of REFs, i.e. variable length collections of elements in which there are no duplicates. Elements are REF ANYs.

-- TYPEs

Handle: TYPE = REF SetRep;
SetRep: TYPE; -- OPAQUE

Element: TYPE = REF ANY;

-- VARIABLES

PHI: Handle; -- the empty set


-- PROCs

New: PROC RETURNS[Handle];
--This creates a new (empty) set.

Equal: PROC[s1, s2: Handle] RETURNS[BOOLEAN];
--This returns TRUE if the two sets (s1 and s2) contain the same elements.

In, Put, Remove: PROC[set: Handle, element: Element] RETURNS[found: BOOLEAN];
--In returns TRUE if the specified Element is a member of the specified set.
--Put causes the specified Element to become a member of the specified set. Put returns TRUE if the specified Element was already in the set (the set is not changed in this case).
--Remove deletes the specified Element from the specified set. It returns FALSE if the element was a member of the set (the set is not changed in this case).

Union, Difference, Intersection: PROC[s1, s2: Handle] RETURNS[ans: Handle];
--These implement the specified operations on sets.

Cardinality: PROC[set: Handle] RETURNS[LONG CARDINAL];
--This returns the number of elements of the specified set.

Enumerate: PROC[set: Handle,
proc: PROC[e: Element] RETURNS[stop: BOOLEAN]]
RETURNS[stopped: BOOLEAN];
--This causes the specified procedure (proc) to be invoked once for each Element of the specified set. If any call of proc returns TRUE, the enumeration is terminated at that point and Enumerate returns TRUE. If no call of proc returns TRUE, then Enumerate returns FALSE. The enumeration order of set elements is arbitrary.
--Modifications may be made to the specified set during its enumeration (e.g. elements added or elements removed by the action of proc), but such elements may or may not be included in the enumeration.

Nth: PROC[set: Handle, index: LONG CARDINAL--from 1--]
RETURNS[element: Element ← NIL];
---This is provided for convenience as an alternative to Enumerate. It returns NIL if index > Cardinality[set]. It returns the nth element of the specified set. The order of set elements is arbitrary.

END.