LongHashMap .java

net.minecraft.util.LongHashMap (class): members: getNumHashElements, numHashElements, percentUseable, getValueByKey, hashArray, modCount, mask, capacity.

net.minecraft.utilclass294 linescommon7.5 KB

API Surface (10)

int getNumHashElements()
V getValueByKey(long p_76164_1_)
boolean containsItem(long p_76161_1_)
void add(long p_76163_1_, V p_76163_3_)
V remove(long p_76159_1_)
long getKey()
V getValue()
boolean equals(Object p_equals_1_)
int hashCode()
String toString()

Fields & Constants (6)

hashArray
numHashElements
mask
capacity
percentUseable
modCount

Code Graph: Dependencies (1)

Edges (calls / imports / inherits / references) resolved by graphify from the AST. Every chip opens the class source.

Full Source

LongHashMap.java294 lines · MCP 1.8.9 · verbatimOriginal ↗
1
package net.minecraft.util;
2
3
public class LongHashMap<V>
4
{
5
    private transient LongHashMap.Entry<V>[] hashArray = new LongHashMap.Entry[4096];
6
7
    /** the number of elements in the hash array */
8
    private transient int numHashElements;
9
    private int mask;
10
11
    /**
12
     * the maximum amount of elements in the hash (probably 3/4 the size due to meh hashing function)
13
     */
14
    private int capacity = 3072;
15
16
    /**
17
     * percent of the hasharray that can be used without hash colliding probably
18
     */
19
    private final float percentUseable = 0.75F;
20
21
    /** count of times elements have been added/removed */
22
    private transient volatile int modCount;
23
24
    public LongHashMap()
25
    {
26
        this.mask = this.hashArray.length - 1;
27
    }
28
29
    /**
30
     * returns the hashed key given the original key
31
     */
32
    private static int getHashedKey(long originalKey)
33
    {
34
        return hash((int)(originalKey ^ originalKey >>> 32));
35
    }
36
37
    /**
38
     * the hash function
39
     */
40
    private static int hash(int integer)
41
    {
42
        integer = integer ^ integer >>> 20 ^ integer >>> 12;
43
        return integer ^ integer >>> 7 ^ integer >>> 4;
44
    }
45
46
    /**
47
     * gets the index in the hash given the array length and the hashed key
48
     */
49
    private static int getHashIndex(int p_76158_0_, int p_76158_1_)
50
    {
51
        return p_76158_0_ & p_76158_1_;
52
    }
53
54
    public int getNumHashElements()
55
    {
56
        return this.numHashElements;
57
    }
58
59
    /**
60
     * get the value from the map given the key
61
     */
62
    public V getValueByKey(long p_76164_1_)
63
    {
64
        int i = getHashedKey(p_76164_1_);
65
66
        for (LongHashMap.Entry<V> entry = this.hashArray[getHashIndex(i, this.mask)]; entry != null; entry = entry.nextEntry)
67
        {
68
            if (entry.key == p_76164_1_)
69
            {
70
                return entry.value;
71
            }
72
        }
73
74
        return (V)null;
75
    }
76
77
    public boolean containsItem(long p_76161_1_)
78
    {
79
        return this.getEntry(p_76161_1_) != null;
80
    }
81
82
    final LongHashMap.Entry<V> getEntry(long p_76160_1_)
83
    {
84
        int i = getHashedKey(p_76160_1_);
85
86
        for (LongHashMap.Entry<V> entry = this.hashArray[getHashIndex(i, this.mask)]; entry != null; entry = entry.nextEntry)
87
        {
88
            if (entry.key == p_76160_1_)
89
            {
90
                return entry;
91
            }
92
        }
93
94
        return null;
95
    }
96
97
    /**
98
     * Add a key-value pair.
99
     */
100
    public void add(long p_76163_1_, V p_76163_3_)
101
    {
102
        int i = getHashedKey(p_76163_1_);
103
        int j = getHashIndex(i, this.mask);
104
105
        for (LongHashMap.Entry<V> entry = this.hashArray[j]; entry != null; entry = entry.nextEntry)
106
        {
107
            if (entry.key == p_76163_1_)
108
            {
109
                entry.value = p_76163_3_;
110
                return;
111
            }
112
        }
113
114
        ++this.modCount;
115
        this.createKey(i, p_76163_1_, p_76163_3_, j);
116
    }
117
118
    /**
119
     * resizes the table
120
     */
121
    private void resizeTable(int p_76153_1_)
122
    {
123
        LongHashMap.Entry<V>[] entry = this.hashArray;
124
        int i = entry.length;
125
126
        if (i == 1073741824)
127
        {
128
            this.capacity = Integer.MAX_VALUE;
129
        }
130
        else
131
        {
132
            LongHashMap.Entry<V>[] entry1 = new LongHashMap.Entry[p_76153_1_];
133
            this.copyHashTableTo(entry1);
134
            this.hashArray = entry1;
135
            this.mask = this.hashArray.length - 1;
136
            this.capacity = (int)((float)p_76153_1_ * this.percentUseable);
137
        }
138
    }
139
140
    /**
141
     * copies the hash table to the specified array
142
     */
143
    private void copyHashTableTo(LongHashMap.Entry<V>[] p_76154_1_)
144
    {
145
        LongHashMap.Entry<V>[] entry = this.hashArray;
146
        int i = p_76154_1_.length;
147
148
        for (int j = 0; j < entry.length; ++j)
149
        {
150
            LongHashMap.Entry<V> entry1 = entry[j];
151
152
            if (entry1 != null)
153
            {
154
                entry[j] = null;
155
156
                while (true)
157
                {
158
                    LongHashMap.Entry<V> entry2 = entry1.nextEntry;
159
                    int k = getHashIndex(entry1.hash, i - 1);
160
                    entry1.nextEntry = p_76154_1_[k];
161
                    p_76154_1_[k] = entry1;
162
                    entry1 = entry2;
163
164
                    if (entry2 == null)
165
                    {
166
                        break;
167
                    }
168
                }
169
            }
170
        }
171
    }
172
173
    /**
174
     * calls the removeKey method and returns removed object
175
     */
176
    public V remove(long p_76159_1_)
177
    {
178
        LongHashMap.Entry<V> entry = this.removeKey(p_76159_1_);
179
        return (V)(entry == null ? null : entry.value);
180
    }
181
182
    final LongHashMap.Entry<V> removeKey(long p_76152_1_)
183
    {
184
        int i = getHashedKey(p_76152_1_);
185
        int j = getHashIndex(i, this.mask);
186
        LongHashMap.Entry<V> entry = this.hashArray[j];
187
        LongHashMap.Entry<V> entry1;
188
        LongHashMap.Entry<V> entry2;
189
190
        for (entry1 = entry; entry1 != null; entry1 = entry2)
191
        {
192
            entry2 = entry1.nextEntry;
193
194
            if (entry1.key == p_76152_1_)
195
            {
196
                ++this.modCount;
197
                --this.numHashElements;
198
199
                if (entry == entry1)
200
                {
201
                    this.hashArray[j] = entry2;
202
                }
203
                else
204
                {
205
                    entry.nextEntry = entry2;
206
                }
207
208
                return entry1;
209
            }
210
211
            entry = entry1;
212
        }
213
214
        return entry1;
215
    }
216
217
    /**
218
     * creates the key in the hash table
219
     */
220
    private void createKey(int p_76156_1_, long p_76156_2_, V p_76156_4_, int p_76156_5_)
221
    {
222
        LongHashMap.Entry<V> entry = this.hashArray[p_76156_5_];
223
        this.hashArray[p_76156_5_] = new LongHashMap.Entry(p_76156_1_, p_76156_2_, p_76156_4_, entry);
224
225
        if (this.numHashElements++ >= this.capacity)
226
        {
227
            this.resizeTable(2 * this.hashArray.length);
228
        }
229
    }
230
231
    static class Entry<V>
232
    {
233
        final long key;
234
        V value;
235
        LongHashMap.Entry<V> nextEntry;
236
        final int hash;
237
238
        Entry(int p_i1553_1_, long p_i1553_2_, V p_i1553_4_, LongHashMap.Entry<V> p_i1553_5_)
239
        {
240
            this.value = p_i1553_4_;
241
            this.nextEntry = p_i1553_5_;
242
            this.key = p_i1553_2_;
243
            this.hash = p_i1553_1_;
244
        }
245
246
        public final long getKey()
247
        {
248
            return this.key;
249
        }
250
251
        public final V getValue()
252
        {
253
            return this.value;
254
        }
255
256
        public final boolean equals(Object p_equals_1_)
257
        {
258
            if (!(p_equals_1_ instanceof LongHashMap.Entry))
259
            {
260
                return false;
261
            }
262
            else
263
            {
264
                LongHashMap.Entry<V> entry = (LongHashMap.Entry)p_equals_1_;
265
                Object object = Long.valueOf(this.getKey());
266
                Object object1 = Long.valueOf(entry.getKey());
267
268
                if (object == object1 || object != null && object.equals(object1))
269
                {
270
                    Object object2 = this.getValue();
271
                    Object object3 = entry.getValue();
272
273
                    if (object2 == object3 || object2 != null && object2.equals(object3))
274
                    {
275
                        return true;
276
                    }
277
                }
278
279
                return false;
280
            }
281
        }
282
283
        public final int hashCode()
284
        {
285
            return LongHashMap.getHashedKey(this.key);
286
        }
287
288
        public final String toString()
289
        {
290
            return this.getKey() + "=" + this.getValue();
291
        }
292
    }
293
}
294

Deobfuscated MCP 1.8.9 source of LongHashMap from Marcelektro/MavenMCP-1.8.9 (net/minecraft/util/LongHashMap.java), stored verbatim.