From 75edf6cd697f897ceb099fe6417ffcbf62b7486b Mon Sep 17 00:00:00 2001 From: Karoy Lorentey Date: Sun, 18 Sep 2022 23:19:25 -0700 Subject: [PATCH] [PersistentDictionary] Add support for merging dictionaries & for in-place value mutations --- .../Node/_Node+Subtree Insertions.swift | 49 +++++++++++++++ .../Node/_Node+UnsafeHandle.swift | 18 ++++-- .../PersistentDictionary+Merge.swift | 62 +++++++++++++++++++ .../PersistentDictionary.swift | 26 ++++++++ 4 files changed, 151 insertions(+), 4 deletions(-) create mode 100644 Sources/PersistentCollections/PersistentDictionary/PersistentDictionary+Merge.swift diff --git a/Sources/PersistentCollections/Node/_Node+Subtree Insertions.swift b/Sources/PersistentCollections/Node/_Node+Subtree Insertions.swift index 44968888..537960fa 100644 --- a/Sources/PersistentCollections/Node/_Node+Subtree Insertions.swift +++ b/Sources/PersistentCollections/Node/_Node+Subtree Insertions.swift @@ -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 { diff --git a/Sources/PersistentCollections/Node/_Node+UnsafeHandle.swift b/Sources/PersistentCollections/Node/_Node+UnsafeHandle.swift index a4fd8531..c3d00b8a 100644 --- a/Sources/PersistentCollections/Node/_Node+UnsafeHandle.swift +++ b/Sources/PersistentCollections/Node/_Node+UnsafeHandle.swift @@ -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 diff --git a/Sources/PersistentCollections/PersistentDictionary/PersistentDictionary+Merge.swift b/Sources/PersistentCollections/PersistentDictionary/PersistentDictionary+Merge.swift new file mode 100644 index 00000000..bbedd0f9 --- /dev/null +++ b/Sources/PersistentCollections/PersistentDictionary/PersistentDictionary+Merge.swift @@ -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( + _ 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( + _ 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( + _ 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( + _ 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 + } +} diff --git a/Sources/PersistentCollections/PersistentDictionary/PersistentDictionary.swift b/Sources/PersistentCollections/PersistentDictionary/PersistentDictionary.swift index 8a7e2565..6ba02313 100644 --- a/Sources/PersistentCollections/PersistentDictionary/PersistentDictionary.swift +++ b/Sources/PersistentCollections/PersistentDictionary/PersistentDictionary.swift @@ -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( + forKey key: Key, + with body: (inout Value?) throws -> R + ) rethrows -> R { + try body(&self[key]) + } + + @inlinable + public mutating func updateValue( + 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