]> git.ipfire.org Git - thirdparty/vectorscan.git/commitdiff
ng_undirected: modernize code
authorJustin Viiret <justin.viiret@intel.com>
Tue, 21 Mar 2017 01:31:08 +0000 (12:31 +1100)
committerMatthew Barr <matthew.barr@intel.com>
Wed, 26 Apr 2017 05:18:13 +0000 (15:18 +1000)
src/nfagraph/ng_calc_components.cpp
src/nfagraph/ng_repeat.cpp
src/nfagraph/ng_undirected.h

index 2c1dbcdb4a4f3bd044390c4660072246973bde1a..a7d8dd69a0c3d7353f02c08116ea389cc976244b 100644 (file)
@@ -277,10 +277,8 @@ void splitIntoComponents(unique_ptr<NGHolder> g,
     DEBUG_PRINTF("%zu vertices in head, %zu in tail, %zu shell edges\n",
                  head_shell.size(), tail_shell.size(), shell_edges.size());
 
-    NFAUndirectedGraph ug;
     ue2::unordered_map<NFAVertex, NFAUndirectedVertex> old2new;
-
-    createUnGraph(*g, true, true, ug, old2new);
+    auto ug = createUnGraph(*g, true, true, old2new);
 
     // Construct reverse mapping.
     ue2::unordered_map<NFAUndirectedVertex, NFAVertex> new2old;
index a16e2715b2ffffb9dcc1c17f710888be99c0703c..96e3266f76dc5859e51e4a8bb5869d02606c60da 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2015-2016, Intel Corporation
+ * Copyright (c) 2015-2017, Intel Corporation
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions are met:
@@ -296,9 +296,8 @@ void splitSubgraph(const NGHolder &g, const deque<NFAVertex> &verts,
     ue2::unordered_map<NFAVertex, NFAVertex> verts_map; // in g -> in verts_g
     fillHolder(&verts_g, g, verts, &verts_map);
 
-    NFAUndirectedGraph ug;
     ue2::unordered_map<NFAVertex, NFAUndirectedVertex> old2new;
-    createUnGraph(verts_g, true, true, ug, old2new);
+    auto ug = createUnGraph(verts_g, true, true, old2new);
 
     ue2::unordered_map<NFAUndirectedVertex, u32> repeatMap;
 
@@ -1020,9 +1019,8 @@ void buildReachSubgraphs(const NGHolder &g, vector<ReachSubgraph> &rs,
         return;
     }
 
-    NFAUndirectedGraph ug;
     unordered_map<RepeatGraph::vertex_descriptor, NFAUndirectedVertex> old2new;
-    createUnGraph(rg, true, true, ug, old2new);
+    auto ug = createUnGraph(rg, true, true, old2new);
 
     unordered_map<NFAUndirectedVertex, u32> repeatMap;
 
index 7df6c7dc4d45f22912f62f39a30d6d1640886674..b9fbd58d2a548e794135045c023fe687dcb239ca 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2015-2016, Intel Corporation
+ * Copyright (c) 2015-2017, Intel Corporation
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions are met:
@@ -30,8 +30,8 @@
  * \brief Create an undirected graph from an NFAGraph.
  */
 
-#ifndef NG_UNDIRECTED_H_CB42C71CF38E3D
-#define NG_UNDIRECTED_H_CB42C71CF38E3D
+#ifndef NG_UNDIRECTED_H
+#define NG_UNDIRECTED_H
 
 #include "ng_holder.h"
 #include "ng_util.h"
@@ -52,13 +52,13 @@ namespace ue2 {
  * of parallel edges. The only vertex property constructed is \a
  * vertex_index_t.
  */
-typedef boost::adjacency_list<boost::setS,        // out edges
-                              boost::listS,       // vertices
-                              boost::undirectedS, // graph is undirected
-                              boost::property<boost::vertex_index_t, size_t> >
-NFAUndirectedGraph;
+using NFAUndirectedGraph =
+    boost::adjacency_list<boost::setS,        // out edges
+                          boost::listS,       // vertices
+                          boost::undirectedS, // graph is undirected
+                          boost::property<boost::vertex_index_t, size_t>>;
 
-typedef NFAUndirectedGraph::vertex_descriptor NFAUndirectedVertex;
+using NFAUndirectedVertex = NFAUndirectedGraph::vertex_descriptor;
 
 /**
  * Make a copy of an NFAGraph with undirected edges, optionally without start
@@ -67,15 +67,17 @@ typedef NFAUndirectedGraph::vertex_descriptor NFAUndirectedVertex;
  * Note that new vertex indices are assigned contiguously in \a vertices(g)
  * order.
  */
-template <typename GraphT>
-void createUnGraph(const GraphT &g,
+template <typename Graph>
+NFAUndirectedGraph createUnGraph(const Graph &g,
            bool excludeStarts,
            bool excludeAccepts,
-           NFAUndirectedGraph &ug,
-           ue2::unordered_map<typename GraphT::vertex_descriptor,
-                              NFAUndirectedVertex> &old2new) {
+           unordered_map<typename Graph::vertex_descriptor,
+                         NFAUndirectedVertex> &old2new) {
+    NFAUndirectedGraph ug;
     size_t idx = 0;
-    typedef typename GraphT::vertex_descriptor VertexT;
+
+    assert(old2new.empty());
+    old2new.reserve(num_vertices(g));
 
     for (auto v : ue2::vertices_range(g)) {
         // skip all accept nodes
@@ -88,32 +90,35 @@ void createUnGraph(const GraphT &g,
             continue;
         }
 
-        NFAUndirectedVertex nuv = boost::add_vertex(ug);
-        old2new[v] = nuv;
+        auto nuv = boost::add_vertex(ug);
+        old2new.emplace(v, nuv);
         boost::put(boost::vertex_index, ug, nuv, idx++);
     }
 
     for (const auto &e : ue2::edges_range(g)) {
-        VertexT src = source(e, g);
-        VertexT targ = target(e, g);
+        auto u = source(e, g);
+        auto v = target(e, g);
 
-        if ((excludeAccepts && is_any_accept(src, g))
-            || (excludeStarts && is_any_start(src, g))) {
+        if ((excludeAccepts && is_any_accept(u, g))
+            || (excludeStarts && is_any_start(u, g))) {
             continue;
         }
 
-        if ((excludeAccepts && is_any_accept(targ, g))
-            || (excludeStarts && is_any_start(targ, g))) {
+        if ((excludeAccepts && is_any_accept(v, g))
+            || (excludeStarts && is_any_start(v, g))) {
             continue;
         }
 
-        NFAUndirectedVertex new_src = old2new[src];
-        NFAUndirectedVertex new_targ = old2new[targ];
+        NFAUndirectedVertex new_u = old2new.at(u);
+        NFAUndirectedVertex new_v = old2new.at(v);
 
-        boost::add_edge(new_src, new_targ, ug);
+        boost::add_edge(new_u, new_v, ug);
     }
+
+    assert(!has_parallel_edge(ug));
+    return ug;
 }
 
 } // namespace ue2
 
-#endif /* NG_UNDIRECTED_H_CB42C71CF38E3D */
+#endif /* NG_UNDIRECTED_H */