Skip to content

p1 #36

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

p1 #36

Show file tree
Hide file tree
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 @@ -9,9 +9,9 @@ public class RectangleSpliterator extends Spliterators.AbstractIntSpliterator {

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

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

@Override
public OfInt trySplit() {
// TODO
throw new UnsupportedOperationException();
int l = endOuterExclusive - startOuterInclusive;
if(l <= 1) return null;
int m = startOuterInclusive + l / 2;
RectangleSpliterator rectangleSpliterator =
new RectangleSpliterator(array, startOuterInclusive, m, startInnerInclusive);
startOuterInclusive = m;
startInnerInclusive = 0;
return rectangleSpliterator;
}

@Override
public long estimateSize() {
return ((long) endOuterExclusive - startOuterInclusive)*innerLength - startInnerInclusive;
return ((long) endOuterExclusive - startOuterInclusive) * innerLength - startInnerInclusive;
}

@Override
public boolean tryAdvance(IntConsumer action) {
// TODO
throw new UnsupportedOperationException();
if (startInnerInclusive >= innerLength) {
if (startOuterInclusive < endOuterExclusive - 1) {
startOuterInclusive += 1;
startInnerInclusive = 0;
} else {
return false;
}
}
action.accept(array[startOuterInclusive][startInnerInclusive]);
startInnerInclusive += 1;
return true;
}

@Override
public void forEachRemaining(IntConsumer action) {
while (startOuterInclusive < endOuterExclusive) {
if (startInnerInclusive < innerLength) {
action.accept(array[startOuterInclusive][startInnerInclusive]);
startInnerInclusive += 1;
} else {
startOuterInclusive += 1;
startInnerInclusive = 0;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package spliterators.part1.exercise;

import org.junit.Before;
import org.junit.Test;

import java.util.Arrays;
import java.util.concurrent.ThreadLocalRandom;
import java.util.stream.StreamSupport;

import static org.junit.Assert.assertEquals;

public class RectangleSpliteratorTest {
private int[][] array;

@Before
public void setup() {
int outerLength = 2;
array = new int[outerLength][];
for (int i = 0; i < array.length; i++) {
int innerLength = 2;
int[] inner = new int[innerLength];
array[i] = inner;
for (int j = 0; j < inner.length; j++) {
inner[j] = ThreadLocalRandom.current().nextInt();
}
}
}

@Test
public void testIt(){
long sum1 = Arrays.stream(array)
.parallel()
.flatMapToInt(Arrays::stream)
.asLongStream()
.sum();

long sum = StreamSupport.intStream(new RectangleSpliterator(array), true)
.asLongStream()
.sum();
assertEquals(sum1, sum);
}
}