From 84e7936100a5777f2571419465d705b2422fc054 Mon Sep 17 00:00:00 2001 From: Jan Travnicek <Jan.Travnicek@fit.cvut.cz> Date: Thu, 12 Jan 2017 21:39:29 +0100 Subject: [PATCH] template some more stringology algorithms --- .../exact/ExactMatchingAutomaton.cpp | 18 +--------- .../exact/ExactMatchingAutomaton.h | 20 ++++++++++- ...tiNondeterministicSubsequenceAutomaton.cpp | 33 ++--------------- ...ultiNondeterministicSubsequenceAutomaton.h | 35 +++++++++++++++++-- 4 files changed, 55 insertions(+), 51 deletions(-) diff --git a/alib2algo/src/stringology/exact/ExactMatchingAutomaton.cpp b/alib2algo/src/stringology/exact/ExactMatchingAutomaton.cpp index e25c880757..ab84e7dac5 100644 --- a/alib2algo/src/stringology/exact/ExactMatchingAutomaton.cpp +++ b/alib2algo/src/stringology/exact/ExactMatchingAutomaton.cpp @@ -19,23 +19,7 @@ automaton::Automaton ExactMatchingAutomaton::construct(const string::String& pat return dispatch(pattern.getData()); } -automaton::NFA < > ExactMatchingAutomaton::construct(const string::LinearString < >& pattern) { - automaton::NFA < > res(DefaultStateType(0)); - res.setInputAlphabet(pattern.getAlphabet()); - for(const DefaultSymbolType& symbol : pattern.getAlphabet()) { - res.addTransition(DefaultStateType(0), symbol, DefaultStateType(0)); - } - int i = 1; - for(const DefaultSymbolType& symbol : pattern.getContent()) { - res.addState(DefaultStateType(i)); - res.addTransition(DefaultStateType(i-1), symbol, DefaultStateType(i)); - i++; - } - res.addFinalState(DefaultStateType(i-1)); - return res; -} - -auto ExactMatchingAutomatonLinearString = ExactMatchingAutomaton::RegistratorWrapper<automaton::NFA < > , string::LinearString < >>(ExactMatchingAutomaton::construct); +auto ExactMatchingAutomatonLinearString = ExactMatchingAutomaton::RegistratorWrapper<automaton::NFA < DefaultSymbolType, unsigned > , string::LinearString < > >(ExactMatchingAutomaton::construct); } /* namespace exact */ diff --git a/alib2algo/src/stringology/exact/ExactMatchingAutomaton.h b/alib2algo/src/stringology/exact/ExactMatchingAutomaton.h index 0f1ee943ab..3df4f87bc0 100644 --- a/alib2algo/src/stringology/exact/ExactMatchingAutomaton.h +++ b/alib2algo/src/stringology/exact/ExactMatchingAutomaton.h @@ -26,9 +26,27 @@ public: */ static automaton::Automaton construct(const string::String& pattern); - static automaton::NFA < > construct(const string::LinearString < >& pattern); + template < class SymbolType > + static automaton::NFA < SymbolType, unsigned > construct(const string::LinearString < SymbolType > & pattern); }; +template < class SymbolType > +automaton::NFA < SymbolType, unsigned > ExactMatchingAutomaton::construct(const string::LinearString < SymbolType > & pattern) { + automaton::NFA < SymbolType, unsigned > res( 0 ); + res.setInputAlphabet(pattern.getAlphabet()); + for(const SymbolType& symbol : pattern.getAlphabet()) { + res.addTransition( 0, symbol, 0); + } + unsigned i = 1; + for(const SymbolType& symbol : pattern.getContent()) { + res.addState( i ); + res.addTransition( i - 1, symbol, i ); + i++; + } + res.addFinalState( i - 1 ); + return res; +} + } /* namespace exact */ } /* namespace stringology */ diff --git a/alib2algo/src/stringology/exact/ExactMultiNondeterministicSubsequenceAutomaton.cpp b/alib2algo/src/stringology/exact/ExactMultiNondeterministicSubsequenceAutomaton.cpp index e1eeca008c..4ef32e4c67 100644 --- a/alib2algo/src/stringology/exact/ExactMultiNondeterministicSubsequenceAutomaton.cpp +++ b/alib2algo/src/stringology/exact/ExactMultiNondeterministicSubsequenceAutomaton.cpp @@ -6,43 +6,14 @@ */ #include "ExactMultiNondeterministicSubsequenceAutomaton.h" -#include <string/LinearString.h> -#include <string/Epsilon.h> #include <common/ContainerConverter.hpp> namespace stringology { namespace exact { -automaton::Automaton ExactMultiNondeterministicSubsequenceAutomaton::construct(const std::set<string::String>& texts) { - return automaton::Automaton(ExactMultiNondeterministicSubsequenceAutomaton::construct(common::ContainerConverter<std::set<string::LinearString < >>, std::set<string::String>, string::LinearString < >>::convert(texts))); -} - -automaton::EpsilonNFA < > ExactMultiNondeterministicSubsequenceAutomaton::construct(const std::set<string::LinearString < >>& texts) { - automaton::EpsilonNFA < > res(DefaultStateType(0)); - res.addFinalState(DefaultStateType(0)); - - int j = 0; - for(const string::LinearString < >& text : texts) { - res.addInputSymbols(text.getAlphabet()); - - res.addState(DefaultStateType(0, j)); - res.addFinalState(DefaultStateType(0, j)); - res.addTransition(DefaultStateType(0), DefaultStateType(0, j)); - - int i = 1; - for(const DefaultSymbolType& symbol : text.getContent()) { - res.addState(DefaultStateType(i, j)); - res.addFinalState(DefaultStateType(i, j)); - - res.addTransition(DefaultStateType(i-1, j), symbol, DefaultStateType(i, j)); - res.addTransition(DefaultStateType(i-1, j), DefaultStateType(i, j)); - i++; - } - j++; - } - - return res; +automaton::Automaton ExactMultiNondeterministicSubsequenceAutomaton::construct ( const std::set < string::String > & texts ) { + return automaton::Automaton ( ExactMultiNondeterministicSubsequenceAutomaton::construct ( common::ContainerConverter < std::set < string::LinearString < > >, std::set < string::String >, string::LinearString < > >::convert ( texts ) ) ); } } /* namespace exact */ diff --git a/alib2algo/src/stringology/exact/ExactMultiNondeterministicSubsequenceAutomaton.h b/alib2algo/src/stringology/exact/ExactMultiNondeterministicSubsequenceAutomaton.h index af468031cc..cd61e16f2a 100644 --- a/alib2algo/src/stringology/exact/ExactMultiNondeterministicSubsequenceAutomaton.h +++ b/alib2algo/src/stringology/exact/ExactMultiNondeterministicSubsequenceAutomaton.h @@ -12,6 +12,8 @@ #include <automaton/FSM/EpsilonNFA.h> #include <string/LinearString.h> #include <string/String.h> +#include <container/ObjectsPair.h> +#include <primitive/Unsigned.h> namespace stringology { @@ -23,11 +25,40 @@ public: * Performs conversion. * @return left regular grammar equivalent to source automaton. */ - static automaton::Automaton construct(const std::set<string::String>& text); + static automaton::Automaton construct ( const std::set < string::String > & text ); - static automaton::EpsilonNFA < > construct(const std::set<string::LinearString < >>& text); + template < class SymbolType > + static automaton::EpsilonNFA < SymbolType, DefaultEpsilonType, std::pair < unsigned, unsigned > > construct ( const std::set < string::LinearString < SymbolType > > & text); }; +template < class SymbolType > +automaton::EpsilonNFA < SymbolType, DefaultEpsilonType, std::pair < unsigned, unsigned > > ExactMultiNondeterministicSubsequenceAutomaton::construct ( const std::set < string::LinearString < SymbolType > > & texts) { + automaton::EpsilonNFA < SymbolType, DefaultEpsilonType, std::pair < unsigned, unsigned > > res ( std::make_pair ( 0u, 0u ) ); + res.addFinalState ( std::make_pair ( 0u, 0u ) ); + + unsigned j = 1; + for ( const string::LinearString < SymbolType > & text : texts ) { + res.addInputSymbols ( text.getAlphabet ( ) ); + + res.addState ( std::make_pair ( 0u, j ) ); + res.addFinalState ( std::make_pair ( 0u, j ) ); + res.addTransition ( std::make_pair ( 0u, 0u ), std::make_pair ( 0u, j ) ); + + unsigned i = 1; + for ( const SymbolType & symbol : text.getContent ( ) ) { + res.addState ( std::make_pair ( i, j ) ); + res.addFinalState ( std::make_pair ( i, j ) ); + + res.addTransition ( std::make_pair ( i - 1, j ), symbol, std::make_pair ( i, j ) ); + res.addTransition ( std::make_pair ( i - 1, j ), std::make_pair ( i, j ) ); + i++; + } + j++; + } + + return res; +} + } /* namespace exact */ } /* namespace stringology */ -- GitLab