From c41cfde634d70c3a50adf3979cc5239e5ae52e73 Mon Sep 17 00:00:00 2001 From: Ben Culkin Date: Thu, 12 Nov 2020 19:40:40 -0500 Subject: Move GroupPartIteration into ListUtils --- .../main/java/bjc/utils/funcutils/ListUtils.java | 82 +++++++++++++++++++++- 1 file changed, 80 insertions(+), 2 deletions(-) (limited to 'base/src/main/java/bjc/utils/funcutils/ListUtils.java') 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 + * The type of element in the list being partitioned + */ +class GroupPartIteration implements Consumer { + /* The list we're returning. */ + private final IList> returnedList; + + /* The current partition of the list. */ + public IList currentPartition; + /* The items rejected from the current partition. */ + private final IList 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 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> returned, final IList rejects, + final int nPerPart, final Function 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; + } + } + } +} -- cgit v1.2.3