]> git.ipfire.org Git - thirdparty/vectorscan.git/commitdiff
ng_depth: modernize findLoopReachable
authorJustin Viiret <justin.viiret@intel.com>
Mon, 1 May 2017 05:08:06 +0000 (15:08 +1000)
committerMatthew Barr <matthew.barr@intel.com>
Tue, 30 May 2017 03:59:00 +0000 (13:59 +1000)
src/nfagraph/ng_depth.cpp

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