From 43948c8bd7aa9e06a32ec8e22dda6520280036a3 Mon Sep 17 00:00:00 2001 From: Jan Travnicek <Jan.Travnicek@fit.cvut.cz> Date: Wed, 25 Oct 2017 07:38:07 +0200 Subject: [PATCH] use vector instead of map in DAWG construction --- .../indexing/ExactSuffixAutomaton.h | 32 +++++++++---------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/alib2algo/src/stringology/indexing/ExactSuffixAutomaton.h b/alib2algo/src/stringology/indexing/ExactSuffixAutomaton.h index 528d580077..c190c5ea1a 100644 --- a/alib2algo/src/stringology/indexing/ExactSuffixAutomaton.h +++ b/alib2algo/src/stringology/indexing/ExactSuffixAutomaton.h @@ -15,7 +15,7 @@ namespace indexing { class ExactSuffixAutomaton { private: 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: template < class SymbolType > @@ -29,52 +29,52 @@ automaton::DFA < SymbolType, unsigned > ExactSuffixAutomaton::construct ( const 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; - for ( const DefaultSymbolType & symbol : pattern.getContent ( ) ) + for ( const SymbolType & symbol : pattern.getContent ( ) ) suffixAutomatonAddSymbol ( suffixAutomaton, symbol, suffixLinks, lastState ); while ( lastState != ( unsigned ) -1 ) { suffixAutomaton.addFinalState ( lastState ); - lastState = suffixLinks.find ( lastState )->second.first; + lastState = suffixLinks [ lastState ].first; } return suffixAutomaton; } 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 ( ); 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; while ( kState != ( unsigned ) -1 && suffixAutomaton.getTransitions ( ).find ( { kState, symbol } ) == suffixAutomaton.getTransitions ( ).end ( ) ) { suffixAutomaton.addTransition ( kState, symbol, newState ); - kState = suffixLinks.find ( kState )->second.first; + kState = suffixLinks [ kState ].first; } if ( kState == ( unsigned ) -1 ) { - suffixLinks.find ( newState )->second.first = 0; + suffixLinks [ newState ].first = 0; } else { unsigned qState = suffixAutomaton.getTransitions ( ).find ( { kState, symbol } )->second; - int kSuffixLength = suffixLinks.find ( kState )->second.second; - int qSuffixLength = suffixLinks.find ( qState )->second.second; + int kSuffixLength = suffixLinks [ kState ].second; + int qSuffixLength = suffixLinks [ qState ].second; if ( kSuffixLength + 1 == qSuffixLength ) { - suffixLinks.find ( newState )->second.first = qState; + suffixLinks [ newState ].first = qState; } else { unsigned cloneState = suffixAutomaton.getStates ( ).size ( ); 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 ) ) suffixAutomaton.addTransition ( cloneState, transition.first.second, transition.second ); @@ -84,11 +84,11 @@ void ExactSuffixAutomaton::suffixAutomatonAddSymbol ( automaton::DFA < SymbolTyp && suffixAutomaton.getTransitions ( ).find ( { kState, symbol } )->second == qState ) { suffixAutomaton.removeTransition ( kState, symbol, qState ); suffixAutomaton.addTransition ( kState, symbol, cloneState ); - kState = suffixLinks.find ( kState )->second.first; + kState = suffixLinks [ kState ].first; } - suffixLinks.find ( qState )->second.first = cloneState; - suffixLinks.find ( newState )->second.first = cloneState; + suffixLinks [ qState ].first = cloneState; + suffixLinks [ newState ].first = cloneState; } } lastState = newState; -- GitLab