Skip to content

Commit

Permalink
Cleanup some NodeLeaf generic and some warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
vishesh committed Aug 19, 2023
1 parent f9a5452 commit 4174087
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 12 deletions.
13 changes: 6 additions & 7 deletions Sources/ARTreeModule/ARTree+delete.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions Sources/ARTreeModule/ARTree+insert.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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
Expand All @@ -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
}
Expand Down
16 changes: 16 additions & 0 deletions Sources/ARTreeModule/ARTree+utils.swift
Original file line number Diff line number Diff line change
@@ -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)
}
}
2 changes: 0 additions & 2 deletions Sources/ARTreeModule/NodeLeaf.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
//===----------------------------------------------------------------------===//

struct NodeLeaf {
typealias Storage = NodeStorage<Self>

var storage: Storage
}

Expand Down

0 comments on commit 4174087

Please sign in to comment.