From 3f2da953e0b067a3353c423cfe7dd63b851e4ab8 Mon Sep 17 00:00:00 2001
From: Jan Travnicek <Jan.Travnicek@fit.cvut.cz>
Date: Wed, 19 Sep 2018 15:29:12 +0200
Subject: [PATCH] adapt grammar rename to template changes

---
 alib2algo/src/grammar/simplify/Rename.cpp | 65 +---------------------
 alib2algo/src/grammar/simplify/Rename.h   | 67 ++++++++++++++++++++++-
 2 files changed, 67 insertions(+), 65 deletions(-)

diff --git a/alib2algo/src/grammar/simplify/Rename.cpp b/alib2algo/src/grammar/simplify/Rename.cpp
index d13aaa7ced..57f0cd654c 100644
--- a/alib2algo/src/grammar/simplify/Rename.cpp
+++ b/alib2algo/src/grammar/simplify/Rename.cpp
@@ -6,75 +6,14 @@
  */
 
 #include "Rename.h"
-
-#include <alib/algorithm>
-#include <alib/map>
-#include <alib/vector>
-
-#include <alib/hexavigesimal>
 #include <registration/AlgoRegistration.hpp>
 
 namespace grammar {
 
 namespace simplify {
 
-grammar::RightRG < > Rename::rename ( const grammar::RightRG < > & rrg ) {
-	int counter = 0;
-
-	ext::map < DefaultSymbolType, DefaultSymbolType > renamingData;
-
-	for ( const DefaultSymbolType & symbol : rrg.getNonterminalAlphabet ( ) )
-		renamingData.insert ( std::make_pair ( symbol, DefaultSymbolType ( ext::toBase26 ( counter++ ) ) ) );
-
-	grammar::RightRG < > result ( renamingData.find ( rrg.getInitialSymbol ( ) )->second );
-	result.setGeneratesEpsilon ( rrg.getGeneratesEpsilon ( ) );
-	result.setTerminalAlphabet ( rrg.getTerminalAlphabet ( ) );
-
-	for ( const DefaultSymbolType & symbol : rrg.getNonterminalAlphabet ( ) )
-		result.addNonterminalSymbol ( renamingData.find ( symbol )->second );
-
-	for ( const std::pair < const DefaultSymbolType, ext::set < ext::variant < DefaultSymbolType, ext::pair < DefaultSymbolType, DefaultSymbolType > > > > & rule : rrg.getRules ( ) )
-		for ( const ext::variant < DefaultSymbolType, ext::pair < DefaultSymbolType, DefaultSymbolType > > & rhs : rule.second )
-			if ( rhs.is < DefaultSymbolType > ( ) ) {
-				result.addRule ( renamingData.find ( rule.first )->second, rhs.get < DefaultSymbolType > ( ) );
-			} else {
-				const ext::pair < DefaultSymbolType, DefaultSymbolType > & realRhs = rhs.get < ext::pair < DefaultSymbolType, DefaultSymbolType > > ( );
-				result.addRule ( renamingData.find ( rule.first )->second, ext::make_pair ( realRhs.first, renamingData.find ( realRhs.second )->second ) );
-			}
-
-	return result;
-}
-
-auto RenameRightRG = registration::AbstractRegister < Rename, grammar::RightRG < >, const grammar::RightRG < > & > ( Rename::rename );
-
-grammar::LeftRG < > Rename::rename ( const grammar::LeftRG < > & lrg ) {
-	int counter = 0;
-
-	ext::map < DefaultSymbolType, DefaultSymbolType > renamingData;
-
-	for ( const DefaultSymbolType & symbol : lrg.getNonterminalAlphabet ( ) )
-		renamingData.insert ( std::make_pair ( symbol, DefaultSymbolType ( ext::toBase26 ( counter++ ) ) ) );
-
-	grammar::LeftRG < > result ( renamingData.find ( lrg.getInitialSymbol ( ) )->second );
-	result.setGeneratesEpsilon ( lrg.getGeneratesEpsilon ( ) );
-	result.setTerminalAlphabet ( lrg.getTerminalAlphabet ( ) );
-
-	for ( const DefaultSymbolType & symbol : lrg.getNonterminalAlphabet ( ) )
-		result.addNonterminalSymbol ( renamingData.find ( symbol )->second );
-
-	for ( const std::pair < const DefaultSymbolType, ext::set < ext::variant < DefaultSymbolType, ext::pair < DefaultSymbolType, DefaultSymbolType > > > > & rule : lrg.getRules ( ) )
-		for ( const ext::variant < DefaultSymbolType, ext::pair < DefaultSymbolType, DefaultSymbolType > > & rhs : rule.second )
-			if ( rhs.is < DefaultSymbolType > ( ) ) {
-				result.addRule ( renamingData.find ( rule.first )->second, rhs.get < DefaultSymbolType > ( ) );
-			} else {
-				const ext::pair < DefaultSymbolType, DefaultSymbolType > & realRhs = rhs.get < ext::pair < DefaultSymbolType, DefaultSymbolType > > ( );
-				result.addRule ( renamingData.find ( rule.first )->second, ext::make_pair ( renamingData.find ( realRhs.first )->second, realRhs.second ) );
-			}
-
-	return result;
-}
-
-auto RenameLeftRG = registration::AbstractRegister < Rename, grammar::LeftRG < >, const grammar::LeftRG < > & > ( Rename::rename );
+auto RenameRightRG = registration::AbstractRegister < Rename, grammar::RightRG < DefaultSymbolType, unsigned >, const grammar::RightRG < > & > ( Rename::rename );
+auto RenameLeftRG = registration::AbstractRegister < Rename, grammar::LeftRG < DefaultSymbolType, unsigned >, const grammar::LeftRG < > & > ( Rename::rename );
 
 } /* namespace simplify */
 
diff --git a/alib2algo/src/grammar/simplify/Rename.h b/alib2algo/src/grammar/simplify/Rename.h
index 2578169e02..195d73c82b 100644
--- a/alib2algo/src/grammar/simplify/Rename.h
+++ b/alib2algo/src/grammar/simplify/Rename.h
@@ -11,16 +11,79 @@
 #include <grammar/Regular/LeftRG.h>
 #include <grammar/Regular/RightRG.h>
 
+#include <alib/algorithm>
+#include <alib/map>
+#include <alib/vector>
+
 namespace grammar {
 
 namespace simplify {
 
 class Rename {
 public:
-	static grammar::RightRG < > rename ( const grammar::RightRG < > & grammar );
-	static grammar::LeftRG < > rename ( const grammar::LeftRG < > & grammar );
+	template < class TerminalSymbolType, class NonterminalSymbolType >
+	static grammar::RightRG < TerminalSymbolType, unsigned > rename ( const grammar::RightRG < TerminalSymbolType, NonterminalSymbolType > & grammar );
+
+	template < class TerminalSymbolType, class NonterminalSymbolType >
+	static grammar::LeftRG < TerminalSymbolType, unsigned > rename ( const grammar::LeftRG < TerminalSymbolType, NonterminalSymbolType > & grammar );
 };
 
+template < class TerminalSymbolType, class NonterminalSymbolType >
+grammar::RightRG < TerminalSymbolType, unsigned > Rename::rename ( const grammar::RightRG < TerminalSymbolType, NonterminalSymbolType > & rrg ) {
+	unsigned counter = 0;
+
+	ext::map < NonterminalSymbolType, unsigned > renamingData;
+
+	for ( const NonterminalSymbolType & symbol : rrg.getNonterminalAlphabet ( ) )
+		renamingData.insert ( std::make_pair ( symbol, counter++ ) );
+
+	grammar::RightRG < TerminalSymbolType, unsigned > result ( renamingData.at ( rrg.getInitialSymbol ( ) ) );
+	result.setGeneratesEpsilon ( rrg.getGeneratesEpsilon ( ) );
+	result.setTerminalAlphabet ( rrg.getTerminalAlphabet ( ) );
+
+	for ( const NonterminalSymbolType & symbol : rrg.getNonterminalAlphabet ( ) )
+		result.addNonterminalSymbol ( renamingData.at ( symbol ) );
+
+	for ( const std::pair < const NonterminalSymbolType, ext::set < ext::variant < TerminalSymbolType, ext::pair < TerminalSymbolType, NonterminalSymbolType > > > > & rule : rrg.getRules ( ) )
+		for ( const ext::variant < TerminalSymbolType, ext::pair < TerminalSymbolType, NonterminalSymbolType > > & rhs : rule.second )
+			if ( rhs.template is < TerminalSymbolType > ( ) ) {
+				result.addRule ( renamingData.at ( rule.first ), rhs.template get < TerminalSymbolType > ( ) );
+			} else {
+				const ext::pair < TerminalSymbolType, NonterminalSymbolType > & realRhs = rhs.template get < ext::pair < TerminalSymbolType, NonterminalSymbolType > > ( );
+				result.addRule ( renamingData.at ( rule.first ), ext::make_pair ( realRhs.first, renamingData.at ( realRhs.second ) ) );
+			}
+
+	return result;
+}
+
+template < class TerminalSymbolType, class NonterminalSymbolType >
+grammar::LeftRG < TerminalSymbolType, unsigned > Rename::rename ( const grammar::LeftRG < TerminalSymbolType, NonterminalSymbolType > & lrg ) {
+	unsigned counter = 0;
+
+	ext::map < NonterminalSymbolType, unsigned > renamingData;
+
+	for ( const NonterminalSymbolType & symbol : lrg.getNonterminalAlphabet ( ) )
+		renamingData.insert ( std::make_pair ( symbol, counter++ ) );
+
+	grammar::LeftRG < TerminalSymbolType, unsigned > result ( renamingData.at ( lrg.getInitialSymbol ( ) ) );
+	result.setGeneratesEpsilon ( lrg.getGeneratesEpsilon ( ) );
+	result.setTerminalAlphabet ( lrg.getTerminalAlphabet ( ) );
+
+	for ( const NonterminalSymbolType & symbol : lrg.getNonterminalAlphabet ( ) )
+		result.addNonterminalSymbol ( renamingData.at ( symbol ) );
+
+	for ( const std::pair < const NonterminalSymbolType, ext::set < ext::variant < TerminalSymbolType, ext::pair < NonterminalSymbolType, TerminalSymbolType > > > > & rule : lrg.getRules ( ) )
+		for ( const ext::variant < TerminalSymbolType, ext::pair < NonterminalSymbolType, TerminalSymbolType > > & rhs : rule.second )
+			if ( rhs.template is < TerminalSymbolType > ( ) ) {
+				result.addRule ( renamingData.at ( rule.first ), rhs.template get < TerminalSymbolType > ( ) );
+			} else {
+				const ext::pair < NonterminalSymbolType, TerminalSymbolType > & realRhs = rhs.template get < ext::pair < NonterminalSymbolType, TerminalSymbolType > > ( );
+				result.addRule ( renamingData.at ( rule.first ), ext::make_pair ( renamingData.at ( realRhs.first ), realRhs.second ) );
+			}
+
+	return result;
+}
+
 } /* namespace simplify */
 
 } /* namespace grammar */
-- 
GitLab