blob: 681d7072d32bd58bc0960410a56c041fb5cba799 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
|
package bjc.utils.funcutils;
import java.util.function.Consumer;
import java.util.function.Function;
import bjc.funcdata.FunctionalList;
import bjc.funcdata.IList;
/**
* Implements a single group partitioning pass on a list.
*
* @author ben
*
* @param <E>
* The type of element in the list being partitioned
*/
final 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;
}
}
}
}
|