| 1 | package net.minecraft.world.phys.shapes;
|
| 2 |
|
| 3 | import it.unimi.dsi.fastutil.doubles.DoubleArrayList;
|
| 4 | import it.unimi.dsi.fastutil.doubles.DoubleList;
|
| 5 | import it.unimi.dsi.fastutil.doubles.DoubleLists;
|
| 6 |
|
| 7 | public class IndirectMerger implements IndexMerger {
|
| 8 | private static final DoubleList EMPTY = DoubleLists.unmodifiable(DoubleArrayList.wrap(new double[]{0.0}));
|
| 9 | private final double[] result;
|
| 10 | private final int[] firstIndices;
|
| 11 | private final int[] secondIndices;
|
| 12 | private final int resultLength;
|
| 13 |
|
| 14 | public IndirectMerger(final DoubleList first, final DoubleList second, final boolean firstOnlyMatters, final boolean secondOnlyMatters) {
|
| 15 | double lastValue = Double.NaN;
|
| 16 | int firstSize = first.size();
|
| 17 | int secondSize = second.size();
|
| 18 | int capacity = firstSize + secondSize;
|
| 19 | this.result = new double[capacity];
|
| 20 | this.firstIndices = new int[capacity];
|
| 21 | this.secondIndices = new int[capacity];
|
| 22 | boolean canSkipFirst = !firstOnlyMatters;
|
| 23 | boolean canSkipSecond = !secondOnlyMatters;
|
| 24 | int resultIndex = 0;
|
| 25 | int firstIndex = 0;
|
| 26 | int secondIndex = 0;
|
| 27 |
|
| 28 | while (true) {
|
| 29 | boolean ranOutOfFirst = firstIndex >= firstSize;
|
| 30 | boolean ranOutOfSecond = secondIndex >= secondSize;
|
| 31 | if (ranOutOfFirst && ranOutOfSecond) {
|
| 32 | this.resultLength = Math.max(1, resultIndex);
|
| 33 | return;
|
| 34 | }
|
| 35 |
|
| 36 | boolean choseFirst = !ranOutOfFirst && (ranOutOfSecond || first.getDouble(firstIndex) < second.getDouble(secondIndex) + 1.0E-7);
|
| 37 | if (choseFirst) {
|
| 38 | firstIndex++;
|
| 39 | if (canSkipFirst && (secondIndex == 0 || ranOutOfSecond)) {
|
| 40 | continue;
|
| 41 | }
|
| 42 | } else {
|
| 43 | secondIndex++;
|
| 44 | if (canSkipSecond && (firstIndex == 0 || ranOutOfFirst)) {
|
| 45 | continue;
|
| 46 | }
|
| 47 | }
|
| 48 |
|
| 49 | int currentFirstIndex = firstIndex - 1;
|
| 50 | int currentSecondIndex = secondIndex - 1;
|
| 51 | double nextValue = choseFirst ? first.getDouble(currentFirstIndex) : second.getDouble(currentSecondIndex);
|
| 52 | if (!(lastValue >= nextValue - 1.0E-7)) {
|
| 53 | this.firstIndices[resultIndex] = currentFirstIndex;
|
| 54 | this.secondIndices[resultIndex] = currentSecondIndex;
|
| 55 | this.result[resultIndex] = nextValue;
|
| 56 | resultIndex++;
|
| 57 | lastValue = nextValue;
|
| 58 | } else {
|
| 59 | this.firstIndices[resultIndex - 1] = currentFirstIndex;
|
| 60 | this.secondIndices[resultIndex - 1] = currentSecondIndex;
|
| 61 | }
|
| 62 | }
|
| 63 | }
|
| 64 |
|
| 65 | @Override
|
| 66 | public boolean forMergedIndexes(final IndexMerger.IndexConsumer consumer) {
|
| 67 | int length = this.resultLength - 1;
|
| 68 |
|
| 69 | for (int i = 0; i < length; i++) {
|
| 70 | if (!consumer.merge(this.firstIndices[i], this.secondIndices[i], i)) {
|
| 71 | return false;
|
| 72 | }
|
| 73 | }
|
| 74 |
|
| 75 | return true;
|
| 76 | }
|
| 77 |
|
| 78 | @Override
|
| 79 | public int size() {
|
| 80 | return this.resultLength;
|
| 81 | }
|
| 82 |
|
| 83 | @Override
|
| 84 | public DoubleList getList() {
|
| 85 | return this.resultLength <= 1 ? EMPTY : DoubleArrayList.wrap(this.result, this.resultLength);
|
| 86 | }
|
| 87 | }
|
| 88 |
|