This repository was archived by the owner on Aug 16, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathnode.go
More file actions
91 lines (74 loc) · 1.83 KB
/
Copy pathnode.go
File metadata and controls
91 lines (74 loc) · 1.83 KB
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
package gann
import (
"github.com/google/uuid"
)
type nodeId string
type direction string
const (
left direction = "left"
right direction = "right"
)
var directions = []direction{left, right}
type node struct {
idxPtr *index
id nodeId
// the normal vector of the hyper plane which splits the space, represented by the node
vec []float64
// children of node. If len equals 0, then it is leaf node.
children map[direction]*node
// In our setting, a `leaf` is a kind of node with len(leaf) > 0
leaf []itemId
}
func (n *node) build(its []*item) {
if len(its) <= n.idxPtr.k {
n.leaf = make([]itemId, len(its))
for i, it := range its {
n.leaf[i] = it.id
}
return
}
n.buildChildren(its)
}
func (n *node) buildChildren(its []*item) {
dItems := map[direction][]*item{}
dVectors := map[direction][][]float64{}
for _, it := range its {
if n.idxPtr.metric.CalcDirectionPriority(n.vec, it.vector) < 0 {
dItems[left] = append(dItems[left], it)
dVectors[left] = append(dVectors[left], it.vector)
} else {
dItems[right] = append(dItems[right], it)
dVectors[right] = append(dVectors[right], it.vector)
}
}
var shouldMerge = false
for _, s := range directions {
if len(dItems[s]) <= n.idxPtr.k {
shouldMerge = true
}
}
if shouldMerge {
n.leaf = make([]itemId, len(its))
for i, it := range its {
n.leaf[i] = it.id
}
return
}
for _, s := range directions {
// build child
c := &node{
vec: n.idxPtr.metric.GetSplittingVector(dVectors[s]),
id: nodeId(uuid.New().String()),
idxPtr: n.idxPtr,
children: make(map[direction]*node, len(directions)),
}
c.build(dItems[s])
// append child for the search phase
n.children[s] = c
// append child to global map for the search phase
n.idxPtr.mux.Lock()
n.idxPtr.nodeIDToNode[c.id] = c
n.idxPtr.mux.Unlock()
}
return
}