public static List <DiffLine> GetDiffLines(IEnumerable <string> lines) { List <DiffLine> personalDiff = new List <DiffLine>(); foreach (string line in lines) { personalDiff.Add(new DiffLine(line)); } var finalLine = new DiffLine(); finalLine.Remove = true; personalDiff.Add(finalLine); var topLine = new DiffLine(); topLine.Remove = true; personalDiff.Insert(0, topLine); return(personalDiff); }
public static DiffBranch GetBestDiffBranch(List <DiffLine> diffBase, IEnumerable <string> inputBase) { List <DiffLine> diffLines = new List <DiffLine>(diffBase); List <string> inputLines = new List <string>(inputBase); inputLines.Insert(0, string.Empty); inputLines.Add(string.Empty); DiffBranch bestBranch = null; List <DiffBranch> currentBranches = new List <DiffBranch>(); currentBranches.Add(new DiffBranch { diffIndexStart = 0, diffIndexEnd = 0, inputIndexStart = 0, inputIndexEnd = 0, cumulativeFounds = 1, diffIndexNext = 1, inputIndexNext = 1, }); int count = 0; while (currentBranches.Count > 0) { //const int branchIndex = 0; for (int branchIndex = currentBranches.Count - 1; branchIndex >= 0; branchIndex--) { count++; DiffBranch currentBranch = currentBranches[branchIndex]; currentBranches.RemoveAt(branchIndex); if (!currentBranch.BetterThan(bestBranch)) { continue; } int inputIndex = currentBranch.inputIndexNext; int diffIndexStart = currentBranch.diffIndexNext; bool foundInstantly = false; string input = inputLines[inputIndex]; for (int diffIndex = diffIndexStart; diffIndex < diffLines.Count; diffIndex++) { DiffLine diff = diffLines[diffIndex]; if (input == diff.Original) { foundInstantly = foundInstantly || diffIndex == diffIndexStart; int foundIndex = 1; for (; foundIndex < Math.Min(diffLines.Count - diffIndex, inputLines.Count - inputIndex); foundIndex++) { if (inputLines[inputIndex + foundIndex] != diffLines[diffIndex + foundIndex].Original) { break; } } bool isToEnd = diffIndex == diffLines.Count - 1; var newBranch = new DiffBranch { parent = currentBranch, diffIndexStart = diffIndexStart, diffIndexEnd = diffIndex, inputIndexStart = inputIndex, inputIndexEnd = isToEnd ? inputLines.Count - 1 : inputIndex, cumulativeDeletions = currentBranch.cumulativeDeletions + (diffIndex - diffIndexStart), cumulativeAdds = currentBranch.cumulativeAdds, cumulativeFounds = currentBranch.cumulativeFounds + foundIndex, diffIndexNext = diffIndex + foundIndex, inputIndexNext = isToEnd ? inputLines.Count : inputIndex + foundIndex, }; newBranch.cumulativeAdds += (newBranch.inputIndexEnd - newBranch.inputIndexStart); if (newBranch.BetterThan(bestBranch)) { if (newBranch.inputIndexNext == inputLines.Count) { bestBranch = newBranch; } else { currentBranches.Insert(branchIndex, newBranch); } } else { break; } } } if (!foundInstantly) { int nextIndex = inputLines.IndexOf(diffLines[diffIndexStart].Original, inputIndex); if (nextIndex != -1) { DiffBranch addBranch = new DiffBranch { parent = currentBranch, diffIndexStart = diffIndexStart, diffIndexEnd = diffIndexStart, inputIndexStart = inputIndex, inputIndexEnd = nextIndex, cumulativeDeletions = currentBranch.cumulativeDeletions, cumulativeAdds = currentBranch.cumulativeAdds + (nextIndex - inputIndex), cumulativeFounds = currentBranch.cumulativeFounds + 1, diffIndexNext = diffIndexStart + 1, inputIndexNext = nextIndex + 1, }; if (addBranch.BetterThan(bestBranch)) { if (addBranch.inputIndexNext == inputLines.Count) { bestBranch = addBranch; } else { currentBranches.Insert(branchIndex, addBranch); } } } else if (inputIndex != inputLines.Count - 1) { DiffBranch addBranch = new DiffBranch { parent = currentBranch, diffIndexStart = diffIndexStart, diffIndexEnd = diffIndexStart, inputIndexStart = inputIndex, inputIndexEnd = inputIndex + 1, cumulativeDeletions = currentBranch.cumulativeDeletions, cumulativeAdds = currentBranch.cumulativeAdds + 1, cumulativeFounds = currentBranch.cumulativeFounds, diffIndexNext = diffIndexStart, inputIndexNext = inputIndex + 1, }; if (addBranch.BetterThan(bestBranch)) { if (addBranch.inputIndexNext == inputLines.Count) { bestBranch = addBranch; } else { currentBranches.Insert(branchIndex, addBranch); } } } } } } if (Cmds.LogCmd.debugChatLogs) { Console.WriteLine($"\nTested {count} DiffBranches"); } return(bestBranch); }