This repository has been archived by the owner on Nov 12, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 60
/
Copy pathFlatListTreeNode.cs
402 lines (371 loc) · 12 KB
/
FlatListTreeNode.cs
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
using System;
using System.Collections.Generic;
using System.Diagnostics;
namespace ICSharpCode.TreeView
{
// This part of SharpTreeNode controls the 'flat list' data structure, which emulates
// a big flat list containing the whole tree; allowing access by visible index.
partial class SharpTreeNode
{
/// <summary>The parent in the flat list</summary>
internal SharpTreeNode listParent;
/// <summary>Left/right nodes in the flat list</summary>
SharpTreeNode left, right;
internal TreeFlattener treeFlattener;
/// <summary>Subtree height in the flat list tree</summary>
byte height = 1;
/// <summary>Length in the flat list, including children (children within the flat list). -1 = invalidated</summary>
int totalListLength = -1;
int Balance {
get { return Height(right) - Height(left); }
}
static int Height(SharpTreeNode node)
{
return node != null ? node.height : 0;
}
internal SharpTreeNode GetListRoot()
{
SharpTreeNode node = this;
while (node.listParent != null)
node = node.listParent;
return node;
}
#region Debugging
[Conditional("DEBUG")]
void CheckRootInvariants()
{
GetListRoot().CheckInvariants();
}
[Conditional("DATACONSISTENCYCHECK")]
void CheckInvariants()
{
Debug.Assert(left == null || left.listParent == this);
Debug.Assert(right == null || right.listParent == this);
Debug.Assert(height == 1 + Math.Max(Height(left), Height(right)));
Debug.Assert(Math.Abs(this.Balance) <= 1);
Debug.Assert(totalListLength == -1 || totalListLength == (left != null ? left.totalListLength : 0) + (isVisible ? 1 : 0) + (right != null ? right.totalListLength : 0));
if (left != null) left.CheckInvariants();
if (right != null) right.CheckInvariants();
}
[Conditional("DEBUG")]
static void DumpTree(SharpTreeNode node)
{
node.GetListRoot().DumpTree();
}
[Conditional("DEBUG")]
void DumpTree()
{
Debug.Indent();
if (left != null)
left.DumpTree();
Debug.Unindent();
Debug.WriteLine("{0}, totalListLength={1}, height={2}, Balance={3}, isVisible={4}", ToString(), totalListLength, height, Balance, isVisible);
Debug.Indent();
if (right != null)
right.DumpTree();
Debug.Unindent();
}
#endregion
#region GetNodeByVisibleIndex / GetVisibleIndexForNode
internal static SharpTreeNode GetNodeByVisibleIndex(SharpTreeNode root, int index)
{
root.GetTotalListLength(); // ensure all list lengths are calculated
Debug.Assert(index >= 0);
Debug.Assert(index < root.totalListLength);
SharpTreeNode node = root;
Debug.Assert(node != null);
while (true) {
if (node.left != null && index < node.left.totalListLength) {
Debug.Assert(node.left != null);
node = node.left;
} else {
if (node.left != null) {
index -= node.left.totalListLength;
}
if (node.isVisible) {
if (index == 0)
return node;
index--;
}
Debug.Assert(node.right != null);
node = node.right;
}
}
}
internal static int GetVisibleIndexForNode(SharpTreeNode node)
{
int index = node.left != null ? node.left.GetTotalListLength() : 0;
while (node.listParent != null) {
if (node == node.listParent.right) {
if (node.listParent.left != null)
index += node.listParent.left.GetTotalListLength();
if (node.listParent.isVisible)
index++;
}
node = node.listParent;
}
return index;
}
#endregion
#region Balancing
/// <summary>
/// Balances the subtree rooted in <paramref name="node"/> and recomputes the 'height' field.
/// This method assumes that the children of this node are already balanced and have an up-to-date 'height' value.
/// </summary>
/// <returns>The new root node</returns>
static SharpTreeNode Rebalance(SharpTreeNode node)
{
Debug.Assert(node.left == null || Math.Abs(node.left.Balance) <= 1);
Debug.Assert(node.right == null || Math.Abs(node.right.Balance) <= 1);
// Keep looping until it's balanced. Not sure if this is stricly required; this is based on
// the Rope code where node merging made this necessary.
while (Math.Abs(node.Balance) > 1) {
// AVL balancing
// note: because we don't care about the identity of concat nodes, this works a little different than usual
// tree rotations: in our implementation, the "this" node will stay at the top, only its children are rearranged
if (node.Balance > 1) {
if (node.right.Balance < 0) {
node.right = node.right.RotateRight();
}
node = node.RotateLeft();
// If 'node' was unbalanced by more than 2, we've shifted some of the inbalance to the left node; so rebalance that.
node.left = Rebalance(node.left);
} else if (node.Balance < -1) {
if (node.left.Balance > 0) {
node.left = node.left.RotateLeft();
}
node = node.RotateRight();
// If 'node' was unbalanced by more than 2, we've shifted some of the inbalance to the right node; so rebalance that.
node.right = Rebalance(node.right);
}
}
Debug.Assert(Math.Abs(node.Balance) <= 1);
node.height = (byte)(1 + Math.Max(Height(node.left), Height(node.right)));
node.totalListLength = -1; // mark for recalculation
// since balancing checks the whole tree up to the root, the whole path will get marked as invalid
return node;
}
internal int GetTotalListLength()
{
if (totalListLength >= 0)
return totalListLength;
int length = (isVisible ? 1 : 0);
if (left != null) {
length += left.GetTotalListLength();
}
if (right != null) {
length += right.GetTotalListLength();
}
return totalListLength = length;
}
SharpTreeNode RotateLeft()
{
/* Rotate tree to the left
*
* this right
* / \ / \
* A right ===> this C
* / \ / \
* B C A B
*/
SharpTreeNode b = right.left;
SharpTreeNode newTop = right;
if (b != null) b.listParent = this;
this.right = b;
newTop.left = this;
newTop.listParent = this.listParent;
this.listParent = newTop;
// rebalance the 'this' node - this is necessary in some bulk insertion cases:
newTop.left = Rebalance(this);
return newTop;
}
SharpTreeNode RotateRight()
{
/* Rotate tree to the right
*
* this left
* / \ / \
* left C ===> A this
* / \ / \
* A B B C
*/
SharpTreeNode b = left.right;
SharpTreeNode newTop = left;
if (b != null) b.listParent = this;
this.left = b;
newTop.right = this;
newTop.listParent = this.listParent;
this.listParent = newTop;
newTop.right = Rebalance(this);
return newTop;
}
static void RebalanceUntilRoot(SharpTreeNode pos)
{
while (pos.listParent != null) {
if (pos == pos.listParent.left) {
pos = pos.listParent.left = Rebalance(pos);
} else {
Debug.Assert(pos == pos.listParent.right);
pos = pos.listParent.right = Rebalance(pos);
}
pos = pos.listParent;
}
SharpTreeNode newRoot = Rebalance(pos);
if (newRoot != pos && pos.treeFlattener != null) {
Debug.Assert(newRoot.treeFlattener == null);
newRoot.treeFlattener = pos.treeFlattener;
pos.treeFlattener = null;
newRoot.treeFlattener.root = newRoot;
}
Debug.Assert(newRoot.listParent == null);
newRoot.CheckInvariants();
}
#endregion
#region Insertion
static void InsertNodeAfter(SharpTreeNode pos, SharpTreeNode newNode)
{
// newNode might be the model root of a whole subtree, so go to the list root of that subtree:
newNode = newNode.GetListRoot();
if (pos.right == null) {
pos.right = newNode;
newNode.listParent = pos;
} else {
// insert before pos.right's leftmost:
pos = pos.right;
while (pos.left != null)
pos = pos.left;
Debug.Assert(pos.left == null);
pos.left = newNode;
newNode.listParent = pos;
}
RebalanceUntilRoot(pos);
}
#endregion
#region Removal
void RemoveNodes(SharpTreeNode start, SharpTreeNode end)
{
// Removes all nodes from start to end (inclusive)
// All removed nodes will be reorganized in a separate tree, do not delete
// regions that don't belong together in the tree model!
List<SharpTreeNode> removedSubtrees = new List<SharpTreeNode>();
SharpTreeNode oldPos;
SharpTreeNode pos = start;
do {
// recalculate the endAncestors every time, because the tree might have been rebalanced
HashSet<SharpTreeNode> endAncestors = new HashSet<SharpTreeNode>();
for (SharpTreeNode tmp = end; tmp != null; tmp = tmp.listParent)
endAncestors.Add(tmp);
removedSubtrees.Add(pos);
if (!endAncestors.Contains(pos)) {
// we can remove pos' right subtree in a single step:
if (pos.right != null) {
removedSubtrees.Add(pos.right);
pos.right.listParent = null;
pos.right = null;
}
}
SharpTreeNode succ = pos.Successor();
DeleteNode(pos); // this will also rebalance out the deletion of the right subtree
oldPos = pos;
pos = succ;
} while (oldPos != end);
// merge back together the removed subtrees:
SharpTreeNode removed = removedSubtrees[0];
for (int i = 1; i < removedSubtrees.Count; i++) {
removed = ConcatTrees(removed, removedSubtrees[i]);
}
}
static SharpTreeNode ConcatTrees(SharpTreeNode first, SharpTreeNode second)
{
SharpTreeNode tmp = first;
while (tmp.right != null)
tmp = tmp.right;
InsertNodeAfter(tmp, second);
return tmp.GetListRoot();
}
SharpTreeNode Successor()
{
if (right != null) {
SharpTreeNode node = right;
while (node.left != null)
node = node.left;
return node;
} else {
SharpTreeNode node = this;
SharpTreeNode oldNode;
do {
oldNode = node;
node = node.listParent;
// loop while we are on the way up from the right part
} while (node != null && node.right == oldNode);
return node;
}
}
static void DeleteNode(SharpTreeNode node)
{
SharpTreeNode balancingNode;
if (node.left == null) {
balancingNode = node.listParent;
node.ReplaceWith(node.right);
node.right = null;
} else if (node.right == null) {
balancingNode = node.listParent;
node.ReplaceWith(node.left);
node.left = null;
} else {
SharpTreeNode tmp = node.right;
while (tmp.left != null)
tmp = tmp.left;
// First replace tmp with tmp.right
balancingNode = tmp.listParent;
tmp.ReplaceWith(tmp.right);
tmp.right = null;
Debug.Assert(tmp.left == null);
Debug.Assert(tmp.listParent == null);
// Now move node's children to tmp:
tmp.left = node.left; node.left = null;
tmp.right = node.right; node.right = null;
if (tmp.left != null) tmp.left.listParent = tmp;
if (tmp.right != null) tmp.right.listParent = tmp;
// Then replace node with tmp
node.ReplaceWith(tmp);
if (balancingNode == node)
balancingNode = tmp;
}
Debug.Assert(node.listParent == null);
Debug.Assert(node.left == null);
Debug.Assert(node.right == null);
node.height = 1;
node.totalListLength = -1;
if (balancingNode != null)
RebalanceUntilRoot(balancingNode);
}
void ReplaceWith(SharpTreeNode node)
{
if (listParent != null) {
if (listParent.left == this) {
listParent.left = node;
} else {
Debug.Assert(listParent.right == this);
listParent.right = node;
}
if (node != null)
node.listParent = listParent;
listParent = null;
} else {
// this was a root node
Debug.Assert(node != null); // cannot delete the only node in the tree
node.listParent = null;
if (treeFlattener != null) {
Debug.Assert(node.treeFlattener == null);
node.treeFlattener = this.treeFlattener;
this.treeFlattener = null;
node.treeFlattener.root = node;
}
}
}
#endregion
}
}