@tsplc/compiler 0.5.4 → 0.5.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/bun-runtime.js +14 -2
- package/dist/bun-runtime.js.map +4 -4
- package/dist/codesys.js +46 -2
- package/dist/codesys.js.map +3 -3
- package/dist/collections.js +395 -0
- package/dist/collections.js.map +10 -0
- package/dist/elaborate-capture.js +64 -5
- package/dist/elaborate-capture.js.map +4 -4
- package/dist/index.js +5345 -1074
- package/dist/index.js.map +32 -25
- package/dist/ir.js +122 -1
- package/dist/ir.js.map +6 -6
- package/package.json +14 -9
- package/src/codesys/numeric.ts +61 -0
- package/src/frontend/control-flow.ts +752 -0
- package/src/frontend/coroutines.ts +171 -7
- package/src/frontend/elaborate/array-searches.ts +216 -0
- package/src/frontend/elaborate/assignments.ts +352 -0
- package/src/frontend/elaborate/calls.ts +488 -49
- package/src/frontend/elaborate/capture-protocol.ts +19 -0
- package/src/frontend/elaborate/capture.ts +82 -3
- package/src/frontend/elaborate/dialect.ts +118 -4
- package/src/frontend/elaborate/expressions.ts +927 -80
- package/src/frontend/elaborate/filtered-arrays.ts +607 -0
- package/src/frontend/elaborate/frame.ts +44 -1
- package/src/frontend/elaborate/generics.ts +137 -0
- package/src/frontend/elaborate/graph.ts +91 -0
- package/src/frontend/elaborate/imports.ts +14 -15
- package/src/frontend/elaborate/index.ts +30 -5
- package/src/frontend/elaborate/initializers.ts +15 -1
- package/src/frontend/elaborate/iterators.ts +1677 -0
- package/src/frontend/elaborate/layouts.ts +41 -19
- package/src/frontend/elaborate/model.ts +64 -8
- package/src/frontend/elaborate/references.ts +749 -0
- package/src/frontend/elaborate/specialize.ts +459 -29
- package/src/frontend/elaborate/statements.ts +552 -133
- package/src/frontend/elaborate/storage.ts +151 -10
- package/src/frontend/elaborate/values.ts +72 -0
- package/src/frontend/elaborate/writes.ts +302 -1
- package/src/frontend/intrinsics.ts +32 -0
- package/src/frontend/module-graph.ts +2 -1
- package/src/frontend/positions.ts +12 -5
- package/src/frontend/sdk.ts +7 -1
- package/src/frontend/typescript.ts +5 -1
- package/src/ir/core-model.ts +2 -6
- package/src/ir/fold.ts +30 -1
- package/src/ir/index.ts +5 -0
- package/src/ir/model.ts +53 -2
- package/src/ir/scalars.ts +126 -0
- package/src/runtime/.runtime-VaUSOu/server/router.ts +17 -0
- package/src/runtime/.runtime-VaUSOu/src/cell.ts +45 -0
- package/src/runtime/.runtime-VaUSOu/src/controller.ts +58 -0
- package/src/runtime/.runtime-VaUSOu/src/lane.ts +6 -0
- package/src/runtime/.runtime-VaUSOu/src/main.ts +11 -0
- package/src/runtime/.runtime-VaUSOu/src/services.ts +8 -0
- package/src/runtime/.runtime-VaUSOu/tsconfig.json +13 -0
- package/src/runtime/.runtime-VaUSOu/tsplc.config.ts +31 -0
- package/src/runtime/.runtime-ybIxYh/server/router.ts +17 -0
- package/src/runtime/.runtime-ybIxYh/src/cell.ts +45 -0
- package/src/runtime/.runtime-ybIxYh/src/controller.ts +58 -0
- package/src/runtime/.runtime-ybIxYh/src/lane.ts +6 -0
- package/src/runtime/.runtime-ybIxYh/src/main.ts +11 -0
- package/src/runtime/.runtime-ybIxYh/src/services.ts +8 -0
- package/src/runtime/.runtime-ybIxYh/tsconfig.json +13 -0
- package/src/runtime/.runtime-ybIxYh/tsplc.config.ts +31 -0
- package/stdlib/collections.ts +514 -0
- package/types/index.d.ts +18 -0
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { initTRPC } from "@trpc/server";
|
|
2
|
+
import { iec, type PlcMeta } from "@tsplc/server";
|
|
3
|
+
import { z } from "zod";
|
|
4
|
+
|
|
5
|
+
const api = initTRPC.meta<PlcMeta>().create();
|
|
6
|
+
/** Callable by the PLC through createServiceClient. */
|
|
7
|
+
const plcProcedure = api.procedure.meta({ plc: true });
|
|
8
|
+
|
|
9
|
+
export const appRouter = api.router({
|
|
10
|
+
cell: api.router({
|
|
11
|
+
report: plcProcedure
|
|
12
|
+
.input(z.strictObject({ count: iec.UINT(), running: z.boolean() }))
|
|
13
|
+
.output(z.strictObject({ answer: iec.DINT() }))
|
|
14
|
+
.mutation(({ input }) => ({ answer: input.count * 2 + 1 })),
|
|
15
|
+
}),
|
|
16
|
+
});
|
|
17
|
+
export type AppRouter = typeof appRouter;
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { retain } from "@tsplc/compiler/retain";
|
|
2
|
+
import { Lane } from "./lane";
|
|
3
|
+
|
|
4
|
+
export type Mode = "idle" | "running";
|
|
5
|
+
|
|
6
|
+
export interface Card {
|
|
7
|
+
input: { sensor: BOOL };
|
|
8
|
+
output: { motion: BOOL; speed: INT };
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
/** The cell's IO, its operator's controls and its counts: what the program publishes. */
|
|
12
|
+
export interface Cell {
|
|
13
|
+
cards: Card[];
|
|
14
|
+
mode: Mode;
|
|
15
|
+
start: BOOL;
|
|
16
|
+
bump: BOOL;
|
|
17
|
+
count: UINT;
|
|
18
|
+
scans: UDINT;
|
|
19
|
+
lanes: Record<Lane, BOOL>;
|
|
20
|
+
lastLane: Lane | undefined;
|
|
21
|
+
report: BOOL;
|
|
22
|
+
replies: UDINT;
|
|
23
|
+
answer: DINT;
|
|
24
|
+
failures: UDINT;
|
|
25
|
+
settings: { speed: INT };
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export const createCell = (): Cell => ({
|
|
29
|
+
cards: Array.from({ length: 3 }, () => ({
|
|
30
|
+
input: { sensor: false },
|
|
31
|
+
output: { motion: false, speed: 0 },
|
|
32
|
+
})),
|
|
33
|
+
mode: "idle",
|
|
34
|
+
start: false,
|
|
35
|
+
bump: false,
|
|
36
|
+
count: 0,
|
|
37
|
+
scans: 0,
|
|
38
|
+
lanes: { [Lane.Unknown]: false, [Lane.Left]: false, [Lane.Right]: false },
|
|
39
|
+
lastLane: undefined,
|
|
40
|
+
report: false,
|
|
41
|
+
replies: 0,
|
|
42
|
+
answer: 0,
|
|
43
|
+
failures: 0,
|
|
44
|
+
settings: retain({ speed: 50 }),
|
|
45
|
+
});
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { telemetry } from "@tsplc/compiler/telemetry";
|
|
2
|
+
import type { Cell } from "./cell";
|
|
3
|
+
import { Lane } from "./lane";
|
|
4
|
+
import type { ReportReply, Services } from "./services";
|
|
5
|
+
|
|
6
|
+
export class CellController {
|
|
7
|
+
private sending = false;
|
|
8
|
+
|
|
9
|
+
constructor(
|
|
10
|
+
private readonly cell: Cell,
|
|
11
|
+
private readonly services: Services,
|
|
12
|
+
) {}
|
|
13
|
+
|
|
14
|
+
scan(): void {
|
|
15
|
+
const cell = this.cell;
|
|
16
|
+
cell.scans += 1;
|
|
17
|
+
for (const card of cell.cards) {
|
|
18
|
+
card.output.motion = card.input.sensor && cell.mode === "running";
|
|
19
|
+
card.output.speed = card.input.sensor ? cell.settings.speed : 0;
|
|
20
|
+
}
|
|
21
|
+
if (cell.start) {
|
|
22
|
+
cell.start = false;
|
|
23
|
+
cell.mode = "running";
|
|
24
|
+
}
|
|
25
|
+
if (cell.bump) {
|
|
26
|
+
cell.bump = false;
|
|
27
|
+
cell.count = cell.count + 1;
|
|
28
|
+
}
|
|
29
|
+
cell.lastLane = undefined;
|
|
30
|
+
if (cell.lanes[Lane.Left]) {
|
|
31
|
+
cell.lastLane = Lane.Left;
|
|
32
|
+
}
|
|
33
|
+
if (cell.lanes[Lane.Right]) {
|
|
34
|
+
cell.lastLane = Lane.Right;
|
|
35
|
+
}
|
|
36
|
+
if (cell.report && !this.sending) {
|
|
37
|
+
cell.report = false;
|
|
38
|
+
void this.send();
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
private async send(): Promise<void> {
|
|
43
|
+
this.sending = true;
|
|
44
|
+
try {
|
|
45
|
+
const reply: ReportReply = await this.services.cell.report({
|
|
46
|
+
count: this.cell.count,
|
|
47
|
+
running: this.cell.mode === "running",
|
|
48
|
+
});
|
|
49
|
+
this.cell.replies += 1;
|
|
50
|
+
this.cell.answer = reply.answer;
|
|
51
|
+
telemetry.info("Report answered", { answer: reply.answer });
|
|
52
|
+
} catch (error) {
|
|
53
|
+
this.cell.failures += 1;
|
|
54
|
+
telemetry.captureException(error);
|
|
55
|
+
}
|
|
56
|
+
this.sending = false;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { createCell } from "./cell";
|
|
2
|
+
import { CellController } from "./controller";
|
|
3
|
+
import { services } from "./services";
|
|
4
|
+
|
|
5
|
+
export const cell = createCell();
|
|
6
|
+
const controller = new CellController(cell, services);
|
|
7
|
+
|
|
8
|
+
/** One PLC scan. */
|
|
9
|
+
export function main(): void {
|
|
10
|
+
controller.scan();
|
|
11
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { inferRouterOutputs } from "@trpc/server";
|
|
2
|
+
import { createServiceClient } from "@tsplc/compiler/async";
|
|
3
|
+
import type { AppRouter } from "../server/router";
|
|
4
|
+
|
|
5
|
+
/** The server procedures the program calls. */
|
|
6
|
+
export const services = createServiceClient<AppRouter>();
|
|
7
|
+
export type Services = typeof services;
|
|
8
|
+
export type ReportReply = inferRouterOutputs<AppRouter>["cell"]["report"];
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"strict": true,
|
|
4
|
+
"target": "ES2022",
|
|
5
|
+
"module": "ESNext",
|
|
6
|
+
"moduleResolution": "Bundler",
|
|
7
|
+
"noEmit": true,
|
|
8
|
+
"skipLibCheck": true,
|
|
9
|
+
"types": ["bun", "@tsplc/compiler/globals"],
|
|
10
|
+
"allowImportingTsExtensions": true
|
|
11
|
+
},
|
|
12
|
+
"include": ["src/**/*.ts", "server/**/*.ts"]
|
|
13
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { defineConfig, ms } from "@tsplc/config";
|
|
2
|
+
|
|
3
|
+
/** A cell whose plain TypeScript program the Bun runtime runs in the compiler's tests. */
|
|
4
|
+
export default defineConfig({
|
|
5
|
+
name: "BunRuntimeCell",
|
|
6
|
+
backend: "codesys",
|
|
7
|
+
build: { outDir: "build/codesys", symbols: true },
|
|
8
|
+
codesys: {
|
|
9
|
+
http: {
|
|
10
|
+
timeoutMs: 2000,
|
|
11
|
+
credentials: [
|
|
12
|
+
{ originEnv: "CELL_SERVER_URL", bearerTokenEnv: "CELL_SERVER_TOKEN" },
|
|
13
|
+
],
|
|
14
|
+
},
|
|
15
|
+
programNames: { main: "Main" },
|
|
16
|
+
},
|
|
17
|
+
compiler: {
|
|
18
|
+
programs: { main: { module: "src/main.ts", export: "main" } },
|
|
19
|
+
},
|
|
20
|
+
tasks: [
|
|
21
|
+
{
|
|
22
|
+
name: "MainTask",
|
|
23
|
+
priority: 1,
|
|
24
|
+
schedule: { kind: "cyclic", interval: ms(20) },
|
|
25
|
+
programs: ["main"],
|
|
26
|
+
},
|
|
27
|
+
],
|
|
28
|
+
publish: { development: ["cell"], production: ["cell"] },
|
|
29
|
+
server: { transport: "http", baseUrlEnv: "CELL_SERVER_URL" },
|
|
30
|
+
typegen: { dir: "generated" },
|
|
31
|
+
});
|
|
@@ -0,0 +1,514 @@
|
|
|
1
|
+
// PLC implementation source: a plain TypeScript program's collections. Setup fixes each one's
|
|
2
|
+
// capacity, so the PLC stores it in arrays allocated once, and every loop over it is bounded by
|
|
3
|
+
// that capacity. The same classes run under Bun for setup, tests and simulation.
|
|
4
|
+
import type { UINT } from "../types/index";
|
|
5
|
+
|
|
6
|
+
/** Bun's, at setup and in simulation; the PLC compiles it as the dialect's `copy`. */
|
|
7
|
+
declare function structuredClone<T>(value: T): T;
|
|
8
|
+
|
|
9
|
+
/** What a full collection does with one more item: refuse it, or make room by dropping the oldest. */
|
|
10
|
+
export type OnFull = "reject" | "dropOldest";
|
|
11
|
+
|
|
12
|
+
export interface BoundedOptions {
|
|
13
|
+
readonly onFull: OnFull;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/** Above this capacity a map with number keys hashes them; at or below it, a scan of every slot is as quick. */
|
|
17
|
+
export const linearLimit = 32;
|
|
18
|
+
|
|
19
|
+
/** A first-in, first-out queue of at most `capacity` items, in a ring. */
|
|
20
|
+
export class BoundedQueue<T> {
|
|
21
|
+
private readonly items: T[];
|
|
22
|
+
private first: UINT = 0;
|
|
23
|
+
private count: UINT = 0;
|
|
24
|
+
/** How many items the queue refused or dropped because it was full. */
|
|
25
|
+
overflowed = 0;
|
|
26
|
+
|
|
27
|
+
/** What an empty slot holds; reads fall back to it where TypeScript can't see the slot is filled. */
|
|
28
|
+
private readonly blank: T;
|
|
29
|
+
|
|
30
|
+
constructor(
|
|
31
|
+
readonly capacity: number,
|
|
32
|
+
blank: () => T,
|
|
33
|
+
private readonly options: BoundedOptions,
|
|
34
|
+
) {
|
|
35
|
+
this.items = Array.from({ length: capacity }, blank);
|
|
36
|
+
this.blank = blank();
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
private item(slot: UINT): T {
|
|
40
|
+
return this.items[slot % this.capacity] ?? this.blank;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
get length(): number {
|
|
44
|
+
return this.count;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** Adds an item at the back; false when the queue is full and refuses it. */
|
|
48
|
+
push(item: T): boolean {
|
|
49
|
+
if (this.count === this.capacity) {
|
|
50
|
+
this.overflowed += 1;
|
|
51
|
+
if (this.options.onFull === "reject") {
|
|
52
|
+
return false;
|
|
53
|
+
}
|
|
54
|
+
this.first = (this.first + 1) % this.capacity;
|
|
55
|
+
this.count -= 1;
|
|
56
|
+
}
|
|
57
|
+
this.items[(this.first + this.count) % this.capacity] = item;
|
|
58
|
+
this.count += 1;
|
|
59
|
+
return true;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** Takes the item at the front, or undefined when empty. */
|
|
63
|
+
shift(): T | undefined {
|
|
64
|
+
if (this.count === 0) {
|
|
65
|
+
return undefined;
|
|
66
|
+
}
|
|
67
|
+
const item = this.item(this.first);
|
|
68
|
+
this.first = (this.first + 1) % this.capacity;
|
|
69
|
+
this.count -= 1;
|
|
70
|
+
return item;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/** Takes the item at the back, or undefined when empty. */
|
|
74
|
+
pop(): T | undefined {
|
|
75
|
+
if (this.count === 0) {
|
|
76
|
+
return undefined;
|
|
77
|
+
}
|
|
78
|
+
this.count -= 1;
|
|
79
|
+
return this.item(this.first + this.count);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/** The item at the front, left in place. */
|
|
83
|
+
peek(): T | undefined {
|
|
84
|
+
return this.count === 0 ? undefined : this.item(this.first);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/** The item `offset` from the front, or undefined past the back. */
|
|
88
|
+
at(offset: UINT): T | undefined {
|
|
89
|
+
return offset < this.count ? this.item(this.first + offset) : undefined;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/** Changes the item `offset` from the front in place; false past the back. */
|
|
93
|
+
update(offset: UINT, change: (item: T) => void): boolean {
|
|
94
|
+
if (offset >= this.count) {
|
|
95
|
+
return false;
|
|
96
|
+
}
|
|
97
|
+
const slot: UINT = (this.first + offset) % this.capacity;
|
|
98
|
+
// A copy, changed and stored back: an element at a runtime slot can't be lent out.
|
|
99
|
+
const item = structuredClone(
|
|
100
|
+
this.items[slot % this.capacity] ?? this.blank,
|
|
101
|
+
);
|
|
102
|
+
change(item);
|
|
103
|
+
this.items[slot % this.capacity] = item;
|
|
104
|
+
return true;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
clear(): void {
|
|
108
|
+
this.first = 0;
|
|
109
|
+
this.count = 0;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
*values(): Generator<T> {
|
|
113
|
+
for (let offset: UINT = 0; offset < this.capacity; offset++) {
|
|
114
|
+
if (offset >= this.count) {
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
117
|
+
// The stored item itself, so a loop that changes it changes the queue's.
|
|
118
|
+
yield this.items[(this.first + offset) % this.capacity] ?? this.blank;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/** A list of at most `capacity` items in order, which can be removed from anywhere. */
|
|
124
|
+
export class BoundedList<T> {
|
|
125
|
+
private readonly items: T[];
|
|
126
|
+
private count: UINT = 0;
|
|
127
|
+
/** How many items the list refused or dropped because it was full. */
|
|
128
|
+
overflowed = 0;
|
|
129
|
+
|
|
130
|
+
/** What an empty slot holds; reads fall back to it where TypeScript can't see the slot is filled. */
|
|
131
|
+
private readonly blank: T;
|
|
132
|
+
|
|
133
|
+
constructor(
|
|
134
|
+
readonly capacity: number,
|
|
135
|
+
blank: () => T,
|
|
136
|
+
private readonly options: BoundedOptions,
|
|
137
|
+
) {
|
|
138
|
+
this.items = Array.from({ length: capacity }, blank);
|
|
139
|
+
this.blank = blank();
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
private item(slot: UINT): T {
|
|
143
|
+
return this.items[slot % this.capacity] ?? this.blank;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
get length(): number {
|
|
147
|
+
return this.count;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/** Adds an item at the end; false when the list is full and refuses it. */
|
|
151
|
+
push(item: T): boolean {
|
|
152
|
+
if (this.count === this.capacity) {
|
|
153
|
+
this.overflowed += 1;
|
|
154
|
+
if (this.options.onFull === "reject") {
|
|
155
|
+
return false;
|
|
156
|
+
}
|
|
157
|
+
this.removeAt(0);
|
|
158
|
+
}
|
|
159
|
+
this.items[this.count % this.capacity] = item;
|
|
160
|
+
this.count += 1;
|
|
161
|
+
return true;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
at(index: UINT): T | undefined {
|
|
165
|
+
return index < this.count ? this.item(index) : undefined;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/** Changes the item at `index` in place; false past the end. */
|
|
169
|
+
update(index: UINT, change: (item: T) => void): boolean {
|
|
170
|
+
if (index >= this.count) {
|
|
171
|
+
return false;
|
|
172
|
+
}
|
|
173
|
+
// A copy, changed and stored back: an element at a runtime slot can't be lent out.
|
|
174
|
+
const item = structuredClone(
|
|
175
|
+
this.items[index % this.capacity] ?? this.blank,
|
|
176
|
+
);
|
|
177
|
+
change(item);
|
|
178
|
+
this.items[index % this.capacity] = item;
|
|
179
|
+
return true;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/** Removes the item at `index`, moving the ones after it up; false past the end. */
|
|
183
|
+
removeAt(index: UINT): boolean {
|
|
184
|
+
if (index >= this.count) {
|
|
185
|
+
return false;
|
|
186
|
+
}
|
|
187
|
+
for (let slot: UINT = 0; slot < this.capacity; slot++) {
|
|
188
|
+
if (slot >= index && slot + 1 < this.count) {
|
|
189
|
+
this.items[slot % this.capacity] = this.item(slot + 1);
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
this.count -= 1;
|
|
193
|
+
return true;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
/** The index of the first item that passes, or -1. */
|
|
197
|
+
findIndex(test: (item: T) => boolean): number {
|
|
198
|
+
for (let slot: UINT = 0; slot < this.capacity; slot++) {
|
|
199
|
+
if (slot < this.count && test(this.item(slot))) {
|
|
200
|
+
return slot;
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
return -1;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
/** Removes the first item that passes; false when none does. */
|
|
207
|
+
remove(test: (item: T) => boolean): boolean {
|
|
208
|
+
for (let slot: UINT = 0; slot < this.capacity; slot++) {
|
|
209
|
+
if (slot < this.count && test(this.item(slot))) {
|
|
210
|
+
return this.removeAt(slot);
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
return false;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
clear(): void {
|
|
217
|
+
this.count = 0;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
*values(): Generator<T> {
|
|
221
|
+
for (let slot: UINT = 0; slot < this.capacity; slot++) {
|
|
222
|
+
if (slot >= this.count) {
|
|
223
|
+
return;
|
|
224
|
+
}
|
|
225
|
+
yield this.items[slot % this.capacity] ?? this.blank;
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
/**
|
|
231
|
+
* A map of at most `capacity` entries. Up to `linearLimit` entries, or with string keys, a lookup
|
|
232
|
+
* scans every slot; a larger map with number keys hashes them into twice as many slots, probing
|
|
233
|
+
* from each key's home slot and closing the gap a deletion leaves.
|
|
234
|
+
*/
|
|
235
|
+
export class BoundedMap<K extends number | string, V> {
|
|
236
|
+
private readonly hashed: boolean;
|
|
237
|
+
private readonly slots: number;
|
|
238
|
+
private readonly slotKeys: K[];
|
|
239
|
+
private readonly slotValues: V[];
|
|
240
|
+
private readonly used: boolean[];
|
|
241
|
+
/** When each slot's entry was added, for dropping the oldest. */
|
|
242
|
+
private readonly ages: number[];
|
|
243
|
+
private added = 0;
|
|
244
|
+
private count = 0;
|
|
245
|
+
/** What an empty slot holds; reads fall back to them where TypeScript can't see a slot is filled. */
|
|
246
|
+
private readonly blankKey: K;
|
|
247
|
+
private readonly blankValue: V;
|
|
248
|
+
/** How many entries the map refused or dropped because it was full. */
|
|
249
|
+
overflowed = 0;
|
|
250
|
+
|
|
251
|
+
constructor(
|
|
252
|
+
readonly capacity: number,
|
|
253
|
+
blankKey: K,
|
|
254
|
+
blank: () => V,
|
|
255
|
+
private readonly options: BoundedOptions,
|
|
256
|
+
) {
|
|
257
|
+
this.hashed = capacity > linearLimit && typeof blankKey === "number";
|
|
258
|
+
this.slots = this.hashed ? capacity * 2 : capacity;
|
|
259
|
+
this.slotKeys = Array.from({ length: this.slots }, () => blankKey);
|
|
260
|
+
this.slotValues = Array.from({ length: this.slots }, blank);
|
|
261
|
+
this.used = Array.from({ length: this.slots }, () => false);
|
|
262
|
+
this.ages = Array.from({ length: this.slots }, () => 0);
|
|
263
|
+
this.blankKey = blankKey;
|
|
264
|
+
this.blankValue = blank();
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
private keyAt(slot: UINT): K {
|
|
268
|
+
return this.slotKeys[slot % this.slots] ?? this.blankKey;
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
private valueAt(slot: UINT): V {
|
|
272
|
+
return this.slotValues[slot % this.slots] ?? this.blankValue;
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
private usedAt(slot: UINT): boolean {
|
|
276
|
+
return this.used[slot % this.slots] ?? false;
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
private ageAt(slot: UINT): number {
|
|
280
|
+
return this.ages[slot % this.slots] ?? 0;
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
get size(): number {
|
|
284
|
+
return this.count;
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
has(key: K): boolean {
|
|
288
|
+
return this.slotOf(key) >= 0;
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
/** The entry for `key`, as a copy, or undefined. Change a stored entry with `update`. */
|
|
292
|
+
get(key: K): V | undefined {
|
|
293
|
+
const found = this.slotOf(key);
|
|
294
|
+
if (found < 0) {
|
|
295
|
+
return undefined;
|
|
296
|
+
}
|
|
297
|
+
return this.valueAt(found);
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
/** Stores the entry for `key`; false when the map is full and refuses a new key. */
|
|
301
|
+
set(key: K, value: V): boolean {
|
|
302
|
+
const found = this.slotOf(key);
|
|
303
|
+
if (found >= 0) {
|
|
304
|
+
const slot: UINT = found;
|
|
305
|
+
this.slotValues[slot % this.slots] = value;
|
|
306
|
+
return true;
|
|
307
|
+
}
|
|
308
|
+
if (this.count === this.capacity) {
|
|
309
|
+
this.overflowed += 1;
|
|
310
|
+
if (this.options.onFull === "reject") {
|
|
311
|
+
return false;
|
|
312
|
+
}
|
|
313
|
+
this.removeSlot(this.oldestSlot());
|
|
314
|
+
}
|
|
315
|
+
const slot = this.freeSlot(key);
|
|
316
|
+
this.slotKeys[slot % this.slots] = key;
|
|
317
|
+
this.slotValues[slot % this.slots] = value;
|
|
318
|
+
this.used[slot % this.slots] = true;
|
|
319
|
+
this.ages[slot % this.slots] = this.added;
|
|
320
|
+
this.added += 1;
|
|
321
|
+
this.count += 1;
|
|
322
|
+
return true;
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
/** Changes the stored entry for `key` in place; false when there is none. */
|
|
326
|
+
update(key: K, change: (entry: V) => void): boolean {
|
|
327
|
+
const found = this.slotOf(key);
|
|
328
|
+
if (found < 0) {
|
|
329
|
+
return false;
|
|
330
|
+
}
|
|
331
|
+
const slot: UINT = found;
|
|
332
|
+
// A copy, changed and stored back: an element at a runtime slot can't be lent out.
|
|
333
|
+
const entry = structuredClone(
|
|
334
|
+
this.slotValues[slot % this.slots] ?? this.blankValue,
|
|
335
|
+
);
|
|
336
|
+
change(entry);
|
|
337
|
+
this.slotValues[slot % this.slots] = entry;
|
|
338
|
+
return true;
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
delete(key: K): boolean {
|
|
342
|
+
const found = this.slotOf(key);
|
|
343
|
+
if (found < 0) {
|
|
344
|
+
return false;
|
|
345
|
+
}
|
|
346
|
+
this.removeSlot(found);
|
|
347
|
+
return true;
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
clear(): void {
|
|
351
|
+
for (let slot: UINT = 0; slot < this.slots; slot++) {
|
|
352
|
+
this.used[slot % this.slots] = false;
|
|
353
|
+
}
|
|
354
|
+
this.count = 0;
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
*keys(): Generator<K> {
|
|
358
|
+
for (let slot: UINT = 0; slot < this.slots; slot++) {
|
|
359
|
+
if (this.usedAt(slot)) {
|
|
360
|
+
yield this.keyAt(slot);
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
*values(): Generator<V> {
|
|
366
|
+
for (let slot: UINT = 0; slot < this.slots; slot++) {
|
|
367
|
+
if (this.usedAt(slot)) {
|
|
368
|
+
yield this.slotValues[slot % this.slots] ?? this.blankValue;
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
/** The slot holding `key`, or -1. */
|
|
374
|
+
private slotOf(key: K): number {
|
|
375
|
+
if (!this.hashed) {
|
|
376
|
+
for (let slot: UINT = 0; slot < this.slots; slot++) {
|
|
377
|
+
if (this.usedAt(slot) && this.keyAt(slot) === key) {
|
|
378
|
+
return slot;
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
return -1;
|
|
382
|
+
}
|
|
383
|
+
const home: UINT = this.home(key);
|
|
384
|
+
for (let probe: UINT = 0; probe < this.slots; probe++) {
|
|
385
|
+
const slot: UINT = (home + probe) % this.slots;
|
|
386
|
+
if (!this.usedAt(slot)) {
|
|
387
|
+
return -1;
|
|
388
|
+
}
|
|
389
|
+
if (this.keyAt(slot) === key) {
|
|
390
|
+
return slot;
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
return -1;
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
/** The slot a new key goes in: the first free one, from its home slot when hashed. */
|
|
397
|
+
private freeSlot(key: K): UINT {
|
|
398
|
+
const home: UINT = this.hashed ? this.home(key) : 0;
|
|
399
|
+
for (let probe: UINT = 0; probe < this.slots; probe++) {
|
|
400
|
+
const slot: UINT = (home + probe) % this.slots;
|
|
401
|
+
if (!this.usedAt(slot)) {
|
|
402
|
+
return slot;
|
|
403
|
+
}
|
|
404
|
+
}
|
|
405
|
+
return 0;
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
/** The slot a number key hashes to. */
|
|
409
|
+
private home(key: K): number {
|
|
410
|
+
if (typeof key !== "number") {
|
|
411
|
+
return 0;
|
|
412
|
+
}
|
|
413
|
+
const remainder = key % this.slots;
|
|
414
|
+
return remainder < 0 ? remainder + this.slots : remainder;
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
/** The slot whose entry was added first. */
|
|
418
|
+
private oldestSlot(): number {
|
|
419
|
+
let oldest = -1;
|
|
420
|
+
for (let slot: UINT = 0; slot < this.slots; slot++) {
|
|
421
|
+
if (
|
|
422
|
+
this.usedAt(slot) &&
|
|
423
|
+
(oldest < 0 || this.ageAt(slot) - this.ageAt(oldest) < 0)
|
|
424
|
+
) {
|
|
425
|
+
oldest = slot;
|
|
426
|
+
}
|
|
427
|
+
}
|
|
428
|
+
return oldest;
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
/**
|
|
432
|
+
* Empties a slot. A hashed map then moves back each entry after it that could sit earlier, so no
|
|
433
|
+
* lookup stops at the gap before reaching its key.
|
|
434
|
+
*/
|
|
435
|
+
private removeSlot(found: number): void {
|
|
436
|
+
if (found < 0) {
|
|
437
|
+
return;
|
|
438
|
+
}
|
|
439
|
+
const removed: UINT = found;
|
|
440
|
+
this.used[removed % this.slots] = false;
|
|
441
|
+
this.count -= 1;
|
|
442
|
+
if (!this.hashed) {
|
|
443
|
+
return;
|
|
444
|
+
}
|
|
445
|
+
let hole: UINT = removed % this.slots;
|
|
446
|
+
for (let probe: UINT = 1; probe < this.slots; probe++) {
|
|
447
|
+
const next: UINT = (removed + probe) % this.slots;
|
|
448
|
+
if (!this.usedAt(next)) {
|
|
449
|
+
return;
|
|
450
|
+
}
|
|
451
|
+
const home: UINT = this.home(this.keyAt(next));
|
|
452
|
+
const stays =
|
|
453
|
+
hole <= next
|
|
454
|
+
? home > hole && home <= next
|
|
455
|
+
: home > hole || home <= next;
|
|
456
|
+
if (!stays) {
|
|
457
|
+
this.slotKeys[hole % this.slots] = this.keyAt(next);
|
|
458
|
+
this.slotValues[hole % this.slots] = this.valueAt(next);
|
|
459
|
+
this.ages[hole % this.slots] = this.ageAt(next);
|
|
460
|
+
this.used[hole % this.slots] = true;
|
|
461
|
+
this.used[next % this.slots] = false;
|
|
462
|
+
hole = next;
|
|
463
|
+
}
|
|
464
|
+
}
|
|
465
|
+
}
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
/** A set of at most `capacity` keys. */
|
|
469
|
+
export class BoundedSet<K extends number | string> {
|
|
470
|
+
private readonly members: BoundedMap<K, boolean>;
|
|
471
|
+
|
|
472
|
+
constructor(
|
|
473
|
+
readonly capacity: number,
|
|
474
|
+
blankKey: K,
|
|
475
|
+
options: BoundedOptions,
|
|
476
|
+
) {
|
|
477
|
+
this.members = new BoundedMap<K, boolean>(
|
|
478
|
+
capacity,
|
|
479
|
+
blankKey,
|
|
480
|
+
() => false,
|
|
481
|
+
options,
|
|
482
|
+
);
|
|
483
|
+
}
|
|
484
|
+
|
|
485
|
+
get size(): number {
|
|
486
|
+
return this.members.size;
|
|
487
|
+
}
|
|
488
|
+
|
|
489
|
+
/** How many keys the set refused or dropped because it was full. */
|
|
490
|
+
get overflowed(): number {
|
|
491
|
+
return this.members.overflowed;
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
has(key: K): boolean {
|
|
495
|
+
return this.members.has(key);
|
|
496
|
+
}
|
|
497
|
+
|
|
498
|
+
/** Adds the key; false when the set is full and refuses it. */
|
|
499
|
+
add(key: K): boolean {
|
|
500
|
+
return this.members.set(key, true);
|
|
501
|
+
}
|
|
502
|
+
|
|
503
|
+
delete(key: K): boolean {
|
|
504
|
+
return this.members.delete(key);
|
|
505
|
+
}
|
|
506
|
+
|
|
507
|
+
clear(): void {
|
|
508
|
+
this.members.clear();
|
|
509
|
+
}
|
|
510
|
+
|
|
511
|
+
*values(): Generator<K> {
|
|
512
|
+
yield* this.members.keys();
|
|
513
|
+
}
|
|
514
|
+
}
|