-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch.go
76 lines (64 loc) · 1.43 KB
/
search.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package rtree
import (
"github.com/intdxdt/mbr"
)
// Search item
func (tree *RTree) Search(query mbr.MBR[float64]) []BoxObject {
var bbox = &query
var result []BoxObject
var nd = &tree.Data
if !intersects(bbox, &nd.bbox) {
return []BoxObject{}
}
var nodesToSearch []*node
var child *node
var childBBox *mbr.MBR[float64]
for {
for i, length := 0, len(nd.children); i < length; i++ {
child = &nd.children[i]
childBBox = &child.bbox
if intersects(bbox, childBBox) {
if nd.leaf {
result = append(result, child.item)
} else if contains(bbox, childBBox) {
result = all(child, result)
} else {
nodesToSearch = append(nodesToSearch, child)
}
}
}
nd, nodesToSearch = popNode(nodesToSearch)
if nd == nil {
break
}
}
//var objs = make([]BoxObject, 0, len(result))
//for i := range result {
// objs = append(objs, result[i].item)
//}
return result
}
// All items from root node
func (tree *RTree) All() []BoxObject {
return all(&tree.Data, []BoxObject{})
}
// all - fetch all items from node
func all(nd *node, result []BoxObject) []BoxObject {
var nodesToSearch []*node
for {
if nd.leaf {
for i := range nd.children {
result = append(result, nd.children[i].item)
}
} else {
for i := range nd.children {
nodesToSearch = append(nodesToSearch, &nd.children[i])
}
}
nd, nodesToSearch = popNode(nodesToSearch)
if nd == nil {
break
}
}
return result
}