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

First_homework (Aleksei Litkovetc) #45

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
@@ -1,6 +1,5 @@
package spliterators.part1.exercise;


import java.util.Spliterator;
import java.util.Spliterators;
import java.util.function.IntConsumer;
Expand All @@ -9,11 +8,11 @@ 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) {
RectangleSpliterator(int[][] array) {
this(array, 0, array.length, 0);
}

Expand All @@ -29,8 +28,18 @@ private RectangleSpliterator(int[][] array, int startOuterInclusive, int endOute

@Override
public OfInt trySplit() {
// TODO
throw new UnsupportedOperationException();
int length = endOuterExclusive - startOuterInclusive;
if (length <= 1) {
return null;
}

int middle = startOuterInclusive + length/2;

final RectangleSpliterator newSpliterator = new RectangleSpliterator(array, startOuterInclusive, middle, startInnerInclusive);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Incorrect. Add test weth .parallel().skip(1)...

startOuterInclusive = middle;

return newSpliterator;
}

@Override
Expand All @@ -40,7 +49,16 @@ public long estimateSize() {

@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;
}
}
18 changes: 18 additions & 0 deletions src/test/java/spliterators/part1/example/ArrayExampleTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package spliterators.part1.example;

import org.junit.Assert;
import org.junit.Test;

import java.util.stream.StreamSupport;

public class ArrayExampleTest {
@Test
public void IntArraySpliterator() {
int[] array = new int[] {1, 2, 3, 4, 5};
final boolean parallel = false;
long res = StreamSupport.intStream(new ArrayExample.IntArraySpliterator(array), parallel)
.asLongStream()
.sum();
Assert.assertEquals(15L, res);
}
}
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);
}
}