Skip to content

Commit 5bc2a52

Browse files
committed
reverse the array instead of sorting again in reverse order
1 parent 2eab8b4 commit 5bc2a52

File tree

1 file changed

+31
-4
lines changed

1 file changed

+31
-4
lines changed

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

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1143,17 +1143,44 @@ private void createArrays() {
11431143
}
11441144

11451145
if (isDefaultLayout || !setting.get(Boolean.class, BOTH_SORT)) {
1146-
downProcessingOrder[i] = layer.toArray(new LayoutNode[layer.size()]);
1147-
upProcessingOrder[i] = layer.toArray(new LayoutNode[layer.size()]);
1146+
downProcessingOrder[i] = layer.toArray(new LayoutNode[0]);
11481147
Arrays.sort(downProcessingOrder[i], downComparer);
1149-
Arrays.sort(upProcessingOrder[i], upComparer);
1148+
upProcessingOrder[i] = reverseSort(downProcessingOrder[i], upComparer);
11501149
} else {
1151-
bothProcessingOrder[i] = layer.toArray(new LayoutNode[layer.size()]);
1150+
bothProcessingOrder[i] = layer.toArray(new LayoutNode[0]);
11521151
Arrays.sort(bothProcessingOrder[i], bothComparer);
11531152
}
11541153
}
11551154
}
11561155

1156+
/**
1157+
* Instead of sorting again in reverse order, just reverse the array. Assert that this produces the same order as sorting .
1158+
*/
1159+
private static LayoutNode[] reverseSort(LayoutNode[] source, Comparator<LayoutNode> upComparer) {
1160+
LayoutNode[] array = source.clone();
1161+
int left = 0, right = array.length - 1;
1162+
while (left < right) {
1163+
LayoutNode temp = array[left];
1164+
array[left] = array[right];
1165+
array[right] = temp;
1166+
left++;
1167+
right--;
1168+
}
1169+
assert verifySort(array, upComparer);
1170+
return array;
1171+
}
1172+
1173+
/**
1174+
* Ensure that the reversed array is sorted in the same order.
1175+
*/
1176+
private static boolean verifySort(LayoutNode[] array, Comparator<LayoutNode> upComparer) {
1177+
LayoutNode[] copy = array.clone();
1178+
Arrays.sort(copy, upComparer);
1179+
assert Arrays.equals(array, copy);
1180+
return true;
1181+
}
1182+
1183+
11571184
@Override
11581185
protected void run() {
11591186
createArrays();

0 commit comments

Comments
 (0)