mirror of
https://github.com/linux-odyssey/linux-odyssey.git
synced 2026-11-07 19:10:12 +08:00
feat: calculate layer
This commit is contained in:
1 parent
c92a2e1d70
commit
fae2d8199d
2 files changed
+80
No files matched your search
@@ -0,0 +1,30 @@
|
||||
export default class DAG {
|
||||
constructor(nodes) {
|
||||
this.nodes = new Map(nodes.map((node) => [node._id, node]))
|
||||
this.edges = []
|
||||
}
|
||||
|
||||
setLayer(id) {
|
||||
console.log(`setLayer(${id})`)
|
||||
const node = this.nodes.get(id)
|
||||
if (!node) {
|
||||
throw new Error(`Node ${id} not found`)
|
||||
}
|
||||
if (node.layer !== undefined) {
|
||||
return node.layer
|
||||
}
|
||||
const previousLayer = node.requirements.map((rId) => this.setLayer(rId))
|
||||
node.layer = Math.max(...previousLayer, 0) + 1
|
||||
return node.layer
|
||||
}
|
||||
|
||||
format() {
|
||||
this.nodes.forEach((_, id) => {
|
||||
this.setLayer(id)
|
||||
})
|
||||
}
|
||||
|
||||
get(id) {
|
||||
return this.nodes.get(id)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user