mirror of
https://github.com/linux-odyssey/linux-odyssey.git
synced 2026-11-06 11:30:13 +08:00
fix: setup Jest with TS
This commit is contained in:
1 parent
557af91d4b
commit
a18e0d9b68
12 files changed
+45
-9
No files matched your search
@@ -0,0 +1,73 @@
|
||||
export default class DAG {
|
||||
#nodes = new Map()
|
||||
|
||||
#edges = new Set()
|
||||
|
||||
#layers = []
|
||||
|
||||
constructor(nodes) {
|
||||
this.#nodes = new Map(nodes.map((node) => [node._id, node]))
|
||||
this.#edges = new Set()
|
||||
this.#nodes.forEach((_, id) => {
|
||||
this.#setLayer(id)
|
||||
this.#addEdges(id)
|
||||
})
|
||||
}
|
||||
|
||||
getNode(id) {
|
||||
return this.#nodes.get(id)
|
||||
}
|
||||
|
||||
getNodes() {
|
||||
return Array.from(this.#nodes.values())
|
||||
}
|
||||
|
||||
getEdges() {
|
||||
return this.#edges
|
||||
}
|
||||
|
||||
getEdgesArray() {
|
||||
return Array.from(this.#edges.values())
|
||||
}
|
||||
|
||||
getLayers() {
|
||||
return this.#layers
|
||||
}
|
||||
|
||||
getLayer(id) {
|
||||
const node = this.getNode(id)
|
||||
if (!node) {
|
||||
throw new Error(`Node ${id} not found`)
|
||||
}
|
||||
return this.#layers[node.layer - 1]
|
||||
}
|
||||
|
||||
#setLayer(id) {
|
||||
const node = this.getNode(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
|
||||
if (this.#layers.length < node.layer) {
|
||||
this.#layers.push(1)
|
||||
} else {
|
||||
this.#layers[node.layer - 1] += 1
|
||||
}
|
||||
node.index = this.#layers[node.layer - 1]
|
||||
return node.layer
|
||||
}
|
||||
|
||||
#addEdges(id) {
|
||||
const node = this.getNode(id)
|
||||
if (!node) {
|
||||
throw new Error(`Node ${id} not found`)
|
||||
}
|
||||
node.requirements.forEach((rId) => {
|
||||
this.#edges.add([rId, id])
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user