diff --git a/alib2algo/src/regexp/transform/RegExpAlternate.cpp b/alib2algo/src/regexp/transform/RegExpAlternate.cpp index 5b4c75857f624b82418d68a46b88ae2051437d6e..990d1c3babdcbd8751dbdd8a95cd41131a3a1470 100644 --- a/alib2algo/src/regexp/transform/RegExpAlternate.cpp +++ b/alib2algo/src/regexp/transform/RegExpAlternate.cpp @@ -6,37 +6,14 @@ */ #include "RegExpAlternate.h" -#include "regexp/formal/FormalRegExpAlternation.h" -#include "regexp/unbounded/UnboundedRegExpAlternation.h" -namespace regexp -{ +namespace regexp { regexp::RegExp RegExpAlternate::alternate(const regexp::RegExp& first, const regexp::RegExp& second) { return dispatch(first.getData(), second.getData()); } -regexp::FormalRegExp < > RegExpAlternate::alternate(const regexp::FormalRegExp < > & first, const regexp::FormalRegExp < > & second) { - return regexp::FormalRegExp < > (RegExpAlternate::alternate(first.getRegExp(), second.getRegExp())); -} - auto RegExpAlternateFormalRegExp = RegExpAlternate::RegistratorWrapper<regexp::FormalRegExp < > , regexp::FormalRegExp < > >(RegExpAlternate::alternate); - -regexp::FormalRegExpStructure < alphabet::Symbol > RegExpAlternate::alternate(const regexp::FormalRegExpStructure < alphabet::Symbol > & first, const regexp::FormalRegExpStructure < alphabet::Symbol > & second) { - return regexp::FormalRegExpStructure < alphabet::Symbol > (regexp::FormalRegExpAlternation < alphabet::Symbol > (first.getStructure(), second.getStructure())); -} - -regexp::UnboundedRegExp < > RegExpAlternate::alternate(const regexp::UnboundedRegExp < > & first, const regexp::UnboundedRegExp < > & second) { - return regexp::UnboundedRegExp < > (RegExpAlternate::alternate(first.getRegExp(), second.getRegExp())); -} - auto RegExpAlternateUnboundedRegExp = RegExpAlternate::RegistratorWrapper<regexp::UnboundedRegExp < >, regexp::UnboundedRegExp < > >(RegExpAlternate::alternate); -regexp::UnboundedRegExpStructure < alphabet::Symbol > RegExpAlternate::alternate(const regexp::UnboundedRegExpStructure < alphabet::Symbol > & first, const regexp::UnboundedRegExpStructure < alphabet::Symbol > & second) { - regexp::UnboundedRegExpAlternation < alphabet::Symbol > con; - con.appendElement(first.getStructure()); - con.appendElement(second.getStructure()); - return regexp::UnboundedRegExpStructure < alphabet::Symbol > (con); -} - } /* namespace regexp */ diff --git a/alib2algo/src/regexp/transform/RegExpAlternate.h b/alib2algo/src/regexp/transform/RegExpAlternate.h index 72a1025120d42d96fa3e9e39d59be919a87596f9..8d2223ccbafde04ab87e6b096c26494e45bf5e91 100644 --- a/alib2algo/src/regexp/transform/RegExpAlternate.h +++ b/alib2algo/src/regexp/transform/RegExpAlternate.h @@ -14,6 +14,9 @@ #include <regexp/formal/FormalRegExp.h> #include <regexp/unbounded/UnboundedRegExp.h> +#include <regexp/formal/FormalRegExpAlternation.h> +#include <regexp/unbounded/UnboundedRegExpAlternation.h> + namespace regexp { /** @@ -24,12 +27,39 @@ class RegExpAlternate : public std::PromotingDoubleDispatch<RegExpAlternate, reg public: static regexp::RegExp alternate(const regexp::RegExp& first, const regexp::RegExp& second); - static regexp::FormalRegExp < > alternate(const regexp::FormalRegExp < > & first, const regexp::FormalRegExp < > & second); - static regexp::FormalRegExpStructure < alphabet::Symbol > alternate(const regexp::FormalRegExpStructure < alphabet::Symbol > & first, const regexp::FormalRegExpStructure < alphabet::Symbol > & second); - static regexp::UnboundedRegExp < > alternate(const regexp::UnboundedRegExp < > & first, const regexp::UnboundedRegExp < > & second); - static regexp::UnboundedRegExpStructure < alphabet::Symbol > alternate(const regexp::UnboundedRegExpStructure < alphabet::Symbol > & first, const regexp::UnboundedRegExpStructure < alphabet::Symbol > & second); + template < class SymbolType > + static regexp::FormalRegExp < SymbolType > alternate(const regexp::FormalRegExp < SymbolType > & first, const regexp::FormalRegExp < SymbolType > & second); + template < class SymbolType > + static regexp::FormalRegExpStructure < SymbolType > alternate(const regexp::FormalRegExpStructure < SymbolType > & first, const regexp::FormalRegExpStructure < SymbolType > & second); + template < class SymbolType > + static regexp::UnboundedRegExp < SymbolType > alternate(const regexp::UnboundedRegExp < SymbolType > & first, const regexp::UnboundedRegExp < SymbolType > & second); + template < class SymbolType > + static regexp::UnboundedRegExpStructure < SymbolType > alternate(const regexp::UnboundedRegExpStructure < SymbolType > & first, const regexp::UnboundedRegExpStructure < SymbolType > & second); }; +template < class SymbolType > +regexp::FormalRegExp < SymbolType > RegExpAlternate::alternate(const regexp::FormalRegExp < SymbolType > & first, const regexp::FormalRegExp < SymbolType > & second) { + return regexp::FormalRegExp < SymbolType > (RegExpAlternate::alternate(first.getRegExp(), second.getRegExp())); +} + +template < class SymbolType > +regexp::FormalRegExpStructure < SymbolType > RegExpAlternate::alternate(const regexp::FormalRegExpStructure < SymbolType > & first, const regexp::FormalRegExpStructure < SymbolType > & second) { + return regexp::FormalRegExpStructure < SymbolType > (regexp::FormalRegExpAlternation < SymbolType > (first.getStructure(), second.getStructure())); +} + +template < class SymbolType > +regexp::UnboundedRegExp < SymbolType > RegExpAlternate::alternate(const regexp::UnboundedRegExp < SymbolType > & first, const regexp::UnboundedRegExp < SymbolType > & second) { + return regexp::UnboundedRegExp < SymbolType > (RegExpAlternate::alternate(first.getRegExp(), second.getRegExp())); +} + +template < class SymbolType > +regexp::UnboundedRegExpStructure < SymbolType > RegExpAlternate::alternate(const regexp::UnboundedRegExpStructure < SymbolType > & first, const regexp::UnboundedRegExpStructure < SymbolType > & second) { + regexp::UnboundedRegExpAlternation < SymbolType > con; + con.appendElement(first.getStructure()); + con.appendElement(second.getStructure()); + return regexp::UnboundedRegExpStructure < SymbolType > (con); +} + } /* namespace regexp */ #endif /* REG_EXP_ALTERNATE_H_ */ diff --git a/alib2algo/src/regexp/transform/RegExpConcatenate.cpp b/alib2algo/src/regexp/transform/RegExpConcatenate.cpp index eee3499afcd7ac63b682589dc5dc76d24aa6efb7..3dc1f3be4e4b3fc2b41b773de9f8b1706e065288 100644 --- a/alib2algo/src/regexp/transform/RegExpConcatenate.cpp +++ b/alib2algo/src/regexp/transform/RegExpConcatenate.cpp @@ -6,37 +6,14 @@ */ #include "RegExpConcatenate.h" -#include "regexp/formal/FormalRegExpConcatenation.h" -#include "regexp/unbounded/UnboundedRegExpConcatenation.h" -namespace regexp -{ +namespace regexp { regexp::RegExp RegExpConcatenate::concatenate(const regexp::RegExp& first, const regexp::RegExp& second) { return dispatch(first.getData(), second.getData()); } -regexp::FormalRegExp < > RegExpConcatenate::concatenate(const regexp::FormalRegExp < > & first, const regexp::FormalRegExp < > & second) { - return regexp::FormalRegExp < > (RegExpConcatenate::concatenate(first.getRegExp(), second.getRegExp())); -} - auto RegExpConcatenateFormalRegExp = RegExpConcatenate::RegistratorWrapper<regexp::FormalRegExp < >, regexp::FormalRegExp < > > (RegExpConcatenate::concatenate); - -regexp::FormalRegExpStructure < alphabet::Symbol > RegExpConcatenate::concatenate(const regexp::FormalRegExpStructure < alphabet::Symbol > & first, const regexp::FormalRegExpStructure < alphabet::Symbol > & second) { - return regexp::FormalRegExpStructure < alphabet::Symbol > (regexp::FormalRegExpConcatenation < alphabet::Symbol > (first.getStructure(), second.getStructure())); -} - -regexp::UnboundedRegExp < > RegExpConcatenate::concatenate(const regexp::UnboundedRegExp < > & first, const regexp::UnboundedRegExp < > & second) { - return regexp::UnboundedRegExp < >(RegExpConcatenate::concatenate(first.getRegExp(), second.getRegExp())); -} - auto RegExpConcatenateUnboundedRegExp = RegExpConcatenate::RegistratorWrapper<regexp::UnboundedRegExp < >, regexp::UnboundedRegExp < > >(RegExpConcatenate::concatenate); -regexp::UnboundedRegExpStructure < alphabet::Symbol > RegExpConcatenate::concatenate(const regexp::UnboundedRegExpStructure < alphabet::Symbol > & first, const regexp::UnboundedRegExpStructure < alphabet::Symbol > & second) { - regexp::UnboundedRegExpConcatenation < alphabet::Symbol > con; - con.appendElement(first.getStructure()); - con.appendElement(second.getStructure()); - return regexp::UnboundedRegExpStructure < alphabet::Symbol > (con); -} - } /* namespace regexp */ diff --git a/alib2algo/src/regexp/transform/RegExpConcatenate.h b/alib2algo/src/regexp/transform/RegExpConcatenate.h index 9faa59cde1fb843a2546cf89643f8ce1dc923a65..9eb038b515f60d6735e407e885719baa27780fbd 100644 --- a/alib2algo/src/regexp/transform/RegExpConcatenate.h +++ b/alib2algo/src/regexp/transform/RegExpConcatenate.h @@ -14,6 +14,9 @@ #include <regexp/formal/FormalRegExp.h> #include <regexp/unbounded/UnboundedRegExp.h> +#include "regexp/formal/FormalRegExpConcatenation.h" +#include "regexp/unbounded/UnboundedRegExpConcatenation.h" + namespace regexp { /** @@ -24,12 +27,39 @@ class RegExpConcatenate : public std::PromotingDoubleDispatch<RegExpConcatenate, public: static regexp::RegExp concatenate(const regexp::RegExp& first, const regexp::RegExp& second); - static regexp::FormalRegExp < > concatenate(const regexp::FormalRegExp < > & first, const regexp::FormalRegExp < > & second); - static regexp::FormalRegExpStructure < alphabet::Symbol > concatenate(const regexp::FormalRegExpStructure < alphabet::Symbol > & first, const regexp::FormalRegExpStructure < alphabet::Symbol > & second); - static regexp::UnboundedRegExp < > concatenate(const regexp::UnboundedRegExp < > & first, const regexp::UnboundedRegExp < > & second); - static regexp::UnboundedRegExpStructure < alphabet::Symbol > concatenate(const regexp::UnboundedRegExpStructure < alphabet::Symbol > & first, const regexp::UnboundedRegExpStructure < alphabet::Symbol > & second); + template < class SymbolType > + static regexp::FormalRegExp < SymbolType > concatenate(const regexp::FormalRegExp < SymbolType > & first, const regexp::FormalRegExp < SymbolType > & second); + template < class SymbolType > + static regexp::FormalRegExpStructure < SymbolType > concatenate(const regexp::FormalRegExpStructure < SymbolType > & first, const regexp::FormalRegExpStructure < SymbolType > & second); + template < class SymbolType > + static regexp::UnboundedRegExp < SymbolType > concatenate(const regexp::UnboundedRegExp < SymbolType > & first, const regexp::UnboundedRegExp < SymbolType > & second); + template < class SymbolType > + static regexp::UnboundedRegExpStructure < SymbolType > concatenate(const regexp::UnboundedRegExpStructure < SymbolType > & first, const regexp::UnboundedRegExpStructure < SymbolType > & second); }; +template < class SymbolType > +regexp::FormalRegExp < SymbolType > RegExpConcatenate::concatenate(const regexp::FormalRegExp < SymbolType > & first, const regexp::FormalRegExp < SymbolType > & second) { + return regexp::FormalRegExp < SymbolType > (RegExpConcatenate::concatenate(first.getRegExp(), second.getRegExp())); +} + +template < class SymbolType > +regexp::FormalRegExpStructure < SymbolType > RegExpConcatenate::concatenate(const regexp::FormalRegExpStructure < SymbolType > & first, const regexp::FormalRegExpStructure < SymbolType > & second) { + return regexp::FormalRegExpStructure < SymbolType > (regexp::FormalRegExpConcatenation < SymbolType > (first.getStructure(), second.getStructure())); +} + +template < class SymbolType > +regexp::UnboundedRegExp < SymbolType > RegExpConcatenate::concatenate(const regexp::UnboundedRegExp < SymbolType > & first, const regexp::UnboundedRegExp < SymbolType > & second) { + return regexp::UnboundedRegExp < SymbolType >(RegExpConcatenate::concatenate(first.getRegExp(), second.getRegExp())); +} + +template < class SymbolType > +regexp::UnboundedRegExpStructure < SymbolType > RegExpConcatenate::concatenate(const regexp::UnboundedRegExpStructure < SymbolType > & first, const regexp::UnboundedRegExpStructure < SymbolType > & second) { + regexp::UnboundedRegExpConcatenation < SymbolType > con; + con.appendElement(first.getStructure()); + con.appendElement(second.getStructure()); + return regexp::UnboundedRegExpStructure < SymbolType > (con); +} + } /* namespace regexp */ #endif /* REG_EXP_CONCATENATE_H_ */ diff --git a/alib2algo/src/regexp/transform/RegExpDerivation.cpp b/alib2algo/src/regexp/transform/RegExpDerivation.cpp index 272eae1bc754bd2981b4929a95077b789557a004..b1e7852540bc459d41968901c335500d0833f4f9 100644 --- a/alib2algo/src/regexp/transform/RegExpDerivation.cpp +++ b/alib2algo/src/regexp/transform/RegExpDerivation.cpp @@ -6,10 +6,6 @@ */ #include "RegExpDerivation.h" -#include <regexp/unbounded/UnboundedRegExpElements.h> -#include <regexp/formal/FormalRegExpElements.h> - -#include "../properties/RegExpEpsilon.h" namespace regexp { @@ -17,123 +13,7 @@ regexp::RegExp RegExpDerivation::derivation(const regexp::RegExp& regexp, const return dispatch(regexp.getData(), string); } -regexp::FormalRegExp < > RegExpDerivation::derivation(const regexp::FormalRegExp < > & regexp, const string::LinearString < >& string) { - std::unique_ptr < regexp::FormalRegExpElement < alphabet::Symbol > > newRegExp ( regexp.getRegExp().getStructure().clone() ); - - for(const auto& symbol : string.getContent()) - newRegExp = newRegExp->accept<std::unique_ptr < regexp::FormalRegExpElement < alphabet::Symbol > >, regexp::RegExpDerivation::Formal>(symbol); - - return regexp::FormalRegExp < > ( regexp::FormalRegExpStructure < alphabet::Symbol > ( std::move ( * newRegExp ) ) ); -} - auto RegExpDerivationFormalRegExp = RegExpDerivation::RegistratorWrapper<regexp::FormalRegExp < >, regexp::FormalRegExp < > >(RegExpDerivation::derivation); - -regexp::UnboundedRegExp < > RegExpDerivation::derivation(const regexp::UnboundedRegExp < > & regexp, const string::LinearString < >& string) { - std::unique_ptr < regexp::UnboundedRegExpElement < alphabet::Symbol > > newRegExp ( regexp.getRegExp().getStructure().clone() ); - - for(const auto& symbol : string.getContent()) - newRegExp = newRegExp->accept<std::unique_ptr < regexp::UnboundedRegExpElement < alphabet::Symbol > >, regexp::RegExpDerivation::Unbounded > ( symbol ); - - return regexp::UnboundedRegExp < > ( regexp::UnboundedRegExpStructure < alphabet::Symbol > ( std::move ( * newRegExp ) ) ); -} - auto RegExpDerivationUnboundedRegExp = RegExpDerivation::RegistratorWrapper<regexp::UnboundedRegExp < >, regexp::UnboundedRegExp < > >(RegExpDerivation::derivation); -// ---------------------------------------------------------------------------- - -std::unique_ptr < regexp::FormalRegExpElement < alphabet::Symbol > > RegExpDerivation::Formal::visit(const regexp::FormalRegExpAlternation < alphabet::Symbol > & alternation, const alphabet::Symbol& argument) { - std::unique_ptr < regexp::FormalRegExpElement < alphabet::Symbol > > leftDerivative = alternation.getLeftElement().accept<std::unique_ptr < regexp::FormalRegExpElement < alphabet::Symbol > >, regexp::RegExpDerivation::Formal>(argument); - std::unique_ptr < regexp::FormalRegExpElement < alphabet::Symbol > > rightDerivative = alternation.getRightElement().accept<std::unique_ptr < regexp::FormalRegExpElement < alphabet::Symbol > >, regexp::RegExpDerivation::Formal>(argument); - - return std::unique_ptr < FormalRegExpElement < alphabet::Symbol > > ( new regexp::FormalRegExpAlternation < alphabet::Symbol > ( std::move ( * leftDerivative ), std::move ( * rightDerivative ) ) ); -} - -std::unique_ptr < regexp::FormalRegExpElement < alphabet::Symbol > > RegExpDerivation::Formal::visit(const regexp::FormalRegExpConcatenation < alphabet::Symbol > & concatenation, const alphabet::Symbol& argument) { - std::unique_ptr < regexp::FormalRegExpElement < alphabet::Symbol > > leftDerivative = concatenation.getLeftElement().accept<std::unique_ptr < regexp::FormalRegExpElement < alphabet::Symbol > >, regexp::RegExpDerivation::Formal>(argument); - - std::unique_ptr < regexp::FormalRegExpElement < alphabet::Symbol > > res ( new regexp::FormalRegExpConcatenation < alphabet::Symbol > ( std::move ( * leftDerivative ), std::move_copy ( concatenation.getRightElement ( ) ) ) ); - - if(regexp::properties::RegExpEpsilon::languageContainsEpsilon(concatenation.getLeftElement())) { - std::unique_ptr < regexp::FormalRegExpElement < alphabet::Symbol > > rightDerivative = concatenation.getRightElement().accept<std::unique_ptr < regexp::FormalRegExpElement < alphabet::Symbol > >, regexp::RegExpDerivation::Formal>(argument); - - res = std::unique_ptr < FormalRegExpElement < alphabet::Symbol > > ( new regexp::FormalRegExpAlternation < alphabet::Symbol > ( std::move ( * res ), std::move( * rightDerivative ) ) ); - } - - return res; -} - -std::unique_ptr < regexp::FormalRegExpElement < alphabet::Symbol > > RegExpDerivation::Formal::visit(const regexp::FormalRegExpIteration < alphabet::Symbol > & iteration, const alphabet::Symbol& argument) { - std::unique_ptr < regexp::FormalRegExpElement < alphabet::Symbol > > elementDerivative = iteration.getElement().accept<std::unique_ptr < regexp::FormalRegExpElement < alphabet::Symbol > >, regexp::RegExpDerivation::Formal > ( argument ); - return std::unique_ptr < FormalRegExpElement < alphabet::Symbol > > ( new regexp::FormalRegExpConcatenation < alphabet::Symbol > ( * elementDerivative, iteration ) ); -} - -std::unique_ptr < regexp::FormalRegExpElement < alphabet::Symbol > > RegExpDerivation::Formal::visit(const regexp::FormalRegExpSymbol < alphabet::Symbol > & symbol, const alphabet::Symbol& argument) { - if(argument == symbol.getSymbol()) - return std::unique_ptr < FormalRegExpElement < alphabet::Symbol > > ( new regexp::FormalRegExpEpsilon < alphabet::Symbol > () ); - else - return std::unique_ptr < FormalRegExpElement < alphabet::Symbol > > ( new regexp::FormalRegExpEmpty < alphabet::Symbol > () ); -} - - -std::unique_ptr < regexp::FormalRegExpElement < alphabet::Symbol > > RegExpDerivation::Formal::visit(const regexp::FormalRegExpEpsilon < alphabet::Symbol > &, const alphabet::Symbol&) { - return std::unique_ptr < FormalRegExpElement < alphabet::Symbol > > ( new regexp::FormalRegExpEmpty < alphabet::Symbol > () ); -} - -std::unique_ptr < regexp::FormalRegExpElement < alphabet::Symbol > > RegExpDerivation::Formal::visit(const regexp::FormalRegExpEmpty < alphabet::Symbol > &, const alphabet::Symbol&) { - return std::unique_ptr < FormalRegExpElement < alphabet::Symbol > > ( new regexp::FormalRegExpEmpty < alphabet::Symbol > () ); -} - -// ---------------------------------------------------------------------------- - -std::unique_ptr < regexp::UnboundedRegExpElement < alphabet::Symbol > > RegExpDerivation::Unbounded::visit(const regexp::UnboundedRegExpAlternation < alphabet::Symbol > & alternation, const alphabet::Symbol& argument) { - std::unique_ptr < regexp::UnboundedRegExpAlternation < alphabet::Symbol > > ret ( new regexp::UnboundedRegExpAlternation < alphabet::Symbol > () ); - - for(const auto& child : alternation.getElements()) - ret->appendElement( * ( child->accept<std::unique_ptr < regexp::UnboundedRegExpElement < alphabet::Symbol > >, regexp::RegExpDerivation::Unbounded > ( argument ) ) ); - - return std::unique_ptr < regexp::UnboundedRegExpElement < alphabet::Symbol > > ( std::move ( ret ) ); -} - -std::unique_ptr < regexp::UnboundedRegExpElement < alphabet::Symbol > > RegExpDerivation::Unbounded::visit(const regexp::UnboundedRegExpConcatenation < alphabet::Symbol > & concatenation, const alphabet::Symbol& argument) { - std::unique_ptr < regexp::UnboundedRegExpAlternation < alphabet::Symbol > > ret ( new regexp::UnboundedRegExpAlternation < alphabet::Symbol > () ); - - for(auto child = concatenation.getElements().begin(); child != concatenation.getElements().end(); ++ child) { - std::unique_ptr < regexp::UnboundedRegExpConcatenation < alphabet::Symbol > > concat ( new regexp::UnboundedRegExpConcatenation < alphabet::Symbol > () ); - concat->appendElement( * ( (*child)->accept<std::unique_ptr < regexp::UnboundedRegExpElement < alphabet::Symbol > >, regexp::RegExpDerivation::Unbounded > ( argument ) ) ); - - auto succeedingElement = child; - while(++succeedingElement != concatenation.getElements().end()) - concat->appendElement(**succeedingElement); - - ret->appendElement(std::move(*concat)); - - if( ! regexp::properties::RegExpEpsilon::languageContainsEpsilon(**child) ) - break; - } - - return std::unique_ptr < regexp::UnboundedRegExpElement < alphabet::Symbol > > ( std::move ( ret ) ); -} - -std::unique_ptr < regexp::UnboundedRegExpElement < alphabet::Symbol > > RegExpDerivation::Unbounded::visit(const regexp::UnboundedRegExpIteration < alphabet::Symbol > & iteration, const alphabet::Symbol& argument) { - std::unique_ptr < UnboundedRegExpConcatenation < alphabet::Symbol > > con ( new regexp::UnboundedRegExpConcatenation < alphabet::Symbol > ( ) ); - con->appendElement ( * ( iteration.getElement().accept<std::unique_ptr < regexp::UnboundedRegExpElement < alphabet::Symbol > >, regexp::RegExpDerivation::Unbounded > ( argument ) ) ); - con->appendElement ( iteration ); - return std::unique_ptr < UnboundedRegExpElement < alphabet::Symbol > > ( std::move ( con ) ); -} - -std::unique_ptr < regexp::UnboundedRegExpElement < alphabet::Symbol > > RegExpDerivation::Unbounded::visit(const regexp::UnboundedRegExpSymbol < alphabet::Symbol > & symbol, const alphabet::Symbol& argument) { - if(argument == symbol.getSymbol()) - return std::unique_ptr < UnboundedRegExpElement < alphabet::Symbol > > ( new regexp::UnboundedRegExpEpsilon < alphabet::Symbol > () ); - else - return std::unique_ptr < UnboundedRegExpElement < alphabet::Symbol > > ( new regexp::UnboundedRegExpEmpty < alphabet::Symbol > () ); -} - -std::unique_ptr < regexp::UnboundedRegExpElement < alphabet::Symbol > > RegExpDerivation::Unbounded::visit(const regexp::UnboundedRegExpEpsilon < alphabet::Symbol > &, const alphabet::Symbol&) { - return std::unique_ptr < UnboundedRegExpElement < alphabet::Symbol > > ( new regexp::UnboundedRegExpEmpty < alphabet::Symbol > () ); -} - -std::unique_ptr < regexp::UnboundedRegExpElement < alphabet::Symbol > > RegExpDerivation::Unbounded::visit(const regexp::UnboundedRegExpEmpty < alphabet::Symbol > &, const alphabet::Symbol&) { - return std::unique_ptr < UnboundedRegExpElement < alphabet::Symbol > > ( new regexp::UnboundedRegExpEmpty < alphabet::Symbol > () ); -} - } /* namespace regexp */ diff --git a/alib2algo/src/regexp/transform/RegExpDerivation.h b/alib2algo/src/regexp/transform/RegExpDerivation.h index c182a16d97cee4da58b34fd69e805e0a91e42e48..9ef036390ab6a974bf7483191aca6c8a303862a5 100644 --- a/alib2algo/src/regexp/transform/RegExpDerivation.h +++ b/alib2algo/src/regexp/transform/RegExpDerivation.h @@ -18,6 +18,11 @@ #include <string/LinearString.h> +#include <regexp/unbounded/UnboundedRegExpElements.h> +#include <regexp/formal/FormalRegExpElements.h> + +#include <regexp/properties/RegExpEpsilon.h> + namespace regexp { /** @@ -27,40 +32,172 @@ namespace regexp { * - Melichar, definition 2.91 in chapter 2.4.3 * - Brzozowski, J. A. - Derivatives of regular expressions (1964) */ -class RegExpDerivation : public std::SingleDispatch < RegExpDerivation, regexp::RegExp, const regexp::RegExpBase &, const string::LinearString < >&> { +class RegExpDerivation : public std::SingleDispatch < RegExpDerivation, regexp::RegExp, const regexp::RegExpBase &, const string::LinearString < > &> { public: /** * @param regexp RegExp to derivate * @param string String to derivate given RegExp over * @return derivative of regexp */ - static regexp::RegExp derivation(const regexp::RegExp& regexp, const string::LinearString < >& string); + static regexp::RegExp derivation(const regexp::RegExp& regexp, const string::LinearString < > & string); - static regexp::FormalRegExp < > derivation(const regexp::FormalRegExp < > & regexp, const string::LinearString < > & string); - static regexp::UnboundedRegExp < > derivation(const regexp::UnboundedRegExp < > & regexp, const string::LinearString < > & string); + template < class SymbolType > + static regexp::FormalRegExp < SymbolType > derivation(const regexp::FormalRegExp < SymbolType > & regexp, const string::LinearString < SymbolType > & string); + template < class SymbolType > + static regexp::UnboundedRegExp < SymbolType > derivation(const regexp::UnboundedRegExp < SymbolType > & regexp, const string::LinearString < SymbolType > & string); private: + template < class SymbolType > class Formal { public: - static std::unique_ptr < regexp::FormalRegExpElement < alphabet::Symbol > > visit(const regexp::FormalRegExpAlternation < alphabet::Symbol > & alternation, const alphabet::Symbol& argument); - static std::unique_ptr < regexp::FormalRegExpElement < alphabet::Symbol > > visit(const regexp::FormalRegExpConcatenation < alphabet::Symbol > & concatenation, const alphabet::Symbol& argument); - static std::unique_ptr < regexp::FormalRegExpElement < alphabet::Symbol > > visit(const regexp::FormalRegExpIteration < alphabet::Symbol > & iteration, const alphabet::Symbol& argument); - static std::unique_ptr < regexp::FormalRegExpElement < alphabet::Symbol > > visit(const regexp::FormalRegExpSymbol < alphabet::Symbol > & symbol, const alphabet::Symbol& argument); - static std::unique_ptr < regexp::FormalRegExpElement < alphabet::Symbol > > visit(const regexp::FormalRegExpEpsilon < alphabet::Symbol > & epsilon, const alphabet::Symbol& argument); - static std::unique_ptr < regexp::FormalRegExpElement < alphabet::Symbol > > visit(const regexp::FormalRegExpEmpty < alphabet::Symbol > & empty, const alphabet::Symbol& argument); + static std::unique_ptr < regexp::FormalRegExpElement < SymbolType > > visit(const regexp::FormalRegExpAlternation < SymbolType > & alternation, const SymbolType& argument); + static std::unique_ptr < regexp::FormalRegExpElement < SymbolType > > visit(const regexp::FormalRegExpConcatenation < SymbolType > & concatenation, const SymbolType& argument); + static std::unique_ptr < regexp::FormalRegExpElement < SymbolType > > visit(const regexp::FormalRegExpIteration < SymbolType > & iteration, const SymbolType& argument); + static std::unique_ptr < regexp::FormalRegExpElement < SymbolType > > visit(const regexp::FormalRegExpSymbol < SymbolType > & symbol, const SymbolType& argument); + static std::unique_ptr < regexp::FormalRegExpElement < SymbolType > > visit(const regexp::FormalRegExpEpsilon < SymbolType > & epsilon, const SymbolType& argument); + static std::unique_ptr < regexp::FormalRegExpElement < SymbolType > > visit(const regexp::FormalRegExpEmpty < SymbolType > & empty, const SymbolType& argument); }; + template < class SymbolType > class Unbounded { public: - static std::unique_ptr < regexp::UnboundedRegExpElement < alphabet::Symbol > > visit(const regexp::UnboundedRegExpAlternation < alphabet::Symbol > & alternation, const alphabet::Symbol& argument); - static std::unique_ptr < regexp::UnboundedRegExpElement < alphabet::Symbol > > visit(const regexp::UnboundedRegExpConcatenation < alphabet::Symbol > & concatenation, const alphabet::Symbol& argument); - static std::unique_ptr < regexp::UnboundedRegExpElement < alphabet::Symbol > > visit(const regexp::UnboundedRegExpIteration < alphabet::Symbol > & iteration, const alphabet::Symbol& argument); - static std::unique_ptr < regexp::UnboundedRegExpElement < alphabet::Symbol > > visit(const regexp::UnboundedRegExpSymbol < alphabet::Symbol > & symbol, const alphabet::Symbol& argument); - static std::unique_ptr < regexp::UnboundedRegExpElement < alphabet::Symbol > > visit(const regexp::UnboundedRegExpEpsilon < alphabet::Symbol > & epsilon, const alphabet::Symbol& argument); - static std::unique_ptr < regexp::UnboundedRegExpElement < alphabet::Symbol > > visit(const regexp::UnboundedRegExpEmpty < alphabet::Symbol > & empty, const alphabet::Symbol& argument); + static std::unique_ptr < regexp::UnboundedRegExpElement < SymbolType > > visit(const regexp::UnboundedRegExpAlternation < SymbolType > & alternation, const SymbolType& argument); + static std::unique_ptr < regexp::UnboundedRegExpElement < SymbolType > > visit(const regexp::UnboundedRegExpConcatenation < SymbolType > & concatenation, const SymbolType& argument); + static std::unique_ptr < regexp::UnboundedRegExpElement < SymbolType > > visit(const regexp::UnboundedRegExpIteration < SymbolType > & iteration, const SymbolType& argument); + static std::unique_ptr < regexp::UnboundedRegExpElement < SymbolType > > visit(const regexp::UnboundedRegExpSymbol < SymbolType > & symbol, const SymbolType& argument); + static std::unique_ptr < regexp::UnboundedRegExpElement < SymbolType > > visit(const regexp::UnboundedRegExpEpsilon < SymbolType > & epsilon, const SymbolType& argument); + static std::unique_ptr < regexp::UnboundedRegExpElement < SymbolType > > visit(const regexp::UnboundedRegExpEmpty < SymbolType > & empty, const SymbolType& argument); }; }; +template < class SymbolType > +regexp::FormalRegExp < SymbolType > RegExpDerivation::derivation(const regexp::FormalRegExp < SymbolType > & regexp, const string::LinearString < SymbolType >& string) { + std::unique_ptr < regexp::FormalRegExpElement < SymbolType > > newRegExp ( regexp.getRegExp().getStructure().clone() ); + + for(const auto& symbol : string.getContent()) + newRegExp = newRegExp->template accept<std::unique_ptr < regexp::FormalRegExpElement < SymbolType > >, regexp::RegExpDerivation::Formal < SymbolType >>(symbol); + + return regexp::FormalRegExp < SymbolType > ( regexp::FormalRegExpStructure < SymbolType > ( std::move ( * newRegExp ) ) ); +} + +template < class SymbolType > +regexp::UnboundedRegExp < SymbolType > RegExpDerivation::derivation(const regexp::UnboundedRegExp < SymbolType > & regexp, const string::LinearString < SymbolType >& string) { + std::unique_ptr < regexp::UnboundedRegExpElement < SymbolType > > newRegExp ( regexp.getRegExp().getStructure().clone() ); + + for(const auto& symbol : string.getContent()) + newRegExp = newRegExp->template accept<std::unique_ptr < regexp::UnboundedRegExpElement < SymbolType > >, regexp::RegExpDerivation::Unbounded < SymbolType > > ( symbol ); + + return regexp::UnboundedRegExp < SymbolType > ( regexp::UnboundedRegExpStructure < SymbolType > ( std::move ( * newRegExp ) ) ); +} + +// ---------------------------------------------------------------------------- + +template < class SymbolType > +std::unique_ptr < regexp::FormalRegExpElement < SymbolType > > RegExpDerivation::Formal < SymbolType >::visit(const regexp::FormalRegExpAlternation < SymbolType > & alternation, const SymbolType& argument) { + std::unique_ptr < regexp::FormalRegExpElement < SymbolType > > leftDerivative = alternation.getLeftElement().template accept<std::unique_ptr < regexp::FormalRegExpElement < SymbolType > >, regexp::RegExpDerivation::Formal < SymbolType >>(argument); + std::unique_ptr < regexp::FormalRegExpElement < SymbolType > > rightDerivative = alternation.getRightElement().template accept<std::unique_ptr < regexp::FormalRegExpElement < SymbolType > >, regexp::RegExpDerivation::Formal < SymbolType >>(argument); + + return std::unique_ptr < FormalRegExpElement < SymbolType > > ( new regexp::FormalRegExpAlternation < SymbolType > ( std::move ( * leftDerivative ), std::move ( * rightDerivative ) ) ); +} + +template < class SymbolType > +std::unique_ptr < regexp::FormalRegExpElement < SymbolType > > RegExpDerivation::Formal < SymbolType >::visit(const regexp::FormalRegExpConcatenation < SymbolType > & concatenation, const SymbolType& argument) { + std::unique_ptr < regexp::FormalRegExpElement < SymbolType > > leftDerivative = concatenation.getLeftElement().template accept<std::unique_ptr < regexp::FormalRegExpElement < SymbolType > >, regexp::RegExpDerivation::Formal < SymbolType >>(argument); + + std::unique_ptr < regexp::FormalRegExpElement < SymbolType > > res ( new regexp::FormalRegExpConcatenation < SymbolType > ( std::move ( * leftDerivative ), std::move_copy ( concatenation.getRightElement ( ) ) ) ); + + if(regexp::properties::RegExpEpsilon::languageContainsEpsilon(concatenation.getLeftElement())) { + std::unique_ptr < regexp::FormalRegExpElement < SymbolType > > rightDerivative = concatenation.getRightElement().template accept<std::unique_ptr < regexp::FormalRegExpElement < SymbolType > >, regexp::RegExpDerivation::Formal < SymbolType >>(argument); + + res = std::unique_ptr < FormalRegExpElement < SymbolType > > ( new regexp::FormalRegExpAlternation < SymbolType > ( std::move ( * res ), std::move( * rightDerivative ) ) ); + } + + return res; +} + +template < class SymbolType > +std::unique_ptr < regexp::FormalRegExpElement < SymbolType > > RegExpDerivation::Formal < SymbolType >::visit(const regexp::FormalRegExpIteration < SymbolType > & iteration, const SymbolType& argument) { + std::unique_ptr < regexp::FormalRegExpElement < SymbolType > > elementDerivative = iteration.getElement().template accept<std::unique_ptr < regexp::FormalRegExpElement < SymbolType > >, regexp::RegExpDerivation::Formal < SymbolType > > ( argument ); + return std::unique_ptr < FormalRegExpElement < SymbolType > > ( new regexp::FormalRegExpConcatenation < SymbolType > ( * elementDerivative, iteration ) ); +} + +template < class SymbolType > +std::unique_ptr < regexp::FormalRegExpElement < SymbolType > > RegExpDerivation::Formal < SymbolType >::visit(const regexp::FormalRegExpSymbol < SymbolType > & symbol, const SymbolType& argument) { + if(argument == symbol.getSymbol()) + return std::unique_ptr < FormalRegExpElement < SymbolType > > ( new regexp::FormalRegExpEpsilon < SymbolType > () ); + else + return std::unique_ptr < FormalRegExpElement < SymbolType > > ( new regexp::FormalRegExpEmpty < SymbolType > () ); +} + +template < class SymbolType > +std::unique_ptr < regexp::FormalRegExpElement < SymbolType > > RegExpDerivation::Formal < SymbolType >::visit(const regexp::FormalRegExpEpsilon < SymbolType > &, const SymbolType&) { + return std::unique_ptr < FormalRegExpElement < SymbolType > > ( new regexp::FormalRegExpEmpty < SymbolType > () ); +} + +template < class SymbolType > +std::unique_ptr < regexp::FormalRegExpElement < SymbolType > > RegExpDerivation::Formal < SymbolType >::visit(const regexp::FormalRegExpEmpty < SymbolType > &, const SymbolType&) { + return std::unique_ptr < FormalRegExpElement < SymbolType > > ( new regexp::FormalRegExpEmpty < SymbolType > () ); +} + +// ---------------------------------------------------------------------------- + +template < class SymbolType > +std::unique_ptr < regexp::UnboundedRegExpElement < SymbolType > > RegExpDerivation::Unbounded < SymbolType >::visit(const regexp::UnboundedRegExpAlternation < SymbolType > & alternation, const SymbolType& argument) { + std::unique_ptr < regexp::UnboundedRegExpAlternation < SymbolType > > ret ( new regexp::UnboundedRegExpAlternation < SymbolType > () ); + + for(const auto& child : alternation.getElements()) + ret->appendElement( * ( child->template accept<std::unique_ptr < regexp::UnboundedRegExpElement < SymbolType > >, regexp::RegExpDerivation::Unbounded < SymbolType > > ( argument ) ) ); + + return std::unique_ptr < regexp::UnboundedRegExpElement < SymbolType > > ( std::move ( ret ) ); +} + +template < class SymbolType > +std::unique_ptr < regexp::UnboundedRegExpElement < SymbolType > > RegExpDerivation::Unbounded < SymbolType >::visit(const regexp::UnboundedRegExpConcatenation < SymbolType > & concatenation, const SymbolType& argument) { + std::unique_ptr < regexp::UnboundedRegExpAlternation < SymbolType > > ret ( new regexp::UnboundedRegExpAlternation < SymbolType > () ); + + for(auto child = concatenation.getElements().begin(); child != concatenation.getElements().end(); ++ child) { + std::unique_ptr < regexp::UnboundedRegExpConcatenation < SymbolType > > concat ( new regexp::UnboundedRegExpConcatenation < SymbolType > () ); + concat->appendElement( * ( (*child)->template accept<std::unique_ptr < regexp::UnboundedRegExpElement < SymbolType > >, regexp::RegExpDerivation::Unbounded < SymbolType > > ( argument ) ) ); + + auto succeedingElement = child; + while(++succeedingElement != concatenation.getElements().end()) + concat->appendElement(**succeedingElement); + + ret->appendElement(std::move(*concat)); + + if( ! regexp::properties::RegExpEpsilon::languageContainsEpsilon(**child) ) + break; + } + + return std::unique_ptr < regexp::UnboundedRegExpElement < SymbolType > > ( std::move ( ret ) ); +} + +template < class SymbolType > +std::unique_ptr < regexp::UnboundedRegExpElement < SymbolType > > RegExpDerivation::Unbounded < SymbolType >::visit(const regexp::UnboundedRegExpIteration < SymbolType > & iteration, const SymbolType& argument) { + std::unique_ptr < UnboundedRegExpConcatenation < SymbolType > > con ( new regexp::UnboundedRegExpConcatenation < SymbolType > ( ) ); + con->appendElement ( * ( iteration.getElement().template accept<std::unique_ptr < regexp::UnboundedRegExpElement < SymbolType > >, regexp::RegExpDerivation::Unbounded < SymbolType > > ( argument ) ) ); + con->appendElement ( iteration ); + return std::unique_ptr < UnboundedRegExpElement < SymbolType > > ( std::move ( con ) ); +} + +template < class SymbolType > +std::unique_ptr < regexp::UnboundedRegExpElement < SymbolType > > RegExpDerivation::Unbounded < SymbolType >::visit(const regexp::UnboundedRegExpSymbol < SymbolType > & symbol, const SymbolType& argument) { + if(argument == symbol.getSymbol()) + return std::unique_ptr < UnboundedRegExpElement < SymbolType > > ( new regexp::UnboundedRegExpEpsilon < SymbolType > () ); + else + return std::unique_ptr < UnboundedRegExpElement < SymbolType > > ( new regexp::UnboundedRegExpEmpty < SymbolType > () ); +} + +template < class SymbolType > +std::unique_ptr < regexp::UnboundedRegExpElement < SymbolType > > RegExpDerivation::Unbounded < SymbolType >::visit(const regexp::UnboundedRegExpEpsilon < SymbolType > &, const SymbolType&) { + return std::unique_ptr < UnboundedRegExpElement < SymbolType > > ( new regexp::UnboundedRegExpEmpty < SymbolType > () ); +} + +template < class SymbolType > +std::unique_ptr < regexp::UnboundedRegExpElement < SymbolType > > RegExpDerivation::Unbounded < SymbolType >::visit(const regexp::UnboundedRegExpEmpty < SymbolType > &, const SymbolType&) { + return std::unique_ptr < UnboundedRegExpElement < SymbolType > > ( new regexp::UnboundedRegExpEmpty < SymbolType > () ); +} + } /* namespace regexp */ #endif /* REGEXPDERIVATION_H_ */ diff --git a/alib2algo/src/regexp/transform/RegExpIntegral.cpp b/alib2algo/src/regexp/transform/RegExpIntegral.cpp index 2ae438ca98ee745141bd4764a852351911abbb7a..f4558d7350da8cb0b2a11abff45ccb62cca2a4d9 100644 --- a/alib2algo/src/regexp/transform/RegExpIntegral.cpp +++ b/alib2algo/src/regexp/transform/RegExpIntegral.cpp @@ -6,8 +6,6 @@ */ #include "RegExpIntegral.h" -#include <regexp/unbounded/UnboundedRegExpElements.h> -#include <regexp/formal/FormalRegExpElements.h> namespace regexp { @@ -15,101 +13,7 @@ regexp::RegExp RegExpIntegral::integral(const regexp::RegExp& regexp, const stri return dispatch(regexp.getData(), string); } -regexp::FormalRegExp < > RegExpIntegral::integral(const regexp::FormalRegExp < > & regexp, const string::LinearString < >& string) { - std::unique_ptr < regexp::FormalRegExpElement < alphabet::Symbol > > newRegExp ( regexp.getRegExp().getStructure().clone() ); - - for(const auto& symbol : string.getContent()) - newRegExp = newRegExp->accept < std::unique_ptr < regexp::FormalRegExpElement < alphabet::Symbol > >, RegExpIntegral::Formal > ( symbol ); - - return regexp::FormalRegExp < > ( regexp::FormalRegExpStructure < alphabet::Symbol > ( * newRegExp ) ); -} - auto RegExpIntegralFormalRegExp = RegExpIntegral::RegistratorWrapper<regexp::FormalRegExp < >, regexp::FormalRegExp < > > ( RegExpIntegral::integral ); - -regexp::UnboundedRegExp < > RegExpIntegral::integral(const regexp::UnboundedRegExp < > & regexp, const string::LinearString < >& string) { - std::unique_ptr < regexp::UnboundedRegExpElement < alphabet::Symbol > > newRegExp ( regexp.getRegExp().getStructure().clone() ); - - for(const auto& symbol : string.getContent()) - newRegExp = newRegExp->accept < std::unique_ptr < regexp::UnboundedRegExpElement < alphabet::Symbol > >, RegExpIntegral::Unbounded > ( symbol ); - - return regexp::UnboundedRegExp < > ( regexp::UnboundedRegExpStructure < alphabet::Symbol > ( * newRegExp ) ); -} - auto RegExpIntegralUnboundedRegExp = RegExpIntegral::RegistratorWrapper<regexp::UnboundedRegExp < >, regexp::UnboundedRegExp < > > ( RegExpIntegral::integral ); -// ---------------------------------------------------------------------------- - -std::unique_ptr < regexp::FormalRegExpElement < alphabet::Symbol > > RegExpIntegral::Formal::visit(const regexp::FormalRegExpAlternation < alphabet::Symbol > & alternation, const alphabet::Symbol& argument) { - std::unique_ptr < regexp::FormalRegExpElement < alphabet::Symbol > > leftIntegral = alternation.getLeftElement().accept < std::unique_ptr < regexp::FormalRegExpElement < alphabet::Symbol > >, RegExpIntegral::Formal > ( argument ); - std::unique_ptr < regexp::FormalRegExpElement < alphabet::Symbol > > rightIntegral = alternation.getRightElement().accept < std::unique_ptr < regexp::FormalRegExpElement < alphabet::Symbol > >, RegExpIntegral::Formal > ( argument ); - - return std::unique_ptr < FormalRegExpElement < alphabet::Symbol > > ( new regexp::FormalRegExpAlternation < alphabet::Symbol > ( std::move ( * leftIntegral ), std::move ( * rightIntegral ) ) ); -} - -std::unique_ptr < regexp::FormalRegExpElement < alphabet::Symbol > > RegExpIntegral::Formal::visit(const regexp::FormalRegExpConcatenation < alphabet::Symbol > & concatenation, const alphabet::Symbol& argument) { - return std::unique_ptr < regexp::FormalRegExpElement < alphabet::Symbol > > ( new regexp::FormalRegExpConcatenation < alphabet::Symbol > ( regexp::FormalRegExpSymbol < alphabet::Symbol > ( argument ), concatenation ) ); -} - -std::unique_ptr < regexp::FormalRegExpElement < alphabet::Symbol > > RegExpIntegral::Formal::visit(const regexp::FormalRegExpIteration < alphabet::Symbol > & iteration, const alphabet::Symbol& argument) { - return std::unique_ptr < regexp::FormalRegExpElement < alphabet::Symbol > > ( new regexp::FormalRegExpConcatenation < alphabet::Symbol > ( regexp::FormalRegExpSymbol < alphabet::Symbol > ( argument ), iteration ) ); -} - -std::unique_ptr < regexp::FormalRegExpElement < alphabet::Symbol > > RegExpIntegral::Formal::visit(const regexp::FormalRegExpSymbol < alphabet::Symbol > & symbol, const alphabet::Symbol& argument) { - return std::unique_ptr < regexp::FormalRegExpElement < alphabet::Symbol > > ( new regexp::FormalRegExpConcatenation < alphabet::Symbol > ( regexp::FormalRegExpSymbol < alphabet::Symbol > ( argument ), symbol ) ); -} - - -std::unique_ptr < regexp::FormalRegExpElement < alphabet::Symbol > > RegExpIntegral::Formal::visit(const regexp::FormalRegExpEpsilon < alphabet::Symbol > &, const alphabet::Symbol& argument) { - return std::unique_ptr < regexp::FormalRegExpElement < alphabet::Symbol > > ( new regexp::FormalRegExpSymbol < alphabet::Symbol > ( argument ) ); -} - -std::unique_ptr < regexp::FormalRegExpElement < alphabet::Symbol > > RegExpIntegral::Formal::visit(const regexp::FormalRegExpEmpty < alphabet::Symbol > &, const alphabet::Symbol&) { - return std::unique_ptr < regexp::FormalRegExpElement < alphabet::Symbol > > ( new regexp::FormalRegExpEmpty < alphabet::Symbol > ( ) ); -} - -// ---------------------------------------------------------------------------- - -std::unique_ptr < regexp::UnboundedRegExpElement < alphabet::Symbol > > RegExpIntegral::Unbounded::visit(const regexp::UnboundedRegExpAlternation < alphabet::Symbol > & alternation, const alphabet::Symbol& argument) { - regexp::UnboundedRegExpAlternation < alphabet::Symbol > * alt = new regexp::UnboundedRegExpAlternation < alphabet::Symbol > (); - - for(const auto& child : alternation.getElements()) - alt->appendElement(* ( child->accept < std::unique_ptr < regexp::UnboundedRegExpElement < alphabet::Symbol > >, RegExpIntegral::Unbounded > (argument) ) ); - - return std::unique_ptr < UnboundedRegExpElement < alphabet::Symbol > > ( alt ); -} - -std::unique_ptr < regexp::UnboundedRegExpElement < alphabet::Symbol > > RegExpIntegral::Unbounded::visit(const regexp::UnboundedRegExpConcatenation < alphabet::Symbol > & concatenation, const alphabet::Symbol& argument) { - regexp::UnboundedRegExpConcatenation < alphabet::Symbol > * con = new regexp::UnboundedRegExpConcatenation < alphabet::Symbol > ( ); - - con->appendElement ( regexp::UnboundedRegExpSymbol < alphabet::Symbol > ( argument ) ); - for ( const auto & element : concatenation.getElements() ) - con->appendElement ( * element ); - - return std::unique_ptr < regexp::UnboundedRegExpElement < alphabet::Symbol > > ( con ); -} - -std::unique_ptr < regexp::UnboundedRegExpElement < alphabet::Symbol > > RegExpIntegral::Unbounded::visit(const regexp::UnboundedRegExpIteration < alphabet::Symbol > & iteration, const alphabet::Symbol& argument) { - regexp::UnboundedRegExpConcatenation < alphabet::Symbol > * con = new regexp::UnboundedRegExpConcatenation < alphabet::Symbol > ( ); - con->appendElement ( regexp::UnboundedRegExpSymbol < alphabet::Symbol > ( argument ) ); - con->appendElement ( iteration ); - - return std::unique_ptr < regexp::UnboundedRegExpElement < alphabet::Symbol > > ( con ); -} - -std::unique_ptr < regexp::UnboundedRegExpElement < alphabet::Symbol > > RegExpIntegral::Unbounded::visit(const regexp::UnboundedRegExpSymbol < alphabet::Symbol > & symbol, const alphabet::Symbol& argument) { - regexp::UnboundedRegExpConcatenation < alphabet::Symbol > * con = new regexp::UnboundedRegExpConcatenation < alphabet::Symbol > ( ); - con->appendElement ( regexp::UnboundedRegExpSymbol < alphabet::Symbol > ( argument ) ); - con->appendElement ( symbol ); - - return std::unique_ptr < regexp::UnboundedRegExpElement < alphabet::Symbol > > ( con ); -} - -std::unique_ptr < regexp::UnboundedRegExpElement < alphabet::Symbol > > RegExpIntegral::Unbounded::visit(const regexp::UnboundedRegExpEpsilon < alphabet::Symbol > &, const alphabet::Symbol& argument) { - return std::unique_ptr < regexp::UnboundedRegExpElement < alphabet::Symbol > > ( new regexp::UnboundedRegExpSymbol < alphabet::Symbol > ( argument ) ); -} - -std::unique_ptr < regexp::UnboundedRegExpElement < alphabet::Symbol > > RegExpIntegral::Unbounded::visit(const regexp::UnboundedRegExpEmpty < alphabet::Symbol > &, const alphabet::Symbol&) { - return std::unique_ptr < regexp::UnboundedRegExpElement < alphabet::Symbol > > ( new regexp::UnboundedRegExpEmpty < alphabet::Symbol > ( ) ); -} - } /* namespace regexp */ diff --git a/alib2algo/src/regexp/transform/RegExpIntegral.h b/alib2algo/src/regexp/transform/RegExpIntegral.h index 4d1440b10e27bdfbfeca57845e517be963777350..f6d26c8a36ea23c87d556b0d140f52305ee29dee 100644 --- a/alib2algo/src/regexp/transform/RegExpIntegral.h +++ b/alib2algo/src/regexp/transform/RegExpIntegral.h @@ -18,46 +18,159 @@ #include <string/LinearString.h> +#include <regexp/unbounded/UnboundedRegExpElements.h> +#include <regexp/formal/FormalRegExpElements.h> + namespace regexp { /** * Calculates integral of regular expression * Source: Melichar definition 2.93 in chapter 2.4.4 */ -class RegExpIntegral : public std::SingleDispatch < RegExpIntegral, regexp::RegExp, const regexp::RegExpBase &, const string::LinearString < >&> { +class RegExpIntegral : public std::SingleDispatch < RegExpIntegral, regexp::RegExp, const regexp::RegExpBase &, const string::LinearString < > & > { public: /** * @param regexp RegExp to integrate * @param string String to integrate given RegExp over * @return integral of regexp */ - static regexp::RegExp integral(const regexp::RegExp& regexp, const string::LinearString < >& string); + static regexp::RegExp integral(const regexp::RegExp& regexp, const string::LinearString < > & string); - static regexp::FormalRegExp < > integral(const regexp::FormalRegExp < > & regexp, const string::LinearString < >& string); - static regexp::UnboundedRegExp < > integral(const regexp::UnboundedRegExp < > & regexp, const string::LinearString < >& string); + template < class SymbolType > + static regexp::FormalRegExp < SymbolType > integral(const regexp::FormalRegExp < SymbolType > & regexp, const string::LinearString < SymbolType >& string); + template < class SymbolType > + static regexp::UnboundedRegExp < SymbolType > integral(const regexp::UnboundedRegExp < SymbolType > & regexp, const string::LinearString < SymbolType >& string); private: + template < class SymbolType > class Formal { public: - static std::unique_ptr < regexp::FormalRegExpElement < alphabet::Symbol > > visit(const regexp::FormalRegExpAlternation < alphabet::Symbol > & alternation, const alphabet::Symbol& argument); - static std::unique_ptr < regexp::FormalRegExpElement < alphabet::Symbol > > visit(const regexp::FormalRegExpConcatenation < alphabet::Symbol > & concatenation, const alphabet::Symbol& argument); - static std::unique_ptr < regexp::FormalRegExpElement < alphabet::Symbol > > visit(const regexp::FormalRegExpIteration < alphabet::Symbol > & iteration, const alphabet::Symbol& argument); - static std::unique_ptr < regexp::FormalRegExpElement < alphabet::Symbol > > visit(const regexp::FormalRegExpSymbol < alphabet::Symbol > & symbol, const alphabet::Symbol& argument); - static std::unique_ptr < regexp::FormalRegExpElement < alphabet::Symbol > > visit(const regexp::FormalRegExpEpsilon < alphabet::Symbol > & epsilon, const alphabet::Symbol& argument); - static std::unique_ptr < regexp::FormalRegExpElement < alphabet::Symbol > > visit(const regexp::FormalRegExpEmpty < alphabet::Symbol > & empty, const alphabet::Symbol& argument); + static std::unique_ptr < regexp::FormalRegExpElement < SymbolType > > visit(const regexp::FormalRegExpAlternation < SymbolType > & alternation, const SymbolType& argument); + static std::unique_ptr < regexp::FormalRegExpElement < SymbolType > > visit(const regexp::FormalRegExpConcatenation < SymbolType > & concatenation, const SymbolType& argument); + static std::unique_ptr < regexp::FormalRegExpElement < SymbolType > > visit(const regexp::FormalRegExpIteration < SymbolType > & iteration, const SymbolType& argument); + static std::unique_ptr < regexp::FormalRegExpElement < SymbolType > > visit(const regexp::FormalRegExpSymbol < SymbolType > & symbol, const SymbolType& argument); + static std::unique_ptr < regexp::FormalRegExpElement < SymbolType > > visit(const regexp::FormalRegExpEpsilon < SymbolType > & epsilon, const SymbolType& argument); + static std::unique_ptr < regexp::FormalRegExpElement < SymbolType > > visit(const regexp::FormalRegExpEmpty < SymbolType > & empty, const SymbolType& argument); }; + template < class SymbolType > class Unbounded { public: - static std::unique_ptr < regexp::UnboundedRegExpElement < alphabet::Symbol > > visit(const regexp::UnboundedRegExpAlternation < alphabet::Symbol > & alternation, const alphabet::Symbol& argument); - static std::unique_ptr < regexp::UnboundedRegExpElement < alphabet::Symbol > > visit(const regexp::UnboundedRegExpConcatenation < alphabet::Symbol > & concatenation, const alphabet::Symbol& argument); - static std::unique_ptr < regexp::UnboundedRegExpElement < alphabet::Symbol > > visit(const regexp::UnboundedRegExpIteration < alphabet::Symbol > & iteration, const alphabet::Symbol& argument); - static std::unique_ptr < regexp::UnboundedRegExpElement < alphabet::Symbol > > visit(const regexp::UnboundedRegExpSymbol < alphabet::Symbol > & symbol, const alphabet::Symbol& argument); - static std::unique_ptr < regexp::UnboundedRegExpElement < alphabet::Symbol > > visit(const regexp::UnboundedRegExpEpsilon < alphabet::Symbol > & epsilon, const alphabet::Symbol& argument); - static std::unique_ptr < regexp::UnboundedRegExpElement < alphabet::Symbol > > visit(const regexp::UnboundedRegExpEmpty < alphabet::Symbol > & empty, const alphabet::Symbol& argument); + static std::unique_ptr < regexp::UnboundedRegExpElement < SymbolType > > visit(const regexp::UnboundedRegExpAlternation < SymbolType > & alternation, const SymbolType& argument); + static std::unique_ptr < regexp::UnboundedRegExpElement < SymbolType > > visit(const regexp::UnboundedRegExpConcatenation < SymbolType > & concatenation, const SymbolType& argument); + static std::unique_ptr < regexp::UnboundedRegExpElement < SymbolType > > visit(const regexp::UnboundedRegExpIteration < SymbolType > & iteration, const SymbolType& argument); + static std::unique_ptr < regexp::UnboundedRegExpElement < SymbolType > > visit(const regexp::UnboundedRegExpSymbol < SymbolType > & symbol, const SymbolType& argument); + static std::unique_ptr < regexp::UnboundedRegExpElement < SymbolType > > visit(const regexp::UnboundedRegExpEpsilon < SymbolType > & epsilon, const SymbolType& argument); + static std::unique_ptr < regexp::UnboundedRegExpElement < SymbolType > > visit(const regexp::UnboundedRegExpEmpty < SymbolType > & empty, const SymbolType& argument); }; }; +template < class SymbolType > +regexp::FormalRegExp < SymbolType > RegExpIntegral::integral(const regexp::FormalRegExp < SymbolType > & regexp, const string::LinearString < SymbolType >& string) { + std::unique_ptr < regexp::FormalRegExpElement < SymbolType > > newRegExp ( regexp.getRegExp().getStructure().clone() ); + + for(const auto& symbol : string.getContent()) + newRegExp = newRegExp->template accept < std::unique_ptr < regexp::FormalRegExpElement < SymbolType > >, RegExpIntegral::Formal < SymbolType > > ( symbol ); + + return regexp::FormalRegExp < SymbolType > ( regexp::FormalRegExpStructure < SymbolType > ( * newRegExp ) ); +} + +template < class SymbolType > +regexp::UnboundedRegExp < SymbolType > RegExpIntegral::integral(const regexp::UnboundedRegExp < SymbolType > & regexp, const string::LinearString < SymbolType >& string) { + std::unique_ptr < regexp::UnboundedRegExpElement < SymbolType > > newRegExp ( regexp.getRegExp().getStructure().clone() ); + + for(const auto& symbol : string.getContent()) + newRegExp = newRegExp->template accept < std::unique_ptr < regexp::UnboundedRegExpElement < SymbolType > >, RegExpIntegral::Unbounded < SymbolType > > ( symbol ); + + return regexp::UnboundedRegExp < SymbolType > ( regexp::UnboundedRegExpStructure < SymbolType > ( * newRegExp ) ); +} + +// ---------------------------------------------------------------------------- + +template < class SymbolType > +std::unique_ptr < regexp::FormalRegExpElement < SymbolType > > RegExpIntegral::Formal < SymbolType >::visit(const regexp::FormalRegExpAlternation < SymbolType > & alternation, const SymbolType& argument) { + std::unique_ptr < regexp::FormalRegExpElement < SymbolType > > leftIntegral = alternation.getLeftElement().template accept < std::unique_ptr < regexp::FormalRegExpElement < SymbolType > >, RegExpIntegral::Formal < SymbolType > > ( argument ); + std::unique_ptr < regexp::FormalRegExpElement < SymbolType > > rightIntegral = alternation.getRightElement().template accept < std::unique_ptr < regexp::FormalRegExpElement < SymbolType > >, RegExpIntegral::Formal < SymbolType > > ( argument ); + + return std::unique_ptr < FormalRegExpElement < SymbolType > > ( new regexp::FormalRegExpAlternation < SymbolType > ( std::move ( * leftIntegral ), std::move ( * rightIntegral ) ) ); +} + +template < class SymbolType > +std::unique_ptr < regexp::FormalRegExpElement < SymbolType > > RegExpIntegral::Formal < SymbolType >::visit(const regexp::FormalRegExpConcatenation < SymbolType > & concatenation, const SymbolType& argument) { + return std::unique_ptr < regexp::FormalRegExpElement < SymbolType > > ( new regexp::FormalRegExpConcatenation < SymbolType > ( regexp::FormalRegExpSymbol < SymbolType > ( argument ), concatenation ) ); +} + +template < class SymbolType > +std::unique_ptr < regexp::FormalRegExpElement < SymbolType > > RegExpIntegral::Formal < SymbolType >::visit(const regexp::FormalRegExpIteration < SymbolType > & iteration, const SymbolType& argument) { + return std::unique_ptr < regexp::FormalRegExpElement < SymbolType > > ( new regexp::FormalRegExpConcatenation < SymbolType > ( regexp::FormalRegExpSymbol < SymbolType > ( argument ), iteration ) ); +} + +template < class SymbolType > +std::unique_ptr < regexp::FormalRegExpElement < SymbolType > > RegExpIntegral::Formal < SymbolType >::visit(const regexp::FormalRegExpSymbol < SymbolType > & symbol, const SymbolType& argument) { + return std::unique_ptr < regexp::FormalRegExpElement < SymbolType > > ( new regexp::FormalRegExpConcatenation < SymbolType > ( regexp::FormalRegExpSymbol < SymbolType > ( argument ), symbol ) ); +} + +template < class SymbolType > +std::unique_ptr < regexp::FormalRegExpElement < SymbolType > > RegExpIntegral::Formal < SymbolType >::visit(const regexp::FormalRegExpEpsilon < SymbolType > &, const SymbolType& argument) { + return std::unique_ptr < regexp::FormalRegExpElement < SymbolType > > ( new regexp::FormalRegExpSymbol < SymbolType > ( argument ) ); +} + +template < class SymbolType > +std::unique_ptr < regexp::FormalRegExpElement < SymbolType > > RegExpIntegral::Formal < SymbolType >::visit(const regexp::FormalRegExpEmpty < SymbolType > &, const SymbolType&) { + return std::unique_ptr < regexp::FormalRegExpElement < SymbolType > > ( new regexp::FormalRegExpEmpty < SymbolType > ( ) ); +} + +// ---------------------------------------------------------------------------- + +template < class SymbolType > +std::unique_ptr < regexp::UnboundedRegExpElement < SymbolType > > RegExpIntegral::Unbounded < SymbolType >::visit(const regexp::UnboundedRegExpAlternation < SymbolType > & alternation, const SymbolType& argument) { + regexp::UnboundedRegExpAlternation < SymbolType > * alt = new regexp::UnboundedRegExpAlternation < SymbolType > (); + + for(const auto& child : alternation.getElements()) + alt->appendElement(* ( child->template accept < std::unique_ptr < regexp::UnboundedRegExpElement < SymbolType > >, RegExpIntegral::Unbounded < SymbolType > > (argument) ) ); + + return std::unique_ptr < UnboundedRegExpElement < SymbolType > > ( alt ); +} + +template < class SymbolType > +std::unique_ptr < regexp::UnboundedRegExpElement < SymbolType > > RegExpIntegral::Unbounded < SymbolType >::visit(const regexp::UnboundedRegExpConcatenation < SymbolType > & concatenation, const SymbolType& argument) { + regexp::UnboundedRegExpConcatenation < SymbolType > * con = new regexp::UnboundedRegExpConcatenation < SymbolType > ( ); + + con->appendElement ( regexp::UnboundedRegExpSymbol < SymbolType > ( argument ) ); + for ( const auto & element : concatenation.getElements() ) + con->appendElement ( * element ); + + return std::unique_ptr < regexp::UnboundedRegExpElement < SymbolType > > ( con ); +} + +template < class SymbolType > +std::unique_ptr < regexp::UnboundedRegExpElement < SymbolType > > RegExpIntegral::Unbounded < SymbolType >::visit(const regexp::UnboundedRegExpIteration < SymbolType > & iteration, const SymbolType& argument) { + regexp::UnboundedRegExpConcatenation < SymbolType > * con = new regexp::UnboundedRegExpConcatenation < SymbolType > ( ); + con->appendElement ( regexp::UnboundedRegExpSymbol < SymbolType > ( argument ) ); + con->appendElement ( iteration ); + + return std::unique_ptr < regexp::UnboundedRegExpElement < SymbolType > > ( con ); +} + +template < class SymbolType > +std::unique_ptr < regexp::UnboundedRegExpElement < SymbolType > > RegExpIntegral::Unbounded < SymbolType >::visit(const regexp::UnboundedRegExpSymbol < SymbolType > & symbol, const SymbolType& argument) { + regexp::UnboundedRegExpConcatenation < SymbolType > * con = new regexp::UnboundedRegExpConcatenation < SymbolType > ( ); + con->appendElement ( regexp::UnboundedRegExpSymbol < SymbolType > ( argument ) ); + con->appendElement ( symbol ); + + return std::unique_ptr < regexp::UnboundedRegExpElement < SymbolType > > ( con ); +} + +template < class SymbolType > +std::unique_ptr < regexp::UnboundedRegExpElement < SymbolType > > RegExpIntegral::Unbounded < SymbolType >::visit(const regexp::UnboundedRegExpEpsilon < SymbolType > &, const SymbolType& argument) { + return std::unique_ptr < regexp::UnboundedRegExpElement < SymbolType > > ( new regexp::UnboundedRegExpSymbol < SymbolType > ( argument ) ); +} + +template < class SymbolType > +std::unique_ptr < regexp::UnboundedRegExpElement < SymbolType > > RegExpIntegral::Unbounded < SymbolType >::visit(const regexp::UnboundedRegExpEmpty < SymbolType > &, const SymbolType&) { + return std::unique_ptr < regexp::UnboundedRegExpElement < SymbolType > > ( new regexp::UnboundedRegExpEmpty < SymbolType > ( ) ); +} + } /* namespace regexp */ #endif /* REGEXPINTEGRAL_H_ */ diff --git a/alib2algo/src/regexp/transform/RegExpIterate.cpp b/alib2algo/src/regexp/transform/RegExpIterate.cpp index 20f01e543f6503dececd0fc71e4a365104957194..b89f3897746c385492c11b69748670b81d24aebf 100644 --- a/alib2algo/src/regexp/transform/RegExpIterate.cpp +++ b/alib2algo/src/regexp/transform/RegExpIterate.cpp @@ -6,8 +6,6 @@ */ #include "RegExpIterate.h" -#include "regexp/formal/FormalRegExpIteration.h" -#include "regexp/unbounded/UnboundedRegExpIteration.h" namespace regexp { @@ -15,24 +13,7 @@ regexp::RegExp RegExpIterate::iterate(const regexp::RegExp& regexp) { return dispatch(regexp.getData()); } -regexp::FormalRegExp < > RegExpIterate::iterate(const regexp::FormalRegExp < > & regexp) { - return regexp::FormalRegExp < >(RegExpIterate::iterate(regexp.getRegExp())); -} - auto RegExpIterateFormalRegExpFormalRegExp = RegExpIterate::RegistratorWrapper<regexp::FormalRegExp < >, regexp::FormalRegExp < > >(RegExpIterate::iterate); - -regexp::FormalRegExpStructure < alphabet::Symbol > RegExpIterate::iterate(const regexp::FormalRegExpStructure < alphabet::Symbol > & regexp) { - return regexp::FormalRegExpStructure < alphabet::Symbol >(regexp::FormalRegExpIteration < alphabet::Symbol > (regexp.getStructure())); -} - -regexp::UnboundedRegExp < > RegExpIterate::iterate(const regexp::UnboundedRegExp < > & regexp) { - return regexp::UnboundedRegExp < >(RegExpIterate::iterate(regexp.getRegExp())); -} - auto RegExpIterateUnboundedRegExpUnboundedRegExp = RegExpIterate::RegistratorWrapper<regexp::UnboundedRegExp < >, regexp::UnboundedRegExp < > >(RegExpIterate::iterate); -regexp::UnboundedRegExpStructure < alphabet::Symbol > RegExpIterate::iterate(const regexp::UnboundedRegExpStructure < alphabet::Symbol > & regexp) { - return regexp::UnboundedRegExpStructure < alphabet::Symbol >(regexp::UnboundedRegExpIteration < alphabet::Symbol > (regexp.getStructure())); -} - } /* namespace regexp */ diff --git a/alib2algo/src/regexp/transform/RegExpIterate.h b/alib2algo/src/regexp/transform/RegExpIterate.h index 27589fe09225b3760a7ae49815b6d7effbce45dc..8a416992cbe43d87f826a77bbed0e6ee420769fd 100644 --- a/alib2algo/src/regexp/transform/RegExpIterate.h +++ b/alib2algo/src/regexp/transform/RegExpIterate.h @@ -10,6 +10,9 @@ #include <core/multipleDispatch.hpp> +#include "regexp/formal/FormalRegExpIteration.h" +#include "regexp/unbounded/UnboundedRegExpIteration.h" + #include <regexp/RegExp.h> #include <regexp/formal/FormalRegExp.h> #include <regexp/unbounded/UnboundedRegExp.h> @@ -24,12 +27,36 @@ class RegExpIterate : public std::SingleDispatch<RegExpIterate, regexp::RegExp, public: static regexp::RegExp iterate(const regexp::RegExp& regexp); - static regexp::FormalRegExp < > iterate(const regexp::FormalRegExp < > & regexp); - static regexp::FormalRegExpStructure < alphabet::Symbol > iterate(const regexp::FormalRegExpStructure < alphabet::Symbol > & regexp); - static regexp::UnboundedRegExp < > iterate(const regexp::UnboundedRegExp < > & regexp); - static regexp::UnboundedRegExpStructure < alphabet::Symbol > iterate(const regexp::UnboundedRegExpStructure < alphabet::Symbol > & regexp); + template < class SymbolType > + static regexp::FormalRegExp < SymbolType > iterate(const regexp::FormalRegExp < SymbolType > & regexp); + template < class SymbolType > + static regexp::FormalRegExpStructure < SymbolType > iterate(const regexp::FormalRegExpStructure < SymbolType > & regexp); + template < class SymbolType > + static regexp::UnboundedRegExp < SymbolType > iterate(const regexp::UnboundedRegExp < SymbolType > & regexp); + template < class SymbolType > + static regexp::UnboundedRegExpStructure < SymbolType > iterate(const regexp::UnboundedRegExpStructure < SymbolType > & regexp); }; +template < class SymbolType > +regexp::FormalRegExp < SymbolType > RegExpIterate::iterate(const regexp::FormalRegExp < SymbolType > & regexp) { + return regexp::FormalRegExp < SymbolType >(RegExpIterate::iterate(regexp.getRegExp())); +} + +template < class SymbolType > +regexp::FormalRegExpStructure < SymbolType > RegExpIterate::iterate(const regexp::FormalRegExpStructure < SymbolType > & regexp) { + return regexp::FormalRegExpStructure < SymbolType >(regexp::FormalRegExpIteration < SymbolType > (regexp.getStructure())); +} + +template < class SymbolType > +regexp::UnboundedRegExp < SymbolType > RegExpIterate::iterate(const regexp::UnboundedRegExp < SymbolType > & regexp) { + return regexp::UnboundedRegExp < SymbolType >(RegExpIterate::iterate(regexp.getRegExp())); +} + +template < class SymbolType > +regexp::UnboundedRegExpStructure < SymbolType > RegExpIterate::iterate(const regexp::UnboundedRegExpStructure < SymbolType > & regexp) { + return regexp::UnboundedRegExpStructure < SymbolType >(regexp::UnboundedRegExpIteration < SymbolType > (regexp.getStructure())); +} + } /* namespace regexp */ #endif /* REG_EXP_ITERATE_H_ */