LichenSetTheory:
CEDAR
DEFINITIONS =
BEGIN
NotMutable: ERROR;
Mapper: TYPE = REF MapperPrivate;
MapperPrivate:
TYPE =
RECORD [
class: MapperClass,
mutable: BOOL ← TRUE,
data: REF ANY];
MapperClass: TYPE = REF MapperClassPrivate;
MapperClassPrivate:
TYPE =
RECORD [
Map: PROC [m: Mapper, domain: REF ANY] RETURNS [range: REF ANY],
SetMapping: PROC [m: Mapper, domain, range: REF ANY] RETURNS [hadMapping: BOOL],
Enumerate: PROC [m: Mapper, Consume: PROC [domain, range: REF ANY]],
Size: PROC [m: Mapper] RETURNS [INT]
];
Map:
PROC [mapper: Mapper, arg:
REF
ANY]
RETURNS [result:
REF
ANY]
= INLINE {result ← mapper.class.Map[mapper, arg]};
SetMapping:
PROC [mapper: Mapper, domain, range:
REF
ANY]
RETURNS [hadMapping:
BOOL]
= INLINE {IF NOT mapper.mutable THEN NotMutable[] ELSE hadMapping ← mapper.class.SetMapping[mapper, domain, range]};
EnumerateMapping:
PROC [mapper: Mapper,
Consume:
PROC [domain, range:
REF
ANY]]
= INLINE {mapper.class.Enumerate[mapper, Consume]};
MapSize:
PROC [mapper: Mapper]
RETURNS [size:
INT]
= INLINE {size ← mapper.class.Size[mapper]};
FreezeMapper:
PROC [mapper: Mapper]
= INLINE {mapper.mutable ← FALSE};
CreateHashMapper: PROC [equal: HashTable.EqualProc ← NIL, hash: HashTable.HashProc ← NIL] RETURNS [m: Mapper];
CreateHashDictionary: PROC [caseSensitive: BOOL] RETURNS [m: Mapper];
Set: TYPE = REF SetPrivate;
SetPrivate:
TYPE =
RECORD [
class: SetClass ← NIL,
mutable: BOOL ← TRUE,
data: REF ANY ← NIL];
SetClass: TYPE = REF SetClassPrivate;
SetClassPrivate:
TYPE =
RECORD [
TestMembership: PROC [set: Set, elt: REF ANY] RETURNS [BOOL],
UnionSingleton: PROC [set: Set, elt: REF ANY] RETURNS [new: BOOL],
UnionSet: PROC [self, other: Set],
RemoveElt: PROC [set: Set, elt: REF ANY],
Enumerate: PROC [set: Set, Consumer: PROC [REF ANY]],
Erase: PROC [set: Set],
Size: PROC [set: Set] RETURNS [INT]
];
DontUnionSingleton: PROC [set: Set, elt: REF ANY] RETURNS [new: BOOL];
DontUnionSet: PROC [self, other: Set];
DontRemoveElt: PROC [set: Set, elt: REF ANY];
DontErase: PROC [set: Set];
HasMember:
PROC [set: Set, elt:
REF
ANY]
RETURNS [
BOOL]
= INLINE {RETURN[set.class.TestMembership[set, elt]]};
UnionSingleton:
PROC [set: Set, elt:
REF
ANY]
RETURNS [new:
BOOL]
= INLINE {IF NOT set.mutable THEN NotMutable[] ELSE new ← set.class.UnionSingleton[set, elt]};
UnionSet:
PROC [self, other: Set]
= INLINE {IF NOT self.mutable THEN NotMutable[] ELSE self.class.UnionSet[self, other]};
RemoveElt:
PROC [set: Set, elt:
REF
ANY]
= INLINE {IF NOT set.mutable THEN NotMutable[] ELSE set.class.RemoveElt[set, elt]};
Enumerate:
PROC [set: Set,
Consumer:
PROC [
REF
ANY]]
= INLINE {set.class.Enumerate[set, Consumer]};
Erase:
PROC [set: Set]
= INLINE {IF NOT set.mutable THEN NotMutable[] ELSE set.class.Erase[set]};
Size:
PROC [set: Set]
RETURNS [
INT]
= INLINE {RETURN[set.class.Size[set]]};
FreezeSet:
PROC [set: Set]
= INLINE {set.mutable ← FALSE};
DestroySet:
PROC [set: Set]
= INLINE {set.class ← NIL; set.data ← NIL};
CreateSingleton: PROC [elt: REF ANY] RETURNS [set: Set];
CreateHashSet: PROC [equal: HashTable.EqualProc ← NIL, hash: HashTable.HashProc ← NIL] RETURNS [set: Set];
SetList: TYPE = LIST OF Set;
CreateUnion:
PROC [sets: SetList, eltsImmutable:
BOOL ←
TRUE]
RETURNS [Set];
If eltsImmutable, the sets cannot change their contents.
END.