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

Part 2 (by Vasilii Bobkov) #55

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

import java.util.Comparator;
import java.util.Spliterator;
import java.util.Spliterators;
import java.util.function.Consumer;
Expand All @@ -8,49 +9,64 @@ public class ZipWithIndexDoubleSpliterator extends Spliterators.AbstractSplitera


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() | NONNULL;
}

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

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

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

return super.trySplit();
if (inner.hasCharacteristics(Spliterator.SUBSIZED)) {
Spliterator<IndexedDoublePair> spliterator = new ZipWithIndexDoubleSpliterator(currentIndex, inner.trySplit());
currentIndex += spliterator.estimateSize();
return spliterator;
} else{
return super.trySplit();
}
}

@Override
public long estimateSize() {
// TODO
throw new UnsupportedOperationException();
return inner.estimateSize();
}

@Override
public Comparator<IndexedDoublePair> getComparator() {
if (inner.hasCharacteristics(SORTED)) {
Comparator<? super Double> comparator = inner.getComparator();
return (o1, o2) -> comparator.compare(o1.getValue(), o2.getValue());
} else {
throw new IllegalStateException();
}
}
}