From 560e522457ecb643acef5a55a982e10b365ba620 Mon Sep 17 00:00:00 2001 From: Justin Viiret Date: Tue, 21 Mar 2017 10:58:26 +1100 Subject: [PATCH] ng_calc_components: add Grey box control --- src/grey.cpp | 2 ++ src/grey.h | 1 + src/nfagraph/ng.cpp | 4 ++-- src/nfagraph/ng_calc_components.cpp | 14 ++++++++++---- src/nfagraph/ng_calc_components.h | 6 ++++-- unit/internal/nfagraph_comp.cpp | 12 +++++++++--- 6 files changed, 28 insertions(+), 11 deletions(-) diff --git a/src/grey.cpp b/src/grey.cpp index ea92fdb5..24140c05 100644 --- a/src/grey.cpp +++ b/src/grey.cpp @@ -42,6 +42,7 @@ namespace ue2 { Grey::Grey(void) : optimiseComponentTree(true), + calcComponents(true), performGraphSimplification(true), prefilterReductions(true), removeEdgeRedundancy(true), @@ -209,6 +210,7 @@ void applyGreyOverrides(Grey *g, const string &s) { } while (0) G_UPDATE(optimiseComponentTree); + G_UPDATE(calcComponents); G_UPDATE(performGraphSimplification); G_UPDATE(prefilterReductions); G_UPDATE(removeEdgeRedundancy); diff --git a/src/grey.h b/src/grey.h index 5fde7b4b..50519418 100644 --- a/src/grey.h +++ b/src/grey.h @@ -41,6 +41,7 @@ struct Grey { bool optimiseComponentTree; + bool calcComponents; bool performGraphSimplification; bool prefilterReductions; bool removeEdgeRedundancy; diff --git a/src/nfagraph/ng.cpp b/src/nfagraph/ng.cpp index bdd767e9..dc74dcee 100644 --- a/src/nfagraph/ng.cpp +++ b/src/nfagraph/ng.cpp @@ -437,7 +437,7 @@ bool NG::addGraph(ExpressionInfo &expr, unique_ptr g_ptr) { // Split the graph into a set of connected components and process those. // Note: this invalidates g_ptr. - auto g_comp = calcComponents(std::move(g_ptr)); + auto g_comp = calcComponents(std::move(g_ptr), cc.grey); assert(!g_comp.empty()); if (!som) { @@ -446,7 +446,7 @@ bool NG::addGraph(ExpressionInfo &expr, unique_ptr g_ptr) { reformLeadingDots(*gc); } - recalcComponents(g_comp); + recalcComponents(g_comp, cc.grey); } if (processComponents(*this, expr, g_comp, som)) { diff --git a/src/nfagraph/ng_calc_components.cpp b/src/nfagraph/ng_calc_components.cpp index ff0d0fe1..2c1dbcdb 100644 --- a/src/nfagraph/ng_calc_components.cpp +++ b/src/nfagraph/ng_calc_components.cpp @@ -55,6 +55,7 @@ #include "ng_prune.h" #include "ng_undirected.h" #include "ng_util.h" +#include "grey.h" #include "ue2common.h" #include "util/graph_range.h" #include "util/make_unique.h" @@ -376,12 +377,13 @@ void splitIntoComponents(unique_ptr g, })); } -deque> calcComponents(unique_ptr g) { +deque> calcComponents(unique_ptr g, + const Grey &grey) { deque> comps; // For trivial cases, we needn't bother running the full // connected_components algorithm. - if (isAlternationOfClasses(*g)) { + if (!grey.calcComponents || isAlternationOfClasses(*g)) { comps.push_back(std::move(g)); return comps; } @@ -402,7 +404,11 @@ deque> calcComponents(unique_ptr g) { return comps; } -void recalcComponents(deque> &comps) { +void recalcComponents(deque> &comps, const Grey &grey) { + if (!grey.calcComponents) { + return; + } + deque> out; for (auto &gc : comps) { @@ -415,7 +421,7 @@ void recalcComponents(deque> &comps) { continue; } - auto gc_comps = calcComponents(std::move(gc)); + auto gc_comps = calcComponents(std::move(gc), grey); out.insert(end(out), std::make_move_iterator(begin(gc_comps)), std::make_move_iterator(end(gc_comps))); } diff --git a/src/nfagraph/ng_calc_components.h b/src/nfagraph/ng_calc_components.h index 0359f4a0..1bcdc5f8 100644 --- a/src/nfagraph/ng_calc_components.h +++ b/src/nfagraph/ng_calc_components.h @@ -39,13 +39,15 @@ namespace ue2 { class NGHolder; +struct Grey; bool isAlternationOfClasses(const NGHolder &g); std::deque> -calcComponents(std::unique_ptr g); +calcComponents(std::unique_ptr g, const Grey &grey); -void recalcComponents(std::deque> &comps); +void recalcComponents(std::deque> &comps, + const Grey &grey); } // namespace ue2 diff --git a/unit/internal/nfagraph_comp.cpp b/unit/internal/nfagraph_comp.cpp index 8aae9519..61b05a46 100644 --- a/unit/internal/nfagraph_comp.cpp +++ b/unit/internal/nfagraph_comp.cpp @@ -43,7 +43,9 @@ TEST(NFAGraph, CalcComp1) { auto graph = constructGraph("abc|def|ghi", 0); ASSERT_TRUE(graph != nullptr); - auto comps = calcComponents(std::move(graph)); + Grey grey; + grey.calcComponents = true; + auto comps = calcComponents(std::move(graph), grey); ASSERT_EQ(3, comps.size()); } @@ -51,7 +53,9 @@ TEST(NFAGraph, CalcComp2) { auto graph = constructGraph("a|b|c|d|e|f|g|h|i", 0); ASSERT_TRUE(graph != nullptr); - auto comps = calcComponents(std::move(graph)); + Grey grey; + grey.calcComponents = true; + auto comps = calcComponents(std::move(graph), grey); // We should be identifying this as a trivial case and not splitting it. ASSERT_EQ(1, comps.size()); @@ -62,7 +66,9 @@ TEST(NFAGraph, RecalcComp1) { comps.push_back(constructGraph("abc|def|ghi", 0)); ASSERT_TRUE(comps.back() != nullptr); - recalcComponents(comps); + Grey grey; + grey.calcComponents = true; + recalcComponents(comps, grey); ASSERT_EQ(3, comps.size()); }