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

algo: factor out common function from two stringology algos

parent 43afb189
No related branches found
No related tags found
1 merge request!215Merge jt
Pipeline #174700 passed with warnings
#pragma once
namespace stringology::common {
inline size_t div_up ( size_t x, size_t y ) {
return x / y + ( x % y == 1 );
}
} /* namespace stringology::common */
...@@ -10,6 +10,8 @@ ...@@ -10,6 +10,8 @@
#include <common/createUnique.hpp> #include <common/createUnique.hpp>
#include <alphabet/EndSymbol.h> #include <alphabet/EndSymbol.h>
   
#include <stringology/common/CommonAlgorithm.h>
namespace stringology::exact { namespace stringology::exact {
   
/** /**
...@@ -17,10 +19,6 @@ namespace stringology::exact { ...@@ -17,10 +19,6 @@ namespace stringology::exact {
* Zvi Galil and Joel Seiferas * Zvi Galil and Joel Seiferas
*/ */
class GalilSeiferas{ class GalilSeiferas{
static size_t div_up ( size_t x, size_t y ) {
return ( x % y == 1 ) ? x / y + 1 : x / y;
}
public: public:
/** /**
* Search for pattern in linear string. * Search for pattern in linear string.
...@@ -35,7 +33,7 @@ ext::set < unsigned > GalilSeiferas::match ( const string::LinearString < Symbol ...@@ -35,7 +33,7 @@ ext::set < unsigned > GalilSeiferas::match ( const string::LinearString < Symbol
ext::set < unsigned > occ; ext::set < unsigned > occ;
   
// add terminating symbol to subject // add terminating symbol to subject
SymbolType endSymbol = common::createUnique ( alphabet::EndSymbol::instance < SymbolType > ( ), subject.getAlphabet ( ), pattern.getAlphabet ( ) ); SymbolType endSymbol = ::common::createUnique ( alphabet::EndSymbol::instance < SymbolType > ( ), subject.getAlphabet ( ), pattern.getAlphabet ( ) );
ext::vector < SymbolType > extendedSubject = subject.getContent ( ); ext::vector < SymbolType > extendedSubject = subject.getContent ( );
extendedSubject.push_back ( endSymbol ); extendedSubject.push_back ( endSymbol );
   
...@@ -67,7 +65,7 @@ newp1: ...@@ -67,7 +65,7 @@ newp1:
   
if ( s + p1 + q1 == m ) if ( s + p1 + q1 == m )
goto search; goto search;
p1 += std::max( static_cast < size_t > ( 1 ) , div_up ( q1 , k ) ); p1 += std::max( static_cast < size_t > ( 1 ) , common::div_up ( q1 , k ) );
q1 = 0; q1 = 0;
goto newp1; goto newp1;
   
...@@ -82,7 +80,7 @@ newp2: ...@@ -82,7 +80,7 @@ newp2:
p2 += p1; p2 += p1;
q2 -= p1; q2 -= p1;
} else { } else {
p2 += std::max ( static_cast < size_t > ( 1 ) , div_up(q2 , k)); p2 += std::max ( static_cast < size_t > ( 1 ) , common::div_up(q2 , k));
q2 = 0; q2 = 0;
} }
goto newp2; goto newp2;
...@@ -94,7 +92,7 @@ parse: ...@@ -94,7 +92,7 @@ parse:
s += p1; s += p1;
q1 -= p1; q1 -= p1;
} }
p1 += std::max ( static_cast < size_t > ( 1 ) , div_up(q1 , k )); p1 += std::max ( static_cast < size_t > ( 1 ) , common::div_up(q1 , k ));
q1 = 0; q1 = 0;
if ( p1 < p2 ) if ( p1 < p2 )
goto parse; goto parse;
...@@ -111,7 +109,7 @@ search: ...@@ -111,7 +109,7 @@ search:
p += p1; p += p1;
q -= p1; q -= p1;
} else { } else {
p += std::max ( static_cast < size_t > ( 1 ), div_up(q, k)); p += std::max ( static_cast < size_t > ( 1 ), common::div_up(q, k));
q = 0; q = 0;
} }
} }
......
...@@ -5,6 +5,8 @@ ...@@ -5,6 +5,8 @@
#include <alib/set> #include <alib/set>
#include <alib/vector> #include <alib/vector>
   
#include <stringology/common/CommonAlgorithm.h>
#include <string/properties/PeriodicPrefix.h> #include <string/properties/PeriodicPrefix.h>
#include <string/LinearString.h> #include <string/LinearString.h>
   
...@@ -12,10 +14,6 @@ namespace stringology::exact { ...@@ -12,10 +14,6 @@ namespace stringology::exact {
   
namespace { namespace {
   
inline size_t div_up ( size_t x, size_t y ) {
return ( x % y == 1 ) ? x / y + 1 : x / y;
}
template <class SymbolType> template <class SymbolType>
ext::set < unsigned > SimpleTextSearching ( const string::LinearString < SymbolType > & subject, const string::LinearString < SymbolType > & pattern ) { ext::set < unsigned > SimpleTextSearching ( const string::LinearString < SymbolType > & subject, const string::LinearString < SymbolType > & pattern ) {
ext::set<unsigned> occ; ext::set<unsigned> occ;
...@@ -33,7 +31,7 @@ ext::set < unsigned > SimpleTextSearching ( const string::LinearString < SymbolT ...@@ -33,7 +31,7 @@ ext::set < unsigned > SimpleTextSearching ( const string::LinearString < SymbolT
if ( j == m ) if ( j == m )
occ.insert(i); occ.insert(i);
   
i += div_up( j + 1, 2 ); i += common::div_up( j + 1, 2 );
} }
return occ; return occ;
} }
...@@ -53,7 +51,7 @@ size_t SimpleTextSearchingFirst ( const ext::vector< SymbolType > & text, const ...@@ -53,7 +51,7 @@ size_t SimpleTextSearchingFirst ( const ext::vector< SymbolType > & text, const
if ( j == m ) if ( j == m )
return i; return i;
   
i += div_up( j + 1, 2 ); i += common::div_up( j + 1, 2 );
} }
return n; return n;
} }
...@@ -80,7 +78,7 @@ ext::set < unsigned > SeqSampling ( const string::LinearString < SymbolType > & ...@@ -80,7 +78,7 @@ ext::set < unsigned > SeqSampling ( const string::LinearString < SymbolType > &
if ( j + 1 < q ) if ( j + 1 < q )
i += p; i += p;
else else
i += div_up ( j + 1, 2 ); i += common::div_up ( j + 1, 2 );
} }
} }
return occ; return occ;
...@@ -106,7 +104,7 @@ size_t SeqSamplingFirst ( const ext::vector< SymbolType > & text, const ext::vec ...@@ -106,7 +104,7 @@ size_t SeqSamplingFirst ( const ext::vector< SymbolType > & text, const ext::vec
if ( j < q - 1 ) if ( j < q - 1 )
i += p; i += p;
else else
i += div_up ( j + 1, 2 ); i += common::div_up ( j + 1, 2 );
} }
} }
return n; return n;
......
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