-
Notifications
You must be signed in to change notification settings - Fork 476
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[COLLECTIONS-800] Adding partitionBalanced(List,int) method #265
Open
ClaudioConsolmagno
wants to merge
1
commit into
apache:master
Choose a base branch
from
ClaudioConsolmagno:COLLECTIONS-800-partition-balanced
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -101,10 +101,12 @@ public void visitKeepCommand(final E object) { | |
private static class Partition<T> extends AbstractList<List<T>> { | ||
private final List<T> list; | ||
private final int size; | ||
private final boolean isBalanced; | ||
|
||
private Partition(final List<T> list, final int size) { | ||
private Partition(final List<T> list, final int size, final boolean isBalanced) { | ||
this.list = list; | ||
this.size = size; | ||
this.isBalanced = isBalanced; | ||
} | ||
|
||
@Override | ||
|
@@ -117,9 +119,25 @@ public List<T> get(final int index) { | |
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 | ||
} | ||
} else { | ||
start = index * size; | ||
currentPartitionSize = Math.min(size, list.size() - start); | ||
} | ||
return list.subList(start, start + currentPartitionSize); | ||
} | ||
|
||
@Override | ||
|
@@ -486,13 +504,52 @@ public static <E> List<E> longestCommonSubsequence(final List<E> listA, final Li | |
* @throws NullPointerException if list is null | ||
* @throws IllegalArgumentException if size is not strictly positive | ||
* @since 4.0 | ||
* @see ListUtils#partitionBalanced(List, int) | ||
*/ | ||
public static <T> List<List<T>> partition(final List<T> list, final int size) { | ||
Objects.requireNonNull(list, "list"); | ||
if (size <= 0) { | ||
throw new IllegalArgumentException("Size must be greater than 0"); | ||
} | ||
return new Partition<>(list, size); | ||
return new Partition<>(list, size, false); | ||
} | ||
|
||
/** | ||
* Returns consecutive {@link List#subList(int, int) sublists} of a | ||
* 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. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see why you would use the example from |
||
* the same input list with a partition size of 4 also yields {@code [[a, b, c], [d, e]]} | ||
* since putting 4 elements in the first partition makes it unbalanced as the last | ||
* partition would only have 1 element. | ||
* <p> | ||
* The outer list is unmodifiable, but reflects the latest state of the | ||
* source list. The inner lists are sublist views of the original list, | ||
* produced on demand using {@link List#subList(int, int)}, and are subject | ||
* to all the usual caveats about modification as explained in that API. | ||
* The size of the produced list is always equals to the size of the list | ||
* produced by using the same input with the {@link ListUtils#partition(List, int)} | ||
* method. | ||
* <p> | ||
* Adapted from http://code.google.com/p/guava-libraries/ | ||
* | ||
* @param <T> the element type | ||
* @param list the list to return consecutive balanced sublists of | ||
* @param size the desired maximum size of each sublist | ||
* @return a list of consecutive sublists balanced to have maximum size difference | ||
* of 1 between them | ||
* @throws NullPointerException if list is null | ||
* @throws IllegalArgumentException if size is not strictly positive | ||
* @since 4.5 | ||
* @see ListUtils#partition(List, int) | ||
*/ | ||
public static <T> List<List<T>> partitionBalanced(final List<T> list, final int size) { | ||
Objects.requireNonNull(list, "list"); | ||
if (size <= 0) { | ||
throw new IllegalArgumentException("Size must be greater than 0"); | ||
} | ||
return new Partition<>(list, size, true); | ||
} | ||
|
||
/** | ||
|
This file contains 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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Newbie myself, but I wonder if that could be written more simply:
...or similar (Note: this is untested). The if condition and subtractions look quite complicated in my opinion.