diff --git a/alib2data/src/regexp/common/RegExpAlphabet.cpp b/alib2data/src/regexp/common/RegExpAlphabet.cpp new file mode 100644 index 0000000000000000000000000000000000000000..cc2ef9333f361b53f17c30bab5aa0a93e383f12c --- /dev/null +++ b/alib2data/src/regexp/common/RegExpAlphabet.cpp @@ -0,0 +1,45 @@ +/* + * RegExpAlphabet.cpp + * + * Created on: Apr 16, 2013 + * Author: Jan Travnicek + */ + +#include "RegExpAlphabet.h" + +#include <algorithm> + +namespace regexp { + +const std::set<alphabet::Symbol>& RegExpAlphabet::getAlphabet() const { + return alphabet; +} + +bool RegExpAlphabet::addSymbolToAlphabet(const alphabet::Symbol& symbol) { + return alphabet.insert(symbol).second; +} + +void RegExpAlphabet::addSymbolsToAlphabet(const std::set<alphabet::Symbol>& symbols) { + for(const alphabet::Symbol& addedSymbol : symbols) { + addSymbolToAlphabet(addedSymbol); + } +} + +void RegExpAlphabet::setAlphabet(const std::set<alphabet::Symbol>& newSymbols) { + std::set<alphabet::Symbol> removed; + std::set_difference(alphabet.begin(), alphabet.end(), newSymbols.begin(), newSymbols.end(), std::inserter(removed, removed.end())); + + std::set<alphabet::Symbol> added; + std::set_difference(newSymbols.begin(), newSymbols.end(), alphabet.begin(), alphabet.end(), std::inserter(added, added.end())); + + for(const alphabet::Symbol& removedSymbol : removed) { + removeSymbolFromAlphabet(removedSymbol); + } + + for(const alphabet::Symbol& addedSymbol : added) { + addSymbolToAlphabet(addedSymbol); + } +} + +} /* namespace regexp */ + diff --git a/alib2data/src/regexp/common/RegExpAlphabet.h b/alib2data/src/regexp/common/RegExpAlphabet.h new file mode 100644 index 0000000000000000000000000000000000000000..6c87ae3eaf4e0207908c5a5c802392875f63e261 --- /dev/null +++ b/alib2data/src/regexp/common/RegExpAlphabet.h @@ -0,0 +1,61 @@ +/* + * RegExpAlphabet.h + * + * Created on: Apr 10, 2013 + * Author: Jan Travnicek + */ + +#ifndef REG_EXP_ALPHABET_H_ +#define REG_EXP_ALPHABET_H_ + +#include <set> +#include "../../alphabet/Symbol.h" + + +namespace regexp { + +/** + * Abstract base class for all strings. Contains common elements of strings. + */ +class RegExpAlphabet { +protected: + std::set<alphabet::Symbol> alphabet; + +public: + /** + * Adds input symbol to input alphabet. + * @param symbol Symbol to add + */ + bool addSymbolToAlphabet(const alphabet::Symbol& symbol); + + /** + * Adds input symbols to input alphabet. + * @param symbols Symbol to add + */ + void addSymbolsToAlphabet(const std::set<alphabet::Symbol>& symbols); + + /** + * Sets input symbols of the regexp. + * @param symbols Symbols to set + */ + void setAlphabet(const std::set<alphabet::Symbol>& symbols); + + /** + * Removes input symbol from the input alphabet. + * @param symbol Symbol to remove + * @throws AutomatonException when symbol is not present in input alphabet + * or when symbol is part of the transition + */ + virtual bool removeSymbolFromAlphabet(const alphabet::Symbol& symbol) = 0; + + /** + * @return the input alphabet + */ + const std::set<alphabet::Symbol>& getAlphabet() const; + +}; + +} /* namespace regexp */ + +#endif /* REG_EXP_ALPHABET_H_ */ + diff --git a/alib2data/src/regexp/formal/FormalRegExp.cpp b/alib2data/src/regexp/formal/FormalRegExp.cpp index c5911cf80e4f7e126fa18aad08c3264e7d8de836..5ee5062e11ebd31996a3c8d571429ec3652d43ee 100644 --- a/alib2data/src/regexp/formal/FormalRegExp.cpp +++ b/alib2data/src/regexp/formal/FormalRegExp.cpp @@ -23,19 +23,22 @@ FormalRegExp::FormalRegExp() { this->regExp = new FormalRegExpEmpty(); } -FormalRegExp::FormalRegExp(const UnboundedRegExp& other) : alphabet(other.getAlphabet()) { +FormalRegExp::FormalRegExp(const UnboundedRegExp& other) { + alphabet = other.getAlphabet(); this->regExp = NULL; FormalRegExpElement* element = other.getRegExp().cloneAsFormal(); setRegExp(std::move(*element)); delete element; } -FormalRegExp::FormalRegExp(const std::set<alphabet::Symbol>& alphabet, const FormalRegExpElement& regExp) : alphabet(alphabet) { +FormalRegExp::FormalRegExp(const std::set<alphabet::Symbol>& alphabet, const FormalRegExpElement& regExp) { + this->alphabet = alphabet; this->regExp = NULL; setRegExp(regExp); } -FormalRegExp::FormalRegExp(std::set<alphabet::Symbol>&& alphabet, FormalRegExpElement&& regExp) : alphabet(std::move(alphabet)) { +FormalRegExp::FormalRegExp(std::set<alphabet::Symbol>&& alphabet, FormalRegExpElement&& regExp) { + this->alphabet = std::move(alphabet); this->regExp = NULL; setRegExp(std::move(regExp)); } @@ -52,11 +55,13 @@ FormalRegExp::FormalRegExp(FormalRegExpElement&& regExp) { setRegExp(std::move(regExp)); } -FormalRegExp::FormalRegExp(const FormalRegExp& other) : regExp(other.regExp->clone()), alphabet(other.alphabet) { +FormalRegExp::FormalRegExp(const FormalRegExp& other) : regExp(other.regExp->clone()) { + alphabet = other.alphabet; this->regExp->attachRegExp(this); } -FormalRegExp::FormalRegExp(FormalRegExp&& other) noexcept : regExp(other.regExp), alphabet(std::move(other.alphabet) ) { +FormalRegExp::FormalRegExp(FormalRegExp&& other) noexcept : regExp(other.regExp) { + alphabet = std::move(other.alphabet); this->regExp->attachRegExp(this); other.regExp = NULL; } @@ -111,26 +116,6 @@ void FormalRegExp::setRegExp(FormalRegExpElement&& regExp) { throw exception::AlibException("Input symbols not in the alphabet."); } -const std::set<alphabet::Symbol>& FormalRegExp::getAlphabet() const { - return alphabet; -} - -bool FormalRegExp::addSymbolToAlphabet(const alphabet::Symbol & symbol) { - return alphabet.insert(symbol).second; -} - -void FormalRegExp::setAlphabet(const std::set<alphabet::Symbol> & symbols) { - std::set<alphabet::Symbol> minimalAlphabet; - this->regExp->computeMinimalAlphabet(minimalAlphabet); - std::set<alphabet::Symbol> removedSymbols; - std::set_difference(minimalAlphabet.begin(), minimalAlphabet.end(), symbols.begin(), symbols.end(), std::inserter(removedSymbols, removedSymbols.end())); - - if(removedSymbols.size() > 0) - throw exception::AlibException("Input symbols are used."); - - this->alphabet = symbols; -} - bool FormalRegExp::removeSymbolFromAlphabet(const alphabet::Symbol & symbol) { if(this->regExp->testSymbol(symbol)) throw exception::AlibException("Input symbol \"" + (std::string) symbol + "\" is used."); diff --git a/alib2data/src/regexp/formal/FormalRegExp.h b/alib2data/src/regexp/formal/FormalRegExp.h index 7a283401c769eb30d152c07dbf87c56ee04d788e..f8d705d9f61978ac86ccdbfd6899d965a96ce6f8 100644 --- a/alib2data/src/regexp/formal/FormalRegExp.h +++ b/alib2data/src/regexp/formal/FormalRegExp.h @@ -15,6 +15,7 @@ #include "FormalRegExpElement.h" #include "../../std/visitor.hpp" #include "../RegExpBase.h" +#include "../common/RegExpAlphabet.h" namespace regexp { @@ -24,12 +25,10 @@ class FormalRegExpElement; * Represents regular expression parsed from the XML. Regular expression is stored * as a tree of RegExpElement. */ -class FormalRegExp : public std::acceptor<FormalRegExp, VisitableRegExpBase, std::acceptor<FormalRegExp, alib::VisitableObjectBase, RegExpBase> > { +class FormalRegExp : public std::acceptor<FormalRegExp, VisitableRegExpBase, std::acceptor<FormalRegExp, alib::VisitableObjectBase, RegExpBase> >, public RegExpAlphabet { protected: FormalRegExpElement* regExp; - std::set<alphabet::Symbol> alphabet; - public: /** * @copydoc FormalRegExpElement::clone() const @@ -81,24 +80,6 @@ public: */ void setRegExp(FormalRegExpElement&& regExp); - /** - * Gets alphabet symbols used in RegExp. - * @return set of alphabet symbols used in regexp. - */ - const std::set<alphabet::Symbol>& getAlphabet() const; - - /** - * Adds symbol to the alphabet available in the regular expression - * @param symbol new symbol added to the alphabet - */ - bool addSymbolToAlphabet(const alphabet::Symbol & symbol); - - /** - * Sets the alphabet of the regular expression - * @param symbols new alphabet - */ - void setAlphabet(const std::set<alphabet::Symbol>& symbols); - /** * Removes symbol from the alphabet of symbol available in the regular expression * @param symbol removed symbol from the alphabet diff --git a/alib2data/src/regexp/unbounded/UnboundedRegExp.cpp b/alib2data/src/regexp/unbounded/UnboundedRegExp.cpp index c9ad463d311b3cec90c341471b05a613fbe0fc5a..cd3f15d6a56c93948e60e3cd5fbcc228bcb99ec4 100644 --- a/alib2data/src/regexp/unbounded/UnboundedRegExp.cpp +++ b/alib2data/src/regexp/unbounded/UnboundedRegExp.cpp @@ -23,19 +23,22 @@ UnboundedRegExp::UnboundedRegExp() { this->regExp = new UnboundedRegExpEmpty(); } -UnboundedRegExp::UnboundedRegExp(const FormalRegExp& other) : alphabet(other.getAlphabet()) { +UnboundedRegExp::UnboundedRegExp(const FormalRegExp& other) { + this->alphabet = other.getAlphabet(); this->regExp = NULL; UnboundedRegExpElement* element = other.getRegExp().cloneAsUnbounded(); setRegExp(std::move(*element)); delete element; } -UnboundedRegExp::UnboundedRegExp(const std::set<alphabet::Symbol>& alphabet, const UnboundedRegExpElement& regExp) : alphabet(alphabet) { +UnboundedRegExp::UnboundedRegExp(const std::set<alphabet::Symbol>& alphabet, const UnboundedRegExpElement& regExp) { + this->alphabet = alphabet; this->regExp = NULL; setRegExp(regExp); } -UnboundedRegExp::UnboundedRegExp(std::set<alphabet::Symbol>&& alphabet, UnboundedRegExpElement&& regExp) : alphabet(std::move(alphabet)) { +UnboundedRegExp::UnboundedRegExp(std::set<alphabet::Symbol>&& alphabet, UnboundedRegExpElement&& regExp) { + this->alphabet = std::move(alphabet); this->regExp = NULL; setRegExp(std::move(regExp)); } @@ -52,11 +55,13 @@ UnboundedRegExp::UnboundedRegExp(UnboundedRegExpElement&& regExp) { setRegExp(std::move(regExp)); } -UnboundedRegExp::UnboundedRegExp(const UnboundedRegExp& other) : regExp(other.regExp->clone()), alphabet(other.alphabet) { +UnboundedRegExp::UnboundedRegExp(const UnboundedRegExp& other) : regExp(other.regExp->clone()) { + alphabet = other.getAlphabet(); this->regExp->attachRegExp(this); } -UnboundedRegExp::UnboundedRegExp(UnboundedRegExp&& other) noexcept : regExp(other.regExp), alphabet(std::move(other.alphabet) ) { +UnboundedRegExp::UnboundedRegExp(UnboundedRegExp&& other) noexcept : regExp(other.regExp) { + alphabet = std::move(other.alphabet); this->regExp->attachRegExp(this); other.regExp = NULL; } @@ -111,30 +116,10 @@ void UnboundedRegExp::setRegExp(UnboundedRegExpElement&& regExp) { throw exception::AlibException("Input symbols not in the alphabet."); } -const std::set<alphabet::Symbol>& UnboundedRegExp::getAlphabet() const { - return alphabet; -} - -bool UnboundedRegExp::addSymbolToAlphabet(const alphabet::Symbol & symbol) { - return alphabet.insert(symbol).second; -} - -void UnboundedRegExp::setAlphabet(const std::set<alphabet::Symbol> & symbols) { - std::set<alphabet::Symbol> minimalAlphabet; - this->regExp->computeMinimalAlphabet(minimalAlphabet); - std::set<alphabet::Symbol> removedSymbols; - std::set_difference(minimalAlphabet.begin(), minimalAlphabet.end(), symbols.begin(), symbols.end(), std::inserter(removedSymbols, removedSymbols.end())); - - if(removedSymbols.size() > 0) - throw exception::AlibException("Input symbols are used."); - - this->alphabet = symbols; -} - bool UnboundedRegExp::removeSymbolFromAlphabet(const alphabet::Symbol & symbol) { if(this->regExp->testSymbol(symbol)) throw exception::AlibException("Input symbol \"" + (std::string) symbol + "\" is used."); - + return alphabet.erase(symbol); } diff --git a/alib2data/src/regexp/unbounded/UnboundedRegExp.h b/alib2data/src/regexp/unbounded/UnboundedRegExp.h index 63c6f448388e7b78a764b1718cb8fd80b2430269..82f551be2f7699bc99e6a27c58b12770c3155ba4 100644 --- a/alib2data/src/regexp/unbounded/UnboundedRegExp.h +++ b/alib2data/src/regexp/unbounded/UnboundedRegExp.h @@ -15,6 +15,7 @@ #include "UnboundedRegExpElement.h" #include "../../std/visitor.hpp" #include "../RegExpBase.h" +#include "../common/RegExpAlphabet.h" namespace regexp { @@ -24,18 +25,16 @@ class UnboundedRegExpElement; * Represents regular expression parsed from the XML. Regular expression is stored * as a tree of RegExpElement. */ -class UnboundedRegExp : public std::acceptor<UnboundedRegExp, VisitableRegExpBase, std::acceptor<UnboundedRegExp, alib::VisitableObjectBase, RegExpBase> > { +class UnboundedRegExp : public std::acceptor<UnboundedRegExp, VisitableRegExpBase, std::acceptor<UnboundedRegExp, alib::VisitableObjectBase, RegExpBase> >, public RegExpAlphabet { protected: UnboundedRegExpElement* regExp; - - std::set<alphabet::Symbol> alphabet; public: /** * @copydoc UnboundedRegExpElement::clone() const */ virtual RegExpBase* clone() const; - + /** * @copydoc UnboundedRegExpElement::plunder() const */ @@ -62,7 +61,7 @@ public: * @return Root node of the regular expression tree */ const UnboundedRegExpElement& getRegExp() const; - + /** * @return Root node of the regular expression tree */ @@ -74,37 +73,19 @@ public: * @param regExp root node to set */ void setRegExp(const UnboundedRegExpElement& regExp); - + /** * Sets the root node of the regular expression tree * @param regExp root node to set */ void setRegExp(UnboundedRegExpElement&& regExp); - - /** - * Gets alphabet symbols used in RegExp. - * @return set of alphabet symbols used in regexp. - */ - const std::set<alphabet::Symbol>& getAlphabet() const; - - /** - * Adds symbol to the alphabet available in the regular expression - * @param symbol new symbol added to the alphabet - */ - bool addSymbolToAlphabet(const alphabet::Symbol & symbol); - /** - * Sets the alphabet of the regular expression - * @param symbols new alphabet - */ - void setAlphabet(const std::set<alphabet::Symbol>& symbols); - /** * Removes symbol from the alphabet of symbol available in the regular expression * @param symbol removed symbol from the alphabet */ bool removeSymbolFromAlphabet(const alphabet::Symbol & symbol); - + /** * Prints XML representation of the RegExp to the output stream. * @param out output stream to which print the RegExp diff --git a/alib2data/src/string/common/StringAlphabet.cpp b/alib2data/src/string/common/StringAlphabet.cpp index 22c34a2203a047422bcd5904890675a2cb7fe62b..9fb8e517412e3c54f166c270630f94b823397767 100644 --- a/alib2data/src/string/common/StringAlphabet.cpp +++ b/alib2data/src/string/common/StringAlphabet.cpp @@ -1,5 +1,5 @@ /* - * InputAlphabet.cpp + * StringAlphabet.cpp * * Created on: Apr 16, 2013 * Author: Jan Travnicek diff --git a/alib2data/src/string/common/StringAlphabet.h b/alib2data/src/string/common/StringAlphabet.h index 6bb3fffe39d37ffbdcb9150900acd3b493bc5618..d8807428441cc4ea0d88aa78088005146ae21319 100644 --- a/alib2data/src/string/common/StringAlphabet.h +++ b/alib2data/src/string/common/StringAlphabet.h @@ -1,5 +1,5 @@ /* - * Alphabet.h + * StringAlphabet.h * * Created on: Apr 10, 2013 * Author: Jan Travnicek