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

simplify formal regexp optimize

parent fcd0154b
No related branches found
No related tags found
No related merge requests found
...@@ -80,7 +80,7 @@ public: ...@@ -80,7 +80,7 @@ public:
static void optimize( regexp::FormalRegExpElement < SymbolType > & regexp ); static void optimize( regexp::FormalRegExpElement < SymbolType > & regexp );
private: private:
template < class SymbolType > template < class SymbolType >
static regexp::FormalRegExpElement < SymbolType > * optimizeInner( const regexp::FormalRegExpElement < SymbolType > & node ); static ext::smart_ptr < regexp::FormalRegExpElement < SymbolType > > optimizeInner( const regexp::FormalRegExpElement < SymbolType > & node );
   
template < class SymbolType > template < class SymbolType >
static ext::rvalue_ref < regexp::UnboundedRegExpElement < SymbolType > > optimizeInner( const regexp::UnboundedRegExpElement < SymbolType > & node ); static ext::rvalue_ref < regexp::UnboundedRegExpElement < SymbolType > > optimizeInner( const regexp::UnboundedRegExpElement < SymbolType > & node );
...@@ -195,9 +195,9 @@ FormalRegExp < SymbolType > RegExpOptimize::optimize( const FormalRegExp < Symbo ...@@ -195,9 +195,9 @@ FormalRegExp < SymbolType > RegExpOptimize::optimize( const FormalRegExp < Symbo
   
template < class SymbolType > template < class SymbolType >
FormalRegExpStructure < SymbolType > RegExpOptimize::optimize( const FormalRegExpStructure < SymbolType > & regexp ) { FormalRegExpStructure < SymbolType > RegExpOptimize::optimize( const FormalRegExpStructure < SymbolType > & regexp ) {
FormalRegExpElement < SymbolType > * optimized = optimizeInner( regexp.getStructure ( ) ); ext::smart_ptr < FormalRegExpElement < SymbolType > > optimized = optimizeInner( regexp.getStructure ( ) );
   
return regexp::FormalRegExpStructure < SymbolType > ( ext::manage_move ( optimized ) ); return regexp::FormalRegExpStructure < SymbolType > ( * optimized );
} }
   
template < class SymbolType > template < class SymbolType >
......
...@@ -11,40 +11,37 @@ namespace simplify { ...@@ -11,40 +11,37 @@ namespace simplify {
   
template < class SymbolType > template < class SymbolType >
void RegExpOptimize::optimize( FormalRegExpElement < SymbolType > & element ) { void RegExpOptimize::optimize( FormalRegExpElement < SymbolType > & element ) {
FormalRegExpElement < SymbolType >* optimized = optimizeInner( element ); ext::smart_ptr < FormalRegExpElement < SymbolType > > optimized = optimizeInner ( element );
   
FormalRegExpAlternation < SymbolType > * alternation = dynamic_cast<FormalRegExpAlternation < SymbolType > *>( & element ); FormalRegExpAlternation < SymbolType > * alternation = dynamic_cast<FormalRegExpAlternation < SymbolType > *>( & element );
if( alternation ) { if( alternation ) {
FormalRegExpAlternation < SymbolType > * alternationOptimized = dynamic_cast<FormalRegExpAlternation < SymbolType > *>( optimized ); FormalRegExpAlternation < SymbolType > * alternationOptimized = dynamic_cast<FormalRegExpAlternation < SymbolType > * > ( optimized.get ( ) );
if( alternationOptimized ) { if( alternationOptimized ) {
* alternation = std::move( * alternationOptimized ); * alternation = std::move( * alternationOptimized );
delete alternationOptimized;
} else { } else {
* alternation = FormalRegExpAlternation < SymbolType > { ext::manage_move ( optimized ), FormalRegExpEmpty < SymbolType > { } }; * alternation = FormalRegExpAlternation < SymbolType > { * optimized, FormalRegExpEmpty < SymbolType > { } };
} }
return; return;
} }
   
FormalRegExpConcatenation < SymbolType > * concatenation = dynamic_cast<FormalRegExpConcatenation < SymbolType > *>( & element ); FormalRegExpConcatenation < SymbolType > * concatenation = dynamic_cast<FormalRegExpConcatenation < SymbolType > *>( & element );
if( concatenation ) { if( concatenation ) {
FormalRegExpConcatenation < SymbolType > * concatenationOptimized = dynamic_cast<FormalRegExpConcatenation < SymbolType > *>( optimized ); FormalRegExpConcatenation < SymbolType > * concatenationOptimized = dynamic_cast<FormalRegExpConcatenation < SymbolType > * > ( optimized.get ( ) );
if( concatenationOptimized ) { if( concatenationOptimized ) {
* concatenation = std::move( * concatenationOptimized ); * concatenation = std::move( * concatenationOptimized );
delete concatenationOptimized;
} else { } else {
* concatenation = FormalRegExpConcatenation < SymbolType > { ext::manage_move ( optimized ), FormalRegExpEpsilon < SymbolType > { } }; * concatenation = FormalRegExpConcatenation < SymbolType > { * optimized, FormalRegExpEpsilon < SymbolType > { } };
} }
return; return;
} }
   
FormalRegExpIteration < SymbolType > * iteration = dynamic_cast<FormalRegExpIteration < SymbolType > *>( & element ); FormalRegExpIteration < SymbolType > * iteration = dynamic_cast<FormalRegExpIteration < SymbolType > *>( & element );
if( iteration ) { if( iteration ) {
FormalRegExpIteration < SymbolType > * iterationOptimized = dynamic_cast<FormalRegExpIteration < SymbolType > *>( optimized ); FormalRegExpIteration < SymbolType > * iterationOptimized = dynamic_cast<FormalRegExpIteration < SymbolType > *>( optimized.get ( ) );
if( iterationOptimized ) { if( iterationOptimized ) {
* iteration = std::move( * iterationOptimized ); * iteration = std::move( * iterationOptimized );
delete iterationOptimized;
} else { } else {
* iteration = FormalRegExpIteration < SymbolType > { ext::manage_move ( optimized ) }; * iteration = FormalRegExpIteration < SymbolType > { optimized };
} }
return; return;
} }
...@@ -54,15 +51,15 @@ void RegExpOptimize::optimize( FormalRegExpElement < SymbolType > & element ) { ...@@ -54,15 +51,15 @@ void RegExpOptimize::optimize( FormalRegExpElement < SymbolType > & element ) {
} }
   
template < class SymbolType > template < class SymbolType >
FormalRegExpElement < SymbolType > * RegExpOptimize::optimizeInner( const FormalRegExpElement < SymbolType > & node ) { ext::smart_ptr < FormalRegExpElement < SymbolType > > RegExpOptimize::optimizeInner( const FormalRegExpElement < SymbolType > & node ) {
FormalRegExpElement < SymbolType >* elem = node.clone(); FormalRegExpElement < SymbolType > * elem = node.clone();
   
// optimize while you can // optimize while you can
while( A1( elem ) || A2( elem ) || A3( elem ) || A4( elem ) || A10( elem ) || V2( elem ) || V5( elem ) || V6( elem ) || X1( elem ) while( A1( elem ) || A2( elem ) || A3( elem ) || A4( elem ) || A10( elem ) || V2( elem ) || V5( elem ) || V6( elem ) || X1( elem )
|| A5( elem ) || A6( elem ) || A7( elem ) || A8( elem ) || A9( elem ) || V8( elem ) //|| V9( elem ) || A5( elem ) || A6( elem ) || A7( elem ) || A8( elem ) || A9( elem ) || V8( elem ) //|| V9( elem )
|| A11( elem ) || V1( elem ) || V3( elem ) || V4( elem ) || V10( elem ) || S(elem) ); || A11( elem ) || V1( elem ) || V3( elem ) || V4( elem ) || V10( elem ) || S(elem) );
   
return elem; return ext::smart_ptr < FormalRegExpElement < SymbolType > > ( elem );
} }
   
template < class SymbolType > template < class SymbolType >
...@@ -70,16 +67,16 @@ bool RegExpOptimize::S( FormalRegExpElement < SymbolType > * & node ) { ...@@ -70,16 +67,16 @@ bool RegExpOptimize::S( FormalRegExpElement < SymbolType > * & node ) {
bool optimized = false; bool optimized = false;
FormalRegExpAlternation < SymbolType > * alternation = dynamic_cast<FormalRegExpAlternation < SymbolType >*>( node ); FormalRegExpAlternation < SymbolType > * alternation = dynamic_cast<FormalRegExpAlternation < SymbolType >*>( node );
if( alternation ) { if( alternation ) {
FormalRegExpElement < SymbolType > * tmp = optimizeInner ( alternation->getLeftElement ( ) ); ext::smart_ptr < FormalRegExpElement < SymbolType > > tmp = optimizeInner ( alternation->getLeftElement ( ) );
if(* tmp != alternation->getLeftElement ( ) ) { if(* tmp != alternation->getLeftElement ( ) ) {
optimized = true; optimized = true;
alternation->setLeftElement ( * ext::smart_ptr < FormalRegExpElement < SymbolType > > ( tmp ) ); alternation->setLeftElement ( * tmp );
} }
   
tmp = optimizeInner ( alternation->getRightElement ( ) ); tmp = optimizeInner ( alternation->getRightElement ( ) );
if(* tmp != alternation->getRightElement ( ) ) { if(* tmp != alternation->getRightElement ( ) ) {
optimized = true; optimized = true;
alternation->setRightElement ( * ext::smart_ptr < FormalRegExpElement < SymbolType > > ( tmp ) ); alternation->setRightElement ( * tmp );
} }
   
return optimized; return optimized;
...@@ -87,16 +84,16 @@ bool RegExpOptimize::S( FormalRegExpElement < SymbolType > * & node ) { ...@@ -87,16 +84,16 @@ bool RegExpOptimize::S( FormalRegExpElement < SymbolType > * & node ) {
   
FormalRegExpConcatenation < SymbolType > * concatenation = dynamic_cast<FormalRegExpConcatenation < SymbolType >*>( node ); FormalRegExpConcatenation < SymbolType > * concatenation = dynamic_cast<FormalRegExpConcatenation < SymbolType >*>( node );
if( concatenation ) { if( concatenation ) {
FormalRegExpElement < SymbolType >* tmp = optimizeInner ( concatenation->getLeftElement() ); ext::smart_ptr < FormalRegExpElement < SymbolType > > tmp = optimizeInner ( concatenation->getLeftElement() );
if(* tmp != concatenation->getLeftElement ( ) ) { if(* tmp != concatenation->getLeftElement ( ) ) {
optimized = true; optimized = true;
concatenation->setLeftElement ( * ext::smart_ptr < FormalRegExpElement < SymbolType > > ( tmp ) ); concatenation->setLeftElement ( * tmp );
} }
   
tmp = optimizeInner ( concatenation->getRightElement ( )); tmp = optimizeInner ( concatenation->getRightElement ( ));
if(* tmp != concatenation->getRightElement ( )) { if(* tmp != concatenation->getRightElement ( )) {
optimized = true; optimized = true;
concatenation->setRightElement ( * ext::smart_ptr < FormalRegExpElement < SymbolType > > ( tmp ) ); concatenation->setRightElement ( * tmp );
} }
   
return optimized; return optimized;
...@@ -104,11 +101,11 @@ bool RegExpOptimize::S( FormalRegExpElement < SymbolType > * & node ) { ...@@ -104,11 +101,11 @@ bool RegExpOptimize::S( FormalRegExpElement < SymbolType > * & node ) {
   
FormalRegExpIteration < SymbolType > * iteration = dynamic_cast<FormalRegExpIteration < SymbolType >*>( node ); FormalRegExpIteration < SymbolType > * iteration = dynamic_cast<FormalRegExpIteration < SymbolType >*>( node );
if( iteration ) { if( iteration ) {
FormalRegExpElement < SymbolType >* tmp = optimizeInner ( iteration->getElement() ); ext::smart_ptr < FormalRegExpElement < SymbolType > > tmp = optimizeInner ( iteration->getElement() );
   
if(* tmp != iteration->getElement ( ) ) { if(* tmp != iteration->getElement ( ) ) {
optimized = true; optimized = true;
iteration->setElement ( * ext::smart_ptr < FormalRegExpElement < SymbolType > > ( tmp ) ); iteration->setElement ( * tmp );
} }
return optimized; return optimized;
} }
......
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