Skip to content

Commit 44e8664

Browse files
committed
Collection cleanups, stream removal and other minor cleanups
1 parent b02ade1 commit 44e8664

File tree

1 file changed

+64
-49
lines changed

1 file changed

+64
-49
lines changed

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

Lines changed: 64 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@
5454
import java.util.concurrent.atomic.AtomicBoolean;
5555
import java.util.function.IntUnaryOperator;
5656
import java.util.logging.Logger;
57-
import java.util.stream.Collectors;
5857

5958
import static org.graalvm.visualizer.settings.layout.LayoutSettings.BOTH_SORT;
6059
import static org.graalvm.visualizer.settings.layout.LayoutSettings.CENTER_CROSSING_X;
@@ -177,7 +176,7 @@ public enum Combine {
177176
private final HashSet<Link> reversedLinks;
178177
private final HashSet<LayoutEdge> longEdges;
179178
private final ArrayList<LayoutNode> nodes;
180-
private final ArrayList<LayoutNode> standAlones;
179+
private final HashSet<LayoutNode> standAlones;
181180
private final HashMap<Vertex, LayoutNode> vertexToLayoutNode;
182181
private final HashMap<Link, List<Point>> reversedLinkStartPoints;
183182
private final HashMap<Link, List<Point>> reversedLinkEndPoints;
@@ -290,9 +289,13 @@ public void loadCrossingNumber(boolean up) {
290289
public int loadCrossingNumber(boolean up, LayoutNode source) {
291290
int count = 0;
292291
if (up) {
293-
count = succs.stream().map((e) -> e.loadCrossingNumber(up, source)).reduce(count, Integer::sum);
292+
for (LayoutEdge e : succs) {
293+
count = count + e.loadCrossingNumber(true, source);
294+
}
294295
} else {
295-
count = preds.stream().map((e) -> e.loadCrossingNumber(up, source)).reduce(count, Integer::sum);
296+
for (LayoutEdge e : preds) {
297+
count = count + e.loadCrossingNumber(false, source);
298+
}
296299
}
297300
return count;
298301
}
@@ -516,7 +519,7 @@ public HierarchicalLayoutManager(Combine b, LayoutSettingBean setting) {
516519
reversedLinkStartPoints = new HashMap<>();
517520
reversedLinkEndPoints = new HashMap<>();
518521
nodes = new ArrayList<>();
519-
standAlones = new ArrayList<>();
522+
standAlones = new HashSet<>();
520523
longEdges = new HashSet<>();
521524
cancelled = new AtomicBoolean(false);
522525
}
@@ -645,16 +648,14 @@ public void doLayout(LayoutGraph graph) {
645648

646649
private class WriteResult extends AlgorithmPart {
647650

648-
private HashMap<Link, List<Point>> splitStartPoints;
649-
private HashMap<Link, List<Point>> splitEndPoints;
650651
private HashMap<Point, Point> pointsIdentity;
651652
private int pointCount;
652653
private final int addition = LAYER_OFFSET / 2;
653654

654655
@Override
655656
protected void run() {
656-
splitStartPoints = new HashMap<>();
657-
splitEndPoints = new HashMap<>();
657+
HashMap<Link, List<Point>> splitStartPoints = new HashMap<>();
658+
HashMap<Link, List<Point>> splitEndPoints = new HashMap<>();
658659
pointsIdentity = new HashMap<>();
659660
HashMap<Vertex, Point> vertexPositions = new HashMap<>();
660661
HashMap<Link, List<Point>> linkPositions = new HashMap<>();
@@ -934,7 +935,7 @@ private boolean allLinksSet() {
934935
return graph.getLinks().stream().filter(l -> l.getFrom().getVertex() != l.getTo().getVertex() && l.getTo().getVertex().isVisible() && l.getFrom().getVertex().isVisible()).map(l -> l.getControlPoints().size()).allMatch(s -> s > 1);
935936
}
936937

937-
private class ReductionEntry {
938+
private static class ReductionEntry {
938939

939940
final Point lastPoint;
940941
final int nextPointIndex;
@@ -1013,10 +1014,11 @@ private List<ReductionEntry> makeReductionEntries(List<Map.Entry<Link, List<Poin
10131014
}
10141015
//collect edges to ReductionEntries (copy reduced Points list if there is branching)
10151016
boolean branching = nexts.size() > 1;
1016-
return nexts.entrySet().stream().map(e
1017-
-> new ReductionEntry(lastPoint, nextIndex, e.getKey(), e.getValue(),
1018-
branching ? new ArrayList<>(reducedPoints) : reducedPoints))
1019-
.collect(Collectors.toList());
1017+
List<ReductionEntry> list = new ArrayList<>();
1018+
for (Map.Entry<Point, List<Map.Entry<Link, List<Point>>>> e : nexts.entrySet()) {
1019+
list.add(new ReductionEntry(lastPoint, nextIndex, e.getKey(), e.getValue(), branching ? new ArrayList<>(reducedPoints) : reducedPoints));
1020+
}
1021+
return list;
10201022
}
10211023

10221024
/**
@@ -1402,7 +1404,11 @@ private int median(ArrayList<Integer> values, LayoutNode n, LayoutNode last, boo
14021404
}
14031405
}
14041406
if (!isDefaultLayout && meanNotMedian) {
1405-
return values.stream().reduce(0, Integer::sum) / values.size();
1407+
int sum = 0;
1408+
for (int v : values) {
1409+
sum += v;
1410+
}
1411+
return sum / values.size();
14061412
}
14071413
values.sort(Integer::compare);
14081414
if (values.size() % 2 == 0) {
@@ -1564,7 +1570,7 @@ public boolean isVisible() {
15641570

15651571
@Override
15661572
public boolean addAll(Collection<? extends LayoutNode> c) {
1567-
c.forEach((n) -> add0(n));
1573+
c.forEach(this::add0);
15681574
return super.addAll(c);
15691575
}
15701576

@@ -1656,11 +1662,7 @@ public void insert(LayoutNode n, int pos) {
16561662
maxX = rightNeighbor.x - offset(n, rightNeighbor) - n.getWholeWidth();
16571663
}
16581664

1659-
if (pos > maxX) {
1660-
n.x = maxX;
1661-
} else {
1662-
n.x = pos;
1663-
}
1665+
n.x = Math.min(pos, maxX);
16641666
assert minX <= maxX : minX + " vs " + maxX;
16651667
}
16661668
positions.add(insertPos, n);
@@ -1690,7 +1692,7 @@ public void preCheck() {
16901692
}
16911693
}
16921694

1693-
@SuppressWarnings({"unchecked", "rawtypes"})
1695+
@SuppressWarnings({"unchecked"})
16941696
private void createLayers() {
16951697
if (!isDefaultLayout && crossingSort && !isCrossingByConnDiff) {
16961698
upCrossing = new ArrayList[layerCount];
@@ -1731,7 +1733,7 @@ private void createLayers() {
17311733
}
17321734
}
17331735
}
1734-
toMove = !routingCrossing ? layers : Arrays.stream(layers).map(l -> l.stream().filter(n -> n.isDummy()).collect(LayoutLayer::new, LayoutLayer::add, LayoutLayer::addAll)).toArray(LayoutLayer[]::new);
1736+
toMove = !routingCrossing ? layers : Arrays.stream(layers).map(l -> l.stream().filter(LayoutNode::isDummy).collect(LayoutLayer::new, LayoutLayer::add, LayoutLayer::addAll)).toArray(LayoutLayer[]::new);
17351737
if (!isDefaultLayout && crossingSort && !isCrossingByConnDiff) {
17361738
for (int i = 0; i < layerCount; ++i) {
17371739
upCrossing[i] = new ArrayList<>(layers[i]);
@@ -1873,7 +1875,7 @@ private void downSweep() {
18731875
}
18741876
if (!isDefaultLayout && isCrossingByConnDiff) {
18751877
changeXOfLayer(i);
1876-
layers[i].sort((n1, n2) -> Integer.compare(n1.getCenterX(), n2.getCenterX()));
1878+
layers[i].sort(Comparator.comparingInt(LayoutNode::getCenterX));
18771879
updateXOfLayer(i);
18781880
} else {
18791881
updateCrossingNumbers(i, true);
@@ -1899,7 +1901,7 @@ private void upSweep() {
18991901

19001902
if (!isDefaultLayout && isCrossingByConnDiff) {
19011903
changeXOfLayer(i);
1902-
layers[i].sort((n1, n2) -> Integer.compare(n1.getCenterX(), n2.getCenterX()));
1904+
layers[i].sort(Comparator.comparingInt(LayoutNode::getCenterX));
19031905
updateXOfLayer(i);
19041906
} else {
19051907
updateCrossingNumbers(i, false);
@@ -2120,7 +2122,7 @@ protected void run() {
21202122
if (portHash.containsKey(i)) {
21212123

21222124
List<LayoutEdge> list = portHash.get(i);
2123-
Collections.sort(list, LAYER_COMPARATOR);
2125+
list.sort(LAYER_COMPARATOR);
21242126

21252127
if (list.size() == 1) {
21262128
processSingleEdge(list.get(0));
@@ -2213,7 +2215,7 @@ public void postCheck() {
22132215

22142216
private class AssignLayers extends AlgorithmPart {
22152217

2216-
Set<LayoutNode> checked = new HashSet<>();
2218+
HashSet<LayoutNode> checked = new HashSet<>();
22172219

22182220
@Override
22192221
public void preCheck() {
@@ -2371,7 +2373,6 @@ private void reassignLayers() {
23712373
lay.addAll(Arrays.asList(HierarchicalLayoutManager.this.layers));
23722374
double avg = lay.stream().mapToInt(l -> l.getMinimalWidth()).sum() / layerCount;
23732375
boolean up, down;
2374-
LayoutNode node = null;
23752376
final boolean isQuick = decreaseLayerWidthDeviationQuick;
23762377
while (!lay.isEmpty()) {
23772378
if (cancelled.get()) {
@@ -2444,9 +2445,7 @@ private void moveUp() {
24442445
al.add(node);
24452446
--node.layer;
24462447
}
2447-
for (LayoutLayer l : reassign) {
2448-
lay.add(l);
2449-
}
2448+
lay.addAll(reassign);
24502449
}
24512450

24522451
private void moveDown() {
@@ -2462,9 +2461,7 @@ private void moveDown() {
24622461
al.add(node);
24632462
++node.layer;
24642463
}
2465-
for (LayoutLayer l : reassign) {
2466-
lay.add(l);
2467-
}
2464+
lay.addAll(reassign);
24682465
}
24692466

24702467
private boolean canMoveUp(LayoutNode node) {
@@ -2573,7 +2570,7 @@ private boolean canMoveDown(LayoutNode node) {
25732570
private void getSizesDown(List<Integer> sizes) {
25742571
assert !checked.isEmpty();
25752572
List<LayoutNode> nodes = new ArrayList<>(checked);
2576-
nodes.sort((n1, n2) -> Integer.compare(n1.layer, n2.layer));
2573+
nodes.sort(Comparator.comparingInt(n -> n.layer));
25772574
int first = nodes.get(0).layer;
25782575
for (LayoutNode node : nodes) {
25792576
int index = node.layer - first;
@@ -2671,7 +2668,7 @@ protected void run() {
26712668
}
26722669

26732670
// Start DFS and reverse back edges
2674-
visited = new HashSet<>();
2671+
visited = new HashSet<>(nodes.size());
26752672
active = new HashSet<>();
26762673
DFS();
26772674
resolveInsOuts();
@@ -2704,7 +2701,7 @@ private void DFS(LayoutNode startNode, boolean vip) {
27042701
ArrayList<LayoutEdge> succs = new ArrayList<>(node.succs);
27052702
if (sortSuccs) {
27062703
//VIPs are first to follow
2707-
Collections.sort(succs, (e1, e2) -> Integer.compare(e1.vip ? e2.to.preds.size() : 0, e2.vip ? e1.to.preds.size() : 0));
2704+
succs.sort((e1, e2) -> Integer.compare(e1.vip ? e2.to.preds.size() : 0, e2.vip ? e1.to.preds.size() : 0));
27082705
}
27092706
for (LayoutEdge e : succs) {
27102707
if (active.contains(e.to)) {
@@ -2719,17 +2716,29 @@ private void DFS(LayoutNode startNode, boolean vip) {
27192716
}
27202717

27212718
private void DFS() {
2722-
nodes.forEach(n -> DFS(n, false));
27232719
if (isDefaultLayout || !unreverseVips) {
2720+
for (LayoutNode n : nodes) {
2721+
DFS(n, false);
2722+
}
27242723
} else {
27252724
//first search from vip starting nodes
2726-
nodes.stream().filter((n) -> n.preds.stream().allMatch((e) -> !e.vip) && n.succs.stream().anyMatch((e) -> e.vip))
2727-
.forEach(n -> DFS(n, true));
2725+
for (LayoutNode node : nodes) {
2726+
if (node.preds.getVips() == 0 && node.succs.getVips() != 0) {
2727+
DFS(node, true);
2728+
}
2729+
}
27282730
if (visited.size() < nodes.size()) {
27292731
//second look from leftover nodes
2730-
nodes.stream().filter((n) -> !visited.contains(n))
2731-
.sorted((n1, n2) -> Integer.compare(n1.preds.size(), n2.preds.size()))
2732-
.forEach(n -> DFS(n, false));
2732+
List<LayoutNode> toSort = new ArrayList<>();
2733+
for (LayoutNode n : nodes) {
2734+
if (!visited.contains(n)) {
2735+
toSort.add(n);
2736+
}
2737+
}
2738+
toSort.sort(Comparator.comparingInt(n -> n.preds.size()));
2739+
for (LayoutNode n : toSort) {
2740+
DFS(n, false);
2741+
}
27332742
}
27342743
}
27352744
}
@@ -3305,7 +3314,7 @@ private void resolveDummyNodes(LayoutNode n, List<LayoutEdge> succs) {
33053314
if (list.size() == 1) {
33063315
resolveDummyNodes(list.get(0));
33073316
} else {
3308-
Collections.sort(list, LAYER_COMPARATOR);
3317+
list.sort(LAYER_COMPARATOR);
33093318
int maxLayer = list.get(list.size() - 1).to.layer;
33103319
int cnt = maxLayer - n.layer - 1;
33113320
LayoutEdge[] edges = new LayoutEdge[cnt];
@@ -3367,7 +3376,7 @@ private void reassignInOutBlockNodes() {
33673376
lay.add(l);
33683377
}
33693378
}
3370-
layers = lay.toArray(new LayoutLayer[lay.size()]);
3379+
layers = lay.toArray(new LayoutLayer[0]);
33713380
layerCount = layers.length;
33723381
}
33733382

@@ -3442,7 +3451,13 @@ private List<Integer> resolveEdgePoints(List<LayoutEdge> edges) {
34423451
}
34433452

34443453
private List<Integer> resolveUnconnectedEdges(List<Integer> positions, List<LayoutEdge> edges) {
3445-
edges = edges.stream().filter(e -> e.link.getControlPoints().contains(null) || e.link.getControlPoints().isEmpty()).collect(Collectors.toList());
3454+
List<LayoutEdge> list = new ArrayList<>();
3455+
for (LayoutEdge edge : edges) {
3456+
if (edge.link.getControlPoints().contains(null) || edge.link.getControlPoints().isEmpty()) {
3457+
list.add(edge);
3458+
}
3459+
}
3460+
edges = list;
34463461
if (edges.isEmpty()) {
34473462
return positions;
34483463
}
@@ -3523,7 +3538,7 @@ private void recreateLayers() {
35233538
int nodeHeight = n.getWholeHeight();
35243539
if (n.vertex instanceof ClusterNode) {
35253540
//we need to calculate with old height of clusternode, because they could grow a lot
3526-
nodeHeight = ((ClusterNode) n.vertex).getCluster().getBounds().height;
3541+
nodeHeight = n.vertex.getCluster().getBounds().height;
35273542
}
35283543
boolean assigned = false;
35293544
int avgPos = n.y + (nodeHeight / 2);
@@ -3548,8 +3563,8 @@ private void recreateLayers() {
35483563
for (LayoutLayer l : layers) {
35493564
l.refresh();
35503565
}
3551-
layers.sort((l1, l2) -> Integer.compare(l1.y, l2.y));
3552-
HierarchicalLayoutManager.this.layers = layers.toArray(new LayoutLayer[layers.size()]);
3566+
layers.sort(Comparator.comparingInt(l -> l.y));
3567+
HierarchicalLayoutManager.this.layers = layers.toArray(new LayoutLayer[0]);
35533568
layerCount = layers.size();
35543569
}
35553570

0 commit comments

Comments
 (0)