From 63371da8aa9edd7f7812c8a56ad573e46df111fd Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Fri, 29 May 2020 11:59:59 +1000 Subject: Avoid to_remove vector in select helper function Remove the need to allocate anything when removing selections. --- src/normal.cc | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) (limited to 'src/normal.cc') diff --git a/src/normal.cc b/src/normal.cc index 53e745e2..8796c86f 100644 --- a/src/normal.cc +++ b/src/normal.cc @@ -92,14 +92,16 @@ void select(Context& context, T func) } else { - Vector to_remove; - for (int i = 0; i < (int)selections.size(); ++i) + auto main_index = selections.main_index(); + auto new_end = selections.begin(); + for (size_t i = 0; i < selections.size(); ++i) { auto& sel = selections[i]; auto res = func(context, sel); if (not res) { - to_remove.push_back(i); + if (i <= main_index and main_index != 0) + --main_index; continue; } @@ -112,12 +114,13 @@ void select(Context& context, T func) } if (not res->captures().empty()) sel.captures() = std::move(res->captures()); + *new_end++ = std::move(sel); } - - if (to_remove.size() == selections.size()) + if (new_end == selections.begin()) throw no_selections_remaining{}; - for (auto& i : to_remove | reverse()) - selections.remove(i); + + selections.set_main_index(main_index); + selections.remove_from(new_end - selections.begin()); } selections.sort_and_merge_overlapping(); -- cgit v1.2.3