@painekillers/tree-builder-core 1.0.0 → 1.1.0
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/graph.js +54 -3
- package/package.json +2 -2
- package/world/bridge.js +2 -2
package/graph.js
CHANGED
|
@@ -79,8 +79,28 @@ export class LayoutNode {
|
|
|
79
79
|
}
|
|
80
80
|
}
|
|
81
81
|
|
|
82
|
+
/*
|
|
83
|
+
Recursion settings
|
|
84
|
+
Null: No recursion
|
|
85
|
+
False: Creates fake nodes when encountering recursion
|
|
86
|
+
True: Connects back to existing instance of a node when recursing
|
|
87
|
+
#: Real recursion to depths #
|
|
88
|
+
*/
|
|
89
|
+
|
|
90
|
+
/*
|
|
91
|
+
Strategies
|
|
92
|
+
- [X] Current BFS (First) — Layer by shortest distance from root; first discovery determines placement.
|
|
93
|
+
- [X] Undirected BFS (Undirected) — Shortest distance to root; layering is on directional.
|
|
94
|
+
- [X] Directed BFS, outward only (Outwards) — Follow prerequisite-to-dependent edges only.
|
|
95
|
+
- [X] Furthest-parent layering (Furthest) — Place each node one layer below its deepest prerequisite.
|
|
96
|
+
*/
|
|
97
|
+
|
|
82
98
|
export class LayoutGraph {
|
|
83
|
-
constructor(graph, root, recurse) {
|
|
99
|
+
constructor(graph, root, recurse, strategy = "First") {
|
|
100
|
+
if(strategy == "First" && typeof recurse == "number") {
|
|
101
|
+
throw new Error(`Cannot use strategy ${strategy} with numeric recursion setting`);
|
|
102
|
+
}
|
|
103
|
+
|
|
84
104
|
this.layers = new Map()
|
|
85
105
|
|
|
86
106
|
const rootNode = graph.nodeFromId(root)
|
|
@@ -145,7 +165,22 @@ export class LayoutGraph {
|
|
|
145
165
|
|
|
146
166
|
//holy crap i cannot think bruh
|
|
147
167
|
if(((recurse === false) || (recurse === null)) && pos.layer !== layer) {
|
|
148
|
-
if(
|
|
168
|
+
if (
|
|
169
|
+
recurse == null ||
|
|
170
|
+
strategy == "Outwards" &&
|
|
171
|
+
from !== null &&
|
|
172
|
+
Math.abs(layer) < Math.abs(from.layer)
|
|
173
|
+
) { continue }
|
|
174
|
+
|
|
175
|
+
if (
|
|
176
|
+
strategy == "Furthest" &&
|
|
177
|
+
(
|
|
178
|
+
(layer > 0 && !node.parents.every(val => visited.has(val.id)))
|
|
179
|
+
||
|
|
180
|
+
(layer < 0 && !node.children.every(val => visited.has(val.id)))
|
|
181
|
+
)
|
|
182
|
+
) { continue }
|
|
183
|
+
|
|
149
184
|
const fn = new LayoutNode(node, layer, this.layers.get(pos.layer)[pos.ind])
|
|
150
185
|
this.layers.get(layer).push(fn)
|
|
151
186
|
|
|
@@ -159,6 +194,21 @@ export class LayoutGraph {
|
|
|
159
194
|
continue
|
|
160
195
|
}
|
|
161
196
|
|
|
197
|
+
if (
|
|
198
|
+
strategy == "Outwards" &&
|
|
199
|
+
from !== null &&
|
|
200
|
+
Math.abs(layer) < Math.abs(from.layer)
|
|
201
|
+
) { continue }
|
|
202
|
+
|
|
203
|
+
if (
|
|
204
|
+
strategy == "Furthest" &&
|
|
205
|
+
(
|
|
206
|
+
(layer > 0 && !node.parents.every(val => visited.has(val.id)))
|
|
207
|
+
||
|
|
208
|
+
(layer < 0 && !node.children.every(val => visited.has(val.id)))
|
|
209
|
+
)
|
|
210
|
+
) { continue }
|
|
211
|
+
|
|
162
212
|
const ln = new LayoutNode(node, layer)
|
|
163
213
|
|
|
164
214
|
this.layers.get(layer).push(ln)
|
|
@@ -185,10 +235,11 @@ export class LayoutGraph {
|
|
|
185
235
|
for (const parent of node.parents) {
|
|
186
236
|
queue.push({
|
|
187
237
|
node: parent,
|
|
188
|
-
layer: layer - 1,
|
|
238
|
+
layer: strategy !== "Undirected" ? layer - 1: layer + 1,
|
|
189
239
|
from: pos
|
|
190
240
|
})
|
|
191
241
|
}
|
|
242
|
+
|
|
192
243
|
}
|
|
193
244
|
}
|
|
194
245
|
}
|
package/package.json
CHANGED
package/world/bridge.js
CHANGED
|
@@ -25,8 +25,8 @@ export default class Bridge {
|
|
|
25
25
|
}
|
|
26
26
|
}
|
|
27
27
|
|
|
28
|
-
calculateLayout(root, recurse){ // Real maxdepth, since we cant actually make it infinite
|
|
29
|
-
this.layout = new LayoutGraph(this.graph, root, recurse)
|
|
28
|
+
calculateLayout(root, recurse, strategy = "First"){ // Real maxdepth, since we cant actually make it infinite
|
|
29
|
+
this.layout = new LayoutGraph(this.graph, root, recurse, strategy)
|
|
30
30
|
// Since JS is goofy like this ill just have recurse be a number when we want recursive
|
|
31
31
|
this.root = this.graph.nodes.get(root)
|
|
32
32
|
|