mirror of
https://github.com/UpsilonNumworks/Upsilon.git
synced 2026-03-27 01:29:58 +01:00
Transform SimplifyAdditionMerge into SimplifyCommutativeMerge
Change-Id: Iec477f6de5d51479370916336699a4ac91680ab4
This commit is contained in:
@@ -25,7 +25,7 @@ objs += $(addprefix poincare/src/layout/,\
|
||||
)
|
||||
objs += $(addprefix poincare/src/simplify/,\
|
||||
simplify_addition_integer.o\
|
||||
simplify_addition_merge.o\
|
||||
simplify_commutative_merge.o\
|
||||
simplify_product_zero.o\
|
||||
)
|
||||
tests += $(addprefix poincare/test/,\
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
#include "simplify/simplify.h"
|
||||
#include "simplify/simplify_product_zero.h"
|
||||
#include "simplify/simplify_addition_integer.h"
|
||||
#include "simplify/simplify_addition_merge.h"
|
||||
#include "simplify/simplify_commutative_merge.h"
|
||||
extern "C" {
|
||||
#include <assert.h>
|
||||
}
|
||||
@@ -13,7 +13,7 @@ extern "C" {
|
||||
static expression_simplifier_t kSimplifiers[] = {
|
||||
&SimplifyProductZero,
|
||||
&SimplifyAdditionInteger,
|
||||
&SimplifyAdditionMerge,
|
||||
&SimplifyCommutativeMerge,
|
||||
nullptr
|
||||
};
|
||||
|
||||
|
||||
@@ -1,43 +0,0 @@
|
||||
#include "simplify_addition_merge.h"
|
||||
#include <poincare/addition.h>
|
||||
extern "C" {
|
||||
#include <stdlib.h>
|
||||
#include <assert.h>
|
||||
}
|
||||
|
||||
Expression * SimplifyAdditionMerge(Expression * e) {
|
||||
if (e->type() != Expression::Type::Addition) {
|
||||
return nullptr;
|
||||
}
|
||||
Addition * a = (Addition *)e;
|
||||
// Let's count how many elements we'll be able to pick up from our children
|
||||
int mergedNumberOfChildren = 0;
|
||||
for (int i=0; i<a->numberOfOperands(); i++) {
|
||||
Expression * operand = a->operand(i);
|
||||
if (operand->type() == Expression::Type::Addition) {
|
||||
mergedNumberOfChildren += operand->numberOfOperands();
|
||||
} else {
|
||||
mergedNumberOfChildren += 1;
|
||||
}
|
||||
}
|
||||
assert(mergedNumberOfChildren >= a->numberOfOperands());
|
||||
if (mergedNumberOfChildren == a->numberOfOperands()) {
|
||||
// Nothing to simplify here
|
||||
return nullptr;
|
||||
}
|
||||
Expression ** children = (Expression **)malloc(sizeof(Expression *) * mergedNumberOfChildren);
|
||||
int childrenIndex = 0;
|
||||
for (int i=0; i<a->numberOfOperands(); i++) {
|
||||
Expression * operand = a->operand(i);
|
||||
if (operand->type() == Expression::Type::Addition) {
|
||||
for (int j=0; j<operand->numberOfOperands(); j++) {
|
||||
children[childrenIndex++] = operand->operand(j);
|
||||
}
|
||||
} else {
|
||||
children[childrenIndex++] = operand;
|
||||
}
|
||||
}
|
||||
Addition * result = new Addition(children, mergedNumberOfChildren, true);
|
||||
free(children);
|
||||
return result;
|
||||
}
|
||||
@@ -1,16 +0,0 @@
|
||||
#ifndef POINCARE_SIMPLIFY_ADDITION_MERGE_H
|
||||
#define POINCARE_SIMPLIFY_ADDITION_MERGE_H
|
||||
|
||||
#include "simplify.h"
|
||||
|
||||
/*
|
||||
* + +
|
||||
* / \ / | \
|
||||
* a + -> a c d
|
||||
* / \
|
||||
* c d
|
||||
*/
|
||||
|
||||
Expression * SimplifyAdditionMerge(Expression * e);
|
||||
|
||||
#endif
|
||||
55
poincare/src/simplify/simplify_commutative_merge.cpp
Normal file
55
poincare/src/simplify/simplify_commutative_merge.cpp
Normal file
@@ -0,0 +1,55 @@
|
||||
#include "simplify_commutative_merge.h"
|
||||
#include <poincare/addition.h>
|
||||
#include <poincare/product.h>
|
||||
extern "C" {
|
||||
#include <stdlib.h>
|
||||
#include <assert.h>
|
||||
}
|
||||
|
||||
Expression * SimplifyCommutativeMerge(Expression * e) {
|
||||
if (e->type() != Expression::Type::Addition &&
|
||||
e->type() != Expression::Type::Product) {
|
||||
return nullptr;
|
||||
}
|
||||
CommutativeOperation * c = (CommutativeOperation *)e;
|
||||
// Let's count how many elements we'll be able to pick up from our children
|
||||
int mergedNumberOfChildren = 0;
|
||||
for (int i=0; i<c->numberOfOperands(); i++) {
|
||||
Expression * operand = c->operand(i);
|
||||
if (operand->type() == c->type()) {
|
||||
mergedNumberOfChildren += operand->numberOfOperands();
|
||||
} else {
|
||||
mergedNumberOfChildren += 1;
|
||||
}
|
||||
}
|
||||
assert(mergedNumberOfChildren >= c->numberOfOperands());
|
||||
if (mergedNumberOfChildren == c->numberOfOperands()) {
|
||||
// Nothing to simplify here
|
||||
return nullptr;
|
||||
}
|
||||
Expression ** children = (Expression **)malloc(sizeof(Expression *) * mergedNumberOfChildren);
|
||||
int childrenIndex = 0;
|
||||
for (int i=0; i<c->numberOfOperands(); i++) {
|
||||
Expression * operand = c->operand(i);
|
||||
if (operand->type() == c->type()) {
|
||||
for (int j=0; j<operand->numberOfOperands(); j++) {
|
||||
children[childrenIndex++] = operand->operand(j);
|
||||
}
|
||||
} else {
|
||||
children[childrenIndex++] = operand;
|
||||
}
|
||||
}
|
||||
CommutativeOperation * result = nullptr;
|
||||
switch(e->type()) {
|
||||
case Expression::Type::Addition:
|
||||
result = new Addition(children, mergedNumberOfChildren, true);
|
||||
break;
|
||||
case Expression::Type::Product:
|
||||
result = new Product(children, mergedNumberOfChildren, true);
|
||||
break;
|
||||
default:
|
||||
assert(false);
|
||||
}
|
||||
free(children);
|
||||
return result;
|
||||
}
|
||||
16
poincare/src/simplify/simplify_commutative_merge.h
Normal file
16
poincare/src/simplify/simplify_commutative_merge.h
Normal file
@@ -0,0 +1,16 @@
|
||||
#ifndef POINCARE_SIMPLIFY_COMMUTATIVE_MERGE_H
|
||||
#define POINCARE_SIMPLIFY_COMMUTATIVE_MERGE_H
|
||||
|
||||
#include "simplify.h"
|
||||
|
||||
/*
|
||||
* + +
|
||||
* / \ / | \
|
||||
* a + -> a c d
|
||||
* / \
|
||||
* c d
|
||||
*/
|
||||
|
||||
Expression * SimplifyCommutativeMerge(Expression * e);
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user