public List <Point> CalculatePath(DiffOption <T> option) { if (!seq1.Any()) { return(Enumerable.Range(0, seq2.Length + 1).Select(i => new Point(0, i)).ToList()); } if (!seq2.Any()) { return(Enumerable.Range(0, seq1.Length + 1).Select(i => new Point(i, 0)).ToList()); } this.option = option; BeginCalculatePath(); while (Next()) { } return(EndCalculatePath()); }
private static DiffStatus GetStatus <T>(Point current, Point prev, ref T[] array1, ref T[] array2, DiffOption <T> option) { if (current.X != prev.X && current.Y != prev.Y) { var equal = option.EqualityComparer != null?option.EqualityComparer.Equals(array1[prev.X - 1], (array2[prev.Y - 1])) : array1[prev.X - 1].Equals(array2[prev.Y - 1]); if (equal) { return(DiffStatus.Equal); } else { return(DiffStatus.Modified); } } else if (current.X != prev.X) { return(DiffStatus.Deleted); } else if (current.Y != prev.Y) { return(DiffStatus.Inserted); } else { throw new Exception(); } }
public static IEnumerable <DiffResult <T> > Diff <T>(IEnumerable <T> seq1, IEnumerable <T> seq2, DiffOption <T> option) { if (seq1 == null || seq2 == null || (!seq1.Any() && !seq2.Any())) { return(Enumerable.Empty <DiffResult <T> >()); } var editGrap = new EditGraph <T>(seq1, seq2); var waypoints = editGrap.CalculatePath(option); return(MakeResults <T>(waypoints, seq1, seq2, option)); }
private static IEnumerable <DiffResult <T> > MakeResults <T>(IEnumerable <Point> waypoints, IEnumerable <T> seq1, IEnumerable <T> seq2, DiffOption <T> option) { var array1 = seq1.ToArray(); var array2 = seq2.ToArray(); foreach (var pair in waypoints.MakePairsWithNext()) { var status = GetStatus(pair.Item1, pair.Item2, ref array1, ref array2, option); T obj1 = default(T); T obj2 = default(T); switch (status) { case DiffStatus.Equal: case DiffStatus.Modified: obj1 = array1[pair.Item2.X - 1]; obj2 = array2[pair.Item2.Y - 1]; break; case DiffStatus.Inserted: obj2 = array2[pair.Item2.Y - 1]; break; case DiffStatus.Deleted: obj1 = array1[pair.Item2.X - 1]; break; } yield return(new DiffResult <T>(obj1, obj2, status)); } }
public static IEnumerable <DiffResult <T> > OptimizedDiff <T>(IEnumerable <T> seq1, IEnumerable <T> seq2, DiffOption <T> option) { return(Diff(seq1, seq2, option).Optimize(option.EqualityComparer)); }