-
Notifications
You must be signed in to change notification settings - Fork 8
/
bkdtree_erase.go
113 lines (104 loc) · 2.53 KB
/
bkdtree_erase.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package bkdtree
import (
"bytes"
"encoding/binary"
"github.com/pkg/errors"
)
//Erase erases given point.
func (bkd *BkdTree) Erase(point Point) (found bool, err error) {
bkd.rwlock.Lock()
defer bkd.rwlock.Unlock()
if !bkd.open {
err = errors.Errorf("(*BkdTree).Erase is not allowed at closed state")
return
}
//Query T0M with p; if found, delete it and return.
found = bkd.eraseT0M(point)
if found {
bkd.NumPoints--
return
}
//Query each non-empty tree in the forest with p; if found, delete it and return
for i := 0; i < len(bkd.trees); i++ {
found, err = bkd.eraseTi(point, i)
if err != nil {
return
} else if found {
bkd.NumPoints--
return
}
}
return
}
func (bkd *BkdTree) eraseT0M(point Point) (found bool) {
pae := PointArrayExt{
data: bkd.t0m.data,
numPoints: int(bkd.t0m.meta.NumPoints),
byDim: 0, //not used
bytesPerDim: bkd.BytesPerDim,
numDims: bkd.NumDims,
pointSize: bkd.pointSize,
}
found = pae.Erase(point)
if found {
bkd.t0m.meta.NumPoints--
writeMetaNumPoints(bkd.t0m.data, &bkd.t0m.meta)
}
return
}
func (bkd *BkdTree) eraseTi(point Point, idx int) (found bool, err error) {
if bkd.trees[idx].meta.NumPoints <= 0 {
return
}
//depth-first erasing from the root node
meta := &bkd.trees[idx].meta
found, err = bkd.eraseNode(point, bkd.trees[idx].data, meta, int(meta.RootOff))
if err != nil {
return
}
if found {
bkd.trees[idx].meta.NumPoints--
writeMetaNumPoints(bkd.trees[idx].data, &bkd.trees[idx].meta)
return
}
return
}
func (bkd *BkdTree) eraseNode(point Point, data []byte, meta *KdTreeExtMeta, nodeOffset int) (found bool, err error) {
var node KdTreeExtIntraNode
br := bytes.NewReader(data[nodeOffset:])
err = node.Read(br)
if err != nil {
return
}
for i, child := range node.Children {
if child.NumPoints <= 0 {
continue
}
if child.Offset < meta.PointsOffEnd {
//leaf node
pae := PointArrayExt{
data: data[int(child.Offset):],
numPoints: int(child.NumPoints),
byDim: 0, //not used
bytesPerDim: bkd.BytesPerDim,
numDims: bkd.NumDims,
pointSize: bkd.pointSize,
}
found = pae.Erase(point)
} else {
//intra node
found, err = bkd.eraseNode(point, data, meta, int(child.Offset))
}
if err != nil {
return
}
if found {
child.NumPoints--
//Attention: offset calculation shall be synced with KdTreeExtIntraNode definion.
off := nodeOffset + 8*int(node.NumStrips) + 16*i + 8
binary.BigEndian.PutUint64(data[off:], child.NumPoints)
break
}
}
return
}