mirror of
https://github.com/VectorCamp/vectorscan.git
synced 2025-11-20 02:47:11 +03:00
Introduce custom adjacency-list based graph
This commit is contained in:
@@ -36,123 +36,33 @@ using namespace std;
|
||||
namespace ue2 {
|
||||
|
||||
// internal use only
|
||||
static NFAVertex addSpecialVertex(NFAGraph &g, SpecialNodes id) {
|
||||
NFAVertex v = add_vertex(g);
|
||||
static NFAVertex addSpecialVertex(NGHolder &g, SpecialNodes id) {
|
||||
NFAVertex v(add_vertex(g));
|
||||
g[v].index = id;
|
||||
return v;
|
||||
}
|
||||
|
||||
NGHolder::NGHolder(void)
|
||||
: g(),
|
||||
// add initial special nodes
|
||||
start(addSpecialVertex(g, NODE_START)),
|
||||
startDs(addSpecialVertex(g, NODE_START_DOTSTAR)),
|
||||
accept(addSpecialVertex(g, NODE_ACCEPT)),
|
||||
acceptEod(addSpecialVertex(g, NODE_ACCEPT_EOD)),
|
||||
// misc data
|
||||
numVertices(N_SPECIALS),
|
||||
numEdges(0),
|
||||
isValidNumEdges(true),
|
||||
isValidNumVertices(true) {
|
||||
|
||||
// wire up some fake edges for the stylized bits of the NFA
|
||||
add_edge(start, startDs, *this);
|
||||
add_edge(startDs, startDs, *this);
|
||||
add_edge(accept, acceptEod, *this);
|
||||
|
||||
g[start].char_reach.setall();
|
||||
g[startDs].char_reach.setall();
|
||||
}
|
||||
|
||||
NGHolder::NGHolder(nfa_kind k)
|
||||
: kind (k), g(),
|
||||
: kind (k),
|
||||
// add initial special nodes
|
||||
start(addSpecialVertex(g, NODE_START)),
|
||||
startDs(addSpecialVertex(g, NODE_START_DOTSTAR)),
|
||||
accept(addSpecialVertex(g, NODE_ACCEPT)),
|
||||
acceptEod(addSpecialVertex(g, NODE_ACCEPT_EOD)),
|
||||
// misc data
|
||||
numVertices(N_SPECIALS),
|
||||
numEdges(0),
|
||||
isValidNumEdges(true),
|
||||
isValidNumVertices(true) {
|
||||
start(addSpecialVertex(*this, NODE_START)),
|
||||
startDs(addSpecialVertex(*this, NODE_START_DOTSTAR)),
|
||||
accept(addSpecialVertex(*this, NODE_ACCEPT)),
|
||||
acceptEod(addSpecialVertex(*this, NODE_ACCEPT_EOD)) {
|
||||
|
||||
// wire up some fake edges for the stylized bits of the NFA
|
||||
add_edge(start, startDs, *this);
|
||||
add_edge(startDs, startDs, *this);
|
||||
add_edge(accept, acceptEod, *this);
|
||||
|
||||
g[start].char_reach.setall();
|
||||
g[startDs].char_reach.setall();
|
||||
(*this)[start].char_reach.setall();
|
||||
(*this)[startDs].char_reach.setall();
|
||||
}
|
||||
|
||||
NGHolder::~NGHolder(void) {
|
||||
DEBUG_PRINTF("destroying holder @ %p\n", this);
|
||||
}
|
||||
|
||||
size_t num_edges(NGHolder &h) {
|
||||
if (!h.isValidNumEdges) {
|
||||
h.numEdges = num_edges(h.g);
|
||||
h.isValidNumEdges = true;
|
||||
}
|
||||
return h.numEdges;
|
||||
}
|
||||
|
||||
size_t num_edges(const NGHolder &h) {
|
||||
if (!h.isValidNumEdges) {
|
||||
return num_edges(h.g);
|
||||
}
|
||||
return h.numEdges;
|
||||
}
|
||||
|
||||
size_t num_vertices(NGHolder &h) {
|
||||
if (!h.isValidNumVertices) {
|
||||
h.numVertices = num_vertices(h.g);
|
||||
h.isValidNumVertices = true;
|
||||
}
|
||||
return h.numVertices;
|
||||
}
|
||||
|
||||
size_t num_vertices(const NGHolder &h) {
|
||||
if (!h.isValidNumVertices) {
|
||||
return num_vertices(h.g);
|
||||
}
|
||||
return h.numVertices;
|
||||
}
|
||||
|
||||
void remove_edge(const NFAEdge &e, NGHolder &h) {
|
||||
remove_edge(e, h.g);
|
||||
assert(!h.isValidNumEdges || h.numEdges > 0);
|
||||
h.numEdges--;
|
||||
}
|
||||
|
||||
void remove_edge(NFAVertex u, NFAVertex v, NGHolder &h) {
|
||||
remove_edge(u, v, h.g);
|
||||
assert(!h.isValidNumEdges || h.numEdges > 0);
|
||||
h.numEdges--;
|
||||
}
|
||||
|
||||
void remove_vertex(NFAVertex v, NGHolder &h) {
|
||||
remove_vertex(v, h.g);
|
||||
assert(!h.isValidNumVertices || h.numVertices > 0);
|
||||
h.numVertices--;
|
||||
}
|
||||
|
||||
void clear_vertex(NFAVertex v, NGHolder &h) {
|
||||
h.isValidNumEdges = false;
|
||||
clear_vertex_faster(v, h.g);
|
||||
}
|
||||
|
||||
void clear_in_edges(NFAVertex v, NGHolder &h) {
|
||||
h.isValidNumEdges = false;
|
||||
clear_in_edges(v, h.g);
|
||||
}
|
||||
|
||||
void clear_out_edges(NFAVertex v, NGHolder &h) {
|
||||
h.isValidNumEdges = false;
|
||||
clear_out_edges(v, h.g);
|
||||
}
|
||||
|
||||
void clear_graph(NGHolder &h) {
|
||||
NGHolder::vertex_iterator vi, ve;
|
||||
for (tie(vi, ve) = vertices(h); vi != ve;) {
|
||||
@@ -166,6 +76,8 @@ void clear_graph(NGHolder &h) {
|
||||
}
|
||||
|
||||
assert(num_vertices(h) == N_SPECIALS);
|
||||
renumber_vertices(h); /* ensure that we reset our next allocated index */
|
||||
renumber_edges(h);
|
||||
|
||||
// Recreate special stylised edges.
|
||||
add_edge(h.start, h.startDs, h);
|
||||
@@ -173,56 +85,13 @@ void clear_graph(NGHolder &h) {
|
||||
add_edge(h.accept, h.acceptEod, h);
|
||||
}
|
||||
|
||||
std::pair<NFAEdge, bool> add_edge(NFAVertex u, NFAVertex v, NGHolder &h) {
|
||||
assert(edge(u, v, h.g).second == false);
|
||||
pair<NFAEdge, bool> e = add_edge(u, v, h.g);
|
||||
h.g[e.first].index = h.numEdges++;
|
||||
assert(!h.isValidNumEdges || h.numEdges > 0); // no wrapping
|
||||
return e;
|
||||
}
|
||||
|
||||
std::pair<NFAEdge, bool> add_edge(NFAVertex u, NFAVertex v,
|
||||
const NFAGraph::edge_property_type &ep,
|
||||
NGHolder &h) {
|
||||
assert(edge(u, v, h.g).second == false);
|
||||
pair<NFAEdge, bool> e = add_edge(u, v, ep, h.g);
|
||||
h.g[e.first].index = h.numEdges++;
|
||||
assert(!h.isValidNumEdges || h.numEdges > 0); // no wrapping
|
||||
return e;
|
||||
}
|
||||
|
||||
NFAVertex add_vertex(NGHolder &h) {
|
||||
NFAVertex v = add_vertex(h.g);
|
||||
h[v].index = h.numVertices++;
|
||||
assert(h.numVertices > 0); // no wrapping
|
||||
return v;
|
||||
}
|
||||
|
||||
NFAVertex add_vertex(const NFAGraph::vertex_property_type &vp, NGHolder &h) {
|
||||
NFAVertex v = add_vertex(h);
|
||||
u32 i = h.g[v].index; /* preserve index */
|
||||
h.g[v] = vp;
|
||||
h.g[v].index = i;
|
||||
return v;
|
||||
}
|
||||
|
||||
void NGHolder::renumberEdges() {
|
||||
numEdges = renumberGraphEdges(g);
|
||||
isValidNumEdges = true;
|
||||
}
|
||||
|
||||
void NGHolder::renumberVertices() {
|
||||
numVertices = renumberGraphVertices(g);
|
||||
isValidNumVertices = true;
|
||||
}
|
||||
|
||||
NFAVertex NGHolder::getSpecialVertex(u32 id) const {
|
||||
switch (id) {
|
||||
case NODE_START: return start;
|
||||
case NODE_START_DOTSTAR: return startDs;
|
||||
case NODE_ACCEPT: return accept;
|
||||
case NODE_ACCEPT_EOD: return acceptEod;
|
||||
default: return nullptr;
|
||||
case NODE_START: return start;
|
||||
case NODE_START_DOTSTAR: return startDs;
|
||||
case NODE_ACCEPT: return accept;
|
||||
case NODE_ACCEPT_EOD: return acceptEod;
|
||||
default: return null_vertex();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user