-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
Expand file tree
/
Copy pathObjectSet.java
More file actions
440 lines (385 loc) · 15.3 KB
/
ObjectSet.java
File metadata and controls
440 lines (385 loc) · 15.3 KB
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
/*******************************************************************************
* Copyright 2011 See AUTHORS file.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
******************************************************************************/
package com.badlogic.gdx.utils;
import java.util.Arrays;
import java.util.Iterator;
import java.util.NoSuchElementException;
import com.badlogic.gdx.math.MathUtils;
/** An unordered set where the keys are objects. Null keys are not allowed. No allocation is done except when growing the table
* size.
* <p>
* This class performs fast contains and remove (typically O(1), worst case O(n) but that is rare in practice). Add may be
* slightly slower, depending on hash collisions. Hashcodes are rehashed to reduce collisions and the need to resize. Load factors
* greater than 0.91 greatly increase the chances to resize to the next higher POT size.
* <p>
* Unordered sets and maps are not designed to provide especially fast iteration. Iteration is faster with OrderedSet and
* OrderedMap.
* <p>
* This implementation uses linear probing with the backward shift algorithm for removal. Hashcodes are rehashed using Fibonacci
* hashing, instead of the more common power-of-two mask, to better distribute poor hashCodes (see <a href=
* "https://probablydance.com/2018/06/16/fibonacci-hashing-the-optimization-that-the-world-forgot-or-a-better-alternative-to-integer-modulo/">Malte
* Skarupke's blog post</a>). Linear probing continues to work even when all hashCodes collide, just more slowly.
* @author Nathan Sweet
* @author Tommy Ettinger */
public class ObjectSet<T> implements Iterable<T> {
public int size;
T[] keyTable;
float loadFactor;
int threshold;
/** Used by {@link #place(Object)} to bit shift the upper bits of a {@code long} into a usable range (>= 0 and <=
* {@link #mask}). The shift can be negative, which is convenient to match the number of bits in mask: if mask is a 7-bit
* number, a shift of -7 shifts the upper 7 bits into the lowest 7 positions. This class sets the shift > 32 and < 64,
* which if used with an int will still move the upper bits of an int to the lower bits due to Java's implicit modulus on
* shifts.
* <p>
* {@link #mask} can also be used to mask the low bits of a number, which may be faster for some hashcodes, if
* {@link #place(Object)} is overridden. */
protected int shift;
/** A bitmask used to confine hashcodes to the size of the table. Must be all 1 bits in its low positions, ie a power of two
* minus 1. If {@link #place(Object)} is overriden, this can be used instead of {@link #shift} to isolate usable bits of a
* hash. */
protected int mask;
private transient ObjectSetIterator iterator1, iterator2;
/** Creates a new set with an initial capacity of 51 and a load factor of 0.8. */
public ObjectSet () {
this(51, 0.8f);
}
/** Creates a new set with a load factor of 0.8.
* @param initialCapacity The backing array size is initialCapacity / loadFactor, increased to the next power of two. */
public ObjectSet (int initialCapacity) {
this(initialCapacity, 0.8f);
}
/** Creates a new set with the specified initial capacity and load factor. This set will hold initialCapacity items before
* growing the backing table.
* @param initialCapacity The backing array size is initialCapacity / loadFactor, increased to the next power of two. */
public ObjectSet (int initialCapacity, float loadFactor) {
if (loadFactor <= 0f || loadFactor >= 1f)
throw new IllegalArgumentException("loadFactor must be > 0 and < 1: " + loadFactor);
this.loadFactor = loadFactor;
int tableSize = tableSize(initialCapacity, loadFactor);
threshold = (int)(tableSize * loadFactor);
mask = tableSize - 1;
shift = Long.numberOfLeadingZeros(mask);
keyTable = (T[])new Object[tableSize];
}
/** Creates a new set identical to the specified set. */
public ObjectSet (ObjectSet<? extends T> set) {
this((int)(set.keyTable.length * set.loadFactor), set.loadFactor);
System.arraycopy(set.keyTable, 0, keyTable, 0, set.keyTable.length);
size = set.size;
}
/** Returns an index >= 0 and <= {@link #mask} for the specified {@code item}.
* <p>
* The default implementation uses Fibonacci hashing on the item's {@link Object#hashCode()}: the hashcode is multiplied by a
* long constant (2 to the 64th, divided by the golden ratio) then the uppermost bits are shifted into the lowest positions to
* obtain an index in the desired range. Multiplication by a long may be slower than int (eg on GWT) but greatly improves
* rehashing, allowing even very poor hashcodes, such as those that only differ in their upper bits, to be used without high
* collision rates. Fibonacci hashing has increased collision rates when all or most hashcodes are multiples of larger
* Fibonacci numbers (see <a href=
* "https://probablydance.com/2018/06/16/fibonacci-hashing-the-optimization-that-the-world-forgot-or-a-better-alternative-to-integer-modulo/">Malte
* Skarupke's blog post</a>).
* <p>
* This method can be overriden to customizing hashing. This may be useful eg in the unlikely event that most hashcodes are
* Fibonacci numbers, if keys provide poor or incorrect hashcodes, or to simplify hashing if keys provide high quality
* hashcodes and don't need Fibonacci hashing: {@code return item.hashCode() & mask;} */
protected int place (T item) {
return (int)(item.hashCode() * 0x9E3779B97F4A7C15L >>> shift);
}
/** Returns the index of the key if already present, else -(index + 1) for the next empty index. This can be overridden in this
* pacakge to compare for equality differently than {@link Object#equals(Object)}. */
int locateKey (T key) {
if (key == null) throw new IllegalArgumentException("key cannot be null.");
T[] keyTable = this.keyTable;
for (int i = place(key);; i = i + 1 & mask) {
T other = keyTable[i];
if (other == null) return -(i + 1); // Empty space is available.
if (other.equals(key)) return i; // Same key was found.
}
}
/** Returns true if the key was added to the set or false if it was already in the set. If this set already contains the key,
* the call leaves the set unchanged and returns false. */
public boolean add (T key) {
int i = locateKey(key);
if (i >= 0) return false; // Existing key was found.
i = -(i + 1); // Empty space was found.
keyTable[i] = key;
if (++size >= threshold) resize(keyTable.length << 1);
return true;
}
public void addAll (Array<? extends T> array) {
addAll(array.items, 0, array.size);
}
public void addAll (Array<? extends T> array, int offset, int length) {
if (offset + length > array.size)
throw new IllegalArgumentException("offset + length must be <= size: " + offset + " + " + length + " <= " + array.size);
addAll(array.items, offset, length);
}
public boolean addAll (T... array) {
return addAll(array, 0, array.length);
}
public boolean addAll (T[] array, int offset, int length) {
ensureCapacity(length);
int oldSize = size;
for (int i = offset, n = i + length; i < n; i++)
add(array[i]);
return oldSize != size;
}
public void addAll (ObjectSet<T> set) {
ensureCapacity(set.size);
T[] keyTable = set.keyTable;
for (int i = 0, n = keyTable.length; i < n; i++) {
T key = keyTable[i];
if (key != null) add(key);
}
}
/** Skips checks for existing keys, doesn't increment size. */
private void addResize (T key) {
T[] keyTable = this.keyTable;
for (int i = place(key);; i = (i + 1) & mask) {
if (keyTable[i] == null) {
keyTable[i] = key;
return;
}
}
}
/** Returns true if the key was removed. */
public boolean remove (T key) {
int i = locateKey(key);
if (i < 0) return false;
T[] keyTable = this.keyTable;
int mask = this.mask, next = i + 1 & mask;
while ((key = keyTable[next]) != null) {
int placement = place(key);
if ((next - placement & mask) > (i - placement & mask)) {
keyTable[i] = key;
i = next;
}
next = next + 1 & mask;
}
keyTable[i] = null;
size--;
return true;
}
/** Returns true if the set has one or more items. */
public boolean notEmpty () {
return size > 0;
}
/** Returns true if the set is empty. */
public boolean isEmpty () {
return size == 0;
}
/** Reduces the size of the backing arrays to be the specified capacity / loadFactor, or less. If the capacity is already less,
* nothing is done. If the set contains more items than the specified capacity, the next highest power of two capacity is used
* instead. */
public void shrink (int maximumCapacity) {
if (maximumCapacity < 0) throw new IllegalArgumentException("maximumCapacity must be >= 0: " + maximumCapacity);
int tableSize = tableSize(maximumCapacity, loadFactor);
if (keyTable.length > tableSize) resize(tableSize);
}
/** Clears the set and reduces the size of the backing arrays to be the specified capacity / loadFactor, if they are larger.
* The reduction is done by allocating new arrays, though for large arrays this can be faster than clearing the existing
* array. */
public void clear (int maximumCapacity) {
int tableSize = tableSize(maximumCapacity, loadFactor);
if (keyTable.length <= tableSize) {
clear();
return;
}
size = 0;
resize(tableSize);
}
/** Clears the set, leaving the backing arrays at the current capacity. When the capacity is high and the population is low,
* iteration can be unnecessarily slow. {@link #clear(int)} can be used to reduce the capacity. */
public void clear () {
if (size == 0) return;
size = 0;
Arrays.fill(keyTable, null);
}
public boolean contains (T key) {
return locateKey(key) >= 0;
}
public @Null T get (T key) {
int i = locateKey(key);
return i < 0 ? null : keyTable[i];
}
public T first () {
T[] keyTable = this.keyTable;
for (int i = 0, n = keyTable.length; i < n; i++)
if (keyTable[i] != null) return keyTable[i];
throw new IllegalStateException("ObjectSet is empty.");
}
/** Increases the size of the backing array to accommodate the specified number of additional items / loadFactor. Useful before
* adding many items to avoid multiple backing array resizes. */
public void ensureCapacity (int additionalCapacity) {
int tableSize = tableSize(size + additionalCapacity, loadFactor);
if (keyTable.length < tableSize) resize(tableSize);
}
private void resize (int newSize) {
int oldCapacity = keyTable.length;
threshold = (int)(newSize * loadFactor);
mask = newSize - 1;
shift = Long.numberOfLeadingZeros(mask);
T[] oldKeyTable = keyTable;
keyTable = (T[])(new Object[newSize]);
if (size > 0) {
for (int i = 0; i < oldCapacity; i++) {
T key = oldKeyTable[i];
if (key != null) addResize(key);
}
}
}
public int hashCode () {
int h = size;
T[] keyTable = this.keyTable;
for (int i = 0, n = keyTable.length; i < n; i++) {
T key = keyTable[i];
if (key != null) h += key.hashCode();
}
return h;
}
public boolean equals (Object obj) {
if (!(obj instanceof ObjectSet)) return false;
ObjectSet other = (ObjectSet)obj;
if (other.size != size) return false;
T[] keyTable = this.keyTable;
for (int i = 0, n = keyTable.length; i < n; i++)
if (keyTable[i] != null && !other.contains(keyTable[i])) return false;
return true;
}
public String toString () {
return '{' + toString(", ") + '}';
}
public String toString (String separator) {
if (size == 0) return "";
java.lang.StringBuilder buffer = new java.lang.StringBuilder(32);
T[] keyTable = this.keyTable;
int i = keyTable.length;
while (i-- > 0) {
T key = keyTable[i];
if (key == null) continue;
buffer.append(key == this ? "(this)" : key);
break;
}
while (i-- > 0) {
T key = keyTable[i];
if (key == null) continue;
buffer.append(separator);
buffer.append(key == this ? "(this)" : key);
}
return buffer.toString();
}
/** Returns an iterator for the keys in the set. Remove is supported.
* <p>
* If {@link Collections#allocateIterators} is false, the same iterator instance is returned each time this method is called.
* Use the {@link ObjectSetIterator} constructor for nested or multithreaded iteration. */
public ObjectSetIterator<T> iterator () {
if (Collections.allocateIterators) return new ObjectSetIterator(this);
if (iterator1 == null) {
iterator1 = new ObjectSetIterator(this);
iterator2 = new ObjectSetIterator(this);
}
if (!iterator1.valid) {
iterator1.reset();
iterator1.valid = true;
iterator2.valid = false;
return iterator1;
}
iterator2.reset();
iterator2.valid = true;
iterator1.valid = false;
return iterator2;
}
static public <T> ObjectSet<T> with (T... array) {
ObjectSet<T> set = new ObjectSet<T>();
set.addAll(array);
return set;
}
static int tableSize (int capacity, float loadFactor) {
if (capacity < 0) throw new IllegalArgumentException("capacity must be >= 0: " + capacity);
int tableSize = MathUtils.nextPowerOfTwo(Math.max(2, (int)Math.ceil(capacity / loadFactor)));
if (tableSize > 1 << 30) throw new IllegalArgumentException("The required capacity is too large: " + capacity);
return tableSize;
}
static public class ObjectSetIterator<K> implements Iterable<K>, Iterator<K> {
public boolean hasNext;
final ObjectSet<K> set;
int nextIndex, currentIndex;
boolean valid = true;
public ObjectSetIterator (ObjectSet<K> set) {
this.set = set;
reset();
}
public void reset () {
currentIndex = -1;
nextIndex = -1;
findNextIndex();
}
private void findNextIndex () {
K[] keyTable = set.keyTable;
for (int n = set.keyTable.length; ++nextIndex < n;) {
if (keyTable[nextIndex] != null) {
hasNext = true;
return;
}
}
hasNext = false;
}
public void remove () {
int i = currentIndex;
if (i < 0) throw new IllegalStateException("next must be called before remove.");
K[] keyTable = set.keyTable;
int mask = set.mask, next = i + 1 & mask;
K key;
while ((key = keyTable[next]) != null) {
int placement = set.place(key);
if ((next - placement & mask) > (i - placement & mask)) {
keyTable[i] = key;
i = next;
}
next = next + 1 & mask;
}
keyTable[i] = null;
set.size--;
if (i != currentIndex) --nextIndex;
currentIndex = -1;
}
public boolean hasNext () {
if (!valid) throw new GdxRuntimeException("#iterator() cannot be used nested.");
return hasNext;
}
public K next () {
if (!hasNext) throw new NoSuchElementException();
if (!valid) throw new GdxRuntimeException("#iterator() cannot be used nested.");
K key = set.keyTable[nextIndex];
currentIndex = nextIndex;
findNextIndex();
return key;
}
public ObjectSetIterator<K> iterator () {
return this;
}
/** Adds the remaining values to the array. */
public Array<K> toArray (Array<K> array) {
while (hasNext)
array.add(next());
return array;
}
/** Returns a new array containing the remaining values. */
public Array<K> toArray () {
return toArray(new Array<K>(true, set.size));
}
}
}