< 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: 91
Line coverage: 100%
Branch coverage
100%
Covered branches: 24
Total branches: 24
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%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
 1//=======================================================================
 2// SwiftStringEqualityComparer.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 SwiftStringEqualityComparer : IEqualityComparer<string>, 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>
 529    public SwiftStringEqualityComparer()
 30    {
 531        _entropy = SwiftHashTools.GetEntropy();
 532    }
 33
 34    /// <inheritdoc/>
 35    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 22436    public bool Equals(string? x, string? y) => x == y || (x != null && y != null && x.Equals(y));
 37
 38    /// <summary>
 39    /// Determines whether the specified objects are equal.
 40    /// </summary>
 41    /// <param name="x">The first object to compare.</param>
 42    /// <param name="y">The second object to compare.</param>
 43    /// <returns><c>true</c> if the specified objects are equal; otherwise, <c>false</c>.</returns>
 44    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 45    public new bool Equals(object? x, object? y)
 46    {
 47        // Use a single null check for both objects to minimize branching
 848        return x == y || (x != null && y != null && (x is string a && y is string b ? a.Equals(b) : x.Equals(y)));
 49    }
 50
 51    /// <summary>
 52    /// Determines whether the specified object is equal to the current comparer.
 53    /// </summary>
 54    /// <param name="obj">The object to compare with the current comparer.</param>
 55    /// <returns><c>true</c> if the specified object is equal to the current comparer; otherwise, <c>false</c>.</returns
 56    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 257    public override bool Equals(object? obj) => obj is SwiftStringEqualityComparer other && _entropy == other._entropy;
 58
 59    /// <inheritdoc/>
 60    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 61    public int GetHashCode(string obj)
 62    {
 44363        if (obj == null) return 0;
 44164        return SwiftHashTools.MurmurHash3(obj, (int)(_entropy & 0x7FFFFFFF));
 65    }
 66
 67    /// <summary>
 68    /// Returns a hash code for the specified object, incorporating entropy for better distribution.
 69    /// </summary>
 70    /// <param name="obj">The object for which to get a hash code.</param>
 71    /// <returns>A hash code for the specified object.</returns>
 72    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 73    public int GetHashCode(object obj)
 74    {
 575        if (obj == null) return 0;
 476        if (obj is string text) return SwiftHashTools.MurmurHash3(text, (int)(_entropy & 0x7FFFFFFF));
 277        return obj.GetHashCode() ^ (int)(_entropy & 0x7FFFFFFF);
 78    }
 79
 80    /// <summary>
 81    /// Returns a hash code for the current comparer.
 82    /// </summary>
 83    /// <returns>A hash code for the current comparer.</returns>
 84    [MethodImpl(MethodImplOptions.NoInlining)]
 285    public override int GetHashCode() => GetType().Name.GetHashCode() ^ (int)(_entropy & 0x7FFFFFFF);
 86
 87    public void GetObjectData(SerializationInfo info, StreamingContext context)
 88    {
 189        info.AddValue("Entropy", _entropy);
 190    }
 91}