StdPairCollections4.Mesa
Last tweaked by Mike Spreitzer on October 16, 1987 10:31:53 am PDT
DIRECTORY Basics, Collections, PairCollections, List, Rope;
StdPairCollections4:
CEDAR
PROGRAM
IMPORTS Collections, PairCollections
EXPORTS Collections
=
BEGIN OPEN Colls:Collections, PairColls:PairCollections, Collections, PairCollections;
Wp:
PROC [pair: Pair]
RETURNS [
REF
ANY]
~ INLINE {RETURN [NEW [Pair ← pair]]};
Np:
PROC [pair:
REF
ANY]
RETURNS [Pair]
~ INLINE {RETURN [NARROW[pair, REF Pair]^]};
Nmv:
PROC [mv: MaybeValue]
RETURNS [MaybePair]
~ INLINE {RETURN [IF mv.found THEN [TRUE, Np[mv.val]] ELSE noMaybePair]};
Wmp:
PROC [mp: MaybePair]
RETURNS [MaybeValue]
~ INLINE {RETURN [IF mp.found THEN [TRUE, Wp[mp.pair]] ELSE noMaybe]};
DefaultQuaIntFn:
PUBLIC
PROC [coll: Collection, dir: Direction]
RETURNS [MaybeValue] ~ {
mpc: MaybeValue ~ coll.QuaPairColl[];
IF NOT mpc.found THEN RETURN [noMaybe];
IF mpc.val=NIL THEN RETURN [[TRUE, NIL]];
{pc: PairColl ~ DeRef[mpc.val];
RETURN DeRef[mpc.val].QuaIntFn}};
DefaultQuaPairColl:
PUBLIC
PROC [coll: Collection]
RETURNS [MaybeValue] ~ {
space: Space ~ coll.SpaceOf;
IF space=NIL OR NOT IsPairSpace[space] THEN RETURN [noMaybe];
{pc: PairColl ~ [
quaClasses
[coll.MayDuplicate]
[coll.OrderStyleOf]
[coll.MutabilityOf],
coll.Refify
];
RETURN [[TRUE, pc.Refify]]}};
QuaClasses: TYPE ~ ARRAY --mayDuplicate--BOOL OF ARRAY OrderStyle OF ARRAY Mutability OF PairCollClass;
quaClasses: REF QuaClasses ~ NEW [QuaClasses ← ALL[ALL[ALL[NIL]]]];
QuaPrimitive:
PROC [pc: PairColl, op:
ATOM, args: ArgList ←
NIL]
RETURNS [PrimitiveAnswer] ~ {
coll: Collection ~ Colls.DeRef[pc.data];
SELECT op
FROM
$Widen => RETURN [yes];
$HasPair => RETURN [IF coll.QualityOf[$HasMember]>cant THEN yes ELSE no];
$Scan, $Extremum, $Get3, $Size, $Copy, $Insulate, $ValueOf, $Freeze, $Thaw, $AddColl, $OrderingOf => RETURN [IF coll.QualityOf[op, args]>cant THEN yes ELSE no];
$RemColl => RETURN [IF coll.QualityOf[$RemoveColl]>cant THEN yes ELSE no];
$Spaces => RETURN [IF coll.QualityOf[$SpaceOf]>cant THEN yes ELSE no];
ENDCASE => RETURN [no];
};
QuaWiden:
PROC [pc: PairColl]
RETURNS [Collection
--of REF Pair--] ~ {
coll: Collection ~ Colls.DeRef[pc.data];
RETURN [coll]};
QuaHasPair:
PROC [pc: PairColl, pair: Pair]
RETURNS [
BOOL] ~ {
coll: Collection ~ Colls.DeRef[pc.data];
RETURN coll.HasMember[Wp[pair]]};
QuaScan:
PROC [pc: PairColl,
Test: Tester, bkwd:
BOOL]
RETURNS [MaybePair] ~ {
coll: Collection ~ Colls.DeRef[pc.data];
Pass: PROC [val: Value] RETURNS [pass: BOOL] ~ {pass ← Test[Np[val]]};
RETURN Nmv[coll.Scan[Pass, bkwd]]};
QuaExtremum:
PROC [pc: PairColl, bkwd, remove:
BOOL]
RETURNS [MaybePair] ~ {
coll: Collection ~ Colls.DeRef[pc.data];
RETURN Nmv[coll.class.Extremum[coll, bkwd, remove]]};
QuaGet3:
PROC [pc: PairColl, pair: Pair]
RETURNS [prev, same, next: MaybePair] ~ {
coll: Collection ~ Colls.DeRef[pc.data];
wPrev, wSame, wNext: MaybeValue;
[wPrev, wSame, wNext] ← coll.Get3[Wp[pair]];
prev ← Nmv[wPrev];
same ← Nmv[wSame];
next ← Nmv[wNext];
RETURN};
QuaSize:
PROC [pc: PairColl, limit:
LNAT]
RETURNS [
LNAT] ~ {
coll: Collection ~ Colls.DeRef[pc.data];
RETURN coll.Size[limit]};
QuaCopy:
PROC [pc: PairColl]
RETURNS [VarPairColl] ~ {
coll: Collection ~ Colls.DeRef[pc.data];
RETURN DeRef[coll.Copy.AsPairColl].AsVar};
QuaInsulate:
PROC [pc: PairColl]
RETURNS [UWPairColl] ~ {
coll: Collection ~ Colls.DeRef[pc.data];
RETURN DeRef[coll.Insulate.AsPairColl].AsUW};
QuaValueOf:
PROC [pc: PairColl]
RETURNS [ConstPairColl] ~ {
coll: Collection ~ Colls.DeRef[pc.data];
RETURN DeRef[coll.ValueOf.AsPairColl].AsConst};
QuaFreeze:
PROC [pc: PairColl]
RETURNS [ConstPairColl] ~ {
coll: Collection ~ Colls.DeRef[pc.data];
RETURN DeRef[coll.Freeze.AsPairColl].AsConst};
QuaThaw:
PROC [pc: PairColl] ~ {
coll: Collection ~ Colls.DeRef[pc.data];
coll.Thaw;
RETURN};
QuaAddColl:
PROC [pc, other: PairColl, if: IfNewsPair, where: Where]
RETURNS [some: NewsSetPair] ~ {
coll: Collection ~ Colls.DeRef[pc.data];
oColl: Collection ~ other.Widen[];
collWhere: Colls.Where ~
WITH where
SELECT
FROM
x: any Where => [any[]],
x: end Where => [end[x.end]],
x: rel Where => [rel[Wp[x.pair], x.reln]],
ENDCASE => ERROR;
IF if#alwaysAdd THEN Cant[pc];
IF pc.Functional#ALL[FALSE] THEN ERROR;
[] ← coll.AddColl[oColl, collWhere];
RETURN};
QuaRemColl:
PROC [pc, other: PairColl, style: RemoveStyle]
RETURNS [hadSome, hadAll: BoolPair] ~ {
coll: Collection ~ Colls.DeRef[pc.data];
oColl: Collection ~ other.Widen[];
IF pc.Functional#ALL[FALSE] THEN ERROR;
[] ← coll.RemoveColl[oColl, style];
RETURN};
QuaSpaces:
PROC [pc: PairColl]
RETURNS [SpacePair] ~ {
coll: Collection ~ Colls.DeRef[pc.data];
space: Space ~ coll.SpaceOf;
RETURN NarrowSpace[space]};
QuaOrderingOf:
PROC [pc: PairColl]
RETURNS [Ordering] ~ {
coll: Collection ~ Colls.DeRef[pc.data];
RETURN NarrowOrdering[coll.OrderingOf]};
Start:
PROC ~ {
FOR mayDuplicate:
BOOL
IN
BOOL
DO
FOR orderStyle: OrderStyle
IN OrderStyle
DO
FOR mutability: Mutability
IN Mutability
DO
quaClasses[mayDuplicate][orderStyle][mutability] ← CreateClass[[
Primitive: QuaPrimitive,
Widen: QuaWiden,
HasPair: QuaHasPair,
Scan: QuaScan,
Extremum: QuaExtremum,
Get3: QuaGet3,
Size: QuaSize,
Copy: QuaCopy,
Insulate: QuaInsulate,
ValueOf: QuaValueOf,
Freeze: QuaFreeze,
Thaw: QuaThaw,
AddColl: QuaAddColl,
RemColl: QuaRemColl,
Spaces: QuaSpaces,
OrderingOf: QuaOrderingOf,
functional: [FALSE, FALSE],
mayDuplicate: mayDuplicate,
orderStyle: orderStyle,
mutability: mutability
]];
ENDLOOP ENDLOOP ENDLOOP;
RETURN};
Start[];
END.