summaryrefslogtreecommitdiff
path: root/BJC-Utils2/src/main/java/bjc/utils/funcdata
diff options
context:
space:
mode:
Diffstat (limited to 'BJC-Utils2/src/main/java/bjc/utils/funcdata')
-rw-r--r--BJC-Utils2/src/main/java/bjc/utils/funcdata/FunctionalList.java172
-rw-r--r--BJC-Utils2/src/main/java/bjc/utils/funcdata/FunctionalMap.java22
-rw-r--r--BJC-Utils2/src/main/java/bjc/utils/funcdata/IFunctionalList.java47
-rw-r--r--BJC-Utils2/src/main/java/bjc/utils/funcdata/IFunctionalMap.java7
4 files changed, 195 insertions, 53 deletions
diff --git a/BJC-Utils2/src/main/java/bjc/utils/funcdata/FunctionalList.java b/BJC-Utils2/src/main/java/bjc/utils/funcdata/FunctionalList.java
index 9574376..9b388b2 100644
--- a/BJC-Utils2/src/main/java/bjc/utils/funcdata/FunctionalList.java
+++ b/BJC-Utils2/src/main/java/bjc/utils/funcdata/FunctionalList.java
@@ -5,17 +5,16 @@ import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.NoSuchElementException;
-import java.util.Random;
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.GenHolder;
-import bjc.utils.data.IHolder;
-import bjc.utils.data.IPair;
-import bjc.utils.data.Pair;
+import bjc.utils.data.experimental.IHolder;
+import bjc.utils.data.experimental.IPair;
+import bjc.utils.data.experimental.Identity;
+import bjc.utils.data.experimental.Pair;
import java.util.ArrayList;
@@ -45,6 +44,8 @@ public class FunctionalList<E> implements Cloneable, IFunctionalList<E> {
/**
* Create a new functional list containing the specified items.
*
+ * Takes O(n) time, where n is the number of items specified
+ *
* @param items
* The items to put into this functional list.
*/
@@ -70,6 +71,8 @@ public class FunctionalList<E> implements Cloneable, IFunctionalList<E> {
/**
* Create a new functional list as a wrapper of a existing list.
*
+ * Takes O(1) time, since it doesn't copy the list.
+ *
* @param backingList
* The list to use as a backing list.
*/
@@ -82,7 +85,9 @@ public class FunctionalList<E> implements Cloneable, IFunctionalList<E> {
wrappedList = backingList;
}
- /* (non-Javadoc)
+ /*
+ * (non-Javadoc)
+ *
* @see bjc.utils.funcdata.IFunctionalList#add(E)
*/
@Override
@@ -90,8 +95,11 @@ public class FunctionalList<E> implements Cloneable, IFunctionalList<E> {
return wrappedList.add(item);
}
- /* (non-Javadoc)
- * @see bjc.utils.funcdata.IFunctionalList#allMatch(java.util.function.Predicate)
+ /*
+ * (non-Javadoc)
+ *
+ * @see bjc.utils.funcdata.IFunctionalList#allMatch(java.util.function.
+ * Predicate)
*/
@Override
public boolean allMatch(Predicate<E> matchPredicate) {
@@ -110,8 +118,11 @@ public class FunctionalList<E> implements Cloneable, IFunctionalList<E> {
return true;
}
- /* (non-Javadoc)
- * @see bjc.utils.funcdata.IFunctionalList#anyMatch(java.util.function.Predicate)
+ /*
+ * (non-Javadoc)
+ *
+ * @see bjc.utils.funcdata.IFunctionalList#anyMatch(java.util.function.
+ * Predicate)
*/
@Override
public boolean anyMatch(Predicate<E> matchPredicate) {
@@ -130,6 +141,13 @@ public class FunctionalList<E> implements Cloneable, IFunctionalList<E> {
return false;
}
+ /**
+ * 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
+ *
+ * @return A list
+ */
@Override
public IFunctionalList<E> clone() {
IFunctionalList<E> clonedList = new FunctionalList<>();
@@ -141,8 +159,12 @@ public class FunctionalList<E> implements Cloneable, IFunctionalList<E> {
return clonedList;
}
- /* (non-Javadoc)
- * @see bjc.utils.funcdata.IFunctionalList#combineWith(bjc.utils.funcdata.IFunctionalList, java.util.function.BiFunction)
+ /*
+ * (non-Javadoc)
+ *
+ * @see
+ * bjc.utils.funcdata.IFunctionalList#combineWith(bjc.utils.funcdata.
+ * IFunctionalList, java.util.function.BiFunction)
*/
@Override
public <T, F> IFunctionalList<F> combineWith(
@@ -173,7 +195,9 @@ public class FunctionalList<E> implements Cloneable, IFunctionalList<E> {
return returnedList;
}
- /* (non-Javadoc)
+ /*
+ * (non-Javadoc)
+ *
* @see bjc.utils.funcdata.IFunctionalList#contains(E)
*/
@Override
@@ -182,7 +206,9 @@ public class FunctionalList<E> implements Cloneable, IFunctionalList<E> {
return this.anyMatch(item::equals);
}
- /* (non-Javadoc)
+ /*
+ * (non-Javadoc)
+ *
* @see bjc.utils.funcdata.IFunctionalList#first()
*/
@Override
@@ -195,8 +221,11 @@ public class FunctionalList<E> implements Cloneable, IFunctionalList<E> {
return wrappedList.get(0);
}
- /* (non-Javadoc)
- * @see bjc.utils.funcdata.IFunctionalList#flatMap(java.util.function.Function)
+ /*
+ * (non-Javadoc)
+ *
+ * @see bjc.utils.funcdata.IFunctionalList#flatMap(java.util.function.
+ * Function)
*/
@Override
public <T> IFunctionalList<T> flatMap(
@@ -224,8 +253,11 @@ public class FunctionalList<E> implements Cloneable, IFunctionalList<E> {
return returnedList;
}
- /* (non-Javadoc)
- * @see bjc.utils.funcdata.IFunctionalList#forEach(java.util.function.Consumer)
+ /*
+ * (non-Javadoc)
+ *
+ * @see bjc.utils.funcdata.IFunctionalList#forEach(java.util.function.
+ * Consumer)
*/
@Override
public void forEach(Consumer<E> action) {
@@ -236,8 +268,12 @@ public class FunctionalList<E> implements Cloneable, IFunctionalList<E> {
wrappedList.forEach(action);
}
- /* (non-Javadoc)
- * @see bjc.utils.funcdata.IFunctionalList#forEachIndexed(java.util.function.BiConsumer)
+ /*
+ * (non-Javadoc)
+ *
+ * @see
+ * bjc.utils.funcdata.IFunctionalList#forEachIndexed(java.util.function
+ * .BiConsumer)
*/
@Override
public void forEachIndexed(BiConsumer<Integer, E> indexedAction) {
@@ -246,7 +282,7 @@ public class FunctionalList<E> implements Cloneable, IFunctionalList<E> {
}
// This is held b/c ref'd variables must be final/effectively final
- GenHolder<Integer> currentIndex = new GenHolder<>(0);
+ IHolder<Integer> currentIndex = new Identity<>(0);
wrappedList.forEach((element) -> {
// Call the action with the index and the value
@@ -258,7 +294,9 @@ public class FunctionalList<E> implements Cloneable, IFunctionalList<E> {
});
}
- /* (non-Javadoc)
+ /*
+ * (non-Javadoc)
+ *
* @see bjc.utils.funcdata.IFunctionalList#getByIndex(int)
*/
@Override
@@ -275,8 +313,12 @@ public class FunctionalList<E> implements Cloneable, IFunctionalList<E> {
return wrappedList;
}
- /* (non-Javadoc)
- * @see bjc.utils.funcdata.IFunctionalList#getMatching(java.util.function.Predicate)
+ /*
+ * (non-Javadoc)
+ *
+ * @see
+ * bjc.utils.funcdata.IFunctionalList#getMatching(java.util.function.
+ * Predicate)
*/
@Override
public IFunctionalList<E> getMatching(Predicate<E> matchPredicate) {
@@ -296,7 +338,9 @@ public class FunctionalList<E> implements Cloneable, IFunctionalList<E> {
return returnedList;
}
- /* (non-Javadoc)
+ /*
+ * (non-Javadoc)
+ *
* @see bjc.utils.funcdata.IFunctionalList#getSize()
*/
@Override
@@ -304,7 +348,9 @@ public class FunctionalList<E> implements Cloneable, IFunctionalList<E> {
return wrappedList.size();
}
- /* (non-Javadoc)
+ /*
+ * (non-Javadoc)
+ *
* @see bjc.utils.funcdata.IFunctionalList#isEmpty()
*/
@Override
@@ -312,8 +358,11 @@ public class FunctionalList<E> implements Cloneable, IFunctionalList<E> {
return wrappedList.isEmpty();
}
- /* (non-Javadoc)
- * @see bjc.utils.funcdata.IFunctionalList#map(java.util.function.Function)
+ /*
+ * (non-Javadoc)
+ *
+ * @see
+ * bjc.utils.funcdata.IFunctionalList#map(java.util.function.Function)
*/
@Override
public <T> IFunctionalList<T> map(Function<E, T> elementTransformer) {
@@ -332,8 +381,11 @@ public class FunctionalList<E> implements Cloneable, IFunctionalList<E> {
return returnedList;
}
- /* (non-Javadoc)
- * @see bjc.utils.funcdata.IFunctionalList#pairWith(bjc.utils.funcdata.IFunctionalList)
+ /*
+ * (non-Javadoc)
+ *
+ * @see bjc.utils.funcdata.IFunctionalList#pairWith(bjc.utils.funcdata.
+ * IFunctionalList)
*/
@Override
public <T> IFunctionalList<IPair<E, T>> pairWith(
@@ -341,7 +393,9 @@ public class FunctionalList<E> implements Cloneable, IFunctionalList<E> {
return combineWith(rightList, Pair<E, T>::new);
}
- /* (non-Javadoc)
+ /*
+ * (non-Javadoc)
+ *
* @see bjc.utils.funcdata.IFunctionalList#partition(int)
*/
@Override
@@ -357,7 +411,7 @@ public class FunctionalList<E> implements Cloneable, IFunctionalList<E> {
IFunctionalList<IFunctionalList<E>> returnedList = new FunctionalList<>();
// The current partition being filled
- GenHolder<IFunctionalList<E>> currentPartition = new GenHolder<>(
+ IHolder<IFunctionalList<E>> currentPartition = new Identity<>(
new FunctionalList<>());
this.forEach((element) -> {
@@ -383,12 +437,14 @@ public class FunctionalList<E> implements Cloneable, IFunctionalList<E> {
* Check if a partition has room for another item
*/
private Boolean isPartitionFull(int numberPerPartition,
- GenHolder<IFunctionalList<E>> currentPartition) {
+ IHolder<IFunctionalList<E>> currentPartition) {
return currentPartition.unwrap(
(partition) -> partition.getSize() >= numberPerPartition);
}
- /* (non-Javadoc)
+ /*
+ * (non-Javadoc)
+ *
* @see bjc.utils.funcdata.IFunctionalList#prepend(E)
*/
@Override
@@ -396,23 +452,28 @@ public class FunctionalList<E> implements Cloneable, IFunctionalList<E> {
wrappedList.add(0, item);
}
- /* (non-Javadoc)
+ /*
+ * (non-Javadoc)
+ *
* @see bjc.utils.funcdata.IFunctionalList#randItem(java.util.Random)
*/
@Override
- public E randItem(Random rnd) {
+ public E randItem(Function<Integer, Integer> rnd) {
if (rnd == null) {
throw new NullPointerException(
"Random source must not be null");
}
- int randomIndex = rnd.nextInt(wrappedList.size());
+ int randomIndex = rnd.apply(wrappedList.size());
return wrappedList.get(randomIndex);
}
- /* (non-Javadoc)
- * @see bjc.utils.funcdata.IFunctionalList#reduceAux(T, java.util.function.BiFunction, java.util.function.Function)
+ /*
+ * (non-Javadoc)
+ *
+ * @see bjc.utils.funcdata.IFunctionalList#reduceAux(T,
+ * java.util.function.BiFunction, java.util.function.Function)
*/
@Override
public <T, F> F reduceAux(T initialValue,
@@ -425,7 +486,7 @@ public class FunctionalList<E> implements Cloneable, IFunctionalList<E> {
}
// The current collapsed list
- IHolder<T> currentState = new GenHolder<>(initialValue);
+ IHolder<T> currentState = new Identity<>(initialValue);
wrappedList.forEach(element -> {
// Accumulate a new value into the state
@@ -437,8 +498,11 @@ public class FunctionalList<E> implements Cloneable, IFunctionalList<E> {
return currentState.unwrap(resultTransformer);
}
- /* (non-Javadoc)
- * @see bjc.utils.funcdata.IFunctionalList#removeIf(java.util.function.Predicate)
+ /*
+ * (non-Javadoc)
+ *
+ * @see bjc.utils.funcdata.IFunctionalList#removeIf(java.util.function.
+ * Predicate)
*/
@Override
public boolean removeIf(Predicate<E> removePredicate) {
@@ -449,7 +513,9 @@ public class FunctionalList<E> implements Cloneable, IFunctionalList<E> {
return wrappedList.removeIf(removePredicate);
}
- /* (non-Javadoc)
+ /*
+ * (non-Javadoc)
+ *
* @see bjc.utils.funcdata.IFunctionalList#removeMatching(E)
*/
@Override
@@ -457,8 +523,11 @@ public class FunctionalList<E> implements Cloneable, IFunctionalList<E> {
removeIf((element) -> element.equals(desiredElement));
}
- /* (non-Javadoc)
- * @see bjc.utils.funcdata.IFunctionalList#search(E, java.util.Comparator)
+ /*
+ * (non-Javadoc)
+ *
+ * @see bjc.utils.funcdata.IFunctionalList#search(E,
+ * java.util.Comparator)
*/
@Override
public E search(E searchKey, Comparator<E> comparator) {
@@ -475,7 +544,9 @@ public class FunctionalList<E> implements Cloneable, IFunctionalList<E> {
return null;
}
- /* (non-Javadoc)
+ /*
+ * (non-Javadoc)
+ *
* @see bjc.utils.funcdata.IFunctionalList#sort(java.util.Comparator)
*/
@Override
@@ -483,7 +554,9 @@ public class FunctionalList<E> implements Cloneable, IFunctionalList<E> {
Collections.sort(wrappedList, comparator);
}
- /* (non-Javadoc)
+ /*
+ * (non-Javadoc)
+ *
* @see bjc.utils.funcdata.IFunctionalList#toIterable()
*/
@Override
@@ -512,4 +585,9 @@ public class FunctionalList<E> implements Cloneable, IFunctionalList<E> {
return sb.toString();
}
+
+ @Override
+ public E[] toArray(E[] arrType) {
+ return wrappedList.toArray(arrType);
+ }
} \ No newline at end of file
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 b505ebd..da30064 100644
--- a/BJC-Utils2/src/main/java/bjc/utils/funcdata/FunctionalMap.java
+++ b/BJC-Utils2/src/main/java/bjc/utils/funcdata/FunctionalMap.java
@@ -6,7 +6,7 @@ import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.function.Function;
-import bjc.utils.data.Pair;
+import bjc.utils.data.experimental.IPair;
/**
* Basic implementation of {@link IFunctionalMap}
@@ -102,6 +102,11 @@ public class FunctionalMap<K, V> implements IFunctionalMap<K, V> {
action.accept(transformer.apply(val));
});
}
+
+ @Override
+ public IFunctionalList<V2> valueList() {
+ return mapToTransform.valueList().map(transformer);
+ }
}
private Map<K, V> wrappedMap;
@@ -134,10 +139,10 @@ public class FunctionalMap<K, V> implements IFunctionalMap<K, V> {
* The entries to put into the map
*/
@SafeVarargs
- public FunctionalMap(Pair<K, V>... entries) {
+ public FunctionalMap(IPair<K, V>... entries) {
this();
- for (Pair<K, V> entry : entries) {
+ for (IPair<K, V> entry : entries) {
entry.doWith((key, val) -> {
wrappedMap.put(key, val);
});
@@ -243,4 +248,15 @@ public class FunctionalMap<K, V> implements IFunctionalMap<K, V> {
public void forEachValue(Consumer<V> action) {
wrappedMap.values().forEach(action);
}
+
+ @Override
+ public IFunctionalList<V> valueList() {
+ FunctionalList<V> values = new FunctionalList<>();
+
+ wrappedMap.values().forEach((value) -> {
+ values.add(value);
+ });
+
+ return values;
+ }
} \ No newline at end of file
diff --git a/BJC-Utils2/src/main/java/bjc/utils/funcdata/IFunctionalList.java b/BJC-Utils2/src/main/java/bjc/utils/funcdata/IFunctionalList.java
index a017aa9..949fc33 100644
--- a/BJC-Utils2/src/main/java/bjc/utils/funcdata/IFunctionalList.java
+++ b/BJC-Utils2/src/main/java/bjc/utils/funcdata/IFunctionalList.java
@@ -1,20 +1,22 @@
package bjc.utils.funcdata;
import java.util.Comparator;
-import java.util.Random;
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;
+import bjc.utils.data.experimental.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 <E>
@@ -25,6 +27,8 @@ public interface IFunctionalList<E> {
/**
* 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.
@@ -35,6 +39,10 @@ public interface IFunctionalList<E> {
* 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
@@ -45,6 +53,10 @@ public interface IFunctionalList<E> {
/**
* 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
@@ -60,6 +72,10 @@ public interface IFunctionalList<E> {
* 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 <T>
* The type of the second list
* @param <F>
@@ -77,6 +93,8 @@ public interface IFunctionalList<E> {
/**
* 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
@@ -86,6 +104,8 @@ public interface IFunctionalList<E> {
/**
* Get the first element in the list
*
+ * Takes O(1) time
+ *
* @return The first element in this list.
*/
E first();
@@ -94,6 +114,9 @@ public interface IFunctionalList<E> {
* 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 <T>
* The type of the flattened list
*
@@ -108,6 +131,9 @@ public interface IFunctionalList<E> {
/**
* 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.
*/
@@ -116,6 +142,9 @@ public interface IFunctionalList<E> {
/**
* 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.
@@ -125,6 +154,8 @@ public interface IFunctionalList<E> {
/**
* 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.
@@ -134,6 +165,7 @@ public interface IFunctionalList<E> {
/**
* 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
@@ -206,7 +238,7 @@ public interface IFunctionalList<E> {
* The random number generator to use.
* @return A random element from this list.
*/
- E randItem(Random rnd);
+ E randItem(Function<Integer, Integer> rnd);
/**
* Reduce this list to a single value, using a accumulative approach.
@@ -278,4 +310,13 @@ public interface IFunctionalList<E> {
* @return An iterable view onto the list
*/
Iterable<E> toIterable();
+
+ /**
+ * Convert this list into an array
+ *
+ * @param arrType
+ * The type of array to return
+ * @return The list, as an array
+ */
+ E[] toArray(E[] arrType);
} \ 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
index e089850..c879229 100644
--- a/BJC-Utils2/src/main/java/bjc/utils/funcdata/IFunctionalMap.java
+++ b/BJC-Utils2/src/main/java/bjc/utils/funcdata/IFunctionalMap.java
@@ -118,4 +118,11 @@ public interface IFunctionalMap<K, V> {
* The action to perform on each value in the map
*/
void forEachValue(Consumer<V> action);
+
+ /**
+ * Get a list of the values in this map
+ *
+ * @return A list of values in this map
+ */
+ IFunctionalList<V> valueList();
} \ No newline at end of file