-
Notifications
You must be signed in to change notification settings - Fork 0
/
annoygomodule.h
197 lines (174 loc) · 4.78 KB
/
annoygomodule.h
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#include "annoylib.h"
#include "kissrandom.h"
using namespace Annoy;
namespace GoAnnoy {
class AnnoyVectorFloat {
protected:
float *ptr;
int len;
public:
~AnnoyVectorFloat() {
free(ptr);
};
float* ArrayPtr() {
return ptr;
};
int Len() {
return len;
};
float Get(int i) {
if (i >= len) {
return 0.0;
}
return ptr[i];
};
void fill_from_vector(vector<float>* v) {
if (ptr != NULL) {
free(ptr);
}
ptr = (float*) malloc(v->size() * sizeof(float));
for (int i = 0; i < v->size(); i++) {
ptr[i] = (float)(*v)[i];
}
len = v->size();
};
};
class AnnoyVectorInt {
protected:
int32_t *ptr;
int len;
public:
~AnnoyVectorInt() {
free(ptr);
};
int32_t* ArrayPtr() {
return ptr;
};
int Len() {
return len;
};
int32_t Get(int i) {
if (i >= len) {
return 0.0;
}
return ptr[i];
};
void fill_from_vector(vector<int32_t>* v) {
if (ptr != NULL) {
free(ptr);
}
ptr = (int32_t*) malloc(v->size() * sizeof(int32_t));
for (int i = 0; i < v->size(); i++) {
ptr[i] = (int32_t)(*v)[i];
}
len = v->size();
};
};
class AnnoyIndex {
protected:
::AnnoyIndexInterface<int32_t, float> *ptr;
int f;
public:
~AnnoyIndex() {
delete ptr;
};
void addItem(int item, const float* w) {
ptr->add_item(item, w);
};
void build(int q) {
ptr->build(q, 1);
};
bool save(const char* filename, bool prefault) {
return ptr->save(filename, prefault);
};
bool save(const char* filename) {
return ptr->save(filename, true);
};
void unload() {
ptr->unload();
};
bool load(const char* filename, bool prefault) {
return ptr->load(filename, prefault);
};
bool load(const char* filename) {
return ptr->load(filename, true);
};
float getDistance(int i, int j) {
return ptr->get_distance(i, j);
};
void getNnsByItem(int item, int n, int search_k, AnnoyVectorInt* out_result, AnnoyVectorFloat* out_distances) {
vector<int32_t>* result = new vector<int32_t>();
vector<float>* distances = new vector<float>();
ptr->get_nns_by_item(item, n, search_k, result, distances);
out_result->fill_from_vector(result);
out_distances->fill_from_vector(distances);
delete result;
delete distances;
};
void getNnsByVector(const float* w, int n, int search_k, AnnoyVectorInt* out_result, AnnoyVectorFloat* out_distances) {
vector<int32_t>* result = new vector<int32_t>();
vector<float>* distances = new vector<float>();
ptr->get_nns_by_vector(w, n, search_k, result, distances);
out_result->fill_from_vector(result);
out_distances->fill_from_vector(distances);
delete result;
delete distances;
};
void getNnsByItem(int item, int n, int search_k, AnnoyVectorInt* out_result) {
vector<int32_t>* result = new vector<int32_t>();
ptr->get_nns_by_item(item, n, search_k, result, NULL);
out_result->fill_from_vector(result);
delete result;
};
void getNnsByVector(const float* w, int n, int search_k, AnnoyVectorInt* out_result) {
vector<int32_t>* result = new vector<int32_t>();
ptr->get_nns_by_vector(w, n, search_k, result, NULL);
out_result->fill_from_vector(result);
delete result;
};
int getNItems() {
return (int)ptr->get_n_items();
};
void verbose(bool v) {
ptr->verbose(v);
};
void getItem(int item, AnnoyVectorFloat *v) {
vector<float>* r = new vector<float>();
r->resize(this->f);
ptr->get_item(item, &r->front());
v->fill_from_vector(r);
};
bool onDiskBuild(const char* filename) {
return ptr->on_disk_build(filename);
};
};
class AnnoyIndexAngular : public AnnoyIndex
{
public:
AnnoyIndexAngular(int f) {
ptr = new ::AnnoyIndex<int32_t, float, ::Angular, ::Kiss64Random, AnnoyIndexSingleThreadedBuildPolicy>(f);
this->f = f;
}
};
class AnnoyIndexEuclidean : public AnnoyIndex {
public:
AnnoyIndexEuclidean(int f) {
ptr = new ::AnnoyIndex<int32_t, float, ::Euclidean, ::Kiss64Random, AnnoyIndexSingleThreadedBuildPolicy>(f);
this->f = f;
}
};
class AnnoyIndexManhattan : public AnnoyIndex {
public:
AnnoyIndexManhattan(int f) {
ptr = new ::AnnoyIndex<int32_t, float, ::Manhattan, ::Kiss64Random, AnnoyIndexSingleThreadedBuildPolicy>(f);
this->f = f;
}
};
class AnnoyIndexDotProduct : public AnnoyIndex {
public:
AnnoyIndexDotProduct(int f) {
ptr = new ::AnnoyIndex<int32_t, float, ::DotProduct, ::Kiss64Random, AnnoyIndexSingleThreadedBuildPolicy>(f);
this->f = f;
}
};
}