Skip to content

Commit b6b338e

Browse files
committed
1. Replace the template interface searchKnn with virtual interface
2. add asser.h, or it will not compile
1 parent 3c6a84f commit b6b338e

File tree

2 files changed

+14
-19
lines changed

2 files changed

+14
-19
lines changed

hnswlib/hnswalg.h

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <unordered_set>
99
#include <list>
1010

11+
#include <assert.h>
1112

1213
namespace hnswlib {
1314
typedef unsigned int tableint;
@@ -406,7 +407,7 @@ namespace hnswlib {
406407
top_candidates.pop();
407408
}
408409

409-
tableint next_closest_entry_point = selectedNeighbors[0];
410+
tableint next_closest_entry_point = selectedNeighbors.back();
410411

411412
{
412413
linklistsizeint *ll_cur;
@@ -1156,24 +1157,19 @@ namespace hnswlib {
11561157
return result;
11571158
};
11581159

1159-
template <typename Comp>
1160-
std::vector<std::pair<dist_t, labeltype>>
1161-
searchKnn(const void* query_data, size_t k, Comp comp) {
1162-
std::vector<std::pair<dist_t, labeltype>> result;
1163-
if (cur_element_count == 0) return result;
1164-
1165-
auto ret = searchKnn(query_data, k);
1166-
1167-
while (!ret.empty()) {
1168-
result.push_back(ret.top());
1169-
ret.pop();
1160+
int searchKnn(const void* x,
1161+
int k, labeltype* labels, dist_t* dists = nullptr) const override {
1162+
if (labels == nullptr) return -1;
1163+
auto ret = searchKnn(x, k);
1164+
for (int i = k - 1; i >= 0; --i) {
1165+
if (dists)
1166+
dists[i] = ret.top().first;
1167+
labels[i] = ret.top().second;
11701168
}
1171-
1172-
std::sort(result.begin(), result.end(), comp);
1173-
1174-
return result;
1169+
return 0;
11751170
}
11761171

1172+
11771173
void checkIntegrity(){
11781174
int connections_checked=0;
11791175
std::vector <int > inbound_connections_num(cur_element_count,0);

hnswlib/hnswlib.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,8 @@ namespace hnswlib {
7171
public:
7272
virtual void addPoint(const void *datapoint, labeltype label)=0;
7373
virtual std::priority_queue<std::pair<dist_t, labeltype >> searchKnn(const void *, size_t) const = 0;
74-
template <typename Comp>
75-
std::vector<std::pair<dist_t, labeltype>> searchKnn(const void*, size_t, Comp) {
76-
}
74+
virtual int searchKnn(const void* x,
75+
int k, labeltype* labels, dist_t* dists) const = 0;
7776
virtual void saveIndex(const std::string &location)=0;
7877
virtual ~AlgorithmInterface(){
7978
}

0 commit comments

Comments
 (0)