Fossil SCM
Improvements to the longest-common-subsequence (LCS) function inside the diff engine.
Commit
477d1150cf95744223fd3cfe33fbbe996bfba486
Parent
1fcc6bda2fd9490…
1 file changed
+9
-8
+9
-8
| --- src/diff.c | ||
| +++ src/diff.c | ||
| @@ -1653,23 +1653,26 @@ | ||
| 1653 | 1653 | int iS1, int iE1, /* Range of lines in p->aFrom[] */ |
| 1654 | 1654 | int iS2, int iE2, /* Range of lines in p->aTo[] */ |
| 1655 | 1655 | int *piSX, int *piEX, /* Write p->aFrom[] common segment here */ |
| 1656 | 1656 | int *piSY, int *piEY /* Write p->aTo[] common segment here */ |
| 1657 | 1657 | ){ |
| 1658 | - double bestScore = -1e30; /* Best score seen so far */ | |
| 1659 | 1658 | int i, j, k; /* Loop counters */ |
| 1660 | 1659 | int n; /* Loop limit */ |
| 1661 | 1660 | DLine *pA, *pB; /* Pointers to lines */ |
| 1662 | 1661 | int iSX, iSY, iEX, iEY; /* Current match */ |
| 1663 | - double score; /* Current score */ | |
| 1664 | - int skew; /* How lopsided is the match */ | |
| 1665 | - int dist; /* Distance of match from center */ | |
| 1662 | + int skew = 0; /* How lopsided is the match */ | |
| 1663 | + int dist = 0; /* Distance of match from center */ | |
| 1666 | 1664 | int mid; /* Center of the span */ |
| 1667 | 1665 | int iSXb, iSYb, iEXb, iEYb; /* Best match so far */ |
| 1668 | 1666 | int iSXp, iSYp, iEXp, iEYp; /* Previous match */ |
| 1669 | - | |
| 1667 | + sqlite3_int64 bestScore; /* Best score so far */ | |
| 1668 | + sqlite3_int64 score; /* Score for current candidate LCS */ | |
| 1669 | + int span; /* combined width of the input sequences */ | |
| 1670 | 1670 | |
| 1671 | + span = (iE1 - iS1) + (iE2 - iS2); | |
| 1672 | + bestScore = -10000; | |
| 1673 | + score = 0; | |
| 1671 | 1674 | iSXb = iSXp = iS1; |
| 1672 | 1675 | iEXb = iEXp = iS1; |
| 1673 | 1676 | iSYb = iSYp = iS2; |
| 1674 | 1677 | iEYb = iEYp = iS2; |
| 1675 | 1678 | mid = (iE1 + iS1)/2; |
| @@ -1707,11 +1710,11 @@ | ||
| 1707 | 1710 | iEY += k; |
| 1708 | 1711 | skew = (iSX-iS1) - (iSY-iS2); |
| 1709 | 1712 | if( skew<0 ) skew = -skew; |
| 1710 | 1713 | dist = (iSX+iEX)/2 - mid; |
| 1711 | 1714 | if( dist<0 ) dist = -dist; |
| 1712 | - score = (iEX - iSX) - 0.05*skew - 0.05*dist; | |
| 1715 | + score = (iEX - iSX)*(sqlite3_int64)span - (skew + dist); | |
| 1713 | 1716 | if( score>bestScore ){ |
| 1714 | 1717 | bestScore = score; |
| 1715 | 1718 | iSXb = iSX; |
| 1716 | 1719 | iSYb = iSY; |
| 1717 | 1720 | iEXb = iEX; |
| @@ -1731,12 +1734,10 @@ | ||
| 1731 | 1734 | *piSX = iSXb; |
| 1732 | 1735 | *piSY = iSYb; |
| 1733 | 1736 | *piEX = iEXb; |
| 1734 | 1737 | *piEY = iEYb; |
| 1735 | 1738 | } |
| 1736 | - /* printf("LCS(%d..%d/%d..%d) = %d..%d/%d..%d\n", | |
| 1737 | - iS1, iE1, iS2, iE2, *piSX, *piEX, *piSY, *piEY); */ | |
| 1738 | 1739 | } |
| 1739 | 1740 | |
| 1740 | 1741 | /* |
| 1741 | 1742 | ** Expand the size of aEdit[] array to hold at least nEdit elements. |
| 1742 | 1743 | */ |
| 1743 | 1744 |
| --- src/diff.c | |
| +++ src/diff.c | |
| @@ -1653,23 +1653,26 @@ | |
| 1653 | int iS1, int iE1, /* Range of lines in p->aFrom[] */ |
| 1654 | int iS2, int iE2, /* Range of lines in p->aTo[] */ |
| 1655 | int *piSX, int *piEX, /* Write p->aFrom[] common segment here */ |
| 1656 | int *piSY, int *piEY /* Write p->aTo[] common segment here */ |
| 1657 | ){ |
| 1658 | double bestScore = -1e30; /* Best score seen so far */ |
| 1659 | int i, j, k; /* Loop counters */ |
| 1660 | int n; /* Loop limit */ |
| 1661 | DLine *pA, *pB; /* Pointers to lines */ |
| 1662 | int iSX, iSY, iEX, iEY; /* Current match */ |
| 1663 | double score; /* Current score */ |
| 1664 | int skew; /* How lopsided is the match */ |
| 1665 | int dist; /* Distance of match from center */ |
| 1666 | int mid; /* Center of the span */ |
| 1667 | int iSXb, iSYb, iEXb, iEYb; /* Best match so far */ |
| 1668 | int iSXp, iSYp, iEXp, iEYp; /* Previous match */ |
| 1669 | |
| 1670 | |
| 1671 | iSXb = iSXp = iS1; |
| 1672 | iEXb = iEXp = iS1; |
| 1673 | iSYb = iSYp = iS2; |
| 1674 | iEYb = iEYp = iS2; |
| 1675 | mid = (iE1 + iS1)/2; |
| @@ -1707,11 +1710,11 @@ | |
| 1707 | iEY += k; |
| 1708 | skew = (iSX-iS1) - (iSY-iS2); |
| 1709 | if( skew<0 ) skew = -skew; |
| 1710 | dist = (iSX+iEX)/2 - mid; |
| 1711 | if( dist<0 ) dist = -dist; |
| 1712 | score = (iEX - iSX) - 0.05*skew - 0.05*dist; |
| 1713 | if( score>bestScore ){ |
| 1714 | bestScore = score; |
| 1715 | iSXb = iSX; |
| 1716 | iSYb = iSY; |
| 1717 | iEXb = iEX; |
| @@ -1731,12 +1734,10 @@ | |
| 1731 | *piSX = iSXb; |
| 1732 | *piSY = iSYb; |
| 1733 | *piEX = iEXb; |
| 1734 | *piEY = iEYb; |
| 1735 | } |
| 1736 | /* printf("LCS(%d..%d/%d..%d) = %d..%d/%d..%d\n", |
| 1737 | iS1, iE1, iS2, iE2, *piSX, *piEX, *piSY, *piEY); */ |
| 1738 | } |
| 1739 | |
| 1740 | /* |
| 1741 | ** Expand the size of aEdit[] array to hold at least nEdit elements. |
| 1742 | */ |
| 1743 |
| --- src/diff.c | |
| +++ src/diff.c | |
| @@ -1653,23 +1653,26 @@ | |
| 1653 | int iS1, int iE1, /* Range of lines in p->aFrom[] */ |
| 1654 | int iS2, int iE2, /* Range of lines in p->aTo[] */ |
| 1655 | int *piSX, int *piEX, /* Write p->aFrom[] common segment here */ |
| 1656 | int *piSY, int *piEY /* Write p->aTo[] common segment here */ |
| 1657 | ){ |
| 1658 | int i, j, k; /* Loop counters */ |
| 1659 | int n; /* Loop limit */ |
| 1660 | DLine *pA, *pB; /* Pointers to lines */ |
| 1661 | int iSX, iSY, iEX, iEY; /* Current match */ |
| 1662 | int skew = 0; /* How lopsided is the match */ |
| 1663 | int dist = 0; /* Distance of match from center */ |
| 1664 | int mid; /* Center of the span */ |
| 1665 | int iSXb, iSYb, iEXb, iEYb; /* Best match so far */ |
| 1666 | int iSXp, iSYp, iEXp, iEYp; /* Previous match */ |
| 1667 | sqlite3_int64 bestScore; /* Best score so far */ |
| 1668 | sqlite3_int64 score; /* Score for current candidate LCS */ |
| 1669 | int span; /* combined width of the input sequences */ |
| 1670 | |
| 1671 | span = (iE1 - iS1) + (iE2 - iS2); |
| 1672 | bestScore = -10000; |
| 1673 | score = 0; |
| 1674 | iSXb = iSXp = iS1; |
| 1675 | iEXb = iEXp = iS1; |
| 1676 | iSYb = iSYp = iS2; |
| 1677 | iEYb = iEYp = iS2; |
| 1678 | mid = (iE1 + iS1)/2; |
| @@ -1707,11 +1710,11 @@ | |
| 1710 | iEY += k; |
| 1711 | skew = (iSX-iS1) - (iSY-iS2); |
| 1712 | if( skew<0 ) skew = -skew; |
| 1713 | dist = (iSX+iEX)/2 - mid; |
| 1714 | if( dist<0 ) dist = -dist; |
| 1715 | score = (iEX - iSX)*(sqlite3_int64)span - (skew + dist); |
| 1716 | if( score>bestScore ){ |
| 1717 | bestScore = score; |
| 1718 | iSXb = iSX; |
| 1719 | iSYb = iSY; |
| 1720 | iEXb = iEX; |
| @@ -1731,12 +1734,10 @@ | |
| 1734 | *piSX = iSXb; |
| 1735 | *piSY = iSYb; |
| 1736 | *piEX = iEXb; |
| 1737 | *piEY = iEYb; |
| 1738 | } |
| 1739 | } |
| 1740 | |
| 1741 | /* |
| 1742 | ** Expand the size of aEdit[] array to hold at least nEdit elements. |
| 1743 | */ |
| 1744 |