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