[PersistentDictionary] Add support for merging dictionaries & for in-place value mutations

This commit is contained in:
Karoy Lorentey committed 2022-09-19 21:25:07 -07:00
1 parent ea30715a6c
commit 75edf6cd69
4 files changed
+151 -4

No files matched your search

@@ -63,6 +63,55 @@ extension _Node {
return old
}
}
@inlinable
internal mutating func insertValue(
forKey key: Key,
_ level: _Level,
_ hash: _Hash,
with value: () -> Value
) -> (inserted: Bool, leaf: _UnmanagedNode, slot: _Slot) {
defer { _invariantCheck() }
let isUnique = self.isUnique()
let r = find(level, key, hash, forInsert: true)
switch r {
case .found(_, let slot):
ensureUnique(isUnique: isUnique)
_invariantCheck() // FIXME
return (false, unmanaged, slot)
case .notFound(let bucket, let slot):
ensureUniqueAndInsertItem(isUnique: isUnique, slot, bucket) {
$0.initialize(to: (key, value()))
}
return (true, unmanaged, slot)
case .newCollision(let bucket, let slot):
let r = ensureUniqueAndMakeNewCollision(
isUnique: isUnique,
level: level,
replacing: slot, bucket,
newHash: hash
) {
$0.initialize(to: (key, value()))
}
return (true, r.leaf, r.slot)
case .expansion(let collisionHash):
let r = _Node.build(
level: level,
item1: { $0.initialize(to: (key, value())) }, hash,
child2: self, collisionHash
)
self = r.top
return (true, r.leaf, r.slot1)
case .descend(_, let slot):
ensureUnique(isUnique: isUnique)
let r = update {
$0[child: slot]
.insertValue(forKey: key, level.descend(), hash, with: value)
}
if r.inserted { count &+= 1 }
return r
}
}
}
extension _Node {
@@ -195,8 +195,13 @@ extension _Node.UnsafeHandle {
@inlinable
internal subscript(child slot: _Slot) -> _Node {
unsafeAddress { UnsafePointer(childPtr(at: slot)) }
nonmutating unsafeMutableAddress { childPtr(at: slot) }
unsafeAddress {
UnsafePointer(childPtr(at: slot))
}
nonmutating unsafeMutableAddress {
assertMutable()
return childPtr(at: slot)
}
}
@inlinable
@@ -240,8 +245,13 @@ extension _Node.UnsafeHandle {
@inlinable
internal subscript(item slot: _Slot) -> Element {
unsafeAddress { UnsafePointer(itemPtr(at: slot)) }
unsafeMutableAddress { itemPtr(at: slot) }
unsafeAddress {
UnsafePointer(itemPtr(at: slot))
}
nonmutating unsafeMutableAddress {
assertMutable()
return itemPtr(at: slot)
}
}
@inlinable
@@ -0,0 +1,62 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift Collections open source project
//
// Copyright (c) 2022 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
//
//===----------------------------------------------------------------------===//
extension PersistentDictionary {
@inlinable
public mutating func merge<S: Sequence>(
_ keysAndValues: __owned S,
uniquingKeysWith combine: (Value, Value) throws -> Value
) rethrows where S.Element == (Key, Value) {
for (key, value) in keysAndValues {
try self.updateValue(forKey: key) { target in
if let old = target {
target = try combine(old, value)
} else {
target = value
}
}
}
}
@_disfavoredOverload // https://github.com/apple/swift-collections/issues/125
@inlinable
public mutating func merge<S: Sequence>(
_ keysAndValues: __owned S,
uniquingKeysWith combine: (Value, Value) throws -> Value
) rethrows where S.Element == Element {
try merge(
keysAndValues.lazy.map { ($0.key, $0.value) },
uniquingKeysWith: combine)
}
@inlinable
public func merging<S: Sequence>(
_ other: __owned S,
uniquingKeysWith combine: (Value, Value) throws -> Value
) rethrows -> Self
where S.Element == (Key, Value) {
var copy = self
try copy.merge(other, uniquingKeysWith: combine)
return copy
}
@_disfavoredOverload // https://github.com/apple/swift-collections/issues/125
@inlinable
public func merging<S: Sequence>(
_ other: __owned S,
uniquingKeysWith combine: (Value, Value) throws -> Value
) rethrows -> Self
where S.Element == Element {
var copy = self
try copy.merge(other, uniquingKeysWith: combine)
return copy
}
}
@@ -250,6 +250,7 @@ extension PersistentDictionary {
}
}
// FIXME: New addition; needs discussion
@inlinable
public func contains(_ key: Key) -> Bool {
_root.containsKey(.top, key, _Hash(key))
@@ -330,6 +331,31 @@ extension PersistentDictionary {
return copy
}
// FIXME: New addition; needs discussion
@inlinable @inline(__always)
public mutating func updateValue<R>(
forKey key: Key,
with body: (inout Value?) throws -> R
) rethrows -> R {
try body(&self[key])
}
@inlinable
public mutating func updateValue<R>(
forKey key: Key,
default defaultValue: @autoclosure () -> Value,
with body: (inout Value) throws -> R
) rethrows -> R {
let hash = _Hash(key)
let r = _root.insertValue(forKey: key, .top, hash, with: defaultValue)
if r.inserted {
_invalidateIndices()
}
return try _Node.UnsafeHandle.update(r.leaf) {
try body(&$0[item: r.slot].value)
}
}
/// Removes the given key and its associated value from the dictionary.
///
/// If the key is found in the dictionary, this method returns the key's