Use inplace_merge for sorting matches in region highlighter

This commit is contained in:
Maxime Coste 2014-01-14 19:42:57 +00:00
parent ab508ea3da
commit c2f18e6e78

View File

@ -633,35 +633,35 @@ private:
{ {
const size_t buf_timestamp = buffer.timestamp(); const size_t buf_timestamp = buffer.timestamp();
// remove out of date matches and update line for others // remove out of date matches and update line for others
for (auto it = matches.begin(); it != matches.end();) auto ins_pos = matches.begin();
for (auto it = ins_pos; it != matches.end(); ++it)
{ {
auto change_it = std::lower_bound(cache.changes.begin(), cache.changes.end(), it->line, auto change_it = std::lower_bound(cache.changes.begin(), cache.changes.end(), it->line,
[](const LineChange& c, const LineCount& l) [](const LineChange& c, const LineCount& l)
{ return c.pos < l; }); { return c.pos < l; });
bool erase = false;
if (change_it != cache.changes.begin()) if (change_it != cache.changes.begin())
{ {
it->line += (change_it-1)->num; it->line += (change_it-1)->num;
if (it->line <= (change_it-1)->pos) erase = it->line <= (change_it-1)->pos;
{
std::swap(*it, matches.back());
matches.pop_back();
continue;
}
} }
if (it->line >= buffer.line_count() or erase = erase or (it->line >= buffer.line_count() or
it->timestamp < buffer.line_timestamp(it->line)) it->timestamp < buffer.line_timestamp(it->line));
{
std::swap(*it, matches.back()); if (not erase)
matches.pop_back();
}
else
{ {
it->timestamp = buf_timestamp; it->timestamp = buf_timestamp;
kak_assert(buffer.is_valid({it->line, it->begin})); kak_assert(buffer.is_valid({it->line, it->begin}));
kak_assert(buffer.is_valid({it->line, it->end})); kak_assert(buffer.is_valid({it->line, it->end}));
++it;
if (ins_pos != it)
*ins_pos = std::move(*it);
++ins_pos;
} }
} }
matches.erase(ins_pos, matches.end());
size_t pivot = matches.size();
// try to find new matches in each updated lines // try to find new matches in each updated lines
for (auto line = 0_line, end = buffer.line_count(); line < end; ++line) for (auto line = 0_line, end = buffer.line_count(); line < end; ++line)
{ {
@ -676,7 +676,8 @@ private:
} }
} }
} }
std::sort(matches.begin(), matches.end(), compare_matches_begin); std::inplace_merge(matches.begin(), matches.begin() + pivot, matches.end(),
compare_matches_begin);
} }
}; };