Skip to content

Commit 2145abc

Browse files
Refactor by moving the recursive copy to the Graph abstraction.
Co-authored-by: Michael Kelley Harris <[email protected]>
1 parent f8815ca commit 2145abc

File tree

2 files changed

+27
-26
lines changed

2 files changed

+27
-26
lines changed

src/graph.js

+22
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,28 @@ export class Graph {
5555
// return Graph.load(module.default)
5656
// }
5757

58+
copy(nid,output) {
59+
const doing = {}
60+
const done = []
61+
62+
const nodecopy = nid => {
63+
if(nid in doing) {
64+
console.log('copied before', nid, 'doing', doing)
65+
return}
66+
console.log('copy start', nid, 'doing', doing)
67+
done.push(nid)
68+
const node = this.nodes[nid]
69+
doing[nid] = output.addNode(node.type,node.props)
70+
for (const rid of node.out) nodecopy(this.rels[rid].to)
71+
for (const rid of node.in) nodecopy(this.rels[rid].from)
72+
console.log('linking',nid,'to',node.out.map(rid => this.rels[rid].to))
73+
for (const rid of node.out) output.addRel('',doing[nid],doing[this.rels[rid].to],{})
74+
}
75+
76+
nodecopy(nid)
77+
return done
78+
}
79+
5880
n(type=null, props={}) {
5981
let nids = Object.keys(this.nodes).map(key => +key)
6082
if (type) nids = nids.filter(nid => this.nodes[nid].type == type)

test/partition.js

+5-26
Original file line numberDiff line numberDiff line change
@@ -12,47 +12,26 @@
1212
function partitions(input) {
1313
const output = [] // graphs
1414
let doing = {} // nid => new nid
15-
const checkpoint = () => {
16-
console.log(output
17-
.map(graph => `${print(graph.nodes)}\n\n${print(graph.rels)}`)
18-
.join("\n\n")+"\n"+('-'.repeat(60)))
19-
}
20-
const nodes = input.nodes
21-
const rels = input.rels
22-
const todo = [...Array(nodes.length).keys()]
15+
let todo = [...Array(input.nodes.length).keys()]
2316
.map(n => [n,Math.random()])
2417
.sort((a,b)=>a[1]-b[1])
2518
.map(v=>v[0])
2619

27-
const copy = nid => {
28-
if(nid in doing) {
29-
console.log('copied before', nid, 'doing', doing)
30-
return}
31-
console.log('copy start', nid, 'doing', doing)
32-
todo.splice(todo.indexOf(nid),1)
33-
const node = nodes[nid]
34-
doing[nid] = output[0].addNode(node.type,node.props)
35-
for (const rid of node.out) copy(rels[rid].to)
36-
for (const rid of node.in) copy(rels[rid].from)
37-
console.log('linking',nid,'to',node.out.map(rid => rels[rid].to))
38-
for (const rid of node.out) output[0].addRel('',doing[nid],doing[rels[rid].to],{})
39-
checkpoint()
40-
}
41-
4220
console.log('order todo',todo)
4321
while(todo.length) {
4422
const nid = todo.shift()
4523
if (nid in doing) {
4624
console.log('did',nid,'already')
4725
continue
4826
}
49-
const node = nodes[nid]
27+
const node = input.nodes[nid]
5028
const title = node.props.name.replaceAll("\n"," ")
5129
if (node.in.length + node.out.length) {
5230
console.log('doing',nid,title)
5331
output.unshift(new Graph())
54-
doing = {}
55-
copy(nid)
32+
const done = input.copy(nid,output[0])
33+
todo = todo.filter(nid => !done.includes(nid))
34+
console.log(`${print(output[0].nodes)}\n\n${print(output[0].rels)}`,"\n")
5635
}
5736
else
5837
console.log('skipping',nid,title)

0 commit comments

Comments
 (0)