diff options
6 files changed, 44 insertions, 25 deletions
diff --git a/BJC-Utils2/src/main/java/bjc/utils/data/GenHolder.java b/BJC-Utils2/src/main/java/bjc/utils/data/GenHolder.java index f5d553c..390982a 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/data/GenHolder.java +++ b/BJC-Utils2/src/main/java/bjc/utils/data/GenHolder.java @@ -16,7 +16,7 @@ public class GenHolder<T> { /** * The state this holder is responsible for. */ - public T held; + private T held; /** * Creates a new empty holder, with its state set to null diff --git a/BJC-Utils2/src/main/java/bjc/utils/funcdata/FunctionalList.java b/BJC-Utils2/src/main/java/bjc/utils/funcdata/FunctionalList.java index 9eb8e17..d33ec28 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/funcdata/FunctionalList.java +++ b/BJC-Utils2/src/main/java/bjc/utils/funcdata/FunctionalList.java @@ -352,11 +352,9 @@ public class FunctionalList<E> implements Cloneable { Function<T, F> finl) { GenHolder<T> acum = new GenHolder<>(val); - wrap.forEach(e -> { - acum.held = bf.apply(e, acum.held); - }); + wrap.forEach(e -> acum.transform((vl) -> bf.apply(e, vl))); - return finl.apply(acum.held); + return acum.unwrap(finl); } /** @@ -400,6 +398,7 @@ public class FunctionalList<E> implements Cloneable { /** * Convert the list into a iterable + * * @return An iterable view onto the list */ public Iterable<E> toIterable() { @@ -422,4 +421,10 @@ public class FunctionalList<E> implements Cloneable { return sb.toString(); } + + public void removeMatching(E obj) { + removeIf((ele) -> { + return ele.equals(obj); + }); + } } diff --git a/BJC-Utils2/src/main/java/bjc/utils/funcutils/StatementUtils.java b/BJC-Utils2/src/main/java/bjc/utils/funcutils/StatementUtils.java new file mode 100644 index 0000000..57895f9 --- /dev/null +++ b/BJC-Utils2/src/main/java/bjc/utils/funcutils/StatementUtils.java @@ -0,0 +1,10 @@ +package bjc.utils.funcutils; + +/** + * Contains methods that produce effects mimicking statements + * @author ben + * + */ +public class StatementUtils { + +} diff --git a/BJC-Utils2/src/main/java/bjc/utils/gen/WeightedRandom.java b/BJC-Utils2/src/main/java/bjc/utils/gen/WeightedRandom.java index 4b0c276..649a240 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/gen/WeightedRandom.java +++ b/BJC-Utils2/src/main/java/bjc/utils/gen/WeightedRandom.java @@ -72,17 +72,18 @@ public class WeightedRandom<E> { GenHolder<Boolean> bl = new GenHolder<>(true); probs.forEachIndexed((i, p) -> { - if (bl.held) { - if (v.held < p) { - res.held = results.getByIndex(i); - bl.held = false; + if (bl.unwrap(vl -> vl)) { + if (v.unwrap((vl) -> vl < p)) { + res.transform((vl) -> results.getByIndex(i)); + + bl.transform((vl) -> false); } else { - v.held -= p; + v.transform((vl) -> vl - p); } } }); - return res.held; + return res.unwrap((vl) -> vl); } /** 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; |
