Use iterative algorith to assign DFS number. This reduces

call stack depth.

llvm-svn: 30575
This commit is contained in:
Devang Patel 2006-09-22 01:05:33 +00:00
parent 7d3fd4f888
commit 0c4e730c9c
2 changed files with 48 additions and 10 deletions

View File

@ -250,16 +250,7 @@ public:
return this->Below(other);
}
void assignDFSNumber(int &num) {
DFSNumIn = num++;
if (Son) {
Son->assignDFSNumber(num);
for (ETNode *son = Son->Right; son != Son; son = son->Right)
son->assignDFSNumber(num);
}
DFSNumOut = num++;
}
void assignDFSNumber (int);
bool hasFather() const {
return Father != NULL;

View File

@ -809,6 +809,53 @@ ETNode *ETNode::NCA(ETNode *other) {
return occmin->OccFor;
}
void ETNode::assignDFSNumber(int num) {
std::vector<ETNode *> workStack;
std::set<ETNode *> visitedNodes;
workStack.push_back(this);
visitedNodes.insert(this);
this->DFSNumIn = num++;
while (!workStack.empty()) {
ETNode *Node = workStack.back();
// If this is leaf node then set DFSNumOut and pop the stack
if (!Node->Son) {
Node->DFSNumOut = num++;
workStack.pop_back();
continue;
}
ETNode *son = Node->Son;
// Visit Node->Son first
if (visitedNodes.count(son) == 0) {
son->DFSNumIn = num++;
workStack.push_back(son);
visitedNodes.insert(son);
continue;
}
bool visitChild = false;
// Visit remaining children
for (ETNode *s = son->Right; s != son && !visitChild; s = s->Right) {
if (visitedNodes.count(s) == 0) {
visitChild = true;
s->DFSNumIn = num++;
workStack.push_back(s);
visitedNodes.insert(s);
}
}
if (!visitChild) {
// If we reach here means all children are visited
Node->DFSNumOut = num++;
workStack.pop_back();
}
}
}
//===----------------------------------------------------------------------===//
// ETForest implementation
//===----------------------------------------------------------------------===//