From 41e85b1493a9253f11aefd179d14c3c01a7c9287 Mon Sep 17 00:00:00 2001 From: bculkin2442 Date: Sun, 21 Feb 2016 15:41:14 -0500 Subject: Commenting of various things --- .../java/bjc/utils/funcdata/FunctionalList.java | 108 ++++++++++++++++----- .../java/bjc/utils/funcdata/FunctionalMap.java | 27 ------ .../utils/funcdata/FunctionalStringTokenizer.java | 9 ++ .../bjc/utils/funcdata/bst/BinarySearchTree.java | 64 ++++++++---- .../main/java/bjc/utils/funcdata/bst/TreeLeaf.java | 84 ++++++++++------ .../main/java/bjc/utils/funcdata/bst/TreeNode.java | 30 ++++-- 6 files changed, 211 insertions(+), 111 deletions(-) delete mode 100644 BJC-Utils2/src/main/java/bjc/utils/funcdata/FunctionalMap.java (limited to 'BJC-Utils2/src/main/java') 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 88db172..9eb8e17 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/funcdata/FunctionalList.java +++ b/BJC-Utils2/src/main/java/bjc/utils/funcdata/FunctionalList.java @@ -26,6 +26,9 @@ import bjc.utils.data.Pair; * The type in this list */ public class FunctionalList implements Cloneable { + /** + * The list used as a backing store + */ private List wrap; /** @@ -92,14 +95,6 @@ public class FunctionalList implements Cloneable { public boolean add(E item) { return wrap.add(item); } - - /** - * Prepend an item to the list - * @param item The item to prepend to the list - */ - public void prepend(E item) { - wrap.add(0, item); - } /** * Check if all of the elements of this list match the specified @@ -136,6 +131,17 @@ public class FunctionalList implements Cloneable { return false; } + @Override + public FunctionalList clone() { + FunctionalList fl = new FunctionalList<>(); + + for (E ele : wrap) { + fl.add(ele); + } + + return fl; + } + /** * Combine this list with another one into a new list and merge the * results. Works sort of like a combined zip/map over resulting pairs. @@ -164,6 +170,17 @@ public class FunctionalList implements Cloneable { return r; } + /** + * Check if the list contains the specified item + * + * @param item + * The item to see if it is contained + * @return Whether or not the specified item is in the list + */ + public boolean contains(E item) { + return this.anyMatch(item::equals); + } + /** * Get the first element in the list * @@ -239,6 +256,25 @@ public class FunctionalList implements Cloneable { return wrap; } + /** + * Retrieve a list containing all elements matching a predicate + * + * @param matchPred + * The predicate to match by + * @return A list containing all elements that match the predicate + */ + public FunctionalList getMatching(Predicate matchPred) { + FunctionalList fl = new FunctionalList<>(); + + this.forEach((elem) -> { + if (matchPred.test(elem)) { + fl.add(elem); + } + }); + + return fl; + } + /** * Check if this list is empty. * @@ -264,6 +300,28 @@ public class FunctionalList implements Cloneable { return fl; } + /** + * Zip two lists into a list of pairs + * + * @param fl + * The list to use as the left side of the pair + * @return A list containing pairs of this element and the specified + * list + */ + public FunctionalList> pairWith(FunctionalList fl) { + return combineWith(fl, Pair::new); + } + + /** + * Prepend an item to the list + * + * @param item + * The item to prepend to the list + */ + public void prepend(E item) { + wrap.add(0, item); + } + /** * Select a random item from this list, using the provided random * number generator. @@ -301,6 +359,17 @@ public class FunctionalList implements Cloneable { return finl.apply(acum.held); } + /** + * Remove all elements that match a given predicate + * + * @param remPred + * The predicate to use to determine elements to delete + * @return Whether there was anything that satisfied the predicate + */ + public boolean removeIf(Predicate remPred) { + return wrap.removeIf(remPred); + } + /** * Perform a binary search for the specified key using the provided * means of comparing elements. Since this IS a binary search, the list @@ -329,6 +398,14 @@ public class FunctionalList implements Cloneable { Collections.sort(wrap, cmp); } + /** + * Convert the list into a iterable + * @return An iterable view onto the list + */ + public Iterable toIterable() { + return wrap; + } + /* * Reduce this item to a form useful for looking at in the debugger. * (non-Javadoc) @@ -345,19 +422,4 @@ public class FunctionalList implements Cloneable { return sb.toString(); } - - public FunctionalList> pairWith(FunctionalList fl) { - return combineWith(fl, Pair::new); - } - - @Override - public FunctionalList clone() { - FunctionalList fl = new FunctionalList<>(); - - for (E ele : wrap) { - fl.add(ele); - } - - return fl; - } } diff --git a/BJC-Utils2/src/main/java/bjc/utils/funcdata/FunctionalMap.java b/BJC-Utils2/src/main/java/bjc/utils/funcdata/FunctionalMap.java deleted file mode 100644 index aee76aa..0000000 --- a/BJC-Utils2/src/main/java/bjc/utils/funcdata/FunctionalMap.java +++ /dev/null @@ -1,27 +0,0 @@ -package bjc.utils.funcdata; - -import java.util.HashMap; -import java.util.Map; - -public class FunctionalMap { - private Map wrap; - - public FunctionalMap() { - wrap = new HashMap(); - } - - public FunctionalMap(Map wrap) { - - } - - - public T get(K key) { - return wrap.get(key); - } - - public void put(K key, T val) { - wrap.put(key, val); - } - - -} 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 9ee6b60..66df7cd 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/funcdata/FunctionalStringTokenizer.java +++ b/BJC-Utils2/src/main/java/bjc/utils/funcdata/FunctionalStringTokenizer.java @@ -11,6 +11,9 @@ import java.util.function.Function; * */ public class FunctionalStringTokenizer { + /** + * The string tokenizer being driven + */ private StringTokenizer inp; /** @@ -23,6 +26,12 @@ public class FunctionalStringTokenizer { this.inp = inp; } + /** + * Create a functional string tokenizer from a given string + * + * @param inp + * The string to tokenize + */ public FunctionalStringTokenizer(String inp) { this.inp = new StringTokenizer(inp); } 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 d028293..7bf0007 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 @@ -12,15 +12,28 @@ import bjc.utils.funcdata.bst.ITreePart.TreeLinearizationMethod; * * @author ben * - * @param The data type stored in the node. + * @param + * The data type stored in the node. */ public class BinarySearchTree { + /** + * The comparator for use in ordering items + */ private Comparator comp; + + /** + * The current count of elements in the tree + */ private int nCount; + + /** + * The root element of the tree + */ private ITreePart root; /** * Create a new tree using the specified way to compare elements. + * * @param cmp */ public BinarySearchTree(Comparator cmp) { @@ -30,7 +43,9 @@ public class BinarySearchTree { /** * Add a node to the binary search tree. - * @param dat The data to add to the binary search tree. + * + * @param dat + * The data to add to the binary search tree. */ public void addNode(T dat) { nCount++; @@ -42,49 +57,51 @@ public class BinarySearchTree { } /** - * Balance the tree, and remove soft-deleted nodes for free. - * Takes O(N) time, but also O(N) space. + * Balance the tree, and remove soft-deleted nodes for free. Takes O(N) + * time, but also O(N) space. */ public void balance() { ArrayList elms = new ArrayList<>(nCount); - + root.forEach(TreeLinearizationMethod.INORDER, e -> elms.add(e)); - + root = null; - + int piv = elms.size() / 2; int adj = 0; - - while((piv - adj) >= 0 && (piv + adj) < elms.size()) { - if(root == null) { + + while ((piv - adj) >= 0 && (piv + adj) < elms.size()) { + if (root == null) { root = new TreeNode(elms.get(piv), null, null); } else { root.add(elms.get(piv + adj), comp); root.add(elms.get(piv - adj), comp); } - + adj++; } - - if((piv - adj) >= 0) { + + if ((piv - adj) >= 0) { root.add(elms.get(piv - adj), comp); - } else if((piv + adj) < elms.size()) { + } else if ((piv + adj) < elms.size()) { root.add(elms.get(piv + adj), comp); } } /** * Get the root of the tree. + * * @return The root of the tree. */ public ITreePart getRoot() { return root; } - + /** - * Soft-delete a node from the tree. - * Soft-deleted nodes stay in the tree until trim()/balance() is invoked, - * and are not included in traversals/finds. + * Soft-delete a node from the tree. Soft-deleted nodes stay in the + * tree until trim()/balance() is invoked, and are not included in + * traversals/finds. + * * @param dat */ public void deleteNode(T dat) { @@ -94,7 +111,9 @@ public class BinarySearchTree { /** * Check if a node is in the tree - * @param dat The node to check the presence of for the tree. + * + * @param dat + * The node to check the presence of for the tree. * @return Whether or not the node is in the tree. */ public boolean isInTree(T dat) { @@ -103,8 +122,11 @@ public class BinarySearchTree { /** * Traverse the tree in a specified way until the function fails - * @param tlm The way to linearize the tree for traversal - * @param p The function to use until it fails + * + * @param tlm + * The way to linearize the tree for traversal + * @param p + * The function to use until it fails */ public void traverse(TreeLinearizationMethod tlm, Predicate p) { root.forEach(tlm, p); diff --git a/BJC-Utils2/src/main/java/bjc/utils/funcdata/bst/TreeLeaf.java b/BJC-Utils2/src/main/java/bjc/utils/funcdata/bst/TreeLeaf.java index 1928185..e2f204a 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/funcdata/bst/TreeLeaf.java +++ b/BJC-Utils2/src/main/java/bjc/utils/funcdata/bst/TreeLeaf.java @@ -7,26 +7,38 @@ import java.util.function.Predicate; /** * A leaf in a tree. + * * @author ben * - * @param The data stored in the tree. + * @param + * The data stored in the tree. */ public class TreeLeaf implements ITreePart { - protected T data; - protected boolean deleted; - + /** + * The data held in this tree leaf + */ + protected T data; + + /** + * Whether this node is soft-deleted or not + */ + protected boolean deleted; + /** * Create a new leaf holding the specified data. - * @param dat The data for the leaf to hold. + * + * @param dat + * The data for the leaf to hold. */ public TreeLeaf(T dat) { data = dat; } - + /* - * Can't add things to a leaf. - * (non-Javadoc) - * @see bjc.utils.data.bst.ITreePart#add(java.lang.Object, java.util.Comparator) + * Can't add things to a leaf. (non-Javadoc) + * + * @see bjc.utils.data.bst.ITreePart#add(java.lang.Object, + * java.util.Comparator) */ @Override public void add(T dat, Comparator comp) { @@ -34,9 +46,11 @@ public class TreeLeaf implements ITreePart { } /* - * Just transform our data. - * (non-Javadoc) - * @see bjc.utils.data.bst.ITreePart#collapse(java.util.function.Function, java.util.function.BiFunction) + * Just transform our data. (non-Javadoc) + * + * @see + * bjc.utils.data.bst.ITreePart#collapse(java.util.function.Function, + * java.util.function.BiFunction) */ @Override public E collapse(Function f, BiFunction bf) { @@ -44,56 +58,62 @@ public class TreeLeaf implements ITreePart { } /* - * Only check our data. - * (non-Javadoc) - * @see bjc.utils.data.bst.ITreePart#contains(java.lang.Object, java.util.Comparator) + * Only check our data. (non-Javadoc) + * + * @see bjc.utils.data.bst.ITreePart#contains(java.lang.Object, + * java.util.Comparator) */ @Override public boolean contains(T data, Comparator cmp) { return this.data.equals(data); } - + /* - * Just get the data - * (non-Javadoc) + * Just get the data (non-Javadoc) + * * @see bjc.utils.data.bst.ITreePart#data() */ @Override public T data() { return data; } - + /* - * Just mark ourselves as "not here" - * (non-Javadoc) - * @see bjc.utils.data.bst.ITreePart#delete(java.lang.Object, java.util.Comparator) + * Just mark ourselves as "not here" (non-Javadoc) + * + * @see bjc.utils.data.bst.ITreePart#delete(java.lang.Object, + * java.util.Comparator) */ @Override public void delete(T dat, Comparator cmp) { - if(data.equals(dat)) { + if (data.equals(dat)) { deleted = true; } } - + /* - * Just walk our data and only succede if the walk does, because there's nowhere left to go. - * (non-Javadoc) - * @see bjc.utils.data.bst.ITreePart#directedWalk(bjc.utils.data.bst.DirectedWalkFunction) + * Just walk our data and only succede if the walk does, because + * there's nowhere left to go. (non-Javadoc) + * + * @see bjc.utils.data.bst.ITreePart#directedWalk(bjc.utils.data.bst. + * DirectedWalkFunction) */ @Override public boolean directedWalk(DirectedWalkFunction ds) { - switch(ds.walk(data)) { + switch (ds.walk(data)) { case SUCCESS: return true; default: return false; } } - + /* - * Just check our data. - * (non-Javadoc) - * @see bjc.utils.data.bst.ITreePart#forEach(bjc.utils.data.bst.ITreePart.TreeLinearizationMethod, java.util.function.Predicate) + * Just check our data. (non-Javadoc) + * + * @see + * bjc.utils.data.bst.ITreePart#forEach(bjc.utils.data.bst.ITreePart. + * TreeLinearizationMethod, java.util.function.Predicate) */ public boolean forEach(TreeLinearizationMethod tlm, Predicate c) { return c.test(data); diff --git a/BJC-Utils2/src/main/java/bjc/utils/funcdata/bst/TreeNode.java b/BJC-Utils2/src/main/java/bjc/utils/funcdata/bst/TreeNode.java index 65eb546..40cc53f 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/funcdata/bst/TreeNode.java +++ b/BJC-Utils2/src/main/java/bjc/utils/funcdata/bst/TreeNode.java @@ -9,19 +9,32 @@ import java.util.function.Predicate; /** * A binary node in a tree. + * * @author ben * - * @param The data type stored in the tree. + * @param + * The data type stored in the tree. */ public class TreeNode extends TreeLeaf { + /** + * The left child of this node + */ private ITreePart left; + + /** + * The right child of this node + */ private ITreePart right; /** * Create a new node with the specified data and children. - * @param data The data to store in this node. - * @param left The left child of this node. - * @param right The right child of this node. + * + * @param data + * The data to store in this node. + * @param left + * The left child of this node. + * @param right + * The right child of this node. */ public TreeNode(T data, ITreePart left, ITreePart right) { super(data); @@ -30,9 +43,10 @@ public class TreeNode extends TreeLeaf { } /* - * Either adds it to the left/right, or undeletes itself. - * (non-Javadoc) - * @see bjc.utils.data.bst.TreeLeaf#add(java.lang.Object, java.util.Comparator) + * Either adds it to the left/right, or undeletes itself. (non-Javadoc) + * + * @see bjc.utils.data.bst.TreeLeaf#add(java.lang.Object, + * java.util.Comparator) */ @Override public void add(T dat, Comparator comp) { @@ -71,7 +85,7 @@ public class TreeNode extends TreeLeaf { return bf.apply(tm, left.collapse(f, bf)); } } else { - if(right != null) { + if (right != null) { return bf.apply(tm, right.collapse(f, bf)); } else { return tm; -- cgit v1.2.3