forked from nmslib/hnswlib
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbindings.cpp
More file actions
265 lines (227 loc) · 8.29 KB
/
bindings.cpp
File metadata and controls
265 lines (227 loc) · 8.29 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
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
#include "bindings.h"
// Implementations for the Index class methods
template<typename dist_t, typename data_t>
Index<dist_t, data_t>::Index(const std::string &space_name, const int dim) : space_name(space_name), dim(dim) {
// Implementation from the original file...
data_must_be_normalized = false;
if(space_name=="L2") {
l2space = new hnswlib::L2Space(dim);
} else if(space_name=="IP") {
l2space = new hnswlib::InnerProductSpace(dim);
} else if(space_name=="COSINE") {
l2space = new hnswlib::InnerProductSpace(dim);
data_must_be_normalized = true;
}
appr_alg = NULL;
index_initialized = false;
index_cleared = false;
}
template<typename dist_t, typename data_t>
int Index<dist_t, data_t>::init_new_index(const size_t maxElements, const size_t M, const size_t efConstruction, const size_t random_seed) {
// Implementation from the original file...
if (appr_alg) {
return RESULT_INDEX_ALREADY_INITIALIZED;
}
appr_alg = new hnswlib::HierarchicalNSW<dist_t>(l2space, maxElements, M, efConstruction, random_seed);
index_initialized = true;
return RESULT_SUCCESSFUL;
}
template<typename dist_t, typename data_t>
int Index<dist_t, data_t>::set_ef(size_t ef) {
appr_alg->ef_ = ef;
return RESULT_SUCCESSFUL; // Assuming you want to return a success code
}
template<typename dist_t, typename data_t>
int Index<dist_t, data_t>::get_ef() {
return appr_alg->ef_;
}
template<typename dist_t, typename data_t>
int Index<dist_t, data_t>::get_ef_construction() {
return appr_alg->ef_construction_;
}
template<typename dist_t, typename data_t>
int Index<dist_t, data_t>::get_M() {
return appr_alg->M_;
}
template<typename dist_t, typename data_t>
int Index<dist_t, data_t>::save_index(const std::string &path_to_index) {
appr_alg->saveIndex(path_to_index);
return RESULT_SUCCESSFUL; // Assuming you want to return a success code
}
template<typename dist_t, typename data_t>
int Index<dist_t, data_t>::load_index(const std::string &path_to_index, size_t max_elements) {
if (appr_alg) {
std::cerr << "Warning: Calling load_index for an already initialized index. Old index is being deallocated.";
delete appr_alg;
}
appr_alg = new hnswlib::HierarchicalNSW<dist_t>(l2space, path_to_index, false, max_elements);
return RESULT_SUCCESSFUL; // Assuming you want to return a success code
}
template<typename dist_t, typename data_t>
void Index<dist_t, data_t>::normalize_array(float* array) {
float norm = 0.0f;
for (int i=0; i<dim; i++) {
norm += (array[i] * array[i]);
}
norm = 1.0f / (sqrtf(norm) + 1e-30f);
for (int i=0; i<dim; i++) {
array[i] = array[i] * norm;
}
}
template<typename dist_t, typename data_t>
int Index<dist_t, data_t>::add_item(float* item, bool item_normalized, int id) {
if (get_current_count() >= get_max_elements()) {
return RESULT_ITEM_CANNOT_BE_INSERTED_INTO_THE_VECTOR_SPACE;
}
if ((data_must_be_normalized == true) && (item_normalized == false)) {
normalize_array(item);
}
int current_id = id != -1 ? id : incremental_id++;
appr_alg->addPoint(item, current_id);
return RESULT_SUCCESSFUL; // Assuming you want to return a success code
}
template<typename dist_t, typename data_t>
int Index<dist_t, data_t>::hasId(int id) {
int label_c;
auto search = (appr_alg->label_lookup_.find(id));
if (search == (appr_alg->label_lookup_.end()) || (appr_alg->isMarkedDeleted(search->second))) {
return RESULT_ID_NOT_IN_INDEX;
}
return RESULT_SUCCESSFUL; // Assuming you want to return a success code
}
template<typename dist_t, typename data_t>
int Index<dist_t, data_t>::getDataById(int id, float* data, int dim) {
int label_c;
auto search = (appr_alg->label_lookup_.find(id));
if (search == (appr_alg->label_lookup_.end()) || (appr_alg->isMarkedDeleted(search->second))) {
return RESULT_ID_NOT_IN_INDEX;
}
label_c = search->second;
char* data_ptrv = (appr_alg->getDataByInternalId(label_c));
float* data_ptr = (float*) data_ptrv;
for (int i = 0; i < dim; i++) {
data[i] = *data_ptr;
data_ptr += 1;
}
return RESULT_SUCCESSFUL; // Assuming you want to return a success code
}
template<typename dist_t, typename data_t>
float Index<dist_t, data_t>::compute_similarity(float* vector1, float* vector2) {
float similarity;
try {
similarity = (appr_alg->fstdistfunc_(vector1, vector2, (appr_alg->dist_func_param_)));
} catch (...) {
similarity = NAN;
}
return similarity;
}
template<typename dist_t, typename data_t>
int Index<dist_t, data_t>::knn_query(float* input, bool input_normalized, int k, int* indices, float* coefficients) {
std::priority_queue<std::pair<dist_t, hnswlib::labeltype >> result;
if ((data_must_be_normalized == true) && (input_normalized == false)) {
normalize_array(input);
}
result = appr_alg->searchKnn((void*) input, k);
if (result.size() != k)
return RESULT_QUERY_CANNOT_RETURN;
for (int i = k - 1; i >= 0; i--) {
auto &result_tuple = result.top();
coefficients[i] = result_tuple.first;
indices[i] = result_tuple.second;
result.pop();
}
return RESULT_SUCCESSFUL; // Assuming you want to return a success code
}
template<typename dist_t, typename data_t>
int Index<dist_t, data_t>::mark_deleted(int label) {
appr_alg->markDelete(label);
return RESULT_SUCCESSFUL; // Assuming you want to return a success code
}
template<typename dist_t, typename data_t>
void Index<dist_t, data_t>::resize_index(size_t new_size) {
appr_alg->resizeIndex(new_size);
}
template<typename dist_t, typename data_t>
int Index<dist_t, data_t>::get_max_elements() const {
return appr_alg->max_elements_;
}
template<typename dist_t, typename data_t>
int Index<dist_t, data_t>::get_current_count() const {
return appr_alg->cur_element_count;
}
template<typename dist_t, typename data_t>
int Index<dist_t, data_t>::clear_index() {
delete l2space;
if (appr_alg)
delete appr_alg;
index_cleared = true;
return RESULT_SUCCESSFUL; // Assuming you want to return a success code
}
template<typename dist_t, typename data_t>
Index<dist_t, data_t>::~Index() {
clear_index();
}
// Implementations for the functions
int initNewIndex(Index<float>* index, int maxNumberOfElements, int M, int efConstruction, int randomSeed) {
return index->init_new_index(maxNumberOfElements, M, efConstruction, randomSeed);
}
Index<float>* createNewIndex(char* spaceName, int dimension){
Index<float>* index;
try {
index = new Index<float>(spaceName, dimension);
} catch (...) {
index = NULL;
}
return index;
}
int addItemToIndex(float* item, int normalized, int label, Index<float>* index) {
return index->add_item(item, normalized, label);
}
int getIndexLength(Index<float>* index) {
if (index->appr_alg) {
return index->appr_alg->cur_element_count;
} else {
return 0;
}
}
int saveIndexToPath(Index<float>* index, char* path) {
std::string path_string(path);
return index->save_index(path_string);
}
int loadIndexFromPath(Index<float>* index, size_t maxNumberOfElements, char* path) {
std::string path_string(path);
return index->load_index(path_string, maxNumberOfElements);
}
int knnQuery(Index<float>* index, float* input, int normalized, int k, int* indices /* output */, float* coefficients /* output */) {
return index->knn_query(input, normalized, k, indices, coefficients);
}
int clearIndex(Index<float>* index) {
return index->clear_index();
}
int setEf(Index<float>* index, int ef) {
return index->set_ef(ef);
}
int getData(Index<float>* index, int id, float* vector, int dim) {
return index->getDataById(id, vector, dim);
}
int hasId(Index<float>* index, int id) {
return index->hasId(id);
}
float computeSimilarity(Index<float>* index, float* vector1, float* vector2) {
return index->compute_similarity(vector1, vector2);
}
int getM(Index<float>* index) {
return index->get_M();
}
int getEfConstruction(Index<float>* index) {
return index->get_ef_construction();
}
int getEf(Index<float>* index) {
return index->get_ef();
}
int markDeleted(Index<float>* index, int id) {
return index->mark_deleted(id);
}
int main(){
return RESULT_SUCCESSFUL;
}