Page 1 of 1

Moved lines detection

Posted: Thu Jun 19, 2008 11:29 am
by tostme
Hi,

Here is one more question about moved lines. I know detecting moved lines in source code is not a trivial algorithm, but I hope you can improve it a little.

In my case when I compare two source code files it detects most of moved lines okay, but there are two little problems:

1. If a continuous block of code with several methods (with empty lines between them) was moved, then Compare It detects every method as a separate move block. I would really like to see it as a single move block. That is if I moved 50 lines of code with three methods, then instead of one move block for 50 lines Compare It shows me three separate move blocks.

2. Some lines are not detected as moved. For example, if I moved a whole class down, then it could be that the beginning of the class and the first method are not detected as a moved block.

If you want I can provide my files.

Posted: Thu Jun 19, 2008 2:27 pm
by grigsoft
Detecting moved lines is not a problem itself, it just takes some time. Currently only full changes blocks are searched. Otherwise it would be necessary to search for each individual line + each combination of them, which would take time. Maybe there is a reason to add some depth level, similar to that of partial match, so it would be possible to choose if you want to spend more time and search for all moved blocks\lines.
Merging of moved blocks, separated with blank lines could be useful, I will try to implement this. Thank you for suggestion!

Posted: Fri Jun 20, 2008 12:29 am
by tostme
Here is an example. This whole method (and actually many more code above and below it) were moved (or basically the class and the file were renamed). I inserted "---" to show boundaries of detected move blocks. Ideally I would want to see it all as a single moved block. Lines starting with "xxx" are not detected as moved.

-------------------------
/// <summary>
/// This code was copied and modified from Rotor.
/// Performs a binary search without boxing elements of the array.
/// </summary>
public static int BinarySearch(int[] array, int index, int length, int value)
{
Debug.Assert(array != null);
-------------------------
xxx
-------------------------
int lb = array.GetLowerBound(0);
Debug.Assert((index >= lb) && (length >= 0));
-------------------------
xxx
-------------------------
Debug.Assert(array.Length - index >= length);
Debug.Assert(array.Rank == 1);
-------------------------
xxx
-------------------------
int lo = index;
int hi = index + length - 1;
-------------------------
xxx
-------------------------
xxx while (lo <= hi)
xxx {
xxx int i = (lo + hi) >> 1;
xxx
-------------------------
int v = array;
-------------------------
xxx
-------------------------
if (v == value) return i;
if (v < value)
{
lo = i + 1;
}
else
{
hi = i - 1;
}
}
-------------------------
xxx
xxx return ~lo;
xxx }
xxx


So as you can see I could be facing a hundred moves instead of where I would expect just one :)

Posted: Mon Jun 23, 2008 7:00 am
by grigsoft
Thank you! Yes, I understand the problem. I will try to fix that.