< Summary

Information
Class: SwiftCollections.SwiftObjectEqualityComparer
Assembly: SwiftCollections
File(s): /home/runner/work/SwiftCollections/SwiftCollections/src/SwiftCollections/EqualityComparer/SwiftObjectEqualityComparer.cs
Line coverage
100%
Covered lines: 11
Uncovered lines: 0
Coverable lines: 11
Total lines: 79
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
 1//=======================================================================
 2// SwiftObjectEqualityComparer.cs
 3//=======================================================================
 4// MIT License, Copyright (c) 2024–present David Oravsky (mrdav30)
 5// See LICENSE file in the project root for full license information.
 6//=======================================================================
 7
 8using SwiftCollections.Utility;
 9using System.Collections;
 10using System.Collections.Generic;
 11using System.Runtime.CompilerServices;
 12using System.Runtime.Serialization;
 13
 14namespace SwiftCollections;
 15
 16/// <summary>
 17/// Provides a randomized equality comparer for objects, enhancing hash code distribution to reduce collisions in hash-b
 18/// </summary>
 19internal sealed class SwiftObjectEqualityComparer : IEqualityComparer<object>, IEqualityComparer, ISerializable, IRandom
 20{
 21    /// <summary>
 22    /// A 64-bit entropy value used to randomize hash codes for better distribution.
 23    /// </summary>
 24    private readonly long _entropy;
 25
 26    /// <summary>
 27    /// Initializes a new instance of the <see cref="SwiftStringEqualityComparer"/> class with a unique entropy value.
 28    /// </summary>
 429    public SwiftObjectEqualityComparer()
 30    {
 431        _entropy = SwiftHashTools.GetEntropy();
 432    }
 33
 34    /// <summary>
 35    /// Determines whether the specified objects are equal.
 36    /// </summary>
 37    /// <param name="x">The first object to compare.</param>
 38    /// <param name="y">The second object to compare.</param>
 39    /// <returns><c>true</c> if the specified objects are equal; otherwise, <c>false</c>.</returns>
 40    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 41    public new bool Equals(object? x, object? y)
 42    {
 43        // Use a single null check for both objects to minimize branching
 544        return x == y || (x != null && y != null && x.Equals(y));
 45    }
 46
 47    /// <summary>
 48    /// Determines whether the specified object is equal to the current comparer.
 49    /// </summary>
 50    /// <param name="obj">The object to compare with the current comparer.</param>
 51    /// <returns><c>true</c> if the specified object is equal to the current comparer; otherwise, <c>false</c>.</returns
 52    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 253    public override bool Equals(object? obj) => obj is SwiftObjectEqualityComparer other && _entropy == other._entropy;
 54
 55    /// <summary>
 56    /// Returns a hash code for the specified object, incorporating entropy for better distribution.
 57    /// </summary>
 58    /// <param name="obj">The object for which to get a hash code.</param>
 59    /// <returns>A hash code for the specified object.</returns>
 60    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 61    public int GetHashCode(object obj)
 62    {
 663        if (obj == null) return 0;
 664        if (obj is string text) return SwiftHashTools.MurmurHash3(text, (int)(_entropy & 0x7FFFFFFF));
 265        return obj.GetHashCode() ^ (int)(_entropy & 0x7FFFFFFF);
 66    }
 67
 68    /// <summary>
 69    /// Returns a hash code for the current comparer.
 70    /// </summary>
 71    /// <returns>A hash code for the current comparer.</returns>
 72    [MethodImpl(MethodImplOptions.NoInlining)]
 273    public override int GetHashCode() => GetType().Name.GetHashCode() ^ (int)(_entropy & 0x7FFFFFFF);
 74
 75    public void GetObjectData(SerializationInfo info, StreamingContext context)
 76    {
 177        info.AddValue("Entropy", _entropy);
 178    }
 79}