A bunch of improvements

This commit is contained in:
Ayaz Hafiz 2023-07-17 18:55:59 -05:00
parent 2b6b1d858d
commit 8388c93e62
No known key found for this signature in database
GPG key ID: 0E2A37416A25EF58
14 changed files with 1317 additions and 182 deletions

View file

@ -44,7 +44,7 @@ function getFlatEvents(events: Event[]): {
export class Engine {
#eventIndexMap: Map<Event, EventIndex>;
#events: Event[];
#subs: Subs = new Subs();
#subs: Subs = Subs.new();
#reverseEvents: Map<EventIndex, RollbackChange> = new Map();
#nextIndexForward: EventIndex = 0 as EventIndex;
@ -82,6 +82,10 @@ export class Engine {
return this.#subs;
}
lastEventIndex(): EventIndex {
return (this.#events.length - 1) as EventIndex;
}
private stepForward(eventIndex: EventIndex): void {
const event = this.#events[eventIndex];
if (!isApplicable(event)) {

View file

@ -59,7 +59,15 @@ export type ChangeEvent =
export type Change = ChangeEvent | RollbackChange;
export class Subs {
#map: Map<Variable, VarType> = new Map();
#map: Map<Variable, VarType>;
private constructor(map: Map<Variable, VarType>) {
this.#map = map;
}
static new(): Subs {
return new Subs(new Map());
}
get(variable: Variable): VarType | undefined {
return this.#map.get(variable);
@ -80,6 +88,41 @@ export class Subs {
}
}
get_root_key(variable: Variable): Variable {
const type = this.get(variable);
if (type === undefined) {
return variable;
}
switch (type.type) {
case "descriptor":
return variable;
case "link":
return this.get_root_key(type.to);
default:
assertExhaustive(type);
}
}
snapshot(): SubsSnapshot {
const snapshotMap = new Map<Variable, VarType>();
for (const [key, value] of this.#map) {
snapshotMap.set(key, { ...value });
}
const snapshot = new Subs(snapshotMap);
return {
get(variable: Variable): VarType | undefined {
return snapshot.get(variable);
},
get_root(variable: Variable): TypeDescriptor | undefined {
return snapshot.get_root(variable);
},
get_root_key(variable: Variable): Variable {
return snapshot.get_root_key(variable);
},
__snapshot__: SnapshotSymbol,
};
}
apply(change: Change): void {
switch (change.type) {
case "VariableUnified": {
@ -119,3 +162,12 @@ export class Subs {
}
}
}
const SnapshotSymbol = Symbol("Snapshot");
export interface SubsSnapshot {
get(variable: Variable): VarType | undefined;
get_root(variable: Variable): TypeDescriptor | undefined;
get_root_key(variable: Variable): Variable;
__snapshot__: typeof SnapshotSymbol;
}