From c078ee5fcf0780824e606808e0a438ef063a3948 Mon Sep 17 00:00:00 2001 From: Jan Travnicek <Jan.Travnicek@fit.cvut.cz> Date: Mon, 2 May 2016 20:56:40 +0200 Subject: [PATCH] make ranked trees use smart pointers --- .../src/arbology/exact/ExactPatternMatch.cpp | 8 +- .../src/arbology/exact/ExactSubtreeMatch.cpp | 4 +- .../exact/ExactSubtreeRepeatsNaive.cpp | 15 ++-- .../arbology/exact/ExactSubtreeRepeatsNaive.h | 2 +- .../src/tree/generate/RandomTreeFactory.cpp | 22 ++--- .../src/tree/common/TreeFromXMLParser.cpp | 13 ++- alib2data/src/tree/common/TreeFromXMLParser.h | 2 +- .../tree/ranked/PrefixRankedBarPattern.cpp | 2 +- .../src/tree/ranked/PrefixRankedBarTree.cpp | 2 +- .../src/tree/ranked/PrefixRankedPattern.cpp | 2 +- .../src/tree/ranked/PrefixRankedTree.cpp | 2 +- alib2data/src/tree/ranked/RankedNode.cpp | 84 ++++++------------- alib2data/src/tree/ranked/RankedNode.h | 31 ++----- .../tree/ranked/RankedNonlinearPattern.cpp | 26 +++--- .../src/tree/ranked/RankedNonlinearPattern.h | 2 +- alib2data/src/tree/ranked/RankedPattern.cpp | 26 +++--- alib2data/src/tree/ranked/RankedPattern.h | 2 +- alib2data/src/tree/ranked/RankedTree.cpp | 26 +++--- alib2data/src/tree/ranked/RankedTree.h | 2 +- alib2data/src/tree/unranked/UnrankedNode.cpp | 9 +- alib2data/src/tree/unranked/UnrankedNode.h | 2 +- .../src/tree/unranked/UnrankedPattern.cpp | 2 +- alib2data/src/tree/unranked/UnrankedTree.cpp | 2 +- alib2data/test-src/tree/PatternTest.cpp | 32 +++---- alib2data/test-src/tree/TreeTest.cpp | 72 ++++++++-------- alib2str/src/tree/TreeFromStringParser.cpp | 16 ++-- alib2str/src/tree/TreeFromStringParser.h | 2 +- alib2str/src/tree/TreeToStringComposer.cpp | 4 +- astat2/src/TreeStat.cpp | 6 +- 29 files changed, 174 insertions(+), 246 deletions(-) diff --git a/alib2algo/src/arbology/exact/ExactPatternMatch.cpp b/alib2algo/src/arbology/exact/ExactPatternMatch.cpp index 20d6f84b2c..aa2738b21a 100644 --- a/alib2algo/src/arbology/exact/ExactPatternMatch.cpp +++ b/alib2algo/src/arbology/exact/ExactPatternMatch.cpp @@ -48,7 +48,7 @@ bool ExactPatternMatch::matchHelper ( const tree::RankedNode & subject, const tr if ( subject.getSymbol ( ) != pattern.getSymbol ( ) ) return false; // ranked symbols are the same; test for number of children is not needed - for ( const std::tuple < const tree::RankedNode *, const tree::RankedNode * > & childs : std::make_tuple_foreach ( subject.getChildren ( ), pattern.getChildren ( ) ) ) + for ( const std::tuple < const std::smart_ptr < const tree::RankedNode >, const std::smart_ptr < const tree::RankedNode > > & childs : std::make_tuple_foreach ( subject.getChildren ( ), pattern.getChildren ( ) ) ) if ( !matchHelper ( * std::get < 0 > ( childs ), * std::get < 1 > ( childs ), subtreeVariable ) ) return false; return true; @@ -70,7 +70,7 @@ bool ExactPatternMatch::matchHelper ( const tree::RankedNode & subject, const tr if ( subject.getSymbol ( ) != pattern.getSymbol ( ) ) return false; // ranked symbols are the same; test for number of children is not needed - for ( const std::tuple < const tree::RankedNode *, const tree::RankedNode *, const tree::RankedNode * > & childs : std::make_tuple_foreach ( subject.getChildren ( ), pattern.getChildren ( ), repeats.getChildren ( ) ) ) + for ( const std::tuple < const std::smart_ptr < const tree::RankedNode >, const std::smart_ptr < const tree::RankedNode >, const std::smart_ptr < const tree::RankedNode > > & childs : std::make_tuple_foreach ( subject.getChildren ( ), pattern.getChildren ( ), repeats.getChildren ( ) ) ) if ( !matchHelper ( * std::get < 0 > ( childs ), * std::get < 1 > ( childs ), subtreeVariable, nonlinearVariables, * std::get < 2 > ( childs ), variablesSetting ) ) return false; return true; @@ -90,7 +90,7 @@ void ExactPatternMatch::matchInternal ( unsigned & index, std::set < unsigned > index++; - for ( const tree::RankedNode * child : subject.getChildren ( ) ) + for ( const std::smart_ptr < const tree::RankedNode > & child : subject.getChildren ( ) ) matchInternal ( index, occ, * child, pattern, subtreeVariable ); } @@ -101,7 +101,7 @@ void ExactPatternMatch::matchInternal ( unsigned & index, std::set < unsigned > index++; - for ( const std::tuple < const tree::RankedNode *, const tree::RankedNode * > & childs : std::make_tuple_foreach ( subject.getChildren ( ), repeats.getChildren ( ) ) ) + for ( const std::tuple < const std::smart_ptr < const tree::RankedNode >, const std::smart_ptr < const tree::RankedNode > > & childs : std::make_tuple_foreach ( subject.getChildren ( ), repeats.getChildren ( ) ) ) matchInternal ( index, occ, * std::get < 0 > ( childs ), pattern, subtreeVariable, nonlinearVariables, * std::get < 1 > ( childs ) ); } diff --git a/alib2algo/src/arbology/exact/ExactSubtreeMatch.cpp b/alib2algo/src/arbology/exact/ExactSubtreeMatch.cpp index 99137ea418..c3aae2a0d2 100644 --- a/alib2algo/src/arbology/exact/ExactSubtreeMatch.cpp +++ b/alib2algo/src/arbology/exact/ExactSubtreeMatch.cpp @@ -37,7 +37,7 @@ bool ExactSubtreeMatch::matchHelper ( const tree::RankedNode & subject, const tr if ( subject.getSymbol ( ) != pattern.getSymbol ( ) ) return false; // ranked symbols are the same; test for number of children is not needed - for ( const std::tuple < const tree::RankedNode *, const tree::RankedNode * > & childs : std::make_tuple_foreach ( subject.getChildren ( ), pattern.getChildren ( ) ) ) + for ( const std::tuple < const std::smart_ptr < const tree::RankedNode >, const std::smart_ptr < const tree::RankedNode > > & childs : std::make_tuple_foreach ( subject.getChildren ( ), pattern.getChildren ( ) ) ) if ( !matchHelper ( * std::get < 0 > ( childs ), * std::get < 1 > ( childs ) ) ) return false; return true; @@ -57,7 +57,7 @@ void ExactSubtreeMatch::matchInternal ( unsigned & index, std::set < unsigned > index++; - for ( const tree::RankedNode * child : subject.getChildren ( ) ) + for ( const std::smart_ptr < const tree::RankedNode > & child : subject.getChildren ( ) ) matchInternal ( index, occ, * child, pattern ); } diff --git a/alib2algo/src/arbology/exact/ExactSubtreeRepeatsNaive.cpp b/alib2algo/src/arbology/exact/ExactSubtreeRepeatsNaive.cpp index c9ecef4b8b..32e1ee6cae 100644 --- a/alib2algo/src/arbology/exact/ExactSubtreeRepeatsNaive.cpp +++ b/alib2algo/src/arbology/exact/ExactSubtreeRepeatsNaive.cpp @@ -22,12 +22,12 @@ tree::Tree ExactSubtreeRepeatsNaive::repeats ( const tree::Tree & tree ) { return dispatch ( tree.getData ( ) ); } -tree::RankedNode * ExactSubtreeRepeatsNaive::repeats ( const tree::RankedNode & node, std::map < std::pair < alphabet::RankedSymbol, std::vector < alphabet::RankedSymbol > >, int > & data, int & minId ) { - std::vector < tree::RankedNode * > children; +tree::RankedNode ExactSubtreeRepeatsNaive::repeats ( const tree::RankedNode & node, std::map < std::pair < alphabet::RankedSymbol, std::vector < alphabet::RankedSymbol > >, int > & data, int & minId ) { + std::vector < std::smart_ptr < tree::RankedNode > > children; std::pair < alphabet::RankedSymbol, std::vector < alphabet::RankedSymbol > > childRepeatsKey ( node.getSymbol ( ), std::vector < alphabet::RankedSymbol > ( ) ); - for ( const tree::RankedNode * child : node.getChildren ( ) ) { - children.push_back ( repeats ( * child, data, minId ) ); + for ( const std::smart_ptr < const tree::RankedNode > & child : node.getChildren() ) { + children.push_back ( std::make_smart < tree::RankedNode > ( repeats ( * child, data, minId ) ) ); childRepeatsKey.second.push_back ( children.back ( )->getSymbol ( ) ); } @@ -35,17 +35,16 @@ tree::RankedNode * ExactSubtreeRepeatsNaive::repeats ( const tree::RankedNode & if ( uniqueRepeatId == 0 ) uniqueRepeatId = minId++; - return new tree::RankedNode ( alphabet::RankedSymbol ( alphabet::symbolFrom ( uniqueRepeatId ), node.getSymbol ( ).getRank ( ) ), std::move ( children ) ); + return tree::RankedNode ( alphabet::RankedSymbol ( alphabet::symbolFrom ( uniqueRepeatId ), node.getSymbol ( ).getRank ( ) ), std::move ( children ) ); } tree::RankedTree ExactSubtreeRepeatsNaive::repeats ( const tree::RankedTree & tree ) { int minId = 1; std::map < std::pair < alphabet::RankedSymbol, std::vector < alphabet::RankedSymbol > >, int > data; - tree::RankedNode * resNode = repeats ( tree.getRoot ( ), data, minId ); - tree::RankedTree res ( * resNode ); + tree::RankedNode resNode = repeats ( tree.getRoot ( ), data, minId ); + tree::RankedTree res ( resNode ); - delete resNode; return res; } diff --git a/alib2algo/src/arbology/exact/ExactSubtreeRepeatsNaive.h b/alib2algo/src/arbology/exact/ExactSubtreeRepeatsNaive.h index 7664fa21aa..2218428cf5 100644 --- a/alib2algo/src/arbology/exact/ExactSubtreeRepeatsNaive.h +++ b/alib2algo/src/arbology/exact/ExactSubtreeRepeatsNaive.h @@ -23,7 +23,7 @@ namespace exact { * Simple computation of subtree repeats */ class ExactSubtreeRepeatsNaive : public std::SingleDispatch < ExactSubtreeRepeatsNaive, tree::Tree, tree::TreeBase > { - static tree::RankedNode * repeats ( const tree::RankedNode & node, std::map < std::pair < alphabet::RankedSymbol, std::vector < alphabet::RankedSymbol > >, int > & data, int & minId ); + static tree::RankedNode repeats ( const tree::RankedNode & node, std::map < std::pair < alphabet::RankedSymbol, std::vector < alphabet::RankedSymbol > >, int > & data, int & minId ); static alphabet::RankedSymbol repeatsPrefixRanked ( const std::vector < alphabet::RankedSymbol > & symbols, std::vector < alphabet::RankedSymbol > & res, std::map < std::pair < alphabet::RankedSymbol, std::vector < alphabet::RankedSymbol > >, int > & data, int & minId, int & index ); static alphabet::RankedSymbol repeatsPrefixRankedBar ( const std::vector < alphabet::RankedSymbol > & symbols, std::vector < alphabet::RankedSymbol > & res, std::map < std::pair < alphabet::RankedSymbol, std::vector < alphabet::RankedSymbol > >, int > & data, int & minId, int & index ); diff --git a/alib2algo/src/tree/generate/RandomTreeFactory.cpp b/alib2algo/src/tree/generate/RandomTreeFactory.cpp index 07c05f451f..d0cd078eb3 100644 --- a/alib2algo/src/tree/generate/RandomTreeFactory.cpp +++ b/alib2algo/src/tree/generate/RandomTreeFactory.cpp @@ -133,31 +133,31 @@ struct Node { } } - RankedNode * createRankedNode ( ) { - std::vector < RankedNode * > children; + RankedNode createRankedNode ( ) { + std::vector < std::smart_ptr < RankedNode > > children; Node * nextChild = child; for ( int i = 0; i < rank; i++ ) { - children.push_back ( nextChild->createRankedNode ( ) ); + children.emplace_back ( std::make_smart < RankedNode > ( nextChild->createRankedNode ( ) ) ); nextChild = nextChild->right; } - return new RankedNode ( alphabet::RankedSymbol ( symbol, rank ), children ); + return RankedNode ( alphabet::RankedSymbol ( symbol, rank ), std::move ( children ) ); } - RankedNode * createRankedPatternNode ( ) { + RankedNode createRankedPatternNode ( ) { if ( rank == 0 ) { - return new RankedNode ( alphabet::RankedSymbol ( alphabet::Symbol ( alphabet::SubtreeWildcardSymbol::SUBTREE_WILDCARD ), 0 ), { } ); + return RankedNode ( alphabet::RankedSymbol ( alphabet::Symbol ( alphabet::SubtreeWildcardSymbol::SUBTREE_WILDCARD ), 0 ), { } ); } else { - std::vector < RankedNode * > children; + std::vector < std::smart_ptr < RankedNode > > children; Node * nextChild = child; for ( int i = 0; i < rank; i++ ) { - children.push_back ( nextChild->createRankedPatternNode ( ) ); + children.emplace_back ( std::make_smart < RankedNode > ( nextChild->createRankedPatternNode ( ) ) ); nextChild = nextChild->right; } - return new RankedNode ( alphabet::RankedSymbol ( symbol, rank ), children ); + return RankedNode ( alphabet::RankedSymbol ( symbol, rank ), std::move ( children ) ); } } @@ -314,7 +314,7 @@ RankedTree RandomTreeFactory::generateRankedTree ( int depth, int nodesCount, in treeRankedAlphabet.insert ( alphabet::RankedSymbol ( * i, it->first ) ); } - RankedTree tree ( treeRankedAlphabet, std::move ( * root->createRankedNode ( ) ) ); + RankedTree tree ( treeRankedAlphabet, std::move ( root->createRankedNode ( ) ) ); delete root; return tree; } @@ -338,7 +338,7 @@ RankedPattern RandomTreeFactory::generateRankedPattern ( int depth, int nodesCou alphabet::RankedSymbol subtreeWildcard ( alphabet::Symbol ( alphabet::SubtreeWildcardSymbol::SUBTREE_WILDCARD ), 0 ); treeRankedAlphabet.insert ( subtreeWildcard ); - RankedPattern tree ( std::move ( subtreeWildcard ), treeRankedAlphabet, std::move ( * root->createRankedPatternNode ( ) ) ); + RankedPattern tree ( std::move ( subtreeWildcard ), treeRankedAlphabet, std::move ( root->createRankedPatternNode ( ) ) ); delete root; return tree; } diff --git a/alib2data/src/tree/common/TreeFromXMLParser.cpp b/alib2data/src/tree/common/TreeFromXMLParser.cpp index 3b42f2b95c..79f8383925 100644 --- a/alib2data/src/tree/common/TreeFromXMLParser.cpp +++ b/alib2data/src/tree/common/TreeFromXMLParser.cpp @@ -88,20 +88,17 @@ std::vector < alphabet::Symbol > TreeFromXMLParser::parseUnrankedContent ( std:: return data; } -RankedNode * TreeFromXMLParser::parseRankedNode ( std::deque < sax::Token >::iterator & input ) { +RankedNode TreeFromXMLParser::parseRankedNode ( std::deque < sax::Token >::iterator & input ) { sax::FromXMLParserHelper::popToken ( input, sax::Token::TokenType::START_ELEMENT, "rankedNode" ); alphabet::RankedSymbol symbol = alib::xmlApi < alphabet::RankedSymbol >::parse ( input ); - int rank = symbol.getRank ( ).getData ( ); - std::vector < RankedNode * > children; - - children.reserve ( rank ); + std::vector < std::smart_ptr < RankedNode > > children; while ( sax::FromXMLParserHelper::isTokenType ( input, sax::Token::TokenType::START_ELEMENT ) ) - children.push_back ( parseRankedNode ( input ) ); + children.emplace_back ( std::make_smart< RankedNode > ( parseRankedNode ( input ) ) ); sax::FromXMLParserHelper::popToken ( input, sax::Token::TokenType::END_ELEMENT, "rankedNode" ); - return new RankedNode ( symbol, children ); + return RankedNode ( std::move ( symbol ), std::move ( children ) ); } UnrankedNode * TreeFromXMLParser::parseUnrankedNode ( std::deque < sax::Token >::iterator & input ) { @@ -114,7 +111,7 @@ UnrankedNode * TreeFromXMLParser::parseUnrankedNode ( std::deque < sax::Token >: sax::FromXMLParserHelper::popToken ( input, sax::Token::TokenType::END_ELEMENT, "unrankedNode" ); - return new UnrankedNode ( symbol, children ); + return new UnrankedNode ( std::move ( symbol ), std::move ( children ) ); } std::set < alphabet::RankedSymbol > TreeFromXMLParser::parseRankedNonlinearVariables ( std::deque < sax::Token >::iterator & input ) { diff --git a/alib2data/src/tree/common/TreeFromXMLParser.h b/alib2data/src/tree/common/TreeFromXMLParser.h index 400758941e..3aab8d039c 100644 --- a/alib2data/src/tree/common/TreeFromXMLParser.h +++ b/alib2data/src/tree/common/TreeFromXMLParser.h @@ -24,7 +24,7 @@ namespace tree { */ class TreeFromXMLParser { public: - static RankedNode * parseRankedNode ( std::deque < sax::Token >::iterator & input ); + static RankedNode parseRankedNode ( std::deque < sax::Token >::iterator & input ); static UnrankedNode * parseUnrankedNode ( std::deque < sax::Token >::iterator & input ); static std::vector < alphabet::RankedSymbol > parseRankedContent ( std::deque < sax::Token >::iterator & input ); diff --git a/alib2data/src/tree/ranked/PrefixRankedBarPattern.cpp b/alib2data/src/tree/ranked/PrefixRankedBarPattern.cpp index 1f17bbe9fa..495ad4e013 100644 --- a/alib2data/src/tree/ranked/PrefixRankedBarPattern.cpp +++ b/alib2data/src/tree/ranked/PrefixRankedBarPattern.cpp @@ -60,7 +60,7 @@ void PrefixRankedBarPattern::toPrefixRankedBar ( const RankedNode & node ) { } else { m_Data.push_back ( node.getSymbol ( ) ); - for ( const RankedNode * child : node.getChildren ( ) ) + for ( const std::smart_ptr < const RankedNode > & child : node.getChildren ( ) ) toPrefixRankedBar ( * child ); m_Data.push_back ( alphabet::RankedSymbol ( bar, node.getSymbol ( ).getRank ( ) ) ); diff --git a/alib2data/src/tree/ranked/PrefixRankedBarTree.cpp b/alib2data/src/tree/ranked/PrefixRankedBarTree.cpp index 9c8fd9c4d7..ccb874c71e 100644 --- a/alib2data/src/tree/ranked/PrefixRankedBarTree.cpp +++ b/alib2data/src/tree/ranked/PrefixRankedBarTree.cpp @@ -48,7 +48,7 @@ PrefixRankedBarTree::PrefixRankedBarTree ( const RankedTree & tree ) : PrefixRan void PrefixRankedBarTree::toPrefixRankedBar ( const RankedNode & node ) { m_Data.push_back ( node.getSymbol ( ) ); - for ( const RankedNode * child : node.getChildren ( ) ) + for ( const std::smart_ptr < const RankedNode > & child : node.getChildren ( ) ) toPrefixRankedBar ( * child ); m_Data.push_back ( alphabet::RankedSymbol ( bar, node.getSymbol ( ).getRank ( ) ) ); diff --git a/alib2data/src/tree/ranked/PrefixRankedPattern.cpp b/alib2data/src/tree/ranked/PrefixRankedPattern.cpp index 9601d6c962..dcd4c392a8 100644 --- a/alib2data/src/tree/ranked/PrefixRankedPattern.cpp +++ b/alib2data/src/tree/ranked/PrefixRankedPattern.cpp @@ -48,7 +48,7 @@ void PrefixRankedPattern::toPrefixRanked ( const RankedNode & node ) { } else { m_Data.push_back ( node.getSymbol ( ) ); - for ( const RankedNode * child : node.getChildren ( ) ) + for ( const std::smart_ptr < const RankedNode > & child : node.getChildren ( ) ) toPrefixRanked ( * child ); } } diff --git a/alib2data/src/tree/ranked/PrefixRankedTree.cpp b/alib2data/src/tree/ranked/PrefixRankedTree.cpp index 48d3c8d62c..abfee11826 100644 --- a/alib2data/src/tree/ranked/PrefixRankedTree.cpp +++ b/alib2data/src/tree/ranked/PrefixRankedTree.cpp @@ -39,7 +39,7 @@ PrefixRankedTree::PrefixRankedTree ( const RankedTree & tree ) : std::Components void PrefixRankedTree::toPrefixRanked ( const RankedNode & node ) { m_Data.push_back ( node.getSymbol ( ) ); - for ( const RankedNode * child : node.getChildren ( ) ) + for ( const std::smart_ptr < const RankedNode > & child : node.getChildren ( ) ) toPrefixRanked ( * child ); } diff --git a/alib2data/src/tree/ranked/RankedNode.cpp b/alib2data/src/tree/ranked/RankedNode.cpp index b1b7615143..6cea1fef7c 100644 --- a/alib2data/src/tree/ranked/RankedNode.cpp +++ b/alib2data/src/tree/ranked/RankedNode.cpp @@ -15,21 +15,18 @@ namespace tree { -RankedNode::RankedNode ( alphabet::RankedSymbol symbol, std::vector < RankedNode * > children ) : symbol ( std::move ( symbol ) ), children ( std::move ( children ) ), alphabet ( NULL ) { +RankedNode::RankedNode ( alphabet::RankedSymbol symbol, std::vector < std::smart_ptr < RankedNode > > children ) : symbol ( std::move ( symbol ) ), children ( std::move ( children ) ), alphabet ( NULL ) { if ( this->children.size ( ) != this->symbol.getRank ( ).getData ( ) ) throw TreeException ( "Number of children doesn't match the rank of the symbol" ); - for ( auto & element : this->children ) + for ( std::smart_ptr < RankedNode > & element : this->children ) element->parent = this; this->attachAlphabet ( NULL ); this->parent = NULL; } -RankedNode::RankedNode ( const RankedNode & other ) : symbol ( other.symbol ), alphabet ( NULL ) { - for ( const auto & element : other.children ) - children.push_back ( element->clone ( ) ); - - for ( auto & element : this->children ) +RankedNode::RankedNode ( const RankedNode & other ) : symbol ( other.symbol ), children ( other.children ), alphabet ( NULL ) { + for ( std::smart_ptr < RankedNode > & element : this->children ) element->parent = this; this->attachAlphabet ( NULL ); @@ -37,9 +34,7 @@ RankedNode::RankedNode ( const RankedNode & other ) : symbol ( other.symbol ), a } RankedNode::RankedNode ( RankedNode && other ) noexcept : symbol ( std::move ( other.symbol ) ), children ( std::move ( other.children ) ), alphabet ( NULL ) { - other.children.clear ( ); - - for ( auto & element : this->children ) + for ( std::smart_ptr < RankedNode > & element : this->children ) element->parent = this; this->attachAlphabet ( NULL ); @@ -69,17 +64,13 @@ RankedNode & RankedNode::operator =( RankedNode && other ) noexcept { } RankedNode::~RankedNode ( ) noexcept { - for ( auto element : children ) - delete element; - - children.clear ( ); } -const std::vector < const RankedNode * > & RankedNode::getChildren ( ) const { - return * reinterpret_cast < const std::vector < const RankedNode * > * > ( & children ); +const std::vector < std::smart_ptr < const RankedNode > > & RankedNode::getChildren ( ) const { + return reinterpret_cast < const std::vector < std::smart_ptr < const RankedNode > > & > ( children ); } -const std::vector < RankedNode * > & RankedNode::getChildren ( ) { +const std::vector < std::smart_ptr < RankedNode > > & RankedNode::getChildren ( ) { return children; } @@ -112,45 +103,22 @@ void RankedNode::swap ( RankedNode & other ) { other.attachAlphabet ( otherParentTree ); } -RankedNode * RankedNode::clone ( ) const { - return new RankedNode ( * this ); -} - -RankedNode * RankedNode::plunder ( ) && { - return new RankedNode ( std::move ( * this ) ); -} - -UnrankedNode * RankedNode::cloneAsUnranked ( ) const { - std::vector < UnrankedNode * > clonedChildren; +UnrankedNode RankedNode::asUnranked ( ) const { + std::vector < UnrankedNode * > unrankedChildren; - for ( const RankedNode * child : this->children ) - clonedChildren.push_back ( child->cloneAsUnranked ( ) ); + for ( const std::smart_ptr < const RankedNode > & child : this->getChildren() ) + unrankedChildren.push_back ( new UnrankedNode ( child->asUnranked ( ) ) ); - return new UnrankedNode ( this->symbol.getSymbol ( ), std::move ( clonedChildren ) ); + return UnrankedNode ( this->symbol.getSymbol ( ), std::move ( unrankedChildren ) ); } int RankedNode::compare ( const RankedNode & other ) const { - int res = this->symbol.compare ( other.symbol ); + auto first = std::tie ( symbol, getChildren() ); + auto second = std::tie ( other.symbol, other.getChildren() ); - if ( res != 0 ) return res; + std::compare < decltype ( first ) > comp; - int thisSize = this->children.size ( ); - int otherSize = other.children.size ( ); - - if ( thisSize < otherSize ) return -1; - - if ( thisSize > otherSize ) return 1; - - auto thisIter = this->children.begin ( ); - auto otherIter = other.children.begin ( ); - - for ( ; thisIter != this->children.end ( ); ++thisIter, ++otherIter ) { - int res = ( * thisIter )->compare ( * * otherIter ); - - if ( res != 0 ) return res; - } - - return 0; + return comp ( first, second ); } void RankedNode::operator >>( std::ostream & out ) const { @@ -165,7 +133,7 @@ std::ostream & operator <<( std::ostream & out, const RankedNode & node ) { bool RankedNode::testSymbol ( const alphabet::RankedSymbol & symbol ) const { if ( symbol == this->symbol ) return true; - for ( const auto & child : this->children ) + for ( const std::smart_ptr < const RankedNode > & child : this->getChildren() ) if ( child->testSymbol ( symbol ) ) return true; return false; @@ -176,7 +144,7 @@ bool RankedNode::attachAlphabet ( const std::set < alphabet::RankedSymbol > * al this->alphabet = alphabet; - for ( const auto & child : this->children ) + for ( std::smart_ptr < RankedNode > & child : this->children ) if ( !child->attachAlphabet ( alphabet ) ) return false; if ( ( this->alphabet != NULL ) && ( this->alphabet->count ( this->symbol ) == 0 ) ) return false; @@ -187,7 +155,7 @@ bool RankedNode::attachAlphabet ( const std::set < alphabet::RankedSymbol > * al void RankedNode::computeMinimalAlphabet ( std::set < alphabet::RankedSymbol > & alphabet ) const { alphabet.insert ( this->symbol ); - for ( const auto & child : this->children ) + for ( const std::smart_ptr < const RankedNode > & child : this->getChildren() ) child->computeMinimalAlphabet ( alphabet ); } @@ -226,20 +194,16 @@ void RankedNode::nicePrint ( std::ostream & os, const std::string & prefix, cons namespace alib { -tree::RankedNode * xmlApi < tree::RankedNode * >::parse ( std::deque < sax::Token >::iterator & input ) { +tree::RankedNode xmlApi < tree::RankedNode >::parse ( std::deque < sax::Token >::iterator & input ) { return tree::TreeFromXMLParser::parseRankedNode ( input ); } -bool xmlApi < tree::RankedNode * >::first ( const std::deque < sax::Token >::const_iterator & ) { +bool xmlApi < tree::RankedNode >::first ( const std::deque < sax::Token >::const_iterator & ) { throw tree::TreeException ( "Unimplemented" ); } -void xmlApi < tree::RankedNode * >::compose ( std::deque < sax::Token > & output, const tree::RankedNode * const & data ) { - tree::TreeToXMLComposer::composeNode ( output, * data ); -} - -void xmlApi < const tree::RankedNode * >::compose ( std::deque < sax::Token > & output, const tree::RankedNode * const & data ) { - tree::TreeToXMLComposer::composeNode ( output, * data ); +void xmlApi < tree::RankedNode >::compose ( std::deque < sax::Token > & output, const tree::RankedNode & data ) { + tree::TreeToXMLComposer::composeNode ( output, data ); } } /* namespace alib */ diff --git a/alib2data/src/tree/ranked/RankedNode.h b/alib2data/src/tree/ranked/RankedNode.h index b389880358..a441bdb545 100644 --- a/alib2data/src/tree/ranked/RankedNode.h +++ b/alib2data/src/tree/ranked/RankedNode.h @@ -24,7 +24,7 @@ class RankedNode { protected: alphabet::RankedSymbol symbol; - std::vector < RankedNode * > children; + std::vector < std::smart_ptr < RankedNode > > children; RankedNode * parent; @@ -44,7 +44,7 @@ protected: void computeMinimalAlphabet ( std::set < alphabet::RankedSymbol > & alphabet ) const; public: - explicit RankedNode ( alphabet::RankedSymbol symbol, std::vector < RankedNode * > children ); + explicit RankedNode ( alphabet::RankedSymbol symbol, std::vector < std::smart_ptr < RankedNode > > children ); RankedNode ( const RankedNode & other ); RankedNode ( RankedNode && other ) noexcept; @@ -52,20 +52,10 @@ public: RankedNode & operator =( RankedNode && other ) noexcept; ~RankedNode ( ) noexcept; - /** - * @copydoc RankedNode::clone() const - */ - RankedNode * clone ( ) const; - - /** - * @copydoc RankedNode::plunder() const - */ - RankedNode * plunder ( ) &&; - /** * @copydoc RankedNode::cloneAsUnranked() const */ - UnrankedNode * cloneAsUnranked ( ) const; + UnrankedNode asUnranked ( ) const; /** * @copydoc RankedNode::testSymbol() const @@ -75,12 +65,12 @@ public: /** * @return children */ - const std::vector < const RankedNode * > & getChildren ( ) const; + const std::vector < std::smart_ptr < const RankedNode > > & getChildren ( ) const; /** * @return children */ - const std::vector < RankedNode * > & getChildren ( ); + const std::vector < std::smart_ptr < RankedNode > > & getChildren ( ); RankedNode * getParent ( ); @@ -120,15 +110,10 @@ public: namespace alib { template < > -struct xmlApi < tree::RankedNode * > { - static tree::RankedNode * parse ( std::deque < sax::Token >::iterator & input ); +struct xmlApi < tree::RankedNode > { + static tree::RankedNode parse ( std::deque < sax::Token >::iterator & input ); static bool first ( const std::deque < sax::Token >::const_iterator & input ); - static void compose ( std::deque < sax::Token > & output, const tree::RankedNode * const & data ); -}; - -template < > -struct xmlApi < const tree::RankedNode * > { - static void compose ( std::deque < sax::Token > & output, const tree::RankedNode * const & data ); + static void compose ( std::deque < sax::Token > & output, const tree::RankedNode & data ); }; } /* namespace alib */ diff --git a/alib2data/src/tree/ranked/RankedNonlinearPattern.cpp b/alib2data/src/tree/ranked/RankedNonlinearPattern.cpp index b579ad3ebf..6b4962fedf 100644 --- a/alib2data/src/tree/ranked/RankedNonlinearPattern.cpp +++ b/alib2data/src/tree/ranked/RankedNonlinearPattern.cpp @@ -25,27 +25,26 @@ namespace tree { -RankedNonlinearPattern::RankedNonlinearPattern ( alphabet::RankedSymbol subtreeWildcard, std::set < alphabet::RankedSymbol > nonlinearVariables, std::set < alphabet::RankedSymbol > alphabet, RankedNode pattern ) : std::Components < RankedNonlinearPattern, alphabet::RankedSymbol, std::tuple < GeneralAlphabet, NonlinearAlphabet >, std::tuple < SubtreeWildcard > > ( std::make_tuple ( std::move ( alphabet ), std::move ( nonlinearVariables ) ), std::make_tuple ( std::move ( subtreeWildcard ) ) ), pattern ( NULL ) { +RankedNonlinearPattern::RankedNonlinearPattern ( alphabet::RankedSymbol subtreeWildcard, std::set < alphabet::RankedSymbol > nonlinearVariables, std::set < alphabet::RankedSymbol > alphabet, RankedNode pattern ) : std::Components < RankedNonlinearPattern, alphabet::RankedSymbol, std::tuple < GeneralAlphabet, NonlinearAlphabet >, std::tuple < SubtreeWildcard > > ( std::make_tuple ( std::move ( alphabet ), std::move ( nonlinearVariables ) ), std::make_tuple ( std::move ( subtreeWildcard ) ) ), pattern ( nullptr ) { setTree ( std::move ( pattern ) ); } -RankedNonlinearPattern::RankedNonlinearPattern ( alphabet::RankedSymbol subtreeWildcard, std::set < alphabet::RankedSymbol > nonlinearVariables, RankedNode pattern ) : std::Components < RankedNonlinearPattern, alphabet::RankedSymbol, std::tuple < GeneralAlphabet, NonlinearAlphabet >, std::tuple < SubtreeWildcard > > ( std::make_tuple ( nonlinearVariables + std::set < alphabet::RankedSymbol > { subtreeWildcard }, nonlinearVariables ), std::make_tuple ( subtreeWildcard ) ), pattern ( NULL ) { +RankedNonlinearPattern::RankedNonlinearPattern ( alphabet::RankedSymbol subtreeWildcard, std::set < alphabet::RankedSymbol > nonlinearVariables, RankedNode pattern ) : std::Components < RankedNonlinearPattern, alphabet::RankedSymbol, std::tuple < GeneralAlphabet, NonlinearAlphabet >, std::tuple < SubtreeWildcard > > ( std::make_tuple ( nonlinearVariables + std::set < alphabet::RankedSymbol > { subtreeWildcard }, nonlinearVariables ), std::make_tuple ( subtreeWildcard ) ), pattern ( nullptr ) { pattern.computeMinimalAlphabet ( accessComponent < GeneralAlphabet > ( ).get ( ) ); setTree ( std::move ( pattern ) ); } -RankedNonlinearPattern::RankedNonlinearPattern ( alphabet::RankedSymbol subtreeWildcard, RankedNode pattern ) : std::Components < RankedNonlinearPattern, alphabet::RankedSymbol, std::tuple < GeneralAlphabet, NonlinearAlphabet >, std::tuple < SubtreeWildcard > > ( std::make_tuple ( std::set < alphabet::RankedSymbol > { subtreeWildcard }, std::set < alphabet::RankedSymbol > { } ), std::make_tuple ( subtreeWildcard ) ), pattern ( NULL ) { +RankedNonlinearPattern::RankedNonlinearPattern ( alphabet::RankedSymbol subtreeWildcard, RankedNode pattern ) : std::Components < RankedNonlinearPattern, alphabet::RankedSymbol, std::tuple < GeneralAlphabet, NonlinearAlphabet >, std::tuple < SubtreeWildcard > > ( std::make_tuple ( std::set < alphabet::RankedSymbol > { subtreeWildcard }, std::set < alphabet::RankedSymbol > { } ), std::make_tuple ( subtreeWildcard ) ), pattern ( nullptr ) { pattern.computeMinimalAlphabet ( accessComponent < GeneralAlphabet > ( ).get ( ) ); setTree ( std::move ( pattern ) ); } -RankedNonlinearPattern::RankedNonlinearPattern ( const RankedNonlinearPattern & other ) : std::Components < RankedNonlinearPattern, alphabet::RankedSymbol, std::tuple < GeneralAlphabet, NonlinearAlphabet >, std::tuple < SubtreeWildcard > > ( std::make_tuple ( other.getAlphabet ( ), other.getNonlinearVariables ( ) ), std::make_tuple ( other.getSubtreeWildcard ( ) ) ), pattern ( other.pattern->clone ( ) ) { +RankedNonlinearPattern::RankedNonlinearPattern ( const RankedNonlinearPattern & other ) : std::Components < RankedNonlinearPattern, alphabet::RankedSymbol, std::tuple < GeneralAlphabet, NonlinearAlphabet >, std::tuple < SubtreeWildcard > > ( std::make_tuple ( other.getAlphabet ( ), other.getNonlinearVariables ( ) ), std::make_tuple ( other.getSubtreeWildcard ( ) ) ), pattern ( other.pattern ) { this->pattern->attachAlphabet ( & ( this->getAlphabet() ) ); } -RankedNonlinearPattern::RankedNonlinearPattern ( RankedNonlinearPattern && other ) noexcept : std::Components < RankedNonlinearPattern, alphabet::RankedSymbol, std::tuple < GeneralAlphabet, NonlinearAlphabet >, std::tuple < SubtreeWildcard > > ( std::make_tuple ( std::move ( other.accessComponent < GeneralAlphabet > ( ).get ( ) ), std::move ( other.accessComponent < NonlinearAlphabet > ( ).get ( ) ) ), std::make_tuple ( std::move ( other.accessElement < SubtreeWildcard > ( ).get ( ) ) ) ), pattern ( other.pattern ) { +RankedNonlinearPattern::RankedNonlinearPattern ( RankedNonlinearPattern && other ) noexcept : std::Components < RankedNonlinearPattern, alphabet::RankedSymbol, std::tuple < GeneralAlphabet, NonlinearAlphabet >, std::tuple < SubtreeWildcard > > ( std::make_tuple ( std::move ( other.accessComponent < GeneralAlphabet > ( ).get ( ) ), std::move ( other.accessComponent < NonlinearAlphabet > ( ).get ( ) ) ), std::make_tuple ( std::move ( other.accessElement < SubtreeWildcard > ( ).get ( ) ) ) ), pattern ( std::move ( other.pattern ) ) { this->pattern->attachAlphabet ( & ( this->getAlphabet() ) ); - other.pattern = NULL; } RankedTreeBase * RankedNonlinearPattern::clone ( ) const { @@ -75,7 +74,6 @@ RankedNonlinearPattern & RankedNonlinearPattern::operator =( RankedNonlinearPatt } RankedNonlinearPattern::~RankedNonlinearPattern ( ) noexcept { - delete pattern; } const RankedNode & RankedNonlinearPattern::getRoot ( ) const { @@ -87,13 +85,10 @@ RankedNode & RankedNonlinearPattern::getRoot ( ) { } void RankedNonlinearPattern::setTree ( RankedNode pattern ) { - delete this->pattern; - this->pattern = std::move ( pattern ).plunder ( ); + this->pattern = std::make_smart < RankedNode > ( std::move ( pattern ) ); - if ( !this->pattern->attachAlphabet ( & ( this->getAlphabet ( ) ) ) ) { - delete this->pattern; + if ( !this->pattern->attachAlphabet ( & ( this->getAlphabet ( ) ) ) ) throw TreeException ( "Input symbols not in the alphabet." ); - } } void RankedNonlinearPattern::operator >>( std::ostream & out ) const { @@ -126,10 +121,9 @@ RankedNonlinearPattern RankedNonlinearPattern::parse ( std::deque < sax::Token > alphabet::RankedSymbol subtreeWildcardSymbol = TreeFromXMLParser::parseSubtreeWildcardRankedSymbol ( input ); std::set < alphabet::RankedSymbol > nonlinearVariables = TreeFromXMLParser::parseRankedNonlinearVariables ( input ); std::set < alphabet::RankedSymbol > rankedAlphabet = TreeFromXMLParser::parseRankedAlphabet ( input ); - RankedNode * root = alib::xmlApi < RankedNode * >::parse ( input ); - RankedNonlinearPattern tree ( std::move ( subtreeWildcardSymbol ), std::move ( nonlinearVariables ), std::move ( rankedAlphabet ), std::move ( * root ) ); + RankedNode root = alib::xmlApi < RankedNode >::parse ( input ); + RankedNonlinearPattern tree ( std::move ( subtreeWildcardSymbol ), std::move ( nonlinearVariables ), std::move ( rankedAlphabet ), std::move ( root ) ); - delete root; sax::FromXMLParserHelper::popToken ( input, sax::Token::TokenType::END_ELEMENT, RankedNonlinearPattern::XML_TAG_NAME ); return tree; } @@ -139,7 +133,7 @@ void RankedNonlinearPattern::compose ( std::deque < sax::Token > & out ) const { TreeToXMLComposer::composeSubtreeWildcard ( out, getSubtreeWildcard() ); TreeToXMLComposer::composeNonlinearVariables ( out, getNonlinearVariables() ); TreeToXMLComposer::composeAlphabet ( out, getAlphabet() ); - alib::xmlApi < RankedNode * >::compose ( out, pattern ); + alib::xmlApi < RankedNode >::compose ( out, getRoot() ); out.emplace_back ( RankedNonlinearPattern::XML_TAG_NAME, sax::Token::TokenType::END_ELEMENT ); } diff --git a/alib2data/src/tree/ranked/RankedNonlinearPattern.h b/alib2data/src/tree/ranked/RankedNonlinearPattern.h index 3835b1b291..3f2410327c 100644 --- a/alib2data/src/tree/ranked/RankedNonlinearPattern.h +++ b/alib2data/src/tree/ranked/RankedNonlinearPattern.h @@ -29,7 +29,7 @@ class NonlinearAlphabet; */ class RankedNonlinearPattern : public RankedTreeBase, public std::Components < RankedNonlinearPattern, alphabet::RankedSymbol, std::tuple < GeneralAlphabet, NonlinearAlphabet >, std::tuple < SubtreeWildcard > > { protected: - RankedNode * pattern; + std::smart_ptr < RankedNode > pattern; public: /** diff --git a/alib2data/src/tree/ranked/RankedPattern.cpp b/alib2data/src/tree/ranked/RankedPattern.cpp index 9cc23a6c63..6e997bf164 100644 --- a/alib2data/src/tree/ranked/RankedPattern.cpp +++ b/alib2data/src/tree/ranked/RankedPattern.cpp @@ -25,28 +25,27 @@ namespace tree { -RankedPattern::RankedPattern ( const UnrankedPattern & other ) : std::Components < RankedPattern, alphabet::RankedSymbol, std::tuple < GeneralAlphabet >, std::tuple < SubtreeWildcard > > ( std::make_tuple ( std::set < alphabet::RankedSymbol > { alphabet::RankedSymbol ( other.getSubtreeWildcard ( ), 0 ) } ), std::make_tuple ( alphabet::RankedSymbol ( other.getSubtreeWildcard ( ), 0 ) ) ), pattern ( other.getRoot ( ).cloneAsRanked ( ) ) { +RankedPattern::RankedPattern ( const UnrankedPattern & other ) : std::Components < RankedPattern, alphabet::RankedSymbol, std::tuple < GeneralAlphabet >, std::tuple < SubtreeWildcard > > ( std::make_tuple ( std::set < alphabet::RankedSymbol > { alphabet::RankedSymbol ( other.getSubtreeWildcard ( ), 0 ) } ), std::make_tuple ( alphabet::RankedSymbol ( other.getSubtreeWildcard ( ), 0 ) ) ), pattern ( std::make_smart < RankedNode > ( other.getRoot ( ).asRanked ( ) ) ) { pattern->computeMinimalAlphabet ( accessComponent < GeneralAlphabet > ( ).get ( ) ); this->pattern->attachAlphabet ( & ( this->getAlphabet ( ) ) ); } -RankedPattern::RankedPattern ( alphabet::RankedSymbol subtreeWildcard, std::set < alphabet::RankedSymbol > alphabet, RankedNode pattern ) : std::Components < RankedPattern, alphabet::RankedSymbol, std::tuple < GeneralAlphabet >, std::tuple < SubtreeWildcard > > ( std::make_tuple ( std::move ( alphabet ) ), std::make_tuple ( std::move ( subtreeWildcard ) ) ), pattern ( NULL ) { +RankedPattern::RankedPattern ( alphabet::RankedSymbol subtreeWildcard, std::set < alphabet::RankedSymbol > alphabet, RankedNode pattern ) : std::Components < RankedPattern, alphabet::RankedSymbol, std::tuple < GeneralAlphabet >, std::tuple < SubtreeWildcard > > ( std::make_tuple ( std::move ( alphabet ) ), std::make_tuple ( std::move ( subtreeWildcard ) ) ), pattern ( nullptr ) { setTree ( std::move ( pattern ) ); } -RankedPattern::RankedPattern ( alphabet::RankedSymbol subtreeWildcard, RankedNode pattern ) : std::Components < RankedPattern, alphabet::RankedSymbol, std::tuple < GeneralAlphabet >, std::tuple < SubtreeWildcard > > ( std::make_tuple ( std::set < alphabet::RankedSymbol > { subtreeWildcard } ), std::make_tuple ( subtreeWildcard ) ), pattern ( NULL ) { +RankedPattern::RankedPattern ( alphabet::RankedSymbol subtreeWildcard, RankedNode pattern ) : std::Components < RankedPattern, alphabet::RankedSymbol, std::tuple < GeneralAlphabet >, std::tuple < SubtreeWildcard > > ( std::make_tuple ( std::set < alphabet::RankedSymbol > { subtreeWildcard } ), std::make_tuple ( subtreeWildcard ) ), pattern ( nullptr ) { pattern.computeMinimalAlphabet ( accessComponent < GeneralAlphabet > ( ).get ( ) ); setTree ( std::move ( pattern ) ); } -RankedPattern::RankedPattern ( const RankedPattern & other ) : std::Components < RankedPattern, alphabet::RankedSymbol, std::tuple < GeneralAlphabet >, std::tuple < SubtreeWildcard > > ( std::make_tuple ( other.getAlphabet ( ) ), std::make_tuple ( other.getSubtreeWildcard ( ) ) ), pattern ( other.pattern->clone ( ) ) { +RankedPattern::RankedPattern ( const RankedPattern & other ) : std::Components < RankedPattern, alphabet::RankedSymbol, std::tuple < GeneralAlphabet >, std::tuple < SubtreeWildcard > > ( std::make_tuple ( other.getAlphabet ( ) ), std::make_tuple ( other.getSubtreeWildcard ( ) ) ), pattern ( other.pattern ) { this->pattern->attachAlphabet ( & ( this->getAlphabet() ) ); } -RankedPattern::RankedPattern ( RankedPattern && other ) noexcept : std::Components < RankedPattern, alphabet::RankedSymbol, std::tuple < GeneralAlphabet >, std::tuple < SubtreeWildcard > > ( std::make_tuple ( std::move ( other.accessComponent < GeneralAlphabet > ( ).get ( ) ) ), std::make_tuple ( std::move ( other.accessElement < SubtreeWildcard > ( ).get ( ) ) ) ), pattern ( other.pattern ) { +RankedPattern::RankedPattern ( RankedPattern && other ) noexcept : std::Components < RankedPattern, alphabet::RankedSymbol, std::tuple < GeneralAlphabet >, std::tuple < SubtreeWildcard > > ( std::make_tuple ( std::move ( other.accessComponent < GeneralAlphabet > ( ).get ( ) ) ), std::make_tuple ( std::move ( other.accessElement < SubtreeWildcard > ( ).get ( ) ) ) ), pattern ( std::move ( other.pattern ) ) { this->pattern->attachAlphabet ( & ( this->getAlphabet() ) ); - other.pattern = NULL; } RankedTreeBase * RankedPattern::clone ( ) const { @@ -75,7 +74,6 @@ RankedPattern & RankedPattern::operator =( RankedPattern && other ) noexcept { } RankedPattern::~RankedPattern ( ) noexcept { - delete pattern; } const RankedNode & RankedPattern::getRoot ( ) const { @@ -87,13 +85,10 @@ RankedNode & RankedPattern::getRoot ( ) { } void RankedPattern::setTree ( RankedNode pattern ) { - delete this->pattern; - this->pattern = std::move ( pattern ).plunder ( ); + this->pattern = std::make_smart < RankedNode > ( std::move ( pattern ) ); - if ( !this->pattern->attachAlphabet ( & ( this->getAlphabet ( ) ) ) ) { - delete this->pattern; + if ( !this->pattern->attachAlphabet ( & ( this->getAlphabet ( ) ) ) ) throw TreeException ( "Input symbols not in the alphabet." ); - } } void RankedPattern::operator >>( std::ostream & out ) const { @@ -125,10 +120,9 @@ RankedPattern RankedPattern::parse ( std::deque < sax::Token >::iterator & input sax::FromXMLParserHelper::popToken ( input, sax::Token::TokenType::START_ELEMENT, RankedPattern::XML_TAG_NAME ); alphabet::RankedSymbol subtreeWildcardSymbol = TreeFromXMLParser::parseSubtreeWildcardRankedSymbol ( input ); std::set < alphabet::RankedSymbol > rankedAlphabet = TreeFromXMLParser::parseRankedAlphabet ( input ); - RankedNode * root = alib::xmlApi < RankedNode * >::parse ( input ); - RankedPattern tree ( std::move ( subtreeWildcardSymbol ), std::move ( rankedAlphabet ), std::move ( * root ) ); + RankedNode root = alib::xmlApi < RankedNode >::parse ( input ); + RankedPattern tree ( std::move ( subtreeWildcardSymbol ), std::move ( rankedAlphabet ), std::move ( root ) ); - delete root; sax::FromXMLParserHelper::popToken ( input, sax::Token::TokenType::END_ELEMENT, RankedPattern::XML_TAG_NAME ); return tree; } @@ -137,7 +131,7 @@ void RankedPattern::compose ( std::deque < sax::Token > & out ) const { out.emplace_back ( RankedPattern::XML_TAG_NAME, sax::Token::TokenType::START_ELEMENT ); TreeToXMLComposer::composeSubtreeWildcard ( out, getSubtreeWildcard() ); TreeToXMLComposer::composeAlphabet ( out, getAlphabet() ); - alib::xmlApi < RankedNode * >::compose ( out, pattern ); + alib::xmlApi < RankedNode >::compose ( out, getRoot() ); out.emplace_back ( RankedPattern::XML_TAG_NAME, sax::Token::TokenType::END_ELEMENT ); } diff --git a/alib2data/src/tree/ranked/RankedPattern.h b/alib2data/src/tree/ranked/RankedPattern.h index ff5df054f1..931dc8abbe 100644 --- a/alib2data/src/tree/ranked/RankedPattern.h +++ b/alib2data/src/tree/ranked/RankedPattern.h @@ -28,7 +28,7 @@ class SubtreeWildcard; */ class RankedPattern : public RankedTreeBase, public std::Components < RankedPattern, alphabet::RankedSymbol, std::tuple < GeneralAlphabet >, std::tuple < SubtreeWildcard > > { protected: - RankedNode * pattern; + std::smart_ptr < RankedNode > pattern; public: /** diff --git a/alib2data/src/tree/ranked/RankedTree.cpp b/alib2data/src/tree/ranked/RankedTree.cpp index cc010d3183..309f645b32 100644 --- a/alib2data/src/tree/ranked/RankedTree.cpp +++ b/alib2data/src/tree/ranked/RankedTree.cpp @@ -25,28 +25,27 @@ namespace tree { -RankedTree::RankedTree ( const UnrankedTree & other ) : std::Components < RankedTree, alphabet::RankedSymbol, std::tuple < GeneralAlphabet >, std::tuple < > > ( std::make_tuple ( std::set < alphabet::RankedSymbol > { } ), std::tuple < > ( ) ), tree ( other.getRoot ( ).cloneAsRanked ( ) ) { +RankedTree::RankedTree ( const UnrankedTree & other ) : std::Components < RankedTree, alphabet::RankedSymbol, std::tuple < GeneralAlphabet >, std::tuple < > > ( std::make_tuple ( std::set < alphabet::RankedSymbol > { } ), std::tuple < > ( ) ), tree ( std::make_smart < RankedNode > ( other.getRoot ( ).asRanked ( ) ) ) { tree->computeMinimalAlphabet ( accessComponent < GeneralAlphabet > ( ).get ( ) ); this->tree->attachAlphabet ( & ( this->getAlphabet ( ) ) ); } -RankedTree::RankedTree ( std::set < alphabet::RankedSymbol > alphabet, RankedNode tree ) : std::Components < RankedTree, alphabet::RankedSymbol, std::tuple < GeneralAlphabet >, std::tuple < > > ( std::make_tuple ( std::move ( alphabet ) ), std::tuple < > ( ) ), tree ( NULL ) { +RankedTree::RankedTree ( std::set < alphabet::RankedSymbol > alphabet, RankedNode tree ) : std::Components < RankedTree, alphabet::RankedSymbol, std::tuple < GeneralAlphabet >, std::tuple < > > ( std::make_tuple ( std::move ( alphabet ) ), std::tuple < > ( ) ), tree ( nullptr ) { setTree ( std::move ( tree ) ); } -RankedTree::RankedTree ( RankedNode tree ) : std::Components < RankedTree, alphabet::RankedSymbol, std::tuple < GeneralAlphabet >, std::tuple < > > ( std::make_tuple ( std::set < alphabet::RankedSymbol > { } ), std::tuple < > ( ) ), tree ( NULL ) { +RankedTree::RankedTree ( RankedNode tree ) : std::Components < RankedTree, alphabet::RankedSymbol, std::tuple < GeneralAlphabet >, std::tuple < > > ( std::make_tuple ( std::set < alphabet::RankedSymbol > { } ), std::tuple < > ( ) ), tree ( nullptr ) { tree.computeMinimalAlphabet ( accessComponent < GeneralAlphabet > ( ).get ( ) ); setTree ( std::move ( tree ) ); } -RankedTree::RankedTree ( const RankedTree & other ) : std::Components < RankedTree, alphabet::RankedSymbol, std::tuple < GeneralAlphabet >, std::tuple < > > ( std::make_tuple ( other.getAlphabet ( ) ), std::tuple < > ( ) ), tree ( other.tree->clone ( ) ) { +RankedTree::RankedTree ( const RankedTree & other ) : std::Components < RankedTree, alphabet::RankedSymbol, std::tuple < GeneralAlphabet >, std::tuple < > > ( std::make_tuple ( other.getAlphabet ( ) ), std::tuple < > ( ) ), tree ( other.tree ) { this->tree->attachAlphabet ( & ( this->getAlphabet ( ) ) ); } -RankedTree::RankedTree ( RankedTree && other ) noexcept : std::Components < RankedTree, alphabet::RankedSymbol, std::tuple < GeneralAlphabet >, std::tuple < > > ( std::make_tuple ( std::move ( other.accessComponent < GeneralAlphabet > ( ).get ( ) ) ), std::tuple < > ( ) ), tree ( other.tree ) { +RankedTree::RankedTree ( RankedTree && other ) noexcept : std::Components < RankedTree, alphabet::RankedSymbol, std::tuple < GeneralAlphabet >, std::tuple < > > ( std::make_tuple ( std::move ( other.accessComponent < GeneralAlphabet > ( ).get ( ) ) ), std::tuple < > ( ) ), tree ( std::move ( other.tree ) ) { this->tree->attachAlphabet ( & ( this->getAlphabet ( ) ) ); - other.tree = NULL; } RankedTreeBase * RankedTree::clone ( ) const { @@ -74,7 +73,6 @@ RankedTree & RankedTree::operator =( RankedTree && other ) noexcept { } RankedTree::~RankedTree ( ) noexcept { - delete tree; } const RankedNode & RankedTree::getRoot ( ) const { @@ -86,13 +84,10 @@ RankedNode & RankedTree::getRoot ( ) { } void RankedTree::setTree ( RankedNode tree ) { - delete this->tree; - this->tree = std::move ( tree ).plunder ( ); + this->tree = std::make_smart < RankedNode > ( std::move ( tree ) ); - if ( !this->tree->attachAlphabet ( & ( this->getAlphabet ( ) ) ) ) { - delete this->tree; + if ( !this->tree->attachAlphabet ( & ( this->getAlphabet ( ) ) ) ) throw TreeException ( "Input symbols not in the alphabet." ); - } } void RankedTree::operator >>( std::ostream & out ) const { @@ -123,10 +118,9 @@ const std::string RankedTree::XML_TAG_NAME = "RankedTree"; RankedTree RankedTree::parse ( std::deque < sax::Token >::iterator & input ) { sax::FromXMLParserHelper::popToken ( input, sax::Token::TokenType::START_ELEMENT, RankedTree::XML_TAG_NAME ); std::set < alphabet::RankedSymbol > rankedAlphabet = TreeFromXMLParser::parseRankedAlphabet ( input ); - RankedNode * root = alib::xmlApi < RankedNode * >::parse ( input ); - RankedTree tree ( std::move ( rankedAlphabet ), std::move ( * root ) ); + RankedNode root = alib::xmlApi < RankedNode >::parse ( input ); + RankedTree tree ( std::move ( rankedAlphabet ), std::move ( root ) ); - delete root; sax::FromXMLParserHelper::popToken ( input, sax::Token::TokenType::END_ELEMENT, RankedTree::XML_TAG_NAME ); return tree; } @@ -134,7 +128,7 @@ RankedTree RankedTree::parse ( std::deque < sax::Token >::iterator & input ) { void RankedTree::compose ( std::deque < sax::Token > & out ) const { out.emplace_back ( RankedTree::XML_TAG_NAME, sax::Token::TokenType::START_ELEMENT ); TreeToXMLComposer::composeAlphabet ( out, getAlphabet ( ) ); - alib::xmlApi < RankedNode * >::compose ( out, tree ); + alib::xmlApi < RankedNode >::compose ( out, * tree ); out.emplace_back ( RankedTree::XML_TAG_NAME, sax::Token::TokenType::END_ELEMENT ); } diff --git a/alib2data/src/tree/ranked/RankedTree.h b/alib2data/src/tree/ranked/RankedTree.h index 9c3244b5b7..3727d052fc 100644 --- a/alib2data/src/tree/ranked/RankedTree.h +++ b/alib2data/src/tree/ranked/RankedTree.h @@ -27,7 +27,7 @@ class GeneralAlphabet; */ class RankedTree : public RankedTreeBase, public std::Components < RankedTree, alphabet::RankedSymbol, std::tuple < GeneralAlphabet >, std::tuple < > > { protected: - RankedNode * tree; + std::smart_ptr < RankedNode > tree; public: /** diff --git a/alib2data/src/tree/unranked/UnrankedNode.cpp b/alib2data/src/tree/unranked/UnrankedNode.cpp index 43aee098bd..59c4a3171e 100644 --- a/alib2data/src/tree/unranked/UnrankedNode.cpp +++ b/alib2data/src/tree/unranked/UnrankedNode.cpp @@ -121,13 +121,14 @@ UnrankedNode * UnrankedNode::plunder ( ) && { return new UnrankedNode ( std::move ( * this ) ); } -RankedNode * UnrankedNode::cloneAsRanked ( ) const { - std::vector < RankedNode * > clonedChildren; +RankedNode UnrankedNode::asRanked ( ) const { + std::vector < std::smart_ptr < RankedNode > > rankedChildren; for ( const UnrankedNode * child : this->children ) - clonedChildren.push_back ( child->cloneAsRanked ( ) ); + rankedChildren.emplace_back ( std::make_smart < RankedNode > ( child->asRanked ( ) ) ); - return new RankedNode ( alphabet::RankedSymbol ( this->symbol, primitive::Unsigned ( clonedChildren.size ( ) ) ), std::move ( clonedChildren ) ); + primitive::Unsigned rank( rankedChildren.size ( ) ); + return RankedNode ( alphabet::RankedSymbol ( this->symbol, std::move ( rank ) ), std::move ( rankedChildren ) ); } int UnrankedNode::compare ( const UnrankedNode & other ) const { diff --git a/alib2data/src/tree/unranked/UnrankedNode.h b/alib2data/src/tree/unranked/UnrankedNode.h index e38ec80a02..3e1653943f 100644 --- a/alib2data/src/tree/unranked/UnrankedNode.h +++ b/alib2data/src/tree/unranked/UnrankedNode.h @@ -65,7 +65,7 @@ public: /** * @copydoc UnrankedNode::cloneAsUnranked() const */ - RankedNode * cloneAsRanked ( ) const; + RankedNode asRanked ( ) const; /** * @copydoc UnrankedNode::testSymbol() const diff --git a/alib2data/src/tree/unranked/UnrankedPattern.cpp b/alib2data/src/tree/unranked/UnrankedPattern.cpp index 6c16330fd0..9d2dd97b27 100644 --- a/alib2data/src/tree/unranked/UnrankedPattern.cpp +++ b/alib2data/src/tree/unranked/UnrankedPattern.cpp @@ -25,7 +25,7 @@ namespace tree { -UnrankedPattern::UnrankedPattern ( const RankedPattern & other ) : std::Components < UnrankedPattern, alphabet::Symbol, std::tuple < GeneralAlphabet >, std::tuple < SubtreeWildcard > > ( std::make_tuple ( std::set < alphabet::Symbol > { other.getSubtreeWildcard ( ).getSymbol ( ) } ), std::make_tuple ( other.getSubtreeWildcard ( ).getSymbol ( ) ) ), pattern ( other.getRoot ( ).cloneAsUnranked ( ) ) { +UnrankedPattern::UnrankedPattern ( const RankedPattern & other ) : std::Components < UnrankedPattern, alphabet::Symbol, std::tuple < GeneralAlphabet >, std::tuple < SubtreeWildcard > > ( std::make_tuple ( std::set < alphabet::Symbol > { other.getSubtreeWildcard ( ).getSymbol ( ) } ), std::make_tuple ( other.getSubtreeWildcard ( ).getSymbol ( ) ) ), pattern ( new UnrankedNode ( other.getRoot ( ).asUnranked ( ) ) ) { std::transform ( other.getAlphabet ( ).begin ( ), other.getAlphabet ( ).end ( ), std::inserter ( accessComponent < GeneralAlphabet > ( ).get ( ), getAlphabet ( ).begin ( ) ), [] ( const alphabet::RankedSymbol & symbol ) { return symbol.getSymbol ( ); } ); diff --git a/alib2data/src/tree/unranked/UnrankedTree.cpp b/alib2data/src/tree/unranked/UnrankedTree.cpp index 7febf5f9ad..8c542527d5 100644 --- a/alib2data/src/tree/unranked/UnrankedTree.cpp +++ b/alib2data/src/tree/unranked/UnrankedTree.cpp @@ -25,7 +25,7 @@ namespace tree { -UnrankedTree::UnrankedTree ( const RankedTree & other ) : std::Components < UnrankedTree, alphabet::Symbol, std::tuple < GeneralAlphabet >, std::tuple < > > ( std::make_tuple ( std::set < alphabet::Symbol > { } ), std::tuple < > ( ) ), tree ( other.getRoot ( ).cloneAsUnranked ( ) ) { +UnrankedTree::UnrankedTree ( const RankedTree & other ) : std::Components < UnrankedTree, alphabet::Symbol, std::tuple < GeneralAlphabet >, std::tuple < > > ( std::make_tuple ( std::set < alphabet::Symbol > { } ), std::tuple < > ( ) ), tree ( new UnrankedNode ( other.getRoot ( ).asUnranked ( ) ) ) { std::transform ( other.getAlphabet ( ).begin ( ), other.getAlphabet ( ).end ( ), std::inserter ( accessComponent < GeneralAlphabet > ( ).get ( ), getAlphabet ( ).begin ( ) ), [] ( const alphabet::RankedSymbol & symbol ) { return symbol.getSymbol ( ); } ); diff --git a/alib2data/test-src/tree/PatternTest.cpp b/alib2data/test-src/tree/PatternTest.cpp index c4a62de7ef..d1140fa74b 100644 --- a/alib2data/test-src/tree/PatternTest.cpp +++ b/alib2data/test-src/tree/PatternTest.cpp @@ -39,10 +39,10 @@ void PatternTest::testRankedPatternParser() { const alphabet::RankedSymbol S (alphabet::Symbol { alphabet::SubtreeWildcardSymbol {} }, 0); const std::set<alphabet::RankedSymbol> alphabet {a, b, c, S}; - tree::RankedNode * node3 = new tree::RankedNode(c, {}); - tree::RankedNode * node4 = new tree::RankedNode(S, {}); - tree::RankedNode * node2 = new tree::RankedNode(b, {node3}); - tree::RankedNode node1(a, {node2, node4}); + tree::RankedNode node3(c, {}); + tree::RankedNode node4(S, {}); + tree::RankedNode node2(b, {std::make_smart<tree::RankedNode>(std::move(node3))}); + tree::RankedNode node1(a, {std::make_smart<tree::RankedNode>(std::move(node2)), std::make_smart<tree::RankedNode>(std::move(node4))}); tree::RankedPattern tree(S, alphabet, std::move(node1)); @@ -119,10 +119,10 @@ void PatternTest::testUnrankedPatternParser() { const alphabet::RankedSymbol rS { alphabet::Symbol { alphabet::SubtreeWildcardSymbol {} }, 0}; const std::set<alphabet::RankedSymbol> ralphabet {ra, rb, rc, rS}; - tree::RankedNode * rnode3 = new tree::RankedNode(rc, {}); - tree::RankedNode * rnode4 = new tree::RankedNode(rS, {}); - tree::RankedNode * rnode2 = new tree::RankedNode(rb, {rnode3}); - tree::RankedNode rnode1(ra, {rnode2, rnode4}); + tree::RankedNode rnode3(rc, {}); + tree::RankedNode rnode4(rS, {}); + tree::RankedNode rnode2(rb, {std::make_smart<tree::RankedNode>(std::move(rnode3))}); + tree::RankedNode rnode1(ra, {std::make_smart<tree::RankedNode>(std::move(rnode2)), std::make_smart<tree::RankedNode>(std::move(rnode4))}); tree::RankedPattern tree2(rS, ralphabet, std::move(rnode1)); @@ -139,10 +139,10 @@ void PatternTest::testPrefixRankedPatternParser() { const alphabet::RankedSymbol S (alphabet::Symbol { alphabet::SubtreeWildcardSymbol {} }, 0); const std::set<alphabet::RankedSymbol> alphabet {a, b, c, S}; - tree::RankedNode * node3 = new tree::RankedNode(c, {}); - tree::RankedNode * node4 = new tree::RankedNode(S, {}); - tree::RankedNode * node2 = new tree::RankedNode(b, {node3}); - tree::RankedNode node1(a, {node2, node4}); + tree::RankedNode node3(c, {}); + tree::RankedNode node4(S, {}); + tree::RankedNode node2(b, {std::make_smart<tree::RankedNode>(std::move(node3))}); + tree::RankedNode node1(a, {std::make_smart<tree::RankedNode>(std::move(node2)), std::make_smart<tree::RankedNode>(std::move(node4))}); tree::RankedPattern pattern(S, alphabet, std::move(node1)); tree::PrefixRankedPattern pattern2(pattern); @@ -173,10 +173,10 @@ void PatternTest::testPrefixRankedBarPatternParser() { const alphabet::RankedSymbol S (alphabet::Symbol { alphabet::SubtreeWildcardSymbol {} }, 0); const std::set<alphabet::RankedSymbol> alphabet {a, b, c, S}; - tree::RankedNode * node3 = new tree::RankedNode(c, {}); - tree::RankedNode * node4 = new tree::RankedNode(S, {}); - tree::RankedNode * node2 = new tree::RankedNode(b, {node3}); - tree::RankedNode node1(a, {node2, node4}); + tree::RankedNode node3(c, {}); + tree::RankedNode node4(S, {}); + tree::RankedNode node2(b, {std::make_smart<tree::RankedNode>(std::move(node3))}); + tree::RankedNode node1(a, {std::make_smart<tree::RankedNode>(std::move(node2)), std::make_smart<tree::RankedNode>(std::move(node4))}); tree::RankedPattern pattern(S, alphabet, std::move(node1)); tree::PrefixRankedBarPattern pattern2(bar, variablesBar, pattern); diff --git a/alib2data/test-src/tree/TreeTest.cpp b/alib2data/test-src/tree/TreeTest.cpp index 60a85c03d1..c50c549532 100644 --- a/alib2data/test-src/tree/TreeTest.cpp +++ b/alib2data/test-src/tree/TreeTest.cpp @@ -31,15 +31,15 @@ void TreeTest::setUp() { void TreeTest::tearDown() { } -tree::RankedNode * prefixToNode(const std::string & s, int & pos) { +tree::RankedNode prefixToNode(const std::string & s, int & pos) { char c = s[pos]; int r = s[pos+1] - '0'; pos += 2; - std::vector<tree::RankedNode *> children (r); + std::vector<std::smart_ptr<tree::RankedNode>> children; for (int i = 0; i < r; i++) { - children[i] = prefixToNode(s, pos); + children.emplace_back(std::make_smart<tree::RankedNode>(prefixToNode(s, pos))); } - return new tree::RankedNode(alphabet::RankedSymbol(c, r), children); + return tree::RankedNode(alphabet::RankedSymbol(c, r), std::move(children)); } void TreeTest::testRankedTreeParser() { @@ -60,10 +60,10 @@ void TreeTest::testRankedTreeParser() { const std::set<alphabet::RankedSymbol> alphabet {a, b, c}; - tree::RankedNode * node3 = new tree::RankedNode(c, {}); - tree::RankedNode * node4 = new tree::RankedNode(c, {}); - tree::RankedNode * node2 = new tree::RankedNode(b, {node3}); - tree::RankedNode node1(a, {node2, node4}); + tree::RankedNode node3(c, {}); + tree::RankedNode node4(c, {}); + tree::RankedNode node2(b, {std::make_smart<tree::RankedNode>(std::move(node3))}); + tree::RankedNode node1(a, {std::make_smart<tree::RankedNode>(std::move(node2)), std::make_smart<tree::RankedNode>(std::move(node4))}); tree::RankedTree tree(alphabet, std::move(node1)); @@ -119,15 +119,15 @@ void TreeTest::testRankedTreeCompare() { const std::set<alphabet::RankedSymbol> alphabet {a, b, c}; - tree::RankedNode * node3 = new tree::RankedNode(c, {}); - tree::RankedNode * node4 = new tree::RankedNode(c, {}); - tree::RankedNode * node2 = new tree::RankedNode(b, {node3}); - tree::RankedNode node1(a, {node2, node4}); + tree::RankedNode node3(c, {}); + tree::RankedNode node4(c, {}); + tree::RankedNode node2(b, {std::make_smart<tree::RankedNode>(std::move(node3))}); + tree::RankedNode node1(a, {std::make_smart<tree::RankedNode>(std::move(node2)), std::make_smart<tree::RankedNode>(std::move(node4))}); - tree::RankedNode * node6 = new tree::RankedNode(c, {}); - tree::RankedNode * node8 = new tree::RankedNode(c, {}); - tree::RankedNode * node7 = new tree::RankedNode(b, {node8}); - tree::RankedNode node5(a, {node6, node7}); + tree::RankedNode node6(c, {}); + tree::RankedNode node8(c, {}); + tree::RankedNode node7(b, {std::make_smart<tree::RankedNode>(std::move(node8))}); + tree::RankedNode node5(a, {std::make_smart<tree::RankedNode>(std::move(node6)), std::make_smart<tree::RankedNode>(std::move(node7))}); tree::RankedTree tree1(alphabet, std::move(node1)); tree::RankedTree tree2(alphabet, std::move(node5)); @@ -147,17 +147,17 @@ void TreeTest::testRankedTreeSymbolValidityCheck() { const std::set<alphabet::RankedSymbol> alphabet {a, b, c, e}; - tree::RankedNode * node3 = new tree::RankedNode(d, {}); - tree::RankedNode * node4 = new tree::RankedNode(c, {}); - tree::RankedNode * node2 = new tree::RankedNode(b, {node3}); - tree::RankedNode node1(a, {node2, node4}); + tree::RankedNode node3(d, {}); + tree::RankedNode node4(c, {}); + tree::RankedNode node2(b, {std::make_smart<tree::RankedNode>(std::move(node3))}); + tree::RankedNode node1(a, {std::make_smart<tree::RankedNode>(std::move(node2)), std::make_smart<tree::RankedNode>(std::move(node4))}); CPPUNIT_ASSERT_THROW(tree::RankedTree(alphabet, std::move(node1)), tree::TreeException); - tree::RankedNode * node6 = new tree::RankedNode(e, {}); - tree::RankedNode * node8 = new tree::RankedNode(c, {}); - tree::RankedNode * node7 = new tree::RankedNode(b, {node8}); - tree::RankedNode node5(a, {node6, node7}); + tree::RankedNode node6(e, {}); + tree::RankedNode node8(c, {}); + tree::RankedNode node7(b, {std::make_smart<tree::RankedNode>(std::move(node8))}); + tree::RankedNode node5(a, {std::make_smart<tree::RankedNode>(std::move(node6)), std::make_smart<tree::RankedNode>(std::move(node7))}); tree::RankedTree * tree = NULL; CPPUNIT_ASSERT_NO_THROW(tree = new tree::RankedTree(alphabet, std::move(node5))); @@ -178,15 +178,15 @@ void TreeTest::testRankedTreeSubtreeSwitch() { const std::set<alphabet::RankedSymbol> alphabet1 {a, b, c, d}; const std::set<alphabet::RankedSymbol> alphabet2 {a, b, c, d}; - tree::RankedNode * node3 = new tree::RankedNode(c, {}); - tree::RankedNode * node4 = new tree::RankedNode(c, {}); - tree::RankedNode * node2 = new tree::RankedNode(b, {node3}); - tree::RankedNode node1(a, {node2, node4}); + tree::RankedNode node3(c, {}); + tree::RankedNode node4(c, {}); + tree::RankedNode node2(b, {std::make_smart<tree::RankedNode>(std::move(node3))}); + tree::RankedNode node1(a, {std::make_smart<tree::RankedNode>(std::move(node2)), std::make_smart<tree::RankedNode>(std::move(node4))}); - tree::RankedNode * node6 = new tree::RankedNode(d, {}); - tree::RankedNode * node8 = new tree::RankedNode(c, {}); - tree::RankedNode * node7 = new tree::RankedNode(b, {node8}); - tree::RankedNode node5(a, {node6, node7}); + tree::RankedNode node6(d, {}); + tree::RankedNode node8(c, {}); + tree::RankedNode node7(b, {std::make_smart<tree::RankedNode>(std::move(node8))}); + tree::RankedNode node5(a, {std::make_smart<tree::RankedNode>(std::move(node6)), std::make_smart<tree::RankedNode>(std::move(node7))}); tree::RankedTree tree1(alphabet1, std::move(node1)); tree::RankedTree tree1Copy (tree1); @@ -388,10 +388,10 @@ void TreeTest::testPrefixRankedBarParser() { const std::set<alphabet::RankedSymbol> alphabet {a, b, c}; - tree::RankedNode * node3 = new tree::RankedNode(c, {}); - tree::RankedNode * node4 = new tree::RankedNode(c, {}); - tree::RankedNode * node2 = new tree::RankedNode(b, {node3}); - tree::RankedNode node1(a, {node2, node4}); + tree::RankedNode node3(c, {}); + tree::RankedNode node4(c, {}); + tree::RankedNode node2(b, {std::make_smart<tree::RankedNode>(std::move(node3))}); + tree::RankedNode node1(a, {std::make_smart<tree::RankedNode>(std::move(node2)), std::make_smart<tree::RankedNode>(std::move(node4))}); tree::RankedTree tree(alphabet, std::move(node1)); tree::PrefixRankedBarTree tree2(bar, tree); diff --git a/alib2str/src/tree/TreeFromStringParser.cpp b/alib2str/src/tree/TreeFromStringParser.cpp index 184718f168..163fd15a6e 100644 --- a/alib2str/src/tree/TreeFromStringParser.cpp +++ b/alib2str/src/tree/TreeFromStringParser.cpp @@ -57,11 +57,11 @@ Tree TreeFromStringParser::parseTree ( std::istream & input, const std::set < FE rank = std::stou ( token.value ); - std::vector < tree::RankedNode * > childs; + std::vector < std::smart_ptr < tree::RankedNode > > childs; bool isPattern = false; for ( unsigned i = 0; i < rank; i++ ) - childs.push_back ( parseRankedContent ( input, isPattern, nonlinearVariables ) ); + childs.emplace_back ( std::make_smart < RankedNode > ( parseRankedContent ( input, isPattern, nonlinearVariables ) ) ); if ( isPattern && nonlinearVariables.size ( ) ) { alphabet::RankedSymbol subtreeWildcard ( alphabet::Symbol ( alphabet::SubtreeWildcardSymbol::SUBTREE_WILDCARD ), 0 ); @@ -114,24 +114,24 @@ Tree TreeFromStringParser::parseTree ( std::istream & input, const std::set < FE } } -tree::RankedNode * TreeFromStringParser::parseRankedContent ( std::istream & input, bool & isPattern, std::set < alphabet::RankedSymbol > & nonlinearVariables ) const { +tree::RankedNode TreeFromStringParser::parseRankedContent ( std::istream & input, bool & isPattern, std::set < alphabet::RankedSymbol > & nonlinearVariables ) const { TreeFromStringLexer::Token token = m_TreeLexer.next ( input ); if ( token.type == TreeFromStringLexer::TokenType::SUBTREE_WILDCARD ) { isPattern = true; alphabet::RankedSymbol subtreeWildcard ( alphabet::Symbol ( alphabet::SubtreeWildcardSymbol::SUBTREE_WILDCARD ), 0 ); - return new RankedNode ( std::move ( subtreeWildcard ), { } ); + return RankedNode ( std::move ( subtreeWildcard ), { } ); } else if ( token.type == TreeFromStringLexer::TokenType::NONLINEAR_VARIABLE ) { isPattern = true; alphabet::RankedSymbol nonlinearVariable ( alphabet::Symbol ( alphabet::NonlinearVariableSymbol ( alphabet::symbolFrom ( token.value ) ) ), 0 ); nonlinearVariables.insert ( nonlinearVariable ); - return new RankedNode ( std::move ( nonlinearVariable ), { } ); + return RankedNode ( std::move ( nonlinearVariable ), { } ); } else { m_TreeLexer.putback ( input, token ); alphabet::Symbol symbol = alib::stringApi < alphabet::Symbol >::parse ( input ); unsigned rank = 0; - std::vector < tree::RankedNode * > childs; + std::vector < std::smart_ptr < tree::RankedNode > > childs; TreeFromStringLexer::Token token = m_TreeLexer.next ( input ); @@ -141,9 +141,9 @@ tree::RankedNode * TreeFromStringParser::parseRankedContent ( std::istream & inp throw exception::CommonException ( "Missing rank" ); for ( unsigned i = 0; i < rank; i++ ) - childs.push_back ( parseRankedContent ( input, isPattern, nonlinearVariables ) ); + childs.emplace_back ( std::make_smart < RankedNode > ( parseRankedContent ( input, isPattern, nonlinearVariables ) ) ); - return new RankedNode ( alphabet::RankedSymbol ( std::move ( symbol ), rank ), std::move ( childs ) ); + return RankedNode ( alphabet::RankedSymbol ( std::move ( symbol ), rank ), std::move ( childs ) ); } } diff --git a/alib2str/src/tree/TreeFromStringParser.h b/alib2str/src/tree/TreeFromStringParser.h index b6782a7dd3..5d17877ee1 100644 --- a/alib2str/src/tree/TreeFromStringParser.h +++ b/alib2str/src/tree/TreeFromStringParser.h @@ -29,7 +29,7 @@ public: TreeFromStringParser ( ) { } private: - tree::RankedNode * parseRankedContent ( std::istream &, bool &, std::set < alphabet::RankedSymbol > & ) const; + tree::RankedNode parseRankedContent ( std::istream &, bool &, std::set < alphabet::RankedSymbol > & ) const; tree::UnrankedNode * parseUnrankedContent ( std::istream &, bool &, std::set < alphabet::Symbol > & ) const; TreeFromStringLexer m_TreeLexer; diff --git a/alib2str/src/tree/TreeToStringComposer.cpp b/alib2str/src/tree/TreeToStringComposer.cpp index 72604aec26..d4fce18151 100644 --- a/alib2str/src/tree/TreeToStringComposer.cpp +++ b/alib2str/src/tree/TreeToStringComposer.cpp @@ -26,7 +26,7 @@ void TreeToStringComposer::compose ( std::ostream & out, const RankedNode & node out << std::utos ( node.getSymbol ( ).getRank ( ).getData ( ) ); - for ( const RankedNode * child : node.getChildren ( ) ) { + for ( const std::smart_ptr < const RankedNode > & child : node.getChildren ( ) ) { out << " "; compose ( out, * child ); } @@ -46,7 +46,7 @@ void TreeToStringComposer::compose ( std::ostream & out, const alphabet::RankedS out << std::utos ( node.getSymbol ( ).getRank ( ).getData ( ) ); - for ( const RankedNode * child : node.getChildren ( ) ) { + for ( const std::smart_ptr < const RankedNode > & child : node.getChildren ( ) ) { out << " "; compose ( out, subtreeWildcard, * child ); } diff --git a/astat2/src/TreeStat.cpp b/astat2/src/TreeStat.cpp index 4cd70eb9ed..69be052ead 100644 --- a/astat2/src/TreeStat.cpp +++ b/astat2/src/TreeStat.cpp @@ -21,7 +21,7 @@ void TreeStat::stat ( const tree::Tree & tree, const TreeSettings & settings ) { unsigned TreeStat::countNodes ( const tree::RankedNode & node ) { unsigned res = 1; - for ( const tree::RankedNode * child : node.getChildren ( ) ) + for ( const std::smart_ptr < const tree::RankedNode > & child : node.getChildren() ) res += countNodes ( * child ); return res; @@ -48,7 +48,7 @@ void TreeStat::stat ( const tree::RankedTree & tree, const TreeSettings & settin switch ( settings.nodes ) { case PrintingOptions::PRINT: - alib::XmlDataFactory::toStdout ( & tree.getRoot ( ) ); + alib::XmlDataFactory::toStdout ( tree.getRoot ( ) ); break; case PrintingOptions::SIZE: @@ -56,7 +56,7 @@ void TreeStat::stat ( const tree::RankedTree & tree, const TreeSettings & settin break; case PrintingOptions::BOTH: - alib::XmlDataFactory::toStdout ( & tree.getRoot ( ) ); + alib::XmlDataFactory::toStdout ( tree.getRoot ( ) ); std::cout << countNodes ( tree.getRoot ( ) ) << std::endl; break; -- GitLab