Browse Source

optimized DocumentSequence

pull/15/head
Siegfried Pammer 15 years ago
parent
commit
944c2369d2
  1. 6
      src/AddIns/DisplayBindings/AvalonEdit.AddIn/Src/DefaultChangeWatcher.cs
  2. 25
      src/AddIns/DisplayBindings/AvalonEdit.AddIn/Src/MyersDiff/DocumentSequence.cs

6
src/AddIns/DisplayBindings/AvalonEdit.AddIn/Src/DefaultChangeWatcher.cs

@ -83,9 +83,11 @@ namespace ICSharpCode.AvalonEdit.AddIn
} else { } else {
changeList.Clear(); changeList.Clear();
Dictionary<string, int> hashes = new Dictionary<string, int>();
MyersDiff.MyersDiff diff = new MyersDiff.MyersDiff( MyersDiff.MyersDiff diff = new MyersDiff.MyersDiff(
new DocumentSequence(baseDocument), new DocumentSequence(baseDocument, hashes),
new DocumentSequence(document) new DocumentSequence(document, hashes)
); );
changeList.Add(new LineChangeInfo(ChangeType.None, "")); changeList.Add(new LineChangeInfo(ChangeType.None, ""));

25
src/AddIns/DisplayBindings/AvalonEdit.AddIn/Src/MyersDiff/DocumentSequence.cs

@ -9,35 +9,36 @@ namespace ICSharpCode.AvalonEdit.AddIn.MyersDiff
{ {
public class DocumentSequence : ISequence public class DocumentSequence : ISequence
{ {
IDocument document; int[] hashes;
List<int> hashCodes;
public DocumentSequence(IDocument document) public DocumentSequence(IDocument document, Dictionary<string, int> hashDict)
{ {
this.document = document; this.hashes = new int[document.TotalNumberOfLines];
this.hashCodes = new List<int>();
for (int i = 1; i <= document.TotalNumberOfLines; i++) { for (int i = 1; i <= document.TotalNumberOfLines; i++) {
hashCodes.Add(document.GetLine(i).Text.GetHashCode()); string text = document.GetLine(i).Text;
int hash;
if (!hashDict.TryGetValue(text, out hash)) {
hash = hashDict.Count;
hashDict.Add(text, hash);
}
hashes[i - 1] = hash;
} }
} }
public int Size() public int Size()
{ {
return document.TotalNumberOfLines; return hashes.Length;
} }
public bool Equals(int i, ISequence other, int j) public bool Equals(int thisIdx, ISequence other, int otherIdx)
{ {
DocumentSequence seq = other as DocumentSequence; DocumentSequence seq = other as DocumentSequence;
if (seq == null) if (seq == null)
return false; return false;
int thisLineHash = hashCodes[i]; return hashes[thisIdx] == seq.hashes[otherIdx];
int otherLineHash = seq.hashCodes[j];
return thisLineHash == otherLineHash;
} }
} }
} }

Loading…
Cancel
Save