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

Aleksandra Pankratova #44

Open
wants to merge 3 commits 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
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>RELEASE</version>
</dependency>
</dependencies>

<properties>
Expand Down
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,42 @@ 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;
RectangleSpliterator rectangleSpliterator = new RectangleSpliterator(array, startOuterInclusive, middle, startInnerInclusive);
startOuterInclusive = middle;
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 >= array[0].length) return false;
action.accept(array[startOuterInclusive][startInnerInclusive]);
startInnerInclusive++;
if (startInnerInclusive == array[0].length && startOuterInclusive < endOuterExclusive) {
startInnerInclusive = 0;
startOuterInclusive++;
}
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,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
Expand Up @@ -22,35 +22,39 @@ private ZipWithIndexDoubleSpliterator(int firstIndex, OfDouble inner) {

@Override
public int characteristics() {
// TODO
throw new UnsupportedOperationException();
return inner.characteristics();
}

@Override
public boolean tryAdvance(Consumer<? super IndexedDoublePair> action) {
// TODO
throw new UnsupportedOperationException();
return inner.tryAdvance((Double d) -> {
action.accept(new IndexedDoublePair(currentIndex, d));
currentIndex++;
});
}

@Override
public void forEachRemaining(Consumer<? super IndexedDoublePair> action) {
// TODO
throw new UnsupportedOperationException();
inner.forEachRemaining((Double d) -> {
action.accept(new IndexedDoublePair(currentIndex, d));

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()).

currentIndex++;
});
}

@Override
public Spliterator<IndexedDoublePair> trySplit() {
// TODO
// if (inner.hasCharacteristics(???)) {
// use inner.trySplit
// } else

if (inner.hasCharacteristics(Spliterator.SUBSIZED)) {
final OfDouble ofDouble = this.inner.trySplit();
if (ofDouble == null) return null;
final ZipWithIndexDoubleSpliterator result = new ZipWithIndexDoubleSpliterator(currentIndex, ofDouble);
currentIndex += ofDouble.estimateSize();
return result;
}
return super.trySplit();
}

@Override
public long estimateSize() {
// TODO
throw new UnsupportedOperationException();
return inner.estimateSize();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Choose a reason for hiding this comment

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

Incorrect. See documentation for SORTED.

}

@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 -> {

Choose a reason for hiding this comment

The 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);

Choose a reason for hiding this comment

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

Use constructor parameter.

index = Math.min((int) (index + split.estimateSize()), array.length);

Choose a reason for hiding this comment

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

Unnecessary. index + split.estimateSize() works just fine.

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;
}
}