-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelper.go
73 lines (62 loc) · 1.58 KB
/
helper.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
package annoy
import (
"encoding/binary"
"math"
)
type Pair struct {
first float32
second int32
}
type PriorityQueue []*Pair
func (pq PriorityQueue) Len() int { return len(pq) }
func (pq PriorityQueue) Less(i, j int) bool {
if pq[i].first == pq[j].first {
return pq[i].second > pq[j].second
}
return pq[i].first > pq[j].first
}
func (pq PriorityQueue) Swap(i, j int) {
pq[i], pq[j] = pq[j], pq[i]
}
func (pq *PriorityQueue) Push(x any) {
item := x.(*Pair)
*pq = append(*pq, item)
}
func (pq *PriorityQueue) Pop() any {
old := *pq
n := len(old)
item := old[n-1]
*pq = old[0 : n-1]
return item
}
func (pq *PriorityQueue) Top() *Pair {
return (*pq)[0]
}
func GetNodePtr(nodes []byte, size int, i int32) *Node {
arrSize := (size - 12) / 4
node := Node{}
node.Descendants = int32(binary.LittleEndian.Uint32(nodes[size*int(i) : size*int(i)+4]))
if node.Descendants > 2 && node.Descendants <= int32(arrSize)+2 {
node.Children = make([]int32, arrSize+2)
for j := 0; j < arrSize+2; j++ {
node.Children[j] = int32(binary.LittleEndian.Uint32(nodes[size*int(i)+4+4*j : size*int(i)+8+4*j]))
}
} else {
node.Children = make([]int32, 2)
for j := 0; j < 2; j++ {
node.Children[j] = int32(binary.LittleEndian.Uint32(nodes[size*int(i)+4+4*j : size*int(i)+8+4*j]))
}
node.V = make([]float32, arrSize)
for j := 0; j < arrSize; j++ {
node.V[j] = math.Float32frombits(binary.LittleEndian.Uint32(nodes[size*int(i)+12+4*j : size*int(i)+16+4*j]))
}
}
return &node
}
func Dot(x, y []float32, f int) float32 {
var s float32
for z := 0; z < f; z++ {
s += x[z] * y[z]
}
return s
}