diff options
Diffstat (limited to 'BJC-Utils2/src/main/java/bjc/utils/graph')
| -rw-r--r-- | BJC-Utils2/src/main/java/bjc/utils/graph/AdjacencyMap.java | 9 | ||||
| -rw-r--r-- | BJC-Utils2/src/main/java/bjc/utils/graph/Graph.java | 22 |
2 files changed, 17 insertions, 14 deletions
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 3b6d6ef..d3a420e 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/graph/AdjacencyMap.java +++ b/BJC-Utils2/src/main/java/bjc/utils/graph/AdjacencyMap.java @@ -48,12 +48,13 @@ public class AdjacencyMap<T> { int col = 0; for (String part : parts) { - aMap.setWeight(row.held, col, Integer.parseInt(part)); + aMap.setWeight(row.unwrap(vl -> vl), col, + Integer.parseInt(part)); col++; } - row.held++; + row.transform((vl) -> vl + 1); }); scn.close(); @@ -96,11 +97,11 @@ public class AdjacencyMap<T> { int rhs = adjMap.get(tgt.getKey()).get(src.getKey()); if (lhs != rhs) { - res.held = false; + res.transform((vl) -> false); } })); - return res.held; + return res.unwrap(vl -> vl); } /** 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 03bc0c2..ca3d06f 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/graph/Graph.java +++ b/BJC-Utils2/src/main/java/bjc/utils/graph/Graph.java @@ -137,34 +137,36 @@ public class Graph<T> { // Start at the initial vertex and visit it GenHolder<T> src = new GenHolder<>(getInitial()); - visited.add(src.held); + visited.add(src.unwrap(vl -> vl)); // Make sure we visit all the nodes while (visited.size() != getVertexCount()) { // Grab all edges adjacent to the provided edge - forAllEdgesMatchingAt(src.held, + forAllEdgesMatchingAt(src.unwrap(vl -> vl), (tgt, weight) -> !visited.contains(tgt), - (tgt, weight) -> availEdges - .add(new Edge<T>(src.held, tgt, weight))); + (tgt, weight) -> availEdges.add(new Edge<T>( + src.unwrap(vl -> vl), tgt, weight))); // Get the edge with the minimum distance - Edge<T> minEdge = availEdges.poll(); + GenHolder<Edge<T>> minEdge = new GenHolder<>( + availEdges.poll()); // Only consider edges where we haven't visited the target of // the edge - while (visited.contains(minEdge.getTarget())) { - minEdge = availEdges.poll(); + while (visited + .contains(minEdge.unwrap(vl -> vl.getTarget()))) { + minEdge.transform((vl) -> availEdges.poll()); } // Add it to our MST - minEdges.add(minEdge); + minEdges.add(minEdge.unwrap(vl -> vl)); // Advance to the next node - src.held = minEdge.getTarget(); + src.transform((vl) -> minEdge.unwrap(vl1 -> vl1.getTarget())); // Visit this node - visited.add(src.held); + visited.add(src.unwrap(vl -> vl)); } return minEdges; |
