-
Notifications
You must be signed in to change notification settings - Fork 0
/
MatchedLineTree.cs
232 lines (190 loc) · 7.31 KB
/
MatchedLineTree.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
using System.Collections.Generic;
using System.Linq;
using RedBlack;
using DiffPatch;
using CountAccessor = RedBlack.RedBlackCountAccesor<PatchReviewer.MatchedLineNode>;
using System;
namespace PatchReviewer
{
public sealed class MatchedLineNode : RedBlackNode<MatchedLineNode>
{
public bool HasLeftLine { get; private set; }
public bool HasRightLine { get; private set; }
public int lineCount, leftLineCount, rightLineCount;
public bool SidesEqual { get; private set; }
public IList<LineRange> leftDiffRanges, rightDiffRanges;
public MatchedLineNode(bool hasLeftLine, bool hasRightLine) {
HasLeftLine = hasLeftLine;
HasRightLine = hasRightLine;
ChildrenChanged();
}
public override void ChildrenChanged() {
int _lineCount = 1;
int _leftLineCount = HasLeftLine ? 1 : 0;
int _rightLineCount = HasRightLine ? 1 : 0;
if (Left != null) {
_lineCount += Left.lineCount;
_leftLineCount += Left.leftLineCount;
_rightLineCount += Left.rightLineCount;
}
if (Right != null) {
_lineCount += Right.lineCount;
_leftLineCount += Right.leftLineCount;
_rightLineCount += Right.rightLineCount;
}
if (lineCount != _lineCount || leftLineCount != _leftLineCount || rightLineCount != _rightLineCount) {
lineCount = _lineCount;
rightLineCount = _rightLineCount;
leftLineCount = _leftLineCount;
Parent?.ChildrenChanged();
}
}
public int LineCount(bool r) => r ? rightLineCount : leftLineCount;
public bool HasLine(bool r) => r ? HasRightLine : HasLeftLine;
public void SetHasLine(bool r, bool has) {
if (r) HasRightLine = has;
else HasLeftLine = has;
leftDiffRanges = rightDiffRanges = null;
ChildrenChanged();
}
public void SetEqual() {
if (!HasRightLine || !HasLeftLine)
throw new ArgumentException("Cannot be equal without having lines for both sides");
SidesEqual = true;
leftDiffRanges = rightDiffRanges = null;
}
public void Compare(int[] matches, string wmLeft, string wmRight, CharRepresenter charRep) {
SidesEqual = false;
if (!HasRightLine || !HasLeftLine)
throw new ArgumentException("Cannot be compared without having lines for both sides");
if (matches.Count(i => i < 0) > matches.Length / 2f) { //not similar enough
leftDiffRanges = rightDiffRanges = null;
return;
}
leftDiffRanges = new List<LineRange>();
rightDiffRanges = new List<LineRange>();
int i1 = 0, i2 = 0;
int offset1 = 0, offset2 = 0;
foreach (var (range1, range2) in LineMatching.UnmatchedRanges(matches, wmRight.Length)) {
while (i1 < range1.start)
offset1 += charRep.GetWord(wmLeft[i1++]).Length;
while (i2 < range2.start)
offset2 += charRep.GetWord(wmRight[i2++]).Length;
int start1 = offset1, start2 = offset2;
while (i1 < range1.end)
offset1 += charRep.GetWord(wmLeft[i1++]).Length;
while (i2 < range2.end)
offset2 += charRep.GetWord(wmRight[i2++]).Length;
leftDiffRanges.Add(new LineRange { start = start1, end = offset1 });
rightDiffRanges.Add(new LineRange { start = start2, end = offset2 });
}
}
public IList<LineRange> DiffRanges(bool r) => r ? rightDiffRanges : leftDiffRanges;
public MatchedLineNode GetCopy => new MatchedLineNode(HasLeftLine, HasRightLine) {
leftDiffRanges = leftDiffRanges,
rightDiffRanges = rightDiffRanges,
SidesEqual = SidesEqual
};
}
public class MatchedLineTree : BaseRedBlackTree<MatchedLineNode>, IReadOnlyList<MatchedLineNode>
{
public class SideAccess
{
public readonly MatchedLineTree tree;
public readonly bool side;
public SideAccess(MatchedLineTree tree, bool side) {
this.tree = tree;
this.side = side;
}
public int Count => tree.Root?.LineCount(side) ?? 0;
public MatchedLineNode this[int i] =>
CountAccessor.GetByIndex(tree, i, n => n.LineCount(side));
public int IndexOf(MatchedLineNode node) =>
CountAccessor.IndexOf(tree, node, n => n.LineCount(side));
public int OppositeIndexOf(MatchedLineNode node) =>
CountAccessor.IndexOf(tree, node, n => n.LineCount(!side));
public int CombinedIndexOf(int lineNo) => lineNo == Count ? tree.Count : CombinedIndexOf(this[lineNo]);
public int CombinedIndexOf(MatchedLineNode node) =>
CountAccessor.IndexOf(tree, node, n => n.lineCount);
public void Recompare(MatchedLineNode node, string ourLine, string otherLine) {
if (ourLine == otherLine) {
node.SetEqual();
return;
}
var charRep = new CharRepresenter();
var wmLeft = charRep.WordsToChars(side ? otherLine : ourLine);
var wmRight = charRep.WordsToChars(side ? ourLine : otherLine);
var match = new PatienceMatch().Match(wmLeft, wmRight, charRep.MaxWordChar);
node.Compare(match, wmLeft, wmRight, charRep);
}
}
public readonly SideAccess leftAccessor;
public readonly SideAccess rightAccessor;
public MatchedLineTree(int[] matches, int rightLineCount) {
BuildFrom(ToNodes(matches, rightLineCount));
leftAccessor = new SideAccess(this, false);
rightAccessor = new SideAccess(this, true);
}
public SideAccess Access(bool side) => side ? rightAccessor : leftAccessor;
public int IndexOf(MatchedLineNode node) => CountAccessor.IndexOf(this, node, n => n.lineCount);
public MatchedLineNode this[int index] => CountAccessor.GetByIndex(this, index, n => n.lineCount);
public IEnumerable<MatchedLineNode> Slice(LineRange range) =>
range.length == 0 ? Enumerable.Empty<MatchedLineNode>() :
this[range.first].To(this[range.last]);
private static IReadOnlyList<MatchedLineNode> ToNodes(int[] matches, int rightLineCount) {
var list = new List<MatchedLineNode>(matches.Length);
int right = 0;
foreach (var m in matches) {
if (m == -1) {
list.Add(new MatchedLineNode(true, false));
continue;
}
while (right < m) {
list.Add(new MatchedLineNode(false, true));
right++;
}
list.Add(new MatchedLineNode(true, true));
right++;
}
while (right < rightLineCount){
list.Add(new MatchedLineNode(false, true));
right++;
}
return list;
}
private void CompareMatched(IReadOnlyList<string> wmLeft, IReadOnlyList<string> wmRight, CharRepresenter charRep) {
var matcher = new PatienceMatch();
int leftLineNo = 0, rightLineNo = 0;
foreach (var line in this) {
if (line.HasLeftLine && line.HasRightLine) {
string left = wmLeft[leftLineNo], right = wmRight[rightLineNo];
if (left == right)
line.SetEqual();
else {
var match = matcher.Match(left, right, charRep.MaxWordChar);
line.Compare(match, left, right, charRep);
}
}
if (line.HasLeftLine) leftLineNo++;
if (line.HasRightLine) rightLineNo++;
}
}
public static MatchedLineTree FromLines(IReadOnlyList<string> leftLines, IReadOnlyList<string> rightLines) {
var lmDiff = new LineMatchedDiffer { MinMatchScore = 0 };
var matchedLines = new MatchedLineTree(lmDiff.Match(leftLines, rightLines), rightLines.Count);
matchedLines.CompareMatched(lmDiff.WordModeLines1, lmDiff.WordModeLines2, lmDiff.charRep);
return matchedLines;
}
public static int[] ToMatches(IEnumerable<MatchedLineNode> nodes, int leftLength) {
var matches = Enumerable.Repeat(-1, leftLength).ToArray();
int i = 0, j = 0;
foreach (var node in nodes) {
if (node.SidesEqual)
matches[i] = j;
if (node.HasLeftLine) i++;
if (node.HasRightLine) j++;
}
return matches;
}
}
}