/
KTypeHashSet.java
820 lines (733 loc) · 22.6 KB
/
KTypeHashSet.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
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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
package com.carrotsearch.hppc;
import java.util.*;
import com.carrotsearch.hppc.cursors.*;
import com.carrotsearch.hppc.predicates.*;
import com.carrotsearch.hppc.procedures.*;
import static com.carrotsearch.hppc.HashContainers.*;
import static com.carrotsearch.hppc.Containers.*;
/**
* A hash set of <code>KType</code>s, implemented using using open addressing
* with linear probing for collision resolution.
*
* <p>
* <strong>Note:</strong> read about
* <a href="{@docRoot}/overview-summary.html#scattervshash">
* important differences between hash and scatter sets</a>.
* </p>
*
* @see KTypeScatterSet
* @see <a href="{@docRoot}/overview-summary.html#interfaces">HPPC interfaces diagram</a>
*/
/*! #if ($TemplateOptions.KTypeGeneric) @SuppressWarnings("unchecked") #end !*/
/*! ${TemplateOptions.generatedAnnotation} !*/
public class KTypeHashSet<KType>
extends AbstractKTypeCollection<KType>
implements /*! #if ($templateonly) !*/ Intrinsics.KeyHasher<KType>, /*! #end !*/
KTypeLookupContainer<KType>,
KTypeSet<KType>,
Preallocable,
Cloneable {
/** The hash array holding keys. */
public /*! #if ($TemplateOptions.KTypeGeneric) !*/
Object []
/*! #else KType [] #end !*/
keys;
/**
* The number of stored keys (assigned key slots), excluding the special
* "empty" key, if any.
*
* @see #size()
* @see #hasEmptyKey
*/
protected int assigned;
/**
* Mask for slot scans in {@link #keys}.
*/
protected int mask;
/**
* We perturb hash values with a container-unique
* seed to avoid problems with nearly-sorted-by-hash
* values on iterations.
*
* @see #hashKey
* @see "http://issues.carrot2.org/browse/HPPC-80"
* @see "http://issues.carrot2.org/browse/HPPC-103"
*/
protected int keyMixer;
/**
* Expand (rehash) {@link #keys} when {@link #assigned} hits this value.
*/
protected int resizeAt;
/**
* Special treatment for the "empty slot" key marker.
*/
protected boolean hasEmptyKey;
/**
* The load factor for {@link #keys}.
*/
protected double loadFactor;
/**
* Per-instance hash order mixing strategy.
* @see #keyMixer
*/
protected HashOrderMixingStrategy orderMixer;
/**
* New instance with sane defaults.
*
* @see #KTypeHashSet(int, double, HashOrderMixingStrategy)
*/
public KTypeHashSet() {
this(DEFAULT_EXPECTED_ELEMENTS, DEFAULT_LOAD_FACTOR);
}
/**
* New instance with sane defaults.
*
* @see #KTypeHashSet(int, double, HashOrderMixingStrategy)
*/
public KTypeHashSet(int expectedElements) {
this(expectedElements, DEFAULT_LOAD_FACTOR);
}
/**
* New instance with sane defaults.
*
* @see #KTypeHashSet(int, double, HashOrderMixingStrategy)
*/
public KTypeHashSet(int expectedElements, double loadFactor) {
this(expectedElements, loadFactor, HashOrderMixing.defaultStrategy());
}
/**
* New instance with the provided defaults.
*
* @param expectedElements
* The expected number of elements guaranteed not to cause a rehash (inclusive).
* @param loadFactor
* The load factor for internal buffers. Insane load factors (zero, full capacity)
* are rejected by {@link #verifyLoadFactor(double)}.
* @param orderMixer
* Hash key order mixing strategy. See {@link HashOrderMixing} for predefined
* implementations. Use constant mixers only if you understand the potential
* consequences.
*/
public KTypeHashSet(int expectedElements, double loadFactor, HashOrderMixingStrategy orderMixer) {
this.orderMixer = orderMixer;
this.loadFactor = verifyLoadFactor(loadFactor);
ensureCapacity(expectedElements);
}
/**
* New instance copying elements from another {@link KTypeContainer}.
*/
public KTypeHashSet(KTypeContainer<? extends KType> container) {
this(container.size());
addAll(container);
}
/**
* {@inheritDoc}
*/
@Override
public boolean add(KType key) {
if (Intrinsics.isEmpty(key)) {
assert Intrinsics.isEmpty(keys[mask + 1]);
boolean added = !hasEmptyKey;
hasEmptyKey = true;
return added;
} else {
final KType [] keys = Intrinsics.<KType[]> cast(this.keys);
final int mask = this.mask;
int slot = hashKey(key) & mask;
KType existing;
while (!Intrinsics.isEmpty(existing = keys[slot])) {
if (Intrinsics.equals(this, key, existing)) {
return false;
}
slot = (slot + 1) & mask;
}
if (assigned == resizeAt) {
allocateThenInsertThenRehash(slot, key);
} else {
keys[slot] = key;
}
assigned++;
return true;
}
}
/**
* Adds all elements from the given list (vararg) to this set.
*
* @return Returns the number of elements actually added as a result of this
* call (not previously present in the set).
*/
/* #if ($TemplateOptions.KTypeGeneric) */
@SafeVarargs
/* #end */
public final int addAll(KType... elements) {
ensureCapacity(elements.length);
int count = 0;
for (KType e : elements) {
if (add(e)) {
count++;
}
}
return count;
}
/**
* Adds all elements from the given {@link KTypeContainer} to this set.
*
* @return Returns the number of elements actually added as a result of this
* call (not previously present in the set).
*/
public int addAll(KTypeContainer<? extends KType> container) {
ensureCapacity(container.size());
return addAll((Iterable<? extends KTypeCursor<? extends KType>>) container);
}
/**
* Adds all elements from the given iterable to this set.
*
* @return Returns the number of elements actually added as a result of this
* call (not previously present in the set).
*/
public int addAll(Iterable<? extends KTypeCursor<? extends KType>> iterable) {
int count = 0;
for (KTypeCursor<? extends KType> cursor : iterable) {
if (add(cursor.value)) {
count++;
}
}
return count;
}
/**
* {@inheritDoc}
*/
@Override
/*! #if ($TemplateOptions.KTypePrimitive)
public KType [] toArray() {
#else !*/
public Object[] toArray() {
/*! #end !*/
final KType[] cloned = Intrinsics.<KType> newArray(size());
int j = 0;
if (hasEmptyKey) {
cloned[j++] = Intrinsics.empty();
}
final KType[] keys = Intrinsics.<KType[]> cast(this.keys);
for (int slot = 0, max = mask; slot <= max; slot++) {
KType existing;
if (!Intrinsics.isEmpty(existing = keys[slot])) {
cloned[j++] = existing;
}
}
return cloned;
}
/**
* An alias for the (preferred) {@link #removeAll}.
*/
public boolean remove(KType key) {
if (Intrinsics.isEmpty(key)) {
boolean hadEmptyKey = hasEmptyKey;
hasEmptyKey = false;
return hadEmptyKey;
} else {
final KType [] keys = Intrinsics.<KType[]> cast(this.keys);
final int mask = this.mask;
int slot = hashKey(key) & mask;
KType existing;
while (!Intrinsics.isEmpty(existing = keys[slot])) {
if (Intrinsics.equals(this, key, existing)) {
shiftConflictingKeys(slot);
return true;
}
slot = (slot + 1) & mask;
}
return false;
}
}
/**
* {@inheritDoc}
*/
@Override
public int removeAll(KType key) {
return remove(key) ? 1 : 0;
}
/**
* {@inheritDoc}
*/
@Override
public int removeAll(KTypePredicate<? super KType> predicate) {
int before = size();
if (hasEmptyKey) {
if (predicate.apply(Intrinsics.<KType> empty())) {
hasEmptyKey = false;
}
}
final KType[] keys = Intrinsics.<KType[]> cast(this.keys);
for (int slot = 0, max = this.mask; slot <= max;) {
KType existing;
if (!Intrinsics.isEmpty(existing = keys[slot])) {
if (predicate.apply(existing)) {
shiftConflictingKeys(slot);
continue; // Repeat the check for the same slot i (shifted).
}
}
slot++;
}
return before - size();
}
/**
* {@inheritDoc}
*/
@Override
public boolean contains(KType key) {
if (Intrinsics.isEmpty(key)) {
return hasEmptyKey;
} else {
final KType [] keys = Intrinsics.<KType[]> cast(this.keys);
final int mask = this.mask;
int slot = hashKey(key) & mask;
KType existing;
while (!Intrinsics.isEmpty(existing = keys[slot])) {
if (Intrinsics.equals(this, key, existing)) {
return true;
}
slot = (slot + 1) & mask;
}
return false;
}
}
/**
* {@inheritDoc}
*/
@Override
public void clear() {
assigned = 0;
hasEmptyKey = false;
Arrays.fill(keys, Intrinsics.<KType> empty());
}
/**
* {@inheritDoc}
*/
@Override
public void release() {
assigned = 0;
hasEmptyKey = false;
keys = null;
ensureCapacity(Containers.DEFAULT_EXPECTED_ELEMENTS);
}
/**
* {@inheritDoc}
*/
@Override
public boolean isEmpty() {
return size() == 0;
}
/**
* Ensure this container can hold at least the
* given number of elements without resizing its buffers.
*
* @param expectedElements The total number of elements, inclusive.
*/
@Override
public void ensureCapacity(int expectedElements) {
if (expectedElements > resizeAt || keys == null) {
final KType[] prevKeys = Intrinsics.<KType[]> cast(this.keys);
allocateBuffers(minBufferSize(expectedElements, loadFactor));
if (prevKeys != null && !isEmpty()) {
rehash(prevKeys);
}
}
}
/**
* {@inheritDoc}
*/
@Override
public int size() {
return assigned + (hasEmptyKey ? 1 : 0);
}
/**
* {@inheritDoc}
*/
@Override
public int hashCode() {
int h = hasEmptyKey ? 0xDEADBEEF : 0;
final KType[] keys = Intrinsics.<KType[]> cast(this.keys);
for (int slot = mask; slot >= 0; slot--) {
KType existing;
if (!Intrinsics.isEmpty(existing = keys[slot])) {
h += BitMixer.mix(existing);
}
}
return h;
}
/**
* {@inheritDoc}
*/
@Override
public boolean equals(Object obj) {
return obj != null &&
getClass() == obj.getClass() &&
sameKeys(getClass().cast(obj));
}
/**
* Return true if all keys of some other container exist in this container.
#if ($TemplateOptions.KTypeGeneric)
* Equality comparison is performed with this object's {@link #equals(Object, Object)}
* method.
#end
*/
private boolean sameKeys(KTypeSet<?> other) {
if (other.size() != size()) {
return false;
}
for (KTypeCursor<?> c : other) {
if (!contains(Intrinsics.<KType> cast(c.value))) {
return false;
}
}
return true;
}
/**
* {@inheritDoc}
*/
@Override
public KTypeHashSet<KType> clone() {
try {
/* #if ($templateOnly) */ @SuppressWarnings("unchecked") /* #end */
KTypeHashSet<KType> cloned = (KTypeHashSet<KType>) super.clone();
cloned.keys = keys.clone();
cloned.hasEmptyKey = cloned.hasEmptyKey;
cloned.orderMixer = orderMixer.clone();
return cloned;
} catch (CloneNotSupportedException e) {
throw new RuntimeException(e);
}
}
/**
* {@inheritDoc}
*/
@Override
public Iterator<KTypeCursor<KType>> iterator() {
return new EntryIterator();
}
/**
* An iterator implementation for {@link #iterator}.
*/
protected final class EntryIterator extends AbstractIterator<KTypeCursor<KType>> {
private final KTypeCursor<KType> cursor;
private final int max = mask + 1;
private int slot = -1;
public EntryIterator() {
cursor = new KTypeCursor<KType>();
}
@Override
protected KTypeCursor<KType> fetch() {
if (slot < max) {
KType existing;
for (slot++; slot < max; slot++) {
if (!Intrinsics.isEmpty(existing = Intrinsics.<KType> cast(keys[slot]))) {
cursor.index = slot;
cursor.value = existing;
return cursor;
}
}
}
if (slot == max && hasEmptyKey) {
cursor.index = slot;
cursor.value = Intrinsics.empty();
slot++;
return cursor;
}
return done();
}
}
/**
* {@inheritDoc}
*/
@Override
public <T extends KTypeProcedure<? super KType>> T forEach(T procedure) {
if (hasEmptyKey) {
procedure.apply(Intrinsics.<KType> empty());
}
final KType[] keys = Intrinsics.<KType[]> cast(this.keys);
for (int slot = 0, max = this.mask; slot <= max;) {
KType existing;
if (!Intrinsics.isEmpty(existing = keys[slot])) {
procedure.apply(existing);
}
}
return procedure;
}
/**
* {@inheritDoc}
*/
@Override
public <T extends KTypePredicate<? super KType>> T forEach(T predicate) {
if (hasEmptyKey) {
if (!predicate.apply(Intrinsics.<KType> empty())) {
return predicate;
}
}
final KType[] keys = Intrinsics.<KType[]> cast(this.keys);
for (int slot = 0, max = this.mask; slot <= max;) {
KType existing;
if (!Intrinsics.isEmpty(existing = keys[slot])) {
if (!predicate.apply(existing)) {
break;
}
}
}
return predicate;
}
/**
* Create a set from a variable number of arguments or an array of
* <code>KType</code>. The elements are copied from the argument to the
* internal buffer.
*/
/* #if ($TemplateOptions.KTypeGeneric) */
@SafeVarargs
/* #end */
public static <KType> KTypeHashSet<KType> from(KType... elements) {
final KTypeHashSet<KType> set = new KTypeHashSet<KType>(elements.length);
set.addAll(elements);
return set;
}
/**
* Returns a hash code for the given key.
*
* The default implementation mixes the hash of the key with {@link #keyMixer}
* to differentiate hash order of keys between hash containers. Helps
* alleviate problems resulting from linear conflict resolution in open
* addressing.
*
* The output from this function should evenly distribute keys across the
* entire integer range.
*/
/*! #if ($templateonly) !*/
@Override
public
/*! #else protected #end !*/
int hashKey(KType key) {
assert !Intrinsics.isEmpty(key); // Handled as a special case (empty slot marker).
return BitMixer.mix(key, this.keyMixer);
}
/**
* Returns a logical "index" of a given key that can be used to speed up
* follow-up logic in certain scenarios (conditional logic).
*
* The semantics of "indexes" are not strictly defined. Indexes may
* (and typically won't be) contiguous.
*
* The index is valid only between modifications (it will not be affected
* by read-only operations).
*
* @see #indexExists
* @see #indexGet
* @see #indexInsert
* @see #indexReplace
*
* @param key
* The key to locate in the set.
* @return A non-negative value of the logical "index" of the key in the set
* or a negative value if the key did not exist.
*/
public int indexOf(KType key) {
final int mask = this.mask;
if (Intrinsics.<KType> isEmpty(key)) {
return hasEmptyKey ? mask + 1 : ~(mask + 1);
} else {
final KType[] keys = Intrinsics.<KType[]> cast(this.keys);
int slot = hashKey(key) & mask;
KType existing;
while (!Intrinsics.<KType> isEmpty(existing = keys[slot])) {
if (Intrinsics.<KType> equals(this, key, existing)) {
return slot;
}
slot = (slot + 1) & mask;
}
return ~slot;
}
}
/**
* @see #indexOf
*
* @param index The index of a given key, as returned from {@link #indexOf}.
* @return Returns <code>true</code> if the index corresponds to an existing key
* or false otherwise. This is equivalent to checking whether the index is
* a positive value (existing keys) or a negative value (non-existing keys).
*/
public boolean indexExists(int index) {
assert index < 0 ||
(index >= 0 && index <= mask) ||
(index == mask + 1 && hasEmptyKey);
return index >= 0;
}
/**
* Returns the exact value of the existing key. This method makes sense for sets
* of objects which define custom key-equality relationship.
*
* @see #indexOf
*
* @param index The index of an existing key.
* @return Returns the equivalent key currently stored in the set.
* @throws AssertionError If assertions are enabled and the index does
* not correspond to an existing key.
*/
public KType indexGet(int index) {
assert index >= 0 : "The index must point at an existing key.";
assert index <= mask ||
(index == mask + 1 && hasEmptyKey);
return Intrinsics.<KType> cast(keys[index]);
}
/**
* Replaces the existing equivalent key with the given one and returns any previous value
* stored for that key.
*
* @see #indexOf
*
* @param index The index of an existing key.
* @param equivalentKey The key to put in the set as a replacement. Must be equivalent to
* the key currently stored at the provided index.
* @return Returns the previous key stored in the set.
* @throws AssertionError If assertions are enabled and the index does
* not correspond to an existing key.
*/
public KType indexReplace(int index, KType equivalentKey) {
assert index >= 0 : "The index must point at an existing key.";
assert index <= mask ||
(index == mask + 1 && hasEmptyKey);
assert Intrinsics.equals(this, keys[index], equivalentKey);
KType previousValue = Intrinsics.<KType> cast(keys[index]);
keys[index] = equivalentKey;
return previousValue;
}
/**
* Inserts a key for an index that is not present in the set. This method
* may help in avoiding double recalculation of the key's hash.
*
* @see #indexOf
*
* @param index The index of a previously non-existing key, as returned from
* {@link #indexOf}.
* @throws AssertionError If assertions are enabled and the index does
* not correspond to an existing key.
*/
public void indexInsert(int index, KType key) {
assert index < 0 : "The index must not point at an existing key.";
index = ~index;
if (Intrinsics.isEmpty(key)) {
assert index == mask + 1;
assert Intrinsics.isEmpty(keys[index]);
hasEmptyKey = true;
} else {
assert Intrinsics.isEmpty(keys[index]);
if (assigned == resizeAt) {
allocateThenInsertThenRehash(index, key);
} else {
keys[index] = key;
}
assigned++;
}
}
/**
* Validate load factor range and return it. Override and suppress if you need
* insane load factors.
*/
protected double verifyLoadFactor(double loadFactor) {
checkLoadFactor(loadFactor, MIN_LOAD_FACTOR, MAX_LOAD_FACTOR);
return loadFactor;
}
/**
* Rehash from old buffers to new buffers.
*/
protected void rehash(KType[] fromKeys) {
assert HashContainers.checkPowerOfTwo(fromKeys.length - 1);
// Rehash all stored keys into the new buffers.
final KType[] keys = Intrinsics.<KType[]> cast(this.keys);
final int mask = this.mask;
KType existing;
for (int i = fromKeys.length - 1; --i >= 0;) {
if (!Intrinsics.isEmpty(existing = fromKeys[i])) {
int slot = hashKey(existing) & mask;
while (!Intrinsics.isEmpty(keys[slot])) {
slot = (slot + 1) & mask;
}
keys[slot] = existing;
}
}
}
/**
* Allocate new internal buffers. This method attempts to allocate
* and assign internal buffers atomically (either allocations succeed or not).
*/
protected void allocateBuffers(int arraySize) {
assert Integer.bitCount(arraySize) == 1;
// Compute new hash mixer candidate before expanding.
final int newKeyMixer = this.orderMixer.newKeyMixer(arraySize);
// Ensure no change is done if we hit an OOM.
KType[] prevKeys = Intrinsics.<KType[]> cast(this.keys);
try {
int emptyElementSlot = 1;
this.keys = Intrinsics.<KType> newArray(arraySize + emptyElementSlot);
} catch (OutOfMemoryError e) {
this.keys = prevKeys;
throw new BufferAllocationException(
"Not enough memory to allocate buffers for rehashing: %,d -> %,d",
e,
this.keys == null ? 0 : size(),
arraySize);
}
this.resizeAt = expandAtCount(arraySize, loadFactor);
this.keyMixer = newKeyMixer;
this.mask = arraySize - 1;
}
/**
* This method is invoked when there is a new key to be inserted into
* the buffer but there is not enough empty slots to do so.
*
* New buffers are allocated. If this succeeds, we know we can proceed
* with rehashing so we assign the pending element to the previous buffer
* (possibly violating the invariant of having at least one empty slot)
* and rehash all keys, substituting new buffers at the end.
*/
protected void allocateThenInsertThenRehash(int slot, KType pendingKey) {
assert assigned == resizeAt
&& Intrinsics.isEmpty(Intrinsics.<KType> cast(keys[slot]))
&& !Intrinsics.isEmpty(pendingKey);
// Try to allocate new buffers first. If we OOM, we leave in a consistent state.
final KType[] prevKeys = Intrinsics.<KType[]> cast(this.keys);
allocateBuffers(nextBufferSize(mask + 1, size(), loadFactor));
assert this.keys.length > prevKeys.length;
// We have succeeded at allocating new data so insert the pending key/value at
// the free slot in the old arrays before rehashing.
prevKeys[slot] = pendingKey;
// Rehash old keys, including the pending key.
rehash(prevKeys);
}
/**
* Shift all the slot-conflicting keys allocated to (and including) <code>slot</code>.
*/
protected void shiftConflictingKeys(int gapSlot) {
final KType[] keys = Intrinsics.<KType[]> cast(this.keys);
final int mask = this.mask;
// Perform shifts of conflicting keys to fill in the gap.
int distance = 0;
while (true) {
final int slot = (gapSlot + (++distance)) & mask;
final KType existing = keys[slot];
if (Intrinsics.isEmpty(existing)) {
break;
}
final int idealSlot = hashKey(existing);
final int shift = (slot - idealSlot) & mask;
if (shift >= distance) {
// Entry at this position was originally at or before the gap slot.
// Move the conflict-shifted entry to the gap's position and repeat the procedure
// for any entries to the right of the current position, treating it
// as the new gap.
keys[gapSlot] = existing;
gapSlot = slot;
distance = 0;
}
}
// Mark the last found gap slot without a conflict as empty.
keys[gapSlot] = Intrinsics.empty();
assigned--;
}
}