|
12 | 12 | function partitions(input) {
|
13 | 13 | const output = [] // graphs
|
14 | 14 | 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()] |
23 | 16 | .map(n => [n,Math.random()])
|
24 | 17 | .sort((a,b)=>a[1]-b[1])
|
25 | 18 | .map(v=>v[0])
|
26 | 19 |
|
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 |
| - |
42 | 20 | console.log('order todo',todo)
|
43 | 21 | while(todo.length) {
|
44 | 22 | const nid = todo.shift()
|
45 | 23 | if (nid in doing) {
|
46 | 24 | console.log('did',nid,'already')
|
47 | 25 | continue
|
48 | 26 | }
|
49 |
| - const node = nodes[nid] |
| 27 | + const node = input.nodes[nid] |
50 | 28 | const title = node.props.name.replaceAll("\n"," ")
|
51 | 29 | if (node.in.length + node.out.length) {
|
52 | 30 | console.log('doing',nid,title)
|
53 | 31 | 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") |
56 | 35 | }
|
57 | 36 | else
|
58 | 37 | console.log('skipping',nid,title)
|
|
0 commit comments