summaryrefslogtreecommitdiff
path: root/base/src/main/java/bjc/utils/funcdata
diff options
context:
space:
mode:
Diffstat (limited to 'base/src/main/java/bjc/utils/funcdata')
-rw-r--r--base/src/main/java/bjc/utils/funcdata/ExtendedMap.java24
-rw-r--r--base/src/main/java/bjc/utils/funcdata/FunctionalList.java91
-rw-r--r--base/src/main/java/bjc/utils/funcdata/FunctionalMap.java18
-rw-r--r--base/src/main/java/bjc/utils/funcdata/bst/BinarySearchTree.java91
-rw-r--r--base/src/main/java/bjc/utils/funcdata/bst/BinarySearchTreeLeaf.java14
-rw-r--r--base/src/main/java/bjc/utils/funcdata/bst/BinarySearchTreeNode.java36
-rw-r--r--base/src/main/java/bjc/utils/funcdata/bst/DirectedWalkFunction.java18
-rw-r--r--base/src/main/java/bjc/utils/funcdata/bst/ITreePart.java70
-rw-r--r--base/src/main/java/bjc/utils/funcdata/bst/TreeLinearizationMethod.java3
-rw-r--r--base/src/main/java/bjc/utils/funcdata/theory/Bifunctor.java98
-rw-r--r--base/src/main/java/bjc/utils/funcdata/theory/Functor.java28
11 files changed, 285 insertions, 206 deletions
diff --git a/base/src/main/java/bjc/utils/funcdata/ExtendedMap.java b/base/src/main/java/bjc/utils/funcdata/ExtendedMap.java
index 909c5e9..0c6389e 100644
--- a/base/src/main/java/bjc/utils/funcdata/ExtendedMap.java
+++ b/base/src/main/java/bjc/utils/funcdata/ExtendedMap.java
@@ -6,11 +6,33 @@ import java.util.function.Function;
import bjc.utils.funcutils.ListUtils;
+/**
+ * An extended version of a map, that stores values into a map, but can look
+ * into a different one for others.
+ *
+ * @author Ben Culkin
+ *
+ * @param <KeyType>
+ * The type of the keys of the map.
+ *
+ * @param <ValueType>
+ * The type of the values of the map.
+ */
class ExtendedMap<KeyType, ValueType> implements IMap<KeyType, ValueType> {
+ /* The map we delegate lookups to. */
private final IMap<KeyType, ValueType> delegate;
-
+ /* The map we store things in. */
private final IMap<KeyType, ValueType> store;
+ /**
+ * Create a new extended map.
+ *
+ * @param delegate
+ * The map to lookup things in.
+ *
+ * @param store
+ * The map to store things in.
+ */
public ExtendedMap(final IMap<KeyType, ValueType> delegate, final IMap<KeyType, ValueType> store) {
this.delegate = delegate;
this.store = store;
diff --git a/base/src/main/java/bjc/utils/funcdata/FunctionalList.java b/base/src/main/java/bjc/utils/funcdata/FunctionalList.java
index 55ea7ff..4cae085 100644
--- a/base/src/main/java/bjc/utils/funcdata/FunctionalList.java
+++ b/base/src/main/java/bjc/utils/funcdata/FunctionalList.java
@@ -27,17 +27,13 @@ import bjc.utils.data.Pair;
* @author ben
*
* @param <E>
- * The type in this list
+ * The type in this list
*/
public class FunctionalList<E> implements Cloneable, IList<E> {
- /*
- * The list used as a backing store
- */
+ /* The list used as a backing store */
private final List<E> wrapped;
- /**
- * Create a new empty functional list.
- */
+ /** Create a new empty functional list. */
public FunctionalList() {
wrapped = new ArrayList<>();
}
@@ -48,7 +44,7 @@ public class FunctionalList<E> implements Cloneable, IList<E> {
* Takes O(n) time, where n is the number of items specified
*
* @param items
- * The items to put into this functional list.
+ * The items to put into this functional list.
*/
@SafeVarargs
public FunctionalList(final E... items) {
@@ -63,7 +59,7 @@ public class FunctionalList<E> implements Cloneable, IList<E> {
* Create a new functional list with the specified size.
*
* @param size
- * The size of the backing list .
+ * The size of the backing list .
*/
private FunctionalList(final int size) {
wrapped = new ArrayList<>(size);
@@ -75,7 +71,7 @@ public class FunctionalList<E> implements Cloneable, IList<E> {
* Takes O(1) time, since it doesn't copy the list.
*
* @param backing
- * The list to use as a backing list.
+ * The list to use as a backing list.
*/
public FunctionalList(final List<E> backing) {
if (backing == null) throw new NullPointerException("Backing list must be non-null");
@@ -94,11 +90,11 @@ public class FunctionalList<E> implements Cloneable, IList<E> {
for (final E item : wrapped) {
if (!predicate.test(item))
- // We've found a non-matching item
+ /* We've found a non-matching item. */
return false;
}
- // All of the items matched
+ /* All of the items matched. */
return true;
}
@@ -108,20 +104,21 @@ public class FunctionalList<E> implements Cloneable, IList<E> {
for (final E item : wrapped) {
if (predicate.test(item))
- // We've found a matching item
+ /* We've found a matching item. */
return true;
}
- // We didn't find a matching item
+ /* We didn't find a matching item. */
return false;
}
/**
- * Clone this list into a new one, and clone the backing list as well
+ * Clone this list into a new one, and clone the backing list as well.
*
- * Takes O(n) time, where n is the number of elements in the list
+ * Takes O(n) time, where n is the number of elements in the list.
*
- * @return A list
+ * @return
+ * A copy of the list.
*/
@Override
public IList<E> clone() {
@@ -136,18 +133,20 @@ public class FunctionalList<E> implements Cloneable, IList<E> {
@Override
public <T, F> IList<F> combineWith(final IList<T> rightList, final BiFunction<E, T, F> itemCombiner) {
- if (rightList == null)
+ if (rightList == null) {
throw new NullPointerException("Target combine list must not be null");
- else if (itemCombiner == null) throw new NullPointerException("Combiner must not be null");
+ } else if (itemCombiner == null) {
+ throw new NullPointerException("Combiner must not be null");
+ }
final IList<F> returned = new FunctionalList<>();
- // Get the iterator for the other list
+ /* Get the iterator for the other list. */
final Iterator<T> rightIterator = rightList.toIterable().iterator();
for (final Iterator<E> leftIterator = wrapped.iterator(); leftIterator.hasNext()
&& rightIterator.hasNext();) {
- // Add the transformed items to the result list
+ /* Add the transformed items to the result list. */
final E leftVal = leftIterator.next();
final T rightVal = rightIterator.next();
@@ -159,7 +158,7 @@ public class FunctionalList<E> implements Cloneable, IList<E> {
@Override
public boolean contains(final E item) {
- // Check if any items in the list match the provided item
+ /* Check if any items in the list match the provided item. */
return this.anyMatch(item::equals);
}
@@ -182,7 +181,7 @@ public class FunctionalList<E> implements Cloneable, IList<E> {
if (expandedElement == null) throw new NullPointerException("Expander returned null list");
- // Add each element to the returned list
+ /* Add each element to the returned list. */
expandedElement.forEach(returned::add);
});
@@ -200,15 +199,14 @@ public class FunctionalList<E> implements Cloneable, IList<E> {
public void forEachIndexed(final BiConsumer<Integer, E> indexedAction) {
if (indexedAction == null) throw new NullPointerException("Action must not be null");
- // This is held b/c ref'd variables must be final/effectively
- // final
+ /* This is held b/c ref'd variables must be final/effectively final. */
final IHolder<Integer> currentIndex = new Identity<>(0);
wrapped.forEach((element) -> {
- // Call the action with the index and the value
+ /* Call the action with the index and the value. */
indexedAction.accept(currentIndex.unwrap(index -> index), element);
- // Increment the value
+ /* Increment the value. */
currentIndex.transform((index) -> index + 1);
});
}
@@ -221,7 +219,8 @@ public class FunctionalList<E> implements Cloneable, IList<E> {
/**
* Get the internal backing list.
*
- * @return The backing list this list is based off of.
+ * @return
+ * The backing list this list is based off of.
*/
public List<E> getInternal() {
return wrapped;
@@ -235,8 +234,7 @@ public class FunctionalList<E> implements Cloneable, IList<E> {
wrapped.forEach((element) -> {
if (predicate.test(element)) {
- // The item matches, so add it to the returned
- // list
+ /* The item matches, so add it to the returned list. */
returned.add(element);
}
});
@@ -254,9 +252,7 @@ public class FunctionalList<E> implements Cloneable, IList<E> {
return wrapped.isEmpty();
}
- /*
- * Check if a partition has room for another item
- */
+ /* Check if a partition has room for another item. */
private Boolean isPartitionFull(final int numberPerPartition, final IHolder<IList<E>> currentPartition) {
return currentPartition.unwrap((partition) -> partition.getSize() >= numberPerPartition);
}
@@ -291,18 +287,18 @@ public class FunctionalList<E> implements Cloneable, IList<E> {
final IList<IList<E>> returned = new FunctionalList<>();
- // The current partition being filled
+ /* The current partition being filled. */
final IHolder<IList<E>> currentPartition = new Identity<>(new FunctionalList<>());
this.forEach(element -> {
if (isPartitionFull(numberPerPartition, currentPartition)) {
- // Add the partition to the list
+ /* Add the partition to the list. */
returned.add(currentPartition.unwrap(partition -> partition));
- // Start a new partition
+ /* Start a new partition. */
currentPartition.transform(partition -> new FunctionalList<>());
} else {
- // Add the element to the current partition
+ /* Add the element to the current partition. */
currentPartition.unwrap(partition -> partition.add(element));
}
});
@@ -327,19 +323,21 @@ public class FunctionalList<E> implements Cloneable, IList<E> {
@Override
public <T, F> F reduceAux(final T initialValue, final BiFunction<E, T, T> stateAccumulator,
final Function<T, F> resultTransformer) {
- if (stateAccumulator == null)
+ if (stateAccumulator == null) {
throw new NullPointerException("Accumulator must not be null");
- else if (resultTransformer == null) throw new NullPointerException("Transformer must not be null");
+ } else if (resultTransformer == null) {
+ throw new NullPointerException("Transformer must not be null");
+ }
- // The current collapsed list
+ /* The current collapsed list. */
final IHolder<T> currentState = new Identity<>(initialValue);
wrapped.forEach(element -> {
- // Accumulate a new value into the state
+ /* Accumulate a new value into the state. */
currentState.transform(state -> stateAccumulator.apply(element, state));
});
- // Convert the state to its final value
+ /* Convert the state to its final value. */
return currentState.unwrap(resultTransformer);
}
@@ -362,19 +360,20 @@ public class FunctionalList<E> implements Cloneable, IList<E> {
@Override
public E search(final E searchKey, final Comparator<E> comparator) {
- // Search our internal list
+ /* Search our internal list. */
final int foundIndex = Collections.binarySearch(wrapped, searchKey, comparator);
- if (foundIndex >= 0) // We found a matching element
+ if (foundIndex >= 0) {
+ /* We found a matching element. */
return wrapped.get(foundIndex);
+ }
- // We didn't find an element
+ /* We didn't find an element. */
return null;
}
@Override
public void sort(final Comparator<E> comparator) {
- // sb.deleteCharAt(sb.length() - 2);
Collections.sort(wrapped, comparator);
}
diff --git a/base/src/main/java/bjc/utils/funcdata/FunctionalMap.java b/base/src/main/java/bjc/utils/funcdata/FunctionalMap.java
index c4f0ff1..1218833 100644
--- a/base/src/main/java/bjc/utils/funcdata/FunctionalMap.java
+++ b/base/src/main/java/bjc/utils/funcdata/FunctionalMap.java
@@ -14,25 +14,25 @@ import bjc.utils.data.IPair;
* @author ben
*
* @param <KeyType>
- * The type of the map's keys
+ * The type of the map's keys.
+ *
* @param <ValueType>
- * The type of the map's values
+ * The type of the map's values.
*/
public class FunctionalMap<KeyType, ValueType> implements IMap<KeyType, ValueType> {
+ /* Our backing store. */
private Map<KeyType, ValueType> wrappedMap;
- /**
- * Create a new blank functional map
- */
+ /** Create a new blank functional map */
public FunctionalMap() {
wrappedMap = new HashMap<>();
}
/**
- * Create a new functional map with the specified entries
+ * Create a new functional map with the specified entries.
*
* @param entries
- * The entries to put into the map
+ * The entries to put into the map.
*/
@SafeVarargs
public FunctionalMap(final IPair<KeyType, ValueType>... entries) {
@@ -46,10 +46,10 @@ public class FunctionalMap<KeyType, ValueType> implements IMap<KeyType, ValueTyp
}
/**
- * Create a new functional map wrapping the specified map
+ * Create a new functional map wrapping the specified map.
*
* @param wrap
- * The map to wrap
+ * The map to wrap.
*/
public FunctionalMap(final Map<KeyType, ValueType> wrap) {
if (wrap == null) throw new NullPointerException("Map to wrap must not be null");
diff --git a/base/src/main/java/bjc/utils/funcdata/bst/BinarySearchTree.java b/base/src/main/java/bjc/utils/funcdata/bst/BinarySearchTree.java
index 8acd477..f9dc4a2 100644
--- a/base/src/main/java/bjc/utils/funcdata/bst/BinarySearchTree.java
+++ b/base/src/main/java/bjc/utils/funcdata/bst/BinarySearchTree.java
@@ -14,29 +14,23 @@ import bjc.utils.funcdata.IList;
* @author ben
*
* @param <T>
- * The data type stored in the node.
+ * The data type stored in the node.
*/
public class BinarySearchTree<T> {
- /*
- * The comparator for use in ordering items
- */
+ /* The comparator for use in ordering items */
private final Comparator<T> comparator;
- /*
- * The current count of elements in the tree
- */
+ /* The current count of elements in the tree */
private int elementCount;
- /*
- * The root element of the tree
- */
+ /* The root element of the tree */
private ITreePart<T> root;
/**
* Create a new tree using the specified way to compare elements.
*
* @param cmp
- * The thing to use for comparing elements
+ * The thing to use for comparing elements
*/
public BinarySearchTree(final Comparator<T> cmp) {
if (cmp == null) throw new NullPointerException("Comparator must not be null");
@@ -49,7 +43,7 @@ public class BinarySearchTree<T> {
* Add a node to the binary search tree.
*
* @param element
- * The data to add to the binary search tree.
+ * The data to add to the binary search tree.
*/
public void addNode(final T element) {
elementCount++;
@@ -62,18 +56,22 @@ public class BinarySearchTree<T> {
}
/**
- * Check if an adjusted pivot falls with the bounds of a list
+ * Check if an adjusted pivot falls with the bounds of a list.
*
* @param elements
- * The list to get bounds from
+ * The list to get bounds from.
+ *
* @param pivot
- * The pivot
+ * The pivot.
+ *
* @param pivotAdjustment
- * The distance from the pivot
- * @return Whether the adjusted pivot is with the list
+ * The distance from the pivot.
+ *
+ * @return
+ * Whether the adjusted pivot is with the list.
*/
private boolean adjustedPivotInBounds(final IList<T> elements, final int pivot, final int pivotAdjustment) {
- return pivot - pivotAdjustment >= 0 && pivot + pivotAdjustment < elements.getSize();
+ return ((pivot - pivotAdjustment) >= 0) && ((pivot + pivotAdjustment) < elements.getSize());
}
/**
@@ -84,34 +82,35 @@ public class BinarySearchTree<T> {
public void balance() {
final IList<T> elements = new FunctionalList<>();
- // Add each element to the list in sorted order
+ /* Add each element to the list in sorted order. */
root.forEach(TreeLinearizationMethod.INORDER, element -> elements.add(element));
- // Clear the tree
+ /* Clear the tree. */
root = null;
- // Set up the pivot and adjustment for readding elements
+ /* Set up the pivot and adjustment for readding elements. */
final int pivot = elements.getSize() / 2;
int pivotAdjustment = 0;
- // Add elements until there aren't any left
+ /* Add elements until there aren't any left. */
while (adjustedPivotInBounds(elements, pivot, pivotAdjustment)) {
if (root == null) {
- // Create a new root element
+ /* Create a new root element. */
root = new BinarySearchTreeNode<>(elements.getByIndex(pivot), null, null);
} else {
- // Add the left and right elements in a balanced
- // manner
+ /*
+ * Add the left and right elements in a balanced
+ * manner.
+ */
root.add(elements.getByIndex(pivot + pivotAdjustment), comparator);
-
root.add(elements.getByIndex(pivot - pivotAdjustment), comparator);
}
- // Increase the distance from the pivot
+ /* Increase the distance from the pivot. */
pivotAdjustment++;
}
- // Add any trailing unbalanced elements
+ /* Add any trailing unbalanced elements. */
if (pivot - pivotAdjustment >= 0) {
root.add(elements.getByIndex(pivot - pivotAdjustment), comparator);
} else if (pivot + pivotAdjustment < elements.getSize()) {
@@ -126,7 +125,7 @@ public class BinarySearchTree<T> {
* invoked, and are not included in traversals/finds.
*
* @param element
- * The node to delete
+ * The node to delete
*/
public void deleteNode(final T element) {
elementCount--;
@@ -137,17 +136,19 @@ public class BinarySearchTree<T> {
/**
* Get the root of the tree.
*
- * @return The root of the tree.
+ * @return
+ * The root of the tree.
*/
public ITreePart<T> getRoot() {
return root;
}
/**
- * Check if a node is in the tree
+ * Check if a node is in the tree.
*
* @param element
- * The node to check the presence of for the tree.
+ * The node to check the presence of for the tree..
+ *
* @return Whether or not the node is in the tree.
*/
public boolean isInTree(final T element) {
@@ -155,37 +156,41 @@ public class BinarySearchTree<T> {
}
/**
- * Traverse the tree in a specified way until the function fails
+ * Traverse the tree in a specified way until the function fails.
*
* @param linearizationMethod
- * The way to linearize the tree for traversal
+ * The way to linearize the tree for traversal.
+ *
* @param traversalPredicate
- * The function to use until it fails
+ * The function to use until it fails.
*/
public void traverse(final TreeLinearizationMethod linearizationMethod, final Predicate<T> traversalPredicate) {
- if (linearizationMethod == null)
+ if (linearizationMethod == null) {
throw new NullPointerException("Linearization method must not be null");
- else if (traversalPredicate == null) throw new NullPointerException("Predicate must not be nulls");
+ } else if (traversalPredicate == null) {
+ throw new NullPointerException("Predicate must not be nulls");
+ }
root.forEach(linearizationMethod, traversalPredicate);
}
- /**
- * Remove all soft-deleted nodes from the tree.
- */
+ /** Remove all soft-deleted nodes from the tree. */
public void trim() {
final List<T> nodes = new ArrayList<>(elementCount);
- // Add all non-soft deleted nodes to the tree in insertion order
+ /*
+ * Add all non-soft deleted nodes to the tree in insertion
+ * order.
+ */
traverse(TreeLinearizationMethod.PREORDER, node -> {
nodes.add(node);
return true;
});
- // Clear the tree
+ /* Clear the tree. */
root = null;
- // Add the nodes to the tree in the order they were inserted
+ /* Add the nodes to the tree in the order they were inserted. */
nodes.forEach(node -> addNode(node));
}
diff --git a/base/src/main/java/bjc/utils/funcdata/bst/BinarySearchTreeLeaf.java b/base/src/main/java/bjc/utils/funcdata/bst/BinarySearchTreeLeaf.java
index 8c4f3f0..46f597e 100644
--- a/base/src/main/java/bjc/utils/funcdata/bst/BinarySearchTreeLeaf.java
+++ b/base/src/main/java/bjc/utils/funcdata/bst/BinarySearchTreeLeaf.java
@@ -11,24 +11,20 @@ import java.util.function.Predicate;
* @author ben
*
* @param <T>
- * The data stored in the tree.
+ * The data stored in the tree.
*/
public class BinarySearchTreeLeaf<T> implements ITreePart<T> {
- /**
- * The data held in this tree leaf
- */
+ /** The data held in this tree leaf */
protected T data;
- /**
- * Whether this node is soft-deleted or not
- */
+ /** Whether this node is soft-deleted or not */
protected boolean isDeleted;
/**
* Create a new leaf holding the specified data.
*
* @param element
- * The data for the leaf to hold.
+ * The data for the leaf to hold.
*/
public BinarySearchTreeLeaf(final T element) {
data = element;
@@ -70,7 +66,7 @@ public class BinarySearchTreeLeaf<T> implements ITreePart<T> {
switch (treeWalker.walk(data)) {
case SUCCESS:
return true;
- // We don't have any children to care about
+ /* We don't have any children to care about. */
case FAILURE:
case LEFT:
case RIGHT:
diff --git a/base/src/main/java/bjc/utils/funcdata/bst/BinarySearchTreeNode.java b/base/src/main/java/bjc/utils/funcdata/bst/BinarySearchTreeNode.java
index 9f45c17..3124474 100644
--- a/base/src/main/java/bjc/utils/funcdata/bst/BinarySearchTreeNode.java
+++ b/base/src/main/java/bjc/utils/funcdata/bst/BinarySearchTreeNode.java
@@ -16,28 +16,26 @@ import java.util.function.Predicate;
* @author ben
*
* @param <T>
- * The data type stored in the tree.
+ * The data type stored in the tree.
*/
public class BinarySearchTreeNode<T> extends BinarySearchTreeLeaf<T> {
- /*
- * The left child of this node
- */
+ /* The left child of this node */
private ITreePart<T> left;
- /*
- * The right child of this node
- */
+ /* The right child of this node */
private ITreePart<T> right;
/**
* Create a new node with the specified data and children.
*
* @param element
- * The data to store in this node.
+ * The data to store in this node.
+ *
* @param lft
- * The left child of this node.
+ * The left child of this node.
+ *
* @param rght
- * The right child of this node.
+ * The right child of this node.
*/
public BinarySearchTreeNode(final T element, final ITreePart<T> lft, final ITreePart<T> rght) {
super(element);
@@ -154,7 +152,6 @@ public class BinarySearchTreeNode<T> extends BinarySearchTreeLeaf<T> {
case RIGHT:
return right.directedWalk(treeWalker);
case FAILURE:
- return false;
default:
return false;
}
@@ -163,9 +160,11 @@ public class BinarySearchTreeNode<T> extends BinarySearchTreeLeaf<T> {
@Override
public boolean forEach(final TreeLinearizationMethod linearizationMethod,
final Predicate<T> traversalPredicate) {
- if (linearizationMethod == null)
+ if (linearizationMethod == null) {
throw new NullPointerException("Linearization method must not be null");
- else if (traversalPredicate == null) throw new NullPointerException("Predicate must not be null");
+ } else if (traversalPredicate == null) {
+ throw new NullPointerException("Predicate must not be null");
+ }
switch (linearizationMethod) {
case PREORDER:
@@ -175,11 +174,13 @@ public class BinarySearchTreeNode<T> extends BinarySearchTreeLeaf<T> {
case POSTORDER:
return postorderTraverse(linearizationMethod, traversalPredicate);
default:
- throw new IllegalArgumentException(
- "Passed an incorrect TreeLinearizationMethod " + linearizationMethod + ". WAT");
+ String msg = String.format("Passed an incorrect TreeLinearizationMethod %s. WAT", linearizationMethod);
+
+ throw new IllegalArgumentException(msg);
}
}
+ /* Do an in-order traversal. */
private boolean inorderTraverse(final TreeLinearizationMethod linearizationMethod,
final Predicate<T> traversalPredicate) {
if (!traverseLeftBranch(linearizationMethod, traversalPredicate)) return false;
@@ -191,6 +192,7 @@ public class BinarySearchTreeNode<T> extends BinarySearchTreeLeaf<T> {
return true;
}
+ /* Do a post-order traversal. */
private boolean postorderTraverse(final TreeLinearizationMethod linearizationMethod,
final Predicate<T> traversalPredicate) {
if (!traverseLeftBranch(linearizationMethod, traversalPredicate)) return false;
@@ -203,6 +205,7 @@ public class BinarySearchTreeNode<T> extends BinarySearchTreeLeaf<T> {
}
+ /* Do a pre-order traversal. */
private boolean preorderTraverse(final TreeLinearizationMethod linearizationMethod,
final Predicate<T> traversalPredicate) {
if (!traverseElement(traversalPredicate)) return false;
@@ -214,6 +217,7 @@ public class BinarySearchTreeNode<T> extends BinarySearchTreeLeaf<T> {
return true;
}
+ /* Traverse an element. */
private boolean traverseElement(final Predicate<T> traversalPredicate) {
boolean nodeSuccesfullyTraversed;
@@ -226,6 +230,7 @@ public class BinarySearchTreeNode<T> extends BinarySearchTreeLeaf<T> {
return nodeSuccesfullyTraversed;
}
+ /* Traverse the left branch of a tree. */
private boolean traverseLeftBranch(final TreeLinearizationMethod linearizationMethod,
final Predicate<T> traversalPredicate) {
boolean leftSuccesfullyTraversed;
@@ -239,6 +244,7 @@ public class BinarySearchTreeNode<T> extends BinarySearchTreeLeaf<T> {
return leftSuccesfullyTraversed;
}
+ /* Traverse the right branch of a tree. */
private boolean traverseRightBranch(final TreeLinearizationMethod linearizationMethod,
final Predicate<T> traversalPredicate) {
boolean rightSuccesfullyTraversed;
diff --git a/base/src/main/java/bjc/utils/funcdata/bst/DirectedWalkFunction.java b/base/src/main/java/bjc/utils/funcdata/bst/DirectedWalkFunction.java
index e11524a..fdf86d7 100644
--- a/base/src/main/java/bjc/utils/funcdata/bst/DirectedWalkFunction.java
+++ b/base/src/main/java/bjc/utils/funcdata/bst/DirectedWalkFunction.java
@@ -6,7 +6,7 @@ package bjc.utils.funcdata.bst;
* @author ben
*
* @param <T>
- * The type of element stored in the walked tree
+ * The type of element stored in the walked tree
*/
@FunctionalInterface
public interface DirectedWalkFunction<T> {
@@ -14,12 +14,9 @@ public interface DirectedWalkFunction<T> {
* Represents the results used to direct a walk in a binary tree.
*
* @author ben
- *
*/
public enum DirectedWalkResult {
- /**
- * Specifies that the function has failed.
- */
+ /** Specifies that the function has failed. */
FAILURE,
/**
* Specifies that the function wants to move left in the tree
@@ -31,10 +28,7 @@ public interface DirectedWalkFunction<T> {
* next.
*/
RIGHT,
- /**
- * Specifies that the function has succesfully completed
- *
- */
+ /** Specifies that the function has succesfully completed */
SUCCESS
}
@@ -42,8 +36,10 @@ public interface DirectedWalkFunction<T> {
* Perform a directed walk on a node of a tree.
*
* @param element
- * The data stored in the node currently being visited
- * @return The way the function wants the walk to go next.
+ * The data stored in the node currently being visited.
+ *
+ * @return
+ * The way the function wants the walk to go next.
*/
public DirectedWalkResult walk(T element);
}
diff --git a/base/src/main/java/bjc/utils/funcdata/bst/ITreePart.java b/base/src/main/java/bjc/utils/funcdata/bst/ITreePart.java
index 3aa8880..a2ce71f 100644
--- a/base/src/main/java/bjc/utils/funcdata/bst/ITreePart.java
+++ b/base/src/main/java/bjc/utils/funcdata/bst/ITreePart.java
@@ -11,53 +11,61 @@ import java.util.function.Predicate;
* @author ben
*
* @param <T>
- * The data contained in this part of the tree.
+ * The data contained in this part of the tree.
*/
public interface ITreePart<T> {
/**
* Add a element below this tree part somewhere.
*
* @param element
- * The element to add below this tree part
+ * The element to add below this tree part
+ *
* @param comparator
- * The thing to use for comparing values to find where to
- * insert the tree part.
+ * The thing to use for comparing values to find where to
+ * insert the tree part.
*/
public void add(T element, Comparator<T> comparator);
/**
- * Collapses this tree part into a single value. Does not change the
- * underlying tree.
+ * Collapses this tree part into a single value.
+ *
+ * Does not change the underlying tree.
*
* @param <E>
- * The type of the final collapsed value
+ * The type of the final collapsed value
*
* @param nodeCollapser
- * The function to use to transform data into mapped
- * form.
+ * The function to use to transform data into mapped form.
+ *
* @param branchCollapser
- * The function to use to collapse data in mapped form
- * into a single value.
- * @return A single value from collapsing the tree.
+ * The function to use to collapse data in mapped form into a
+ * single value.
+ *
+ * @return
+ * A single value from collapsing the tree.
*/
public <E> E collapse(Function<T, E> nodeCollapser, BiFunction<E, E, E> branchCollapser);
/**
- * Check if this tre part or below it contains the specified data item
+ * Check if this tre part or below it contains the specified data item.
*
* @param element
- * The data item to look for.
+ * The data item to look for.
+ *
* @param comparator
- * The comparator to use to search for the data item
- * @return Whether or not the given item is contained in this tree part
- * or its children.
+ * The comparator to use to search for the data item.
+ *
+ * @return
+ * Whether or not the given item is contained in this tree part or
+ * its children.
*/
public boolean contains(T element, Comparator<T> comparator);
/**
* Get the data associated with this tree part.
*
- * @return The data associated with this tree part.
+ * @return
+ * The data associated with this tree part.
*/
public T data();
@@ -65,9 +73,10 @@ public interface ITreePart<T> {
* Remove the given node from this tree part and any of its children.
*
* @param element
- * The data item to remove.
+ * The data item to remove.
+ *
* @param comparator
- * The comparator to use to search for the data item.
+ * The comparator to use to search for the data item.
*/
public void delete(T element, Comparator<T> comparator);
@@ -75,22 +84,27 @@ public interface ITreePart<T> {
* Execute a directed walk through the tree.
*
* @param walker
- * The function to use to direct the walk through the
- * tree.
- * @return Whether the directed walk finished successfully.
+ * The function to use to direct the walk through the
+ * tree.
+ *
+ * @return
+ * Whether the directed walk finished successfully.
*/
public boolean directedWalk(DirectedWalkFunction<T> walker);
/**
* Execute a provided function for each element of tree it succesfully
- * completes for
+ * completes for.
*
* @param linearizationMethod
- * The way to linearize the tree for executing
+ * The way to linearize the tree for executing.
+ *
* @param predicate
- * The predicate to apply to each element, where it
- * returning false terminates traversal early
- * @return Whether the traversal finished succesfully
+ * The predicate to apply to each element, where it returning false
+ * terminates traversal early.
+ *
+ * @return
+ * Whether the traversal finished succesfully.
*/
public boolean forEach(TreeLinearizationMethod linearizationMethod, Predicate<T> predicate);
}
diff --git a/base/src/main/java/bjc/utils/funcdata/bst/TreeLinearizationMethod.java b/base/src/main/java/bjc/utils/funcdata/bst/TreeLinearizationMethod.java
index 0c83867..80afaf2 100644
--- a/base/src/main/java/bjc/utils/funcdata/bst/TreeLinearizationMethod.java
+++ b/base/src/main/java/bjc/utils/funcdata/bst/TreeLinearizationMethod.java
@@ -4,7 +4,6 @@ package bjc.utils.funcdata.bst;
* Represents the ways to linearize a tree for traversal.
*
* @author ben
- *
*/
public enum TreeLinearizationMethod {
/**
@@ -22,4 +21,4 @@ public enum TreeLinearizationMethod {
* then the right part.
*/
PREORDER
-} \ No newline at end of file
+}
diff --git a/base/src/main/java/bjc/utils/funcdata/theory/Bifunctor.java b/base/src/main/java/bjc/utils/funcdata/theory/Bifunctor.java
index 13c1709..a94a7b5 100644
--- a/base/src/main/java/bjc/utils/funcdata/theory/Bifunctor.java
+++ b/base/src/main/java/bjc/utils/funcdata/theory/Bifunctor.java
@@ -3,14 +3,15 @@ package bjc.utils.funcdata.theory;
import java.util.function.Function;
/**
- * A functor over a pair of heterogeneous types
+ * A functor over a pair of heterogeneous types.
*
* @author ben
+ *
* @param <LeftType>
- * The type stored on the 'left' of the pair
- * @param <RightType>
- * The type stored on the 'right' of the pair
+ * The type stored on the 'left' of the pair.
*
+ * @param <RightType>
+ * The type stored on the 'right' of the pair.
*/
public interface Bifunctor<LeftType, RightType> {
/**
@@ -18,10 +19,17 @@ public interface Bifunctor<LeftType, RightType> {
*
* @author EVE
*
- * @param <OldLeft>
+ * @param <OldLeft>
+ * The old left type.
+ *
* @param <OldRight>
+ * The old right type.
+ *
* @param <NewLeft>
+ * The new left type.
+ *
* @param <NewRight>
+ * The new right type.
*/
public interface BifunctorMap<OldLeft, OldRight, NewLeft, NewRight>
extends Function<Bifunctor<OldLeft, OldRight>, Bifunctor<NewLeft, NewRight>> {
@@ -34,8 +42,13 @@ public interface Bifunctor<LeftType, RightType> {
* @author EVE
*
* @param <OldLeft>
+ * The old left type.
+ *
* @param <OldRight>
+ * The old right type.
+ *
* @param <NewLeft>
+ * The new left type.
*/
public interface LeftBifunctorMap<OldLeft, OldRight, NewLeft>
extends BifunctorMap<OldLeft, OldRight, NewLeft, OldRight> {
@@ -48,8 +61,13 @@ public interface Bifunctor<LeftType, RightType> {
* @author EVE
*
* @param <OldLeft>
+ * The old left type.
+ *
* @param <OldRight>
+ * The old right type.
+ *
* @param <NewRight>
+ * The new right type.
*/
public interface RightBifunctorMap<OldLeft, OldRight, NewRight>
extends BifunctorMap<OldLeft, OldRight, OldLeft, NewRight> {
@@ -58,21 +76,28 @@ public interface Bifunctor<LeftType, RightType> {
/**
* Lift a pair of functions to a single function that maps over both
- * parts of a pair
+ * parts of a pair.
*
* @param <OldLeft>
- * The old left type of the pair
+ * The old left type of the pair.
+ *
* @param <OldRight>
- * The old right type of the pair
+ * The old right type of the pair.
+ *
* @param <NewLeft>
- * The new left type of the pair
+ * The new left type of the pair.
+ *
* @param <NewRight>
- * The new right type of the pair
+ * The new right type of the pair.
+ *
* @param leftFunc
- * The function that maps over the left of the pair
+ * The function that maps over the left of the pair.
+ *
* @param rightFunc
- * The function that maps over the right of the pair
- * @return A function that maps over both parts of the pair
+ * The function that maps over the right of the pair.
+ *
+ * @return
+ * A function that maps over both parts of the pair.
*/
public default <OldLeft, OldRight, NewLeft, NewRight> BifunctorMap<OldLeft, OldRight, NewLeft, NewRight> bimap(
final Function<OldLeft, NewLeft> leftFunc, final Function<OldRight, NewRight> rightFunc) {
@@ -90,50 +115,61 @@ public interface Bifunctor<LeftType, RightType> {
}
/**
- * Lift a function to operate over the left part of this pair
+ * Lift a function to operate over the left part of this pair.
*
* @param <OldLeft>
- * The old left type of the pair
+ * The old left type of the pair.
+ *
* @param <OldRight>
- * The old right type of the pair
+ * The old right type of the pair.
+ *
* @param <NewLeft>
- * The new left type of the pair
+ * The new left type of the pair.
+ *
* @param func
- * The function to lift to work over the left side of the
- * pair
- * @return The function lifted to work over the left side of bifunctors
+ * The function to lift to work over the left side of the pair.
+ *
+ * @return
+ * The function lifted to work over the left side of bifunctors.
*/
public <OldLeft, OldRight, NewLeft> LeftBifunctorMap<OldLeft, OldRight, NewLeft> fmapLeft(
Function<OldLeft, NewLeft> func);
/**
- * Lift a function to operate over the right part of this pair
+ * Lift a function to operate over the right part of this pair.
*
* @param <OldLeft>
- * The old left type of the pair
+ * The old left type of the pair.
+ *
* @param <OldRight>
- * The old right type of the pair
+ * The old right type of the pair.
+ *
* @param <NewRight>
- * The new right type of the pair
+ * The new right type of the pair.
+ *
* @param func
- * The function to lift to work over the right side of
- * the pair
- * @return The function lifted to work over the right side of bifunctors
+ * The function to lift to work over the right side of
+ * the pair.
+ *
+ * @return
+ * The function lifted to work over the right side of bifunctors.
*/
public <OldLeft, OldRight, NewRight> RightBifunctorMap<OldLeft, OldRight, NewRight> fmapRight(
Function<OldRight, NewRight> func);
/**
- * Get the value contained on the left of this bifunctor
+ * Get the value contained on the left of this bifunctor.
*
- * @return The value on the left side of this bifunctor
+ * @return
+ * The value on the left side of this bifunctor.
*/
public LeftType getLeft();
/**
- * Get the value contained on the right of this bifunctor
+ * Get the value contained on the right of this bifunctor.
*
- * @return The value on the right of this bifunctor
+ * @return
+ * The value on the right of this bifunctor.
*/
public RightType getRight();
}
diff --git a/base/src/main/java/bjc/utils/funcdata/theory/Functor.java b/base/src/main/java/bjc/utils/funcdata/theory/Functor.java
index 1c53284..9efa883 100644
--- a/base/src/main/java/bjc/utils/funcdata/theory/Functor.java
+++ b/base/src/main/java/bjc/utils/funcdata/theory/Functor.java
@@ -4,36 +4,42 @@ import java.util.function.Function;
/**
* Represents a container or context some sort usually, but the precise
- * definition is that it represents exactly what it is defined as
+ * definition is that it represents exactly what it is defined as.
*
* @author ben
+ *
* @param <ContainedType>
- * The value inside the functor
+ * The value inside the functor.
*/
public interface Functor<ContainedType> {
/**
- * Converts a normal function to operate over values in a functor.
+ * Converts a normal function to operate over values in a functor..
*
* N.B: Even though the type signature implies that you can apply the
* resulting function to any type of functor, it is only safe to call it
- * on instances of the type of functor you called fmap on.
+ * on instances of the type of functor you called fmap on..
*
* @param <ArgType>
- * The argument of the function
+ * The argument of the function.
+ *
* @param <ReturnType>
- * The return type of the function
+ * The return type of the function.
+ *
* @param func
- * The function to convert
- * @return The passed in function converted to work over a particular
- * type of functors
+ * The function to convert.
+ *
+ * @return
+ * The passed in function converted to work over a particular
+ * type of functors.
*/
public <ArgType, ReturnType> Function<Functor<ArgType>, Functor<ReturnType>> fmap(
Function<ArgType, ReturnType> func);
/**
- * Retrieve the thing inside this functor
+ * Retrieve the thing inside this functor.
*
- * @return The thing inside this functor
+ * @return
+ * The thing inside this functor.
*/
public ContainedType getValue();
}