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

another addTransition method + simlifications

parent 45297d73
No related branches found
No related tags found
No related merge requests found
...@@ -51,43 +51,35 @@ void EpsilonNFA::removeInputSymbol(const alphabet::Symbol& symbol) { ...@@ -51,43 +51,35 @@ void EpsilonNFA::removeInputSymbol(const alphabet::Symbol& symbol) {
   
} }
   
void EpsilonNFA::addTransition(const State& from, const alphabet::Symbol& input, const State& to) { void EpsilonNFA::addTransition(const State& from, const std::variant<string::Epsilon, alphabet::Symbol>& input, const State& to) {
if (states.find(from) == states.end()) if (states.find(from) == states.end())
throw AutomatonException("State \"" + from.getName() + "\" doesn't exist."); throw AutomatonException("State \"" + from.getName() + "\" doesn't exist.");
   
if (inputAlphabet.find(input) == inputAlphabet.end()) if (input.is<alphabet::Symbol>() && inputAlphabet.find(input.get<alphabet::Symbol>()) == inputAlphabet.end())
throw AutomatonException("Input symbol \"" + input.getSymbol() + "\" doesn't exist."); throw AutomatonException("Input symbol \"" + input.get<alphabet::Symbol>().getSymbol() + "\" doesn't exist.");
   
if (states.find(to) == states.end()) if (states.find(to) == states.end())
throw AutomatonException("State \"" + to.getName() + "\" doesn't exist."); throw AutomatonException("State \"" + to.getName() + "\" doesn't exist.");
   
std::variant<string::Epsilon, alphabet::Symbol> inputVariant; std::pair<State, std::variant<string::Epsilon, alphabet::Symbol> > key = std::make_pair(from, input);
inputVariant.set<alphabet::Symbol>(input);
std::pair<State, std::variant<string::Epsilon, alphabet::Symbol> > key = std::make_pair(from, inputVariant);
std::pair<std::set<automaton::State>::iterator, bool> ret = transitions[key].insert(to); std::pair<std::set<automaton::State>::iterator, bool> ret = transitions[key].insert(to);
if (!ret.second) if (!ret.second)
throw AutomatonException( throw AutomatonException(
"Transition (\"" + from.getName() + "\", \"" + input.getSymbol() + "\") -> \"" + to.getName() "Transition (\"" + from.getName() + "\", \"" + input.get<alphabet::Symbol>().getSymbol() + "\") -> \"" + to.getName()
+ "\" already exists."); + "\" already exists.");
} }
   
void EpsilonNFA::addTransition(const State& from, const State& to) { void EpsilonNFA::addTransition(const State& from, const alphabet::Symbol& input, const State& to) {
if (states.find(from) == states.end()) std::variant<string::Epsilon, alphabet::Symbol> inputVariant;
throw AutomatonException("State \"" + from.getName() + "\" doesn't exist."); inputVariant.set<alphabet::Symbol>(input);
addTransition(from, inputVariant, to);
if (states.find(to) == states.end()) }
throw AutomatonException("State \"" + to.getName() + "\" doesn't exist.");
   
void EpsilonNFA::addTransition(const State& from, const State& to) {
std::variant<string::Epsilon, alphabet::Symbol> inputVariant; std::variant<string::Epsilon, alphabet::Symbol> inputVariant;
inputVariant.set<string::Epsilon>(string::Epsilon()); inputVariant.set<string::Epsilon>(string::Epsilon());
std::pair<State, std::variant<string::Epsilon, alphabet::Symbol> > key = std::make_pair(from, inputVariant); addTransition(from, inputVariant, to);
std::pair<std::set<automaton::State>::iterator, bool> ret = transitions[key].insert(to);
if (!ret.second)
throw AutomatonException(
"Transition (\"" + from.getName() + "\", \" \\eps \") -> \"" + to.getName()
+ "\" already exists.");
} }
   
void EpsilonNFA::removeTransition(const State& from, const alphabet::Symbol& input, const State& to) { void EpsilonNFA::removeTransition(const State& from, const alphabet::Symbol& input, const State& to) {
......
...@@ -47,7 +47,16 @@ public: ...@@ -47,7 +47,16 @@ public:
* @param next next state * @param next next state
* @throws AutomatonException when transition already exists or when transition contains state or symbol not present in the automaton * @throws AutomatonException when transition already exists or when transition contains state or symbol not present in the automaton
*/ */
void addTransition(const State& current, const alphabet::Symbol& input, const State& next); void addTransition(const State& from, const std::variant<string::Epsilon, alphabet::Symbol>& input, const State& to);
/**
* Adds transition defined by parameters to the automaton.
* @param current current state
* @param input input symbol
* @param next next state
* @throws AutomatonException when transition already exists or when transition contains state or symbol not present in the automaton
*/
void addTransition(const State& from, const alphabet::Symbol& input, const State& to);
   
/** /**
* Adds transition defined by parameters to the automaton. * Adds transition defined by parameters to the automaton.
...@@ -55,7 +64,7 @@ public: ...@@ -55,7 +64,7 @@ public:
* @param next next state * @param next next state
* @throws AutomatonException when transition already exists or when transition contains state or symbol not present in the automaton * @throws AutomatonException when transition already exists or when transition contains state or symbol not present in the automaton
*/ */
void addTransition(const State& current, const State& next); void addTransition(const State& from, const State& to);
   
/** /**
* Removes transition from the automaton. * Removes transition from the automaton.
......
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