< Summary

Information
Class: SwiftCollections.SwiftStringEqualityComparer
Assembly: SwiftCollections
File(s): /home/runner/work/SwiftCollections/SwiftCollections/src/SwiftCollections/EqualityComparer/SwiftStringEqualityComparer.cs
Line coverage
100%
Covered lines: 24
Uncovered lines: 0
Coverable lines: 24
Total lines: 84
Line coverage: 100%
Branch coverage
95%
Covered branches: 23
Total branches: 24
Branch coverage: 95.8%
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(...)90%1010100%
Equals(...)100%22100%
GetHashCode(...)100%22100%
GetHashCode(...)100%44100%
GetHashCode()100%11100%
GetObjectData(...)100%11100%

File(s)

/home/runner/work/SwiftCollections/SwiftCollections/src/SwiftCollections/EqualityComparer/SwiftStringEqualityComparer.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 SwiftStringEqualityComparer : IEqualityComparer<string>, 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>
 521    public SwiftStringEqualityComparer()
 522    {
 523        _entropy = SwiftHashTools.GetEntropy();
 524    }
 25
 26    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 27    public bool Equals(string x, string y)
 22428    {
 22429        return x == y || (x != null && y != null && x.Equals(y));
 22430    }
 31
 32    /// <summary>
 33    /// Determines whether the specified objects are equal.
 34    /// </summary>
 35    /// <param name="x">The first object to compare.</param>
 36    /// <param name="y">The second object to compare.</param>
 37    /// <returns><c>true</c> if the specified objects are equal; otherwise, <c>false</c>.</returns>
 38    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 39    public new bool Equals(object x, object y)
 540    {
 41        // Use a single null check for both objects to minimize branching
 542        return x == y || (x != null && y != null && (x is string a && y is string b ? a.Equals(b) : x.Equals(y)));
 543    }
 44
 45    /// <summary>
 46    /// Determines whether the specified object is equal to the current comparer.
 47    /// </summary>
 48    /// <param name="obj">The object to compare with the current comparer.</param>
 49    /// <returns><c>true</c> if the specified object is equal to the current comparer; otherwise, <c>false</c>.</returns
 50    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 251    public override bool Equals(object obj) => obj is SwiftStringEqualityComparer other && _entropy == other._entropy;
 52
 53    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 54    public int GetHashCode(string obj)
 44255    {
 44356        if (obj == null) return 0;
 44157        return SwiftHashTools.MurmurHash3(obj, (int)(_entropy & 0x7FFFFFFF));
 44258    }
 59
 60    /// <summary>
 61    /// Returns a hash code for the specified object, incorporating entropy for better distribution.
 62    /// </summary>
 63    /// <param name="obj">The object for which to get a hash code.</param>
 64    /// <returns>A hash code for the specified object.</returns>
 65    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 66    public int GetHashCode(object obj)
 467    {
 568        if (obj == null) return 0;
 469        if (obj is string text) return SwiftHashTools.MurmurHash3(text, (int)(_entropy & 0x7FFFFFFF));
 270        return obj.GetHashCode() ^ (int)(_entropy & 0x7FFFFFFF);
 471    }
 72
 73    /// <summary>
 74    /// Returns a hash code for the current comparer.
 75    /// </summary>
 76    /// <returns>A hash code for the current comparer.</returns>
 77    [MethodImpl(MethodImplOptions.NoInlining)]
 278    public override int GetHashCode() => GetType().Name.GetHashCode() ^ (int)(_entropy & 0x7FFFFFFF);
 79
 80    public void GetObjectData(SerializationInfo info, StreamingContext context)
 181    {
 182        info.AddValue("Entropy", _entropy);
 183    }
 84}