From 2dda6f1e978f5834f69059358ad1eaa5b3f85092 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jan=20Tr=C3=A1vn=C3=AD=C4=8Dek?= <jan.travnicek@fit.cvut.cz>
Date: Sun, 30 Jan 2022 10:59:24 +0100
Subject: [PATCH] algo: factor out common function from two stringology algos

---
 .../src/stringology/common/CommonAlgorithm.h     |  9 +++++++++
 alib2algo/src/stringology/exact/GalilSeiferas.h  | 16 +++++++---------
 .../src/stringology/exact/SequentialSampling.h   | 14 ++++++--------
 3 files changed, 22 insertions(+), 17 deletions(-)
 create mode 100644 alib2algo/src/stringology/common/CommonAlgorithm.h

diff --git a/alib2algo/src/stringology/common/CommonAlgorithm.h b/alib2algo/src/stringology/common/CommonAlgorithm.h
new file mode 100644
index 0000000000..a1e2279b76
--- /dev/null
+++ b/alib2algo/src/stringology/common/CommonAlgorithm.h
@@ -0,0 +1,9 @@
+#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 */
diff --git a/alib2algo/src/stringology/exact/GalilSeiferas.h b/alib2algo/src/stringology/exact/GalilSeiferas.h
index cbf5b84710..76650d61e5 100644
--- a/alib2algo/src/stringology/exact/GalilSeiferas.h
+++ b/alib2algo/src/stringology/exact/GalilSeiferas.h
@@ -10,6 +10,8 @@
 #include <common/createUnique.hpp>
 #include <alphabet/EndSymbol.h>
 
+#include <stringology/common/CommonAlgorithm.h>
+
 namespace stringology::exact {
 
 /**
@@ -17,10 +19,6 @@ namespace stringology::exact {
  * Zvi Galil and Joel Seiferas
  */
 class GalilSeiferas{
-	static size_t div_up ( size_t x, size_t y ) {
-		return ( x % y == 1 ) ? x / y + 1 : x / y;
-	}
-
 public:
 	/**
 	 * Search for pattern in linear string.
@@ -35,7 +33,7 @@ ext::set < unsigned > GalilSeiferas::match ( const string::LinearString < Symbol
 	ext::set < unsigned > occ;
 
 	// 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 ( );
 	extendedSubject.push_back ( endSymbol );
 
@@ -67,7 +65,7 @@ newp1:
 
 	if ( s + p1 + q1 == m )
 		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;
 	goto newp1;
 
@@ -82,7 +80,7 @@ newp2:
 		p2 += p1;
 		q2 -= p1;
 	} 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;
 	}
 	goto newp2;
@@ -94,7 +92,7 @@ parse:
 		s += 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;
 	if ( p1 < p2 )
 		goto parse;
@@ -111,7 +109,7 @@ search:
 			p += p1;
 			q -= p1;
 		} 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;
 		}
 	}
diff --git a/alib2algo/src/stringology/exact/SequentialSampling.h b/alib2algo/src/stringology/exact/SequentialSampling.h
index cc9daa6b69..558e6b79db 100644
--- a/alib2algo/src/stringology/exact/SequentialSampling.h
+++ b/alib2algo/src/stringology/exact/SequentialSampling.h
@@ -5,6 +5,8 @@
 #include <alib/set>
 #include <alib/vector>
 
+#include <stringology/common/CommonAlgorithm.h>
+
 #include <string/properties/PeriodicPrefix.h>
 #include <string/LinearString.h>
 
@@ -12,10 +14,6 @@ namespace stringology::exact {
 
 namespace {
 
-inline size_t div_up ( size_t x, size_t y ) {
-	return ( x % y == 1 ) ? x / y + 1 : x / y;
-}
-
 template <class SymbolType>
 ext::set < unsigned > SimpleTextSearching ( const string::LinearString < SymbolType > & subject, const string::LinearString < SymbolType > & pattern ) {
 	ext::set<unsigned> occ;
@@ -33,7 +31,7 @@ ext::set < unsigned > SimpleTextSearching ( const string::LinearString < SymbolT
 		if ( j == m )
 			occ.insert(i);
 
-		i += div_up( j + 1, 2 );
+		i += common::div_up( j + 1, 2 );
 	}
 	return occ;
 }
@@ -53,7 +51,7 @@ size_t SimpleTextSearchingFirst ( const ext::vector< SymbolType > & text, const
 		if ( j == m )
 			return i;
 
-		i += div_up( j + 1, 2 );
+		i += common::div_up( j + 1, 2 );
 	}
 	return n;
 }
@@ -80,7 +78,7 @@ ext::set < unsigned > SeqSampling ( const string::LinearString < SymbolType > &
 			if ( j + 1 < q )
 				i += p;
 			else
-				i += div_up ( j + 1, 2 );
+				i += common::div_up ( j + 1, 2 );
 		}
 	}
 	return occ;
@@ -106,7 +104,7 @@ size_t SeqSamplingFirst ( const ext::vector< SymbolType > & text, const ext::vec
 			if ( j < q - 1 )
 				i += p;
 			else
-				i += div_up ( j + 1, 2 );
+				i += common::div_up ( j + 1, 2 );
 		}
 	}
 	return n;
-- 
GitLab