-
Notifications
You must be signed in to change notification settings - Fork 0
/
goannoy.go
153 lines (129 loc) · 3.94 KB
/
goannoy.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package goannoy
// #include "goannoy.h"
import "C"
import (
"github.com/m10an/goannoy/errors"
"runtime"
"unsafe"
)
type AnnoyIndex struct {
index *C.AnnoyIndex
nFeatures int
}
type Distance int
const (
Angular Distance = iota
Euclidean
Manhattan
DotProduct
)
func NewAnnoyIndex(d Distance, f int) (*AnnoyIndex, error) {
var a *AnnoyIndex
if f < 1 {
return nil, errors.ValueError("number of features must be natural")
}
switch d {
case Angular:
a = &AnnoyIndex{C.CreateAnnoyIndexAngular(C.int(f)), f}
case Euclidean:
a = &AnnoyIndex{C.CreateAnnoyIndexEuclidean(C.int(f)), f}
case Manhattan:
a = &AnnoyIndex{C.CreateAnnoyIndexManhattan(C.int(f)), f}
case DotProduct:
a = &AnnoyIndex{C.CreateAnnoyIndexDotProduct(C.int(f)), f}
default:
return nil, errors.ValueError("unknown distance metric")
}
runtime.SetFinalizer(a, deleteAnnoyIndex)
return a, nil
}
func deleteAnnoyIndex(a *AnnoyIndex) {
C.DeleteAnnoyIndex(a.index)
}
func (a *AnnoyIndex) AddItem(item int, w []float32) {
errMsg := new(*C.char)
if !bool(C.AddItem(a.index, C.GoInt(item), (*C.float)(&w[0]), errMsg)) {
defer C.free(unsafe.Pointer(*errMsg))
panic(C.GoString(*errMsg))
}
}
func (a *AnnoyIndex) GetNItems() int {
return int(C.GetNItems(a.index))
}
func (a *AnnoyIndex) Build(nTrees int) {
errMsg := new(*C.char)
if !bool(C.Build(a.index, C.int(nTrees), errMsg)) {
defer C.free(unsafe.Pointer(*errMsg))
panic(C.GoString(*errMsg))
}
}
func (a *AnnoyIndex) Unbuild() {
errMsg := new(*C.char)
if !bool(C.Unbuild(a.index, errMsg)) {
defer C.free(unsafe.Pointer(*errMsg))
panic(C.GoString(*errMsg))
}
}
func (a *AnnoyIndex) Save(filename string, prefault bool) {
errMsg := new(*C.char)
chars := C.CString(filename)
defer C.free(unsafe.Pointer(chars))
if !bool(C.Save(a.index, chars, C.bool(prefault), errMsg)) {
defer C.free(unsafe.Pointer(*errMsg))
panic(C.GoString(*errMsg))
}
}
func (a *AnnoyIndex) Unload() {
C.Unload(a.index)
}
func (a *AnnoyIndex) Load(filename string, prefault bool) {
errMsg := new(*C.char)
chars := C.CString(filename)
defer C.free(unsafe.Pointer(chars))
if !bool(C.Load(a.index, chars, C.bool(prefault), errMsg)) {
defer C.free(unsafe.Pointer(*errMsg))
panic(C.GoString(*errMsg))
}
}
func (a *AnnoyIndex) GetDistance(i, j int) float32 {
return float32(C.GetDistance(a.index, C.int(i), C.int(j)))
}
func (a *AnnoyIndex) GetNNsByItem(item, n, kSearch int) []int32 {
result := make([]int32, n)
found := C.GetNNsByItem(a.index, C.int(item), C.int(n), C.int(kSearch), (*C.GoInt32)(&result[0]))
return result[:found]
}
func (a *AnnoyIndex) GetNNsByItemWithDistances(item, n, kSearch int) ([]int32, []float32) {
result := make([]int32, n)
distances := make([]float32, n)
found := C.GetNNsByItemWithDistances(a.index, C.int(item), C.int(n), C.int(kSearch), (*C.GoInt32)(&result[0]), (*C.float)(&distances[0]))
return result[:found], distances[:found]
}
func (a *AnnoyIndex) GetNNsByVector(w []float32, n, kSearch int) []int32 {
result := make([]int32, n)
found := C.GetNNsByVector(a.index, (*C.float)(&w[0]), C.int(n), C.int(kSearch), (*C.GoInt32)(&result[0]))
return result[:found]
}
func (a *AnnoyIndex) GetNNsByVectorWithDistances(w []float32, n, kSearch int) ([]int32, []float32) {
result := make([]int32, n)
distances := make([]float32, n)
found := C.GetNNsByVectorWithDistances(a.index, (*C.float)(&w[0]), C.int(n), C.int(kSearch), (*C.GoInt32)(&result[0]), (*C.float)(&distances[0]))
return result[:found], distances[:found]
}
func (a *AnnoyIndex) GetItem(item int) []float32 {
vector := make([]float32, a.nFeatures)
C.GetItem(a.index, C.int(item), (*C.float)(&vector[0]))
return vector
}
func (a *AnnoyIndex) OnDiskBuild(filename string) {
errMsg := new(*C.char)
chars := C.CString(filename)
defer C.free(unsafe.Pointer(chars))
if !bool(C.OnDiskBuild(a.index, chars, errMsg)) {
defer C.free(unsafe.Pointer(*errMsg))
panic(C.GoString(*errMsg))
}
}
func (a *AnnoyIndex) Verbose(v bool) {
C.Verbose(a.index, C.bool(v))
}