Skip to content

Commit

Permalink
Allow values of any type as labels
Browse files Browse the repository at this point in the history
  • Loading branch information
FeldrinH committed Aug 29, 2024
1 parent 75e249a commit e8e3f5e
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 33 deletions.
4 changes: 2 additions & 2 deletions src/main/java/ee/ut/dendroloj/Dendrologist.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public static void paint(Color color) {
* @param right function that takes in a node and returns the right child of that node (or null if absent)
* @param <T> type of nodes in the tree
*/
public static <T> void drawBinaryTree(T root, Function<T, String> label, Function<T, T> left, Function<T, T> right) {
public static <T> void drawBinaryTree(T root, Function<T, Object> label, Function<T, T> left, Function<T, T> right) {
drawTree(root, label, n -> Arrays.asList(left.apply(n), right.apply(n)));
}

Expand All @@ -112,7 +112,7 @@ public static <T> void drawBinaryTree(T root, Function<T, String> label, Functio
* (children may include null values to indicate empty/missing branches)
* @param <T> type of nodes in the tree
*/
public static <T> void drawTree(T root, Function<T, String> label, Function<T, List<T>> children) {
public static <T> void drawTree(T root, Function<T, Object> label, Function<T, List<T>> children) {
if (isHeadless()) {
return;
}
Expand Down
7 changes: 4 additions & 3 deletions src/main/java/ee/ut/dendroloj/GenericTreeLayout.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@

class GenericTreeLayout {

public static <T> Graph assembleGraph(T root, Function<T, String> getLabel, Function<T, List<T>> getChildren) {
public static <T> Graph assembleGraph(T root, Function<T, Object> getLabel, Function<T, List<T>> getChildren) {
Graph graph = new MultiGraph("dendroloj");
addToGraph(graph, root, null, 0.0, 0.0, getLabel, getChildren);
return graph;
}

private static <T> LayoutResult addToGraph(Graph graph, T node, Node parent, double x, double y,
Function<T, String> getLabel, Function<T, List<T>> getChildren) {
Function<T, Object> getLabel, Function<T, List<T>> getChildren) {
if (node == null) {
return new LayoutResult(1.0, 0.0);
}
Expand All @@ -27,7 +27,8 @@ private static <T> LayoutResult addToGraph(Graph graph, T node, Node parent, dou
if (current == null) {
visited = false;
current = graph.addNode(nodeId);
current.setAttribute("label", getLabel.apply(node));
Object label = getLabel.apply(node);
current.setAttribute("label", label == null ? null : label.toString());
} else {
visited = true;
}
Expand Down
50 changes: 25 additions & 25 deletions src/main/java/ee/ut/dendroloj/GraphCanvas.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

/**
* A helper class for drawing a graph.
* Can be passed to {@link Dendrologist#drawGraph(GraphCanvas)} to render the drawn graph on screen.
* Can be passed to {@link Dendrologist#drawGraph} to render the drawn graph on screen.
*
* @param <V> type of graph vertices
*/
Expand All @@ -29,17 +29,17 @@ public final class GraphCanvas<V> {
/**
* Draws a vertex with the given label.
*/
public void drawVertex(V vertex, String label) {
public void drawVertex(V vertex, Object label) {
if (vertex == null) throw new NullPointerException("Vertex must not be null");
addVertex(new Vertex<>(vertex, label, null));
addVertex(new Vertex<>(vertex, label == null ? null : label.toString(), null));
}

/**
* Draws a vertex with the given label.
*/
public void drawVertex(V vertex, String label, Color color) {
public void drawVertex(V vertex, Object label, Color color) {
if (vertex == null) throw new NullPointerException("Vertex must not be null");
addVertex(new Vertex<>(vertex, label, color));
addVertex(new Vertex<>(vertex, label == null ? null : label.toString(), color));
}

// public void drawFixedVertex(V vertex, double x, double y) {
Expand All @@ -50,32 +50,32 @@ public void drawVertex(V vertex, String label, Color color) {
/**
* Draws a vertex with the given label at the given coordinates.
*/
public void drawFixedVertex(V vertex, double x, double y, String label) {
public void drawFixedVertex(V vertex, double x, double y, Object label) {
if (vertex == null) throw new NullPointerException("Vertex must not be null");
addVertex(new Vertex<>(vertex, label, null, x, y));
addVertex(new Vertex<>(vertex, label == null ? null : label.toString(), null, x, y));
}

/**
* Draws a vertex with the given label at the given coordinates.
*/
public void drawFixedVertex(V vertex, double x, double y, String label, Color color) {
public void drawFixedVertex(V vertex, double x, double y, Object label, Color color) {
if (vertex == null) throw new NullPointerException("Vertex must not be null");
addVertex(new Vertex<>(vertex, label, color, x, y));
addVertex(new Vertex<>(vertex, label == null ? null : label.toString(), color, x, y));
}

/**
* @deprecated use {@link #drawFixedVertex(V, double, double, String)} instead
* @deprecated use {@link #drawFixedVertex(V, double, double, Object)} instead
*/
@Deprecated
public void drawFixedVertex(V vertex, String label, double x, double y) {
public void drawFixedVertex(V vertex, Object label, double x, double y) {
drawFixedVertex(vertex, x, y, label);
}

/**
* @deprecated use {@link #drawFixedVertex(V, double, double, String, Color)} instead
* @deprecated use {@link #drawFixedVertex(V, double, double, Object, Color)} instead
*/
@Deprecated
public void drawFixedVertex(V vertex, String label, double x, double y, Color color) {
public void drawFixedVertex(V vertex, Object label, double x, double y, Color color) {
drawFixedVertex(vertex, x, y, label, color);
}

Expand All @@ -98,16 +98,16 @@ public void drawEdge(V v1, V v2) {
/**
* Draws an undirected edge from vertex v1 to vertex v2.
*/
public void drawEdge(V v1, V v2, String label) {
public void drawEdge(V v1, V v2, Object label) {
drawEdge(v1, v2, label, null);
}

/**
* Draws an undirected edge from vertex v1 to vertex v2.
*/
public void drawEdge(V v1, V v2, String label, Color color) {
public void drawEdge(V v1, V v2, Object label, Color color) {
if (v1 == null || v2 == null) throw new NullPointerException("Target vertices must not be null");
edges.add(new Edge<>(false, v1, v2, label, color));
edges.add(new Edge<>(false, v1, v2, label == null ? null : label.toString(), color));
}

/**
Expand All @@ -120,26 +120,26 @@ public void drawDirectedEdge(V v1, V v2) {
/**
* Draws a directed edge from vertex v1 to vertex v2.
*/
public void drawDirectedEdge(V v1, V v2, String label) {
public void drawDirectedEdge(V v1, V v2, Object label) {
drawDirectedEdge(v1, v2, label, null);
}

/**
* Draws a directed edge from vertex v1 to vertex v2.
*/
public void drawDirectedEdge(V v1, V v2, String label, Color color) {
public void drawDirectedEdge(V v1, V v2, Object label, Color color) {
if (v1 == null || v2 == null) throw new NullPointerException("Target vertices must not be null");
String v1id = IdHelper.getNodeId(v1);
String v2id = IdHelper.getNodeId(v2);
Edge<V> edge = new Edge<>(true, v1, v2, label, color);
Edge<V> collapsibleEdge = collapsibleEdges.get(new IdPair(v2id, v1id));
edges.add(edge);
collapsibleEdges.putIfAbsent(new IdPair(v1id, v2id), edge);
if (collapsibleEdge != null && !collapsibleEdge.collapsed && Objects.equals(collapsibleEdge.label, label)) {
String v1Id = IdHelper.getNodeId(v1);
String v2Id = IdHelper.getNodeId(v2);
Edge<V> edge = new Edge<>(true, v1, v2, label == null ? null : label.toString(), color);
Edge<V> collapsibleEdge = collapsibleEdges.get(new IdPair(v2Id, v1Id));
if (collapsibleEdge != null && !collapsibleEdge.collapsed && Objects.equals(collapsibleEdge.label, edge.label)) {
edge.collapsed = true;
edge.arrowOnly = true;
collapsibleEdge.collapsed = true;
}
edges.add(edge);
collapsibleEdges.putIfAbsent(new IdPair(v1Id, v2Id), edge);
}

private static class IdPair {
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/GraafKatsed.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public static void main(String[] args) {
}
for (int i = 0; i < M.length; i++) {
for (int j = 0; j < M.length; j++) {
if (i != j && M[i][j] != -1) lõuend.drawDirectedEdge(i, j, String.valueOf(M[i][j]));
if (i != j && M[i][j] != -1) lõuend.drawDirectedEdge(i, j, M[i][j]);
}
}
Dendrologist.drawGraph(lõuend);
Expand All @@ -53,7 +53,7 @@ public static void main(String[] args) {
for (Tipp tipp : tipud) {
lõuend.drawFixedVertex(tipp, tipp.x, tipp.y, tipp.tähis);
for (Kaar kaar : tipp.kaared) {
lõuend.drawEdge(tipp, kaar.lõppTipp, String.valueOf(kaar.kaal));
lõuend.drawEdge(tipp, kaar.lõppTipp, kaar.kaal);
}
}
Dendrologist.drawGraph(lõuend);
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/KuhjaKatsed.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public static void main(String[] args) {
List<Integer> kuhi = List.of(10, 6, 7, 3, 5, 2, 3, 9, 4, 12, 5, -7, 5, 3, 6, 6, 5, 16);
Dendrologist.drawBinaryTree(
0,
i -> String.valueOf(kuhi.get(i)),
kuhi::get,
i -> vasakAlluv(kuhi, i) == -1 ? null : vasakAlluv(kuhi, i),
i -> paremAlluv(kuhi, i) == -1 ? null : paremAlluv(kuhi, i)
);
Expand Down

0 comments on commit e8e3f5e

Please sign in to comment.