Skip to content

Drozdov Igor part 3 #59

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

Open
wants to merge 5 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
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ public class ArrayZipWithIndexExample {

public static class IndexedArraySpliterator<T> extends Spliterators.AbstractSpliterator<IndexedPair<T>> {


private final T[] array;
private int startInclusive;
private final int endExclusive;
Expand Down
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
@@ -1,56 +1,65 @@
package spliterators.part2.exercise;

import spliterators.part3.exercise.ZipWithArraySpliterator;

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

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() | Spliterator.DISTINCT /*& ~Spliterator.SORTED*/;
}

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

@Override
public Comparator<? super IndexedDoublePair> getComparator() {
return (i1, i2) -> inner.getComparator().compare(i1.getValue(),i2.getValue());
}

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

@Override
public Spliterator<IndexedDoublePair> trySplit() {
// TODO
// if (inner.hasCharacteristics(???)) {
// use inner.trySplit
// } else
if (inner.hasCharacteristics(Spliterator.SUBSIZED)) {
long innerSize = inner.estimateSize();
OfDouble ofDouble = inner.trySplit();

return super.trySplit();
ZipWithIndexDoubleSpliterator zipWithIndexDoubleSpliterator = new ZipWithIndexDoubleSpliterator(currentIndex, ofDouble);
currentIndex = (int) (currentIndex + innerSize / 2);
return zipWithIndexDoubleSpliterator;
} else {
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 @@ -7,42 +7,58 @@
public class ZipWithArraySpliterator<A, B> extends Spliterators.AbstractSpliterator<Pair<A, B>> {


private final Spliterator<A> inner;
private Spliterator<A> inner;
int currentIndex;
private final B[] array;

public ZipWithArraySpliterator(Spliterator<A> inner, B[] array) {
super(Long.MAX_VALUE, 0); // FIXME:
// TODO
throw new UnsupportedOperationException();
this(inner, array, 0);
}

private ZipWithArraySpliterator(Spliterator<A> inner, B[] array, int index) {
super(Math.min(inner.estimateSize(), array.length), inner.characteristics());
this.inner = inner;
this.array = array;
this.currentIndex = index;
}

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

@Override
public boolean tryAdvance(Consumer<? super Pair<A, B>> action) {
// TODO
throw new UnsupportedOperationException();
return inner.tryAdvance(value ->
action.accept(
new Pair<>(value, array[currentIndex++])
)
);
}

@Override
public void forEachRemaining(Consumer<? super Pair<A, B>> action) {
// TODO
throw new UnsupportedOperationException();
for (; currentIndex < estimateSize(); ++currentIndex) {
inner.tryAdvance(value -> new Pair<>(value, array[currentIndex]));
}
}

@Override
public Spliterator<Pair<A, B>> trySplit() {
// TODO
throw new UnsupportedOperationException();
if (inner.hasCharacteristics(Spliterator.SUBSIZED)) {
long innerSize = inner.estimateSize();
Spliterator<A> aSpliterator = inner.trySplit();

ZipWithArraySpliterator<A, B> abZipWithArraySpliterator = new ZipWithArraySpliterator<>(aSpliterator, array, currentIndex);
currentIndex = (int) (currentIndex + innerSize / 2);
return abZipWithArraySpliterator;
} else {
return super.trySplit();
}
}

@Override
public long estimateSize() {
// TODO
throw new UnsupportedOperationException();
return Math.min(array.length - currentIndex, inner.estimateSize());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package spliterators.part2.exercise;

import org.junit.Test;

import java.util.Spliterator;
import java.util.stream.DoubleStream;
import java.util.stream.StreamSupport;

import static org.junit.Assert.*;

/**
* Created by student on 7/17/17.
*/
public class ZipWithIndexDoubleSpliteratorTest {

private int maxSize;

@Test
public void testPar() {
maxSize = 100;
Spliterator.OfDouble spliterator = DoubleStream.iterate(1.0, i -> i + 0.1)
.limit(maxSize)
.spliterator();

double sum = StreamSupport.stream(new ZipWithIndexDoubleSpliterator(spliterator), true)
.mapToDouble(z -> z.getIndex()*z.getValue())
.sum();

double res = 0.0;

for (int i = 0; i < maxSize; ++i) {
res += i * (1.0 + 0.1*i);
}

assertEquals(sum, res, 0.1);
}

@Test
public void testSeq() {
maxSize = 100;
Spliterator.OfDouble spliterator = DoubleStream.iterate(1.0, i -> i + 0.1)
.limit(maxSize)
.spliterator();

double sum = StreamSupport.stream(new ZipWithIndexDoubleSpliterator(spliterator), false)
.mapToDouble(z -> z.getIndex()*z.getValue())
.sum();

double res = 0.0;

for (int i = 0; i < maxSize; ++i) {
res += i * (1.0 + 0.1*i);
}

assertEquals(sum, res, 0.1);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
package spliterators.part3.exercise;

import org.junit.Test;

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

import static org.junit.Assert.*;

/**
* Created by student on 7/18/17.
*/
public class ZipWithArraySpliteratorTest {

private int maxSize;

@Test
public void testPar() {
maxSize = 10000;

Double[] array = generateDouble();
double sum = StreamSupport.stream(new ZipWithArraySpliterator<>(Arrays.spliterator(array), Arrays.copyOf(array, array.length)), true)
.mapToDouble(pair -> pair.getA() - pair.getB())
.sum();

double res = 0.0;

assertEquals(sum, res, 0.1);
}

private Double[] generateDouble() {
Double[] doubles = new Double[maxSize];

for (int i = 0; i < maxSize; i++) {
doubles[i] = Math.random();
}

return doubles;
}

@Test
public void testSeq() {
maxSize = 10000;

Double[] array = generateDouble();
double sum = StreamSupport.stream(new ZipWithArraySpliterator<>(Arrays.spliterator(array), Arrays.copyOf(array, array.length)), true)
.mapToDouble(pair -> pair.getA() - pair.getB())
.sum();

double res = 0.0;

assertEquals(sum, res, 0.1);
}

@Test
public void testParDifSize() {
maxSize = 10000;

Double[] array = generateDouble();
double sum = StreamSupport.stream(new ZipWithArraySpliterator<>(Arrays.spliterator(array), Arrays.copyOf(array, array.length/2)), true)
.mapToDouble(pair -> pair.getA() - pair.getB())
.sum();

double res = 0.0;

assertEquals(sum, res, 0.1);
}


@Test
public void testSeqDifSize() {
maxSize = 10000;

Double[] array = generateDouble();
double sum = StreamSupport.stream(new ZipWithArraySpliterator<>(Arrays.spliterator(array), Arrays.copyOf(array, array.length/2)), true)
.mapToDouble(pair -> pair.getA() - pair.getB())
.sum();

double res = 0.0;

assertEquals(sum, res, 0.1);
}

@Test
public void emptyRight() {
maxSize = 10000;

Double[] array = generateDouble();
double sum = StreamSupport.stream(new ZipWithArraySpliterator<>(Arrays.spliterator(array), Arrays.copyOf(array, 0)), true)
.mapToDouble(pair -> pair.getA() - pair.getB())
.sum();

double res = 0.0;

assertEquals(sum, res, 0.1);
}

@Test
public void emptLeft() {
maxSize = 10000;

Double[] array = generateDouble();
double sum = StreamSupport.stream(new ZipWithArraySpliterator<>(Arrays.spliterator(Arrays.copyOf(array, 0)), array), true)
.mapToDouble(pair -> pair.getA() - pair.getB())
.sum();

double res = 0.0;

assertEquals(sum, res, 0.1);
}

}