< Summary

Information
Class: SwiftCollections.SwiftStringEqualityComparer
Assembly: SwiftCollections
File(s): /home/runner/work/SwiftCollections/SwiftCollections/src/SwiftCollections/EqualityComparer/SwiftStringEqualityComparer.cs
Line coverage
100%
Covered lines: 14
Uncovered lines: 0
Coverable lines: 14
Total lines: 83
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()
 22    {
 523        _entropy = SwiftHashTools.GetEntropy();
 524    }
 25
 26    /// <inheritdoc/>
 27    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 22428    public bool Equals(string? x, string? y) => x == y || (x != null && y != null && x.Equals(y));
 29
 30    /// <summary>
 31    /// Determines whether the specified objects are equal.
 32    /// </summary>
 33    /// <param name="x">The first object to compare.</param>
 34    /// <param name="y">The second object to compare.</param>
 35    /// <returns><c>true</c> if the specified objects are equal; otherwise, <c>false</c>.</returns>
 36    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 37    public new bool Equals(object? x, object? y)
 38    {
 39        // Use a single null check for both objects to minimize branching
 540        return x == y || (x != null && y != null && (x is string a && y is string b ? a.Equals(b) : x.Equals(y)));
 41    }
 42
 43    /// <summary>
 44    /// Determines whether the specified object is equal to the current comparer.
 45    /// </summary>
 46    /// <param name="obj">The object to compare with the current comparer.</param>
 47    /// <returns><c>true</c> if the specified object is equal to the current comparer; otherwise, <c>false</c>.</returns
 48    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 249    public override bool Equals(object? obj) => obj is SwiftStringEqualityComparer other && _entropy == other._entropy;
 50
 51    /// <inheritdoc/>
 52    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 53    public int GetHashCode(string obj)
 54    {
 44355        if (obj == null) return 0;
 44156        return SwiftHashTools.MurmurHash3(obj, (int)(_entropy & 0x7FFFFFFF));
 57    }
 58
 59    /// <summary>
 60    /// Returns a hash code for the specified object, incorporating entropy for better distribution.
 61    /// </summary>
 62    /// <param name="obj">The object for which to get a hash code.</param>
 63    /// <returns>A hash code for the specified object.</returns>
 64    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 65    public int GetHashCode(object obj)
 66    {
 567        if (obj == null) return 0;
 468        if (obj is string text) return SwiftHashTools.MurmurHash3(text, (int)(_entropy & 0x7FFFFFFF));
 269        return obj.GetHashCode() ^ (int)(_entropy & 0x7FFFFFFF);
 70    }
 71
 72    /// <summary>
 73    /// Returns a hash code for the current comparer.
 74    /// </summary>
 75    /// <returns>A hash code for the current comparer.</returns>
 76    [MethodImpl(MethodImplOptions.NoInlining)]
 277    public override int GetHashCode() => GetType().Name.GetHashCode() ^ (int)(_entropy & 0x7FFFFFFF);
 78
 79    public void GetObjectData(SerializationInfo info, StreamingContext context)
 80    {
 181        info.AddValue("Entropy", _entropy);
 182    }
 83}