From 4174087d02405ae1b18f3672a4ed726e69155ecf Mon Sep 17 00:00:00 2001 From: Vishesh Yadav Date: Fri, 18 Aug 2023 19:45:17 -0700 Subject: [PATCH] Cleanup some NodeLeaf generic and some warnings --- Sources/ARTreeModule/ARTree+delete.swift | 13 ++++++------- Sources/ARTreeModule/ARTree+insert.swift | 6 +++--- Sources/ARTreeModule/ARTree+utils.swift | 16 ++++++++++++++++ Sources/ARTreeModule/NodeLeaf.swift | 2 -- 4 files changed, 25 insertions(+), 12 deletions(-) create mode 100644 Sources/ARTreeModule/ARTree+utils.swift diff --git a/Sources/ARTreeModule/ARTree+delete.swift b/Sources/ARTreeModule/ARTree+delete.swift index 7639881c5..910a84aab 100644 --- a/Sources/ARTreeModule/ARTree+delete.swift +++ b/Sources/ARTreeModule/ARTree+delete.swift @@ -26,24 +26,23 @@ extension ARTree { private mutating func _delete(node: RawNode, ref: inout ChildSlotPtr?, key: Key, depth: Int) -> Bool { - var newDepth = depth - var _node = node - - if _node.type == .leaf { - let leaf: NodeLeaf = _node.toLeafNode() + if node.type == .leaf { + let leaf: NodeLeaf = node.toLeafNode() if !leaf.keyEquals(with: key, depth: depth) { return false } ref?.pointee = nil - leaf.withValue(of: Value.self) { + _ = leaf.withValue(of: Value.self) { $0.deinitialize(count: 1) } return true } - var node = _node.toInternalNode() + var newDepth = depth + var node = node.toInternalNode() + if node.partialLength > 0 { let matchedBytes = node.prefixMismatch(withKey: key, fromIndex: depth) assert(matchedBytes <= node.partialLength) diff --git a/Sources/ARTreeModule/ARTree+insert.swift b/Sources/ARTreeModule/ARTree+insert.swift index c871e3303..f69cff06c 100644 --- a/Sources/ARTreeModule/ARTree+insert.swift +++ b/Sources/ARTreeModule/ARTree+insert.swift @@ -28,7 +28,7 @@ extension ARTree { fatalError("replace not supported") } - let newLeaf = NodeLeaf.allocate(key: key, value: value, of: Value.self) + let newLeaf = Self.allocateLeaf(key: key, value: value) var longestPrefix = leaf.longestCommonPrefix(with: newLeaf, fromIndex: depth) var newNode = Node4.allocate() @@ -76,7 +76,7 @@ extension ARTree { node.partialBytes.shiftLeft(toIndex: prefixDiff + 1) node.partialLength -= prefixDiff + 1 - let newLeaf = NodeLeaf.allocate(key: key, value: value, of: Value.self) + let newLeaf = Self.allocateLeaf(key: key, value: value) newNode.addChild(forKey: key[depth + prefixDiff], node: newLeaf) ref?.pointee = newNode.rawNode return true @@ -86,7 +86,7 @@ extension ARTree { // Find next child to continue. guard let next = node.child(forKey: key[depth], ref: &ref) else { // No child, insert leaf within us. - let newLeaf = NodeLeaf.allocate(key: key, value: value, of: Value.self) + let newLeaf = Self.allocateLeaf(key: key, value: value) node.addChild(forKey: key[depth], node: newLeaf, ref: ref) return true } diff --git a/Sources/ARTreeModule/ARTree+utils.swift b/Sources/ARTreeModule/ARTree+utils.swift new file mode 100644 index 000000000..85a518745 --- /dev/null +++ b/Sources/ARTreeModule/ARTree+utils.swift @@ -0,0 +1,16 @@ +//===----------------------------------------------------------------------===// +// +// This source file is part of the Swift Collections open source project +// +// Copyright (c) 2023 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 ARTree { + static func allocateLeaf(key: Key, value: Value) -> NodeLeaf { + return NodeLeaf.allocate(key: key, value: value, of: Value.self) + } +} diff --git a/Sources/ARTreeModule/NodeLeaf.swift b/Sources/ARTreeModule/NodeLeaf.swift index 6d789da86..970fb4eed 100644 --- a/Sources/ARTreeModule/NodeLeaf.swift +++ b/Sources/ARTreeModule/NodeLeaf.swift @@ -10,8 +10,6 @@ //===----------------------------------------------------------------------===// struct NodeLeaf { - typealias Storage = NodeStorage - var storage: Storage }