| 1 | package net.minecraft.world.level.lighting;
|
| 2 |
|
| 3 | import it.unimi.dsi.fastutil.longs.Long2ByteMap;
|
| 4 | import it.unimi.dsi.fastutil.longs.Long2ByteOpenHashMap;
|
| 5 | import it.unimi.dsi.fastutil.longs.LongArrayList;
|
| 6 | import it.unimi.dsi.fastutil.longs.LongList;
|
| 7 | import java.util.function.LongPredicate;
|
| 8 | import net.minecraft.util.Mth;
|
| 9 |
|
| 10 | public abstract class DynamicGraphMinFixedPoint {
|
| 11 | public static final long SOURCE = Long.MAX_VALUE;
|
| 12 | private static final int NO_COMPUTED_LEVEL = 255;
|
| 13 | protected final int levelCount;
|
| 14 | private final LeveledPriorityQueue priorityQueue;
|
| 15 | private final Long2ByteMap computedLevels;
|
| 16 | private volatile boolean hasWork;
|
| 17 |
|
| 18 | protected DynamicGraphMinFixedPoint(final int levelCount, final int minQueueSize, final int minMapSize) {
|
| 19 | if (levelCount >= 254) {
|
| 20 | throw new IllegalArgumentException("Level count must be < 254.");
|
| 21 | }
|
| 22 |
|
| 23 | this.levelCount = levelCount;
|
| 24 | this.priorityQueue = new LeveledPriorityQueue(levelCount, minQueueSize);
|
| 25 | this.computedLevels = new Long2ByteOpenHashMap(minMapSize, 0.5F) {
|
| 26 | @Override
|
| 27 | protected void rehash(final int newN) {
|
| 28 | if (newN > minMapSize) {
|
| 29 | super.rehash(newN);
|
| 30 | }
|
| 31 | }
|
| 32 | };
|
| 33 | this.computedLevels.defaultReturnValue((byte)-1);
|
| 34 | }
|
| 35 |
|
| 36 | protected void removeFromQueue(final long node) {
|
| 37 | int computedLevel = this.computedLevels.remove(node) & 255;
|
| 38 | if (computedLevel != 255) {
|
| 39 | int level = this.getLevel(node);
|
| 40 | int priority = this.calculatePriority(level, computedLevel);
|
| 41 | this.priorityQueue.dequeue(node, priority, this.levelCount);
|
| 42 | this.hasWork = !this.priorityQueue.isEmpty();
|
| 43 | }
|
| 44 | }
|
| 45 |
|
| 46 | public void removeIf(final LongPredicate pred) {
|
| 47 | LongList nodesToRemove = new LongArrayList();
|
| 48 | this.computedLevels.keySet().forEach(node -> {
|
| 49 | if (pred.test(node)) {
|
| 50 | nodesToRemove.add(node);
|
| 51 | }
|
| 52 | });
|
| 53 | nodesToRemove.forEach(this::removeFromQueue);
|
| 54 | }
|
| 55 |
|
| 56 | private int calculatePriority(final int level, final int computedLevel) {
|
| 57 | return Math.min(Math.min(level, computedLevel), this.levelCount - 1);
|
| 58 | }
|
| 59 |
|
| 60 | protected void checkNode(final long node) {
|
| 61 | this.checkEdge(node, node, this.levelCount - 1, false);
|
| 62 | }
|
| 63 |
|
| 64 | protected void checkEdge(final long from, final long to, final int newLevelFrom, final boolean onlyDecreased) {
|
| 65 | this.checkEdge(from, to, newLevelFrom, this.getLevel(to), this.computedLevels.get(to) & 255, onlyDecreased);
|
| 66 | this.hasWork = !this.priorityQueue.isEmpty();
|
| 67 | }
|
| 68 |
|
| 69 | private void checkEdge(final long from, final long to, int newLevelFrom, int levelTo, int oldComputedLevel, final boolean onlyDecreased) {
|
| 70 | if (!this.isSource(to)) {
|
| 71 | newLevelFrom = Mth.clamp(newLevelFrom, 0, this.levelCount - 1);
|
| 72 | levelTo = Mth.clamp(levelTo, 0, this.levelCount - 1);
|
| 73 | boolean wasConsistent = oldComputedLevel == 255;
|
| 74 | if (wasConsistent) {
|
| 75 | oldComputedLevel = levelTo;
|
| 76 | }
|
| 77 |
|
| 78 | int newComputedLevel;
|
| 79 | if (onlyDecreased) {
|
| 80 | newComputedLevel = Math.min(oldComputedLevel, newLevelFrom);
|
| 81 | } else {
|
| 82 | newComputedLevel = Mth.clamp(this.getComputedLevel(to, from, newLevelFrom), 0, this.levelCount - 1);
|
| 83 | }
|
| 84 |
|
| 85 | int oldPriority = this.calculatePriority(levelTo, oldComputedLevel);
|
| 86 | if (levelTo != newComputedLevel) {
|
| 87 | int newPriority = this.calculatePriority(levelTo, newComputedLevel);
|
| 88 | if (oldPriority != newPriority && !wasConsistent) {
|
| 89 | this.priorityQueue.dequeue(to, oldPriority, newPriority);
|
| 90 | }
|
| 91 |
|
| 92 | this.priorityQueue.enqueue(to, newPriority);
|
| 93 | this.computedLevels.put(to, (byte)newComputedLevel);
|
| 94 | } else if (!wasConsistent) {
|
| 95 | this.priorityQueue.dequeue(to, oldPriority, this.levelCount);
|
| 96 | this.computedLevels.remove(to);
|
| 97 | }
|
| 98 | }
|
| 99 | }
|
| 100 |
|
| 101 | protected final void checkNeighbor(final long from, final long to, final int level, final boolean onlyDecreased) {
|
| 102 | int storedOldComputedLevel = this.computedLevels.get(to) & 255;
|
| 103 | int levelFrom = Mth.clamp(this.computeLevelFromNeighbor(from, to, level), 0, this.levelCount - 1);
|
| 104 | if (onlyDecreased) {
|
| 105 | this.checkEdge(from, to, levelFrom, this.getLevel(to), storedOldComputedLevel, onlyDecreased);
|
| 106 | } else {
|
| 107 | boolean wasConsistent = storedOldComputedLevel == 255;
|
| 108 | int oldComputedLevel;
|
| 109 | if (wasConsistent) {
|
| 110 | oldComputedLevel = Mth.clamp(this.getLevel(to), 0, this.levelCount - 1);
|
| 111 | } else {
|
| 112 | oldComputedLevel = storedOldComputedLevel;
|
| 113 | }
|
| 114 |
|
| 115 | if (levelFrom == oldComputedLevel) {
|
| 116 | this.checkEdge(from, to, this.levelCount - 1, wasConsistent ? oldComputedLevel : this.getLevel(to), storedOldComputedLevel, onlyDecreased);
|
| 117 | }
|
| 118 | }
|
| 119 | }
|
| 120 |
|
| 121 | protected final boolean hasWork() {
|
| 122 | return this.hasWork;
|
| 123 | }
|
| 124 |
|
| 125 | protected final int runUpdates(int count) {
|
| 126 | if (this.priorityQueue.isEmpty()) {
|
| 127 | return count;
|
| 128 | }
|
| 129 |
|
| 130 | while (!this.priorityQueue.isEmpty() && count > 0) {
|
| 131 | count--;
|
| 132 | long node = this.priorityQueue.removeFirstLong();
|
| 133 | int level = Mth.clamp(this.getLevel(node), 0, this.levelCount - 1);
|
| 134 | int computedLevel = this.computedLevels.remove(node) & 255;
|
| 135 | if (computedLevel < level) {
|
| 136 | this.setLevel(node, computedLevel);
|
| 137 | this.checkNeighborsAfterUpdate(node, computedLevel, true);
|
| 138 | } else if (computedLevel > level) {
|
| 139 | this.setLevel(node, this.levelCount - 1);
|
| 140 | if (computedLevel != this.levelCount - 1) {
|
| 141 | this.priorityQueue.enqueue(node, this.calculatePriority(this.levelCount - 1, computedLevel));
|
| 142 | this.computedLevels.put(node, (byte)computedLevel);
|
| 143 | }
|
| 144 |
|
| 145 | this.checkNeighborsAfterUpdate(node, level, false);
|
| 146 | }
|
| 147 | }
|
| 148 |
|
| 149 | this.hasWork = !this.priorityQueue.isEmpty();
|
| 150 | return count;
|
| 151 | }
|
| 152 |
|
| 153 | public int getQueueSize() {
|
| 154 | return this.computedLevels.size();
|
| 155 | }
|
| 156 |
|
| 157 | protected boolean isSource(final long node) {
|
| 158 | return node == Long.MAX_VALUE;
|
| 159 | }
|
| 160 |
|
| 161 | protected abstract int getComputedLevel(final long node, final long knownParent, final int knownLevelFromParent);
|
| 162 |
|
| 163 | protected abstract void checkNeighborsAfterUpdate(final long node, final int level, final boolean onlyDecrease);
|
| 164 |
|
| 165 | protected abstract int getLevel(long node);
|
| 166 |
|
| 167 | protected abstract void setLevel(long node, int level);
|
| 168 |
|
| 169 | protected abstract int computeLevelFromNeighbor(long from, long to, final int fromLevel);
|
| 170 | }
|
| 171 |
|