< Summary

Information
Class: SwiftCollections.SwiftObjectEqualityComparer
Assembly: SwiftCollections
File(s): /home/runner/work/SwiftCollections/SwiftCollections/src/SwiftCollections/EqualityComparer/SwiftObjectEqualityComparer.cs
Line coverage
100%
Covered lines: 17
Uncovered lines: 0
Coverable lines: 17
Total lines: 71
Line coverage: 100%
Branch coverage
100%
Covered branches: 12
Total branches: 12
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor()100%11100%
Equals(...)100%66100%
Equals(...)100%22100%
GetHashCode(...)100%44100%
GetHashCode()100%11100%
GetObjectData(...)100%11100%

File(s)

/home/runner/work/SwiftCollections/SwiftCollections/src/SwiftCollections/EqualityComparer/SwiftObjectEqualityComparer.cs

#LineLine coverage
 1using System.Collections;
 2using System.Collections.Generic;
 3using System.Runtime.CompilerServices;
 4using System.Runtime.Serialization;
 5
 6namespace SwiftCollections;
 7
 8/// <summary>
 9/// Provides a randomized equality comparer for objects, enhancing hash code distribution to reduce collisions in hash-b
 10/// </summary>
 11internal sealed class SwiftObjectEqualityComparer : IEqualityComparer<object>, IEqualityComparer, ISerializable, IRandom
 12{
 13    /// <summary>
 14    /// A 64-bit entropy value used to randomize hash codes for better distribution.
 15    /// </summary>
 16    private readonly long _entropy;
 17
 18    /// <summary>
 19    /// Initializes a new instance of the <see cref="SwiftStringEqualityComparer"/> class with a unique entropy value.
 20    /// </summary>
 421    public SwiftObjectEqualityComparer()
 422    {
 423        _entropy = SwiftHashTools.GetEntropy();
 424    }
 25
 26    /// <summary>
 27    /// Determines whether the specified objects are equal.
 28    /// </summary>
 29    /// <param name="x">The first object to compare.</param>
 30    /// <param name="y">The second object to compare.</param>
 31    /// <returns><c>true</c> if the specified objects are equal; otherwise, <c>false</c>.</returns>
 32    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 33    public new bool Equals(object x, object y)
 534    {
 35        // Use a single null check for both objects to minimize branching
 536        return x == y || (x != null && y != null && x.Equals(y));
 537    }
 38
 39    /// <summary>
 40    /// Determines whether the specified object is equal to the current comparer.
 41    /// </summary>
 42    /// <param name="obj">The object to compare with the current comparer.</param>
 43    /// <returns><c>true</c> if the specified object is equal to the current comparer; otherwise, <c>false</c>.</returns
 44    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 245    public override bool Equals(object obj) => obj is SwiftObjectEqualityComparer other && _entropy == other._entropy;
 46
 47    /// <summary>
 48    /// Returns a hash code for the specified object, incorporating entropy for better distribution.
 49    /// </summary>
 50    /// <param name="obj">The object for which to get a hash code.</param>
 51    /// <returns>A hash code for the specified object.</returns>
 52    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 53    public int GetHashCode(object obj)
 554    {
 655        if (obj == null) return 0;
 656        if (obj is string text) return SwiftHashTools.MurmurHash3(text, (int)(_entropy & 0x7FFFFFFF));
 257        return obj.GetHashCode() ^ (int)(_entropy & 0x7FFFFFFF);
 558    }
 59
 60    /// <summary>
 61    /// Returns a hash code for the current comparer.
 62    /// </summary>
 63    /// <returns>A hash code for the current comparer.</returns>
 64    [MethodImpl(MethodImplOptions.NoInlining)]
 265    public override int GetHashCode() => GetType().Name.GetHashCode() ^ (int)(_entropy & 0x7FFFFFFF);
 66
 67    public void GetObjectData(SerializationInfo info, StreamingContext context)
 168    {
 169        info.AddValue("Entropy", _entropy);
 170    }
 71}