diff --git a/hnswlib/hnswalg.h b/hnswlib/hnswalg.h index 8ee24d95..4ebcc2d0 100644 --- a/hnswlib/hnswalg.h +++ b/hnswlib/hnswalg.h @@ -20,7 +20,7 @@ namespace hnswlib { typedef unsigned int tableint; typedef unsigned int linklistsizeint; - template + template class HierarchicalNSW : public AlgorithmInterface { public: @@ -600,6 +600,7 @@ namespace hnswlib { revSize_ = 1.0 / mult_; ef_ = 10; for (size_t i = 0; i < cur_element_count; i++) { + label_lookup_[getExternalLabel(i)]=i; unsigned int linkListSize; readBinaryPOD(input, linkListSize); if (linkListSize == 0) { @@ -617,6 +618,7 @@ namespace hnswlib { return; } + template std::vector getDataByLabel(labeltype label) { tableint label_c = label_lookup_[label]; @@ -645,6 +647,7 @@ namespace hnswlib { throw std::runtime_error("The number of elements exceeds the specified limit"); }; cur_c = cur_element_count; + label_lookup_[label] = cur_c; // expected unique, if not will overwrite cur_element_count++; } std::unique_lock lock_el(link_list_locks_[cur_c]); @@ -667,7 +670,7 @@ namespace hnswlib { // Initialisation of the data and label memcpy(getExternalLabeLp(cur_c), &label, sizeof(labeltype)); memcpy(getDataByInternalId(cur_c), data_point, data_size_); - label_lookup_[label] = cur_c; // expected unique, if not will overwrite + if (curlevel) { linkLists_[cur_c] = (char *) malloc(size_links_per_element_ * curlevel + 1); diff --git a/python_bindings/bindings.cpp b/python_bindings/bindings.cpp index 06a049ff..bd782a55 100644 --- a/python_bindings/bindings.cpp +++ b/python_bindings/bindings.cpp @@ -97,7 +97,7 @@ class Index { throw new std::runtime_error("The index is already initiated."); } cur_l = 0; - appr_alg = new hnswlib::HierarchicalNSW(l2space, maxElements, M, efConstruction, random_seed); + appr_alg = new hnswlib::HierarchicalNSW(l2space, maxElements, M, efConstruction, random_seed); index_inited = true; ep_added = false; } @@ -119,7 +119,7 @@ class Index { std::cerr<<"Warning: Calling load_index for an already inited index. Old index is being deallocated."; delete appr_alg; } - appr_alg = new hnswlib::HierarchicalNSW(l2space, path_to_index, false, max_elements); + appr_alg = new hnswlib::HierarchicalNSW(l2space, path_to_index, false, max_elements); cur_l = appr_alg->cur_element_count; } void normalize_vector(float *data, float *norm_array){ @@ -230,7 +230,7 @@ class Index { std::vector> data; for (auto id : ids) { - data.push_back(appr_alg->getDataByLabel(id)); + data.push_back(appr_alg->template getDataByLabel(id)); } return data; } @@ -341,7 +341,7 @@ class Index { bool normalize; int num_threads_default; hnswlib::labeltype cur_l; - hnswlib::HierarchicalNSW *appr_alg; + hnswlib::HierarchicalNSW *appr_alg; hnswlib::SpaceInterface *l2space; ~Index() {