From ce161a059dbf3c6267dfeebd129f0d789fd5fa61 Mon Sep 17 00:00:00 2001 From: Tomas Pecka <peckato1@fit.cvut.cz> Date: Wed, 9 Mar 2016 11:28:27 +0100 Subject: [PATCH] formatting --- alib2algo/src/regexp/GlushkovTraversal.cpp | 490 ++++++++++----------- alib2algo/src/regexp/GlushkovTraversal.h | 86 ++-- 2 files changed, 264 insertions(+), 312 deletions(-) diff --git a/alib2algo/src/regexp/GlushkovTraversal.cpp b/alib2algo/src/regexp/GlushkovTraversal.cpp index 465c626fb1..36668e9a00 100644 --- a/alib2algo/src/regexp/GlushkovTraversal.cpp +++ b/alib2algo/src/regexp/GlushkovTraversal.cpp @@ -9,428 +9,382 @@ #include "properties/RegExpEpsilon.h" -namespace regexp -{ +namespace regexp { -const GlushkovSymbol& GlushkovTraversal::findSymbol( regexp::UnboundedRegExpSymbol const * const symbol, const std::set<GlushkovSymbol> & symbolSet ) -{ - auto it = find_if( symbolSet.begin( ), symbolSet.end( ), [ symbol ]( GlushkovSymbol const& gs ) -> bool { - return gs.getSymbolPtr( ) == symbol; - } ); +const GlushkovSymbol & GlushkovTraversal::findSymbol ( regexp::UnboundedRegExpSymbol const * const symbol, const std::set < GlushkovSymbol > & symbolSet ) { + auto it = find_if ( symbolSet.begin ( ), symbolSet.end ( ), [symbol] ( GlushkovSymbol const & gs ) -> bool { + return gs.getSymbolPtr ( ) == symbol; + } ); - if( it == symbolSet.end( ) ) - throw( "GlushkovTraversal - Can not find GlushkovSymbol for regexp node." ); + if ( it == symbolSet.end ( ) ) + throw ( "GlushkovTraversal - Can not find GlushkovSymbol for regexp node." ); return * it; } -bool GlushkovTraversal::pos( GlushkovSymbol const& symbol, regexp::UnboundedRegExp const * const & node ) -{ - return pos( & node->getRegExp(), symbol.getSymbolPtr( ) ); +bool GlushkovTraversal::pos ( GlushkovSymbol const & symbol, regexp::UnboundedRegExp const * const & node ) { + return pos ( & node->getRegExp ( ), symbol.getSymbolPtr ( ) ); } -std::set<GlushkovSymbol> GlushkovTraversal::first( regexp::UnboundedRegExp const& re ) -{ - std::set<GlushkovSymbol> firstSet, symbolSet = getSymbols( re ); +std::set < GlushkovSymbol > GlushkovTraversal::first ( regexp::UnboundedRegExp const & re ) { + std::set < GlushkovSymbol > firstSet, symbolSet = getSymbols ( re ); - for( auto const& s : first( & re.getRegExp() ) ) - firstSet.insert( findSymbol( s, symbolSet ) ); + for ( auto const & s : first ( & re.getRegExp ( ) ) ) + firstSet.insert ( findSymbol ( s, symbolSet ) ); return firstSet; } -std::set<GlushkovSymbol> GlushkovTraversal::last( regexp::UnboundedRegExp const& re ) -{ - std::set<GlushkovSymbol> lastSet, symbolSet = getSymbols( re ); +std::set < GlushkovSymbol > GlushkovTraversal::last ( regexp::UnboundedRegExp const & re ) { + std::set < GlushkovSymbol > lastSet, symbolSet = getSymbols ( re ); - for( auto const& s : last( & re.getRegExp() ) ) - lastSet.insert( findSymbol( s, symbolSet ) ); + for ( auto const & s : last ( & re.getRegExp ( ) ) ) + lastSet.insert ( findSymbol ( s, symbolSet ) ); return lastSet; } -std::set<GlushkovSymbol> GlushkovTraversal::follow( regexp::UnboundedRegExp const& re, GlushkovSymbol const& symbol ) -{ - std::set<GlushkovSymbol> followSet, symbolSet = getSymbols( re ); +std::set < GlushkovSymbol > GlushkovTraversal::follow ( regexp::UnboundedRegExp const & re, GlushkovSymbol const & symbol ) { + std::set < GlushkovSymbol > followSet, symbolSet = getSymbols ( re ); - for( auto const& s: follow( & re.getRegExp(), symbol.getSymbolPtr( ) ) ) - followSet.insert( findSymbol( s, symbolSet ) ); + for ( auto const & s : follow ( & re.getRegExp ( ), symbol.getSymbolPtr ( ) ) ) + followSet.insert ( findSymbol ( s, symbolSet ) ); return followSet; } // ----------------------------------------------------------------------------- -std::set<regexp::UnboundedRegExpSymbol const *> GlushkovTraversal::first( regexp::UnboundedRegExpElement const * const & node ) -{ - regexp::UnboundedRegExpAlternation const * const alternation = dynamic_cast<regexp::UnboundedRegExpAlternation const * const>( node ); - regexp::UnboundedRegExpConcatenation const * const concatenation = dynamic_cast<regexp::UnboundedRegExpConcatenation const * const>( node ); - regexp::UnboundedRegExpIteration const * const iteration = dynamic_cast<regexp::UnboundedRegExpIteration const * const>( node ); - regexp::UnboundedRegExpSymbol const * const symbol = dynamic_cast<regexp::UnboundedRegExpSymbol const * const>( node ); - regexp::UnboundedRegExpEmpty const * const empty = dynamic_cast<regexp::UnboundedRegExpEmpty const * const>( node ); - regexp::UnboundedRegExpEpsilon const * const eps = dynamic_cast<regexp::UnboundedRegExpEpsilon const * const>( node ); - - if( alternation ) - return first( alternation ); - else if( concatenation ) - return first( concatenation ); - else if( iteration ) - return first( iteration ); - else if( eps ) - return first( eps ); - else if( empty ) - return first( empty ); - else if( symbol ) - return first( symbol ); - - throw exception::AlibException( "GlushkovTraversal::first - invalid RegExpElement node" ); -} - -std::set<regexp::UnboundedRegExpSymbol const *> GlushkovTraversal::first( regexp::UnboundedRegExpAlternation const * const & node ) -{ - std::set<regexp::UnboundedRegExpSymbol const *> ret, tmp; - - for( auto const& element : node->getElements( ) ) - { - tmp = first( element ); - ret.insert( tmp.begin( ), tmp.end( ) ); +std::set < regexp::UnboundedRegExpSymbol const * > GlushkovTraversal::first ( regexp::UnboundedRegExpElement const * const & node ) { + regexp::UnboundedRegExpAlternation const * const alternation = dynamic_cast < regexp::UnboundedRegExpAlternation const * const > ( node ); + regexp::UnboundedRegExpConcatenation const * const concatenation = dynamic_cast < regexp::UnboundedRegExpConcatenation const * const > ( node ); + regexp::UnboundedRegExpIteration const * const iteration = dynamic_cast < regexp::UnboundedRegExpIteration const * const > ( node ); + regexp::UnboundedRegExpSymbol const * const symbol = dynamic_cast < regexp::UnboundedRegExpSymbol const * const > ( node ); + regexp::UnboundedRegExpEmpty const * const empty = dynamic_cast < regexp::UnboundedRegExpEmpty const * const > ( node ); + regexp::UnboundedRegExpEpsilon const * const eps = dynamic_cast < regexp::UnboundedRegExpEpsilon const * const > ( node ); + + if ( alternation ) + return first ( alternation ); + else if ( concatenation ) + return first ( concatenation ); + else if ( iteration ) + return first ( iteration ); + else if ( eps ) + return first ( eps ); + else if ( empty ) + return first ( empty ); + else if ( symbol ) + return first ( symbol ); + + throw exception::AlibException ( "GlushkovTraversal::first - invalid RegExpElement node" ); +} + +std::set < regexp::UnboundedRegExpSymbol const * > GlushkovTraversal::first ( regexp::UnboundedRegExpAlternation const * const & node ) { + std::set < regexp::UnboundedRegExpSymbol const * > ret, tmp; + + for ( auto const & element : node->getElements ( ) ) { + tmp = first ( element ); + ret.insert ( tmp.begin ( ), tmp.end ( ) ); } return ret; } -std::set<regexp::UnboundedRegExpSymbol const *> GlushkovTraversal::first( regexp::UnboundedRegExpConcatenation const * const & node ) -{ - std::set<regexp::UnboundedRegExpSymbol const *> ret, tmp; +std::set < regexp::UnboundedRegExpSymbol const * > GlushkovTraversal::first ( regexp::UnboundedRegExpConcatenation const * const & node ) { + std::set < regexp::UnboundedRegExpSymbol const * > ret, tmp; - for( auto const& element : node->getElements( ) ) - { - tmp = first( element ); - ret.insert( tmp.begin( ), tmp.end( ) ); + for ( auto const & element : node->getElements ( ) ) { + tmp = first ( element ); + ret.insert ( tmp.begin ( ), tmp.end ( ) ); - if(! regexp::properties::RegExpEpsilon::languageContainsEpsilon(*element)) // If regexp of this subtree can match epsilon, then we need to add next subtree + if ( !regexp::properties::RegExpEpsilon::languageContainsEpsilon ( * element ) ) // If regexp of this subtree can match epsilon, then we need to add next subtree break; } return ret; } -std::set<regexp::UnboundedRegExpSymbol const *> GlushkovTraversal::first( regexp::UnboundedRegExpIteration const * const & node ) -{ - return first( & node->getElement( ) ); +std::set < regexp::UnboundedRegExpSymbol const * > GlushkovTraversal::first ( regexp::UnboundedRegExpIteration const * const & node ) { + return first ( & node->getElement ( ) ); } -std::set<regexp::UnboundedRegExpSymbol const *> GlushkovTraversal::first( regexp::UnboundedRegExpSymbol const * const & node ) -{ - return std::set<regexp::UnboundedRegExpSymbol const *> { node }; +std::set < regexp::UnboundedRegExpSymbol const * > GlushkovTraversal::first ( regexp::UnboundedRegExpSymbol const * const & node ) { + return std::set < regexp::UnboundedRegExpSymbol const * > { + node + } } -std::set<regexp::UnboundedRegExpSymbol const *> GlushkovTraversal::first( regexp::UnboundedRegExpEpsilon const * const & /* node */ ) -{ - return std::set<regexp::UnboundedRegExpSymbol const *>( ); +std::set < regexp::UnboundedRegExpSymbol const * > GlushkovTraversal::first ( regexp::UnboundedRegExpEpsilon const * const & /* node */ ) { + return std::set < regexp::UnboundedRegExpSymbol const * > ( ); } -std::set<regexp::UnboundedRegExpSymbol const *> GlushkovTraversal::first( regexp::UnboundedRegExpEmpty const * const & /* node */ ) -{ - return std::set<regexp::UnboundedRegExpSymbol const *>( ); +std::set < regexp::UnboundedRegExpSymbol const * > GlushkovTraversal::first ( regexp::UnboundedRegExpEmpty const * const & /* node */ ) { + return std::set < regexp::UnboundedRegExpSymbol const * > ( ); } // ---------------------------------------------------------------------------- -std::set<regexp::UnboundedRegExpSymbol const *> GlushkovTraversal::last( regexp::UnboundedRegExpElement const * const & node ) -{ - regexp::UnboundedRegExpAlternation const * const alternation = dynamic_cast<regexp::UnboundedRegExpAlternation const * const>( node ); - regexp::UnboundedRegExpConcatenation const * const concatenation = dynamic_cast<regexp::UnboundedRegExpConcatenation const * const>( node ); - regexp::UnboundedRegExpIteration const * const iteration = dynamic_cast<regexp::UnboundedRegExpIteration const * const>( node ); - regexp::UnboundedRegExpSymbol const * const symbol = dynamic_cast<regexp::UnboundedRegExpSymbol const * const>( node ); - regexp::UnboundedRegExpEmpty const * const empty = dynamic_cast<regexp::UnboundedRegExpEmpty const * const>( node ); - regexp::UnboundedRegExpEpsilon const * const eps = dynamic_cast<regexp::UnboundedRegExpEpsilon const * const>( node ); - - if( symbol ) - return last( symbol ); - else if( alternation ) - return last( alternation ); - else if( concatenation ) - return last( concatenation ); - else if( iteration ) - return last( iteration ); - else if( eps ) - return last( eps ); - else if( empty ) - return last( empty ); - - throw exception::AlibException( "GlushkovTraversal::last - invalid RegExpElement node" ); -} - -std::set<regexp::UnboundedRegExpSymbol const *> GlushkovTraversal::last( regexp::UnboundedRegExpAlternation const * const & node ) -{ - std::set<regexp::UnboundedRegExpSymbol const *> ret; - - for( auto const& element : node->getElements( ) ) - { - std::set<regexp::UnboundedRegExpSymbol const *> tmp = last( element ); - ret.insert( tmp.begin( ), tmp.end( ) ); +std::set < regexp::UnboundedRegExpSymbol const * > GlushkovTraversal::last ( regexp::UnboundedRegExpElement const * const & node ) { + regexp::UnboundedRegExpAlternation const * const alternation = dynamic_cast < regexp::UnboundedRegExpAlternation const * const > ( node ); + regexp::UnboundedRegExpConcatenation const * const concatenation = dynamic_cast < regexp::UnboundedRegExpConcatenation const * const > ( node ); + regexp::UnboundedRegExpIteration const * const iteration = dynamic_cast < regexp::UnboundedRegExpIteration const * const > ( node ); + regexp::UnboundedRegExpSymbol const * const symbol = dynamic_cast < regexp::UnboundedRegExpSymbol const * const > ( node ); + regexp::UnboundedRegExpEmpty const * const empty = dynamic_cast < regexp::UnboundedRegExpEmpty const * const > ( node ); + regexp::UnboundedRegExpEpsilon const * const eps = dynamic_cast < regexp::UnboundedRegExpEpsilon const * const > ( node ); + + if ( symbol ) + return last ( symbol ); + else if ( alternation ) + return last ( alternation ); + else if ( concatenation ) + return last ( concatenation ); + else if ( iteration ) + return last ( iteration ); + else if ( eps ) + return last ( eps ); + else if ( empty ) + return last ( empty ); + + throw exception::AlibException ( "GlushkovTraversal::last - invalid RegExpElement node" ); +} + +std::set < regexp::UnboundedRegExpSymbol const * > GlushkovTraversal::last ( regexp::UnboundedRegExpAlternation const * const & node ) { + std::set < regexp::UnboundedRegExpSymbol const * > ret; + + for ( auto const & element : node->getElements ( ) ) { + std::set < regexp::UnboundedRegExpSymbol const * > tmp = last ( element ); + ret.insert ( tmp.begin ( ), tmp.end ( ) ); } return ret; } -std::set<regexp::UnboundedRegExpSymbol const *> GlushkovTraversal::last( regexp::UnboundedRegExpConcatenation const * const & node ) -{ - std::set<regexp::UnboundedRegExpSymbol const *> ret, tmp; +std::set < regexp::UnboundedRegExpSymbol const * > GlushkovTraversal::last ( regexp::UnboundedRegExpConcatenation const * const & node ) { + std::set < regexp::UnboundedRegExpSymbol const * > ret, tmp; - for( auto it = node->getElements( ).rbegin( ); it != node->getElements( ).rend( ) ; it ++ ) - { - tmp = last( *it ); - ret.insert( tmp.begin( ), tmp.end( ) ); + for ( auto it = node->getElements ( ).rbegin ( ); it != node->getElements ( ).rend ( ); it++ ) { + tmp = last ( * it ); + ret.insert ( tmp.begin ( ), tmp.end ( ) ); - if( ! regexp::properties::RegExpEpsilon::languageContainsEpsilon(**it) ) + if ( !regexp::properties::RegExpEpsilon::languageContainsEpsilon ( * * it ) ) break; } return ret; } -std::set<regexp::UnboundedRegExpSymbol const *> GlushkovTraversal::last( regexp::UnboundedRegExpIteration const * const & node ) -{ - return last( & node->getElement( ) ); +std::set < regexp::UnboundedRegExpSymbol const * > GlushkovTraversal::last ( regexp::UnboundedRegExpIteration const * const & node ) { + return last ( & node->getElement ( ) ); } -std::set<regexp::UnboundedRegExpSymbol const *> GlushkovTraversal::last( regexp::UnboundedRegExpSymbol const * const & node ) -{ - return std::set<regexp::UnboundedRegExpSymbol const *> { node }; +std::set < regexp::UnboundedRegExpSymbol const * > GlushkovTraversal::last ( regexp::UnboundedRegExpSymbol const * const & node ) { + return std::set < regexp::UnboundedRegExpSymbol const * > { + node + } } -std::set<regexp::UnboundedRegExpSymbol const *> GlushkovTraversal::last( regexp::UnboundedRegExpEpsilon const * const & /* node */ ) -{ - return std::set<regexp::UnboundedRegExpSymbol const *>( ); +std::set < regexp::UnboundedRegExpSymbol const * > GlushkovTraversal::last ( regexp::UnboundedRegExpEpsilon const * const & /* node */ ) { + return std::set < regexp::UnboundedRegExpSymbol const * > ( ); } -std::set<regexp::UnboundedRegExpSymbol const *> GlushkovTraversal::last( regexp::UnboundedRegExpEmpty const * const & /* node */ ) -{ - return std::set<regexp::UnboundedRegExpSymbol const *>( ); +std::set < regexp::UnboundedRegExpSymbol const * > GlushkovTraversal::last ( regexp::UnboundedRegExpEmpty const * const & /* node */ ) { + return std::set < regexp::UnboundedRegExpSymbol const * > ( ); } // ---------------------------------------------------------------------------- -std::set<regexp::UnboundedRegExpSymbol const *> GlushkovTraversal::follow( regexp::UnboundedRegExpElement const * const & node, regexp::UnboundedRegExpSymbol const * const & symbolptr ) -{ - regexp::UnboundedRegExpAlternation const * const alternation = dynamic_cast<regexp::UnboundedRegExpAlternation const * const>( node ); - regexp::UnboundedRegExpConcatenation const * const concatenation = dynamic_cast<regexp::UnboundedRegExpConcatenation const * const>( node ); - regexp::UnboundedRegExpIteration const * const iteration = dynamic_cast<regexp::UnboundedRegExpIteration const * const>( node ); - regexp::UnboundedRegExpSymbol const * const symbol = dynamic_cast<regexp::UnboundedRegExpSymbol const * const>( node ); - regexp::UnboundedRegExpEmpty const * const empty = dynamic_cast<regexp::UnboundedRegExpEmpty const * const>( node ); - regexp::UnboundedRegExpEpsilon const * const eps = dynamic_cast<regexp::UnboundedRegExpEpsilon const * const>( node ); +std::set < regexp::UnboundedRegExpSymbol const * > GlushkovTraversal::follow ( regexp::UnboundedRegExpElement const * const & node, regexp::UnboundedRegExpSymbol const * const & symbolptr ) { + regexp::UnboundedRegExpAlternation const * const alternation = dynamic_cast < regexp::UnboundedRegExpAlternation const * const > ( node ); + regexp::UnboundedRegExpConcatenation const * const concatenation = dynamic_cast < regexp::UnboundedRegExpConcatenation const * const > ( node ); + regexp::UnboundedRegExpIteration const * const iteration = dynamic_cast < regexp::UnboundedRegExpIteration const * const > ( node ); + regexp::UnboundedRegExpSymbol const * const symbol = dynamic_cast < regexp::UnboundedRegExpSymbol const * const > ( node ); + regexp::UnboundedRegExpEmpty const * const empty = dynamic_cast < regexp::UnboundedRegExpEmpty const * const > ( node ); + regexp::UnboundedRegExpEpsilon const * const eps = dynamic_cast < regexp::UnboundedRegExpEpsilon const * const > ( node ); - if( alternation ) - return follow( alternation, symbolptr ); + if ( alternation ) + return follow ( alternation, symbolptr ); - else if( concatenation ) - return follow( concatenation, symbolptr ); + else if ( concatenation ) + return follow ( concatenation, symbolptr ); - else if( iteration ) - return follow( iteration, symbolptr ); + else if ( iteration ) + return follow ( iteration, symbolptr ); - else if( symbol ) - return follow( symbol, symbolptr ); + else if ( symbol ) + return follow ( symbol, symbolptr ); - else if( empty ) - return follow( empty, symbolptr ); + else if ( empty ) + return follow ( empty, symbolptr ); - else if( eps ) - return follow( eps, symbolptr ); + else if ( eps ) + return follow ( eps, symbolptr ); - throw exception::AlibException( "GlushkovTraversal::follow() - unknown RegExpElement node" ); + throw exception::AlibException ( "GlushkovTraversal::follow() - unknown RegExpElement node" ); } -std::set<regexp::UnboundedRegExpSymbol const *> GlushkovTraversal::follow( regexp::UnboundedRegExpAlternation const * const & node, regexp::UnboundedRegExpSymbol const * const & symbolptr ) -{ - for( auto const& element : node->getElements( ) ) - if( pos( element, symbolptr ) ) - return follow( element, symbolptr ); +std::set < regexp::UnboundedRegExpSymbol const * > GlushkovTraversal::follow ( regexp::UnboundedRegExpAlternation const * const & node, regexp::UnboundedRegExpSymbol const * const & symbolptr ) { + for ( auto const & element : node->getElements ( ) ) + if ( pos ( element, symbolptr ) ) + return follow ( element, symbolptr ); - throw exception::AlibException( "GlushkovTraversal::follow(Alt)" ); + throw exception::AlibException ( "GlushkovTraversal::follow(Alt)" ); } -std::set<regexp::UnboundedRegExpSymbol const *> GlushkovTraversal::follow( regexp::UnboundedRegExpConcatenation const * const & node, regexp::UnboundedRegExpSymbol const * const & symbolptr ) -{ - std::set<regexp::UnboundedRegExpSymbol const *> ret, tmp, lastSet; +std::set < regexp::UnboundedRegExpSymbol const * > GlushkovTraversal::follow ( regexp::UnboundedRegExpConcatenation const * const & node, regexp::UnboundedRegExpSymbol const * const & symbolptr ) { + std::set < regexp::UnboundedRegExpSymbol const * > ret, tmp, lastSet; - for( auto e = node->getElements( ).begin( ); e != node->getElements( ).end( ); e ++ ) - { - if( ! pos( *e, symbolptr ) ) + for ( auto e = node->getElements ( ).begin ( ); e != node->getElements ( ).end ( ); e++ ) { + if ( !pos ( * e, symbolptr ) ) continue; - tmp = follow( *e, symbolptr ); - ret.insert( tmp.begin( ), tmp.end( ) ); + tmp = follow ( * e, symbolptr ); + ret.insert ( tmp.begin ( ), tmp.end ( ) ); - lastSet = last( *e ); - if( lastSet.find(symbolptr) != lastSet.end() ) - { - for( auto f = next( e ); f != node->getElements( ).end( ); f ++ ) - { - tmp = first( *f ); - ret.insert( tmp.begin( ), tmp.end( ) ); + lastSet = last ( * e ); - if( ! regexp::properties::RegExpEpsilon::languageContainsEpsilon( **f ) ) + if ( lastSet.find ( symbolptr ) != lastSet.end ( ) ) + for ( auto f = next ( e ); f != node->getElements ( ).end ( ); f++ ) { + tmp = first ( * f ); + ret.insert ( tmp.begin ( ), tmp.end ( ) ); + + if ( !regexp::properties::RegExpEpsilon::languageContainsEpsilon ( * * f ) ) break; } - } + } + return ret; } -std::set<regexp::UnboundedRegExpSymbol const *> GlushkovTraversal::follow( regexp::UnboundedRegExpIteration const * const & node, regexp::UnboundedRegExpSymbol const * const & symbolptr ) -{ - std::set<regexp::UnboundedRegExpSymbol const *> ret = follow( & node->getElement( ), symbolptr ); - std::set<regexp::UnboundedRegExpSymbol const *> lastSet = last( & node->getElement( ) ); +std::set < regexp::UnboundedRegExpSymbol const * > GlushkovTraversal::follow ( regexp::UnboundedRegExpIteration const * const & node, regexp::UnboundedRegExpSymbol const * const & symbolptr ) { + std::set < regexp::UnboundedRegExpSymbol const * > ret = follow ( & node->getElement ( ), symbolptr ); + std::set < regexp::UnboundedRegExpSymbol const * > lastSet = last ( & node->getElement ( ) ); - if( lastSet.find(symbolptr) != lastSet.end() ) - { - std::set<regexp::UnboundedRegExpSymbol const *> firstSet = first( & node->getElement( ) ); - ret.insert( firstSet.begin( ), firstSet.end( ) ); + if ( lastSet.find ( symbolptr ) != lastSet.end ( ) ) { + std::set < regexp::UnboundedRegExpSymbol const * > firstSet = first ( & node->getElement ( ) ); + ret.insert ( firstSet.begin ( ), firstSet.end ( ) ); } return ret; } -std::set<regexp::UnboundedRegExpSymbol const *> GlushkovTraversal::follow( regexp::UnboundedRegExpSymbol const * const & /* node */, regexp::UnboundedRegExpSymbol const * const & /* symbolptr */ ) -{ - return std::set<regexp::UnboundedRegExpSymbol const *>( ); +std::set < regexp::UnboundedRegExpSymbol const * > GlushkovTraversal::follow ( regexp::UnboundedRegExpSymbol const * const & /* node */, regexp::UnboundedRegExpSymbol const * const & /* symbolptr */ ) { + return std::set < regexp::UnboundedRegExpSymbol const * > ( ); } -std::set<regexp::UnboundedRegExpSymbol const *> GlushkovTraversal::follow( regexp::UnboundedRegExpEmpty const * const & /* node */, regexp::UnboundedRegExpSymbol const * const & /* symbolptr */ ) -{ - return std::set<regexp::UnboundedRegExpSymbol const *>( ); +std::set < regexp::UnboundedRegExpSymbol const * > GlushkovTraversal::follow ( regexp::UnboundedRegExpEmpty const * const & /* node */, regexp::UnboundedRegExpSymbol const * const & /* symbolptr */ ) { + return std::set < regexp::UnboundedRegExpSymbol const * > ( ); } -std::set<regexp::UnboundedRegExpSymbol const *> GlushkovTraversal::follow( regexp::UnboundedRegExpEpsilon const * const & /* node */, regexp::UnboundedRegExpSymbol const * const & /* symbolptr */ ) -{ - return std::set<regexp::UnboundedRegExpSymbol const *>( ); +std::set < regexp::UnboundedRegExpSymbol const * > GlushkovTraversal::follow ( regexp::UnboundedRegExpEpsilon const * const & /* node */, regexp::UnboundedRegExpSymbol const * const & /* symbolptr */ ) { + return std::set < regexp::UnboundedRegExpSymbol const * > ( ); } // ---------------------------------------------------------------------------- -bool GlushkovTraversal::pos( regexp::UnboundedRegExpElement const * const & node, regexp::UnboundedRegExpSymbol const * const & symbolptr ) -{ - regexp::UnboundedRegExpAlternation const * const alternation = dynamic_cast<regexp::UnboundedRegExpAlternation const * const>( node ); - regexp::UnboundedRegExpConcatenation const * const concatenation = dynamic_cast<regexp::UnboundedRegExpConcatenation const * const>( node ); - regexp::UnboundedRegExpIteration const * const iteration = dynamic_cast<regexp::UnboundedRegExpIteration const * const>( node ); - regexp::UnboundedRegExpSymbol const * const symbol = dynamic_cast<regexp::UnboundedRegExpSymbol const * const>( node ); - regexp::UnboundedRegExpEmpty const * const empty = dynamic_cast<regexp::UnboundedRegExpEmpty const * const>( node ); - regexp::UnboundedRegExpEpsilon const * const eps = dynamic_cast<regexp::UnboundedRegExpEpsilon const * const>( node ); +bool GlushkovTraversal::pos ( regexp::UnboundedRegExpElement const * const & node, regexp::UnboundedRegExpSymbol const * const & symbolptr ) { + regexp::UnboundedRegExpAlternation const * const alternation = dynamic_cast < regexp::UnboundedRegExpAlternation const * const > ( node ); + regexp::UnboundedRegExpConcatenation const * const concatenation = dynamic_cast < regexp::UnboundedRegExpConcatenation const * const > ( node ); + regexp::UnboundedRegExpIteration const * const iteration = dynamic_cast < regexp::UnboundedRegExpIteration const * const > ( node ); + regexp::UnboundedRegExpSymbol const * const symbol = dynamic_cast < regexp::UnboundedRegExpSymbol const * const > ( node ); + regexp::UnboundedRegExpEmpty const * const empty = dynamic_cast < regexp::UnboundedRegExpEmpty const * const > ( node ); + regexp::UnboundedRegExpEpsilon const * const eps = dynamic_cast < regexp::UnboundedRegExpEpsilon const * const > ( node ); - if( alternation ) - return pos( alternation, symbolptr ); + if ( alternation ) + return pos ( alternation, symbolptr ); - else if( concatenation ) - return pos( concatenation, symbolptr ); + else if ( concatenation ) + return pos ( concatenation, symbolptr ); - else if( iteration ) - return pos( iteration, symbolptr ); + else if ( iteration ) + return pos ( iteration, symbolptr ); - else if( symbol ) - return pos( symbol, symbolptr ); + else if ( symbol ) + return pos ( symbol, symbolptr ); - else if( empty ) - return pos( empty, symbolptr ); + else if ( empty ) + return pos ( empty, symbolptr ); - else if( eps ) - return pos( eps, symbolptr ); + else if ( eps ) + return pos ( eps, symbolptr ); - throw exception::AlibException( "GlushkovTraversal::pos() - unknown RegExpElement node" ); + throw exception::AlibException ( "GlushkovTraversal::pos() - unknown RegExpElement node" ); } -bool GlushkovTraversal::pos( regexp::UnboundedRegExpAlternation const * const & node, regexp::UnboundedRegExpSymbol const * const & symbolptr ) -{ - for( auto const& element : node->getElements( ) ) - if( pos( element, symbolptr ) ) +bool GlushkovTraversal::pos ( regexp::UnboundedRegExpAlternation const * const & node, regexp::UnboundedRegExpSymbol const * const & symbolptr ) { + for ( auto const & element : node->getElements ( ) ) + if ( pos ( element, symbolptr ) ) return true; + return false; } -bool GlushkovTraversal::pos( regexp::UnboundedRegExpConcatenation const * const & node, regexp::UnboundedRegExpSymbol const * const & symbolptr ) -{ - for( auto const& element : node->getElements( ) ) - if( pos( element, symbolptr ) ) +bool GlushkovTraversal::pos ( regexp::UnboundedRegExpConcatenation const * const & node, regexp::UnboundedRegExpSymbol const * const & symbolptr ) { + for ( auto const & element : node->getElements ( ) ) + if ( pos ( element, symbolptr ) ) return true; + return false; } -bool GlushkovTraversal::pos( regexp::UnboundedRegExpIteration const * const & node, regexp::UnboundedRegExpSymbol const * const & symbolptr ) -{ - return pos( & node->getElement( ), symbolptr ); +bool GlushkovTraversal::pos ( regexp::UnboundedRegExpIteration const * const & node, regexp::UnboundedRegExpSymbol const * const & symbolptr ) { + return pos ( & node->getElement ( ), symbolptr ); } -bool GlushkovTraversal::pos( regexp::UnboundedRegExpSymbol const * const & node, regexp::UnboundedRegExpSymbol const * const & symbolptr ) -{ +bool GlushkovTraversal::pos ( regexp::UnboundedRegExpSymbol const * const & node, regexp::UnboundedRegExpSymbol const * const & symbolptr ) { return symbolptr == node; } -bool GlushkovTraversal::pos( regexp::UnboundedRegExpEmpty const * const & /* node */, regexp::UnboundedRegExpSymbol const * const & /* symbolptr */ ) -{ +bool GlushkovTraversal::pos ( regexp::UnboundedRegExpEmpty const * const & /* node */, regexp::UnboundedRegExpSymbol const * const & /* symbolptr */ ) { return false; } -bool GlushkovTraversal::pos( regexp::UnboundedRegExpEpsilon const * const & /* node */, regexp::UnboundedRegExpSymbol const * const & /* symbolptr */ ) -{ +bool GlushkovTraversal::pos ( regexp::UnboundedRegExpEpsilon const * const & /* node */, regexp::UnboundedRegExpSymbol const * const & /* symbolptr */ ) { return false; } // ---------------------------------------------------------------------------- -std::set<GlushkovSymbol> GlushkovTraversal::getSymbols( regexp::UnboundedRegExp const& re ) -{ - std::set<GlushkovSymbol> alphabet; +std::set < GlushkovSymbol > GlushkovTraversal::getSymbols ( regexp::UnboundedRegExp const & re ) { + std::set < GlushkovSymbol > alphabet; int i = 1; - getSymbols( & re.getRegExp( ), alphabet, i ); + getSymbols ( & re.getRegExp ( ), alphabet, i ); return alphabet; } -void GlushkovTraversal::getSymbols( regexp::UnboundedRegExpElement const * const & node, std::set<GlushkovSymbol> & alphabet, int & i ) -{ - regexp::UnboundedRegExpAlternation const * const alternation = dynamic_cast<regexp::UnboundedRegExpAlternation const * const>( node ); - regexp::UnboundedRegExpConcatenation const * const concatenation = dynamic_cast<regexp::UnboundedRegExpConcatenation const * const>( node ); - regexp::UnboundedRegExpIteration const * const iteration = dynamic_cast<regexp::UnboundedRegExpIteration const * const>( node ); - regexp::UnboundedRegExpSymbol const * const symbol = dynamic_cast<regexp::UnboundedRegExpSymbol const * const>( node ); - regexp::UnboundedRegExpEmpty const * const empty = dynamic_cast<regexp::UnboundedRegExpEmpty const * const>( node ); - regexp::UnboundedRegExpEpsilon const * const eps = dynamic_cast<regexp::UnboundedRegExpEpsilon const * const>( node ); +void GlushkovTraversal::getSymbols ( regexp::UnboundedRegExpElement const * const & node, std::set < GlushkovSymbol > & alphabet, int & i ) { + regexp::UnboundedRegExpAlternation const * const alternation = dynamic_cast < regexp::UnboundedRegExpAlternation const * const > ( node ); + regexp::UnboundedRegExpConcatenation const * const concatenation = dynamic_cast < regexp::UnboundedRegExpConcatenation const * const > ( node ); + regexp::UnboundedRegExpIteration const * const iteration = dynamic_cast < regexp::UnboundedRegExpIteration const * const > ( node ); + regexp::UnboundedRegExpSymbol const * const symbol = dynamic_cast < regexp::UnboundedRegExpSymbol const * const > ( node ); + regexp::UnboundedRegExpEmpty const * const empty = dynamic_cast < regexp::UnboundedRegExpEmpty const * const > ( node ); + regexp::UnboundedRegExpEpsilon const * const eps = dynamic_cast < regexp::UnboundedRegExpEpsilon const * const > ( node ); - if( symbol ) - { - alphabet.insert( GlushkovSymbol( symbol, i ++ ) ); + if ( symbol ) { + alphabet.insert ( GlushkovSymbol ( symbol, i++ ) ); return; - } + } else if ( alternation ) { + for ( const auto & element : alternation->getElements ( ) ) + getSymbols ( element, alphabet, i ); - else if( alternation ) - { - for( const auto & element : alternation->getElements( ) ) - getSymbols( element, alphabet, i ); return; - } + } else if ( concatenation ) { + for ( const auto & element : concatenation->getElements ( ) ) + getSymbols ( element, alphabet, i ); - else if( concatenation ) - { - for( const auto & element : concatenation->getElements( ) ) - getSymbols( element, alphabet, i ); return; - } - - else if( iteration ) - { - getSymbols( & iteration->getElement( ), alphabet, i ); + } else if ( iteration ) { + getSymbols ( & iteration->getElement ( ), alphabet, i ); return; - } - - else if( empty ) + } else if ( empty ) { return; - - else if( eps ) + } else if ( eps ) { return; + } - throw exception::AlibException( "GlushkovTraversal::getSymbols() - unknown RegExpElement node" ); + throw exception::AlibException ( "GlushkovTraversal::getSymbols() - unknown RegExpElement node" ); } } /* namespace conversions */ diff --git a/alib2algo/src/regexp/GlushkovTraversal.h b/alib2algo/src/regexp/GlushkovTraversal.h index 1e1def46e6..4ddc81df9f 100644 --- a/alib2algo/src/regexp/GlushkovTraversal.h +++ b/alib2algo/src/regexp/GlushkovTraversal.h @@ -26,41 +26,39 @@ #include "GlushkovSymbol.h" #include "GlushkovPair.h" -namespace regexp -{ +namespace regexp { /** * RegExp tree traversal utils for Glushkov algorithm. * * Thanks to http://www.sciencedirect.com/science/article/pii/S030439759700296X for better follow() solution. */ -class GlushkovTraversal -{ +class GlushkovTraversal { public: /** * @param re RegExp to probe * @return all RegExpSymbols whichcan start the word. */ - static std::set<GlushkovSymbol> first( regexp::UnboundedRegExp const& re ); + static std::set < GlushkovSymbol > first ( regexp::UnboundedRegExp const & re ); /** * @param re RegExp to probe * @return all RegExpSymbols that can terminate the word. */ - static std::set<GlushkovSymbol> last( regexp::UnboundedRegExp const& re ); + static std::set < GlushkovSymbol > last ( regexp::UnboundedRegExp const & re ); /** * @param re RegExp to probe * @param symbol GlushkovSymbol for which we need the follow() * @return all symbols that can follow specific symbol in word */ - static std::set<GlushkovSymbol> follow( regexp::UnboundedRegExp const& re, GlushkovSymbol const& symbol ); + static std::set < GlushkovSymbol > follow ( regexp::UnboundedRegExp const & re, GlushkovSymbol const & symbol ); /** * @param re RegExp to probe * @return symbols of regexp tree in order of they occurence in regexp. */ - static std::set<GlushkovSymbol> getSymbols( regexp::UnboundedRegExp const& re ); + static std::set < GlushkovSymbol > getSymbols ( regexp::UnboundedRegExp const & re ); private: /** @@ -68,46 +66,46 @@ private: * @param symbolSet set of gl.symbols * @return GlushkovSymbol equivalent for RegExpSymbol pointer */ - static GlushkovSymbol const& findSymbol( regexp::UnboundedRegExpSymbol const * const symbol, const std::set<GlushkovSymbol> & symbolSet ); + static GlushkovSymbol const & findSymbol ( regexp::UnboundedRegExpSymbol const * const symbol, const std::set < GlushkovSymbol > & symbolSet ); /** * @return bool true if symbol pointer is in this subtree */ - static bool pos( GlushkovSymbol const & symbol, regexp::UnboundedRegExp const * const & node ); - - static void getSymbols( regexp::UnboundedRegExpElement const * const & node, std::set<GlushkovSymbol> & alphabet, int & i ); - - static std::set<regexp::UnboundedRegExpSymbol const *> first( regexp::UnboundedRegExpElement const * const & node ); - static std::set<regexp::UnboundedRegExpSymbol const *> first( regexp::UnboundedRegExpAlternation const * const & node ); - static std::set<regexp::UnboundedRegExpSymbol const *> first( regexp::UnboundedRegExpConcatenation const * const & node ); - static std::set<regexp::UnboundedRegExpSymbol const *> first( regexp::UnboundedRegExpIteration const * const & node ); - static std::set<regexp::UnboundedRegExpSymbol const *> first( regexp::UnboundedRegExpSymbol const * const & node ); - static std::set<regexp::UnboundedRegExpSymbol const *> first( regexp::UnboundedRegExpEmpty const * const & node ); - static std::set<regexp::UnboundedRegExpSymbol const *> first( regexp::UnboundedRegExpEpsilon const * const & node ); - - static std::set<regexp::UnboundedRegExpSymbol const *> last( regexp::UnboundedRegExpElement const * const & node ); - static std::set<regexp::UnboundedRegExpSymbol const *> last( regexp::UnboundedRegExpAlternation const * const & node ); - static std::set<regexp::UnboundedRegExpSymbol const *> last( regexp::UnboundedRegExpConcatenation const * const & node ); - static std::set<regexp::UnboundedRegExpSymbol const *> last( regexp::UnboundedRegExpIteration const * const & node ); - static std::set<regexp::UnboundedRegExpSymbol const *> last( regexp::UnboundedRegExpSymbol const * const & node ); - static std::set<regexp::UnboundedRegExpSymbol const *> last( regexp::UnboundedRegExpEmpty const * const & node ); - static std::set<regexp::UnboundedRegExpSymbol const *> last( regexp::UnboundedRegExpEpsilon const * const & node ); - - static bool pos( regexp::UnboundedRegExpElement const * const & node, regexp::UnboundedRegExpSymbol const * const & symbSearch ); - static bool pos( regexp::UnboundedRegExpAlternation const * const & node, regexp::UnboundedRegExpSymbol const * const & symbSearch ); - static bool pos( regexp::UnboundedRegExpConcatenation const * const & node, regexp::UnboundedRegExpSymbol const * const & symbSearch ); - static bool pos( regexp::UnboundedRegExpIteration const * const & node, regexp::UnboundedRegExpSymbol const * const & symbSearch ); - static bool pos( regexp::UnboundedRegExpSymbol const * const & node, regexp::UnboundedRegExpSymbol const * const & symbSearch ); - static bool pos( regexp::UnboundedRegExpEmpty const * const & node, regexp::UnboundedRegExpSymbol const * const & symbSearch ); - static bool pos( regexp::UnboundedRegExpEpsilon const * const & node, regexp::UnboundedRegExpSymbol const * const & symbSearch ); - - static std::set<regexp::UnboundedRegExpSymbol const *> follow( regexp::UnboundedRegExpElement const * const & node, regexp::UnboundedRegExpSymbol const * const & symbFollow ); - static std::set<regexp::UnboundedRegExpSymbol const *> follow( regexp::UnboundedRegExpAlternation const * const & node, regexp::UnboundedRegExpSymbol const * const & symbFollow ); - static std::set<regexp::UnboundedRegExpSymbol const *> follow( regexp::UnboundedRegExpConcatenation const * const & node, regexp::UnboundedRegExpSymbol const * const & symbFollow ); - static std::set<regexp::UnboundedRegExpSymbol const *> follow( regexp::UnboundedRegExpIteration const * const & node, regexp::UnboundedRegExpSymbol const * const & symbFollow ); - static std::set<regexp::UnboundedRegExpSymbol const *> follow( regexp::UnboundedRegExpSymbol const * const & node, regexp::UnboundedRegExpSymbol const * const & symbFollow ); - static std::set<regexp::UnboundedRegExpSymbol const *> follow( regexp::UnboundedRegExpEmpty const * const & node, regexp::UnboundedRegExpSymbol const * const & symbFollow ); - static std::set<regexp::UnboundedRegExpSymbol const *> follow( regexp::UnboundedRegExpEpsilon const * const & node, regexp::UnboundedRegExpSymbol const * const & symbFollow ); + static bool pos ( GlushkovSymbol const & symbol, regexp::UnboundedRegExp const * const & node ); + + static void getSymbols ( regexp::UnboundedRegExpElement const * const & node, std::set < GlushkovSymbol > & alphabet, int & i ); + + static std::set < regexp::UnboundedRegExpSymbol const * > first ( regexp::UnboundedRegExpElement const * const & node ); + static std::set < regexp::UnboundedRegExpSymbol const * > first ( regexp::UnboundedRegExpAlternation const * const & node ); + static std::set < regexp::UnboundedRegExpSymbol const * > first ( regexp::UnboundedRegExpConcatenation const * const & node ); + static std::set < regexp::UnboundedRegExpSymbol const * > first ( regexp::UnboundedRegExpIteration const * const & node ); + static std::set < regexp::UnboundedRegExpSymbol const * > first ( regexp::UnboundedRegExpSymbol const * const & node ); + static std::set < regexp::UnboundedRegExpSymbol const * > first ( regexp::UnboundedRegExpEmpty const * const & node ); + static std::set < regexp::UnboundedRegExpSymbol const * > first ( regexp::UnboundedRegExpEpsilon const * const & node ); + + static std::set < regexp::UnboundedRegExpSymbol const * > last ( regexp::UnboundedRegExpElement const * const & node ); + static std::set < regexp::UnboundedRegExpSymbol const * > last ( regexp::UnboundedRegExpAlternation const * const & node ); + static std::set < regexp::UnboundedRegExpSymbol const * > last ( regexp::UnboundedRegExpConcatenation const * const & node ); + static std::set < regexp::UnboundedRegExpSymbol const * > last ( regexp::UnboundedRegExpIteration const * const & node ); + static std::set < regexp::UnboundedRegExpSymbol const * > last ( regexp::UnboundedRegExpSymbol const * const & node ); + static std::set < regexp::UnboundedRegExpSymbol const * > last ( regexp::UnboundedRegExpEmpty const * const & node ); + static std::set < regexp::UnboundedRegExpSymbol const * > last ( regexp::UnboundedRegExpEpsilon const * const & node ); + + static bool pos ( regexp::UnboundedRegExpElement const * const & node, regexp::UnboundedRegExpSymbol const * const & symbSearch ); + static bool pos ( regexp::UnboundedRegExpAlternation const * const & node, regexp::UnboundedRegExpSymbol const * const & symbSearch ); + static bool pos ( regexp::UnboundedRegExpConcatenation const * const & node, regexp::UnboundedRegExpSymbol const * const & symbSearch ); + static bool pos ( regexp::UnboundedRegExpIteration const * const & node, regexp::UnboundedRegExpSymbol const * const & symbSearch ); + static bool pos ( regexp::UnboundedRegExpSymbol const * const & node, regexp::UnboundedRegExpSymbol const * const & symbSearch ); + static bool pos ( regexp::UnboundedRegExpEmpty const * const & node, regexp::UnboundedRegExpSymbol const * const & symbSearch ); + static bool pos ( regexp::UnboundedRegExpEpsilon const * const & node, regexp::UnboundedRegExpSymbol const * const & symbSearch ); + + static std::set < regexp::UnboundedRegExpSymbol const * > follow ( regexp::UnboundedRegExpElement const * const & node, regexp::UnboundedRegExpSymbol const * const & symbFollow ); + static std::set < regexp::UnboundedRegExpSymbol const * > follow ( regexp::UnboundedRegExpAlternation const * const & node, regexp::UnboundedRegExpSymbol const * const & symbFollow ); + static std::set < regexp::UnboundedRegExpSymbol const * > follow ( regexp::UnboundedRegExpConcatenation const * const & node, regexp::UnboundedRegExpSymbol const * const & symbFollow ); + static std::set < regexp::UnboundedRegExpSymbol const * > follow ( regexp::UnboundedRegExpIteration const * const & node, regexp::UnboundedRegExpSymbol const * const & symbFollow ); + static std::set < regexp::UnboundedRegExpSymbol const * > follow ( regexp::UnboundedRegExpSymbol const * const & node, regexp::UnboundedRegExpSymbol const * const & symbFollow ); + static std::set < regexp::UnboundedRegExpSymbol const * > follow ( regexp::UnboundedRegExpEmpty const * const & node, regexp::UnboundedRegExpSymbol const * const & symbFollow ); + static std::set < regexp::UnboundedRegExpSymbol const * > follow ( regexp::UnboundedRegExpEpsilon const * const & node, regexp::UnboundedRegExpSymbol const * const & symbFollow ); }; } /* namespace conversions */ -- GitLab