From ba0a3f53a88842a94ea6a1a6d45f61416b593e47 Mon Sep 17 00:00:00 2001 From: bculkin2442 Date: Wed, 28 Sep 2016 07:54:53 -0400 Subject: Update various things. --- .../main/java/bjc/utils/funcutils/ListUtils.java | 43 ++++++++++++++++++++++ 1 file changed, 43 insertions(+) (limited to 'BJC-Utils2/src/main/java/bjc/utils/funcutils') 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 f09a794..d65603b 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/funcutils/ListUtils.java +++ b/BJC-Utils2/src/main/java/bjc/utils/funcutils/ListUtils.java @@ -3,6 +3,7 @@ package bjc.utils.funcutils; import java.util.ArrayList; import java.util.Deque; import java.util.function.Function; +import java.util.function.Supplier; import bjc.utils.data.IHolder; import bjc.utils.data.IPair; @@ -175,6 +176,7 @@ public class ListUtils { * The function to determine the count for each element for * @param numberPerPartition * The number of elements to put in each partition + * * @return A list partitioned according to the above rules */ public static IList> groupPartition(IList input, @@ -294,4 +296,45 @@ public class ListUtils { return returnedList.getValue(); } + + public static IList padList(IList list, + Function counter, int size, + Supplier padSource) { + IHolder countHolder = new Identity<>(0); + + IList ret = list.map((elm) -> { + countHolder.map((val) -> val + counter.apply(elm)); + + return elm; + }); + + if (countHolder.unwrap((val) -> val % size != 0)) { + // We need to pad + int needed = countHolder.unwrap((val) -> val % size); + int threshold = 0; + + while (needed > 0 && threshold <= MAX_NTRIESPART) { + E val = padSource.get(); + int count = counter.apply(val); + + if (count <= needed) { + ret.add(val); + + threshold = 0; + + needed -= count; + } else { + threshold += 1; + } + } + + if (threshold > MAX_NTRIESPART) { + throw new IllegalArgumentException("Heuristic (more than " + + MAX_NTRIESPART + + " iterations of attempting to pad) detected unpaddable list "); + } + } + + return ret; + } } \ No newline at end of file -- cgit v1.2.3