[apps] Remove dead development code

This commit is contained in:
Léa Saviot
2018-07-31 11:38:28 +02:00
parent 5767e4ffc2
commit e4596e9ecb
11 changed files with 0 additions and 652 deletions

View File

@@ -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\
)

View File

@@ -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;
}

View File

@@ -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; i<numberOfChildren(); i++) {
float approximateI = child(i)->approximate();
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<AdditionNode> {
public:
AdditionRef(ExpressionRef e1, ExpressionRef e2) :
ExpressionReference<AdditionNode>()
{
addChild(e2);
addChild(e1);
}
};
#endif

View File

@@ -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 <stdio.h>
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<AllocationFailedExpressionNode> {
public:
using ExpressionReference<AllocationFailedExpressionNode>::ExpressionReference;
};
#endif

View File

@@ -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();
}

View File

@@ -1,47 +0,0 @@
#ifndef EXPRESSION_NODE_H
#define EXPRESSION_NODE_H
#include <poincare/serializable_node.h>
#include <stdint.h>
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<ExpressionNode *>(childAtIndex(i)); }
protected:
void sortChildren();
};
#endif

View File

@@ -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;
}

View File

@@ -1,64 +0,0 @@
#ifndef POINCARE_EXPRESSION_REFERENCE_H
#define POINCARE_EXPRESSION_REFERENCE_H
#include <poincare/expression_node.h>
#include <poincare/serializable_reference.h>
#include <stdio.h>
template <class T>
class ExpressionReference : public SerializableReference<T> {
public:
using SerializableReference<T>::SerializableReference;
/* Allow every ExpressionReference<T> to be transformed into an
* ExpressionReference<ExpressionNode>, i.e. ExpressionRef */
operator ExpressionReference<ExpressionNode>() const {
return ExpressionReference<ExpressionNode>(this->node());
}
static TreeNode * FailedAllocationStaticNode();
ExpressionReference<ExpressionNode> childAtIndex(int i) {
return ExpressionReference<ExpressionNode>(TreeReference<T>::treeChildAtIndex(i).node());
}
void replaceChildAtIndex(int oldChildIndex, ExpressionReference<ExpressionNode> newChild) {
TreeReference<T>::replaceChildAtIndex(oldChildIndex, newChild);
}
float approximate() const {
return this->node()->approximate();
}
ExpressionReference<ExpressionNode> deepReduce() {
ExpressionReference<ExpressionNode> result = ExpressionReference<ExpressionNode>(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<ExpressionNode> ExpressionRef;
#endif

View File

@@ -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<FloatNode> {
public:
FloatRef(float f) : ExpressionReference<FloatNode>() {
if (!(this->node()->isAllocationFailure())) {
this->node()->setFloat(f);
}
}
};
#endif

View File

@@ -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"

View File

@@ -1,341 +0,0 @@
#include "refs.h"
#include <stdio.h>
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;
}
}