public static int Difference <T>(T[] a, T[] b) { EfficientArray <T> a1 = new EfficientArray <T>(a); EfficientArray <T> b1 = new EfficientArray <T>(b); return(Difference(a1, b1)); }
public static int Difference <T>(EfficientArray <T> a, EfficientArray <T> b) { if (a.Length == 0) { return(b.Length); } if (b.Length == 0) { return(a.Length); } int[,] lcs = PositionAndLengthLcs(a, b); if (lcs[0, 1] == 0) {//no substrings return(Math.Max(a.Length, b.Length)); } EfficientArray <T> a1 = SubArray(a, 0, lcs[0, 0]); EfficientArray <T> b1 = SubArray(b, 0, lcs[1, 0]); int upperA = lcs[0, 0] + lcs[0, 1], upperB = lcs[1, 0] + lcs[1, 1]; EfficientArray <T> a2 = SubArray(a, upperA, a.Length - upperA); EfficientArray <T> b2 = SubArray(b, upperB, b.Length - upperB); return(Difference(a1, b1) + Difference(a2, b2)); }
public static int[,] PositionAndLengthLcs <T>(EfficientArray <T> a, EfficientArray <T> b) { var lengths = new int[a.Length, b.Length]; int greatestLength = 0; int[,] positionAndLengthLcs = new int[2, 2]; //position and length, for a and b for (int i = 0; i < a.Length; i++) { for (int j = 0; j < b.Length; j++) { if (a[i].Equals(b[j])) { lengths[i, j] = i == 0 || j == 0 ? 1 : lengths[i - 1, j - 1] + 1; if (lengths[i, j] > greatestLength) { greatestLength = lengths[i, j]; positionAndLengthLcs[0, 0] = i - greatestLength + 1; positionAndLengthLcs[0, 1] = greatestLength; positionAndLengthLcs[1, 0] = j - greatestLength + 1; positionAndLengthLcs[1, 1] = greatestLength; } } else { lengths[i, j] = 0; } } } return(positionAndLengthLcs); }
public static EfficientArray <T> SubArray <T>(this EfficientArray <T> data, int index, int length) { return(new EfficientArray <T>(data, index, length)); }
public EfficientArray(EfficientArray <T> array, int startIndex, int length) { this.array = array.array; this.startIndex = startIndex; this.length = length; }
public EfficientArray(EfficientArray <T> array) { this.array = array.array; startIndex = array.startIndex; length = array.length; }