feat: Add edges

This commit is contained in:
Lin, Hung-Shin committed 2023-11-15 14:43:34 +08:00
1 parent fae2d8199d
commit e18f0db5ad
2 files changed
+144 -50

No files matched your search

+19 -8
View File
@@ -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)
}
}