diff --git a/poincare/include/poincare/tree_node.h b/poincare/include/poincare/tree_node.h index 8eb766f55..28172ff8f 100644 --- a/poincare/include/poincare/tree_node.h +++ b/poincare/include/poincare/tree_node.h @@ -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) { diff --git a/poincare/include/poincare/tree_pool.h b/poincare/include/poincare/tree_pool.h index 11392a421..00d549714 100644 --- a/poincare/include/poincare/tree_pool.h +++ b/poincare/include/poincare/tree_pool.h @@ -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 diff --git a/poincare/src/tree_node.cpp b/poincare/src/tree_node.cpp index 0b00571d2..8a93ac1e0 100644 --- a/poincare/src/tree_node.cpp +++ b/poincare/src/tree_node.cpp @@ -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