ng_depth: modernize findLoopReachable

This commit is contained in:
Justin Viiret 2017-05-01 15:08:06 +10:00 committed by Matthew Barr
parent dfe1b8a2af
commit c17085ba35

View File

@ -124,34 +124,35 @@ private:
} // namespace } // namespace
template<class GraphT> template<class Graph>
static static
void findLoopReachable(const GraphT &g, vector<bool> findLoopReachable(const Graph &g,
const typename GraphT::vertex_descriptor srcVertex, const typename Graph::vertex_descriptor src) {
vector<bool> &deadNodes) { vector<bool> deadNodes(num_vertices(g));
typedef typename GraphT::edge_descriptor EdgeT;
typedef typename GraphT::vertex_descriptor VertexT; using Edge = typename Graph::edge_descriptor;
typedef set<EdgeT> EdgeSet; using Vertex = typename Graph::vertex_descriptor;
using EdgeSet = set<Edge>;
EdgeSet deadEdges; EdgeSet deadEdges;
BackEdges<EdgeSet> be(deadEdges); BackEdges<EdgeSet> be(deadEdges);
depth_first_search(g, visitor(be).root_vertex(srcVertex)); depth_first_search(g, visitor(be).root_vertex(src));
auto af = make_bad_edge_filter(&deadEdges); auto af = make_bad_edge_filter(&deadEdges);
auto acyclic_g = make_filtered_graph(g, af); auto acyclic_g = make_filtered_graph(g, af);
vector<VertexT> topoOrder; /* actually reverse topological order */ vector<Vertex> topoOrder; /* actually reverse topological order */
topoOrder.reserve(deadNodes.size()); topoOrder.reserve(deadNodes.size());
topological_sort(acyclic_g, back_inserter(topoOrder)); topological_sort(acyclic_g, back_inserter(topoOrder));
for (const auto &e : deadEdges) { for (const auto &e : deadEdges) {
u32 srcIdx = g[source(e, g)].index; size_t srcIdx = g[source(e, g)].index;
if (srcIdx != NODE_START_DOTSTAR) { if (srcIdx != NODE_START_DOTSTAR) {
deadNodes[srcIdx] = true; deadNodes[srcIdx] = true;
} }
} }
for (VertexT v : reverse(topoOrder)) { for (auto v : reverse(topoOrder)) {
for (const auto &e : in_edges_range(v, g)) { for (const auto &e : in_edges_range(v, g)) {
if (deadNodes[g[source(e, g)].index]) { if (deadNodes[g[source(e, g)].index]) {
deadNodes[g[v].index] = true; deadNodes[g[v].index] = true;
@ -159,6 +160,8 @@ void findLoopReachable(const GraphT &g,
} }
} }
} }
return deadNodes;
} }
template <class GraphT> template <class GraphT>
@ -282,8 +285,7 @@ vector<NFAVertexDepth> calcDepths(const NGHolder &g) {
* create a filtered graph for max depth calculations: all nodes/edges * create a filtered graph for max depth calculations: all nodes/edges
* reachable from a loop need to be removed * reachable from a loop need to be removed
*/ */
vector<bool> deadNodes(numVertices); auto deadNodes = findLoopReachable(g, g.start);
findLoopReachable(g, g.start, deadNodes);
DEBUG_PRINTF("doing start\n"); DEBUG_PRINTF("doing start\n");
calcAndStoreDepth(g, g.start, deadNodes, dMin, dMax, depths, calcAndStoreDepth(g, g.start, deadNodes, dMin, dMax, depths,
@ -313,8 +315,7 @@ vector<NFAVertexRevDepth> calcRevDepths(const NGHolder &g) {
* create a filtered graph for max depth calculations: all nodes/edges * create a filtered graph for max depth calculations: all nodes/edges
* reachable from a loop need to be removed * reachable from a loop need to be removed
*/ */
vector<bool> deadNodes(numVertices); auto deadNodes = findLoopReachable(rg, g.acceptEod);
findLoopReachable(rg, g.acceptEod, deadNodes);
DEBUG_PRINTF("doing accept\n"); DEBUG_PRINTF("doing accept\n");
calcAndStoreDepth<RevNFAGraph, NFAVertexRevDepth>( calcAndStoreDepth<RevNFAGraph, NFAVertexRevDepth>(
@ -341,8 +342,7 @@ vector<NFAVertexBidiDepth> calcBidiDepths(const NGHolder &g) {
* create a filtered graph for max depth calculations: all nodes/edges * create a filtered graph for max depth calculations: all nodes/edges
* reachable from a loop need to be removed * reachable from a loop need to be removed
*/ */
vector<bool> deadNodes(numVertices); auto deadNodes = findLoopReachable(g, g.start);
findLoopReachable(g, g.start, deadNodes);
DEBUG_PRINTF("doing start\n"); DEBUG_PRINTF("doing start\n");
calcAndStoreDepth<NGHolder, NFAVertexBidiDepth>( calcAndStoreDepth<NGHolder, NFAVertexBidiDepth>(
@ -356,8 +356,7 @@ vector<NFAVertexBidiDepth> calcBidiDepths(const NGHolder &g) {
/* Now go backwards */ /* Now go backwards */
typedef reverse_graph<NGHolder, const NGHolder &> RevNFAGraph; typedef reverse_graph<NGHolder, const NGHolder &> RevNFAGraph;
const RevNFAGraph rg(g); const RevNFAGraph rg(g);
deadNodes.assign(numVertices, false); deadNodes = findLoopReachable(rg, g.acceptEod);
findLoopReachable(rg, g.acceptEod, deadNodes);
DEBUG_PRINTF("doing accept\n"); DEBUG_PRINTF("doing accept\n");
calcAndStoreDepth<RevNFAGraph, NFAVertexBidiDepth>( calcAndStoreDepth<RevNFAGraph, NFAVertexBidiDepth>(
@ -376,8 +375,7 @@ vector<DepthMinMax> calcDepthsFrom(const NGHolder &g, const NFAVertex src) {
assert(hasCorrectlyNumberedVertices(g)); assert(hasCorrectlyNumberedVertices(g));
const size_t numVertices = num_vertices(g); const size_t numVertices = num_vertices(g);
vector<bool> deadNodes(numVertices); auto deadNodes = findLoopReachable(g, g.start);
findLoopReachable(g, g.start, deadNodes);
vector<int> dMin, dMax; vector<int> dMin, dMax;
calcDepthFromSource(g, src, deadNodes, dMin, dMax); calcDepthFromSource(g, src, deadNodes, dMin, dMax);