[
https://issues.apache.org/jira/browse/COLLECTIONS-800?focusedWorklogId=698182&page=com.atlassian.jira.plugin.system.issuetabpanels:worklog-tabpanel#worklog-698182
]
ASF GitHub Bot logged work on COLLECTIONS-800:
----------------------------------------------
Author: ASF GitHub Bot
Created on: 18/Dec/21 02:19
Start Date: 18/Dec/21 02:19
Worklog Time Spent: 10m
Work Description: LarsBodewig commented on a change in pull request #265:
URL:
https://github.com/apache/commons-collections/pull/265#discussion_r771769961
##########
File path: src/main/java/org/apache/commons/collections4/ListUtils.java
##########
@@ -117,9 +119,25 @@ private Partition(final List<T> list, final int size) {
throw new IndexOutOfBoundsException("Index " + index + " must
be less than size " +
listSize);
}
- final int start = index * size;
- final int end = Math.min(start + size, list.size());
- return list.subList(start, end);
+ int start;
+ int currentPartitionSize;
+ if (isBalanced) {
+ // evenly distribute partitions
+ currentPartitionSize = (int) Math.ceil((double) list.size() /
(double) listSize);
+ start = index * currentPartitionSize;
+ // remainder of above is the threshold for which indices
greater than will have one less element
+ final int threshold = (list.size() % listSize);
+ // when currentPartitionSize is 1 or threshold is 0 there's
nothing to balance
+ // 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
Review comment:
Newbie myself, but I wonder if that could be written more simply:
```java
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.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
Issue Time Tracking
-------------------
Worklog Id: (was: 698182)
Time Spent: 40m (was: 0.5h)
> Provide a ListUtils.partitionBalanced(List, int) method where returned
> sublists are "balanced"
> ----------------------------------------------------------------------------------------------
>
> Key: COLLECTIONS-800
> URL: https://issues.apache.org/jira/browse/COLLECTIONS-800
> Project: Commons Collections
> Issue Type: New Feature
> Components: List
> Reporter: Claudio
> Priority: Minor
> Time Spent: 40m
> Remaining Estimate: 0h
>
> [https://github.com/apache/commons-collections/pull/265]
> The new method re-uses some of the logic of the existing
> ListUtils.partition(List, int). The difference with that method is that the
> returned sublists are "balanced" so that they all have the same amount of
> elements (with a maximum of 1 element difference). Some examples:
> A list of 10 elements, partition 10:
> * partition() and partitionBalanced() both return a single sublist with 10
> elements
> A list of 11 elements, partition 10:
> * partition() returns a sublist of 10 and another sublist of 1 element
> * partitionBalanced() method returns a sublist of 6 and another sublist of 5
> elements
> A list of 14 elements, partition 10:
> * partition() returns a sublist of 10 and another sublist of 4 elements
> * partitionBalanced() method returns two sublists of 7 elements each
> A list of 20 elements, partition 10:
> * partition() and partitionBalanced() both return two sublists of 10 elements
>
--
This message was sent by Atlassian Jira
(v8.20.1#820001)