From 9927dfb9c2340985a5aba6d7a81bc147232a4c4f Mon Sep 17 00:00:00 2001
From: Jan Travnicek <Jan.Travnicek@fit.cvut.cz>
Date: Thu, 21 Feb 2019 08:43:04 +0100
Subject: [PATCH] simplify glushkov rte implementation

---
 .../convert/ToPostfixPushdownAutomatonGlushkov.h   | 13 +++----------
 .../ToPostfixPushdownAutomatonGlushkovNaive.h      | 14 ++++----------
 2 files changed, 7 insertions(+), 20 deletions(-)

diff --git a/alib2algo/src/rte/convert/ToPostfixPushdownAutomatonGlushkov.h b/alib2algo/src/rte/convert/ToPostfixPushdownAutomatonGlushkov.h
index 64289f293f..5a71ed75c5 100644
--- a/alib2algo/src/rte/convert/ToPostfixPushdownAutomatonGlushkov.h
+++ b/alib2algo/src/rte/convert/ToPostfixPushdownAutomatonGlushkov.h
@@ -10,6 +10,7 @@
 
 #include <alib/set>
 #include <alib/variant>
+#include <alib/algorithm>
 
 #include <global/GlobalData.h>
 
@@ -46,14 +47,6 @@ private:
 		return common::ranked_symbol < SymbolType, RankType > ( symbol.getSymbol ( ).first, symbol.getRank ( ) );
 	}
 
-	template < class SymbolType, class RankType >
-	static bool isSubstSymbolPresent ( const ext::set < common::ranked_symbol < ext::pair < SymbolType, unsigned >, RankType > > & container, const TAlphabet < SymbolType, RankType > & substAlphabet ) {
-		ext::vector < common::ranked_symbol < ext::pair < SymbolType, unsigned >, RankType > > intersection;
-		std::set_intersection ( container.begin ( ), container.end ( ), substAlphabet.begin ( ), substAlphabet.end ( ), std::back_inserter ( intersection ) );
-		return intersection.size ( ) > 0;
-	}
-
-
 public:
 	/**
 	 * Implements conversion of the regular tree expressions to a real-time height-deterministic pushdown automaton usign algorithm similar to Glushkov's method of neighbours.
@@ -87,7 +80,7 @@ automaton::NPDA < ext::variant < common::ranked_symbol < SymbolType, RankType >,
 		followSet.insert ( std::make_pair ( symbol, rte::GlushkovFollow::follow ( indexedRTE, symbol, substMapTree ) ) );
 
 	/* check for exceptions -> there must be NO substitution symbol in first set */
-	if ( isSubstSymbolPresent ( firstSet, indexedRTE.getSubstitutionAlphabet ( ) ) )
+	if ( ! ext::excludes ( firstSet.begin ( ), firstSet.end ( ), indexedRTE.getSubstitutionAlphabet ( ).begin ( ), indexedRTE.getSubstitutionAlphabet ( ).end ( ) ) )
 		throw exception::CommonException ( "GlushkovRTE: Substitution symbol appeared in the first set" );
 	/* check end */
 
@@ -95,7 +88,7 @@ automaton::NPDA < ext::variant < common::ranked_symbol < SymbolType, RankType >,
 	for ( const std::pair < const common::ranked_symbol < ext::pair < SymbolType, unsigned >, RankType >, ext::set < GlushkovFollow::TFollowTuple < SymbolType, RankType > > > & kv : followSet )
 		for ( const GlushkovFollow::TFollowTuple < SymbolType, RankType > & followTuple : kv.second ) // TFollowTuple = vector < set < ranked_symbol > >
 			for ( const ext::set < common::ranked_symbol < ext::pair < SymbolType, unsigned >, RankType > > & followTupleElem : followTuple )
-				if ( isSubstSymbolPresent ( followTupleElem, indexedRTE.getSubstitutionAlphabet ( ) ) )
+				if ( ! ext::excludes ( followTupleElem.begin ( ), followTupleElem.end ( ), indexedRTE.getSubstitutionAlphabet ( ).begin ( ), indexedRTE.getSubstitutionAlphabet ( ).end ( ) ) )
 					throw exception::CommonException ( "GlushkovRTE: Substitution symbol appeared in a follow set" );
 	/* check end */
 
diff --git a/alib2algo/src/rte/convert/ToPostfixPushdownAutomatonGlushkovNaive.h b/alib2algo/src/rte/convert/ToPostfixPushdownAutomatonGlushkovNaive.h
index 587df49cc9..fcdc55877c 100644
--- a/alib2algo/src/rte/convert/ToPostfixPushdownAutomatonGlushkovNaive.h
+++ b/alib2algo/src/rte/convert/ToPostfixPushdownAutomatonGlushkovNaive.h
@@ -35,13 +35,6 @@ class ToPostfixPushdownAutomatonGlushkovNaive {
 		return common::ranked_symbol < SymbolType, RankType > ( symbol.getSymbol ( ).first, symbol.getRank ( ) );
 	}
 
-	template < class SymbolType, class RankType >
-	static bool isSubstSymbolPresent ( const ext::set < common::ranked_symbol < ext::pair < SymbolType, unsigned >, RankType > > & container, const ext::set < common::ranked_symbol < ext::pair < SymbolType, unsigned >, RankType > > & substAlphabet ) {
-		ext::vector < common::ranked_symbol < ext::pair < SymbolType, unsigned >, RankType > > intersection;
-		std::set_intersection ( container.begin ( ), container.end ( ), substAlphabet.begin ( ), substAlphabet.end ( ), std::back_inserter ( intersection ) );
-		return intersection.size ( ) > 0;
-	}
-
 public:
 	/**
 	 * Implements conversion of the regular tree expressions to a real-time height-deterministic pushdown automaton usign algorithm similar to Glushkov's method of neighbours.
@@ -74,13 +67,14 @@ automaton::NPDA < ext::variant < common::ranked_symbol < SymbolType, RankType >,
 		followSet.insert ( std::make_pair ( symbol, rte::GlushkovFollowNaive::follow ( indexedRTE, symbol ) ) );
 
 	 /* check for exceptions -> there must be NO substitution symbol in first or follow sets */
-	if ( isSubstSymbolPresent ( firstSet, indexedRTE.getSubstitutionAlphabet ( ) ) )
+	if ( ! ext::excludes ( firstSet.begin ( ), firstSet.end ( ), indexedRTE.getSubstitutionAlphabet ( ).begin ( ), indexedRTE.getSubstitutionAlphabet ( ).end ( ) ) )
 		throw exception::CommonException ( "GlushkovRTE: Substitution symbol appeared in the first set" );
 
 	for ( const auto & kv : followSet )
 		for ( const auto & followTuple : kv.second )
-			if ( isSubstSymbolPresent ( ext::set < common::ranked_symbol < ext::pair < SymbolType, unsigned >, RankType > > ( followTuple.begin ( ), followTuple.end ( ) ), indexedRTE.getSubstitutionAlphabet ( ) ) )
-				throw exception::CommonException ( "GlushkovRTE: Substitution symbol appeared in a follow set" );
+			for ( const common::ranked_symbol < ext::pair < SymbolType, unsigned >, RankType > & followTupleElem : followTuple )
+				if ( indexedRTE.getSubstitutionAlphabet ( ).count ( followTupleElem ) )
+					throw exception::CommonException ( "GlushkovRTE: Substitution symbol appeared in a follow set" );
 
 	/* check end */
 
-- 
GitLab