Skip to content

Commit

Permalink
Merge pull request #60 from tyrzhang/bugfix/tyr_delete_all_in_differe…
Browse files Browse the repository at this point in the history
…nt_order
  • Loading branch information
dhconnelly authored Dec 20, 2024
2 parents 88764d0 + 1cd5974 commit 9d74c6b
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 1 deletion.
33 changes: 32 additions & 1 deletion rtree.go
Original file line number Diff line number Diff line change
Expand Up @@ -549,7 +549,38 @@ func (tree *Rtree) DeleteWithComparator(obj Spatial, cmp Comparator) bool {
tree.condenseTree(n)
tree.size--

if !tree.root.leaf && len(tree.root.entries) == 1 {
/*
when the tree is deep, and deleting nodes, will cause the issue.
the tree could be like this: one obj but 3 levels depth.
{
"size": 1,
"depth": 3,
"root": {
"entries": [
{
"bb": "[1.00, 2.00]x[1.00, 2.00]",
"child": {
"entries": [
{
"bb": "[1.00, 2.00]x[1.00, 2.00]",
"child": {
"leaf": true,
"entries": [
{
"bb": "[1.00, 2.00]x[1.00, 2.00]"
}
]
}
}
]
}
}
]
}
}
so we need to merge the root in loop, instead of once.
*/
for !tree.root.leaf && len(tree.root.entries) == 1 {
tree.root = tree.root.entries[0].child
}

Expand Down
40 changes: 40 additions & 0 deletions rtree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1371,6 +1371,46 @@ func TestMinMaxDistFloatingPointRoundingError(t *testing.T) {
}
}

func TestInsertThenDeleteAllInDifferentOrder(t *testing.T) {
rects := []Rect{
mustRect(Point{1, 1}, []float64{1, 1}),
mustRect(Point{2, 2}, []float64{1, 1}),
mustRect(Point{3, 3}, []float64{1, 1}),
mustRect(Point{4, 4}, []float64{1, 1}),
mustRect(Point{5, 5}, []float64{1, 1}),
}
things := []Spatial{}
for i := range rects {
things = append(things, &rects[i])
}

deleteOrders := [][]int{
{0, 1, 2, 3, 4},
// in this case, the last delete will cause the issue: no thing but 2 levels depth.
// {"size":0,"depth":2,"root":{"entries":[]}}
{1, 2, 3, 4, 0},
}
for _, order := range deleteOrders {
rt := NewTree(2, 2, 2)
for _, thing := range things {
rt.Insert(thing)
}
if rt.Size() != 5 {
t.Errorf("Insert failed to insert")
}

for _, idx := range order {
rt.Delete(things[idx])
}
if rt.Size() != 0 {
t.Errorf("Delete failed to delete, got size: %d, expected size: 0", rt.Size())
}
if rt.Depth() != 1 {
t.Errorf("Delete failed to delete, got depth: %d, expected depth: 1", rt.Depth())
}
}
}

func ensureOrderedSubset(t *testing.T, actual []Spatial, expected []Spatial) {
for i := range actual {
if len(expected)-1 < i || actual[i] != expected[i] {
Expand Down

0 comments on commit 9d74c6b

Please sign in to comment.