Skip to content

Commit

Permalink
Fixing storage reference bug in KDTree
Browse files Browse the repository at this point in the history
  • Loading branch information
LuizZak committed Jul 24, 2024
1 parent 536213a commit 46863ad
Showing 1 changed file with 6 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ public struct KDTree<Element: KDTreeLocatable> where Element.Vector: VectorCompa

/// Inserts a given element on this k-d tree.
public mutating func insert(_ element: Element) {
ensureUnique()

guard let root else {
root = Subdivision(
state: .empty(
Expand All @@ -128,12 +130,13 @@ public struct KDTree<Element: KDTreeLocatable> where Element.Vector: VectorCompa
let path = subdivisionPath(forInserting: element)
let reversed = path.reversed

ensureUnique()
root.inserting(element, path: reversed.asPath)
}

/// If `element` is contained within this k-d tree, it is removed in-place.
public mutating func remove(_ element: Element) where Element: Equatable {
ensureUnique()

guard let root else {
return
}
Expand All @@ -156,11 +159,12 @@ public struct KDTree<Element: KDTreeLocatable> where Element.Vector: VectorCompa

/// Removes an element at a given index in this k-d tree.
public mutating func remove(at index: Index) {
ensureUnique()

guard let root, root.pathExists(index.path) else {
fatalError("Index \(index) is not part of this k-d tree.")
}

ensureUnique()
self.root = root.removing(at: index.path)
}

Expand Down

0 comments on commit 46863ad

Please sign in to comment.