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

algo: better definition of link to infinity

parent 01630588
No related branches found
No related tags found
1 merge request!179C casts to C++ casts redesign
......@@ -4,6 +4,8 @@
 
#pragma once
 
#include <limits>
#include <indexes/stringology/FactorOracleAutomaton.h>
#include <string/LinearString.h>
 
......@@ -13,6 +15,8 @@ namespace indexing {
 
class ExactFactorOracleAutomaton {
private:
static constexpr unsigned infinity = std::numeric_limits < unsigned >::max ( );
template < class SymbolType >
static void oracleAddLetter ( automaton::DFA < SymbolType, unsigned > & oracle, const SymbolType & symbol, ext::map < unsigned, unsigned > & supplyFunction );
 
......@@ -28,11 +32,11 @@ public:
 
template < class SymbolType >
indexes::stringology::FactorOracleAutomaton < SymbolType > ExactFactorOracleAutomaton::construct ( const string::LinearString < SymbolType > & pattern ) {
automaton::DFA < SymbolType, unsigned > oracleAutomaton ( 0 );
automaton::DFA < SymbolType, unsigned > oracleAutomaton ( 0u );
 
oracleAutomaton.addFinalState ( oracleAutomaton.getInitialState ( ) );
 
ext::map < unsigned, unsigned > supplyFunction { { 0u, ( unsigned ) -1 } };
ext::vector < unsigned > supplyFunction { infinity }; //vector is fine, the state number is exactly the index to the vector
 
oracleAutomaton.setInputAlphabet ( pattern.getAlphabet ( ) );
 
......@@ -53,14 +57,14 @@ void ExactFactorOracleAutomaton::oracleAddLetter ( automaton::DFA < SymbolType,
oracle.addTransition ( lastState, symbol, newState );
unsigned kState = supplyFunction.find( lastState )->second;
 
while ( kState != ( unsigned ) -1 && oracle.getTransitions ( ).find ( { kState, symbol } ) == oracle.getTransitions ( ).end ( ) ) {
while ( kState != infinity && oracle.getTransitions ( ).find ( { kState, symbol } ) == oracle.getTransitions ( ).end ( ) ) {
oracle.addTransition ( kState, symbol, newState );
kState = supplyFunction.find( kState )->second;
}
 
unsigned supplyState = 0;
 
if ( kState != ( unsigned ) -1 )
if ( kState != infinity )
supplyState = oracle.getTransitions ( ).find( { kState, symbol } )->second;
 
supplyFunction.insert( { newState, supplyState } );
......
......@@ -4,6 +4,8 @@
 
#pragma once
 
#include <limits>
#include <indexes/stringology/SuffixAutomaton.h>
#include <string/LinearString.h>
 
......@@ -15,6 +17,8 @@ namespace indexing {
 
class ExactSuffixAutomaton {
private:
static constexpr unsigned infinity = std::numeric_limits < unsigned >::max ( );
template < class SymbolType >
static void suffixAutomatonAddSymbol ( automaton::DFA < SymbolType, unsigned > & suffixAutomaton, const SymbolType & symbol, std::vector < std::pair < unsigned, int > > & suffixLinks, unsigned & lastState );
 
......@@ -30,7 +34,7 @@ indexes::stringology::SuffixAutomaton < SymbolType > ExactSuffixAutomaton::const
 
suffixAutomaton.setInputAlphabet ( pattern.getAlphabet ( ) );
 
std::vector < std::pair < unsigned, int > > suffixLinks = { { ( unsigned ) -1, 0 } }; //vector is fine, the state number is exactly the index to the vector
std::vector < std::pair < unsigned, int > > suffixLinks = { { infinity, 0 } }; //vector is fine, the state number is exactly the index to the vector
unsigned lastState = 0;
 
if ( common::GlobalData::verbose )
......@@ -43,7 +47,7 @@ indexes::stringology::SuffixAutomaton < SymbolType > ExactSuffixAutomaton::const
suffixAutomatonAddSymbol ( suffixAutomaton, symbol, suffixLinks, lastState );
}
 
while ( lastState != ( unsigned ) -1 ) {
while ( lastState != infinity ) {
suffixAutomaton.addFinalState ( lastState );
lastState = suffixLinks [ lastState ].first;
}
......@@ -59,16 +63,16 @@ void ExactSuffixAutomaton::suffixAutomatonAddSymbol ( automaton::DFA < SymbolTyp
 
int lastSuffixLength = suffixLinks [ lastState ].second;
 
suffixLinks.emplace_back ( ( unsigned ) -1, lastSuffixLength + 1 );
suffixLinks.emplace_back ( infinity, lastSuffixLength + 1 );
 
unsigned kState = lastState;
 
while ( kState != ( unsigned ) -1 && suffixAutomaton.getTransitions ( ).find ( { kState, symbol } ) == suffixAutomaton.getTransitions ( ).end ( ) ) {
while ( kState != infinity && suffixAutomaton.getTransitions ( ).find ( { kState, symbol } ) == suffixAutomaton.getTransitions ( ).end ( ) ) {
suffixAutomaton.addTransition ( kState, symbol, newState );
kState = suffixLinks [ kState ].first;
}
 
if ( kState == ( unsigned ) -1 ) {
if ( kState == infinity ) {
suffixLinks [ newState ].first = 0;
} else {
unsigned qState = suffixAutomaton.getTransitions ( ).find ( { kState, symbol } )->second;
......@@ -87,7 +91,7 @@ void ExactSuffixAutomaton::suffixAutomatonAddSymbol ( automaton::DFA < SymbolTyp
for ( const auto & transition : suffixAutomaton.getTransitionsFromState ( qState ) )
suffixAutomaton.addTransition ( cloneState, transition.first.second, transition.second );
 
while ( kState != ( unsigned ) -1 && suffixAutomaton.removeTransition ( kState, symbol, qState ) ) {
while ( kState != infinity && suffixAutomaton.removeTransition ( kState, symbol, qState ) ) {
suffixAutomaton.addTransition ( kState, symbol, cloneState );
kState = suffixLinks [ kState ].first;
}
......
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