You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Zero/ZeroLevel/Services/Extensions/CollectionComparsionExtensi...

102 lines
4.0 KiB

6 years ago
using System;
using System.Collections.Generic;
using System.Linq;
namespace ZeroLevel
{
public static partial class CollectionComparsionExtensions
{
private sealed class SimpleComparer<T> : IEqualityComparer<T>
{
private readonly Func<T, T, bool> _comparer;
public SimpleComparer() { _comparer = (a, b) => object.Equals(a, b); }
public SimpleComparer(Func<T, T, bool> comparer) { _comparer = comparer; }
public bool Equals(T x, T y)
{
return _comparer(x, y);
}
public int GetHashCode(T obj)
{
return obj.GetHashCode();
}
}
/// <summary>
/// Проверяет на одинаковое содержание коллекции строк, в том числе по разному отсортированные
/// </summary>
public static bool StringEnumerableEquals(this IEnumerable<string> A, IEnumerable<string> B)
{
if (A == null && B == null) return true;
if (A == null || B == null) return false;
return A.Count() == B.Count() && A.Intersect(B).Count() == B.Count();
}
/// <summary>
/// Проверяет на одинаковое содержание коллекций, в том числе по разному отсортированных
/// </summary>
public static bool NoOrderingEquals<T>(this IEnumerable<T> A, IEnumerable<T> B)
{
if (A == null && B == null) return true;
if (A == null || B == null) return false;
return A.Count() == B.Count() && A.Intersect(B, new SimpleComparer<T>()).Count() == B.Count();
}
public static bool NoOrderingEquals<T>(this IEnumerable<T> A, IEnumerable<T> B, Func<T, T, bool> comparer)
{
if (A == null && B == null) return true;
if (A == null || B == null) return false;
return A.Count() == B.Count() && A.Intersect(B, new SimpleComparer<T>(comparer)).Count() == B.Count();
}
/// <summary>
/// Проверяет на одинаковое содержание коллекций
/// </summary>
public static bool OrderingEquals<T>(this IEnumerable<T> A, IEnumerable<T> B)
{
if (A == null && B == null) return true;
if (A == null || B == null) return false;
if (A.Count() != B.Count()) return false;
var enumA = A.GetEnumerator();
var enumB = B.GetEnumerator();
while (enumA.MoveNext() && enumB.MoveNext())
{
if (enumA.Current == null && enumB.Current == null) continue;
if (enumA.Current == null || enumB.Current == null) return false;
if (enumA.Current.Equals(enumB.Current) == false) return false;
}
return true;
}
public static bool OrderingEquals<T>(this IEnumerable<T> A, IEnumerable<T> B, Func<T, T, bool> comparer)
{
if (A == null && B == null) return true;
if (A == null || B == null) return false;
if (A.Count() != B.Count()) return false;
var enumA = A.GetEnumerator();
var enumB = B.GetEnumerator();
while (enumA.MoveNext() && enumB.MoveNext())
{
if (enumA.Current == null && enumB.Current == null) continue;
if (enumA.Current == null || enumB.Current == null) return false;
if (comparer(enumA.Current, enumB.Current) == false) return false;
}
return true;
}
/// <summary>
/// Расчет хеша для коллекции
/// </summary>
public static int GetEnumHashCode<T>(this IEnumerable<T> A)
{
int hc = 0;
if (A != null)
{
foreach (var p in A)
hc ^= p.GetHashCode();
}
return hc;
}
}
}

Powered by TurnKey Linux.