Skip to content

Commit c42841f

Browse files
committed
Introduce canonicalizeNodeAtIndex function that takes nullable MutabilityOwnership
It replaces updateNodeAtIndex and mutableUpdateNodeAtIndex
1 parent 6b9cdc0 commit c42841f

File tree

1 file changed

+4
-24
lines changed
  • core/commonMain/src/implementations/immutableSet

1 file changed

+4
-24
lines changed

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

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ internal class TrieNode<E>(
117117
}
118118

119119
/** The given [newNode] must not be a part of any persistent set instance. */
120-
private fun updateNodeAtIndex(nodeIndex: Int, newNode: TrieNode<E>): TrieNode<E> {
120+
private fun canonicalizeNodeAtIndex(nodeIndex: Int, newNode: TrieNode<E>, owner: MutabilityOwnership?): TrieNode<E> {
121121
// assert(buffer[nodeIndex] !== newNode)
122122
val cell: Any?
123123

@@ -132,24 +132,6 @@ internal class TrieNode<E>(
132132
cell = newNode
133133
}
134134

135-
return setCellAtIndex(nodeIndex, cell, owner = null)
136-
}
137-
138-
/** The given [newNode] must not be a part of any persistent set instance. */
139-
private fun mutableUpdateNodeAtIndex(nodeIndex: Int, newNode: TrieNode<E>, owner: MutabilityOwnership): TrieNode<E> {
140-
val cell: Any?
141-
142-
val newNodeBuffer = newNode.buffer
143-
if (newNodeBuffer.size == 1 && newNodeBuffer[0] !is TrieNode<*>) {
144-
if (buffer.size == 1) {
145-
newNode.bitmap = bitmap
146-
return newNode
147-
}
148-
cell = newNodeBuffer[0]
149-
} else {
150-
cell = newNode
151-
}
152-
153135
return setCellAtIndex(nodeIndex, cell, owner)
154136
}
155137

@@ -808,7 +790,7 @@ internal class TrieNode<E>(
808790
targetNode.remove(elementHash, element, shift + LOG_MAX_BRANCHING_FACTOR)
809791
}
810792
if (targetNode === newNode) return this
811-
return updateNodeAtIndex(cellIndex, newNode)
793+
return canonicalizeNodeAtIndex(cellIndex, newNode, owner = null)
812794
}
813795
// element is directly in buffer
814796
if (element == buffer[cellIndex]) {
@@ -836,10 +818,8 @@ internal class TrieNode<E>(
836818
// Otherwise the single element would have been lifted up.
837819
// If targetNode is owned by mutator, this node is also owned by mutator. Thus no new node will be created to replace this node.
838820
// If newNode !== targetNode, it is newly created.
839-
if (targetNode.ownedBy === mutator.ownership || targetNode !== newNode) {
840-
return mutableUpdateNodeAtIndex(cellIndex, newNode, mutator.ownership)
841-
}
842-
return this
821+
if (targetNode.ownedBy !== mutator.ownership && targetNode === newNode) return this
822+
return canonicalizeNodeAtIndex(cellIndex, newNode, mutator.ownership)
843823
}
844824
// element is directly in buffer
845825
if (element == buffer[cellIndex]) {

0 commit comments

Comments
 (0)