From 9724f8c3cc92282fd9576c6eab38d449aecc991d Mon Sep 17 00:00:00 2001 From: Justin Viiret Date: Tue, 21 Mar 2017 12:31:08 +1100 Subject: [PATCH] ng_undirected: modernize code --- src/nfagraph/ng_calc_components.cpp | 4 +- src/nfagraph/ng_repeat.cpp | 8 ++-- src/nfagraph/ng_undirected.h | 59 ++++++++++++++++------------- 3 files changed, 36 insertions(+), 35 deletions(-) diff --git a/src/nfagraph/ng_calc_components.cpp b/src/nfagraph/ng_calc_components.cpp index 2c1dbcdb..a7d8dd69 100644 --- a/src/nfagraph/ng_calc_components.cpp +++ b/src/nfagraph/ng_calc_components.cpp @@ -277,10 +277,8 @@ void splitIntoComponents(unique_ptr 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 old2new; - - createUnGraph(*g, true, true, ug, old2new); + auto ug = createUnGraph(*g, true, true, old2new); // Construct reverse mapping. ue2::unordered_map new2old; diff --git a/src/nfagraph/ng_repeat.cpp b/src/nfagraph/ng_repeat.cpp index a16e2715..96e3266f 100644 --- a/src/nfagraph/ng_repeat.cpp +++ b/src/nfagraph/ng_repeat.cpp @@ -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 &verts, ue2::unordered_map verts_map; // in g -> in verts_g fillHolder(&verts_g, g, verts, &verts_map); - NFAUndirectedGraph ug; ue2::unordered_map old2new; - createUnGraph(verts_g, true, true, ug, old2new); + auto ug = createUnGraph(verts_g, true, true, old2new); ue2::unordered_map repeatMap; @@ -1020,9 +1019,8 @@ void buildReachSubgraphs(const NGHolder &g, vector &rs, return; } - NFAUndirectedGraph ug; unordered_map old2new; - createUnGraph(rg, true, true, ug, old2new); + auto ug = createUnGraph(rg, true, true, old2new); unordered_map repeatMap; diff --git a/src/nfagraph/ng_undirected.h b/src/nfagraph/ng_undirected.h index 7df6c7dc..b9fbd58d 100644 --- a/src/nfagraph/ng_undirected.h +++ b/src/nfagraph/ng_undirected.h @@ -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 > -NFAUndirectedGraph; +using NFAUndirectedGraph = + boost::adjacency_list>; -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 -void createUnGraph(const GraphT &g, +template +NFAUndirectedGraph createUnGraph(const Graph &g, bool excludeStarts, bool excludeAccepts, - NFAUndirectedGraph &ug, - ue2::unordered_map &old2new) { + unordered_map &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 */