| 1 | package net.minecraft.world.level.lighting;
|
| 2 |
|
| 3 | import com.google.common.annotations.VisibleForTesting;
|
| 4 | import it.unimi.dsi.fastutil.HashCommon;
|
| 5 | import it.unimi.dsi.fastutil.longs.Long2LongLinkedOpenHashMap;
|
| 6 | import it.unimi.dsi.fastutil.longs.LongLinkedOpenHashSet;
|
| 7 | import java.util.NoSuchElementException;
|
| 8 | import net.minecraft.util.Mth;
|
| 9 |
|
| 10 | public class SpatialLongSet extends LongLinkedOpenHashSet {
|
| 11 | private final SpatialLongSet.InternalMap map;
|
| 12 |
|
| 13 | public SpatialLongSet(final int expected, final float f) {
|
| 14 | super(expected, f);
|
| 15 | this.map = new SpatialLongSet.InternalMap(expected / 64, f);
|
| 16 | }
|
| 17 |
|
| 18 | @Override
|
| 19 | public boolean add(final long k) {
|
| 20 | return this.map.addBit(k);
|
| 21 | }
|
| 22 |
|
| 23 | @Override
|
| 24 | public boolean rem(final long k) {
|
| 25 | return this.map.removeBit(k);
|
| 26 | }
|
| 27 |
|
| 28 | @Override
|
| 29 | public long removeFirstLong() {
|
| 30 | return this.map.removeFirstBit();
|
| 31 | }
|
| 32 |
|
| 33 | @Override
|
| 34 | public int size() {
|
| 35 | throw new UnsupportedOperationException();
|
| 36 | }
|
| 37 |
|
| 38 | @Override
|
| 39 | public boolean isEmpty() {
|
| 40 | return this.map.isEmpty();
|
| 41 | }
|
| 42 |
|
| 43 | @VisibleForTesting
|
| 44 | static class InternalMap extends Long2LongLinkedOpenHashMap {
|
| 45 | private static final int X_BITS = Mth.log2(60000000);
|
| 46 | private static final int Z_BITS = Mth.log2(60000000);
|
| 47 | private static final int Y_BITS = 64 - X_BITS - Z_BITS;
|
| 48 | private static final int Y_OFFSET = 0;
|
| 49 | private static final int Z_OFFSET = Y_BITS;
|
| 50 | private static final int X_OFFSET = Y_BITS + Z_BITS;
|
| 51 | private static final long OUTER_MASK = 3L << X_OFFSET | 3L | 3L << Z_OFFSET;
|
| 52 | private int lastPos = -1;
|
| 53 | private long lastOuterKey;
|
| 54 | private final int minSize;
|
| 55 |
|
| 56 | public InternalMap(final int expected, final float f) {
|
| 57 | super(expected, f);
|
| 58 | this.minSize = expected;
|
| 59 | }
|
| 60 |
|
| 61 | @VisibleForTesting
|
| 62 | static long getOuterKey(final long key) {
|
| 63 | return key & ~OUTER_MASK;
|
| 64 | }
|
| 65 |
|
| 66 | @VisibleForTesting
|
| 67 | static int getInnerKey(final long key) {
|
| 68 | int innerX = (int)(key >>> X_OFFSET & 3L);
|
| 69 | int innerY = (int)(key >>> 0 & 3L);
|
| 70 | int innerZ = (int)(key >>> Z_OFFSET & 3L);
|
| 71 | return innerX << 4 | innerZ << 2 | innerY;
|
| 72 | }
|
| 73 |
|
| 74 | @VisibleForTesting
|
| 75 | static long getFullKey(long outerKey, final int innerKey) {
|
| 76 | outerKey |= (long)(innerKey >>> 4 & 3) << X_OFFSET;
|
| 77 | outerKey |= (long)(innerKey >>> 2 & 3) << Z_OFFSET;
|
| 78 | return outerKey | (long)(innerKey >>> 0 & 3) << 0;
|
| 79 | }
|
| 80 |
|
| 81 | public boolean addBit(final long key) {
|
| 82 | long outerKey = getOuterKey(key);
|
| 83 | int innerKey = getInnerKey(key);
|
| 84 | long bitMask = 1L << innerKey;
|
| 85 | int pos;
|
| 86 | if (outerKey == 0L) {
|
| 87 | if (this.containsNullKey) {
|
| 88 | return this.replaceBit(this.n, bitMask);
|
| 89 | }
|
| 90 |
|
| 91 | this.containsNullKey = true;
|
| 92 | pos = this.n;
|
| 93 | } else {
|
| 94 | if (this.lastPos != -1 && outerKey == this.lastOuterKey) {
|
| 95 | return this.replaceBit(this.lastPos, bitMask);
|
| 96 | }
|
| 97 |
|
| 98 | long[] keys = this.key;
|
| 99 | pos = (int)HashCommon.mix(outerKey) & this.mask;
|
| 100 |
|
| 101 | for (long curr = keys[pos]; curr != 0L; curr = keys[pos]) {
|
| 102 | if (curr == outerKey) {
|
| 103 | this.lastPos = pos;
|
| 104 | this.lastOuterKey = outerKey;
|
| 105 | return this.replaceBit(pos, bitMask);
|
| 106 | }
|
| 107 |
|
| 108 | pos = pos + 1 & this.mask;
|
| 109 | }
|
| 110 | }
|
| 111 |
|
| 112 | this.key[pos] = outerKey;
|
| 113 | this.value[pos] = bitMask;
|
| 114 | if (this.size == 0) {
|
| 115 | this.first = this.last = pos;
|
| 116 | this.link[pos] = -1L;
|
| 117 | } else {
|
| 118 | this.link[this.last] = this.link[this.last] ^ (this.link[this.last] ^ pos & 4294967295L) & 4294967295L;
|
| 119 | this.link[pos] = (this.last & 4294967295L) << 32 | 4294967295L;
|
| 120 | this.last = pos;
|
| 121 | }
|
| 122 |
|
| 123 | if (this.size++ >= this.maxFill) {
|
| 124 | this.rehash(HashCommon.arraySize(this.size + 1, this.f));
|
| 125 | }
|
| 126 |
|
| 127 | return false;
|
| 128 | }
|
| 129 |
|
| 130 | private boolean replaceBit(final int pos, final long bitMask) {
|
| 131 | boolean oldValue = (this.value[pos] & bitMask) != 0L;
|
| 132 | this.value[pos] = this.value[pos] | bitMask;
|
| 133 | return oldValue;
|
| 134 | }
|
| 135 |
|
| 136 | public boolean removeBit(final long key) {
|
| 137 | long outerKey = getOuterKey(key);
|
| 138 | int innerKey = getInnerKey(key);
|
| 139 | long bitMask = 1L << innerKey;
|
| 140 | if (outerKey == 0L) {
|
| 141 | return this.containsNullKey ? this.removeFromNullEntry(bitMask) : false;
|
| 142 | }
|
| 143 |
|
| 144 | if (this.lastPos != -1 && outerKey == this.lastOuterKey) {
|
| 145 | return this.removeFromEntry(this.lastPos, bitMask);
|
| 146 | }
|
| 147 |
|
| 148 | long[] keys = this.key;
|
| 149 | int pos = (int)HashCommon.mix(outerKey) & this.mask;
|
| 150 |
|
| 151 | for (long curr = keys[pos]; curr != 0L; curr = keys[pos]) {
|
| 152 | if (outerKey == curr) {
|
| 153 | this.lastPos = pos;
|
| 154 | this.lastOuterKey = outerKey;
|
| 155 | return this.removeFromEntry(pos, bitMask);
|
| 156 | }
|
| 157 |
|
| 158 | pos = pos + 1 & this.mask;
|
| 159 | }
|
| 160 |
|
| 161 | return false;
|
| 162 | }
|
| 163 |
|
| 164 | private boolean removeFromNullEntry(final long bitMask) {
|
| 165 | if ((this.value[this.n] & bitMask) == 0L) {
|
| 166 | return false;
|
| 167 | }
|
| 168 |
|
| 169 | this.value[this.n] = this.value[this.n] & ~bitMask;
|
| 170 | if (this.value[this.n] != 0L) {
|
| 171 | return true;
|
| 172 | }
|
| 173 |
|
| 174 | this.containsNullKey = false;
|
| 175 | this.size--;
|
| 176 | this.fixPointers(this.n);
|
| 177 | if (this.size < this.maxFill / 4 && this.n > 16) {
|
| 178 | this.rehash(this.n / 2);
|
| 179 | }
|
| 180 |
|
| 181 | return true;
|
| 182 | }
|
| 183 |
|
| 184 | private boolean removeFromEntry(final int pos, final long bitMask) {
|
| 185 | if ((this.value[pos] & bitMask) == 0L) {
|
| 186 | return false;
|
| 187 | }
|
| 188 |
|
| 189 | this.value[pos] = this.value[pos] & ~bitMask;
|
| 190 | if (this.value[pos] != 0L) {
|
| 191 | return true;
|
| 192 | }
|
| 193 |
|
| 194 | this.lastPos = -1;
|
| 195 | this.size--;
|
| 196 | this.fixPointers(pos);
|
| 197 | this.shiftKeys(pos);
|
| 198 | if (this.size < this.maxFill / 4 && this.n > 16) {
|
| 199 | this.rehash(this.n / 2);
|
| 200 | }
|
| 201 |
|
| 202 | return true;
|
| 203 | }
|
| 204 |
|
| 205 | public long removeFirstBit() {
|
| 206 | if (this.size == 0) {
|
| 207 | throw new NoSuchElementException();
|
| 208 | }
|
| 209 |
|
| 210 | int pos = this.first;
|
| 211 | long outerKey = this.key[pos];
|
| 212 | int innerKey = Long.numberOfTrailingZeros(this.value[pos]);
|
| 213 | this.value[pos] = this.value[pos] & ~(1L << innerKey);
|
| 214 | if (this.value[pos] == 0L) {
|
| 215 | this.removeFirstLong();
|
| 216 | this.lastPos = -1;
|
| 217 | }
|
| 218 |
|
| 219 | return getFullKey(outerKey, innerKey);
|
| 220 | }
|
| 221 |
|
| 222 | @Override
|
| 223 | protected void rehash(final int newN) {
|
| 224 | if (newN > this.minSize) {
|
| 225 | super.rehash(newN);
|
| 226 | }
|
| 227 | }
|
| 228 | }
|
| 229 | }
|
| 230 |
|