From 61fd71f69e080790da722e0e03b71ecd7c2538a2 Mon Sep 17 00:00:00 2001 From: bculkin2442 Date: Tue, 10 May 2016 16:02:45 -0400 Subject: General update --- .../main/java/bjc/utils/funcdata/ExtendedMap.java | 18 +- .../java/bjc/utils/funcdata/FunctionalList.java | 42 +-- .../java/bjc/utils/funcdata/FunctionalMap.java | 12 +- .../utils/funcdata/FunctionalStringTokenizer.java | 6 +- .../java/bjc/utils/funcdata/IFunctionalList.java | 350 --------------------- .../java/bjc/utils/funcdata/IFunctionalMap.java | 137 -------- .../src/main/java/bjc/utils/funcdata/IList.java | 350 +++++++++++++++++++++ .../src/main/java/bjc/utils/funcdata/IMap.java | 137 ++++++++ .../src/main/java/bjc/utils/funcdata/ITree.java | 2 +- .../main/java/bjc/utils/funcdata/PushdownMap.java | 10 +- .../bjc/utils/funcdata/TransformedValueMap.java | 14 +- .../src/main/java/bjc/utils/funcdata/Tree.java | 16 +- .../bjc/utils/funcdata/bst/BinarySearchTree.java | 6 +- 13 files changed, 550 insertions(+), 550 deletions(-) delete mode 100644 BJC-Utils2/src/main/java/bjc/utils/funcdata/IFunctionalList.java delete mode 100644 BJC-Utils2/src/main/java/bjc/utils/funcdata/IFunctionalMap.java create mode 100644 BJC-Utils2/src/main/java/bjc/utils/funcdata/IList.java create mode 100644 BJC-Utils2/src/main/java/bjc/utils/funcdata/IMap.java (limited to 'BJC-Utils2/src/main/java/bjc/utils/funcdata') diff --git a/BJC-Utils2/src/main/java/bjc/utils/funcdata/ExtendedMap.java b/BJC-Utils2/src/main/java/bjc/utils/funcdata/ExtendedMap.java index 9776961..378a4a0 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/funcdata/ExtendedMap.java +++ b/BJC-Utils2/src/main/java/bjc/utils/funcdata/ExtendedMap.java @@ -7,13 +7,13 @@ import java.util.function.Function; import bjc.utils.funcutils.ListUtils; class ExtendedMap - implements IFunctionalMap { - private IFunctionalMap delegate; + implements IMap { + private IMap delegate; - private IFunctionalMap store; + private IMap store; - public ExtendedMap(IFunctionalMap delegate, - IFunctionalMap store) { + public ExtendedMap(IMap delegate, + IMap store) { this.delegate = delegate; this.store = store; } @@ -28,7 +28,7 @@ class ExtendedMap } @Override - public IFunctionalMap extend() { + public IMap extend() { return new ExtendedMap<>(this, new FunctionalMap<>()); } @@ -68,12 +68,12 @@ class ExtendedMap } @Override - public IFunctionalList keyList() { + public IList keyList() { return ListUtils.mergeLists(store.keyList(), delegate.keyList()); } @Override - public IFunctionalMap mapValues( + public IMap mapValues( Function transformer) { return new TransformedValueMap<>(this, transformer); } @@ -89,7 +89,7 @@ class ExtendedMap } @Override - public IFunctionalList valueList() { + public IList valueList() { return ListUtils.mergeLists(store.valueList(), delegate.valueList()); } 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 8579693..638ad7e 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/funcdata/FunctionalList.java +++ b/BJC-Utils2/src/main/java/bjc/utils/funcdata/FunctionalList.java @@ -27,7 +27,7 @@ import bjc.utils.data.Pair; * @param * The type in this list */ -public class FunctionalList implements Cloneable, IFunctionalList { +public class FunctionalList implements Cloneable, IList { /** * The list used as a backing store */ @@ -148,8 +148,8 @@ public class FunctionalList implements Cloneable, IFunctionalList { * @return A list */ @Override - public IFunctionalList clone() { - IFunctionalList clonedList = new FunctionalList<>(); + public IList clone() { + IList clonedList = new FunctionalList<>(); for (E element : wrappedList) { clonedList.add(element); @@ -166,8 +166,8 @@ public class FunctionalList implements Cloneable, IFunctionalList { * IFunctionalList, java.util.function.BiFunction) */ @Override - public IFunctionalList combineWith( - IFunctionalList rightList, + public IList combineWith( + IList rightList, BiFunction itemCombiner) { if (rightList == null) { throw new NullPointerException( @@ -176,7 +176,7 @@ public class FunctionalList implements Cloneable, IFunctionalList { throw new NullPointerException("Combiner must not be null"); } - IFunctionalList returnedList = new FunctionalList<>(); + IList returnedList = new FunctionalList<>(); // Get the iterator for the other list Iterator rightIterator = rightList.toIterable().iterator(); @@ -227,17 +227,17 @@ public class FunctionalList implements Cloneable, IFunctionalList { * Function) */ @Override - public IFunctionalList flatMap( - Function> elementExpander) { + public IList flatMap( + Function> elementExpander) { if (elementExpander == null) { throw new NullPointerException("Expander must not be null"); } - IFunctionalList returnedList = new FunctionalList<>( + IList returnedList = new FunctionalList<>( this.wrappedList.size()); forEach(element -> { - IFunctionalList expandedElement = elementExpander + IList expandedElement = elementExpander .apply(element); if (expandedElement == null) { @@ -320,12 +320,12 @@ public class FunctionalList implements Cloneable, IFunctionalList { * Predicate) */ @Override - public IFunctionalList getMatching(Predicate matchPredicate) { + public IList getMatching(Predicate matchPredicate) { if (matchPredicate == null) { throw new NullPointerException("Predicate must not be null"); } - IFunctionalList returnedList = new FunctionalList<>(); + IList returnedList = new FunctionalList<>(); wrappedList.forEach((element) -> { if (matchPredicate.test(element)) { @@ -361,7 +361,7 @@ public class FunctionalList implements Cloneable, IFunctionalList { * Check if a partition has room for another item */ private Boolean isPartitionFull(int numberPerPartition, - IHolder> currentPartition) { + IHolder> currentPartition) { return currentPartition.unwrap( (partition) -> partition.getSize() >= numberPerPartition); } @@ -373,12 +373,12 @@ public class FunctionalList implements Cloneable, IFunctionalList { * bjc.utils.funcdata.IFunctionalList#map(java.util.function.Function) */ @Override - public IFunctionalList map(Function elementTransformer) { + public IList map(Function elementTransformer) { if (elementTransformer == null) { throw new NullPointerException("Transformer must be not null"); } - IFunctionalList returnedList = new FunctionalList<>( + IList returnedList = new FunctionalList<>( this.wrappedList.size()); forEach(element -> { @@ -396,8 +396,8 @@ public class FunctionalList implements Cloneable, IFunctionalList { * IFunctionalList) */ @Override - public IFunctionalList> pairWith( - IFunctionalList rightList) { + public IList> pairWith( + IList rightList) { return combineWith(rightList, Pair::new); } @@ -407,7 +407,7 @@ public class FunctionalList implements Cloneable, IFunctionalList { * @see bjc.utils.funcdata.IFunctionalList#partition(int) */ @Override - public IFunctionalList> partition( + public IList> partition( int numberPerPartition) { if (numberPerPartition < 1 || numberPerPartition > wrappedList.size()) { @@ -416,10 +416,10 @@ public class FunctionalList implements Cloneable, IFunctionalList { + wrappedList.size()); } - IFunctionalList> returnedList = new FunctionalList<>(); + IList> returnedList = new FunctionalList<>(); // The current partition being filled - IHolder> currentPartition = new Identity<>( + IHolder> currentPartition = new Identity<>( new FunctionalList<>()); this.forEach((element) -> { @@ -559,7 +559,7 @@ public class FunctionalList implements Cloneable, IFunctionalList { } @Override - public IFunctionalList tail() { + public IList tail() { return new FunctionalList<>(wrappedList.subList(1, getSize())); } diff --git a/BJC-Utils2/src/main/java/bjc/utils/funcdata/FunctionalMap.java b/BJC-Utils2/src/main/java/bjc/utils/funcdata/FunctionalMap.java index fb8bb81..0002a58 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/funcdata/FunctionalMap.java +++ b/BJC-Utils2/src/main/java/bjc/utils/funcdata/FunctionalMap.java @@ -9,7 +9,7 @@ import java.util.function.Function; import bjc.utils.data.IPair; /** - * Basic implementation of {@link IFunctionalMap} + * Basic implementation of {@link IMap} * * @author ben * @@ -19,7 +19,7 @@ import bjc.utils.data.IPair; * The type of the map's values */ public class FunctionalMap - implements IFunctionalMap { + implements IMap { private Map wrappedMap; /** @@ -71,7 +71,7 @@ public class FunctionalMap } @Override - public IFunctionalMap extend() { + public IMap extend() { return new ExtendedMap<>(this, new FunctionalMap<>()); } @@ -115,7 +115,7 @@ public class FunctionalMap } @Override - public IFunctionalList keyList() { + public IList keyList() { FunctionalList keys = new FunctionalList<>(); wrappedMap.keySet().forEach((key) -> { @@ -132,7 +132,7 @@ public class FunctionalMap * Function) */ @Override - public IFunctionalMap mapValues( + public IMap mapValues( Function transformer) { if (transformer == null) { throw new NullPointerException("Transformer must not be null"); @@ -166,7 +166,7 @@ public class FunctionalMap } @Override - public IFunctionalList valueList() { + public IList valueList() { FunctionalList values = new FunctionalList<>(); wrappedMap.values().forEach((value) -> { 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 aa6fca3..18617d1 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/funcdata/FunctionalStringTokenizer.java +++ b/BJC-Utils2/src/main/java/bjc/utils/funcdata/FunctionalStringTokenizer.java @@ -140,7 +140,7 @@ public class FunctionalStringTokenizer { * * @return This tokenizer, converted into a list of strings */ - public IFunctionalList toList() { + public IList toList() { return toList((String element) -> element); } @@ -155,13 +155,13 @@ public class FunctionalStringTokenizer { * The function to use to convert tokens. * @return A list containing all of the converted tokens. */ - public IFunctionalList toList( + public IList toList( Function tokenTransformer) { if (tokenTransformer == null) { throw new NullPointerException("Transformer must not be null"); } - IFunctionalList returnList = new FunctionalList<>(); + IList returnList = new FunctionalList<>(); // Add each token to the list after transforming it forEachToken(token -> { diff --git a/BJC-Utils2/src/main/java/bjc/utils/funcdata/IFunctionalList.java b/BJC-Utils2/src/main/java/bjc/utils/funcdata/IFunctionalList.java deleted file mode 100644 index 5327dbe..0000000 --- a/BJC-Utils2/src/main/java/bjc/utils/funcdata/IFunctionalList.java +++ /dev/null @@ -1,350 +0,0 @@ -package bjc.utils.funcdata; - -import java.util.Comparator; -import java.util.function.BiConsumer; -import java.util.function.BiFunction; -import java.util.function.Consumer; -import java.util.function.Function; -import java.util.function.Predicate; - -import bjc.utils.data.IPair; - -/** - * A wrapper over another list that provides eager functional operations - * over it. Differs from a stream in every way except for the fact that - * they both provide functional operations. - * - * NOTE: The indications of complexity for methods assume that the backing - * list has performance on the order of an array - * - * @author ben - * - * @param - * The type in this list - */ -public interface IFunctionalList { - - /** - * Add an item to this list - * - * Takes O(1) time. - * - * @param item - * The item to add to this list. - * @return Whether the item was added to the list succesfully. - */ - boolean add(ContainedType item); - - /** - * Check if all of the elements of this list match the specified - * predicate. - * - * Takes O(f * p(n)) time on average, where f is defined as the average - * number of elements in a list until the predicate returns false, and - * p is the average running time of the predicate - * - * @param matchPredicate - * The predicate to use for checking. - * @return Whether all of the elements of the list match the specified - * predicate. - */ - boolean allMatch(Predicate matchPredicate); - - /** - * Check if any of the elements in this list match the specified list. - * - * Takes O(f * p(n)) time on average, where f is defined as the average - * number of elements in a list until the predicate returns true, and p - * is the average running time of the predicate - * - * @param matchPredicate - * The predicate to use for checking. - * @return Whether any element in the list matches the provided - * predicate. - */ - boolean anyMatch(Predicate matchPredicate); - - /** - * 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. - * Does not change the underlying list. - * - * NOTE: The returned list will have the length of the shorter of this - * list and the combined one. - * - * Takes O(q * c(q)), where q is defined as the length of the shorter - * of this list and the provided one, and c is the running time of the - * combiner. - * - * @param - * The type of the second list - * @param - * The type of the combined list - * - * @param rightList - * The list to combine with - * @param itemCombiner - * The function to use for combining element pairs. - * @return A new list containing the merged pairs of lists. - */ - IFunctionalList combineWith( - IFunctionalList rightList, - BiFunction itemCombiner); - - /** - * Check if the list contains the specified item - * - * Takes O(n) time, assuming object compare in constant time. - * - * @param item - * The item to see if it is contained - * @return Whether or not the specified item is in the list - */ - boolean contains(ContainedType item); - - /** - * Get the first element in the list - * - * Takes O(1) time - * - * @return The first element in this list. - */ - ContainedType first(); - - /** - * Apply a function to each member of the list, then flatten the - * results. Does not change the underlying list. - * - * Takes O(n * m) time, where m is the average number of elements in - * the returned list. - * - * @param - * The type of the flattened list - * - * @param elementExpander - * The function to apply to each member of the list. - * @return A new list containing the flattened results of applying the - * provided function. - */ - IFunctionalList flatMap( - Function> elementExpander); - - /** - * Apply a given action for each member of the list - * - * Takes O(n * f(n)) time, where n is the length of the list, and f is - * the running time of the action. - * - * @param action - * The action to apply to each member of the list. - */ - void forEach(Consumer action); - - /** - * Apply a given function to each element in the list and its index. - * - * Takes O(n * f(n)) time, where n is the length of the list, and f is - * the running time of the action. - * - * @param indexedAction - * The function to apply to each element in the list and its - * index. - */ - void forEachIndexed(BiConsumer indexedAction); - - /** - * Retrieve a value in the list by its index. - * - * Takes O(1) time. - * - * @param index - * The index to retrieve a value from. - * @return The value at the specified index in the list. - */ - ContainedType getByIndex(int index); - - /** - * Retrieve a list containing all elements matching a predicate - * - * Takes O(n) time, where n is the number of elements in the list - * - * @param matchPredicate - * The predicate to match by - * @return A list containing all elements that match the predicate - */ - IFunctionalList getMatching( - Predicate matchPredicate); - - /** - * Retrieve the size of the wrapped list - * - * @return The size of the wrapped list - */ - int getSize(); - - /** - * Check if this list is empty. - * - * @return Whether or not this list is empty. - */ - boolean isEmpty(); - - /** - * Create a new list by applying the given function to each element in - * the list. Does not change the underlying list. - * - * @param - * The type of the transformed list - * - * @param elementTransformer - * The function to apply to each element in the list - * @return A new list containing the mapped elements of this list. - */ - IFunctionalList map( - Function elementTransformer); - - /** - * Zip two lists into a list of pairs - * - * @param - * The type of the second list - * - * @param rightList - * The list to use as the left side of the pair - * @return A list containing pairs of this element and the specified - * list - */ - IFunctionalList> pairWith( - IFunctionalList rightList); - - /** - * Partition this list into a list of sublists - * - * @param numberPerPartition - * The size of elements to put into each one of the sublists - * @return A list partitioned into partitions of size nPerPart - */ - IFunctionalList> partition( - int numberPerPartition); - - /** - * Prepend an item to the list - * - * @param item - * The item to prepend to the list - */ - void prepend(ContainedType item); - - /** - * Select a random item from this list, using the provided random - * number generator. - * - * @param rnd - * The random number generator to use. - * @return A random element from this list. - */ - ContainedType randItem(Function rnd); - - /** - * Select a random item from the list, using a default random number - * generator - * - * @return A random item from the list - */ - default ContainedType randItem() { - return randItem((num) -> (int) (Math.random() * num)); - } - - /** - * Reduce this list to a single value, using a accumulative approach. - * - * @param - * The in-between type of the values - * @param - * The final value type - * - * @param initialValue - * The initial value of the accumulative state. - * @param stateAccumulator - * The function to use to combine a list element with the - * accumulative state. - * @param resultTransformer - * The function to use to convert the accumulative state - * into a final result. - * @return A single value condensed from this list and transformed into - * its final state. - */ - ReducedType reduceAux(StateType initialValue, - BiFunction stateAccumulator, - Function resultTransformer); - - /** - * Remove all elements that match a given predicate - * - * @param removePredicate - * The predicate to use to determine elements to delete - * @return Whether there was anything that satisfied the predicate - */ - boolean removeIf(Predicate removePredicate); - - /** - * Remove all parameters that match a given parameter - * - * @param desiredElement - * The object to remove all matching copies of - */ - void removeMatching(ContainedType desiredElement); - - /** - * Reverse the contents of this list in place - */ - void reverse(); - - /** - * Perform a binary search for the specified key using the provided - * means of comparing elements. Since this IS a binary search, the list - * must have been sorted before hand. - * - * @param searchKey - * The key to search for. - * @param comparator - * The way to compare elements for searching. Pass null to - * use the natural ordering for E - * @return The element if it is in this list, or null if it is not. - */ - ContainedType search(ContainedType searchKey, - Comparator comparator); - - /** - * Sort the elements of this list using the provided way of comparing - * elements. Does change the underlying list. - * - * @param comparator - * The way to compare elements for sorting. Pass null to use - * E's natural ordering - */ - void sort(Comparator comparator); - - /** - * Get the tail of this list (the list without the first element - * - * @return The list without the first element - */ - public IFunctionalList tail(); - - /** - * Convert this list into an array - * - * @param arrType - * The type of array to return - * @return The list, as an array - */ - ContainedType[] toArray(ContainedType[] arrType); - - /** - * Convert the list into a iterable - * - * @return An iterable view onto the list - */ - Iterable toIterable(); -} \ No newline at end of file diff --git a/BJC-Utils2/src/main/java/bjc/utils/funcdata/IFunctionalMap.java b/BJC-Utils2/src/main/java/bjc/utils/funcdata/IFunctionalMap.java deleted file mode 100644 index 8a54246..0000000 --- a/BJC-Utils2/src/main/java/bjc/utils/funcdata/IFunctionalMap.java +++ /dev/null @@ -1,137 +0,0 @@ -package bjc.utils.funcdata; - -import java.util.function.BiConsumer; -import java.util.function.Consumer; -import java.util.function.Function; - -/** - * Functional wrapper over map providing some useful things - * - * @author ben - * - * @param - * The type of this map's keys - * @param - * The type of this map's values - * - */ -public interface IFunctionalMap { - - /** - * Check if this map contains the specified key - * - * @param key - * The key to check - * @return Whether or not the map contains the key - */ - boolean containsKey(KeyType key); - - /** - * Extends this map, creating a new map that will delegate queries to - * the map, but store any added values itself - * - * @return An extended map - */ - IFunctionalMap extend(); - - /** - * Execute an action for each entry in the map - * - * @param action - * the action to execute for each entry in the map - */ - void forEach(BiConsumer action); - - /** - * Perform an action for each key in the map - * - * @param action - * The action to perform on each key in the map - */ - void forEachKey(Consumer action); - - /** - * Perform an action for each value in the map - * - * @param action - * The action to perform on each value in the map - */ - void forEachValue(Consumer action); - - /** - * Get the value assigned to the given key - * - * @param key - * The key to look for a value under - * @return The value of the key - * - * - */ - ValueType get(KeyType key); - - /** - * Get the number of entries in this map - * - * @return The number of entries in this map - */ - int getSize(); - - /** - * Get a list of all the keys in this map - * - * @return A list of all the keys in this map - */ - IFunctionalList keyList(); - - /** - * Transform the values returned by this map. - * - * NOTE: This transform is applied once for each lookup of a value, so - * the transform passed should be a proper function, or things will - * likely not work as expected. - * - * @param - * The new type of returned values - * @param transformer - * The function to use to transform values - * @return The map where each value will be transformed after lookup - */ - IFunctionalMap mapValues( - Function transformer); - - /** - * Add an entry to the map - * - * @param key - * The key to put the value under - * @param val - * The value to add - * @return The previous value of the key in the map, or null if the key - * wasn't in the map. However, note that it may also return - * null if the key was set to null. - * - * @throws UnsupportedOperationException - * if the map implementation doesn't support modifying the - * map - */ - ValueType put(KeyType key, ValueType val); - - /** - * Remove the value bound to the key - * - * @param key - * The key to remove from the map - * @return The previous value for the key in the map, or null if the - * key wasn't in the class. NOTE: Just because you recieved - * null, doesn't mean the map wasn't changed. It may mean that - * someone put a null value for that key into the map - */ - ValueType remove(KeyType key); - - /** - * Get a list of the values in this map - * - * @return A list of values in this map - */ - IFunctionalList valueList(); -} \ No newline at end of file diff --git a/BJC-Utils2/src/main/java/bjc/utils/funcdata/IList.java b/BJC-Utils2/src/main/java/bjc/utils/funcdata/IList.java new file mode 100644 index 0000000..c45551e --- /dev/null +++ b/BJC-Utils2/src/main/java/bjc/utils/funcdata/IList.java @@ -0,0 +1,350 @@ +package bjc.utils.funcdata; + +import java.util.Comparator; +import java.util.function.BiConsumer; +import java.util.function.BiFunction; +import java.util.function.Consumer; +import java.util.function.Function; +import java.util.function.Predicate; + +import bjc.utils.data.IPair; + +/** + * A wrapper over another list that provides eager functional operations + * over it. Differs from a stream in every way except for the fact that + * they both provide functional operations. + * + * NOTE: The indications of complexity for methods assume that the backing + * list has performance on the order of an array + * + * @author ben + * + * @param + * The type in this list + */ +public interface IList { + + /** + * Add an item to this list + * + * Takes O(1) time. + * + * @param item + * The item to add to this list. + * @return Whether the item was added to the list succesfully. + */ + boolean add(ContainedType item); + + /** + * Check if all of the elements of this list match the specified + * predicate. + * + * Takes O(f * p(n)) time on average, where f is defined as the average + * number of elements in a list until the predicate returns false, and + * p is the average running time of the predicate + * + * @param matchPredicate + * The predicate to use for checking. + * @return Whether all of the elements of the list match the specified + * predicate. + */ + boolean allMatch(Predicate matchPredicate); + + /** + * Check if any of the elements in this list match the specified list. + * + * Takes O(f * p(n)) time on average, where f is defined as the average + * number of elements in a list until the predicate returns true, and p + * is the average running time of the predicate + * + * @param matchPredicate + * The predicate to use for checking. + * @return Whether any element in the list matches the provided + * predicate. + */ + boolean anyMatch(Predicate matchPredicate); + + /** + * 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. + * Does not change the underlying list. + * + * NOTE: The returned list will have the length of the shorter of this + * list and the combined one. + * + * Takes O(q * c(q)), where q is defined as the length of the shorter + * of this list and the provided one, and c is the running time of the + * combiner. + * + * @param + * The type of the second list + * @param + * The type of the combined list + * + * @param rightList + * The list to combine with + * @param itemCombiner + * The function to use for combining element pairs. + * @return A new list containing the merged pairs of lists. + */ + IList combineWith( + IList rightList, + BiFunction itemCombiner); + + /** + * Check if the list contains the specified item + * + * Takes O(n) time, assuming object compare in constant time. + * + * @param item + * The item to see if it is contained + * @return Whether or not the specified item is in the list + */ + boolean contains(ContainedType item); + + /** + * Get the first element in the list + * + * Takes O(1) time + * + * @return The first element in this list. + */ + ContainedType first(); + + /** + * Apply a function to each member of the list, then flatten the + * results. Does not change the underlying list. + * + * Takes O(n * m) time, where m is the average number of elements in + * the returned list. + * + * @param + * The type of the flattened list + * + * @param elementExpander + * The function to apply to each member of the list. + * @return A new list containing the flattened results of applying the + * provided function. + */ + IList flatMap( + Function> elementExpander); + + /** + * Apply a given action for each member of the list + * + * Takes O(n * f(n)) time, where n is the length of the list, and f is + * the running time of the action. + * + * @param action + * The action to apply to each member of the list. + */ + void forEach(Consumer action); + + /** + * Apply a given function to each element in the list and its index. + * + * Takes O(n * f(n)) time, where n is the length of the list, and f is + * the running time of the action. + * + * @param indexedAction + * The function to apply to each element in the list and its + * index. + */ + void forEachIndexed(BiConsumer indexedAction); + + /** + * Retrieve a value in the list by its index. + * + * Takes O(1) time. + * + * @param index + * The index to retrieve a value from. + * @return The value at the specified index in the list. + */ + ContainedType getByIndex(int index); + + /** + * Retrieve a list containing all elements matching a predicate + * + * Takes O(n) time, where n is the number of elements in the list + * + * @param matchPredicate + * The predicate to match by + * @return A list containing all elements that match the predicate + */ + IList getMatching( + Predicate matchPredicate); + + /** + * Retrieve the size of the wrapped list + * + * @return The size of the wrapped list + */ + int getSize(); + + /** + * Check if this list is empty. + * + * @return Whether or not this list is empty. + */ + boolean isEmpty(); + + /** + * Create a new list by applying the given function to each element in + * the list. Does not change the underlying list. + * + * @param + * The type of the transformed list + * + * @param elementTransformer + * The function to apply to each element in the list + * @return A new list containing the mapped elements of this list. + */ + IList map( + Function elementTransformer); + + /** + * Zip two lists into a list of pairs + * + * @param + * The type of the second list + * + * @param rightList + * The list to use as the left side of the pair + * @return A list containing pairs of this element and the specified + * list + */ + IList> pairWith( + IList rightList); + + /** + * Partition this list into a list of sublists + * + * @param numberPerPartition + * The size of elements to put into each one of the sublists + * @return A list partitioned into partitions of size nPerPart + */ + IList> partition( + int numberPerPartition); + + /** + * Prepend an item to the list + * + * @param item + * The item to prepend to the list + */ + void prepend(ContainedType item); + + /** + * Select a random item from this list, using the provided random + * number generator. + * + * @param rnd + * The random number generator to use. + * @return A random element from this list. + */ + ContainedType randItem(Function rnd); + + /** + * Select a random item from the list, using a default random number + * generator + * + * @return A random item from the list + */ + default ContainedType randItem() { + return randItem((num) -> (int) (Math.random() * num)); + } + + /** + * Reduce this list to a single value, using a accumulative approach. + * + * @param + * The in-between type of the values + * @param + * The final value type + * + * @param initialValue + * The initial value of the accumulative state. + * @param stateAccumulator + * The function to use to combine a list element with the + * accumulative state. + * @param resultTransformer + * The function to use to convert the accumulative state + * into a final result. + * @return A single value condensed from this list and transformed into + * its final state. + */ + ReducedType reduceAux(StateType initialValue, + BiFunction stateAccumulator, + Function resultTransformer); + + /** + * Remove all elements that match a given predicate + * + * @param removePredicate + * The predicate to use to determine elements to delete + * @return Whether there was anything that satisfied the predicate + */ + boolean removeIf(Predicate removePredicate); + + /** + * Remove all parameters that match a given parameter + * + * @param desiredElement + * The object to remove all matching copies of + */ + void removeMatching(ContainedType desiredElement); + + /** + * Reverse the contents of this list in place + */ + void reverse(); + + /** + * Perform a binary search for the specified key using the provided + * means of comparing elements. Since this IS a binary search, the list + * must have been sorted before hand. + * + * @param searchKey + * The key to search for. + * @param comparator + * The way to compare elements for searching. Pass null to + * use the natural ordering for E + * @return The element if it is in this list, or null if it is not. + */ + ContainedType search(ContainedType searchKey, + Comparator comparator); + + /** + * Sort the elements of this list using the provided way of comparing + * elements. Does change the underlying list. + * + * @param comparator + * The way to compare elements for sorting. Pass null to use + * E's natural ordering + */ + void sort(Comparator comparator); + + /** + * Get the tail of this list (the list without the first element + * + * @return The list without the first element + */ + public IList tail(); + + /** + * Convert this list into an array + * + * @param arrType + * The type of array to return + * @return The list, as an array + */ + ContainedType[] toArray(ContainedType[] arrType); + + /** + * Convert the list into a iterable + * + * @return An iterable view onto the list + */ + Iterable toIterable(); +} \ No newline at end of file diff --git a/BJC-Utils2/src/main/java/bjc/utils/funcdata/IMap.java b/BJC-Utils2/src/main/java/bjc/utils/funcdata/IMap.java new file mode 100644 index 0000000..f5f7a26 --- /dev/null +++ b/BJC-Utils2/src/main/java/bjc/utils/funcdata/IMap.java @@ -0,0 +1,137 @@ +package bjc.utils.funcdata; + +import java.util.function.BiConsumer; +import java.util.function.Consumer; +import java.util.function.Function; + +/** + * Functional wrapper over map providing some useful things + * + * @author ben + * + * @param + * The type of this map's keys + * @param + * The type of this map's values + * + */ +public interface IMap { + + /** + * Check if this map contains the specified key + * + * @param key + * The key to check + * @return Whether or not the map contains the key + */ + boolean containsKey(KeyType key); + + /** + * Extends this map, creating a new map that will delegate queries to + * the map, but store any added values itself + * + * @return An extended map + */ + IMap extend(); + + /** + * Execute an action for each entry in the map + * + * @param action + * the action to execute for each entry in the map + */ + void forEach(BiConsumer action); + + /** + * Perform an action for each key in the map + * + * @param action + * The action to perform on each key in the map + */ + void forEachKey(Consumer action); + + /** + * Perform an action for each value in the map + * + * @param action + * The action to perform on each value in the map + */ + void forEachValue(Consumer action); + + /** + * Get the value assigned to the given key + * + * @param key + * The key to look for a value under + * @return The value of the key + * + * + */ + ValueType get(KeyType key); + + /** + * Get the number of entries in this map + * + * @return The number of entries in this map + */ + int getSize(); + + /** + * Get a list of all the keys in this map + * + * @return A list of all the keys in this map + */ + IList keyList(); + + /** + * Transform the values returned by this map. + * + * NOTE: This transform is applied once for each lookup of a value, so + * the transform passed should be a proper function, or things will + * likely not work as expected. + * + * @param + * The new type of returned values + * @param transformer + * The function to use to transform values + * @return The map where each value will be transformed after lookup + */ + IMap mapValues( + Function transformer); + + /** + * Add an entry to the map + * + * @param key + * The key to put the value under + * @param val + * The value to add + * @return The previous value of the key in the map, or null if the key + * wasn't in the map. However, note that it may also return + * null if the key was set to null. + * + * @throws UnsupportedOperationException + * if the map implementation doesn't support modifying the + * map + */ + ValueType put(KeyType key, ValueType val); + + /** + * Remove the value bound to the key + * + * @param key + * The key to remove from the map + * @return The previous value for the key in the map, or null if the + * key wasn't in the class. NOTE: Just because you recieved + * null, doesn't mean the map wasn't changed. It may mean that + * someone put a null value for that key into the map + */ + ValueType remove(KeyType key); + + /** + * Get a list of the values in this map + * + * @return A list of values in this map + */ + IList valueList(); +} \ No newline at end of file diff --git a/BJC-Utils2/src/main/java/bjc/utils/funcdata/ITree.java b/BJC-Utils2/src/main/java/bjc/utils/funcdata/ITree.java index 026f3f8..e0c63e3 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/funcdata/ITree.java +++ b/BJC-Utils2/src/main/java/bjc/utils/funcdata/ITree.java @@ -43,7 +43,7 @@ public interface ITree { */ public ReturnedType collapse( Function leafTransform, - Function, NewType>> nodeCollapser, + Function, NewType>> nodeCollapser, Function resultTransformer); /** diff --git a/BJC-Utils2/src/main/java/bjc/utils/funcdata/PushdownMap.java b/BJC-Utils2/src/main/java/bjc/utils/funcdata/PushdownMap.java index cc31923..ed9aa63 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/funcdata/PushdownMap.java +++ b/BJC-Utils2/src/main/java/bjc/utils/funcdata/PushdownMap.java @@ -13,7 +13,7 @@ import java.util.function.Function; * @param */ public class PushdownMap - implements IFunctionalMap { + implements IMap { @Override public boolean containsKey(KeyType key) { @@ -22,7 +22,7 @@ public class PushdownMap } @Override - public IFunctionalMap extend() { + public IMap extend() { // TODO Auto-generated method stub return null; } @@ -58,13 +58,13 @@ public class PushdownMap } @Override - public IFunctionalList keyList() { + public IList keyList() { // TODO Auto-generated method stub return null; } @Override - public IFunctionalMap mapValues( + public IMap mapValues( Function transformer) { // TODO Auto-generated method stub return null; @@ -83,7 +83,7 @@ public class PushdownMap } @Override - public IFunctionalList valueList() { + public IList valueList() { // TODO Auto-generated method stub return null; } diff --git a/BJC-Utils2/src/main/java/bjc/utils/funcdata/TransformedValueMap.java b/BJC-Utils2/src/main/java/bjc/utils/funcdata/TransformedValueMap.java index 1d52d82..cf54935 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/funcdata/TransformedValueMap.java +++ b/BJC-Utils2/src/main/java/bjc/utils/funcdata/TransformedValueMap.java @@ -17,11 +17,11 @@ import java.util.function.Function; * The type of the transformed values */ final class TransformedValueMap - implements IFunctionalMap { - private IFunctionalMap mapToTransform; + implements IMap { + private IMap mapToTransform; private Function transformer; - public TransformedValueMap(IFunctionalMap destMap, + public TransformedValueMap(IMap destMap, Function transform) { mapToTransform = destMap; transformer = transform; @@ -33,7 +33,7 @@ final class TransformedValueMap } @Override - public IFunctionalMap extend() { + public IMap extend() { return new ExtendedMap<>(this, new FunctionalMap<>()); } @@ -67,12 +67,12 @@ final class TransformedValueMap } @Override - public IFunctionalList keyList() { + public IList keyList() { return mapToTransform.keyList(); } @Override - public IFunctionalMap mapValues( + public IMap mapValues( Function transform) { return new TransformedValueMap<>(this, transform); } @@ -94,7 +94,7 @@ final class TransformedValueMap } @Override - public IFunctionalList valueList() { + public IList valueList() { return mapToTransform.valueList().map(transformer); } } \ No newline at end of file diff --git a/BJC-Utils2/src/main/java/bjc/utils/funcdata/Tree.java b/BJC-Utils2/src/main/java/bjc/utils/funcdata/Tree.java index 4ddcd45..34b70d5 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/funcdata/Tree.java +++ b/BJC-Utils2/src/main/java/bjc/utils/funcdata/Tree.java @@ -17,7 +17,7 @@ import bjc.utils.funcutils.StringUtils; */ public class Tree implements ITree { private ContainedType data; - private IFunctionalList> children; + private IList> children; private boolean hasChildren; @@ -44,7 +44,7 @@ public class Tree implements ITree { * A list of children for this node */ public Tree(ContainedType leafToken, - IFunctionalList> childrn) { + IList> childrn) { data = leafToken; hasChildren = true; @@ -95,7 +95,7 @@ public class Tree implements ITree { @Override public ReturnedType collapse( Function leafTransform, - Function, NewType>> nodeCollapser, + Function, NewType>> nodeCollapser, Function resultTransformer) { return resultTransformer @@ -129,12 +129,12 @@ public class Tree implements ITree { protected NewType internalCollapse( Function leafTransform, - Function, NewType>> nodeCollapser) { + Function, NewType>> nodeCollapser) { if (hasChildren) { - Function, NewType> nodeTransformer = nodeCollapser + Function, NewType> nodeTransformer = nodeCollapser .apply(data); - IFunctionalList collapsedChildren = children + IList collapsedChildren = children .map((child) -> { return child.collapse(leafTransform, nodeCollapser, (subTreeVal) -> subTreeVal); @@ -169,7 +169,7 @@ public class Tree implements ITree { Function leafTransformer, Function operatorTransformer) { if (hasChildren) { - IFunctionalList> mappedChildren = children + IList> mappedChildren = children .map((child) -> { return child.rebuildTree(leafTransformer, operatorTransformer); @@ -279,7 +279,7 @@ public class Tree implements ITree { public ITree transformTree( Function transformer) { if (hasChildren) { - IFunctionalList> transformedChildren = children + IList> transformedChildren = children .map((child) -> child.transformTree(transformer)); return new Tree<>(transformer.apply(data), 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 712f0e3..9cfc9a4 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 @@ -6,7 +6,7 @@ import java.util.List; import java.util.function.Predicate; import bjc.utils.funcdata.FunctionalList; -import bjc.utils.funcdata.IFunctionalList; +import bjc.utils.funcdata.IList; /** * A binary search tree, with some mild support for functional traversal. @@ -74,7 +74,7 @@ public class BinarySearchTree { * The distance from the pivot * @return Whether the adjusted pivot is with the list */ - private boolean adjustedPivotInBounds(IFunctionalList elements, + private boolean adjustedPivotInBounds(IList elements, int pivot, int pivotAdjustment) { return (pivot - pivotAdjustment) >= 0 && (pivot + pivotAdjustment) < elements.getSize(); @@ -85,7 +85,7 @@ public class BinarySearchTree { * time, but also O(N) space. */ public void balance() { - IFunctionalList elements = new FunctionalList<>(); + IList elements = new FunctionalList<>(); // Add each element to the list in sorted order rootElement.forEach(TreeLinearizationMethod.INORDER, -- cgit v1.2.3