private void GetLongestSourceMatch(DiffState curItem, int destIndex,int destEnd, int sourceStart,int sourceEnd) { int maxDestLength = (destEnd - destIndex) + 1; int curLength = 0; int curBestLength = 0; int curBestIndex = -1; int maxLength = 0; for (int sourceIndex = sourceStart; sourceIndex <= sourceEnd; sourceIndex++) { maxLength = Math.Min(maxDestLength,(sourceEnd - sourceIndex) + 1); if (maxLength <= curBestLength) { //No chance to find a longer one any more break; } curLength = GetSourceMatchLength(destIndex,sourceIndex,maxLength); if (curLength > curBestLength) { //This is the best match so far curBestIndex = sourceIndex; curBestLength = curLength; } //jump over the match sourceIndex += curBestLength; } //DiffState cur = _stateList.GetByIndex(destIndex); if (curBestIndex == -1) { curItem.SetNoMatch(); } else { curItem.SetMatch(curBestIndex, curBestLength); } }
private void GetLongestSourceMatch(DiffState curItem, int destIndex, int destEnd, int sourceStart, int sourceEnd) { int maxDestLength = (destEnd - destIndex) + 1; int curLength = 0; int curBestLength = 0; int curBestIndex = -1; int maxLength = 0; for (int sourceIndex = sourceStart; sourceIndex <= sourceEnd; sourceIndex++) { maxLength = Math.Min(maxDestLength, (sourceEnd - sourceIndex) + 1); if (maxLength <= curBestLength) { //No chance to find a longer one any more break; } curLength = GetSourceMatchLength(destIndex, sourceIndex, maxLength); if (curLength > curBestLength) { //This is the best match so far curBestIndex = sourceIndex; curBestLength = curLength; } //jump over the match sourceIndex += curBestLength; } //DiffState cur = _stateList.GetByIndex(destIndex); if (curBestIndex == -1) { curItem.SetNoMatch(); } else { curItem.SetMatch(curBestIndex, curBestLength); } }
public DiffState GetByIndex(int index) { #if USE_HASH_TABLE DiffState retval = (DiffState)_table[index]; if (retval == null) { retval = new DiffState(); _table.Add(index,retval); } #else DiffState retval = _array[index]; if (retval == null) { retval = new DiffState(); _array[index] = retval; } #endif return retval; }
private void ProcessRange(int destStart, int destEnd, int sourceStart, int sourceEnd) { int curBestIndex = -1; int curBestLength = -1; int maxPossibleDestLength = 0; DiffState curItem = null; DiffState bestItem = null; for (int destIndex = destStart; destIndex <= destEnd; destIndex++) { maxPossibleDestLength = (destEnd - destIndex) + 1; if (maxPossibleDestLength <= curBestLength) { //we won't find a longer one even if we looked break; } curItem = _diffStateList.GetByIndex(destIndex); if (!curItem.HasValidLength(sourceStart, sourceEnd, maxPossibleDestLength)) { //recalc new best length since it isn't valid or has never been done. GetLongestSourceMatch(curItem, destIndex, destEnd, sourceStart, sourceEnd); } if (curItem.Status == DiffStatus.Matched) { switch (_level) { case DiffEngineLevel.Fast: if (curItem.Length > curBestLength) { //this is longest match so far curBestIndex = destIndex; curBestLength = curItem.Length; bestItem = curItem; } //Jump over the match destIndex += curItem.Length - 1; break; case DiffEngineLevel.Medium: if (curItem.Length > curBestLength) { //this is longest match so far curBestIndex = destIndex; curBestLength = curItem.Length; bestItem = curItem; //Jump over the match destIndex += curItem.Length - 1; } break; default: if (curItem.Length > curBestLength) { //this is longest match so far curBestIndex = destIndex; curBestLength = curItem.Length; bestItem = curItem; } break; } } } if (curBestIndex < 0) { // there are no matches in this span } else { int sourceIndex = bestItem.StartIndex; _matchList.Add(DiffResultSpan.CreateNoChange(curBestIndex, sourceIndex, curBestLength)); if (destStart < curBestIndex && sourceStart < sourceIndex) { // Recursive call to process lower indexes ProcessRange(destStart, curBestIndex - 1, sourceStart, sourceIndex - 1); } int upperDestStart = curBestIndex + curBestLength; int upperSourceStart = sourceIndex + curBestLength; if (destEnd > upperDestStart && sourceEnd > upperSourceStart) { // Recursive call to process upper indexes ProcessRange(upperDestStart, destEnd, upperSourceStart, sourceEnd); } } }
private void GetLongestSourceMatch(DiffState curItem, int destIndex, int destEnd, int sourceStart, int sourceEnd) { int maxDestLength = (destEnd - destIndex) + 1;//新版本最大行数 int curLength = 0; int curBestLength = 0; int curBestIndex = -1; int maxLength = 0; for (int sourceIndex = sourceStart; sourceIndex <= sourceEnd; sourceIndex++) { maxLength = Math.Min(maxDestLength, (sourceEnd - sourceIndex) + 1); if (maxLength <= curBestLength) { //No chance to find a longer one any more break; } //比如当前新文件的当前行为第1行,1-9行相同Unchanged,第10行不匹配Different,返回 10-0 = 10 相同数目...... curLength = GetSourceMatchLength(destIndex, sourceIndex, maxLength); if (curLength > curBestLength) { //This is the best match so far curBestIndex = sourceIndex; curBestLength = curLength;//当前有10行匹配相同 } //jump over the match,跳出匹配项 sourceIndex += curBestLength; //将索引增加匹配的个数,比较下一行 } //设置当前行的状态: if (curBestIndex == -1)//如果从第一行找到最后一行,没有相同项目。设置每个行为不匹配状态 { curItem.SetNoMatch(); } else { curItem.SetMatch(curBestIndex, curBestLength); } }
private void ProcessRange(int destStart, int destEnd, int sourceStart, int sourceEnd) { int curBestIndex = -1; //当前匹配项 int curBestLength = -1; //当前匹配状态(匹配、不匹配) int maxPossibleDestLength = 0; //最大行数 DiffState curItem = null; DiffState bestItem = null; //循环新版本所有行 for (int destIndex = destStart; destIndex <= destEnd; destIndex++) { maxPossibleDestLength = (destEnd - destIndex) + 1; if (maxPossibleDestLength <= curBestLength) { break; } //循环新文件的当前行状态对象: curItem = _stateList.GetByIndex(destIndex); if (!curItem.HasValidLength(sourceStart, sourceEnd, maxPossibleDestLength)) { //如果装填不知道,返回true //设置当前行的状态 //recalc new best length since it isn't valid or has never been done. GetLongestSourceMatch(curItem, destIndex, destEnd, sourceStart, sourceEnd); } //如果新文件当前行和老文件匹配(相同) if (curItem.Status == DiffStatus.Matched) { switch (_level) { case DiffEngineLevel.FastImperfect: if (curItem.Length > curBestLength) { //this is longest match so far curBestIndex = destIndex; curBestLength = curItem.Length; bestItem = curItem; } //Jump over the match destIndex += curItem.Length - 1; //当前第9行不相同,从第9行开始往下比较下一个不匹配项 break; } } } if (curBestIndex < 0) { //we are done - there are no matches in this span } else //如果新文件当前行和老文件匹配(相同) { int sourceIndex = bestItem.StartIndex; _matchList.Add(DiffResultSpan.CreateNoChange(curBestIndex, sourceIndex, curBestLength)); if (destStart < curBestIndex) { //Still have more lower destination data if (sourceStart < sourceIndex) { //Still have more lower source data // Recursive call to process lower indexes ProcessRange(destStart, curBestIndex - 1, sourceStart, sourceIndex - 1); } } int upperDestStart = curBestIndex + curBestLength; int upperSourceStart = sourceIndex + curBestLength; if (destEnd > upperDestStart) { //we still have more upper dest data if (sourceEnd > upperSourceStart) { //set still have more upper source data // Recursive call to process upper indexes ProcessRange(upperDestStart, destEnd, upperSourceStart, sourceEnd); } } } }