From 8e28c58104473dbaa743ae3e7c267a5f21e87793 Mon Sep 17 00:00:00 2001 From: Jan Travnicek <Jan.Travnicek@fit.cvut.cz> Date: Tue, 24 May 2016 20:04:07 +0200 Subject: [PATCH] simplify derivate, integral, factory --- .../regexp/generate/RandomRegExpFactory.cpp | 31 +-- .../src/regexp/generate/RandomRegExpFactory.h | 2 +- .../src/regexp/transform/RegExpDerivation.cpp | 211 +++++++----------- .../src/regexp/transform/RegExpIntegral.cpp | 204 ++++++----------- .../transform/RegExpConcatenateTest.cpp | 6 +- 5 files changed, 163 insertions(+), 291 deletions(-) diff --git a/alib2algo/src/regexp/generate/RandomRegExpFactory.cpp b/alib2algo/src/regexp/generate/RandomRegExpFactory.cpp index 020566de23..dedd3371ff 100644 --- a/alib2algo/src/regexp/generate/RandomRegExpFactory.cpp +++ b/alib2algo/src/regexp/generate/RandomRegExpFactory.cpp @@ -67,16 +67,12 @@ regexp::UnboundedRegExp RandomRegExpFactory::generateUnboundedRegExp( size_t lea } regexp::UnboundedRegExp RandomRegExpFactory::SimpleUnboundedRegExp( size_t n, size_t h, const std::vector<const regexp::UnboundedRegExpElement*> & elems) { - - const regexp::UnboundedRegExpElement* elem = SimpleUnboundedRegExpElement(n, h, elems); - regexp::UnboundedRegExp res(std::move(*elem)); - delete elem; - return res; + return regexp::UnboundedRegExp (SimpleUnboundedRegExpElement(n, h, elems)); } -const regexp::UnboundedRegExpElement* RandomRegExpFactory::SimpleUnboundedRegExpElement(size_t n, size_t h, const std::vector<const regexp::UnboundedRegExpElement*> & elems) { +std::rvalue_ref < regexp::UnboundedRegExpElement > RandomRegExpFactory::SimpleUnboundedRegExpElement(size_t n, size_t h, const std::vector<const regexp::UnboundedRegExpElement*> & elems) { if(h == 0 || n == 0) { - return elems[ std::random_devices::semirandom() % elems.size( ) ]->clone(); + return std::rvalue_ref < UnboundedRegExpElement > ( elems[ std::random_devices::semirandom() % elems.size( ) ]->clone() ); } else { unsigned childNodes = std::random_devices::semirandom() % 10; if(childNodes < 4) childNodes = 1; @@ -115,29 +111,22 @@ const regexp::UnboundedRegExpElement* RandomRegExpFactory::SimpleUnboundedRegExp subSizes[0] = n - subSizes[1]; } if(childNodes == 1) { - const regexp::UnboundedRegExpElement* subElem = SimpleUnboundedRegExpElement(n, h - 1, elems); - regexp::UnboundedRegExpIteration iter(std::move(*subElem)); - delete subElem; - return new regexp::UnboundedRegExpIteration(iter); + return std::rvalue_ref < regexp::UnboundedRegExpElement> (new regexp::UnboundedRegExpIteration (SimpleUnboundedRegExpElement(n, h - 1, elems))); } int nodeType = std::random_devices::semirandom() % 2; if(nodeType == 0) { - regexp::UnboundedRegExpConcatenation con; + regexp::UnboundedRegExpConcatenation* con = new regexp::UnboundedRegExpConcatenation(); for(unsigned i = 0; i < childNodes; i++) { - const regexp::UnboundedRegExpElement* subElem = SimpleUnboundedRegExpElement(subSizes[i], h - 1, elems); - con.appendElement(std::move(*subElem)); - delete subElem; + con->appendElement(std::move(SimpleUnboundedRegExpElement(subSizes[i], h - 1, elems))); } - return new regexp::UnboundedRegExpConcatenation(con); + return std::rvalue_ref < regexp::UnboundedRegExpElement > ( con ); } else { - regexp::UnboundedRegExpAlternation con; + regexp::UnboundedRegExpAlternation* alt = new regexp::UnboundedRegExpAlternation(); for(unsigned i = 0; i < childNodes; i++) { - const regexp::UnboundedRegExpElement* subElem = SimpleUnboundedRegExpElement(subSizes[i], h - 1, elems); - con.appendElement(std::move(*subElem)); - delete subElem; + alt->appendElement(std::move(SimpleUnboundedRegExpElement(subSizes[i], h - 1, elems))); } - return new regexp::UnboundedRegExpAlternation(con); + return std::rvalue_ref < regexp::UnboundedRegExpElement > (alt); } } diff --git a/alib2algo/src/regexp/generate/RandomRegExpFactory.h b/alib2algo/src/regexp/generate/RandomRegExpFactory.h index e55cd443bd..c56d5031d2 100644 --- a/alib2algo/src/regexp/generate/RandomRegExpFactory.h +++ b/alib2algo/src/regexp/generate/RandomRegExpFactory.h @@ -24,7 +24,7 @@ public: private: static regexp::UnboundedRegExp SimpleUnboundedRegExp( size_t n, size_t h, const std::vector<const regexp::UnboundedRegExpElement*> & elems); - static const regexp::UnboundedRegExpElement* SimpleUnboundedRegExpElement(size_t n, size_t h, const std::vector<const regexp::UnboundedRegExpElement*> & elems); + static std::rvalue_ref < regexp::UnboundedRegExpElement > SimpleUnboundedRegExpElement(size_t n, size_t h, const std::vector<const regexp::UnboundedRegExpElement*> & elems); }; } /* namespace generate */ diff --git a/alib2algo/src/regexp/transform/RegExpDerivation.cpp b/alib2algo/src/regexp/transform/RegExpDerivation.cpp index 8a4fdf13b3..735b8bba8a 100644 --- a/alib2algo/src/regexp/transform/RegExpDerivation.cpp +++ b/alib2algo/src/regexp/transform/RegExpDerivation.cpp @@ -18,206 +18,155 @@ 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) -{ - const regexp::FormalRegExpElement* newRegExp = regexp.getRegExp().clone(); +regexp::FormalRegExp RegExpDerivation::derivation(const regexp::FormalRegExp& regexp, const string::LinearString& string) { + std::unique_ptr < regexp::FormalRegExpElement > newRegExp ( regexp.getRegExp().clone() ); - for(const auto& symbol : string.getContent()) - { - std::pair<alphabet::Symbol, regexp::FormalRegExpElement*> out(symbol, nullptr); + for(const auto& symbol : string.getContent()) { + std::pair<alphabet::Symbol, std::unique_ptr < regexp::FormalRegExpElement > > out(symbol, nullptr); newRegExp->Accept((void*) &out, RegExpDerivation::REGEXP_DERIVATION); - delete newRegExp; - newRegExp = out.second; + newRegExp = std::move ( out.second ); } - regexp::FormalRegExp res(std::move(*newRegExp)); - delete newRegExp; - return res; + return regexp::FormalRegExp ( 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) -{ - const regexp::UnboundedRegExpElement* newRegExp = regexp.getRegExp().clone(); +regexp::UnboundedRegExp RegExpDerivation::derivation(const regexp::UnboundedRegExp& regexp, const string::LinearString& string) { + std::unique_ptr < regexp::UnboundedRegExpElement > newRegExp ( regexp.getRegExp().clone() ); - for(const auto& symbol : string.getContent()) - { - std::pair<alphabet::Symbol, regexp::UnboundedRegExpElement*> out(symbol, nullptr); + for(const auto& symbol : string.getContent()) { + std::pair<alphabet::Symbol, std::unique_ptr < regexp::UnboundedRegExpElement > > out(symbol, nullptr); newRegExp->Accept((void*) &out, RegExpDerivation::REGEXP_DERIVATION); - delete newRegExp; - newRegExp = out.second; + newRegExp = std::move ( out.second ); } - regexp::UnboundedRegExp res(std::move(*newRegExp)); - delete newRegExp; - return res; + return regexp::UnboundedRegExp (std::move (* newRegExp)); } auto RegExpDerivationUnboundedRegExp = RegExpDerivation::RegistratorWrapper<regexp::UnboundedRegExp, regexp::UnboundedRegExp>(RegExpDerivation::derivation); // ---------------------------------------------------------------------------- -void RegExpDerivation::Visit(void* userData, const regexp::FormalRegExpAlternation& alternation) const -{ - std::pair<alphabet::Symbol, regexp::FormalRegExpElement*> &out = *((std::pair<alphabet::Symbol, regexp::FormalRegExpElement*>*) userData); +void RegExpDerivation::Visit(void* userData, const regexp::FormalRegExpAlternation& alternation) const { + std::pair<alphabet::Symbol, std::unique_ptr < regexp::FormalRegExpElement>> &out = *((std::pair<alphabet::Symbol, std::unique_ptr < regexp::FormalRegExpElement > >*) userData); - static_cast<const regexp::FormalRegExpElement&>(alternation.getLeftElement()).Accept(userData, *this); - const regexp::FormalRegExpElement* leftDerivative = out.second; + alternation.getLeftElement().Accept(userData, *this); + std::unique_ptr < regexp::FormalRegExpElement > leftDerivative = std::move ( out.second ); - static_cast<const regexp::FormalRegExpElement&>(alternation.getRightElement()).Accept(userData, *this); - const regexp::FormalRegExpElement* rightDerivative = out.second; + alternation.getRightElement().Accept(userData, *this); - out.second = new regexp::FormalRegExpAlternation(std::move(*leftDerivative), std::move(*rightDerivative)); - delete leftDerivative; - delete rightDerivative; + out.second = std::unique_ptr < FormalRegExpElement > ( new regexp::FormalRegExpAlternation( std::move ( * leftDerivative ), std::move ( * out.second ) ) ); } -void RegExpDerivation::Visit(void* userData, const regexp::FormalRegExpConcatenation& concatenation) const -{ - std::pair<alphabet::Symbol, regexp::FormalRegExpElement*> &out = *((std::pair<alphabet::Symbol, regexp::FormalRegExpElement*>*) userData); - - static_cast<const regexp::FormalRegExpElement&>(concatenation.getLeftElement()).Accept(userData, *this); - const regexp::FormalRegExpElement* leftDerivative = out.second; - const regexp::FormalRegExpElement* rightElement = concatenation.getRightElement().clone(); - out.second = new regexp::FormalRegExpConcatenation(std::move(*leftDerivative), std::move(*rightElement)); - delete leftDerivative; - delete rightElement; - - if(regexp::properties::RegExpEpsilon::languageContainsEpsilon(concatenation.getLeftElement())) - { - const regexp::FormalRegExpElement *alternation = out.second, *rightDerivative = nullptr; - static_cast<const regexp::FormalRegExpElement&>(concatenation.getRightElement()).Accept(userData, *this); - rightDerivative = out.second; - - out.second = new regexp::FormalRegExpAlternation(std::move(*alternation), std::move(*rightDerivative)); - delete alternation; - delete rightDerivative; +void RegExpDerivation::Visit(void* userData, const regexp::FormalRegExpConcatenation& concatenation) const { + std::pair<alphabet::Symbol, std::unique_ptr < regexp::FormalRegExpElement>> &out = *((std::pair<alphabet::Symbol, std::unique_ptr < regexp::FormalRegExpElement > >*) userData); + + concatenation.getLeftElement().Accept(userData, *this); + + out.second = std::unique_ptr < FormalRegExpElement > ( new regexp::FormalRegExpConcatenation( std::move ( * out.second ), std::move_copy ( concatenation.getRightElement ( ) ) ) ); + + if(regexp::properties::RegExpEpsilon::languageContainsEpsilon(concatenation.getLeftElement())) { + std::unique_ptr < regexp::FormalRegExpElement > alternation = std::move ( out.second ); + concatenation.getRightElement().Accept(userData, *this); + + out.second = std::unique_ptr < FormalRegExpElement > ( new regexp::FormalRegExpAlternation ( std::move ( * alternation ), std::move( * out.second ) ) ); } } -void RegExpDerivation::Visit(void* userData, const regexp::FormalRegExpIteration& iteration) const -{ - std::pair<alphabet::Symbol, regexp::FormalRegExpElement*> &out = *((std::pair<alphabet::Symbol, regexp::FormalRegExpElement*>*) userData); +void RegExpDerivation::Visit(void* userData, const regexp::FormalRegExpIteration& iteration) const { + std::pair<alphabet::Symbol, std::unique_ptr < regexp::FormalRegExpElement>> &out = *((std::pair<alphabet::Symbol, std::unique_ptr < regexp::FormalRegExpElement > >*) userData); - static_cast<const regexp::FormalRegExpElement&>(iteration.getElement()).Accept(userData, *this); - const regexp::FormalRegExpElement* iterDerivative = out.second; - const regexp::FormalRegExpElement* iter = iteration.clone(); + iteration.getElement().Accept(userData, *this); - out.second = new regexp::FormalRegExpConcatenation(std::move(*iterDerivative), std::move(*iter)); - delete iterDerivative; - delete iter; + out.second = std::unique_ptr < FormalRegExpElement > ( new regexp::FormalRegExpConcatenation( std::move ( * out.second ), iteration ) ); } -void RegExpDerivation::Visit(void* userData, const regexp::FormalRegExpSymbol& symbol) const -{ - std::pair<alphabet::Symbol, regexp::FormalRegExpElement*> &out = *((std::pair<alphabet::Symbol, regexp::FormalRegExpElement*>*) userData); +void RegExpDerivation::Visit(void* userData, const regexp::FormalRegExpSymbol& symbol) const { + std::pair<alphabet::Symbol, std::unique_ptr < regexp::FormalRegExpElement>> &out = *((std::pair<alphabet::Symbol, std::unique_ptr < regexp::FormalRegExpElement > >*) userData); if(out.first == symbol.getSymbol()) - out.second = new regexp::FormalRegExpEpsilon(); + out.second = std::unique_ptr < FormalRegExpElement > ( new regexp::FormalRegExpEpsilon() ); else - out.second = new regexp::FormalRegExpEmpty(); + out.second = std::unique_ptr < FormalRegExpElement > ( new regexp::FormalRegExpEmpty() ); } -void RegExpDerivation::Visit(void* userData, const regexp::FormalRegExpEpsilon&) const -{ - std::pair<alphabet::Symbol, regexp::FormalRegExpElement*> &out = *((std::pair<alphabet::Symbol, regexp::FormalRegExpElement*>*) userData); - out.second = new regexp::FormalRegExpEmpty(); +void RegExpDerivation::Visit(void* userData, const regexp::FormalRegExpEpsilon&) const { + std::pair<alphabet::Symbol, std::unique_ptr < regexp::FormalRegExpElement>> &out = *((std::pair<alphabet::Symbol, std::unique_ptr < regexp::FormalRegExpElement > >*) userData); + out.second = std::unique_ptr < FormalRegExpElement > ( new regexp::FormalRegExpEmpty() ); } -void RegExpDerivation::Visit(void* userData, const regexp::FormalRegExpEmpty&) const -{ - std::pair<alphabet::Symbol, regexp::FormalRegExpElement*> &out = *((std::pair<alphabet::Symbol, regexp::FormalRegExpElement*>*) userData); - out.second = new regexp::FormalRegExpEmpty(); +void RegExpDerivation::Visit(void* userData, const regexp::FormalRegExpEmpty&) const { + std::pair<alphabet::Symbol, std::unique_ptr < regexp::FormalRegExpElement>> &out = *((std::pair<alphabet::Symbol, std::unique_ptr < regexp::FormalRegExpElement > >*) userData); + out.second = std::unique_ptr < FormalRegExpElement > ( new regexp::FormalRegExpEmpty() ); } // ---------------------------------------------------------------------------- -void RegExpDerivation::Visit(void* userData, const regexp::UnboundedRegExpAlternation& alternation) const -{ - std::pair<alphabet::Symbol, regexp::UnboundedRegExpElement*> &out = *((std::pair<alphabet::Symbol, regexp::UnboundedRegExpElement*>*) userData); - regexp::UnboundedRegExpAlternation* ret = new regexp::UnboundedRegExpAlternation(); - - for(const auto& child : alternation.getElements()) - { - static_cast<const regexp::UnboundedRegExpElement&>(*child).Accept(userData, *this); - ret->appendElement(std::move(*out.second)); - delete out.second; +void RegExpDerivation::Visit(void* userData, const regexp::UnboundedRegExpAlternation& alternation) const { + std::pair<alphabet::Symbol, std::unique_ptr < regexp::UnboundedRegExpElement > > &out = *((std::pair<alphabet::Symbol, std::unique_ptr < regexp::UnboundedRegExpElement>>*) userData); + std::unique_ptr < regexp::UnboundedRegExpAlternation > ret ( new regexp::UnboundedRegExpAlternation() ); + + for(const auto& child : alternation.getElements()) { + child->Accept(userData, *this); + ret->appendElement(std::move( * out.second ) ); } - out.second = ret; + out.second = std::move ( ret ); } -void RegExpDerivation::Visit(void* userData, const regexp::UnboundedRegExpConcatenation& concatenation) const -{ - std::pair<alphabet::Symbol, regexp::UnboundedRegExpElement*> &out = *((std::pair<alphabet::Symbol, regexp::UnboundedRegExpElement*>*) userData); - regexp::UnboundedRegExpAlternation* ret = new regexp::UnboundedRegExpAlternation(); +void RegExpDerivation::Visit(void* userData, const regexp::UnboundedRegExpConcatenation& concatenation) const { + std::pair<alphabet::Symbol, std::unique_ptr < regexp::UnboundedRegExpElement > > &out = *((std::pair<alphabet::Symbol, std::unique_ptr < regexp::UnboundedRegExpElement>>*) userData); + std::unique_ptr < regexp::UnboundedRegExpAlternation > ret ( new regexp::UnboundedRegExpAlternation() ); - for(auto child = concatenation.getElements().begin(); child != concatenation.getElements().end(); child ++) - { - regexp::UnboundedRegExpConcatenation* concat = new regexp::UnboundedRegExpConcatenation(); - static_cast<const regexp::UnboundedRegExpElement&>(**child).Accept(userData, *this); - concat->appendElement(std::move(*out.second)); - delete out.second; + for(auto child = concatenation.getElements().begin(); child != concatenation.getElements().end(); child ++) { + std::unique_ptr < regexp::UnboundedRegExpConcatenation > concat ( new regexp::UnboundedRegExpConcatenation() ); + (*child)->Accept(userData, *this); + concat->appendElement( std::move ( * out.second ) ); auto succeedingElement = child; while(++succeedingElement != concatenation.getElements().end()) - { - regexp::UnboundedRegExpElement* tmp = (*succeedingElement)->clone(); - concat->appendElement(std::move(*tmp)); - delete tmp; - } + concat->appendElement(**succeedingElement); ret->appendElement(std::move(*concat)); - delete concat; - if(regexp::properties::RegExpEpsilon::languageContainsEpsilon(**child)) - continue; // this IF construct is intentional "to match algorithm" - break; + if( ! regexp::properties::RegExpEpsilon::languageContainsEpsilon(**child) ) + break; } - out.second = ret; + out.second = std::move ( ret ); } -void RegExpDerivation::Visit(void* userData, const regexp::UnboundedRegExpIteration& iteration) const -{ - std::pair<alphabet::Symbol, regexp::UnboundedRegExpElement*> &out = *((std::pair<alphabet::Symbol, regexp::UnboundedRegExpElement*>*) userData); - - static_cast<const regexp::UnboundedRegExpElement&>(iteration.getElement()).Accept(userData, *this); - const regexp::UnboundedRegExpElement* iterDerivative = out.second; - const regexp::UnboundedRegExpElement* iter = iteration.clone(); +void RegExpDerivation::Visit(void* userData, const regexp::UnboundedRegExpIteration& iteration) const { + std::pair<alphabet::Symbol, std::unique_ptr < regexp::UnboundedRegExpElement > > &out = *((std::pair<alphabet::Symbol, std::unique_ptr < regexp::UnboundedRegExpElement>>*) userData); - regexp::UnboundedRegExpConcatenation* ret = new regexp::UnboundedRegExpConcatenation(); - ret->appendElement(std::move(*iterDerivative)); - ret->appendElement(std::move(*iter)); - delete iterDerivative; - delete iter; + iteration.getElement().Accept(userData, *this); - out.second = ret; + UnboundedRegExpConcatenation * con = new regexp::UnboundedRegExpConcatenation( ); + con->appendElement ( std::move ( * out.second ) ); + con->appendElement ( iteration ); + out.second = std::unique_ptr < UnboundedRegExpElement > ( con ); } -void RegExpDerivation::Visit(void* userData, const regexp::UnboundedRegExpSymbol& symbol) const -{ - std::pair<alphabet::Symbol, regexp::UnboundedRegExpElement*> &out = *((std::pair<alphabet::Symbol, regexp::UnboundedRegExpElement*>*) userData); +void RegExpDerivation::Visit(void* userData, const regexp::UnboundedRegExpSymbol& symbol) const { + std::pair<alphabet::Symbol, std::unique_ptr < regexp::UnboundedRegExpElement > > &out = *((std::pair<alphabet::Symbol, std::unique_ptr < regexp::UnboundedRegExpElement>>*) userData); if(out.first == symbol.getSymbol()) - out.second = new regexp::UnboundedRegExpEpsilon(); + out.second = std::unique_ptr < UnboundedRegExpElement > ( new regexp::UnboundedRegExpEpsilon() ); else - out.second = new regexp::UnboundedRegExpEmpty(); + out.second = std::unique_ptr < UnboundedRegExpElement > ( new regexp::UnboundedRegExpEmpty() ); } -void RegExpDerivation::Visit(void* userData, const regexp::UnboundedRegExpEpsilon&) const -{ - std::pair<alphabet::Symbol, regexp::UnboundedRegExpElement*> &out = *((std::pair<alphabet::Symbol, regexp::UnboundedRegExpElement*>*) userData); - out.second = new regexp::UnboundedRegExpEmpty(); +void RegExpDerivation::Visit(void* userData, const regexp::UnboundedRegExpEpsilon&) const { + std::pair<alphabet::Symbol, std::unique_ptr < regexp::UnboundedRegExpElement > > &out = *((std::pair<alphabet::Symbol, std::unique_ptr < regexp::UnboundedRegExpElement>>*) userData); + out.second = std::unique_ptr < UnboundedRegExpElement > ( new regexp::UnboundedRegExpEmpty() ); } -void RegExpDerivation::Visit(void* userData, const regexp::UnboundedRegExpEmpty&) const -{ - std::pair<alphabet::Symbol, regexp::UnboundedRegExpElement*> &out = *((std::pair<alphabet::Symbol, regexp::UnboundedRegExpElement*>*) userData); - out.second = new regexp::UnboundedRegExpEmpty(); +void RegExpDerivation::Visit(void* userData, const regexp::UnboundedRegExpEmpty&) const { + std::pair<alphabet::Symbol, std::unique_ptr < regexp::UnboundedRegExpElement > > &out = *((std::pair<alphabet::Symbol, std::unique_ptr < regexp::UnboundedRegExpElement>>*) userData); + out.second = std::unique_ptr < UnboundedRegExpElement > ( new regexp::UnboundedRegExpEmpty() ); } const RegExpDerivation RegExpDerivation::REGEXP_DERIVATION; diff --git a/alib2algo/src/regexp/transform/RegExpIntegral.cpp b/alib2algo/src/regexp/transform/RegExpIntegral.cpp index 35036c9e60..80444ede0f 100644 --- a/alib2algo/src/regexp/transform/RegExpIntegral.cpp +++ b/alib2algo/src/regexp/transform/RegExpIntegral.cpp @@ -9,206 +9,140 @@ #include <regexp/unbounded/UnboundedRegExpElements.h> #include <regexp/formal/FormalRegExpElements.h> -namespace regexp -{ +namespace regexp{ -regexp::RegExp RegExpIntegral::integral(const regexp::RegExp& regexp, const string::LinearString& string) -{ +regexp::RegExp RegExpIntegral::integral(const regexp::RegExp& regexp, const string::LinearString& string) { return dispatch(regexp.getData(), string); } -regexp::FormalRegExp RegExpIntegral::integral(const regexp::FormalRegExp& regexp, const string::LinearString& string) -{ - const regexp::FormalRegExpElement* newRegExp = regexp.getRegExp().clone(); - - for(auto it = string.getContent().begin(); it != string.getContent().end(); it ++) - { - const auto& symbol = *it; +regexp::FormalRegExp RegExpIntegral::integral(const regexp::FormalRegExp& regexp, const string::LinearString& string) { + std::unique_ptr < regexp::FormalRegExpElement > newRegExp ( regexp.getRegExp().clone() ); - std::pair<alphabet::Symbol, regexp::FormalRegExpElement*> out(symbol, nullptr); + for(const auto& symbol : string.getContent()) { + std::pair<alphabet::Symbol, std::unique_ptr < regexp::FormalRegExpElement > > out(symbol, nullptr); newRegExp->Accept((void*) &out, RegExpIntegral::REGEXP_INTEGRAL); - delete newRegExp; - newRegExp = out.second; + newRegExp = std::move ( out.second ); } - regexp::FormalRegExp res(std::move(*newRegExp)); - delete newRegExp; - return res; + return regexp::FormalRegExp (* newRegExp); } auto RegExpIntegralFormalRegExp = RegExpIntegral::RegistratorWrapper<regexp::FormalRegExp, regexp::FormalRegExp>(RegExpIntegral::integral); -regexp::UnboundedRegExp RegExpIntegral::integral(const regexp::UnboundedRegExp& regexp, const string::LinearString& string) -{ - const regexp::UnboundedRegExpElement* newRegExp = regexp.getRegExp().clone(); - - for(auto it = string.getContent().begin(); it != string.getContent().end(); it ++) - { - const auto& symbol = *it; +regexp::UnboundedRegExp RegExpIntegral::integral(const regexp::UnboundedRegExp& regexp, const string::LinearString& string) { + std::unique_ptr < regexp::UnboundedRegExpElement > newRegExp ( regexp.getRegExp().clone() ); - std::pair<alphabet::Symbol, regexp::UnboundedRegExpElement*> out(symbol, nullptr); + for(const auto& symbol : string.getContent()) { + std::pair<alphabet::Symbol, std::unique_ptr < regexp::UnboundedRegExpElement > > out(symbol, nullptr); newRegExp->Accept((void*) &out, RegExpIntegral::REGEXP_INTEGRAL); - delete newRegExp; - newRegExp = out.second; + newRegExp = std::move ( out.second ); } - regexp::UnboundedRegExp res(std::move(*newRegExp)); - delete newRegExp; - return res; + return regexp::UnboundedRegExp (* newRegExp); } auto RegExpIntegralUnboundedRegExp = RegExpIntegral::RegistratorWrapper<regexp::UnboundedRegExp, regexp::UnboundedRegExp>(RegExpIntegral::integral); // ---------------------------------------------------------------------------- -void RegExpIntegral::Visit(void* userData, const regexp::FormalRegExpAlternation& alternation) const -{ - std::pair<alphabet::Symbol, regexp::FormalRegExpElement*> &out = *((std::pair<alphabet::Symbol, regexp::FormalRegExpElement*>*) userData); +void RegExpIntegral::Visit(void* userData, const regexp::FormalRegExpAlternation& alternation) const { + std::pair<alphabet::Symbol, std::unique_ptr < regexp::FormalRegExpElement>> &out = *((std::pair<alphabet::Symbol, std::unique_ptr < regexp::FormalRegExpElement > >*) userData); - static_cast<const regexp::FormalRegExpElement&>(alternation.getLeftElement()).Accept(userData, *this); - const regexp::FormalRegExpElement* leftIntegral = out.second; + alternation.getLeftElement().Accept(userData, *this); + std::unique_ptr < regexp::FormalRegExpElement > leftIntegral = std::move ( out.second ); - static_cast<const regexp::FormalRegExpElement&>(alternation.getRightElement()).Accept(userData, *this); - const regexp::FormalRegExpElement* rightIntegral = out.second; + alternation.getRightElement().Accept(userData, *this); - out.second = new regexp::FormalRegExpAlternation(std::move(*leftIntegral), std::move(*rightIntegral)); - delete leftIntegral; - delete rightIntegral; + out.second = std::unique_ptr < FormalRegExpElement > ( new regexp::FormalRegExpAlternation( std::move ( * leftIntegral ), std::move ( * out.second ) ) ); } -void RegExpIntegral::Visit(void* userData, const regexp::FormalRegExpConcatenation& concatenation) const -{ - std::pair<alphabet::Symbol, regexp::FormalRegExpElement*> &out = *((std::pair<alphabet::Symbol, regexp::FormalRegExpElement*>*) userData); - - regexp::FormalRegExpSymbol* symb = new regexp::FormalRegExpSymbol(out.first); - regexp::FormalRegExpElement* concat = concatenation.clone(); - regexp::FormalRegExpConcatenation* ret = new regexp::FormalRegExpConcatenation(std::move(*symb), std::move(*concat)); +void RegExpIntegral::Visit(void* userData, const regexp::FormalRegExpConcatenation& concatenation) const { + std::pair<alphabet::Symbol, std::unique_ptr < regexp::FormalRegExpElement>> &out = *((std::pair<alphabet::Symbol, std::unique_ptr < regexp::FormalRegExpElement > >*) userData); - delete concat; - delete symb; - - out.second = ret; + out.second = std::unique_ptr < regexp::FormalRegExpElement > ( new regexp::FormalRegExpConcatenation ( regexp::FormalRegExpSymbol ( out.first ), concatenation ) ); } -void RegExpIntegral::Visit(void* userData, const regexp::FormalRegExpIteration& iteration) const -{ - std::pair<alphabet::Symbol, regexp::FormalRegExpElement*> &out = *((std::pair<alphabet::Symbol, regexp::FormalRegExpElement*>*) userData); +void RegExpIntegral::Visit(void* userData, const regexp::FormalRegExpIteration& iteration) const { + std::pair<alphabet::Symbol, std::unique_ptr < regexp::FormalRegExpElement>> &out = *((std::pair<alphabet::Symbol, std::unique_ptr < regexp::FormalRegExpElement > >*) userData); - regexp::FormalRegExpSymbol* symb = new regexp::FormalRegExpSymbol(out.first); - regexp::FormalRegExpElement* iter = iteration.clone(); - regexp::FormalRegExpConcatenation* ret = new regexp::FormalRegExpConcatenation(std::move(*symb), std::move(*iter)); - - delete iter; - delete symb; - - out.second = ret; + out.second = std::unique_ptr < regexp::FormalRegExpElement > ( new regexp::FormalRegExpConcatenation ( regexp::FormalRegExpSymbol ( out.first ), iteration ) ); } -void RegExpIntegral::Visit(void* userData, const regexp::FormalRegExpSymbol& symbol) const -{ - std::pair<alphabet::Symbol, regexp::FormalRegExpElement*> &out = *((std::pair<alphabet::Symbol, regexp::FormalRegExpElement*>*) userData); - - regexp::FormalRegExpSymbol* symb = new regexp::FormalRegExpSymbol(out.first); - regexp::FormalRegExpElement* symb2 = symbol.clone(); - regexp::FormalRegExpConcatenation* ret = new regexp::FormalRegExpConcatenation(std::move(*symb), std::move(*symb2)); - - delete symb2; - delete symb; +void RegExpIntegral::Visit(void* userData, const regexp::FormalRegExpSymbol& symbol) const { + std::pair<alphabet::Symbol, std::unique_ptr < regexp::FormalRegExpElement>> &out = *((std::pair<alphabet::Symbol, std::unique_ptr < regexp::FormalRegExpElement > >*) userData); - out.second = ret; + out.second = std::unique_ptr < regexp::FormalRegExpElement > ( new regexp::FormalRegExpConcatenation ( regexp::FormalRegExpSymbol ( out.first ), symbol ) ); } -void RegExpIntegral::Visit(void* userData, const regexp::FormalRegExpEpsilon&) const -{ - std::pair<alphabet::Symbol, regexp::FormalRegExpElement*> &out = *((std::pair<alphabet::Symbol, regexp::FormalRegExpElement*>*) userData); - regexp::FormalRegExpSymbol* symb = new regexp::FormalRegExpSymbol(out.first); - out.second = symb; +void RegExpIntegral::Visit(void* userData, const regexp::FormalRegExpEpsilon&) const { + std::pair<alphabet::Symbol, std::unique_ptr < regexp::FormalRegExpElement>> &out = *((std::pair<alphabet::Symbol, std::unique_ptr < regexp::FormalRegExpElement > >*) userData); + out.second = std::unique_ptr < regexp::FormalRegExpElement > ( new regexp::FormalRegExpSymbol( out.first ) ); } -void RegExpIntegral::Visit(void* userData, const regexp::FormalRegExpEmpty&) const -{ - std::pair<alphabet::Symbol, regexp::FormalRegExpElement*> &out = *((std::pair<alphabet::Symbol, regexp::FormalRegExpElement*>*) userData); - out.second = new regexp::FormalRegExpEmpty(); +void RegExpIntegral::Visit(void* userData, const regexp::FormalRegExpEmpty&) const { + std::pair<alphabet::Symbol, std::unique_ptr < regexp::FormalRegExpElement>> &out = *((std::pair<alphabet::Symbol, std::unique_ptr < regexp::FormalRegExpElement > >*) userData); + out.second = std::unique_ptr < regexp::FormalRegExpElement > ( new regexp::FormalRegExpEmpty( ) ); } // ---------------------------------------------------------------------------- void RegExpIntegral::Visit(void* userData, const regexp::UnboundedRegExpAlternation& alternation) const { - std::pair<alphabet::Symbol, regexp::UnboundedRegExpElement*> &out = *((std::pair<alphabet::Symbol, regexp::UnboundedRegExpElement*>*) userData); - regexp::UnboundedRegExpAlternation* ret = new regexp::UnboundedRegExpAlternation(); - - for(const auto& child : alternation.getElements()) - { - static_cast<const regexp::UnboundedRegExpElement&>(*child).Accept(userData, *this); - ret->appendElement(std::move(*out.second)); - delete out.second; + std::pair<alphabet::Symbol, std::unique_ptr < regexp::UnboundedRegExpElement>> &out = *((std::pair<alphabet::Symbol, std::unique_ptr < regexp::UnboundedRegExpElement > >*) userData); + + regexp::UnboundedRegExpAlternation* alt = new regexp::UnboundedRegExpAlternation(); + + for(const auto& child : alternation.getElements()) { + child->Accept(userData, *this); + alt->appendElement(std::move(*out.second)); } - out.second = ret; + out.second = std::unique_ptr < UnboundedRegExpElement > ( alt ); } -void RegExpIntegral::Visit(void* userData, const regexp::UnboundedRegExpConcatenation& concatenation) const -{ - std::pair<alphabet::Symbol, regexp::UnboundedRegExpElement*> &out = *((std::pair<alphabet::Symbol, regexp::UnboundedRegExpElement*>*) userData); +void RegExpIntegral::Visit(void* userData, const regexp::UnboundedRegExpConcatenation& concatenation) const { + std::pair<alphabet::Symbol, std::unique_ptr < regexp::UnboundedRegExpElement>> &out = *((std::pair<alphabet::Symbol, std::unique_ptr < regexp::UnboundedRegExpElement > >*) userData); - regexp::UnboundedRegExpSymbol* symb = new regexp::UnboundedRegExpSymbol(out.first); - regexp::UnboundedRegExpElement* concat = concatenation.clone(); - regexp::UnboundedRegExpConcatenation* ret = new regexp::UnboundedRegExpConcatenation(); - ret->appendElement(std::move(*symb)); - ret->appendElement(std::move(*concat)); + regexp::UnboundedRegExpConcatenation * con = new regexp::UnboundedRegExpConcatenation( ); - delete concat; - delete symb; + con->appendElement ( regexp::UnboundedRegExpSymbol ( out.first ) ); + for ( const auto & element : concatenation.getElements() ) + con->appendElement ( * element ); - out.second = ret; + out.second = std::unique_ptr < regexp::UnboundedRegExpElement > ( con ); } -void RegExpIntegral::Visit(void* userData, const regexp::UnboundedRegExpIteration& iteration) const -{ - std::pair<alphabet::Symbol, regexp::UnboundedRegExpElement*> &out = *((std::pair<alphabet::Symbol, regexp::UnboundedRegExpElement*>*) userData); - - regexp::UnboundedRegExpSymbol* symb = new regexp::UnboundedRegExpSymbol(out.first); - regexp::UnboundedRegExpElement* iter = iteration.clone(); - regexp::UnboundedRegExpConcatenation* ret = new regexp::UnboundedRegExpConcatenation(); - ret->appendElement(std::move(*symb)); - ret->appendElement(std::move(*iter)); +void RegExpIntegral::Visit(void* userData, const regexp::UnboundedRegExpIteration& iteration) const { + std::pair<alphabet::Symbol, std::unique_ptr < regexp::UnboundedRegExpElement>> &out = *((std::pair<alphabet::Symbol, std::unique_ptr < regexp::UnboundedRegExpElement > >*) userData); - delete iter; - delete symb; + regexp::UnboundedRegExpConcatenation * con = new regexp::UnboundedRegExpConcatenation( ); + con->appendElement ( regexp::UnboundedRegExpSymbol ( out.first ) ); + con->appendElement ( iteration ); - out.second = ret; + out.second = std::unique_ptr < regexp::UnboundedRegExpElement > ( con ); } -void RegExpIntegral::Visit(void* userData, const regexp::UnboundedRegExpSymbol& symbol) const -{ - std::pair<alphabet::Symbol, regexp::UnboundedRegExpElement*> &out = *((std::pair<alphabet::Symbol, regexp::UnboundedRegExpElement*>*) userData); - - regexp::UnboundedRegExpSymbol* symb = new regexp::UnboundedRegExpSymbol(out.first); - regexp::UnboundedRegExpElement* symb2 = symbol.clone(); - regexp::UnboundedRegExpConcatenation* ret = new regexp::UnboundedRegExpConcatenation(); - ret->appendElement(std::move(*symb)); - ret->appendElement(std::move(*symb2)); +void RegExpIntegral::Visit(void* userData, const regexp::UnboundedRegExpSymbol& symbol) const { + std::pair<alphabet::Symbol, std::unique_ptr < regexp::UnboundedRegExpElement>> &out = *((std::pair<alphabet::Symbol, std::unique_ptr < regexp::UnboundedRegExpElement > >*) userData); - delete symb2; - delete symb; + regexp::UnboundedRegExpConcatenation * con = new regexp::UnboundedRegExpConcatenation( ); + con->appendElement ( regexp::UnboundedRegExpSymbol ( out.first ) ); + con->appendElement ( symbol ); - out.second = ret; + out.second = std::unique_ptr < regexp::UnboundedRegExpElement > ( con ); } -void RegExpIntegral::Visit(void* userData, const regexp::UnboundedRegExpEpsilon&) const -{ - std::pair<alphabet::Symbol, regexp::UnboundedRegExpElement*> &out = *((std::pair<alphabet::Symbol, regexp::UnboundedRegExpElement*>*) userData); +void RegExpIntegral::Visit(void* userData, const regexp::UnboundedRegExpEpsilon&) const { + std::pair<alphabet::Symbol, std::unique_ptr < regexp::UnboundedRegExpElement>> &out = *((std::pair<alphabet::Symbol, std::unique_ptr < regexp::UnboundedRegExpElement > >*) userData); - regexp::UnboundedRegExpSymbol* symb = new regexp::UnboundedRegExpSymbol(out.first); - out.second = symb; + out.second = std::unique_ptr < regexp::UnboundedRegExpElement > ( new regexp::UnboundedRegExpSymbol( out.first ) ); } -void RegExpIntegral::Visit(void* userData, const regexp::UnboundedRegExpEmpty&) const -{ - std::pair<alphabet::Symbol, regexp::UnboundedRegExpElement*> &out = *((std::pair<alphabet::Symbol, regexp::UnboundedRegExpElement*>*) userData); - out.second = new regexp::UnboundedRegExpEmpty(); +void RegExpIntegral::Visit(void* userData, const regexp::UnboundedRegExpEmpty&) const { + std::pair<alphabet::Symbol, std::unique_ptr < regexp::UnboundedRegExpElement>> &out = *((std::pair<alphabet::Symbol, std::unique_ptr < regexp::UnboundedRegExpElement > >*) userData); + + out.second = std::unique_ptr < regexp::UnboundedRegExpElement > ( new regexp::UnboundedRegExpEmpty( ) ); } const RegExpIntegral RegExpIntegral::REGEXP_INTEGRAL; diff --git a/alib2algo/test-src/regexp/transform/RegExpConcatenateTest.cpp b/alib2algo/test-src/regexp/transform/RegExpConcatenateTest.cpp index d50d8d4079..ec9d299307 100644 --- a/alib2algo/test-src/regexp/transform/RegExpConcatenateTest.cpp +++ b/alib2algo/test-src/regexp/transform/RegExpConcatenateTest.cpp @@ -54,10 +54,10 @@ void RegExpConcatenateTest::testRegExpConcatenate() { std::string inputr = "(#0 #E )(#E a b)"; regexp::RegExp tmp = alib::StringDataFactory::fromString<regexp::RegExp>(inputr); - regexp::RegExp rer(regexp::FormalRegExp(static_cast<const regexp::UnboundedRegExp&>(tmp.getData()))); + regexp::RegExp rer(regexp::FormalRegExp(dynamic_cast<const regexp::UnboundedRegExp&>(tmp.getData()))); - std::cout << (static_cast<const regexp::UnboundedRegExp&>(tmp.getData())).getAlphabet() << std::endl; - std::cout << (static_cast<const regexp::FormalRegExp&>(tmp.getData())).getAlphabet() << std::endl; + std::cout << (dynamic_cast<const regexp::UnboundedRegExp&>(tmp.getData())).getAlphabet() << std::endl; + std::cout << (dynamic_cast<const regexp::FormalRegExp&>(rer.getData())).getAlphabet() << std::endl; std::cout << re << std::endl; std::cout << rer << std::endl; -- GitLab