Last active
January 1, 2016 01:58
-
-
Save omerfarukz/8075841 to your computer and use it in GitHub Desktop.
GenericDiffrenceDetector
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var diffDetactor = new GenericDiffrenceDetector<DepthloyFileInfo>(); | |
diffDetactor.CompareFunc = (Source, Destination) => { return Source.RelativePath.CompareTo(Destination.RelativePath); }; | |
diffDetactor.DetectChangesFunc = (Source, Destination) => { return Source.ContentHash == Destination.ContentHash; }; | |
diffDetactor.SortFunc = (Source) => { return Source.RelativePath; }; | |
var diffResult = diffDetactor.GetDiffrences(sourceList, destinationList); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// 2013-12-22 Omer Faruk ZORLU | |
/// http://zor.lu | |
namespace Depthloy.Common.Helpers | |
{ | |
using Depthloy.Common.Domain; | |
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
public class GenericDiffrenceDetector<T> | |
{ | |
public Func<T, object> SortFunc { get; set; } | |
public Func<T, T, int> CompareFunc { get; set; } | |
public Func<T, T, bool> DetectChangesFunc { get; set; } | |
public List<DiffrenceResult<T>> GetDiffrences(List<T> source, List<T> destination) | |
{ | |
//TODO argument exception | |
source = source.OrderBy(SortFunc).ToList(); | |
destination = destination.OrderBy(SortFunc).ToList(); | |
var diffrences = new List<DiffrenceResult<T>>(); | |
int sourceIndex = 0; | |
int destinationIndex = 0; | |
while (true) | |
{ | |
if (sourceIndex == source.Count && destinationIndex == destination.Count) | |
{ | |
break; | |
} | |
if (sourceIndex == source.Count) | |
{ | |
diffrences.Add(new DiffrenceResult<T>(destination[destinationIndex], FileTargets.WDELETE)); | |
destinationIndex += 1; | |
continue; | |
} | |
if (destinationIndex == destination.Count) | |
{ | |
diffrences.Add(new DiffrenceResult<T>(source[sourceIndex], FileTargets.WCREATE)); | |
sourceIndex += 1; | |
continue; | |
} | |
int compareResult = CompareFunc(source[sourceIndex], destination[destinationIndex]); | |
if (0 == compareResult) | |
{ | |
if (DetectChangesFunc(destination[destinationIndex], source[sourceIndex])) | |
{ | |
//IDENTICAL | |
} | |
else | |
{ | |
diffrences.Add(new DiffrenceResult<T>(destination[destinationIndex], FileTargets.WMERGE)); | |
} | |
sourceIndex += 1; | |
destinationIndex += 1; | |
} | |
else if (0 < compareResult) | |
{ | |
diffrences.Add(new DiffrenceResult<T>(destination[destinationIndex], FileTargets.WDELETE)); | |
destinationIndex += 1; | |
} | |
else if (0 > compareResult) | |
{ | |
diffrences.Add(new DiffrenceResult<T>(source[sourceIndex], FileTargets.WDELETE)); | |
sourceIndex += 1; | |
} | |
} | |
return diffrences; | |
} | |
} | |
public class DiffrenceResult<T> | |
{ | |
public T Value { get; set; } | |
public FileTargets Target { get; set; } | |
public DiffrenceResult(T value, FileTargets fileTarget) | |
{ | |
Value = value; | |
Target = fileTarget; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
this's cool code, dude :D