-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathZipWithIndexDoubleSpliterator.java
More file actions
executable file
·65 lines (52 loc) · 2.11 KB
/
ZipWithIndexDoubleSpliterator.java
File metadata and controls
executable file
·65 lines (52 loc) · 2.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
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 long currentIndex;
public ZipWithIndexDoubleSpliterator(OfDouble inner) {
this(0, inner);
}
private ZipWithIndexDoubleSpliterator(long firstIndex, OfDouble inner) {
super(inner.estimateSize(), inner.characteristics());
currentIndex = firstIndex;
this.inner = inner;
}
@Override
public int characteristics() {
return inner.characteristics() | Spliterator.DISTINCT /*& ~Spliterator.SORTED*/;
}
@Override
public boolean tryAdvance(Consumer<? super IndexedDoublePair> action) {
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) {
inner.forEachRemaining(
(double d) -> action.accept(new IndexedDoublePair(currentIndex++, d))
);
}
@Override
public Spliterator<IndexedDoublePair> trySplit() {
if (inner.hasCharacteristics(Spliterator.SUBSIZED)) {
long innerSize = inner.estimateSize();
OfDouble ofDouble = inner.trySplit();
ZipWithIndexDoubleSpliterator zipWithIndexDoubleSpliterator = new ZipWithIndexDoubleSpliterator(currentIndex, ofDouble);
currentIndex = (int) (currentIndex + innerSize / 2);
return zipWithIndexDoubleSpliterator;
} else {
return super.trySplit();
}
}
@Override
public long estimateSize() {
return inner.estimateSize();
}
}