diff --git a/alib2algo/src/automaton/run/Accept.cpp b/alib2algo/src/automaton/run/Accept.cpp index 50554d04be5498b972b2376f619460677097d6f3..f969473d1c9293d69aaeb3ec32bd83973c1697dc 100644 --- a/alib2algo/src/automaton/run/Accept.cpp +++ b/alib2algo/src/automaton/run/Accept.cpp @@ -21,87 +21,102 @@ namespace automaton { namespace run { -bool Accept::accept(const automaton::Automaton& automaton, const alib::Object& object) { - return getInstance().dispatch(automaton.getData(), object.getData()); +bool Accept::accept ( const automaton::Automaton & automaton, const alib::Object & object ) { + return getInstance ( ).dispatch ( automaton.getData ( ), object.getData ( ) ); } -bool Accept::accept(const automaton::Automaton& automaton, const string::LinearString& string) { - return getInstance().dispatch(automaton.getData(), string); +bool Accept::accept ( const automaton::Automaton & automaton, const string::LinearString & string ) { + return getInstance ( ).dispatch ( automaton.getData ( ), string ); } -bool Accept::accept(const automaton::Automaton& automaton, const tree::RankedTree& tree) { - return getInstance().dispatch(automaton.getData(), tree); +bool Accept::accept ( const automaton::Automaton & automaton, const tree::RankedTree & tree ) { + return getInstance ( ).dispatch ( automaton.getData ( ), tree ); } -bool Accept::accept(const automaton::DFA& automaton, const string::LinearString& string) { - std::tuple<bool, automaton::State, std::set<unsigned>> res = Run::calculateState(automaton, string); - return std::get<0>(res) && automaton.getFinalStates().count(std::get<1>(res)); +bool Accept::accept ( const automaton::DFA & automaton, const string::LinearString & string ) { + std::tuple < bool, automaton::State, std::set < unsigned > > res = Run::calculateState ( automaton, string ); + + return std::get < 0 > ( res ) && automaton.getFinalStates ( ).count ( std::get < 1 > ( res ) ); } -auto AcceptDFALinearString = Accept::RegistratorWrapper<bool, automaton::DFA, string::LinearString>(Accept::getInstance(), Accept::accept); +auto AcceptDFALinearString = Accept::RegistratorWrapper < bool, automaton::DFA, string::LinearString > ( Accept::getInstance ( ), Accept::accept ); + +bool Accept::accept ( const automaton::NFA & automaton, const string::LinearString & string ) { + std::tuple < bool, std::set < automaton::State >, std::set < unsigned > > res = Run::calculateStates ( automaton, string ); -bool Accept::accept( const automaton::NFA& automaton, const string::LinearString & string ) { - std::tuple<bool, std::set<automaton::State>, std::set<unsigned>> res = Run::calculateStates(automaton, string); - return std::get<0>(res) && std::any_of(std::get<1>(res).begin(), std::get<1>(res).end(), [&](const automaton::State& state) { return automaton.getFinalStates().count(state); } ); + return std::get < 0 > ( res ) && std::any_of ( std::get < 1 > ( res ).begin ( ), std::get < 1 > ( res ).end ( ), [&] ( const automaton::State & state ) { + return automaton.getFinalStates ( ).count ( state ); + } ); } -auto AcceptNFALinearString = Accept::RegistratorWrapper<bool, automaton::NFA, string::LinearString>(Accept::getInstance(), Accept::accept); +auto AcceptNFALinearString = Accept::RegistratorWrapper < bool, automaton::NFA, string::LinearString > ( Accept::getInstance ( ), Accept::accept ); -bool Accept::accept(const automaton::DFTA& automaton, const tree::RankedTree& tree) { - std::tuple<bool, automaton::State, std::set<unsigned>> res = Run::calculateState(automaton, tree); - return std::get<0>(res) && automaton.getFinalStates().count(std::get<1>(res)); +bool Accept::accept ( const automaton::DFTA & automaton, const tree::RankedTree & tree ) { + std::tuple < bool, automaton::State, std::set < unsigned > > res = Run::calculateState ( automaton, tree ); + + return std::get < 0 > ( res ) && automaton.getFinalStates ( ).count ( std::get < 1 > ( res ) ); } -auto AcceptDFTARankedTree = Accept::RegistratorWrapper<bool, automaton::DFTA, tree::RankedTree>(Accept::getInstance(), Accept::accept); +auto AcceptDFTARankedTree = Accept::RegistratorWrapper < bool, automaton::DFTA, tree::RankedTree > ( Accept::getInstance ( ), Accept::accept ); + +bool Accept::accept ( const automaton::NFTA & automaton, const tree::RankedTree & tree ) { + std::tuple < bool, std::set < automaton::State >, std::set < unsigned > > res = Run::calculateStates ( automaton, tree ); -bool Accept::accept(const automaton::NFTA& automaton, const tree::RankedTree & tree) { - std::tuple<bool, std::set<automaton::State>, std::set<unsigned>> res = Run::calculateStates(automaton, tree); - return std::get<0>(res) && std::any_of(std::get<1>(res).begin(), std::get<1>(res).end(), [&](const automaton::State& state) { return automaton.getFinalStates().count(state); } ); + return std::get < 0 > ( res ) && std::any_of ( std::get < 1 > ( res ).begin ( ), std::get < 1 > ( res ).end ( ), [&] ( const automaton::State & state ) { + return automaton.getFinalStates ( ).count ( state ); + } ); } -auto AcceptNFTARankedTree = Accept::RegistratorWrapper<bool, automaton::NFTA, tree::RankedTree>(Accept::getInstance(), Accept::accept); +auto AcceptNFTARankedTree = Accept::RegistratorWrapper < bool, automaton::NFTA, tree::RankedTree > ( Accept::getInstance ( ), Accept::accept ); + +bool Accept::accept ( const automaton::DPDA & automaton, const string::LinearString & string ) { + automaton::State state = automaton.getInitialState ( ); + std::deque < alphabet::Symbol > pushdownStore ( { automaton.getInitialSymbol ( ) } ); + + for ( const alphabet::Symbol & symbol : string.getContent ( ) ) { + auto transitions = automaton.getTransitionsFromState ( state ); + auto transition = transitions.begin ( ); + int pushdownStoreSize = pushdownStore.size ( ); + + for ( ; transition != transitions.end ( ); transition++ ) { + if ( std::get < 1 > ( transition->first ) != symbol ) continue; -bool Accept::accept(const automaton::DPDA& automaton, const string::LinearString& string) { - automaton::State state = automaton.getInitialState(); - std::deque<alphabet::Symbol> pushdownStore({automaton.getInitialSymbol()}); + const std::vector < alphabet::Symbol > & pop = std::get < 2 > ( transition->first ); + int popSize = pop.size ( ); - for(const alphabet::Symbol& symbol : string.getContent()) { - auto transitions = automaton.getTransitionsFromState(state); - auto transition = transitions.begin(); - int pushdownStoreSize = pushdownStore.size(); - for( ; transition != transitions.end(); transition++) { - if(std::get<1>(transition->first) != symbol) continue; + if ( pushdownStoreSize < popSize ) continue; - const std::vector<alphabet::Symbol> & pop = std::get<2>(transition->first); - int popSize = pop.size(); - if(pushdownStoreSize < popSize) continue; bool sign = true; - for(int i = 1; i <= popSize; i++) { - if(pop[popSize - i] != pushdownStore[pushdownStoreSize - i]) { + + for ( int i = 1; i <= popSize; i++ ) + if ( pop[popSize - i] != pushdownStore[pushdownStoreSize - i] ) { sign = false; break; } - } - if(!sign) continue; + + + + if ( !sign ) continue; break; } - if(transition == transitions.end()) return false; + if ( transition == transitions.end ( ) ) return false; - for(auto pop : std::get<2>(transition->first)) pushdownStore.pop_back(); + for ( auto pop : std::get < 2 > ( transition->first ) ) pushdownStore.pop_back ( ); state = transition->second.first; - for(const alphabet::Symbol& push : transition->second.second) pushdownStore.push_back(push); - } - if(automaton.getFinalStates().empty()) { - return pushdownStore.empty(); - } else { - return automaton.getFinalStates().count(state); + + for ( const alphabet::Symbol & push : transition->second.second ) pushdownStore.push_back ( push ); } + + if ( automaton.getFinalStates ( ).empty ( ) ) + return pushdownStore.empty ( ); + else + return automaton.getFinalStates ( ).count ( state ); } -auto AcceptDPDALinearString = Accept::RegistratorWrapper<bool, automaton::DPDA, string::LinearString>(Accept::getInstance(), Accept::accept); +auto AcceptDPDALinearString = Accept::RegistratorWrapper < bool, automaton::DPDA, string::LinearString > ( Accept::getInstance ( ), Accept::accept ); } /* namespace run */ diff --git a/alib2algo/src/automaton/run/Accept.h b/alib2algo/src/automaton/run/Accept.h index ef9201c3758eb52b0d2e6c6250ef0414358e9158..d1d759b67961936ad9a7ded2eb0b0e881933ecf6 100644 --- a/alib2algo/src/automaton/run/Accept.h +++ b/alib2algo/src/automaton/run/Accept.h @@ -18,27 +18,29 @@ namespace automaton { namespace run { -class Accept : public std::DoubleDispatch<bool, automaton::AutomatonBase, alib::ObjectBase> { +class Accept : public std::DoubleDispatch < bool, automaton::AutomatonBase, alib::ObjectBase > { public: /** * Performs conversion. * @return left regular grammar equivalent to source automaton. */ - static bool accept(const automaton::Automaton& automaton, const alib::Object& object); - static bool accept(const automaton::Automaton& automaton, const string::LinearString& string); - static bool accept(const automaton::Automaton& automaton, const tree::RankedTree& string); + static bool accept ( const automaton::Automaton & automaton, const alib::Object & object ); + static bool accept ( const automaton::Automaton & automaton, const string::LinearString & string ); + static bool accept ( const automaton::Automaton & automaton, const tree::RankedTree & string ); - static bool accept(const automaton::DFA& automaton, const string::LinearString& string); - static bool accept(const automaton::NFA& automaton, const string::LinearString& string); - static bool accept(const automaton::DPDA& automaton, const string::LinearString& string); - static bool accept(const automaton::DFTA& automaton, const tree::RankedTree& tree); - static bool accept(const automaton::NFTA& automaton, const tree::RankedTree& tree); + static bool accept ( const automaton::DFA & automaton, const string::LinearString & string ); + static bool accept ( const automaton::NFA & automaton, const string::LinearString & string ); + static bool accept ( const automaton::DPDA & automaton, const string::LinearString & string ); + static bool accept ( const automaton::DFTA & automaton, const tree::RankedTree & tree ); + static bool accept ( const automaton::NFTA & automaton, const tree::RankedTree & tree ); public: - static Accept& getInstance() { + static Accept & getInstance ( ) { static Accept res; + return res; } + }; } /* namespace run */ diff --git a/alib2algo/src/automaton/run/Occurrences.cpp b/alib2algo/src/automaton/run/Occurrences.cpp index 9716e5cb7af5be78fad480c4850989c12c17b194..820c7bc26b922a81a78dfb57389bd27d9d5db9ca 100644 --- a/alib2algo/src/automaton/run/Occurrences.cpp +++ b/alib2algo/src/automaton/run/Occurrences.cpp @@ -19,85 +19,95 @@ namespace automaton { namespace run { -std::set<unsigned> Occurrences::occurrences(const automaton::Automaton& automaton, const alib::Object& object) { - return getInstance().dispatch(automaton.getData(), object.getData()); +std::set < unsigned > Occurrences::occurrences ( const automaton::Automaton & automaton, const alib::Object & object ) { + return getInstance ( ).dispatch ( automaton.getData ( ), object.getData ( ) ); } -std::set<unsigned> Occurrences::occurrences(const automaton::Automaton& automaton, const string::LinearString& string) { - return getInstance().dispatch(automaton.getData(), string); +std::set < unsigned > Occurrences::occurrences ( const automaton::Automaton & automaton, const string::LinearString & string ) { + return getInstance ( ).dispatch ( automaton.getData ( ), string ); } -std::set<unsigned> Occurrences::occurrences(const automaton::Automaton& automaton, const tree::RankedTree& tree) { - return getInstance().dispatch(automaton.getData(), tree); +std::set < unsigned > Occurrences::occurrences ( const automaton::Automaton & automaton, const tree::RankedTree & tree ) { + return getInstance ( ).dispatch ( automaton.getData ( ), tree ); } -std::set<unsigned> Occurrences::occurrences(const automaton::DFA& automaton, const string::LinearString& string) { - std::tuple<bool, automaton::State, std::set<unsigned>> res = Run::calculateState(automaton, string); - return std::get<2>(res); +std::set < unsigned > Occurrences::occurrences ( const automaton::DFA & automaton, const string::LinearString & string ) { + std::tuple < bool, automaton::State, std::set < unsigned > > res = Run::calculateState ( automaton, string ); + + return std::get < 2 > ( res ); } -auto OccurrencesDFALinearString = Occurrences::RegistratorWrapper<std::set<unsigned>, automaton::DFA, string::LinearString>(Occurrences::getInstance(), Occurrences::occurrences); +auto OccurrencesDFALinearString = Occurrences::RegistratorWrapper < std::set < unsigned >, automaton::DFA, string::LinearString > ( Occurrences::getInstance ( ), Occurrences::occurrences ); + +std::set < unsigned > Occurrences::occurrences ( const automaton::DFTA & automaton, const tree::RankedTree & tree ) { + std::tuple < bool, automaton::State, std::set < unsigned > > res = Run::calculateState ( automaton, tree ); -std::set<unsigned> Occurrences::occurrences(const automaton::DFTA& automaton, const tree::RankedTree & tree) { - std::tuple<bool, automaton::State, std::set<unsigned>> res = Run::calculateState(automaton, tree); - return std::get<2>(res); + return std::get < 2 > ( res ); } -auto OccurrencesDFTARankedTree = Occurrences::RegistratorWrapper<std::set<unsigned>, automaton::DFTA, tree::RankedTree>(Occurrences::getInstance(), Occurrences::occurrences); +auto OccurrencesDFTARankedTree = Occurrences::RegistratorWrapper < std::set < unsigned >, automaton::DFTA, tree::RankedTree > ( Occurrences::getInstance ( ), Occurrences::occurrences ); -std::set<unsigned> Occurrences::occurrences(const automaton::DPDA& automaton, const string::LinearString& string) { - automaton::State state = automaton.getInitialState(); - std::deque<alphabet::Symbol> pushdownStore {automaton.getInitialSymbol()}; +std::set < unsigned > Occurrences::occurrences ( const automaton::DPDA & automaton, const string::LinearString & string ) { + automaton::State state = automaton.getInitialState ( ); + std::deque < alphabet::Symbol > pushdownStore { + automaton.getInitialSymbol ( ) + }; unsigned i = 0; - std::set<unsigned> occ; + std::set < unsigned > occ; + + for ( const alphabet::Symbol & symbol : string.getContent ( ) ) { + if ( automaton.getFinalStates ( ).count ( state ) ) + occ.insert ( i ); - for(const alphabet::Symbol& symbol : string.getContent()) { - if(automaton.getFinalStates().count(state)) - occ.insert(i); + auto transitions = automaton.getTransitionsFromState ( state ); + auto transition = transitions.begin ( ); + unsigned pushdownStoreSize = pushdownStore.size ( ); - auto transitions = automaton.getTransitionsFromState(state); - auto transition = transitions.begin(); - unsigned pushdownStoreSize = pushdownStore.size(); - for(auto transition = transitions.begin(); transition != transitions.end(); transition++) { - if(std::get<1>(transition->first) != symbol) continue; + for ( auto transition = transitions.begin ( ); transition != transitions.end ( ); transition++ ) { + if ( std::get < 1 > ( transition->first ) != symbol ) continue; + + const std::vector < alphabet::Symbol > & pop = std::get < 2 > ( transition->first ); + unsigned popSize = pop.size ( ); + + if ( pushdownStoreSize < popSize ) continue; - const std::vector<alphabet::Symbol> & pop = std::get<2>(transition->first); - unsigned popSize = pop.size(); - if(pushdownStoreSize < popSize) continue; bool sign = true; - for(unsigned i = 1; i <= popSize; i++) { - if(pop[popSize - i] != pushdownStore[pushdownStoreSize - i]) { + + for ( unsigned i = 1; i <= popSize; i++ ) + if ( pop[popSize - i] != pushdownStore[pushdownStoreSize - i] ) { sign = false; break; } - } - if(!sign) continue; + + + + if ( !sign ) continue; break; } - if(transition == transitions.end()) - throw exception::AlibException("Transition not present"); + if ( transition == transitions.end ( ) ) + throw exception::AlibException ( "Transition not present" ); - for(auto pop : std::get<2>(transition->first)) pushdownStore.pop_back(); + for ( auto pop : std::get < 2 > ( transition->first ) ) pushdownStore.pop_back ( ); state = transition->second.first; - for(const alphabet::Symbol& push : transition->second.second) pushdownStore.push_back(push); + + for ( const alphabet::Symbol & push : transition->second.second ) pushdownStore.push_back ( push ); i++; } - if(automaton.getFinalStates().count(state)) - occ.insert(i); + if ( automaton.getFinalStates ( ).count ( state ) ) + occ.insert ( i ); - if(pushdownStore.empty()) { + if ( pushdownStore.empty ( ) ) return occ; - } else { - return {}; - } + else + return { }; } -auto OccurrencesDPDALinearString = Occurrences::RegistratorWrapper<std::set<unsigned>, automaton::DPDA, string::LinearString>(Occurrences::getInstance(), Occurrences::occurrences); +auto OccurrencesDPDALinearString = Occurrences::RegistratorWrapper < std::set < unsigned >, automaton::DPDA, string::LinearString > ( Occurrences::getInstance ( ), Occurrences::occurrences ); } /* namespace run */ diff --git a/alib2algo/src/automaton/run/Occurrences.h b/alib2algo/src/automaton/run/Occurrences.h index 505cc36d27a9bd037998e271b43a73615c2b025b..bd915abc92debe600b52c6111e741c0518290a30 100644 --- a/alib2algo/src/automaton/run/Occurrences.h +++ b/alib2algo/src/automaton/run/Occurrences.h @@ -18,25 +18,27 @@ namespace automaton { namespace run { -class Occurrences : public std::DoubleDispatch<std::set<unsigned>, automaton::AutomatonBase, alib::ObjectBase> { +class Occurrences : public std::DoubleDispatch < std::set < unsigned >, automaton::AutomatonBase, alib::ObjectBase > { public: /** * Performs conversion. * @return left regular grammar equivalent to source automaton. */ - static std::set<unsigned> occurrences(const automaton::Automaton& automaton, const alib::Object& object); - static std::set<unsigned> occurrences(const automaton::Automaton& automaton, const string::LinearString& string); - static std::set<unsigned> occurrences(const automaton::Automaton& automaton, const tree::RankedTree& string); + static std::set < unsigned > occurrences ( const automaton::Automaton & automaton, const alib::Object & object ); + static std::set < unsigned > occurrences ( const automaton::Automaton & automaton, const string::LinearString & string ); + static std::set < unsigned > occurrences ( const automaton::Automaton & automaton, const tree::RankedTree & string ); - static std::set<unsigned> occurrences(const automaton::DFA& automaton, const string::LinearString& string); - static std::set<unsigned> occurrences(const automaton::DPDA& automaton, const string::LinearString& string); - static std::set<unsigned> occurrences(const automaton::DFTA& automaton, const tree::RankedTree & tree); + static std::set < unsigned > occurrences ( const automaton::DFA & automaton, const string::LinearString & string ); + static std::set < unsigned > occurrences ( const automaton::DPDA & automaton, const string::LinearString & string ); + static std::set < unsigned > occurrences ( const automaton::DFTA & automaton, const tree::RankedTree & tree ); public: - static Occurrences& getInstance() { + static Occurrences & getInstance ( ) { static Occurrences res; + return res; } + }; } /* namespace run */ diff --git a/alib2algo/src/automaton/run/Result.cpp b/alib2algo/src/automaton/run/Result.cpp index a28eab15f5782d9ef0d84c0c2b64bc0cef1840a7..7ccc9d6a3d37d39a0ce757c64e3e64dea0569e76 100644 --- a/alib2algo/src/automaton/run/Result.cpp +++ b/alib2algo/src/automaton/run/Result.cpp @@ -19,68 +19,79 @@ namespace automaton { namespace run { -label::Label Result::result(const automaton::Automaton& automaton, const alib::Object& object) { - return getInstance().dispatch(automaton.getData(), object.getData()); +label::Label Result::result ( const automaton::Automaton & automaton, const alib::Object & object ) { + return getInstance ( ).dispatch ( automaton.getData ( ), object.getData ( ) ); } -label::Label Result::result(const automaton::DFA& automaton, const string::LinearString& string) { - std::tuple<bool, automaton::State, std::set<unsigned>> res = Run::calculateState(automaton, string); - if(std::get<0>(res)) return std::get<1>(res).getName(); - return label::labelFrom("fail"); +label::Label Result::result ( const automaton::DFA & automaton, const string::LinearString & string ) { + std::tuple < bool, automaton::State, std::set < unsigned > > res = Run::calculateState ( automaton, string ); + + if ( std::get < 0 > ( res ) ) return std::get < 1 > ( res ).getName ( ); + + return label::labelFrom ( "fail" ); } -auto ResultDFALinearString = Result::RegistratorWrapper<label::Label, automaton::DFA, string::LinearString>(Result::getInstance(), Result::result); +auto ResultDFALinearString = Result::RegistratorWrapper < label::Label, automaton::DFA, string::LinearString > ( Result::getInstance ( ), Result::result ); + +label::Label Result::result ( const automaton::DFTA & automaton, const tree::RankedTree & tree ) { + std::tuple < bool, automaton::State, std::set < unsigned > > res = Run::calculateState ( automaton, tree ); + + if ( std::get < 0 > ( res ) ) return std::get < 1 > ( res ).getName ( ); -label::Label Result::result(const automaton::DFTA& automaton, const tree::RankedTree& tree) { - std::tuple<bool, automaton::State, std::set<unsigned>> res = Run::calculateState(automaton, tree); - if(std::get<0>(res)) return std::get<1>(res).getName(); - return label::labelFrom("fail"); + return label::labelFrom ( "fail" ); } -auto ResultDFTARankedTree = Result::RegistratorWrapper<label::Label, automaton::DFTA, tree::RankedTree>(Result::getInstance(), Result::result); +auto ResultDFTARankedTree = Result::RegistratorWrapper < label::Label, automaton::DFTA, tree::RankedTree > ( Result::getInstance ( ), Result::result ); + +label::Label Result::result ( const automaton::DPDA & automaton, const string::LinearString & string ) { + automaton::State state = automaton.getInitialState ( ); + std::deque < alphabet::Symbol > pushdownStore ( { automaton.getInitialSymbol ( ) } ); + + for ( const alphabet::Symbol & symbol : string.getContent ( ) ) { + auto transitions = automaton.getTransitionsFromState ( state ); + auto transition = transitions.begin ( ); + int pushdownStoreSize = pushdownStore.size ( ); + + for ( auto transition = transitions.begin ( ); transition != transitions.end ( ); transition++ ) { + if ( std::get < 1 > ( transition->first ) != symbol ) continue; -label::Label Result::result(const automaton::DPDA& automaton, const string::LinearString& string) { - automaton::State state = automaton.getInitialState(); - std::deque<alphabet::Symbol> pushdownStore({automaton.getInitialSymbol()}); + const std::vector < alphabet::Symbol > & pop = std::get < 2 > ( transition->first ); + int popSize = pop.size ( ); - for(const alphabet::Symbol& symbol : string.getContent()) { - auto transitions = automaton.getTransitionsFromState(state); - auto transition = transitions.begin(); - int pushdownStoreSize = pushdownStore.size(); - for(auto transition = transitions.begin(); transition != transitions.end(); transition++) { - if(std::get<1>(transition->first) != symbol) continue; + if ( pushdownStoreSize < popSize ) continue; - const std::vector<alphabet::Symbol> & pop = std::get<2>(transition->first); - int popSize = pop.size(); - if(pushdownStoreSize < popSize) continue; bool sign = true; - for(int i = 1; i <= popSize; i++) { - if(pop[popSize - i] != pushdownStore[pushdownStoreSize - i]) { + + for ( int i = 1; i <= popSize; i++ ) + if ( pop[popSize - i] != pushdownStore[pushdownStoreSize - i] ) { sign = false; break; } - } - if(!sign) continue; + + + + if ( !sign ) continue; break; } - if(transition == transitions.end()) - throw exception::AlibException("Transition not present"); + if ( transition == transitions.end ( ) ) + throw exception::AlibException ( "Transition not present" ); - for(auto pop : std::get<2>(transition->first)) pushdownStore.pop_back(); + for ( auto pop : std::get < 2 > ( transition->first ) ) pushdownStore.pop_back ( ); state = transition->second.first; - for(const alphabet::Symbol& push : transition->second.second) pushdownStore.push_back(push); - } - if(automaton.getFinalStates().empty()) { - return label::Label(label::LabelSetLabel(std::set<label::Label>{})); - } else { - return state.getName(); + + for ( const alphabet::Symbol & push : transition->second.second ) pushdownStore.push_back ( push ); } + + if ( automaton.getFinalStates ( ).empty ( ) ) + return label::Label ( label::LabelSetLabel ( std::set < label::Label > { } ) ); + else + return state.getName ( ); } -auto ResultDPDALinearString = Result::RegistratorWrapper<label::Label, automaton::DPDA, string::LinearString>(Result::getInstance(), Result::result); +auto ResultDPDALinearString = Result::RegistratorWrapper < label::Label, automaton::DPDA, string::LinearString > ( Result::getInstance ( ), Result::result ); } /* namespace run */ diff --git a/alib2algo/src/automaton/run/Result.h b/alib2algo/src/automaton/run/Result.h index 302789ee438ea36874fc571f38c2765a440463cc..a172ef755d89b9673d689c0bb93b9a492cecad41 100644 --- a/alib2algo/src/automaton/run/Result.h +++ b/alib2algo/src/automaton/run/Result.h @@ -19,22 +19,24 @@ namespace automaton { namespace run { -class Result : public std::DoubleDispatch<label::Label, automaton::AutomatonBase, alib::ObjectBase> { +class Result : public std::DoubleDispatch < label::Label, automaton::AutomatonBase, alib::ObjectBase > { public: /** * Performs conversion. * @return left regular grammar equivalent to source automaton. */ - static label::Label result(const automaton::Automaton& automaton, const alib::Object& object); + static label::Label result ( const automaton::Automaton & automaton, const alib::Object & object ); - static label::Label result(const automaton::DFA& automaton, const string::LinearString& string); - static label::Label result(const automaton::DPDA& automaton, const string::LinearString& string); - static label::Label result(const automaton::DFTA& automaton, const tree::RankedTree & tree); + static label::Label result ( const automaton::DFA & automaton, const string::LinearString & string ); + static label::Label result ( const automaton::DPDA & automaton, const string::LinearString & string ); + static label::Label result ( const automaton::DFTA & automaton, const tree::RankedTree & tree ); - static Result& getInstance() { + static Result & getInstance ( ) { static Result res; + return res; } + }; } /* namespace run */ diff --git a/alib2algo/src/automaton/run/Run.cpp b/alib2algo/src/automaton/run/Run.cpp index d46f0ecdc0588b5762d16a2df21bcae511c18fa4..df75580b91b6d57f538fdc97d9aa7f9053d3055c 100644 --- a/alib2algo/src/automaton/run/Run.cpp +++ b/alib2algo/src/automaton/run/Run.cpp @@ -20,132 +20,158 @@ namespace run { // ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -std::tuple<bool, State, std::set<unsigned>> Run::calculateState(const automaton::DFA& automaton, const string::LinearString & string) { +std::tuple < bool, State, std::set < unsigned > > Run::calculateState ( const automaton::DFA & automaton, const string::LinearString & string ) { bool res = true; unsigned i = 0; - std::set<unsigned> occurrences; - automaton::State state = automaton.getInitialState(); - if(automaton.getFinalStates().count(state)) - occurrences.insert(i); - - for(const alphabet::Symbol& symbol : string.getContent()) { - auto transition = automaton.getTransitions().find(std::make_pair(state, symbol) ); - if(transition == automaton.getTransitions().end()) { + std::set < unsigned > occurrences; + automaton::State state = automaton.getInitialState ( ); + + if ( automaton.getFinalStates ( ).count ( state ) ) + occurrences.insert ( i ); + + for ( const alphabet::Symbol & symbol : string.getContent ( ) ) { + auto transition = automaton.getTransitions ( ).find ( std::make_pair ( state, symbol ) ); + + if ( transition == automaton.getTransitions ( ).end ( ) ) { res = false; break; } state = transition->second; i++; - if(automaton.getFinalStates().count(state)) - occurrences.insert(i); + + if ( automaton.getFinalStates ( ).count ( state ) ) + occurrences.insert ( i ); } - return std::make_tuple(res, state, occurrences); + return std::make_tuple ( res, state, occurrences ); } // ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -std::tuple<bool, std::set<State>, std::set<unsigned>> Run::calculateStates(const automaton::NFA& automaton, const string::LinearString& string) { +std::tuple < bool, std::set < State >, std::set < unsigned > > Run::calculateStates ( const automaton::NFA & automaton, const string::LinearString & string ) { bool res = true; unsigned i = 0; - std::set<unsigned> occurrences; - std::set<automaton::State> states { automaton.getInitialState() }; - for(const State& state : states ) - if(automaton.getFinalStates().count(state)) - occurrences.insert(i); - - for(const alphabet::Symbol& symbol : string.getContent()) { - std::set<State> next; - for(const State& state : states) { - auto transitions = automaton.getTransitions().find(std::make_pair(state, symbol)); - if(transitions == automaton.getTransitions().end()) continue; - - next.insert( transitions->second.begin(), transitions->second.end() ); + std::set < unsigned > occurrences; + std::set < automaton::State > states { + automaton.getInitialState ( ) + }; + + for ( const State & state : states ) + if ( automaton.getFinalStates ( ).count ( state ) ) + occurrences.insert ( i ); + + for ( const alphabet::Symbol & symbol : string.getContent ( ) ) { + std::set < State > next; + + for ( const State & state : states ) { + auto transitions = automaton.getTransitions ( ).find ( std::make_pair ( state, symbol ) ); + + if ( transitions == automaton.getTransitions ( ).end ( ) ) continue; + + next.insert ( transitions->second.begin ( ), transitions->second.end ( ) ); } i++; states = next; - for(const State& state : states ) - if(automaton.getFinalStates().count(state)) - occurrences.insert(i); + + for ( const State & state : states ) + if ( automaton.getFinalStates ( ).count ( state ) ) + occurrences.insert ( i ); + } - return std::make_tuple(res, states, occurrences); + return std::make_tuple ( res, states, occurrences ); } // ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -std::pair<bool, State> Run::calculateState(const automaton::DFTA & automaton, const tree::RankedNode & node, std::set<unsigned> & occ, unsigned & i) { - std::vector<State> states; - states.reserve(node.getSymbol().getRank().getData()); +std::pair < bool, State > Run::calculateState ( const automaton::DFTA & automaton, const tree::RankedNode & node, std::set < unsigned > & occ, unsigned & i ) { + std::vector < State > states; + + states.reserve ( node.getSymbol ( ).getRank ( ).getData ( ) ); unsigned tmp = i; i++; bool sign = true; - for (const auto & child : node.getChildren()) { - std::pair<bool, State> res = calculateState(automaton, *child, occ, i); - if (res.first == false) sign = false; - else states.push_back(res.second); + + for ( const auto & child : node.getChildren ( ) ) { + std::pair < bool, State > res = calculateState ( automaton, * child, occ, i ); + + if ( res.first == false ) + sign = false; + else + states.push_back ( res.second ); } - if(!sign) return std::make_pair(false, automaton::State("fail")); + if ( !sign ) return std::make_pair ( false, automaton::State ( "fail" ) ); - const auto & it = automaton.getTransitions().find(std::make_pair(node.getSymbol(), states)); - if (it == automaton.getTransitions().end()) return std::make_pair(false, automaton::State("fail")); + const auto & it = automaton.getTransitions ( ).find ( std::make_pair ( node.getSymbol ( ), states ) ); - State state = it -> second; - if (automaton.getFinalStates().count(state)) occ.insert(tmp); - return std::make_pair(true, state); + if ( it == automaton.getTransitions ( ).end ( ) ) return std::make_pair ( false, automaton::State ( "fail" ) ); + + State state = it->second; + + if ( automaton.getFinalStates ( ).count ( state ) ) occ.insert ( tmp ); + + return std::make_pair ( true, state ); } -std::tuple<bool, State, std::set<unsigned>> Run::calculateState(const automaton::DFTA& automaton, const tree::RankedTree & tree) { - std::set<unsigned> occ; +std::tuple < bool, State, std::set < unsigned > > Run::calculateState ( const automaton::DFTA & automaton, const tree::RankedTree & tree ) { + std::set < unsigned > occ; unsigned i = 0; - std::pair<bool, State> res = calculateState(automaton, tree.getRoot(), occ, i); - return std::make_tuple(res.first, res.second, occ); + std::pair < bool, State > res = calculateState ( automaton, tree.getRoot ( ), occ, i ); + + return std::make_tuple ( res.first, res.second, occ ); } // ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -std::pair<bool, std::set<State>> Run::calculateStates(const automaton::NFTA& automaton, const tree::RankedNode & node, std::set<unsigned>& occ, unsigned & i) { - std::vector<std::set<State> > states; - states.reserve(node.getSymbol().getRank().getData()); +std::pair < bool, std::set < State > > Run::calculateStates ( const automaton::NFTA & automaton, const tree::RankedNode & node, std::set < unsigned > & occ, unsigned & i ) { + std::vector < std::set < State > > states; + + states.reserve ( node.getSymbol ( ).getRank ( ).getData ( ) ); unsigned tmp = i; i++; bool sign = true; - for (const auto & child : node.getChildren()) { - std::pair<bool, std::set<State>> childStates = calculateStates(automaton, *child, occ, i); - if (childStates.second.empty()) + + for ( const auto & child : node.getChildren ( ) ) { + std::pair < bool, std::set < State > > childStates = calculateStates ( automaton, * child, occ, i ); + + if ( childStates.second.empty ( ) ) sign = false; - states.push_back(childStates.second); - } - std::set<State> res; - for (const auto & transition : automaton.getTransitions()) { - if (transition.first.first != node.getSymbol()) continue; - unsigned rank = transition.first.first.getRank().getData(); - for(unsigned i = 0; i < rank ; i++) - if (!states[i].count(transition.first.second[i])) break; - - if (i == rank) res.insert(transition.second.begin(), transition.second.end()); + states.push_back ( childStates.second ); } - for(const State & state : res) { - if(automaton.getFinalStates().count(state)) occ.insert(tmp); + std::set < State > res; + + for ( const auto & transition : automaton.getTransitions ( ) ) { + if ( transition.first.first != node.getSymbol ( ) ) continue; + + unsigned rank = transition.first.first.getRank ( ).getData ( ); + + for ( unsigned i = 0; i < rank; i++ ) + if ( !states[i].count ( transition.first.second[i] ) ) break; + + if ( i == rank ) res.insert ( transition.second.begin ( ), transition.second.end ( ) ); } - return std::make_pair(sign, res); + for ( const State & state : res ) + if ( automaton.getFinalStates ( ).count ( state ) ) occ.insert ( tmp ); + + return std::make_pair ( sign, res ); } -std::tuple<bool, std::set<State>, std::set<unsigned>> Run::calculateStates(const automaton::NFTA& automaton, const tree::RankedTree & tree) { - std::set<unsigned> occ; +std::tuple < bool, std::set < State >, std::set < unsigned > > Run::calculateStates ( const automaton::NFTA & automaton, const tree::RankedTree & tree ) { + std::set < unsigned > occ; unsigned i = 0; - std::pair<bool, std::set<State>> res = calculateStates(automaton, tree.getRoot(), occ, i); - return std::make_tuple(res.first, res.second, occ); + std::pair < bool, std::set < State > > res = calculateStates ( automaton, tree.getRoot ( ), occ, i ); + + return std::make_tuple ( res.first, res.second, occ ); } // ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- diff --git a/alib2algo/src/automaton/run/Run.h b/alib2algo/src/automaton/run/Run.h index 516339555ad2a54de70567caf044afa06f05cbcb..8f0a9d55bde574a9699ea07d9911862bc7e9c009 100644 --- a/alib2algo/src/automaton/run/Run.h +++ b/alib2algo/src/automaton/run/Run.h @@ -18,14 +18,14 @@ namespace automaton { namespace run { class Run { - static std::pair<bool, State> calculateState(const automaton::DFTA & automaton, const tree::RankedNode & node, std::set<unsigned> & occ, unsigned & i); - static std::pair<bool, std::set<State>> calculateStates(const automaton::NFTA& automaton, const tree::RankedNode & node, std::set<unsigned>& occ, unsigned & i); + static std::pair < bool, State > calculateState ( const automaton::DFTA & automaton, const tree::RankedNode & node, std::set < unsigned > & occ, unsigned & i ); + static std::pair < bool, std::set < State > > calculateStates ( const automaton::NFTA & automaton, const tree::RankedNode & node, std::set < unsigned > & occ, unsigned & i ); public: - static std::tuple<bool, State, std::set<unsigned>> calculateState(const automaton::DFA& automaton, const string::LinearString & string); - static std::tuple<bool, std::set<State>, std::set<unsigned>> calculateStates(const automaton::NFA& automaton, const string::LinearString& string); - static std::tuple<bool, State, std::set<unsigned>> calculateState(const automaton::DFTA& automaton, const tree::RankedTree & tree); - static std::tuple<bool, std::set<State>, std::set<unsigned>> calculateStates(const automaton::NFTA& automaton, const tree::RankedTree & tree); + static std::tuple < bool, State, std::set < unsigned > > calculateState ( const automaton::DFA & automaton, const string::LinearString & string ); + static std::tuple < bool, std::set < State >, std::set < unsigned > > calculateStates ( const automaton::NFA & automaton, const string::LinearString & string ); + static std::tuple < bool, State, std::set < unsigned > > calculateState ( const automaton::DFTA & automaton, const tree::RankedTree & tree ); + static std::tuple < bool, std::set < State >, std::set < unsigned > > calculateStates ( const automaton::NFTA & automaton, const tree::RankedTree & tree ); }; } /* namespace run */