Introduce custom adjacency-list based graph

This commit is contained in:
Alex Coyte
2016-08-24 16:12:51 +10:00
committed by Matthew Barr
parent 05683655cb
commit e1e9010cac
92 changed files with 3730 additions and 1812 deletions

View File

@@ -309,14 +309,10 @@ static
bool hasInEdgeTops(const NGHolder &g, NFAVertex v) {
bool exists;
NFAEdge e;
tie(e, exists) = edge_by_target(g.start, v, g);
if (exists && !g[e].tops.empty()) {
return true;
}
return false;
tie(e, exists) = edge(g.start, v, g);
return exists && !g[e].tops.empty();
}
/** Transform (1), removal of redundant vertices. */
static
bool doUselessMergePass(NGHolder &g, som_type som, VertexInfoMap &infoMap,
@@ -348,8 +344,7 @@ bool doUselessMergePass(NGHolder &g, som_type som, VertexInfoMap &infoMap,
}
if (info.pred.empty() || info.succ.empty()) {
DEBUG_PRINTF("vertex %u has empty pred/succ list\n",
g[v].index);
DEBUG_PRINTF("vertex %zu has empty pred/succ list\n", g[v].index);
assert(0); // non-special states should always have succ/pred lists
continue;
}
@@ -448,7 +443,7 @@ bool doUselessMergePass(NGHolder &g, som_type som, VertexInfoMap &infoMap,
CharReach &otherReach = g[t].char_reach;
if (currReach.isSubsetOf(otherReach)) {
DEBUG_PRINTF("removing redundant vertex %u (keeping %u)\n",
DEBUG_PRINTF("removing redundant vertex %zu (keeping %zu)\n",
g[v].index, g[t].index);
markForRemoval(v, infoMap, removable);
changed = true;
@@ -539,9 +534,6 @@ bool doDiamondMergePass(NGHolder &g, som_type som, VertexInfoMap &infoMap,
continue;
}
/* ensure that we look for candidates in the same order */
sort(intersection.begin(), intersection.end(), make_index_ordering(g));
const CharReach &currReach = g[v].char_reach;
const auto &currReports = g[v].reports;
for (auto t : intersection) {
@@ -578,8 +570,8 @@ bool doDiamondMergePass(NGHolder &g, som_type som, VertexInfoMap &infoMap,
CharReach &otherReach = g[t].char_reach;
otherReach |= currReach;
// v can be removed
DEBUG_PRINTF("removing redundant vertex %u and merging "
"reachability with vertex %u\n",
DEBUG_PRINTF("removing redundant vertex %zu and merging "
"reachability with vertex %zu\n",
g[v].index, g[t].index);
markForRemoval(v, infoMap, removable);
changed = true;
@@ -645,14 +637,14 @@ bool reversePathReachSubset(const NFAEdge &e, const NFAVertex &dom,
}
NFAVertex start = source(e, g);
using RevGraph = boost::reverse_graph<NFAGraph, const NFAGraph &>;
using RevGraph = boost::reverse_graph<NGHolder, const NGHolder &>;
map<RevGraph::vertex_descriptor, boost::default_color_type> vertexColor;
// Walk the graph backwards from v, examining each node. We fail (return
// false) if we encounter a node with reach NOT a subset of domReach, and
// we stop searching at dom.
try {
depth_first_visit(RevGraph(g.g), start,
depth_first_visit(RevGraph(g), start,
ReachSubsetVisitor(domReach),
make_assoc_property_map(vertexColor),
VertexIs<RevGraph, RevGraph::vertex_descriptor>(dom));
@@ -674,16 +666,15 @@ bool forwardPathReachSubset(const NFAEdge &e, const NFAVertex &dom,
}
NFAVertex start = target(e, g);
map<NFAGraph::vertex_descriptor, boost::default_color_type> vertexColor;
map<NFAVertex, boost::default_color_type> vertexColor;
// Walk the graph forward from v, examining each node. We fail (return
// false) if we encounter a node with reach NOT a subset of domReach, and
// we stop searching at dom.
try {
depth_first_visit(g.g, start,
ReachSubsetVisitor(domReach),
depth_first_visit(g, start, ReachSubsetVisitor(domReach),
make_assoc_property_map(vertexColor),
VertexIs<NFAGraph, NFAVertex>(dom));
VertexIs<NGHolder, NFAVertex>(dom));
} catch(ReachMismatch&) {
return false;
}
@@ -775,9 +766,8 @@ void findCyclicDom(NGHolder &g, vector<bool> &cyclic,
continue;
}
DEBUG_PRINTF("vertex %u is dominated by directly-connected cyclic "
"vertex %u\n", g[v].index,
g[dom].index);
DEBUG_PRINTF("vertex %zu is dominated by directly-connected cyclic "
"vertex %zu\n", g[v].index, g[dom].index);
// iff all paths through in-edge e of v involve vertices whose
// reachability is a subset of reach(dom), we can delete edge e.
@@ -787,8 +777,8 @@ void findCyclicDom(NGHolder &g, vector<bool> &cyclic,
}
if (reversePathReachSubset(e, dom, g)) {
DEBUG_PRINTF("edge (%u, %u) can be removed: leading paths "
"share dom reach\n",
DEBUG_PRINTF("edge (%zu, %zu) can be removed: leading "
"paths share dom reach\n",
g[source(e, g)].index, g[target(e, g)].index);
dead.insert(e);
if (source(e, g) == v) {
@@ -814,11 +804,9 @@ void findCyclicPostDom(NGHolder &g, vector<bool> &cyclic,
// Path out through a post-dominator (e.g. a?.+foobar')
NFAVertex postdom = postdominators[v];
if (postdom && cyclic[g[postdom].index]
&& edge(v, postdom, g).second) {
DEBUG_PRINTF("vertex %u is postdominated by directly-connected "
"cyclic vertex %u\n", g[v].index,
g[postdom].index);
if (postdom && cyclic[g[postdom].index] && edge(v, postdom, g).second) {
DEBUG_PRINTF("vertex %zu is postdominated by directly-connected "
"cyclic vertex %zu\n", g[v].index, g[postdom].index);
// iff all paths through in-edge e of v involve vertices whose
// reachability is a subset of reach(dom), we can delete edge e.
@@ -828,8 +816,8 @@ void findCyclicPostDom(NGHolder &g, vector<bool> &cyclic,
}
if (forwardPathReachSubset(e, postdom, g)) {
DEBUG_PRINTF("edge (%u, %u) can be removed: trailing paths "
"share postdom reach\n",
DEBUG_PRINTF("edge (%zu, %zu) can be removed: trailing "
"paths share postdom reach\n",
g[source(e, g)].index, g[target(e, g)].index);
if (target(e, g) == v) {
cyclic[g[v].index] = false;
@@ -844,7 +832,7 @@ void findCyclicPostDom(NGHolder &g, vector<bool> &cyclic,
bool removeRedundancy(NGHolder &g, som_type som) {
DEBUG_PRINTF("rr som = %d\n", (int)som);
g.renumberVertices();
renumber_vertices(g);
// Cheap check: if all the non-special vertices have in-degree one and
// out-degree one, there's no redundancy in this here graph and we can