diff options
Diffstat (limited to 'BJC-Utils2/src/main/java/bjc/utils/funcdata')
6 files changed, 109 insertions, 72 deletions
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 72c5843..810e4f1 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/funcdata/FunctionalList.java +++ b/BJC-Utils2/src/main/java/bjc/utils/funcdata/FunctionalList.java @@ -153,6 +153,11 @@ public class FunctionalList<E> implements Cloneable { * NOTE: The returned list will have the length of the shorter of this * list and the combined one. * + * @param <T> + * The type of the second list + * @param <F> + * The type of the combined list + * * @param l * The list to combine with * @param bf @@ -197,6 +202,9 @@ public class FunctionalList<E> implements Cloneable { * Apply a function to each member of the list, then flatten the * results. Does not change the underlying list. * + * @param <T> + * The type of the flattened list + * * @param f * The function to apply to each member of the list. * @return A new list containing the flattened results of applying the @@ -277,6 +285,15 @@ public class FunctionalList<E> implements Cloneable { } /** + * Retrieve the size of the wrapped list + * + * @return The size of the wrapped list + */ + public int getSize() { + return wrap.size(); + } + + /** * Check if this list is empty. * * @return Whether or not this list is empty. @@ -289,6 +306,9 @@ public class FunctionalList<E> implements Cloneable { * Create a new list by applying the given function to each element in * the list. Does not change the underlying list. * + * @param <T> + * The type of the transformed list + * * @param f * The function to apply to each element in the list * @return A new list containing the mapped elements of this list. @@ -304,6 +324,9 @@ public class FunctionalList<E> implements Cloneable { /** * Zip two lists into a list of pairs * + * @param <T> + * The type of the second list + * * @param fl * The list to use as the left side of the pair * @return A list containing pairs of this element and the specified @@ -314,6 +337,31 @@ public class FunctionalList<E> implements Cloneable { } /** + * Partition this list into a list of sublists + * + * @param nPerPart + * The size of elements to put into each one of the sublists + * @return A list partitioned into partitions of size nPerPart + */ + public FunctionalList<FunctionalList<E>> partition(int nPerPart) { + FunctionalList<FunctionalList<E>> ret = new FunctionalList<>(); + + GenHolder<FunctionalList<E>> currPart = + new GenHolder<>(new FunctionalList<>()); + + this.forEach((val) -> { + if (currPart.unwrap((vl) -> vl.getSize() >= nPerPart)) { + ret.add(currPart.unwrap((vl) -> vl)); + currPart.transform((vl) -> new FunctionalList<>()); + } else { + currPart.unwrap((vl) -> vl.add(val)); + } + }); + + return ret; + } + + /** * Prepend an item to the list * * @param item @@ -338,6 +386,11 @@ public class FunctionalList<E> implements Cloneable { /** * Reduce this list to a single value, using a accumulative approach. * + * @param <T> + * The in-between type of the values + * @param <F> + * The final value type + * * @param val * The initial value of the accumulative state. * @param bf @@ -369,6 +422,12 @@ public class FunctionalList<E> implements Cloneable { return wrap.removeIf(remPred); } + /** + * Remove all parameters that match a given parameter + * + * @param obj + * The object to remove all matching copies of + */ public void removeMatching(E obj) { removeIf((ele) -> ele.equals(obj)); } @@ -416,6 +475,7 @@ public class FunctionalList<E> implements Cloneable { * * @see java.lang.Object#toString() */ + @Override public String toString() { StringBuilder sb = new StringBuilder("("); @@ -426,38 +486,4 @@ public class FunctionalList<E> implements Cloneable { return sb.toString(); } - - /** - * Retrieve the size of the wrapped list - * - * @return The size of the wrapped list - */ - public int getSize() { - return wrap.size(); - } - - /** - * Partition this list into a list of sublists - * - * @param nPerPart - * The size of elements to put into each one of the sublists - * @return A list partitioned into partitions of size nPerPart - */ - public FunctionalList<FunctionalList<E>> partition(int nPerPart) { - FunctionalList<FunctionalList<E>> ret = new FunctionalList<>(); - - GenHolder<FunctionalList<E>> currPart = new GenHolder<>( - new FunctionalList<>()); - - this.forEach((val) -> { - if (currPart.unwrap((vl) -> vl.getSize() >= nPerPart)) { - ret.add(currPart.unwrap((vl) -> vl)); - currPart.transform((vl) -> new FunctionalList<>()); - } else { - currPart.unwrap((vl) -> vl.add(val)); - } - }); - - return ret; - } } diff --git a/BJC-Utils2/src/main/java/bjc/utils/funcdata/FunctionalStringTokenizer.java b/BJC-Utils2/src/main/java/bjc/utils/funcdata/FunctionalStringTokenizer.java index ed4b9d3..aa59a31 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/funcdata/FunctionalStringTokenizer.java +++ b/BJC-Utils2/src/main/java/bjc/utils/funcdata/FunctionalStringTokenizer.java @@ -12,19 +12,20 @@ import java.util.function.Function; */ public class FunctionalStringTokenizer { /** - * The string tokenizer being driven + * Create a new tokenizer from the specified string. + * + * @param s + * The string to create a tokenizer from. + * @return A new tokenizer that splits the provided string on spaces. */ - private StringTokenizer inp; + public static FunctionalStringTokenizer fromString(String s) { + return new FunctionalStringTokenizer(new StringTokenizer(s, " ")); + } /** - * Create a functional string tokenizer from a non-functional one - * - * @param inp - * The non-functional string tokenizer to wrap + * The string tokenizer being driven */ - public FunctionalStringTokenizer(StringTokenizer inp) { - this.inp = inp; - } + private StringTokenizer inp; /** * Create a functional string tokenizer from a given string @@ -40,14 +41,26 @@ public class FunctionalStringTokenizer { * Create a functional string tokenizer from a given string and set of * seperators * - * @param inp The string to tokenize - * @param seps The string to use for splitting + * @param inp + * The string to tokenize + * @param seps + * The string to use for splitting */ public FunctionalStringTokenizer(String inp, String seps) { this.inp = new StringTokenizer(inp, seps); } /** + * Create a functional string tokenizer from a non-functional one + * + * @param inp + * The non-functional string tokenizer to wrap + */ + public FunctionalStringTokenizer(StringTokenizer inp) { + this.inp = inp; + } + + /** * Execute a provided action for each of the remaining tokens * * @param f @@ -60,6 +73,15 @@ public class FunctionalStringTokenizer { } /** + * Get the string tokenizer encapsuled by this + * + * @return The encapsulated tokenizer + */ + public StringTokenizer getInternal() { + return inp; + } + + /** * Return the next token from the tokenizer Returns null if no more * tokens are available * @@ -73,6 +95,9 @@ public class FunctionalStringTokenizer { * Convert the contents of this tokenizer into a list. Consumes all of * the input from this tokenizer. * + * @param <E> + * The type of the converted tokens + * * @param f * The function to use to convert tokens. * @return A list containing all of the converted tokens. @@ -84,23 +109,4 @@ public class FunctionalStringTokenizer { return r; } - - /** - * Create a new tokenizer from the specified string. - * - * @param s - * The string to create a tokenizer from. - * @return A new tokenizer that splits the provided string on spaces. - */ - public static FunctionalStringTokenizer fromString(String s) { - return new FunctionalStringTokenizer(new StringTokenizer(s, " ")); - } - - /** - * Get the string tokenizer encapsuled by this - * @return The encapsulated tokenizer - */ - public StringTokenizer getInternal() { - return inp; - } } diff --git a/BJC-Utils2/src/main/java/bjc/utils/funcdata/ITreePart.java b/BJC-Utils2/src/main/java/bjc/utils/funcdata/ITreePart.java index 24e68d4..7d8b6b0 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/funcdata/ITreePart.java +++ b/BJC-Utils2/src/main/java/bjc/utils/funcdata/ITreePart.java @@ -55,6 +55,9 @@ public interface ITreePart<T> { * Collapses this tree part into a single value. Does not change the * underlying tree. * + * @param <E> + * The type of the final collapsed value + * * @param f * The function to use to transform data into mapped form. * @param bf diff --git a/BJC-Utils2/src/main/java/bjc/utils/funcdata/bst/BinarySearchTree.java b/BJC-Utils2/src/main/java/bjc/utils/funcdata/bst/BinarySearchTree.java index 3f65481..0bd0119 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/funcdata/bst/BinarySearchTree.java +++ b/BJC-Utils2/src/main/java/bjc/utils/funcdata/bst/BinarySearchTree.java @@ -53,7 +53,7 @@ public class BinarySearchTree<T> { nCount++; if (root == null) { - root = new BinarySearchTreeNode<T>(dat, null, null); + root = new BinarySearchTreeNode<>(dat, null, null); } else { root.add(dat, comp); } @@ -75,7 +75,8 @@ public class BinarySearchTree<T> { while ((piv - adj) >= 0 && (piv + adj) < elms.getSize()) { if (root == null) { - root = new BinarySearchTreeNode<T>(elms.getByIndex(piv), null, null); + root = new BinarySearchTreeNode<>(elms.getByIndex(piv), + null, null); } else { root.add(elms.getByIndex(piv + adj), comp); root.add(elms.getByIndex(piv - adj), comp); diff --git a/BJC-Utils2/src/main/java/bjc/utils/funcdata/bst/BinarySearchTreeLeaf.java b/BJC-Utils2/src/main/java/bjc/utils/funcdata/bst/BinarySearchTreeLeaf.java index 02b9c7a..9befd17 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/funcdata/bst/BinarySearchTreeLeaf.java +++ b/BJC-Utils2/src/main/java/bjc/utils/funcdata/bst/BinarySearchTreeLeaf.java @@ -66,8 +66,8 @@ public class BinarySearchTreeLeaf<T> implements ITreePart<T> { * java.util.Comparator) */ @Override - public boolean contains(T data, Comparator<T> cmp) { - return this.data.equals(data); + public boolean contains(T dat, Comparator<T> cmp) { + return this.data.equals(dat); } /* @@ -117,6 +117,7 @@ public class BinarySearchTreeLeaf<T> implements ITreePart<T> { * bjc.utils.data.bst.ITreePart#forEach(bjc.utils.data.bst.ITreePart. * TreeLinearizationMethod, java.util.function.Predicate) */ + @Override public boolean forEach(TreeLinearizationMethod tlm, Predicate<T> c) { return c.test(data); } diff --git a/BJC-Utils2/src/main/java/bjc/utils/funcdata/bst/BinarySearchTreeNode.java b/BJC-Utils2/src/main/java/bjc/utils/funcdata/bst/BinarySearchTreeNode.java index 30a9fbd..715456c 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/funcdata/bst/BinarySearchTreeNode.java +++ b/BJC-Utils2/src/main/java/bjc/utils/funcdata/bst/BinarySearchTreeNode.java @@ -55,7 +55,7 @@ public class BinarySearchTreeNode<T> extends BinarySearchTreeLeaf<T> { switch (comp.compare(data, dat)) { case -1: if (left == null) { - left = new BinarySearchTreeNode<T>(dat, null, null); + left = new BinarySearchTreeNode<>(dat, null, null); } else { left.add(dat, comp); } @@ -68,7 +68,7 @@ public class BinarySearchTreeNode<T> extends BinarySearchTreeLeaf<T> { } case 1: if (right == null) { - right = new BinarySearchTreeNode<T>(dat, null, null); + right = new BinarySearchTreeNode<>(dat, null, null); } else { right.add(dat, comp); } @@ -96,9 +96,9 @@ public class BinarySearchTreeNode<T> extends BinarySearchTreeLeaf<T> { } @Override - public boolean contains(T data, Comparator<T> cmp) { + public boolean contains(T dat, Comparator<T> cmp) { return directedWalk(ds -> { - switch (cmp.compare(data, ds)) { + switch (cmp.compare(dat, ds)) { case -1: return LEFT; case 0: |
