Skip to content
Snippets Groups Projects
Commit 5834e381 authored by Jan Trávníček's avatar Jan Trávníček
Browse files

simplify internal representation of RTE Elements

parent 79638013
No related branches found
No related tags found
No related merge requests found
......@@ -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 ) {
......
......@@ -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 {
......
......@@ -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 );
......
......@@ -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 {
......
......@@ -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 );
......
......@@ -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 {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment