mirror of
https://github.com/linux-odyssey/linux-odyssey.git
synced 2026-11-07 19:10:12 +08:00
wip: replace packages with pure files
This commit is contained in:
1 parent
2d95ca382f
commit
87b8ca5d04
112 files changed
+740
-522
No files matched your search
@@ -0,0 +1,85 @@
|
||||
export interface NodeInput {
|
||||
id: string
|
||||
requirements: string[]
|
||||
}
|
||||
|
||||
type Node<T extends NodeInput> = T & {
|
||||
layer: number
|
||||
index: number
|
||||
}
|
||||
|
||||
export default class DAG<T extends NodeInput> {
|
||||
private nodes = new Map<string, Node<T>>()
|
||||
private edges = new Set<[string, string]>()
|
||||
private layers: number[] = []
|
||||
|
||||
constructor(nodes: T[]) {
|
||||
this.nodes = new Map(
|
||||
nodes.map((node) => [node.id, { ...node, layer: 0, index: 0 }])
|
||||
)
|
||||
this.edges = new Set<[string, string]>()
|
||||
for (const id of this.nodes.keys()) {
|
||||
this.setLayer(id)
|
||||
this.addEdges(id)
|
||||
}
|
||||
}
|
||||
|
||||
getNode(id: string) {
|
||||
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: string) {
|
||||
const node = this.nodes.get(id)
|
||||
if (!node) {
|
||||
throw new Error(`Node ${id} not found`)
|
||||
}
|
||||
return this.layers[node.layer - 1] || 0
|
||||
}
|
||||
|
||||
private setLayer(id: string) {
|
||||
const node = this.nodes.get(id)
|
||||
if (!node) {
|
||||
throw new Error(`Node ${id} not found`)
|
||||
}
|
||||
if (node.layer !== 0) {
|
||||
return node.layer
|
||||
}
|
||||
const previousLayer = node.requirements.map((rId: string) =>
|
||||
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: string) {
|
||||
const node = this.nodes.get(id)
|
||||
if (!node) {
|
||||
throw new Error(`Node ${id} not found`)
|
||||
}
|
||||
node.requirements.forEach((rId: string) => {
|
||||
this.edges.add([rId, id])
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user