ng_calc_components: add Grey box control

This commit is contained in:
Justin Viiret 2017-03-21 10:58:26 +11:00 committed by Matthew Barr
parent ba867ebaff
commit 560e522457
6 changed files with 28 additions and 11 deletions

View File

@ -42,6 +42,7 @@ namespace ue2 {
Grey::Grey(void) : Grey::Grey(void) :
optimiseComponentTree(true), optimiseComponentTree(true),
calcComponents(true),
performGraphSimplification(true), performGraphSimplification(true),
prefilterReductions(true), prefilterReductions(true),
removeEdgeRedundancy(true), removeEdgeRedundancy(true),
@ -209,6 +210,7 @@ void applyGreyOverrides(Grey *g, const string &s) {
} while (0) } while (0)
G_UPDATE(optimiseComponentTree); G_UPDATE(optimiseComponentTree);
G_UPDATE(calcComponents);
G_UPDATE(performGraphSimplification); G_UPDATE(performGraphSimplification);
G_UPDATE(prefilterReductions); G_UPDATE(prefilterReductions);
G_UPDATE(removeEdgeRedundancy); G_UPDATE(removeEdgeRedundancy);

View File

@ -41,6 +41,7 @@ struct Grey {
bool optimiseComponentTree; bool optimiseComponentTree;
bool calcComponents;
bool performGraphSimplification; bool performGraphSimplification;
bool prefilterReductions; bool prefilterReductions;
bool removeEdgeRedundancy; bool removeEdgeRedundancy;

View File

@ -437,7 +437,7 @@ bool NG::addGraph(ExpressionInfo &expr, unique_ptr<NGHolder> g_ptr) {
// Split the graph into a set of connected components and process those. // Split the graph into a set of connected components and process those.
// Note: this invalidates g_ptr. // 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()); assert(!g_comp.empty());
if (!som) { if (!som) {
@ -446,7 +446,7 @@ bool NG::addGraph(ExpressionInfo &expr, unique_ptr<NGHolder> g_ptr) {
reformLeadingDots(*gc); reformLeadingDots(*gc);
} }
recalcComponents(g_comp); recalcComponents(g_comp, cc.grey);
} }
if (processComponents(*this, expr, g_comp, som)) { if (processComponents(*this, expr, g_comp, som)) {

View File

@ -55,6 +55,7 @@
#include "ng_prune.h" #include "ng_prune.h"
#include "ng_undirected.h" #include "ng_undirected.h"
#include "ng_util.h" #include "ng_util.h"
#include "grey.h"
#include "ue2common.h" #include "ue2common.h"
#include "util/graph_range.h" #include "util/graph_range.h"
#include "util/make_unique.h" #include "util/make_unique.h"
@ -376,12 +377,13 @@ void splitIntoComponents(unique_ptr<NGHolder> g,
})); }));
} }
deque<unique_ptr<NGHolder>> calcComponents(unique_ptr<NGHolder> g) { deque<unique_ptr<NGHolder>> calcComponents(unique_ptr<NGHolder> g,
const Grey &grey) {
deque<unique_ptr<NGHolder>> comps; deque<unique_ptr<NGHolder>> comps;
// For trivial cases, we needn't bother running the full // For trivial cases, we needn't bother running the full
// connected_components algorithm. // connected_components algorithm.
if (isAlternationOfClasses(*g)) { if (!grey.calcComponents || isAlternationOfClasses(*g)) {
comps.push_back(std::move(g)); comps.push_back(std::move(g));
return comps; return comps;
} }
@ -402,7 +404,11 @@ deque<unique_ptr<NGHolder>> calcComponents(unique_ptr<NGHolder> g) {
return comps; return comps;
} }
void recalcComponents(deque<unique_ptr<NGHolder>> &comps) { void recalcComponents(deque<unique_ptr<NGHolder>> &comps, const Grey &grey) {
if (!grey.calcComponents) {
return;
}
deque<unique_ptr<NGHolder>> out; deque<unique_ptr<NGHolder>> out;
for (auto &gc : comps) { for (auto &gc : comps) {
@ -415,7 +421,7 @@ void recalcComponents(deque<unique_ptr<NGHolder>> &comps) {
continue; 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)), out.insert(end(out), std::make_move_iterator(begin(gc_comps)),
std::make_move_iterator(end(gc_comps))); std::make_move_iterator(end(gc_comps)));
} }

View File

@ -39,13 +39,15 @@
namespace ue2 { namespace ue2 {
class NGHolder; class NGHolder;
struct Grey;
bool isAlternationOfClasses(const NGHolder &g); bool isAlternationOfClasses(const NGHolder &g);
std::deque<std::unique_ptr<NGHolder>> std::deque<std::unique_ptr<NGHolder>>
calcComponents(std::unique_ptr<NGHolder> g); calcComponents(std::unique_ptr<NGHolder> g, const Grey &grey);
void recalcComponents(std::deque<std::unique_ptr<NGHolder>> &comps); void recalcComponents(std::deque<std::unique_ptr<NGHolder>> &comps,
const Grey &grey);
} // namespace ue2 } // namespace ue2

View File

@ -43,7 +43,9 @@ TEST(NFAGraph, CalcComp1) {
auto graph = constructGraph("abc|def|ghi", 0); auto graph = constructGraph("abc|def|ghi", 0);
ASSERT_TRUE(graph != nullptr); 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()); 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); auto graph = constructGraph("a|b|c|d|e|f|g|h|i", 0);
ASSERT_TRUE(graph != nullptr); 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. // We should be identifying this as a trivial case and not splitting it.
ASSERT_EQ(1, comps.size()); ASSERT_EQ(1, comps.size());
@ -62,7 +66,9 @@ TEST(NFAGraph, RecalcComp1) {
comps.push_back(constructGraph("abc|def|ghi", 0)); comps.push_back(constructGraph("abc|def|ghi", 0));
ASSERT_TRUE(comps.back() != nullptr); ASSERT_TRUE(comps.back() != nullptr);
recalcComponents(comps); Grey grey;
grey.calcComponents = true;
recalcComponents(comps, grey);
ASSERT_EQ(3, comps.size()); ASSERT_EQ(3, comps.size());
} }