| 1 | package net.minecraft.util;
|
| 2 |
|
| 3 | import com.google.common.base.Predicates;
|
| 4 | import com.google.common.collect.Iterators;
|
| 5 | import java.util.Arrays;
|
| 6 | import java.util.Iterator;
|
| 7 | import net.minecraft.core.IdMap;
|
| 8 | import org.jspecify.annotations.Nullable;
|
| 9 |
|
| 10 | public class CrudeIncrementalIntIdentityHashBiMap<K> implements IdMap<K> {
|
| 11 | private static final int NOT_FOUND = -1;
|
| 12 | private static final Object EMPTY_SLOT = null;
|
| 13 | private static final float LOADFACTOR = 0.8F;
|
| 14 | private @Nullable K[] keys;
|
| 15 | private int[] values;
|
| 16 | private @Nullable K[] byId;
|
| 17 | private int nextId;
|
| 18 | private int size;
|
| 19 |
|
| 20 | private CrudeIncrementalIntIdentityHashBiMap(final int capacity) {
|
| 21 | this.keys = (K[])(new Object[capacity]);
|
| 22 | this.values = new int[capacity];
|
| 23 | this.byId = (K[])(new Object[capacity]);
|
| 24 | }
|
| 25 |
|
| 26 | private CrudeIncrementalIntIdentityHashBiMap(final K[] keys, final int[] values, final K[] byId, final int nextId, final int size) {
|
| 27 | this.keys = keys;
|
| 28 | this.values = values;
|
| 29 | this.byId = byId;
|
| 30 | this.nextId = nextId;
|
| 31 | this.size = size;
|
| 32 | }
|
| 33 |
|
| 34 | public static <A> CrudeIncrementalIntIdentityHashBiMap<A> create(final int initialCapacity) {
|
| 35 | return new CrudeIncrementalIntIdentityHashBiMap((int)(initialCapacity / 0.8F));
|
| 36 | }
|
| 37 |
|
| 38 | @Override
|
| 39 | public int getId(final @Nullable K thing) {
|
| 40 | return this.getValue(this.indexOf(thing, this.hash(thing)));
|
| 41 | }
|
| 42 |
|
| 43 | @Override
|
| 44 | public @Nullable K byId(final int id) {
|
| 45 | return id >= 0 && id < this.byId.length ? this.byId[id] : null;
|
| 46 | }
|
| 47 |
|
| 48 | private int getValue(final int index) {
|
| 49 | return index == -1 ? -1 : this.values[index];
|
| 50 | }
|
| 51 |
|
| 52 | public boolean contains(final K key) {
|
| 53 | return this.getId(key) != -1;
|
| 54 | }
|
| 55 |
|
| 56 | public boolean contains(final int id) {
|
| 57 | return this.byId(id) != null;
|
| 58 | }
|
| 59 |
|
| 60 | public int add(final K key) {
|
| 61 | int value = this.nextId();
|
| 62 | this.addMapping(key, value);
|
| 63 | return value;
|
| 64 | }
|
| 65 |
|
| 66 | private int nextId() {
|
| 67 | while (this.nextId < this.byId.length && this.byId[this.nextId] != null) {
|
| 68 | this.nextId++;
|
| 69 | }
|
| 70 |
|
| 71 | return this.nextId;
|
| 72 | }
|
| 73 |
|
| 74 | private void grow(final int newSize) {
|
| 75 | K[] oldKeys = this.keys;
|
| 76 | int[] oldValues = this.values;
|
| 77 | CrudeIncrementalIntIdentityHashBiMap<K> resized = new CrudeIncrementalIntIdentityHashBiMap<>(newSize);
|
| 78 |
|
| 79 | for (int i = 0; i < oldKeys.length; i++) {
|
| 80 | if (oldKeys[i] != null) {
|
| 81 | resized.addMapping(oldKeys[i], oldValues[i]);
|
| 82 | }
|
| 83 | }
|
| 84 |
|
| 85 | this.keys = resized.keys;
|
| 86 | this.values = resized.values;
|
| 87 | this.byId = resized.byId;
|
| 88 | this.nextId = resized.nextId;
|
| 89 | this.size = resized.size;
|
| 90 | }
|
| 91 |
|
| 92 | public void addMapping(final K key, final int id) {
|
| 93 | int minSize = Math.max(id, this.size + 1);
|
| 94 | if (minSize >= this.keys.length * 0.8F) {
|
| 95 | int newSize = this.keys.length << 1;
|
| 96 |
|
| 97 | while (newSize < id) {
|
| 98 | newSize <<= 1;
|
| 99 | }
|
| 100 |
|
| 101 | this.grow(newSize);
|
| 102 | }
|
| 103 |
|
| 104 | int index = this.findEmpty(this.hash(key));
|
| 105 | this.keys[index] = key;
|
| 106 | this.values[index] = id;
|
| 107 | this.byId[id] = key;
|
| 108 | this.size++;
|
| 109 | if (id == this.nextId) {
|
| 110 | this.nextId++;
|
| 111 | }
|
| 112 | }
|
| 113 |
|
| 114 | private int hash(final @Nullable K key) {
|
| 115 | return (Mth.murmurHash3Mixer(System.identityHashCode(key)) & 2147483647) % this.keys.length;
|
| 116 | }
|
| 117 |
|
| 118 | private int indexOf(final @Nullable K key, final int startFrom) {
|
| 119 | for (int i = startFrom; i < this.keys.length; i++) {
|
| 120 | if (this.keys[i] == key) {
|
| 121 | return i;
|
| 122 | }
|
| 123 |
|
| 124 | if (this.keys[i] == EMPTY_SLOT) {
|
| 125 | return -1;
|
| 126 | }
|
| 127 | }
|
| 128 |
|
| 129 | for (int i = 0; i < startFrom; i++) {
|
| 130 | if (this.keys[i] == key) {
|
| 131 | return i;
|
| 132 | }
|
| 133 |
|
| 134 | if (this.keys[i] == EMPTY_SLOT) {
|
| 135 | return -1;
|
| 136 | }
|
| 137 | }
|
| 138 |
|
| 139 | return -1;
|
| 140 | }
|
| 141 |
|
| 142 | private int findEmpty(final int startFrom) {
|
| 143 | for (int i = startFrom; i < this.keys.length; i++) {
|
| 144 | if (this.keys[i] == EMPTY_SLOT) {
|
| 145 | return i;
|
| 146 | }
|
| 147 | }
|
| 148 |
|
| 149 | for (int i = 0; i < startFrom; i++) {
|
| 150 | if (this.keys[i] == EMPTY_SLOT) {
|
| 151 | return i;
|
| 152 | }
|
| 153 | }
|
| 154 |
|
| 155 | throw new RuntimeException("Overflowed :(");
|
| 156 | }
|
| 157 |
|
| 158 | @Override
|
| 159 | public Iterator<K> iterator() {
|
| 160 | return Iterators.filter(Iterators.forArray(this.byId), Predicates.notNull());
|
| 161 | }
|
| 162 |
|
| 163 | public void clear() {
|
| 164 | Arrays.fill(this.keys, null);
|
| 165 | Arrays.fill(this.byId, null);
|
| 166 | this.nextId = 0;
|
| 167 | this.size = 0;
|
| 168 | }
|
| 169 |
|
| 170 | @Override
|
| 171 | public int size() {
|
| 172 | return this.size;
|
| 173 | }
|
| 174 |
|
| 175 | public CrudeIncrementalIntIdentityHashBiMap<K> copy() {
|
| 176 | return new CrudeIncrementalIntIdentityHashBiMap<>(
|
| 177 | (K[])((Object[])this.keys.clone()), (int[])this.values.clone(), (K[])((Object[])this.byId.clone()), this.nextId, this.size
|
| 178 | );
|
| 179 | }
|
| 180 | }
|
| 181 |
|