From 2d8b321d424251838ac7089a28a7b18f0de8bf3e Mon Sep 17 00:00:00 2001
From: Jan Travnicek <Jan.Travnicek@fit.cvut.cz>
Date: Thu, 21 Feb 2019 20:53:45 +0100
Subject: [PATCH] glushkov rte preprocess substitution map while traversal

---
 .../rte/glushkov/GlushkovSubstitutionMap.h    | 64 ++++++++-----------
 1 file changed, 27 insertions(+), 37 deletions(-)

diff --git a/alib2algo/src/rte/glushkov/GlushkovSubstitutionMap.h b/alib2algo/src/rte/glushkov/GlushkovSubstitutionMap.h
index f4e91675c1..39991dda70 100644
--- a/alib2algo/src/rte/glushkov/GlushkovSubstitutionMap.h
+++ b/alib2algo/src/rte/glushkov/GlushkovSubstitutionMap.h
@@ -38,9 +38,6 @@ private:
 	template < class SymbolType, class RankType >
 	static void preprocessSubMap ( TSubstMap < SymbolType, RankType > & subMap, const TAlphabet < SymbolType, RankType > & alphabetK );
 
-	template < class SymbolType, class RankType >
-	static void subst_symbol_replaces ( ext::map < const rte::FormalRTEElement < ext::pair < SymbolType, unsigned >, RankType > *, TSubstMap < SymbolType, RankType > > & substMapTree, const TAlphabet < SymbolType, RankType > & alphabetK );
-
 public:
 	/**
 	 * @param re rte to probe
@@ -52,26 +49,19 @@ public:
 	template < class SymbolType, class RankType >
 	class Formal {
 	public:
-		static void visit ( const rte::FormalRTEElement < ext::pair < SymbolType, unsigned >, RankType > & node, TSubstMap < SymbolType, RankType > & subM, TSubstMapTree < SymbolType, RankType > & subMapTree );
-		static void visit ( const rte::FormalRTEAlternation < ext::pair < SymbolType, unsigned >, RankType > & node, TSubstMap < SymbolType, RankType > & subM, TSubstMapTree < SymbolType, RankType > & subMapTree );
-		static void visit ( const rte::FormalRTESubstitution < ext::pair < SymbolType, unsigned >, RankType > & node, TSubstMap < SymbolType, RankType > & subM, TSubstMapTree < SymbolType, RankType > & subMapTree );
-		static void visit ( const rte::FormalRTEIteration < ext::pair < SymbolType, unsigned >, RankType > & node, TSubstMap < SymbolType, RankType > & subM, TSubstMapTree < SymbolType, RankType > & subMapTree );
-		static void visit ( const rte::FormalRTESymbolAlphabet < ext::pair < SymbolType, unsigned >, RankType > & node, TSubstMap < SymbolType, RankType > & subM, TSubstMapTree < SymbolType, RankType > & subMapTree );
-		static void visit ( const rte::FormalRTESymbolSubst < ext::pair < SymbolType, unsigned >, RankType > & node, TSubstMap < SymbolType, RankType > & subM, TSubstMapTree < SymbolType, RankType > & subMapTree );
-		static void visit ( const rte::FormalRTEEmpty < ext::pair < SymbolType, unsigned >, RankType > & node, TSubstMap < SymbolType, RankType > & subM, TSubstMapTree < SymbolType, RankType > & subMapTree );
+		static void visit ( const rte::FormalRTEElement < ext::pair < SymbolType, unsigned >, RankType > & node, const TAlphabet < SymbolType, RankType > & alphabetK, TSubstMap < SymbolType, RankType > & subM, TSubstMapTree < SymbolType, RankType > & subMapTree );
+		static void visit ( const rte::FormalRTEAlternation < ext::pair < SymbolType, unsigned >, RankType > & node, const TAlphabet < SymbolType, RankType > & alphabetK, TSubstMap < SymbolType, RankType > & subM, TSubstMapTree < SymbolType, RankType > & subMapTree );
+		static void visit ( const rte::FormalRTESubstitution < ext::pair < SymbolType, unsigned >, RankType > & node, const TAlphabet < SymbolType, RankType > & alphabetK, TSubstMap < SymbolType, RankType > & subM, TSubstMapTree < SymbolType, RankType > & subMapTree );
+		static void visit ( const rte::FormalRTEIteration < ext::pair < SymbolType, unsigned >, RankType > & node, const TAlphabet < SymbolType, RankType > & alphabetK, TSubstMap < SymbolType, RankType > & subM, TSubstMapTree < SymbolType, RankType > & subMapTree );
+		static void visit ( const rte::FormalRTESymbolAlphabet < ext::pair < SymbolType, unsigned >, RankType > & node, const TAlphabet < SymbolType, RankType > & alphabetK, TSubstMap < SymbolType, RankType > & subM, TSubstMapTree < SymbolType, RankType > & subMapTree );
+		static void visit ( const rte::FormalRTESymbolSubst < ext::pair < SymbolType, unsigned >, RankType > & node, const TAlphabet < SymbolType, RankType > & alphabetK, TSubstMap < SymbolType, RankType > & subM, TSubstMapTree < SymbolType, RankType > & subMapTree );
+		static void visit ( const rte::FormalRTEEmpty < ext::pair < SymbolType, unsigned >, RankType > & node, const TAlphabet < SymbolType, RankType > & alphabetK, TSubstMap < SymbolType, RankType > & subM, TSubstMapTree < SymbolType, RankType > & subMapTree );
 	};
 
 };
 
 // -----------------------------------------------------------------------------
 
-template < class SymbolType, class RankType >
-void GlushkovSubstitutionMap::subst_symbol_replaces ( ext::map < const rte::FormalRTEElement < ext::pair < SymbolType, unsigned >, RankType > *, TSubstMap < SymbolType, RankType > > & substMapTree, const TAlphabet < SymbolType, RankType > & alphabetK ) {
-	for ( std::pair < const rte::FormalRTEElement < ext::pair < SymbolType, unsigned >, RankType > * const, TSubstMap < SymbolType, RankType > > & kv : substMapTree ) {
-		preprocessSubMap ( kv.second, alphabetK );
-	}
-}
-
 /**
  * Preprocessing:
  *  - Let k1, k2 be elements of alphabet K.
@@ -109,23 +99,23 @@ ext::map < const rte::FormalRTEElement < ext::pair < SymbolType, unsigned >, Ran
 		subMap.insert ( std::make_pair ( ssymb, TAlphabet < SymbolType, RankType > { } ) );
 
 	/* recursively compute substMap */
-	rte.getRTE ( ).getStructure ( ).template accept < void, GlushkovSubstitutionMap::Formal < SymbolType, RankType > > ( subMap, subMapTree );
-	subst_symbol_replaces ( subMapTree, rte.getSubstitutionAlphabet ( ) );
+	rte.getRTE ( ).getStructure ( ).template accept < void, GlushkovSubstitutionMap::Formal < SymbolType, RankType > > ( rte.getSubstitutionAlphabet ( ), subMap, subMapTree );
 	return subMapTree;
 }
 
 // -----------------------------------------------------------------------------
 
 template < class SymbolType, class RankType >
-void GlushkovSubstitutionMap::Formal < SymbolType, RankType >::visit ( const rte::FormalRTEAlternation < ext::pair < SymbolType, unsigned >, RankType > & node, TSubstMap < SymbolType, RankType > & subMap, TSubstMapTree < SymbolType, RankType > & subMapTree ) {
-	subMapTree.insert ( std::make_pair ( & node, subMap ) );
-	node.getLeftElement ( ).template accept < void, GlushkovSubstitutionMap::Formal < SymbolType, RankType > > ( subMap, subMapTree );
-	node.getRightElement ( ).template accept < void, GlushkovSubstitutionMap::Formal < SymbolType, RankType > > ( subMap, subMapTree );
+void GlushkovSubstitutionMap::Formal < SymbolType, RankType >::visit ( const rte::FormalRTEAlternation < ext::pair < SymbolType, unsigned >, RankType > & node, const TAlphabet < SymbolType, RankType > & alphabetK, TSubstMap < SymbolType, RankType > & subMap, TSubstMapTree < SymbolType, RankType > & subMapTree ) {
+	preprocessSubMap ( subMapTree.insert ( std::make_pair ( & node, subMap ) ).first->second, alphabetK );
+
+	node.getLeftElement ( ).template accept < void, GlushkovSubstitutionMap::Formal < SymbolType, RankType > > ( alphabetK, subMap, subMapTree );
+	node.getRightElement ( ).template accept < void, GlushkovSubstitutionMap::Formal < SymbolType, RankType > > ( alphabetK, subMap, subMapTree );
 }
 
 template < class SymbolType, class RankType >
-void GlushkovSubstitutionMap::Formal < SymbolType, RankType >::visit ( const rte::FormalRTESubstitution < ext::pair < SymbolType, unsigned >, RankType > & node, TSubstMap < SymbolType, RankType > & subMap, TSubstMapTree < SymbolType, RankType > & subMapTree ) {
-	subMapTree.insert ( std::make_pair ( & node, subMap ) );
+void GlushkovSubstitutionMap::Formal < SymbolType, RankType >::visit ( const rte::FormalRTESubstitution < ext::pair < SymbolType, unsigned >, RankType > & node, const TAlphabet < SymbolType, RankType > & alphabetK, TSubstMap < SymbolType, RankType > & subMap, TSubstMapTree < SymbolType, RankType > & subMapTree ) {
+	preprocessSubMap ( subMapTree.insert ( std::make_pair ( & node, subMap ) ).first->second, alphabetK );
 
 	// prepare left map
 	auto itMap = subMap.find ( node.getSubstitutionSymbol ( ).getSymbol ( ) );
@@ -135,40 +125,40 @@ void GlushkovSubstitutionMap::Formal < SymbolType, RankType >::visit ( const rte
 	for ( const auto & s : node.getRightElement ( ).template accept < ext::set < common::ranked_symbol < ext::pair < SymbolType, unsigned >, RankType > >, GlushkovFirst::Formal < ext::pair < SymbolType, unsigned >, RankType > > ( ) )
 		itMap->second.insert ( s );
 
-	node.getLeftElement ( ).template accept < void, GlushkovSubstitutionMap::Formal < SymbolType, RankType > > ( subMap, subMapTree );
+	node.getLeftElement ( ).template accept < void, GlushkovSubstitutionMap::Formal < SymbolType, RankType > > ( alphabetK, subMap, subMapTree );
 
 	// restore original map
 	itMap->second = std::move ( backup );
-	node.getRightElement ( ).template accept < void, GlushkovSubstitutionMap::Formal < SymbolType, RankType > > ( subMap, subMapTree );
+	node.getRightElement ( ).template accept < void, GlushkovSubstitutionMap::Formal < SymbolType, RankType > > ( alphabetK, subMap, subMapTree );
 }
 
 template < class SymbolType, class RankType >
-void GlushkovSubstitutionMap::Formal < SymbolType, RankType >::visit ( const rte::FormalRTEIteration < ext::pair < SymbolType, unsigned >, RankType > & node, TSubstMap < SymbolType, RankType > & subMap, TSubstMapTree < SymbolType, RankType > & subMapTree ) {
-	subMapTree.insert ( std::make_pair ( & node, subMap ) );
+void GlushkovSubstitutionMap::Formal < SymbolType, RankType >::visit ( const rte::FormalRTEIteration < ext::pair < SymbolType, unsigned >, RankType > & node, const TAlphabet < SymbolType, RankType > & alphabetK, TSubstMap < SymbolType, RankType > & subMap, TSubstMapTree < SymbolType, RankType > & subMapTree ) {
+	preprocessSubMap ( subMapTree.insert ( std::make_pair ( & node, subMap ) ).first->second, alphabetK );
 
 	for ( const auto & s : node.getElement ( ).template accept < TAlphabet < SymbolType, RankType >, GlushkovFirst::Formal < ext::pair < SymbolType, unsigned >, RankType > > ( ) )
 		subMap[node.getSubstitutionSymbol ( ).getSymbol ( )].insert ( s );
 
-	node.getElement ( ).template accept < void, GlushkovSubstitutionMap::Formal < SymbolType, RankType > > ( subMap, subMapTree );
+	node.getElement ( ).template accept < void, GlushkovSubstitutionMap::Formal < SymbolType, RankType > > ( alphabetK, subMap, subMapTree );
 }
 
 template < class SymbolType, class RankType >
-void GlushkovSubstitutionMap::Formal < SymbolType, RankType >::visit ( const rte::FormalRTESymbolAlphabet < ext::pair < SymbolType, unsigned >, RankType > & node, TSubstMap < SymbolType, RankType > & subMap, TSubstMapTree < SymbolType, RankType > & subMapTree ) {
-	subMapTree.insert ( std::make_pair ( & node, subMap ) );
+void GlushkovSubstitutionMap::Formal < SymbolType, RankType >::visit ( const rte::FormalRTESymbolAlphabet < ext::pair < SymbolType, unsigned >, RankType > & node, const TAlphabet < SymbolType, RankType > & alphabetK, TSubstMap < SymbolType, RankType > & subMap, TSubstMapTree < SymbolType, RankType > & subMapTree ) {
+	preprocessSubMap ( subMapTree.insert ( std::make_pair ( & node, subMap ) ).first->second, alphabetK );
 
 	for ( const auto & c : node.getElements ( ) ) {
-		c . template accept < void, GlushkovSubstitutionMap::Formal < SymbolType, RankType > > ( subMap, subMapTree );
+		c . template accept < void, GlushkovSubstitutionMap::Formal < SymbolType, RankType > > ( alphabetK, subMap, subMapTree );
 	}
 }
 
 template < class SymbolType, class RankType >
-void GlushkovSubstitutionMap::Formal < SymbolType, RankType >::visit ( const rte::FormalRTESymbolSubst < ext::pair < SymbolType, unsigned >, RankType > & node, TSubstMap < SymbolType, RankType > & subMap, TSubstMapTree < SymbolType, RankType > & subMapTree ) {
-	subMapTree.insert ( std::make_pair ( & node, subMap ) );
+void GlushkovSubstitutionMap::Formal < SymbolType, RankType >::visit ( const rte::FormalRTESymbolSubst < ext::pair < SymbolType, unsigned >, RankType > & node, const TAlphabet < SymbolType, RankType > & alphabetK, TSubstMap < SymbolType, RankType > & subMap, TSubstMapTree < SymbolType, RankType > & subMapTree ) {
+	preprocessSubMap ( subMapTree.insert ( std::make_pair ( & node, subMap ) ).first->second, alphabetK );
 }
 
 template < class SymbolType, class RankType >
-void GlushkovSubstitutionMap::Formal < SymbolType, RankType >::visit ( const rte::FormalRTEEmpty < ext::pair < SymbolType, unsigned >, RankType > & node, TSubstMap < SymbolType, RankType > & subMap, TSubstMapTree < SymbolType, RankType > & subMapTree ) {
-	subMapTree.insert ( std::make_pair ( & node, subMap ) );
+void GlushkovSubstitutionMap::Formal < SymbolType, RankType >::visit ( const rte::FormalRTEEmpty < ext::pair < SymbolType, unsigned >, RankType > & node, const TAlphabet < SymbolType, RankType > & alphabetK, TSubstMap < SymbolType, RankType > & subMap, TSubstMapTree < SymbolType, RankType > & subMapTree ) {
+	preprocessSubMap ( subMapTree.insert ( std::make_pair ( & node, subMap ) ).first->second, alphabetK );
 }
 
 } /* namespace rte */
-- 
GitLab