< Summary

Information
Class: SwiftCollections.SwiftDeterministicStringEqualityComparer
Assembly: SwiftCollections
File(s): /home/runner/work/SwiftCollections/SwiftCollections/src/SwiftCollections/EqualityComparer/SwiftDeterministicStringEqualityComparer.cs
Line coverage
100%
Covered lines: 23
Uncovered lines: 0
Coverable lines: 23
Total lines: 56
Line coverage: 100%
Branch coverage
91%
Covered branches: 22
Total branches: 24
Branch coverage: 91.6%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

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

File(s)

/home/runner/work/SwiftCollections/SwiftCollections/src/SwiftCollections/EqualityComparer/SwiftDeterministicStringEqualityComparer.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 deterministic string comparer used by SwiftCollections when no comparer is supplied.
 10/// </summary>
 11internal sealed class SwiftDeterministicStringEqualityComparer : IEqualityComparer<string>, IEqualityComparer, ISerializ
 12{
 13    private readonly int _seed;
 14
 615    public SwiftDeterministicStringEqualityComparer() : this(SwiftHashTools.DefaultDeterministicStringHashSeed) { }
 16
 717    internal SwiftDeterministicStringEqualityComparer(int seed)
 718    {
 719        _seed = seed;
 720    }
 21
 22    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 5408523    public bool Equals(string x, string y) => x == y || (x != null && y != null && x.Equals(y));
 24
 25    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 26    public new bool Equals(object x, object y)
 527    {
 528        return x == y || (x != null && y != null && (x is string a && y is string b ? a.Equals(b) : x.Equals(y)));
 529    }
 30
 31    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 332    public override bool Equals(object obj) => obj is SwiftDeterministicStringEqualityComparer other && _seed == other._
 33
 34    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 35    public int GetHashCode(string obj)
 30485436    {
 30485537        if (obj == null) return 0;
 30485338        return SwiftHashTools.MurmurHash3(obj, _seed);
 30485439    }
 40
 41    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 42    public int GetHashCode(object obj)
 343    {
 444        if (obj == null) return 0;
 245        if (obj is string text) return SwiftHashTools.MurmurHash3(text, _seed);
 246        return obj.GetHashCode() ^ _seed;
 347    }
 48
 49    [MethodImpl(MethodImplOptions.NoInlining)]
 250    public override int GetHashCode() => GetType().Name.GetHashCode() ^ _seed;
 51
 52    public void GetObjectData(SerializationInfo info, StreamingContext context)
 153    {
 154        info.AddValue("Seed", _seed);
 155    }
 56}