-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsplit.go
82 lines (67 loc) · 1.9 KB
/
split.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
package hdb
import (
"math"
"sort"
)
// _split overflowed dbNode into two
func (tree *Hdb) split(insertPath []*dbNode, level int) {
var nd = insertPath[level]
var newNode = createDBNode(nil, nd.height, nd.leaf, []dbNode{})
var M = len(nd.children)
var m = tree.minEntries
tree.chooseSplitAxis(nd, m, M)
var at = tree.chooseSplitIndex(nd, m, M)
//perform split at index
nd.children, newNode.children = splitAtIndex(nd.children, at)
calcBBox(nd)
calcBBox(&newNode)
if level > 0 {
insertPath[level-1].addChild(newNode)
} else {
tree.splitRoot(*nd, newNode)
}
}
//_splitRoot splits the root of tree.
func (tree *Hdb) splitRoot(nd, other dbNode) {
// split root dbNode
tree.data = createDBNode(nil, nd.height+1, false, []dbNode{nd, other}, )
calcBBox(&tree.data)
}
//_chooseSplitIndex selects split index.
func (tree *Hdb) chooseSplitIndex(nd *dbNode, m, M int) int {
var i, index int
var overlap, area, minOverlap, minArea float64
minOverlap, minArea = math.Inf(1), math.Inf(1)
for i = m; i <= M-m; i++ {
var bbox1 = distBBox(nd, 0, i)
var bbox2 = distBBox(nd, i, M)
overlap = intersectionArea(&bbox1, &bbox2)
area = bbox1.Area() + bbox2.Area()
// choose distribution with minimum overlap
if overlap < minOverlap {
minOverlap = overlap
index = i
if area < minArea {
minArea = area
}
} else if overlap == minOverlap {
// otherwise choose distribution with minimum area
if area < minArea {
minArea = area
index = i
}
}
}
return index
}
//_chooseSplitAxis selects split axis : sorts dbNode children
//by the best axis for split.
func (tree *Hdb) chooseSplitAxis(nd *dbNode, m, M int) {
var xMargin = tree.allDistMargin(nd, m, M, byX)
var yMargin = tree.allDistMargin(nd, m, M, byY)
// if total distributions margin value is minimal for x, sort by minX,
// otherwise it's already sorted by minY
if xMargin < yMargin {
sort.Sort(&xNodePath{nd.children})
}
}