-
Notifications
You must be signed in to change notification settings - Fork 46
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
Aleksandra Pankratova #44
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/** | ||
* Created by Aleksandra_Pankratova on 22-Apr-17. | ||
*/ | ||
package spliterators.part1.exercise; | ||
|
||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
import java.util.Arrays; | ||
import java.util.stream.StreamSupport; | ||
|
||
public class RectangleSpliteratorTest { | ||
@Test | ||
public void spliteratorTest() { | ||
int[][] array = {{12, 21, 37, 49, 53}, {61, 72, 81, 99, 100}, {43, 53, 66, 74, 89}}; | ||
long actual = StreamSupport | ||
.intStream(new RectangleSpliterator(array), true) | ||
.asLongStream() | ||
.sum(); | ||
long expected = Arrays | ||
.stream(array) | ||
.parallel() | ||
.flatMapToInt(Arrays::stream) | ||
.asLongStream() | ||
.sum(); | ||
Assert.assertEquals(expected, actual); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,40 +9,53 @@ public class ZipWithArraySpliterator<A, B> extends Spliterators.AbstractSplitera | |
|
||
private final Spliterator<A> inner; | ||
private final B[] array; | ||
private int index; | ||
|
||
public ZipWithArraySpliterator(Spliterator<A> inner, B[] array) { | ||
super(Long.MAX_VALUE, 0); // FIXME: | ||
// TODO | ||
throw new UnsupportedOperationException(); | ||
super(Math.min(inner.estimateSize(), array.length), inner.characteristics()); | ||
this.inner = inner; | ||
this.array = array; | ||
index = 0; | ||
} | ||
|
||
@Override | ||
public int characteristics() { | ||
// TODO | ||
throw new UnsupportedOperationException(); | ||
return inner.characteristics(); | ||
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. Incorrect. See documentation for |
||
} | ||
|
||
@Override | ||
public boolean tryAdvance(Consumer<? super Pair<A, B>> action) { | ||
// TODO | ||
throw new UnsupportedOperationException(); | ||
return index < array.length && inner.tryAdvance(a -> { | ||
action.accept(new Pair<>(a, array[index])); | ||
index++; | ||
}); | ||
} | ||
|
||
@Override | ||
public void forEachRemaining(Consumer<? super Pair<A, B>> action) { | ||
// TODO | ||
throw new UnsupportedOperationException(); | ||
inner.forEachRemaining(a -> { | ||
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. Incorrect. Add test with array shorter than spliterator. |
||
action.accept(new Pair<>(a, array[index])); | ||
index++; | ||
}); | ||
} | ||
|
||
@Override | ||
public Spliterator<Pair<A, B>> trySplit() { | ||
// TODO | ||
throw new UnsupportedOperationException(); | ||
if (!inner.hasCharacteristics(SUBSIZED)) return super.trySplit(); | ||
Spliterator<A> split = inner.trySplit(); | ||
if (split == null) return null; | ||
final ZipWithArraySpliterator<A, B> spliterator = new ZipWithArraySpliterator<>(split, array); | ||
spliterator.setIndex(index); | ||
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. Use constructor parameter. |
||
index = Math.min((int) (index + split.estimateSize()), array.length); | ||
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. Unnecessary. |
||
return spliterator; | ||
} | ||
|
||
@Override | ||
public long estimateSize() { | ||
// TODO | ||
throw new UnsupportedOperationException(); | ||
return Math.min(inner.estimateSize(), array.length - index); | ||
} | ||
} | ||
|
||
private void setIndex(int index) { | ||
this.index = index; | ||
} | ||
} |
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.
Incorrect. Add test with
collect(toList())
.