mirror of
https://github.com/ICDSystems/ICD.Common.Utils.git
synced 2026-02-10 10:15:04 +00:00
feat: Added IsDistinct extension method
This commit is contained in:
@@ -966,6 +966,52 @@ namespace ICD.Common.Utils.Extensions
|
||||
return output;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns true if all of the items in the sequence are equal, or the sequence is empty.
|
||||
/// </summary>
|
||||
/// <typeparam name="TItem"></typeparam>
|
||||
/// <param name="extends"></param>
|
||||
/// <returns></returns>
|
||||
public static bool IsDistinct<TItem>([NotNull] this IEnumerable<TItem> extends)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
|
||||
IEqualityComparer<TItem> comparer = EqualityComparer<TItem>.Default;
|
||||
return extends.IsDistinct(comparer);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns true if all of the items in the sequence are equal, or the sequence is empty.
|
||||
/// </summary>
|
||||
/// <typeparam name="TItem"></typeparam>
|
||||
/// <param name="extends"></param>
|
||||
/// <param name="comparer"></param>
|
||||
/// <returns></returns>
|
||||
public static bool IsDistinct<TItem>([NotNull] this IEnumerable<TItem> extends, [NotNull] IEqualityComparer<TItem> comparer)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
|
||||
TItem other = default(TItem);
|
||||
bool first = true;
|
||||
|
||||
foreach (TItem item in extends)
|
||||
{
|
||||
if (first)
|
||||
{
|
||||
other = item;
|
||||
first = false;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!comparer.Equals(item, other))
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets distinct elements from the sequence based on given property.
|
||||
/// </summary>
|
||||
|
||||
Reference in New Issue
Block a user