void SgNode::PathToRoot(SgVectorOf<SgNode>* path) const { path->Clear(); for (SgNode* node = const_cast<SgNode*>(this); node; node = node->Father()) path->PushBack(node); }
SgNode* SgNode::Root() const { SgNode* node = const_cast<SgNode*>(this); while (node->HasFather()) node = node->Father(); return node; }
SgNode* SgNode::TopProp(SgPropID id) const { SgNode* node = const_cast<SgNode*>(this); while (node->HasFather() && ! node->Get(id)) node = node->Father(); // Return either root node or node with property. return node; }
void SgNode::PromotePath() { SgNode* node = this; while (node) { node->PromoteNode(); // promote all nodes on the path to the root node = node->Father(); } }
SgNode* SgNode::NextDepthFirst() const { if (HasSon()) return LeftMostSon(); else { SgNode* node = const_cast<SgNode*>(this); while (node->HasFather() && ! node->HasRightBrother()) node = node->Father(); if (node->HasRightBrother()) node = node->RightBrother(); return node; } }
SgNode* SgNode::NodeInDirection(Direction dir) const { SgNode* node = 0; switch (dir) { case PREVIOUS: node = Father(); break; case NEXT: node = LeftMostSon(); break; case NEXT_RIGHTMOST: node = RightMostSon(); break; case PREV_DEPTHFIRST: node = PrevDepthFirst(); break; case NEXT_DEPTHFIRST: node = NextDepthFirst(); break; case PREV_TERMINAL: node = PrevDepthFirst(); while (! node->IsTerminal()) node = node->PrevDepthFirst(); break; case NEXT_TERMINAL: node = NextDepthFirst(); while (! node->IsTerminal()) node = node->NextDepthFirst(); break; case PREV_BRANCH: node = Father(); while (node && node->HasFather() && ! node->IsBranchPoint()) node = node->Father(); break; case NEXT_BRANCH: node = LeftMostSon(); while (node && ! node->IsTerminal() && ! node->IsBranchPoint()) node = node->LeftMostSon(); break; case LEFT_BROTHER: if (HasLeftBrother()) node = LeftBrother(); else if (HasBrother()) node = Father()->RightMostSon(); // wrap around break; case RIGHT_BROTHER: if (HasRightBrother()) node = RightBrother(); else if (HasBrother()) node = Father()->LeftMostSon(); // wrap around break; case MAIN_BRANCH: { SgNode* t = const_cast<SgNode*>(this); while (t->HasFather()) { if (t->HasLeftBrother()) node = t->Father(); t = t->Father(); } } break; case START_OF_GAME: node = Root(); break; case END_OF_GAME: node = const_cast<SgNode*>(this); while (! node->IsTerminal()) node = node->LeftMostSon(); break; default: SG_ASSERT(false); } return node; }