mirror of
https://github.com/VectorCamp/vectorscan.git
synced 2025-06-28 16:41:01 +03:00
ng_region: clean up and modernise
This commit is contained in:
parent
ca22edc9d3
commit
699ab4190a
@ -70,9 +70,9 @@ using namespace std;
|
|||||||
|
|
||||||
namespace ue2 {
|
namespace ue2 {
|
||||||
|
|
||||||
typedef ue2::unordered_set<NFAEdge> BackEdgeSet;
|
using BackEdgeSet = unordered_set<NFAEdge>;
|
||||||
typedef boost::filtered_graph<NGHolder, bad_edge_filter<BackEdgeSet>>
|
using AcyclicGraph =
|
||||||
AcyclicGraph;
|
boost::filtered_graph<NGHolder, bad_edge_filter<BackEdgeSet>>;
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
struct exit_info {
|
struct exit_info {
|
||||||
@ -85,8 +85,8 @@ struct exit_info {
|
|||||||
|
|
||||||
static
|
static
|
||||||
void checkAndAddExitCandidate(const AcyclicGraph &g,
|
void checkAndAddExitCandidate(const AcyclicGraph &g,
|
||||||
const ue2::unordered_set<NFAVertex> &r,
|
const unordered_set<NFAVertex> &r, NFAVertex v,
|
||||||
NFAVertex v, vector<exit_info> &exits) {
|
vector<exit_info> &exits) {
|
||||||
exit_info v_exit(v);
|
exit_info v_exit(v);
|
||||||
auto &open = v_exit.open;
|
auto &open = v_exit.open;
|
||||||
|
|
||||||
@ -104,7 +104,7 @@ void checkAndAddExitCandidate(const AcyclicGraph &g,
|
|||||||
}
|
}
|
||||||
|
|
||||||
static
|
static
|
||||||
void findExits(const AcyclicGraph &g, const ue2::unordered_set<NFAVertex> &r,
|
void findExits(const AcyclicGraph &g, const unordered_set<NFAVertex> &r,
|
||||||
vector<exit_info> &exits) {
|
vector<exit_info> &exits) {
|
||||||
exits.clear();
|
exits.clear();
|
||||||
for (auto v : r) {
|
for (auto v : r) {
|
||||||
@ -113,7 +113,7 @@ void findExits(const AcyclicGraph &g, const ue2::unordered_set<NFAVertex> &r,
|
|||||||
}
|
}
|
||||||
|
|
||||||
static
|
static
|
||||||
void refineExits(const AcyclicGraph &g, const ue2::unordered_set<NFAVertex> &r,
|
void refineExits(const AcyclicGraph &g, const unordered_set<NFAVertex> &r,
|
||||||
NFAVertex new_v, vector<exit_info> &exits) {
|
NFAVertex new_v, vector<exit_info> &exits) {
|
||||||
/* new_v is no long an open edge */
|
/* new_v is no long an open edge */
|
||||||
for (auto &exit : exits) {
|
for (auto &exit : exits) {
|
||||||
@ -121,8 +121,7 @@ void refineExits(const AcyclicGraph &g, const ue2::unordered_set<NFAVertex> &r,
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* no open edges: no longer an exit */
|
/* no open edges: no longer an exit */
|
||||||
exits.erase(
|
exits.erase(remove_if(exits.begin(), exits.end(),
|
||||||
remove_if(exits.begin(), exits.end(),
|
|
||||||
[&](const exit_info &exit) { return exit.open.empty(); }),
|
[&](const exit_info &exit) { return exit.open.empty(); }),
|
||||||
exits.end());
|
exits.end());
|
||||||
|
|
||||||
@ -162,8 +161,8 @@ bool exitValid(UNUSED const AcyclicGraph &g, const vector<exit_info> &exits,
|
|||||||
}
|
}
|
||||||
|
|
||||||
static
|
static
|
||||||
void setRegion(const ue2::unordered_set<NFAVertex> &r, u32 rid,
|
void setRegion(const unordered_set<NFAVertex> &r, u32 rid,
|
||||||
ue2::unordered_map<NFAVertex, u32> ®ions) {
|
unordered_map<NFAVertex, u32> ®ions) {
|
||||||
for (auto v : r) {
|
for (auto v : r) {
|
||||||
regions[v] = rid;
|
regions[v] = rid;
|
||||||
}
|
}
|
||||||
@ -173,34 +172,34 @@ static
|
|||||||
void buildInitialCandidate(const AcyclicGraph &g,
|
void buildInitialCandidate(const AcyclicGraph &g,
|
||||||
vector<NFAVertex>::const_reverse_iterator &it,
|
vector<NFAVertex>::const_reverse_iterator &it,
|
||||||
const vector<NFAVertex>::const_reverse_iterator &ite,
|
const vector<NFAVertex>::const_reverse_iterator &ite,
|
||||||
ue2::unordered_set<NFAVertex> *candidate,
|
unordered_set<NFAVertex> &candidate,
|
||||||
/* in exits of prev region;
|
/* in exits of prev region;
|
||||||
* out exits from candidate */
|
* out exits from candidate */
|
||||||
vector<exit_info> &exits,
|
vector<exit_info> &exits,
|
||||||
flat_set<NFAVertex> *open_jumps) {
|
flat_set<NFAVertex> &open_jumps) {
|
||||||
if (it == ite) {
|
if (it == ite) {
|
||||||
candidate->clear();
|
candidate.clear();
|
||||||
exits.clear();
|
exits.clear();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (exits.empty()) {
|
if (exits.empty()) {
|
||||||
DEBUG_PRINTF("odd\n");
|
DEBUG_PRINTF("odd\n");
|
||||||
candidate->clear();
|
candidate.clear();
|
||||||
DEBUG_PRINTF("adding %zu to initial\n", g[*it].index);
|
DEBUG_PRINTF("adding %zu to initial\n", g[*it].index);
|
||||||
candidate->insert(*it);
|
candidate.insert(*it);
|
||||||
open_jumps->erase(*it);
|
open_jumps.erase(*it);
|
||||||
checkAndAddExitCandidate(g, *candidate, *it, exits);
|
checkAndAddExitCandidate(g, candidate, *it, exits);
|
||||||
++it;
|
++it;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
auto enters = exits.front().open; // copy
|
auto enters = exits.front().open; // copy
|
||||||
candidate->clear();
|
candidate.clear();
|
||||||
|
|
||||||
for (; it != ite; ++it) {
|
for (; it != ite; ++it) {
|
||||||
DEBUG_PRINTF("adding %zu to initial\n", g[*it].index);
|
DEBUG_PRINTF("adding %zu to initial\n", g[*it].index);
|
||||||
candidate->insert(*it);
|
candidate.insert(*it);
|
||||||
if (contains(enters, *it)) {
|
if (contains(enters, *it)) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@ -208,24 +207,24 @@ void buildInitialCandidate(const AcyclicGraph &g,
|
|||||||
|
|
||||||
if (it != ite) {
|
if (it != ite) {
|
||||||
enters.erase(*it);
|
enters.erase(*it);
|
||||||
*open_jumps = move(enters);
|
open_jumps = move(enters);
|
||||||
DEBUG_PRINTF("oj size = %zu\n", open_jumps->size());
|
DEBUG_PRINTF("oj size = %zu\n", open_jumps.size());
|
||||||
++it;
|
++it;
|
||||||
} else {
|
} else {
|
||||||
open_jumps->clear();
|
open_jumps.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
findExits(g, *candidate, exits);
|
findExits(g, candidate, exits);
|
||||||
}
|
}
|
||||||
|
|
||||||
static
|
static
|
||||||
void findDagLeaders(const NGHolder &h, const AcyclicGraph &g,
|
void findDagLeaders(const NGHolder &h, const AcyclicGraph &g,
|
||||||
const vector<NFAVertex> &topo,
|
const vector<NFAVertex> &topo,
|
||||||
ue2::unordered_map<NFAVertex, u32> ®ions) {
|
unordered_map<NFAVertex, u32> ®ions) {
|
||||||
assert(!topo.empty());
|
assert(!topo.empty());
|
||||||
u32 curr_id = 0;
|
u32 curr_id = 0;
|
||||||
vector<NFAVertex>::const_reverse_iterator t_it = topo.rbegin();
|
auto t_it = topo.rbegin();
|
||||||
ue2::unordered_set<NFAVertex> candidate;
|
unordered_set<NFAVertex> candidate;
|
||||||
flat_set<NFAVertex> open_jumps;
|
flat_set<NFAVertex> open_jumps;
|
||||||
DEBUG_PRINTF("adding %zu to current\n", g[*t_it].index);
|
DEBUG_PRINTF("adding %zu to current\n", g[*t_it].index);
|
||||||
assert(t_it != topo.rend());
|
assert(t_it != topo.rend());
|
||||||
@ -251,8 +250,8 @@ void findDagLeaders(const NGHolder &h, const AcyclicGraph &g,
|
|||||||
DEBUG_PRINTF("setting region %u\n", curr_id);
|
DEBUG_PRINTF("setting region %u\n", curr_id);
|
||||||
}
|
}
|
||||||
setRegion(candidate, curr_id++, regions);
|
setRegion(candidate, curr_id++, regions);
|
||||||
buildInitialCandidate(g, t_it, topo.rend(), &candidate, exits,
|
buildInitialCandidate(g, t_it, topo.rend(), candidate, exits,
|
||||||
&open_jumps);
|
open_jumps);
|
||||||
} else {
|
} else {
|
||||||
NFAVertex curr = *t_it;
|
NFAVertex curr = *t_it;
|
||||||
DEBUG_PRINTF("adding %zu to current\n", g[curr].index);
|
DEBUG_PRINTF("adding %zu to current\n", g[curr].index);
|
||||||
@ -271,7 +270,7 @@ void findDagLeaders(const NGHolder &h, const AcyclicGraph &g,
|
|||||||
static
|
static
|
||||||
void mergeUnderBackEdges(const NGHolder &g, const vector<NFAVertex> &topo,
|
void mergeUnderBackEdges(const NGHolder &g, const vector<NFAVertex> &topo,
|
||||||
const BackEdgeSet &backEdges,
|
const BackEdgeSet &backEdges,
|
||||||
ue2::unordered_map<NFAVertex, u32> ®ions) {
|
unordered_map<NFAVertex, u32> ®ions) {
|
||||||
for (const auto &e : backEdges) {
|
for (const auto &e : backEdges) {
|
||||||
NFAVertex u = source(e, g);
|
NFAVertex u = source(e, g);
|
||||||
NFAVertex v = target(e, g);
|
NFAVertex v = target(e, g);
|
||||||
@ -341,7 +340,7 @@ void reorderSpecials(const NGHolder &w, const AcyclicGraph &acyclic_g,
|
|||||||
|
|
||||||
static
|
static
|
||||||
void liftSinks(const AcyclicGraph &acyclic_g, vector<NFAVertex> &topoOrder) {
|
void liftSinks(const AcyclicGraph &acyclic_g, vector<NFAVertex> &topoOrder) {
|
||||||
ue2::unordered_set<NFAVertex> sinks;
|
unordered_set<NFAVertex> sinks;
|
||||||
for (auto v : vertices_range(acyclic_g)) {
|
for (auto v : vertices_range(acyclic_g)) {
|
||||||
if (is_special(v, acyclic_g)) {
|
if (is_special(v, acyclic_g)) {
|
||||||
continue;
|
continue;
|
||||||
@ -386,7 +385,7 @@ void liftSinks(const AcyclicGraph &acyclic_g, vector<NFAVertex> &topoOrder) {
|
|||||||
}
|
}
|
||||||
NFAVertex s = *ri;
|
NFAVertex s = *ri;
|
||||||
DEBUG_PRINTF("handling sink %zu\n", acyclic_g[s].index);
|
DEBUG_PRINTF("handling sink %zu\n", acyclic_g[s].index);
|
||||||
ue2::unordered_set<NFAVertex> parents;
|
unordered_set<NFAVertex> parents;
|
||||||
for (const auto &e : in_edges_range(s, acyclic_g)) {
|
for (const auto &e : in_edges_range(s, acyclic_g)) {
|
||||||
parents.insert(NFAVertex(source(e, acyclic_g)));
|
parents.insert(NFAVertex(source(e, acyclic_g)));
|
||||||
}
|
}
|
||||||
@ -437,7 +436,7 @@ vector<NFAVertex> buildTopoOrder(const NGHolder &w,
|
|||||||
return topoOrder;
|
return topoOrder;
|
||||||
}
|
}
|
||||||
|
|
||||||
ue2::unordered_map<NFAVertex, u32> assignRegions(const NGHolder &g) {
|
unordered_map<NFAVertex, u32> assignRegions(const NGHolder &g) {
|
||||||
assert(hasCorrectlyNumberedVertices(g));
|
assert(hasCorrectlyNumberedVertices(g));
|
||||||
const u32 numVertices = num_vertices(g);
|
const u32 numVertices = num_vertices(g);
|
||||||
DEBUG_PRINTF("assigning regions for %u vertices in holder\n", numVertices);
|
DEBUG_PRINTF("assigning regions for %u vertices in holder\n", numVertices);
|
||||||
@ -459,7 +458,7 @@ ue2::unordered_map<NFAVertex, u32> assignRegions(const NGHolder &g) {
|
|||||||
vector<NFAVertex> topoOrder = buildTopoOrder(g, acyclic_g, colours);
|
vector<NFAVertex> topoOrder = buildTopoOrder(g, acyclic_g, colours);
|
||||||
|
|
||||||
// Everybody starts in region 0.
|
// Everybody starts in region 0.
|
||||||
ue2::unordered_map<NFAVertex, u32> regions;
|
unordered_map<NFAVertex, u32> regions;
|
||||||
regions.reserve(numVertices);
|
regions.reserve(numVertices);
|
||||||
for (auto v : vertices_range(g)) {
|
for (auto v : vertices_range(g)) {
|
||||||
regions.emplace(v, 0);
|
regions.emplace(v, 0);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user