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

Drozdov Igor part2 #52

Open
wants to merge 2 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,61 @@
package spliterators.part2.exercise;

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)) {
OfDouble ofDouble = inner.trySplit();
currentIndex = currentIndex + estimateSize() / 2;
return new ZipWithIndexDoubleSpliterator(0, ofDouble);
} else {
return super.trySplit();
}

return super.trySplit();
}

@Override
public long estimateSize() {
// TODO
throw new UnsupportedOperationException();
return inner.estimateSize();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package spliterators.part2.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/17/17.
*/
public class ZipWithIndexDoubleSpliteratorTest {

private int maxSize;

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


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

double res = 0.0;

for (int i = 0; i < maxSize; ++i) {
res += i * (array[i]);
}

assertEquals(sum, res, 0.1);
}

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

for (double aDouble : doubles) {
aDouble = Math.random();
}

return doubles;
}

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

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

double res = 0.0;

for (int i = 0; i < maxSize; ++i) {
res += i * (array[i]);
}

assertEquals(sum, res, 0.1);
}
}