From ab508ea3da188c669c11200c893a0348f67a1a0c Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Mon, 13 Jan 2014 22:23:40 +0000 Subject: [PATCH] Much faster implementation of SelectionList::merge_overlapping --- src/selection.hh | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/src/selection.hh b/src/selection.hh index aad74f95..66b348d1 100644 --- a/src/selection.hh +++ b/src/selection.hh @@ -120,18 +120,24 @@ struct SelectionList : std::vector void merge_overlapping(OverlapsFunc overlaps) { kak_assert(std::is_sorted(begin(), end(), compare_selections)); - for (size_t i = 0; i+1 < size() and size() > 1;) + size_t i = 0; + for (size_t j = 1; j < size(); ++j) { - if (overlaps((*this)[i], (*this)[i+1])) + if (overlaps((*this)[i], (*this)[j])) { - (*this)[i].merge_with((*this)[i+1]); - erase(begin() + i + 1); - if (i + 1 <= m_main) + (*this)[i].merge_with((*this)[j]); + if (i < m_main) --m_main; } else - ++i; + { + ++i; + if (i != j) + (*this)[i] = std::move((*this)[j]); + } } + erase(begin() + i + 1, end()); + kak_assert(std::is_sorted(begin(), end(), compare_selections)); } void sort_and_merge_overlapping()