summaryrefslogtreecommitdiff
path: root/BJC-Utils2/src/main/java/bjc/utils/funcutils
diff options
context:
space:
mode:
Diffstat (limited to 'BJC-Utils2/src/main/java/bjc/utils/funcutils')
-rw-r--r--BJC-Utils2/src/main/java/bjc/utils/funcutils/FunctionalFileVisitor.java3
-rw-r--r--BJC-Utils2/src/main/java/bjc/utils/funcutils/ListUtils.java45
-rw-r--r--BJC-Utils2/src/main/java/bjc/utils/funcutils/TreeUtils.java56
3 files changed, 89 insertions, 15 deletions
diff --git a/BJC-Utils2/src/main/java/bjc/utils/funcutils/FunctionalFileVisitor.java b/BJC-Utils2/src/main/java/bjc/utils/funcutils/FunctionalFileVisitor.java
index 4310416..db6c43b 100644
--- a/BJC-Utils2/src/main/java/bjc/utils/funcutils/FunctionalFileVisitor.java
+++ b/BJC-Utils2/src/main/java/bjc/utils/funcutils/FunctionalFileVisitor.java
@@ -7,6 +7,9 @@ import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.function.BiPredicate;
+/*
+ * Functional implementation of a file visitor.
+ */
final class FunctionalFileVisitor extends SimpleFileVisitor<Path> {
private final BiPredicate<Path, BasicFileAttributes> predicate;
private final BiPredicate<Path, BasicFileAttributes> action;
diff --git a/BJC-Utils2/src/main/java/bjc/utils/funcutils/ListUtils.java b/BJC-Utils2/src/main/java/bjc/utils/funcutils/ListUtils.java
index 52a2437..c0daa1e 100644
--- a/BJC-Utils2/src/main/java/bjc/utils/funcutils/ListUtils.java
+++ b/BJC-Utils2/src/main/java/bjc/utils/funcutils/ListUtils.java
@@ -95,13 +95,19 @@ public class ListUtils {
E element = null;
for (final int index = 0; itr.hasNext(); element = itr.next()) {
- // n - m
+ /*
+ * n - m
+ */
final int winningChance = number - selected.getSize();
- // N - t
+ /*
+ * N - t
+ */
final int totalChance = total - (index - 1);
- // Probability of selecting the t+1'th element
+ /*
+ * Probability of selecting the t+1'th element
+ */
if (NumberUtils.isProbable(winningChance, totalChance, rng)) {
selected.add(element);
}
@@ -185,17 +191,20 @@ public class ListUtils {
&& !rejected.isEmpty(); numberOfIterations++) {
input.forEach(it);
- if (rejected.isEmpty()) // Nothing was rejected, so
- // we're
- // done
+ if (rejected.isEmpty()) {
+ /*
+ * Nothing was rejected, so we're done
+ */
return returned;
+ }
}
- throw new IllegalArgumentException("Heuristic (more than " + MAX_NTRIESPART
- + " iterations of partitioning) detected unpartitionable list " + input.toString()
- + "\nThe following elements were not partitioned: " + rejected.toString()
- + "\nCurrent group in formation: " + it.currentPartition.toString()
- + "\nPreviously formed groups: " + returned.toString());
+
+ final String fmt = "Heuristic (more than %d iterations of partitioning) detected an unpartitionable list. (%s)\nThe following elements were not partitioned: %s\nCurrent group in formation: %s\nPreviously formed groups: %s\n";
+
+ final String msg = String.format(fmt, MAX_NTRIESPART, input.toString(), rejected.toString(), it.currentPartition.toString(), returned.toString());
+
+ throw new IllegalArgumentException(msg);
}
/**
@@ -250,7 +259,9 @@ public class ListUtils {
}
if (count % size != 0) {
- // We need to pad
+ /*
+ * We need to pad
+ */
int needed = count % size;
int threshold = 0;
@@ -269,9 +280,13 @@ public class ListUtils {
}
}
- if (threshold > MAX_NTRIESPART)
- throw new IllegalArgumentException("Heuristic (more than " + MAX_NTRIESPART
- + " iterations of attempting to pad) detected unpaddable list ");
+ if (threshold > MAX_NTRIESPART) {
+ final String fmt = "Heuristic (more than %d iterations of attempting to pad) detected an unpaddable list. (%s)\nPartially padded list: %S";
+
+ final String msg = String.format(fmt, MAX_NTRIESPART, list.toString(), returned.toString());
+
+ throw new IllegalArgumentException(msg);
+ }
}
return returned;
diff --git a/BJC-Utils2/src/main/java/bjc/utils/funcutils/TreeUtils.java b/BJC-Utils2/src/main/java/bjc/utils/funcutils/TreeUtils.java
new file mode 100644
index 0000000..dcd5738
--- /dev/null
+++ b/BJC-Utils2/src/main/java/bjc/utils/funcutils/TreeUtils.java
@@ -0,0 +1,56 @@
+package bjc.utils.funcutils;
+
+import java.util.LinkedList;
+import java.util.function.Predicate;
+
+import bjc.utils.data.ITree;
+import bjc.utils.funcdata.FunctionalList;
+import bjc.utils.funcdata.IList;
+
+/**
+ * Implements various utilities for trees.
+ *
+ * @author Benjamin Culkin
+ */
+public class TreeUtils {
+ /*
+ * Convert a tree into a list of outline nodes that match a certain
+ * path.
+ */
+ public static <T> IList<IList<T>> outlineTree(ITree<T> tre, Predicate<T> leafMarker) {
+ IList<IList<T>> paths = new FunctionalList<>();
+
+ LinkedList<T> path = new LinkedList<>();
+ path.add(tre.getHead());
+
+ tre.doForChildren((child) -> findPath(child, path, leafMarker, paths));
+
+ return paths;
+ }
+
+ private static <T> void findPath(ITree<T> subtree, LinkedList<T> path, Predicate<T> leafMarker, IList<IList<T>> paths) {
+ if(subtree.getChildrenCount() == 0 && leafMarker.test(subtree.getHead())) {
+ /*
+ * We're at a matching leaf node. Add it.
+ */
+ IList<T> finalPath = new FunctionalList<>();
+
+ for(T ePath : path) {
+ finalPath.add(ePath);
+ }
+
+ finalPath.add(subtree.getHead());
+
+ paths.add(finalPath);
+ } else {
+ /*
+ * Check the children of this node.
+ */
+ path.add(subtree.getHead());
+
+ subtree.doForChildren((child) -> findPath(child, path, leafMarker, paths));
+
+ path.removeLast();
+ }
+ }
+}