diff --git a/src/main/java/spliterators/part1/exercise/RectangleSpliterator.java b/src/main/java/spliterators/part1/exercise/RectangleSpliterator.java index 678f4f5..4f01047 100755 --- a/src/main/java/spliterators/part1/exercise/RectangleSpliterator.java +++ b/src/main/java/spliterators/part1/exercise/RectangleSpliterator.java @@ -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); @@ -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 @@ -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){ + startInnerInclusive = 0; + startOuterInclusive++; + } + return true; + } + return false; + } + + @Override + public void forEachRemaining(Consumer action) { + while (startOuterInclusive < endOuterExclusive){ + while (startInnerInclusive < array[0].length){ + action.accept(array[startOuterInclusive][startInnerInclusive]); + startInnerInclusive++; + } + startOuterInclusive++; + startInnerInclusive = 0; + } + } } diff --git a/src/main/java/spliterators/part2/exercise/IndexedDoublePair.java b/src/main/java/spliterators/part2/exercise/IndexedDoublePair.java index d3c0162..424948b 100755 --- a/src/main/java/spliterators/part2/exercise/IndexedDoublePair.java +++ b/src/main/java/spliterators/part2/exercise/IndexedDoublePair.java @@ -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; } diff --git a/src/main/java/spliterators/part2/exercise/ZipWithIndexDoubleSpliterator.java b/src/main/java/spliterators/part2/exercise/ZipWithIndexDoubleSpliterator.java index deb4867..b72a1df 100755 --- a/src/main/java/spliterators/part2/exercise/ZipWithIndexDoubleSpliterator.java +++ b/src/main/java/spliterators/part2/exercise/ZipWithIndexDoubleSpliterator.java @@ -3,18 +3,19 @@ import java.util.Spliterator; import java.util.Spliterators; import java.util.function.Consumer; +import java.util.function.DoubleConsumer; public class ZipWithIndexDoubleSpliterator extends Spliterators.AbstractSpliterator { 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; @@ -22,35 +23,46 @@ private ZipWithIndexDoubleSpliterator(int firstIndex, OfDouble inner) { @Override public int characteristics() { - // TODO - throw new UnsupportedOperationException(); + return inner.characteristics(); } @Override public boolean tryAdvance(Consumer 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 action) { - // TODO - throw new UnsupportedOperationException(); + DoubleConsumer consumer = value -> action.accept(new IndexedDoublePair(currentIndex,value)); + inner.forEachRemaining(consumer); + currentIndex++; } @Override public Spliterator 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(); } } diff --git a/src/test/java/part1/RectangleSpliteratorTest.java b/src/test/java/part1/RectangleSpliteratorTest.java new file mode 100644 index 0000000..b73959c --- /dev/null +++ b/src/test/java/part1/RectangleSpliteratorTest.java @@ -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); + } + +} diff --git a/src/test/java/part2/ZipWithIndexDoubleSpliteratorTest.java b/src/test/java/part2/ZipWithIndexDoubleSpliteratorTest.java new file mode 100644 index 0000000..1e445bd --- /dev/null +++ b/src/test/java/part2/ZipWithIndexDoubleSpliteratorTest.java @@ -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 expected = Arrays.asList(1.4, 2.2, 4.9, 5.3, 8.7, 9.1); + final List actual = StreamSupport.stream(new ZipWithIndexDoubleSpliterator(Arrays.spliterator(array)), true) + .map(IndexedDoublePair::getValue) + .collect(Collectors.toList()); + + assertEquals(expected, actual); + } + + @Test + public void testIndexedDoublePair(){ + final List 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 actual = StreamSupport.stream(new ZipWithIndexDoubleSpliterator(Arrays.spliterator(array)), true) + .collect(Collectors.toList()); + + assertEquals(expected, actual); + } + +}