diff --git a/alib2/src/automaton/PDA/NPDA.cpp b/alib2/src/automaton/PDA/NPDA.cpp index 9225975116326e3e923f7fa8f15ba267d5b6afa0..0d7aff51ac0b70453bc0d389071673742b534cfc 100644 --- a/alib2/src/automaton/PDA/NPDA.cpp +++ b/alib2/src/automaton/PDA/NPDA.cpp @@ -70,13 +70,13 @@ bool NPDA::removeStackSymbol(const alphabet::Symbol& symbol) { return stackAlphabet.erase(symbol); } -bool NPDA::addTransition(const State& from, const alphabet::Symbol& input, const std::vector<alphabet::Symbol>& pop, const State& to, const std::vector<alphabet::Symbol>& push) { +bool NPDA::addTransition(const State& from, const std::variant<string::Epsilon, alphabet::Symbol>& input, const std::vector<alphabet::Symbol>& pop, const State& to, const std::vector<alphabet::Symbol>& push) { if (states.find(from) == states.end()) { throw AutomatonException("State \"" + (std::string) from.getName() + "\" doesn't exist."); } - if (inputAlphabet.find(input) == inputAlphabet.end()) { - throw AutomatonException("Input symbol \"" + (std::string) input + "\" doesn't exist."); + if (input.is<alphabet::Symbol>() && inputAlphabet.find(input.get<alphabet::Symbol>()) == inputAlphabet.end()) { + throw AutomatonException("Input symbol \"" + (std::string) input.get<alphabet::Symbol>() + "\" doesn't exist."); } if (states.find(to) == states.end()) { @@ -95,25 +95,43 @@ bool NPDA::addTransition(const State& from, const alphabet::Symbol& input, const } } - std::variant<string::Epsilon, alphabet::Symbol> inputVariant; - inputVariant.set<alphabet::Symbol>(input); - std::tuple<State, std::variant<string::Epsilon, alphabet::Symbol>, std::vector<alphabet::Symbol> > key(from, inputVariant, pop); - + std::tuple<State, std::variant<string::Epsilon, alphabet::Symbol>, std::vector<alphabet::Symbol> > key(from, input, pop); std::pair<automaton::State, std::vector<alphabet::Symbol> > value = std::make_pair(to, push); return transitions[key].insert(value).second; } -bool NPDA::removeTransition(const State& from, const alphabet::Symbol& input, const std::vector<alphabet::Symbol>& pop, const State& to, const std::vector<alphabet::Symbol>& push) { +bool NPDA::addTransition(const State& from, const alphabet::Symbol& input, const std::vector<alphabet::Symbol>& pop, const State& to, const std::vector<alphabet::Symbol>& push) { std::variant<string::Epsilon, alphabet::Symbol> inputVariant; inputVariant.set<alphabet::Symbol>(input); - std::tuple<State, std::variant<string::Epsilon, alphabet::Symbol>, std::vector<alphabet::Symbol> > key(from, inputVariant, pop); - + return addTransition(from, inputVariant, pop, to, push); +} + +bool NPDA::addTransition(const State& from, const std::vector<alphabet::Symbol>& pop, const State& to, const std::vector<alphabet::Symbol>& push) { + std::variant<string::Epsilon, alphabet::Symbol> inputVariant; + inputVariant.set<string::Epsilon>(string::Epsilon::EPSILON); + return addTransition(from, inputVariant, pop, to, push); +} + +bool NPDA::removeTransition(const State& from, const std::variant<string::Epsilon, alphabet::Symbol>& input, const std::vector<alphabet::Symbol>& pop, const State& to, const std::vector<alphabet::Symbol>& push) { + std::tuple<State, std::variant<string::Epsilon, alphabet::Symbol>, std::vector<alphabet::Symbol> > key(from, input, pop); std::pair<automaton::State, std::vector<alphabet::Symbol> > value = std::make_pair(to, push); return transitions[key].erase(value); } +bool NPDA::removeTransition(const State& from, const alphabet::Symbol& input, const std::vector<alphabet::Symbol>& pop, const State& to, const std::vector<alphabet::Symbol>& push) { + std::variant<string::Epsilon, alphabet::Symbol> inputVariant; + inputVariant.set<alphabet::Symbol>(input); + return removeTransition(from, inputVariant, pop, to, push); +} + +bool NPDA::removeTransition(const State& from, const std::vector<alphabet::Symbol>& pop, const State& to, const std::vector<alphabet::Symbol>& push) { + std::variant<string::Epsilon, alphabet::Symbol> inputVariant; + inputVariant.set<string::Epsilon>(string::Epsilon::EPSILON); + return removeTransition(from, inputVariant, pop, to, push); +} + const std::map<std::tuple<State, std::variant<string::Epsilon, alphabet::Symbol>, std::vector<alphabet::Symbol> >, std::set<std::pair<State, std::vector<alphabet::Symbol> > > >& NPDA::getTransitions() const { return transitions; } diff --git a/alib2/src/automaton/PDA/NPDA.h b/alib2/src/automaton/PDA/NPDA.h index ec46075001be390f20068808c2038dd539d0de8d..693cb0242df6b270da682d3dde42bd0626fea021 100644 --- a/alib2/src/automaton/PDA/NPDA.h +++ b/alib2/src/automaton/PDA/NPDA.h @@ -53,15 +53,23 @@ public: * @throws AutomatonException when some part of the transition is not present * in the NPDA (state, input symbol, stack symbol) or when transition already exists */ + bool addTransition(const State& from, const std::variant<string::Epsilon, alphabet::Symbol>& input, const std::vector<alphabet::Symbol>& pop, const State& to, const std::vector<alphabet::Symbol>& push); + bool addTransition(const State& from, const alphabet::Symbol& input, const std::vector<alphabet::Symbol>& pop, const State& to, const std::vector<alphabet::Symbol>& push); + bool addTransition(const State& from, const std::vector<alphabet::Symbol>& pop, const State& to, const std::vector<alphabet::Symbol>& push); + /** * Removes the transition from the NPDA. * @param transition transition to remove * @throws AutomatonException when transition is not present in the NPDA */ + bool removeTransition(const State& from, const std::variant<string::Epsilon, alphabet::Symbol>& input, const std::vector<alphabet::Symbol>& pop, const State& to, const std::vector<alphabet::Symbol>& push); + bool removeTransition(const State& from, const alphabet::Symbol& input, const std::vector<alphabet::Symbol>& pop, const State& to, const std::vector<alphabet::Symbol>& push); + bool removeTransition(const State& from, const std::vector<alphabet::Symbol>& pop, const State& to, const std::vector<alphabet::Symbol>& push); + /** * @return NPDA transitions */ diff --git a/alib2/src/automaton/PDA/SinglePopNPDA.cpp b/alib2/src/automaton/PDA/SinglePopNPDA.cpp index daf9ec411c6eb9ad2828d1a7de0df73d8d190f51..eafeef82122fd568d3fea86e2afaa913a210b8ba 100644 --- a/alib2/src/automaton/PDA/SinglePopNPDA.cpp +++ b/alib2/src/automaton/PDA/SinglePopNPDA.cpp @@ -68,13 +68,13 @@ bool SinglePopNPDA::removeStackSymbol(const alphabet::Symbol& symbol) { return stackAlphabet.erase(symbol); } -bool SinglePopNPDA::addTransition(const State& from, const alphabet::Symbol& input, const alphabet::Symbol& pop, const State& to, const std::vector<alphabet::Symbol>& push) { +bool SinglePopNPDA::addTransition(const State& from, const std::variant<string::Epsilon, alphabet::Symbol>& input, const alphabet::Symbol& pop, const State& to, const std::vector<alphabet::Symbol>& push) { if (states.find(from) == states.end()) { throw AutomatonException("State \"" + (std::string) from.getName() + "\" doesn't exist."); } - if (inputAlphabet.find(input) == inputAlphabet.end()) { - throw AutomatonException("Input symbol \"" + (std::string) input + "\" doesn't exist."); + if (input.is<alphabet::Symbol>() && inputAlphabet.find(input.get<alphabet::Symbol>()) == inputAlphabet.end()) { + throw AutomatonException("Input symbol \"" + (std::string) input.get<alphabet::Symbol>() + "\" doesn't exist."); } if (states.find(to) == states.end()) { @@ -91,25 +91,43 @@ bool SinglePopNPDA::addTransition(const State& from, const alphabet::Symbol& inp } } - std::variant<string::Epsilon, alphabet::Symbol> inputVariant; - inputVariant.set<alphabet::Symbol>(input); - std::tuple<State, std::variant<string::Epsilon, alphabet::Symbol>, alphabet::Symbol> key(from, inputVariant, pop); - + std::tuple<State, std::variant<string::Epsilon, alphabet::Symbol>, alphabet::Symbol> key(from, input, pop); std::pair<automaton::State, std::vector<alphabet::Symbol> > value = std::make_pair(to, push); return transitions[key].insert(value).second; } -bool SinglePopNPDA::removeTransition(const State& from, const alphabet::Symbol& input, const alphabet::Symbol& pop, const State& to, const std::vector<alphabet::Symbol>& push) { +bool SinglePopNPDA::addTransition(const State& from, const alphabet::Symbol& input, const alphabet::Symbol& pop, const State& to, const std::vector<alphabet::Symbol>& push) { std::variant<string::Epsilon, alphabet::Symbol> inputVariant; inputVariant.set<alphabet::Symbol>(input); - std::tuple<State, std::variant<string::Epsilon, alphabet::Symbol>, alphabet::Symbol> key(from, inputVariant, pop); - + return addTransition(from, inputVariant, pop, to, push); +} + +bool SinglePopNPDA::addTransition(const State& from, const alphabet::Symbol& pop, const State& to, const std::vector<alphabet::Symbol>& push) { + std::variant<string::Epsilon, alphabet::Symbol> inputVariant; + inputVariant.set<string::Epsilon>(string::Epsilon::EPSILON); + return addTransition(from, inputVariant, pop, to, push); +} + +bool SinglePopNPDA::removeTransition(const State& from, const std::variant<string::Epsilon, alphabet::Symbol>& input, const alphabet::Symbol& pop, const State& to, const std::vector<alphabet::Symbol>& push) { + std::tuple<State, std::variant<string::Epsilon, alphabet::Symbol>, alphabet::Symbol> key(from, input, pop); std::pair<automaton::State, std::vector<alphabet::Symbol> > value = std::make_pair(to, push); return transitions[key].erase(value); } +bool SinglePopNPDA::removeTransition(const State& from, const alphabet::Symbol& input, const alphabet::Symbol& pop, const State& to, const std::vector<alphabet::Symbol>& push) { + std::variant<string::Epsilon, alphabet::Symbol> inputVariant; + inputVariant.set<alphabet::Symbol>(input); + return removeTransition(from, inputVariant, pop, to, push); +} + +bool SinglePopNPDA::removeTransition(const State& from, const alphabet::Symbol& pop, const State& to, const std::vector<alphabet::Symbol>& push) { + std::variant<string::Epsilon, alphabet::Symbol> inputVariant; + inputVariant.set<string::Epsilon>(string::Epsilon::EPSILON); + return removeTransition(from, inputVariant, pop, to, push); +} + const std::map<std::tuple<State, std::variant<string::Epsilon, alphabet::Symbol>, alphabet::Symbol>, std::set<std::pair<State, std::vector<alphabet::Symbol> > > >& SinglePopNPDA::getTransitions() const { return transitions; } diff --git a/alib2/src/automaton/PDA/SinglePopNPDA.h b/alib2/src/automaton/PDA/SinglePopNPDA.h index d3cc5b1629a3dfae91cfbc4e8e3e8a3025686381..3c17c1437214dd0a30b28894c0df8a7f1b973db0 100644 --- a/alib2/src/automaton/PDA/SinglePopNPDA.h +++ b/alib2/src/automaton/PDA/SinglePopNPDA.h @@ -53,14 +53,22 @@ public: * @throws AutomatonException when some part of the transition is not present * in the SinglePopNPDA (state, input symbol, stack symbol) or when transition already exists */ + bool addTransition(const State& from, const std::variant<string::Epsilon, alphabet::Symbol>& input, const alphabet::Symbol& pop, const State& to, const std::vector<alphabet::Symbol>& push); + bool addTransition(const State& from, const alphabet::Symbol& input, const alphabet::Symbol& pop, const State& to, const std::vector<alphabet::Symbol>& push); + + bool addTransition(const State& from, const alphabet::Symbol& pop, const State& to, const std::vector<alphabet::Symbol>& push); /** * Removes the transition from the SinglePopNPDA. * @param transition transition to remove * @throws AutomatonException when transition is not present in the SinglePopNPDA */ + bool removeTransition(const State& from, const std::variant<string::Epsilon, alphabet::Symbol>& input, const alphabet::Symbol& pop, const State& to, const std::vector<alphabet::Symbol>& push); + bool removeTransition(const State& from, const alphabet::Symbol& input, const alphabet::Symbol& pop, const State& to, const std::vector<alphabet::Symbol>& push); + + bool removeTransition(const State& from, const alphabet::Symbol& pop, const State& to, const std::vector<alphabet::Symbol>& push); /** * @return SinglePopNPDA transitions