Make LayoutGroup.moveBy() linear time

This commit is contained in:
Ian Gulliver
2019-07-05 06:10:41 +00:00
parent 9331f7b101
commit 396436aa01
3 changed files with 16 additions and 18 deletions

View File

@@ -34,7 +34,7 @@ class GraphNode {
for (let from of this.linksIn) {
from.setSubgraph(subgraph, nodes);
}
for (let group of this.groups.values()) {
for (let group of this.groups) {
for (let node of group.nodes) {
node.setSubgraph(subgraph, nodes);
}
@@ -64,7 +64,7 @@ class GraphNode {
this.addAffinity(to, d => d <= 2 ? -INF : d * 11);
to.addAffinity(this, d => d <= 2 ? -INF : d * 9);
}
for (let group of this.groups.values()) {
for (let group of this.groups) {
for (let node of group.nodes) {
this.addAffinity(node, d => d * 100);
}

View File

@@ -53,11 +53,11 @@ class Layout {
this.groups_ = [];
for (let group of this.graph_.groups) {
let nodes = this.nodesFromGraphNodes(group.nodes);
this.groups_.push(new LayoutGroup(group, nodes));
this.groups_.push(new LayoutGroup(group, this.nodesByPos_, nodes));
}
for (let subgraph of this.graph_.nodesBySubgraph.values()) {
let nodes = this.nodesFromGraphNodes(subgraph);
this.groups_.push(new LayoutGroup(null, nodes));
this.groups_.push(new LayoutGroup(null, this.nodesByPos_, nodes));
}
}

View File

@@ -1,6 +1,7 @@
class LayoutGroup {
constructor(graphGroup, nodes) {
constructor(graphGroup, nodesByPos, nodes) {
this.graphGroup_ = graphGroup;
this.nodesByPos_ = nodesByPos;
this.nodes = new Set(nodes);
this.tension = 0;
}
@@ -9,7 +10,7 @@ class LayoutGroup {
// Groups don't track tension, since we always want to sort last for total
// tension
this.vec = [0, 0];
for (let node of this.nodes.values()) {
for (let node of this.nodes) {
for (let i of [0, 1]) {
this.vec[i] += node.vec[i];
};
@@ -17,7 +18,7 @@ class LayoutGroup {
}
offsetCollides(offset) {
for (let node of this.nodes.values()) {
for (let node of this.nodes) {
let other = node.offsetCollides(offset);
if (other && !this.nodes.has(other)) {
return other;
@@ -27,27 +28,24 @@ class LayoutGroup {
}
savePos() {
for (let node of this.nodes.values()) {
for (let node of this.nodes) {
node.savePos();
}
}
restorePos() {
for (let node of this.nodes.values()) {
for (let node of this.nodes) {
node.restorePos();
}
}
moveBy(offset) {
let nodes = new Set(this.nodes.values());
while (nodes.size) {
for (let node of nodes) {
if (node.offsetCollides(offset)) {
continue;
}
node.moveBy(offset);
nodes.delete(node);
}
for (let node of this.nodes) {
node.moveBy(offset);
}
// Fix up nodesByPos, as intra-group collisions may have corrupted it
for (let node of this.nodes) {
this.nodesByPos_.set(node.pos.toString(), node);
}
}