| 1 | package net.minecraft.client.searchtree;
|
| 2 |
|
| 3 | import com.google.common.collect.AbstractIterator;
|
| 4 | import com.google.common.collect.Iterators;
|
| 5 | import com.google.common.collect.PeekingIterator;
|
| 6 | import java.util.Comparator;
|
| 7 | import java.util.Iterator;
|
| 8 | import net.fabricmc.api.EnvType;
|
| 9 | import net.fabricmc.api.Environment;
|
| 10 |
|
| 11 | @Environment(EnvType.CLIENT)
|
| 12 | public class MergingUniqueIterator<T> extends AbstractIterator<T> {
|
| 13 | private final PeekingIterator<T> firstIterator;
|
| 14 | private final PeekingIterator<T> secondIterator;
|
| 15 | private final Comparator<T> comparator;
|
| 16 |
|
| 17 | public MergingUniqueIterator(final Iterator<T> firstIterator, final Iterator<T> secondIterator, final Comparator<T> comparator) {
|
| 18 | this.firstIterator = Iterators.peekingIterator(firstIterator);
|
| 19 | this.secondIterator = Iterators.peekingIterator(secondIterator);
|
| 20 | this.comparator = comparator;
|
| 21 | }
|
| 22 |
|
| 23 | @Override
|
| 24 | protected T computeNext() {
|
| 25 | boolean firstEmpty = !this.firstIterator.hasNext();
|
| 26 | boolean secondEmpty = !this.secondIterator.hasNext();
|
| 27 | if (firstEmpty && secondEmpty) {
|
| 28 | return this.endOfData();
|
| 29 | }
|
| 30 |
|
| 31 | if (firstEmpty) {
|
| 32 | return this.secondIterator.next();
|
| 33 | }
|
| 34 |
|
| 35 | if (secondEmpty) {
|
| 36 | return this.firstIterator.next();
|
| 37 | }
|
| 38 |
|
| 39 | int compare = this.comparator.compare(this.firstIterator.peek(), this.secondIterator.peek());
|
| 40 | if (compare == 0) {
|
| 41 | this.secondIterator.next();
|
| 42 | }
|
| 43 |
|
| 44 | return compare <= 0 ? this.firstIterator.next() : this.secondIterator.next();
|
| 45 | }
|
| 46 | }
|
| 47 |
|