Skip to content
Snippets Groups Projects
Commit 43948c8b authored by Jan Trávníček's avatar Jan Trávníček
Browse files

use vector instead of map in DAWG construction

parent a5519062
No related branches found
No related tags found
No related merge requests found
...@@ -15,7 +15,7 @@ namespace indexing { ...@@ -15,7 +15,7 @@ namespace indexing {
class ExactSuffixAutomaton { class ExactSuffixAutomaton {
private: private:
template < class SymbolType > template < class SymbolType >
static void suffixAutomatonAddSymbol ( automaton::DFA < SymbolType, unsigned > & suffixAutomaton, const SymbolType & symbol, std::map < unsigned, std::pair < unsigned, int > > & suffixLinks, unsigned & lastState ); static void suffixAutomatonAddSymbol ( automaton::DFA < SymbolType, unsigned > & suffixAutomaton, const SymbolType & symbol, std::vector < std::pair < unsigned, int > > & suffixLinks, unsigned & lastState );
   
public: public:
template < class SymbolType > template < class SymbolType >
...@@ -29,52 +29,52 @@ automaton::DFA < SymbolType, unsigned > ExactSuffixAutomaton::construct ( const ...@@ -29,52 +29,52 @@ automaton::DFA < SymbolType, unsigned > ExactSuffixAutomaton::construct ( const
   
suffixAutomaton.setInputAlphabet ( pattern.getAlphabet ( ) ); suffixAutomaton.setInputAlphabet ( pattern.getAlphabet ( ) );
   
std::map < unsigned, std::pair < unsigned, int > > suffixLinks = { { 0u, { ( unsigned ) -1, 0 } } }; std::vector < std::pair < unsigned, int > > suffixLinks = { { ( unsigned ) -1, 0 } }; //vector is fine, the state number is exactly the index to the vector
unsigned lastState = 0; unsigned lastState = 0;
   
for ( const DefaultSymbolType & symbol : pattern.getContent ( ) ) for ( const SymbolType & symbol : pattern.getContent ( ) )
suffixAutomatonAddSymbol ( suffixAutomaton, symbol, suffixLinks, lastState ); suffixAutomatonAddSymbol ( suffixAutomaton, symbol, suffixLinks, lastState );
   
while ( lastState != ( unsigned ) -1 ) { while ( lastState != ( unsigned ) -1 ) {
suffixAutomaton.addFinalState ( lastState ); suffixAutomaton.addFinalState ( lastState );
lastState = suffixLinks.find ( lastState )->second.first; lastState = suffixLinks [ lastState ].first;
} }
   
return suffixAutomaton; return suffixAutomaton;
} }
   
template < class SymbolType > template < class SymbolType >
void ExactSuffixAutomaton::suffixAutomatonAddSymbol ( automaton::DFA < SymbolType, unsigned > & suffixAutomaton, const SymbolType & symbol, std::map < unsigned, std::pair < unsigned, int > > & suffixLinks, unsigned & lastState ) { void ExactSuffixAutomaton::suffixAutomatonAddSymbol ( automaton::DFA < SymbolType, unsigned > & suffixAutomaton, const SymbolType & symbol, std::vector < std::pair < unsigned, int > > & suffixLinks, unsigned & lastState ) {
unsigned newState = suffixAutomaton.getStates ( ).size ( ); unsigned newState = suffixAutomaton.getStates ( ).size ( );
   
suffixAutomaton.addState ( newState ); suffixAutomaton.addState ( newState );
   
int lastSuffixLength = suffixLinks.find ( lastState )->second.second; int lastSuffixLength = suffixLinks [ lastState ].second;
   
suffixLinks.insert ( { newState, { ( unsigned ) -1, lastSuffixLength + 1 } } ); suffixLinks.emplace_back ( ( unsigned ) -1, lastSuffixLength + 1 );
   
unsigned kState = lastState; unsigned kState = lastState;
   
while ( kState != ( unsigned ) -1 && suffixAutomaton.getTransitions ( ).find ( { kState, symbol } ) == suffixAutomaton.getTransitions ( ).end ( ) ) { while ( kState != ( unsigned ) -1 && suffixAutomaton.getTransitions ( ).find ( { kState, symbol } ) == suffixAutomaton.getTransitions ( ).end ( ) ) {
suffixAutomaton.addTransition ( kState, symbol, newState ); suffixAutomaton.addTransition ( kState, symbol, newState );
kState = suffixLinks.find ( kState )->second.first; kState = suffixLinks [ kState ].first;
} }
   
if ( kState == ( unsigned ) -1 ) { if ( kState == ( unsigned ) -1 ) {
suffixLinks.find ( newState )->second.first = 0; suffixLinks [ newState ].first = 0;
} else { } else {
unsigned qState = suffixAutomaton.getTransitions ( ).find ( { kState, symbol } )->second; unsigned qState = suffixAutomaton.getTransitions ( ).find ( { kState, symbol } )->second;
   
int kSuffixLength = suffixLinks.find ( kState )->second.second; int kSuffixLength = suffixLinks [ kState ].second;
int qSuffixLength = suffixLinks.find ( qState )->second.second; int qSuffixLength = suffixLinks [ qState ].second;
   
if ( kSuffixLength + 1 == qSuffixLength ) { if ( kSuffixLength + 1 == qSuffixLength ) {
suffixLinks.find ( newState )->second.first = qState; suffixLinks [ newState ].first = qState;
} else { } else {
unsigned cloneState = suffixAutomaton.getStates ( ).size ( ); unsigned cloneState = suffixAutomaton.getStates ( ).size ( );
suffixAutomaton.addState ( cloneState ); suffixAutomaton.addState ( cloneState );
   
suffixLinks.insert ( { cloneState, { suffixLinks.find ( qState )->second.first, kSuffixLength + 1 } } ); suffixLinks.emplace_back ( suffixLinks [ qState ].first, kSuffixLength + 1 );
   
for ( const auto & transition : suffixAutomaton.getTransitionsFromState ( qState ) ) for ( const auto & transition : suffixAutomaton.getTransitionsFromState ( qState ) )
suffixAutomaton.addTransition ( cloneState, transition.first.second, transition.second ); suffixAutomaton.addTransition ( cloneState, transition.first.second, transition.second );
...@@ -84,11 +84,11 @@ void ExactSuffixAutomaton::suffixAutomatonAddSymbol ( automaton::DFA < SymbolTyp ...@@ -84,11 +84,11 @@ void ExactSuffixAutomaton::suffixAutomatonAddSymbol ( automaton::DFA < SymbolTyp
&& suffixAutomaton.getTransitions ( ).find ( { kState, symbol } )->second == qState ) { && suffixAutomaton.getTransitions ( ).find ( { kState, symbol } )->second == qState ) {
suffixAutomaton.removeTransition ( kState, symbol, qState ); suffixAutomaton.removeTransition ( kState, symbol, qState );
suffixAutomaton.addTransition ( kState, symbol, cloneState ); suffixAutomaton.addTransition ( kState, symbol, cloneState );
kState = suffixLinks.find ( kState )->second.first; kState = suffixLinks [ kState ].first;
} }
   
suffixLinks.find ( qState )->second.first = cloneState; suffixLinks [ qState ].first = cloneState;
suffixLinks.find ( newState )->second.first = cloneState; suffixLinks [ newState ].first = cloneState;
} }
} }
lastState = newState; lastState = newState;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment