-
-
Notifications
You must be signed in to change notification settings - Fork 150
/
index.ts
172 lines (148 loc) · 4.54 KB
/
index.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
import type { ICopy, Nullable, Pair } from "@thi.ng/api";
import { ArraySet, EquivMap, union } from "@thi.ng/associative";
import { equiv } from "@thi.ng/equiv";
import { illegalArgs } from "@thi.ng/errors";
import { filter, reduce, reducer } from "@thi.ng/transducers";
/**
* {@link DGraph} factory function using optional provided edge pairs. If given,
* each pair is a `[node, parent]` tuple, or using `[node, null]` to merely
* register a node in the graph (without dependencies).
*
* @param edges
*/
export const defDGraph = <T>(edges?: Iterable<Pair<T, Nullable<T>>>) =>
new DGraph<T>(edges);
export class DGraph<T> implements Iterable<T>, ICopy<DGraph<T>> {
dependencies: EquivMap<T, ArraySet<T>>;
dependents: EquivMap<T, ArraySet<T>>;
constructor(edges?: Iterable<Pair<T, Nullable<T>>>) {
this.dependencies = new EquivMap<T, ArraySet<T>>();
this.dependents = new EquivMap<T, ArraySet<T>>();
if (edges) {
for (let [a, b] of edges) {
b != null ? this.addDependency(a, b) : this.addNode(a);
}
}
}
*[Symbol.iterator]() {
yield* this.sort();
}
get [Symbol.species]() {
return DGraph;
}
copy() {
const g = new DGraph<T>();
for (let e of this.dependencies) {
g.dependencies.set(e[0], e[1].copy());
}
for (let e of this.dependents) {
g.dependents.set(e[0], e[1].copy());
}
return g;
}
addNode(node: T) {
!this.dependencies.has(node) &&
this.dependencies.set(node, new ArraySet());
return this;
}
addDependency(node: T, dep: T) {
if (equiv(node, dep) || this.depends(dep, node)) {
illegalArgs(`Circular dependency between: ${node} & ${dep}`);
}
let deps = this.dependencies.get(node);
this.dependencies.set(node, deps ? deps.add(dep) : new ArraySet([dep]));
deps = this.dependents.get(dep);
this.dependents.set(dep, deps ? deps.add(node) : new ArraySet([node]));
return this;
}
addDependencies(node: T, deps: Iterable<T>) {
for (let d of deps) {
this.addDependency(node, d);
}
}
removeEdge(node: T, dep: T) {
let deps = this.dependencies.get(node);
if (deps) {
deps.delete(dep);
}
deps = this.dependents.get(dep);
if (deps) {
deps.delete(node);
}
return this;
}
removeNode(x: T) {
this.dependencies.delete(x);
return this;
}
depends(x: T, y: T) {
return this.transitiveDependencies(x).has(y);
}
dependent(x: T, y: T) {
return this.transitiveDependents(x).has(y);
}
immediateDependencies(x: T): Set<T> {
return this.dependencies.get(x) || new ArraySet();
}
immediateDependents(x: T): Set<T> {
return this.dependents.get(x) || new ArraySet();
}
isLeaf(x: T) {
return this.immediateDependents(x).size === 0;
}
isRoot(x: T) {
return this.immediateDependencies(x).size === 0;
}
nodes(): Set<T> {
return union(
new ArraySet(this.dependencies.keys()),
new ArraySet(this.dependents.keys())
);
}
leaves() {
return filter((node: T) => this.isLeaf(node), this.nodes());
}
roots() {
return filter((node: T) => this.isRoot(node), this.nodes());
}
transitiveDependencies(x: T) {
return transitive(this.dependencies, x);
}
transitiveDependents(x: T) {
return transitive(this.dependents, x);
}
sort() {
const sorted: T[] = [];
const g = this.copy();
let queue = new ArraySet(g.leaves());
while (true) {
if (!queue.size) {
return sorted.reverse();
}
const node = queue.first()!;
queue.delete(node);
for (let d of [...g.immediateDependencies(node)]) {
g.removeEdge(node, d);
if (g.isLeaf(d)) {
queue.add(d);
}
}
sorted.push(node);
g.removeNode(node);
}
}
}
const transitive = <T>(nodes: EquivMap<T, ArraySet<T>>, x: T): Set<T> => {
const deps: ArraySet<T> = nodes.get(x)!;
if (deps) {
return reduce(
reducer(
<any>null,
(acc, k: T) => <ArraySet<T>>union(acc, transitive(nodes, k))
),
deps,
deps
);
}
return new ArraySet<T>();
};