void AddPrefix( Suffix &active, int last_char_index )
{
int parent_node;
int last_parent_node = -1;
for ( ; ; ) {
Edge edge;
parent_node = active.origin_node;
if ( active.Explicit() ) {
edge = Edge::Find( active.origin_node, T[ last_char_index ] );
if ( edge.start_node != -1 )
break;
} else { //implicit node, a little more complicated
edge = Edge::Find( active.origin_node, T[ active.first_char_index ] );
int span = active.last_char_index - active.first_char_index;
if ( T[ edge.first_char_index + span + 1 ] == T[ last_char_index ] )
break;
parent_node = edge.SplitEdge( active );
}
Edge *new_edge = new Edge( last_char_index, T.N, parent_node );
new_edge->Insert();
//cout << "Created edge to new leaf: " << *new_edge << "\n";
AddSuffixLink( last_parent_node, parent_node );
if ( active.origin_node == 0 ) {
//cout << "Can't follow suffix link, I'm at the root\n";
active.first_char_index++;
} else {
/*
cout << "Following suffix link from node "
<< active.origin_node
<< " to node "
<< Suffix_Nodes[ active.origin_node ].suffix_node
<< ".\n";
*/
active.origin_node = Suffix_Nodes[ active.origin_node ].suffix_node;
//cout << "New prefix : " << active << "\n";
}
active.Canonize();
//ADDED THIS DELETE HERE
delete(new_edge);
new_edge = NULL;
}
AddSuffixLink( last_parent_node, parent_node );
active.last_char_index++; //Now the endpoint is the next active point
active.Canonize();
};
and
int Edge::SplitEdge( Suffix &s )
{
//cout << "Splitting edge: " << *this << "\n";
Remove();
Edge *new_edge =
new Edge( first_char_index,
first_char_index + s.last_char_index - s.first_char_index,
s.origin_node );
new_edge->Insert();
Suffix_Nodes[ new_edge->end_node ].suffix_node = s.origin_node;
first_char_index += s.last_char_index - s.first_char_index + 1;
start_node = new_edge->end_node;
Insert();
//cout << "New edge: " << *new_edge << "\n";
//cout << "Old edge: " << *this << "\n";
//ADDED THIS DELETE HERE
delete(new_edge);
//return new_edge->end_node;
return(start_node);
}