-
Notifications
You must be signed in to change notification settings - Fork 10
/
dag-map.ts
216 lines (197 loc) · 5.12 KB
/
dag-map.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
export interface Callback<T> {
(key: string, value: T | undefined): void;
}
export type MaybeStringOrArray = string | string[] | undefined;
/**
* A topologically ordered map of key/value pairs with a simple API for adding constraints.
*
* Edges can forward reference keys that have not been added yet (the forward reference will
* map the key to undefined).
*/
export default class DAG<T> {
private _vertices = new Vertices<T>();
/**
* Adds a key/value pair with dependencies on other key/value pairs.
*
* @public
* @param key The key of the vertex to be added.
* @param value The value of that vertex.
* @param before A key or array of keys of the vertices that must
* be visited before this vertex.
* @param after An string or array of strings with the keys of the
* vertices that must be after this vertex is visited.
*/
public add(
key: string,
value: T | undefined,
before?: MaybeStringOrArray,
after?: MaybeStringOrArray
) {
if (!key) throw new Error("argument `key` is required");
let vertices = this._vertices;
let v = vertices.add(key);
v.val = value;
if (before) {
if (typeof before === "string") {
vertices.addEdge(v, vertices.add(before));
} else {
for (let i = 0; i < before.length; i++) {
vertices.addEdge(v, vertices.add(before[i]));
}
}
}
if (after) {
if (typeof after === "string") {
vertices.addEdge(vertices.add(after), v);
} else {
for (let i = 0; i < after.length; i++) {
vertices.addEdge(vertices.add(after[i]), v);
}
}
}
}
/**
* Visits key/value pairs in topological order.
*
* @public
* @param callback The function to be invoked with each key/value.
*/
public each(callback: Callback<T>) {
this._vertices.walk(callback);
}
}
/** @private */
class Vertices<T> {
[index: number]: Vertex<T>;
length = 0;
private stack: IntStack = new IntStack();
private path: IntStack = new IntStack();
private result: IntStack = new IntStack();
public add(key: string): Vertex<T> {
if (!key) throw new Error("missing key");
let l = this.length | 0;
let vertex: Vertex<T>;
for (let i = 0; i < l; i++) {
vertex = this[i];
if (vertex.key === key) return vertex;
}
this.length = l + 1;
return (this[l] = {
idx: l,
key: key,
val: undefined,
out: false,
flag: false,
length: 0
});
}
public addEdge(v: Vertex<T>, w: Vertex<T>): void {
this.check(v, w.key);
let l = w.length | 0;
for (let i = 0; i < l; i++) {
if (w[i] === v.idx) return;
}
w.length = l + 1;
w[l] = v.idx;
v.out = true;
}
public walk(cb: Callback<T>): void {
this.reset();
for (let i = 0; i < this.length; i++) {
let vertex = this[i];
if (vertex.out) continue;
this.visit(vertex, "");
}
this.each(this.result, cb);
}
private check(v: Vertex<T>, w: string): void {
if (v.key === w) {
throw new Error("cycle detected: " + w + " <- " + w);
}
// quick check
if (v.length === 0) return;
// shallow check
for (let i = 0; i < v.length; i++) {
let key = this[v[i]].key;
if (key === w) {
throw new Error("cycle detected: " + w + " <- " + v.key + " <- " + w);
}
}
// deep check
this.reset();
this.visit(v, w);
if (this.path.length > 0) {
let msg = "cycle detected: " + w;
this.each(this.path, key => {
msg += " <- " + key;
});
throw new Error(msg);
}
}
private reset(): void {
this.stack.length = 0;
this.path.length = 0;
this.result.length = 0;
for (let i = 0, l = this.length; i < l; i++) {
this[i].flag = false;
}
}
private visit(start: Vertex<T>, search: string): void {
let { stack, path, result } = this;
stack.push(start.idx);
while (stack.length) {
let index = stack.pop() | 0;
if (index >= 0) {
// enter
let vertex = this[index];
if (vertex.flag) continue;
vertex.flag = true;
path.push(index);
if (search === vertex.key) break;
// push exit
stack.push(~index);
this.pushIncoming(vertex);
} else {
// exit
path.pop();
result.push(~index);
}
}
}
private pushIncoming(incomming: ArrayLike<number>): void {
let { stack } = this;
for (let i = incomming.length - 1; i >= 0; i--) {
let index = incomming[i];
if (!this[index].flag) {
stack.push(index);
}
}
}
private each(indices: IntStack, cb: Callback<T>): void {
for (let i = 0, l = indices.length; i < l; i++) {
let vertex = this[indices[i]];
cb(vertex.key, vertex.val);
}
}
}
/** @private */
interface Vertex<T> {
idx: number;
key: string;
val: T | undefined;
out: boolean;
flag: boolean;
[index: number]: number;
length: number;
}
/** @private */
class IntStack {
[index: number]: number;
public length = 0;
push(n: number) {
this[this.length++] = n | 0;
}
pop() {
return this[--this.length] | 0;
}
}