-
Notifications
You must be signed in to change notification settings - Fork 0
/
layer.ts
92 lines (74 loc) · 2.24 KB
/
layer.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import { Point } from "../util/types";
// Implements a lazily-evaluated, cached, immutable sparse map from coordinates to T
// This is global
const cache: Record<string, any> = {};
export type Layer<T> = {
name: string,
func: (p: Point) => T,
};
export type Overlay<T> = {
cells: Record<string, T>;
};
function parseCoord(x: string): Point {
const parts = x.split(',');
return { x: parseInt(parts[0]), y: parseInt(parts[1]) };
}
function unparseNamedCoord(name: string, p: Point): string {
return `${name},${p.x},${p.y}`;
}
function unparseCoord(p: Point): string {
return `${p.x},${p.y}`;
}
export function mkLayer<T>(name: string, func: (p: Point) => T): Layer<T> {
return { name, func };
}
export function mkOverlay<T>(): Overlay<T> {
return { cells: {} };
}
export function getLayer<T>(layer: Layer<T>, p: Point): T {
const upc = unparseNamedCoord(layer.name, p);
if (cache[upc] === undefined) {
cache[upc] = layer.func(p);
}
return cache[upc];
}
export function setOverlay<T>(layer: Overlay<T>, p: Point, v: T): void {
layer.cells[unparseCoord(p)] = v;
}
export function getOverlayLayer<T>(layer1: Overlay<T>, layer2: Layer<T>, p: Point): T {
const k = unparseCoord(p);
const v1 = layer1.cells[k];
if (v1 !== undefined) return v1;
return getLayer(layer2, p);
}
export function getOverlay<T>(layer: Overlay<T>, p: Point): T | undefined {
return layer.cells[unparseCoord(p)];
}
export function overlayForEach<T>(layer: Overlay<T>, kont: (p: Point) => void): void {
Object.keys(layer.cells).forEach(k => {
kont(parseCoord(k));
});
}
export function overlayPoints<T>(layer: Overlay<T>): Point[] {
return Object.keys(layer.cells).map(k => parseCoord(k));
}
export function overlayAny<T>(layer: Overlay<T>, predicate: (p: Point) => boolean): boolean {
for (const k of Object.keys(layer.cells)) {
if (predicate(parseCoord(k))) {
return true;
}
}
return false;
}
export function mkOverlayFrom(points: Point[]): Overlay<boolean> {
const layer: Overlay<boolean> = mkOverlay();
points.forEach(p => {
setOverlay(layer, p, true);
});
return layer;
}
export function combineOverlay<T>(layer1: Overlay<T>, layer2: Overlay<T>): Overlay<T> {
return {
cells: { ...layer1.cells, ...layer2.cells }
}
}