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