diff --git a/alib2algo/src/regexp/simplify/RegExpOptimizeUnboundedPart.cxx b/alib2algo/src/regexp/simplify/RegExpOptimizeUnboundedPart.cxx index 75c1235a44e6cde1ed5a2b7a12024840c4eb4ec7..1546e14a143919ec402df4dac01e4bc8aed6a87b 100644 --- a/alib2algo/src/regexp/simplify/RegExpOptimizeUnboundedPart.cxx +++ b/alib2algo/src/regexp/simplify/RegExpOptimizeUnboundedPart.cxx @@ -588,7 +588,7 @@ bool RegExpOptimize::V5( UnboundedRegExpAlternation & node ) { UnboundedRegExpConcatenation * iterConcat = dynamic_cast<UnboundedRegExpConcatenation*>( iter->getChild().get() ); if( iterConcat->getChildren().size( ) != ( unsigned ) distance( std::next( itC ), concat->getChildren().end( ) ) - || ! equal( iterConcat->getChildren().begin( ), iterConcat->getChildren().end( ), std::next( itC ), + || ! equal( iterConcat->getChildren().begin( ), iterConcat->getChildren().end( ), std::next( itC ), [ ]( const std::smart_ptr < UnboundedRegExpElement > & a, const std::smart_ptr < UnboundedRegExpElement > & b ) -> bool{ return *a == *b; } ) ) { itC++; continue; @@ -604,29 +604,25 @@ bool RegExpOptimize::V5( UnboundedRegExpAlternation & node ) { } // store everything before iteration as "a" - UnboundedRegExpElement * regexpA; + UnboundedRegExpConcatenation tmpAY; if( concat->getChildren().begin( ) == itC ) { - regexpA = new UnboundedRegExpEpsilon( ); + tmpAY.pushBackChild( std::smart_ptr < UnboundedRegExpElement > ( new UnboundedRegExpEpsilon( ) ) ); } else { UnboundedRegExpConcatenation tmpA; tmpA.insert( tmpA.getChildren().end( ), concat->getChildren().begin( ), itC ); - regexpA = optimizeInner( tmpA ); + tmpAY.pushBackChild( std::smart_ptr < UnboundedRegExpElement > ( optimizeInner( tmpA ) ) ); } // store everything behind iteration's followup element as "y" - UnboundedRegExpElement * regexpY; if( itStartY == concat->getChildren().end( ) ) { - regexpY = new UnboundedRegExpEpsilon( ); + tmpAY.pushBackChild( std::smart_ptr < UnboundedRegExpElement > ( new UnboundedRegExpEpsilon( ) ) ); } else { UnboundedRegExpConcatenation tmpY; tmpY.insert( tmpY.getChildren().end( ), itStartY, concat->getChildren().end( ) ); - regexpY = optimizeInner( tmpY ); + tmpAY.pushBackChild( std::smart_ptr < UnboundedRegExpElement > ( optimizeInner( tmpY ) ) ); } // concatenate "a" and "y" and see if they exist somewhere in parent alternation ( node.getChildren() ) - UnboundedRegExpConcatenation tmpAY; - tmpAY.pushBackChild( std::smart_ptr < UnboundedRegExpElement > ( regexpA ) ); - tmpAY.pushBackChild( std::smart_ptr < UnboundedRegExpElement > ( regexpY ) ); UnboundedRegExpElement * regexpAY = optimizeInner( tmpAY ); auto iterAY = find_if( node.getChildren().begin( ), node.getChildren().end( ), [ regexpAY ] ( const std::smart_ptr < UnboundedRegExpElement > & a ) -> bool{ return *a == *regexpAY; } ); @@ -710,30 +706,24 @@ bool RegExpOptimize::V6( UnboundedRegExpAlternation & node ) { advance( itStartX, -1 ); } - // store everything before x as "a" - UnboundedRegExpElement * regexpA; + // concatenate "a" and "y" and see if they exist somewhere in parent alternation ( node->getChildren() ) + UnboundedRegExpConcatenation tmpAY; if( concat->getChildren().begin( ) == itStartX ) { - regexpA = new UnboundedRegExpEpsilon( ); + tmpAY.pushBackChild( std::smart_ptr < UnboundedRegExpElement > ( new UnboundedRegExpEpsilon( ) ) ); } else { UnboundedRegExpConcatenation tmpA; tmpA.insert( tmpA.getChildren().end( ), concat->getChildren().begin( ), itStartX ); - regexpA = optimizeInner( tmpA ); + tmpAY.pushBackChild( std::smart_ptr < UnboundedRegExpElement > ( optimizeInner( tmpA ) ) ); } - // store everything behind iteration's followup element as "y" - UnboundedRegExpElement * regexpY; if( std::next( itC ) == concat->getChildren().end( ) ) { - regexpY = new UnboundedRegExpEpsilon( ); + tmpAY.pushBackChild( std::smart_ptr < UnboundedRegExpElement > ( new UnboundedRegExpEpsilon( ) ) ); } else { UnboundedRegExpConcatenation tmpY; tmpY.insert( tmpY.getChildren().end( ), std::next( itC ), concat->getChildren ( ).end( ) ); - regexpY = optimizeInner( tmpY ); + tmpAY.pushBackChild( std::smart_ptr < UnboundedRegExpElement > ( optimizeInner( tmpY ) ) ); } - // concatenate "a" and "y" and see if they exist somewhere in parent alternation ( node->getChildren() ) - UnboundedRegExpConcatenation tmpAY; - tmpAY.pushBackChild( std::smart_ptr < UnboundedRegExpElement > ( regexpA ) ); - tmpAY.pushBackChild( std::smart_ptr < UnboundedRegExpElement > ( regexpY ) ); UnboundedRegExpElement * regexpAY = optimizeInner( tmpAY ); auto iterAY = find_if( node.getChildren().begin( ), node.getChildren().end( ), [ regexpAY ] ( const std::smart_ptr < UnboundedRegExpElement > & a ) -> bool{ return *a == *regexpAY; } ); @@ -744,10 +734,12 @@ bool RegExpOptimize::V6( UnboundedRegExpAlternation & node ) { continue; } + // if so make a x* y and replace a x x* y tmpAY.insert( tmpAY.getChildren ( ).begin ( ) + 1, * itC ); node.setChild( std::smart_ptr < UnboundedRegExpElement > ( optimizeInner( tmpAY ) ), itA ); + // remove a y itA = node.getChildren().erase( iterAY ); optimized = true;