[PersistentCollections] _Node.update → _Node.updateValue

To prevent confusion with the low-level _Node.update method.

This also switches PersistentSet.insert to use `_Node.insert` instead of `_Node.updateValue`, to fix its semantics. `insert` no longer invalidates indices when the new member already exists in the collection.
This commit is contained in:
Karoy Lorentey committed 2022-09-30 12:19:40 -07:00
1 parent 8e3e205954
commit 6dfad714a1
4 files changed
+15 -11

No files matched your search

@@ -120,9 +120,9 @@ extension _Node {
}
@inlinable
internal mutating func update(
internal mutating func updateValue(
_ level: _Level,
_ key: Key,
forKey key: Key,
_ hash: _Hash,
_ inserter: (UnsafeMutablePointer<Element>) -> Void
) -> (inserted: Bool, leaf: _UnmanagedNode, slot: _Slot) {
@@ -159,7 +159,8 @@ extension _Node {
case .descend(_, let slot):
ensureUnique(isUnique: isUnique)
let r = update {
$0[child: slot].update(level.descend(), key, hash, inserter)
$0[child: slot].updateValue(
level.descend(), forKey: key, hash, inserter)
}
if r.inserted { count &+= 1 }
return r
@@ -127,7 +127,7 @@ extension _Node {
_finalizeRemoval(.top, state.hash, at: state.path)
case (false, true):
// Insertion
let r = update(.top, state.key, state.hash) {
let r = updateValue(.top, forKey: state.key, state.hash) {
$0.initialize(to: (state.key, state.value.unsafelyUnwrapped))
}
assert(r.inserted)
@@ -279,7 +279,9 @@ extension PersistentDictionary {
_ value: __owned Value, forKey key: Key
) -> Value? {
let hash = _Hash(key)
let r = _root.update(.top, key, hash) { $0.initialize(to: (key, value)) }
let r = _root.updateValue(.top, forKey: key, hash) {
$0.initialize(to: (key, value))
}
_invalidateIndices()
if r.inserted { return nil }
return _Node.UnsafeHandle.update(r.leaf) {
@@ -296,7 +298,9 @@ extension PersistentDictionary {
_ value: __owned Value, forKey key: Key
) -> Bool {
let hash = _Hash(key)
let r = _root.update(.top, key, hash) { $0.initialize(to: (key, value)) }
let r = _root.updateValue(.top, forKey: key, hash) {
$0.initialize(to: (key, value))
}
if r.inserted { return true }
_Node.UnsafeHandle.update(r.leaf) {
$0[item: r.slot].value = value
@@ -328,7 +332,7 @@ extension PersistentDictionary {
with body: (inout Value) throws -> R
) rethrows -> R {
let hash = _Hash(key)
let r = _root.update(.top, key, hash) {
let r = _root.updateValue(.top, forKey: key, hash) {
$0.initialize(to: (key, defaultValue()))
}
return try _Node.UnsafeHandle.update(r.leaf) {
@@ -24,7 +24,7 @@ extension PersistentSet: SetAlgebra {
) -> (inserted: Bool, memberAfterInsert: Element) {
let hash = _Hash(newMember)
_invalidateIndices()
let r = _root.update(.top, newMember, hash) {
let r = _root.insert(.top, newMember, hash) {
$0.initialize(to: (newMember, ()))
}
if r.inserted {
@@ -39,13 +39,12 @@ extension PersistentSet: SetAlgebra {
@inlinable
internal mutating func _insert(_ newMember: __owned Element) -> Bool {
let hash = _Hash(newMember)
let r = _root.update(.top, newMember, hash) {
let r = _root.insert(.top, newMember, hash) {
$0.initialize(to: (newMember, ()))
}
return r.inserted
}
@discardableResult
@inlinable
public mutating func remove(_ member: Element) -> Element? {
@@ -58,7 +57,7 @@ extension PersistentSet: SetAlgebra {
@inlinable
public mutating func update(with newMember: __owned Element) -> Element? {
let hash = _Hash(newMember)
let r = _root.update(.top, newMember, hash) {
let r = _root.updateValue(.top, forKey: newMember, hash) {
$0.initialize(to: (newMember, ()))
}
if r.inserted { return nil }