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

spliterators_part1_Komarova #66

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@

public class RectangleSpliterator extends Spliterators.AbstractIntSpliterator {

private final int innerLength;
private final int[][] array;
private final int startOuterInclusive;
private final int endOuterExclusive;
private final int startInnerInclusive;
private int innerLength;
private int[][] array;
private int startOuterInclusive;
private int endOuterExclusive;
private int startInnerInclusive;

public RectangleSpliterator(int[][] array) {
this(array, 0, array.length, 0);
Expand All @@ -29,8 +29,15 @@ private RectangleSpliterator(int[][] array, int startOuterInclusive, int endOute

@Override
public OfInt trySplit() {
// TODO
throw new UnsupportedOperationException();
final int outerLength = endOuterExclusive = startOuterInclusive;
if (outerLength <= 1) {
return null;
}
final int outerMiddle = startOuterInclusive + outerLength / 2;

final RectangleSpliterator result = new RectangleSpliterator(array, startOuterInclusive, outerMiddle, startInnerInclusive);
startOuterInclusive = outerMiddle;
return result;
}

@Override
Expand All @@ -40,7 +47,14 @@ public long estimateSize() {

@Override
public boolean tryAdvance(IntConsumer action) {
// TODO
throw new UnsupportedOperationException();
if (startOuterInclusive >= endOuterExclusive && startInnerInclusive <= innerLength) {
return false;
}
for (int i = startInnerInclusive; i < innerLength; i++) {
int value = array[startInnerInclusive][startInnerInclusive];
startInnerInclusive++;
action.accept(value);
}
return true;
}
}