Reuse for_n_best when sorting values from complete options

While at it, remove a needless reserve() call and reserve an extra slot
because "InsertCompleter::try_complete" might add one more element.
This commit is contained in:
Johannes Altmanninger 2023-12-02 09:31:48 +01:00
parent 84ecd41da1
commit d6215dc25d

View File

@ -164,7 +164,7 @@ InsertCompletion complete_word(const SelectionList& sels,
constexpr size_t max_count = 100; constexpr size_t max_count = 100;
// Gather best max_count matches // Gather best max_count matches
InsertCompletion::CandidateList candidates; InsertCompletion::CandidateList candidates;
candidates.reserve(std::min(matches.size(), max_count)); candidates.reserve(std::min(matches.size(), max_count) + 1);
for_n_best(matches, max_count, [](auto& lhs, auto& rhs) { return rhs < lhs; }, for_n_best(matches, max_count, [](auto& lhs, auto& rhs) { return rhs < lhs; },
[&](RankedMatchAndBuffer& m) { [&](RankedMatchAndBuffer& m) {
@ -314,20 +314,19 @@ InsertCompletion complete_option(const SelectionList& sels,
constexpr size_t max_count = 100; constexpr size_t max_count = 100;
// Gather best max_count matches // Gather best max_count matches
auto greater = [](auto& lhs, auto& rhs) { return rhs < lhs; };
auto first = matches.begin(), last = matches.end();
std::make_heap(first, last, greater);
InsertCompletion::CandidateList candidates; InsertCompletion::CandidateList candidates;
candidates.reserve(std::min(matches.size(), max_count)); candidates.reserve(std::min(matches.size(), max_count) + 1);
candidates.reserve(matches.size());
while (candidates.size() < max_count and first != last) for_n_best(matches, max_count, [](auto& lhs, auto& rhs) { return rhs < lhs; },
{ [&](RankedMatchAndInfo& m) {
if (candidates.empty() or candidates.back().completion != first->candidate() if (not candidates.empty()
or candidates.back().on_select != first->on_select) and candidates.back().completion == m.candidate()
candidates.push_back({ first->candidate().str(), first->on_select.str(), and candidates.back().on_select == m.on_select)
std::move(first->menu_entry) }); return false;
std::pop_heap(first, last--, greater); candidates.push_back({ m.candidate().str(), m.on_select.str(),
} std::move(m.menu_entry) });
return true;
});
auto end = cursor_pos; auto end = cursor_pos;
if (match[3].matched) if (match[3].matched)