Skip to content

Commit 3b47212

Browse files
committed
Reduce use of TreeSet
1 parent 92c641d commit 3b47212

File tree

1 file changed

+17
-10
lines changed

1 file changed

+17
-10
lines changed

visualizer/IdealGraphVisualizer/HierarchicalLayout/src/main/java/org/graalvm/visualizer/hierarchicallayout/HierarchicalLayoutManager.java

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,10 @@ private class LayoutNode {
173173
public final List<LayoutEdge> succs = new ArrayList<>();
174174
public int pos = -1; // Position within layer
175175

176+
public int getPos() {
177+
return pos;
178+
}
179+
176180
public float crossingNumber = 0;
177181

178182
public void loadCrossingNumber(boolean up) {
@@ -1414,11 +1418,12 @@ public void refresh() {
14141418

14151419
private class NodeRow {
14161420

1417-
private final TreeSet<LayoutNode> treeSet;
1421+
private final ArrayList<LayoutNode> positions;
14181422
private final int[] space;
1423+
static final Comparator<LayoutNode> COMPARATOR = Comparator.comparingInt(LayoutNode::getPos);
14191424

14201425
public NodeRow(int[] space) {
1421-
treeSet = new TreeSet<>(NODE_POSITION_COMPARATOR);
1426+
this.positions = new ArrayList<>();
14221427
this.space = space;
14231428
}
14241429

@@ -1429,22 +1434,24 @@ public int offset(LayoutNode n1, LayoutNode n2) {
14291434
}
14301435

14311436
public void insert(LayoutNode n, int pos) {
1432-
SortedSet<LayoutNode> headSet = treeSet.headSet(n);
14331437
LayoutNode leftNeighbor;
14341438
int minX = Integer.MIN_VALUE;
1435-
if (!headSet.isEmpty()) {
1436-
leftNeighbor = headSet.last();
1439+
int idx = Collections.binarySearch(positions, n, COMPARATOR);
1440+
if (idx > 0) {
1441+
throw new IllegalArgumentException("already in list");
1442+
}
1443+
int insertPos = -idx - 1; // where you would insert
1444+
leftNeighbor = insertPos > 0 ? positions.get(insertPos - 1) : null;
1445+
LayoutNode rightNeighbor = insertPos < positions.size() ? positions.get(insertPos) : null;
1446+
if (leftNeighbor != null) {
14371447
minX = leftNeighbor.getRightSide() + offset(leftNeighbor, n);
14381448
}
14391449

14401450
if (pos < minX) {
14411451
n.x = minX;
14421452
} else {
1443-
LayoutNode rightNeighbor;
1444-
SortedSet<LayoutNode> tailSet = treeSet.tailSet(n);
14451453
int maxX = Integer.MAX_VALUE;
1446-
if (!tailSet.isEmpty()) {
1447-
rightNeighbor = tailSet.first();
1454+
if (rightNeighbor != null) {
14481455
maxX = rightNeighbor.x - offset(n, rightNeighbor) - n.getWholeWidth();
14491456
}
14501457

@@ -1455,7 +1462,7 @@ public void insert(LayoutNode n, int pos) {
14551462
}
14561463
assert minX <= maxX : minX + " vs " + maxX;
14571464
}
1458-
treeSet.add(n);
1465+
positions.add(insertPos, n);
14591466
}
14601467
}
14611468

0 commit comments

Comments
 (0)