diff --git a/Makefile b/Makefile index e2ef28479..55257d260 100644 --- a/Makefile +++ b/Makefile @@ -5,7 +5,7 @@ test: $(OBJS) clang++ $(CXXFLAGS) $^ -o $@ clean: - rm -f $(OBJS) + rm -f $(OBJS) test %.o: %.cpp clang++ $(CXXFLAGS) -c $< -o $@ diff --git a/addition_node.h b/addition_node.h index e559e7a34..e3a8ab618 100644 --- a/addition_node.h +++ b/addition_node.h @@ -6,9 +6,6 @@ class AdditionNode : public ExpressionNode { public: - AdditionNode() : ExpressionNode() { - printf("Create Addition\n"); - } #if TREE_LOGGING const char * description() const override { return "Addition"; @@ -21,6 +18,10 @@ public: } return result; } + + int numberOfChildren() const override { + return 2; + } /* Expression simplify() override { // Scan operands, merge constants @@ -31,17 +32,13 @@ public: */ }; -#include "end_node.h" - class Addition : public ExpressionReference { public: Addition(Expression e1, Expression e2) : ExpressionReference() { - ExpressionReference end; - ExpressionNode::Pool()->log(); - addOperand(end); - ExpressionNode::Pool()->log(); + addOperand(e2); + addOperand(e1); } }; diff --git a/end_node.h b/end_node.h deleted file mode 100644 index e454765c1..000000000 --- a/end_node.h +++ /dev/null @@ -1,25 +0,0 @@ -#ifndef END_NODE_H -#define END_NODE_H - -#include "expression_reference.h" -#include "expression_node.h" - -class EndNode : public ExpressionNode { -public: - EndNode() : ExpressionNode() { - printf("Create end node\n"); - } - float approximate() override { - assert(false); - return 0.0f; - } -#if TREE_LOGGING - const char * description() const override { - return "End"; - } -#endif -}; - -//typedef ExpressionReference End; - -#endif diff --git a/test.cpp b/test.cpp index 7a643d9bd..f58e54047 100644 --- a/test.cpp +++ b/test.cpp @@ -13,8 +13,6 @@ void poolTest() { AdditionNode * h = new AdditionNode(); - EndNode * i = new EndNode(); - ExpressionNode::Pool()->log(); delete g; @@ -26,17 +24,20 @@ Expression buildAddition() { Float smallFloat(0.2f); Float bigFloat(3.4f); - ExpressionNode::Pool()->log(); - Addition a(smallFloat, bigFloat); ExpressionNode::Pool()->log(); + printf("EXITING\n"); + printf("smallFloat ref = %d\n", smallFloat.identifier()); + printf("bigFloat ref = %d\n", bigFloat.identifier()); return a; } int main() { printf("Hello\n"); - buildAddition(); + Expression a = buildAddition(); + printf("HAS RETURNED\n"); + ExpressionNode::Pool()->log(); diff --git a/tree_node.cpp b/tree_node.cpp index f5e69032f..94f029d6b 100644 --- a/tree_node.cpp +++ b/tree_node.cpp @@ -2,15 +2,14 @@ #include "expression_node.h" void TreeNode::release() { + printf("release of %d\n", m_identifier); m_referenceCounter--; - for (TreeNode * child : directChildren()) { - child->release(); - } if (m_referenceCounter == 0) { + for (TreeNode * child : directChildren()) { + child->release(); + } + printf("DELETE %d(%p)\n", m_identifier, this); delete this; - //dealloc(); - printf("Will log\n"); - ExpressionNode::Pool()->log(); } } diff --git a/tree_node.h b/tree_node.h index 8034c120e..5d764026f 100644 --- a/tree_node.h +++ b/tree_node.h @@ -58,7 +58,7 @@ public: return *this; } }; - Iterator begin() const { return Iterator(m_node); } + Iterator begin() const { return Iterator(m_node->next()); } Iterator end() const { return Iterator(m_node->nextSibling()); } private: TreeNode * m_node; @@ -93,23 +93,8 @@ public: return m_referenceCounter; } - virtual bool hasVariableNumberOfChildren() const { - return false; - } - - int numberOfChildren() const { - if (hasVariableNumberOfChildren()) { - int numberOfChildren = 0; - TreeNode * child = next(); - while (!child->isEndMarker()) { - child = child->nextSibling(); - numberOfChildren++; - } - return numberOfChildren; - } else { - // TODO: Make this function virtual - return 0; - } + virtual int numberOfChildren() const { + return 0; } TreeNode * childAtIndex(int i) const { @@ -119,20 +104,11 @@ public: while (i>0) { child = child->nextSibling(); assert(child != nullptr); - assert(!child->isEndMarker()); i--; } return child; } - /* - void addChild(TreeNode * node) { - // This will move node in the pool so that it becomes - // a children of this - pool->move(node, this + size()); - } - */ - //private: // FIXME: Make this private @@ -142,21 +118,6 @@ public: { } - constexpr static int EmptyIdentifier = 0; - constexpr static int EndMarkerIdentifier = 1; - - bool isEndMarker() const { - return (m_identifier == EndMarkerIdentifier); - } - - bool isEmpty() const { - return (m_identifier == EmptyIdentifier); - } - - void markAsEmpty() { - m_identifier = EmptyIdentifier; - } - 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 @@ -164,22 +125,14 @@ public: } TreeNode * nextSibling() const { - TreeNode * n = const_cast(this); - int depth = 0; + TreeNode * node = const_cast(this); + int remainingNodesToVisit = 0; do { - if (n->hasVariableNumberOfChildren()) { - depth++; - } - /* - if (n->isEndMarker()) { - depth--; - } - */ - n = n->next(); - // TODO: Return nullptr if n overflows the pool! - assert(depth >= 0); - } while(depth != 0); - return n; + remainingNodesToVisit += node->numberOfChildren(); + node = node->next(); + remainingNodesToVisit--; + } while (remainingNodesToVisit > 0); + return node; } size_t deepSize() const { diff --git a/tree_pool.h b/tree_pool.h index 63b68fedd..fbf11f375 100644 --- a/tree_pool.h +++ b/tree_pool.h @@ -10,6 +10,7 @@ public: TreePool() : m_lastIdentifier(0), m_cursor(m_buffer) {} int generateIdentifier() { + printf("Generating identifier %d\n", m_lastIdentifier); /* For now we're not bothering with making sure the identifiers are indeed * unique. We're just assuming we'll never overflow... */ //assert(node(m_lastIdentifier) == nullptr); diff --git a/tree_reference.h b/tree_reference.h index 3a47f47f5..7bedd292a 100644 --- a/tree_reference.h +++ b/tree_reference.h @@ -13,37 +13,14 @@ public: m_pool(tr.m_pool), m_identifier(tr.m_identifier) { - printf("TreeReference copy\n"); + printf("TreeReference copy of %d\n", m_identifier); node()->retain(); } ~TreeReference() { - TreeNode * node = this->node(); - node->release(); -#if 0 - if (node->retainCount() == 0) { - - - // Here we deal with removing a node. - // It's not as easy as one may think. - // -> When a node is not needed anymore - - - printf("Discarding node %d(%p)\n", node->identifier(), node); - - // Here the static_cast should fail if T is not a subclass of TreeNode - size_t deepNodeSize = node->deepSize(); -#if 0 - // Here, if needed, call reclaimIdentifier - for (TreeNode * child : node->depthFirstChildren()) { - m_pool->reclaimIdentifier(child->identifier()); - } - m_pool->reclaimIdentifier(node->identifier()); -#endif - static_cast(node)->~T(); - m_pool->dealloc(node, deepNodeSize); - } -#endif + printf("TreeRef destroy of %d\n", m_identifier); + assert(node()->m_identifier == m_identifier); + node()->release(); } operator TreeReference() const { @@ -83,12 +60,15 @@ public: return static_cast(m_pool->node(m_identifier)); } + int identifier() const { return m_identifier; } + protected: TreeReference(TreePool * pool) : m_pool(pool) { TreeNode * node = new T(); m_identifier = node->identifier(); + printf("TreeNode orig build of %d\n", m_identifier); } private: @@ -97,6 +77,7 @@ private: m_identifier(node->identifier()) //m_cachedNode(node) { + printf("TreeNode build of %d\n", m_identifier); node->retain(); }