fix: DAG setLayer logic

This commit is contained in:
Justin Lin committed 2025-04-23 17:58:24 +08:00
1 parent 9b5da4e818
commit 56049a0a29
2 files changed
+37 -37

No files matched your search

+3 -3
View File
@@ -18,10 +18,10 @@ export default class DAG<T extends NodeInput> {
nodes.map((node) => [node.id, { ...node, layer: 0, index: 0 }])
)
this.edges = new Set<[string, string]>()
this.nodes.forEach((_, id) => {
for (const id of this.nodes.keys()) {
this.setLayer(id)
this.addEdges(id)
})
}
}
getNode(id: string) {
@@ -57,7 +57,7 @@ export default class DAG<T extends NodeInput> {
if (!node) {
throw new Error(`Node ${id} not found`)
}
if (node.layer !== undefined) {
if (node.layer !== 0) {
return node.layer
}
const previousLayer = node.requirements.map((rId: string) =>
+34 -34
View File
@@ -5,82 +5,82 @@ describe('dag layers', () => {
it('two layer', () => {
const data = [
{
_id: 'helloworld',
id: 'helloworld',
requirements: [],
},
{
_id: 'spell',
id: 'spell',
requirements: ['helloworld'],
},
]
const dag = new DAG(data)
expect(dag.getNode('helloworld').layer).toBe(1)
expect(dag.getNode('spell').layer).toBe(2)
expect(dag.getNode('helloworld')?.layer).toBe(1)
expect(dag.getNode('spell')?.layer).toBe(2)
})
it('branch', () => {
const data = [
{
_id: 'helloworld',
id: 'helloworld',
requirements: [],
},
{
_id: 'spell',
id: 'spell',
requirements: ['helloworld'],
},
{
_id: 'discover',
id: 'discover',
requirements: ['helloworld'],
},
]
const dag = new DAG(data)
expect(dag.getNode('helloworld').layer).toBe(1)
expect(dag.getNode('spell').layer).toBe(2)
expect(dag.getNode('discover').layer).toBe(2)
expect(dag.getNode('helloworld')?.layer).toBe(1)
expect(dag.getNode('spell')?.layer).toBe(2)
expect(dag.getNode('discover')?.layer).toBe(2)
})
it('merge', () => {
const data = [
{
_id: 'A',
id: 'A',
requirements: [],
},
{
_id: 'B',
id: 'B',
requirements: [],
},
{
_id: 'C',
id: 'C',
requirements: ['A', 'B'],
},
]
const dag = new DAG(data)
expect(dag.getNode('A').layer).toBe(1)
expect(dag.getNode('B').layer).toBe(1)
expect(dag.getNode('C').layer).toBe(2)
expect(dag.getNode('A')?.layer).toBe(1)
expect(dag.getNode('B')?.layer).toBe(1)
expect(dag.getNode('C')?.layer).toBe(2)
})
it('skip', () => {
const data = [
{
_id: 'A',
id: 'A',
requirements: [],
},
{
_id: 'B',
id: 'B',
requirements: [],
},
{
_id: 'C',
id: 'C',
requirements: ['A'],
},
{
_id: 'D',
id: 'D',
requirements: ['B', 'C'],
},
]
const dag = new DAG(data)
expect(dag.getNode('A').layer).toBe(1)
expect(dag.getNode('B').layer).toBe(1)
expect(dag.getNode('C').layer).toBe(2)
expect(dag.getNode('D').layer).toBe(3)
expect(dag.getNode('A')?.layer).toBe(1)
expect(dag.getNode('B')?.layer).toBe(1)
expect(dag.getNode('C')?.layer).toBe(2)
expect(dag.getNode('D')?.layer).toBe(3)
})
})
@@ -88,11 +88,11 @@ describe('dag edges', () => {
it('one edge', () => {
const data = [
{
_id: 'helloworld',
id: 'helloworld',
requirements: [],
},
{
_id: 'spell',
id: 'spell',
requirements: ['helloworld'],
},
]
@@ -104,15 +104,15 @@ describe('dag edges', () => {
it('branch edges', () => {
const data = [
{
_id: 'helloworld',
id: 'helloworld',
requirements: [],
},
{
_id: 'spell',
id: 'spell',
requirements: ['helloworld'],
},
{
_id: 'discover',
id: 'discover',
requirements: ['helloworld'],
},
]
@@ -131,11 +131,11 @@ describe('dag layers', () => {
it('two layer', () => {
const data = [
{
_id: 'helloworld',
id: 'helloworld',
requirements: [],
},
{
_id: 'spell',
id: 'spell',
requirements: ['helloworld'],
},
]
@@ -147,15 +147,15 @@ describe('dag layers', () => {
it('branch layers', () => {
const data = [
{
_id: 'helloworld',
id: 'helloworld',
requirements: [],
},
{
_id: 'spell',
id: 'spell',
requirements: ['helloworld'],
},
{
_id: 'discover',
id: 'discover',
requirements: ['helloworld'],
},
]