-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch.go
72 lines (61 loc) · 1.33 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
package hdb
import (
"github.com/intdxdt/mbr"
"github.com/TopoSimplify/node"
)
//Search item
func (tree *Hdb) Search(query mbr.MBR) []*node.Node {
var bbox = &query
var result []*node.Node
var nd = &tree.data
if !intersects(bbox, &nd.bbox) {
return []*node.Node{}
}
var nodesToSearch []*dbNode
var child *dbNode
var childBBox *mbr.MBR
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
}
}
return result
}
//All items from root dbNode
func (tree *Hdb) All() []*node.Node {
return all(&tree.data, []*node.Node{})
}
//all - fetch all items from dbNode
func all(nd *dbNode, result []*node.Node) []*node.Node {
var nodesToSearch []*dbNode
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
}