From 7ea47d711f85bd331c9616c84ac545f23a784c05 Mon Sep 17 00:00:00 2001 From: Michael Steindorfer Date: Tue, 6 Sep 2022 14:02:54 +0200 Subject: [PATCH] [Capsule] Simplify deletion methods --- .../PersistentDictionary.swift | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/Sources/PersistentCollections/PersistentDictionary.swift b/Sources/PersistentCollections/PersistentDictionary.swift index 6bf71512..31357691 100644 --- a/Sources/PersistentCollections/PersistentDictionary.swift +++ b/Sources/PersistentCollections/PersistentDictionary.swift @@ -88,8 +88,7 @@ public struct PersistentDictionary where Key: Hashable { if let value = optionalValue { insert(key: key, value: value) } else { - let mutate = isKnownUniquelyReferenced(&self.rootNode) - delete(mutate, key: key) + delete(key: key) } } } @@ -134,7 +133,7 @@ public struct PersistentDictionary where Key: Hashable { } else { return self } } - // TODO signature adopted from `Dictionary`, unify with API + // TODO: signature adopted from `Dictionary`, unify with API @discardableResult public mutating func updateValue(_ value: Value, forKey key: Key) -> Value? { let oldValue = get(key) @@ -142,13 +141,9 @@ public struct PersistentDictionary where Key: Hashable { return oldValue } - public mutating func delete(_ key: Key) { - let mutate = isKnownUniquelyReferenced(&self.rootNode) - delete(mutate, key: key) - } + public mutating func delete(key: Key) { + let isStorageKnownUniquelyReferenced = isKnownUniquelyReferenced(&self.rootNode) - // querying `isKnownUniquelyReferenced(&self.rootNode)` from within the body of the function always yields `false` - mutating func delete(_ isStorageKnownUniquelyReferenced: Bool, key: Key) { var effect = DictionaryEffect() let keyHash = computeHash(key) let newRootNode = rootNode.removeOrRemoving(isStorageKnownUniquelyReferenced, key, keyHash, 0, &effect) @@ -169,11 +164,11 @@ public struct PersistentDictionary where Key: Hashable { } else { return self } } - // TODO signature adopted from `Dictionary`, unify with API + // TODO: signature adopted from `Dictionary`, unify with API @discardableResult public mutating func removeValue(forKey key: Key) -> Value? { if let value = get(key) { - delete(key) + delete(key: key) return value } return nil