From 00129d6f0a988d946873b99b876365aec43c818a Mon Sep 17 00:00:00 2001
From: Jan Travnicek <Jan.Travnicek@fit.cvut.cz>
Date: Mon, 2 Apr 2018 22:09:57 +0200
Subject: [PATCH] take out searching sbar and variable in pattern

---
 .../tree/properties/BadCharacterShiftTable.h  |  8 ++-
 .../properties/FirstVariableOffsetFront.h     | 49 ++++++++++++++++++
 .../properties/FirstVariablesBarOffsetFront.h | 49 ++++++++++++++++++
 .../tree/properties/LastVariableOffsetBack.h  | 50 +++++++++++++++++++
 .../ReversedBadCharacterShiftTable.h          | 15 ++----
 5 files changed, 156 insertions(+), 15 deletions(-)
 create mode 100644 alib2algo/src/tree/properties/FirstVariableOffsetFront.h
 create mode 100644 alib2algo/src/tree/properties/FirstVariablesBarOffsetFront.h
 create mode 100644 alib2algo/src/tree/properties/LastVariableOffsetBack.h

diff --git a/alib2algo/src/tree/properties/BadCharacterShiftTable.h b/alib2algo/src/tree/properties/BadCharacterShiftTable.h
index d7b31da764..388f8b91e7 100644
--- a/alib2algo/src/tree/properties/BadCharacterShiftTable.h
+++ b/alib2algo/src/tree/properties/BadCharacterShiftTable.h
@@ -12,6 +12,8 @@
 #include <tree/ranked/PrefixRankedBarPattern.h>
 #include <tree/ranked/PrefixRankedBarNonlinearPattern.h>
 
+#include "LastVariableOffsetBack.h"
+
 #include <alib/set>
 #include <alib/map>
 
@@ -53,11 +55,7 @@ ext::map < common::ranked_symbol < SymbolType, RankType >, size_t > BadCharacter
 
 	 // find the distance between the end of the pattern and the index
 	 // of the last symbol representing the variable
-	unsigned lastSOffset = pattern.getContent ( ).size ( );
-
-	for ( unsigned i = 0; i < pattern.getContent ( ).size ( ); i++ )
-		if ( pattern.getContent ( )[i] == pattern.getSubtreeWildcard ( ) || pattern.getNonlinearVariables ( ).count ( pattern.getContent ( )[i] ) )
-			lastSOffset = pattern.getContent ( ).size ( ) - i - 1;
+	size_t lastSOffset = LastVariableOffsetBack::offset ( pattern );
 
 	// limit the shift by occurrence of the last variable
 
diff --git a/alib2algo/src/tree/properties/FirstVariableOffsetFront.h b/alib2algo/src/tree/properties/FirstVariableOffsetFront.h
new file mode 100644
index 0000000000..ccd90eacbd
--- /dev/null
+++ b/alib2algo/src/tree/properties/FirstVariableOffsetFront.h
@@ -0,0 +1,49 @@
+/*
+ * FirstVariableOffsetFront.h
+ *
+ *  Created on: 5. 11. 2014
+ *      Author: Jan Travnicek
+ */
+
+#ifndef _FIRST_VARIABLE_OFFSET_FRONT_H_
+#define _FIRST_VARIABLE_OFFSET_FRONT_H_
+
+#include <tree/ranked/PrefixRankedPattern.h>
+#include <tree/ranked/PrefixRankedNonlinearPattern.h>
+
+namespace tree {
+
+namespace properties {
+
+class FirstVariableOffsetFront {
+public:
+	template < class SymbolType, class RankType >
+	static size_t offset ( const tree::PrefixRankedPattern < SymbolType, RankType > & pattern );
+	template < class SymbolType, class RankType >
+	static size_t offset ( const tree::PrefixRankedNonlinearPattern < SymbolType, RankType > & pattern );
+
+};
+
+template < class SymbolType, class RankType >
+size_t FirstVariableOffsetFront::offset ( const tree::PrefixRankedPattern < SymbolType, RankType > & pattern ) {
+	return offset ( tree::PrefixRankedNonlinearPattern < SymbolType, RankType > ( pattern ) );
+}
+
+template < class SymbolType, class RankType >
+size_t FirstVariableOffsetFront::offset ( const tree::PrefixRankedNonlinearPattern < SymbolType, RankType > & pattern ) {
+	 // find the distance between the beginning of the pattern and the index
+	 // of the first symbol representing the variable's bar
+	size_t res = pattern.getContent ( ).size ( ) + 1;
+
+	for ( int i = ( int ) pattern.getContent ( ).size ( ) - 1; i >= 0; i-- )
+		if ( pattern.getContent ( )[i] == pattern.getSubtreeWildcard ( ) || pattern.getNonlinearVariables ( ).count ( pattern.getContent ( )[i] ) )
+			res = i;
+
+	return res;
+}
+
+} /* namespace properties */
+
+} /* namespace tree */
+
+#endif /* _FIRST_VARIABLES_OFFSET_FRONT_H_ */
diff --git a/alib2algo/src/tree/properties/FirstVariablesBarOffsetFront.h b/alib2algo/src/tree/properties/FirstVariablesBarOffsetFront.h
new file mode 100644
index 0000000000..f5577a1aa3
--- /dev/null
+++ b/alib2algo/src/tree/properties/FirstVariablesBarOffsetFront.h
@@ -0,0 +1,49 @@
+/*
+ * FirstVariablesBarOffsetFront.h
+ *
+ *  Created on: 5. 11. 2014
+ *      Author: Jan Travnicek
+ */
+
+#ifndef _FIRST_VARIABLES_BAR_OFFSET_FRONT_H_
+#define _FIRST_VARIABLES_BAR_OFFSET_FRONT_H_
+
+#include <tree/ranked/PrefixRankedBarPattern.h>
+#include <tree/ranked/PrefixRankedBarNonlinearPattern.h>
+
+namespace tree {
+
+namespace properties {
+
+class FirstVariablesBarOffsetFront {
+public:
+	template < class SymbolType, class RankType >
+	static size_t offset ( const tree::PrefixRankedBarPattern < SymbolType, RankType > & pattern );
+	template < class SymbolType, class RankType >
+	static size_t offset ( const tree::PrefixRankedBarNonlinearPattern < SymbolType, RankType > & pattern );
+
+};
+
+template < class SymbolType, class RankType >
+size_t FirstVariablesBarOffsetFront::offset ( const tree::PrefixRankedBarPattern < SymbolType, RankType > & pattern ) {
+	return offset ( tree::PrefixRankedBarNonlinearPattern < SymbolType, RankType > ( pattern ) );
+}
+
+template < class SymbolType, class RankType >
+size_t FirstVariablesBarOffsetFront::offset ( const tree::PrefixRankedBarNonlinearPattern < SymbolType, RankType > & pattern ) {
+	 // find the distance between the beginning of the pattern and the index
+	 // of the first symbol representing the variable's bar
+	size_t res = pattern.getContent ( ).size ( ) + 1;
+
+	for ( int i = ( int ) pattern.getContent ( ).size ( ) - 1; i >= 0; i-- )
+		if ( pattern.getContent ( )[i] == pattern.getVariablesBar ( ) )
+			res = i;
+
+	return res;
+}
+
+} /* namespace properties */
+
+} /* namespace tree */
+
+#endif /* _FIRST_VARIABLES_BAR_OFFSET_FRONT_H_ */
diff --git a/alib2algo/src/tree/properties/LastVariableOffsetBack.h b/alib2algo/src/tree/properties/LastVariableOffsetBack.h
new file mode 100644
index 0000000000..a498c3d37d
--- /dev/null
+++ b/alib2algo/src/tree/properties/LastVariableOffsetBack.h
@@ -0,0 +1,50 @@
+/*
+ * LastVariableOffsetBack.h
+ *
+ *  Created on: 2. 4. 2018
+ *      Author: Jan Travnicek
+ */
+
+#ifndef _LAST_VARIABLE_OFFSET_BACK_H_
+#define _LAST_VARIABLE_OFFSET_BACK_H_
+
+#include <alphabet/RankedSymbol.h>
+#include <tree/ranked/PrefixRankedBarPattern.h>
+#include <tree/ranked/PrefixRankedBarNonlinearPattern.h>
+
+namespace tree {
+
+namespace properties {
+
+class LastVariableOffsetBack {
+public:
+	template < class SymbolType, class RankType >
+	static size_t offset ( const tree::PrefixRankedBarPattern < SymbolType, RankType > & pattern );
+	template < class SymbolType, class RankType >
+	static size_t offset ( const tree::PrefixRankedBarNonlinearPattern < SymbolType, RankType > & pattern );
+
+};
+
+template < class SymbolType, class RankType >
+size_t LastVariableOffsetBack::offset ( const tree::PrefixRankedBarPattern < SymbolType, RankType > & pattern ) {
+	return offset ( tree::PrefixRankedBarNonlinearPattern < SymbolType, RankType > ( pattern ) );
+}
+
+template < class SymbolType, class RankType >
+size_t LastVariableOffsetBack::offset ( const tree::PrefixRankedBarNonlinearPattern < SymbolType, RankType > & pattern ) {
+	 // find the distance between the end of the pattern and the index
+	 // of the last symbol representing the variable
+	size_t offset = pattern.getContent ( ).size ( );
+
+	for ( size_t i = 0; i < pattern.getContent ( ).size ( ); i++ )
+		if ( pattern.getContent ( )[i] == pattern.getSubtreeWildcard ( ) || pattern.getNonlinearVariables ( ).count ( pattern.getContent ( )[i] ) )
+			offset = pattern.getContent ( ).size ( ) - i - 1;
+
+	return offset;
+}
+
+} /* namespace properties */
+
+} /* namespace tree */
+
+#endif /* _LAST_VARIABLE_OFFSET_BACK_H_ */
diff --git a/alib2algo/src/tree/properties/ReversedBadCharacterShiftTable.h b/alib2algo/src/tree/properties/ReversedBadCharacterShiftTable.h
index df7d3aff10..9f3978967a 100644
--- a/alib2algo/src/tree/properties/ReversedBadCharacterShiftTable.h
+++ b/alib2algo/src/tree/properties/ReversedBadCharacterShiftTable.h
@@ -16,6 +16,9 @@
 #include <tree/ranked/PrefixRankedPattern.h>
 #include <tree/ranked/PrefixRankedNonlinearPattern.h>
 
+#include "FirstVariablesBarOffsetFront.h"
+#include "FirstVariableOffsetFront.h"
+
 namespace tree {
 
 namespace properties {
@@ -61,11 +64,7 @@ ext::map < common::ranked_symbol < SymbolType, RankType >, size_t > ReversedBadC
 
 	 // find the distance between the beginning of the pattern and the index
 	 // of the first symbol representing the variable's bar
-	unsigned firstSBarOffset = pattern.getContent ( ).size ( ) + 1;
-
-	for ( int i = ( int ) pattern.getContent ( ).size ( ) - 1; i >= 0; i-- )
-		if ( pattern.getContent ( )[i] == pattern.getVariablesBar ( ) )
-			firstSBarOffset = i;
+	size_t firstSBarOffset = FirstVariablesBarOffsetFront::offset ( pattern );
 
 	// limit the shift by occurrence of the last variable
 
@@ -121,11 +120,7 @@ ext::map < common::ranked_symbol < SymbolType, RankType >, size_t > ReversedBadC
 
 	 // find the distance between the beginning of the pattern and the index
 	 // of the first symbol representing the variable's bar
-	unsigned firstSOffset = pattern.getContent ( ).size ( ) + 1;
-
-	for ( int i = ( int ) pattern.getContent ( ).size ( ) - 1; i >= 0; i-- )
-		if ( pattern.getContent ( )[i] == pattern.getSubtreeWildcard ( ) || pattern.getNonlinearVariables ( ).count ( pattern.getContent ( )[i] ) )
-			firstSOffset = i;
+	size_t firstSOffset = FirstVariableOffsetFront::offset ( pattern );
 
 	if ( firstSOffset == 0 ) firstSOffset = 1;
 
-- 
GitLab