diff --git a/src/nfagraph/ng_calc_components.cpp b/src/nfagraph/ng_calc_components.cpp index 54221c7b..bfe73eb2 100644 --- a/src/nfagraph/ng_calc_components.cpp +++ b/src/nfagraph/ng_calc_components.cpp @@ -235,8 +235,7 @@ void splitIntoComponents(unique_ptr g, *shell_comp = false; // Compute "shell" head and tail subgraphs. - vector depths; - calcDepths(*g, depths); + auto depths = calcBidiDepths(*g); auto head_shell = findHeadShell(*g, depths, max_head_depth); auto tail_shell = findTailShell(*g, depths, max_tail_depth); for (auto v : head_shell) { diff --git a/src/nfagraph/ng_depth.cpp b/src/nfagraph/ng_depth.cpp index 63e0e46b..6c45fa2f 100644 --- a/src/nfagraph/ng_depth.cpp +++ b/src/nfagraph/ng_depth.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: @@ -26,7 +26,8 @@ * POSSIBILITY OF SUCH DAMAGE. */ -/** \file +/** + * \file * \brief NFA graph vertex depth calculations. */ #include "ng_depth.h" @@ -269,12 +270,11 @@ void calcAndStoreDepth(const Graph &g, } } -void calcDepths(const NGHolder &g, std::vector &depths) { +vector calcDepths(const NGHolder &g) { assert(hasCorrectlyNumberedVertices(g)); const size_t numVertices = num_vertices(g); - depths.clear(); - depths.resize(numVertices); + vector depths(numVertices); vector dMin; vector dMax; @@ -291,14 +291,15 @@ void calcDepths(const NGHolder &g, std::vector &depths) { DEBUG_PRINTF("doing startds\n"); calcAndStoreDepth(g, g.startDs, deadNodes, dMin, dMax, depths, &NFAVertexDepth::fromStartDotStar); + + return depths; } -void calcDepths(const NGHolder &g, std::vector &depths) { +vector calcRevDepths(const NGHolder &g) { assert(hasCorrectlyNumberedVertices(g)); const size_t numVertices = num_vertices(g); - depths.clear(); - depths.resize(numVertices); + vector depths(numVertices); vector dMin; vector dMax; @@ -324,14 +325,15 @@ void calcDepths(const NGHolder &g, std::vector &depths) { calcAndStoreDepth( rg, g.acceptEod, deadNodes, dMin, dMax, depths, &NFAVertexRevDepth::toAcceptEod); + + return depths; } -void calcDepths(const NGHolder &g, vector &depths) { +vector calcBidiDepths(const NGHolder &g) { assert(hasCorrectlyNumberedVertices(g)); const size_t numVertices = num_vertices(g); - depths.clear(); - depths.resize(numVertices); + vector depths(numVertices); vector dMin; vector dMax; @@ -366,10 +368,11 @@ void calcDepths(const NGHolder &g, vector &depths) { calcAndStoreDepth( rg, g.acceptEod, deadNodes, dMin, dMax, depths, &NFAVertexBidiDepth::toAcceptEod); + + return depths; } -void calcDepthsFrom(const NGHolder &g, const NFAVertex src, - vector &depths) { +vector calcDepthsFrom(const NGHolder &g, const NFAVertex src) { assert(hasCorrectlyNumberedVertices(g)); const size_t numVertices = num_vertices(g); @@ -379,13 +382,14 @@ void calcDepthsFrom(const NGHolder &g, const NFAVertex src, vector dMin, dMax; calcDepthFromSource(g, src, deadNodes, dMin, dMax); - depths.clear(); - depths.resize(numVertices); + vector depths(numVertices); for (auto v : vertices_range(g)) { - u32 idx = g[v].index; + auto idx = g[v].index; depths.at(idx) = getDepths(idx, dMin, dMax); } + + return depths; } } // namespace ue2 diff --git a/src/nfagraph/ng_depth.h b/src/nfagraph/ng_depth.h index 16231ea1..36cca87e 100644 --- a/src/nfagraph/ng_depth.h +++ b/src/nfagraph/ng_depth.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: @@ -26,23 +26,22 @@ * POSSIBILITY OF SUCH DAMAGE. */ -/** \file +/** + * \file * \brief NFA graph vertex depth calculations. */ -#ifndef STRUCTURAL_ANALYSIS_H -#define STRUCTURAL_ANALYSIS_H +#ifndef NG_DEPTH_H +#define NG_DEPTH_H -#include "nfagraph/ng_holder.h" #include "ue2common.h" +#include "nfagraph/ng_holder.h" #include "util/depth.h" #include namespace ue2 { -class NGHolder; - /** * \brief Encapsulates min/max depths relative to the start and startDs * vertices. @@ -72,28 +71,29 @@ struct NFAVertexBidiDepth { }; /** - * \brief Calculate depths from start and startDs. - * Fills the vector \p depths (indexed by \p vertex_index). + * \brief Calculate depths from start and startDs. Returns them in a vector, + * indexed by vertex index. */ -void calcDepths(const NGHolder &g, std::vector &depths); +std::vector calcDepths(const NGHolder &g); /** - * \brief Calculate depths to accept and acceptEod. - * Fills the vector \p depths (indexed by \p vertex_index). + * \brief Calculate depths to accept and acceptEod. Returns them in a vector, + * indexed by vertex index. */ -void calcDepths(const NGHolder &g, std::vector &depths); +std::vector calcRevDepths(const NGHolder &g); /** - * \brief Calculate depths to/from all special vertices. - * Fills the vector \p depths (indexed by \p vertex_index). + * \brief Calculate depths to/from all special vertices. Returns them in a + * vector, indexed by vertex index. */ -void calcDepths(const NGHolder &g, std::vector &depths); +std::vector calcBidiDepths(const NGHolder &g); -/** Calculate the (min, max) depths from the given \p src to every vertex in - * the graph and return them in a vector, indexed by \p vertex_index. */ -void calcDepthsFrom(const NGHolder &g, const NFAVertex src, - std::vector &depths); +/** + * \brief Calculate the (min, max) depths from the given \p src to every vertex + * in the graph and return them in a vector, indexed by \p vertex_index. + */ +std::vector calcDepthsFrom(const NGHolder &g, const NFAVertex src); } // namespace ue2 -#endif +#endif // NG_DEPTH_H diff --git a/src/nfagraph/ng_equivalence.cpp b/src/nfagraph/ng_equivalence.cpp index b9e2bd0d..f03a6629 100644 --- a/src/nfagraph/ng_equivalence.cpp +++ b/src/nfagraph/ng_equivalence.cpp @@ -342,9 +342,9 @@ vector partitionGraph(vector> &infos, vector rdepths; if (eq == LEFT_EQUIVALENCE) { - calcDepths(g, depths); + depths = calcDepths(g); } else { - calcDepths(g, rdepths); + rdepths = calcRevDepths(g); } // partition the graph based on CharReach diff --git a/src/nfagraph/ng_expr_info.cpp b/src/nfagraph/ng_expr_info.cpp index 9417b674..6a625ddf 100644 --- a/src/nfagraph/ng_expr_info.cpp +++ b/src/nfagraph/ng_expr_info.cpp @@ -147,8 +147,7 @@ void fillExpressionInfo(ReportManager &rm, NGHolder &g, removeLeadingVirtualVerticesFromRoot(g, g.start); removeLeadingVirtualVerticesFromRoot(g, g.startDs); - vector depths; - calcDepthsFrom(g, g.start, depths); + auto depths = calcDepthsFrom(g, g.start); DepthMinMax d; diff --git a/src/nfagraph/ng_extparam.cpp b/src/nfagraph/ng_extparam.cpp index 19fa2295..bc7f81ef 100644 --- a/src/nfagraph/ng_extparam.cpp +++ b/src/nfagraph/ng_extparam.cpp @@ -734,8 +734,7 @@ void pruneExtUnreachable(NGHolder &g, const ReportManager &rm) { const auto &report = rm.getReport(*reports.begin()); - vector depths; - calcDepths(g, depths); + auto depths = calcBidiDepths(g); vector dead; @@ -957,8 +956,7 @@ void replaceMinLengthWithOffset(NGHolder &g, ReportManager &rm) { */ static void removeUnneededOffsetBounds(NGHolder &g, ReportManager &rm) { - vector depths; - calcDepths(g, depths); + auto depths = calcDepths(g); replaceReports(g, [&](NFAVertex v, ReportID id) { const auto &d = depths.at(g[v].index); diff --git a/src/nfagraph/ng_fuzzy.cpp b/src/nfagraph/ng_fuzzy.cpp index fc468126..2c3d85bd 100644 --- a/src/nfagraph/ng_fuzzy.cpp +++ b/src/nfagraph/ng_fuzzy.cpp @@ -603,9 +603,7 @@ private: // check if we will edit our way into a vacuous pattern static bool will_turn_vacuous(const NGHolder &g, u32 edit_distance) { - vector depths; - - calcDepths(g, depths); + auto depths = calcRevDepths(g); depth min_depth = depth::infinity(); auto idx = g[g.start].index; diff --git a/src/nfagraph/ng_puff.cpp b/src/nfagraph/ng_puff.cpp index 7281471f..984518b0 100644 --- a/src/nfagraph/ng_puff.cpp +++ b/src/nfagraph/ng_puff.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: @@ -94,8 +94,7 @@ void wireNewAccepts(NGHolder &g, NFAVertex head, static bool isFixedDepth(const NGHolder &g, NFAVertex v) { // If the vertex is reachable from startDs, it can't be fixed depth. - vector depthFromStartDs; - calcDepthsFrom(g, g.startDs, depthFromStartDs); + auto depthFromStartDs = calcDepthsFrom(g, g.startDs); u32 idx = g[v].index; const DepthMinMax &ds = depthFromStartDs.at(idx); @@ -104,8 +103,7 @@ bool isFixedDepth(const NGHolder &g, NFAVertex v) { return false; } - vector depthFromStart; - calcDepthsFrom(g, g.start, depthFromStart); + auto depthFromStart = calcDepthsFrom(g, g.start); /* we can still consider the head of a puff chain as at fixed depth if * it has a self-loop: so we look at all the preds of v (other than v diff --git a/src/nfagraph/ng_repeat.cpp b/src/nfagraph/ng_repeat.cpp index c51618ea..60ad2200 100644 --- a/src/nfagraph/ng_repeat.cpp +++ b/src/nfagraph/ng_repeat.cpp @@ -118,13 +118,12 @@ struct ReachSubgraph { static void findInitDepths(const NGHolder &g, ue2::unordered_map &depths) { - vector d; - calcDepths(g, d); + auto d = calcDepths(g); for (auto v : vertices_range(g)) { - u32 idx = g[v].index; + size_t idx = g[v].index; assert(idx < d.size()); - depths.insert(make_pair(v, d[idx])); + depths.emplace(v, d[idx]); } } diff --git a/src/nfagraph/ng_som_util.cpp b/src/nfagraph/ng_som_util.cpp index 78a39119..a3b6ee5f 100644 --- a/src/nfagraph/ng_som_util.cpp +++ b/src/nfagraph/ng_som_util.cpp @@ -78,8 +78,8 @@ vector getDistancesFromSOM(const NGHolder &g_orig) { //dumpGraph("som_depth.dot", g); - vector temp_depths; // numbered by vertex index in g - calcDepthsFrom(g, g.start, temp_depths); + // Find depths, indexed by vertex index in g + auto temp_depths = calcDepthsFrom(g, g.start); // Transfer depths, indexed by vertex index in g_orig. vector depths(num_vertices(g_orig)); diff --git a/src/nfagraph/ng_stop.cpp b/src/nfagraph/ng_stop.cpp index e601f541..c335540a 100644 --- a/src/nfagraph/ng_stop.cpp +++ b/src/nfagraph/ng_stop.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2015, 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: @@ -60,10 +60,9 @@ namespace { /** Depths from start, startDs for this graph. */ struct InitDepths { - explicit InitDepths(const NGHolder &g) { - calcDepthsFrom(g, g.start, start); - calcDepthsFrom(g, g.startDs, startDs); - } + explicit InitDepths(const NGHolder &g) + : start(calcDepthsFrom(g, g.start)), + startDs(calcDepthsFrom(g, g.startDs)) {} depth maxDist(const NGHolder &g, NFAVertex v) const { u32 idx = g[v].index; diff --git a/src/nfagraph/ng_violet.cpp b/src/nfagraph/ng_violet.cpp index e2825643..4195045c 100644 --- a/src/nfagraph/ng_violet.cpp +++ b/src/nfagraph/ng_violet.cpp @@ -1472,8 +1472,7 @@ void avoidOutfixes(RoseInGraph &vg, bool last_chance, if (last_chance) { /* look for a prefix split as it allows us to accept very weak anchored * literals. */ - vector depths; - calcDepths(h, depths); + auto depths = calcDepths(h); split = findBestPrefixSplit(h, depths, vg, {e}, last_chance, cc); @@ -1973,8 +1972,7 @@ bool improvePrefix(NGHolder &h, RoseInGraph &vg, const vector &ee, renumber_vertices(h); renumber_edges(h); - vector depths; - calcDepths(h, depths); + auto depths = calcDepths(h); /* If the reason the prefix is not transient is due to a very long literal * following, we can make it transient by restricting ourselves to using @@ -2856,8 +2854,7 @@ bool splitForImplementabilty(RoseInGraph &vg, NGHolder &h, unique_ptr split; bool last_chance = true; if (h.kind == NFA_PREFIX) { - vector depths; - calcDepths(h, depths); + auto depths = calcDepths(h); split = findBestPrefixSplit(h, depths, vg, edges, last_chance, cc); } else { diff --git a/src/rose/rose_build_add.cpp b/src/rose/rose_build_add.cpp index 26f88445..4c895caf 100644 --- a/src/rose/rose_build_add.cpp +++ b/src/rose/rose_build_add.cpp @@ -1928,8 +1928,7 @@ void removeAddedLiterals(RoseBuildImpl &tbi, const flat_set &lit_ids) { } bool RoseBuildImpl::addAnchoredAcyclic(const NGHolder &h) { - vector vertexDepths; - calcDepthsFrom(h, h.start, vertexDepths); + auto vertexDepths = calcDepthsFrom(h, h.start); map > reportMap; /* NFAVertex -> literal ids */ map depthMap; /* literal id -> min/max depth */ diff --git a/src/smallwrite/smallwrite_build.cpp b/src/smallwrite/smallwrite_build.cpp index 4acfc713..ba2f244d 100644 --- a/src/smallwrite/smallwrite_build.cpp +++ b/src/smallwrite/smallwrite_build.cpp @@ -198,8 +198,7 @@ static bool pruneOverlong(NGHolder &g, const depth &max_depth, const ReportManager &rm) { bool modified = false; - std::vector depths; - calcDepths(g, depths); + auto depths = calcDepths(g); for (auto v : vertices_range(g)) { if (is_special(v, g)) {