From c8373364742915f9fb90e14fab665df0b2d12023 Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Wed, 25 Jan 2012 18:51:47 +0000 Subject: [PATCH] optimize SelectionHighlighters, stop copying Selections --- src/highlighters.cc | 42 ++++++++++++++++++++++++------------------ 1 file changed, 24 insertions(+), 18 deletions(-) diff --git a/src/highlighters.cc b/src/highlighters.cc index 6d3700c7..ed1d4abe 100644 --- a/src/highlighters.cc +++ b/src/highlighters.cc @@ -193,63 +193,69 @@ public: void operator()(DisplayBuffer& display_buffer) { - SelectionList sorted_selections = m_window.selections(); + typedef std::pair BufferRange; - std::sort(sorted_selections.begin(), sorted_selections.end(), - [](const Selection& lhs, const Selection& rhs) { return lhs.begin() < rhs.begin(); }); + std::vector selections; + for (auto& sel : m_window.selections()) + selections.push_back(BufferRange(sel.begin(), sel.end())); + + std::sort(selections.begin(), selections.end(), + [](const BufferRange& lhs, const BufferRange& rhs) + { return lhs.first < rhs.first; }); auto atom_it = display_buffer.begin(); - auto sel_it = sorted_selections.begin(); + auto sel_it = selections.begin(); while (atom_it != display_buffer.end() - and sel_it != sorted_selections.end()) + and sel_it != selections.end()) { - Selection& sel = *sel_it; + BufferRange& sel = *sel_it; DisplayAtom& atom = *atom_it; // [###------] - if (atom.begin() >= sel.begin() and atom.begin() < sel.end() and atom.end() > sel.end()) + if (atom.begin() >= sel.first and atom.begin() < sel.second and + atom.end() > sel.second) { - atom_it = display_buffer.split(atom_it, sel.end()); + atom_it = display_buffer.split(atom_it, sel.second); atom_it->attribute() |= Attributes::Underline; ++atom_it; ++sel_it; } // [---###---] - else if (atom.begin() < sel.begin() and atom.end() > sel.end()) + else if (atom.begin() < sel.first and atom.end() > sel.second) { - atom_it = display_buffer.split(atom_it, sel.begin()); - atom_it = display_buffer.split(++atom_it, sel.end()); + atom_it = display_buffer.split(atom_it, sel.first); + atom_it = display_buffer.split(++atom_it, sel.second); atom_it->attribute() |= Attributes::Underline; ++atom_it; ++sel_it; } // [------###] - else if (atom.begin() < sel.begin() and atom.end() > sel.begin()) + else if (atom.begin() < sel.first and atom.end() > sel.first) { - atom_it = ++display_buffer.split(atom_it, sel.begin()); + atom_it = ++display_buffer.split(atom_it, sel.first); atom_it->attribute() |= Attributes::Underline; ++atom_it; } // [#########] - else if (atom.begin() >= sel.begin() and atom.end() <= sel.end()) + else if (atom.begin() >= sel.first and atom.end() <= sel.second) { atom_it->attribute() |= Attributes::Underline; ++atom_it; } // [---------] - else if (atom.begin() >= sel.end()) + else if (atom.begin() >= sel.second) ++sel_it; // [---------] - else if (atom.end() <= sel.begin()) + else if (atom.end() <= sel.first) ++atom_it; else assert(false); } boost::regex ex("\n"); - for (auto& sel : sorted_selections) - colorize_regex_range(display_buffer, sel.begin(), sel.end(), + for (auto& sel : selections) + colorize_regex_range(display_buffer, sel.first, sel.second, ex, Color::Default, Color::Yellow); }