| | | 1 | | //======================================================================= |
| | | 2 | | // SwiftDeterministicStringEqualityComparer.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 | | |
| | | 8 | | using SwiftCollections.Utility; |
| | | 9 | | using System.Collections; |
| | | 10 | | using System.Collections.Generic; |
| | | 11 | | using System.Runtime.CompilerServices; |
| | | 12 | | using System.Runtime.Serialization; |
| | | 13 | | |
| | | 14 | | namespace SwiftCollections; |
| | | 15 | | |
| | | 16 | | /// <summary> |
| | | 17 | | /// Provides a deterministic string comparer used by SwiftCollections when no comparer is supplied. |
| | | 18 | | /// </summary> |
| | | 19 | | internal sealed class SwiftDeterministicStringEqualityComparer : IEqualityComparer<string>, IEqualityComparer, ISerializ |
| | | 20 | | { |
| | | 21 | | private readonly int _seed; |
| | | 22 | | |
| | 4 | 23 | | public SwiftDeterministicStringEqualityComparer() : this(SwiftHashTools.DefaultDeterministicStringHashSeed) { } |
| | | 24 | | |
| | 7 | 25 | | internal SwiftDeterministicStringEqualityComparer(int seed) |
| | | 26 | | { |
| | 7 | 27 | | _seed = seed; |
| | 7 | 28 | | } |
| | | 29 | | |
| | | 30 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 54086 | 31 | | public bool Equals(string? x, string? y) => x == y || (x != null && y != null && x.Equals(y)); |
| | | 32 | | |
| | | 33 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 34 | | public new bool Equals(object? x, object? y) |
| | | 35 | | { |
| | 8 | 36 | | return x == y || (x != null && y != null && (x is string a && y is string b ? a.Equals(b) : x.Equals(y))); |
| | | 37 | | } |
| | | 38 | | |
| | | 39 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 3 | 40 | | public override bool Equals(object? obj) => obj is SwiftDeterministicStringEqualityComparer other && _seed == other. |
| | | 41 | | |
| | | 42 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 43 | | public int GetHashCode(string obj) |
| | | 44 | | { |
| | 211990 | 45 | | if (obj == null) return 0; |
| | 211988 | 46 | | return SwiftHashTools.MurmurHash3(obj, _seed); |
| | | 47 | | } |
| | | 48 | | |
| | | 49 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 50 | | public int GetHashCode(object obj) |
| | | 51 | | { |
| | 5 | 52 | | if (obj == null) return 0; |
| | 4 | 53 | | if (obj is string text) return SwiftHashTools.MurmurHash3(text, _seed); |
| | 2 | 54 | | return obj.GetHashCode() ^ _seed; |
| | | 55 | | } |
| | | 56 | | |
| | | 57 | | [MethodImpl(MethodImplOptions.NoInlining)] |
| | 2 | 58 | | public override int GetHashCode() => GetType().Name.GetHashCode() ^ _seed; |
| | | 59 | | |
| | | 60 | | public void GetObjectData(SerializationInfo info, StreamingContext context) |
| | | 61 | | { |
| | 1 | 62 | | info.AddValue("Seed", _seed); |
| | 1 | 63 | | } |
| | | 64 | | } |