ng_depth: rename calcDepth functions, return vec

This commit is contained in:
Justin Viiret 2017-05-01 14:57:05 +10:00 committed by Matthew Barr
parent 15c8a7bd98
commit dfe1b8a2af
14 changed files with 65 additions and 76 deletions

View File

@ -235,8 +235,7 @@ void splitIntoComponents(unique_ptr<NGHolder> g,
*shell_comp = false; *shell_comp = false;
// Compute "shell" head and tail subgraphs. // Compute "shell" head and tail subgraphs.
vector<NFAVertexBidiDepth> depths; auto depths = calcBidiDepths(*g);
calcDepths(*g, depths);
auto head_shell = findHeadShell(*g, depths, max_head_depth); auto head_shell = findHeadShell(*g, depths, max_head_depth);
auto tail_shell = findTailShell(*g, depths, max_tail_depth); auto tail_shell = findTailShell(*g, depths, max_tail_depth);
for (auto v : head_shell) { for (auto v : head_shell) {

View 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 * Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met: * modification, are permitted provided that the following conditions are met:
@ -26,7 +26,8 @@
* POSSIBILITY OF SUCH DAMAGE. * POSSIBILITY OF SUCH DAMAGE.
*/ */
/** \file /**
* \file
* \brief NFA graph vertex depth calculations. * \brief NFA graph vertex depth calculations.
*/ */
#include "ng_depth.h" #include "ng_depth.h"
@ -269,12 +270,11 @@ void calcAndStoreDepth(const Graph &g,
} }
} }
void calcDepths(const NGHolder &g, std::vector<NFAVertexDepth> &depths) { vector<NFAVertexDepth> calcDepths(const NGHolder &g) {
assert(hasCorrectlyNumberedVertices(g)); assert(hasCorrectlyNumberedVertices(g));
const size_t numVertices = num_vertices(g); const size_t numVertices = num_vertices(g);
depths.clear();
depths.resize(numVertices);
vector<NFAVertexDepth> depths(numVertices);
vector<int> dMin; vector<int> dMin;
vector<int> dMax; vector<int> dMax;
@ -291,14 +291,15 @@ void calcDepths(const NGHolder &g, std::vector<NFAVertexDepth> &depths) {
DEBUG_PRINTF("doing startds\n"); DEBUG_PRINTF("doing startds\n");
calcAndStoreDepth(g, g.startDs, deadNodes, dMin, dMax, depths, calcAndStoreDepth(g, g.startDs, deadNodes, dMin, dMax, depths,
&NFAVertexDepth::fromStartDotStar); &NFAVertexDepth::fromStartDotStar);
return depths;
} }
void calcDepths(const NGHolder &g, std::vector<NFAVertexRevDepth> &depths) { vector<NFAVertexRevDepth> calcRevDepths(const NGHolder &g) {
assert(hasCorrectlyNumberedVertices(g)); assert(hasCorrectlyNumberedVertices(g));
const size_t numVertices = num_vertices(g); const size_t numVertices = num_vertices(g);
depths.clear();
depths.resize(numVertices);
vector<NFAVertexRevDepth> depths(numVertices);
vector<int> dMin; vector<int> dMin;
vector<int> dMax; vector<int> dMax;
@ -324,14 +325,15 @@ void calcDepths(const NGHolder &g, std::vector<NFAVertexRevDepth> &depths) {
calcAndStoreDepth<RevNFAGraph, NFAVertexRevDepth>( calcAndStoreDepth<RevNFAGraph, NFAVertexRevDepth>(
rg, g.acceptEod, deadNodes, dMin, dMax, depths, rg, g.acceptEod, deadNodes, dMin, dMax, depths,
&NFAVertexRevDepth::toAcceptEod); &NFAVertexRevDepth::toAcceptEod);
return depths;
} }
void calcDepths(const NGHolder &g, vector<NFAVertexBidiDepth> &depths) { vector<NFAVertexBidiDepth> calcBidiDepths(const NGHolder &g) {
assert(hasCorrectlyNumberedVertices(g)); assert(hasCorrectlyNumberedVertices(g));
const size_t numVertices = num_vertices(g); const size_t numVertices = num_vertices(g);
depths.clear();
depths.resize(numVertices);
vector<NFAVertexBidiDepth> depths(numVertices);
vector<int> dMin; vector<int> dMin;
vector<int> dMax; vector<int> dMax;
@ -366,10 +368,11 @@ void calcDepths(const NGHolder &g, vector<NFAVertexBidiDepth> &depths) {
calcAndStoreDepth<RevNFAGraph, NFAVertexBidiDepth>( calcAndStoreDepth<RevNFAGraph, NFAVertexBidiDepth>(
rg, g.acceptEod, deadNodes, dMin, dMax, depths, rg, g.acceptEod, deadNodes, dMin, dMax, depths,
&NFAVertexBidiDepth::toAcceptEod); &NFAVertexBidiDepth::toAcceptEod);
return depths;
} }
void calcDepthsFrom(const NGHolder &g, const NFAVertex src, vector<DepthMinMax> calcDepthsFrom(const NGHolder &g, const NFAVertex src) {
vector<DepthMinMax> &depths) {
assert(hasCorrectlyNumberedVertices(g)); assert(hasCorrectlyNumberedVertices(g));
const size_t numVertices = num_vertices(g); const size_t numVertices = num_vertices(g);
@ -379,13 +382,14 @@ void calcDepthsFrom(const NGHolder &g, const NFAVertex src,
vector<int> dMin, dMax; vector<int> dMin, dMax;
calcDepthFromSource(g, src, deadNodes, dMin, dMax); calcDepthFromSource(g, src, deadNodes, dMin, dMax);
depths.clear(); vector<DepthMinMax> depths(numVertices);
depths.resize(numVertices);
for (auto v : vertices_range(g)) { for (auto v : vertices_range(g)) {
u32 idx = g[v].index; auto idx = g[v].index;
depths.at(idx) = getDepths(idx, dMin, dMax); depths.at(idx) = getDepths(idx, dMin, dMax);
} }
return depths;
} }
} // namespace ue2 } // namespace ue2

View 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 * Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met: * modification, are permitted provided that the following conditions are met:
@ -26,23 +26,22 @@
* POSSIBILITY OF SUCH DAMAGE. * POSSIBILITY OF SUCH DAMAGE.
*/ */
/** \file /**
* \file
* \brief NFA graph vertex depth calculations. * \brief NFA graph vertex depth calculations.
*/ */
#ifndef STRUCTURAL_ANALYSIS_H #ifndef NG_DEPTH_H
#define STRUCTURAL_ANALYSIS_H #define NG_DEPTH_H
#include "nfagraph/ng_holder.h"
#include "ue2common.h" #include "ue2common.h"
#include "nfagraph/ng_holder.h"
#include "util/depth.h" #include "util/depth.h"
#include <vector> #include <vector>
namespace ue2 { namespace ue2 {
class NGHolder;
/** /**
* \brief Encapsulates min/max depths relative to the start and startDs * \brief Encapsulates min/max depths relative to the start and startDs
* vertices. * vertices.
@ -72,28 +71,29 @@ struct NFAVertexBidiDepth {
}; };
/** /**
* \brief Calculate depths from start and startDs. * \brief Calculate depths from start and startDs. Returns them in a vector,
* Fills the vector \p depths (indexed by \p vertex_index). * indexed by vertex index.
*/ */
void calcDepths(const NGHolder &g, std::vector<NFAVertexDepth> &depths); std::vector<NFAVertexDepth> calcDepths(const NGHolder &g);
/** /**
* \brief Calculate depths to accept and acceptEod. * \brief Calculate depths to accept and acceptEod. Returns them in a vector,
* Fills the vector \p depths (indexed by \p vertex_index). * indexed by vertex index.
*/ */
void calcDepths(const NGHolder &g, std::vector<NFAVertexRevDepth> &depths); std::vector<NFAVertexRevDepth> calcRevDepths(const NGHolder &g);
/** /**
* \brief Calculate depths to/from all special vertices. * \brief Calculate depths to/from all special vertices. Returns them in a
* Fills the vector \p depths (indexed by \p vertex_index). * vector, indexed by vertex index.
*/ */
void calcDepths(const NGHolder &g, std::vector<NFAVertexBidiDepth> &depths); std::vector<NFAVertexBidiDepth> 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. */ * \brief Calculate the (min, max) depths from the given \p src to every vertex
void calcDepthsFrom(const NGHolder &g, const NFAVertex src, * in the graph and return them in a vector, indexed by \p vertex_index.
std::vector<DepthMinMax> &depths); */
std::vector<DepthMinMax> calcDepthsFrom(const NGHolder &g, const NFAVertex src);
} // namespace ue2 } // namespace ue2
#endif #endif // NG_DEPTH_H

View File

@ -342,9 +342,9 @@ vector<VertexInfoSet> partitionGraph(vector<unique_ptr<VertexInfo>> &infos,
vector<NFAVertexRevDepth> rdepths; vector<NFAVertexRevDepth> rdepths;
if (eq == LEFT_EQUIVALENCE) { if (eq == LEFT_EQUIVALENCE) {
calcDepths(g, depths); depths = calcDepths(g);
} else { } else {
calcDepths(g, rdepths); rdepths = calcRevDepths(g);
} }
// partition the graph based on CharReach // partition the graph based on CharReach

View File

@ -147,8 +147,7 @@ void fillExpressionInfo(ReportManager &rm, NGHolder &g,
removeLeadingVirtualVerticesFromRoot(g, g.start); removeLeadingVirtualVerticesFromRoot(g, g.start);
removeLeadingVirtualVerticesFromRoot(g, g.startDs); removeLeadingVirtualVerticesFromRoot(g, g.startDs);
vector<DepthMinMax> depths; auto depths = calcDepthsFrom(g, g.start);
calcDepthsFrom(g, g.start, depths);
DepthMinMax d; DepthMinMax d;

View File

@ -734,8 +734,7 @@ void pruneExtUnreachable(NGHolder &g, const ReportManager &rm) {
const auto &report = rm.getReport(*reports.begin()); const auto &report = rm.getReport(*reports.begin());
vector<NFAVertexBidiDepth> depths; auto depths = calcBidiDepths(g);
calcDepths(g, depths);
vector<NFAEdge> dead; vector<NFAEdge> dead;
@ -957,8 +956,7 @@ void replaceMinLengthWithOffset(NGHolder &g, ReportManager &rm) {
*/ */
static static
void removeUnneededOffsetBounds(NGHolder &g, ReportManager &rm) { void removeUnneededOffsetBounds(NGHolder &g, ReportManager &rm) {
vector<NFAVertexDepth> depths; auto depths = calcDepths(g);
calcDepths(g, depths);
replaceReports(g, [&](NFAVertex v, ReportID id) { replaceReports(g, [&](NFAVertex v, ReportID id) {
const auto &d = depths.at(g[v].index); const auto &d = depths.at(g[v].index);

View File

@ -603,9 +603,7 @@ private:
// check if we will edit our way into a vacuous pattern // check if we will edit our way into a vacuous pattern
static static
bool will_turn_vacuous(const NGHolder &g, u32 edit_distance) { bool will_turn_vacuous(const NGHolder &g, u32 edit_distance) {
vector<NFAVertexRevDepth> depths; auto depths = calcRevDepths(g);
calcDepths(g, depths);
depth min_depth = depth::infinity(); depth min_depth = depth::infinity();
auto idx = g[g.start].index; auto idx = g[g.start].index;

View 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 * Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met: * modification, are permitted provided that the following conditions are met:
@ -94,8 +94,7 @@ void wireNewAccepts(NGHolder &g, NFAVertex head,
static static
bool isFixedDepth(const NGHolder &g, NFAVertex v) { bool isFixedDepth(const NGHolder &g, NFAVertex v) {
// If the vertex is reachable from startDs, it can't be fixed depth. // If the vertex is reachable from startDs, it can't be fixed depth.
vector<DepthMinMax> depthFromStartDs; auto depthFromStartDs = calcDepthsFrom(g, g.startDs);
calcDepthsFrom(g, g.startDs, depthFromStartDs);
u32 idx = g[v].index; u32 idx = g[v].index;
const DepthMinMax &ds = depthFromStartDs.at(idx); const DepthMinMax &ds = depthFromStartDs.at(idx);
@ -104,8 +103,7 @@ bool isFixedDepth(const NGHolder &g, NFAVertex v) {
return false; return false;
} }
vector<DepthMinMax> depthFromStart; auto depthFromStart = calcDepthsFrom(g, g.start);
calcDepthsFrom(g, g.start, depthFromStart);
/* we can still consider the head of a puff chain as at fixed depth if /* 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 * it has a self-loop: so we look at all the preds of v (other than v

View File

@ -118,13 +118,12 @@ struct ReachSubgraph {
static static
void findInitDepths(const NGHolder &g, void findInitDepths(const NGHolder &g,
ue2::unordered_map<NFAVertex, NFAVertexDepth> &depths) { ue2::unordered_map<NFAVertex, NFAVertexDepth> &depths) {
vector<NFAVertexDepth> d; auto d = calcDepths(g);
calcDepths(g, d);
for (auto v : vertices_range(g)) { for (auto v : vertices_range(g)) {
u32 idx = g[v].index; size_t idx = g[v].index;
assert(idx < d.size()); assert(idx < d.size());
depths.insert(make_pair(v, d[idx])); depths.emplace(v, d[idx]);
} }
} }

View File

@ -78,8 +78,8 @@ vector<DepthMinMax> getDistancesFromSOM(const NGHolder &g_orig) {
//dumpGraph("som_depth.dot", g); //dumpGraph("som_depth.dot", g);
vector<DepthMinMax> temp_depths; // numbered by vertex index in g // Find depths, indexed by vertex index in g
calcDepthsFrom(g, g.start, temp_depths); auto temp_depths = calcDepthsFrom(g, g.start);
// Transfer depths, indexed by vertex index in g_orig. // Transfer depths, indexed by vertex index in g_orig.
vector<DepthMinMax> depths(num_vertices(g_orig)); vector<DepthMinMax> depths(num_vertices(g_orig));

View File

@ -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 * Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met: * modification, are permitted provided that the following conditions are met:
@ -60,10 +60,9 @@ namespace {
/** Depths from start, startDs for this graph. */ /** Depths from start, startDs for this graph. */
struct InitDepths { struct InitDepths {
explicit InitDepths(const NGHolder &g) { explicit InitDepths(const NGHolder &g)
calcDepthsFrom(g, g.start, start); : start(calcDepthsFrom(g, g.start)),
calcDepthsFrom(g, g.startDs, startDs); startDs(calcDepthsFrom(g, g.startDs)) {}
}
depth maxDist(const NGHolder &g, NFAVertex v) const { depth maxDist(const NGHolder &g, NFAVertex v) const {
u32 idx = g[v].index; u32 idx = g[v].index;

View File

@ -1472,8 +1472,7 @@ void avoidOutfixes(RoseInGraph &vg, bool last_chance,
if (last_chance) { if (last_chance) {
/* look for a prefix split as it allows us to accept very weak anchored /* look for a prefix split as it allows us to accept very weak anchored
* literals. */ * literals. */
vector<NFAVertexDepth> depths; auto depths = calcDepths(h);
calcDepths(h, depths);
split = findBestPrefixSplit(h, depths, vg, {e}, last_chance, cc); split = findBestPrefixSplit(h, depths, vg, {e}, last_chance, cc);
@ -1973,8 +1972,7 @@ bool improvePrefix(NGHolder &h, RoseInGraph &vg, const vector<RoseInEdge> &ee,
renumber_vertices(h); renumber_vertices(h);
renumber_edges(h); renumber_edges(h);
vector<NFAVertexDepth> depths; auto depths = calcDepths(h);
calcDepths(h, depths);
/* If the reason the prefix is not transient is due to a very long literal /* 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 * following, we can make it transient by restricting ourselves to using
@ -2856,8 +2854,7 @@ bool splitForImplementabilty(RoseInGraph &vg, NGHolder &h,
unique_ptr<VertLitInfo> split; unique_ptr<VertLitInfo> split;
bool last_chance = true; bool last_chance = true;
if (h.kind == NFA_PREFIX) { if (h.kind == NFA_PREFIX) {
vector<NFAVertexDepth> depths; auto depths = calcDepths(h);
calcDepths(h, depths);
split = findBestPrefixSplit(h, depths, vg, edges, last_chance, cc); split = findBestPrefixSplit(h, depths, vg, edges, last_chance, cc);
} else { } else {

View File

@ -1928,8 +1928,7 @@ void removeAddedLiterals(RoseBuildImpl &tbi, const flat_set<u32> &lit_ids) {
} }
bool RoseBuildImpl::addAnchoredAcyclic(const NGHolder &h) { bool RoseBuildImpl::addAnchoredAcyclic(const NGHolder &h) {
vector<DepthMinMax> vertexDepths; auto vertexDepths = calcDepthsFrom(h, h.start);
calcDepthsFrom(h, h.start, vertexDepths);
map<NFAVertex, set<u32> > reportMap; /* NFAVertex -> literal ids */ map<NFAVertex, set<u32> > reportMap; /* NFAVertex -> literal ids */
map<u32, DepthMinMax> depthMap; /* literal id -> min/max depth */ map<u32, DepthMinMax> depthMap; /* literal id -> min/max depth */

View File

@ -198,8 +198,7 @@ static
bool pruneOverlong(NGHolder &g, const depth &max_depth, bool pruneOverlong(NGHolder &g, const depth &max_depth,
const ReportManager &rm) { const ReportManager &rm) {
bool modified = false; bool modified = false;
std::vector<NFAVertexDepth> depths; auto depths = calcDepths(g);
calcDepths(g, depths);
for (auto v : vertices_range(g)) { for (auto v : vertices_range(g)) {
if (is_special(v, g)) { if (is_special(v, g)) {