Skip to content

Commit 4b94413

Browse files
author
Abduqodiri Qurbonzoda
committed
Allow hashSet collisions to be located at any level
1 parent 01838db commit 4b94413

File tree

6 files changed

+175
-114
lines changed

6 files changed

+175
-114
lines changed

core/commonMain/src/implementations/immutableMap/TrieNode.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ internal const val MAX_BRANCHING_FACTOR = 32
1212
internal const val LOG_MAX_BRANCHING_FACTOR = 5
1313
internal const val MAX_BRANCHING_FACTOR_MINUS_ONE = MAX_BRANCHING_FACTOR - 1
1414
internal const val ENTRY_SIZE = 2
15-
internal const val MAX_SHIFT = 30
1615

1716
/**
1817
* Gets trie index segment of the specified [index] at the level specified by [shift].

core/commonMain/src/implementations/immutableSet/PersistentHashSet.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ internal class PersistentHashSet<E>(internal val node: TrieNode<E>,
5252
}
5353

5454
internal companion object {
55-
private val EMPTY = PersistentHashSet(TrieNode.EMPTY, 0)
55+
private val EMPTY = PersistentHashSet(CompactTrieNode.EMPTY, 0)
5656
internal fun <E> emptyOf(): PersistentSet<E> = PersistentHashSet.EMPTY
5757
}
5858
}

core/commonMain/src/implementations/immutableSet/PersistentHashSetBuilder.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,13 @@ internal class PersistentHashSetBuilder<E>(private var set: PersistentHashSet<E>
4646
override fun remove(element: E): Boolean {
4747
val size = this.size
4848
@Suppress("UNCHECKED_CAST")
49-
node = node.mutableRemove(element.hashCode(), element, 0, this) ?: TrieNode.EMPTY as TrieNode<E>
49+
node = node.mutableRemove(element.hashCode(), element, 0, this) ?: CompactTrieNode.EMPTY as TrieNode<E>
5050
return size != this.size
5151
}
5252

5353
override fun clear() {
5454
@Suppress("UNCHECKED_CAST")
55-
node = TrieNode.EMPTY as TrieNode<E>
55+
node = CompactTrieNode.EMPTY as TrieNode<E>
5656
size = 0
5757
}
5858

core/commonMain/src/implementations/immutableSet/PersistentHashSetIterator.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ internal open class PersistentHashSetIterator<E>(node: TrieNode<E>) : Iterator<E
7878
}
7979

8080
internal class TrieNodeIterator<out E> {
81-
private var buffer = TrieNode.EMPTY.buffer
81+
private var buffer = CompactTrieNode.EMPTY.buffer
8282
private var index = 0
8383

8484
fun reset(buffer: Array<Any?>, index: Int = 0) {

core/commonMain/src/implementations/immutableSet/PersistentHashSetMutableIterator.kt

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ internal class PersistentHashSetMutableIterator<E>(private val builder: Persiste
3838
}
3939

4040
private fun resetPath(hashCode: Int, node: TrieNode<*>, element: E, pathIndex: Int) {
41-
if (isCollision(node)) {
41+
if (node.isCollision()) {
4242
val index = node.buffer.indexOf(element)
4343
assert(index != -1)
4444
path[pathIndex].reset(node.buffer, index)
@@ -48,7 +48,7 @@ internal class PersistentHashSetMutableIterator<E>(private val builder: Persiste
4848

4949
val position = 1 shl ((hashCode shr (pathIndex * LOG_MAX_BRANCHING_FACTOR)) and MAX_BRANCHING_FACTOR_MINUS_ONE)
5050
@UseExperimental(ExperimentalStdlibApi::class)
51-
val index = (node.bitmap and (position - 1)).countOneBits()
51+
val index = ((node as CompactTrieNode<*>).bitmap and (position - 1)).countOneBits()
5252

5353
path[pathIndex].reset(node.buffer, index)
5454

@@ -61,10 +61,6 @@ internal class PersistentHashSetMutableIterator<E>(private val builder: Persiste
6161
}
6262
}
6363

64-
private fun isCollision(node: TrieNode<*>): Boolean {
65-
return node.bitmap == 0
66-
}
67-
6864
private fun checkNextWasInvoked() {
6965
if (!nextWasInvoked)
7066
throw IllegalStateException()

0 commit comments

Comments
 (0)