Skip to content

Commit

Permalink
UPDATED HNSWLIB to the latest 0.7.0 version
Browse files Browse the repository at this point in the history
  • Loading branch information
ShravanSunder committed May 31, 2023
1 parent 4d5b45a commit e20b781
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 13 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "hnswlib-wasm",
"version": "0.0.10-0",
"version": "0.1.0-0",
"description": "typescript and wasm bindings for Hnswlib",
"module": "./dist/hnswlib.js",
"types": "./dist/hnswlib.d.ts",
Expand Down
4 changes: 2 additions & 2 deletions src/hnswlib/bruteforce.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,15 +102,15 @@ class BruteforceSearch : public AlgorithmInterface<dist_t> {
assert(k <= cur_element_count);
std::priority_queue<std::pair<dist_t, labeltype >> topResults;
if (cur_element_count == 0) return topResults;
for (size_t i = 0; i < k; i++) {
for (int i = 0; i < k; i++) {
dist_t dist = fstdistfunc_(query_data, data_ + size_per_element_ * i, dist_func_param_);
labeltype label = *((labeltype*) (data_ + size_per_element_ * i + data_size_));
if ((!isIdAllowed) || (*isIdAllowed)(label)) {
topResults.push(std::pair<dist_t, labeltype>(dist, label));
}
}
dist_t lastdist = topResults.empty() ? std::numeric_limits<dist_t>::max() : topResults.top().first;
for (size_t i = k; i < cur_element_count; i++) {
for (int i = k; i < cur_element_count; i++) {
dist_t dist = fstdistfunc_(query_data, data_ + size_per_element_ * i, dist_func_param_);
if (dist <= lastdist) {
labeltype label = *((labeltype *) (data_ + size_per_element_ * i + data_size_));
Expand Down
10 changes: 5 additions & 5 deletions src/hnswlib/hnswalg.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ class HierarchicalNSW : public AlgorithmInterface<dist_t> {
size_t ef_construction = 200,
size_t random_seed = 100,
bool allow_replace_deleted = false)
: label_op_locks_(MAX_LABEL_OPERATION_LOCKS),
link_list_locks_(max_elements),
: link_list_locks_(max_elements),
label_op_locks_(MAX_LABEL_OPERATION_LOCKS),
element_levels_(max_elements),
allow_replace_deleted_(allow_replace_deleted) {
max_elements_ = max_elements;
Expand Down Expand Up @@ -739,7 +739,7 @@ class HierarchicalNSW : public AlgorithmInterface<dist_t> {
std::vector<data_t> getDataByLabel(labeltype label) const {
// lock all operations with element by label
std::unique_lock <std::mutex> lock_label(getLabelOpMutex(label));

std::unique_lock <std::mutex> lock_table(label_lookup_lock);
auto search = label_lookup_.find(label);
if (search == label_lookup_.end() || isMarkedDeleted(search->second)) {
Expand All @@ -752,7 +752,7 @@ class HierarchicalNSW : public AlgorithmInterface<dist_t> {
size_t dim = *((size_t *) dist_func_param_);
std::vector<data_t> data;
data_t* data_ptr = (data_t*) data_ptrv;
for (size_t i = 0; i < dim; i++) {
for (int i = 0; i < dim; i++) {
data.push_back(*data_ptr);
data_ptr += 1;
}
Expand Down Expand Up @@ -801,7 +801,7 @@ class HierarchicalNSW : public AlgorithmInterface<dist_t> {

/*
* Removes the deleted mark of the node, does NOT really change the current graph.
*
*
* Note: the method is not safe to use when replacement of deleted elements is enabled,
* because elements marked as deleted can be completely removed by addPoint
*/
Expand Down
1 change: 0 additions & 1 deletion src/hnswlib/hnswlib.h
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,6 @@ typedef size_t labeltype;
class BaseFilterFunctor {
public:
virtual bool operator()(hnswlib::labeltype id) { return true; }
virtual ~BaseFilterFunctor() {};
};

template <typename T>
Expand Down
9 changes: 5 additions & 4 deletions src/wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ namespace emscripten {
uint32_t dim_;
std::unique_ptr<hnswlib::InnerProductSpace> ipspace_;

InnerProductSpace(uint32_t dim): dim_(dim) {
InnerProductSpace(uint32_t dim) : dim_(dim) {
ipspace_ = std::unique_ptr<hnswlib::InnerProductSpace>(new hnswlib::InnerProductSpace(static_cast<size_t>(dim_)));
}

Expand All @@ -287,9 +287,9 @@ namespace emscripten {


/*****************/
class CustomFilterFunctor: public hnswlib::BaseFilterFunctor {
class CustomFilterFunctor : public hnswlib::BaseFilterFunctor {
public:
CustomFilterFunctor(emscripten::val callback): callback_(callback) {}
CustomFilterFunctor(emscripten::val callback) : callback_(callback) {}

bool operator()(hnswlib::labeltype id) override {
if (callback_.isUndefined() || callback_.isNull()) {
Expand All @@ -306,7 +306,7 @@ namespace emscripten {
}

// Explicitly declare the destructor with the same exception specification as the base class
~CustomFilterFunctor() noexcept override = default;
~CustomFilterFunctor() noexcept = default;

private:
emscripten::val callback_;
Expand Down Expand Up @@ -461,6 +461,7 @@ namespace emscripten {
emscripten::val distances = emscripten::val::array();
emscripten::val neighbors = emscripten::val::array();


// Reverse the loop order
for (int32_t i = static_cast<int32_t>(n_results) - 1; i >= 0; i--) {
auto nn = knn.top();
Expand Down
1 change: 1 addition & 0 deletions test/HierarchicalNSW.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { expect } from 'vitest';

describe('hnswlib.HierarchicalNSW', () => {
afterAll(() => {
console.log(testHnswlibModule, (testHnswlibModule as any).FS);
process.stdout.write('');
});

Expand Down

0 comments on commit e20b781

Please sign in to comment.