Skip to content

Commit

Permalink
Refactor by moving the recursive copy to the Graph abstraction.
Browse files Browse the repository at this point in the history
Co-authored-by: Michael Kelley Harris <kelley@sourcecell.com>
  • Loading branch information
WardCunningham and mkelleyharris committed Mar 8, 2024
1 parent f8815ca commit 2145abc
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 26 deletions.
22 changes: 22 additions & 0 deletions src/graph.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,28 @@ export class Graph {
// return Graph.load(module.default)
// }

copy(nid,output) {
const doing = {}
const done = []

const nodecopy = nid => {
if(nid in doing) {
console.log('copied before', nid, 'doing', doing)
return}
console.log('copy start', nid, 'doing', doing)
done.push(nid)
const node = this.nodes[nid]
doing[nid] = output.addNode(node.type,node.props)
for (const rid of node.out) nodecopy(this.rels[rid].to)
for (const rid of node.in) nodecopy(this.rels[rid].from)
console.log('linking',nid,'to',node.out.map(rid => this.rels[rid].to))
for (const rid of node.out) output.addRel('',doing[nid],doing[this.rels[rid].to],{})
}

nodecopy(nid)
return done
}

n(type=null, props={}) {
let nids = Object.keys(this.nodes).map(key => +key)
if (type) nids = nids.filter(nid => this.nodes[nid].type == type)
Expand Down
31 changes: 5 additions & 26 deletions test/partition.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,47 +12,26 @@
function partitions(input) {
const output = [] // graphs
let doing = {} // nid => new nid
const checkpoint = () => {
console.log(output
.map(graph => `${print(graph.nodes)}\n\n${print(graph.rels)}`)
.join("\n\n")+"\n"+('-'.repeat(60)))
}
const nodes = input.nodes
const rels = input.rels
const todo = [...Array(nodes.length).keys()]
let todo = [...Array(input.nodes.length).keys()]
.map(n => [n,Math.random()])
.sort((a,b)=>a[1]-b[1])
.map(v=>v[0])

const copy = nid => {
if(nid in doing) {
console.log('copied before', nid, 'doing', doing)
return}
console.log('copy start', nid, 'doing', doing)
todo.splice(todo.indexOf(nid),1)
const node = nodes[nid]
doing[nid] = output[0].addNode(node.type,node.props)
for (const rid of node.out) copy(rels[rid].to)
for (const rid of node.in) copy(rels[rid].from)
console.log('linking',nid,'to',node.out.map(rid => rels[rid].to))
for (const rid of node.out) output[0].addRel('',doing[nid],doing[rels[rid].to],{})
checkpoint()
}

console.log('order todo',todo)
while(todo.length) {
const nid = todo.shift()
if (nid in doing) {
console.log('did',nid,'already')
continue
}
const node = nodes[nid]
const node = input.nodes[nid]
const title = node.props.name.replaceAll("\n"," ")
if (node.in.length + node.out.length) {
console.log('doing',nid,title)
output.unshift(new Graph())
doing = {}
copy(nid)
const done = input.copy(nid,output[0])
todo = todo.filter(nid => !done.includes(nid))
console.log(`${print(output[0].nodes)}\n\n${print(output[0].rels)}`,"\n")
}
else
console.log('skipping',nid,title)
Expand Down

0 comments on commit 2145abc

Please sign in to comment.