summaryrefslogtreecommitdiff
path: root/BJC-Utils2/src/main/java/bjc/utils/funcutils
diff options
context:
space:
mode:
authorbjculkin <bjculkin@mix.wvu.edu>2017-04-07 16:06:18 -0400
committerbjculkin <bjculkin@mix.wvu.edu>2017-04-07 16:06:18 -0400
commitf4baa925b0b5590bc8b12ba5f32e0218384c8efc (patch)
tree6c8f0eceaeaabc807e8cfd17e13bba86b8981970 /BJC-Utils2/src/main/java/bjc/utils/funcutils
parent7692fa077a84972231948354d3f0de99f27a9ad7 (diff)
Add simple toggle values
Diffstat (limited to 'BJC-Utils2/src/main/java/bjc/utils/funcutils')
-rw-r--r--BJC-Utils2/src/main/java/bjc/utils/funcutils/ListUtils.java92
1 files changed, 26 insertions, 66 deletions
diff --git a/BJC-Utils2/src/main/java/bjc/utils/funcutils/ListUtils.java b/BJC-Utils2/src/main/java/bjc/utils/funcutils/ListUtils.java
index 27666dd..ad0f565 100644
--- a/BJC-Utils2/src/main/java/bjc/utils/funcutils/ListUtils.java
+++ b/BJC-Utils2/src/main/java/bjc/utils/funcutils/ListUtils.java
@@ -1,11 +1,9 @@
package bjc.utils.funcutils;
-import bjc.utils.data.IPair;
import bjc.utils.funcdata.FunctionalList;
import bjc.utils.funcdata.IList;
import java.util.ArrayList;
-import java.util.Deque;
import java.util.Iterator;
import java.util.function.Function;
import java.util.function.Supplier;
@@ -29,40 +27,38 @@ public class ListUtils {
* @return The collapsed string of tokens
*/
public static String collapseTokens(IList<String> input) {
- if (input == null)
- throw new NullPointerException("Input must not be null");
+ if(input == null) throw new NullPointerException("Input must not be null");
return collapseTokens(input, "");
}
/**
* Collapse a string of tokens into a single string, adding the desired
- * seperator after each token
+ * separator after each token
*
* @param input
* The list of tokens to collapse
* @param seperator
- * The seperator to use for seperating tokens
+ * The separator to use for separating tokens
* @return The collapsed string of tokens
*/
public static String collapseTokens(IList<String> input, String seperator) {
- if (input == null)
+ if(input == null)
throw new NullPointerException("Input must not be null");
- else if (seperator == null)
- throw new NullPointerException("Seperator must not be null");
+ else if(seperator == null) throw new NullPointerException("Seperator must not be null");
- if (input.getSize() < 1)
+ if(input.getSize() < 1)
return "";
- else if (input.getSize() == 1)
+ else if(input.getSize() == 1)
return input.first();
else {
StringBuilder state = new StringBuilder();
int i = 1;
- for (String itm : input.toIterable()) {
+ for(String itm : input.toIterable()) {
state.append(itm);
- if (i != input.getSize()) {
+ if(i != input.getSize()) {
state.append(seperator);
}
@@ -74,22 +70,6 @@ public class ListUtils {
}
/**
- * Split off affixes from tokens
- *
- * @param input
- * The tokens to deaffix
- * @param operators
- * The affixes to remove
- * @return The tokens that have been deaffixed
- *
- * @deprecated Replaced by SimpleTokenSplitter.
- */
- @Deprecated
- public static IList<String> deAffixTokens(IList<String> input, Deque<IPair<String, String>> operators) {
- return null;
- }
-
- /**
* Select a number of random items from the list without replacement
*
* @param <E>
@@ -113,7 +93,7 @@ public class ListUtils {
Iterator<E> itr = list.toIterable().iterator();
E element = null;
- for (int index = 0; itr.hasNext(); element = itr.next()) {
+ for(int index = 0; itr.hasNext(); element = itr.next()) {
// n - m
int winningChance = number - selected.getSize();
@@ -121,7 +101,7 @@ public class ListUtils {
int totalChance = total - (index - 1);
// Probability of selecting the t+1'th element
- if (NumberUtils.isProbable(winningChance, totalChance, rng)) {
+ if(NumberUtils.isProbable(winningChance, totalChance, rng)) {
selected.add(element);
}
}
@@ -147,7 +127,7 @@ public class ListUtils {
public static <E> IList<E> drawWithReplacement(IList<E> list, int number, Function<Integer, Integer> rng) {
IList<E> selected = new FunctionalList<>(new ArrayList<>(number));
- for (int i = 0; i < number; i++) {
+ for(int i = 0; i < number; i++) {
selected.add(list.randItem(rng));
}
@@ -173,11 +153,11 @@ public class ListUtils {
*/
public static <E> IList<IList<E>> groupPartition(IList<E> input, Function<E, Integer> counter,
int partitionSize) {
- if (input == null)
+ if(input == null)
throw new NullPointerException("Input list must not be null");
- else if (counter == null)
+ else if(counter == null)
throw new NullPointerException("Counter must not be null");
- else if (partitionSize < 1 || partitionSize > input.getSize()) {
+ else if(partitionSize < 1 || partitionSize > input.getSize()) {
String fmt = "%d is not a valid partition size. Must be between 1 and %d";
String msg = String.format(fmt, partitionSize, input.getSize());
@@ -199,13 +179,13 @@ public class ListUtils {
/*
* Run up to a certain number of passes
*/
- for (int numberOfIterations = 0; numberOfIterations < MAX_NTRIESPART
+ for(int numberOfIterations = 0; numberOfIterations < MAX_NTRIESPART
&& !rejected.isEmpty(); numberOfIterations++) {
input.forEach(it);
- if (rejected.isEmpty()) // Nothing was rejected, so
- // we're
- // done
+ if(rejected.isEmpty()) // Nothing was rejected, so
+ // we're
+ // done
return returned;
}
@@ -229,8 +209,8 @@ public class ListUtils {
public static <E> IList<E> mergeLists(IList<E>... lists) {
IList<E> returned = new FunctionalList<>();
- for (IList<E> list : lists) {
- for (E itm : list.toIterable()) {
+ for(IList<E> list : lists) {
+ for(E itm : list.toIterable()) {
returned.add(itm);
}
}
@@ -260,22 +240,22 @@ public class ListUtils {
IList<E> returned = new FunctionalList<>();
- for (E itm : list.toIterable()) {
+ for(E itm : list.toIterable()) {
count += counter.apply(itm);
returned.add(itm);
}
- if (count % size != 0) {
+ if(count % size != 0) {
// We need to pad
int needed = count % size;
int threshold = 0;
- while (needed > 0 && threshold <= MAX_NTRIESPART) {
+ while(needed > 0 && threshold <= MAX_NTRIESPART) {
E val = padder.get();
int newCount = counter.apply(val);
- if (newCount <= needed) {
+ if(newCount <= needed) {
returned.add(val);
threshold = 0;
@@ -286,31 +266,11 @@ public class ListUtils {
}
}
- if (threshold > MAX_NTRIESPART)
+ if(threshold > MAX_NTRIESPART)
throw new IllegalArgumentException("Heuristic (more than " + MAX_NTRIESPART
+ " iterations of attempting to pad) detected unpaddable list ");
}
return returned;
}
-
- /**
- * Split tokens in a list of tokens into multiple tokens.
- *
- * The intended use is for expression parsers so that you can enter
- * something like 1+1 instead of 1 + 1.
- *
- * @param input
- * The tokens to split
- * @param operators
- * Pairs of operators to split on and regexes that match
- * those operators
- * @return A list of tokens split on all the operators
- *
- * @deprecated Use SimpleTokenSplitter now
- */
- @Deprecated
- public static IList<String> splitTokens(IList<String> input, Deque<IPair<String, String>> operators) {
- return null;
- }
}