diff --git a/alib2algo/src/rte/GlushkovTraversal.cpp b/alib2algo/src/rte/GlushkovTraversal.cpp index 04dc0fae19593f26c50bb92e97cba14b37fed638..048737a85746093aa63d4e7096d1b117518650c1 100644 --- a/alib2algo/src/rte/GlushkovTraversal.cpp +++ b/alib2algo/src/rte/GlushkovTraversal.cpp @@ -111,7 +111,7 @@ std::set < rte::FormalRTESymbol const * > GlushkovTraversal::first ( rte::Formal ret.insert ( tmp.begin ( ), tmp.end ( ) ); auto it = std::find_if ( ret.begin ( ), ret.end ( ), [node] ( rte::FormalRTESymbol const * a ) { - return a->getSymbol ( ) == node->getSubstitutionSymbol ( ); + return * a == node->getSubstitutionSymbol ( ); } ); if ( it != ret.end ( ) ) { @@ -335,7 +335,7 @@ void preprocessSubMap ( const std::set < alphabet::RankedSymbol > & alphabetK, s } } -std::set < std::vector < rte::FormalRTESymbol const * > > replaceConstants ( const std::set < alphabet::RankedSymbol > & alphabetK, const std::vector < rte::FormalRTESymbol const * > & f, const std::map < alphabet::RankedSymbol, std::set < rte::FormalRTESymbol const * > > & subMap2 ) { +std::set < std::vector < rte::FormalRTESymbol const * > > replaceConstants ( const std::set < alphabet::RankedSymbol > & alphabetK, const std::vector < std::smart_ptr < const rte::FormalRTESymbol > > & f, const std::map < alphabet::RankedSymbol, std::set < rte::FormalRTESymbol const * > > & subMap2 ) { // std::cerr << " replC for " << f << std::endl; std::set < std::vector < rte::FormalRTESymbol const * > > ret; @@ -349,7 +349,7 @@ std::set < std::vector < rte::FormalRTESymbol const * > > replaceConstants ( con if ( subIt == subMap.end ( ) ) { std::vector < rte::FormalRTESymbol const * > v; - v.push_back ( f[0] ); + v.push_back ( f[0].get() ); ret.insert ( v ); } else { for ( const auto & subSymbol : subIt->second ) { @@ -367,7 +367,7 @@ std::set < std::vector < rte::FormalRTESymbol const * > > replaceConstants ( con if ( subIt == subMap.end ( ) ) for ( const auto & r : ret ) { std::vector < rte::FormalRTESymbol const * > v = r; - v.push_back ( f[i] ); + v.push_back ( f[i].get() ); tmp.insert ( v ); } @@ -401,7 +401,7 @@ std::set < std::vector < rte::FormalRTESymbol const * > > replaceConstants ( con std::set < std::vector < rte::FormalRTESymbol const * > > GlushkovTraversal::follow ( rte::FormalRTESymbol const * const & node, rte::FormalRTESymbol const * const & symbolptr, const std::set < alphabet::RankedSymbol > & alphabetK, std::map < alphabet::RankedSymbol, std::set < rte::FormalRTESymbol const * > > & subMap ) { std::set < std::vector < rte::FormalRTESymbol const * > > ret, tmp; - std::vector < rte::FormalRTESymbol const * > children = node->getElements ( ); + const std::vector < std::smart_ptr < const rte::FormalRTESymbol > > & children = node->getElements ( ); if ( symbolptr == node ) { ret = replaceConstants ( alphabetK, children, subMap ); @@ -411,7 +411,7 @@ std::set < std::vector < rte::FormalRTESymbol const * > > GlushkovTraversal::fol } for ( const auto & c : children ) { - tmp = follow ( c, symbolptr, alphabetK, subMap ); + tmp = follow ( c.get(), symbolptr, alphabetK, subMap ); ret.insert ( tmp.begin ( ), tmp.end ( ) ); } @@ -465,7 +465,7 @@ bool GlushkovTraversal::pos ( rte::FormalRTESymbol const * const & node, rte::Fo if ( symbolptr == node ) return true; for ( auto const & element : node->getElements ( ) ) - if ( pos ( element, symbolptr ) ) + if ( pos ( element.get(), symbolptr ) ) return true; return false; @@ -500,7 +500,7 @@ void GlushkovTraversal::getSymbols ( rte::FormalRTEElement const * const & node, posSet.insert ( GlushkovSymbol ( symbol, i++ ) ); for ( const auto & c : symbol->getElements ( ) ) - getSymbols ( c, alphabetF, posSet, i ); + getSymbols ( c.get(), alphabetF, posSet, i ); return; } else if ( alternation ) { diff --git a/alib2data/src/rte/common/RTEToXMLComposer.cpp b/alib2data/src/rte/common/RTEToXMLComposer.cpp index a986b18bb1051595c46120328fecade55648e867..d83898460674bac3093b1e65e28d9ded9043f1b8 100644 --- a/alib2data/src/rte/common/RTEToXMLComposer.cpp +++ b/alib2data/src/rte/common/RTEToXMLComposer.cpp @@ -97,7 +97,7 @@ void RTEToXMLComposer::Visit ( void * userData, const FormalRTESymbol & symbol ) out.emplace_back ( sax::Token ( "symbol", sax::Token::TokenType::START_ELEMENT ) ); alib::xmlApi < alphabet::RankedSymbol >::compose ( out, symbol.getSymbol ( ) ); - for ( const FormalRTEElement * const & element : symbol.getElements ( ) ) + for ( const std::smart_ptr < const FormalRTESymbol > & element : symbol.getElements ( ) ) element->Accept ( userData, * this ); out.emplace_back ( sax::Token ( "symbol", sax::Token::TokenType::END_ELEMENT ) ); diff --git a/alib2data/src/rte/formal/FormalRTEAlternation.cpp b/alib2data/src/rte/formal/FormalRTEAlternation.cpp index 4e460103e49c6577c78210492473461da875f402..f7ccb0adb23f3138dde4177c8516cb4a9070833a 100644 --- a/alib2data/src/rte/formal/FormalRTEAlternation.cpp +++ b/alib2data/src/rte/formal/FormalRTEAlternation.cpp @@ -6,25 +6,20 @@ namespace rte { -FormalRTEAlternation::FormalRTEAlternation ( FormalRTEElement && left, FormalRTEElement && right ) { - this->left = std::move ( left ).plunder ( ); - this->right = std::move ( right ).plunder ( ); +FormalRTEAlternation::FormalRTEAlternation ( FormalRTEElement && leftElement, FormalRTEElement && rightElement ) : left ( nullptr ), right ( nullptr ) { + setLeftElement ( std::move ( leftElement ) ); + setRightElement ( std::move ( rightElement ) ); } -FormalRTEAlternation::FormalRTEAlternation ( const FormalRTEElement & left, const FormalRTEElement & right ) { - this->left = left.clone ( ); - this->right = right.clone ( ); +FormalRTEAlternation::FormalRTEAlternation ( const FormalRTEElement & left, const FormalRTEElement & right ) : FormalRTEAlternation ( std::move_copy ( left ), std::move_copy ( right ) ) { } -FormalRTEAlternation::FormalRTEAlternation ( const FormalRTEAlternation & other ) { - this->left = other.left->clone ( ); - this->right = other.right->clone ( ); +FormalRTEAlternation::FormalRTEAlternation ( const FormalRTEAlternation & other ) : left ( other.left ), right ( other.right ) { } FormalRTEAlternation::FormalRTEAlternation ( FormalRTEAlternation && other ) noexcept : left ( std::move ( other.left ) ), right ( std::move ( other.right ) ) { - other.left = NULL; - other.right = NULL; - this->attachRTE ( NULL ); + left->attachRTE ( NULL ); + right->attachRTE ( NULL ); } FormalRTEAlternation & FormalRTEAlternation::operator =( const FormalRTEAlternation & other ) { @@ -47,8 +42,6 @@ FormalRTEAlternation & FormalRTEAlternation::operator =( FormalRTEAlternation && } FormalRTEAlternation::~FormalRTEAlternation ( ) noexcept { - delete left; - delete right; } const FormalRTEElement & FormalRTEAlternation::getLeftElement ( ) const { @@ -72,13 +65,10 @@ void FormalRTEAlternation::setLeftElement ( const FormalRTEElement & element ) { } void FormalRTEAlternation::setLeftElement ( FormalRTEElement && element ) { - FormalRTEElement * elem = std::move ( element ).plunder ( ); + this->left = std::smart_ptr < FormalRTEElement > ( std::move ( element ).plunder ( ) ); - if ( this->parentRTE && !elem->attachRTE ( this->parentRTE ) ) + if ( this->parentRTE && !left->attachRTE ( this->parentRTE ) ) throw exception::CommonException ( "Input symbols not in the alphabet." ); - - delete left; - this->left = elem; } void FormalRTEAlternation::setRightElement ( const FormalRTEElement & element ) { @@ -86,13 +76,10 @@ void FormalRTEAlternation::setRightElement ( const FormalRTEElement & element ) } void FormalRTEAlternation::setRightElement ( FormalRTEElement && element ) { - FormalRTEElement * elem = std::move ( element ).plunder ( ); + this->right = std::smart_ptr < FormalRTEElement > ( std::move ( element ).plunder ( ) ); - if ( this->parentRTE && !elem->attachRTE ( this->parentRTE ) ) + if ( this->parentRTE && !right->attachRTE ( this->parentRTE ) ) throw exception::CommonException ( "Input symbols not in the alphabet." ); - - delete right; - this->right = elem; } FormalRTEAlternation * FormalRTEAlternation::clone ( ) const { diff --git a/alib2data/src/rte/formal/FormalRTEAlternation.h b/alib2data/src/rte/formal/FormalRTEAlternation.h index fb1f5555578599790df7b5068c9947ceca62b380..b7d2801143dbe5eeab6a4369e5776e47a0e3b26a 100644 --- a/alib2data/src/rte/formal/FormalRTEAlternation.h +++ b/alib2data/src/rte/formal/FormalRTEAlternation.h @@ -17,8 +17,8 @@ protected: // virtual UnboundedRTEElement * cloneAsUnbounded ( ) const; - FormalRTEElement * left; - FormalRTEElement * right; + std::smart_ptr < FormalRTEElement > left; + std::smart_ptr < FormalRTEElement > right; /** * @copydoc FormalRTEElement::attachRTE() diff --git a/alib2data/src/rte/formal/FormalRTEIteration.cpp b/alib2data/src/rte/formal/FormalRTEIteration.cpp index 8a719c8cda2bc07ebf209f26a7d2dc7433f49bc9..eb016a3ef1152b137c40c150167d6c547c41aa13 100644 --- a/alib2data/src/rte/formal/FormalRTEIteration.cpp +++ b/alib2data/src/rte/formal/FormalRTEIteration.cpp @@ -6,22 +6,20 @@ namespace rte { -FormalRTEIteration::FormalRTEIteration ( FormalRTEElement && element, FormalRTESymbol && substitutionSymbol ) : element ( NULL ), substitutionSymbol ( NULL ) { - this->setElement ( std::move ( element ) ); - this->setSubstitutionSymbol ( std::move ( substitutionSymbol ) ); +FormalRTEIteration::FormalRTEIteration ( FormalRTEElement && element, FormalRTESymbol substitutionSymbol ) : element ( nullptr ), substitutionSymbol ( nullptr ) { + setElement ( std::move ( element ) ); + setSubstitutionSymbol ( std::move ( substitutionSymbol ) ); } -FormalRTEIteration::FormalRTEIteration ( const FormalRTEElement & element, const FormalRTESymbol & substitutionSymbol ) : element ( NULL ), substitutionSymbol ( NULL ) { - this->setElement ( element ); - this->setSubstitutionSymbol ( substitutionSymbol ); +FormalRTEIteration::FormalRTEIteration ( const FormalRTEElement & element, FormalRTESymbol substitutionSymbol ) : FormalRTEIteration ( std::move_copy ( element ), std::move ( substitutionSymbol ) ) { } -FormalRTEIteration::FormalRTEIteration ( const FormalRTEIteration & other ) : element ( other.element->clone ( ) ), substitutionSymbol ( other.substitutionSymbol->clone ( ) ) { +FormalRTEIteration::FormalRTEIteration ( const FormalRTEIteration & other ) : element ( other.element ), substitutionSymbol ( other.substitutionSymbol ) { } -FormalRTEIteration::FormalRTEIteration ( FormalRTEIteration && other ) noexcept : element ( other.element ), substitutionSymbol ( std::move ( other.substitutionSymbol ) ) { - other.element = NULL; - this->attachRTE ( NULL ); +FormalRTEIteration::FormalRTEIteration ( FormalRTEIteration && other ) noexcept : element ( std::move ( other.element ) ), substitutionSymbol ( std::move ( other.substitutionSymbol ) ) { + element->attachRTE ( NULL ); + substitutionSymbol->attachRTE ( NULL ); } FormalRTEIteration & FormalRTEIteration::operator =( const FormalRTEIteration & other ) { @@ -44,7 +42,6 @@ FormalRTEIteration & FormalRTEIteration::operator =( FormalRTEIteration && other } FormalRTEIteration::~FormalRTEIteration ( ) noexcept { - delete element; } const FormalRTEElement & FormalRTEIteration::getElement ( ) const { @@ -67,18 +64,18 @@ void FormalRTEIteration::setElement ( const FormalRTEElement & element ) { setElement ( std::move_copy ( element ) ); } -void FormalRTEIteration::setElement ( FormalRTEElement && element ) { - FormalRTEElement * elem = std::move ( element ).plunder ( ); +void FormalRTEIteration::setElement ( FormalRTEElement && elementParam ) { + element = std::smart_ptr < FormalRTEElement > ( std::move ( elementParam ).plunder ( ) ); - if ( this->parentRTE && !this->element->attachRTE ( this->parentRTE ) ) + if ( this->parentRTE && !element->attachRTE ( this->parentRTE ) ) throw exception::CommonException ( "Input symbols not in the alphabet." ); - - delete this->element; - this->element = elem; } -void FormalRTEIteration::setSubstitutionSymbol ( FormalRTESymbol substitutionSymbol ) { - this->substitutionSymbol = std::move ( substitutionSymbol ).plunder ( ); +void FormalRTEIteration::setSubstitutionSymbol ( FormalRTESymbol symbol ) { + substitutionSymbol = std::smart_ptr < FormalRTESymbol > ( std::move ( symbol ).plunder ( ) ); + + if ( this->parentRTE && !substitutionSymbol->attachRTE ( this->parentRTE ) ) + throw exception::CommonException ( "Input symbols not in the alphabet." ); } FormalRTEIteration * FormalRTEIteration::clone ( ) const { @@ -107,7 +104,7 @@ int FormalRTEIteration::compare ( const FormalRTEIteration & other ) const { } void FormalRTEIteration::operator >>( std::ostream & out ) const { - out << "(RTEFormalRTEIteration " << substitutionSymbol << " " << * element << ")"; + out << "(RTEFormalRTEIteration " << * substitutionSymbol << " " << * element << ")"; } bool FormalRTEIteration::testSymbol ( const alphabet::RankedSymbol & symbol ) const { diff --git a/alib2data/src/rte/formal/FormalRTEIteration.h b/alib2data/src/rte/formal/FormalRTEIteration.h index e48bc6c049b8bcecd3b99a7b53b4474367c42934..2348476c183eea30ceb52ad6b659aa6e27dab96f 100644 --- a/alib2data/src/rte/formal/FormalRTEIteration.h +++ b/alib2data/src/rte/formal/FormalRTEIteration.h @@ -19,8 +19,8 @@ class RTEOptimize; */ class FormalRTEIteration : public FormalRTEElement { protected: - FormalRTEElement * element; - FormalRTESymbol * substitutionSymbol; + std::smart_ptr < FormalRTEElement > element; + std::smart_ptr < FormalRTESymbol > substitutionSymbol; /** * @copydoc FormalRTEElement::clone() const @@ -38,8 +38,8 @@ public: visitor.Visit ( userData, * this ); } - explicit FormalRTEIteration ( FormalRTEElement &&, FormalRTESymbol && ); - explicit FormalRTEIteration ( const FormalRTEElement &, const FormalRTESymbol & ); + explicit FormalRTEIteration ( FormalRTEElement &&, FormalRTESymbol ); + explicit FormalRTEIteration ( const FormalRTEElement &, FormalRTESymbol ); FormalRTEIteration ( const FormalRTEIteration & other ); FormalRTEIteration ( FormalRTEIteration && other ) noexcept; diff --git a/alib2data/src/rte/formal/FormalRTESubstitution.cpp b/alib2data/src/rte/formal/FormalRTESubstitution.cpp index c002c433678fe7a81785382536dd955f4ce80069..da512c949912bbea4f4f5310813a1c2d1e6eb525 100644 --- a/alib2data/src/rte/formal/FormalRTESubstitution.cpp +++ b/alib2data/src/rte/formal/FormalRTESubstitution.cpp @@ -6,26 +6,22 @@ namespace rte { -FormalRTESubstitution::FormalRTESubstitution ( FormalRTEElement && left, FormalRTEElement && right, FormalRTESymbol && substitutionSymbol ) : left ( NULL ), right ( NULL ), substitutionSymbol ( NULL ) { - this->left = std::move ( left ).plunder ( ); - this->right = std::move ( right ).plunder ( ); - this->substitutionSymbol = std::move ( substitutionSymbol ).plunder ( ); +FormalRTESubstitution::FormalRTESubstitution ( FormalRTEElement && left, FormalRTEElement && right, FormalRTESymbol substitutionSymbol ) : left ( nullptr ), right ( nullptr ), substitutionSymbol ( nullptr ) { + setLeftElement( std::move ( left ) ); + setRightElement ( std::move ( right ) ); + setSubstitutionSymbol ( std::move ( substitutionSymbol ) ); } -FormalRTESubstitution::FormalRTESubstitution ( const FormalRTEElement & left, const FormalRTEElement & right, const FormalRTESymbol & substitutionSymbol ) : left ( NULL ), right ( NULL ), substitutionSymbol ( NULL ) { - this->left = left.clone ( ); - this->right = right.clone ( ); - this->substitutionSymbol = substitutionSymbol.clone ( ); +FormalRTESubstitution::FormalRTESubstitution ( const FormalRTEElement & left, const FormalRTEElement & right, FormalRTESymbol substitutionSymbol ) : FormalRTESubstitution ( std::move_copy ( left ), std::move_copy ( right ), std::move ( substitutionSymbol ) ) { } -FormalRTESubstitution::FormalRTESubstitution ( const FormalRTESubstitution & other ) : left ( other.left->clone ( ) ), right ( other.right->clone ( ) ), substitutionSymbol ( other.substitutionSymbol->clone ( ) ) { +FormalRTESubstitution::FormalRTESubstitution ( const FormalRTESubstitution & other ) : left ( other.left ), right ( other.right ), substitutionSymbol ( other.substitutionSymbol ) { } FormalRTESubstitution::FormalRTESubstitution ( FormalRTESubstitution && other ) noexcept : left ( std::move ( other.left ) ), right ( std::move ( other.right ) ), substitutionSymbol ( std::move ( other.substitutionSymbol ) ) { - other.left = NULL; - other.right = NULL; - other.substitutionSymbol = NULL; - this->attachRTE ( NULL ); + left->attachRTE ( NULL ); + right->attachRTE ( NULL ); + substitutionSymbol->attachRTE ( NULL ); } FormalRTESubstitution & FormalRTESubstitution::operator =( const FormalRTESubstitution & other ) { @@ -49,8 +45,6 @@ FormalRTESubstitution & FormalRTESubstitution::operator =( FormalRTESubstitution } FormalRTESubstitution::~FormalRTESubstitution ( ) noexcept { - delete left; - delete right; } const FormalRTEElement & FormalRTESubstitution::getLeftElement ( ) const { @@ -82,13 +76,10 @@ void FormalRTESubstitution::setLeftElement ( const FormalRTEElement & element ) } void FormalRTESubstitution::setLeftElement ( FormalRTEElement && element ) { - FormalRTEElement * elem = std::move ( element ).plunder ( ); + left = std::smart_ptr < FormalRTEElement > ( std::move ( element ).plunder ( ) ); - if ( this->parentRTE && !elem->attachRTE ( this->parentRTE ) ) + if ( this->parentRTE && !left->attachRTE ( this->parentRTE ) ) throw exception::CommonException ( "Input symbols not in the alphabet." ); - - delete left; - this->left = elem; } void FormalRTESubstitution::setRightElement ( const FormalRTEElement & element ) { @@ -96,17 +87,17 @@ void FormalRTESubstitution::setRightElement ( const FormalRTEElement & element ) } void FormalRTESubstitution::setRightElement ( FormalRTEElement && element ) { - FormalRTEElement * elem = std::move ( element ).plunder ( ); + right = std::smart_ptr < FormalRTEElement > ( std::move ( element ).plunder ( ) ); - if ( this->parentRTE && !elem->attachRTE ( this->parentRTE ) ) + if ( this->parentRTE && !right->attachRTE ( this->parentRTE ) ) throw exception::CommonException ( "Input symbols not in the alphabet." ); - - delete left; - this->right = elem; } -void FormalRTESubstitution::setSubstitutionSymbol ( FormalRTESymbol substitutionSymbol ) { - this->substitutionSymbol = std::move ( substitutionSymbol ).plunder ( ); +void FormalRTESubstitution::setSubstitutionSymbol ( FormalRTESymbol symbol ) { + substitutionSymbol = std::smart_ptr < FormalRTESymbol > ( std::move ( symbol ).plunder ( ) ); + + if ( this->parentRTE && !substitutionSymbol->attachRTE ( this->parentRTE ) ) + throw exception::CommonException ( "Input symbols not in the alphabet." ); } FormalRTESubstitution * FormalRTESubstitution::clone ( ) const { @@ -144,7 +135,7 @@ int FormalRTESubstitution::compare ( const FormalRTESubstitution & other ) const void FormalRTESubstitution::operator >>( std::ostream & out ) const { out << "(FormalRTESubstitution"; out << " " << * left; - out << " " << substitutionSymbol; + out << " " << * substitutionSymbol; out << " " << * right; out << ")"; } diff --git a/alib2data/src/rte/formal/FormalRTESubstitution.h b/alib2data/src/rte/formal/FormalRTESubstitution.h index 835078bc9ee6d4cc2ed8c3378640a3e8e8094f9d..cedc6023c7f6c106bd68a877f893e89748d30462 100644 --- a/alib2data/src/rte/formal/FormalRTESubstitution.h +++ b/alib2data/src/rte/formal/FormalRTESubstitution.h @@ -19,9 +19,9 @@ protected: // virtual UnboundedRTEElement * cloneAsUnbounded ( ) const; - FormalRTEElement * left; - FormalRTEElement * right; - FormalRTESymbol * substitutionSymbol; // substite this in left by right + std::smart_ptr < FormalRTEElement > left; + std::smart_ptr < FormalRTEElement > right; + std::smart_ptr < FormalRTESymbol > substitutionSymbol; // substite this in left by right /** * @copydoc FormalRTEElement::attachRTE() @@ -33,8 +33,8 @@ public: visitor.Visit ( userData, * this ); } - explicit FormalRTESubstitution ( FormalRTEElement && left, FormalRTEElement && right, FormalRTESymbol && substitutionSymbol ); - explicit FormalRTESubstitution ( const FormalRTEElement & left, const FormalRTEElement & right, const FormalRTESymbol & substitutionSymbol ); + explicit FormalRTESubstitution ( FormalRTEElement && left, FormalRTEElement && right, FormalRTESymbol substitutionSymbol ); + explicit FormalRTESubstitution ( const FormalRTEElement & left, const FormalRTEElement & right, FormalRTESymbol substitutionSymbol ); FormalRTESubstitution ( const FormalRTESubstitution & other ); FormalRTESubstitution ( FormalRTESubstitution && other ) noexcept; diff --git a/alib2data/src/rte/formal/FormalRTESymbol.cpp b/alib2data/src/rte/formal/FormalRTESymbol.cpp index ea7037cc82493c62fefd01a827afe3cef7c9c28f..e6a0008b68178dd408369f3319fab4655e12da31 100644 --- a/alib2data/src/rte/formal/FormalRTESymbol.cpp +++ b/alib2data/src/rte/formal/FormalRTESymbol.cpp @@ -33,10 +33,7 @@ FormalRTESymbol & FormalRTESymbol::operator =( FormalRTESymbol && other ) noexce return * this; } -FormalRTESymbol::FormalRTESymbol ( const alphabet::RankedSymbol & symbol ) : symbol ( symbol ) { -} - -FormalRTESymbol::FormalRTESymbol ( alphabet::RankedSymbol && symbol ) : symbol ( std::move ( symbol ) ) { +FormalRTESymbol::FormalRTESymbol ( alphabet::RankedSymbol symbol ) : symbol ( std::move ( symbol ) ) { } FormalRTESymbol * FormalRTESymbol::clone ( ) const { @@ -47,36 +44,24 @@ FormalRTESymbol * FormalRTESymbol::plunder ( ) && { return new FormalRTESymbol ( std::move ( * this ) ); } -const std::vector < const FormalRTESymbol * > & FormalRTESymbol::getElements ( ) const { - return * reinterpret_cast < const std::vector < const FormalRTESymbol * > * > ( & elements ); +const std::vector < std::smart_ptr < const FormalRTESymbol > > & FormalRTESymbol::getElements ( ) const { + return reinterpret_cast < const std::vector < std::smart_ptr < const FormalRTESymbol > > & > ( elements ); } -std::vector < FormalRTESymbol * > & FormalRTESymbol::getElements ( ) { +std::vector < std::smart_ptr < FormalRTESymbol > > & FormalRTESymbol::getElements ( ) { return elements; } -void FormalRTESymbol::appendElement ( const FormalRTESymbol & element ) { - if ( primitive::Unsigned ( this->elements.size ( ) ) >= this->symbol.getRank ( ) ) - throw exception::CommonException ( "Ranked node cannot have more children then its rank" ); - - FormalRTESymbol * elem = element.clone ( ); - - if ( this->parentRTE && !elem->attachRTE ( this->parentRTE ) ) - throw exception::CommonException ( "Input symbols not in the alphabet." ); - - this->elements.push_back ( elem ); -} - -void FormalRTESymbol::appendElement ( FormalRTESymbol && element ) { +void FormalRTESymbol::appendElement ( FormalRTESymbol element ) { if ( primitive::Unsigned ( this->elements.size ( ) ) >= this->symbol.getRank ( ) ) throw exception::CommonException ( "Ranked node cannot have more children then its rank" ); - FormalRTESymbol * elem = std::move ( element ).plunder ( ); + std::smart_ptr < FormalRTESymbol > elem ( std::move ( element ).plunder ( ) ); if ( this->parentRTE && !elem->attachRTE ( this->parentRTE ) ) throw exception::CommonException ( "Input symbols not in the alphabet." ); - this->elements.push_back ( elem ); + this->elements.push_back ( std::move ( elem ) ); } /* @@ -84,13 +69,6 @@ void FormalRTESymbol::appendElement ( FormalRTESymbol && element ) { * return new UnboundedRTESymbol(this->symbol); * } */ -bool FormalRTESymbol::operator ==( const alphabet::RankedSymbol & other ) const { - return this->symbol == other; -} - -bool operator ==( const alphabet::RankedSymbol & first, const FormalRTESymbol & second ) { - return first == second.symbol; -} int FormalRTESymbol::compare ( const FormalRTESymbol & other ) const { auto first = std::tie ( symbol, getElements ( ) ); @@ -116,7 +94,7 @@ bool FormalRTESymbol::attachRTE ( const FormalRTE * regexp ) { if ( regexp == NULL ) return true; - for ( FormalRTESymbol * const & element : this->elements ) + for ( const std::smart_ptr < FormalRTESymbol > & element : this->elements ) if ( !element->attachRTE ( regexp ) ) return false; return this->parentRTE->getAlphabet ( ).find ( this->symbol ) != this->parentRTE->getAlphabet ( ).end ( ) || this->parentRTE->getConstantAlphabet ( ).find ( this->symbol ) != this->parentRTE->getConstantAlphabet ( ).end ( ); diff --git a/alib2data/src/rte/formal/FormalRTESymbol.h b/alib2data/src/rte/formal/FormalRTESymbol.h index 4dec3933f520a47820f7223bc8a3214170157e03..7468b3cd152faca68f42f568dfcea4da6dc4db9e 100644 --- a/alib2data/src/rte/formal/FormalRTESymbol.h +++ b/alib2data/src/rte/formal/FormalRTESymbol.h @@ -14,7 +14,7 @@ namespace rte { class FormalRTESymbol : public FormalRTEElement { protected: alphabet::RankedSymbol symbol; - std::vector < FormalRTESymbol * > elements; // can be empty (leaf) + std::vector < std::smart_ptr < FormalRTESymbol > > elements; // can be empty (leaf) /** * @copydoc FormalRTEElement::clone() const @@ -32,8 +32,7 @@ public: visitor.Visit ( userData, * this ); } - explicit FormalRTESymbol ( const alphabet::RankedSymbol & symbol ); - explicit FormalRTESymbol ( alphabet::RankedSymbol && symbol ); + explicit FormalRTESymbol ( alphabet::RankedSymbol symbol ); FormalRTESymbol ( const FormalRTESymbol & other ); FormalRTESymbol ( FormalRTESymbol && other ) noexcept; @@ -60,14 +59,10 @@ public: */ virtual void computeMinimalAlphabet ( std::set < alphabet::RankedSymbol > & alphabetF, std::set < alphabet::RankedSymbol > & alphabetK ) const; - const std::vector < const FormalRTESymbol * > & getElements ( ) const; - std::vector < FormalRTESymbol * > & getElements ( ); + const std::vector < std::smart_ptr < const FormalRTESymbol > > & getElements ( ) const; + std::vector < std::smart_ptr < FormalRTESymbol > > & getElements ( ); - void appendElement ( const FormalRTESymbol & element ); - void appendElement ( FormalRTESymbol && element ); - - bool operator ==( const alphabet::RankedSymbol & ) const; - friend bool operator ==( const alphabet::RankedSymbol &, const FormalRTESymbol & ); + void appendElement ( 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 ); @@ -88,6 +83,9 @@ public: const alphabet::RankedSymbol & getSymbol ( ) const; virtual explicit operator std::string ( ) const; + + friend class FormalRTEIteration; + friend class FormalRTESubstitution; }; } /* namespace rte */