-
Notifications
You must be signed in to change notification settings - Fork 46
part3 (Kozyrev Egor) #42
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
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 |
---|---|---|
@@ -1,48 +1,80 @@ | ||
package spliterators.part3.exercise; | ||
|
||
import java.util.Comparator; | ||
import java.util.Spliterator; | ||
import java.util.Spliterators; | ||
import java.util.function.Consumer; | ||
|
||
public class ZipWithArraySpliterator<A, B> extends Spliterators.AbstractSpliterator<Pair<A, B>> { | ||
|
||
|
||
private final Spliterator<A> inner; | ||
private final B[] array; | ||
private int currentIndex; | ||
private int endIndex; | ||
|
||
public ZipWithArraySpliterator(Spliterator<A> inner, B[] array) { | ||
super(Long.MAX_VALUE, 0); // FIXME: | ||
// TODO | ||
throw new UnsupportedOperationException(); | ||
this(inner, array, 0, array.length); | ||
} | ||
|
||
public ZipWithArraySpliterator(Spliterator<A> inner, B[] array, int startIndex, int endIndex) { | ||
super(array.length, inner.characteristics()); | ||
this.inner = inner; | ||
this.array = array; | ||
this.currentIndex = startIndex; | ||
this.endIndex = endIndex; | ||
} | ||
|
||
@Override | ||
public int characteristics() { | ||
// TODO | ||
throw new UnsupportedOperationException(); | ||
return inner.characteristics() | ||
| NONNULL | ||
| SIZED; | ||
} | ||
|
||
@Override | ||
public boolean tryAdvance(Consumer<? super Pair<A, B>> action) { | ||
// TODO | ||
throw new UnsupportedOperationException(); | ||
return (currentIndex < endIndex | ||
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 |
||
&& inner.tryAdvance(a -> action.accept(new Pair<A, B>(a, array[currentIndex++])))); | ||
} | ||
|
||
@Override | ||
public void forEachRemaining(Consumer<? super Pair<A, B>> action) { | ||
// TODO | ||
throw new UnsupportedOperationException(); | ||
while (currentIndex < endIndex) { | ||
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. Inefficient. You can optimize this in case |
||
if (!inner.tryAdvance(a -> action.accept(new Pair<A, B>(a, array[currentIndex++])))) { | ||
return; | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public Spliterator<Pair<A, B>> trySplit() { | ||
// TODO | ||
throw new UnsupportedOperationException(); | ||
Spliterator<A> splitInner = this.inner.trySplit(); | ||
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 not |
||
if (splitInner == null || array.length - currentIndex <= 1) { | ||
return null; | ||
} | ||
|
||
if (inner.hasCharacteristics(SUBSIZED)) { | ||
int newStart = currentIndex; | ||
currentIndex += splitInner.estimateSize(); | ||
int newEnd = currentIndex < array.length ? currentIndex : array.length; | ||
|
||
return new ZipWithArraySpliterator<>(splitInner, array, newStart, newEnd); | ||
} else { | ||
return super.trySplit(); | ||
} | ||
} | ||
|
||
@Override | ||
public long estimateSize() { | ||
// TODO | ||
throw new UnsupportedOperationException(); | ||
return endIndex - currentIndex; | ||
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. |
||
} | ||
|
||
@Override | ||
public Comparator<Pair<A, B>> getComparator() { | ||
if (inner.hasCharacteristics(SORTED)) { | ||
return (p1, p2) -> inner.getComparator().compare(p1.getA(), p2.getA()); | ||
} else { | ||
throw new IllegalStateException(); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package spliterators.part3.exercise; | ||
|
||
import org.junit.Test; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.stream.StreamSupport; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
|
||
public class ZipWithArraySpliteratorTest { | ||
|
||
private List<Integer> listWithLessSize = new ArrayList<>(Arrays.asList(0, 1, 2, 3, 4, 5)); | ||
private List<Integer> listWithSameSize = new ArrayList<>(Arrays.asList(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)); | ||
private List<Integer> listWithMoreSize = new ArrayList<>(Arrays.asList(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14)); | ||
|
||
private Integer[] array = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; | ||
|
||
@Test | ||
public void testCount() { | ||
final long expectedLess = listWithLessSize.size(); | ||
final long actualLess = StreamSupport | ||
.stream(new ZipWithArraySpliterator<>(listWithLessSize.spliterator(), array), true) | ||
.count(); | ||
assertEquals(expectedLess, actualLess); | ||
|
||
final long expectedSame = listWithSameSize.size(); | ||
final long actualSame = StreamSupport | ||
.stream(new ZipWithArraySpliterator<>(listWithSameSize.spliterator(), array), true) | ||
.count(); | ||
assertEquals(expectedSame, actualSame); | ||
|
||
final long expectedMore = array.length; | ||
final long actualMore = StreamSupport | ||
.stream(new ZipWithArraySpliterator<>(listWithMoreSize.spliterator(), array), true) | ||
.count(); | ||
assertEquals(expectedMore, actualMore); | ||
} | ||
|
||
@Test | ||
public void testSkip() { | ||
long actual = StreamSupport | ||
.stream(new ZipWithArraySpliterator<>(listWithSameSize.spliterator(), array), true) | ||
.skip(2) | ||
.mapToInt(Pair::getA) | ||
.sum(); | ||
assertEquals(44, actual); | ||
} | ||
|
||
@Test | ||
public void testLimit() { | ||
long actual = StreamSupport | ||
.stream(new ZipWithArraySpliterator<>(listWithSameSize.spliterator(), array), true) | ||
.limit(4) | ||
.mapToInt(Pair::getA) | ||
.sum(); | ||
assertEquals(6, actual); | ||
} | ||
} |
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.
Array may be bigger than the unsized inner.