Ejemplo n.º 1
0
        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);
        }
Ejemplo n.º 2
0
        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);
        }
Ejemplo n.º 3
0
        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);
        }
Ejemplo n.º 4
0
 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));
Ejemplo n.º 5
0
        public virtual int GetHashCode(TCollection obj, EqualityComparerContext context)
        {
            if (obj == null)
            {
                return(0);
            }

            return(GetHashCodeCore(obj, context));
        }
Ejemplo n.º 6
0
        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);
            }
        }
Ejemplo n.º 7
0
        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));
        }
Ejemplo n.º 8
0
        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);
        }
Ejemplo n.º 9
0
        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();
                }
            }
        }
Ejemplo n.º 10
0
        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;
 }
Ejemplo n.º 12
0
 protected abstract int GetHashCodeCore(TCollection obj, EqualityComparerContext context);
Ejemplo n.º 13
0
 protected abstract bool EqualsCore(TCollection x, TCollection y, EqualityComparerContext context);
Ejemplo n.º 14
0
 protected virtual bool AreEquals(EqualityComparerContext context, T x, T y) =>
 (!context.TryAdd(x) | !context.TryAdd(y)) || ElementEqualityComparer.Equals(x, y);