Skip to content

Commit 5ffa476

Browse files
committed
Introduce moveElementToNode function that takes nullable MutabilityOwnership
It replaces moveElementToNode and mutableMoveElementToNode
1 parent c42841f commit 5ffa476

File tree

1 file changed

+5
-17
lines changed
  • core/commonMain/src/implementations/immutableSet

1 file changed

+5
-17
lines changed

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

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -153,21 +153,9 @@ internal class TrieNode<E>(
153153
}
154154

155155
private fun moveElementToNode(elementIndex: Int, newElementHash: Int, newElement: E,
156-
shift: Int): TrieNode<E> {
157-
val newBuffer = buffer.copyOf()
158-
newBuffer[elementIndex] = makeNodeAtIndex(elementIndex, newElementHash, newElement, shift, null)
159-
return TrieNode(bitmap, newBuffer)
160-
}
161-
162-
private fun mutableMoveElementToNode(elementIndex: Int, newElementHash: Int, newElement: E,
163-
shift: Int, owner: MutabilityOwnership): TrieNode<E> {
164-
if (ownedBy === owner) {
165-
buffer[elementIndex] = makeNodeAtIndex(elementIndex, newElementHash, newElement, shift, owner)
166-
return this
167-
}
168-
val newBuffer = buffer.copyOf()
169-
newBuffer[elementIndex] = makeNodeAtIndex(elementIndex, newElementHash, newElement, shift, owner)
170-
return TrieNode(bitmap, newBuffer, owner)
156+
shift: Int, owner: MutabilityOwnership?): TrieNode<E> {
157+
val node = makeNodeAtIndex(elementIndex, newElementHash, newElement, shift, owner)
158+
return setCellAtIndex(elementIndex, node, owner)
171159
}
172160

173161
private fun makeNode(elementHash1: Int, element1: E, elementHash2: Int, element2: E,
@@ -746,7 +734,7 @@ internal class TrieNode<E>(
746734
}
747735
// element is directly in buffer
748736
if (element == buffer[cellIndex]) return this
749-
return moveElementToNode(cellIndex, elementHash, element, shift)
737+
return moveElementToNode(cellIndex, elementHash, element, shift, owner = null)
750738
}
751739

752740
fun mutableAdd(elementHash: Int, element: E, shift: Int, mutator: PersistentHashSetBuilder<*>): TrieNode<E> {
@@ -771,7 +759,7 @@ internal class TrieNode<E>(
771759
// element is directly in buffer
772760
if (element == buffer[cellIndex]) return this
773761
mutator.size++
774-
return mutableMoveElementToNode(cellIndex, elementHash, element, shift, mutator.ownership)
762+
return moveElementToNode(cellIndex, elementHash, element, shift, mutator.ownership)
775763
}
776764

777765
fun remove(elementHash: Int, element: E, shift: Int): TrieNode<E> {

0 commit comments

Comments
 (0)