Skip to content

Instantly share code, notes, and snippets.

@omerfarukz
Last active January 1, 2016 01:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save omerfarukz/8075841 to your computer and use it in GitHub Desktop.
Save omerfarukz/8075841 to your computer and use it in GitHub Desktop.
GenericDiffrenceDetector
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);
/// 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;
}
}
}
@yemrekeskin
Copy link

this's cool code, dude :D

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment