diff --git a/hnswlib/bruteforce.h b/hnswlib/bruteforce.h index 8727cc8a..1c73e185 100644 --- a/hnswlib/bruteforce.h +++ b/hnswlib/bruteforce.h @@ -108,7 +108,9 @@ class BruteforceSearch : public AlgorithmInterface { assert(k <= cur_element_count); std::priority_queue> topResults; if (cur_element_count == 0) return topResults; - for (int i = 0; i < k; i++) { + + int i = 0; + for (; topResults.size() < k && i < cur_element_count; 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)) { @@ -116,7 +118,7 @@ class BruteforceSearch : public AlgorithmInterface { } } dist_t lastdist = topResults.empty() ? std::numeric_limits::max() : topResults.top().first; - for (int i = k; i < cur_element_count; i++) { + for (; 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_));