Scoop API

scoop:scoop.std:0.1.0

src/collections/hash_storage.scoop

Selected production source · src/collections/hash_storage.scoop

1package scoop.collections
3internal struct HashEntry<K, V>(val key: K, val value: V, val mixedHash: ULong)
5internal fun hashMixed(hash: Long): ULong {
6 var value = hash.toULong()
7 value = (value xor (value shr 30L)) * 0xbf58476d1ce4e5b9uL
8 value = (value xor (value shr 27L)) * 0x94d049bb133111ebuL
9 return value xor (value shr 31L)
12internal fun hashH2(mixedHash: ULong): ULong = mixedHash and 0x7fuL
14internal fun hashControlMatches(word: ULong, control: ULong): ULong {
15 val compared = word xor (control * 0x0101010101010101uL)
16 return (compared - 0x0101010101010101uL) and compared.inv() and 0x8080808080808080uL
19internal fun hashLowestMatch(mask: ULong): Long {
20 var value = mask
21 var bits = 0L
22 if ((value and 0xffffffffuL) == 0uL) {
23 value = value shr 32L;
24 bits += 32L
25 }
26 if ((value and 0xffffuL) == 0uL) {
27 value = value shr 16L;
28 bits += 16L
29 }
30 if ((value and 0xffuL) == 0uL) {
31 bits += 8L
32 }
33 return bits / 8L
36internal fun hashControlAt(word: ULong, offset: Long): ULong = (word shr (offset * 8L)) and 0xffuL
38internal 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 offset
44 candidates = candidates and (candidates - 1uL)
45 }
46 return -1L
49// Each control word describes eight entries. Full controls are H2 values in 0..127.
50internal 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.size
59 internal val capacity: Long get() = elements.size
60 internal val groupMask: Long get() = groupCount - 1L
62 internal fun read(index: Long): HashEntry<K, V> {
63 @Unsafe
64 {
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 + offset
80 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) < 0x80uL
85 internal fun isDeleted(index: Long): Boolean = control(index / 8L, index % 8L) == 0xfeuL
87 private fun setControl(index: Long, value: ULong) {
88 val group = index / 8L
89 val shift = (index % 8L) * 8L
90 val mask = 0xffuL shl shift
91 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 / 8L
106 val controls = word(group)
107 val canBeEmpty = hashControlMatches(controls, 0x80uL) != 0uL
108 elements[index] = MaybeUninit<HashEntry<K, V>>.uninit()
109 val shift = (index % 8L) * 8L
110 val mask = 0xffuL shl shift
111 val value =
112 if (canBeEmpty)
113 0x80uL
114 else
115 0xfeuL
116 this.controls[group] = (controls and mask.inv()) or (value shl shift)
117 return canBeEmpty
118 }
120 internal fun vacant(mixedHash: ULong): Long {
121 var group = startGroup(mixedHash)
122 var step = 0L
123 var visited = 0L
124 while (visited < groupCount) {
125 val empty = hashControlMatches(word(group), 0x80uL)
126 if (empty != 0uL)
127 return index(group, hashLowestMatch(empty))
128 step += 1L
129 group = (group + step) and groupMask
130 visited += 1L
131 }
132 throw IllegalStateException(Some("hash table has no empty slot"))
133 }
135 internal fun cleared() {
136 var group = 0L
137 while (group < groupCount) {
138 val word = word(group)
139 var offset = 0L
140 while (offset < 8L) {
141 if (hashControlAt(word, offset) < 0x80uL) {
142 elements[index(group, offset)] = MaybeUninit<HashEntry<K, V>>.uninit()
143 }
144 offset += 1L
145 }
146 controls[group] = 0x8080808080808080uL
147 group += 1L
148 }
149 }
151 internal fun rehashed(capacity: Long): HashStorage<K, V> {
152 val result = HashStorage<K, V>(capacity)
153 var group = 0L
154 while (group < groupCount) {
155 val word = word(group)
156 var offset = 0L
157 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 += 1L
163 }
164 group += 1L
165 }
166 return result
167 }