Skip to content

Commit

Permalink
Improve layouting for unbalanced call graphs
Browse files Browse the repository at this point in the history
  • Loading branch information
FeldrinH committed Sep 22, 2023
1 parent 9fe090d commit 80e3304
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 13 deletions.
47 changes: 37 additions & 10 deletions src/main/java/ee/ut/dendroloj/SimpleTreeLayout.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public static void addStepAndUpdateGraph() {
List<Element> newElements = new ArrayList<>();
double x = 0.0;
for (CallTreeNode node : root.getChildren()) {
x += updateGraph(node, !isLatestStepActive, newElements, x, 0.0, 1.0, null) + 1.0;
x += updateGraph(node, !isLatestStepActive, newElements, x, 0.0, 1.0, null).width + 1.0;
}
steps.add(newElements);

Expand Down Expand Up @@ -83,8 +83,8 @@ private static void setActiveStep(int newActiveStep) {
activeStep = newActiveStep;
}

private static double updateGraph(CallTreeNode current, boolean hideNewElements, List<Element> newElements,
double x, double y, double minWidth, CallTreeNode parent) {
private static LayoutResult updateGraph(CallTreeNode current, boolean hideNewElements, List<Element> newElements,
double x, double y, double minWidth, CallTreeNode parent) {
// Currently mutable arguments and return values show the value they had when they were first added to the graph.
// TODO: Show old values of mutable values when scrolling through history?

Expand Down Expand Up @@ -133,16 +133,43 @@ private static double updateGraph(CallTreeNode current, boolean hideNewElements,
}
}

double childrenMinWidth = Math.max(0.4, Math.min(1.0, 1.0 - (current.getChildren().size() - 2) * 0.2));
double width = 0.0;
for (CallTreeNode child : current.getChildren()) {
width += updateGraph(child, hideNewElements, newElements, x + width, y - 2.0, childrenMinWidth, current);;
double width = 0.0, firstChildOffset = 0.0, lastChildOffset = 0.0;
final List<CallTreeNode> children = current.getChildren();
if (children.isEmpty()) {
width = minWidth;
} else {
final boolean isShallow = current.getChildren().stream().allMatch(child -> child.getChildren().isEmpty());
final double childrenMinWidth = isShallow ? Math.max(0.4, Math.min(1.0, 1.0 - (current.getChildren().size() - 2) * 0.2)) : 1.0;

final int leftReferenceNode = (children.size() - 1) / 2;
final int rightReferenceNode = children.size() / 2;

for (int i = 0; i < children.size(); i++) {
LayoutResult result = updateGraph(children.get(i), hideNewElements, newElements, x + width, y - 2.0, childrenMinWidth, current);
if (i == leftReferenceNode) {
firstChildOffset = width + result.offset;
}
if (i == rightReferenceNode) {
lastChildOffset = width + result.offset;
}
width += result.width;
}
}
if (width < minWidth) width = minWidth;

node.setAttribute("xy", x + 0.5 * width, y);
double offset = 0.5 * (firstChildOffset + lastChildOffset);
node.setAttribute("xy", x + offset, y);

return new LayoutResult(width, offset);
}

return width;
private static class LayoutResult {
public final double width;
public final double offset;

public LayoutResult(double width, double offset) {
this.width = width;
this.offset = offset;
}
}
}

Expand Down
13 changes: 10 additions & 3 deletions src/test/java/Katsed.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ static int fib(int n) {
return fib(n - 2) + fib(n - 1);
}

@Grow
static int fib3(int n) {
if (n < 2) return n;
return fib3(n - 3) + fib3(n - 2) + fib3(n - 1);
}

@Grow
static void tree(int w, int h) {
if (h <= 1) {
Expand Down Expand Up @@ -61,11 +67,12 @@ public static void main(String[] args) {

Dendrologist.wakeUp();

unbalancedTree(5, 4);
tree(5, 4);
unbalancedTree(6, 3);
tree(5, 3);
// fib3(6);

// fib(16);
// fib(5);
// fib(8);
// pööraJupid(new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11});
}
}

0 comments on commit 80e3304

Please sign in to comment.