dynamic_layout_hierarchy.cpp
5.45 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#include <poincare/dynamic_layout_hierarchy.h>
#include "empty_layout.h"
#include <poincare/expression_layout_cursor.h>
extern "C" {
#include <assert.h>
#include <stdlib.h>
}
namespace Poincare {
DynamicLayoutHierarchy::DynamicLayoutHierarchy() :
ExpressionLayout(),
m_children(nullptr),
m_numberOfChildren(0)
{
}
DynamicLayoutHierarchy::DynamicLayoutHierarchy(const ExpressionLayout * const * children, int numberOfChildren, bool cloneChildren) :
ExpressionLayout(),
m_numberOfChildren(numberOfChildren)
{
assert(children != nullptr);
m_children = new const ExpressionLayout * [numberOfChildren];
for (int i = 0; i < numberOfChildren; i++) {
assert(children[i] != nullptr);
if (cloneChildren) {
m_children[i] = children[i]->clone();
} else {
m_children[i] = children[i];
}
const_cast<ExpressionLayout *>(m_children[i])->setParent(this);
}
}
DynamicLayoutHierarchy::~DynamicLayoutHierarchy() {
removeAndDeleteChildren();
}
void DynamicLayoutHierarchy::mergeChildrenAtIndex(DynamicLayoutHierarchy * eL, int index, bool removeEmptyChildren) {
int indexForInsertion = index;
int indexOfEL = indexOfChild(eL);
if (indexOfEL >= 0) {
removeChildAtIndex(indexOfEL, false);
if (indexOfEL < index) {
indexForInsertion--;
}
}
addChildrenAtIndex(eL->children(), eL->numberOfChildren(), indexForInsertion, removeEmptyChildren);
eL->removeDetachedChildren();
delete eL;
}
void DynamicLayoutHierarchy::addChildrenAtIndex(const ExpressionLayout * const * operands, int numberOfOperands, int indexForInsertion, bool removeEmptyChildren) {
assert(numberOfOperands > 0);
const ExpressionLayout ** newOperands = new const ExpressionLayout * [m_numberOfChildren+numberOfOperands];
int currentIndex = 0;
assert(indexForInsertion >= 0 && indexForInsertion <= m_numberOfChildren);
for (int i = 0; i < indexForInsertion; i++) {
newOperands[currentIndex++] = m_children[i];
}
for (int i = 0; i < numberOfOperands; i++) {
if (i == 0
&& operands[0]->isEmpty()
&& numberOfOperands > 1
&& operands[1]->mustHaveLeftSibling()
&& indexForInsertion > 0)
{
/* If the first added operand is Empty because its right sibling needs a
* left sibling, remove it if any previous child could be such a left
* sibling. */
continue;
}
if (!removeEmptyChildren
|| !operands[i]->isEmpty()
|| (i < numberOfOperands-1 && operands[i+1]->mustHaveLeftSibling()))
{
const_cast<ExpressionLayout *>(operands[i])->setParent(this);
newOperands[currentIndex++] = operands[i];
}
}
for (int i = indexForInsertion; i < m_numberOfChildren; i++) {
newOperands[currentIndex++] = m_children[i];
}
if (m_children != nullptr) {
delete[] m_children;
}
m_children = newOperands;
m_numberOfChildren = currentIndex;
}
bool DynamicLayoutHierarchy::addChildAtIndex(ExpressionLayout * child, int index) {
assert(index >= 0 && index <= m_numberOfChildren);
const ExpressionLayout ** newChildren = new const ExpressionLayout * [m_numberOfChildren+1];
int j = 0;
for (int i = 0; i <= m_numberOfChildren; i++) {
if (i == index) {
child->setParent(this);
newChildren[i] = child;
} else {
newChildren[i] = m_children[j++];
}
}
delete[] m_children;
m_children = newChildren;
m_numberOfChildren += 1;
return true;
}
void DynamicLayoutHierarchy::removeChildAtIndex(int index, bool deleteAfterRemoval) {
assert(index >= 0 && index < m_numberOfChildren);
if (deleteAfterRemoval) {
delete m_children[index];
} else {
const_cast<ExpressionLayout *>(m_children[index])->setParent(nullptr);
}
m_numberOfChildren--;
for (int j=index; j<m_numberOfChildren; j++) {
m_children[j] = m_children[j+1];
}
}
void DynamicLayoutHierarchy::removePointedChildAtIndexAndMoveCursor(int index, bool deleteAfterRemoval, ExpressionLayoutCursor * cursor) {
assert(index >= 0 && index < numberOfChildren());
assert(cursor->pointedExpressionLayout()->hasAncestor(child(index), true));
if (numberOfChildren() == 1) {
if (m_parent) {
if (!deleteAfterRemoval) {
detachChild(editableChild(0));
}
m_parent->removePointedChildAtIndexAndMoveCursor(m_parent->indexOfChild(this), true, cursor);
return;
}
removeChildAtIndex(index, deleteAfterRemoval);
cursor->setPointedExpressionLayout(this);
cursor->setPosition(ExpressionLayoutCursor::Position::Left);
return;
}
removeChildAtIndex(index, deleteAfterRemoval);
if (index < numberOfChildren()) {
cursor->setPointedExpressionLayout(editableChild(index));
cursor->setPosition(ExpressionLayoutCursor::Position::Left);
return;
}
int indexOfNewPointedLayout = index - 1;
assert(indexOfNewPointedLayout >= 0);
assert(indexOfNewPointedLayout < numberOfChildren());
cursor->setPointedExpressionLayout(editableChild(indexOfNewPointedLayout));
}
void DynamicLayoutHierarchy::removeAndDeleteChildren() {
if (m_children != nullptr) {
for (int i = 0; i < m_numberOfChildren; i++) {
if (m_children[i] != nullptr) {
delete m_children[i];
}
}
}
delete[] m_children;
m_children = nullptr;
m_numberOfChildren = 0;
}
void DynamicLayoutHierarchy::removeDetachedChildren() {
int currentIndex = 0;
for (int i = 0; i < m_numberOfChildren; i++) {
if (m_children[i] != nullptr && m_children[i]->parent() == this) {
m_children[currentIndex++] = m_children[i];
}
}
m_numberOfChildren = currentIndex;
}
}