static_hierarchy.cpp
2.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include <poincare/static_hierarchy.h>
#include <poincare/expression_array.h>
extern "C" {
#include <assert.h>
}
namespace Poincare {
template<int T>
StaticHierarchy<T>::StaticHierarchy() :
Expression(),
m_operands{}
{
}
template<int T>
StaticHierarchy<T>::StaticHierarchy(const Expression * const * operands, bool cloneOperands) :
Expression()
{
build(operands, T, cloneOperands);
}
template<>
StaticHierarchy<1>::StaticHierarchy(const Expression * e, bool cloneOperands) :
StaticHierarchy((Expression **)&e, cloneOperands)
{
}
template<>
StaticHierarchy<2>::StaticHierarchy(const Expression * e1, const Expression * e2, bool cloneOperands) :
StaticHierarchy(ExpressionArray(e1, e2).array(), cloneOperands)
{
}
template<int T>
StaticHierarchy<T>::~StaticHierarchy() {
for (int i = 0; i < T; i++) {
if (m_operands[i] != nullptr) {
delete m_operands[i];
}
}
}
template<int T>
void StaticHierarchy<T>::setArgument(ListData * listData, int numberOfOperands, bool clone) {
build(listData->operands(), listData->numberOfOperands(), clone);
}
template<int T>
bool StaticHierarchy<T>::hasValidNumberOfOperands(int numberOfOperands) const {
return numberOfOperands == T;
}
template<int T>
void StaticHierarchy<T>::build(const Expression * const * operands, int numberOfOperands, bool cloneOperands) {
assert(operands != nullptr);
assert(numberOfOperands <= T);
for (int i=0; i < numberOfOperands; i++) {
assert(operands[i] != nullptr);
if (cloneOperands) {
m_operands[i] = operands[i]->clone();
} else {
m_operands[i] = operands[i];
}
const_cast<Expression *>(m_operands[i])->setParent(this);
}
}
template<int T>
int StaticHierarchy<T>::simplificationOrderSameType(const Expression * e, bool canBeInterrupted) const {
for (int i = 0; i < this->numberOfOperands(); i++) {
// The NULL node is the least node type.
if (e->numberOfOperands() <= i) {
return 1;
}
if (SimplificationOrder(this->operand(i), e->operand(i), canBeInterrupted) != 0) {
return SimplificationOrder(this->operand(i), e->operand(i), canBeInterrupted);
}
}
// The NULL node is the least node type.
if (e->numberOfOperands() > numberOfOperands()) {
return -1;
}
return 0;
}
template class Poincare::StaticHierarchy<0>;
template class Poincare::StaticHierarchy<1>;
template class Poincare::StaticHierarchy<2>;
template class Poincare::StaticHierarchy<3>;
}