[COLLECTIONS-800] Adding partitionBalanced(List,int) method#265
Open
ClaudioConsolmagno wants to merge 1 commit intoapache:masterfrom
Open
[COLLECTIONS-800] Adding partitionBalanced(List,int) method#265ClaudioConsolmagno wants to merge 1 commit intoapache:masterfrom
ClaudioConsolmagno wants to merge 1 commit intoapache:masterfrom
Conversation
LarsBodewig
reviewed
Dec 18, 2021
| // when index hasn't crossed the threshold we don't need balancing yet | ||
| if (currentPartitionSize > 1 && threshold > 0 && index >= threshold) { | ||
| start -= (index - threshold); // adjust start as partitions before threshold have one less element | ||
| currentPartitionSize--; // currentPartitionSize is decremented as threshold is crossed |
There was a problem hiding this comment.
Newbie myself, but I wonder if that could be written more simply:
evenlyDistributedSize = ...
if (index < threshold) { // lists before threshold should contain +1
start = index * (evenlyDistributedSize + 1);
end = start + (evenlyDistributedSize + 1);
} else {
start = index * evenlyDistributedSize + threshold; // offset other starting positions by threshold
end = start + evenlyDistributedSize;
}...or similar (Note: this is untested). The if condition and subtractions look quite complicated in my opinion.
LarsBodewig
reviewed
Dec 18, 2021
| * list, partitioned in a way to balance entries across all sublists. For example, | ||
| * partitioning a list containing {@code [a, b, c, d, e]} with a partition | ||
| * size of 3 yields {@code [[a, b, c], [d, e]]} -- an outer list containing | ||
| * two inner lists of three and two elements, all in the original order. Partitioning |
There was a problem hiding this comment.
I see why you would use the example from ListUtils.partition, however this could be confusing IMO if you start comparing what method to use and see no difference so far. I would opt for a more clear example here.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Returns consecutive sublists of a list, partitioned in a way to balance entries across all sublists. See COLLECTIONS-800 for more details.
Implementation Notes:
Partitionclass is a private static class not used by anything else I think it's ok to just use isBalanced as a boolean instead of, say, an enum or having a separate brand new private class for this new method.partition()method.forEachtest cases (I'm a fan of Spock and data driven testing).