Computes the differences between two arbitrary lists.
|
Use this class to compare two arbitrary lists of data. Normally the lists will be of characters, to perform a character-level comparison, or of strings, to perform a line-by-line comparison, of two strings.
Any object type may be contained in the lists, and comparisons among objects in the lists will be made with the System.Object.Equals method of the objects. If the Equals method is overridden, the the System.Object.GetHashCode method must also be overridden so that any two objects considered equal always have the same hash code.
A user-supplied IComparer and IHashCodeProvider may be supplied to control how equality between elements in the list is determined.
The differences between the lists are computed when the Diff object is constructed. The differences themselves are represented as Algorithm.Diff.Diff+Hunk objects. To access the hunks, use a for-each over the Diff object. Each hunk represents a range of corresponding elements in the two lists.
| C# Example |
using Algorithm.Diff;
// Performs a line-by-line, case-sensitive, whitespace-ignoring comparison.
diff = new Diff(file1.Split('\n'), file2.Split('\n'), true, false);
// Compares the contents of two ILists, using the objects' own Equals() methods
ArrayList list1 = ...;
ArrayList list2 = ...;
diff = new Diff(list1, list2)
// Compares the contents of two ILists, using custom comparison and hash code generation functions
diff = new Diff(list1, list2, comparer, hashcoder)
foreach (Diff.Hunk hunk in diff) {
Console.WriteLine(hunk);
}
|
See Also: Inherited members from object.
| Compares two lists using a user-supplied comparer and hash code provider. |
| Performs a line-by-line comparison of two files. |
| Compares two lists of strings, i.e. performs a line-by-line comparison of two documents. |
| Left [read-only] | IList . The list used as the left document. |
| Right [read-only] | IList . The list used as the right document. |
|
CreatePatch
() Creates a Algorithm.Diff.Patch object for the diff. |
Compares two lists using a user-supplied comparer and hash code provider.
The Diff object will be initialized as the comparison between the two lists, using the comparison and hash code provider objects if supplied.
Both the comparison and hash code provider objects are optional, and if one is provided it is not strictly necessary to provide the other. However, it must be the case that when two objects are considered equal (either by their own Equals method or by the user-given comparer), then they must have the same hash code. It is generally the case that when a comparer object is provided, a hash code provider must be given as well.
Comparer objects are supposed to return -1, 0, or 1 depending on whether the first object is "less" than the second, equal, or greater. The Diff class is not concerned with the ordering of objects, so a comparer used here can return either 1 or -1 when the two objects being compared are not equal.
Performs a line-by-line comparison of two files.
Compares two lists of strings, i.e. performs a line-by-line comparison of two documents.
Creates a Algorithm.Diff.Patch object for the diff.
The list used as the left document.
The list used as the right document.