From ac182b04d1772944ba1473a129a35198aa486afc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9a=20Saviot?= Date: Fri, 22 Jun 2018 10:39:40 +0200 Subject: [PATCH] Clean TreeNode --- tree_node.cpp | 15 +++++++++++- tree_node.h | 68 +++++++++++++++++---------------------------------- 2 files changed, 37 insertions(+), 46 deletions(-) diff --git a/tree_node.cpp b/tree_node.cpp index 678205491..e05cc7bc7 100644 --- a/tree_node.cpp +++ b/tree_node.cpp @@ -2,7 +2,7 @@ #include "tree_pool.h" #include "expression_node.h" -TreeNode * TreeNode::parent() const { +TreeNode * TreeNode::treeParent() const { int cursor = -1; TreeNode * parentsHistory[TreePool::MaxNumberOfNodes]; int numberOfChildrenHistory[TreePool::MaxNumberOfNodes]; @@ -31,6 +31,19 @@ TreeNode * TreeNode::parent() const { return nullptr; } +TreeNode * TreeNode::treeChildAtIndex(int i) const { + assert(i >= 0); + assert(i < numberOfChildren()); + TreeNode * child = next(); + while (i > 0) { + child = child->nextSibling(); + assert(child != nullptr); + i--; + } + return child; +} + + void TreeNode::release() { printf("Release %d(%p)\n", m_identifier, this); m_referenceCounter--; diff --git a/tree_node.h b/tree_node.h index f94978c70..d892f6829 100644 --- a/tree_node.h +++ b/tree_node.h @@ -20,9 +20,29 @@ class TreeNode { friend class TreePool; public: virtual ~TreeNode() {} - int indentifier() const { return m_identifier; } - TreeNode * parent() const; + // Attributes + virtual size_t size() const = 0; + int identifier() const { return m_identifier; } + int retainCount() const { return m_referenceCounter; } +#if TREE_LOGGING + virtual const char * description() const { + return "UNKNOWN"; + } +#endif + + // Node operations + void retain() { m_referenceCounter++; } + void release(); + void rename(int identifier) { + m_identifier = identifier; + m_referenceCounter = 1; + } + + // Hierarchy + TreeNode * treeParent() const; + virtual int numberOfChildren() const { return 0; } + TreeNode * treeChildAtIndex(int i) const; class Iterator { public: @@ -50,8 +70,6 @@ public: TreeNode * m_node; }; - Direct directChildren() { return Direct(this); } - class DepthFirst { public: DepthFirst(TreeNode * node) : m_node(node) {} @@ -69,49 +87,9 @@ public: TreeNode * m_node; }; + Direct directChildren() { return Direct(this); } DepthFirst depthFirstChildren() { return DepthFirst(this); } - int identifier() const { return m_identifier; } - -#if TREE_LOGGING - virtual const char * description() const { - return "UNKNOWN"; - } -#endif - - virtual size_t size() const = 0; - - void retain() { - m_referenceCounter++; - } - - void release(); - - void rename(int identifier) { - m_identifier = identifier; - m_referenceCounter = 1; - } - - int retainCount() const { - return m_referenceCounter; - } - - virtual int numberOfChildren() const { - return 0; - } - - TreeNode * treeChildAtIndex(int i) const { - assert(i >= 0); - assert(i < numberOfChildren()); - TreeNode * child = next(); - while (i > 0) { - child = child->nextSibling(); - assert(child != nullptr); - i--; - } - return child; - } - TreeNode * next() const { // Simple version would be "return this + 1;", with pointer arithmetics taken care of by the compiler. // Unfortunately, we want TreeNode to have a VARIABLE size