From 7396c939907e34fb3f650f953c5294c7512f2825 Mon Sep 17 00:00:00 2001 From: Justin Viiret Date: Wed, 15 Mar 2017 09:29:44 +1100 Subject: [PATCH] ng_region: clean up refineExits --- src/nfagraph/ng_region.cpp | 44 +++++++++++++++++++++----------------- 1 file changed, 24 insertions(+), 20 deletions(-) diff --git a/src/nfagraph/ng_region.cpp b/src/nfagraph/ng_region.cpp index 39c0f683..8adecfcf 100644 --- a/src/nfagraph/ng_region.cpp +++ b/src/nfagraph/ng_region.cpp @@ -86,7 +86,7 @@ struct exit_info { static void checkAndAddExitCandidate(const AcyclicGraph &g, const ue2::unordered_set &r, - NFAVertex v, vector *exits) { + NFAVertex v, vector &exits) { // set when we find our first candidate. decltype(exit_info::open) *open = nullptr; @@ -94,8 +94,8 @@ void checkAndAddExitCandidate(const AcyclicGraph &g, for (auto w : adjacent_vertices_range(v, g)) { if (!contains(r, NFAVertex(w))) { if (!open) { - exits->push_back(exit_info(NFAVertex(v))); - open = &exits->back().open; + exits.emplace_back(NFAVertex(v)); + open = &exits.back().open; } open->insert(NFAVertex(w)); } @@ -107,28 +107,31 @@ void checkAndAddExitCandidate(const AcyclicGraph &g, } static -void findExits(const AcyclicGraph &g, const ue2::unordered_set &r, - vector *exits) { - exits->clear(); +vector findExits(const AcyclicGraph &g, + const ue2::unordered_set &r) { + vector exits; for (auto v : r) { checkAndAddExitCandidate(g, r, v, exits); } + + return exits; } static void refineExits(const AcyclicGraph &g, const ue2::unordered_set &r, - NFAVertex new_v, vector *exits) { - for (u32 i = 0; i < exits->size(); i++) { - (*exits)[i].open.erase(new_v); /* new_v is no long an open edge */ - if ((*exits)[i].open.empty()) { /* no open edges: no longer an exit */ - /* shuffle to back and kill */ - (*exits)[i] = exits->back(); - exits->pop_back(); - i--; - } + NFAVertex new_v, vector &exits) { + /* new_v is no long an open edge */ + for (auto &exit : exits) { + exit.open.erase(new_v); } + /* no open edges: no longer an exit */ + exits.erase( + remove_if(exits.begin(), exits.end(), + [&](const exit_info &exit) { return exit.open.empty(); }), + exits.end()); + checkAndAddExitCandidate(g, r, new_v, exits); } @@ -193,7 +196,7 @@ void buildInitialCandidate(const AcyclicGraph &g, DEBUG_PRINTF("adding %zu to initial\n", g[*it].index); candidate->insert(*it); open_jumps->erase(*it); - checkAndAddExitCandidate(g, *candidate, *it, exits); + checkAndAddExitCandidate(g, *candidate, *it, *exits); ++it; return; } @@ -218,7 +221,7 @@ void buildInitialCandidate(const AcyclicGraph &g, open_jumps->clear(); } - findExits(g, *candidate, exits); + *exits = findExits(g, *candidate); } static @@ -228,7 +231,6 @@ void findDagLeaders(const NGHolder &h, const AcyclicGraph &g, assert(!topo.empty()); u32 curr_id = 0; vector::const_reverse_iterator t_it = topo.rbegin(); - vector exits; ue2::unordered_set candidate; flat_set open_jumps; DEBUG_PRINTF("adding %zu to current\n", g[*t_it].index); @@ -237,7 +239,8 @@ void findDagLeaders(const NGHolder &h, const AcyclicGraph &g, DEBUG_PRINTF("adding %zu to current\n", g[*t_it].index); assert(t_it != topo.rend()); candidate.insert(*t_it++); - findExits(g, candidate, &exits); + + auto exits = findExits(g, candidate); while (t_it != topo.rend()) { assert(!candidate.empty()); @@ -260,7 +263,7 @@ void findDagLeaders(const NGHolder &h, const AcyclicGraph &g, DEBUG_PRINTF("adding %zu to current\n", g[curr].index); candidate.insert(curr); open_jumps.erase(curr); - refineExits(g, candidate, *t_it, &exits); + refineExits(g, candidate, *t_it, exits); DEBUG_PRINTF(" open jumps %zu exits %zu\n", open_jumps.size(), exits.size()); ++t_it; @@ -416,6 +419,7 @@ vector buildTopoOrder(const NGHolder &w, const AcyclicGraph &acyclic_g, vector &colours) { vector topoOrder; + topoOrder.reserve(num_vertices(w)); topological_sort(acyclic_g, back_inserter(topoOrder), color_map(make_iterator_property_map(colours.begin(),