scoop:scoop.std:0.1.0
src/collections/hash_storage.scoop
Selected production source · src/collections/hash_storage.scoop
1
package scoop.collections3
internal struct HashEntry<K, V>(val key: K, val value: V, val mixedHash: ULong)5
internal fun hashMixed(hash: Long): ULong {6
var value = hash.toULong()7
value = (value xor (value shr 30L)) * 0xbf58476d1ce4e5b9uL8
value = (value xor (value shr 27L)) * 0x94d049bb133111ebuL9
return value xor (value shr 31L)10
}12
internal fun hashH2(mixedHash: ULong): ULong = mixedHash and 0x7fuL14
internal fun hashControlMatches(word: ULong, control: ULong): ULong {15
val compared = word xor (control * 0x0101010101010101uL)16
return (compared - 0x0101010101010101uL) and compared.inv() and 0x8080808080808080uL17
}19
internal fun hashLowestMatch(mask: ULong): Long {20
var value = mask21
var bits = 0L22
if ((value and 0xffffffffuL) == 0uL) {23
value = value shr 32L;24
bits += 32L25
}26
if ((value and 0xffffuL) == 0uL) {27
value = value shr 16L;28
bits += 16L29
}30
if ((value and 0xffuL) == 0uL) {31
bits += 8L32
}33
return bits / 8L34
}36
internal fun hashControlAt(word: ULong, offset: Long): ULong = (word shr (offset * 8L)) and 0xffuL38
internal fun hashFirstControl(word: ULong, expected: ULong): Long {39
var candidates = hashControlMatches(word, expected)40
while (candidates != 0uL) {41
val offset = hashLowestMatch(candidates)42
if (hashControlAt(word, offset) == expected)43
return offset44
candidates = candidates and (candidates - 1uL)45
}46
return -1L47
}49
// Each control word describes eight entries. Full controls are H2 values in 0..127.50
internal class HashStorage<K, V> internal constructor(capacity: Long) {51
private val controls: MutableArray<ULong> =52
MutableArray<ULong>(capacity / 8L) { _ -> 0x8080808080808080uL }53
private val elements: MutableArray<MaybeUninit<HashEntry<K, V>>> =54
MutableArray<MaybeUninit<HashEntry<K, V>>>(capacity) {55
_ -> MaybeUninit<HashEntry<K, V>>.uninit()56
}58
internal val groupCount: Long get() = controls.size59
internal val capacity: Long get() = elements.size60
internal val groupMask: Long get() = groupCount - 1L62
internal fun read(index: Long): HashEntry<K, V> {63
@Unsafe64
{65
return elements[index].assumeInit()66
}67
}69
internal fun word(group: Long): ULong = controls[group]71
internal fun control(group: Long, offset: Long): ULong = hashControlAt(word(group), offset)73
internal fun matches(group: Long, control: ULong): ULong =74
hashControlMatches(controls[group], control)76
internal fun first(group: Long, expected: ULong): Long = hashFirstControl(word(group), expected)78
internal fun index(group: Long, offset: Long): Long = group * 8L + offset80
internal fun startGroup(mixedHash: ULong): Long =81
((mixedHash shr 7L) and groupMask.toULong()).toLong()83
internal fun isFull(index: Long): Boolean = control(index / 8L, index % 8L) < 0x80uL85
internal fun isDeleted(index: Long): Boolean = control(index / 8L, index % 8L) == 0xfeuL87
private fun setControl(index: Long, value: ULong) {88
val group = index / 8L89
val shift = (index % 8L) * 8L90
val mask = 0xffuL shl shift91
controls[group] = (controls[group] and mask.inv()) or (value shl shift)92
}94
internal fun write(index: Long, entry: HashEntry<K, V>) {95
elements[index] = MaybeUninit<HashEntry<K, V>>.initialized(entry)96
setControl(index, hashH2(entry.mixedHash))97
}99
internal fun replace(index: Long, entry: HashEntry<K, V>) {100
elements[index] = MaybeUninit<HashEntry<K, V>>.initialized(entry)101
}103
// Returns true when a deleted slot could be restored to empty.104
internal fun erase(index: Long): Boolean {105
val group = index / 8L106
val controls = word(group)107
val canBeEmpty = hashControlMatches(controls, 0x80uL) != 0uL108
elements[index] = MaybeUninit<HashEntry<K, V>>.uninit()109
val shift = (index % 8L) * 8L110
val mask = 0xffuL shl shift111
val value =112
if (canBeEmpty)113
0x80uL114
else115
0xfeuL116
this.controls[group] = (controls and mask.inv()) or (value shl shift)117
return canBeEmpty118
}120
internal fun vacant(mixedHash: ULong): Long {121
var group = startGroup(mixedHash)122
var step = 0L123
var visited = 0L124
while (visited < groupCount) {125
val empty = hashControlMatches(word(group), 0x80uL)126
if (empty != 0uL)127
return index(group, hashLowestMatch(empty))128
step += 1L129
group = (group + step) and groupMask130
visited += 1L131
}132
throw IllegalStateException(Some("hash table has no empty slot"))133
}135
internal fun cleared() {136
var group = 0L137
while (group < groupCount) {138
val word = word(group)139
var offset = 0L140
while (offset < 8L) {141
if (hashControlAt(word, offset) < 0x80uL) {142
elements[index(group, offset)] = MaybeUninit<HashEntry<K, V>>.uninit()143
}144
offset += 1L145
}146
controls[group] = 0x8080808080808080uL147
group += 1L148
}149
}151
internal fun rehashed(capacity: Long): HashStorage<K, V> {152
val result = HashStorage<K, V>(capacity)153
var group = 0L154
while (group < groupCount) {155
val word = word(group)156
var offset = 0L157
while (offset < 8L) {158
if (hashControlAt(word, offset) < 0x80uL) {159
val entry = read(index(group, offset))160
result.write(result.vacant(entry.mixedHash), entry)161
}162
offset += 1L163
}164
group += 1L165
}166
return result167
}168
}