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

part1 and part2 done by Gladun Nikita #43

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
Expand Up @@ -3,15 +3,16 @@

import java.util.Spliterator;
import java.util.Spliterators;
import java.util.function.Consumer;
import java.util.function.IntConsumer;

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,8 +30,19 @@ private RectangleSpliterator(int[][] array, int startOuterInclusive, int endOute

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

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

startOuterInclusive = middle;
startInnerInclusive = 0;

return rectangleSpliterator;
}

@Override
Expand All @@ -40,7 +52,28 @@ public long estimateSize() {

@Override
public boolean tryAdvance(IntConsumer action) {
// TODO
throw new UnsupportedOperationException();
if(startInnerInclusive < array[0].length){
action.accept(array[startOuterInclusive][startInnerInclusive]);
startInnerInclusive++;
if(startInnerInclusive == array[0].length && startOuterInclusive < endOuterExclusive){

Choose a reason for hiding this comment

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

Move this check to the beginning of the method to reduce nesting.

startInnerInclusive = 0;
startOuterInclusive++;
}
return true;
}
return false;
}

@Override
public void forEachRemaining(Consumer<? super Integer> action) {
while (startOuterInclusive < endOuterExclusive){
while (startInnerInclusive < array[0].length){
action.accept(array[startOuterInclusive][startInnerInclusive]);
startInnerInclusive++;
}
startOuterInclusive++;
startInnerInclusive = 0;
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
import org.apache.commons.lang3.builder.ToStringBuilder;

public class IndexedDoublePair {
private final int index;
private final long index;
private final double value;

public IndexedDoublePair(int index, double value) {
public IndexedDoublePair(long index, double value) {
this.index = index;
this.value = value;
}

public int getIndex() {
public long getIndex() {
return index;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,54 +3,66 @@
import java.util.Spliterator;
import java.util.Spliterators;
import java.util.function.Consumer;
import java.util.function.DoubleConsumer;

public class ZipWithIndexDoubleSpliterator extends Spliterators.AbstractSpliterator<IndexedDoublePair> {


private final OfDouble inner;
private int currentIndex;
private long currentIndex;

public ZipWithIndexDoubleSpliterator(OfDouble inner) {
this(0, inner);
}

private ZipWithIndexDoubleSpliterator(int firstIndex, OfDouble inner) {
private ZipWithIndexDoubleSpliterator(long firstIndex, OfDouble inner) {
super(inner.estimateSize(), inner.characteristics());
currentIndex = firstIndex;
this.inner = inner;
}

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

@Override
public boolean tryAdvance(Consumer<? super IndexedDoublePair> action) {
// TODO
throw new UnsupportedOperationException();
DoubleConsumer consumer = value -> action.accept(new IndexedDoublePair(currentIndex, value));
if(inner.tryAdvance(consumer)){
currentIndex++;
return true;
}
return false;
}

@Override
public void forEachRemaining(Consumer<? super IndexedDoublePair> action) {
// TODO
throw new UnsupportedOperationException();
DoubleConsumer consumer = value -> action.accept(new IndexedDoublePair(currentIndex,value));
inner.forEachRemaining(consumer);
currentIndex++;

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

}

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

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

@Override
public long estimateSize() {
// TODO
throw new UnsupportedOperationException();
return inner.estimateSize();
}
}
38 changes: 38 additions & 0 deletions src/test/java/part1/RectangleSpliteratorTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package part1;

import org.junit.Assert;
import org.junit.Test;
import spliterators.part1.exercise.RectangleSpliterator;

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

/**
* Created by nikita on 4/11/2017.
*/
public class RectangleSpliteratorTest {

private int[][] getRandomArray(final int outerLength, final int innerLenght) {
final int[][] result = new int[outerLength][innerLenght];

for (int i = 0; i < outerLength; i++) {
for(int j = 0; j< innerLenght; j++){
result[i][j] = j;
}
}

return result;
}

@Test
public void trySplit(){
final int[][] array = getRandomArray(2,4);
final long actual = StreamSupport.stream(new RectangleSpliterator(array), true).count();
final long expected = Arrays.stream(array).parallel().flatMapToInt(Arrays::stream).count();
final long actual2 = StreamSupport.stream(new RectangleSpliterator(array), true).skip(5).count();

Assert.assertEquals(expected, actual);
Assert.assertEquals(3, actual2);
}

}
59 changes: 59 additions & 0 deletions src/test/java/part2/ZipWithIndexDoubleSpliteratorTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package part2;

import static org.junit.Assert.assertEquals;

import org.junit.Assert;
import org.junit.Test;
import spliterators.part2.exercise.IndexedDoublePair;
import spliterators.part2.exercise.ZipWithIndexDoubleSpliterator;

import java.util.Arrays;
import java.util.List;
import java.util.Spliterator;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

/**
* Created by nikita on 4/13/2017.
*/
public class ZipWithIndexDoubleSpliteratorTest {

final double[] array = {1.4, 2.2, 4.9, 5.3, 8.7, 9.1};

@Test
public void testCount(){
Spliterator.OfDouble spliterator = Arrays.spliterator(array);

final long actual = StreamSupport.stream(new ZipWithIndexDoubleSpliterator(spliterator), true).count();

Assert.assertEquals(6, actual);
}

@Test
public void tetsToList(){
final List<Double> expected = Arrays.asList(1.4, 2.2, 4.9, 5.3, 8.7, 9.1);
final List<Double> actual = StreamSupport.stream(new ZipWithIndexDoubleSpliterator(Arrays.spliterator(array)), true)
.map(IndexedDoublePair::getValue)
.collect(Collectors.toList());

assertEquals(expected, actual);
}

@Test
public void testIndexedDoublePair(){
final List<IndexedDoublePair> expected = Arrays.asList(
new IndexedDoublePair(0, 1.4),
new IndexedDoublePair(1, 2.2),
new IndexedDoublePair(2, 4.9),
new IndexedDoublePair(3, 5.3),
new IndexedDoublePair(4, 8.7),
new IndexedDoublePair(5, 9.1)
);

List<IndexedDoublePair> actual = StreamSupport.stream(new ZipWithIndexDoubleSpliterator(Arrays.spliterator(array)), true)
.collect(Collectors.toList());

assertEquals(expected, actual);
}

}