Introduce custom adjacency-list based graph

This commit is contained in:
Alex Coyte
2016-08-24 16:12:51 +10:00
committed by Matthew Barr
parent 05683655cb
commit e1e9010cac
92 changed files with 3730 additions and 1812 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -84,7 +84,7 @@ TEST(NFAGraph, RemoveEquivalence1) {
ASSERT_TRUE(tmpcr.test('a'));
}
// check if we found our vertex
ASSERT_TRUE(a != nullptr);
ASSERT_TRUE(a != NGHolder::null_vertex());
// There should be two edges from v to nodes with reachability 'b' and 'c'
NFAVertex b = NGHolder::null_vertex();
@@ -101,8 +101,8 @@ TEST(NFAGraph, RemoveEquivalence1) {
}
}
// check if we found our vertices
ASSERT_TRUE(b != nullptr);
ASSERT_TRUE(c != nullptr);
ASSERT_TRUE(b != NGHolder::null_vertex());
ASSERT_TRUE(c != NGHolder::null_vertex());
// both vertices should have an edge to accept
ASSERT_TRUE(edge(b, g.accept, g).second);
@@ -145,7 +145,7 @@ TEST(NFAGraph, RemoveEquivalence2) {
ASSERT_TRUE(tmpcr.test('a'));
}
// check if we found our vertex
ASSERT_TRUE(a != nullptr);
ASSERT_TRUE(a != NGHolder::null_vertex());
// There should be two edges from v to nodes with reachability 'b' and 'c'
NFAVertex b = NGHolder::null_vertex();
@@ -162,8 +162,8 @@ TEST(NFAGraph, RemoveEquivalence2) {
}
}
// check if we found our vertices
ASSERT_TRUE(b != nullptr);
ASSERT_TRUE(c != nullptr);
ASSERT_TRUE(b != NGHolder::null_vertex());
ASSERT_TRUE(c != NGHolder::null_vertex());
// both new vertices should have edges from startDs
ASSERT_TRUE(edge(g.startDs, b, g).second);
@@ -207,7 +207,7 @@ TEST(NFAGraph, RemoveEquivalence3) {
ASSERT_TRUE(tmpcr.test('a'));
}
// check if we found our 'a'
ASSERT_TRUE(a != nullptr);
ASSERT_TRUE(a != NGHolder::null_vertex());
// There should be an edge from 'a' to '.'
ASSERT_EQ(1U, out_degree(a, g));
@@ -234,7 +234,6 @@ TEST(NFAGraph, RemoveEquivalence3) {
NFAVertex X = NGHolder::null_vertex();
NFAVertex Y = NGHolder::null_vertex();
for (NFAVertex tmp : adjacent_vertices_range(dot2, g)) {
// we already know about dot1, so skip it
if (tmp == dot1) {
continue;
@@ -251,8 +250,8 @@ TEST(NFAGraph, RemoveEquivalence3) {
}
}
// check if we found both vertices
ASSERT_TRUE(X != nullptr);
ASSERT_TRUE(Y != nullptr);
ASSERT_TRUE(X != NGHolder::null_vertex());
ASSERT_TRUE(Y != NGHolder::null_vertex());
// finally, check if these two vertices only have edges to accept
ASSERT_EQ(1U, out_degree(X, g));
@@ -306,8 +305,8 @@ TEST(NFAGraph, RemoveEquivalence4) {
}
}
// check if we found both vertices
ASSERT_TRUE(X != nullptr);
ASSERT_TRUE(Y != nullptr);
ASSERT_TRUE(X != NGHolder::null_vertex());
ASSERT_TRUE(Y != NGHolder::null_vertex());
// now, find first dot from X
ASSERT_EQ(1U, out_degree(X, g));
@@ -351,7 +350,7 @@ TEST(NFAGraph, RemoveEquivalence4) {
}
}
// make sure we found our 'a'
ASSERT_TRUE(a != nullptr);
ASSERT_TRUE(a != NGHolder::null_vertex());
// now, check if 'a' has an edge to accept
ASSERT_EQ(1U, out_degree(a, g));
@@ -396,7 +395,7 @@ TEST(NFAGraph, RemoveEquivalence5) {
ASSERT_TRUE(edge(v, v, g).second);
}
// check if we found our vertex
ASSERT_TRUE(v != nullptr);
ASSERT_TRUE(v != NGHolder::null_vertex());
// now, find the vertex leading to accept
NFAVertex v2 = NGHolder::null_vertex();
@@ -414,7 +413,7 @@ TEST(NFAGraph, RemoveEquivalence5) {
ASSERT_TRUE(edge(tmp, g.accept, g).second);
}
// check if we found our vertex
ASSERT_TRUE(v2 != nullptr);
ASSERT_TRUE(v2 != NGHolder::null_vertex());
}
// catching UE-2692
@@ -452,7 +451,7 @@ TEST(NFAGraph, RemoveEquivalence6) {
ASSERT_TRUE(edge(v, g.accept, g).second);
}
// check if we found our vertex
ASSERT_TRUE(v != nullptr);
ASSERT_TRUE(v != NGHolder::null_vertex());
}
// catching UE-2692
@@ -492,7 +491,7 @@ TEST(NFAGraph, RemoveEquivalence7) {
ASSERT_EQ(1U, proper_out_degree(v, g));
}
// check if we found our vertex
ASSERT_TRUE(v != nullptr);
ASSERT_TRUE(v != NGHolder::null_vertex());
// find the next vertex and ensure it has an edge to accept
NFAVertex v2 = NGHolder::null_vertex();
@@ -511,7 +510,7 @@ TEST(NFAGraph, RemoveEquivalence7) {
ASSERT_TRUE(edge(v2, g.accept, g).second);
}
// check if we found our vertex
ASSERT_TRUE(v2 != nullptr);
ASSERT_TRUE(v2 != NGHolder::null_vertex());
}
TEST(NFAGraph, RemoveEquivalence_Reports1) {

View File

@@ -55,13 +55,13 @@ TEST(NFAGraph, RemoveRedundancy1) {
unique_ptr<NGWrapper> graph(constructGraphWithCC("(a|b)c", cc, 0));
ASSERT_TRUE(graph.get() != nullptr);
NGHolder &g = *graph;
// Run removeRedundancy
removeRedundancy(*graph, SOM_NONE);
NFAGraph &g = graph->g;
removeRedundancy(g, SOM_NONE);
// Our graph should only have two non-special nodes
ASSERT_EQ((size_t)N_SPECIALS + 2, num_vertices(*graph));
ASSERT_EQ((size_t)N_SPECIALS + 2, num_vertices(g));
// Dot-star start state should be connected to itself and a single other
// vertex
@@ -98,13 +98,13 @@ TEST(NFAGraph, RemoveRedundancy2) {
unique_ptr<NGWrapper> graph(constructGraphWithCC("a.*b?c", cc,
HS_FLAG_DOTALL));
ASSERT_TRUE(graph.get() != nullptr);
NGHolder &g = *graph;
// Run removeRedundancy
removeRedundancy(*graph, SOM_NONE);
NFAGraph &g = graph->g;
removeRedundancy(g, SOM_NONE);
// Our graph should now have only 3 non-special vertices
ASSERT_EQ((size_t)N_SPECIALS + 3, num_vertices(*graph));
ASSERT_EQ((size_t)N_SPECIALS + 3, num_vertices(g));
// Dot-star start state should be connected to itself and a single other
// vertex
@@ -156,12 +156,12 @@ TEST(NFAGraph, RemoveRedundancy3) {
cc, 0));
ASSERT_TRUE(graph.get() != nullptr);
unsigned countBefore = num_vertices(graph->g);
unsigned countBefore = num_vertices(*graph);
removeRedundancy(*graph, SOM_NONE);
// The '(a|b)?' construction (two states) should have disappeared, leaving
// this expr as 'foobar.*teakettle'
ASSERT_EQ(countBefore - 2, num_vertices(graph->g));
ASSERT_EQ(countBefore - 2, num_vertices(*graph));
}
TEST(NFAGraph, RemoveRedundancy4) {
@@ -169,11 +169,11 @@ TEST(NFAGraph, RemoveRedundancy4) {
unique_ptr<NGWrapper> graph(constructGraphWithCC("foo([A-Z]|a|b|q)", cc, 0));
ASSERT_TRUE(graph.get() != nullptr);
unsigned countBefore = num_vertices(graph->g);
unsigned countBefore = num_vertices(*graph);
removeRedundancy(*graph, SOM_NONE);
// We should end up with the alternation collapsing into one state
ASSERT_EQ(countBefore - 3, num_vertices(graph->g));
ASSERT_EQ(countBefore - 3, num_vertices(*graph));
}
TEST(NFAGraph, RemoveRedundancy5) {
@@ -182,12 +182,12 @@ TEST(NFAGraph, RemoveRedundancy5) {
cc, 0));
ASSERT_TRUE(graph.get() != nullptr);
unsigned countBefore = num_vertices(graph->g);
unsigned countBefore = num_vertices(*graph);
removeRedundancy(*graph, SOM_NONE);
// Since we don't return a start offset, the first state ('[0-9]?') is
// redundant.
ASSERT_EQ(countBefore - 1, num_vertices(graph->g));
ASSERT_EQ(countBefore - 1, num_vertices(*graph));
}
TEST(NFAGraph, RemoveEdgeRedundancy1) {
@@ -196,12 +196,12 @@ TEST(NFAGraph, RemoveEdgeRedundancy1) {
auto graph = constructGraphWithCC("A+hatstand", cc, HS_FLAG_DOTALL);
ASSERT_TRUE(graph.get() != nullptr);
unsigned countBefore = num_edges(graph->g);
unsigned countBefore = num_edges(*graph);
removeEdgeRedundancy(*graph, SOM_NONE, cc);
// One edge (the self-loop on the leading A+) should have been removed.
ASSERT_EQ(countBefore - 1, num_edges(graph->g));
ASSERT_EQ(countBefore - 1, num_edges(*graph));
}
TEST(NFAGraph, RemoveEdgeRedundancy2) {
@@ -210,12 +210,12 @@ TEST(NFAGraph, RemoveEdgeRedundancy2) {
auto graph = constructGraphWithCC("foo.*A*bar", cc, HS_FLAG_DOTALL);
ASSERT_TRUE(graph.get() != nullptr);
size_t numEdgesBefore = num_edges(graph->g);
size_t numVertsBefore = num_vertices(graph->g);
size_t numEdgesBefore = num_edges(*graph);
size_t numVertsBefore = num_vertices(*graph);
removeEdgeRedundancy(*graph, SOM_NONE, cc);
// The .* should swallow up the A* and its self-loop.
ASSERT_EQ(numEdgesBefore - 4, num_edges(graph->g));
ASSERT_EQ(numVertsBefore - 1, num_vertices(graph->g));
ASSERT_EQ(numEdgesBefore - 4, num_edges(*graph));
ASSERT_EQ(numVertsBefore - 1, num_vertices(*graph));
}

View File

@@ -64,7 +64,6 @@ RoseVertex addVertex(RoseBuildImpl &build, RoseVertex parent, u32 lit_id) {
RoseGraph &g = build.g;
RoseVertex v = add_vertex(g);
g[v].idx = build.vertexIndex++;
g[v].min_offset = 0;
g[v].max_offset = ROSE_BOUND_INF;
g[v].literals.insert(lit_id);