Blame view

build4/epsilon-master/poincare/src/list_data.cpp 1.21 KB
6663b6c9   adorian   projet complet av...
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
  extern "C" {
  #include <assert.h>
  #include <stdlib.h>
  }
  #include <poincare/list_data.h>
  
  namespace Poincare {
  
  ListData::ListData() :
    m_numberOfOperands(0),
    m_operands(new Expression*[0])
  {}
  
  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;
    }
  }
  
  }