code cleanup in diff
This commit is contained in:
1 parent
ffeb86dd8c
commit
8be0c3e03e
1 file changed
+5
-98
@@ -63,7 +63,7 @@ namespace bts.utils {
|
||||
int[] downVector = new int[2 * MAX + 2];
|
||||
int[] upVector = new int[2 * MAX + 2];
|
||||
|
||||
LCS(leftData, 0, leftData.length, rightData, 0, rightData.length, downVector, upVector);
|
||||
lcs(leftData, 0, leftData.length, rightData, 0, rightData.length, downVector, upVector);
|
||||
return createDiffs(leftData, rightData);
|
||||
}
|
||||
|
||||
@@ -102,7 +102,8 @@ namespace bts.utils {
|
||||
}
|
||||
}
|
||||
|
||||
private static void LCS(DiffData dataLeft, int lowerLeft, int upperLeft, DiffData dataRight, int lowerRight, int upperRight, int[] downVector, int[] upVector) {
|
||||
// Longest common substring algorithm
|
||||
private static void lcs(DiffData dataLeft, int lowerLeft, int upperLeft, DiffData dataRight, int lowerRight, int upperRight, int[] downVector, int[] upVector) {
|
||||
// Fast walkthrough equal lines at the start
|
||||
while (lowerLeft < upperLeft &&
|
||||
lowerRight < upperRight &&
|
||||
@@ -136,8 +137,8 @@ namespace bts.utils {
|
||||
var smsrd = sms(dataLeft, lowerLeft, upperLeft, dataRight, lowerRight, upperRight, downVector, upVector);
|
||||
|
||||
// The path is from LowerX to (x,y) and (x,y) to UpperX
|
||||
LCS(dataLeft, lowerLeft, smsrd.x, dataRight, lowerRight, smsrd.y, downVector, upVector);
|
||||
LCS(dataLeft, smsrd.x, upperLeft, dataRight, smsrd.y, upperRight, downVector, upVector);
|
||||
lcs(dataLeft, lowerLeft, smsrd.x, dataRight, lowerRight, smsrd.y, downVector, upVector);
|
||||
lcs(dataLeft, smsrd.x, upperLeft, dataRight, smsrd.y, upperRight, downVector, upVector);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -228,100 +229,6 @@ namespace bts.utils {
|
||||
|
||||
throw new ApplicationException("Unexpected code path");
|
||||
}
|
||||
|
||||
static ShortestMiddleSnake SMS(
|
||||
DiffData dataLeft, int lowerLeft, int upperLeft,
|
||||
DiffData dataRight, int lowerRight, int upperRight,
|
||||
int[] downVector, int[] upVector) {
|
||||
|
||||
int MAX = dataLeft.length + dataRight.length + 1;
|
||||
|
||||
int downK = lowerLeft - lowerRight; // the k-line to start the forward search
|
||||
int upK = upperLeft - upperRight; // the k-line to start the reverse search
|
||||
|
||||
int delta = (upperLeft - lowerLeft) - (upperRight - lowerRight);
|
||||
bool delta_is_odd = isOdd(delta);
|
||||
bool delta_is_even = !delta_is_odd;
|
||||
|
||||
int downOffset = MAX - downK;
|
||||
int upOffset = MAX - upK;
|
||||
|
||||
int MaxD = ((upperLeft - lowerLeft + upperRight - lowerRight) / 2) + 1;
|
||||
|
||||
// init vectors
|
||||
downVector[downOffset + downK + 1] = lowerLeft;
|
||||
upVector[upOffset + upK - 1] = upperLeft;
|
||||
|
||||
for (int d = 0; d <= MaxD; ++d) {
|
||||
|
||||
// Extend the forward path.
|
||||
for (int k = downK - d; k <= downK + d; k += 2) {
|
||||
// find the only or better starting point
|
||||
int x, y;
|
||||
if (k == downK - d) {
|
||||
x = downVector[downOffset + k + 1]; // down
|
||||
}
|
||||
else {
|
||||
x = downVector[downOffset + k - 1] + 1; // a step to the right
|
||||
if ((k < downK + d) && (downVector[downOffset + k + 1] >= x)) {
|
||||
x = downVector[downOffset + k + 1]; // down
|
||||
}
|
||||
}
|
||||
y = x - k;
|
||||
|
||||
// find the end of the furthest reaching forward D-path in diagonal k.
|
||||
while ((x < upperLeft) && (y < upperRight) && (dataLeft.data[x] == dataRight.data[y])) {
|
||||
x++; y++;
|
||||
}
|
||||
downVector[downOffset + k] = x;
|
||||
|
||||
// overlap ?
|
||||
if (delta_is_odd && (upK - d < k) && (k < upK + d)) {
|
||||
if (upVector[upOffset + k] <= downVector[downOffset + k]) {
|
||||
return new ShortestMiddleSnake() {
|
||||
x = downVector[downOffset + k],
|
||||
y = downVector[downOffset + k] - k
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Extend the reverse path.
|
||||
for (int k = upK - d; k <= upK + d; k += 2) {
|
||||
|
||||
// find the only or better starting point
|
||||
int x, y;
|
||||
if (k == upK + d) {
|
||||
x = upVector[upOffset + k - 1]; // up
|
||||
}
|
||||
else {
|
||||
x = upVector[upOffset + k + 1] - 1; // left
|
||||
if ((k > upK - d) && (upVector[upOffset + k - 1] < x)) {
|
||||
x = upVector[upOffset + k - 1]; // up
|
||||
}
|
||||
}
|
||||
y = x - k;
|
||||
|
||||
while ((x > lowerLeft) && (y > lowerRight) && (dataLeft.data[x - 1] == dataRight.data[y - 1])) {
|
||||
x--; y--; // diagonal
|
||||
}
|
||||
upVector[upOffset + k] = x;
|
||||
|
||||
// overlap ?
|
||||
if (delta_is_even && (downK - d <= k) && (k <= downK + d)) {
|
||||
if (upVector[upOffset + k] <= downVector[downOffset + k]) {
|
||||
return new ShortestMiddleSnake() {
|
||||
x = downVector[downOffset + k],
|
||||
y = downVector[downOffset + k] - k
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
throw new ApplicationException("Unexpected code path");
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user