Continuing simplification

This commit is contained in:
Léa Saviot
2018-06-29 20:40:05 +02:00
parent 3135fff759
commit 958baee216
6 changed files with 50 additions and 1 deletions

View File

@@ -40,6 +40,9 @@ public:
i++;
}
// Step 2: Sort the operands
sortChildren();
return false;
}

View File

@@ -5,3 +5,7 @@
TreeNode * ExpressionNode::FailedAllocationStaticNode() {
return ExpressionRef::FailedAllocationStaticNode();
}
void ExpressionNode::sortChildren() {
ExpressionRef(this).sortChildren();
}

View File

@@ -46,6 +46,8 @@ public:
// Hierarchy
ExpressionNode * child(int i) { return static_cast<ExpressionNode *>(childTreeAtIndex(i)); }
protected:
void sortChildren();
};
#endif

View File

@@ -38,6 +38,23 @@ public:
void shallowReduce() {
return this->castedNode()->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).castedNode()->type() > this->childAtIndex(j+1).castedNode()->type()) {
this->swapChildren(j, j+1);
isSorted = false;
}
}
if (isSorted) {
return;
}
}
}
};
typedef ExpressionReference<ExpressionNode> ExpressionRef;

View File

@@ -239,6 +239,28 @@ void testSimplify() {
assert(a.numberOfChildren() == 3);
}
void testChildSort() {
printf("Child sort test\n");
AdditionRef a(
AdditionRef(
FloatRef(1.0f),
FloatRef(2.0f)),
FloatRef(3.0f));
a.addChild(FloatRef(0.0f));
assert(a.childAtIndex(0).castedNode()->type() == ExpressionNode::Type::Float);
assert(a.childAtIndex(1).castedNode()->type() == ExpressionNode::Type::Addition);
assert(a.childAtIndex(2).castedNode()->type() == ExpressionNode::Type::Float);
a.sortChildren();
assert(a.childAtIndex(0).castedNode()->type() == ExpressionNode::Type::Float);
assert(a.childAtIndex(1).castedNode()->type() == ExpressionNode::Type::Float);
assert(a.childAtIndex(2).castedNode()->type() == ExpressionNode::Type::Addition);
}
void testPoolLayoutAllocationFail() {
printf("Pool layout allocation fail test\n");
@@ -279,6 +301,7 @@ int main() {
runTest(testPoolExpressionAllocationFailOnImbricatedAdditions);
runTest(testStealOperand);
runTest(testSimplify);
runTest(testChildSort);
printf("\n*******************\nEnd of tests\n*******************\n\n");
return 0;
}

View File

@@ -13,7 +13,7 @@ template <typename T>
class TreeReference {
friend class TreeNode;
friend class AdditionNode;
friend class ExpressionNode;
friend class Cursor;
template <typename U>
friend class TreeReference;