using System.Collections.Generic;
namespace JM.LinqFaster
{
public static partial class LinqFaster
{
// -------------------------- LISTS --------------------------------------------
///
/// Removes duplicate elements from source, does not maintain order. Elements will be
/// sorted in ascending order.
///
/// The list to remove duplicate elements from.
/// Optional IEqualityComparer to compare values.
/// Optional IComparer to compare values.
public static void DistinctInPlaceF(this List source, IEqualityComparer eqComparer = null, IComparer comparer = null)
{
if (source == null)
{
throw Error.ArgumentNull("source");
}
if (comparer == null)
{
comparer = Comparer.Default;
}
if (eqComparer == null)
{
eqComparer = EqualityComparer.Default;
}
source.Sort(comparer);
TSource oldV = source[0];
int pos = 1;
for (int i = 1; i < source.Count; i++)
{
var newV = source[i];
source[pos] = newV;
if (!eqComparer.Equals(newV, oldV))
{
pos++;
}
oldV = newV;
}
source.RemoveRange(pos, source.Count - pos);
}
}
}