From 77fcc58d1facffbc3af50be8c05985350e9f1355 Mon Sep 17 00:00:00 2001 From: bculkin2442 Date: Sun, 17 Apr 2016 15:01:44 -0400 Subject: Code maintenace and changes --- .../main/java/bjc/utils/graph/AdjacencyMap.java | 74 +++++++++++----------- .../src/main/java/bjc/utils/graph/Graph.java | 36 +++++------ 2 files changed, 56 insertions(+), 54 deletions(-) (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 56b4653..7b240b8 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/graph/AdjacencyMap.java +++ b/BJC-Utils2/src/main/java/bjc/utils/graph/AdjacencyMap.java @@ -7,12 +7,13 @@ import java.util.InputMismatchException; import java.util.Scanner; import java.util.stream.IntStream; -import bjc.utils.data.experimental.IHolder; -import bjc.utils.data.experimental.Identity; +import bjc.utils.data.IHolder; +import bjc.utils.data.Identity; import bjc.utils.funcdata.FunctionalList; import bjc.utils.funcdata.FunctionalMap; import bjc.utils.funcdata.IFunctionalList; import bjc.utils.funcdata.IFunctionalMap; +import bjc.utils.funcutils.FuncUtils; /** * An adjacency map representing a graph @@ -67,54 +68,54 @@ public class AdjacencyMap { IFunctionalList vertices = new FunctionalList<>(); - IntStream.range(0, numVertices) - .forEach(element -> vertices.add(element)); + FuncUtils.doTimes(numVertices, + (vertexNo) -> vertices.add(vertexNo)); adjacencyMap = new AdjacencyMap<>(vertices); IHolder row = new Identity<>(0); inputSource.forEachRemaining((strang) -> { - String[] parts = strang.split(" "); + readRow(adjacencyMap, numVertices, row, strang); + }); + } - if (parts.length != numVertices) { - throw new InputMismatchException( - "Must specify a weight for all " + numVertices - + " vertices"); - } + return adjacencyMap; + } - int column = 0; + private static void readRow(AdjacencyMap adjacencyMap, + int numVertices, IHolder row, String strang) { + String[] parts = strang.split(" "); - for (String part : parts) { - int columnWeight; + if (parts.length != numVertices) { + throw new InputMismatchException( + "Must specify a weight for all " + numVertices + + " vertices"); + } - try { - columnWeight = Integer.parseInt(part); - } catch (NumberFormatException nfex) { - InputMismatchException imex = - new InputMismatchException("" + part - + " is not a valid weight."); + int column = 0; - imex.initCause(nfex); + for (String part : parts) { + int columnWeight; - throw imex; - } + try { + columnWeight = Integer.parseInt(part); + } catch (NumberFormatException nfex) { + InputMismatchException imex = new InputMismatchException( + "" + part + " is not a valid weight."); - adjacencyMap.setWeight(row.unwrap(number -> number), - column, columnWeight); + imex.initCause(nfex); - column++; - } + throw imex; + } - row.transform((number) -> { - int newNumber = number + 1; + adjacencyMap.setWeight(row.getValue(), column, + columnWeight); - return newNumber; - }); - }); + column++; } - return adjacencyMap; + row.transform((rowNumber) -> rowNumber + 1); } /** @@ -137,8 +138,9 @@ public class AdjacencyMap { vertices.forEach(vertex -> { IFunctionalMap vertexRow = new FunctionalMap<>(); - vertices.forEach( - targetVertex -> vertexRow.put(targetVertex, 0)); + vertices.forEach(targetVertex -> { + vertexRow.put(targetVertex, 0); + }); adjacencyMap.put(vertex, vertexRow); }); @@ -157,12 +159,12 @@ public class AdjacencyMap { int inverseValue = adjacencyMap.get(targetKey).get(key); if (targetValue != inverseValue) { - result.transform((bool) -> false); + result.replace(false); } }); }); - return result.unwrap(bool -> bool); + return result.getValue(); } /** 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 a72304d..d08c3f9 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/graph/Graph.java +++ b/BJC-Utils2/src/main/java/bjc/utils/graph/Graph.java @@ -10,8 +10,8 @@ import java.util.Set; import java.util.function.BiConsumer; import java.util.function.BiPredicate; -import bjc.utils.data.experimental.IHolder; -import bjc.utils.data.experimental.Identity; +import bjc.utils.data.IHolder; +import bjc.utils.data.Identity; import bjc.utils.funcdata.FunctionalMap; import bjc.utils.funcdata.IFunctionalList; import bjc.utils.funcdata.IFunctionalMap; @@ -25,7 +25,6 @@ import bjc.utils.funcdata.IFunctionalMap; * The label for vertices */ public class Graph { - /** * The backing representation of the graph */ @@ -157,20 +156,20 @@ public class Graph { // Start at the initial vertex and visit it IHolder sourceVertex = new Identity<>(getInitial()); - visitedVertexes.add(sourceVertex.unwrap(vertex -> vertex)); + visitedVertexes.add(sourceVertex.getValue()); // Make sure we visit all the nodes while (visitedVertexes.size() != getVertexCount()) { // Grab all edges adjacent to the provided edge - forAllEdgesMatchingAt(sourceVertex.unwrap(vertex -> vertex), - (targetVertex, - vertexWeight) -> !visitedVertexes - .contains(targetVertex), - (targetVertex, - vertexWeight) -> availableEdges.add(new Edge<>( - sourceVertex.unwrap(vertex -> vertex), - targetVertex, vertexWeight))); + forAllEdgesMatchingAt(sourceVertex.getValue(), + (targetVertex, vertexWeight) -> { + return !visitedVertexes.contains(targetVertex); + }, (targetVertex, vertexWeight) -> { + availableEdges.add(new Edge<>( + sourceVertex.unwrap(vertex -> vertex), + targetVertex, vertexWeight)); + }); // Get the edge with the minimum distance IHolder> minimumEdge = @@ -178,20 +177,19 @@ public class Graph { // Only consider edges where we haven't visited the target of // the edge - while (visitedVertexes.contains( - minimumEdge.unwrap(vertex -> vertex.getTarget()))) { + while (visitedVertexes.contains(minimumEdge.getValue())) { minimumEdge.transform((edge) -> availableEdges.poll()); } // Add it to our MST - minimumEdges.add(minimumEdge.unwrap(edge -> edge)); + minimumEdges.add(minimumEdge.getValue()); // Advance to the next node sourceVertex.transform((vertex) -> minimumEdge .unwrap(edge -> edge.getTarget())); // Visit this node - visitedVertexes.add(sourceVertex.unwrap(vertex -> vertex)); + visitedVertexes.add(sourceVertex.getValue()); } return minimumEdges; @@ -281,8 +279,10 @@ public class Graph { public static Graph fromEdgeList(List> edges) { Graph g = new Graph<>(); - edges.forEach(edge -> g.addEdge(edge.getSource(), edge.getTarget(), - edge.getDistance(), true)); + edges.forEach(edge -> { + g.addEdge(edge.getSource(), edge.getTarget(), + edge.getDistance(), true); + }); return g; } -- cgit v1.2.3