[poincare] Check if the node is static before some TreeNode operations

This commit is contained in:
Léa Saviot
2018-07-30 16:16:11 +02:00
parent 9a0602719e
commit 4b190093e7
3 changed files with 26 additions and 10 deletions

View File

@@ -22,11 +22,12 @@ public:
virtual ~TreeNode() {}
// Attributes
bool isStatic() const;
virtual size_t size() const = 0;
int identifier() const { return m_identifier; }
int retainCount() const { return m_referenceCounter; }
void setReferenceCounter(int refCount) { //TODO make this method privte with only friends that can access it
if (m_identifier < 0) {
if (isStatic()) {
// Do not retain static nodes
return;
}
@@ -51,7 +52,7 @@ public:
// Node operations
void retain() {
if (m_identifier < 0) {
if (isStatic()) {
// Do not retain static nodes
return;
}
@@ -156,11 +157,7 @@ public:
}
protected:
TreeNode() :
m_identifier(-1),
m_referenceCounter(1)
{
}
TreeNode();
TreeNode * lastChild() const {
if (numberOfChildren() == 0) {

View File

@@ -12,6 +12,8 @@ class TreePool {
friend class TreeNode;
public:
static constexpr int NoNodeIdentifier = -1;
static constexpr int FirstStaticNodeIdentifier = -2;
static TreePool * sharedPool();
// Node
@@ -108,8 +110,8 @@ private:
node->rename(generateIdentifier(), unregisterPreviousIdentifier);
}
int identifierOfStaticNodeAtIndex(int index) const { return - (index+2);} // We do not want positive indexes that are reserved for pool nodes, and -1 is reserved for node initialisation.
int indexOfStaticNode(int id) const { return -id-2;}
int identifierOfStaticNodeAtIndex(int index) const { return FirstStaticNodeIdentifier-index;} // We do not want positive indexes that are reserved for pool nodes, and -1 is reserved for node initialisation.
int indexOfStaticNode(int id) const { return -(id - FirstStaticNodeIdentifier);}
// Iterators

View File

@@ -5,10 +5,14 @@
namespace Poincare {
bool TreeNode::isStatic() const {
return m_identifier <= TreePool::FirstStaticNodeIdentifier;
}
// Node operations
void TreeNode::release() {
if (m_identifier < 0) {
if (isStatic()) {
// Do not release static nodes
return;
}
@@ -46,6 +50,9 @@ void TreeNode::rename(int identifier, bool unregisterPreviousIdentifier) {
// Hierarchy
TreeNode * TreeNode::parent() const {
if (isStatic()) {
return nullptr;
}
/* Choose between these algorithms: the first has complexity O(numberNodes)
* but uses O(3maxNumberNodes) space. The second is much clearer for the
* reader and uses no space, but has complexity O(numberNodes^2) */
@@ -93,6 +100,9 @@ TreeNode * TreeNode::parent() const {
}
TreeNode * TreeNode::root() {
if (isStatic()) {
return this;
}
for (TreeNode * root : TreePool::sharedPool()->roots()) {
if (hasAncestor(root, true)) {
return root;
@@ -192,6 +202,13 @@ bool TreeNode::hasSibling(const TreeNode * e) const {
return false;
}
TreeNode::TreeNode() :
m_identifier(TreePool::NoNodeIdentifier),
m_referenceCounter(1)
{
}
size_t TreeNode::deepSize(int realNumberOfChildren) const {
if (realNumberOfChildren == -1) {
// TODO: Error handling