diff options
| author | Ben Culkin <scorpress@gmail.com> | 2020-11-12 19:40:40 -0500 |
|---|---|---|
| committer | Ben Culkin <scorpress@gmail.com> | 2020-11-12 19:40:40 -0500 |
| commit | c41cfde634d70c3a50adf3979cc5239e5ae52e73 (patch) | |
| tree | 0c385feb14091d0e40ece32289a7d591be9d9dd1 /base/src/main/java/bjc/utils/funcutils/ListUtils.java | |
| parent | bc356a1556cf82b4d29b8713fb3d5dbe65dc514a (diff) | |
Move GroupPartIteration into ListUtils
Diffstat (limited to 'base/src/main/java/bjc/utils/funcutils/ListUtils.java')
| -rw-r--r-- | base/src/main/java/bjc/utils/funcutils/ListUtils.java | 82 |
1 files changed, 80 insertions, 2 deletions
diff --git a/base/src/main/java/bjc/utils/funcutils/ListUtils.java b/base/src/main/java/bjc/utils/funcutils/ListUtils.java index f689d6c..672a12d 100644 --- a/base/src/main/java/bjc/utils/funcutils/ListUtils.java +++ b/base/src/main/java/bjc/utils/funcutils/ListUtils.java @@ -4,8 +4,7 @@ import java.util.ArrayList; import java.util.Iterator; import java.util.LinkedList; import java.util.List; -import java.util.function.Function; -import java.util.function.Supplier; +import java.util.function.*; import bjc.funcdata.FunctionalList; import bjc.funcdata.IList; @@ -441,3 +440,82 @@ public class ListUtils { list.set(b, tmp); } } + +/** + * Implements a single group partitioning pass on a list. + * + * @author ben + * + * @param <E> + * The type of element in the list being partitioned + */ +class GroupPartIteration<E> implements Consumer<E> { + /* The list we're returning. */ + private final IList<IList<E>> returnedList; + + /* The current partition of the list. */ + public IList<E> currentPartition; + /* The items rejected from the current partition. */ + private final IList<E> rejectedItems; + + /* The number of items in the current partition. */ + private int numberInCurrentPartition; + /* The number of items in each partition. */ + private final int numberPerPartition; + + /* The function to use to count an item. */ + private final Function<E, Integer> elementCounter; + + /** + * Create a new group partitioning iteration. + * + * @param returned + * The list containing all of the existing partitions. + * + * @param rejects + * The items that have been rejected from a partition for being + * too large. + * + * @param nPerPart + * The combined value of items that should go into each + * partition. + * + * @param eleCount + * The function to use to determine the value of an item. + */ + public GroupPartIteration(final IList<IList<E>> returned, final IList<E> rejects, + final int nPerPart, final Function<E, Integer> eleCount) { + this.returnedList = returned; + this.rejectedItems = rejects; + this.numberPerPartition = nPerPart; + this.elementCounter = eleCount; + + this.currentPartition = new FunctionalList<>(); + this.numberInCurrentPartition = 0; + } + + @Override + public void accept(final E value) { + final boolean shouldStartPartition + = numberInCurrentPartition >= numberPerPartition; + + if (shouldStartPartition) { + returnedList.add(currentPartition); + + currentPartition = new FunctionalList<>(); + numberInCurrentPartition = 0; + } else { + final int currentElementCount = elementCounter.apply(value); + + final boolean shouldReject = (numberInCurrentPartition + currentElementCount) + >= numberPerPartition; + + if (shouldReject) { + rejectedItems.add(value); + } else { + currentPartition.add(value); + numberInCurrentPartition += currentElementCount; + } + } + } +} |
