From 5834e38116eb979d7f36311d579ee0c54afbd166 Mon Sep 17 00:00:00 2001
From: Jan Travnicek <Jan.Travnicek@fit.cvut.cz>
Date: Thu, 23 Jun 2016 08:22:12 +0200
Subject: [PATCH] simplify internal representation of RTE Elements

---
 .../src/rte/formal/FormalRTEAlternation.cpp   | 16 ++--------
 .../src/rte/formal/FormalRTEIteration.cpp     | 21 +++++---------
 alib2data/src/rte/formal/FormalRTEIteration.h |  3 +-
 .../src/rte/formal/FormalRTESubstitution.cpp  | 29 +++++--------------
 .../src/rte/formal/FormalRTESubstitution.h    |  3 +-
 alib2data/src/rte/formal/FormalRTESymbol.cpp  | 23 +++------------
 6 files changed, 23 insertions(+), 72 deletions(-)

diff --git a/alib2data/src/rte/formal/FormalRTEAlternation.cpp b/alib2data/src/rte/formal/FormalRTEAlternation.cpp
index 31b5de9617..4e460103e4 100644
--- a/alib2data/src/rte/formal/FormalRTEAlternation.cpp
+++ b/alib2data/src/rte/formal/FormalRTEAlternation.cpp
@@ -68,13 +68,7 @@ FormalRTEElement & FormalRTEAlternation::getRightElement ( ) {
 }
 
 void FormalRTEAlternation::setLeftElement ( const FormalRTEElement & element ) {
-	FormalRTEElement * elem = element.clone ( );
-
-	if ( this->parentRTE && !elem->attachRTE ( this->parentRTE ) )
-		throw exception::CommonException ( "Input symbols not in the alphabet." );
-
-	delete left;
-	this->left = elem;
+	setLeftElement ( std::move_copy ( element ) );
 }
 
 void FormalRTEAlternation::setLeftElement ( FormalRTEElement && element ) {
@@ -88,13 +82,7 @@ void FormalRTEAlternation::setLeftElement ( FormalRTEElement && element ) {
 }
 
 void FormalRTEAlternation::setRightElement ( const FormalRTEElement & element ) {
-	FormalRTEElement * elem = element.clone ( );
-
-	if ( this->parentRTE && !elem->attachRTE ( this->parentRTE ) )
-		throw exception::CommonException ( "Input symbols not in the alphabet." );
-
-	delete right;
-	this->right = elem;
+	setRightElement ( std::move_copy ( element ) );
 }
 
 void FormalRTEAlternation::setRightElement ( FormalRTEElement && element ) {
diff --git a/alib2data/src/rte/formal/FormalRTEIteration.cpp b/alib2data/src/rte/formal/FormalRTEIteration.cpp
index 72f6b50a01..8a719c8cda 100644
--- a/alib2data/src/rte/formal/FormalRTEIteration.cpp
+++ b/alib2data/src/rte/formal/FormalRTEIteration.cpp
@@ -64,13 +64,7 @@ FormalRTESymbol & FormalRTEIteration::getSubstitutionSymbol ( ) {
 }
 
 void FormalRTEIteration::setElement ( const FormalRTEElement & element ) {
-	FormalRTEElement * elem = element.clone ( );
-
-	if ( this->parentRTE && !this->element->attachRTE ( this->parentRTE ) )
-		throw exception::CommonException ( "Input symbols not in the alphabet." );
-
-	delete this->element;
-	this->element = elem;
+	setElement ( std::move_copy ( element ) );
 }
 
 void FormalRTEIteration::setElement ( FormalRTEElement && element ) {
@@ -83,11 +77,7 @@ void FormalRTEIteration::setElement ( FormalRTEElement && element ) {
 	this->element = elem;
 }
 
-void FormalRTEIteration::setSubstitutionSymbol ( const FormalRTESymbol & substitutionSymbol ) {
-	this->substitutionSymbol = substitutionSymbol.clone ( );
-}
-
-void FormalRTEIteration::setSubstitutionSymbol ( FormalRTESymbol && substitutionSymbol ) {
+void FormalRTEIteration::setSubstitutionSymbol ( FormalRTESymbol substitutionSymbol ) {
 	this->substitutionSymbol = std::move ( substitutionSymbol ).plunder ( );
 }
 
@@ -108,7 +98,12 @@ FormalRTEIteration * FormalRTEIteration::plunder ( ) && {
  * }
  */
 int FormalRTEIteration::compare ( const FormalRTEIteration & other ) const {
-	return element->compare ( * other.element );
+	auto first = std::tie ( getElement ( ), getSubstitutionSymbol ( ) );
+	auto second = std::tie ( other.getElement ( ), other.getSubstitutionSymbol ( ) );
+
+	std::compare < decltype ( first ) > comp;
+
+	return comp ( first, second );
 }
 
 void FormalRTEIteration::operator >>( std::ostream & out ) const {
diff --git a/alib2data/src/rte/formal/FormalRTEIteration.h b/alib2data/src/rte/formal/FormalRTEIteration.h
index 4b228bfe2a..e48bc6c049 100644
--- a/alib2data/src/rte/formal/FormalRTEIteration.h
+++ b/alib2data/src/rte/formal/FormalRTEIteration.h
@@ -85,8 +85,7 @@ public:
 	void setElement ( const FormalRTEElement & element );
 	void setElement ( FormalRTEElement && element );
 
-	void setSubstitutionSymbol ( const FormalRTESymbol & element );
-	void setSubstitutionSymbol ( FormalRTESymbol && element );
+	void setSubstitutionSymbol ( FormalRTESymbol element );
 
 	virtual int compare ( const FormalRTEElement & other ) const {
 		if ( std::type_index ( typeid ( * this ) ) == std::type_index ( typeid ( other ) ) ) return this->compare ( ( decltype ( * this ) )other );
diff --git a/alib2data/src/rte/formal/FormalRTESubstitution.cpp b/alib2data/src/rte/formal/FormalRTESubstitution.cpp
index 16151201fc..c002c43367 100644
--- a/alib2data/src/rte/formal/FormalRTESubstitution.cpp
+++ b/alib2data/src/rte/formal/FormalRTESubstitution.cpp
@@ -78,13 +78,7 @@ FormalRTESymbol & FormalRTESubstitution::getSubstitutionSymbol ( ) {
 }
 
 void FormalRTESubstitution::setLeftElement ( const FormalRTEElement & element ) {
-	FormalRTEElement * elem = element.clone ( );
-
-	if ( this->parentRTE && !elem->attachRTE ( this->parentRTE ) )
-		throw exception::CommonException ( "Input symbols not in the alphabet." );
-
-	delete left;
-	this->left = elem;
+	setLeftElement ( std::move_copy ( element ) );
 }
 
 void FormalRTESubstitution::setLeftElement ( FormalRTEElement && element ) {
@@ -98,13 +92,7 @@ void FormalRTESubstitution::setLeftElement ( FormalRTEElement && element ) {
 }
 
 void FormalRTESubstitution::setRightElement ( const FormalRTEElement & element ) {
-	FormalRTEElement * elem = element.clone ( );
-
-	if ( this->parentRTE && !elem->attachRTE ( this->parentRTE ) )
-		throw exception::CommonException ( "Input symbols not in the alphabet." );
-
-	delete left;
-	this->right = elem;
+	setRightElement ( std::move_copy ( element ) );
 }
 
 void FormalRTESubstitution::setRightElement ( FormalRTEElement && element ) {
@@ -117,11 +105,7 @@ void FormalRTESubstitution::setRightElement ( FormalRTEElement && element ) {
 	this->right = elem;
 }
 
-void FormalRTESubstitution::setSubstitutionSymbol ( const FormalRTESymbol & substitutionSymbol ) {
-	this->substitutionSymbol = substitutionSymbol.clone ( );
-}
-
-void FormalRTESubstitution::setSubstitutionSymbol ( FormalRTESymbol && substitutionSymbol ) {
+void FormalRTESubstitution::setSubstitutionSymbol ( FormalRTESymbol substitutionSymbol ) {
 	this->substitutionSymbol = std::move ( substitutionSymbol ).plunder ( );
 }
 
@@ -149,11 +133,12 @@ FormalRTESubstitution * FormalRTESubstitution::plunder ( ) && {
 }
 
 int FormalRTESubstitution::compare ( const FormalRTESubstitution & other ) const {
-	int res = left->compare ( * other.left );
+	auto first = std::tie ( getLeftElement ( ), getRightElement ( ), getSubstitutionSymbol ( ) );
+	auto second = std::tie ( other.getLeftElement ( ), other.getRightElement ( ), other.getSubstitutionSymbol ( ) );
 
-	if ( res == 0 ) res = right->compare ( * other.right );
+	std::compare < decltype ( first ) > comp;
 
-	return res;
+	return comp ( first, second );
 }
 
 void FormalRTESubstitution::operator >>( std::ostream & out ) const {
diff --git a/alib2data/src/rte/formal/FormalRTESubstitution.h b/alib2data/src/rte/formal/FormalRTESubstitution.h
index c8cee7c513..835078bc9e 100644
--- a/alib2data/src/rte/formal/FormalRTESubstitution.h
+++ b/alib2data/src/rte/formal/FormalRTESubstitution.h
@@ -85,8 +85,7 @@ public:
 	void setRightElement ( const FormalRTEElement & element );
 	void setRightElement ( FormalRTEElement && element );
 
-	void setSubstitutionSymbol ( const FormalRTESymbol & element );
-	void setSubstitutionSymbol ( FormalRTESymbol && element );
+	void setSubstitutionSymbol ( FormalRTESymbol element );
 
 	virtual int compare ( const FormalRTEElement & other ) const {
 		if ( std::type_index ( typeid ( * this ) ) == std::type_index ( typeid ( other ) ) ) return this->compare ( ( decltype ( * this ) )other );
diff --git a/alib2data/src/rte/formal/FormalRTESymbol.cpp b/alib2data/src/rte/formal/FormalRTESymbol.cpp
index c7955055f4..ea7037cc82 100644
--- a/alib2data/src/rte/formal/FormalRTESymbol.cpp
+++ b/alib2data/src/rte/formal/FormalRTESymbol.cpp
@@ -93,27 +93,12 @@ bool operator ==( const alphabet::RankedSymbol & first, const FormalRTESymbol &
 }
 
 int FormalRTESymbol::compare ( const FormalRTESymbol & other ) const {
-	int res = this->symbol.compare ( other.symbol );
+	auto first = std::tie ( symbol, getElements ( ) );
+	auto second = std::tie ( symbol, other.getElements ( ) );
 
-	if ( res != 0 ) return res;
+	std::compare < decltype ( first ) > comp;
 
-	size_t thisSize	 = this->elements.size ( );
-	size_t otherSize = other.elements.size ( );
-
-	if ( thisSize < otherSize ) return -1;
-
-	if ( thisSize > otherSize ) return 1;
-
-	auto thisIter  = this->elements.begin ( );
-	auto otherIter = other.elements.begin ( );
-
-	for ( ; thisIter != this->elements.end ( ); ++thisIter, ++otherIter ) {
-		int res = ( * thisIter )->compare ( * * otherIter );
-
-		if ( res != 0 ) return res;
-	}
-
-	return 0;
+	return comp ( first, second );
 }
 
 void FormalRTESymbol::operator >>( std::ostream & out ) const {
-- 
GitLab