Skip to content
Closed
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
52 changes: 47 additions & 5 deletions rtree.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"fmt"
"math"
"sort"
"strings"
)

// Comparator compares two spatials and returns whether they are equal.
Expand Down Expand Up @@ -71,7 +72,7 @@ func (tree *Rtree) Size() int {
}

func (tree *Rtree) String() string {
return "foo"
return fmt.Sprintf(`{"size":%d,"depth":%d,"root":%v}`, tree.size, tree.height, tree.root)
}

// Depth returns the maximum depth of tree.
Expand Down Expand Up @@ -223,7 +224,17 @@ type node struct {
}

func (n *node) String() string {
return fmt.Sprintf("node{leaf: %v, entries: %v}", n.leaf, n.entries)
format := func(v []entry) string {
var s []string
for _, e := range v {
s = append(s, fmt.Sprintf("%v", e))
}
return fmt.Sprintf(`[%v]`, strings.Join(s, ","))
}
if n.leaf {
return fmt.Sprintf(`{"leaf":%v,"entries":%v}`, n.leaf, format(n.entries))
}
return fmt.Sprintf(`{"entries":%v}`, format(n.entries))
}

// entry represents a spatial index record stored in a tree node.
Expand All @@ -235,9 +246,9 @@ type entry struct {

func (e entry) String() string {
if e.child != nil {
return fmt.Sprintf("entry{bb: %v, child: %v}", e.bb, e.child)
return fmt.Sprintf(`{"bb":"%v","child":%v}`, e.bb, e.child)
}
return fmt.Sprintf("entry{bb: %v, obj: %v}", e.bb, e.obj)
return fmt.Sprintf(`{"bb":"%v"}`, e.bb)
}

// Spatial is an interface for objects that can be stored in an Rtree and queried.
Expand Down Expand Up @@ -549,7 +560,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