list_data.cpp
1.13 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
extern "C" {
#include <assert.h>
#include <stdlib.h>
}
#include <poincare/list_data.h>
namespace Poincare {
ListData::ListData(Expression * operand) :
m_numberOfOperands(1),
m_operands(new Expression*[1])
{
assert(operand != nullptr);
m_operands[0] = operand;
}
ListData::~ListData() {
for (int i=0; i<m_numberOfOperands; i++) {
if (m_operands[i]) {
delete m_operands[i];
}
}
delete[] m_operands;
}
void ListData::pushExpression(Expression * operand) {
Expression ** newOperands = new Expression *[m_numberOfOperands+1];
for (int i=0; i<m_numberOfOperands; i++) {
newOperands[i] = m_operands[i];
}
delete [] m_operands;
newOperands[m_numberOfOperands] = operand;
m_operands = newOperands;
m_numberOfOperands++;
}
int ListData::numberOfOperands() const {
return m_numberOfOperands;
}
Expression ** ListData::operands() const {
return m_operands;
}
const Expression * ListData::operand(int i) const {
assert(i >= 0);
assert(i < m_numberOfOperands);
return m_operands[i];
}
void ListData::detachOperands() {
for (int i = 0; i < m_numberOfOperands; i++) {
m_operands[i] = nullptr;
}
}
}