optimize merge_overlappings

assume selections are sorted, so we have a linear complexity algorithm
instead of O(n²).
This commit is contained in:
Maxime Coste 2012-12-13 18:45:32 +01:00
parent 3aee1c37fb
commit 8e170e4385

View File

@ -121,18 +121,18 @@ std::vector<String> Editor::selections_content() const
static void merge_overlapping(SelectionList& selections)
{
for (size_t i = 0; i < selections.size(); ++i)
assert(std::is_sorted(selections.begin(), selections.end(),
[](const Selection& lhs, const Selection& rhs)
{ return lhs.begin() < rhs.begin(); }));
for (size_t i = 0; i < selections.size()-1;)
{
for (size_t j = i+1; j < selections.size();)
if (overlaps(selections[i], selections[i+1]))
{
if (overlaps(selections[i], selections[j]))
{
selections[i].merge_with(selections[j]);
selections.erase(selections.begin() + j);
selections[i].merge_with(selections[i+1]);
selections.erase(selections.begin() + i + 1);
}
else
++j;
}
++i;
}
}