-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
PatchIntHashMap.java
263 lines (222 loc) · 5.6 KB
/
PatchIntHashMap.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
/**
* This file is LGPL licensed.
* "Borrowed" from CraftBukkit.
*/
package nallar.patched.collection;
import nallar.patched.annotation.FakeExtend;
import nallar.patched.annotation.Generic;
import nallar.tickthreading.patcher.Declare;
import net.minecraft.util.IntHashMap;
import java.util.*;
@SuppressWarnings("unchecked")
@FakeExtend
public abstract class PatchIntHashMap<V> extends IntHashMap<V> {
private static final int EMPTY_KEY = Integer.MIN_VALUE;
private static final int BUCKET_SIZE = 4096;
int[][] keys;
private Object[][] values;
int size;
int modCount;
public PatchIntHashMap() {
initialize();
}
@Override
public boolean containsItem(int key) {
return lookup(key) != null;
}
@Override
@Generic
public V lookup(int key) {
int index = keyIndex(key) & (BUCKET_SIZE - 1);
int[] inner = keys[index];
if (inner == null) {
return null;
}
for (int i = 0; i < inner.length; i++) {
int innerKey = inner[i];
if (innerKey == EMPTY_KEY) {
return null;
} else if (innerKey == key) {
Object[] value = values[index];
if (value != null) {
return (V) value[i];
}
}
}
return null;
}
@Override
public void addKey(int key, Object value) {
put(key, value);
}
@Override
@Declare
public synchronized V put(int key, Object value) {
int index = (int) (keyIndex(key) & (BUCKET_SIZE - 1));
int[] innerKeys = keys[index];
Object[] innerValues = values[index];
if (innerKeys == null) {
// need to make a new chain
keys[index] = innerKeys = new int[8];
Arrays.fill(innerKeys, EMPTY_KEY);
values[index] = innerValues = new Object[8];
innerKeys[0] = key;
innerValues[0] = value;
size++;
} else {
int i;
for (i = 0; i < innerKeys.length; i++) {
// found an empty spot in the chain to put this
int currentKey = innerKeys[i];
if (currentKey == EMPTY_KEY) {
size++;
}
if (currentKey == EMPTY_KEY || currentKey == key) {
Object old = innerValues[i];
innerKeys[i] = key;
innerValues[i] = value;
return (V) old;
}
}
// chain is full, resize it and add our new entry
keys[index] = innerKeys = Arrays.copyOf(innerKeys, i << 1);
Arrays.fill(innerKeys, i, innerKeys.length, EMPTY_KEY);
values[index] = innerValues = Arrays.copyOf(innerValues, i << 1);
innerKeys[i] = key;
innerValues[i] = value;
size++;
}
return null;
}
@Override
public Object removeObject(int key) {
int index = (keyIndex(key) & (BUCKET_SIZE - 1));
int[] inner = keys[index];
if (inner == null) {
return null;
}
for (int i = 0; i < inner.length; i++) {
// hit the end of the chain, didn't find this entry
if (inner[i] == EMPTY_KEY) {
break;
}
if (inner[i] == key) {
Object value = values[index][i];
for (i++; i < inner.length; i++) {
if (inner[i] == EMPTY_KEY) {
break;
}
inner[i - 1] = inner[i];
values[index][i - 1] = values[index][i];
}
inner[i - 1] = EMPTY_KEY;
values[index][i - 1] = null;
size--;
modCount++;
return value;
}
}
return null;
}
@Override
public void clearMap() {
if (size == 0) {
return;
}
modCount++;
size = 0;
Arrays.fill(keys, null);
Arrays.fill(values, null);
}
private void initialize() {
keys = new int[BUCKET_SIZE][];
values = new Object[BUCKET_SIZE][];
}
private static int keyIndex(int key) {
key ^= key >> 16;
key *= 0x85ebca6b;
key ^= key >> 13;
key *= 0xc2b2ae35;
key ^= key >> 16;
return key;
}
private class KeyIterator implements Iterator {
final ValueIterator iterator;
public KeyIterator() {
iterator = new ValueIterator();
}
@Override
public void remove() {
iterator.remove();
}
@Override
public boolean hasNext() {
return iterator.hasNext();
}
@Override
public Integer next() {
iterator.next();
return iterator.prevKey;
}
}
private class ValueIterator implements Iterator {
private int count;
private int index;
private int innerIndex;
private int expectedModCount;
private int lastReturned = EMPTY_KEY;
int prevKey = EMPTY_KEY;
Object prevValue;
ValueIterator() {
expectedModCount = PatchIntHashMap.this.modCount;
}
@Override
public boolean hasNext() {
return count < PatchIntHashMap.this.size;
}
@Override
public void remove() {
if (PatchIntHashMap.this.modCount != expectedModCount) {
throw new ConcurrentModificationException();
}
if (lastReturned == EMPTY_KEY) {
throw new IllegalStateException();
}
count--;
PatchIntHashMap.this.removeObject(lastReturned);
lastReturned = EMPTY_KEY;
expectedModCount = PatchIntHashMap.this.modCount;
}
@Override
public Object next() {
if (PatchIntHashMap.this.modCount != expectedModCount) {
throw new ConcurrentModificationException();
}
if (!hasNext()) {
throw new NoSuchElementException();
}
int[][] keys = PatchIntHashMap.this.keys;
count++;
if (prevKey != EMPTY_KEY) {
innerIndex++;
}
for (; index < keys.length; index++) {
if (keys[index] != null) {
if (innerIndex < keys[index].length) {
int key = keys[index][innerIndex];
Object value = values[index][innerIndex];
if (key == EMPTY_KEY) {
break;
}
lastReturned = key;
prevKey = key;
prevValue = value;
return prevValue;
}
innerIndex = 0;
}
}
throw new NoSuchElementException();
}
}
}