From 2d69dd86b941ff8c47b4a03d086fa9ac03cf312d Mon Sep 17 00:00:00 2001 From: Jan Travnicek <Jan.Travnicek@fit.cvut.cz> Date: Thu, 27 Aug 2015 17:51:52 +0200 Subject: [PATCH] fix test symbol in trees --- .../src/tree/common/TreeToXMLComposer.cpp | 115 ++++----- alib2data/src/tree/ranked/RankedNode.cpp | 217 +++++++++-------- alib2data/src/tree/unranked/UnrankedNode.cpp | 222 ++++++++++-------- 3 files changed, 299 insertions(+), 255 deletions(-) diff --git a/alib2data/src/tree/common/TreeToXMLComposer.cpp b/alib2data/src/tree/common/TreeToXMLComposer.cpp index c3760cdb8c..6a34cf7e90 100644 --- a/alib2data/src/tree/common/TreeToXMLComposer.cpp +++ b/alib2data/src/tree/common/TreeToXMLComposer.cpp @@ -10,81 +10,84 @@ namespace tree { -void TreeToXMLComposer::composeBar(std::deque<sax::Token>& out, const alphabet::Symbol& symbol) { - out.emplace_back(sax::Token("bar", sax::Token::TokenType::START_ELEMENT)); - alib::xmlApi<alphabet::Symbol>::compose(out, symbol); - out.emplace_back(sax::Token("bar", sax::Token::TokenType::END_ELEMENT)); +void TreeToXMLComposer::composeBar ( std::deque < sax::Token > & out, const alphabet::Symbol & symbol ) { + out.emplace_back ( sax::Token ( "bar", sax::Token::TokenType::START_ELEMENT ) ); + alib::xmlApi < alphabet::Symbol >::compose ( out, symbol ); + out.emplace_back ( sax::Token ( "bar", sax::Token::TokenType::END_ELEMENT ) ); } -void TreeToXMLComposer::composeVariablesBar(std::deque<sax::Token>& out, const alphabet::Symbol& symbol) { - out.emplace_back(sax::Token("variablesBar", sax::Token::TokenType::START_ELEMENT)); - alib::xmlApi<alphabet::Symbol>::compose(out, symbol); - out.emplace_back(sax::Token("variablesBar", sax::Token::TokenType::END_ELEMENT)); +void TreeToXMLComposer::composeVariablesBar ( std::deque < sax::Token > & out, const alphabet::Symbol & symbol ) { + out.emplace_back ( sax::Token ( "variablesBar", sax::Token::TokenType::START_ELEMENT ) ); + alib::xmlApi < alphabet::Symbol >::compose ( out, symbol ); + out.emplace_back ( sax::Token ( "variablesBar", sax::Token::TokenType::END_ELEMENT ) ); } -void TreeToXMLComposer::composeSubtreeWildcard(std::deque<sax::Token>& out, const alphabet::Symbol& symbol) { - out.emplace_back(sax::Token("subtreeWildcard", sax::Token::TokenType::START_ELEMENT)); - alib::xmlApi<alphabet::Symbol>::compose(out, symbol); - out.emplace_back(sax::Token("subtreeWildcard", sax::Token::TokenType::END_ELEMENT)); +void TreeToXMLComposer::composeSubtreeWildcard ( std::deque < sax::Token > & out, const alphabet::Symbol & symbol ) { + out.emplace_back ( sax::Token ( "subtreeWildcard", sax::Token::TokenType::START_ELEMENT ) ); + alib::xmlApi < alphabet::Symbol >::compose ( out, symbol ); + out.emplace_back ( sax::Token ( "subtreeWildcard", sax::Token::TokenType::END_ELEMENT ) ); } -void TreeToXMLComposer::composeSubtreeWildcard(std::deque<sax::Token>& out, const alphabet::RankedSymbol& symbol) { - out.emplace_back(sax::Token("subtreeWildcard", sax::Token::TokenType::START_ELEMENT)); - alib::xmlApi<alphabet::RankedSymbol>::compose(out, symbol); - out.emplace_back(sax::Token("subtreeWildcard", sax::Token::TokenType::END_ELEMENT)); +void TreeToXMLComposer::composeSubtreeWildcard ( std::deque < sax::Token > & out, const alphabet::RankedSymbol & symbol ) { + out.emplace_back ( sax::Token ( "subtreeWildcard", sax::Token::TokenType::START_ELEMENT ) ); + alib::xmlApi < alphabet::RankedSymbol >::compose ( out, symbol ); + out.emplace_back ( sax::Token ( "subtreeWildcard", sax::Token::TokenType::END_ELEMENT ) ); } -void TreeToXMLComposer::composeAlphabet(std::deque<sax::Token>& out, const std::set<alphabet::RankedSymbol>& symbols) { - out.emplace_back(sax::Token("rankedAlphabet", sax::Token::TokenType::START_ELEMENT)); - for (const auto& symbol : symbols) { - alib::xmlApi<alphabet::RankedSymbol>::compose(out, symbol); - } - out.emplace_back(sax::Token("rankedAlphabet", sax::Token::TokenType::END_ELEMENT)); -} +void TreeToXMLComposer::composeAlphabet ( std::deque < sax::Token > & out, const std::set < alphabet::RankedSymbol > & symbols ) { + out.emplace_back ( sax::Token ( "rankedAlphabet", sax::Token::TokenType::START_ELEMENT ) ); + + for ( const auto & symbol : symbols ) + alib::xmlApi < alphabet::RankedSymbol >::compose ( out, symbol ); -void TreeToXMLComposer::composeAlphabet(std::deque<sax::Token>& out, const std::set<alphabet::Symbol>& symbols) { - out.emplace_back(sax::Token("alphabet", sax::Token::TokenType::START_ELEMENT)); - for (const auto& symbol : symbols) { - alib::xmlApi<alphabet::Symbol>::compose(out, symbol); - } - out.emplace_back(sax::Token("alphabet", sax::Token::TokenType::END_ELEMENT)); + out.emplace_back ( sax::Token ( "rankedAlphabet", sax::Token::TokenType::END_ELEMENT ) ); } -void TreeToXMLComposer::composeNode(std::deque<sax::Token>& out, const RankedNode& node) { - out.emplace_back(sax::Token("rankedNode", sax::Token::TokenType::START_ELEMENT)); - alib::xmlApi<alphabet::RankedSymbol>::compose(out, node.getSymbol()); - for(const auto& child : node.getChildren()) { - composeNode(out, *child); - } +void TreeToXMLComposer::composeAlphabet ( std::deque < sax::Token > & out, const std::set < alphabet::Symbol > & symbols ) { + out.emplace_back ( sax::Token ( "alphabet", sax::Token::TokenType::START_ELEMENT ) ); + + for ( const auto & symbol : symbols ) + alib::xmlApi < alphabet::Symbol >::compose ( out, symbol ); - out.emplace_back(sax::Token("rankedNode", sax::Token::TokenType::END_ELEMENT)); + out.emplace_back ( sax::Token ( "alphabet", sax::Token::TokenType::END_ELEMENT ) ); } -void TreeToXMLComposer::composeNode(std::deque<sax::Token>& out, const UnrankedNode& node) { - out.emplace_back(sax::Token("unrankedNode", sax::Token::TokenType::START_ELEMENT)); - alib::xmlApi<alphabet::Symbol>::compose(out, node.getSymbol()); - for(const auto& child : node.getChildren()) { - composeNode(out, *child); - } +void TreeToXMLComposer::composeNode ( std::deque < sax::Token > & out, const RankedNode & node ) { + out.emplace_back ( sax::Token ( "rankedNode", sax::Token::TokenType::START_ELEMENT ) ); + alib::xmlApi < alphabet::RankedSymbol >::compose ( out, node.getSymbol ( ) ); - out.emplace_back(sax::Token("unrankedNode", sax::Token::TokenType::END_ELEMENT)); + for ( const auto & child : node.getChildren ( ) ) + composeNode ( out, * child ); + + out.emplace_back ( sax::Token ( "rankedNode", sax::Token::TokenType::END_ELEMENT ) ); } -void TreeToXMLComposer::composeContent(std::deque<sax::Token>& out, const std::vector<alphabet::Symbol>& content) { - out.emplace_back("content", sax::Token::TokenType::START_ELEMENT); - for(const auto& symbol : content) { - alib::xmlApi<alphabet::Symbol>::compose(out, symbol); - } - out.emplace_back("content", sax::Token::TokenType::END_ELEMENT); +void TreeToXMLComposer::composeNode ( std::deque < sax::Token > & out, const UnrankedNode & node ) { + out.emplace_back ( sax::Token ( "unrankedNode", sax::Token::TokenType::START_ELEMENT ) ); + alib::xmlApi < alphabet::Symbol >::compose ( out, node.getSymbol ( ) ); + + for ( const auto & child : node.getChildren ( ) ) + composeNode ( out, * child ); + + out.emplace_back ( sax::Token ( "unrankedNode", sax::Token::TokenType::END_ELEMENT ) ); } -void TreeToXMLComposer::composeContent(std::deque<sax::Token>& out, const std::vector<alphabet::RankedSymbol>& content) { - out.emplace_back("content", sax::Token::TokenType::START_ELEMENT); - for(const auto& symbol : content) { - alib::xmlApi<alphabet::RankedSymbol>::compose(out, symbol); - } - out.emplace_back("content", sax::Token::TokenType::END_ELEMENT); +void TreeToXMLComposer::composeContent ( std::deque < sax::Token > & out, const std::vector < alphabet::Symbol > & content ) { + out.emplace_back ( "content", sax::Token::TokenType::START_ELEMENT ); + + for ( const auto & symbol : content ) + alib::xmlApi < alphabet::Symbol >::compose ( out, symbol ); + + out.emplace_back ( "content", sax::Token::TokenType::END_ELEMENT ); } -} /* namespace automaton */ +void TreeToXMLComposer::composeContent ( std::deque < sax::Token > & out, const std::vector < alphabet::RankedSymbol > & content ) { + out.emplace_back ( "content", sax::Token::TokenType::START_ELEMENT ); + + for ( const auto & symbol : content ) + alib::xmlApi < alphabet::RankedSymbol >::compose ( out, symbol ); + + out.emplace_back ( "content", sax::Token::TokenType::END_ELEMENT ); +} +} /* namespace tree */ diff --git a/alib2data/src/tree/ranked/RankedNode.cpp b/alib2data/src/tree/ranked/RankedNode.cpp index 30acd5f3e7..23430d7cb2 100644 --- a/alib2data/src/tree/ranked/RankedNode.cpp +++ b/alib2data/src/tree/ranked/RankedNode.cpp @@ -13,190 +13,211 @@ namespace tree { -RankedNode::RankedNode(alphabet::RankedSymbol symbol, std::vector<RankedNode *> children) : symbol(std::move(symbol)), children(std::move(children)), parentTree(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) { +RankedNode::RankedNode ( alphabet::RankedSymbol symbol, std::vector < RankedNode * > children ) : symbol ( std::move ( symbol ) ), children ( std::move ( children ) ), parentTree ( 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 ) element->parent = this; - } - this->attachTree(NULL); + + this->attachTree ( NULL ); this->parent = NULL; } -RankedNode::RankedNode(const RankedNode& other) : symbol(other.symbol), parentTree(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 ), parentTree ( NULL ) { + for ( const auto & element : other.children ) + children.push_back ( element->clone ( ) ); + + for ( auto & element : this->children ) element->parent = this; - } - this->attachTree(NULL); + + this->attachTree ( NULL ); this->parent = NULL; } -RankedNode::RankedNode(RankedNode&& other) noexcept : symbol(std::move(other.symbol)), children(std::move(other.children)), parentTree(NULL) { - other.children.clear(); - for (auto& element : this->children) { +RankedNode::RankedNode ( RankedNode && other ) noexcept : symbol ( std::move ( other.symbol ) ), children ( std::move ( other.children ) ), parentTree ( NULL ) { + other.children.clear ( ); + + for ( auto & element : this->children ) element->parent = this; - } - this->attachTree(NULL); + + this->attachTree ( NULL ); this->parent = NULL; } -RankedNode& RankedNode::operator=(const RankedNode& other) { - if (this == &other) { - return *this; - } +RankedNode & RankedNode::operator =( const RankedNode & other ) { + if ( this == & other ) + return * this; - *this = RankedNode(other); + * this = RankedNode ( other ); - return *this; + return * this; } -RankedNode& RankedNode::operator=(RankedNode&& other) noexcept { - std::swap(this->symbol, other.symbol); - std::swap(this->children, other.children); - std::swap(this->parentTree, other.parentTree); // this->parentTree is stored within other.parentTree and it is reattached on the next line +RankedNode & RankedNode::operator =( RankedNode && other ) noexcept { + std::swap ( this->symbol, other.symbol ); + std::swap ( this->children, other.children ); + std::swap ( this->parentTree, other.parentTree ); // this->parentTree is stored within other.parentTree and it is reattached on the next line - for (auto& element : this->children) { + for ( auto & element : this->children ) element->parent = this; - } - this->attachTree(other.parentTree); - return *this; + this->attachTree ( other.parentTree ); + + return * this; } -RankedNode::~RankedNode() noexcept { - for (auto element : children) { +RankedNode::~RankedNode ( ) noexcept { + for ( auto element : children ) delete element; - } - children.clear(); + + children.clear ( ); } -const std::vector<const RankedNode*> & RankedNode::getChildren() const { - return * reinterpret_cast<const std::vector<const RankedNode*> * > (&children); +const std::vector < const RankedNode * > & RankedNode::getChildren ( ) const { + return * reinterpret_cast < const std::vector < const RankedNode * > * > ( & children ); } -const std::vector<RankedNode*> & RankedNode::getChildren() { +const std::vector < RankedNode * > & RankedNode::getChildren ( ) { return children; } -RankedNode * RankedNode::getParent() { +RankedNode * RankedNode::getParent ( ) { return parent; } -const RankedNode * RankedNode::getParent() const { +const RankedNode * RankedNode::getParent ( ) const { return parent; } -void RankedNode::setSymbol(alphabet::RankedSymbol symbol) { - if(symbol.getRank() != this->symbol.getRank()) throw TreeException("Number of children doesn't match the rank of the symbol"); - this->symbol = std::move(symbol); - if(this->parentTree != NULL && this->parentTree->getAlphabet().find(this->symbol) == this->parentTree->getAlphabet().end()) throw TreeException("Symbol is not in the alphabet"); +void RankedNode::setSymbol ( alphabet::RankedSymbol symbol ) { + if ( symbol.getRank ( ) != this->symbol.getRank ( ) ) throw TreeException ( "Number of children doesn't match the rank of the symbol" ); + + this->symbol = std::move ( symbol ); + + if ( ( this->parentTree != NULL ) && ( this->parentTree->getAlphabet ( ).find ( this->symbol ) == this->parentTree->getAlphabet ( ).end ( ) ) ) throw TreeException ( "Symbol is not in the alphabet" ); } -void RankedNode::swap(RankedNode& other) { - const RankedAlphabet* thisParentTree = this->parentTree; - const RankedAlphabet* otherParentTree = other.parentTree; +void RankedNode::swap ( RankedNode & other ) { + const RankedAlphabet * thisParentTree = this->parentTree; + const RankedAlphabet * otherParentTree = other.parentTree; + + RankedNode tmp = std::move ( other ); - RankedNode tmp = std::move(other); - other = std::move(*this); - *this = std::move(tmp); + other = std::move ( * this ); + * this = std::move ( tmp ); - this -> attachTree(thisParentTree); - other . attachTree(otherParentTree); + this->attachTree ( thisParentTree ); + other.attachTree ( otherParentTree ); } -RankedNode* RankedNode::clone() const { - return new RankedNode(*this); +RankedNode * RankedNode::clone ( ) const { + return new RankedNode ( * this ); } -RankedNode* RankedNode::plunder() && { - return new RankedNode(std::move(*this)); +RankedNode * RankedNode::plunder ( ) && { + return new RankedNode ( std::move ( * this ) ); } -UnrankedNode* RankedNode::cloneAsUnranked() const { - std::vector<UnrankedNode*> clonedChildren; - for(const RankedNode* child : this->children) { - clonedChildren.push_back(child->cloneAsUnranked()); - } - return new UnrankedNode(this->symbol.getSymbol(), std::move(clonedChildren)); +UnrankedNode * RankedNode::cloneAsUnranked ( ) const { + std::vector < UnrankedNode * > clonedChildren; + + for ( const RankedNode * child : this->children ) + clonedChildren.push_back ( child->cloneAsUnranked ( ) ); + + return new UnrankedNode ( this->symbol.getSymbol ( ), std::move ( clonedChildren ) ); } -int RankedNode::compare(const RankedNode& other) const { - int res = this->symbol.compare(other.symbol); - if(res != 0) return res; +int RankedNode::compare ( const RankedNode & other ) const { + int res = this->symbol.compare ( other.symbol ); + + if ( res != 0 ) return res; + + int thisSize = this->children.size ( ); + int otherSize = other.children.size ( ); - int thisSize = this->children.size(); - int otherSize = other.children.size(); - if(thisSize < otherSize) return -1; - 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; + 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; } -void RankedNode::operator>>(std::ostream& out) const { +void RankedNode::operator >>( std::ostream & out ) const { out << "(RankedNode " << " symbol = " << this->symbol << " children = " << this->children << "})"; } -std::ostream& operator<<(std::ostream& out, const RankedNode& node) { +std::ostream & operator <<( std::ostream & out, const RankedNode & node ) { node >> out; return out; } -bool RankedNode::testSymbol( const alphabet::RankedSymbol & symbol ) const { - return symbol == this->symbol; +bool RankedNode::testSymbol ( const alphabet::RankedSymbol & symbol ) const { + if ( symbol == this->symbol ) return true; + + for ( const auto & child : this->children ) + if ( child->testSymbol ( symbol ) ) return true; + + return false; } -bool RankedNode::attachTree(const RankedAlphabet * tree ) { - if(this->parentTree == tree) return true; +bool RankedNode::attachTree ( const RankedAlphabet * tree ) { + if ( this->parentTree == tree ) return true; this->parentTree = tree; - for(const auto& child : this->children) - if(!child->attachTree(tree)) return false; - if(this->parentTree != NULL && this->parentTree->getAlphabet().find(this->symbol) == this->parentTree->getAlphabet().end()) return false; + + for ( const auto & child : this->children ) + if ( !child->attachTree ( tree ) ) return false; + + if ( ( this->parentTree != NULL ) && ( this->parentTree->getAlphabet ( ).find ( this->symbol ) == this->parentTree->getAlphabet ( ).end ( ) ) ) return false; + return true; } -void RankedNode::computeMinimalAlphabet( std::set<alphabet::RankedSymbol>& alphabet ) const { - alphabet.insert(this->symbol); - for(const auto& child : this->children) - child->computeMinimalAlphabet(alphabet); +void RankedNode::computeMinimalAlphabet ( std::set < alphabet::RankedSymbol > & alphabet ) const { + alphabet.insert ( this->symbol ); + + for ( const auto & child : this->children ) + child->computeMinimalAlphabet ( alphabet ); } -const alphabet::RankedSymbol& RankedNode::getSymbol() const { +const alphabet::RankedSymbol & RankedNode::getSymbol ( ) const { return this->symbol; } -RankedNode::operator std::string() const { +RankedNode::operator std::string ( ) const { std::stringstream ss; - ss << *this; - return ss.str(); + ss << * this; + return ss.str ( ); } -void RankedNode::nicePrint(std::ostream & os, const std::string & prefix, const bool last) const { +void RankedNode::nicePrint ( std::ostream & os, const std::string & prefix, const bool last ) const { os << prefix; - std::string nextPrefix(prefix); - if (last) { + std::string nextPrefix ( prefix ); + + if ( last ) { os << "\\-"; nextPrefix += " "; } else { os << "|-"; nextPrefix += "| "; } - os << (std::string) symbol.getSymbol() << "(" << symbol.getRank().getData() << ")" << std::endl; - for (unsigned int i = 0; i < children.size(); ++i) { + os << ( std::string ) symbol.getSymbol ( ) << "(" << symbol.getRank ( ).getData ( ) << ")" << std::endl; + + for ( unsigned int i = 0; i < children.size ( ); ++i ) { os << nextPrefix << "|" << std::endl; - children[i] -> nicePrint(os, nextPrefix, i == children.size()-1); + children[i]->nicePrint ( os, nextPrefix, i == children.size ( ) - 1 ); } } } /* namespace tree */ - diff --git a/alib2data/src/tree/unranked/UnrankedNode.cpp b/alib2data/src/tree/unranked/UnrankedNode.cpp index 32a0ca32ad..2f5c880e3d 100644 --- a/alib2data/src/tree/unranked/UnrankedNode.cpp +++ b/alib2data/src/tree/unranked/UnrankedNode.cpp @@ -14,192 +14,212 @@ namespace tree { -UnrankedNode::UnrankedNode(alphabet::Symbol symbol, std::vector<UnrankedNode *> children) : symbol(std::move(symbol)), children(std::move(children)), parentTree(NULL) { - for (auto& element : this->children) { +UnrankedNode::UnrankedNode ( alphabet::Symbol symbol, std::vector < UnrankedNode * > children ) : symbol ( std::move ( symbol ) ), children ( std::move ( children ) ), parentTree ( NULL ) { + for ( auto & element : this->children ) element->parent = this; - } - this->attachTree(NULL); + + this->attachTree ( NULL ); } -UnrankedNode::UnrankedNode(const UnrankedNode& other) : symbol(other.symbol), parentTree(NULL) { - for (const auto& element : other.children) { - children.push_back(element->clone()); - } - for (auto& element : this->children) { +UnrankedNode::UnrankedNode ( const UnrankedNode & other ) : symbol ( other.symbol ), parentTree ( NULL ) { + for ( const auto & element : other.children ) + children.push_back ( element->clone ( ) ); + + for ( auto & element : this->children ) element->parent = this; - } - this->attachTree(NULL); + + this->attachTree ( NULL ); } -UnrankedNode::UnrankedNode(UnrankedNode&& other) noexcept : symbol(std::move(other.symbol)), children(std::move(other.children)), parentTree(NULL) { - other.children.clear(); - for (auto& element : this->children) { +UnrankedNode::UnrankedNode ( UnrankedNode && other ) noexcept : symbol ( std::move ( other.symbol ) ), children ( std::move ( other.children ) ), parentTree ( NULL ) { + other.children.clear ( ); + + for ( auto & element : this->children ) element->parent = this; - } - this->attachTree(NULL); + + this->attachTree ( NULL ); } -UnrankedNode& UnrankedNode::operator=(const UnrankedNode& other) { - if (this == &other) { - return *this; - } +UnrankedNode & UnrankedNode::operator =( const UnrankedNode & other ) { + if ( this == & other ) + return * this; - *this = UnrankedNode(other); + * this = UnrankedNode ( other ); - return *this; + return * this; } -UnrankedNode& UnrankedNode::operator=(UnrankedNode&& other) noexcept { - std::swap(this->symbol, other.symbol); - std::swap(this->children, other.children); - std::swap(this->parentTree, other.parentTree); // this->parentTree is stored within other.parentTree and it is reattached on the next line +UnrankedNode & UnrankedNode::operator =( UnrankedNode && other ) noexcept { + std::swap ( this->symbol, other.symbol ); + std::swap ( this->children, other.children ); + std::swap ( this->parentTree, other.parentTree ); // this->parentTree is stored within other.parentTree and it is reattached on the next line - for (auto& element : this->children) { + for ( auto & element : this->children ) element->parent = this; - } - this->attachTree(other.parentTree); - return *this; + this->attachTree ( other.parentTree ); + + return * this; } -UnrankedNode::~UnrankedNode() noexcept { - for (auto element : children) { +UnrankedNode::~UnrankedNode ( ) noexcept { + for ( auto element : children ) delete element; - } - children.clear(); + + children.clear ( ); } -const std::vector<const UnrankedNode*> & UnrankedNode::getChildren() const { - return * reinterpret_cast<const std::vector<const UnrankedNode*> * > (&children); +const std::vector < const UnrankedNode * > & UnrankedNode::getChildren ( ) const { + return * reinterpret_cast < const std::vector < const UnrankedNode * > * > ( & children ); } -const std::vector<UnrankedNode*> & UnrankedNode::getChildren() { +const std::vector < UnrankedNode * > & UnrankedNode::getChildren ( ) { return children; } -UnrankedNode * UnrankedNode::getParent() { +UnrankedNode * UnrankedNode::getParent ( ) { return parent; } -const UnrankedNode * UnrankedNode::getParent() const { +const UnrankedNode * UnrankedNode::getParent ( ) const { return parent; } -void UnrankedNode::setSymbol(alphabet::Symbol symbol) { - this->symbol = std::move(symbol); - if(this->parentTree != NULL && this->parentTree->getAlphabet().find(this->symbol) == this->parentTree->getAlphabet().end()) throw TreeException("Symbol is not in the alphabet"); +void UnrankedNode::setSymbol ( alphabet::Symbol symbol ) { + this->symbol = std::move ( symbol ); + + if ( ( this->parentTree != NULL ) && ( this->parentTree->getAlphabet ( ).find ( this->symbol ) == this->parentTree->getAlphabet ( ).end ( ) ) ) throw TreeException ( "Symbol is not in the alphabet" ); } -void UnrankedNode::pushBackChild(const UnrankedNode & node) { - UnrankedNode* tmp = node.clone(); - tmp->attachTree(this->parentTree); - this->children.push_back(tmp); +void UnrankedNode::pushBackChild ( const UnrankedNode & node ) { + UnrankedNode * tmp = node.clone ( ); + + tmp->attachTree ( this->parentTree ); + this->children.push_back ( tmp ); } -void UnrankedNode::swap(UnrankedNode& other) { - const UnrankedAlphabet* thisParentTree = this->parentTree; - const UnrankedAlphabet* otherParentTree = other.parentTree; +void UnrankedNode::swap ( UnrankedNode & other ) { + const UnrankedAlphabet * thisParentTree = this->parentTree; + const UnrankedAlphabet * otherParentTree = other.parentTree; - UnrankedNode tmp = std::move(other); - other = std::move(*this); - *this = std::move(tmp); + UnrankedNode tmp = std::move ( other ); - this -> attachTree(thisParentTree); - other . attachTree(otherParentTree); + other = std::move ( * this ); + * this = std::move ( tmp ); + + this->attachTree ( thisParentTree ); + other.attachTree ( otherParentTree ); } -UnrankedNode* UnrankedNode::clone() const { - return new UnrankedNode(*this); +UnrankedNode * UnrankedNode::clone ( ) const { + return new UnrankedNode ( * this ); } -UnrankedNode* UnrankedNode::plunder() && { - return new UnrankedNode(std::move(*this)); +UnrankedNode * UnrankedNode::plunder ( ) && { + return new UnrankedNode ( std::move ( * this ) ); } -RankedNode* UnrankedNode::cloneAsRanked() const { - std::vector<RankedNode*> clonedChildren; - for(const UnrankedNode* child : this->children) { - clonedChildren.push_back(child->cloneAsRanked()); - } - return new RankedNode(alphabet::RankedSymbol(this->symbol, primitive::Unsigned(clonedChildren.size())), std::move(clonedChildren)); +RankedNode * UnrankedNode::cloneAsRanked ( ) const { + std::vector < RankedNode * > clonedChildren; + + for ( const UnrankedNode * child : this->children ) + clonedChildren.push_back ( child->cloneAsRanked ( ) ); + + return new RankedNode ( alphabet::RankedSymbol ( this->symbol, primitive::Unsigned ( clonedChildren.size ( ) ) ), std::move ( clonedChildren ) ); } -int UnrankedNode::compare(const UnrankedNode& other) const { - std::compare<alphabet::Symbol> comp; - int res = comp(this->symbol, other.symbol); - if(res != 0) return res; +int UnrankedNode::compare ( const UnrankedNode & other ) const { + std::compare < alphabet::Symbol > comp; + int res = comp ( this->symbol, other.symbol ); + + if ( res != 0 ) return res; + + int thisSize = this->children.size ( ); + int otherSize = other.children.size ( ); + + if ( thisSize < otherSize ) return -1; - int thisSize = this->children.size(); - int otherSize = other.children.size(); - if(thisSize < otherSize) return -1; - 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; + 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; } -void UnrankedNode::operator>>(std::ostream& out) const { +void UnrankedNode::operator >>( std::ostream & out ) const { out << "(UnrankedNode " << " symbol = " << this->symbol << " children = " << this->children << "})"; } -std::ostream& operator<<(std::ostream& out, const UnrankedNode& node) { +std::ostream & operator <<( std::ostream & out, const UnrankedNode & node ) { node >> out; return out; } -bool UnrankedNode::testSymbol( const alphabet::Symbol & symbol ) const { - return symbol == this->symbol; +bool UnrankedNode::testSymbol ( const alphabet::Symbol & symbol ) const { + if ( symbol == this->symbol ) return true; + + for ( const auto & child : this->children ) + if ( child->testSymbol ( symbol ) ) return true; + + return false; } -bool UnrankedNode::attachTree(const UnrankedAlphabet * tree ) { - if(this->parentTree == tree) return true; +bool UnrankedNode::attachTree ( const UnrankedAlphabet * tree ) { + if ( this->parentTree == tree ) return true; this->parentTree = tree; - for(const auto& child : this->children) - if(!child->attachTree(tree)) return false; - if(this->parentTree != NULL && this->parentTree->getAlphabet().find(this->symbol) == this->parentTree->getAlphabet().end()) return false; + + for ( const auto & child : this->children ) + if ( !child->attachTree ( tree ) ) return false; + + if ( ( this->parentTree != NULL ) && ( this->parentTree->getAlphabet ( ).find ( this->symbol ) == this->parentTree->getAlphabet ( ).end ( ) ) ) return false; + return true; } -void UnrankedNode::computeMinimalAlphabet( std::set<alphabet::Symbol>& alphabet ) const { - alphabet.insert(this->symbol); - for(const auto& child : this->children) - child->computeMinimalAlphabet(alphabet); +void UnrankedNode::computeMinimalAlphabet ( std::set < alphabet::Symbol > & alphabet ) const { + alphabet.insert ( this->symbol ); + + for ( const auto & child : this->children ) + child->computeMinimalAlphabet ( alphabet ); } -const alphabet::Symbol& UnrankedNode::getSymbol() const { +const alphabet::Symbol & UnrankedNode::getSymbol ( ) const { return this->symbol; } -UnrankedNode::operator std::string() const { +UnrankedNode::operator std::string ( ) const { std::stringstream ss; - ss << *this; - return ss.str(); + ss << * this; + return ss.str ( ); } -void UnrankedNode::nicePrint(std::ostream & os, const std::string & prefix, const bool last) const { +void UnrankedNode::nicePrint ( std::ostream & os, const std::string & prefix, const bool last ) const { os << prefix; - std::string nextPrefix(prefix); - if (last) { + std::string nextPrefix ( prefix ); + + if ( last ) { os << "\\-"; nextPrefix += " "; } else { os << "|-"; nextPrefix += "| "; } - os << (std::string) symbol << std::endl; - for (unsigned int i = 0; i < children.size(); ++i) { + os << ( std::string ) symbol << std::endl; + + for ( unsigned int i = 0; i < children.size ( ); ++i ) { os << nextPrefix << "|" << std::endl; - children[i] -> nicePrint(os, nextPrefix, i == children.size()-1); + children[i]->nicePrint ( os, nextPrefix, i == children.size ( ) - 1 ); } } } /* namespace tree */ - -- GitLab