| 1 | package net.minecraft.util;
|
| 2 |
|
| 3 | public class IntHashMap<V>
|
| 4 | {
|
| 5 | private transient IntHashMap.Entry<V>[] slots = new IntHashMap.Entry[16];
|
| 6 |
|
| 7 | /** The number of items stored in this map */
|
| 8 | private transient int count;
|
| 9 |
|
| 10 | /** The grow threshold */
|
| 11 | private int threshold = 12;
|
| 12 |
|
| 13 | /** The scale factor used to determine when to grow the table */
|
| 14 | private final float growFactor = 0.75F;
|
| 15 |
|
| 16 | /**
|
| 17 | * Makes the passed in integer suitable for hashing by a number of shifts
|
| 18 | */
|
| 19 | private static int computeHash(int integer)
|
| 20 | {
|
| 21 | integer = integer ^ integer >>> 20 ^ integer >>> 12;
|
| 22 | return integer ^ integer >>> 7 ^ integer >>> 4;
|
| 23 | }
|
| 24 |
|
| 25 | /**
|
| 26 | * Computes the index of the slot for the hash and slot count passed in.
|
| 27 | */
|
| 28 | private static int getSlotIndex(int hash, int slotCount)
|
| 29 | {
|
| 30 | return hash & slotCount - 1;
|
| 31 | }
|
| 32 |
|
| 33 | /**
|
| 34 | * Returns the object associated to a key
|
| 35 | */
|
| 36 | public V lookup(int p_76041_1_)
|
| 37 | {
|
| 38 | int i = computeHash(p_76041_1_);
|
| 39 |
|
| 40 | for (IntHashMap.Entry<V> entry = this.slots[getSlotIndex(i, this.slots.length)]; entry != null; entry = entry.nextEntry)
|
| 41 | {
|
| 42 | if (entry.hashEntry == p_76041_1_)
|
| 43 | {
|
| 44 | return entry.valueEntry;
|
| 45 | }
|
| 46 | }
|
| 47 |
|
| 48 | return (V)null;
|
| 49 | }
|
| 50 |
|
| 51 | /**
|
| 52 | * Returns true if this hash table contains the specified item.
|
| 53 | */
|
| 54 | public boolean containsItem(int p_76037_1_)
|
| 55 | {
|
| 56 | return this.lookupEntry(p_76037_1_) != null;
|
| 57 | }
|
| 58 |
|
| 59 | final IntHashMap.Entry<V> lookupEntry(int p_76045_1_)
|
| 60 | {
|
| 61 | int i = computeHash(p_76045_1_);
|
| 62 |
|
| 63 | for (IntHashMap.Entry<V> entry = this.slots[getSlotIndex(i, this.slots.length)]; entry != null; entry = entry.nextEntry)
|
| 64 | {
|
| 65 | if (entry.hashEntry == p_76045_1_)
|
| 66 | {
|
| 67 | return entry;
|
| 68 | }
|
| 69 | }
|
| 70 |
|
| 71 | return null;
|
| 72 | }
|
| 73 |
|
| 74 | /**
|
| 75 | * Adds a key and associated value to this map
|
| 76 | */
|
| 77 | public void addKey(int p_76038_1_, V p_76038_2_)
|
| 78 | {
|
| 79 | int i = computeHash(p_76038_1_);
|
| 80 | int j = getSlotIndex(i, this.slots.length);
|
| 81 |
|
| 82 | for (IntHashMap.Entry<V> entry = this.slots[j]; entry != null; entry = entry.nextEntry)
|
| 83 | {
|
| 84 | if (entry.hashEntry == p_76038_1_)
|
| 85 | {
|
| 86 | entry.valueEntry = p_76038_2_;
|
| 87 | return;
|
| 88 | }
|
| 89 | }
|
| 90 |
|
| 91 | this.insert(i, p_76038_1_, p_76038_2_, j);
|
| 92 | }
|
| 93 |
|
| 94 | /**
|
| 95 | * Increases the number of hash slots
|
| 96 | */
|
| 97 | private void grow(int p_76047_1_)
|
| 98 | {
|
| 99 | IntHashMap.Entry<V>[] entry = this.slots;
|
| 100 | int i = entry.length;
|
| 101 |
|
| 102 | if (i == 1073741824)
|
| 103 | {
|
| 104 | this.threshold = Integer.MAX_VALUE;
|
| 105 | }
|
| 106 | else
|
| 107 | {
|
| 108 | IntHashMap.Entry<V>[] entry1 = new IntHashMap.Entry[p_76047_1_];
|
| 109 | this.copyTo(entry1);
|
| 110 | this.slots = entry1;
|
| 111 | this.threshold = (int)((float)p_76047_1_ * this.growFactor);
|
| 112 | }
|
| 113 | }
|
| 114 |
|
| 115 | /**
|
| 116 | * Copies the hash slots to a new array
|
| 117 | */
|
| 118 | private void copyTo(IntHashMap.Entry<V>[] p_76048_1_)
|
| 119 | {
|
| 120 | IntHashMap.Entry<V>[] entry = this.slots;
|
| 121 | int i = p_76048_1_.length;
|
| 122 |
|
| 123 | for (int j = 0; j < entry.length; ++j)
|
| 124 | {
|
| 125 | IntHashMap.Entry<V> entry1 = entry[j];
|
| 126 |
|
| 127 | if (entry1 != null)
|
| 128 | {
|
| 129 | entry[j] = null;
|
| 130 |
|
| 131 | while (true)
|
| 132 | {
|
| 133 | IntHashMap.Entry<V> entry2 = entry1.nextEntry;
|
| 134 | int k = getSlotIndex(entry1.slotHash, i);
|
| 135 | entry1.nextEntry = p_76048_1_[k];
|
| 136 | p_76048_1_[k] = entry1;
|
| 137 | entry1 = entry2;
|
| 138 |
|
| 139 | if (entry2 == null)
|
| 140 | {
|
| 141 | break;
|
| 142 | }
|
| 143 | }
|
| 144 | }
|
| 145 | }
|
| 146 | }
|
| 147 |
|
| 148 | /**
|
| 149 | * Removes the specified object from the map and returns it
|
| 150 | */
|
| 151 | public V removeObject(int p_76049_1_)
|
| 152 | {
|
| 153 | IntHashMap.Entry<V> entry = this.removeEntry(p_76049_1_);
|
| 154 | return (V)(entry == null ? null : entry.valueEntry);
|
| 155 | }
|
| 156 |
|
| 157 | final IntHashMap.Entry<V> removeEntry(int p_76036_1_)
|
| 158 | {
|
| 159 | int i = computeHash(p_76036_1_);
|
| 160 | int j = getSlotIndex(i, this.slots.length);
|
| 161 | IntHashMap.Entry<V> entry = this.slots[j];
|
| 162 | IntHashMap.Entry<V> entry1;
|
| 163 | IntHashMap.Entry<V> entry2;
|
| 164 |
|
| 165 | for (entry1 = entry; entry1 != null; entry1 = entry2)
|
| 166 | {
|
| 167 | entry2 = entry1.nextEntry;
|
| 168 |
|
| 169 | if (entry1.hashEntry == p_76036_1_)
|
| 170 | {
|
| 171 | --this.count;
|
| 172 |
|
| 173 | if (entry == entry1)
|
| 174 | {
|
| 175 | this.slots[j] = entry2;
|
| 176 | }
|
| 177 | else
|
| 178 | {
|
| 179 | entry.nextEntry = entry2;
|
| 180 | }
|
| 181 |
|
| 182 | return entry1;
|
| 183 | }
|
| 184 |
|
| 185 | entry = entry1;
|
| 186 | }
|
| 187 |
|
| 188 | return entry1;
|
| 189 | }
|
| 190 |
|
| 191 | /**
|
| 192 | * Removes all entries from the map
|
| 193 | */
|
| 194 | public void clearMap()
|
| 195 | {
|
| 196 | IntHashMap.Entry<V>[] entry = this.slots;
|
| 197 |
|
| 198 | for (int i = 0; i < entry.length; ++i)
|
| 199 | {
|
| 200 | entry[i] = null;
|
| 201 | }
|
| 202 |
|
| 203 | this.count = 0;
|
| 204 | }
|
| 205 |
|
| 206 | /**
|
| 207 | * Adds an object to a slot
|
| 208 | */
|
| 209 | private void insert(int p_76040_1_, int p_76040_2_, V p_76040_3_, int p_76040_4_)
|
| 210 | {
|
| 211 | IntHashMap.Entry<V> entry = this.slots[p_76040_4_];
|
| 212 | this.slots[p_76040_4_] = new IntHashMap.Entry(p_76040_1_, p_76040_2_, p_76040_3_, entry);
|
| 213 |
|
| 214 | if (this.count++ >= this.threshold)
|
| 215 | {
|
| 216 | this.grow(2 * this.slots.length);
|
| 217 | }
|
| 218 | }
|
| 219 |
|
| 220 | static class Entry<V>
|
| 221 | {
|
| 222 | final int hashEntry;
|
| 223 | V valueEntry;
|
| 224 | IntHashMap.Entry<V> nextEntry;
|
| 225 | final int slotHash;
|
| 226 |
|
| 227 | Entry(int p_i1552_1_, int p_i1552_2_, V p_i1552_3_, IntHashMap.Entry<V> p_i1552_4_)
|
| 228 | {
|
| 229 | this.valueEntry = p_i1552_3_;
|
| 230 | this.nextEntry = p_i1552_4_;
|
| 231 | this.hashEntry = p_i1552_2_;
|
| 232 | this.slotHash = p_i1552_1_;
|
| 233 | }
|
| 234 |
|
| 235 | public final int getHash()
|
| 236 | {
|
| 237 | return this.hashEntry;
|
| 238 | }
|
| 239 |
|
| 240 | public final V getValue()
|
| 241 | {
|
| 242 | return this.valueEntry;
|
| 243 | }
|
| 244 |
|
| 245 | public final boolean equals(Object p_equals_1_)
|
| 246 | {
|
| 247 | if (!(p_equals_1_ instanceof IntHashMap.Entry))
|
| 248 | {
|
| 249 | return false;
|
| 250 | }
|
| 251 | else
|
| 252 | {
|
| 253 | IntHashMap.Entry<V> entry = (IntHashMap.Entry)p_equals_1_;
|
| 254 | Object object = Integer.valueOf(this.getHash());
|
| 255 | Object object1 = Integer.valueOf(entry.getHash());
|
| 256 |
|
| 257 | if (object == object1 || object != null && object.equals(object1))
|
| 258 | {
|
| 259 | Object object2 = this.getValue();
|
| 260 | Object object3 = entry.getValue();
|
| 261 |
|
| 262 | if (object2 == object3 || object2 != null && object2.equals(object3))
|
| 263 | {
|
| 264 | return true;
|
| 265 | }
|
| 266 | }
|
| 267 |
|
| 268 | return false;
|
| 269 | }
|
| 270 | }
|
| 271 |
|
| 272 | public final int hashCode()
|
| 273 | {
|
| 274 | return IntHashMap.computeHash(this.hashEntry);
|
| 275 | }
|
| 276 |
|
| 277 | public final String toString()
|
| 278 | {
|
| 279 | return this.getHash() + "=" + this.getValue();
|
| 280 | }
|
| 281 | }
|
| 282 | }
|
| 283 |
|