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

part2 done #54

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
@@ -1,12 +1,13 @@
package spliterators.part2.exercise;

import java.util.Comparator;
import java.util.Optional;
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;

Expand All @@ -15,42 +16,55 @@ public ZipWithIndexDoubleSpliterator(OfDouble inner) {
}

private ZipWithIndexDoubleSpliterator(int firstIndex, OfDouble inner) {
super(inner.estimateSize(), inner.characteristics());
super(inner.estimateSize(), IMMUTABLE | ORDERED | SORTED | SIZED | SUBSIZED | NONNULL);
currentIndex = firstIndex;
this.inner = inner;
}

@Override
public int characteristics() {
// TODO
throw new UnsupportedOperationException();
return IMMUTABLE | ORDERED | SORTED | SIZED | SUBSIZED | NONNULL;
}

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

@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();
while (inner.tryAdvance((double d) ->
action.accept(new IndexedDoublePair(currentIndex, d)))) {
currentIndex++;
}
}

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

return super.trySplit();
if (inner.hasCharacteristics(SUBSIZED)) {
return Optional.ofNullable(inner.trySplit())
.map(ofD -> {
int newCurrentIndex = currentIndex;
currentIndex += (int) ofD.estimateSize();
return new ZipWithIndexDoubleSpliterator(newCurrentIndex, ofD);})
.orElse(null);
} else {
return null;
}
}

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

import org.junit.Test;

import java.util.Arrays;
import java.util.List;
import java.util.concurrent.ThreadLocalRandom;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;

import static java.util.stream.Collectors.toList;
import static org.junit.Assert.assertEquals;

public class ZipWithIndexDoubleSpliteratorTest {

private double[] getRandomArray(int length) {
final double[] result = new double[length];

for (int i = 0; i < length; i++) {
result[i] = ThreadLocalRandom.current().nextDouble();
}

return result;
}

@Test
public void comparePaired() {
final double[] randomArray = getRandomArray(1000);

final List<IndexedDoublePair> result1 =
Stream.iterate(
new IndexedDoublePair(0, randomArray[0]),
p -> new IndexedDoublePair(p.getIndex() + 1, randomArray[p.getIndex() + 1]))
.limit(randomArray.length)
.collect(toList());

final List<IndexedDoublePair> result2 =
StreamSupport.stream(new ZipWithIndexDoubleSpliterator(Arrays.stream(randomArray).spliterator()), true)
.map(p -> new IndexedDoublePair(p.getIndex() + 1, p.getValue()))
.map(p -> new IndexedDoublePair(p.getIndex() - 1, p.getValue()))
.collect(toList());

assertEquals(result1, result2);

}

}