Skip to content

Commit 24e973a

Browse files
author
Abduqodiri Qurbonzoda
committed
Allow hashSet collisions to be located at any level
1 parent ee4c5f6 commit 24e973a

File tree

6 files changed

+175
-114
lines changed

6 files changed

+175
-114
lines changed

kotlinx-collections-immutable/src/main/kotlin/kotlinx/collections/immutable/implementations/immutableMap/TrieNode.kt

-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ internal const val MAX_BRANCHING_FACTOR = 32
2323
internal const val LOG_MAX_BRANCHING_FACTOR = 5
2424
internal const val MAX_BRANCHING_FACTOR_MINUS_ONE = MAX_BRANCHING_FACTOR - 1
2525
internal const val ENTRY_SIZE = 2
26-
internal const val MAX_SHIFT = 30
2726

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

kotlinx-collections-immutable/src/main/kotlin/kotlinx/collections/immutable/implementations/immutableSet/PersistentHashSet.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ internal class PersistentHashSet<E>(internal val node: TrieNode<E>,
6363
}
6464

6565
internal companion object {
66-
private val EMPTY = PersistentHashSet(TrieNode.EMPTY, 0)
66+
private val EMPTY = PersistentHashSet(CompactTrieNode.EMPTY, 0)
6767
internal fun <E> emptyOf(): PersistentSet<E> = PersistentHashSet.EMPTY
6868
}
6969
}

kotlinx-collections-immutable/src/main/kotlin/kotlinx/collections/immutable/implementations/immutableSet/PersistentHashSetBuilder.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,13 @@ internal class PersistentHashSetBuilder<E>(private var set: PersistentHashSet<E>
5757
override fun remove(element: E): Boolean {
5858
val size = this.size
5959
@Suppress("UNCHECKED_CAST")
60-
node = node.mutableRemove(element.hashCode(), element, 0, this) ?: TrieNode.EMPTY as TrieNode<E>
60+
node = node.mutableRemove(element.hashCode(), element, 0, this) ?: CompactTrieNode.EMPTY as TrieNode<E>
6161
return size != this.size
6262
}
6363

6464
override fun clear() {
6565
@Suppress("UNCHECKED_CAST")
66-
node = TrieNode.EMPTY as TrieNode<E>
66+
node = CompactTrieNode.EMPTY as TrieNode<E>
6767
size = 0
6868
}
6969

kotlinx-collections-immutable/src/main/kotlin/kotlinx/collections/immutable/implementations/immutableSet/PersistentHashSetIterator.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ internal open class PersistentHashSetIterator<E>(node: TrieNode<E>) : Iterator<E
8585
}
8686

8787
internal class TrieNodeIterator<out E> {
88-
private var buffer = TrieNode.EMPTY.buffer
88+
private var buffer = CompactTrieNode.EMPTY.buffer
8989
private var index = 0
9090

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

kotlinx-collections-immutable/src/main/kotlin/kotlinx/collections/immutable/implementations/immutableSet/PersistentHashSetMutableIterator.kt

+2-6
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ internal class PersistentHashSetMutableIterator<E>(private val builder: Persiste
4747
}
4848

4949
private fun resetPath(hashCode: Int, node: TrieNode<*>, element: E, pathIndex: Int) {
50-
if (isCollision(node)) {
50+
if (node.isCollision()) {
5151
val index = node.buffer.indexOf(element)
5252
assert(index != -1)
5353
path[pathIndex].reset(node.buffer, index)
@@ -56,7 +56,7 @@ internal class PersistentHashSetMutableIterator<E>(private val builder: Persiste
5656
}
5757

5858
val position = 1 shl ((hashCode shr (pathIndex * LOG_MAX_BRANCHING_FACTOR)) and MAX_BRANCHING_FACTOR_MINUS_ONE)
59-
val index = Integer.bitCount(node.bitmap and (position - 1))
59+
val index = Integer.bitCount((node as CompactTrieNode<*>).bitmap and (position - 1))
6060

6161
path[pathIndex].reset(node.buffer, index)
6262

@@ -69,10 +69,6 @@ internal class PersistentHashSetMutableIterator<E>(private val builder: Persiste
6969
}
7070
}
7171

72-
private fun isCollision(node: TrieNode<*>): Boolean {
73-
return node.bitmap == 0
74-
}
75-
7672
private fun checkNextWasInvoked() {
7773
if (!nextWasInvoked)
7874
throw IllegalStateException()

0 commit comments

Comments
 (0)