Skip to content
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

Implement BatchingSpliterator#trySplit #909

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 35 additions & 2 deletions src/main/java/com/pivovarit/collectors/BatchingSpliterator.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;

import static java.util.stream.Stream.empty;
import static java.util.stream.Stream.of;
Expand All @@ -16,8 +17,8 @@
*/
final class BatchingSpliterator<T> implements Spliterator<List<T>> {

private final List<T> source;
private final int maxChunks;
private List<T> source;
private int maxChunks;

private int chunks;
private int chunkSize;
Expand Down Expand Up @@ -80,6 +81,17 @@ public boolean tryAdvance(Consumer<? super List<T>> action) {

@Override
public Spliterator<List<T>> trySplit() {
if (actualBatchCount(source, chunks) > 1 || consumed == 0 ) {
var first = source.subList(0, source.size() / 2);
var second = source.subList(source.size() / 2, source.size());
var originalChunks = chunks;

source = first;
chunks = originalChunks % 2 == 0 ? originalChunks / 2 : originalChunks / 2 + 1;
maxChunks = Math.min(source.size(), chunks);
chunkSize = (int) Math.ceil(((double) source.size()) / chunks);
return new BatchingSpliterator<>(second, originalChunks / 2);
}
return null;
}

Expand All @@ -92,4 +104,25 @@ public long estimateSize() {
public int characteristics() {
return ORDERED | SIZED;
}

private static <T> int actualBatchCount(List<T> list, int numberOfBatches) {
int batchSize = list.size() / numberOfBatches;
int remainder = list.size() % numberOfBatches;

int batches = 0;
int currentIndex = 0;

for (int i = 0; i < numberOfBatches; i++) {
int currentBatchSize = batchSize + (remainder > 0 ? 1 : 0);
remainder--;

int nextIndex = Math.min(currentIndex + currentBatchSize, list.size());
if (currentIndex < nextIndex) {
batches++;
}
currentIndex = nextIndex;
}

return batches;
}
}