From a505a3c855bd066a3bf8016150947c96f960a2e1 Mon Sep 17 00:00:00 2001
From: Jan Travnicek <Jan.Travnicek@fit.cvut.cz>
Date: Tue, 14 Jun 2016 20:24:11 +0200
Subject: [PATCH] simplify creation of unique symbols and states

---
 alib2algo/src/grammar/parsing/LR0Parser.cpp   | 16 ++++++-------
 alib2algo/src/grammar/parsing/LRParser.cpp    | 14 +++--------
 .../grammar/simplify/GrammarToCNFTest.cpp     |  6 ++---
 alib2data/src/alphabet/Symbol.cpp             | 24 +++++--------------
 alib2data/src/automaton/Automaton.cpp         |  6 ++---
 5 files changed, 23 insertions(+), 43 deletions(-)

diff --git a/alib2algo/src/grammar/parsing/LR0Parser.cpp b/alib2algo/src/grammar/parsing/LR0Parser.cpp
index 0f8ea06326..65354ff9bb 100644
--- a/alib2algo/src/grammar/parsing/LR0Parser.cpp
+++ b/alib2algo/src/grammar/parsing/LR0Parser.cpp
@@ -28,17 +28,17 @@ LR0Items LR0Parser::getClosure ( LR0Items items, grammar::CFG originalGrammar )
 				unsigned position = item.first;
 				std::vector < alphabet::Symbol > rightHandSide = item.second;
 
-				if ( position == rightHandSide.size ( ) ) {
+				if ( position == rightHandSide.size ( ) )
 					continue;
-				}
 
 				std::map < alphabet::Symbol, std::set < std::vector < alphabet::Symbol > > >::const_iterator rulesIterator = originalGrammar.getRules ( ) . find(rightHandSide[position]);
-				if ( rulesIterator != originalGrammar.getRules ( ) . end ( ) ) {
-					for ( const std::vector < alphabet::Symbol > & rule : rulesIterator->second ) {
-						if (items[rightHandSide[position]].find ( { 0, rule } ) == items[rightHandSide[position]].end ( ) ) {
-							changed = true;
-							items[rightHandSide[position]].insert ( { 0, rule } );
-						}
+				if ( rulesIterator == originalGrammar.getRules ( ) . end ( ) )
+					continue;
+
+				for ( const std::vector < alphabet::Symbol > & rule : rulesIterator->second ) {
+					if (items[rightHandSide[position]].find ( { 0, rule } ) == items[rightHandSide[position]].end ( ) ) {
+						changed = true;
+						items[rightHandSide[position]].insert ( { 0, rule } );
 					}
 				}
 			}
diff --git a/alib2algo/src/grammar/parsing/LRParser.cpp b/alib2algo/src/grammar/parsing/LRParser.cpp
index 5336ce63a3..6275d1442d 100644
--- a/alib2algo/src/grammar/parsing/LRParser.cpp
+++ b/alib2algo/src/grammar/parsing/LRParser.cpp
@@ -15,18 +15,11 @@ namespace grammar {
 namespace parsing {
 
 alphabet::Symbol LRParser::getEndOfInputSymbol ( grammar::CFG originalGrammar ) {
-	alphabet::Symbol endOfInputSymbol {alphabet::EndSymbol()};
-	while ( originalGrammar.getTerminalAlphabet ( ) . find ( endOfInputSymbol ) != originalGrammar.getTerminalAlphabet ( ) . end ( ) ) {
-		endOfInputSymbol.inc ( );
-	}
-	return endOfInputSymbol;
+	return alphabet::createUniqueSymbol ( alphabet::Symbol ( alphabet::EndSymbol ( ) ), originalGrammar.getTerminalAlphabet ( ), originalGrammar.getNonterminalAlphabet ( ) );
 }
 
 grammar::CFG LRParser::getAugmentedGrammar ( grammar::CFG originalGrammar ) {
-	alphabet::Symbol initialSymbol = originalGrammar.getInitialSymbol ( );
-	do {
-		initialSymbol = initialSymbol.next ( );
-	} while ( originalGrammar.getNonterminalAlphabet ( ) . find ( initialSymbol ) != originalGrammar.getNonterminalAlphabet ( ) . end ( ) );
+	alphabet::Symbol initialSymbol = alphabet::createUniqueSymbol ( originalGrammar.getInitialSymbol(), originalGrammar.getTerminalAlphabet ( ), originalGrammar.getNonterminalAlphabet ( ) );
 
 	originalGrammar.addNonterminalSymbol ( initialSymbol );
 	originalGrammar.addRule ( initialSymbol, { originalGrammar.getInitialSymbol ( ) } );
@@ -42,9 +35,8 @@ bool LRParser::parse ( LRActionTable actionTable, LRGotoTable gotoTable, automat
 	unsigned currentPosition = 0;
 	while ( true ) {
 		LRActionTable::iterator actionIterator = actionTable.find ( { states.top ( ), input[currentPosition] } );
-		if ( actionIterator == actionTable.end ( ) ) {
+		if ( actionIterator == actionTable.end ( ) )
 			return false;
-		}
 
 		switch ( actionIterator->second.first ) {
 			case LRAction::Shift:
diff --git a/alib2algo/test-src/grammar/simplify/GrammarToCNFTest.cpp b/alib2algo/test-src/grammar/simplify/GrammarToCNFTest.cpp
index c380d7864a..9efbbadbef 100644
--- a/alib2algo/test-src/grammar/simplify/GrammarToCNFTest.cpp
+++ b/alib2algo/test-src/grammar/simplify/GrammarToCNFTest.cpp
@@ -78,9 +78,9 @@ void GrammarToCNFTest::testToCNFRules2() {
 	alphabet::Symbol aP = alphabet::symbolFrom("a'");
 	alphabet::Symbol bP = alphabet::symbolFrom("b'");
 	alphabet::Symbol cP = alphabet::symbolFrom("c'");
-	alphabet::Symbol Xb = alphabet::Symbol(alphabet::UniqueSymbol(alphabet::Symbol(alphabet::SymbolPairSymbol(std::make_pair(X, b))), primitive::Integer(0)));
-	alphabet::Symbol aX = alphabet::Symbol(alphabet::UniqueSymbol(alphabet::Symbol(alphabet::SymbolPairSymbol(std::make_pair(a, X))), primitive::Integer(0)));
-	alphabet::Symbol bX = alphabet::Symbol(alphabet::UniqueSymbol(alphabet::Symbol(alphabet::SymbolPairSymbol(std::make_pair(b, X))), primitive::Integer(0)));
+	alphabet::Symbol Xb = alphabet::Symbol(alphabet::Symbol(alphabet::SymbolPairSymbol(std::make_pair(X, b))));
+	alphabet::Symbol aX = alphabet::Symbol(alphabet::Symbol(alphabet::SymbolPairSymbol(std::make_pair(a, X))));
+	alphabet::Symbol bX = alphabet::Symbol(alphabet::Symbol(alphabet::SymbolPairSymbol(std::make_pair(b, X))));
 
 	grammar::CNF grammar3(S);
 	grammar3.setNonterminalAlphabet({S, X, Y, aP, bP, cP, Xb, aX, bX});
diff --git a/alib2data/src/alphabet/Symbol.cpp b/alib2data/src/alphabet/Symbol.cpp
index 5950769270..886350a107 100644
--- a/alib2data/src/alphabet/Symbol.cpp
+++ b/alib2data/src/alphabet/Symbol.cpp
@@ -31,37 +31,25 @@ alphabet::Symbol createUniqueSymbol ( alphabet::Symbol attempt, const std::set <
 	int i = 0;
 
 	do {
-		attempt.inc ( );
-
 		if ( ( terminalAlphabet.count ( attempt ) == 0 ) && ( nonterminalAlphabet.count ( attempt ) == 0 ) )
 			return attempt;
-	} while ( ++i < INT_MAX );
+
+		attempt.inc ( );
+	} while ( i++ < INT_MAX );
 
 	throw SymbolException ( "Could not create unique symbol." );
 }
 
 alphabet::Symbol symbolFrom ( int number ) {
-	return alphabet::Symbol {
-			   alphabet::LabeledSymbol {
-				   label::labelFrom ( number )
-			   }
-	};
+	return alphabet::Symbol { alphabet::LabeledSymbol { label::labelFrom ( number ) } };
 }
 
 alphabet::Symbol symbolFrom ( char character ) {
-	return alphabet::Symbol {
-			   alphabet::LabeledSymbol {
-				   label::labelFrom ( character )
-			   }
-	};
+	return alphabet::Symbol { alphabet::LabeledSymbol { label::labelFrom ( character ) } };
 }
 
 alphabet::Symbol symbolFrom ( std::string string ) {
-	return alphabet::Symbol {
-			   alphabet::LabeledSymbol {
-				   label::labelFrom ( std::move ( string ) )
-			   }
-	};
+	return alphabet::Symbol { alphabet::LabeledSymbol { label::labelFrom ( std::move ( string ) ) } };
 }
 
 alphabet::Symbol symbolFrom ( const char * string ) {
diff --git a/alib2data/src/automaton/Automaton.cpp b/alib2data/src/automaton/Automaton.cpp
index e31eb82604..357ea9f76a 100644
--- a/alib2data/src/automaton/Automaton.cpp
+++ b/alib2data/src/automaton/Automaton.cpp
@@ -19,11 +19,11 @@ State createUniqueState ( State nextState, const std::set < State > & other ) {
 	int i = 0;
 
 	do {
-		nextState.getName ( ).inc ( );
-
 		if ( other.count ( nextState ) == 0 )
 			return nextState;
-	} while ( ++i < INT_MAX );
+
+		nextState.getName ( ).inc ( );
+	} while ( i++ < INT_MAX );
 
 	throw AutomatonException ( "Could not create unique state." );
 }
-- 
GitLab