SetBasicsImpl.Mesa
Last tweaked by Mike Spreitzer on December 21, 1987 3:45:57 pm PST
DIRECTORY AbSets, Atom, Basics, Rope, RopeHash, SetBasics;
SetBasicsImpl:
CEDAR
MONITOR
LOCKS lp USING lp: LockPtr
IMPORTS Basics, Rope, RopeHash, SetBasics
EXPORTS SetBasics, AbSets
=
BEGIN OPEN SetBasics;
LockPtr: TYPE ~ LONG POINTER TO MONITORLOCK;
noRef: PUBLIC NoRef ~ NEW [NoReferent];
noValue: PUBLIC Value ~ [ra: noRef, i: 0];
noMaybe: PUBLIC MaybeValue ~ [found: FALSE, it: noValue];
fullInterval: PUBLIC Interval ~ ALL[noValue];
BasicContains: PROC [data: REF ANY, v: Value] RETURNS [BOOL] --TestProc-- ~ {RETURN[TRUE]};
refs: PUBLIC Space ~ NEW [SpacePrivate ← [Contains: BasicContains, Equal: RefsEqual, Hash: RefsHash, Compare: RefsCompare, name: "refs"]];
RefsEqual: PROC [data: REF ANY, v1, v2: Value] RETURNS [BOOL] --EqualProc-- ~ {RETURN [v1.ra=v2.ra]};
RefsHash: PROC [data: REF ANY, v: Value] RETURNS [CARDINAL] --HashProc-- ~ {RETURN HashRefI[v.ra]};
RefsCompare: PROC [data: REF ANY, v1, v2: Value] RETURNS [Comparison] --CompareProc-- ~ {RETURN CompareRefI[v1.ra, v2.ra]};
ints: PUBLIC Space ~ NEW [SpacePrivate ← [Contains: BasicContains, Equal: IntsEqual, Hash: IntsHash, Compare: IntsCompare, name: "ints"]];
IntsEqual: PROC [data: REF ANY, v1, v2: Value] RETURNS [BOOL] --EqualProc-- ~ {RETURN [v1.i=v2.i]};
IntsHash: PROC [data: REF ANY, v: Value] RETURNS [CARDINAL] --HashProc-- ~ {RETURN [HashIntI[v.i]]};
IntsCompare: PROC [data: REF ANY, v1, v2: Value] RETURNS [Comparison] --CompareProc-- ~ {RETURN CompareIntI[v1.i, v2.i]};
ropes:
PUBLIC
ARRAY
--case matters--
BOOL
OF Space ~ [
FALSE: NEW [SpacePrivate ← [Contains: RopesContains, Equal: RopesEqual, Hash: RopesHash, Compare: RopesCompare, name: "ropes mod case", data: NEW [BOOL ← FALSE]]],
TRUE: NEW [SpacePrivate ← [Contains: RopesContains, Equal: RopesEqual, Hash: RopesHash, Compare: RopesCompare, name: "ropes with case", data: NEW [BOOL ← TRUE]]]];
RopesContains:
PROC [data:
REF
ANY, v: Value]
RETURNS [
BOOL] ~ {
RETURN [
WITH v.ra
SELECT
FROM
x: ROPE => TRUE,
ENDCASE => FALSE]};
RopesHash:
PROC [data:
REF
ANY, v: Value]
RETURNS [
CARDINAL] ~ {
r: ROPE ~ NARROW[v.VA];
case: REF BOOL ~ NARROW[data];
RETURN [RopeHash.FromRope[r, case^]]};
RopesEqual:
PROC [data:
REF
ANY, v1, v2: Value]
RETURNS [
BOOL] ~ {
r1: ROPE ~ NARROW[v1.VA];
r2: ROPE ~ NARROW[v2.VA];
case: REF BOOL ~ NARROW[data];
RETURN r1.Equal[r2, case^]};
RopesCompare:
PROC [data:
REF
ANY, v1, v2: Value]
RETURNS [Comparison] ~ {
r1: ROPE ~ NARROW[v1.VA];
r2: ROPE ~ NARROW[v2.VA];
case: REF BOOL ~ NARROW[data];
RETURN r1.Compare[r2, case^]};
spaceSpace: PUBLIC Space ~ NEW [SpacePrivate ← [Contains: SpacesContains, Equal: RefsEqual, Hash: RefsHash, Compare: RefsCompare, name: "spaces"]];
SpacesContains:
PROC [data:
REF
ANY, v: Value]
RETURNS [
BOOL] ~ {
RETURN [
WITH v.ra
SELECT
FROM
x: Space => TRUE,
ENDCASE => FALSE]};
UpdateSpaceOther:
PUBLIC
PROC [s: Space,
Update:
PROC [Atom.PropList]
RETURNS [Atom.PropList]] ~ {
WithLock:
ENTRY
PROC [lp: LockPtr] ~ {
ENABLE UNWIND => NULL;
s.other ← Update[s.other];
RETURN};
TRUSTED {WithLock[@s.LOCK]};
RETURN};
Lexer: TYPE ~ REF LexerPrivate;
LexerPrivate: TYPE ~ RECORD [prefix, repeat: SpaceList];
CreateLORASpace:
PUBLIC
PROC [prefix, repeat: SpaceList]
RETURNS [space: Space] ~ {
Fmt:
PROC [sl: SpaceList]
RETURNS [rope:
ROPE ←
NIL] ~ {
FOR sl ← sl, sl.rest
WHILE sl#
NIL
DO
rope ← IF rope=NIL THEN sl.first.name ELSE rope.Cat[", ", sl.first.name];
ENDLOOP;
RETURN};
space ←
NEW [SpacePrivate ← [
Contains: LORAContains,
Equal: LORAEqual,
Hash: LORAHash,
Compare: LORACompare,
name: Rope.Cat["LORA space [", Fmt[prefix], "] [", Fmt[repeat], "]"],
data: NEW [LexerPrivate ← [prefix, repeat]]
]];
RETURN};
LORAContains:
PROC [data:
REF
ANY, v: Value]
RETURNS [
BOOL] ~ {
RETURN [
WITH v.ra
SELECT
FROM
x: LORA => TRUE,
ENDCASE => FALSE]};
LORAHash:
PROC [data:
REF
ANY, v: Value]
RETURNS [hash:
CARDINAL ← 0] ~ {
lr: Lexer ~ NARROW[data];
sl: SpaceList ← lr.prefix;
FOR l:
LORA ←
NARROW[v.ra], l.rest
WHILE l #
NIL
DO
IF sl=NIL THEN {sl ← lr.repeat; IF sl=NIL THEN ERROR};
hash ← hash*5 + sl.first.SHash[AV[l.first]];
sl ← sl.rest;
ENDLOOP;
RETURN};
LORAEqual:
PROC [data:
REF
ANY, v1, v2: Value]
RETURNS [
BOOL] ~ {
RETURN [LORACompare[data, v1, v2]=equal]};
LORACompare:
PROC [data:
REF
ANY, v1, v2: Value]
RETURNS [c: Basics.Comparison ← equal] ~ {
lr: Lexer ~ NARROW[data];
l1: LORA ← NARROW[v1.VA];
l2: LORA ← NARROW[v2.VA];
sl: SpaceList ← lr.prefix;
WHILE l1#
NIL
AND l2#
NIL
DO
IF sl=NIL THEN {sl ← lr.repeat; IF sl=NIL THEN ERROR};
IF (c ← sl.first.SCompare[AV[l1.first], AV[l2.first]])#equal THEN RETURN;
l1 ← l1.rest;
l2 ← l2.rest;
sl ← sl.rest;
ENDLOOP;
RETURN [
SELECT
TRUE
FROM
l1#NIL => greater,
l2#NIL => less,
ENDCASE => equal];
};
SpaceOrder:
PUBLIC
PROC [space: Space]
RETURNS [Order] ~ {
RETURN [[space.Compare, space.data]]};
END.