Skip to content

Commit

Permalink
Fix deletion when node4/16 is full
Browse files Browse the repository at this point in the history
  • Loading branch information
vishesh committed Aug 19, 2023
1 parent 91e2c7c commit f9a5452
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 11 deletions.
7 changes: 2 additions & 5 deletions Sources/ARTreeModule/Node16.swift
Original file line number Diff line number Diff line change
Expand Up @@ -194,12 +194,9 @@ extension Node16: InternalNode {
assert(index < Self.numKeys, "index can't >= 16 in Node16")
assert(index < count, "not enough childs in node")

let childBuf = child(at: index)
// childBuf?.deallocate()

withBody { keys, childs in
keys[self.count] = 0
childs[self.count] = nil
keys[index] = 0
childs[index] = nil

count -= 1
keys.shiftLeft(startIndex: index + 1, endIndex: count, by: 1)
Expand Down
1 change: 0 additions & 1 deletion Sources/ARTreeModule/Node256.swift
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,6 @@ extension Node256: InternalNode {

public mutating func deleteChild(at index: Index, ref: ChildSlotPtr?) {
return withBody { childs in
// childs[index]?.deallocate()
childs[index] = nil
count -= 1

Expand Down
7 changes: 2 additions & 5 deletions Sources/ARTreeModule/Node4.swift
Original file line number Diff line number Diff line change
Expand Up @@ -168,12 +168,9 @@ extension Node4: InternalNode {
assert(index < 4, "index can't >= 4 in Node4")
assert(index < count, "not enough childs in node")

let childBuf = child(at: index)
// childBuf?.deallocate()

withBody { keys, childs in
keys[self.count] = 0
childs[self.count] = nil
keys[index] = 0
childs[index] = nil

count -= 1
keys.shiftLeft(startIndex: index + 1, endIndex: count, by: 1)
Expand Down
29 changes: 29 additions & 0 deletions Tests/ARTreeModuleTests/Node4Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,35 @@ final class ARTreeNode4Tests: XCTestCase {
XCTAssertEqual(ptr?.type, .leaf)
}

func test4DeleteFromFull() throws {
var node = Node4.allocate()
node.addChild(forKey: 1, node: NodeLeaf.allocate(key: [1], value: 1, of: Int.self))
node.addChild(forKey: 2, node: NodeLeaf.allocate(key: [2], value: 2, of: Int.self))
node.addChild(forKey: 3, node: NodeLeaf.allocate(key: [3], value: 3, of: Int.self))
node.addChild(forKey: 4, node: NodeLeaf.allocate(key: [4], value: 4, of: Int.self))
XCTAssertEqual(node.type, .node4)
XCTAssertEqual(
node.print(value: Int.self),
"○ Node4 {childs=4, partial=[]}\n" +
"├──○ 1: 1[1] -> 1\n" +
"├──○ 2: 1[2] -> 2\n" +
"├──○ 3: 1[3] -> 3\n" +
"└──○ 4: 1[4] -> 4")
node.deleteChild(at: 1)
XCTAssertEqual(
node.print(value: Int.self),
"○ Node4 {childs=3, partial=[]}\n" +
"├──○ 1: 1[1] -> 1\n" +
"├──○ 3: 1[3] -> 3\n" +
"└──○ 4: 1[4] -> 4")

var ptr: RawNode? = node.rawNode
node.deleteChild(at: 1, ref: &ptr)
node.deleteChild(at: 1, ref: &ptr)
XCTAssertNotNil(ptr)
XCTAssertEqual(ptr?.type, .leaf)
}

func test4ExapandTo16() throws {
var node = Node4.allocate()
node.addChild(forKey: 1, node: NodeLeaf.allocate(key: [1], value: [1], of: [UInt8].self))
Expand Down

0 comments on commit f9a5452

Please sign in to comment.