-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathImmutableTextNode.cs
More file actions
188 lines (159 loc) · 4.56 KB
/
Copy pathImmutableTextNode.cs
File metadata and controls
188 lines (159 loc) · 4.56 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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
namespace PixUI.CodeEditor;
internal abstract class Node
{
public abstract int Length { get; }
public abstract char GetCharAt(int index);
#if __WEB__
public abstract void CopyTo(int srcOffset, Uint16Array dest, int count);
#else
public abstract void CopyTo(int srcOffset, Span<char> dest, int count);
#endif
public abstract Node SubNode(int start, int end);
public override string ToString()
{
#if __WEB__
throw new NotImplementedException();
#else
var len = Length;
var data = new char[len];
CopyTo(0, data.AsSpan(), len);
return new string(data);
#endif
}
public Node SubSequence(int start, int end)
{
return SubNode(start, end);
}
}
internal sealed class LeafNode : Node
{
#if __WEB__
public LeafNode(Uint16Array data)
{
_data = data;
}
private readonly Uint16Array _data;
public override int Length => _data.length;
#else
public LeafNode(char[] data)
{
_data = data;
}
private readonly char[] _data;
public override int Length => _data.Length;
#endif
public override char GetCharAt(int index) => _data[index];
#if __WEB__
public override void CopyTo(int srcOffset, Uint16Array dest, int count)
{
var src = _data.subarray(srcOffset, srcOffset + count);
dest.set(src);
}
#else
public override void CopyTo(int srcOffset, Span<char> dest, int count)
{
var src = _data.AsSpan(srcOffset, count);
src.CopyTo(dest);
}
#endif
public override Node SubNode(int start, int end)
{
if (start == 0 && end == Length)
return this;
#if __WEB__
var subArray = new Uint16Array(end - start);
subArray.set(_data.subarray(start, end));
return new LeafNode(subArray);
#else
var subArray = new char[end - start];
Array.Copy(_data, start, subArray, 0, subArray.Length);
return new LeafNode(subArray);
#endif
}
public override string ToString()
{
#if __WEB__
throw new Exception();
#else
return new string(_data);
#endif
}
}
internal sealed class CompositeNode : Node
{
public CompositeNode(Node head, Node tail)
{
_count = head.Length + tail.Length;
this.Head = head;
this.Tail = tail;
}
private readonly int _count;
internal readonly Node Head;
internal readonly Node Tail;
public override int Length => _count;
public override char GetCharAt(int index)
{
var headLength = Head.Length;
return index < headLength
? Head.GetCharAt(index)
: Tail.GetCharAt(index - headLength);
}
internal Node RotateRight()
{
// See: http://en.wikipedia.org/wiki/Tree_rotation
if (Head is CompositeNode p)
{
return new CompositeNode(p.Head, new CompositeNode(p.Tail, Tail));
}
return this; // Head not a composite, cannot rotate.
}
internal Node RotateLeft()
{
// See: http://en.wikipedia.org/wiki/Tree_rotation
if (Tail is CompositeNode q)
{
return new CompositeNode(new CompositeNode(Head, q.Head), q.Tail);
}
return this; // Tail not a composite, cannot rotate.
}
#if __WEB__
public override void CopyTo(int srcOffset, Uint16Array dest, int count)
#else
public override void CopyTo(int srcOffset, Span<char> dest, int count)
#endif
{
var cesure = Head.Length;
if (srcOffset + count <= cesure)
{
Head.CopyTo(srcOffset, dest, count);
}
else if (srcOffset >= cesure)
{
Tail.CopyTo(srcOffset - cesure, dest, count);
}
else
{
// Overlaps head and tail.
var headChunkSize = cesure - srcOffset;
Head.CopyTo(srcOffset, dest, headChunkSize);
#if __WEB__
Tail.CopyTo(0, dest.subarray(headChunkSize), count - headChunkSize);
#else
Tail.CopyTo(0, dest.Slice(headChunkSize), count - headChunkSize);
#endif
}
}
public override Node SubNode(int start, int end)
{
var cesure = Head.Length;
if (end <= cesure)
return Head.SubNode(start, end);
if (start >= cesure)
return Tail.SubNode(start - cesure, end - cesure);
if ((start == 0) && (end == _count))
return this;
// Overlaps head and tail.
return ImmutableText.ConcatNodes(Head.SubNode(start, cesure),
Tail.SubNode(0, end - cesure));
}
}