Skip to content
Snippets Groups Projects
Commit 59ed12c1 authored by Jan Trávníček's avatar Jan Trávníček
Browse files

improve one of an optimization of unbounded regexp

parent d11c789b
No related branches found
No related tags found
1 merge request!166Fix205
Pipeline #99748 passed with warnings
......@@ -530,18 +530,37 @@ template < class SymbolType >
bool RegExpOptimize::Unbounded < SymbolType >::V4( UnboundedRegExpIteration < SymbolType > & node ) {
 
// interpretation: if iteration's element is concat and every concat's element is iteration
UnboundedRegExpConcatenation < SymbolType > * cont = dynamic_cast < UnboundedRegExpConcatenation < SymbolType > * > ( & node.getChild ( ) );
if ( ! cont || ! all_of ( cont->begin( ), cont->end ( ), [ ] ( const UnboundedRegExpElement < SymbolType > & a ) -> bool { return dynamic_cast < const UnboundedRegExpIteration < SymbolType > * > ( & a ); } ) )
return false;
// or if iteration's element is alternation and inside it, there is a concat wher every concat's element is iteration
auto areAllItersInConcat = [] ( UnboundedRegExpElement < SymbolType > & testedNode ) {
UnboundedRegExpConcatenation < SymbolType > * cont = dynamic_cast < UnboundedRegExpConcatenation < SymbolType > * > ( & testedNode );
return cont && all_of ( cont->begin( ), cont->end ( ), [ ] ( const UnboundedRegExpElement < SymbolType > & a ) -> bool { return dynamic_cast < const UnboundedRegExpIteration < SymbolType > * > ( & a ); } );
};
 
UnboundedRegExpAlternation < SymbolType > newAlt;
auto toAlternationOfChildren = [] ( UnboundedRegExpConcatenation < SymbolType > & cont ) {
UnboundedRegExpAlternation < SymbolType > newAlt;
 
for ( UnboundedRegExpElement < SymbolType > & n : * cont )
newAlt.pushBackChild ( std::move ( static_cast < UnboundedRegExpIteration < SymbolType > & > ( n ).getChild ( ) ) );
for ( UnboundedRegExpElement < SymbolType > & n : cont )
newAlt.pushBackChild ( std::move ( static_cast < UnboundedRegExpIteration < SymbolType > & > ( n ).getChild ( ) ) );
 
node.setChild ( Unbounded < SymbolType >::visit ( std::move ( newAlt ), true ) );
return Unbounded < SymbolType >::visit ( std::move ( newAlt ), true );
};
 
return true;
if ( areAllItersInConcat ( node.getChild ( ) ) ) {
node.setChild ( toAlternationOfChildren ( static_cast < UnboundedRegExpConcatenation < SymbolType > & > ( node.getChild ( ) ) ) );
return true;
} else if ( dynamic_cast < UnboundedRegExpAlternation < SymbolType > * > ( & node.getChild ( ) ) ) {
UnboundedRegExpAlternation < SymbolType > & alt = static_cast < UnboundedRegExpAlternation < SymbolType > & > ( node.getChild ( ) );
bool res = false;
for ( size_t i = 0; i < alt.getChildren ( ).size ( ); ++ i ) {
if ( areAllItersInConcat ( alt.getChild ( i ) ) ) {
alt.setChild ( toAlternationOfChildren ( static_cast < UnboundedRegExpConcatenation < SymbolType > & > ( alt.getChild ( i ) ) ), i );
res = true;
}
}
return res;
}
return false;
}
 
/**
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment