From e4596e9ecbe8c17baf1f4e668fa89a1056863aad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9a=20Saviot?= Date: Tue, 31 Jul 2018 11:38:28 +0200 Subject: [PATCH] [apps] Remove dead development code --- apps/tree/Makefile | 11 - apps/tree/addition_node.cpp | 48 --- apps/tree/addition_node.h | 50 --- apps/tree/allocation_failed_expression_node.h | 26 -- apps/tree/expression_node.cpp | 11 - apps/tree/expression_node.h | 47 --- apps/tree/expression_reference.cpp | 12 - apps/tree/expression_reference.h | 64 ---- apps/tree/float_node.h | 34 -- apps/tree/refs.h | 8 - apps/tree/test.cpp | 341 ------------------ 11 files changed, 652 deletions(-) delete mode 100644 apps/tree/Makefile delete mode 100644 apps/tree/addition_node.cpp delete mode 100644 apps/tree/addition_node.h delete mode 100644 apps/tree/allocation_failed_expression_node.h delete mode 100644 apps/tree/expression_node.cpp delete mode 100644 apps/tree/expression_node.h delete mode 100644 apps/tree/expression_reference.cpp delete mode 100644 apps/tree/expression_reference.h delete mode 100644 apps/tree/float_node.h delete mode 100644 apps/tree/refs.h delete mode 100644 apps/tree/test.cpp diff --git a/apps/tree/Makefile b/apps/tree/Makefile deleted file mode 100644 index 8366d07be..000000000 --- a/apps/tree/Makefile +++ /dev/null @@ -1,11 +0,0 @@ -app_objs = $(addprefix apps/tree/,\ - addition_node.o\ - expression_node.o\ - expression_reference.o\ - layout_cursor.o\ - layout_node.o\ - layout_reference.o\ - tree_node.o\ - tree_pool.o\ -) - diff --git a/apps/tree/addition_node.cpp b/apps/tree/addition_node.cpp deleted file mode 100644 index 78bf1c70f..000000000 --- a/apps/tree/addition_node.cpp +++ /dev/null @@ -1,48 +0,0 @@ -#include "addition_node.h" -#include "float_node.h" - -bool AdditionNode::shallowReduce() { - if (ExpressionNode::shallowReduce()) { - return true; - } - /* Step 1: Addition is associative, so let's start by merging children which - * also are additions themselves. */ - int i = 0; - int initialNumberOfChildren = numberOfChildren(); - while (i < initialNumberOfChildren) { - ExpressionNode * currentChild = child(i); - if (currentChild->type() == Type::Addition) { - TreeRef(this).mergeChildren(TreeRef(currentChild)); - // Is it ok to modify memory while executing ? - continue; - } - i++; - } - - // Step 2: Sort the operands - sortChildren(); - - /* Step 3: Factorize like terms. Thanks to the simplification order, those are - * next to each other at this point. */ - i = 0; - while (i < numberOfChildren()-1) { - ExpressionNode * e1 = child(i); - ExpressionNode * e2 = child(i+1); - if (e1->type() == Type::Float && e2->type() == Type::Float) { - float sum = e1->approximate() + e2->approximate(); - // Remove first e2 then e1, else the pointers change - removeChild(e2); - removeChild(e1); - FloatRef f(sum); - addChildAtIndex(f.node(), i); - continue; - } - /*if (TermsHaveIdenticalNonRationalFactors(e1, e2)) { //TODO - factorizeOperands(e1, e2); //TODO - continue; - }*/ - i++; - } - - return false; -} diff --git a/apps/tree/addition_node.h b/apps/tree/addition_node.h deleted file mode 100644 index 00edffee3..000000000 --- a/apps/tree/addition_node.h +++ /dev/null @@ -1,50 +0,0 @@ -#ifndef ADDITION_NODE_H -#define ADDITION_NODE_H - -#include "expression_reference.h" -#include "expression_node.h" - -class AdditionNode : public ExpressionNode { -public: - const char * description() const override { return "Addition"; } - size_t size() const override { return sizeof(AdditionNode); } - Type type() const override { return Type::Addition; } - - float approximate() override { - float result = 0.0f; - for (int i=0; iapproximate(); - if (approximateI == -1) { - return -1; - } - result += approximateI; - } - return result; - } - - bool shallowReduce() override; - int numberOfChildren() const override { return m_numberOfChildren; } - void incrementNumberOfChildren(int increment = 1) override { m_numberOfChildren+= increment; } - void decrementNumberOfChildren(int decrement = 1) override { - assert(m_numberOfChildren > 0); - m_numberOfChildren-= decrement; - } - void eraseNumberOfChildren() override { - m_numberOfChildren = 0; - } - -private: - int m_numberOfChildren; -}; - -class AdditionRef : public ExpressionReference { -public: - AdditionRef(ExpressionRef e1, ExpressionRef e2) : - ExpressionReference() - { - addChild(e2); - addChild(e1); - } -}; - -#endif diff --git a/apps/tree/allocation_failed_expression_node.h b/apps/tree/allocation_failed_expression_node.h deleted file mode 100644 index 03283957e..000000000 --- a/apps/tree/allocation_failed_expression_node.h +++ /dev/null @@ -1,26 +0,0 @@ -#ifndef ALLOCATION_FAILED_EXPRESSION_NODE_H -#define ALLOCATION_FAILED_EXPRESSION_NODE_H - -#include "expression_node.h" -#include "expression_reference.h" -#include - -class AllocationFailedExpressionNode : public ExpressionNode { -public: - // ExpressionNode - float approximate() override { return -1; } // Should return nan - - // TreeNode - size_t size() const override { return sizeof(AllocationFailedExpressionNode); } - const char * description() const override { return "Allocation Failed"; } - Type type() const override { return Type::AllocationFailure; } - int numberOfChildren() const override { return 0; } - bool isAllocationFailure() const override { return true; } -}; - -class AllocationFailedExpressionRef : public ExpressionReference { -public: - using ExpressionReference::ExpressionReference; -}; - -#endif diff --git a/apps/tree/expression_node.cpp b/apps/tree/expression_node.cpp deleted file mode 100644 index e392a95a6..000000000 --- a/apps/tree/expression_node.cpp +++ /dev/null @@ -1,11 +0,0 @@ -#include "expression_node.h" -#include "allocation_failed_expression_node.h" -#include "expression_reference.h" - -TreeNode * ExpressionNode::FailedAllocationStaticNode() { - return ExpressionRef::FailedAllocationStaticNode(); -} - -void ExpressionNode::sortChildren() { - ExpressionRef(this).sortChildren(); -} diff --git a/apps/tree/expression_node.h b/apps/tree/expression_node.h deleted file mode 100644 index 3a3e5b143..000000000 --- a/apps/tree/expression_node.h +++ /dev/null @@ -1,47 +0,0 @@ -#ifndef EXPRESSION_NODE_H -#define EXPRESSION_NODE_H - -#include -#include - -class ExpressionNode : public SerializableNode { -public: - enum class Type : uint8_t { - AllocationFailure = 0, - Float = 1, - Addition, - SimplificationRoot - }; - - // Expression - virtual Type type() const = 0; - virtual float approximate() = 0; - - void deepReduce() { - for (int i = 0; i < numberOfChildren(); i++) { - child(i)->deepReduce(); - } - shallowReduce(); - } - - virtual bool shallowReduce() { - for (int i = 0; i < numberOfChildren(); i++) { - if (child(i)->isAllocationFailure()) { - replaceWithAllocationFailure(); - return true; - } - } - return false; - } - - // Allocation failure - static TreeNode * FailedAllocationStaticNode(); - TreeNode * failedAllocationStaticNode() override { return FailedAllocationStaticNode(); } - - // Hierarchy - ExpressionNode * child(int i) { return static_cast(childAtIndex(i)); } -protected: - void sortChildren(); -}; - -#endif diff --git a/apps/tree/expression_reference.cpp b/apps/tree/expression_reference.cpp deleted file mode 100644 index e1fbefb11..000000000 --- a/apps/tree/expression_reference.cpp +++ /dev/null @@ -1,12 +0,0 @@ -#include "expression_reference.h" -#include "allocation_failed_expression_node.h" - -template<> -TreeNode * ExpressionRef::FailedAllocationStaticNode() { - static AllocationFailedExpressionNode FailureNode; - if (FailureNode.identifier() >= -1) { - int newIdentifier = TreePool::sharedPool()->registerStaticNode(&FailureNode); - FailureNode.rename(newIdentifier); - } - return &FailureNode; -} diff --git a/apps/tree/expression_reference.h b/apps/tree/expression_reference.h deleted file mode 100644 index 8a57a1884..000000000 --- a/apps/tree/expression_reference.h +++ /dev/null @@ -1,64 +0,0 @@ -#ifndef POINCARE_EXPRESSION_REFERENCE_H -#define POINCARE_EXPRESSION_REFERENCE_H - -#include -#include - -#include - -template -class ExpressionReference : public SerializableReference { -public: - using SerializableReference::SerializableReference; - - /* Allow every ExpressionReference to be transformed into an - * ExpressionReference, i.e. ExpressionRef */ - operator ExpressionReference() const { - return ExpressionReference(this->node()); - } - - static TreeNode * FailedAllocationStaticNode(); - - ExpressionReference childAtIndex(int i) { - return ExpressionReference(TreeReference::treeChildAtIndex(i).node()); - } - - void replaceChildAtIndex(int oldChildIndex, ExpressionReference newChild) { - TreeReference::replaceChildAtIndex(oldChildIndex, newChild); - } - - float approximate() const { - return this->node()->approximate(); - } - - ExpressionReference deepReduce() { - ExpressionReference result = ExpressionReference(this->clone().node()); - result.node()->deepReduce(); - return result; - } - - void shallowReduce() { - return this->node()->shallowReduce(); - } - - void sortChildren() { - for (int i = this->numberOfChildren()-1; i > 0; i--) { - bool isSorted = true; - for (int j = 0; j < this->numberOfChildren()-1; j++) { - if (this->childAtIndex(j).node()->type() > this->childAtIndex(j+1).node()->type()) { - this->swapChildren(j, j+1); - isSorted = false; - } - } - if (isSorted) { - return; - } - } - } - - -}; - -typedef ExpressionReference ExpressionRef; - -#endif diff --git a/apps/tree/float_node.h b/apps/tree/float_node.h deleted file mode 100644 index 42bf7a132..000000000 --- a/apps/tree/float_node.h +++ /dev/null @@ -1,34 +0,0 @@ -#ifndef FLOAT_NODE_H -#define FLOAT_NODE_H - -#include "expression_reference.h" -#include "expression_node.h" - -class FloatNode : public ExpressionNode { -public: - FloatNode(float value = 0) : - ExpressionNode(), - m_value(value) - {} - size_t size() const override { return sizeof(FloatNode); } - Type type() const override { return Type::Float; } - int numberOfChildren() const override { return 0; } - float approximate() override { return m_value; } - const char * description() const override { - return m_value > 1 ? "BigFloat" : "SmallFloat"; - } - void setFloat(float f) { m_value = f; } -private: - float m_value; -}; - -class FloatRef : public ExpressionReference { -public: - FloatRef(float f) : ExpressionReference() { - if (!(this->node()->isAllocationFailure())) { - this->node()->setFloat(f); - } - } -}; - -#endif diff --git a/apps/tree/refs.h b/apps/tree/refs.h deleted file mode 100644 index 7135baceb..000000000 --- a/apps/tree/refs.h +++ /dev/null @@ -1,8 +0,0 @@ -#include "addition_node.h" -#include "allocation_failed_expression_node.h" -#include "float_node.h" - -#include "char_layout_node.h" -#include "horizontal_layout_node.h" - -#include "layout_cursor.h" diff --git a/apps/tree/test.cpp b/apps/tree/test.cpp deleted file mode 100644 index f1af21a02..000000000 --- a/apps/tree/test.cpp +++ /dev/null @@ -1,341 +0,0 @@ -#include "refs.h" -#include - -namespace Test { - -void logPool() { - TreePool::sharedPool()->log(); -} - -void assert_expression_approximates_to(ExpressionRef e, float result) { - float b = e.approximate(); - if (b > result) { - assert(b - result < 0.000001f); - } else { - assert(result - b < 0.000001f); - } -} - -AdditionRef buildAddition() { - int initialNumberOfNodes = TreePool::sharedPool()->numberOfNodes(); - FloatRef smallFloat(0.2f); - FloatRef bigFloat(3.4f); - AdditionRef a(smallFloat, bigFloat); - assert(TreePool::sharedPool()->numberOfNodes() == initialNumberOfNodes + 3); - return a; -} - -void testAddition() { -#if TREE_LOG - printf("Addition test\n"); -#endif - int initialNumberOfNodes = TreePool::sharedPool()->numberOfNodes(); - AdditionRef a = buildAddition(); - assert(TreePool::sharedPool()->numberOfNodes() == initialNumberOfNodes + 3); - - assert_expression_approximates_to(a, 3.6f); - - FloatRef smallFloat(1.3f); - a.replaceChildAtIndex(0, smallFloat); - - assert_expression_approximates_to(a, 4.7f); - - int firstChildIdentifier = a.childAtIndex(0).identifier(); - int secondChildIdentifier = a.childAtIndex(1).identifier(); - a.swapChildren(1,0); - assert(a.childAtIndex(0).identifier() == secondChildIdentifier); - assert(a.childAtIndex(1).identifier() == firstChildIdentifier); -} - -void createNodes() { - FloatRef smallFloat(0.2f); - FloatRef bigFloat(3.4f); - AdditionRef a(smallFloat, bigFloat); - ExpressionRef e = a; - ExpressionRef f = e; -} - -void testPoolEmpties() { -#if TREE_LOG - printf("Pool empties test\n"); -#endif - int initialNumberOfNodes = TreePool::sharedPool()->numberOfNodes(); - createNodes(); - assert(TreePool::sharedPool()->numberOfNodes() == initialNumberOfNodes); -} - -void testCursorCreateAndRetain() { -#if TREE_LOG - printf("Cursor create and retain test\n"); -#endif - int initialNumberOfNodes = TreePool::sharedPool()->numberOfNodes(); - CharLayoutRef aChar('a'); - CharLayoutRef bChar('b'); - int aCharID = aChar.identifier(); - int bCharID = bChar.identifier(); - assert(bCharID = aCharID + 1); - assert(aChar.nodeRetainCount() == 1); - assert(bChar.nodeRetainCount() == 1); - assert(strcmp(aChar.node()->description(), "Char a") == 0); - assert(strcmp(bChar.node()->description(), "Char b") == 0); - assert(TreePool::sharedPool()->numberOfNodes() == initialNumberOfNodes + 2); - - HorizontalLayoutRef h(aChar, bChar); - assert(aChar.identifier() == aCharID); - assert(bChar.identifier() == bCharID); - assert(h.identifier() == bCharID + 1); - assert(aChar.nodeRetainCount() == 2); - assert(bChar.nodeRetainCount() == 2); - assert(h.nodeRetainCount() == 1); - assert(aChar == h.childAtIndex(0)); - assert(bChar == h.childAtIndex(1)); - - LayoutCursor cursorA = aChar.cursor(); - assert(cursorA.layoutIdentifier() == aChar.identifier()); - assert(aChar.nodeRetainCount() == 3); -} - -void testCursorMoveLeft() { -#if TREE_LOG - printf("Cursor move left test\n"); -#endif - CharLayoutRef aChar('a'); - CharLayoutRef bChar('b'); - HorizontalLayoutRef h(aChar, bChar); - - LayoutCursor cursor = h.childAtIndex(1).cursor(); - assert(bChar.nodeRetainCount() == 3); - assert(cursor.layoutIdentifier() == h.childAtIndex(1).identifier()); - - bool recompute = false; - assert(cursor.layoutIdentifier() == bChar.identifier()); - assert(cursor.position() == LayoutCursor::Position::Right); - cursor.moveLeft(&recompute); - assert(cursor.layoutIdentifier() == bChar.identifier()); - assert(cursor.position() == LayoutCursor::Position::Left); - assert(bChar.nodeRetainCount() == 3); - assert(aChar.nodeRetainCount() == 2); - cursor.moveLeft(&recompute); - assert(cursor.layoutIdentifier() == aChar.identifier()); - assert(cursor.position() == LayoutCursor::Position::Left); - assert(aChar.nodeRetainCount() == 3); -} - -void testPoolExpressionAllocationFail() { -#if TREE_LOG - printf("Pool expression allocation fail test\n"); -#endif - - // Fill the pool for size 256 - FloatRef f1(0.0f); - FloatRef f2(1.0f); - AdditionRef a1(f1, f2); - - assert_expression_approximates_to(a1, 1); - - FloatRef f3(2.0f); - FloatRef f4(3.0f); - FloatRef f5(4.0f); - FloatRef f6(5.0f); - FloatRef f7(6.0f); - FloatRef f8(7.0f); - FloatRef f9(8.0f); - - // Allocation fail - FloatRef f11(10.0f); - AdditionRef a(f11, f3); - - assert_expression_approximates_to(a, -1); - - f1.replaceWith(f11); - - assert_expression_approximates_to(a1, -1); -} - -void testPoolExpressionAllocationFail2() { -#if TREE_LOG - printf("Pool expression allocation multiple fail test\n"); -#endif - - // Fill the pool for size 256 - FloatRef f1(0.0f); - FloatRef f2(1.0f); - AdditionRef a1(f1, f2); - - assert_expression_approximates_to(a1, 1); - - FloatRef f3(2.0f); - FloatRef f4(3.0f); - AdditionRef a2(f3, f4); - - assert_expression_approximates_to(a2, 5); - - FloatRef f5(4.0f); - FloatRef f6(5.0f); - FloatRef f7(6.0f); - FloatRef f8(7.0f); - // Allocation fail - FloatRef f9(8.0f); - FloatRef f10(8.0f); - - f1.replaceWith(f9); - - assert_expression_approximates_to(a1, -1); - - f3.replaceWith(f10); - - assert_expression_approximates_to(a2, -1); - assert_expression_approximates_to(a1, -1); -} - -void testPoolExpressionAllocationFailOnImbricatedAdditions() { -#if TREE_LOG - printf("Pool expression allocation fail second test\n"); -#endif - - // Fill the pool for size 256 - FloatRef f1(0.0f); - FloatRef f2(1.0f); - AdditionRef a1(f1, f2); - - assert_expression_approximates_to(a1, 1); - - FloatRef f3(2.0f); - AdditionRef a2(a1, f3); - - assert_expression_approximates_to(a2, 3); - - FloatRef f4(3.0f); - FloatRef f5(4.0f); - FloatRef f6(5.0f); - FloatRef f7(6.0f); - FloatRef f8(7.0f); - // Allocation fail - FloatRef f9(7.0f); - f1.replaceWith(f9); - - assert_expression_approximates_to(a2, -1); - - a2.removeChild(a1); - - assert_expression_approximates_to(a2, 2); -} - -void testStealOperand() { -#if TREE_LOG - printf("Steal operand test\n"); -#endif - - FloatRef f1(0.0f); - FloatRef f2(1.0f); - AdditionRef a1(f1, f2); - - assert_expression_approximates_to(a1, 1); - - FloatRef f3(2.0f); - FloatRef f4(3.0f); - AdditionRef a2(f4, f3); - - assert_expression_approximates_to(a2, 5); - - a1.addChild(f4); - - assert_expression_approximates_to(a1, 4); - assert_expression_approximates_to(a2, 2); -} - -void testSimplify() { -#if TREE_LOG - printf("Simplify test\n"); -#endif - AdditionRef a( - AdditionRef( - FloatRef(0.0f), - FloatRef(1.0f)), - FloatRef(2.0f)); - - assert_expression_approximates_to(a, 3); - ExpressionRef b = a.deepReduce(); - assert_expression_approximates_to(a, 3); - assert_expression_approximates_to(b, 3); - assert(b.numberOfChildren() == 1); - assert(b.childAtIndex(0).node()->type() == ExpressionNode::Type::Float); - assert(b.childAtIndex(0).node()->approximate() == 3.0f); -} - -void testChildSort() { -#if TREE_LOG - printf("Child sort test\n"); -#endif - - AdditionRef a( - AdditionRef( - FloatRef(1.0f), - FloatRef(2.0f)), - FloatRef(3.0f)); - a.addChild(FloatRef(0.0f)); - - assert(a.childAtIndex(0).node()->type() == ExpressionNode::Type::Float); - assert(a.childAtIndex(1).node()->type() == ExpressionNode::Type::Addition); - assert(a.childAtIndex(2).node()->type() == ExpressionNode::Type::Float); - - a.sortChildren(); - - assert(a.childAtIndex(0).node()->type() == ExpressionNode::Type::Float); - assert(a.childAtIndex(1).node()->type() == ExpressionNode::Type::Float); - assert(a.childAtIndex(2).node()->type() == ExpressionNode::Type::Addition); -} - - -void testPoolLayoutAllocationFail() { -#if TREE_LOG - printf("Pool layout allocation fail test\n"); -#endif - - // Fill the pool for size 256 - CharLayoutRef char1('a'); - CharLayoutRef char2('b'); - CharLayoutRef char3('a'); - CharLayoutRef char4('b'); - CharLayoutRef char5('a'); - CharLayoutRef char6('b'); - CharLayoutRef char7('a'); - CharLayoutRef char8('b'); - CharLayoutRef char9('a'); - CharLayoutRef char10('b'); - - // Allocation fail - CharLayoutRef char11('a'); - assert(strcmp(char11.node()->description(), "Allocation Failed") == 0); -} - -typedef void (test)(); -void runTest(test t) { - int initialNumberOfNodes = TreePool::sharedPool()->numberOfNodes(); - t(); - assert(TreePool::sharedPool()->numberOfNodes() == initialNumberOfNodes); -} - -int main() { -#if TREE_LOG - printf("\n*******************\nStart running tests\n*******************\n\n"); -#endif - assert(TreePool::sharedPool()->numberOfNodes() == 0); - runTest(testAddition); - runTest(testPoolEmpties); - runTest(testCursorCreateAndRetain); - runTest(testCursorMoveLeft); - runTest(testPoolExpressionAllocationFail); - runTest(testPoolExpressionAllocationFail2); - runTest(testPoolExpressionAllocationFailOnImbricatedAdditions); - runTest(testStealOperand); - runTest(testSimplify); - runTest(testChildSort); - runTest(testPoolLayoutAllocationFail); -#if TREE_LOG - printf("\n*******************\nEnd of tests\n*******************\n\n"); -#endif - return 0; -} - -}