diff --git a/alib2algo/src/stringology/exact/Dogaru.h b/alib2algo/src/stringology/exact/Dogaru.h index 5643c55b1f9dfd3fb4da8f9f21c9b7b7c553d229..b660f83a642a5915d06bfbf5a3169937e7d7bb6c 100644 --- a/alib2algo/src/stringology/exact/Dogaru.h +++ b/alib2algo/src/stringology/exact/Dogaru.h @@ -7,11 +7,7 @@ #include <string/LinearString.h> - -namespace stringology { - -namespace exact { - +namespace stringology::exact { /** * Implementation of the Dogaru algorithm from article "On the All Occurrences of a Word in a Text" by O.C.Dogaru @@ -24,30 +20,52 @@ public: */ template < class SymbolType > static ext::set < unsigned > match ( const string::LinearString < SymbolType > & subject, const string::LinearString < SymbolType > & pattern ); - }; template < class SymbolType > ext::set < unsigned > Dogaru::match ( const string::LinearString < SymbolType > & subject, const string::LinearString < SymbolType > & pattern ) { - ext::set<unsigned> occ; + ext::set < unsigned > occ; const auto & text = subject.getContent(); const auto & pat = pattern.getContent(); - long int n = text.size(), m = pat.size(); + long int n = text.size(); + long int m = pat.size(); - unsigned i = 0 , j = 0 , k = 0; + unsigned i = 0; + unsigned j = 0; + unsigned k = 0; measurements::start ( "Algorithm", measurements::Type::ALGORITHM ); do { j = 0; - while ( j < m && pat[j] == text[i] ) { ++ i; ++ j; } - if ( j == m ) { occ.insert( i - j ); continue; } - one: ++ i; - while ( i <= n - m + j && pat[j] != text[i] ) ++ i; - if ( i > n - m + j ) { measurements::end(); return occ; } + while ( j < m && pat[j] == text[i] ) { + ++ i; + ++ j; + } + + if ( j == m ) { + occ.insert( i - j ); + continue; + } +one: + ++ i; + while ( i <= n - m + j && pat[j] != text[i] ) + ++ i; + + if ( i > n - m + j ) { + measurements::end(); + return occ; + } + k = 0; - while ( k <= m - 1 && pat[k] == text[ i - j + k ] ) ++ k; - if ( k == m ) { occ.insert( i - j ); i = i - j + m; } - else { goto one;} + while ( k <= m - 1 && pat[k] == text[ i - j + k ] ) + ++ k; + + if ( k == m ) { + occ.insert( i - j ); + i = i - j + m; + } else { + goto one; + } } while ( i < n - m + j ); @@ -55,8 +73,4 @@ ext::set < unsigned > Dogaru::match ( const string::LinearString < SymbolType > return occ; } - -} /* namespace exact */ - -} /* namespace stringology */ - +} /* namespace stringology::exact */