From eb30c23bb03579bf839189ab0d2ad172d5b07766 Mon Sep 17 00:00:00 2001 From: bculkin2442 Date: Sun, 21 Feb 2016 15:41:54 -0500 Subject: Commenting of various things and minor refactoring --- .../main/java/bjc/utils/graph/AdjacencyMap.java | 118 +++++++++++++++------ BJC-Utils2/src/main/java/bjc/utils/graph/Edge.java | 51 ++++++++- .../src/main/java/bjc/utils/graph/GenHolder.java | 75 ------------- .../src/main/java/bjc/utils/graph/Graph.java | 17 +++ 4 files changed, 148 insertions(+), 113 deletions(-) delete mode 100644 BJC-Utils2/src/main/java/bjc/utils/graph/GenHolder.java (limited to 'BJC-Utils2/src/main/java/bjc/utils/graph') diff --git a/BJC-Utils2/src/main/java/bjc/utils/graph/AdjacencyMap.java b/BJC-Utils2/src/main/java/bjc/utils/graph/AdjacencyMap.java index 7a79233..3b6d6ef 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/graph/AdjacencyMap.java +++ b/BJC-Utils2/src/main/java/bjc/utils/graph/AdjacencyMap.java @@ -10,9 +10,68 @@ import java.util.Scanner; import java.util.Set; import java.util.stream.IntStream; +import bjc.utils.data.GenHolder; + +/** + * An adjacency map representing a graph + * + * @author ben + * + * @param + * The type of the nodes in the graph + */ public class AdjacencyMap { + /** + * Create an adjacency map from a stream of text + * + * @param stream + * The stream of text to read in + * @return An adjacency map defined by the text + */ + public static AdjacencyMap fromStream(InputStream stream) { + Scanner scn = new Scanner(stream); + scn.useDelimiter("\n"); + + // First, read in number of vertices + int numVertices = Integer.parseInt(scn.next()); + + Set vertices = new HashSet<>(); + IntStream.range(0, numVertices).forEach(e -> vertices.add(e)); + + // Create the adjacency map + AdjacencyMap aMap = new AdjacencyMap<>(vertices); + + GenHolder row = new GenHolder<>(0); + + scn.forEachRemaining((strang) -> { + String[] parts = strang.split(" "); + int col = 0; + + for (String part : parts) { + aMap.setWeight(row.held, col, Integer.parseInt(part)); + + col++; + } + + row.held++; + }); + + scn.close(); + + return aMap; + } + + /** + * The backing storage of the map + */ private Map> adjMap; + /** + * Create a new map from a set of vertices + * + * @param vertices + * The set of vertices to create a map from + */ public AdjacencyMap(Set vertices) { vertices.forEach(src -> { Map srcRow = new HashMap<>(); @@ -23,6 +82,11 @@ public class AdjacencyMap { }); } + /** + * Check if the graph is directed + * + * @return Whether or not the graph is directed + */ public boolean isDirected() { GenHolder res = new GenHolder<>(true); @@ -39,10 +103,25 @@ public class AdjacencyMap { return res.held; } + /** + * Set the weight of an edge + * + * @param src + * The source node of the edge + * @param tgt + * The target node of the edge + * @param weight + * The weight of the edge + */ public void setWeight(T src, T tgt, int weight) { adjMap.get(src).put(tgt, weight); } + /** + * Convert this to a different graph representation + * + * @return The new representation of this graph + */ public Graph toGraph() { Graph ret = new Graph<>(); @@ -54,39 +133,12 @@ public class AdjacencyMap { return ret; } - public static AdjacencyMap fromStream(InputStream stream) { - Scanner scn = new Scanner(stream); - scn.useDelimiter("\n"); - - // First, read in number of vertices - int numVertices = Integer.parseInt(scn.next()); - - Set vertices = new HashSet<>(); - IntStream.range(0, numVertices).forEach(e -> vertices.add(e)); - - // Create the adjacency map - AdjacencyMap aMap = new AdjacencyMap<>(vertices); - - GenHolder row = new GenHolder<>(0); - - scn.forEachRemaining((strang) -> { - String[] parts = strang.split(" "); - int col = 0; - - for (String part : parts) { - aMap.setWeight(row.held, col, Integer.parseInt(part)); - - col++; - } - - row.held++; - }); - - scn.close(); - - return aMap; - } - + /** + * Convert an adjacency map back into a stream + * + * @param os + * The stream to convert to + */ public void toStream(OutputStream os) { PrintStream ps = new PrintStream(os); diff --git a/BJC-Utils2/src/main/java/bjc/utils/graph/Edge.java b/BJC-Utils2/src/main/java/bjc/utils/graph/Edge.java index 9fb20c0..cf0764b 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/graph/Edge.java +++ b/BJC-Utils2/src/main/java/bjc/utils/graph/Edge.java @@ -1,27 +1,68 @@ package bjc.utils.graph; +/** + * An edge in a weighted graph + * + * @author ben + * + * @param + * The type of the nodes in the graph + */ public class Edge { - private final T source, target; + /** + * The distance from initial to terminal node + */ private final int distance; + /** + * The initial and terminal nodes of this edge + */ + private final T source, target; + + /** + * Create a new edge with set parameters + * + * @param node1 + * The initial node of the edge + * @param node2 + * The terminal node of the edge + * @param distance + * The distance between initial and terminal edge + */ public Edge(T node1, T node2, int distance) { this.source = node1; this.target = node2; this.distance = distance; } + /** + * Get the distance in this edge + * + * @return The distance between the initial and terminal nodes of this + * edge + */ + public int getDistance() { + return distance; + } + + /** + * Get the initial node of an edge + * + * @return The initial node of this edge + */ public T getSource() { return source; } + /** + * Get the target node of an edge + * + * @return The target node of this edge + */ public T getTarget() { return target; } - public int getDistance() { - return distance; - } - @Override public String toString() { return " first vertex " + source + " to vertex " + target diff --git a/BJC-Utils2/src/main/java/bjc/utils/graph/GenHolder.java b/BJC-Utils2/src/main/java/bjc/utils/graph/GenHolder.java deleted file mode 100644 index df363fa..0000000 --- a/BJC-Utils2/src/main/java/bjc/utils/graph/GenHolder.java +++ /dev/null @@ -1,75 +0,0 @@ -package bjc.utils.graph; - -import java.util.function.Function; - -/** - * Holds a single value of a specific type. This is used for indirect - * references to data, and more specifically for accessing non-final - * variables from a lambda. AKA the identity monad - * - * @author ben - * - * @param - * The type of the data being held - */ -public class GenHolder { - /** - * The state this holder is responsible for. - */ - public T held; - - /** - * Creates a new empty holder, with its state set to null - */ - public GenHolder() { - held = null; - } - - /** - * Creates a new holder, with its state initialized to the provided - * value - * - * @param held - * The state to initialize this holder to. - */ - public GenHolder(T hld) { - held = hld; - } - - /** - * Apply the given transformation to the held value. Returns the holder - * for allowing chaining of transforms - * - * @param f - * The transform to apply to the value - * @return The holder - */ - public GenHolder transform(Function f) { - held = f.apply(held); - - return this; - } - - /** - * Return the result of applying the given transformation to the held - * value Doesn't change the held value - * - * @param f - * The transformation to apply - * @return A holder with the transformed value - */ - public GenHolder map(Function f) { - return new GenHolder(f.apply(held)); - } - - /** - * Returns a raw mapped value, not contained in a GenHolder - * - * @param f - * The function to use for mapping the value - * @return The mapped value outside of a GenHolder - */ - public E unwrap(Function f) { - return f.apply(held); - } -} diff --git a/BJC-Utils2/src/main/java/bjc/utils/graph/Graph.java b/BJC-Utils2/src/main/java/bjc/utils/graph/Graph.java index 69e42e4..03bc0c2 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/graph/Graph.java +++ b/BJC-Utils2/src/main/java/bjc/utils/graph/Graph.java @@ -13,6 +13,8 @@ import java.util.Set; import java.util.function.BiConsumer; import java.util.function.BiPredicate; +import bjc.utils.data.GenHolder; + /** * A directed weighted graph, where the vertices have some arbitrary label * @@ -23,6 +25,9 @@ import java.util.function.BiPredicate; */ public class Graph { + /** + * The backing representation of the graph + */ private final Map> graph; /** @@ -165,6 +170,11 @@ public class Graph { return minEdges; } + /** + * Get the count of the vertices in this graph + * + * @return A count of the vertices in this graph + */ public int getVertexCount() { return graph.size(); } @@ -225,6 +235,13 @@ public class Graph { return aMap; } + /** + * Create a graph from a list of edges + * + * @param edges + * The list of edges to build from + * @return A graph built from the provided edge-list + */ public static Graph fromEdgeList(List> edges) { Graph g = new Graph<>(); -- cgit v1.2.3