From 3aad19966f81189219d00d0ea905b6b1e83d5576 Mon Sep 17 00:00:00 2001 From: Adrian Kummerlaender Date: Fri, 26 Sep 2014 22:24:49 +0200 Subject: Simplified Tree::addNode and continued cleanup * member method "addNode" of "Tree" was relieved of its uneccessary "place" argument * again contified where possible ** e.g. the "buildTree" local element vectors for storing the lexer result --- src/tree.cc | 62 +++++++++++++++++++++++++++---------------------------------- src/tree.h | 7 ++++--- 2 files changed, 31 insertions(+), 38 deletions(-) diff --git a/src/tree.cc b/src/tree.cc index 8ccb108..11bb299 100644 --- a/src/tree.cc +++ b/src/tree.cc @@ -52,15 +52,16 @@ std::string Tree::print() const { << this->term_ << "\"" << std::endl - << "{" << std::endl + << "{" + << std::endl << "node [shape = box];" << std::endl; - size_t i{}; + std::size_t nodeIndex{}; for ( auto&& node : this->node_collection_ ) { out << "node" - << i + << nodeIndex << " [ label = \"" << node->print() << "\"];" @@ -68,58 +69,49 @@ std::string Tree::print() const { if ( node->rightChild != nullptr && node->leftChild != nullptr ) { - size_t j{}; + std::size_t childIndex{}; for ( auto&& child : this->node_collection_ ) { - if ( child.get() == node->leftChild ) { + if ( child.get() == node->leftChild || + child.get() == node->rightChild ) { out << "\"node" - << i - << "\" -> \"node" - << j + << nodeIndex + << "\" -> \"node" + << childIndex << "\";" << std::endl; } - if ( child.get() == node->rightChild ) { - out << "\"node" - << i - << "\" -> \"node" - << j - << "\";" - << std::endl; - } - - ++j; + ++childIndex; } } - ++i; + ++nodeIndex; } - out << "}" - << std::endl; + out << "}" << std::endl; return out.str(); } -template -Node* Tree::addNode(Node** place, Args&&... args) { +template +Node* Tree::addNode(Args&&... args) { this->node_collection_.emplace_back( - std::make_unique(std::forward(args)...) + std::make_unique( + std::forward(args)... + ) ); - if ( place != nullptr ) { - *place = this->node_collection_.back().get(); - } - return this->node_collection_.back().get(); } -Node* Tree::buildTree(std::string term) { +Node* Tree::buildTree(const std::string& term) { std::stack operands; std::stack operators; - std::vector topElements(lexer(term)); + const std::vector topElements{ + lexer(term) + }; for ( auto elementIterator = topElements.begin(); elementIterator != topElements.end(); @@ -132,7 +124,7 @@ Node* Tree::buildTree(std::string term) { element.size() == 1 ) { if ( operators.empty() ) { operators.push( - this->addNode(nullptr, elementToken) + this->addNode(elementToken) ); } else { OperatorNode*const lastNode( @@ -141,7 +133,7 @@ Node* Tree::buildTree(std::string term) { if ( precedence(elementToken) > precedence(lastNode->token()) ) { operators.push( - this->addNode(nullptr, elementToken) + this->addNode(elementToken) ); } else { Node*const currOperator = popNode(operators); @@ -154,14 +146,15 @@ Node* Tree::buildTree(std::string term) { } } } else { - std::vector subElements(lexer(element)); + const std::vector subElements{ + lexer(element) + }; if ( subElements.size() == 1 ) { switch ( determineToken(subElements[0][0]) ) { case TokenType::VALUE_NUMBER: case TokenType::OPERATOR_MINUS: { operands.push(this->addNode( - nullptr, doubleToString(subElements[0]) )); @@ -169,7 +162,6 @@ Node* Tree::buildTree(std::string term) { } case TokenType::VALUE_IDENTIFIER: { operands.push(this->addNode( - nullptr, subElements[0], this->constants_ )); diff --git a/src/tree.h b/src/tree.h index 186f162..e2eb7a6 100644 --- a/src/tree.h +++ b/src/tree.h @@ -18,9 +18,10 @@ class Tree { std::string print() const; private: - template - Node* addNode(Node**, Args&&... args); - Node* buildTree(std::string); + template + Node* addNode(Args&&... args); + + Node* buildTree(const std::string&); const std::string term_; const ConstantMap* constants_; -- cgit v1.2.3