use sort instead of heap

This commit is contained in:
Ludovic Jozeau 2020-07-20 20:54:24 +02:00
parent 54a38bcc74
commit 407f78e658
No known key found for this signature in database
GPG Key ID: 881AEC6AAD9BEA6C

View File

@ -156,32 +156,25 @@ void MessageHandler::textDocument_documentSymbol(JsonReader &reader,
syms.reserve(file->symbol2refcnt.size()); syms.reserve(file->symbol2refcnt.size());
/** /**
* std::heap is a max heap (we swap lhs/rhs to make a min heap)
*
* with 2 ranges that start at the same Position, we want the wider one * with 2 ranges that start at the same Position, we want the wider one
* first * first (swap lhs/rhs)
*
*/ */
auto sym_cmp = [](ExtentRef const &lhs, ExtentRef const &rhs) { auto sym_cmp = [](ExtentRef const &lhs, ExtentRef const &rhs) {
return rhs.extent.start < lhs.extent.start || return lhs.extent.start < rhs.extent.start ||
(rhs.extent.start == lhs.extent.start && (lhs.extent.start == rhs.extent.start &&
lhs.extent.end < rhs.extent.end); rhs.extent.end < lhs.extent.end);
}; };
for (auto [sym, refcnt] : file->symbol2refcnt) { for (auto [sym, refcnt] : file->symbol2refcnt) {
if (refcnt <= 0 || !sym.extent.valid()) if (refcnt <= 0 || !sym.extent.valid())
continue; continue;
syms.push_back(sym); syms.push_back(sym);
std::push_heap(std::begin(syms), std::end(syms), sym_cmp);
} }
std::sort(std::begin(syms), std::end(syms), sym_cmp);
std::vector<DocumentSymbol> result; std::vector<DocumentSymbol> result;
std::stack<DocumentSymbol *> indent; std::stack<DocumentSymbol *> indent;
while (!syms.empty()) { for (auto sym : syms) {
std::pop_heap(std::begin(syms), std::end(syms), sym_cmp);
auto sym = syms.back();
syms.pop_back();
DocumentSymbol ds; DocumentSymbol ds;
if (auto range = getLsRange(wf, sym.range)) { if (auto range = getLsRange(wf, sym.range)) {
ds.selectionRange = *range; ds.selectionRange = *range;
@ -194,7 +187,7 @@ void MessageHandler::textDocument_documentSymbol(JsonReader &reader,
range1 && range1->includes(*range)) range1 && range1->includes(*range))
ds.range = *range1; ds.range = *range1;
} }
withEntity(db, sym, [&, sym = sym](const auto &entity) { withEntity(db, sym, [&](const auto &entity) {
auto const *def = entity.anyDef(); auto const *def = entity.anyDef();
if (!def) if (!def)
return; return;