Skip to content

Commit 9129c34

Browse files
Add clustering outer loop to copy
1 parent 74e91a4 commit 9129c34

File tree

2 files changed

+30
-6
lines changed

2 files changed

+30
-6
lines changed

src/graph.js

+20-6
Original file line numberDiff line numberDiff line change
@@ -60,22 +60,36 @@ export class Graph {
6060
const done = []
6161

6262
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)
63+
if(nid in doing) return
6764
done.push(nid)
6865
const node = this.nodes[nid]
6966
doing[nid] = output.addNode(node.type,node.props)
7067
for (const rid of node.out) nodecopy(this.rels[rid].to)
7168
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))
7369
for (const rid of node.out) output.addRel('',doing[nid],doing[this.rels[rid].to],{})
7470
}
7571

7672
nodecopy(nid)
7773
return done
78-
}
74+
}
75+
76+
clusters() {
77+
const result = []
78+
const todo = [...this.nodes.keys()]
79+
const doit = nid => {
80+
const graph = new Graph()
81+
const done = this.copy(nid,graph)
82+
for (const nid of done) {
83+
const index = todo.indexOf(nid)
84+
todo.splice(index, 1)
85+
}
86+
return graph
87+
}
88+
while(todo.length){
89+
result.push(doit(todo[0]))
90+
}
91+
return result
92+
}
7993

8094
n(type=null, props={}) {
8195
let nids = Object.keys(this.nodes).map(key => +key)

test/copy.test.js

+10
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,14 @@ Deno.test("We don't have aliased relations in the copy", async () => {
5656
assertEquals(names(cluster), ["Good Bye","Mixing","Transmission","Watch"]);
5757
assertEquals(relations(local), ["Mixing -> Transmission","Mixing -> Watch","Search -> Sites","Search -> Solo","Transmission -> Mixing"]);
5858
assertEquals(relations(cluster), ["Mixing -> Good Bye","Mixing -> Transmission","Mixing -> Watch","Transmission -> Mixing"]);
59+
});
60+
61+
Deno.test("We can find all of the clusters and copy each one", async () => {
62+
const norm = aa => aa.map(a=>names(a)).sort((a,b) => a[0]<b[0] ? 1 : -1)
63+
const local = Graph.load(JSON.parse(input.stringify()))
64+
const result = local.clusters()
65+
assertEquals(result.length,3)
66+
const want = [["Mixing","Transmission","Watch"],["Search","Sites","Solo"],["Indexing"]].sort((a,b) => a[0]<b[0] ? 1 : -1)
67+
assertEquals(norm(result),want)
68+
5969
});

0 commit comments

Comments
 (0)