From 25db92b92cb5b7fefc0557d9013ed3489ad17b4e Mon Sep 17 00:00:00 2001 From: Jan Travnicek <Jan.Travnicek@fit.cvut.cz> Date: Tue, 25 Sep 2018 08:24:20 +0200 Subject: [PATCH] simplify ToCNF algo --- alib2algo/src/grammar/simplify/ToCNF.h | 41 +++++++------------------- 1 file changed, 11 insertions(+), 30 deletions(-) diff --git a/alib2algo/src/grammar/simplify/ToCNF.h b/alib2algo/src/grammar/simplify/ToCNF.h index 5e233c6a58..dd7d4d2839 100644 --- a/alib2algo/src/grammar/simplify/ToCNF.h +++ b/alib2algo/src/grammar/simplify/ToCNF.h @@ -130,41 +130,22 @@ grammar::CNF < TerminalSymbolType, ext::variant < TerminalSymbolType, Nontermin template < class TerminalSymbolType, class NonterminalSymbolType > void splitRule ( ext::vector < ext::variant < TerminalSymbolType, NonterminalSymbolType > > lhs, const ext::vector < ext::variant < TerminalSymbolType, NonterminalSymbolType > > & rhs, grammar::CNF < TerminalSymbolType, ext::vector < ext::variant < TerminalSymbolType, NonterminalSymbolType > > > & result ) { - switch ( rhs.size ( ) ) { - case 2: { - ext::vector < ext::variant < TerminalSymbolType, NonterminalSymbolType > > left { rhs [ 0 ] }; - ext::vector < ext::variant < TerminalSymbolType, NonterminalSymbolType > > right { rhs [ 1 ] }; - result.addRule ( std::move ( lhs ), ext::make_pair ( std::move ( left ), std::move ( right ) ) ); - break; - } - case 3: { - ext::vector < ext::variant < TerminalSymbolType, NonterminalSymbolType > > left { rhs [ 0 ] }; - - ext::vector < ext::variant < TerminalSymbolType, NonterminalSymbolType > > right ( std::next ( rhs.begin ( ) ), rhs.end ( ) ); - if ( result.addNonterminalSymbol ( right ) ) - splitRule ( right, right, result ); + ext::vector < ext::variant < TerminalSymbolType, NonterminalSymbolType > > left; + ext::vector < ext::variant < TerminalSymbolType, NonterminalSymbolType > > right; - result.addRule ( std::move ( lhs ), ext::make_pair ( std::move ( left ), std::move ( right ) ) ); - break; - } - default: { - ext::vector < ext::variant < TerminalSymbolType, NonterminalSymbolType > > left; - for ( unsigned i = 0; i < rhs.size ( ) / 2; ++ i ) - left.push_back ( rhs [ i ] ); + for ( unsigned i = 0; i < rhs.size ( ) / 2; ++ i ) + left.push_back ( rhs [ i ] ); - if ( result.addNonterminalSymbol ( left ) ) - splitRule ( left, left, result ); + if ( result.addNonterminalSymbol ( left ) && left.size ( ) > 1 ) + splitRule ( left, left, result ); - ext::vector < ext::variant < TerminalSymbolType, NonterminalSymbolType > > right; - for ( unsigned i = rhs.size ( ) / 2; i < rhs.size ( ); ++ i ) - right.push_back ( rhs [ i ] ); + for ( unsigned i = rhs.size ( ) / 2; i < rhs.size ( ); ++ i ) + right.push_back ( rhs [ i ] ); - if ( result.addNonterminalSymbol ( right ) ) - splitRule ( right, right, result ); + if ( result.addNonterminalSymbol ( right ), right.size ( ) > 1 ) + splitRule ( right, right, result ); - result.addRule ( std::move ( lhs ), ext::make_pair ( std::move ( left ), std::move ( right ) ) ); - } - } + result.addRule ( std::move ( lhs ), ext::make_pair ( std::move ( left ), std::move ( right ) ) ); } template < class T, class TerminalSymbolType = typename grammar::TerminalSymbolTypeOfGrammar < T >, class NonterminalSymbolType = typename grammar::NonterminalSymbolTypeOfGrammar < T > > -- GitLab