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 | using LineNode = DocumentLine; |
---|
24 | |
---|
25 | // A tree node in the document line tree. |
---|
26 | // For the purpose of the invariants, "children", "descendents", "siblings" etc. include the DocumentLine object, |
---|
27 | // it is treated as a third child node between left and right. |
---|
28 | |
---|
29 | // Originally, this was a separate class, with a reference to the documentLine. The documentLine had a reference |
---|
30 | // back to the node. To save memory, the same object is used for both the documentLine and the line node. |
---|
31 | // This saves 16 bytes per line (8 byte object overhead + two pointers). |
---|
32 | // sealed class LineNode |
---|
33 | // { |
---|
34 | // internal readonly DocumentLine documentLine; |
---|
35 | partial class DocumentLine |
---|
36 | { |
---|
37 | internal DocumentLine left, right, parent; |
---|
38 | internal bool color; |
---|
39 | // optimization note: I tried packing color and isDeleted into a single byte field, but that |
---|
40 | // actually increased the memory requirements. The JIT packs two bools and a byte (delimiterSize) |
---|
41 | // into a single DWORD, but two bytes get each their own DWORD. Three bytes end up in the same DWORD, so |
---|
42 | // apparently the JIT only optimizes for memory when there are at least three small fields. |
---|
43 | // Currently, DocumentLine takes 36 bytes on x86 (8 byte object overhead, 3 pointers, 3 ints, and another DWORD |
---|
44 | // for the small fields). |
---|
45 | // TODO: a possible optimization would be to combine 'totalLength' and the small fields into a single uint. |
---|
46 | // delimiterSize takes only two bits, the two bools take another two bits; so there's still |
---|
47 | // 28 bits left for totalLength. 268435455 characters per line should be enough for everyone :) |
---|
48 | |
---|
49 | /// <summary> |
---|
50 | /// Resets the line to enable its reuse after a document rebuild. |
---|
51 | /// </summary> |
---|
52 | internal void ResetLine() |
---|
53 | { |
---|
54 | totalLength = delimiterLength = 0; |
---|
55 | isDeleted = color = false; |
---|
56 | left = right = parent = null; |
---|
57 | } |
---|
58 | |
---|
59 | internal LineNode InitLineNode() |
---|
60 | { |
---|
61 | this.nodeTotalCount = 1; |
---|
62 | this.nodeTotalLength = this.TotalLength; |
---|
63 | return this; |
---|
64 | } |
---|
65 | |
---|
66 | internal LineNode LeftMost { |
---|
67 | get { |
---|
68 | LineNode node = this; |
---|
69 | while (node.left != null) |
---|
70 | node = node.left; |
---|
71 | return node; |
---|
72 | } |
---|
73 | } |
---|
74 | |
---|
75 | internal LineNode RightMost { |
---|
76 | get { |
---|
77 | LineNode node = this; |
---|
78 | while (node.right != null) |
---|
79 | node = node.right; |
---|
80 | return node; |
---|
81 | } |
---|
82 | } |
---|
83 | |
---|
84 | /// <summary> |
---|
85 | /// The number of lines in this node and its child nodes. |
---|
86 | /// Invariant: |
---|
87 | /// nodeTotalCount = 1 + left.nodeTotalCount + right.nodeTotalCount |
---|
88 | /// </summary> |
---|
89 | internal int nodeTotalCount; |
---|
90 | |
---|
91 | /// <summary> |
---|
92 | /// The total text length of this node and its child nodes. |
---|
93 | /// Invariant: |
---|
94 | /// nodeTotalLength = left.nodeTotalLength + documentLine.TotalLength + right.nodeTotalLength |
---|
95 | /// </summary> |
---|
96 | internal int nodeTotalLength; |
---|
97 | } |
---|
98 | } |
---|