1 | // Copyright (c) 2014 AlphaSierraPapa for the SharpDevelop Team |
---|
2 | // |
---|
3 | // Permission is hereby granted, free of charge, to any person obtaining a copy of this |
---|
4 | // software and associated documentation files (the "Software"), to deal in the Software |
---|
5 | // without restriction, including without limitation the rights to use, copy, modify, merge, |
---|
6 | // publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons |
---|
7 | // to whom the Software is furnished to do so, subject to the following conditions: |
---|
8 | // |
---|
9 | // The above copyright notice and this permission notice shall be included in all copies or |
---|
10 | // substantial portions of the Software. |
---|
11 | // |
---|
12 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, |
---|
13 | // INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR |
---|
14 | // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE |
---|
15 | // FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR |
---|
16 | // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
---|
17 | // DEALINGS IN THE SOFTWARE. |
---|
18 | |
---|
19 | using System; |
---|
20 | |
---|
21 | namespace ICSharpCode.AvalonEdit.Document |
---|
22 | { |
---|
23 | /// <summary> |
---|
24 | /// A TextAnchorNode is placed in the TextAnchorTree. |
---|
25 | /// It describes a section of text with a text anchor at the end of the section. |
---|
26 | /// A weak reference is used to refer to the TextAnchor. (to save memory, we derive from WeakReference instead of referencing it) |
---|
27 | /// </summary> |
---|
28 | sealed class TextAnchorNode : WeakReference |
---|
29 | { |
---|
30 | internal TextAnchorNode left, right, parent; |
---|
31 | internal bool color; |
---|
32 | internal int length; |
---|
33 | internal int totalLength; // totalLength = length + left.totalLength + right.totalLength |
---|
34 | |
---|
35 | public TextAnchorNode(TextAnchor anchor) : base(anchor) |
---|
36 | { |
---|
37 | } |
---|
38 | |
---|
39 | internal TextAnchorNode LeftMost { |
---|
40 | get { |
---|
41 | TextAnchorNode node = this; |
---|
42 | while (node.left != null) |
---|
43 | node = node.left; |
---|
44 | return node; |
---|
45 | } |
---|
46 | } |
---|
47 | |
---|
48 | internal TextAnchorNode RightMost { |
---|
49 | get { |
---|
50 | TextAnchorNode node = this; |
---|
51 | while (node.right != null) |
---|
52 | node = node.right; |
---|
53 | return node; |
---|
54 | } |
---|
55 | } |
---|
56 | |
---|
57 | /// <summary> |
---|
58 | /// Gets the inorder successor of the node. |
---|
59 | /// </summary> |
---|
60 | internal TextAnchorNode Successor { |
---|
61 | get { |
---|
62 | if (right != null) { |
---|
63 | return right.LeftMost; |
---|
64 | } else { |
---|
65 | TextAnchorNode node = this; |
---|
66 | TextAnchorNode oldNode; |
---|
67 | do { |
---|
68 | oldNode = node; |
---|
69 | node = node.parent; |
---|
70 | // go up until we are coming out of a left subtree |
---|
71 | } while (node != null && node.right == oldNode); |
---|
72 | return node; |
---|
73 | } |
---|
74 | } |
---|
75 | } |
---|
76 | |
---|
77 | /// <summary> |
---|
78 | /// Gets the inorder predecessor of the node. |
---|
79 | /// </summary> |
---|
80 | internal TextAnchorNode Predecessor { |
---|
81 | get { |
---|
82 | if (left != null) { |
---|
83 | return left.RightMost; |
---|
84 | } else { |
---|
85 | TextAnchorNode node = this; |
---|
86 | TextAnchorNode oldNode; |
---|
87 | do { |
---|
88 | oldNode = node; |
---|
89 | node = node.parent; |
---|
90 | // go up until we are coming out of a right subtree |
---|
91 | } while (node != null && node.left == oldNode); |
---|
92 | return node; |
---|
93 | } |
---|
94 | } |
---|
95 | } |
---|
96 | |
---|
97 | public override string ToString() |
---|
98 | { |
---|
99 | return "[TextAnchorNode Length=" + length + " TotalLength=" + totalLength + " Target=" + Target + "]"; |
---|
100 | } |
---|
101 | } |
---|
102 | } |
---|