mirror of
https://github.com/linux-odyssey/linux-odyssey.git
synced 2026-11-07 19:10:12 +08:00
feat: Add edges
This commit is contained in:
1 parent
fae2d8199d
commit
e18f0db5ad
2 files changed
+144
-50
No files matched your search
+19
-8
@@ -1,7 +1,18 @@
|
||||
export default class DAG {
|
||||
constructor(nodes) {
|
||||
this.nodes = new Map(nodes.map((node) => [node._id, node]))
|
||||
this.edges = []
|
||||
this.edges = new Set()
|
||||
}
|
||||
|
||||
format() {
|
||||
this.nodes.forEach((_, id) => {
|
||||
this.setLayer(id)
|
||||
this.addEdges(id)
|
||||
})
|
||||
}
|
||||
|
||||
get(id) {
|
||||
return this.nodes.get(id)
|
||||
}
|
||||
|
||||
setLayer(id) {
|
||||
@@ -18,13 +29,13 @@ export default class DAG {
|
||||
return node.layer
|
||||
}
|
||||
|
||||
format() {
|
||||
this.nodes.forEach((_, id) => {
|
||||
this.setLayer(id)
|
||||
addEdges(id) {
|
||||
const node = this.nodes.get(id)
|
||||
if (!node) {
|
||||
throw new Error(`Node ${id} not found`)
|
||||
}
|
||||
node.requirements.forEach((rId) => {
|
||||
this.edges.add([rId, id])
|
||||
})
|
||||
}
|
||||
|
||||
get(id) {
|
||||
return this.nodes.get(id)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user