Skip to content

Commit 7584789

Browse files
saurabh1002Copilot
andauthored
fix std::nth_element edge case
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
1 parent 547fe13 commit 7584789

1 file changed

Lines changed: 6 additions & 3 deletions

File tree

cpp/map_closures/MapClosures.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -186,9 +186,12 @@ std::vector<ClosureCandidate> MapClosures::GetTopKClosures(
186186
}
187187
if (k != -1 && !closures.empty()) {
188188
const int top_k = std::min(k, static_cast<int>(closures.size()));
189-
const auto kth = closures.begin() + top_k;
190-
std::nth_element(closures.begin(), kth, closures.end(), compare_closure_candidates);
191-
closures.resize(top_k);
189+
if (top_k < static_cast<int>(closures.size())) {
190+
const auto kth = closures.begin() + top_k;
191+
std::nth_element(
192+
closures.begin(), kth, closures.end(), compare_closure_candidates);
193+
closures.resize(top_k);
194+
}
192195
std::sort(closures.begin(), closures.end(), compare_closure_candidates);
193196
}
194197
}

0 commit comments

Comments
 (0)