ReplaceWith and ReplaceChild methods on References

This commit is contained in:
Léa Saviot
2018-06-22 14:05:23 +02:00
parent 0fb04ea45a
commit 6f598acc10
5 changed files with 81 additions and 63 deletions

View File

@@ -2,6 +2,32 @@
#include "tree_pool.h"
#include "expression_node.h"
// Node operations
void TreeNode::release() {
printf("Release %d(%p)\n", m_identifier, this);
m_referenceCounter--;
if (m_referenceCounter == 0) {
if (numberOfChildren() != 0) {
int lastIdentifier = lastChild()->identifier();
TreeNode * child = next();
bool lastChildReleased = false;
while (!lastChildReleased) {
lastChildReleased = child->identifier() == lastIdentifier;
int nextSiblingIdentifier = lastChildReleased ? -1 : child->nextSibling()->identifier();
child->release();
if (nextSiblingIdentifier != -1) {
child = TreePool::sharedPool()->node(nextSiblingIdentifier);
}
}
}
printf("Delete %d(%p)\n", m_identifier, this);
TreePool::sharedPool()->discardTreeNode(this);
}
}
// Hierarchy
TreeNode * TreeNode::parentTree() const {
// Choose between those two algorithms: the first has complexity O(numberNodes) but uses 0(3maxNumberNodes) space
// The second is much clearer for the reader and uses no space, but has complexity 0
@@ -130,25 +156,3 @@ bool TreeNode::hasSibling(const TreeNode * e) const {
}
return false;
}
void TreeNode::release() {
printf("Release %d(%p)\n", m_identifier, this);
m_referenceCounter--;
if (m_referenceCounter == 0) {
if (numberOfChildren() != 0) {
int lastIdentifier = lastChild()->identifier();
TreeNode * child = next();
bool lastChildReleased = false;
while (!lastChildReleased) {
lastChildReleased = child->identifier() == lastIdentifier;
int nextSiblingIdentifier = lastChildReleased ? -1 : child->nextSibling()->identifier();
child->release();
if (nextSiblingIdentifier != -1) {
child = TreePool::sharedPool()->node(nextSiblingIdentifier);
}
}
}
printf("Delete %d(%p)\n", m_identifier, this);
TreePool::sharedPool()->discardTreeNode(this);
}
}