mirror of
https://github.com/UpsilonNumworks/Upsilon.git
synced 2026-03-23 15:50:49 +01:00
Continuing simplification
This commit is contained in:
@@ -40,6 +40,9 @@ public:
|
||||
i++;
|
||||
}
|
||||
|
||||
// Step 2: Sort the operands
|
||||
sortChildren();
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
@@ -5,3 +5,7 @@
|
||||
TreeNode * ExpressionNode::FailedAllocationStaticNode() {
|
||||
return ExpressionRef::FailedAllocationStaticNode();
|
||||
}
|
||||
|
||||
void ExpressionNode::sortChildren() {
|
||||
ExpressionRef(this).sortChildren();
|
||||
}
|
||||
|
||||
@@ -46,6 +46,8 @@ public:
|
||||
|
||||
// Hierarchy
|
||||
ExpressionNode * child(int i) { return static_cast<ExpressionNode *>(childTreeAtIndex(i)); }
|
||||
protected:
|
||||
void sortChildren();
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
@@ -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;
|
||||
|
||||
23
test.cpp
23
test.cpp
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user