Skip to content

Commit 9fffbf4

Browse files
authored
Mark iterator done when no more new neighbors can be retreived, (#130)
Mark the iterator done when no more new neighbors can be retrieved, even though there may be more nodes available in the index.
1 parent 22811fa commit 9fffbf4

File tree

2 files changed

+20
-10
lines changed

2 files changed

+20
-10
lines changed

include/svs/index/vamana/iterator.h

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@ template <typename Index, typename QueryType> class BatchIterator {
182182
iteration_ = 0;
183183
yielded_.clear();
184184
results_.clear();
185+
is_exhausted_ = false;
185186
}
186187

187188
/// @brief Adapts an internal neighbor to an external neighbor.
@@ -217,12 +218,14 @@ template <typename Index, typename QueryType> class BatchIterator {
217218
/// @brief Return the batch number corresponding to the current buffer.
218219
size_t batch_number() const { return iteration_; }
219220

220-
/// @brief Return whether the entire entries in the index have been yielded.
221+
/// @brief Returns whether iterator can find more neighbors or not for the given query.
221222
///
222-
/// The transition from not done to done will be triggered by a call to ``next()``.
223-
/// The contents of ``batch_number()`` and ``parameters_for_current_iteration()`` will
224-
/// then remain unchanged by subsequent invocations of ``next()``.
225-
bool done() const { return yielded_.size() == parent_->size(); }
223+
/// The iterator is considered done when all the available nodes have been yielded or
224+
/// when the search can not find any more neighbors. The transition from not done to
225+
/// done will be triggered by a call to ``next()``. The contents of ``batch_number()``
226+
/// and ``parameters_for_current_iteration()`` will then remain unchanged by subsequent
227+
/// invocations of ``next()``.
228+
bool done() const { return (yielded_.size() == parent_->size() || is_exhausted_); }
226229

227230
/// @brief Forces the next iteration to restart the search from scratch.
228231
void restart_next_search() { restart_search_ = true; }
@@ -306,6 +309,11 @@ template <typename Index, typename QueryType> class BatchIterator {
306309
++iteration_;
307310
restart_search_ = false;
308311
copy_from_scratch(batch_size);
312+
// If result is empty after calling next(), mark the iterator as exhausted.
313+
// The iterator will not be able to find any more neighbors.
314+
if (results_.size() == 0 && batch_size > 0) {
315+
is_exhausted_ = true;
316+
}
309317
}
310318

311319
private:
@@ -318,6 +326,7 @@ template <typename Index, typename QueryType> class BatchIterator {
318326
bool restart_search_ = true; // Whether the next search should restart from scratch.
319327
size_t extra_search_buffer_capacity_ =
320328
svs::UNSIGNED_INTEGER_PLACEHOLDER; // Extra buffer capacity for the next search.
329+
bool is_exhausted_ = false; // Whether the iterator is exhausted.
321330
};
322331

323332
// Deduction Guides

include/svs/orchestrators/vamana_iterator.h

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -160,12 +160,13 @@ class VamanaIterator {
160160
/// This can be helpful for measuring performance and verifying recall values.
161161
void restart_next_search() const { impl_->restart_next_search(); }
162162

163-
/// @brief Return whether or not all entries in the index have been yielded.
163+
/// @brief Returns whether iterator can find more neighbors or not for the given query.
164164
///
165-
/// This transition is triggered by an invocation of ``next()``.
166-
/// After ``done() == true``, future calls to ``next()`` will yield an empty set of
167-
/// candidates and will not change the results of ``batch_number()`` or
168-
/// ``parameters_for_current_iteration()``.
165+
/// The iterator is considered done when all the available nodes have been yielded or
166+
/// when the search can not find any more neighbors. The transition from not done to
167+
/// done will be triggered by a call to ``next()``. The contents of ``batch_number()``
168+
/// and ``parameters_for_current_iteration()`` will then remain unchanged by subsequent
169+
/// invocations of ``next()``.
169170
bool done() const { return impl_->done(); }
170171

171172
/// @brief Update the iterator with a new query.

0 commit comments

Comments
 (0)