//该方法将不同行细分为:修改、删除、新增类型; private bool AddChanges( ArrayList report, int curDest, int nextDest, int curSource, int nextSource) { bool retval = false; int diffDest = nextDest - curDest;//下一个区域的索引减去当前区域索引(区域总共多少行?) int diffSource = nextSource - curSource; int minDiff = 0; if (diffDest > 0) { if (diffSource > 0) { minDiff = Math.Min(diffDest, diffSource); //curDest---新版本文件中区域块第一行的索引 //curSource --- 老版本文件中区域块第一行索引 //minDiff ---区域块中行数目 report.Add(DiffResultSpan.CreateReplace(curDest, curSource, minDiff)); //比如老版本10行,新版本5行;合并后10行不同,前5行是修改状态,后5行是新增状态 //minDiff = 5; if (diffDest > diffSource) { curDest += minDiff; report.Add(DiffResultSpan.CreateAddDestination(curDest, diffDest - diffSource)); } else//比如老版本3行,新版本5行;合并后5行不同,前3行是修改状态,后2行是删除状态 //minDiff = 3; { if (diffSource > diffDest) { curSource += minDiff; report.Add(DiffResultSpan.CreateDeleteSource(curSource, diffSource - diffDest)); } } } else //diffSource == 0表示老版本没有行,新版本有,表示新增行 { report.Add(DiffResultSpan.CreateAddDestination(curDest, diffDest)); } retval = true; } else { if (diffSource > 0) { report.Add(DiffResultSpan.CreateDeleteSource(curSource, diffSource)); retval = true; } } return(retval); }
public ArrayList DiffReport() { ArrayList retval = new ArrayList(); int dcount = _destination.Count(); int scount = _source.Count(); //Deal with the special case of empty files if (dcount == 0) { if (scount > 0) { retval.Add(DiffResultSpan.CreateDeleteSource(0, scount)); } return(retval); } else { if (scount == 0) { retval.Add(DiffResultSpan.CreateAddDestination(0, dcount)); return(retval); } } _matchList.Sort(); int curDest = 0; int curSource = 0; DiffResultSpan last = null; //Process each match record foreach (DiffResultSpan drs in _matchList) { if ((!AddChanges(retval, curDest, drs.DestIndex, curSource, drs.SourceIndex)) && (last != null)) { last.AddLength(drs.Length); } else { retval.Add(drs); } curDest = drs.DestIndex + drs.Length; curSource = drs.SourceIndex + drs.Length; last = drs; } //Process any tail end data AddChanges(retval, curDest, dcount, curSource, scount); return(retval); }
public DiffResultSpanCollection DiffReport() { DiffResultSpanCollection returnResultSpanCollection = new DiffResultSpanCollection(); int dcount = _dest.Count(); int scount = _source.Count(); //Deal with the special case of empty files if (dcount == 0) { if (scount > 0) { returnResultSpanCollection.Add(DiffResultSpan.CreateDeleteSource(0, scount)); } return(returnResultSpanCollection); } else { if (scount == 0) { returnResultSpanCollection.Add(DiffResultSpan.CreateAddDestination(0, dcount)); return(returnResultSpanCollection); } } _matchList.Sort(); int curDest = 0; int curSource = 0; DiffResultSpan lastResultSpan = null; //Process each match record foreach (DiffResultSpan resultSpan in _matchList) { if ((!AddChanges(returnResultSpanCollection, curDest, resultSpan.DestIndex, curSource, resultSpan.SourceIndex)) && (lastResultSpan != null)) { lastResultSpan.AddLength(resultSpan.Length); } else { returnResultSpanCollection.Add(resultSpan); } curDest = resultSpan.DestIndex + resultSpan.Length; curSource = resultSpan.SourceIndex + resultSpan.Length; lastResultSpan = resultSpan; } //Process any tail end data AddChanges(returnResultSpanCollection, curDest, dcount, curSource, scount); return(returnResultSpanCollection); }
//添加不同行的区域块,从第一行到最后一行,相邻的行状态相同为一个区域块 //DiffReport返回参数解释: //int DestIndex ---区域块第一行在新版本文件中的原来的索引 //SourceIndex ---区域块第一行在老版本文件中的原来的索引 //int Length ---当前区域块中的行的个数 //Status ---改区域块中所有行共同的状态(Add/Delete/Modify) public ArrayList DiffReport() { ArrayList resultList = new ArrayList();//返回值 int dcount = _dest.Count(); int scount = _source.Count(); //处理空文件 if (dcount == 0) { if (scount > 0) { resultList.Add(DiffResultSpan.CreateDeleteSource(0, scount)); } return(resultList); } else { if (scount == 0) { resultList.Add(DiffResultSpan.CreateAddDestination(0, dcount)); return(resultList); } } //_matchList添加了相同行和不同行区域块,但是不同行又分为:新增、删除、修改三个状态没有区分 _matchList.Sort(); int curNewIndex = 0; int curOldIndex = 0; DiffResultSpan last = null; //Process each match record foreach (DiffResultSpan drs in _matchList) { if ((!AddChanges(resultList, curNewIndex, drs.DestIndex, curOldIndex, drs.SourceIndex)) && (last != null)) { last.AddLength(drs.Length); } else { resultList.Add(drs); } curNewIndex = drs.DestIndex + drs.Length;//新版本当前区域块第一行的索引。 curOldIndex = drs.SourceIndex + drs.Length; last = drs; } //Process any tail end data AddChanges(resultList, curNewIndex, dcount, curOldIndex, scount); return(resultList); }
private bool AddChanges( List <DiffResultSpan> report, int curDest, int nextDest, int curSource, int nextSource) { bool retval = false; int diffDest = nextDest - curDest; int diffSource = nextSource - curSource; int minDiff = 0; if (diffDest > 0) { if (diffSource > 0) { minDiff = Math.Min(diffDest, diffSource); report.Add(DiffResultSpan.CreateReplace(curDest, curSource, minDiff)); if (diffDest > diffSource) { curDest += minDiff; report.Add(DiffResultSpan.CreateAddDestination(curDest, diffDest - diffSource)); } else { if (diffSource > diffDest) { curSource += minDiff; report.Add(DiffResultSpan.CreateDeleteSource(curSource, diffSource - diffDest)); } } } else { report.Add(DiffResultSpan.CreateAddDestination(curDest, diffDest)); } retval = true; } else { if (diffSource > 0) { report.Add(DiffResultSpan.CreateDeleteSource(curSource, diffSource)); retval = true; } } return(retval); }