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 completed #67

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
@@ -2,55 +2,70 @@

import java.util.Spliterator;
import java.util.Spliterators;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;
import java.util.function.Consumer;

public class ZipWithIndexDoubleSpliterator extends Spliterators.AbstractSpliterator<IndexedDoublePair> {


private final OfDouble inner;
private int currentIndex;
private AtomicLong 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;
if (! inner.hasCharacteristics(SUBSIZED)) throw new IllegalStateException("Zip got not subsized Spliterator");
currentIndex = new AtomicLong(firstIndex);
this.inner = inner;
}

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

@Override
public boolean tryAdvance(Consumer<? super IndexedDoublePair> action) {
// TODO
throw new UnsupportedOperationException();
final boolean res =
inner.tryAdvance((Double v) ->
action.accept(new IndexedDoublePair((int)currentIndex.get(), v)));
if (res) currentIndex.incrementAndGet();
return res;
}

@Override
public void forEachRemaining(Consumer<? super IndexedDoublePair> action) {
// TODO
throw new UnsupportedOperationException();
inner.forEachRemaining((Double v) -> {
action.accept(new IndexedDoublePair((int)currentIndex.get(), v));
currentIndex.incrementAndGet();
});
}

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

return super.trySplit();
if (inner.hasCharacteristics(SUBSIZED)) {
OfDouble newSplit = inner.trySplit();
if (newSplit == null)
return null;
Spliterator<IndexedDoublePair> zipped =
new ZipWithIndexDoubleSpliterator(currentIndex.get(), newSplit);
currentIndex.addAndGet(newSplit.estimateSize());
return zipped;
} 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
@@ -0,0 +1,49 @@
package spliterators.part2.example;

import org.junit.Test;
import spliterators.part2.exercise.ZipWithIndexDoubleSpliterator;
import spliterators.part3.exercise.Pair;

import java.util.List;
import java.util.Spliterators;
import java.util.concurrent.ThreadLocalRandom;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertThat;

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

return result;
}

@Test
public void nonzeroLengthSuccess() {
final double[] randomArray = getRandomArray(10);

final List<String> collect = StreamSupport.stream(new ZipWithIndexDoubleSpliterator(
Spliterators.spliterator(randomArray, 0)), true)
.map(p -> new Pair<>(p.getIndex() + 1, p.getValue() + 1))
.map(Pair::toString)
.collect(Collectors.toList());
assertThat(collect.size(), is(10));
}

@Test
public void zeroLengthSuccess() {
final double[] randomArray = getRandomArray(0);

final List<String> collect = StreamSupport.stream(new ZipWithIndexDoubleSpliterator(
Spliterators.spliterator(randomArray, 0)), true)
.map(p -> new Pair<>(p.getIndex() + 1, p.getValue() + 1))
.map(Pair::toString)
.collect(Collectors.toList());
assertThat(collect.size(), is(0));
}
}