TreeNode::parent algorithm

This commit is contained in:
Léa Saviot
2018-06-22 10:23:40 +02:00
parent 1adff31abd
commit 287c9ca990
3 changed files with 59 additions and 12 deletions

View File

@@ -2,6 +2,35 @@
#include "tree_pool.h"
#include "expression_node.h"
TreeNode * TreeNode::parent() const {
int cursor = -1;
TreeNode * parentsHistory[TreePool::MaxNumberOfNodes];
int numberOfChildrenHistory[TreePool::MaxNumberOfNodes];
int childrenVisitedCountHistory[TreePool::MaxNumberOfNodes];
for (TreeNode * node : TreePool::sharedPool()->allNodes()) {
if (node->identifier() == m_identifier) {
printf("Parent of %d is %d\n", m_identifier, cursor >= 0 ? parentsHistory[cursor]->identifier() : -1);
return cursor >= 0 ? parentsHistory[cursor] : nullptr;
}
if (cursor >= 0) {
childrenVisitedCountHistory[cursor] = childrenVisitedCountHistory[cursor]+1;
}
int nodeChildrenCount = node->numberOfChildren();
if (nodeChildrenCount > 0) {
cursor++;
parentsHistory[cursor] = node;
numberOfChildrenHistory[cursor] = nodeChildrenCount;
childrenVisitedCountHistory[cursor] = 0;
} else {
if (cursor >= 0 && (numberOfChildrenHistory[cursor] == childrenVisitedCountHistory[cursor])) {
cursor--;
}
}
}
assert(false);
return nullptr;
}
void TreeNode::release() {
printf("Release %d(%p)\n", m_identifier, this);
m_referenceCounter--;