protected override int GetHashCodeCore(T[] obj, EqualityComparerContext context) { if (obj.Length == 0) { return(EmptyCollectionHashCode); } int hashCode = HashHelper.HashSeed; unchecked { int i; for (i = 0; i < obj.Length - obj.Length % 4; i += 4) { hashCode = CalcCombinedHashCode(context, obj[i + 0], hashCode); hashCode = CalcCombinedHashCode(context, obj[i + 1], hashCode); hashCode = CalcCombinedHashCode(context, obj[i + 2], hashCode); hashCode = CalcCombinedHashCode(context, obj[i + 3], hashCode); } for (i = 0; i < obj.Length; i++) { hashCode = CalcCombinedHashCode(context, obj[i], hashCode); } } return(hashCode); }
protected override bool EqualsCore(T[] x, T[] y, EqualityComparerContext context) { if (x.Length != y.Length) { return(false); } int i = 0; for (; i < x.Length - x.Length % 4; i += 4) { if (!AreEquals(context, x[i + 0], y[i + 0]) || !AreEquals(context, x[i + 1], y[i + 1]) || !AreEquals(context, x[i + 2], y[i + 2]) || !AreEquals(context, x[i + 3], y[i + 3])) { return(false); } } for (; i < x.Length; i++) { if (!AreEquals(context, x[i], y[i])) { return(false); } } return(true); }
protected override bool EqualsCore(List <T> x, List <T> y, EqualityComparerContext context) { if (x.Count != y.Count) { return(false); } int i = 0; for (; i < x.Count - x.Count % 4; i += 4) { if (!AreEquals(context, x[i + 0], y[i + 0]) || !AreEquals(context, x[i + 1], y[i + 1]) || !AreEquals(context, x[i + 2], y[i + 2]) || !AreEquals(context, x[i + 3], y[i + 3])) { return(false); } } for (; i < x.Count; i++) { if (!AreEquals(context, x[i], y[i])) { return(false); } } return(true); }
protected virtual int CalcCombinedHashCode(EqualityComparerContext context, T element, int hashCode) => HashHelper.CombineHashCodes( hashCode * 397, element != null ? context.TryAdd(element) ? ElementEqualityComparer.GetHashCode(element) : ~hashCode : ~(hashCode ^ HashHelper.HashSeed));
public virtual int GetHashCode(TCollection obj, EqualityComparerContext context) { if (obj == null) { return(0); } return(GetHashCodeCore(obj, context)); }
protected override int GetHashCodeCore(IEnumerable <T> obj, EqualityComparerContext context) { unchecked { int hashCode = HashHelper.HashSeed; foreach (var element in obj) { hashCode = CalcCombinedHashCode(context, element, hashCode); } return(hashCode); } }
public virtual bool Equals(TCollection x, TCollection y, EqualityComparerContext context) { if (x == null) { return(y == null); } if (ReferenceEquals(x, y)) { return(true); } if (x.GetType() != y.GetType()) { return(false); } return(EqualsCore(x, y, context)); }
protected override bool EqualsCore(IEnumerable <T> x, IEnumerable <T> y, EqualityComparerContext context) { if (x is ICollection <T> xCollection && y is ICollection <T> yCollection && xCollection.Count != yCollection.Count) { return(false); } if (x is IReadOnlyCollection <T> xReadOnlyCollection && y is IReadOnlyCollection <T> yReadOnlyCollection && xReadOnlyCollection.Count != yReadOnlyCollection.Count) { return(false); } using (var xEnumerator = x.GetEnumerator()) using (var yEnumerator = y.GetEnumerator()) { while (true) { var canMoveX = xEnumerator.MoveNext(); var canMoveY = yEnumerator.MoveNext(); if (canMoveX != canMoveY) { return(false); } if (!canMoveX) { break; } var xValue = xEnumerator.Current; var yValue = yEnumerator.Current; if (!AreEquals(context, xValue, yValue)) { return(false); } } } return(true); }
protected override bool EqualsCore(TCollection x, TCollection y) { EqualityComparerContext context = EqualityComparerContext.Current; bool isAcquired = false; if (!context.IsAcquired) { context.IsAcquired = isAcquired = true; context.Free(); } try { return(Equals(x, y, context)); } finally { if (isAcquired) { context.IsAcquired = false; context.Free(); } } }
protected override int GetHashCodeCore(TCollection obj) { EqualityComparerContext context = EqualityComparerContext.Current; bool isAcquired = false; if (!context.IsAcquired) { context.IsAcquired = isAcquired = true; context.Free(); } try { return(GetHashCode(obj, context)); } finally { if (isAcquired) { context.IsAcquired = false; context.Free(); } } }
public EqualityComparerSteps(EqualityComparerContext context) { _context = context; }
protected abstract int GetHashCodeCore(TCollection obj, EqualityComparerContext context);
protected abstract bool EqualsCore(TCollection x, TCollection y, EqualityComparerContext context);
protected virtual bool AreEquals(EqualityComparerContext context, T x, T y) => (!context.TryAdd(x) | !context.TryAdd(y)) || ElementEqualityComparer.Equals(x, y);