Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading