From 1d04b9c8876a3421799b75578bb080f9e9243576 Mon Sep 17 00:00:00 2001 From: Jan Travnicek <Jan.Travnicek@fit.cvut.cz> Date: Mon, 5 May 2014 12:32:21 +0200 Subject: [PATCH] remove Transition and refactor UnknownTransition --- alib2/src/automaton/Transition.cpp | 45 ------- alib2/src/automaton/Transition.h | 58 --------- alib2/src/automaton/UnknownTransition.cpp | 145 ++++++++-------------- alib2/src/automaton/UnknownTransition.h | 76 ++++++++---- 4 files changed, 105 insertions(+), 219 deletions(-) delete mode 100644 alib2/src/automaton/Transition.cpp delete mode 100644 alib2/src/automaton/Transition.h diff --git a/alib2/src/automaton/Transition.cpp b/alib2/src/automaton/Transition.cpp deleted file mode 100644 index ee404b7352..0000000000 --- a/alib2/src/automaton/Transition.cpp +++ /dev/null @@ -1,45 +0,0 @@ -/* - * Transition.cpp - * - * Created on: Apr 16, 2013 - * Author: martin - */ - -#include "Transition.h" - -namespace automaton { - -Transition::Transition(const State& current, const State& next) : - from(current), to(next) { - -} - -Transition::~Transition() { - -} -void Transition::setFrom(const State& state) { - from = state; -} - -const State& Transition::getFrom() const { - return from; -} - -void Transition::setTo(const State& state) { - to = state; -} - -const State& Transition::getTo() const { - return to; -} - -bool Transition::containsState(const State& state) const { - return from == state || to == state; -} - -std::ostream& operator<<(std::ostream& out, const Transition& transition) { - transition >> out; - return out; -} - -} /* namespace automaton */ diff --git a/alib2/src/automaton/Transition.h b/alib2/src/automaton/Transition.h deleted file mode 100644 index b89f3b0c44..0000000000 --- a/alib2/src/automaton/Transition.h +++ /dev/null @@ -1,58 +0,0 @@ -/* - * Transition.h - * - * Created on: Apr 16, 2013 - * Author: martin - */ - -#ifndef TRANSITION_H_ -#define TRANSITION_H_ - -#include "State.h" - -namespace automaton { - -/** - * Abstract base class for all transitions. Contains common elements of transitions. - */ -class Transition { -protected: - State from; - State to; -public: - Transition(const State& current, const State& next); - virtual ~Transition(); - - /** - * @param state State from which the transition goes - */ - void setFrom(const State& state); - - /** - * @return State form which the transition goes - */ - const State& getFrom() const; - - /** - * @param state State to which the transition goes - */ - void setTo(const State& state); - - /** - * @return State to which the transition goes - */ - const State& getTo() const; - - /** - * Determines whether State is used in this transition either as from state or to state. - * @return true when transition contains the state, false otherwise - */ - bool containsState(const State& state) const; - - friend std::ostream& operator<<(std::ostream&, const Transition&); - - virtual std::ostream& operator>>(std::ostream& out) const = 0; -}; - -} /* namespace automaton */ -#endif /* TRANSITION_H_ */ diff --git a/alib2/src/automaton/UnknownTransition.cpp b/alib2/src/automaton/UnknownTransition.cpp index 8543dccb7f..8ecbe847a1 100644 --- a/alib2/src/automaton/UnknownTransition.cpp +++ b/alib2/src/automaton/UnknownTransition.cpp @@ -9,42 +9,63 @@ namespace automaton { -using namespace std; +UnknownTransition::UnknownTransition(const State& from, const State& to, const std::list<alphabet::Symbol>& pop, const std::list<alphabet::Symbol>& push, const alphabet::Symbol& input, const alphabet::Symbol& output, Shift shift) : + from(from), to(to), pop(pop), push(push), input(input), output(output), shift(shift) { -UnknownTransition::UnknownTransition() : - Transition(State(""), State("")), output(""), input("") { - shift = NOT_SET; } -const list<Symbol>& UnknownTransition::getPop() const { +UnknownTransition::~UnknownTransition() { + +} +void UnknownTransition::setFrom(const State& state) { + from = state; +} + +const State& UnknownTransition::getFrom() const { + return from; +} + +void UnknownTransition::setTo(const State& state) { + to = state; +} + +const State& UnknownTransition::getTo() const { + return to; +} + +void UnknownTransition::setPop(const std::list<alphabet::Symbol>& pop) { + this->pop = pop; +} + +const std::list<alphabet::Symbol>& UnknownTransition::getPop() const { return pop; } -void UnknownTransition::addPop(const Symbol& symbol) { +void UnknownTransition::addPop(const alphabet::Symbol& symbol) { pop.push_back(symbol); } -const list<Symbol>& UnknownTransition::getPush() const { +const std::list<alphabet::Symbol>& UnknownTransition::getPush() const { return push; } -void UnknownTransition::addPush(const Symbol& symbol) { +void UnknownTransition::addPush(const alphabet::Symbol& symbol) { push.push_back(symbol); } -const Symbol& UnknownTransition::getOutput() const { +const alphabet::Symbol& UnknownTransition::getOutput() const { return output; } -void UnknownTransition::setOutput(const Symbol& symbol) { +void UnknownTransition::setOutput(const alphabet::Symbol& symbol) { output = symbol; } -const Symbol& UnknownTransition::getInput() const { +const alphabet::Symbol& UnknownTransition::getInput() const { return output; } -void UnknownTransition::setInput(const Symbol& symbol) { +void UnknownTransition::setInput(const alphabet::Symbol& symbol) { output = symbol; } @@ -61,116 +82,52 @@ bool UnknownTransition::operator <(const UnknownTransition& other) const { return from < other.from; } else if (input != other.input) { return input < other.input; + } else if (pop != other.pop) { + return pop < other.pop; + } else if (shift != other.shift) { + return shift < other.shift; } else if (to != other.to) { return to < other.to; } else if (output != other.output) { return output < other.output; - } else if (shift != other.shift) { - return shift < other.shift; - } - - std::list<Symbol>::const_iterator it; - std::list<Symbol>::const_iterator it2; - - //compare pop size - if (pop.size() != other.pop.size()) - return pop.size() < other.pop.size(); - - //compare pop content - it = pop.begin(); - it2 = other.pop.begin(); - while (it != pop.end()) { - if ((*it) != (*it2)) - return (*it) < (*it2); - it++; - it2++; - } - - //compare push size - if (push.size() != other.push.size()) - return push.size() < other.push.size(); - - //compare push content - it = push.begin(); - it2 = other.push.begin(); - while (it != push.end()) { - if ((*it) != (*it2)) - return (*it) < (*it2); - it++; - it2++; + } else { + return push < other.push; } - - return false; } bool UnknownTransition::operator ==(const UnknownTransition& other) const { - if (from == other.from && input == other.input && to == other.to && output == other.output - && shift == other.shift) { - - //compare pop size - if (pop.size() != other.pop.size()) - return false; - //compare push size - if (push.size() != other.push.size()) - return false; - - std::list<Symbol>::const_iterator it; - std::list<Symbol>::const_iterator it2; - - //compare pop content - it = pop.begin(); - it2 = other.pop.begin(); - while (it != pop.end()) { - if ((*it) != (*it2)) - return false; - it++; - it2++; - } - - //compare push content - it = push.begin(); - it2 = other.push.begin(); - while (it != push.end()) { - if ((*it) != (*it2)) - return false; - it++; - it2++; - } - return true; - } else { - return false; - } + return from == other.from && input == other.input && pop == other.pop && shift == other.shift && to == other.to && output == other.output && push == other.push; } bool UnknownTransition::operator !=(const UnknownTransition& other) const { - return !((*this) == other); + return !(*this == other); } -std::ostream& UnknownTransition::operator>>(std::ostream& out) const { +std::ostream& operator<<(std::ostream& out, const UnknownTransition& transition) { bool first; - out << "UnknownTransition from = " << this->from - << " to = " << this->to - << " input = " << this->input - << " output = " << this->output + out << "UnknownTransition from = " << transition.from + << " to = " << transition.to + << " input = " << transition.input + << " output = " << transition.output << " pop = ["; first = true; - for(list<Symbol>::const_iterator iter = this->pop.begin(); iter != this->pop.end(); iter++) { + for(std::list<alphabet::Symbol>::const_iterator iter = transition.pop.begin(); iter != transition.pop.end(); iter++) { if(!first) out << ", "; first = false; out << *iter; } out << "] push = ["; - + first = true; - for(list<Symbol>::const_iterator iter = this->push.begin(); iter != this->push.end(); iter++) { + for(std::list<alphabet::Symbol>::const_iterator iter = transition.push.begin(); iter != transition.push.end(); iter++) { if(!first) out << ", "; first = false; out << *iter; } - out << "] shift = " << SHIFT_NAMES[this->shift]; - + out << "] shift = " << SHIFT_NAMES[transition.shift]; + return out; } diff --git a/alib2/src/automaton/UnknownTransition.h b/alib2/src/automaton/UnknownTransition.h index 18ba5b0d21..e126f54a2a 100644 --- a/alib2/src/automaton/UnknownTransition.h +++ b/alib2/src/automaton/UnknownTransition.h @@ -5,79 +5,110 @@ * Author: Martin Zak */ -#ifndef UNKNOWNTRANSITION_H_ -#define UNKNOWNTRANSITION_H_ +#ifndef UNKNOWN_TRANSITION_H_ +#define UNKNOWN_TRANSITION_H_ #include <list> -#include "Transition.h" #include "State.h" #include "../alphabet/Symbol.h" #include "Shift.h" namespace automaton { -using namespace std; -using namespace alphabet; - /** * Class representing unknown transition parsed from XML. Part of UnknownAutomaton. */ -class UnknownTransition: public Transition { +class UnknownTransition { protected: - list<Symbol> pop; - list<Symbol> push; + State from; + State to; + + std::list<alphabet::Symbol> pop; + std::list<alphabet::Symbol> push; + + alphabet::Symbol input; + alphabet::Symbol output; - Symbol output; - Symbol input; Shift shift; public: - UnknownTransition(); + UnknownTransition(const State& from, const State& to, const std::list<alphabet::Symbol>& pop, const std::list<alphabet::Symbol>& push, const alphabet::Symbol& input, const alphabet::Symbol& output, Shift shift); + ~UnknownTransition(); + + /** + * @param state State from which the transition goes + */ + void setFrom(const State& state); + + /** + * @return State form which the transition goes + */ + const State& getFrom() const; + + /** + * @param state State to which the transition goes + */ + void setTo(const State& state); + + /** + * @return State to which the transition goes + */ + const State& getTo() const; + + /** + * @param pop list of symbols that are popped from the stack + */ + void setPop(const std::list<alphabet::Symbol>& pop); /** * @return list of symbols that are popped from the stack */ - const list<Symbol>& getPop() const; + const std::list<alphabet::Symbol>& getPop() const; /** * Add symbol to the end of the list of symbols that are popped from the stack. * @param symbol Symbol to add */ - void addPop(const Symbol& symbol); + void addPop(const alphabet::Symbol& symbol); + + /** + * @param push list of symbols that are pushed to the stack + */ + void setPush(std::list<alphabet::Symbol>& push); /** * @return list of symbols that are pushed to the stack */ - const list<Symbol>& getPush() const; + const std::list<alphabet::Symbol>& getPush() const; /** * Add symbol to the end of the list of symbols that are pushed to the stack. * @param symbol Symbol to add */ - void addPush(const Symbol& symbol); + void addPush(const alphabet::Symbol& symbol); /** * @return the output symbol of the transition */ - const Symbol& getOutput() const; + const alphabet::Symbol& getInput() const; /** * Sets the output Symbol of the transition. * @param symbol Symbol to set */ - void setOutput(const Symbol& symbol); + void setInput(const alphabet::Symbol& symbol); /** * @return the output symbol of the transition */ - const Symbol& getInput() const; + const alphabet::Symbol& getOutput() const; /** * Sets the output Symbol of the transition. * @param symbol Symbol to set */ - void setInput(const Symbol& symbol); + void setOutput(const alphabet::Symbol& symbol); /** * @return direction of movement of the reading head @@ -94,8 +125,9 @@ public: bool operator ==(const UnknownTransition& other) const; bool operator !=(const UnknownTransition& other) const; - virtual std::ostream& operator>>(std::ostream&) const; + friend std::ostream& operator<<(std::ostream&, const UnknownTransition&); }; } /* namespace automaton */ -#endif /* UNKNOWNTRANSITION_H_ */ + +#endif /* UNKNOWN_TRANSITION_H_ */ -- GitLab